Commit | Line | Data |
---|---|---|
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 | |
20 | */ | |
460b9539 | 21 | |
22 | #ifndef ECLIENT_H | |
23 | #define ECLIENT_H | |
24 | ||
ad492e00 RK |
25 | #include "rights.h" |
26 | ||
5626f6d2 | 27 | /* Asynchronous client interface */ |
460b9539 | 28 | |
0e4472a0 | 29 | /** @brief Handle type */ |
460b9539 | 30 | typedef struct disorder_eclient disorder_eclient; |
31 | ||
32 | struct queue_entry; | |
33 | ||
0e4472a0 | 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 | |
460b9539 | 39 | |
0e4472a0 | 40 | /** @brief Callbacks for all clients |
41 | * | |
42 | * These must all be valid. | |
43 | */ | |
460b9539 | 44 | typedef struct disorder_eclient_callbacks { |
1f3ce240 | 45 | /** @brief Called when a communication error occurs. |
0e4472a0 | 46 | * @param u from disorder_eclient_new() |
47 | * @param msg error message | |
1f3ce240 RK |
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. | |
0e4472a0 | 52 | */ |
460b9539 | 53 | void (*comms_error)(void *u, const char *msg); |
460b9539 | 54 | |
0e4472a0 | 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 | |
1f3ce240 RK |
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. | |
0e4472a0 | 64 | */ |
460b9539 | 65 | void (*protocol_error)(void *u, void *v, int code, const char *msg); |
0e4472a0 | 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 | */ | |
460b9539 | 75 | void (*poll)(void *u, disorder_eclient *c, int fd, unsigned mode); |
460b9539 | 76 | |
0e4472a0 | 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 | */ | |
460b9539 | 83 | void (*report)(void *u, const char *msg); |
460b9539 | 84 | } disorder_eclient_callbacks; |
85 | ||
0e4472a0 | 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 | */ | |
460b9539 | 93 | typedef struct disorder_eclient_log_callbacks { |
0e4472a0 | 94 | /** @brief Called on (re-)connection */ |
460b9539 | 95 | void (*connected)(void *v); |
58d2aad2 RK |
96 | |
97 | /** @brief Called when @p track finished playing successfully */ | |
460b9539 | 98 | void (*completed)(void *v, const char *track); |
58d2aad2 RK |
99 | |
100 | /** @brief Called when @p track fails for some reason */ | |
460b9539 | 101 | void (*failed)(void *v, const char *track, const char *status); |
58d2aad2 RK |
102 | |
103 | /** @brief Called when @p user moves some track or tracks in the queue | |
104 | * | |
105 | * Fetch the queue again to find out what the new order is - the | |
106 | * rearrangement could in principle be arbitrarily complicated. | |
107 | */ | |
460b9539 | 108 | void (*moved)(void *v, const char *user); |
58d2aad2 RK |
109 | |
110 | /** @brief Called when @p track starts playing | |
111 | * | |
112 | * @p user might be 0. | |
113 | */ | |
460b9539 | 114 | void (*playing)(void *v, const char *track, const char *user/*maybe 0*/); |
58d2aad2 RK |
115 | |
116 | /** @brief Called when @p q is added to the queue | |
117 | * | |
118 | * Fetch the queue again to find out where the in the queue it was added. | |
119 | */ | |
460b9539 | 120 | void (*queue)(void *v, struct queue_entry *q); |
58d2aad2 RK |
121 | |
122 | /** @brief Called when @p q is added to the recent list */ | |
460b9539 | 123 | void (*recent_added)(void *v, struct queue_entry *q); |
58d2aad2 RK |
124 | |
125 | /** @brief Called when @p id is removed from the recent list */ | |
460b9539 | 126 | void (*recent_removed)(void *v, const char *id); |
58d2aad2 | 127 | |
59cf25c4 | 128 | /** @brief Called when @p id is removed from the queue |
58d2aad2 RK |
129 | * |
130 | * @p user might be 0. | |
131 | */ | |
460b9539 | 132 | void (*removed)(void *v, const char *id, const char *user/*maybe 0*/); |
58d2aad2 RK |
133 | |
134 | /** @brief Called when @p track is scratched */ | |
460b9539 | 135 | void (*scratched)(void *v, const char *track, const char *user); |
58d2aad2 RK |
136 | |
137 | /** @brief Called with the current state whenever it changes | |
138 | * | |
139 | * State bits are: | |
140 | * - @ref DISORDER_PLAYING_ENABLED | |
141 | * - @ref DISORDER_RANDOM_ENABLED | |
142 | * - @ref DISORDER_TRACK_PAUSED | |
143 | * - @ref DISORDER_PLAYING | |
144 | * - @ref DISORDER_CONNECTED | |
145 | */ | |
460b9539 | 146 | void (*state)(void *v, unsigned long state); |
58d2aad2 RK |
147 | |
148 | /** @brief Called when the volume changes */ | |
460b9539 | 149 | void (*volume)(void *v, int left, int right); |
58d2aad2 RK |
150 | |
151 | /** @brief Called when a rescan completes */ | |
e025abff | 152 | void (*rescanned)(void *v); |
58d2aad2 RK |
153 | |
154 | /** @brief Called when a user is created (admins only) */ | |
155 | void (*user_add)(void *v, const char *user); | |
156 | ||
157 | /** @brief Called when a user is confirmed (admins only) */ | |
158 | void (*user_confirm)(void *v, const char *user); | |
159 | ||
160 | /** @brief Called when a user is deleted (admins only) */ | |
161 | void (*user_delete)(void *v, const char *user); | |
162 | ||
163 | /** @brief Called when a user is edited (admins only) */ | |
164 | void (*user_edit)(void *v, const char *user, const char *property); | |
ad492e00 RK |
165 | |
166 | /** @brief Called when your rights change */ | |
167 | void (*rights_changed)(void *v, rights_type new_rights); | |
9433fabf RK |
168 | |
169 | /** @brief Called when a track is adopted */ | |
170 | void (*adopted)(void *v, const char *id, const char *who); | |
812b526d | 171 | |
5c102112 RK |
172 | /** @brief Called when a new playlist is created */ |
173 | void (*playlist_created)(void *v, const char *playlist, const char *sharing); | |
174 | ||
175 | /** @brief Called when a playlist is modified */ | |
176 | void (*playlist_modified)(void *v, const char *playlist, const char *sharing); | |
177 | ||
178 | /** @brief Called when a new playlist is deleted */ | |
179 | void (*playlist_deleted)(void *v, const char *playlist); | |
ad0c0f36 RK |
180 | |
181 | /** @brief Called when a global pref is changed or delete */ | |
182 | void (*global_pref)(void *v, const char *pref, const char *value/*or NULL*/); | |
460b9539 | 183 | } disorder_eclient_log_callbacks; |
184 | ||
185 | /* State bits */ | |
0e4472a0 | 186 | |
187 | /** @brief Play is enabled */ | |
188 | #define DISORDER_PLAYING_ENABLED 0x00000001 | |
189 | ||
190 | /** @brief Random play is enabled */ | |
191 | #define DISORDER_RANDOM_ENABLED 0x00000002 | |
192 | ||
8f763f1b RK |
193 | /** @brief Track is paused |
194 | * | |
195 | * This is only meaningful if @ref DISORDER_PLAYING is set | |
196 | */ | |
0e4472a0 | 197 | #define DISORDER_TRACK_PAUSED 0x00000004 |
460b9539 | 198 | |
8f763f1b RK |
199 | /** @brief Track is playing |
200 | * | |
201 | * This can be set even if the current track is paused (in which case @ref | |
202 | * DISORDER_TRACK_PAUSED) will also be set. | |
203 | */ | |
204 | #define DISORDER_PLAYING 0x00000008 | |
205 | ||
206 | /** @brief Connected to server | |
207 | * | |
208 | * By connected it is meant that commands have a reasonable chance of being | |
209 | * processed soon, not merely that a TCP connection exists - for instance if | |
210 | * the client is still authenticating then that does not count as connected. | |
211 | */ | |
212 | #define DISORDER_CONNECTED 0x00000010 | |
213 | ||
00959300 RK |
214 | char *disorder_eclient_interpret_state(unsigned long statebits); |
215 | ||
460b9539 | 216 | struct queue_entry; |
217 | struct kvp; | |
218 | struct sink; | |
219 | ||
220 | /* Completion callbacks. These provide the result of operations to the caller. | |
699517af | 221 | * Unlike in earlier releases, these are not allowed to be NULL. */ |
460b9539 | 222 | |
53fa91bb RK |
223 | /** @brief Trivial completion callback |
224 | * @param v User data | |
e2717131 | 225 | * @param err Error string or NULL on succes |
53fa91bb RK |
226 | */ |
227 | typedef void disorder_eclient_no_response(void *v, | |
e2717131 | 228 | const char *err); |
460b9539 | 229 | |
1bd1b63c RK |
230 | /** @brief String result completion callback |
231 | * @param v User data | |
e2717131 | 232 | * @param err Error string or NULL on succes |
658c8a35 | 233 | * @param value Result or NULL |
1bd1b63c | 234 | * |
06bfbba4 RK |
235 | * @p error will be NULL on success. In this case @p value will be the result |
236 | * (which might be NULL for disorder_eclient_get(), | |
53740beb RK |
237 | * disorder_eclient_get_global(), disorder_eclient_userinfo() and |
238 | * disorder_eclient_playlist_get_share()). | |
06bfbba4 RK |
239 | * |
240 | * @p error will be non-NULL on failure. In this case @p value is always NULL. | |
1bd1b63c | 241 | */ |
06bfbba4 | 242 | typedef void disorder_eclient_string_response(void *v, |
e2717131 | 243 | const char *err, |
06bfbba4 | 244 | const char *value); |
460b9539 | 245 | |
3a8d0ea1 | 246 | /** @brief Integer result completion callback |
658c8a35 | 247 | * @param v User data |
e2717131 | 248 | * @param err Error string or NULL on succes |
658c8a35 RK |
249 | * @param value Result or 0 |
250 | * | |
251 | * @p error will be NULL on success. In this case @p value will be the result. | |
252 | * | |
253 | * @p error will be non-NULL on failure. In this case @p value is always 0. | |
254 | */ | |
255 | typedef void disorder_eclient_integer_response(void *v, | |
e2717131 | 256 | const char *err, |
658c8a35 | 257 | long value); |
699517af RK |
258 | /** @brief Volume completion callback |
259 | * @param v User data | |
e2717131 | 260 | * @param err Error string or NULL on success |
699517af RK |
261 | * @param l Left channel volume |
262 | * @param r Right channel volume | |
263 | * | |
264 | * @p error will be NULL on success. In this case @p l and @p r will be the | |
265 | * result. | |
266 | * | |
267 | * @p error will be non-NULL on failure. In this case @p l and @p r are always | |
268 | * 0. | |
269 | */ | |
ad131c25 RK |
270 | typedef void disorder_eclient_pair_integer_response(void *v, |
271 | const char *err, | |
272 | long l, long r); | |
460b9539 | 273 | |
3035257f RK |
274 | /** @brief Queue request completion callback |
275 | * @param v User data | |
e2717131 | 276 | * @param err Error string or NULL on success |
3035257f RK |
277 | * @param q Head of queue data list |
278 | * | |
4190a4d9 RK |
279 | * @p error will be NULL on success. In this case @p q will be the (head of |
280 | * the) result. | |
3035257f RK |
281 | * |
282 | * @p error will be non-NULL on failure. In this case @p q may be NULL but | |
283 | * MIGHT also be some subset of the queue. For consistent behavior it should | |
284 | * be ignored in the error case. | |
285 | */ | |
286 | typedef void disorder_eclient_queue_response(void *v, | |
e2717131 | 287 | const char *err, |
3035257f | 288 | struct queue_entry *q); |
460b9539 | 289 | |
ad131c25 RK |
290 | #define disorder_eclient_playing_response disorder_eclient_queue_response |
291 | ||
4190a4d9 RK |
292 | /** @brief List request completion callback |
293 | * @param v User data | |
e2717131 | 294 | * @param err Error string or NULL on success |
4190a4d9 RK |
295 | * @param nvec Number of elements in response list |
296 | * @param vec Pointer to response list | |
297 | * | |
298 | * @p error will be NULL on success. In this case @p nvec and @p vec will give | |
53740beb RK |
299 | * the result, or be -1 and NULL respectively e.g. from |
300 | * disorder_eclient_playlist_get() if there is no such playlist. | |
4190a4d9 RK |
301 | * |
302 | * @p error will be non-NULL on failure. In this case @p nvec and @p vec will | |
303 | * be 0 and NULL. | |
304 | */ | |
305 | typedef void disorder_eclient_list_response(void *v, | |
e2717131 | 306 | const char *err, |
4190a4d9 | 307 | int nvec, char **vec); |
460b9539 | 308 | |
309 | disorder_eclient *disorder_eclient_new(const disorder_eclient_callbacks *cb, | |
310 | void *u); | |
311 | /* Create a new client */ | |
312 | ||
313 | void disorder_eclient_close(disorder_eclient *c); | |
314 | /* Close C */ | |
315 | ||
8f763f1b | 316 | unsigned long disorder_eclient_state(const disorder_eclient *c); |
ad58ebcc | 317 | |
460b9539 | 318 | void disorder_eclient_polled(disorder_eclient *c, unsigned mode); |
319 | /* Should be called when c's FD is readable and/or writable, and in any case | |
320 | * from time to time (so that retries work). */ | |
321 | ||
460b9539 | 322 | int disorder_eclient_scratch_playing(disorder_eclient *c, |
323 | disorder_eclient_no_response *completed, | |
324 | void *v); | |
325 | /* scratch the playing track whatever it is */ | |
326 | ||
460b9539 | 327 | int disorder_eclient_log(disorder_eclient *c, |
328 | const disorder_eclient_log_callbacks *callbacks, | |
329 | void *v); | |
330 | /* Make this a log client (forever - it automatically becomes one again upon | |
331 | * reconnection) */ | |
332 | ||
a99c4e9a RK |
333 | int disorder_eclient_rtp_address(disorder_eclient *c, |
334 | disorder_eclient_list_response *completed, | |
335 | void *v); | |
336 | ||
68210888 RK |
337 | void disorder_eclient_enable_connect(disorder_eclient *c); |
338 | void disorder_eclient_disable_connect(disorder_eclient *c); | |
ad131c25 RK |
339 | |
340 | #include "eclient-stubs.h" | |
53740beb | 341 | |
460b9539 | 342 | #endif |
343 | ||
344 | /* | |
345 | Local Variables: | |
346 | c-basic-offset:2 | |
347 | comment-column:40 | |
348 | fill-column:79 | |
349 | indent-tabs-mode:nil | |
350 | End: | |
351 | */ |