output: add _get_plugin()
[libmpdclient.git] / src / queue.c
blob0c8ed7158292f35ddcde5af2e8ff56b097ec7966
1 /* libmpdclient
2 (c) 2003-2017 The Music Player Daemon Project
3 This project's homepage is: http://www.musicpd.org
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
9 - Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
12 - Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
20 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <mpd/queue.h>
30 #include <mpd/send.h>
31 #include <mpd/recv.h>
32 #include <mpd/pair.h>
33 #include <mpd/response.h>
34 #include <mpd/song.h>
35 #include "internal.h"
36 #include "isend.h"
37 #include "run.h"
39 #include <limits.h>
40 #include <stdio.h>
41 #include <stdlib.h>
43 bool
44 mpd_send_list_queue_meta(struct mpd_connection *connection)
46 return mpd_send_command(connection, "playlistinfo", NULL);
49 bool
50 mpd_send_list_queue_range_meta(struct mpd_connection *connection,
51 unsigned start, unsigned end)
53 return mpd_send_range_command(connection, "playlistinfo", start, end);
56 bool
57 mpd_send_get_queue_song_pos(struct mpd_connection *connection, unsigned pos)
59 return mpd_send_int_command(connection, "playlistinfo", pos);
62 struct mpd_song *
63 mpd_run_get_queue_song_pos(struct mpd_connection *connection, unsigned pos)
65 struct mpd_song *song;
67 if (!mpd_run_check(connection) ||
68 !mpd_send_get_queue_song_pos(connection, pos))
69 return NULL;
71 song = mpd_recv_song(connection);
72 if (!mpd_response_finish(connection) && song != NULL) {
73 mpd_song_free(song);
74 return NULL;
77 return song;
81 bool
82 mpd_send_get_queue_song_id(struct mpd_connection *connection, unsigned id)
84 return mpd_send_int_command(connection, "playlistid", id);
87 struct mpd_song *
88 mpd_run_get_queue_song_id(struct mpd_connection *connection, unsigned id)
90 struct mpd_song *song;
92 if (!mpd_run_check(connection) ||
93 !mpd_send_get_queue_song_id(connection, id))
94 return NULL;
96 song = mpd_recv_song(connection);
97 if (!mpd_response_finish(connection) && song != NULL) {
98 mpd_song_free(song);
99 return NULL;
102 return song;
106 bool
107 mpd_send_queue_changes_meta(struct mpd_connection *connection,
108 unsigned version)
110 return mpd_send_ll_command(connection, "plchanges", version);
113 bool
114 mpd_send_queue_changes_meta_range(struct mpd_connection *connection,
115 unsigned version,
116 unsigned start, unsigned end)
118 return mpd_send_u_range_command(connection, "plchanges",
119 version, start, end);
122 bool
123 mpd_send_queue_changes_brief(struct mpd_connection *connection,
124 unsigned version)
126 return mpd_send_ll_command(connection, "plchangesposid", version);
129 bool
130 mpd_send_queue_changes_brief_range(struct mpd_connection *connection,
131 unsigned version,
132 unsigned start, unsigned end)
134 return mpd_send_u_range_command(connection, "plchangesposid",
135 version, start, end);
138 bool
139 mpd_recv_queue_change_brief(struct mpd_connection *connection,
140 unsigned *position_r, unsigned *id_r)
142 struct mpd_pair *pair;
144 pair = mpd_recv_pair_named(connection, "cpos");
145 if (pair == NULL)
146 return false;
148 *position_r = atoi(pair->value);
149 mpd_return_pair(connection, pair);
151 pair = mpd_recv_pair_named(connection, "Id");
152 if (pair == NULL) {
153 mpd_return_pair(connection, pair);
155 if (!mpd_error_is_defined(&connection->error)) {
156 mpd_error_code(&connection->error,
157 MPD_ERROR_MALFORMED);
158 mpd_error_message(&connection->error,
159 "No id received");
162 return false;
165 *id_r = atoi(pair->value);
166 mpd_return_pair(connection, pair);
168 return !mpd_error_is_defined(&connection->error);
171 bool
172 mpd_send_add(struct mpd_connection *connection, const char *file)
174 return mpd_send_command(connection, "add", file, NULL);
177 bool
178 mpd_run_add(struct mpd_connection *connection, const char *uri)
180 return mpd_run_check(connection) &&
181 mpd_send_add(connection, uri) &&
182 mpd_response_finish(connection);
185 bool
186 mpd_send_add_id(struct mpd_connection *connection, const char *file)
188 return mpd_send_command(connection, "addid", file, NULL);
191 bool
192 mpd_send_add_id_to(struct mpd_connection *connection, const char *uri,
193 unsigned to)
195 return mpd_send_s_u_command(connection, "addid", uri, to);
199 mpd_recv_song_id(struct mpd_connection *connection)
201 struct mpd_pair *pair;
202 int id = -1;
204 pair = mpd_recv_pair_named(connection, "Id");
205 if (pair != NULL) {
206 id = atoi(pair->value);
207 mpd_return_pair(connection, pair);
210 return id;
214 mpd_run_add_id(struct mpd_connection *connection, const char *file)
216 int id;
218 if (!mpd_run_check(connection) ||
219 !mpd_send_add_id(connection, file))
220 return -1;
222 id = mpd_recv_song_id(connection);
224 if (!mpd_response_finish(connection))
225 id = -1;
227 return id;
231 mpd_run_add_id_to(struct mpd_connection *connection, const char *uri,
232 unsigned to)
234 int id;
236 if (!mpd_run_check(connection) ||
237 !mpd_send_add_id_to(connection, uri, to))
238 return -1;
240 id = mpd_recv_song_id(connection);
242 if (!mpd_response_finish(connection))
243 id = -1;
245 return id;
248 bool
249 mpd_send_delete(struct mpd_connection *connection, unsigned pos)
251 return mpd_send_int_command(connection, "delete", pos);
254 bool
255 mpd_run_delete(struct mpd_connection *connection, unsigned pos)
257 return mpd_run_check(connection) &&
258 mpd_send_delete(connection, pos) &&
259 mpd_response_finish(connection);
262 bool
263 mpd_send_delete_range(struct mpd_connection *connection,
264 unsigned start, unsigned end)
266 return mpd_send_range_command(connection, "delete", start, end);
269 bool
270 mpd_run_delete_range(struct mpd_connection *connection,
271 unsigned start, unsigned end)
273 return mpd_run_check(connection) &&
274 mpd_send_delete_range(connection, start, end) &&
275 mpd_response_finish(connection);
278 bool
279 mpd_send_delete_id(struct mpd_connection *connection, unsigned id)
281 return mpd_send_int_command(connection, "deleteid", id);
284 bool
285 mpd_run_delete_id(struct mpd_connection *connection, unsigned id)
287 return mpd_run_check(connection) &&
288 mpd_send_delete_id(connection, id) &&
289 mpd_response_finish(connection);
292 bool
293 mpd_send_shuffle(struct mpd_connection *connection)
295 return mpd_send_command(connection, "shuffle", NULL);
298 bool
299 mpd_run_shuffle(struct mpd_connection *connection)
301 return mpd_run_check(connection) &&
302 mpd_send_shuffle(connection) &&
303 mpd_response_finish(connection);
306 bool
307 mpd_send_shuffle_range(struct mpd_connection *connection, unsigned start, unsigned end)
309 return mpd_send_range_command(connection, "shuffle", start, end);
312 bool
313 mpd_run_shuffle_range(struct mpd_connection *connection,
314 unsigned start, unsigned end)
316 return mpd_run_check(connection) &&
317 mpd_send_shuffle_range(connection, start, end) &&
318 mpd_response_finish(connection);
321 bool
322 mpd_send_clear(struct mpd_connection *connection)
324 return mpd_send_command(connection, "clear", NULL);
327 bool
328 mpd_run_clear(struct mpd_connection *connection)
330 return mpd_run_check(connection) &&
331 mpd_send_clear(connection) &&
332 mpd_response_finish(connection);
335 bool
336 mpd_send_move(struct mpd_connection *connection, unsigned from, unsigned to)
338 return mpd_send_int2_command(connection, "move", from, to);
341 bool
342 mpd_run_move(struct mpd_connection *connection, unsigned from, unsigned to)
344 return mpd_run_check(connection) &&
345 mpd_send_move(connection, from, to) &&
346 mpd_response_finish(connection);
349 bool
350 mpd_send_move_id(struct mpd_connection *connection, unsigned from, unsigned to)
352 return mpd_send_int2_command(connection, "moveid", from, to);
355 bool
356 mpd_run_move_id(struct mpd_connection *connection, unsigned from, unsigned to)
358 return mpd_run_check(connection) &&
359 mpd_send_move_id(connection, from, to) &&
360 mpd_response_finish(connection);
363 bool
364 mpd_send_move_range(struct mpd_connection *connection,
365 unsigned start, unsigned end, unsigned to)
367 return mpd_send_range_u_command(connection, "move", start, end, to);
370 bool
371 mpd_run_move_range(struct mpd_connection *connection,
372 unsigned start, unsigned end, unsigned to)
374 return mpd_run_check(connection) &&
375 mpd_send_move_range(connection, start, end, to) &&
376 mpd_response_finish(connection);
379 bool
380 mpd_send_swap(struct mpd_connection *connection, unsigned pos1, unsigned pos2)
382 return mpd_send_int2_command(connection, "swap", pos1, pos2);
385 bool
386 mpd_run_swap(struct mpd_connection *connection, unsigned pos1, unsigned pos2)
388 return mpd_run_check(connection) &&
389 mpd_send_swap(connection, pos1, pos2) &&
390 mpd_response_finish(connection);
393 bool
394 mpd_send_swap_id(struct mpd_connection *connection, unsigned id1, unsigned id2)
396 return mpd_send_int2_command(connection, "swapid", id1, id2);
399 bool
400 mpd_run_swap_id(struct mpd_connection *connection, unsigned id1, unsigned id2)
402 return mpd_run_check(connection) &&
403 mpd_send_swap_id(connection, id1, id2) &&
404 mpd_response_finish(connection);
407 bool
408 mpd_send_add_tag_id(struct mpd_connection *connection, unsigned id,
409 enum mpd_tag_type tag, const char *value)
411 return mpd_send_u_s_s_command(connection, "addtagid",
412 id, mpd_tag_name(tag), value);
415 bool
416 mpd_run_add_tag_id(struct mpd_connection *connection, unsigned id,
417 enum mpd_tag_type tag, const char *value)
419 return mpd_run_check(connection) &&
420 mpd_send_add_tag_id(connection, id, tag, value) &&
421 mpd_response_finish(connection);
424 bool
425 mpd_send_clear_tag_id(struct mpd_connection *connection, unsigned id,
426 enum mpd_tag_type tag)
428 return mpd_send_u_s_command(connection, "cleartagid",
429 id, mpd_tag_name(tag));
432 bool
433 mpd_run_clear_tag_id(struct mpd_connection *connection, unsigned id,
434 enum mpd_tag_type tag)
436 return mpd_run_check(connection) &&
437 mpd_send_clear_tag_id(connection, id, tag) &&
438 mpd_response_finish(connection);
441 bool
442 mpd_send_clear_all_tags_id(struct mpd_connection *connection, unsigned id)
444 return mpd_send_int_command(connection, "cleartagid", id);
447 bool
448 mpd_run_clear_all_tags_id(struct mpd_connection *connection, unsigned id)
450 return mpd_run_check(connection) &&
451 mpd_send_clear_all_tags_id(connection, id) &&
452 mpd_response_finish(connection);
455 bool
456 mpd_send_prio(struct mpd_connection *connection, int priority,
457 unsigned position)
459 return mpd_send_int2_command(connection, "prio", priority, position);
462 bool
463 mpd_run_prio(struct mpd_connection *connection, int priority,
464 unsigned position)
466 return mpd_run_check(connection) &&
467 mpd_send_prio(connection, priority, position) &&
468 mpd_response_finish(connection);
471 bool
472 mpd_send_prio_range(struct mpd_connection *connection, int priority,
473 unsigned start, unsigned end)
475 return mpd_send_i_range_command(connection, "prio", priority,
476 start, end);
479 bool
480 mpd_run_prio_range(struct mpd_connection *connection, int priority,
481 unsigned start, unsigned end)
483 return mpd_run_check(connection) &&
484 mpd_send_prio_range(connection, priority, start, end) &&
485 mpd_response_finish(connection);
488 bool
489 mpd_send_prio_id(struct mpd_connection *connection, int priority,
490 unsigned id)
492 return mpd_send_int2_command(connection, "prioid", priority, id);
495 bool
496 mpd_run_prio_id(struct mpd_connection *connection, int priority,
497 unsigned id)
499 return mpd_run_check(connection) &&
500 mpd_send_prio_id(connection, priority, id) &&
501 mpd_response_finish(connection);