chiark / gitweb /
Track properties
[disorder] / server / disorder-server.h
1 /*
2  * This file is part of DisOrder
3  * Copyright (C) 2008, 2009 Richard Kettlewell
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 /** @file server/disorder-server.h
19  * @brief Definitions for server and allied utilities
20  */
21
22 #ifndef DISORDER_SERVER_H
23 #define DISORDER_SERVER_H
24
25 #include "common.h"
26
27 #include <db.h>
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <fnmatch.h>
31 #include <gcrypt.h>
32 #include <getopt.h>
33 #include <grp.h>
34 #include <locale.h>
35 #include <netinet/in.h>
36 #include <pcre.h>
37 #include <pwd.h>
38 #include <signal.h>
39 #include <stddef.h>
40 #include <sys/socket.h>
41 #include <sys/stat.h>
42 #include <sys/time.h>
43 #include <sys/types.h>
44 #include <sys/un.h>
45 #include <sys/resource.h>
46 #include <syslog.h>
47 #include <time.h>
48 #include <unistd.h>
49
50 #include "addr.h"
51 #include "authhash.h"
52 #include "base64.h"
53 #include "cache.h"
54 #include "charset.h"
55 #include "configuration.h"
56 #include "cookies.h"
57 #include "defs.h"
58 #include "disorder.h"
59 #include "event.h"
60 #include "eventlog.h"
61 #include "eventlog.h"
62 #include "hash.h"
63 #include "hex.h"
64 #include "inputline.h"
65 #include "kvp.h"
66 #include "log.h"
67 #include "logfd.h"
68 #include "mem.h"
69 #include "mime.h"
70 #include "printf.h"
71 #include "queue.h"
72 #include "random.h"
73 #include "rights.h"
74 #include "sendmail.h"
75 #include "sink.h"
76 #include "speaker-protocol.h"
77 #include "split.h"
78 #include "syscalls.h"
79 #include "table.h"
80 #include "trackdb-int.h"
81 #include "trackdb.h"
82 #include "trackname.h"
83 #include "uaudio.h"
84 #include "unicode.h"
85 #include "user.h"
86 #include "vector.h"
87 #include "version.h"
88 #include "wstat.h"
89
90 extern const struct uaudio *api;
91
92 void daemonize(const char *tag, int fac, const char *pidfile);
93 /* Go into background.  Send stdout/stderr to syslog.
94  * If @pri@ is non-null, it should be "facility.level"
95  * If @tag@ is non-null, it is used as a tag to each message
96  * If @pidfile@ is non-null, the PID is written to that file.
97  */
98
99 void quit(ev_source *ev) attribute((noreturn));
100 /* terminate the daemon */
101
102 int reconfigure(ev_source *ev, unsigned flags);
103 /* reconfigure */
104
105 void reset_sockets(ev_source *ev);
106
107 /** @brief Set when starting server */
108 #define RECONFIGURE_FIRST 0x0001
109
110 /** @brief Set when reloading after SIGHUP etc */
111 #define RECONFIGURE_RELOADING 0x0002
112
113 void dbparams_check(void);
114
115 extern struct queue_entry qhead;
116 /* queue of things yet to be played.  the head will be played
117  * soonest. */
118
119 extern struct queue_entry phead;
120 /* things that have been played in the past.  the head is the oldest. */
121
122 extern long pcount;
123
124 void queue_read(void);
125 /* read the queue in.  Calls @fatal@ on error. */
126
127 void queue_write(void);
128 /* write the queue out.  Calls @fatal@ on error. */
129
130 void recent_read(void);
131 /* read the recently played list in.  Calls @fatal@ on error. */
132
133 void recent_write(void);
134 /* write the recently played list out.  Calls @fatal@ on error. */
135
136 struct queue_entry *queue_add(const char *track, const char *submitter,
137                               int where, const char *target,
138                               enum track_origin origin);
139 #define WHERE_START 0                   /* Add to head of queue */
140 #define WHERE_END 1                     /* Add to end of queue */
141 #define WHERE_BEFORE_RANDOM 2           /* End, or before random track */
142 #define WHERE_AFTER 3                   /* After the target */
143 /* add an entry to the queue.  Return a pointer to the new entry. */
144
145 void queue_remove(struct queue_entry *q, const char *who);
146 /* remove an from the queue */
147
148 struct queue_entry *queue_find(const char *key);
149 /* find a track in the queue by name or ID */
150
151 void queue_played(struct queue_entry *q);
152 /* add @q@ to the played list */
153
154 int queue_move(struct queue_entry *q, int delta, const char *who);
155 /* move element @q@ in the queue towards the front (@delta@ > 0) or towards the
156  * back (@delta@ < 0).  The return value is the leftover delta once we've hit
157  * the end in whichever direction we were going. */
158
159 void queue_moveafter(struct queue_entry *target,
160                      int nqs, struct queue_entry **qs, const char *who);
161 /* Move all the elements QS to just after TARGET, or to the head if
162  * TARGET=0. */
163
164 void queue_fix_sofar(struct queue_entry *q);
165 /* Fix up the sofar field for standalone players */
166
167 void schedule_init(ev_source *ev);
168 const char *schedule_add(ev_source *ev,
169                          struct kvp *actiondata);
170 int schedule_del(const char *id);
171 struct kvp *schedule_get(const char *id);
172 char **schedule_list(int *neventsp);
173
174 extern struct queue_entry *playing;     /* playing track or 0 */
175 extern int paused;                      /* non-0 if paused */
176
177 void play(ev_source *ev);
178 /* try to play something, if playing is enabled and nothing is playing
179  * already */
180
181 int playing_is_enabled(void);
182 /* return true iff playing is enabled */
183
184 void enable_playing(const char *who, ev_source *ev);
185 /* enable playing */
186
187 void disable_playing(const char *who);
188 /* disable playing. */
189
190 int random_is_enabled(void);
191 /* return true iff random play is enabled */
192
193 void enable_random(const char *who, ev_source *ev);
194 /* enable random play */
195
196 void disable_random(const char *who);
197 /* disable random play */
198
199 void scratch(const char *who, const char *id);
200 /* scratch the playing track.  @who@ identifies the scratcher. @id@ is
201  * the ID or a null pointer. */
202
203 void quitting(ev_source *ev);
204 /* called to terminate current track and shut down speaker process
205  * when quitting */
206
207 void speaker_setup(ev_source *ev);
208 /* set up the speaker subprocess */
209
210 void speaker_reload(void);
211 /* Tell the speaker process to reload its configuration. */
212
213 int pause_playing(const char *who);
214 /* Pause the current track.  Return 0 on success, -1 on error.  WHO
215  * can be 0. */
216
217 void resume_playing(const char *who);
218 /* Resume after a pause.  WHO can be 0. */
219
220 int prepare(ev_source *ev,
221             struct queue_entry *q);
222 /* Prepare to play Q */
223
224 void abandon(ev_source *ev,
225              struct queue_entry *q);
226 /* Abandon a possibly-prepared track. */
227
228 void add_random_track(ev_source *ev);
229 /* If random play is enabled then try to add a track to the queue. */
230
231 int server_start(ev_source *ev, int pf,
232                  size_t socklen, const struct sockaddr *sa,
233                  const char *name);
234 /* start listening.  Return the fd. */
235
236 int server_stop(ev_source *ev, int fd);
237 /* Stop listening on @fd@ */
238
239 extern int volume_left, volume_right;   /* last known volume */
240
241 extern int wideopen;                    /* blindly accept all logins */
242
243 /* plugins */
244
245 struct plugin;
246
247 typedef void *plugin_handle;
248 typedef void function_t(void);
249
250 const struct plugin *open_plugin(const char *name,
251                                  unsigned flags);
252 #define PLUGIN_FATAL 0x0001             /* fatal() on error */
253 /* Open a plugin.  Returns a null pointer on error or a handle to it
254  * on success. */
255
256 function_t *get_plugin_function(const struct plugin *handle,
257                                 const char *symbol);
258 const void *get_plugin_object(const struct plugin *handle,
259                               const char *symbol);
260 /* Look up a function or an object in a plugin */
261
262 /* track length computation ***************************************************/
263
264 long tracklength(const char *plugin, const char *track, const char *path);
265
266 /* collection interface *******************************************************/
267
268 void scan(const char *module, const char *root);
269 /* write a list of path names below @root@ to standard output. */
270   
271 int check(const char *module, const char *root, const char *path);
272 /* Recheck a track, given its root and path name.  Return 1 if it
273  * exists, 0 if it does not exist and -1 if an error occurred. */
274
275 /* notification interface *****************************************************/
276
277 void notify_play(const char *track,
278                  const char *submitter);
279 /* we're going to play @track@.  It was submitted by @submitter@
280  * (might be a null pointer) */
281
282 void notify_scratch(const char *track,
283                     const char *submitter,
284                     const char *scratcher,
285                     int seconds);
286 /* @scratcher@ scratched @track@ after @seconds@.  It was submitted by
287  * @submitter@ (might be a null pointer) */
288
289 void notify_not_scratched(const char *track,
290                           const char *submitter);
291 /* @track@ (submitted by @submitter@, which might be a null pointer)
292  * was not scratched. */
293
294 void notify_queue(const char *track,
295                   const char *submitter);
296 /* @track@ was queued by @submitter@ */
297
298 void notify_queue_remove(const char *track,
299                          const char *remover);
300 /* @track@ removed from the queue by @remover@ (never a null pointer) */
301
302 void notify_queue_move(const char *track,
303                        const char *mover);
304 /* @track@ moved in the queue by @mover@ (never a null pointer) */
305
306 void notify_pause(const char *track,
307                   const char *pauser);
308 /* TRACK was paused by PAUSER (might be a null pointer) */
309
310 void notify_resume(const char *track,
311                    const char *resumer);
312 /* TRACK was resumed by PAUSER (might be a null pointer) */
313
314 /* track playing **************************************************************/
315
316 unsigned long play_get_type(const struct plugin *pl);
317 /* Get the type word for this plugin */
318
319 void *play_prefork(const struct plugin *pl,
320                    const char *track);
321 /* Call the prefork function for PL and return the user data */
322
323 void play_track(const struct plugin *pl,
324                 const char *const *parameters,
325                 int nparameters,
326                 const char *path,
327                 const char *track);
328 /* play a track.  Called inside a fork. */
329
330 void play_cleanup(const struct plugin *pl, void *data);
331 /* Call the cleanup function for PL if necessary */
332
333 int play_pause(const struct plugin *pl, long *playedp, void *data);
334 /* Pause track. */
335
336 void play_resume(const struct plugin *pl, void *data);
337 /* Resume track. */
338
339 /* background process support *************************************************/
340
341 /** @brief Child process parameters */
342 struct pbgc_params {
343   /** @brief Length of player command */
344   int argc;
345   /** @brief Player command */
346   const char **argv;
347   /** @brief Device to wait for or NULL */
348   const char *waitdevice;
349   /** @brief Raw track name */
350   const char *rawpath;
351 };
352
353 /** @brief Callback to play or prepare a track
354  * @param q Track to play or decode
355  * @param bgdata User data pointer
356  * @return Exit code
357  */
358 typedef int play_background_child_fn(struct queue_entry *q,
359                                      const struct pbgc_params *params,
360                                      void *bgdata);
361
362 int play_background(ev_source *ev,
363                     const struct stringlist *player,
364                     struct queue_entry *q,
365                     play_background_child_fn *child,
366                     void *bgdata);
367
368 /* Return values from start(),  prepare() and play_background() */
369
370 #define START_OK 0         /**< @brief Succeeded. */
371 #define START_HARDFAIL 1   /**< @brief Track is broken. */
372 #define START_SOFTFAIL 2   /**< @brief Track OK, system (temporarily?) broken */
373
374 #endif /* DISORDER_SERVER_H */
375
376 /*
377 Local Variables:
378 c-basic-offset:2
379 comment-column:40
380 fill-column:79
381 indent-tabs-mode:nil
382 End:
383 */