chiark / gitweb /
Add kvp_make(), to make a kvp list in a single function call.
[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  * It is always allowed for these to be null pointers if you don't care about
148  * the result. */
149
150 typedef void disorder_eclient_no_response(void *v);
151 /* completion callback with no data */
152
153 /** @brief String result completion callback
154  * @param v User data
155  * @param error Error string or NULL on succes
156  * @param value or NULL if not found
157  *
158  * @p error will be NULL on success.  In this case @p value will be the result
159  * (which might be NULL for disorder_eclient_get(),
160  * disorder_eclient_get_global() and disorder_eclient_userinfo()).
161  *
162  * @p error will be non-NULL on failure.  In this case @p value is always NULL.
163  */
164 typedef void disorder_eclient_string_response(void *v,
165                                               const char *error,
166                                               const char *value);
167
168 typedef void disorder_eclient_integer_response(void *v, long value);
169 /* completion callback with a integer result */
170
171 typedef void disorder_eclient_volume_response(void *v, int l, int r);
172 /* completion callback with a pair of integer results */
173
174 typedef void disorder_eclient_queue_response(void *v, struct queue_entry *q);
175 /* completion callback for queue/recent listing */
176
177 typedef void disorder_eclient_list_response(void *v, int nvec, char **vec);
178 /* completion callback for file listing etc */
179
180 disorder_eclient *disorder_eclient_new(const disorder_eclient_callbacks *cb,
181                                        void *u);
182 /* Create a new client */
183
184 void disorder_eclient_close(disorder_eclient *c);
185 /* Close C */
186
187 unsigned long disorder_eclient_state(const disorder_eclient *c);
188
189 void disorder_eclient_polled(disorder_eclient *c, unsigned mode);
190 /* Should be called when c's FD is readable and/or writable, and in any case
191  * from time to time (so that retries work). */
192
193 int disorder_eclient_version(disorder_eclient *c,
194                              disorder_eclient_string_response *completed,
195                              void *v);
196 /* fetch the server version */
197
198 int disorder_eclient_play(disorder_eclient *c,
199                           const char *track,
200                           disorder_eclient_no_response *completed,
201                           void *v);
202 /* add a track to the queue */
203
204 int disorder_eclient_pause(disorder_eclient *c,
205                            disorder_eclient_no_response *completed,
206                            void *v);
207 /* add a track to the queue */
208
209 int disorder_eclient_resume(disorder_eclient *c,
210                             disorder_eclient_no_response *completed,
211                             void *v);
212 /* add a track to the queue */
213
214 int disorder_eclient_scratch(disorder_eclient *c,
215                              const char *id,
216                              disorder_eclient_no_response *completed,
217                              void *v);
218 /* scratch a track by ID */
219
220 int disorder_eclient_scratch_playing(disorder_eclient *c,
221                                      disorder_eclient_no_response *completed,
222                                      void *v);
223 /* scratch the playing track whatever it is */
224
225 int disorder_eclient_remove(disorder_eclient *c,
226                             const char *id,
227                             disorder_eclient_no_response *completed,
228                             void *v);
229 /* remove a track from the queue */
230
231 int disorder_eclient_moveafter(disorder_eclient *c,
232                                const char *target,
233                                int nids,
234                                const char **ids,
235                                disorder_eclient_no_response *completed,
236                                void *v);
237 /* move tracks within the queue */
238
239 int disorder_eclient_playing(disorder_eclient *c,
240                              disorder_eclient_queue_response *completed,
241                              void *v);
242 /* find the currently playing track (0 for none) */
243
244 int disorder_eclient_queue(disorder_eclient *c,
245                            disorder_eclient_queue_response *completed,
246                            void *v);
247 /* list recently played tracks */
248
249 int disorder_eclient_recent(disorder_eclient *c,
250                             disorder_eclient_queue_response *completed,
251                             void *v);
252 /* list recently played tracks */
253
254 int disorder_eclient_files(disorder_eclient *c,
255                            disorder_eclient_list_response *completed,
256                            const char *dir,
257                            const char *re,
258                            void *v);
259 /* list files in a directory, matching RE if not a null pointer */
260
261 int disorder_eclient_dirs(disorder_eclient *c,
262                           disorder_eclient_list_response *completed,
263                           const char *dir,
264                           const char *re,
265                           void *v);
266 /* list directories in a directory, matching RE if not a null pointer */
267
268 int disorder_eclient_namepart(disorder_eclient *c,
269                               disorder_eclient_string_response *completed,
270                               const char *track,
271                               const char *context,
272                               const char *part,
273                               void *v);
274 /* look up a track name part */
275
276 int disorder_eclient_length(disorder_eclient *c,
277                             disorder_eclient_integer_response *completed,
278                             const char *track,
279                             void *v);
280 /* look up a track name length */
281
282 int disorder_eclient_volume(disorder_eclient *c,
283                             disorder_eclient_volume_response *callback,
284                             int l, int r,
285                             void *v);
286 /* If L and R are both -ve gets the volume.
287  * If neither are -ve then sets the volume.
288  * Otherwise asserts!
289  */
290
291 int disorder_eclient_enable(disorder_eclient *c,
292                             disorder_eclient_no_response *callback,
293                             void *v);
294 int disorder_eclient_disable(disorder_eclient *c,
295                              disorder_eclient_no_response *callback,
296                              void *v);
297 int disorder_eclient_random_enable(disorder_eclient *c,
298                                    disorder_eclient_no_response *callback,
299                                    void *v);
300 int disorder_eclient_random_disable(disorder_eclient *c,
301                                     disorder_eclient_no_response *callback,
302                                     void *v);
303 /* Enable/disable play/random play */
304
305 int disorder_eclient_resolve(disorder_eclient *c,
306                              disorder_eclient_string_response *completed,
307                              const char *track,
308                              void *v);
309 /* Resolve aliases */
310
311 int disorder_eclient_log(disorder_eclient *c,
312                          const disorder_eclient_log_callbacks *callbacks,
313                          void *v);
314 /* Make this a log client (forever - it automatically becomes one again upon
315  * reconnection) */
316
317 int disorder_eclient_get(disorder_eclient *c,
318                          disorder_eclient_string_response *completed,
319                          const char *track, const char *pref,
320                          void *v);
321 int disorder_eclient_set(disorder_eclient *c,
322                          disorder_eclient_no_response *completed,
323                          const char *track, const char *pref, 
324                          const char *value,
325                          void *v);
326 int disorder_eclient_unset(disorder_eclient *c,
327                            disorder_eclient_no_response *completed,
328                            const char *track, const char *pref, 
329                            void *v);
330 /* Get/set preference values */
331
332 int disorder_eclient_search(disorder_eclient *c,
333                             disorder_eclient_list_response *completed,
334                             const char *terms,
335                             void *v);
336
337 int disorder_eclient_nop(disorder_eclient *c,
338                          disorder_eclient_no_response *completed,
339                          void *v);
340
341 int disorder_eclient_new_tracks(disorder_eclient *c,
342                                 disorder_eclient_list_response *completed,
343                                 int max,
344                                 void *v);
345
346 int disorder_eclient_rtp_address(disorder_eclient *c,
347                                  disorder_eclient_list_response *completed,
348                                  void *v);
349
350 int disorder_eclient_users(disorder_eclient *c,
351                            disorder_eclient_list_response *completed,
352                            void *v);
353 int disorder_eclient_deluser(disorder_eclient *c,
354                              disorder_eclient_no_response *completed,
355                              const char *user,
356                              void *v);
357 int disorder_eclient_userinfo(disorder_eclient *c,
358                               disorder_eclient_string_response *completed,
359                               const char *user,
360                               const char *property,
361                               void *v);
362 int disorder_eclient_edituser(disorder_eclient *c,
363                               disorder_eclient_no_response *completed,
364                               const char *user,
365                               const char *property,
366                               const char *value,
367                               void *v);
368 int disorder_eclient_adduser(disorder_eclient *c,
369                              disorder_eclient_no_response *completed,
370                              const char *user,
371                              const char *password,
372                              const char *rights,
373                              void *v);
374
375 #endif
376
377 /*
378 Local Variables:
379 c-basic-offset:2
380 comment-column:40
381 fill-column:79
382 indent-tabs-mode:nil
383 End:
384 */