chiark / gitweb /
Switch to GPL v3
[disorder] / lib / eclient.h
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder.
1020001c 3 * Copyright (C) 2006, 2007 Richard Kettlewell
460b9539 4 *
e7eb3a27 5 * This program is free software: you can redistribute it and/or modify
460b9539 6 * it under the terms of the GNU General Public License as published by
e7eb3a27 7 * the Free Software Foundation, either version 3 of the License, or
460b9539 8 * (at your option) any later version.
e7eb3a27
RK
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 *
460b9539 15 * You should have received a copy of the GNU General Public License
e7eb3a27 16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
460b9539 17 */
0e4472a0 18/** @file lib/eclient.h
19 * @brief Client code for event-driven programs
20 */
460b9539 21
22#ifndef ECLIENT_H
23#define ECLIENT_H
24
ad492e00
RK
25#include "rights.h"
26
5626f6d2 27/* Asynchronous client interface */
460b9539 28
0e4472a0 29/** @brief Handle type */
460b9539 30typedef struct disorder_eclient disorder_eclient;
31
32struct queue_entry;
33
0e4472a0 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
460b9539 39
0e4472a0 40/** @brief Callbacks for all clients
41 *
42 * These must all be valid.
43 */
460b9539 44typedef struct disorder_eclient_callbacks {
1f3ce240 45 /** @brief Called when a communication error occurs.
0e4472a0 46 * @param u from disorder_eclient_new()
47 * @param msg error message
1f3ce240
RK
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.
0e4472a0 52 */
460b9539 53 void (*comms_error)(void *u, const char *msg);
460b9539 54
0e4472a0 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
1f3ce240
RK
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.
0e4472a0 64 */
460b9539 65 void (*protocol_error)(void *u, void *v, int code, const char *msg);
0e4472a0 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 */
460b9539 75 void (*poll)(void *u, disorder_eclient *c, int fd, unsigned mode);
460b9539 76
0e4472a0 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 */
460b9539 83 void (*report)(void *u, const char *msg);
460b9539 84} disorder_eclient_callbacks;
85
0e4472a0 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 */
460b9539 93typedef struct disorder_eclient_log_callbacks {
0e4472a0 94 /** @brief Called on (re-)connection */
460b9539 95 void (*connected)(void *v);
58d2aad2
RK
96
97 /** @brief Called when @p track finished playing successfully */
460b9539 98 void (*completed)(void *v, const char *track);
58d2aad2
RK
99
100 /** @brief Called when @p track fails for some reason */
460b9539 101 void (*failed)(void *v, const char *track, const char *status);
58d2aad2
RK
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 */
460b9539 108 void (*moved)(void *v, const char *user);
58d2aad2
RK
109
110 /** @brief Called when @p track starts playing
111 *
112 * @p user might be 0.
113 */
460b9539 114 void (*playing)(void *v, const char *track, const char *user/*maybe 0*/);
58d2aad2
RK
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 */
460b9539 120 void (*queue)(void *v, struct queue_entry *q);
58d2aad2
RK
121
122 /** @brief Called when @p q is added to the recent list */
460b9539 123 void (*recent_added)(void *v, struct queue_entry *q);
58d2aad2
RK
124
125 /** @brief Called when @p id is removed from the recent list */
460b9539 126 void (*recent_removed)(void *v, const char *id);
58d2aad2
RK
127
128 /** @brief Called when @id is removed from the queue
129 *
130 * @p user might be 0.
131 */
460b9539 132 void (*removed)(void *v, const char *id, const char *user/*maybe 0*/);
58d2aad2
RK
133
134 /** @brief Called when @p track is scratched */
460b9539 135 void (*scratched)(void *v, const char *track, const char *user);
58d2aad2
RK
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 */
460b9539 146 void (*state)(void *v, unsigned long state);
58d2aad2
RK
147
148 /** @brief Called when the volume changes */
460b9539 149 void (*volume)(void *v, int left, int right);
58d2aad2
RK
150
151 /** @brief Called when a rescan completes */
e025abff 152 void (*rescanned)(void *v);
58d2aad2
RK
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);
ad492e00
RK
165
166 /** @brief Called when your rights change */
167 void (*rights_changed)(void *v, rights_type new_rights);
460b9539 168} disorder_eclient_log_callbacks;
169
170/* State bits */
0e4472a0 171
172/** @brief Play is enabled */
173#define DISORDER_PLAYING_ENABLED 0x00000001
174
175/** @brief Random play is enabled */
176#define DISORDER_RANDOM_ENABLED 0x00000002
177
8f763f1b
RK
178/** @brief Track is paused
179 *
180 * This is only meaningful if @ref DISORDER_PLAYING is set
181 */
0e4472a0 182#define DISORDER_TRACK_PAUSED 0x00000004
460b9539 183
8f763f1b
RK
184/** @brief Track is playing
185 *
186 * This can be set even if the current track is paused (in which case @ref
187 * DISORDER_TRACK_PAUSED) will also be set.
188 */
189#define DISORDER_PLAYING 0x00000008
190
191/** @brief Connected to server
192 *
193 * By connected it is meant that commands have a reasonable chance of being
194 * processed soon, not merely that a TCP connection exists - for instance if
195 * the client is still authenticating then that does not count as connected.
196 */
197#define DISORDER_CONNECTED 0x00000010
198
00959300
RK
199char *disorder_eclient_interpret_state(unsigned long statebits);
200
460b9539 201struct queue_entry;
202struct kvp;
203struct sink;
204
205/* Completion callbacks. These provide the result of operations to the caller.
699517af 206 * Unlike in earlier releases, these are not allowed to be NULL. */
460b9539 207
53fa91bb
RK
208/** @brief Trivial completion callback
209 * @param v User data
e2717131 210 * @param err Error string or NULL on succes
53fa91bb
RK
211 */
212typedef void disorder_eclient_no_response(void *v,
e2717131 213 const char *err);
460b9539 214
1bd1b63c
RK
215/** @brief String result completion callback
216 * @param v User data
e2717131 217 * @param err Error string or NULL on succes
658c8a35 218 * @param value Result or NULL
1bd1b63c 219 *
06bfbba4
RK
220 * @p error will be NULL on success. In this case @p value will be the result
221 * (which might be NULL for disorder_eclient_get(),
222 * disorder_eclient_get_global() and disorder_eclient_userinfo()).
223 *
224 * @p error will be non-NULL on failure. In this case @p value is always NULL.
1bd1b63c 225 */
06bfbba4 226typedef void disorder_eclient_string_response(void *v,
e2717131 227 const char *err,
06bfbba4 228 const char *value);
460b9539 229
658c8a35
RK
230/** @brief String result completion callback
231 * @param v User data
e2717131 232 * @param err Error string or NULL on succes
658c8a35
RK
233 * @param value Result or 0
234 *
235 * @p error will be NULL on success. In this case @p value will be the result.
236 *
237 * @p error will be non-NULL on failure. In this case @p value is always 0.
238 */
239typedef void disorder_eclient_integer_response(void *v,
e2717131 240 const char *err,
658c8a35 241 long value);
699517af
RK
242/** @brief Volume completion callback
243 * @param v User data
e2717131 244 * @param err Error string or NULL on success
699517af
RK
245 * @param l Left channel volume
246 * @param r Right channel volume
247 *
248 * @p error will be NULL on success. In this case @p l and @p r will be the
249 * result.
250 *
251 * @p error will be non-NULL on failure. In this case @p l and @p r are always
252 * 0.
253 */
254typedef void disorder_eclient_volume_response(void *v,
e2717131 255 const char *err,
699517af 256 int l, int r);
460b9539 257
3035257f
RK
258/** @brief Queue request completion callback
259 * @param v User data
e2717131 260 * @param err Error string or NULL on success
3035257f
RK
261 * @param q Head of queue data list
262 *
4190a4d9
RK
263 * @p error will be NULL on success. In this case @p q will be the (head of
264 * the) result.
3035257f
RK
265 *
266 * @p error will be non-NULL on failure. In this case @p q may be NULL but
267 * MIGHT also be some subset of the queue. For consistent behavior it should
268 * be ignored in the error case.
269 */
270typedef void disorder_eclient_queue_response(void *v,
e2717131 271 const char *err,
3035257f 272 struct queue_entry *q);
460b9539 273
4190a4d9
RK
274/** @brief List request completion callback
275 * @param v User data
e2717131 276 * @param err Error string or NULL on success
4190a4d9
RK
277 * @param nvec Number of elements in response list
278 * @param vec Pointer to response list
279 *
280 * @p error will be NULL on success. In this case @p nvec and @p vec will give
281 * the result.
282 *
283 * @p error will be non-NULL on failure. In this case @p nvec and @p vec will
284 * be 0 and NULL.
285 */
286typedef void disorder_eclient_list_response(void *v,
e2717131 287 const char *err,
4190a4d9 288 int nvec, char **vec);
460b9539 289
290disorder_eclient *disorder_eclient_new(const disorder_eclient_callbacks *cb,
291 void *u);
292/* Create a new client */
293
294void disorder_eclient_close(disorder_eclient *c);
295/* Close C */
296
8f763f1b 297unsigned long disorder_eclient_state(const disorder_eclient *c);
ad58ebcc 298
460b9539 299void disorder_eclient_polled(disorder_eclient *c, unsigned mode);
300/* Should be called when c's FD is readable and/or writable, and in any case
301 * from time to time (so that retries work). */
302
303int disorder_eclient_version(disorder_eclient *c,
304 disorder_eclient_string_response *completed,
305 void *v);
306/* fetch the server version */
307
308int disorder_eclient_play(disorder_eclient *c,
309 const char *track,
310 disorder_eclient_no_response *completed,
311 void *v);
312/* add a track to the queue */
313
314int disorder_eclient_pause(disorder_eclient *c,
315 disorder_eclient_no_response *completed,
316 void *v);
317/* add a track to the queue */
318
319int disorder_eclient_resume(disorder_eclient *c,
320 disorder_eclient_no_response *completed,
321 void *v);
322/* add a track to the queue */
323
324int disorder_eclient_scratch(disorder_eclient *c,
325 const char *id,
326 disorder_eclient_no_response *completed,
327 void *v);
328/* scratch a track by ID */
329
330int disorder_eclient_scratch_playing(disorder_eclient *c,
331 disorder_eclient_no_response *completed,
332 void *v);
333/* scratch the playing track whatever it is */
334
335int disorder_eclient_remove(disorder_eclient *c,
336 const char *id,
337 disorder_eclient_no_response *completed,
338 void *v);
339/* remove a track from the queue */
340
341int disorder_eclient_moveafter(disorder_eclient *c,
342 const char *target,
343 int nids,
344 const char **ids,
345 disorder_eclient_no_response *completed,
346 void *v);
347/* move tracks within the queue */
348
349int disorder_eclient_playing(disorder_eclient *c,
350 disorder_eclient_queue_response *completed,
351 void *v);
352/* find the currently playing track (0 for none) */
353
354int disorder_eclient_queue(disorder_eclient *c,
355 disorder_eclient_queue_response *completed,
356 void *v);
357/* list recently played tracks */
358
359int disorder_eclient_recent(disorder_eclient *c,
360 disorder_eclient_queue_response *completed,
361 void *v);
362/* list recently played tracks */
363
364int disorder_eclient_files(disorder_eclient *c,
365 disorder_eclient_list_response *completed,
366 const char *dir,
367 const char *re,
368 void *v);
369/* list files in a directory, matching RE if not a null pointer */
370
371int disorder_eclient_dirs(disorder_eclient *c,
372 disorder_eclient_list_response *completed,
373 const char *dir,
374 const char *re,
375 void *v);
376/* list directories in a directory, matching RE if not a null pointer */
377
378int disorder_eclient_namepart(disorder_eclient *c,
379 disorder_eclient_string_response *completed,
380 const char *track,
381 const char *context,
382 const char *part,
383 void *v);
384/* look up a track name part */
385
386int disorder_eclient_length(disorder_eclient *c,
387 disorder_eclient_integer_response *completed,
388 const char *track,
389 void *v);
390/* look up a track name length */
391
392int disorder_eclient_volume(disorder_eclient *c,
393 disorder_eclient_volume_response *callback,
394 int l, int r,
395 void *v);
396/* If L and R are both -ve gets the volume.
397 * If neither are -ve then sets the volume.
398 * Otherwise asserts!
399 */
400
401int disorder_eclient_enable(disorder_eclient *c,
402 disorder_eclient_no_response *callback,
403 void *v);
404int disorder_eclient_disable(disorder_eclient *c,
405 disorder_eclient_no_response *callback,
406 void *v);
407int disorder_eclient_random_enable(disorder_eclient *c,
408 disorder_eclient_no_response *callback,
409 void *v);
410int disorder_eclient_random_disable(disorder_eclient *c,
411 disorder_eclient_no_response *callback,
412 void *v);
413/* Enable/disable play/random play */
414
415int disorder_eclient_resolve(disorder_eclient *c,
416 disorder_eclient_string_response *completed,
417 const char *track,
418 void *v);
419/* Resolve aliases */
420
421int disorder_eclient_log(disorder_eclient *c,
422 const disorder_eclient_log_callbacks *callbacks,
423 void *v);
424/* Make this a log client (forever - it automatically becomes one again upon
425 * reconnection) */
426
427int disorder_eclient_get(disorder_eclient *c,
428 disorder_eclient_string_response *completed,
429 const char *track, const char *pref,
430 void *v);
431int disorder_eclient_set(disorder_eclient *c,
432 disorder_eclient_no_response *completed,
433 const char *track, const char *pref,
434 const char *value,
435 void *v);
436int disorder_eclient_unset(disorder_eclient *c,
437 disorder_eclient_no_response *completed,
438 const char *track, const char *pref,
439 void *v);
440/* Get/set preference values */
441
442int disorder_eclient_search(disorder_eclient *c,
443 disorder_eclient_list_response *completed,
444 const char *terms,
445 void *v);
446
7858930d 447int disorder_eclient_nop(disorder_eclient *c,
448 disorder_eclient_no_response *completed,
449 void *v);
450
2a10b70b
RK
451int disorder_eclient_new_tracks(disorder_eclient *c,
452 disorder_eclient_list_response *completed,
453 int max,
454 void *v);
a99c4e9a
RK
455
456int disorder_eclient_rtp_address(disorder_eclient *c,
457 disorder_eclient_list_response *completed,
458 void *v);
459
ffc4dbaf
RK
460int disorder_eclient_users(disorder_eclient *c,
461 disorder_eclient_list_response *completed,
462 void *v);
4aa8a0a4
RK
463int disorder_eclient_deluser(disorder_eclient *c,
464 disorder_eclient_no_response *completed,
465 const char *user,
466 void *v);
e61aef23
RK
467int disorder_eclient_userinfo(disorder_eclient *c,
468 disorder_eclient_string_response *completed,
469 const char *user,
470 const char *property,
471 void *v);
472int disorder_eclient_edituser(disorder_eclient *c,
473 disorder_eclient_no_response *completed,
474 const char *user,
475 const char *property,
476 const char *value,
477 void *v);
0d719587
RK
478int disorder_eclient_adduser(disorder_eclient *c,
479 disorder_eclient_no_response *completed,
480 const char *user,
481 const char *password,
482 const char *rights,
483 void *v);
68210888
RK
484void disorder_eclient_enable_connect(disorder_eclient *c);
485void disorder_eclient_disable_connect(disorder_eclient *c);
486
460b9539 487#endif
488
489/*
490Local Variables:
491c-basic-offset:2
492comment-column:40
493fill-column:79
494indent-tabs-mode:nil
495End:
496*/