screen_queue: use mpdclient_settings_name() for the title
[ncmpc.git] / src / screen.c
blob6d2610ad1770e461bd72a503ada7a016407bec3e
1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2017 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 "screen.h"
21 #include "screen_interface.h"
22 #include "screen_list.h"
23 #include "screen_status.h"
24 #include "config.h"
25 #include "i18n.h"
26 #include "charset.h"
27 #include "mpdclient.h"
28 #include "options.h"
29 #include "player_command.h"
30 #include "screen_help.h"
31 #include "screen_queue.h"
32 #include "screen_file.h"
33 #include "screen_artist.h"
34 #include "screen_search.h"
35 #include "screen_song.h"
36 #include "screen_keydef.h"
37 #include "screen_lyrics.h"
38 #include "screen_outputs.h"
39 #include "screen_chat.h"
41 #include <mpd/client.h>
43 #include <stdlib.h>
44 #include <unistd.h>
45 #include <string.h>
46 #include <time.h>
48 /* screens */
50 struct screen screen = {
51 .current_page = &screen_queue,
54 static const struct screen_functions *mode_fn_prev = &screen_queue;
56 void
57 screen_switch(const struct screen_functions *sf, struct mpdclient *c)
59 assert(sf != NULL);
61 if (sf == screen.current_page)
62 return;
64 mode_fn_prev = screen.current_page;
66 /* close the old mode */
67 if (screen.current_page->close != NULL)
68 screen.current_page->close();
70 /* get functions for the new mode */
71 screen.current_page = sf;
73 /* open the new mode */
74 if (sf->open != NULL)
75 sf->open(c);
77 screen_paint(c, true);
80 void
81 screen_swap(struct mpdclient *c, const struct mpd_song *song)
83 if (song != NULL)
85 if (false)
86 { /* just a hack to make the ifdefs less ugly */ }
87 #ifdef ENABLE_SONG_SCREEN
88 if (mode_fn_prev == &screen_song)
89 screen_song_switch(c, song);
90 #endif
91 #ifdef ENABLE_LYRICS_SCREEN
92 else if (mode_fn_prev == &screen_lyrics)
93 screen_lyrics_switch(c, song, true);
94 #endif
95 else
96 screen_switch(mode_fn_prev, c);
98 else
99 screen_switch(mode_fn_prev, c);
102 static int
103 find_configured_screen(const char *name)
105 unsigned i;
107 for (i = 0; options.screen_list[i] != NULL; ++i)
108 if (strcmp(options.screen_list[i], name) == 0)
109 return i;
111 return -1;
114 static void
115 screen_next_mode(struct mpdclient *c, int offset)
117 int max = g_strv_length(options.screen_list);
119 /* find current screen */
120 int current = find_configured_screen(screen_get_name(screen.current_page));
121 int next = current + offset;
122 if (next<0)
123 next = max-1;
124 else if (next>=max)
125 next = 0;
127 const struct screen_functions *sf =
128 screen_lookup_name(options.screen_list[next]);
129 if (sf != NULL)
130 screen_switch(sf, c);
133 void
134 screen_update(struct mpdclient *c)
136 #ifndef NCMPC_MINI
137 static bool was_connected;
138 static bool initialized = false;
139 static bool repeat;
140 static bool random_enabled;
141 static bool single;
142 static bool consume;
143 static unsigned crossfade;
145 /* print a message if mpd status has changed */
146 if ((c->events & MPD_IDLE_OPTIONS) && c->status != NULL) {
147 if (!initialized) {
148 repeat = mpd_status_get_repeat(c->status);
149 random_enabled = mpd_status_get_random(c->status);
150 single = mpd_status_get_single(c->status);
151 consume = mpd_status_get_consume(c->status);
152 crossfade = mpd_status_get_crossfade(c->status);
153 initialized = true;
156 if (repeat != mpd_status_get_repeat(c->status))
157 screen_status_printf(mpd_status_get_repeat(c->status) ?
158 _("Repeat mode is on") :
159 _("Repeat mode is off"));
161 if (random_enabled != mpd_status_get_random(c->status))
162 screen_status_printf(mpd_status_get_random(c->status) ?
163 _("Random mode is on") :
164 _("Random mode is off"));
166 if (single != mpd_status_get_single(c->status))
167 screen_status_printf(mpd_status_get_single(c->status) ?
168 /* "single" mode means
169 that MPD will
170 automatically stop
171 after playing one
172 single song */
173 _("Single mode is on") :
174 _("Single mode is off"));
176 if (consume != mpd_status_get_consume(c->status))
177 screen_status_printf(mpd_status_get_consume(c->status) ?
178 /* "consume" mode means
179 that MPD removes each
180 song which has
181 finished playing */
182 _("Consume mode is on") :
183 _("Consume mode is off"));
185 if (crossfade != mpd_status_get_crossfade(c->status))
186 screen_status_printf(_("Crossfade %d seconds"),
187 mpd_status_get_crossfade(c->status));
189 repeat = mpd_status_get_repeat(c->status);
190 random_enabled = mpd_status_get_random(c->status);
191 single = mpd_status_get_single(c->status);
192 consume = mpd_status_get_consume(c->status);
193 crossfade = mpd_status_get_crossfade(c->status);
196 if ((c->events & MPD_IDLE_DATABASE) != 0 && was_connected &&
197 mpdclient_is_connected(c))
198 screen_status_printf(_("Database updated"));
199 was_connected = mpdclient_is_connected(c);
200 #endif
202 /* update the main window */
203 if (screen.current_page->update != NULL)
204 screen.current_page->update(c);
206 screen_paint(c, false);
209 #ifdef HAVE_GETMOUSE
211 screen_get_mouse_event(struct mpdclient *c, unsigned long *bstate, int *row)
213 MEVENT event;
215 /* retrieve the mouse event from curses */
216 #ifdef PDCURSES
217 nc_getmouse(&event);
218 #else
219 getmouse(&event);
220 #endif
221 /* calculate the selected row in the list window */
222 *row = event.y - screen.title_bar.window.rows;
223 /* copy button state bits */
224 *bstate = event.bstate;
225 /* if button 2 was pressed switch screen */
226 if (event.bstate & BUTTON2_CLICKED) {
227 screen_cmd(c, CMD_SCREEN_NEXT);
228 return 1;
231 return 0;
233 #endif
235 void
236 screen_cmd(struct mpdclient *c, command_t cmd)
238 #ifndef NCMPC_MINI
239 if (screen.welcome_source_id != 0) {
240 g_source_remove(screen.welcome_source_id);
241 screen.welcome_source_id = 0;
243 #endif
245 if (screen.current_page->cmd != NULL &&
246 screen.current_page->cmd(c, cmd))
247 return;
249 if (handle_player_command(c, cmd))
250 return;
252 switch(cmd) {
253 case CMD_TOGGLE_FIND_WRAP:
254 options.find_wrap = !options.find_wrap;
255 screen_status_printf(options.find_wrap ?
256 _("Find mode: Wrapped") :
257 _("Find mode: Normal"));
258 break;
259 case CMD_TOGGLE_AUTOCENTER:
260 options.auto_center = !options.auto_center;
261 screen_status_printf(options.auto_center ?
262 _("Auto center mode: On") :
263 _("Auto center mode: Off"));
264 break;
265 case CMD_SCREEN_UPDATE:
266 screen_paint(c, true);
267 break;
268 case CMD_SCREEN_PREVIOUS:
269 screen_next_mode(c, -1);
270 break;
271 case CMD_SCREEN_NEXT:
272 screen_next_mode(c, 1);
273 break;
274 case CMD_SCREEN_PLAY:
275 screen_switch(&screen_queue, c);
276 break;
277 case CMD_SCREEN_FILE:
278 screen_switch(&screen_browse, c);
279 break;
280 #ifdef ENABLE_HELP_SCREEN
281 case CMD_SCREEN_HELP:
282 screen_switch(&screen_help, c);
283 break;
284 #endif
285 #ifdef ENABLE_SEARCH_SCREEN
286 case CMD_SCREEN_SEARCH:
287 screen_switch(&screen_search, c);
288 break;
289 #endif
290 #ifdef ENABLE_ARTIST_SCREEN
291 case CMD_SCREEN_ARTIST:
292 screen_switch(&screen_artist, c);
293 break;
294 #endif
295 #ifdef ENABLE_SONG_SCREEN
296 case CMD_SCREEN_SONG:
297 screen_switch(&screen_song, c);
298 break;
299 #endif
300 #ifdef ENABLE_KEYDEF_SCREEN
301 case CMD_SCREEN_KEYDEF:
302 screen_switch(&screen_keydef, c);
303 break;
304 #endif
305 #ifdef ENABLE_LYRICS_SCREEN
306 case CMD_SCREEN_LYRICS:
307 screen_switch(&screen_lyrics, c);
308 break;
309 #endif
310 #ifdef ENABLE_OUTPUTS_SCREEN
311 case CMD_SCREEN_OUTPUTS:
312 screen_switch(&screen_outputs, c);
313 break;
314 #endif
315 #ifdef ENABLE_CHAT_SCREEN
316 case CMD_SCREEN_CHAT:
317 screen_switch(&screen_chat, c);
318 break;
319 #endif
320 case CMD_SCREEN_SWAP:
321 screen_swap(c, NULL);
322 break;
324 default:
325 break;