2 * Copyright (C) 2007 Andre Noll <maan@tuebingen.mpg.de>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file afs.h Exported symbols of the audio file selector. */
9 /** Audio file selector data stored in the audio file table. */
11 /** Seconds since the epoch. */
13 /** Bit field of set attributes. */
15 /** Counts how many times the file was selected. */
17 /** Image blob associated with this file (foreign key). */
19 /** Lyrics blob associated with this file (foreign key). */
22 uint8_t audio_format_id;
23 /** Amplification value. */
28 * Events caused by changes to an afs table.
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
36 /** An attribute was added. */
38 /** An attribute was renamed. */
40 /** An attribute was removed. */
42 /** The afs info struct of an audio file changed. */
44 /** The afh info struct of an audio file changed. */
46 /** An audio file was renamed. */
48 /** An audio file was added. */
50 /** An audio file is about to be removed. */
52 /** A new blob was added. */
54 /** A blob was renamed. */
56 /** A blob is about to be removed. */
61 * Used as data for \ref afs_event() for events of type \p ATTRIBUTE_ADD.
63 struct rmatt_event_data {
64 /** The name of the attribute being added. */
66 /** Its bit number. */
71 * Used as data for \ref afs_event() for events of type \p ATTRIBUTE_AFSI_CHANGE.
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;
80 /** Function pointers for table handling. */
82 /** Initializes the other pointers in this struct. */
83 void (*init)(struct afs_table *t);
84 /** The name of this table. */
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. */
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,
97 /** How audio files are selected by afs. */
99 /** Admissible files are determined by a mood definition. */
101 /** All listed files are admissible. */
106 * Codes used for communication between the server and the afs process.
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.
119 enum afs_server_code {
120 /** An audio file was successfully opened. */
122 /** No admissible audio file was found. */
126 /** Flags passed to for_each_matching_row(). */
127 enum pattern_match_flags {
128 /** Loop in reverse order. */
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,
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 \ref pattern_match_flags. */
146 /** This value is passed verbatim to fnmatch(). */
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 */
152 /** Data pointer passed to the action function. */
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);
160 /** Arguments passed to each afs callback. */
161 struct afs_callback_arg {
162 /** The local socket connecting afs and the command handler. */
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;
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.
177 * \sa \ref send_callback_request().
179 typedef int afs_callback(struct afs_callback_arg *aca);
182 * Some AFS callbacks need to send data back to the command handler. Pointers
183 * to this type of function are passed to \ref send_callback_request() and
184 * related functions to dispatch the data in the command handler process.
186 typedef int callback_result_handler(struct osl_object *result, uint8_t band, void *private);
187 int afs_cb_result_handler(struct osl_object *result, uint8_t band, void *private);
188 int pass_buffer_as_shm(int fd, uint8_t band, const char *buf, size_t size);
190 /** Structure passed to the AFS max_size handler. */
191 struct afs_max_size_handler_data {
192 /** Local socket connecting the command handler and the AFS process. */
194 /** The sideband designator for this data packet. */
199 * Standard max_size handler for AFS commands.
201 * \param buf Contains (part of) the AFS command output.
202 * \param size The number of bytes in \a buf.
203 * \param private Pointer to a \ref afs_max_size_handler_data structure.
205 * Whenever the output of an AFS command exceeds the maximal size of a shared
206 * memory area, the max size handler of the para_buffer which holds the command
207 * output is called with \a private being a pointer to a structure of type
208 * afs_max_size_handler_data.
210 * \return The return value of the underlying call to \ref
211 * pass_buffer_as_shm().
213 _static_inline_ int afs_max_size_handler(char *buf, size_t size, void *private)
215 struct afs_max_size_handler_data *amshd = private;
216 return pass_buffer_as_shm(amshd->fd, amshd->band, buf, size);
219 __noreturn void afs_init(uint32_t cookie, int socket_fd);
220 __must_check int afs_event(enum afs_events event, struct para_buffer *pb,
222 int send_callback_request(afs_callback *f, struct osl_object *query,
223 callback_result_handler *result_handler,
224 void *private_result_data);
225 int send_lls_callback_request(afs_callback *f,
226 const struct lls_command * const cmd,
227 struct lls_parse_result *lpr, void *private_result_data);
228 int string_compare(const struct osl_object *obj1, const struct osl_object *obj2);
229 int for_each_matching_row(struct pattern_match_data *pmd);
232 void score_init(struct afs_table *t);
233 int admissible_file_loop(void *data, osl_rbtree_loop_func *func);
234 int score_get_best(struct osl_row **aft_row, long *score);
235 int get_score_and_aft_row(struct osl_row *score_row, long *score, struct osl_row **aft_row);
236 int score_add(const struct osl_row *row, long score);
237 int score_update(const struct osl_row *aft_row, long new_score);
238 int get_num_admissible_files(unsigned *num);
239 int score_delete(const struct osl_row *aft_row);
240 int clear_score_table(void);
241 int row_belongs_to_score_table(const struct osl_row *aft_row, unsigned *rank);
244 void attribute_init(struct afs_table *t);
245 void get_attribute_bitmap(const uint64_t *atts, char *buf); /* needed by com_ls() */
246 int get_attribute_bitnum_by_name(const char *att_name, unsigned char *bitnum);
247 int get_attribute_text(uint64_t *atts, const char *delim, char **text);
248 int attribute_check_callback(struct afs_callback_arg *aca);
251 void aft_init(struct afs_table *t);
252 int aft_get_row_of_path(const char *path, struct osl_row **row);
253 int aft_check_attributes(uint64_t att_mask, struct para_buffer *pb);
254 int open_and_update_audio_file(struct audio_file_data *afd);
255 int load_afd(int shmid, struct audio_file_data *afd);
256 int get_afsi_of_row(const struct osl_row *row, struct afs_info *afsi);
257 int get_afhi_of_row(const struct osl_row *row, struct afh_info *afhi);
258 int get_audio_file_path_of_row(const struct osl_row *row, char **path);
259 int audio_file_loop(void *private_data, osl_rbtree_loop_func *func);
260 int aft_check_callback(struct afs_callback_arg *aca);
263 int playlist_open(const char *name);
264 void playlist_close(void);
265 int playlist_check_callback(struct afs_callback_arg *aca);
267 /** evaluates to 1 if x < y, to -1 if x > y and to 0 if x == y */
268 #define NUM_COMPARE(x, y) ((int)((x) < (y)) - (int)((x) > (y)))
271 /** Define exported functions and a table pointer for an osl blob table. */
272 #define DECLARE_BLOB_SYMBOLS(table_name, cmd_prefix) \
273 void table_name ## _init(struct afs_table *t); \
274 int cmd_prefix ## _get_name_by_id(uint32_t id, char **name); \
275 int cmd_prefix ## _get_def_by_id(uint32_t id, struct osl_object *def); \
276 int cmd_prefix ## _get_def_by_name(char *name, struct osl_object *def); \
277 int cmd_prefix ## _get_name_and_def_by_row(const struct osl_row *row, \
278 char **name, struct osl_object *def); \
279 int table_name ##_event_handler(enum afs_events event, \
280 struct para_buffer *pb, void *data); \
281 extern struct osl_table *table_name ## _table;
283 DECLARE_BLOB_SYMBOLS(lyrics, lyr);
284 DECLARE_BLOB_SYMBOLS(images, img);
285 DECLARE_BLOB_SYMBOLS(moods, mood);
286 DECLARE_BLOB_SYMBOLS(playlists, pl);
288 /** The columns of an abstract blob table. */
289 enum blob_table_columns {
290 /** The identifier, a positive integer that never repeats. */
292 /** The unique name of the blob. */
294 /** The actual blob contents. */
296 /** A blob table has that many columns. */