Code

screen: eliminated state macros
[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 "screen.h"
24 #include "i18n.h"
25 #include "screen_client.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         if (seek_id < 0)
36                 return;
38         if (c->song != NULL && (unsigned)seek_id == mpd_song_get_id(c->song))
39                 if (!mpd_run_seek_id(c->connection, seek_id, seek_target_time))
40                         mpdclient_handle_error(c);
42         seek_id = -1;
43 }
45 /**
46  * This timer is invoked after seeking when the user hasn't typed a
47  * key for 500ms.  It is used to do the real seeking.
48  */
49 static gboolean
50 seek_timer(gpointer data)
51 {
52         struct mpdclient *c = data;
54         seek_source_id = 0;
55         commit_seek(c);
56         return false;
57 }
59 static void
60 schedule_seek_timer(struct mpdclient *c)
61 {
62         assert(seek_source_id == 0);
64         seek_source_id = g_timeout_add(500, seek_timer, c);
65 }
67 void
68 cancel_seek_timer(void)
69 {
70         if (seek_source_id != 0) {
71                 g_source_remove(seek_source_id);
72                 seek_source_id = 0;
73         }
74 }
76 bool
77 handle_player_command(struct mpdclient *c, command_t cmd)
78 {
79         const struct mpd_song *song;
81         if (c->connection == NULL || c->status == NULL)
82                 return false;
84         cancel_seek_timer();
86         switch(cmd) {
87                 /*
88         case CMD_PLAY:
89                 mpdclient_cmd_play(c, MPD_PLAY_AT_BEGINNING);
90                 break;
91                 */
92         case CMD_PAUSE:
93                 if (!mpd_run_pause(c->connection,
94                                    mpd_status_get_state(c->status) != MPD_STATE_PAUSE))
95                         mpdclient_handle_error(c);
96                 break;
97         case CMD_STOP:
98                 if (!mpd_run_stop(c->connection))
99                         mpdclient_handle_error(c);
100                 break;
101         case CMD_CROP:
102                 mpdclient_cmd_crop(c);
103                 break;
104         case CMD_SEEK_FORWARD:
105                 song = mpdclient_get_current_song(c);
106                 if (song != NULL) {
107                         if (seek_id != (int)mpd_song_get_id(song)) {
108                                 seek_id = mpd_song_get_id(song);
109                                 seek_target_time = mpd_status_get_elapsed_time(c->status);
110                         }
111                         seek_target_time+=options.seek_time;
112                         if (seek_target_time > (int)mpd_status_get_total_time(c->status))
113                                 seek_target_time = mpd_status_get_total_time(c->status);
114                         schedule_seek_timer(c);
115                 }
116                 break;
117                 /* fall through... */
118         case CMD_TRACK_NEXT:
119                 if (!mpd_run_next(c->connection))
120                         mpdclient_handle_error(c);
121                 break;
122         case CMD_SEEK_BACKWARD:
123                 song = mpdclient_get_current_song(c);
124                 if (song != NULL) {
125                         if (seek_id != (int)mpd_song_get_id(song)) {
126                                 seek_id = mpd_song_get_id(c->song);
127                                 seek_target_time = mpd_status_get_elapsed_time(c->status);
128                         }
129                         seek_target_time-=options.seek_time;
130                         if (seek_target_time < 0)
131                                 seek_target_time=0;
132                         schedule_seek_timer(c);
133                 }
134                 break;
135         case CMD_TRACK_PREVIOUS:
136                 if (!mpd_run_previous(c->connection))
137                         mpdclient_handle_error(c);
138                 break;
139         case CMD_SHUFFLE:
140                 if (mpd_run_shuffle(c->connection))
141                         screen_status_message(_("Shuffled playlist"));
142                 else
143                         mpdclient_handle_error(c);
144                 break;
145         case CMD_CLEAR:
146                 if (mpdclient_cmd_clear(c) == 0)
147                         screen_status_message(_("Cleared playlist"));
148                 break;
149         case CMD_REPEAT:
150                 if (!mpd_run_repeat(c->connection,
151                                     !mpd_status_get_repeat(c->status)))
152                         mpdclient_handle_error(c);
153                 break;
154         case CMD_RANDOM:
155                 if (!mpd_run_random(c->connection,
156                                     !mpd_status_get_random(c->status)))
157                         mpdclient_handle_error(c);
158                 break;
159         case CMD_SINGLE:
160                 if (!mpd_run_single(c->connection,
161                                     !mpd_status_get_single(c->status)))
162                         mpdclient_handle_error(c);
163                 break;
164         case CMD_CONSUME:
165                 if (!mpd_run_consume(c->connection,
166                                      !mpd_status_get_consume(c->status)))
167                         mpdclient_handle_error(c);
168                 break;
169         case CMD_CROSSFADE:
170                 if (!mpd_run_crossfade(c->connection,
171                                        mpd_status_get_crossfade(c->status) > 0
172                                        ? 0 : options.crossfade_time))
173                         mpdclient_handle_error(c);
174                 break;
175         case CMD_DB_UPDATE:
176                 screen_database_update(c, NULL);
177                 break;
178         case CMD_VOLUME_UP:
179                 mpdclient_cmd_volume_up(c);
180                 break;
181         case CMD_VOLUME_DOWN:
182                 mpdclient_cmd_volume_down(c);
183                 break;
185         default:
186                 return false;
187         }
189         return true;