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