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
27 /* Asynchronous client interface */
29 /** @brief Handle type */
30 typedef struct disorder_eclient disorder_eclient;
34 /** @brief Set to read from the FD */
35 #define DISORDER_POLL_READ 1u
37 /** @brief Set to write to the FD */
38 #define DISORDER_POLL_WRITE 2u
40 /** @brief Callbacks for all clients
42 * These must all be valid.
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
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.
53 void (*comms_error)(void *u, const char *msg);
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
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
65 void (*protocol_error)(void *u, void *v, int code, const char *msg);
67 /** @brief Set poll/select flags
68 * @param u from disorder_eclient_new()
70 * @param fd file descriptor
71 * @param mode bitmap (@ref DISORDER_POLL_READ and/or @ref DISORDER_POLL_WRITE)
73 * Before @p fd is closed you will always get a call with @p mode = 0.
75 void (*poll)(void *u, disorder_eclient *c, int fd, unsigned mode);
77 /** @brief Report current activity
78 * @param u from disorder_eclient_new()
79 * @param msg Current activity or NULL
81 * Called with @p msg = NULL when there's nothing going on.
83 void (*report)(void *u, const char *msg);
84 } disorder_eclient_callbacks;
86 /** @brief Callbacks for log clients
88 * All of these are allowed to be a null pointers in which case you don't get
89 * told about that log event.
91 * See disorder_protocol(5) for full documentation.
93 typedef struct disorder_eclient_log_callbacks {
94 /** @brief Called on (re-)connection */
95 void (*connected)(void *v);
97 /** @brief Called when @p track finished playing successfully */
98 void (*completed)(void *v, const char *track);
100 /** @brief Called when @p track fails for some reason */
101 void (*failed)(void *v, const char *track, const char *status);
103 /** @brief Called when @p user moves some track or tracks in the queue
105 * Fetch the queue again to find out what the new order is - the
106 * rearrangement could in principle be arbitrarily complicated.
108 void (*moved)(void *v, const char *user);
110 /** @brief Called when @p track starts playing
112 * @p user might be 0.
114 void (*playing)(void *v, const char *track, const char *user/*maybe 0*/);
116 /** @brief Called when @p q is added to the queue
118 * Fetch the queue again to find out where the in the queue it was added.
120 void (*queue)(void *v, struct queue_entry *q);
122 /** @brief Called when @p q is added to the recent list */
123 void (*recent_added)(void *v, struct queue_entry *q);
125 /** @brief Called when @p id is removed from the recent list */
126 void (*recent_removed)(void *v, const char *id);
128 /** @brief Called when @id is removed from the queue
130 * @p user might be 0.
132 void (*removed)(void *v, const char *id, const char *user/*maybe 0*/);
134 /** @brief Called when @p track is scratched */
135 void (*scratched)(void *v, const char *track, const char *user);
137 /** @brief Called with the current state whenever it changes
140 * - @ref DISORDER_PLAYING_ENABLED
141 * - @ref DISORDER_RANDOM_ENABLED
142 * - @ref DISORDER_TRACK_PAUSED
143 * - @ref DISORDER_PLAYING
144 * - @ref DISORDER_CONNECTED
146 void (*state)(void *v, unsigned long state);
148 /** @brief Called when the volume changes */
149 void (*volume)(void *v, int left, int right);
151 /** @brief Called when a rescan completes */
152 void (*rescanned)(void *v);
154 /** @brief Called when a user is created (admins only) */
155 void (*user_add)(void *v, const char *user);
157 /** @brief Called when a user is confirmed (admins only) */
158 void (*user_confirm)(void *v, const char *user);
160 /** @brief Called when a user is deleted (admins only) */
161 void (*user_delete)(void *v, const char *user);
163 /** @brief Called when a user is edited (admins only) */
164 void (*user_edit)(void *v, const char *user, const char *property);
165 } disorder_eclient_log_callbacks;
169 /** @brief Play is enabled */
170 #define DISORDER_PLAYING_ENABLED 0x00000001
172 /** @brief Random play is enabled */
173 #define DISORDER_RANDOM_ENABLED 0x00000002
175 /** @brief Track is paused
177 * This is only meaningful if @ref DISORDER_PLAYING is set
179 #define DISORDER_TRACK_PAUSED 0x00000004
181 /** @brief Track is playing
183 * This can be set even if the current track is paused (in which case @ref
184 * DISORDER_TRACK_PAUSED) will also be set.
186 #define DISORDER_PLAYING 0x00000008
188 /** @brief Connected to server
190 * By connected it is meant that commands have a reasonable chance of being
191 * processed soon, not merely that a TCP connection exists - for instance if
192 * the client is still authenticating then that does not count as connected.
194 #define DISORDER_CONNECTED 0x00000010
196 char *disorder_eclient_interpret_state(unsigned long statebits);
202 /* Completion callbacks. These provide the result of operations to the caller.
203 * Unlike in earlier releases, these are not allowed to be NULL. */
205 /** @brief Trivial completion callback
207 * @param error Error string or NULL on succes
209 typedef void disorder_eclient_no_response(void *v,
212 /** @brief String result completion callback
214 * @param error Error string or NULL on succes
215 * @param value Result or NULL
217 * @p error will be NULL on success. In this case @p value will be the result
218 * (which might be NULL for disorder_eclient_get(),
219 * disorder_eclient_get_global() and disorder_eclient_userinfo()).
221 * @p error will be non-NULL on failure. In this case @p value is always NULL.
223 typedef void disorder_eclient_string_response(void *v,
227 /** @brief String result completion callback
229 * @param error Error string or NULL on succes
230 * @param value Result or 0
232 * @p error will be NULL on success. In this case @p value will be the result.
234 * @p error will be non-NULL on failure. In this case @p value is always 0.
236 typedef void disorder_eclient_integer_response(void *v,
239 /** @brief Volume completion callback
241 * @param error Error string or NULL on success
242 * @param l Left channel volume
243 * @param r Right channel volume
245 * @p error will be NULL on success. In this case @p l and @p r will be the
248 * @p error will be non-NULL on failure. In this case @p l and @p r are always
251 typedef void disorder_eclient_volume_response(void *v,
255 /** @brief Queue request completion callback
257 * @param error Error string or NULL on success
258 * @param q Head of queue data list
260 * @p error will be NULL on success. In this case @p q will be the (head of
263 * @p error will be non-NULL on failure. In this case @p q may be NULL but
264 * MIGHT also be some subset of the queue. For consistent behavior it should
265 * be ignored in the error case.
267 typedef void disorder_eclient_queue_response(void *v,
269 struct queue_entry *q);
271 /** @brief List request completion callback
273 * @param error Error string or NULL on success
274 * @param nvec Number of elements in response list
275 * @param vec Pointer to response list
277 * @p error will be NULL on success. In this case @p nvec and @p vec will give
280 * @p error will be non-NULL on failure. In this case @p nvec and @p vec will
283 typedef void disorder_eclient_list_response(void *v,
285 int nvec, char **vec);
287 disorder_eclient *disorder_eclient_new(const disorder_eclient_callbacks *cb,
289 /* Create a new client */
291 void disorder_eclient_close(disorder_eclient *c);
294 unsigned long disorder_eclient_state(const disorder_eclient *c);
296 void disorder_eclient_polled(disorder_eclient *c, unsigned mode);
297 /* Should be called when c's FD is readable and/or writable, and in any case
298 * from time to time (so that retries work). */
300 int disorder_eclient_version(disorder_eclient *c,
301 disorder_eclient_string_response *completed,
303 /* fetch the server version */
305 int disorder_eclient_play(disorder_eclient *c,
307 disorder_eclient_no_response *completed,
309 /* add a track to the queue */
311 int disorder_eclient_pause(disorder_eclient *c,
312 disorder_eclient_no_response *completed,
314 /* add a track to the queue */
316 int disorder_eclient_resume(disorder_eclient *c,
317 disorder_eclient_no_response *completed,
319 /* add a track to the queue */
321 int disorder_eclient_scratch(disorder_eclient *c,
323 disorder_eclient_no_response *completed,
325 /* scratch a track by ID */
327 int disorder_eclient_scratch_playing(disorder_eclient *c,
328 disorder_eclient_no_response *completed,
330 /* scratch the playing track whatever it is */
332 int disorder_eclient_remove(disorder_eclient *c,
334 disorder_eclient_no_response *completed,
336 /* remove a track from the queue */
338 int disorder_eclient_moveafter(disorder_eclient *c,
342 disorder_eclient_no_response *completed,
344 /* move tracks within the queue */
346 int disorder_eclient_playing(disorder_eclient *c,
347 disorder_eclient_queue_response *completed,
349 /* find the currently playing track (0 for none) */
351 int disorder_eclient_queue(disorder_eclient *c,
352 disorder_eclient_queue_response *completed,
354 /* list recently played tracks */
356 int disorder_eclient_recent(disorder_eclient *c,
357 disorder_eclient_queue_response *completed,
359 /* list recently played tracks */
361 int disorder_eclient_files(disorder_eclient *c,
362 disorder_eclient_list_response *completed,
366 /* list files in a directory, matching RE if not a null pointer */
368 int disorder_eclient_dirs(disorder_eclient *c,
369 disorder_eclient_list_response *completed,
373 /* list directories in a directory, matching RE if not a null pointer */
375 int disorder_eclient_namepart(disorder_eclient *c,
376 disorder_eclient_string_response *completed,
381 /* look up a track name part */
383 int disorder_eclient_length(disorder_eclient *c,
384 disorder_eclient_integer_response *completed,
387 /* look up a track name length */
389 int disorder_eclient_volume(disorder_eclient *c,
390 disorder_eclient_volume_response *callback,
393 /* If L and R are both -ve gets the volume.
394 * If neither are -ve then sets the volume.
398 int disorder_eclient_enable(disorder_eclient *c,
399 disorder_eclient_no_response *callback,
401 int disorder_eclient_disable(disorder_eclient *c,
402 disorder_eclient_no_response *callback,
404 int disorder_eclient_random_enable(disorder_eclient *c,
405 disorder_eclient_no_response *callback,
407 int disorder_eclient_random_disable(disorder_eclient *c,
408 disorder_eclient_no_response *callback,
410 /* Enable/disable play/random play */
412 int disorder_eclient_resolve(disorder_eclient *c,
413 disorder_eclient_string_response *completed,
416 /* Resolve aliases */
418 int disorder_eclient_log(disorder_eclient *c,
419 const disorder_eclient_log_callbacks *callbacks,
421 /* Make this a log client (forever - it automatically becomes one again upon
424 int disorder_eclient_get(disorder_eclient *c,
425 disorder_eclient_string_response *completed,
426 const char *track, const char *pref,
428 int disorder_eclient_set(disorder_eclient *c,
429 disorder_eclient_no_response *completed,
430 const char *track, const char *pref,
433 int disorder_eclient_unset(disorder_eclient *c,
434 disorder_eclient_no_response *completed,
435 const char *track, const char *pref,
437 /* Get/set preference values */
439 int disorder_eclient_search(disorder_eclient *c,
440 disorder_eclient_list_response *completed,
444 int disorder_eclient_nop(disorder_eclient *c,
445 disorder_eclient_no_response *completed,
448 int disorder_eclient_new_tracks(disorder_eclient *c,
449 disorder_eclient_list_response *completed,
453 int disorder_eclient_rtp_address(disorder_eclient *c,
454 disorder_eclient_list_response *completed,
457 int disorder_eclient_users(disorder_eclient *c,
458 disorder_eclient_list_response *completed,
460 int disorder_eclient_deluser(disorder_eclient *c,
461 disorder_eclient_no_response *completed,
464 int disorder_eclient_userinfo(disorder_eclient *c,
465 disorder_eclient_string_response *completed,
467 const char *property,
469 int disorder_eclient_edituser(disorder_eclient *c,
470 disorder_eclient_no_response *completed,
472 const char *property,
475 int disorder_eclient_adduser(disorder_eclient *c,
476 disorder_eclient_no_response *completed,
478 const char *password,