chiark / gitweb /
compute Sentence_Break as well
[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 (e.g. connected refused) occurs.
46    * @param u from disorder_eclient_new()
47    * @param msg error message
48    */
49   void (*comms_error)(void *u, const char *msg);
50   
51   /** @brief Called when a command fails (including initial authorization).
52    * @param u from disorder_eclient_new()
53    * @param v from failed command, or NULL if during setup
54    * @param msg error message
55    */
56   void (*protocol_error)(void *u, void *v, int code, const char *msg);
57
58   /** @brief Set poll/select flags
59    * @param u from disorder_eclient_new()
60    * @param c handle
61    * @param fd file descriptor
62    * @param mode bitmap (@ref DISORDER_POLL_READ and/or @ref DISORDER_POLL_WRITE)
63    *
64    * Before @p fd is closed you will always get a call with @p mode = 0.
65    */
66   void (*poll)(void *u, disorder_eclient *c, int fd, unsigned mode);
67
68   /** @brief Report current activity
69    * @param u from disorder_eclient_new()
70    * @param msg Current activity or NULL
71    *
72    * Called with @p msg = NULL when there's nothing going on.
73    */
74   void (*report)(void *u, const char *msg);
75 } disorder_eclient_callbacks;
76
77 /** @brief Callbacks for log clients
78  *
79  * All of these are allowed to be a null pointers in which case you don't get
80  * told about that log event.
81  *
82  * See disorder_protocol(5) for full documentation.
83  */
84 typedef struct disorder_eclient_log_callbacks {
85   /** @brief Called on (re-)connection */
86   void (*connected)(void *v);
87   
88   void (*completed)(void *v, const char *track);
89   void (*failed)(void *v, const char *track, const char *status);
90   void (*moved)(void *v, const char *user);
91   void (*playing)(void *v, const char *track, const char *user/*maybe 0*/);
92   void (*queue)(void *v, struct queue_entry *q);
93   void (*recent_added)(void *v, struct queue_entry *q);
94   void (*recent_removed)(void *v, const char *id);
95   void (*removed)(void *v, const char *id, const char *user/*maybe 0*/);
96   void (*scratched)(void *v, const char *track, const char *user);
97   void (*state)(void *v, unsigned long state);
98   void (*volume)(void *v, int left, int right);
99   void (*rescanned)(void *v);
100 } disorder_eclient_log_callbacks;
101
102 /* State bits */
103
104 /** @brief Play is enabled */
105 #define DISORDER_PLAYING_ENABLED  0x00000001
106
107 /** @brief Random play is enabled */
108 #define DISORDER_RANDOM_ENABLED   0x00000002
109
110 /** @brief Track is paused
111  *
112  * This is only meaningful if @ref DISORDER_PLAYING is set
113  */
114 #define DISORDER_TRACK_PAUSED     0x00000004
115
116 /** @brief Track is playing
117  *
118  * This can be set even if the current track is paused (in which case @ref
119  * DISORDER_TRACK_PAUSED) will also be set.
120  */
121 #define DISORDER_PLAYING    0x00000008
122
123 /** @brief Connected to server
124  *
125  * By connected it is meant that commands have a reasonable chance of being
126  * processed soon, not merely that a TCP connection exists - for instance if
127  * the client is still authenticating then that does not count as connected.
128  */
129 #define DISORDER_CONNECTED        0x00000010
130
131 char *disorder_eclient_interpret_state(unsigned long statebits);
132
133 struct queue_entry;
134 struct kvp;
135 struct sink;
136
137 /* Completion callbacks.  These provide the result of operations to the caller.
138  * It is always allowed for these to be null pointers if you don't care about
139  * the result. */
140
141 typedef void disorder_eclient_no_response(void *v);
142 /* completion callback with no data */
143
144 typedef void disorder_eclient_string_response(void *v, const char *value);
145 /* completion callback with a string result */
146
147 typedef void disorder_eclient_integer_response(void *v, long value);
148 /* completion callback with a integer result */
149
150 typedef void disorder_eclient_volume_response(void *v, int l, int r);
151 /* completion callback with a pair of integer results */
152
153 typedef void disorder_eclient_queue_response(void *v, struct queue_entry *q);
154 /* completion callback for queue/recent listing */
155
156 typedef void disorder_eclient_list_response(void *v, int nvec, char **vec);
157 /* completion callback for file listing etc */
158
159 disorder_eclient *disorder_eclient_new(const disorder_eclient_callbacks *cb,
160                                        void *u);
161 /* Create a new client */
162
163 void disorder_eclient_close(disorder_eclient *c);
164 /* Close C */
165
166 unsigned long disorder_eclient_state(const disorder_eclient *c);
167
168 void disorder_eclient_polled(disorder_eclient *c, unsigned mode);
169 /* Should be called when c's FD is readable and/or writable, and in any case
170  * from time to time (so that retries work). */
171
172 int disorder_eclient_version(disorder_eclient *c,
173                              disorder_eclient_string_response *completed,
174                              void *v);
175 /* fetch the server version */
176
177 int disorder_eclient_play(disorder_eclient *c,
178                           const char *track,
179                           disorder_eclient_no_response *completed,
180                           void *v);
181 /* add a track to the queue */
182
183 int disorder_eclient_pause(disorder_eclient *c,
184                            disorder_eclient_no_response *completed,
185                            void *v);
186 /* add a track to the queue */
187
188 int disorder_eclient_resume(disorder_eclient *c,
189                             disorder_eclient_no_response *completed,
190                             void *v);
191 /* add a track to the queue */
192
193 int disorder_eclient_scratch(disorder_eclient *c,
194                              const char *id,
195                              disorder_eclient_no_response *completed,
196                              void *v);
197 /* scratch a track by ID */
198
199 int disorder_eclient_scratch_playing(disorder_eclient *c,
200                                      disorder_eclient_no_response *completed,
201                                      void *v);
202 /* scratch the playing track whatever it is */
203
204 int disorder_eclient_remove(disorder_eclient *c,
205                             const char *id,
206                             disorder_eclient_no_response *completed,
207                             void *v);
208 /* remove a track from the queue */
209
210 int disorder_eclient_moveafter(disorder_eclient *c,
211                                const char *target,
212                                int nids,
213                                const char **ids,
214                                disorder_eclient_no_response *completed,
215                                void *v);
216 /* move tracks within the queue */
217
218 int disorder_eclient_playing(disorder_eclient *c,
219                              disorder_eclient_queue_response *completed,
220                              void *v);
221 /* find the currently playing track (0 for none) */
222
223 int disorder_eclient_queue(disorder_eclient *c,
224                            disorder_eclient_queue_response *completed,
225                            void *v);
226 /* list recently played tracks */
227
228 int disorder_eclient_recent(disorder_eclient *c,
229                             disorder_eclient_queue_response *completed,
230                             void *v);
231 /* list recently played tracks */
232
233 int disorder_eclient_files(disorder_eclient *c,
234                            disorder_eclient_list_response *completed,
235                            const char *dir,
236                            const char *re,
237                            void *v);
238 /* list files in a directory, matching RE if not a null pointer */
239
240 int disorder_eclient_dirs(disorder_eclient *c,
241                           disorder_eclient_list_response *completed,
242                           const char *dir,
243                           const char *re,
244                           void *v);
245 /* list directories in a directory, matching RE if not a null pointer */
246
247 int disorder_eclient_namepart(disorder_eclient *c,
248                               disorder_eclient_string_response *completed,
249                               const char *track,
250                               const char *context,
251                               const char *part,
252                               void *v);
253 /* look up a track name part */
254
255 int disorder_eclient_length(disorder_eclient *c,
256                             disorder_eclient_integer_response *completed,
257                             const char *track,
258                             void *v);
259 /* look up a track name length */
260
261 int disorder_eclient_volume(disorder_eclient *c,
262                             disorder_eclient_volume_response *callback,
263                             int l, int r,
264                             void *v);
265 /* If L and R are both -ve gets the volume.
266  * If neither are -ve then sets the volume.
267  * Otherwise asserts!
268  */
269
270 int disorder_eclient_enable(disorder_eclient *c,
271                             disorder_eclient_no_response *callback,
272                             void *v);
273 int disorder_eclient_disable(disorder_eclient *c,
274                              disorder_eclient_no_response *callback,
275                              void *v);
276 int disorder_eclient_random_enable(disorder_eclient *c,
277                                    disorder_eclient_no_response *callback,
278                                    void *v);
279 int disorder_eclient_random_disable(disorder_eclient *c,
280                                     disorder_eclient_no_response *callback,
281                                     void *v);
282 /* Enable/disable play/random play */
283
284 int disorder_eclient_resolve(disorder_eclient *c,
285                              disorder_eclient_string_response *completed,
286                              const char *track,
287                              void *v);
288 /* Resolve aliases */
289
290 int disorder_eclient_log(disorder_eclient *c,
291                          const disorder_eclient_log_callbacks *callbacks,
292                          void *v);
293 /* Make this a log client (forever - it automatically becomes one again upon
294  * reconnection) */
295
296 int disorder_eclient_get(disorder_eclient *c,
297                          disorder_eclient_string_response *completed,
298                          const char *track, const char *pref,
299                          void *v);
300 int disorder_eclient_set(disorder_eclient *c,
301                          disorder_eclient_no_response *completed,
302                          const char *track, const char *pref, 
303                          const char *value,
304                          void *v);
305 int disorder_eclient_unset(disorder_eclient *c,
306                            disorder_eclient_no_response *completed,
307                            const char *track, const char *pref, 
308                            void *v);
309 /* Get/set preference values */
310
311 int disorder_eclient_search(disorder_eclient *c,
312                             disorder_eclient_list_response *completed,
313                             const char *terms,
314                             void *v);
315
316 int disorder_eclient_nop(disorder_eclient *c,
317                          disorder_eclient_no_response *completed,
318                          void *v);
319
320 int disorder_eclient_new_tracks(disorder_eclient *c,
321                                 disorder_eclient_list_response *completed,
322                                 int max,
323                                 void *v);
324
325 int disorder_eclient_rtp_address(disorder_eclient *c,
326                                  disorder_eclient_list_response *completed,
327                                  void *v);
328
329 #endif
330
331 /*
332 Local Variables:
333 c-basic-offset:2
334 comment-column:40
335 fill-column:79
336 indent-tabs-mode:nil
337 End:
338 */