Adding some support for using "idle" to get changed events.
[libmpd.git] / src / libmpd-player.c
blob57b5e17ab75e38690b54e6b060695185c68b6418
1 /*
2 *Copyright (C) 2004-2007 Qball Cow <Qball@qballcow.nl>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public
15 * License along with this program; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
20 #include <stdio.h>
21 #include <stdlib.h>
22 #define __USE_GNU
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"
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, "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);
104 mpd_unlock_conn(mi);
105 if (mpd_status_update(mi)) {
106 return MPD_STATUS_FAILED;
108 return MPD_OK;
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);
131 mpd_unlock_conn(mi);
132 if (mpd_status_update(mi)) {
133 return MPD_STATUS_FAILED;
135 return MPD_OK;
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);
153 mpd_unlock_conn(mi);
154 if (mpd_status_update(mi)) {
155 return MPD_STATUS_FAILED;
157 return MPD_OK;
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);
175 mpd_unlock_conn(mi);
176 if (mpd_status_update(mi)) {
177 return MPD_STATUS_FAILED;
179 return MPD_OK;
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);
203 mpd_unlock_conn(mi);
204 if (mpd_status_update(mi)) {
205 return MPD_STATUS_FAILED;
207 return MPD_OK;
210 int mpd_player_seek(MpdObj * mi, int sec)
212 int cur_song = mpd_player_get_current_song_pos(mi);
213 if (cur_song < 0) {
214 debug_printf(DEBUG_ERROR, "mpd_player_get_current_song_pos returned error\n");
215 return cur_song;
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);
232 mpd_unlock_conn(mi);
233 if (mpd_status_update(mi)) {
234 return MPD_STATUS_FAILED;
236 return MPD_OK;
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);
266 mpd_unlock_conn(mi);
267 mpd_status_queue_update(mi);
268 return MPD_OK;
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);
300 mpd_unlock_conn(mi);
301 mpd_status_queue_update(mi);
302 return MPD_OK;