Code

player_command: break from switch if not playing
[ncmpc.git] / src / player_command.c
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 bool
86 handle_player_command(struct mpdclient *c, command_t cmd)
87 {
88         struct mpd_connection *connection;
89         const struct mpd_song *song;
91         if (!mpdclient_is_connected(c) || c->status == NULL)
92                 return false;
94         cancel_seek_timer();
96         switch(cmd) {
97                 /*
98         case CMD_PLAY:
99                 mpdclient_cmd_play(c, MPD_PLAY_AT_BEGINNING);
100                 break;
101                 */
102         case CMD_PAUSE:
103                 connection = mpdclient_get_connection(c);
104                 if (connection == NULL)
105                         break;
107                 if (!mpd_run_pause(connection,
108                                    mpd_status_get_state(c->status) != MPD_STATE_PAUSE))
109                         mpdclient_handle_error(c);
110                 break;
111         case CMD_STOP:
112                 connection = mpdclient_get_connection(c);
113                 if (connection == NULL)
114                         break;
116                 if (!mpd_run_stop(connection))
117                         mpdclient_handle_error(c);
118                 break;
119         case CMD_CROP:
120                 mpdclient_cmd_crop(c);
121                 break;
122         case CMD_SEEK_FORWARD:
123                 song = mpdclient_get_current_song(c);
124                 if (song == NULL)
125                         break;
127                 if (seek_id != (int)mpd_song_get_id(song)) {
128                         seek_id = mpd_song_get_id(song);
129                         seek_target_time = mpd_status_get_elapsed_time(c->status);
130                 }
132                 seek_target_time += options.seek_time;
133                 if (seek_target_time > (int)mpd_status_get_total_time(c->status))
134                         seek_target_time = mpd_status_get_total_time(c->status);
136                 schedule_seek_timer(c);
137                 break;
139         case CMD_TRACK_NEXT:
140                 connection = mpdclient_get_connection(c);
141                 if (connection == NULL)
142                         break;
144                 if (!mpd_run_next(connection))
145                         mpdclient_handle_error(c);
146                 break;
147         case CMD_SEEK_BACKWARD:
148                 song = mpdclient_get_current_song(c);
149                 if (song == NULL)
150                         break;
152                 if (seek_id != (int)mpd_song_get_id(song)) {
153                         seek_id = mpd_song_get_id(c->song);
154                         seek_target_time = mpd_status_get_elapsed_time(c->status);
155                 }
157                 seek_target_time -= options.seek_time;
158                 if (seek_target_time < 0)
159                         seek_target_time = 0;
161                 schedule_seek_timer(c);
162                 break;
164         case CMD_TRACK_PREVIOUS:
165                 connection = mpdclient_get_connection(c);
166                 if (connection == NULL)
167                         break;
169                 if (!mpd_run_previous(connection))
170                         mpdclient_handle_error(c);
171                 break;
172         case CMD_SHUFFLE:
173                 connection = mpdclient_get_connection(c);
174                 if (connection == NULL)
175                         break;
177                 if (mpd_run_shuffle(connection))
178                         screen_status_message(_("Shuffled playlist"));
179                 else
180                         mpdclient_handle_error(c);
181                 break;
182         case CMD_CLEAR:
183                 connection = mpdclient_get_connection(c);
184                 if (connection == NULL)
185                         break;
187                 if (mpdclient_cmd_clear(c))
188                         screen_status_message(_("Cleared playlist"));
189                 break;
190         case CMD_REPEAT:
191                 connection = mpdclient_get_connection(c);
192                 if (connection == NULL)
193                         break;
195                 if (!mpd_run_repeat(connection,
196                                     !mpd_status_get_repeat(c->status)))
197                         mpdclient_handle_error(c);
198                 break;
199         case CMD_RANDOM:
200                 connection = mpdclient_get_connection(c);
201                 if (connection == NULL)
202                         break;
204                 if (!mpd_run_random(connection,
205                                     !mpd_status_get_random(c->status)))
206                         mpdclient_handle_error(c);
207                 break;
208         case CMD_SINGLE:
209                 connection = mpdclient_get_connection(c);
210                 if (connection == NULL)
211                         break;
213                 if (!mpd_run_single(connection,
214                                     !mpd_status_get_single(c->status)))
215                         mpdclient_handle_error(c);
216                 break;
217         case CMD_CONSUME:
218                 connection = mpdclient_get_connection(c);
219                 if (connection == NULL)
220                         break;
222                 if (!mpd_run_consume(connection,
223                                      !mpd_status_get_consume(c->status)))
224                         mpdclient_handle_error(c);
225                 break;
226         case CMD_CROSSFADE:
227                 connection = mpdclient_get_connection(c);
228                 if (connection == NULL)
229                         break;
231                 if (!mpd_run_crossfade(connection,
232                                        mpd_status_get_crossfade(c->status) > 0
233                                        ? 0 : options.crossfade_time))
234                         mpdclient_handle_error(c);
235                 break;
236         case CMD_DB_UPDATE:
237                 screen_database_update(c, NULL);
238                 break;
239         case CMD_VOLUME_UP:
240                 mpdclient_cmd_volume_up(c);
241                 break;
242         case CMD_VOLUME_DOWN:
243                 mpdclient_cmd_volume_down(c);
244                 break;
246         default:
247                 return false;
248         }
250         return true;