Fix documentation of check_receiver_arg().
[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         /** Null-terminated array of patterns. */
149         struct osl_object patterns;
150         /** Data pointer passed to the action function. */
151         void *data;
152         /** Gets increased by one for each match. */
153         unsigned num_matches;
154         /** For each matching row, this function will be called. */
155         int (*action)(struct osl_table *table, struct osl_row *row, const char *name, void *data);
156 };
157
158 /** Arguments passed to each afs callback. */
159 struct afs_callback_arg {
160         /** The local socket connecting afs and the command handler. */
161         int fd;
162         /** Callback-specific data. */
163         struct osl_object query;
164         /** Will be written on band SBD_OUTPUT, fully buffered. */
165         struct para_buffer pbout;
166 };
167
168 /**
169  * Afs command handlers run as a process which is not related to the afs
170  * process, i.e. they can not change the address space of afs directly.
171  * Therefore afs commands typically consist of two functions: The command
172  * handler and the corresponding callback function that runs in afs context.
173  *
174  * \sa send_callback_request().
175  */
176 typedef int afs_callback(struct afs_callback_arg *aca);
177
178 /**
179  * Callbacks send chunks to data back to the command handler. Pointers to
180  * this type of function are used by \ref send_callback_request and friends
181  * to deal with the data in the command handler process.
182  *
183  * \sa \ref send_callback_request().
184  */
185 typedef int callback_result_handler(struct osl_object *result, uint8_t band, void *private);
186 int afs_cb_result_handler(struct osl_object *result, uint8_t band, void *private);
187 int pass_buffer_as_shm(int fd, uint8_t band, const char *buf, size_t size);
188
189 /** Structure passed to the AFS max_size handler. */
190 struct afs_max_size_handler_data {
191         /** Local socket connecting the command handler and the AFS process. */
192         int fd;
193         /** The sideband designator for this data packet. */
194         uint8_t band;
195 };
196
197 /**
198  * Standard max_size handler for AFS commands.
199  *
200  * \param buf Contains (part of) the AFS command output.
201  * \param size The number of bytes in \a buf.
202  * \param private Pointer to a \ref afs_max_size_handler_data structure.
203  *
204  * Whenever the output of an AFS command exceeds the maximal size of a shared
205  * memory area, the max size handler of the para_buffer which holds the command
206  * output is called with \a private being a pointer to a structure of type
207  * afs_max_size_handler_data.
208  *
209  * \return The return value of the underlying call to \ref
210  *  pass_buffer_as_shm().
211  */
212 _static_inline_ int afs_max_size_handler(char *buf, size_t size, void *private)
213 {
214         struct afs_max_size_handler_data *amshd = private;
215         return pass_buffer_as_shm(amshd->fd, amshd->band, buf, size);
216 }
217
218 __noreturn void afs_init(uint32_t cookie, int socket_fd);
219 __must_check int afs_event(enum afs_events event, struct para_buffer *pb,
220         void *data);
221 int send_callback_request(afs_callback *f, struct osl_object *query,
222                 callback_result_handler *result_handler,
223                 void *private_result_data);
224 int send_option_arg_callback_request(struct osl_object *options,
225                 int argc,  char * const * const argv, afs_callback *f,
226                 callback_result_handler *result_handler,
227                 void *private_result_data);
228 int send_standard_callback_request(int argc,  char * const * const argv,
229                 afs_callback *f, callback_result_handler *result_handler,
230                 void *private_result_data);
231 int string_compare(const struct osl_object *obj1, const struct osl_object *obj2);
232 int for_each_matching_row(struct pattern_match_data *pmd);
233
234 /* score */
235 void score_init(struct afs_table *t);
236 int admissible_file_loop(void *data, osl_rbtree_loop_func *func);
237 int score_get_best(struct osl_row **aft_row, long *score);
238 int get_score_and_aft_row(struct osl_row *score_row, long *score, struct osl_row **aft_row);
239 int score_add(const struct osl_row *row, long score);
240 int score_update(const struct osl_row *aft_row, long new_score);
241 int get_num_admissible_files(unsigned *num);
242 int score_delete(const struct osl_row *aft_row);
243 int clear_score_table(void);
244 int row_belongs_to_score_table(const struct osl_row *aft_row, unsigned *rank);
245
246 /* attribute */
247 void attribute_init(struct afs_table *t);
248 void get_attribute_bitmap(const uint64_t *atts, char *buf); /* needed by com_ls() */
249 int get_attribute_bitnum_by_name(const char *att_name, unsigned char *bitnum);
250 int get_attribute_text(uint64_t *atts, const char *delim, char **text);
251 int attribute_check_callback(struct afs_callback_arg *aca);
252
253 /* aft */
254 void aft_init(struct afs_table *t);
255 int aft_get_row_of_path(const char *path, struct osl_row **row);
256 int aft_check_attributes(uint64_t att_mask, struct para_buffer *pb);
257 int open_and_update_audio_file(struct audio_file_data *afd);
258 int load_afd(int shmid, struct audio_file_data *afd);
259 int get_afsi_of_row(const struct osl_row *row, struct afs_info *afsi);
260 int get_afhi_of_row(const struct osl_row *row, struct afh_info *afhi);
261 int get_audio_file_path_of_row(const struct osl_row *row, char **path);
262 int audio_file_loop(void *private_data, osl_rbtree_loop_func *func);
263 int aft_check_callback(struct afs_callback_arg *aca);
264
265 /* playlist */
266 int playlist_open(char *name);
267 void playlist_close(void);
268 int playlist_check_callback(struct afs_callback_arg *aca);
269
270 /** evaluates to 1 if x < y, to -1 if x > y and to 0 if x == y */
271 #define NUM_COMPARE(x, y) ((int)((x) < (y)) - (int)((x) > (y)))
272
273
274 /** Define exported functions and a table pointer for an osl blob table. */
275 #define DECLARE_BLOB_SYMBOLS(table_name, cmd_prefix) \
276         void table_name ## _init(struct afs_table *t); \
277         int cmd_prefix ## _get_name_by_id(uint32_t id, char **name); \
278         int cmd_prefix ## _get_def_by_id(uint32_t id, struct osl_object *def); \
279         int cmd_prefix ## _get_def_by_name(char *name, struct osl_object *def); \
280         int cmd_prefix ## _get_name_and_def_by_row(const struct osl_row *row, \
281                 char **name, struct osl_object *def); \
282         int table_name ##_event_handler(enum afs_events event, \
283                 struct para_buffer *pb, void *data); \
284         extern struct osl_table *table_name ## _table;
285
286 DECLARE_BLOB_SYMBOLS(lyrics, lyr);
287 DECLARE_BLOB_SYMBOLS(images, img);
288 DECLARE_BLOB_SYMBOLS(moods, mood);
289 DECLARE_BLOB_SYMBOLS(playlists, pl);
290
291 /** The columns of an abstract blob table. */
292 enum blob_table_columns {
293         /** The identifier, a positive integer that never repeats. */
294         BLOBCOL_ID,
295         /** The unique name of the blob. */
296         BLOBCOL_NAME,
297         /** The actual blob contents. */
298         BLOBCOL_DEF,
299         /** A blob table has that many columns. */
300         NUM_BLOB_COLUMNS
301 };