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