2 * This file is part of DisOrder.
3 * Copyright (C) 2006, 2007 Richard Kettlewell
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.
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.
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
20 /** @file lib/eclient.h
21 * @brief Client code for event-driven programs
29 /* Asynchronous client interface */
31 /** @brief Handle type */
32 typedef struct disorder_eclient disorder_eclient;
36 /** @brief Set to read from the FD */
37 #define DISORDER_POLL_READ 1u
39 /** @brief Set to write to the FD */
40 #define DISORDER_POLL_WRITE 2u
42 /** @brief Callbacks for all clients
44 * These must all be valid.
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
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.
55 void (*comms_error)(void *u, const char *msg);
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
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
67 void (*protocol_error)(void *u, void *v, int code, const char *msg);
69 /** @brief Set poll/select flags
70 * @param u from disorder_eclient_new()
72 * @param fd file descriptor
73 * @param mode bitmap (@ref DISORDER_POLL_READ and/or @ref DISORDER_POLL_WRITE)
75 * Before @p fd is closed you will always get a call with @p mode = 0.
77 void (*poll)(void *u, disorder_eclient *c, int fd, unsigned mode);
79 /** @brief Report current activity
80 * @param u from disorder_eclient_new()
81 * @param msg Current activity or NULL
83 * Called with @p msg = NULL when there's nothing going on.
85 void (*report)(void *u, const char *msg);
86 } disorder_eclient_callbacks;
88 /** @brief Callbacks for log clients
90 * All of these are allowed to be a null pointers in which case you don't get
91 * told about that log event.
93 * See disorder_protocol(5) for full documentation.
95 typedef struct disorder_eclient_log_callbacks {
96 /** @brief Called on (re-)connection */
97 void (*connected)(void *v);
99 /** @brief Called when @p track finished playing successfully */
100 void (*completed)(void *v, const char *track);
102 /** @brief Called when @p track fails for some reason */
103 void (*failed)(void *v, const char *track, const char *status);
105 /** @brief Called when @p user moves some track or tracks in the queue
107 * Fetch the queue again to find out what the new order is - the
108 * rearrangement could in principle be arbitrarily complicated.
110 void (*moved)(void *v, const char *user);
112 /** @brief Called when @p track starts playing
114 * @p user might be 0.
116 void (*playing)(void *v, const char *track, const char *user/*maybe 0*/);
118 /** @brief Called when @p q is added to the queue
120 * Fetch the queue again to find out where the in the queue it was added.
122 void (*queue)(void *v, struct queue_entry *q);
124 /** @brief Called when @p q is added to the recent list */
125 void (*recent_added)(void *v, struct queue_entry *q);
127 /** @brief Called when @p id is removed from the recent list */
128 void (*recent_removed)(void *v, const char *id);
130 /** @brief Called when @id is removed from the queue
132 * @p user might be 0.
134 void (*removed)(void *v, const char *id, const char *user/*maybe 0*/);
136 /** @brief Called when @p track is scratched */
137 void (*scratched)(void *v, const char *track, const char *user);
139 /** @brief Called with the current state whenever it changes
142 * - @ref DISORDER_PLAYING_ENABLED
143 * - @ref DISORDER_RANDOM_ENABLED
144 * - @ref DISORDER_TRACK_PAUSED
145 * - @ref DISORDER_PLAYING
146 * - @ref DISORDER_CONNECTED
148 void (*state)(void *v, unsigned long state);
150 /** @brief Called when the volume changes */
151 void (*volume)(void *v, int left, int right);
153 /** @brief Called when a rescan completes */
154 void (*rescanned)(void *v);
156 /** @brief Called when a user is created (admins only) */
157 void (*user_add)(void *v, const char *user);
159 /** @brief Called when a user is confirmed (admins only) */
160 void (*user_confirm)(void *v, const char *user);
162 /** @brief Called when a user is deleted (admins only) */
163 void (*user_delete)(void *v, const char *user);
165 /** @brief Called when a user is edited (admins only) */
166 void (*user_edit)(void *v, const char *user, const char *property);
168 /** @brief Called when your rights change */
169 void (*rights_changed)(void *v, rights_type new_rights);
170 } disorder_eclient_log_callbacks;
174 /** @brief Play is enabled */
175 #define DISORDER_PLAYING_ENABLED 0x00000001
177 /** @brief Random play is enabled */
178 #define DISORDER_RANDOM_ENABLED 0x00000002
180 /** @brief Track is paused
182 * This is only meaningful if @ref DISORDER_PLAYING is set
184 #define DISORDER_TRACK_PAUSED 0x00000004
186 /** @brief Track is playing
188 * This can be set even if the current track is paused (in which case @ref
189 * DISORDER_TRACK_PAUSED) will also be set.
191 #define DISORDER_PLAYING 0x00000008
193 /** @brief Connected to server
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.
199 #define DISORDER_CONNECTED 0x00000010
201 char *disorder_eclient_interpret_state(unsigned long statebits);
207 /* Completion callbacks. These provide the result of operations to the caller.
208 * Unlike in earlier releases, these are not allowed to be NULL. */
210 /** @brief Trivial completion callback
212 * @param err Error string or NULL on succes
214 typedef void disorder_eclient_no_response(void *v,
217 /** @brief String result completion callback
219 * @param err Error string or NULL on succes
220 * @param value Result or NULL
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(), disorder_eclient_userinfo() and
225 * disorder_eclient_playlist_get_share()).
227 * @p error will be non-NULL on failure. In this case @p value is always NULL.
229 typedef void disorder_eclient_string_response(void *v,
233 /** @brief String result completion callback
235 * @param err Error string or NULL on succes
236 * @param value Result or 0
238 * @p error will be NULL on success. In this case @p value will be the result.
240 * @p error will be non-NULL on failure. In this case @p value is always 0.
242 typedef void disorder_eclient_integer_response(void *v,
245 /** @brief Volume completion callback
247 * @param err Error string or NULL on success
248 * @param l Left channel volume
249 * @param r Right channel volume
251 * @p error will be NULL on success. In this case @p l and @p r will be the
254 * @p error will be non-NULL on failure. In this case @p l and @p r are always
257 typedef void disorder_eclient_volume_response(void *v,
261 /** @brief Queue request completion callback
263 * @param err Error string or NULL on success
264 * @param q Head of queue data list
266 * @p error will be NULL on success. In this case @p q will be the (head of
269 * @p error will be non-NULL on failure. In this case @p q may be NULL but
270 * MIGHT also be some subset of the queue. For consistent behavior it should
271 * be ignored in the error case.
273 typedef void disorder_eclient_queue_response(void *v,
275 struct queue_entry *q);
277 /** @brief List request completion callback
279 * @param err Error string or NULL on success
280 * @param nvec Number of elements in response list
281 * @param vec Pointer to response list
283 * @p error will be NULL on success. In this case @p nvec and @p vec will give
284 * the result, or be -1 and NULL respectively e.g. from
285 * disorder_eclient_playlist_get() if there is no such playlist.
287 * @p error will be non-NULL on failure. In this case @p nvec and @p vec will
290 typedef void disorder_eclient_list_response(void *v,
292 int nvec, char **vec);
294 disorder_eclient *disorder_eclient_new(const disorder_eclient_callbacks *cb,
296 /* Create a new client */
298 void disorder_eclient_close(disorder_eclient *c);
301 unsigned long disorder_eclient_state(const disorder_eclient *c);
303 void disorder_eclient_polled(disorder_eclient *c, unsigned mode);
304 /* Should be called when c's FD is readable and/or writable, and in any case
305 * from time to time (so that retries work). */
307 int disorder_eclient_version(disorder_eclient *c,
308 disorder_eclient_string_response *completed,
310 /* fetch the server version */
312 int disorder_eclient_play(disorder_eclient *c,
314 disorder_eclient_no_response *completed,
316 /* add a track to the queue */
318 int disorder_eclient_pause(disorder_eclient *c,
319 disorder_eclient_no_response *completed,
321 /* add a track to the queue */
323 int disorder_eclient_resume(disorder_eclient *c,
324 disorder_eclient_no_response *completed,
326 /* add a track to the queue */
328 int disorder_eclient_scratch(disorder_eclient *c,
330 disorder_eclient_no_response *completed,
332 /* scratch a track by ID */
334 int disorder_eclient_scratch_playing(disorder_eclient *c,
335 disorder_eclient_no_response *completed,
337 /* scratch the playing track whatever it is */
339 int disorder_eclient_remove(disorder_eclient *c,
341 disorder_eclient_no_response *completed,
343 /* remove a track from the queue */
345 int disorder_eclient_moveafter(disorder_eclient *c,
349 disorder_eclient_no_response *completed,
351 /* move tracks within the queue */
353 int disorder_eclient_playing(disorder_eclient *c,
354 disorder_eclient_queue_response *completed,
356 /* find the currently playing track (0 for none) */
358 int disorder_eclient_queue(disorder_eclient *c,
359 disorder_eclient_queue_response *completed,
361 /* list recently played tracks */
363 int disorder_eclient_recent(disorder_eclient *c,
364 disorder_eclient_queue_response *completed,
366 /* list recently played tracks */
368 int disorder_eclient_files(disorder_eclient *c,
369 disorder_eclient_list_response *completed,
373 /* list files in a directory, matching RE if not a null pointer */
375 int disorder_eclient_dirs(disorder_eclient *c,
376 disorder_eclient_list_response *completed,
380 /* list directories in a directory, matching RE if not a null pointer */
382 int disorder_eclient_namepart(disorder_eclient *c,
383 disorder_eclient_string_response *completed,
388 /* look up a track name part */
390 int disorder_eclient_length(disorder_eclient *c,
391 disorder_eclient_integer_response *completed,
394 /* look up a track name length */
396 int disorder_eclient_volume(disorder_eclient *c,
397 disorder_eclient_volume_response *callback,
400 /* If L and R are both -ve gets the volume.
401 * If neither are -ve then sets the volume.
405 int disorder_eclient_enable(disorder_eclient *c,
406 disorder_eclient_no_response *callback,
408 int disorder_eclient_disable(disorder_eclient *c,
409 disorder_eclient_no_response *callback,
411 int disorder_eclient_random_enable(disorder_eclient *c,
412 disorder_eclient_no_response *callback,
414 int disorder_eclient_random_disable(disorder_eclient *c,
415 disorder_eclient_no_response *callback,
417 /* Enable/disable play/random play */
419 int disorder_eclient_resolve(disorder_eclient *c,
420 disorder_eclient_string_response *completed,
423 /* Resolve aliases */
425 int disorder_eclient_log(disorder_eclient *c,
426 const disorder_eclient_log_callbacks *callbacks,
428 /* Make this a log client (forever - it automatically becomes one again upon
431 int disorder_eclient_get(disorder_eclient *c,
432 disorder_eclient_string_response *completed,
433 const char *track, const char *pref,
435 int disorder_eclient_set(disorder_eclient *c,
436 disorder_eclient_no_response *completed,
437 const char *track, const char *pref,
440 int disorder_eclient_unset(disorder_eclient *c,
441 disorder_eclient_no_response *completed,
442 const char *track, const char *pref,
444 /* Get/set preference values */
446 int disorder_eclient_search(disorder_eclient *c,
447 disorder_eclient_list_response *completed,
451 int disorder_eclient_nop(disorder_eclient *c,
452 disorder_eclient_no_response *completed,
455 int disorder_eclient_new_tracks(disorder_eclient *c,
456 disorder_eclient_list_response *completed,
460 int disorder_eclient_rtp_address(disorder_eclient *c,
461 disorder_eclient_list_response *completed,
464 int disorder_eclient_users(disorder_eclient *c,
465 disorder_eclient_list_response *completed,
467 int disorder_eclient_deluser(disorder_eclient *c,
468 disorder_eclient_no_response *completed,
471 int disorder_eclient_userinfo(disorder_eclient *c,
472 disorder_eclient_string_response *completed,
474 const char *property,
476 int disorder_eclient_edituser(disorder_eclient *c,
477 disorder_eclient_no_response *completed,
479 const char *property,
482 int disorder_eclient_adduser(disorder_eclient *c,
483 disorder_eclient_no_response *completed,
485 const char *password,
488 void disorder_eclient_enable_connect(disorder_eclient *c);
489 void disorder_eclient_disable_connect(disorder_eclient *c);
490 int disorder_eclient_playlists(disorder_eclient *c,
491 disorder_eclient_list_response *completed,
493 int disorder_eclient_playlist_delete(disorder_eclient *c,
494 disorder_eclient_no_response *completed,
495 const char *playlist,
497 int disorder_eclient_playlist_lock(disorder_eclient *c,
498 disorder_eclient_no_response *completed,
499 const char *playlist,
501 int disorder_eclient_playlist_unlock(disorder_eclient *c,
502 disorder_eclient_no_response *completed,
504 int disorder_eclient_playlist_set_share(disorder_eclient *c,
505 disorder_eclient_no_response *completed,
506 const char *playlist,
509 int disorder_eclient_playlist_get_share(disorder_eclient *c,
510 disorder_eclient_string_response *completed,
511 const char *playlist,
513 int disorder_eclient_playlist_set(disorder_eclient *c,
514 disorder_eclient_no_response *completed,
515 const char *playlist,
519 int disorder_eclient_playlist_get(disorder_eclient *c,
520 disorder_eclient_list_response *completed,
521 const char *playlist,