0d242de017fe6d1d34110a74250a664ef92183cc
1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2017 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 */
20 #include "screen.h"
21 #include "screen_interface.h"
22 #include "screen_list.h"
23 #include "screen_utils.h"
24 #include "screen_status.h"
25 #include "config.h"
26 #include "i18n.h"
27 #include "charset.h"
28 #include "mpdclient.h"
29 #include "utils.h"
30 #include "options.h"
31 #include "colors.h"
32 #include "player_command.h"
33 #include "screen_help.h"
34 #include "screen_queue.h"
35 #include "screen_file.h"
36 #include "screen_artist.h"
37 #include "screen_search.h"
38 #include "screen_song.h"
39 #include "screen_keydef.h"
40 #include "screen_lyrics.h"
41 #include "screen_outputs.h"
42 #include "screen_chat.h"
44 #include <mpd/client.h>
46 #include <stdlib.h>
47 #include <unistd.h>
48 #include <string.h>
49 #include <time.h>
50 #include <locale.h>
52 #ifndef NCMPC_MINI
53 /** welcome message time [s] */
54 static const GTime SCREEN_WELCOME_TIME = 10;
55 #endif
57 /* minimum window size */
58 static const unsigned SCREEN_MIN_COLS = 14;
59 static const unsigned SCREEN_MIN_ROWS = 5;
61 /* screens */
63 struct screen screen;
64 static const struct screen_functions *mode_fn = &screen_queue;
65 static const struct screen_functions *mode_fn_prev = &screen_queue;
67 gboolean
68 screen_is_visible(const struct screen_functions *sf)
69 {
70 return sf == mode_fn;
71 }
73 void
74 screen_switch(const struct screen_functions *sf, struct mpdclient *c)
75 {
76 assert(sf != NULL);
78 if (sf == mode_fn)
79 return;
81 mode_fn_prev = mode_fn;
83 /* close the old mode */
84 if (mode_fn->close != NULL)
85 mode_fn->close();
87 /* get functions for the new mode */
88 mode_fn = sf;
90 /* open the new mode */
91 if (mode_fn->open != NULL)
92 mode_fn->open(c);
94 screen_paint(c, true);
95 }
97 void
98 screen_swap(struct mpdclient *c, const struct mpd_song *song)
99 {
100 if (song != NULL)
101 {
102 if (false)
103 { /* just a hack to make the ifdefs less ugly */ }
104 #ifdef ENABLE_SONG_SCREEN
105 if (mode_fn_prev == &screen_song)
106 screen_song_switch(c, song);
107 #endif
108 #ifdef ENABLE_LYRICS_SCREEN
109 else if (mode_fn_prev == &screen_lyrics)
110 screen_lyrics_switch(c, song, true);
111 #endif
112 else
113 screen_switch(mode_fn_prev, c);
114 }
115 else
116 screen_switch(mode_fn_prev, c);
117 }
119 static int
120 find_configured_screen(const char *name)
121 {
122 unsigned i;
124 for (i = 0; options.screen_list[i] != NULL; ++i)
125 if (strcmp(options.screen_list[i], name) == 0)
126 return i;
128 return -1;
129 }
131 static void
132 screen_next_mode(struct mpdclient *c, int offset)
133 {
134 int max = g_strv_length(options.screen_list);
136 /* find current screen */
137 int current = find_configured_screen(screen_get_name(mode_fn));
138 int next = current + offset;
139 if (next<0)
140 next = max-1;
141 else if (next>=max)
142 next = 0;
144 const struct screen_functions *sf =
145 screen_lookup_name(options.screen_list[next]);
146 if (sf != NULL)
147 screen_switch(sf, c);
148 }
150 static void
151 paint_top_window(const struct mpdclient *c)
152 {
153 const char *title =
154 #ifndef NCMPC_MINI
155 screen.welcome_source_id == 0 &&
156 #endif
157 mode_fn->get_title != NULL
158 ? mode_fn->get_title(screen.buf, screen.buf_size)
159 : "";
160 assert(title != NULL);
162 title_bar_paint(&screen.title_bar, title, c->status);
163 }
165 static void
166 update_progress_window(struct mpdclient *c, bool repaint)
167 {
168 unsigned elapsed;
169 if (c->status == NULL)
170 elapsed = 0;
171 else if (seek_id >= 0 && seek_id == mpd_status_get_song_id(c->status))
172 elapsed = seek_target_time;
173 else
174 elapsed = mpd_status_get_elapsed_time(c->status);
176 unsigned duration = mpdclient_is_playing(c)
177 ? mpd_status_get_total_time(c->status)
178 : 0;
180 if (progress_bar_set(&screen.progress_bar, elapsed, duration) ||
181 repaint)
182 progress_bar_paint(&screen.progress_bar);
183 }
185 void
186 screen_exit(void)
187 {
188 if (mode_fn->close != NULL)
189 mode_fn->close();
191 screen_list_exit();
193 string_list_free(screen.find_history);
194 g_free(screen.buf);
195 g_free(screen.findbuf);
197 title_bar_deinit(&screen.title_bar);
198 delwin(screen.main_window.w);
199 progress_bar_deinit(&screen.progress_bar);
200 status_bar_deinit(&screen.status_bar);
202 #ifndef NCMPC_MINI
203 if (screen.welcome_source_id != 0)
204 g_source_remove(screen.welcome_source_id);
205 #endif
206 }
208 void
209 screen_resize(struct mpdclient *c)
210 {
211 const unsigned cols = COLS, rows = LINES;
212 if (cols < SCREEN_MIN_COLS || rows < SCREEN_MIN_ROWS) {
213 screen_exit();
214 fprintf(stderr, "%s\n", _("Error: Screen too small"));
215 exit(EXIT_FAILURE);
216 }
218 #ifdef PDCURSES
219 resize_term(rows, cols);
220 #else
221 resizeterm(rows, cols);
222 #endif
224 title_bar_resize(&screen.title_bar, cols);
226 /* main window */
227 screen.main_window.cols = cols;
228 screen.main_window.rows = rows - 4;
229 wresize(screen.main_window.w, screen.main_window.rows, cols);
231 /* progress window */
232 progress_bar_resize(&screen.progress_bar, cols, rows - 2, 0);
234 /* status window */
235 status_bar_resize(&screen.status_bar, cols, rows - 1, 0);
237 screen.buf_size = cols;
238 g_free(screen.buf);
239 screen.buf = g_malloc(cols);
241 /* resize all screens */
242 screen_list_resize(screen.main_window.cols, screen.main_window.rows);
244 /* ? - without this the cursor becomes visible with aterm & Eterm */
245 curs_set(1);
246 curs_set(0);
248 screen_paint(c, true);
249 }
251 #ifndef NCMPC_MINI
252 static gboolean
253 welcome_timer_callback(gpointer data)
254 {
255 struct mpdclient *c = data;
257 screen.welcome_source_id = 0;
259 paint_top_window(c);
260 doupdate();
262 return false;
263 }
264 #endif
266 void
267 screen_init(struct mpdclient *c)
268 {
269 const unsigned cols = COLS, rows = LINES;
270 if (cols < SCREEN_MIN_COLS || rows < SCREEN_MIN_ROWS) {
271 fprintf(stderr, "%s\n", _("Error: Screen too small"));
272 exit(EXIT_FAILURE);
273 }
275 screen.buf = g_malloc(cols);
276 screen.buf_size = cols;
277 screen.findbuf = NULL;
279 #ifndef NCMPC_MINI
280 if (options.welcome_screen_list)
281 screen.welcome_source_id =
282 g_timeout_add_seconds(SCREEN_WELCOME_TIME,
283 welcome_timer_callback, c);
284 #endif
286 /* create top window */
287 title_bar_init(&screen.title_bar, cols, 0, 0);
289 /* create main window */
290 window_init(&screen.main_window, rows - 4, cols, 2, 0);
292 if (!options.hardware_cursor)
293 leaveok(screen.main_window.w, TRUE);
295 keypad(screen.main_window.w, TRUE);
297 /* create progress window */
298 progress_bar_init(&screen.progress_bar, cols, rows - 2, 0);
300 /* create status window */
301 status_bar_init(&screen.status_bar, cols, rows - 1, 0);
303 #ifdef ENABLE_COLORS
304 if (options.enable_colors) {
305 /* set background attributes */
306 wbkgd(stdscr, COLOR_PAIR(COLOR_LIST));
307 wbkgd(screen.main_window.w, COLOR_PAIR(COLOR_LIST));
308 wbkgd(screen.title_bar.window.w, COLOR_PAIR(COLOR_TITLE));
309 wbkgd(screen.progress_bar.window.w,
310 COLOR_PAIR(COLOR_PROGRESSBAR));
311 wbkgd(screen.status_bar.window.w, COLOR_PAIR(COLOR_STATUS));
312 colors_use(screen.progress_bar.window.w, COLOR_PROGRESSBAR);
313 }
314 #endif
316 /* initialize screens */
317 screen_list_init(screen.main_window.w,
318 screen.main_window.cols, screen.main_window.rows);
320 if (mode_fn->open != NULL)
321 mode_fn->open(c);
322 }
324 void
325 screen_paint(struct mpdclient *c, bool main_dirty)
326 {
327 /* update title/header window */
328 paint_top_window(c);
330 /* paint the bottom window */
332 update_progress_window(c, true);
333 status_bar_paint(&screen.status_bar, c->status, c->song);
335 /* paint the main window */
337 if (main_dirty) {
338 wclear(screen.main_window.w);
339 if (mode_fn->paint != NULL)
340 mode_fn->paint();
341 }
343 /* move the cursor to the origin */
345 if (!options.hardware_cursor)
346 wmove(screen.main_window.w, 0, 0);
348 wnoutrefresh(screen.main_window.w);
350 /* tell curses to update */
351 doupdate();
352 }
354 void
355 screen_update(struct mpdclient *c)
356 {
357 #ifndef NCMPC_MINI
358 static bool was_connected;
359 static bool initialized = false;
360 static bool repeat;
361 static bool random_enabled;
362 static bool single;
363 static bool consume;
364 static unsigned crossfade;
366 /* print a message if mpd status has changed */
367 if ((c->events & MPD_IDLE_OPTIONS) && c->status != NULL) {
368 if (!initialized) {
369 repeat = mpd_status_get_repeat(c->status);
370 random_enabled = mpd_status_get_random(c->status);
371 single = mpd_status_get_single(c->status);
372 consume = mpd_status_get_consume(c->status);
373 crossfade = mpd_status_get_crossfade(c->status);
374 initialized = true;
375 }
377 if (repeat != mpd_status_get_repeat(c->status))
378 screen_status_printf(mpd_status_get_repeat(c->status) ?
379 _("Repeat mode is on") :
380 _("Repeat mode is off"));
382 if (random_enabled != mpd_status_get_random(c->status))
383 screen_status_printf(mpd_status_get_random(c->status) ?
384 _("Random mode is on") :
385 _("Random mode is off"));
387 if (single != mpd_status_get_single(c->status))
388 screen_status_printf(mpd_status_get_single(c->status) ?
389 /* "single" mode means
390 that MPD will
391 automatically stop
392 after playing one
393 single song */
394 _("Single mode is on") :
395 _("Single mode is off"));
397 if (consume != mpd_status_get_consume(c->status))
398 screen_status_printf(mpd_status_get_consume(c->status) ?
399 /* "consume" mode means
400 that MPD removes each
401 song which has
402 finished playing */
403 _("Consume mode is on") :
404 _("Consume mode is off"));
406 if (crossfade != mpd_status_get_crossfade(c->status))
407 screen_status_printf(_("Crossfade %d seconds"),
408 mpd_status_get_crossfade(c->status));
410 repeat = mpd_status_get_repeat(c->status);
411 random_enabled = mpd_status_get_random(c->status);
412 single = mpd_status_get_single(c->status);
413 consume = mpd_status_get_consume(c->status);
414 crossfade = mpd_status_get_crossfade(c->status);
415 }
417 if ((c->events & MPD_IDLE_DATABASE) != 0 && was_connected &&
418 mpdclient_is_connected(c))
419 screen_status_printf(_("Database updated"));
420 was_connected = mpdclient_is_connected(c);
421 #endif
423 /* update the main window */
424 if (mode_fn->update != NULL)
425 mode_fn->update(c);
427 screen_paint(c, false);
428 }
430 #ifdef HAVE_GETMOUSE
431 int
432 screen_get_mouse_event(struct mpdclient *c, unsigned long *bstate, int *row)
433 {
434 MEVENT event;
436 /* retrieve the mouse event from curses */
437 #ifdef PDCURSES
438 nc_getmouse(&event);
439 #else
440 getmouse(&event);
441 #endif
442 /* calculate the selected row in the list window */
443 *row = event.y - screen.title_bar.window.rows;
444 /* copy button state bits */
445 *bstate = event.bstate;
446 /* if button 2 was pressed switch screen */
447 if (event.bstate & BUTTON2_CLICKED) {
448 screen_cmd(c, CMD_SCREEN_NEXT);
449 return 1;
450 }
452 return 0;
453 }
454 #endif
456 void
457 screen_cmd(struct mpdclient *c, command_t cmd)
458 {
459 #ifndef NCMPC_MINI
460 if (screen.welcome_source_id != 0) {
461 g_source_remove(screen.welcome_source_id);
462 screen.welcome_source_id = 0;
463 }
464 #endif
466 if (mode_fn->cmd != NULL && mode_fn->cmd(c, cmd))
467 return;
469 if (handle_player_command(c, cmd))
470 return;
472 switch(cmd) {
473 case CMD_TOGGLE_FIND_WRAP:
474 options.find_wrap = !options.find_wrap;
475 screen_status_printf(options.find_wrap ?
476 _("Find mode: Wrapped") :
477 _("Find mode: Normal"));
478 break;
479 case CMD_TOGGLE_AUTOCENTER:
480 options.auto_center = !options.auto_center;
481 screen_status_printf(options.auto_center ?
482 _("Auto center mode: On") :
483 _("Auto center mode: Off"));
484 break;
485 case CMD_SCREEN_UPDATE:
486 screen_paint(c, true);
487 break;
488 case CMD_SCREEN_PREVIOUS:
489 screen_next_mode(c, -1);
490 break;
491 case CMD_SCREEN_NEXT:
492 screen_next_mode(c, 1);
493 break;
494 case CMD_SCREEN_PLAY:
495 screen_switch(&screen_queue, c);
496 break;
497 case CMD_SCREEN_FILE:
498 screen_switch(&screen_browse, c);
499 break;
500 #ifdef ENABLE_HELP_SCREEN
501 case CMD_SCREEN_HELP:
502 screen_switch(&screen_help, c);
503 break;
504 #endif
505 #ifdef ENABLE_SEARCH_SCREEN
506 case CMD_SCREEN_SEARCH:
507 screen_switch(&screen_search, c);
508 break;
509 #endif
510 #ifdef ENABLE_ARTIST_SCREEN
511 case CMD_SCREEN_ARTIST:
512 screen_switch(&screen_artist, c);
513 break;
514 #endif
515 #ifdef ENABLE_SONG_SCREEN
516 case CMD_SCREEN_SONG:
517 screen_switch(&screen_song, c);
518 break;
519 #endif
520 #ifdef ENABLE_KEYDEF_SCREEN
521 case CMD_SCREEN_KEYDEF:
522 screen_switch(&screen_keydef, c);
523 break;
524 #endif
525 #ifdef ENABLE_LYRICS_SCREEN
526 case CMD_SCREEN_LYRICS:
527 screen_switch(&screen_lyrics, c);
528 break;
529 #endif
530 #ifdef ENABLE_OUTPUTS_SCREEN
531 case CMD_SCREEN_OUTPUTS:
532 screen_switch(&screen_outputs, c);
533 break;
534 #endif
535 #ifdef ENABLE_CHAT_SCREEN
536 case CMD_SCREEN_CHAT:
537 screen_switch(&screen_chat, c);
538 break;
539 #endif
540 case CMD_SCREEN_SWAP:
541 screen_swap(c, NULL);
542 break;
544 default:
545 break;
546 }
547 }