chiark / gitweb /
disorder_eclient_queue_response now gets error as well as success
[disorder] / lib / eclient.h
1 /*
2  * This file is part of DisOrder.
3  * Copyright (C) 2006, 2007 Richard Kettlewell
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  */
20 /** @file lib/eclient.h
21  * @brief Client code for event-driven programs
22  */
23
24 #ifndef ECLIENT_H
25 #define ECLIENT_H
26
27 /* Asynchronous client interface */
28
29 /** @brief Handle type */
30 typedef struct disorder_eclient disorder_eclient;
31
32 struct queue_entry;
33
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
39
40 /** @brief Callbacks for all clients
41  *
42  * These must all be valid.
43  */
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
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.
52    */
53   void (*comms_error)(void *u, const char *msg);
54   
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
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.
64    */
65   void (*protocol_error)(void *u, void *v, int code, const char *msg);
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    */
75   void (*poll)(void *u, disorder_eclient *c, int fd, unsigned mode);
76
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    */
83   void (*report)(void *u, const char *msg);
84 } disorder_eclient_callbacks;
85
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  */
93 typedef struct disorder_eclient_log_callbacks {
94   /** @brief Called on (re-)connection */
95   void (*connected)(void *v);
96   
97   void (*completed)(void *v, const char *track);
98   void (*failed)(void *v, const char *track, const char *status);
99   void (*moved)(void *v, const char *user);
100   void (*playing)(void *v, const char *track, const char *user/*maybe 0*/);
101   void (*queue)(void *v, struct queue_entry *q);
102   void (*recent_added)(void *v, struct queue_entry *q);
103   void (*recent_removed)(void *v, const char *id);
104   void (*removed)(void *v, const char *id, const char *user/*maybe 0*/);
105   void (*scratched)(void *v, const char *track, const char *user);
106   void (*state)(void *v, unsigned long state);
107   void (*volume)(void *v, int left, int right);
108   void (*rescanned)(void *v);
109 } disorder_eclient_log_callbacks;
110
111 /* State bits */
112
113 /** @brief Play is enabled */
114 #define DISORDER_PLAYING_ENABLED  0x00000001
115
116 /** @brief Random play is enabled */
117 #define DISORDER_RANDOM_ENABLED   0x00000002
118
119 /** @brief Track is paused
120  *
121  * This is only meaningful if @ref DISORDER_PLAYING is set
122  */
123 #define DISORDER_TRACK_PAUSED     0x00000004
124
125 /** @brief Track is playing
126  *
127  * This can be set even if the current track is paused (in which case @ref
128  * DISORDER_TRACK_PAUSED) will also be set.
129  */
130 #define DISORDER_PLAYING    0x00000008
131
132 /** @brief Connected to server
133  *
134  * By connected it is meant that commands have a reasonable chance of being
135  * processed soon, not merely that a TCP connection exists - for instance if
136  * the client is still authenticating then that does not count as connected.
137  */
138 #define DISORDER_CONNECTED        0x00000010
139
140 char *disorder_eclient_interpret_state(unsigned long statebits);
141
142 struct queue_entry;
143 struct kvp;
144 struct sink;
145
146 /* Completion callbacks.  These provide the result of operations to the caller.
147  * Unlike in earlier releases, these are not allowed to be NULL. */
148
149 /** @brief Trivial completion callback
150  * @param v User data
151  * @param error Error string or NULL on succes
152  */
153 typedef void disorder_eclient_no_response(void *v,
154                                           const char *error);
155
156 /** @brief String result completion callback
157  * @param v User data
158  * @param error Error string or NULL on succes
159  * @param value Result or NULL
160  *
161  * @p error will be NULL on success.  In this case @p value will be the result
162  * (which might be NULL for disorder_eclient_get(),
163  * disorder_eclient_get_global() and disorder_eclient_userinfo()).
164  *
165  * @p error will be non-NULL on failure.  In this case @p value is always NULL.
166  */
167 typedef void disorder_eclient_string_response(void *v,
168                                               const char *error,
169                                               const char *value);
170
171 /** @brief String result completion callback
172  * @param v User data
173  * @param error Error string or NULL on succes
174  * @param value Result or 0
175  *
176  * @p error will be NULL on success.  In this case @p value will be the result.
177  *
178  * @p error will be non-NULL on failure.  In this case @p value is always 0.
179  */
180 typedef void disorder_eclient_integer_response(void *v,
181                                                const char *error,
182                                                long value);
183 /** @brief Volume completion callback
184  * @param v User data
185  * @param error Error string or NULL on success
186  * @param l Left channel volume
187  * @param r Right channel volume
188  *
189  * @p error will be NULL on success.  In this case @p l and @p r will be the
190  * result.
191  *
192  * @p error will be non-NULL on failure.  In this case @p l and @p r are always
193  * 0.
194  */
195 typedef void disorder_eclient_volume_response(void *v,
196                                               const char *error,
197                                               int l, int r);
198
199 /** @brief Queue request completion callback
200  * @param v User data
201  * @param error Error string or NULL on success
202  * @param q Head of queue data list
203  *
204  * @p error will be NULL on success.  In this case @p q be the (head of the)
205  * result.
206  *
207  * @p error will be non-NULL on failure.  In this case @p q may be NULL but
208  * MIGHT also be some subset of the queue.  For consistent behavior it should
209  * be ignored in the error case.
210  */
211 typedef void disorder_eclient_queue_response(void *v,
212                                              const char *error,
213                                              struct queue_entry *q);
214
215 typedef void disorder_eclient_list_response(void *v, int nvec, char **vec);
216 /* completion callback for file listing etc */
217
218 disorder_eclient *disorder_eclient_new(const disorder_eclient_callbacks *cb,
219                                        void *u);
220 /* Create a new client */
221
222 void disorder_eclient_close(disorder_eclient *c);
223 /* Close C */
224
225 unsigned long disorder_eclient_state(const disorder_eclient *c);
226
227 void disorder_eclient_polled(disorder_eclient *c, unsigned mode);
228 /* Should be called when c's FD is readable and/or writable, and in any case
229  * from time to time (so that retries work). */
230
231 int disorder_eclient_version(disorder_eclient *c,
232                              disorder_eclient_string_response *completed,
233                              void *v);
234 /* fetch the server version */
235
236 int disorder_eclient_play(disorder_eclient *c,
237                           const char *track,
238                           disorder_eclient_no_response *completed,
239                           void *v);
240 /* add a track to the queue */
241
242 int disorder_eclient_pause(disorder_eclient *c,
243                            disorder_eclient_no_response *completed,
244                            void *v);
245 /* add a track to the queue */
246
247 int disorder_eclient_resume(disorder_eclient *c,
248                             disorder_eclient_no_response *completed,
249                             void *v);
250 /* add a track to the queue */
251
252 int disorder_eclient_scratch(disorder_eclient *c,
253                              const char *id,
254                              disorder_eclient_no_response *completed,
255                              void *v);
256 /* scratch a track by ID */
257
258 int disorder_eclient_scratch_playing(disorder_eclient *c,
259                                      disorder_eclient_no_response *completed,
260                                      void *v);
261 /* scratch the playing track whatever it is */
262
263 int disorder_eclient_remove(disorder_eclient *c,
264                             const char *id,
265                             disorder_eclient_no_response *completed,
266                             void *v);
267 /* remove a track from the queue */
268
269 int disorder_eclient_moveafter(disorder_eclient *c,
270                                const char *target,
271                                int nids,
272                                const char **ids,
273                                disorder_eclient_no_response *completed,
274                                void *v);
275 /* move tracks within the queue */
276
277 int disorder_eclient_playing(disorder_eclient *c,
278                              disorder_eclient_queue_response *completed,
279                              void *v);
280 /* find the currently playing track (0 for none) */
281
282 int disorder_eclient_queue(disorder_eclient *c,
283                            disorder_eclient_queue_response *completed,
284                            void *v);
285 /* list recently played tracks */
286
287 int disorder_eclient_recent(disorder_eclient *c,
288                             disorder_eclient_queue_response *completed,
289                             void *v);
290 /* list recently played tracks */
291
292 int disorder_eclient_files(disorder_eclient *c,
293                            disorder_eclient_list_response *completed,
294                            const char *dir,
295                            const char *re,
296                            void *v);
297 /* list files in a directory, matching RE if not a null pointer */
298
299 int disorder_eclient_dirs(disorder_eclient *c,
300                           disorder_eclient_list_response *completed,
301                           const char *dir,
302                           const char *re,
303                           void *v);
304 /* list directories in a directory, matching RE if not a null pointer */
305
306 int disorder_eclient_namepart(disorder_eclient *c,
307                               disorder_eclient_string_response *completed,
308                               const char *track,
309                               const char *context,
310                               const char *part,
311                               void *v);
312 /* look up a track name part */
313
314 int disorder_eclient_length(disorder_eclient *c,
315                             disorder_eclient_integer_response *completed,
316                             const char *track,
317                             void *v);
318 /* look up a track name length */
319
320 int disorder_eclient_volume(disorder_eclient *c,
321                             disorder_eclient_volume_response *callback,
322                             int l, int r,
323                             void *v);
324 /* If L and R are both -ve gets the volume.
325  * If neither are -ve then sets the volume.
326  * Otherwise asserts!
327  */
328
329 int disorder_eclient_enable(disorder_eclient *c,
330                             disorder_eclient_no_response *callback,
331                             void *v);
332 int disorder_eclient_disable(disorder_eclient *c,
333                              disorder_eclient_no_response *callback,
334                              void *v);
335 int disorder_eclient_random_enable(disorder_eclient *c,
336                                    disorder_eclient_no_response *callback,
337                                    void *v);
338 int disorder_eclient_random_disable(disorder_eclient *c,
339                                     disorder_eclient_no_response *callback,
340                                     void *v);
341 /* Enable/disable play/random play */
342
343 int disorder_eclient_resolve(disorder_eclient *c,
344                              disorder_eclient_string_response *completed,
345                              const char *track,
346                              void *v);
347 /* Resolve aliases */
348
349 int disorder_eclient_log(disorder_eclient *c,
350                          const disorder_eclient_log_callbacks *callbacks,
351                          void *v);
352 /* Make this a log client (forever - it automatically becomes one again upon
353  * reconnection) */
354
355 int disorder_eclient_get(disorder_eclient *c,
356                          disorder_eclient_string_response *completed,
357                          const char *track, const char *pref,
358                          void *v);
359 int disorder_eclient_set(disorder_eclient *c,
360                          disorder_eclient_no_response *completed,
361                          const char *track, const char *pref, 
362                          const char *value,
363                          void *v);
364 int disorder_eclient_unset(disorder_eclient *c,
365                            disorder_eclient_no_response *completed,
366                            const char *track, const char *pref, 
367                            void *v);
368 /* Get/set preference values */
369
370 int disorder_eclient_search(disorder_eclient *c,
371                             disorder_eclient_list_response *completed,
372                             const char *terms,
373                             void *v);
374
375 int disorder_eclient_nop(disorder_eclient *c,
376                          disorder_eclient_no_response *completed,
377                          void *v);
378
379 int disorder_eclient_new_tracks(disorder_eclient *c,
380                                 disorder_eclient_list_response *completed,
381                                 int max,
382                                 void *v);
383
384 int disorder_eclient_rtp_address(disorder_eclient *c,
385                                  disorder_eclient_list_response *completed,
386                                  void *v);
387
388 int disorder_eclient_users(disorder_eclient *c,
389                            disorder_eclient_list_response *completed,
390                            void *v);
391 int disorder_eclient_deluser(disorder_eclient *c,
392                              disorder_eclient_no_response *completed,
393                              const char *user,
394                              void *v);
395 int disorder_eclient_userinfo(disorder_eclient *c,
396                               disorder_eclient_string_response *completed,
397                               const char *user,
398                               const char *property,
399                               void *v);
400 int disorder_eclient_edituser(disorder_eclient *c,
401                               disorder_eclient_no_response *completed,
402                               const char *user,
403                               const char *property,
404                               const char *value,
405                               void *v);
406 int disorder_eclient_adduser(disorder_eclient *c,
407                              disorder_eclient_no_response *completed,
408                              const char *user,
409                              const char *password,
410                              const char *rights,
411                              void *v);
412
413 #endif
414
415 /*
416 Local Variables:
417 c-basic-offset:2
418 comment-column:40
419 fill-column:79
420 indent-tabs-mode:nil
421 End:
422 */