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