chiark / gitweb /
6901961872bd848bcfd32ab94211c1133d048fe0
[disorder] / lib / eclient.h
1 /*
2  * This file is part of DisOrder.
3  * Copyright (C) 2006, 2007 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 2 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, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * 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, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA
19  */
20 /** @file lib/eclient.h
21  * @brief Client code for event-driven programs
22  */
23
24 #ifndef ECLIENT_H
25 #define ECLIENT_H
26
27 #include "rights.h"
28
29 /* Asynchronous client interface */
30
31 /** @brief Handle type */
32 typedef struct disorder_eclient disorder_eclient;
33
34 struct queue_entry;
35
36 /** @brief Set to read from the FD */
37 #define DISORDER_POLL_READ 1u
38
39 /** @brief Set to write to the FD */
40 #define DISORDER_POLL_WRITE 2u
41
42 /** @brief Callbacks for all clients
43  *
44  * These must all be valid.
45  */
46 typedef struct disorder_eclient_callbacks {
47   /** @brief Called when a communication error occurs.
48    * @param u from disorder_eclient_new()
49    * @param msg error message
50    *
51    * This might be called at any time, and indicates a low-level error,
52    * e.g. connection refused by the server.  It does not mean that any requests
53    * made of the owning eclient will not be fulfilled at some point.
54    */
55   void (*comms_error)(void *u, const char *msg);
56   
57   /** @brief Called when a command fails (including initial authorization).
58    * @param u from disorder_eclient_new()
59    * @param v from failed command, or NULL if during setup
60    * @param msg error message
61    *
62    * This call is obsolete at least in its current form, in which it is used to
63    * report most errors from most requests.  Ultimately requests-specific
64    * errors will be reported in a request-specific way rather than via this
65    * generic callback.
66    */
67   void (*protocol_error)(void *u, void *v, int code, const char *msg);
68
69   /** @brief Set poll/select flags
70    * @param u from disorder_eclient_new()
71    * @param c handle
72    * @param fd file descriptor
73    * @param mode bitmap (@ref DISORDER_POLL_READ and/or @ref DISORDER_POLL_WRITE)
74    *
75    * Before @p fd is closed you will always get a call with @p mode = 0.
76    */
77   void (*poll)(void *u, disorder_eclient *c, int fd, unsigned mode);
78
79   /** @brief Report current activity
80    * @param u from disorder_eclient_new()
81    * @param msg Current activity or NULL
82    *
83    * Called with @p msg = NULL when there's nothing going on.
84    */
85   void (*report)(void *u, const char *msg);
86 } disorder_eclient_callbacks;
87
88 /** @brief Callbacks for log clients
89  *
90  * All of these are allowed to be a null pointers in which case you don't get
91  * told about that log event.
92  *
93  * See disorder_protocol(5) for full documentation.
94  */
95 typedef struct disorder_eclient_log_callbacks {
96   /** @brief Called on (re-)connection */
97   void (*connected)(void *v);
98
99   /** @brief Called when @p track finished playing successfully */
100   void (*completed)(void *v, const char *track);
101
102   /** @brief Called when @p track fails for some reason */
103   void (*failed)(void *v, const char *track, const char *status);
104
105   /** @brief Called when @p user moves some track or tracks in the queue
106    *
107    * Fetch the queue again to find out what the new order is - the
108    * rearrangement could in principle be arbitrarily complicated.
109    */
110   void (*moved)(void *v, const char *user);
111
112   /** @brief Called when @p track starts playing
113    *
114    * @p user might be 0.
115    */
116   void (*playing)(void *v, const char *track, const char *user/*maybe 0*/);
117
118   /** @brief Called when @p q is added to the queue
119    *
120    * Fetch the queue again to find out where the in the queue it was added.
121    */   
122   void (*queue)(void *v, struct queue_entry *q);
123
124   /** @brief Called when @p q is added to the recent list */
125   void (*recent_added)(void *v, struct queue_entry *q);
126
127   /** @brief Called when @p id is removed from the recent list */
128   void (*recent_removed)(void *v, const char *id);
129
130   /** @brief Called when @id is removed from the queue
131    *
132    * @p user might be 0.
133    */
134   void (*removed)(void *v, const char *id, const char *user/*maybe 0*/);
135
136   /** @brief Called when @p track is scratched */
137   void (*scratched)(void *v, const char *track, const char *user);
138
139   /** @brief Called with the current state whenever it changes
140    *
141    * State bits are:
142    * - @ref DISORDER_PLAYING_ENABLED
143    * - @ref DISORDER_RANDOM_ENABLED
144    * - @ref DISORDER_TRACK_PAUSED
145    * - @ref DISORDER_PLAYING
146    * - @ref DISORDER_CONNECTED
147    */
148   void (*state)(void *v, unsigned long state);
149
150   /** @brief Called when the volume changes */
151   void (*volume)(void *v, int left, int right);
152
153   /** @brief Called when a rescan completes */
154   void (*rescanned)(void *v);
155
156   /** @brief Called when a user is created (admins only) */
157   void (*user_add)(void *v, const char *user);
158
159   /** @brief Called when a user is confirmed (admins only) */
160   void (*user_confirm)(void *v, const char *user);
161
162   /** @brief Called when a user is deleted (admins only) */
163   void (*user_delete)(void *v, const char *user);
164
165   /** @brief Called when a user is edited (admins only) */
166   void (*user_edit)(void *v, const char *user, const char *property);
167
168   /** @brief Called when your rights change */
169   void (*rights_changed)(void *v, rights_type new_rights);
170 } disorder_eclient_log_callbacks;
171
172 /* State bits */
173
174 /** @brief Play is enabled */
175 #define DISORDER_PLAYING_ENABLED  0x00000001
176
177 /** @brief Random play is enabled */
178 #define DISORDER_RANDOM_ENABLED   0x00000002
179
180 /** @brief Track is paused
181  *
182  * This is only meaningful if @ref DISORDER_PLAYING is set
183  */
184 #define DISORDER_TRACK_PAUSED     0x00000004
185
186 /** @brief Track is playing
187  *
188  * This can be set even if the current track is paused (in which case @ref
189  * DISORDER_TRACK_PAUSED) will also be set.
190  */
191 #define DISORDER_PLAYING    0x00000008
192
193 /** @brief Connected to server
194  *
195  * By connected it is meant that commands have a reasonable chance of being
196  * processed soon, not merely that a TCP connection exists - for instance if
197  * the client is still authenticating then that does not count as connected.
198  */
199 #define DISORDER_CONNECTED        0x00000010
200
201 char *disorder_eclient_interpret_state(unsigned long statebits);
202
203 struct queue_entry;
204 struct kvp;
205 struct sink;
206
207 /* Completion callbacks.  These provide the result of operations to the caller.
208  * Unlike in earlier releases, these are not allowed to be NULL. */
209
210 /** @brief Trivial completion callback
211  * @param v User data
212  * @param err Error string or NULL on succes
213  */
214 typedef void disorder_eclient_no_response(void *v,
215                                           const char *err);
216
217 /** @brief String result completion callback
218  * @param v User data
219  * @param err Error string or NULL on succes
220  * @param value Result or NULL
221  *
222  * @p error will be NULL on success.  In this case @p value will be the result
223  * (which might be NULL for disorder_eclient_get(),
224  * disorder_eclient_get_global() and disorder_eclient_userinfo()).
225  *
226  * @p error will be non-NULL on failure.  In this case @p value is always NULL.
227  */
228 typedef void disorder_eclient_string_response(void *v,
229                                               const char *err,
230                                               const char *value);
231
232 /** @brief String result completion callback
233  * @param v User data
234  * @param err Error string or NULL on succes
235  * @param value Result or 0
236  *
237  * @p error will be NULL on success.  In this case @p value will be the result.
238  *
239  * @p error will be non-NULL on failure.  In this case @p value is always 0.
240  */
241 typedef void disorder_eclient_integer_response(void *v,
242                                                const char *err,
243                                                long value);
244 /** @brief Volume completion callback
245  * @param v User data
246  * @param err Error string or NULL on success
247  * @param l Left channel volume
248  * @param r Right channel volume
249  *
250  * @p error will be NULL on success.  In this case @p l and @p r will be the
251  * result.
252  *
253  * @p error will be non-NULL on failure.  In this case @p l and @p r are always
254  * 0.
255  */
256 typedef void disorder_eclient_volume_response(void *v,
257                                               const char *err,
258                                               int l, int r);
259
260 /** @brief Queue request completion callback
261  * @param v User data
262  * @param err Error string or NULL on success
263  * @param q Head of queue data list
264  *
265  * @p error will be NULL on success.  In this case @p q will be the (head of
266  * the) result.
267  *
268  * @p error will be non-NULL on failure.  In this case @p q may be NULL but
269  * MIGHT also be some subset of the queue.  For consistent behavior it should
270  * be ignored in the error case.
271  */
272 typedef void disorder_eclient_queue_response(void *v,
273                                              const char *err,
274                                              struct queue_entry *q);
275
276 /** @brief List request completion callback
277  * @param v User data
278  * @param err Error string or NULL on success
279  * @param nvec Number of elements in response list
280  * @param vec Pointer to response list
281  *
282  * @p error will be NULL on success.  In this case @p nvec and @p vec will give
283  * the result.
284  *
285  * @p error will be non-NULL on failure.  In this case @p nvec and @p vec will
286  * be 0 and NULL.
287  */
288 typedef void disorder_eclient_list_response(void *v,
289                                             const char *err,
290                                             int nvec, char **vec);
291
292 disorder_eclient *disorder_eclient_new(const disorder_eclient_callbacks *cb,
293                                        void *u);
294 /* Create a new client */
295
296 void disorder_eclient_close(disorder_eclient *c);
297 /* Close C */
298
299 unsigned long disorder_eclient_state(const disorder_eclient *c);
300
301 void disorder_eclient_polled(disorder_eclient *c, unsigned mode);
302 /* Should be called when c's FD is readable and/or writable, and in any case
303  * from time to time (so that retries work). */
304
305 int disorder_eclient_version(disorder_eclient *c,
306                              disorder_eclient_string_response *completed,
307                              void *v);
308 /* fetch the server version */
309
310 int disorder_eclient_play(disorder_eclient *c,
311                           const char *track,
312                           disorder_eclient_no_response *completed,
313                           void *v);
314 /* add a track to the queue */
315
316 int disorder_eclient_pause(disorder_eclient *c,
317                            disorder_eclient_no_response *completed,
318                            void *v);
319 /* add a track to the queue */
320
321 int disorder_eclient_resume(disorder_eclient *c,
322                             disorder_eclient_no_response *completed,
323                             void *v);
324 /* add a track to the queue */
325
326 int disorder_eclient_scratch(disorder_eclient *c,
327                              const char *id,
328                              disorder_eclient_no_response *completed,
329                              void *v);
330 /* scratch a track by ID */
331
332 int disorder_eclient_scratch_playing(disorder_eclient *c,
333                                      disorder_eclient_no_response *completed,
334                                      void *v);
335 /* scratch the playing track whatever it is */
336
337 int disorder_eclient_remove(disorder_eclient *c,
338                             const char *id,
339                             disorder_eclient_no_response *completed,
340                             void *v);
341 /* remove a track from the queue */
342
343 int disorder_eclient_moveafter(disorder_eclient *c,
344                                const char *target,
345                                int nids,
346                                const char **ids,
347                                disorder_eclient_no_response *completed,
348                                void *v);
349 /* move tracks within the queue */
350
351 int disorder_eclient_playing(disorder_eclient *c,
352                              disorder_eclient_queue_response *completed,
353                              void *v);
354 /* find the currently playing track (0 for none) */
355
356 int disorder_eclient_queue(disorder_eclient *c,
357                            disorder_eclient_queue_response *completed,
358                            void *v);
359 /* list recently played tracks */
360
361 int disorder_eclient_recent(disorder_eclient *c,
362                             disorder_eclient_queue_response *completed,
363                             void *v);
364 /* list recently played tracks */
365
366 int disorder_eclient_files(disorder_eclient *c,
367                            disorder_eclient_list_response *completed,
368                            const char *dir,
369                            const char *re,
370                            void *v);
371 /* list files in a directory, matching RE if not a null pointer */
372
373 int disorder_eclient_dirs(disorder_eclient *c,
374                           disorder_eclient_list_response *completed,
375                           const char *dir,
376                           const char *re,
377                           void *v);
378 /* list directories in a directory, matching RE if not a null pointer */
379
380 int disorder_eclient_namepart(disorder_eclient *c,
381                               disorder_eclient_string_response *completed,
382                               const char *track,
383                               const char *context,
384                               const char *part,
385                               void *v);
386 /* look up a track name part */
387
388 int disorder_eclient_length(disorder_eclient *c,
389                             disorder_eclient_integer_response *completed,
390                             const char *track,
391                             void *v);
392 /* look up a track name length */
393
394 int disorder_eclient_volume(disorder_eclient *c,
395                             disorder_eclient_volume_response *callback,
396                             int l, int r,
397                             void *v);
398 /* If L and R are both -ve gets the volume.
399  * If neither are -ve then sets the volume.
400  * Otherwise asserts!
401  */
402
403 int disorder_eclient_enable(disorder_eclient *c,
404                             disorder_eclient_no_response *callback,
405                             void *v);
406 int disorder_eclient_disable(disorder_eclient *c,
407                              disorder_eclient_no_response *callback,
408                              void *v);
409 int disorder_eclient_random_enable(disorder_eclient *c,
410                                    disorder_eclient_no_response *callback,
411                                    void *v);
412 int disorder_eclient_random_disable(disorder_eclient *c,
413                                     disorder_eclient_no_response *callback,
414                                     void *v);
415 /* Enable/disable play/random play */
416
417 int disorder_eclient_resolve(disorder_eclient *c,
418                              disorder_eclient_string_response *completed,
419                              const char *track,
420                              void *v);
421 /* Resolve aliases */
422
423 int disorder_eclient_log(disorder_eclient *c,
424                          const disorder_eclient_log_callbacks *callbacks,
425                          void *v);
426 /* Make this a log client (forever - it automatically becomes one again upon
427  * reconnection) */
428
429 int disorder_eclient_get(disorder_eclient *c,
430                          disorder_eclient_string_response *completed,
431                          const char *track, const char *pref,
432                          void *v);
433 int disorder_eclient_set(disorder_eclient *c,
434                          disorder_eclient_no_response *completed,
435                          const char *track, const char *pref, 
436                          const char *value,
437                          void *v);
438 int disorder_eclient_unset(disorder_eclient *c,
439                            disorder_eclient_no_response *completed,
440                            const char *track, const char *pref, 
441                            void *v);
442 /* Get/set preference values */
443
444 int disorder_eclient_search(disorder_eclient *c,
445                             disorder_eclient_list_response *completed,
446                             const char *terms,
447                             void *v);
448
449 int disorder_eclient_nop(disorder_eclient *c,
450                          disorder_eclient_no_response *completed,
451                          void *v);
452
453 int disorder_eclient_new_tracks(disorder_eclient *c,
454                                 disorder_eclient_list_response *completed,
455                                 int max,
456                                 void *v);
457
458 int disorder_eclient_rtp_address(disorder_eclient *c,
459                                  disorder_eclient_list_response *completed,
460                                  void *v);
461
462 int disorder_eclient_users(disorder_eclient *c,
463                            disorder_eclient_list_response *completed,
464                            void *v);
465 int disorder_eclient_deluser(disorder_eclient *c,
466                              disorder_eclient_no_response *completed,
467                              const char *user,
468                              void *v);
469 int disorder_eclient_userinfo(disorder_eclient *c,
470                               disorder_eclient_string_response *completed,
471                               const char *user,
472                               const char *property,
473                               void *v);
474 int disorder_eclient_edituser(disorder_eclient *c,
475                               disorder_eclient_no_response *completed,
476                               const char *user,
477                               const char *property,
478                               const char *value,
479                               void *v);
480 int disorder_eclient_adduser(disorder_eclient *c,
481                              disorder_eclient_no_response *completed,
482                              const char *user,
483                              const char *password,
484                              const char *rights,
485                              void *v);
486 void disorder_eclient_enable_connect(disorder_eclient *c);
487 void disorder_eclient_disable_connect(disorder_eclient *c);
488   
489 #endif
490
491 /*
492 Local Variables:
493 c-basic-offset:2
494 comment-column:40
495 fill-column:79
496 indent-tabs-mode:nil
497 End:
498 */