1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2010 The Music Player Daemon Project
3 * Project homepage: http://musicpd.org
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.
20 #include "player_command.h"
21 #include "mpdclient.h"
24 #include "screen_client.h"
25 #include "screen_status.h"
30 static guint seek_source_id
;
33 commit_seek(struct mpdclient
*c
)
35 struct mpd_connection
*connection
;
40 connection
= mpdclient_get_connection(c
);
41 if (connection
== NULL
) {
46 if (c
->song
!= NULL
&& (unsigned)seek_id
== mpd_song_get_id(c
->song
))
47 if (!mpd_run_seek_id(connection
, seek_id
, seek_target_time
))
48 mpdclient_handle_error(c
);
54 * This timer is invoked after seeking when the user hasn't typed a
55 * key for 500ms. It is used to do the real seeking.
58 seek_timer(gpointer data
)
60 struct mpdclient
*c
= data
;
64 mpdclient_put_connection(c
);
69 schedule_seek_timer(struct mpdclient
*c
)
71 assert(seek_source_id
== 0);
73 seek_source_id
= g_timeout_add(500, seek_timer
, c
);
77 cancel_seek_timer(void)
79 if (seek_source_id
!= 0) {
80 g_source_remove(seek_source_id
);
86 setup_seek(struct mpdclient
*c
)
88 if (!mpdclient_is_playing(c
))
91 if (seek_id
!= (int)mpd_status_get_song_id(c
->status
)) {
92 seek_id
= mpd_status_get_song_id(c
->status
);
93 seek_target_time
= mpd_status_get_elapsed_time(c
->status
);
96 schedule_seek_timer(c
);
102 handle_player_command(struct mpdclient
*c
, command_t cmd
)
104 struct mpd_connection
*connection
;
106 if (!mpdclient_is_connected(c
) || c
->status
== NULL
)
114 mpdclient_cmd_play(c, MPD_PLAY_AT_BEGINNING);
118 connection
= mpdclient_get_connection(c
);
119 if (connection
== NULL
)
122 if (!mpd_run_pause(connection
,
123 mpd_status_get_state(c
->status
) != MPD_STATE_PAUSE
))
124 mpdclient_handle_error(c
);
127 connection
= mpdclient_get_connection(c
);
128 if (connection
== NULL
)
131 if (!mpd_run_stop(connection
))
132 mpdclient_handle_error(c
);
135 mpdclient_cmd_crop(c
);
137 case CMD_SEEK_FORWARD
:
141 seek_target_time
+= options
.seek_time
;
142 if (seek_target_time
> (int)mpd_status_get_total_time(c
->status
))
143 seek_target_time
= mpd_status_get_total_time(c
->status
);
147 connection
= mpdclient_get_connection(c
);
148 if (connection
== NULL
)
151 if (!mpd_run_next(connection
))
152 mpdclient_handle_error(c
);
154 case CMD_SEEK_BACKWARD
:
158 seek_target_time
-= options
.seek_time
;
159 if (seek_target_time
< 0)
160 seek_target_time
= 0;
163 case CMD_TRACK_PREVIOUS
:
164 connection
= mpdclient_get_connection(c
);
165 if (connection
== NULL
)
168 if (!mpd_run_previous(connection
))
169 mpdclient_handle_error(c
);
172 connection
= mpdclient_get_connection(c
);
173 if (connection
== NULL
)
176 if (mpd_run_shuffle(connection
))
177 screen_status_message(_("Shuffled playlist"));
179 mpdclient_handle_error(c
);
182 connection
= mpdclient_get_connection(c
);
183 if (connection
== NULL
)
186 if (mpdclient_cmd_clear(c
))
187 screen_status_message(_("Cleared playlist"));
190 connection
= mpdclient_get_connection(c
);
191 if (connection
== NULL
)
194 if (!mpd_run_repeat(connection
,
195 !mpd_status_get_repeat(c
->status
)))
196 mpdclient_handle_error(c
);
199 connection
= mpdclient_get_connection(c
);
200 if (connection
== NULL
)
203 if (!mpd_run_random(connection
,
204 !mpd_status_get_random(c
->status
)))
205 mpdclient_handle_error(c
);
208 connection
= mpdclient_get_connection(c
);
209 if (connection
== NULL
)
212 if (!mpd_run_single(connection
,
213 !mpd_status_get_single(c
->status
)))
214 mpdclient_handle_error(c
);
217 connection
= mpdclient_get_connection(c
);
218 if (connection
== NULL
)
221 if (!mpd_run_consume(connection
,
222 !mpd_status_get_consume(c
->status
)))
223 mpdclient_handle_error(c
);
226 connection
= mpdclient_get_connection(c
);
227 if (connection
== NULL
)
230 if (!mpd_run_crossfade(connection
,
231 mpd_status_get_crossfade(c
->status
) > 0
232 ? 0 : options
.crossfade_time
))
233 mpdclient_handle_error(c
);
236 screen_database_update(c
, NULL
);
239 mpdclient_cmd_volume_up(c
);
241 case CMD_VOLUME_DOWN
:
242 mpdclient_cmd_volume_down(c
);