chiark / gitweb /
uaudio: more sophisticated choice of default playback API
[disorder] / lib / eclient.h
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder.
5c102112 3 * Copyright (C) 2006-2008 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
d0f01ef6
RK
20 *
21 * See @ref lib/eclient-stubs.h for the (generated) per-command entry points.
0e4472a0 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 129
59cf25c4 130 /** @brief Called when @p id is removed from the queue
58d2aad2
RK
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);
9433fabf
RK
170
171 /** @brief Called when a track is adopted */
172 void (*adopted)(void *v, const char *id, const char *who);
812b526d 173
5c102112
RK
174 /** @brief Called when a new playlist is created */
175 void (*playlist_created)(void *v, const char *playlist, const char *sharing);
176
177 /** @brief Called when a playlist is modified */
178 void (*playlist_modified)(void *v, const char *playlist, const char *sharing);
179
180 /** @brief Called when a new playlist is deleted */
181 void (*playlist_deleted)(void *v, const char *playlist);
ad0c0f36
RK
182
183 /** @brief Called when a global pref is changed or delete */
184 void (*global_pref)(void *v, const char *pref, const char *value/*or NULL*/);
460b9539 185} disorder_eclient_log_callbacks;
186
187/* State bits */
0e4472a0 188
189/** @brief Play is enabled */
190#define DISORDER_PLAYING_ENABLED 0x00000001
191
192/** @brief Random play is enabled */
193#define DISORDER_RANDOM_ENABLED 0x00000002
194
8f763f1b
RK
195/** @brief Track is paused
196 *
197 * This is only meaningful if @ref DISORDER_PLAYING is set
198 */
0e4472a0 199#define DISORDER_TRACK_PAUSED 0x00000004
460b9539 200
8f763f1b
RK
201/** @brief Track is playing
202 *
203 * This can be set even if the current track is paused (in which case @ref
204 * DISORDER_TRACK_PAUSED) will also be set.
205 */
206#define DISORDER_PLAYING 0x00000008
207
208/** @brief Connected to server
209 *
210 * By connected it is meant that commands have a reasonable chance of being
211 * processed soon, not merely that a TCP connection exists - for instance if
212 * the client is still authenticating then that does not count as connected.
213 */
214#define DISORDER_CONNECTED 0x00000010
215
00959300
RK
216char *disorder_eclient_interpret_state(unsigned long statebits);
217
460b9539 218struct queue_entry;
219struct kvp;
220struct sink;
221
222/* Completion callbacks. These provide the result of operations to the caller.
699517af 223 * Unlike in earlier releases, these are not allowed to be NULL. */
460b9539 224
53fa91bb
RK
225/** @brief Trivial completion callback
226 * @param v User data
e2717131 227 * @param err Error string or NULL on succes
53fa91bb
RK
228 */
229typedef void disorder_eclient_no_response(void *v,
e2717131 230 const char *err);
460b9539 231
1bd1b63c
RK
232/** @brief String result completion callback
233 * @param v User data
e2717131 234 * @param err Error string or NULL on succes
658c8a35 235 * @param value Result or NULL
1bd1b63c 236 *
06bfbba4
RK
237 * @p error will be NULL on success. In this case @p value will be the result
238 * (which might be NULL for disorder_eclient_get(),
53740beb
RK
239 * disorder_eclient_get_global(), disorder_eclient_userinfo() and
240 * disorder_eclient_playlist_get_share()).
06bfbba4
RK
241 *
242 * @p error will be non-NULL on failure. In this case @p value is always NULL.
1bd1b63c 243 */
06bfbba4 244typedef void disorder_eclient_string_response(void *v,
e2717131 245 const char *err,
06bfbba4 246 const char *value);
460b9539 247
3a8d0ea1 248/** @brief Integer result completion callback
658c8a35 249 * @param v User data
e2717131 250 * @param err Error string or NULL on succes
658c8a35
RK
251 * @param value Result or 0
252 *
253 * @p error will be NULL on success. In this case @p value will be the result.
254 *
255 * @p error will be non-NULL on failure. In this case @p value is always 0.
256 */
257typedef void disorder_eclient_integer_response(void *v,
e2717131 258 const char *err,
658c8a35 259 long value);
699517af
RK
260/** @brief Volume completion callback
261 * @param v User data
e2717131 262 * @param err Error string or NULL on success
699517af
RK
263 * @param l Left channel volume
264 * @param r Right channel volume
265 *
266 * @p error will be NULL on success. In this case @p l and @p r will be the
267 * result.
268 *
269 * @p error will be non-NULL on failure. In this case @p l and @p r are always
270 * 0.
271 */
ad131c25
RK
272typedef void disorder_eclient_pair_integer_response(void *v,
273 const char *err,
274 long l, long r);
460b9539 275
3035257f
RK
276/** @brief Queue request completion callback
277 * @param v User data
e2717131 278 * @param err Error string or NULL on success
3035257f
RK
279 * @param q Head of queue data list
280 *
4190a4d9
RK
281 * @p error will be NULL on success. In this case @p q will be the (head of
282 * the) result.
3035257f
RK
283 *
284 * @p error will be non-NULL on failure. In this case @p q may be NULL but
285 * MIGHT also be some subset of the queue. For consistent behavior it should
286 * be ignored in the error case.
287 */
288typedef void disorder_eclient_queue_response(void *v,
e2717131 289 const char *err,
3035257f 290 struct queue_entry *q);
460b9539 291
ad131c25
RK
292#define disorder_eclient_playing_response disorder_eclient_queue_response
293
4190a4d9
RK
294/** @brief List request completion callback
295 * @param v User data
e2717131 296 * @param err Error string or NULL on success
4190a4d9
RK
297 * @param nvec Number of elements in response list
298 * @param vec Pointer to response list
299 *
300 * @p error will be NULL on success. In this case @p nvec and @p vec will give
53740beb
RK
301 * the result, or be -1 and NULL respectively e.g. from
302 * disorder_eclient_playlist_get() if there is no such playlist.
4190a4d9
RK
303 *
304 * @p error will be non-NULL on failure. In this case @p nvec and @p vec will
305 * be 0 and NULL.
306 */
307typedef void disorder_eclient_list_response(void *v,
e2717131 308 const char *err,
4190a4d9 309 int nvec, char **vec);
460b9539 310
311disorder_eclient *disorder_eclient_new(const disorder_eclient_callbacks *cb,
312 void *u);
313/* Create a new client */
314
315void disorder_eclient_close(disorder_eclient *c);
316/* Close C */
317
8f763f1b 318unsigned long disorder_eclient_state(const disorder_eclient *c);
ad58ebcc 319
460b9539 320void disorder_eclient_polled(disorder_eclient *c, unsigned mode);
321/* Should be called when c's FD is readable and/or writable, and in any case
322 * from time to time (so that retries work). */
323
460b9539 324int disorder_eclient_scratch_playing(disorder_eclient *c,
325 disorder_eclient_no_response *completed,
326 void *v);
327/* scratch the playing track whatever it is */
328
460b9539 329int disorder_eclient_log(disorder_eclient *c,
330 const disorder_eclient_log_callbacks *callbacks,
331 void *v);
332/* Make this a log client (forever - it automatically becomes one again upon
333 * reconnection) */
334
a99c4e9a
RK
335int disorder_eclient_rtp_address(disorder_eclient *c,
336 disorder_eclient_list_response *completed,
337 void *v);
338
68210888
RK
339void disorder_eclient_enable_connect(disorder_eclient *c);
340void disorder_eclient_disable_connect(disorder_eclient *c);
ad131c25
RK
341
342#include "eclient-stubs.h"
53740beb 343
460b9539 344#endif
345
346/*
347Local Variables:
348c-basic-offset:2
349comment-column:40
350fill-column:79
351indent-tabs-mode:nil
352End:
353*/