manual: Omit level 3 headers from table of contents.
[paraslash.git] / afs.h
1 /*
2  * Copyright (C) 2007 Andre Noll <maan@tuebingen.mpg.de>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file afs.h Exported symbols of the audio file selector. */
8
9 /** Audio file selector data stored in the audio file table. */
10 struct afs_info {
11         /** Seconds since the epoch. */
12         uint64_t last_played;
13         /** Bit field of set attributes. */
14         uint64_t attributes;
15         /** Counts how many times the file was selected. */
16         uint32_t num_played;
17         /** Image blob associated with this file (foreign key). */
18         uint32_t image_id;
19         /** Lyrics blob associated with this file (foreign key). */
20         uint32_t lyrics_id;
21         /** Mp3, ogg, ... */
22         uint8_t audio_format_id;
23         /** Amplification value. */
24         uint8_t amp;
25 };
26
27 /**
28  * Events caused by changes to an afs table.
29  *
30  * Whenever an afs table changes, an event is generated which causes afs to
31  * call the event handlers of all other tables. For example, if an audio file
32  * is added, the event handler of the mood table checks the new file for
33  * admissibility.
34  */
35 enum afs_events {
36         /** An attribute was added. */
37         ATTRIBUTE_ADD,
38         /** An attribute was renamed. */
39         ATTRIBUTE_RENAME,
40         /** An attribute was removed. */
41         ATTRIBUTE_REMOVE,
42         /** The afs info struct of an audio file changed. */
43         AFSI_CHANGE,
44         /** The afh info struct of an audio file changed. */
45         AFHI_CHANGE,
46         /** An audio file was renamed. */
47         AUDIO_FILE_RENAME,
48         /** An audio file was added. */
49         AUDIO_FILE_ADD,
50         /** An audio file is about to be removed. */
51         AUDIO_FILE_REMOVE,
52         /** A new blob was added. */
53         BLOB_ADD,
54         /** A blob was renamed. */
55         BLOB_RENAME,
56         /** A blob is about to be removed. */
57         BLOB_REMOVE,
58 };
59
60 /**
61  * Used as data for \ref afs_event() for events of type \p ATTRIBUTE_ADD.
62  */
63 struct rmatt_event_data {
64         /** The name of the attribute being added. */
65         const char *name;
66         /** Its bit number. */
67         unsigned char bitnum;
68 };
69
70 /**
71  * Used as data for \ref afs_event() for events of type \p ATTRIBUTE_AFSI_CHANGE.
72  */
73 struct afsi_change_event_data {
74         /** Pointer to the row that has changed. */
75         struct osl_row *aft_row;
76         /** Afs info before the change. */
77         struct afs_info *old_afsi;
78 };
79
80 /** Function pointers for table handling.  */
81 struct afs_table {
82         /** Initializes the other pointers in this struct. */
83         void (*init)(struct afs_table *t);
84         /** The name of this table. */
85         const char *name;
86         /** Gets called on startup and on \p SIGHUP. */
87         int (*open)(const char *base_dir);
88         /** Gets called on shutdown and on \p SIGHUP. */
89         void (*close)(void);
90         /** Called by the \a init afs command. */
91         int (*create)(const char *);
92         /** Handles afs events. */
93         int (*event_handler)(enum afs_events event, struct para_buffer *pb,
94                 void *data);
95 };
96
97 /** How audio files are selected by afs. */
98 enum play_mode {
99         /** Admissible files are determined by a mood definition. */
100         PLAY_MODE_MOOD,
101         /** All listed files are admissible. */
102         PLAY_MODE_PLAYLIST,
103 };
104
105 /**
106  * Codes used for communication between the server and the afs process.
107  *
108  * Before forking the afs child, para_server creates a bidirectional pipe
109  * through which both processes communicate. Usually para_server requests a new
110  * audio file in order to start streaming or when the end of the current audio file
111  * has been reached.  The afs process responds to such a request by sending
112  * back an eight byte buffer. The first four bytes is the uint32_t
113  * representation of the code, usually \p NEXT_AUDIO_FILE if an admissible
114  * audio file was found, successfully opened and verified. The other four bytes
115  * represent the shared memory id of the shared memory area that contains
116  * details about the audio file to be streamed next. The open file descriptor
117  * of that file is also passed from afs to para_server through the same pipe.
118  */
119 enum afs_server_code {
120         /** An audio file was successfully opened. */
121         NEXT_AUDIO_FILE,
122         /** No admissible audio file was found. */
123         NO_ADMISSIBLE_FILES,
124 };
125
126 /** Flags passed to for_each_matching_row(). */
127 enum pattern_match_flags {
128         /** Loop in reverse order. */
129         PM_REVERSE_LOOP = 1,
130         /** If no pattern is given, loop over all rows. */
131         PM_NO_PATTERN_MATCHES_EVERYTHING = 2,
132         /** If the data in match_column is the empty string, skip this row. */
133         PM_SKIP_EMPTY_NAME = 4,
134 };
135
136 /** Structure passed to for_each_matching_row(). */
137 struct pattern_match_data {
138         /** Loop over all rows in this table. */
139         struct osl_table *table;
140         /** Determines the loop order. Must be an rbtree column. */
141         unsigned loop_col_num;
142         /** Data from this column is matched against the given patterns. */
143         unsigned match_col_num;
144         /** \see pattern_match_flags. */
145         unsigned pm_flags;
146         /** This value is passed verbatim to fnmatch(). */
147         int fnmatch_flags;
148         /** Obtained by deserializing the query buffer in the callback. */
149         struct lls_parse_result *lpr;
150         /** Do not try to match the first inputs of lpr */
151         unsigned input_skip;
152         /** Data pointer passed to the action function. */
153         void *data;
154         /** Gets increased by one for each match. */
155         unsigned num_matches;
156         /** For each matching row, this function will be called. */
157         int (*action)(struct osl_table *table, struct osl_row *row, const char *name, void *data);
158 };
159
160 /** Arguments passed to each afs callback. */
161 struct afs_callback_arg {
162         /** The local socket connecting afs and the command handler. */
163         int fd;
164         /** Callback-specific data. */
165         struct osl_object query;
166         /** Will be written on band SBD_OUTPUT, fully buffered. */
167         struct para_buffer pbout;
168         struct lls_parse_result *lpr;
169 };
170
171 /**
172  * Afs command handlers run as a process which is not related to the afs
173  * process, i.e. they can not change the address space of afs directly.
174  * Therefore afs commands typically consist of two functions: The command
175  * handler and the corresponding callback function that runs in afs context.
176  *
177  * \sa send_callback_request().
178  */
179 typedef int afs_callback(struct afs_callback_arg *aca);
180
181 /**
182  * Callbacks send chunks to data back to the command handler. Pointers to
183  * this type of function are used by \ref send_callback_request and friends
184  * to deal with the data in the command handler process.
185  *
186  * \sa \ref send_callback_request().
187  */
188 typedef int callback_result_handler(struct osl_object *result, uint8_t band, void *private);
189 int afs_cb_result_handler(struct osl_object *result, uint8_t band, void *private);
190 int pass_buffer_as_shm(int fd, uint8_t band, const char *buf, size_t size);
191
192 /** Structure passed to the AFS max_size handler. */
193 struct afs_max_size_handler_data {
194         /** Local socket connecting the command handler and the AFS process. */
195         int fd;
196         /** The sideband designator for this data packet. */
197         uint8_t band;
198 };
199
200 /**
201  * Standard max_size handler for AFS commands.
202  *
203  * \param buf Contains (part of) the AFS command output.
204  * \param size The number of bytes in \a buf.
205  * \param private Pointer to a \ref afs_max_size_handler_data structure.
206  *
207  * Whenever the output of an AFS command exceeds the maximal size of a shared
208  * memory area, the max size handler of the para_buffer which holds the command
209  * output is called with \a private being a pointer to a structure of type
210  * afs_max_size_handler_data.
211  *
212  * \return The return value of the underlying call to \ref
213  *  pass_buffer_as_shm().
214  */
215 _static_inline_ int afs_max_size_handler(char *buf, size_t size, void *private)
216 {
217         struct afs_max_size_handler_data *amshd = private;
218         return pass_buffer_as_shm(amshd->fd, amshd->band, buf, size);
219 }
220
221 __noreturn void afs_init(uint32_t cookie, int socket_fd);
222 __must_check int afs_event(enum afs_events event, struct para_buffer *pb,
223         void *data);
224 int send_callback_request(afs_callback *f, struct osl_object *query,
225                 callback_result_handler *result_handler,
226                 void *private_result_data);
227 int send_lls_callback_request(afs_callback *f,
228                 const struct lls_command * const cmd,
229                 struct lls_parse_result *lpr, void *private_result_data);
230 int string_compare(const struct osl_object *obj1, const struct osl_object *obj2);
231 int for_each_matching_row(struct pattern_match_data *pmd);
232
233 /* score */
234 void score_init(struct afs_table *t);
235 int admissible_file_loop(void *data, osl_rbtree_loop_func *func);
236 int score_get_best(struct osl_row **aft_row, long *score);
237 int get_score_and_aft_row(struct osl_row *score_row, long *score, struct osl_row **aft_row);
238 int score_add(const struct osl_row *row, long score);
239 int score_update(const struct osl_row *aft_row, long new_score);
240 int get_num_admissible_files(unsigned *num);
241 int score_delete(const struct osl_row *aft_row);
242 int clear_score_table(void);
243 int row_belongs_to_score_table(const struct osl_row *aft_row, unsigned *rank);
244
245 /* attribute */
246 void attribute_init(struct afs_table *t);
247 void get_attribute_bitmap(const uint64_t *atts, char *buf); /* needed by com_ls() */
248 int get_attribute_bitnum_by_name(const char *att_name, unsigned char *bitnum);
249 int get_attribute_text(uint64_t *atts, const char *delim, char **text);
250 int attribute_check_callback(struct afs_callback_arg *aca);
251
252 /* aft */
253 void aft_init(struct afs_table *t);
254 int aft_get_row_of_path(const char *path, struct osl_row **row);
255 int aft_check_attributes(uint64_t att_mask, struct para_buffer *pb);
256 int open_and_update_audio_file(struct audio_file_data *afd);
257 int load_afd(int shmid, struct audio_file_data *afd);
258 int get_afsi_of_row(const struct osl_row *row, struct afs_info *afsi);
259 int get_afhi_of_row(const struct osl_row *row, struct afh_info *afhi);
260 int get_audio_file_path_of_row(const struct osl_row *row, char **path);
261 int audio_file_loop(void *private_data, osl_rbtree_loop_func *func);
262 int aft_check_callback(struct afs_callback_arg *aca);
263
264 /* playlist */
265 int playlist_open(const char *name);
266 void playlist_close(void);
267 int playlist_check_callback(struct afs_callback_arg *aca);
268
269 /** evaluates to 1 if x < y, to -1 if x > y and to 0 if x == y */
270 #define NUM_COMPARE(x, y) ((int)((x) < (y)) - (int)((x) > (y)))
271
272
273 /** Define exported functions and a table pointer for an osl blob table. */
274 #define DECLARE_BLOB_SYMBOLS(table_name, cmd_prefix) \
275         void table_name ## _init(struct afs_table *t); \
276         int cmd_prefix ## _get_name_by_id(uint32_t id, char **name); \
277         int cmd_prefix ## _get_def_by_id(uint32_t id, struct osl_object *def); \
278         int cmd_prefix ## _get_def_by_name(char *name, struct osl_object *def); \
279         int cmd_prefix ## _get_name_and_def_by_row(const struct osl_row *row, \
280                 char **name, struct osl_object *def); \
281         int table_name ##_event_handler(enum afs_events event, \
282                 struct para_buffer *pb, void *data); \
283         extern struct osl_table *table_name ## _table;
284
285 DECLARE_BLOB_SYMBOLS(lyrics, lyr);
286 DECLARE_BLOB_SYMBOLS(images, img);
287 DECLARE_BLOB_SYMBOLS(moods, mood);
288 DECLARE_BLOB_SYMBOLS(playlists, pl);
289
290 /** The columns of an abstract blob table. */
291 enum blob_table_columns {
292         /** The identifier, a positive integer that never repeats. */
293         BLOBCOL_ID,
294         /** The unique name of the blob. */
295         BLOBCOL_NAME,
296         /** The actual blob contents. */
297         BLOBCOL_DEF,
298         /** A blob table has that many columns. */
299         NUM_BLOB_COLUMNS
300 };