plugin: convert plugin_fd_add() to method
[ncmpc-debian.git] / src / player_command.cxx
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2019 The Music Player Daemon Project
3  * Project homepage: http://musicpd.org
4  *
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.
9  *
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.
14  *
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  */
19
20 #include "player_command.hxx"
21 #include "DelayedSeek.hxx"
22 #include "Command.hxx"
23 #include "mpdclient.hxx"
24 #include "Options.hxx"
25 #include "i18n.h"
26 #include "screen_client.hxx"
27 #include "screen_status.hxx"
28
29 bool
30 handle_player_command(struct mpdclient &c, DelayedSeek &seek, Command cmd)
31 {
32         if (!c.IsConnected() || c.status == nullptr)
33                 return false;
34
35         seek.Cancel();
36
37         switch(cmd) {
38                 struct mpd_connection *connection;
39
40                 /*
41         case Command::PLAY:
42                 mpdclient_cmd_play(c, MPD_PLAY_AT_BEGINNING);
43                 break;
44                 */
45         case Command::PAUSE:
46                 connection = c.GetConnection();
47                 if (connection == nullptr)
48                         break;
49
50                 if (!mpd_run_pause(connection, c.state != MPD_STATE_PAUSE))
51                         c.HandleError();
52                 break;
53         case Command::STOP:
54                 connection = c.GetConnection();
55                 if (connection == nullptr)
56                         break;
57
58                 if (!mpd_run_stop(connection))
59                         c.HandleError();
60                 break;
61         case Command::CROP:
62                 mpdclient_cmd_crop(&c);
63                 break;
64         case Command::SEEK_FORWARD:
65                 seek.Seek(options.seek_time);
66                 break;
67
68         case Command::TRACK_NEXT:
69                 connection = c.GetConnection();
70                 if (connection == nullptr)
71                         break;
72
73                 if (!mpd_run_next(connection))
74                         c.HandleError();
75                 break;
76         case Command::SEEK_BACKWARD:
77                 seek.Seek(-int(options.seek_time));
78                 break;
79
80         case Command::TRACK_PREVIOUS:
81                 connection = c.GetConnection();
82                 if (connection == nullptr)
83                         break;
84
85                 if (!mpd_run_previous(connection))
86                         c.HandleError();
87                 break;
88         case Command::SHUFFLE:
89                 connection = c.GetConnection();
90                 if (connection == nullptr)
91                         break;
92
93                 if (mpd_run_shuffle(connection))
94                         screen_status_message(_("Shuffled queue"));
95                 else
96                         c.HandleError();
97                 break;
98         case Command::CLEAR:
99                 if (c.RunClearQueue())
100                         screen_status_message(_("Cleared queue"));
101                 break;
102         case Command::REPEAT:
103                 connection = c.GetConnection();
104                 if (connection == nullptr)
105                         break;
106
107                 if (!mpd_run_repeat(connection,
108                                     !mpd_status_get_repeat(c.status)))
109                         c.HandleError();
110                 break;
111         case Command::RANDOM:
112                 connection = c.GetConnection();
113                 if (connection == nullptr)
114                         break;
115
116                 if (!mpd_run_random(connection,
117                                     !mpd_status_get_random(c.status)))
118                         c.HandleError();
119                 break;
120         case Command::SINGLE:
121                 connection = c.GetConnection();
122                 if (connection == nullptr)
123                         break;
124
125                 if (!mpd_run_single(connection,
126                                     !mpd_status_get_single(c.status)))
127                         c.HandleError();
128                 break;
129         case Command::CONSUME:
130                 connection = c.GetConnection();
131                 if (connection == nullptr)
132                         break;
133
134                 if (!mpd_run_consume(connection,
135                                      !mpd_status_get_consume(c.status)))
136                         c.HandleError();
137                 break;
138         case Command::CROSSFADE:
139                 connection = c.GetConnection();
140                 if (connection == nullptr)
141                         break;
142
143                 if (!mpd_run_crossfade(connection,
144                                        mpd_status_get_crossfade(c.status) > 0
145                                        ? 0 : options.crossfade_time))
146                         c.HandleError();
147                 break;
148         case Command::DB_UPDATE:
149                 screen_database_update(&c, nullptr);
150                 break;
151         case Command::VOLUME_UP:
152                 c.RunVolumeUp();
153                 break;
154         case Command::VOLUME_DOWN:
155                 c.RunVolumeDown();
156                 break;
157
158         default:
159                 return false;
160         }
161
162         return true;
163 }