1 /* libmpd (high level libmpdclient library)
2 * Copyright (C) 2004-2009 Qball Cow <qball@sarine.nl>
3 * Project homepage: http://gmpcwiki.sarine.nl/
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.
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.
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.
27 #include "debug_printf.h"
29 #include "libmpd-internal.h"
31 int mpd_player_get_state(MpdObj
* mi
)
33 if (!mpd_check_connected(mi
)) {
34 debug_printf(DEBUG_WARNING
, "not connected\n");
35 return MPD_NOT_CONNECTED
;
37 if (mpd_status_check(mi
) != MPD_OK
) {
38 debug_printf(DEBUG_WARNING
, "Failed to get status\n");
39 return MPD_STATUS_FAILED
;
41 return mi
->status
->state
;
44 int mpd_player_get_current_song_id(MpdObj
* mi
)
46 if (!mpd_check_connected(mi
)) {
47 debug_printf(DEBUG_WARNING
, "not connected\n");
48 return MPD_NOT_CONNECTED
;
50 if (mpd_status_check(mi
) != MPD_OK
) {
51 debug_printf(DEBUG_ERROR
, "Failed to get status\n");
52 return MPD_STATUS_FAILED
;
54 /* check if in valid state */
55 if (mpd_player_get_state(mi
) != MPD_PLAYER_PLAY
&&
56 mpd_player_get_state(mi
) != MPD_PLAYER_PAUSE
) {
57 return MPD_PLAYER_NOT_PLAYING
;
59 /* just to be sure check */
60 if (!mi
->status
->playlistLength
) {
61 return MPD_PLAYLIST_EMPTY
;
63 return mi
->status
->songid
;
66 int mpd_player_get_current_song_pos(MpdObj
* mi
)
68 if (!mpd_check_connected(mi
)) {
69 debug_printf(DEBUG_WARNING
, "not connected\n");
70 return MPD_NOT_CONNECTED
;
72 if (mpd_status_check(mi
)!= MPD_OK
) {
73 debug_printf(DEBUG_ERROR
, "Failed to get status\n");
74 return MPD_STATUS_FAILED
;
76 /* check if in valid state */
77 if (mpd_player_get_state(mi
) != MPD_PLAYER_PLAY
&&
78 mpd_player_get_state(mi
) != MPD_PLAYER_PAUSE
) {
79 return MPD_PLAYER_NOT_PLAYING
;
81 /* just to be sure check */
82 if (!mi
->status
->playlistLength
) {
83 return MPD_PLAYLIST_EMPTY
;
85 return mi
->status
->song
;
88 int mpd_player_play_id(MpdObj
* mi
, int id
)
90 debug_printf(DEBUG_INFO
, "trying to play id: %i\n", id
);
91 if (!mpd_check_connected(mi
)) {
92 debug_printf(DEBUG_WARNING
, "not connected\n");
93 return MPD_NOT_CONNECTED
;
95 if (mpd_lock_conn(mi
)) {
96 debug_printf(DEBUG_WARNING
, "lock failed\n");
97 return MPD_LOCK_FAILED
;
100 mpd_sendPlayIdCommand(mi
->connection
, id
);
101 mpd_finishCommand(mi
->connection
);
105 if (mpd_status_update(mi
)) {
106 return MPD_STATUS_FAILED
;
111 int mpd_player_play(MpdObj
* mi
)
113 return mpd_player_play_id(mi
, -1);
116 int mpd_player_stop(MpdObj
* mi
)
118 if (!mpd_check_connected(mi
)) {
119 debug_printf(DEBUG_WARNING
, "not connected\n");
120 return MPD_NOT_CONNECTED
;
122 if (mpd_lock_conn(mi
)) {
123 debug_printf(DEBUG_WARNING
, "lock failed\n");
124 return MPD_LOCK_FAILED
;
127 mpd_sendStopCommand(mi
->connection
);
128 mpd_finishCommand(mi
->connection
);
132 if (mpd_status_update(mi
)) {
133 return MPD_STATUS_FAILED
;
138 int mpd_player_next(MpdObj
* mi
)
140 if (!mpd_check_connected(mi
)) {
141 debug_printf(DEBUG_WARNING
, "not connected\n");
142 return MPD_NOT_CONNECTED
;
144 if (mpd_lock_conn(mi
)) {
145 debug_printf(DEBUG_WARNING
, "lock failed\n");
146 return MPD_LOCK_FAILED
;
149 mpd_sendNextCommand(mi
->connection
);
150 mpd_finishCommand(mi
->connection
);
154 if (mpd_status_update(mi
)) {
155 return MPD_STATUS_FAILED
;
160 int mpd_player_prev(MpdObj
* mi
)
162 if (!mpd_check_connected(mi
)) {
163 debug_printf(DEBUG_WARNING
, "not connected\n");
164 return MPD_NOT_CONNECTED
;
166 if (mpd_lock_conn(mi
)) {
167 debug_printf(DEBUG_WARNING
, "lock failed\n");
168 return MPD_LOCK_FAILED
;
171 mpd_sendPrevCommand(mi
->connection
);
172 mpd_finishCommand(mi
->connection
);
176 if (mpd_status_update(mi
)) {
177 return MPD_STATUS_FAILED
;
183 int mpd_player_pause(MpdObj
* mi
)
185 if (!mpd_check_connected(mi
)) {
186 debug_printf(DEBUG_WARNING
, "not connected\n");
187 return MPD_NOT_CONNECTED
;
189 if (mpd_lock_conn(mi
)) {
190 debug_printf(DEBUG_WARNING
, "lock failed\n");
191 return MPD_LOCK_FAILED
;
194 if (mpd_player_get_state(mi
) == MPD_PLAYER_PAUSE
) {
195 mpd_sendPauseCommand(mi
->connection
, 0);
196 mpd_finishCommand(mi
->connection
);
197 } else if (mpd_player_get_state(mi
) == MPD_PLAYER_PLAY
) {
198 mpd_sendPauseCommand(mi
->connection
, 1);
199 mpd_finishCommand(mi
->connection
);
204 if (mpd_status_update(mi
)) {
205 return MPD_STATUS_FAILED
;
210 int mpd_player_seek(MpdObj
* mi
, int sec
)
212 int cur_song
= mpd_player_get_current_song_pos(mi
);
214 debug_printf(DEBUG_ERROR
, "mpd_player_get_current_song_pos returned error\n");
217 if (!mpd_check_connected(mi
)) {
218 debug_printf(DEBUG_WARNING
, "not connected\n");
219 return MPD_NOT_CONNECTED
;
221 if (mpd_lock_conn(mi
)) {
222 debug_printf(DEBUG_WARNING
, "lock failed\n");
223 return MPD_LOCK_FAILED
;
226 debug_printf(DEBUG_INFO
, "seeking in song %i to %i sec\n", cur_song
, sec
);
228 mpd_sendSeekCommand(mi
->connection
, cur_song
, sec
);
229 mpd_finishCommand(mi
->connection
);
233 if (mpd_status_update(mi
)) {
234 return MPD_STATUS_FAILED
;
239 int mpd_player_get_repeat(MpdObj
* mi
)
241 if (!mpd_check_connected(mi
)) {
242 debug_printf(DEBUG_WARNING
, "not connected\n");
243 return MPD_NOT_CONNECTED
;
245 if (mpd_status_check(mi
) != MPD_OK
) {
246 debug_printf(DEBUG_WARNING
, "Failed grabbing status\n");
247 return MPD_NOT_CONNECTED
;
249 return mi
->status
->repeat
;
253 int mpd_player_set_repeat(MpdObj
* mi
, int repeat
)
255 if (!mpd_check_connected(mi
)) {
256 debug_printf(DEBUG_WARNING
, "not connected\n");
257 return MPD_NOT_CONNECTED
;
259 if (mpd_lock_conn(mi
)) {
260 debug_printf(DEBUG_WARNING
, "lock failed\n");
261 return MPD_LOCK_FAILED
;
263 mpd_sendRepeatCommand(mi
->connection
, repeat
);
264 mpd_finishCommand(mi
->connection
);
267 mpd_status_queue_update(mi
);
273 int mpd_player_get_random(MpdObj
* mi
)
275 if (!mpd_check_connected(mi
)) {
276 debug_printf(DEBUG_WARNING
, "not connected\n");
277 return MPD_NOT_CONNECTED
;
279 if (mpd_status_check(mi
) != MPD_OK
) {
280 debug_printf(DEBUG_WARNING
, "Failed grabbing status\n");
281 return MPD_NOT_CONNECTED
;
283 return mi
->status
->random
;
287 int mpd_player_set_random(MpdObj
* mi
, int random
)
289 if (!mpd_check_connected(mi
)) {
290 debug_printf(DEBUG_WARNING
, "not connected\n");
291 return MPD_NOT_CONNECTED
;
293 if (mpd_lock_conn(mi
)) {
294 debug_printf(DEBUG_WARNING
, "lock failed\n");
295 return MPD_LOCK_FAILED
;
297 mpd_sendRandomCommand(mi
->connection
, random
);
298 mpd_finishCommand(mi
->connection
);
301 mpd_status_queue_update(mi
);