505d2fc596d47709089fbb4bef3cbda925a3a1d5
1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2009 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.
18 */
20 #include "player_command.h"
21 #include "mpdclient.h"
22 #include "options.h"
23 #include "i18n.h"
24 #include "screen_client.h"
25 #include "screen_message.h"
27 int seek_id = -1;
28 int seek_target_time;
30 static guint seek_source_id;
32 static void
33 commit_seek(struct mpdclient *c)
34 {
35 struct mpd_connection *connection;
37 if (seek_id < 0)
38 return;
40 connection = mpdclient_get_connection(c);
41 if (connection == NULL) {
42 seek_id = -1;
43 return;
44 }
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);
50 seek_id = -1;
51 }
53 /**
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.
56 */
57 static gboolean
58 seek_timer(gpointer data)
59 {
60 struct mpdclient *c = data;
62 seek_source_id = 0;
63 commit_seek(c);
64 mpdclient_put_connection(c);
65 return false;
66 }
68 static void
69 schedule_seek_timer(struct mpdclient *c)
70 {
71 assert(seek_source_id == 0);
73 seek_source_id = g_timeout_add(500, seek_timer, c);
74 }
76 void
77 cancel_seek_timer(void)
78 {
79 if (seek_source_id != 0) {
80 g_source_remove(seek_source_id);
81 seek_source_id = 0;
82 }
83 }
85 static bool
86 setup_seek(struct mpdclient *c)
87 {
88 const struct mpd_song *song;
90 song = mpdclient_get_current_song(c);
91 if (song == NULL)
92 return false;
94 if (seek_id != (int)mpd_song_get_id(song)) {
95 seek_id = mpd_song_get_id(song);
96 seek_target_time = mpd_status_get_elapsed_time(c->status);
97 }
99 schedule_seek_timer(c);
101 return true;
102 }
104 bool
105 handle_player_command(struct mpdclient *c, command_t cmd)
106 {
107 struct mpd_connection *connection;
109 if (!mpdclient_is_connected(c) || c->status == NULL)
110 return false;
112 cancel_seek_timer();
114 switch(cmd) {
115 /*
116 case CMD_PLAY:
117 mpdclient_cmd_play(c, MPD_PLAY_AT_BEGINNING);
118 break;
119 */
120 case CMD_PAUSE:
121 connection = mpdclient_get_connection(c);
122 if (connection == NULL)
123 break;
125 if (!mpd_run_pause(connection,
126 mpd_status_get_state(c->status) != MPD_STATE_PAUSE))
127 mpdclient_handle_error(c);
128 break;
129 case CMD_STOP:
130 connection = mpdclient_get_connection(c);
131 if (connection == NULL)
132 break;
134 if (!mpd_run_stop(connection))
135 mpdclient_handle_error(c);
136 break;
137 case CMD_CROP:
138 mpdclient_cmd_crop(c);
139 break;
140 case CMD_SEEK_FORWARD:
141 if (!setup_seek(c))
142 break;
144 seek_target_time += options.seek_time;
145 if (seek_target_time > (int)mpd_status_get_total_time(c->status))
146 seek_target_time = mpd_status_get_total_time(c->status);
147 break;
149 case CMD_TRACK_NEXT:
150 connection = mpdclient_get_connection(c);
151 if (connection == NULL)
152 break;
154 if (!mpd_run_next(connection))
155 mpdclient_handle_error(c);
156 break;
157 case CMD_SEEK_BACKWARD:
158 if (!setup_seek(c))
159 break;
161 seek_target_time -= options.seek_time;
162 if (seek_target_time < 0)
163 seek_target_time = 0;
164 break;
166 case CMD_TRACK_PREVIOUS:
167 connection = mpdclient_get_connection(c);
168 if (connection == NULL)
169 break;
171 if (!mpd_run_previous(connection))
172 mpdclient_handle_error(c);
173 break;
174 case CMD_SHUFFLE:
175 connection = mpdclient_get_connection(c);
176 if (connection == NULL)
177 break;
179 if (mpd_run_shuffle(connection))
180 screen_status_message(_("Shuffled playlist"));
181 else
182 mpdclient_handle_error(c);
183 break;
184 case CMD_CLEAR:
185 connection = mpdclient_get_connection(c);
186 if (connection == NULL)
187 break;
189 if (mpdclient_cmd_clear(c))
190 screen_status_message(_("Cleared playlist"));
191 break;
192 case CMD_REPEAT:
193 connection = mpdclient_get_connection(c);
194 if (connection == NULL)
195 break;
197 if (!mpd_run_repeat(connection,
198 !mpd_status_get_repeat(c->status)))
199 mpdclient_handle_error(c);
200 break;
201 case CMD_RANDOM:
202 connection = mpdclient_get_connection(c);
203 if (connection == NULL)
204 break;
206 if (!mpd_run_random(connection,
207 !mpd_status_get_random(c->status)))
208 mpdclient_handle_error(c);
209 break;
210 case CMD_SINGLE:
211 connection = mpdclient_get_connection(c);
212 if (connection == NULL)
213 break;
215 if (!mpd_run_single(connection,
216 !mpd_status_get_single(c->status)))
217 mpdclient_handle_error(c);
218 break;
219 case CMD_CONSUME:
220 connection = mpdclient_get_connection(c);
221 if (connection == NULL)
222 break;
224 if (!mpd_run_consume(connection,
225 !mpd_status_get_consume(c->status)))
226 mpdclient_handle_error(c);
227 break;
228 case CMD_CROSSFADE:
229 connection = mpdclient_get_connection(c);
230 if (connection == NULL)
231 break;
233 if (!mpd_run_crossfade(connection,
234 mpd_status_get_crossfade(c->status) > 0
235 ? 0 : options.crossfade_time))
236 mpdclient_handle_error(c);
237 break;
238 case CMD_DB_UPDATE:
239 screen_database_update(c, NULL);
240 break;
241 case CMD_VOLUME_UP:
242 mpdclient_cmd_volume_up(c);
243 break;
244 case CMD_VOLUME_DOWN:
245 mpdclient_cmd_volume_down(c);
246 break;
248 default:
249 return false;
250 }
252 return true;
253 }