1 /* libmpd (high level libmpdclient library)
2 * Copyright (C) 2004-2009 Qball Cow <qball@sarine.nl>
3 * Project homepage: http://gmpcwiki.sarine.nl/
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,
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
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #define __USE_GNU
23
24 #include <string.h>
25 #include <stdarg.h>
26 #include <config.h>
27 #include "debug_printf.h"
28 #include "libmpd.h"
29 #include "libmpd-internal.h"
30
mpd_player_get_state(MpdObj * mi)31 int mpd_player_get_state(MpdObj * mi)
32 {
33 if (!mpd_check_connected(mi)) {
34 debug_printf(DEBUG_WARNING, "not connected\n");
35 return MPD_NOT_CONNECTED;
36 }
37 if (mpd_status_check(mi) != MPD_OK) {
38 debug_printf(DEBUG_WARNING, "Failed to get status\n");
39 return MPD_STATUS_FAILED;
40 }
41 return mi->status->state;
42 }
mpd_player_get_next_song_id(MpdObj * mi)43 int mpd_player_get_next_song_id(MpdObj *mi)
44 {
45 if (!mpd_check_connected(mi)) {
46 debug_printf(DEBUG_WARNING, "not connected\n");
47 return MPD_NOT_CONNECTED;
48 }
49 if (mpd_status_check(mi) != MPD_OK) {
50 debug_printf(DEBUG_ERROR, "Failed to get status\n");
51 return MPD_STATUS_FAILED;
52 }
53 /* check if in valid state */
54 if (mpd_player_get_state(mi) != MPD_PLAYER_PLAY &&
55 mpd_player_get_state(mi) != MPD_PLAYER_PAUSE) {
56 return MPD_PLAYER_NOT_PLAYING;
57 }
58 /* just to be sure check */
59 if (!mi->status->playlistLength) {
60 return MPD_PLAYLIST_EMPTY;
61 }
62 return mi->status->nextsongid;
63 }
mpd_player_get_next_song_pos(MpdObj * mi)64 int mpd_player_get_next_song_pos(MpdObj *mi)
65 {
66 if (!mpd_check_connected(mi)) {
67 debug_printf(DEBUG_WARNING, "not connected\n");
68 return MPD_NOT_CONNECTED;
69 }
70 if (mpd_status_check(mi) != MPD_OK) {
71 debug_printf(DEBUG_ERROR, "Failed to get status\n");
72 return MPD_STATUS_FAILED;
73 }
74 /* check if in valid state */
75 if (mpd_player_get_state(mi) != MPD_PLAYER_PLAY &&
76 mpd_player_get_state(mi) != MPD_PLAYER_PAUSE) {
77 return MPD_PLAYER_NOT_PLAYING;
78 }
79 /* just to be sure check */
80 if (!mi->status->playlistLength) {
81 return MPD_PLAYLIST_EMPTY;
82 }
83 return mi->status->nextsong;
84 }
mpd_player_get_current_song_id(MpdObj * mi)85 int mpd_player_get_current_song_id(MpdObj * mi)
86 {
87 if (!mpd_check_connected(mi)) {
88 debug_printf(DEBUG_WARNING, "not connected\n");
89 return MPD_NOT_CONNECTED;
90 }
91 if (mpd_status_check(mi) != MPD_OK) {
92 debug_printf(DEBUG_ERROR, "Failed to get status\n");
93 return MPD_STATUS_FAILED;
94 }
95 /* check if in valid state */
96 if (mpd_player_get_state(mi) != MPD_PLAYER_PLAY &&
97 mpd_player_get_state(mi) != MPD_PLAYER_PAUSE) {
98 return MPD_PLAYER_NOT_PLAYING;
99 }
100 /* just to be sure check */
101 if (!mi->status->playlistLength) {
102 return MPD_PLAYLIST_EMPTY;
103 }
104 return mi->status->songid;
105 }
106
mpd_player_get_current_song_pos(MpdObj * mi)107 int mpd_player_get_current_song_pos(MpdObj * mi)
108 {
109 if (!mpd_check_connected(mi)) {
110 debug_printf(DEBUG_WARNING, "not connected\n");
111 return MPD_NOT_CONNECTED;
112 }
113 if (mpd_status_check(mi)!= MPD_OK) {
114 debug_printf(DEBUG_ERROR, "Failed to get status\n");
115 return MPD_STATUS_FAILED;
116 }
117 /* check if in valid state */
118 if (mpd_player_get_state(mi) != MPD_PLAYER_PLAY &&
119 mpd_player_get_state(mi) != MPD_PLAYER_PAUSE) {
120 return MPD_PLAYER_NOT_PLAYING;
121 }
122 /* just to be sure check */
123 if (!mi->status->playlistLength) {
124 return MPD_PLAYLIST_EMPTY;
125 }
126 return mi->status->song;
127 }
128
mpd_player_play_id(MpdObj * mi,int id)129 int mpd_player_play_id(MpdObj * mi, int id)
130 {
131 debug_printf(DEBUG_INFO, "trying to play id: %i\n", id);
132 if (!mpd_check_connected(mi)) {
133 debug_printf(DEBUG_WARNING, "not connected\n");
134 return MPD_NOT_CONNECTED;
135 }
136 if (mpd_lock_conn(mi)) {
137 debug_printf(DEBUG_WARNING, "lock failed\n");
138 return MPD_LOCK_FAILED;
139 }
140
141 mpd_sendPlayIdCommand(mi->connection, id);
142 mpd_finishCommand(mi->connection);
143
144
145 mpd_unlock_conn(mi);
146 if (mpd_status_update(mi)) {
147 return MPD_STATUS_FAILED;
148 }
149 return MPD_OK;
150 }
151
mpd_player_play(MpdObj * mi)152 int mpd_player_play(MpdObj * mi)
153 {
154 return mpd_player_play_id(mi, -1);
155 }
156
mpd_player_stop(MpdObj * mi)157 int mpd_player_stop(MpdObj * mi)
158 {
159 if (!mpd_check_connected(mi)) {
160 debug_printf(DEBUG_WARNING, "not connected\n");
161 return MPD_NOT_CONNECTED;
162 }
163 if (mpd_lock_conn(mi)) {
164 debug_printf(DEBUG_WARNING, "lock failed\n");
165 return MPD_LOCK_FAILED;
166 }
167
168 mpd_sendStopCommand(mi->connection);
169 mpd_finishCommand(mi->connection);
170
171
172 mpd_unlock_conn(mi);
173 if (mpd_status_update(mi)) {
174 return MPD_STATUS_FAILED;
175 }
176 return MPD_OK;
177 }
178
mpd_player_next(MpdObj * mi)179 int mpd_player_next(MpdObj * mi)
180 {
181 if (!mpd_check_connected(mi)) {
182 debug_printf(DEBUG_WARNING, "not connected\n");
183 return MPD_NOT_CONNECTED;
184 }
185 if (mpd_lock_conn(mi)) {
186 debug_printf(DEBUG_WARNING, "lock failed\n");
187 return MPD_LOCK_FAILED;
188 }
189
190 mpd_sendNextCommand(mi->connection);
191 mpd_finishCommand(mi->connection);
192
193
194 mpd_unlock_conn(mi);
195 if (mpd_status_update(mi)) {
196 return MPD_STATUS_FAILED;
197 }
198 return MPD_OK;
199 }
200
mpd_player_prev(MpdObj * mi)201 int mpd_player_prev(MpdObj * mi)
202 {
203 if (!mpd_check_connected(mi)) {
204 debug_printf(DEBUG_WARNING, "not connected\n");
205 return MPD_NOT_CONNECTED;
206 }
207 if (mpd_lock_conn(mi)) {
208 debug_printf(DEBUG_WARNING, "lock failed\n");
209 return MPD_LOCK_FAILED;
210 }
211
212 mpd_sendPrevCommand(mi->connection);
213 mpd_finishCommand(mi->connection);
214
215
216 mpd_unlock_conn(mi);
217 if (mpd_status_update(mi)) {
218 return MPD_STATUS_FAILED;
219 }
220 return MPD_OK;
221 }
222
223
mpd_player_pause(MpdObj * mi)224 int mpd_player_pause(MpdObj * mi)
225 {
226 if (!mpd_check_connected(mi)) {
227 debug_printf(DEBUG_WARNING, "not connected\n");
228 return MPD_NOT_CONNECTED;
229 }
230 if (mpd_lock_conn(mi)) {
231 debug_printf(DEBUG_WARNING, "lock failed\n");
232 return MPD_LOCK_FAILED;
233 }
234
235 if (mpd_player_get_state(mi) == MPD_PLAYER_PAUSE) {
236 mpd_sendPauseCommand(mi->connection, 0);
237 mpd_finishCommand(mi->connection);
238 } else if (mpd_player_get_state(mi) == MPD_PLAYER_PLAY) {
239 mpd_sendPauseCommand(mi->connection, 1);
240 mpd_finishCommand(mi->connection);
241 }
242
243
244 mpd_unlock_conn(mi);
245 if (mpd_status_update(mi)) {
246 return MPD_STATUS_FAILED;
247 }
248 return MPD_OK;
249 }
250
mpd_player_seek(MpdObj * mi,int sec)251 int mpd_player_seek(MpdObj * mi, int sec)
252 {
253 int cur_song = mpd_player_get_current_song_pos(mi);
254 if (cur_song < 0) {
255 debug_printf(DEBUG_ERROR, "mpd_player_get_current_song_pos returned error\n");
256 return cur_song;
257 }
258 if (!mpd_check_connected(mi)) {
259 debug_printf(DEBUG_WARNING, "not connected\n");
260 return MPD_NOT_CONNECTED;
261 }
262 if (mpd_lock_conn(mi)) {
263 debug_printf(DEBUG_WARNING, "lock failed\n");
264 return MPD_LOCK_FAILED;
265 }
266
267 debug_printf(DEBUG_INFO, "seeking in song %i to %i sec\n", cur_song, sec);
268
269 mpd_sendSeekCommand(mi->connection, cur_song, sec);
270 mpd_finishCommand(mi->connection);
271
272
273 mpd_unlock_conn(mi);
274 if (mpd_status_update(mi)) {
275 return MPD_STATUS_FAILED;
276 }
277 return MPD_OK;
278 }
279
mpd_player_get_consume(MpdObj * mi)280 int mpd_player_get_consume(MpdObj * mi)
281 {
282 if (!mpd_check_connected(mi)) {
283 debug_printf(DEBUG_WARNING, "not connected\n");
284 return MPD_NOT_CONNECTED;
285 }
286 if (mpd_status_check(mi) != MPD_OK) {
287 debug_printf(DEBUG_WARNING, "Failed grabbing status\n");
288 return MPD_NOT_CONNECTED;
289 }
290 return mi->status->consume;
291 }
mpd_player_set_single(MpdObj * mi,int single)292 int mpd_player_set_single(MpdObj * mi, int single)
293 {
294 if (!mpd_check_connected(mi)) {
295 debug_printf(DEBUG_WARNING, "not connected\n");
296 return MPD_NOT_CONNECTED;
297 }
298 if (mpd_lock_conn(mi)) {
299 debug_printf(DEBUG_WARNING, "lock failed\n");
300 return MPD_LOCK_FAILED;
301 }
302 mpd_sendSingleCommand(mi->connection, single);
303 mpd_finishCommand(mi->connection);
304
305 mpd_unlock_conn(mi);
306 mpd_status_queue_update(mi);
307 return MPD_OK;
308 }
mpd_player_get_single(MpdObj * mi)309 int mpd_player_get_single(MpdObj * mi)
310 {
311 if (!mpd_check_connected(mi)) {
312 debug_printf(DEBUG_WARNING, "not connected\n");
313 return MPD_NOT_CONNECTED;
314 }
315 if (mpd_status_check(mi) != MPD_OK) {
316 debug_printf(DEBUG_WARNING, "Failed grabbing status\n");
317 return MPD_NOT_CONNECTED;
318 }
319 return mi->status->single;
320 }
mpd_player_set_consume(MpdObj * mi,int consume)321 int mpd_player_set_consume(MpdObj * mi, int consume)
322 {
323 if (!mpd_check_connected(mi)) {
324 debug_printf(DEBUG_WARNING, "not connected\n");
325 return MPD_NOT_CONNECTED;
326 }
327 if (mpd_lock_conn(mi)) {
328 debug_printf(DEBUG_WARNING, "lock failed\n");
329 return MPD_LOCK_FAILED;
330 }
331 mpd_sendConsumeCommand(mi->connection, consume);
332 mpd_finishCommand(mi->connection);
333
334 mpd_unlock_conn(mi);
335 mpd_status_queue_update(mi);
336 return MPD_OK;
337 }
338
339
mpd_player_get_repeat(MpdObj * mi)340 int mpd_player_get_repeat(MpdObj * mi)
341 {
342 if (!mpd_check_connected(mi)) {
343 debug_printf(DEBUG_WARNING, "not connected\n");
344 return MPD_NOT_CONNECTED;
345 }
346 if (mpd_status_check(mi) != MPD_OK) {
347 debug_printf(DEBUG_WARNING, "Failed grabbing status\n");
348 return MPD_NOT_CONNECTED;
349 }
350 return mi->status->repeat;
351 }
352
353
mpd_player_set_repeat(MpdObj * mi,int repeat)354 int mpd_player_set_repeat(MpdObj * mi, int repeat)
355 {
356 if (!mpd_check_connected(mi)) {
357 debug_printf(DEBUG_WARNING, "not connected\n");
358 return MPD_NOT_CONNECTED;
359 }
360 if (mpd_lock_conn(mi)) {
361 debug_printf(DEBUG_WARNING, "lock failed\n");
362 return MPD_LOCK_FAILED;
363 }
364 mpd_sendRepeatCommand(mi->connection, repeat);
365 mpd_finishCommand(mi->connection);
366
367 mpd_unlock_conn(mi);
368 mpd_status_queue_update(mi);
369 return MPD_OK;
370 }
371
372
373
mpd_player_get_random(MpdObj * mi)374 int mpd_player_get_random(MpdObj * mi)
375 {
376 if (!mpd_check_connected(mi)) {
377 debug_printf(DEBUG_WARNING, "not connected\n");
378 return MPD_NOT_CONNECTED;
379 }
380 if (mpd_status_check(mi) != MPD_OK) {
381 debug_printf(DEBUG_WARNING, "Failed grabbing status\n");
382 return MPD_NOT_CONNECTED;
383 }
384 return mi->status->random;
385 }
386
387
mpd_player_set_random(MpdObj * mi,int random)388 int mpd_player_set_random(MpdObj * mi, int random)
389 {
390 if (!mpd_check_connected(mi)) {
391 debug_printf(DEBUG_WARNING, "not connected\n");
392 return MPD_NOT_CONNECTED;
393 }
394 if (mpd_lock_conn(mi)) {
395 debug_printf(DEBUG_WARNING, "lock failed\n");
396 return MPD_LOCK_FAILED;
397 }
398 mpd_sendRandomCommand(mi->connection, random);
399 mpd_finishCommand(mi->connection);
400
401 mpd_unlock_conn(mi);
402 mpd_status_queue_update(mi);
403 return MPD_OK;
404 }
405