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 int SCREEN_MIN_COLS = 14;
59 static const int 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);
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 char *header, const struct mpdclient *c)
152 {
153 title_bar_paint(&screen.title_bar, header, c->status);
154 }
156 static void
157 update_progress_window(struct mpdclient *c, bool repaint)
158 {
159 unsigned elapsed;
160 if (c->status == NULL)
161 elapsed = 0;
162 else if (seek_id >= 0 && seek_id == mpd_status_get_song_id(c->status))
163 elapsed = seek_target_time;
164 else
165 elapsed = mpd_status_get_elapsed_time(c->status);
167 unsigned duration = mpdclient_is_playing(c)
168 ? mpd_status_get_total_time(c->status)
169 : 0;
171 if (progress_bar_set(&screen.progress_bar, elapsed, duration) ||
172 repaint)
173 progress_bar_paint(&screen.progress_bar);
174 }
176 void
177 screen_exit(void)
178 {
179 if (mode_fn->close != NULL)
180 mode_fn->close();
182 screen_list_exit();
184 string_list_free(screen.find_history);
185 g_free(screen.buf);
186 g_free(screen.findbuf);
188 title_bar_deinit(&screen.title_bar);
189 delwin(screen.main_window.w);
190 progress_bar_deinit(&screen.progress_bar);
191 status_bar_deinit(&screen.status_bar);
193 #ifndef NCMPC_MINI
194 if (screen.welcome_source_id != 0)
195 g_source_remove(screen.welcome_source_id);
196 #endif
197 }
199 void
200 screen_resize(struct mpdclient *c)
201 {
202 if (COLS<SCREEN_MIN_COLS || LINES<SCREEN_MIN_ROWS) {
203 screen_exit();
204 fprintf(stderr, "%s\n", _("Error: Screen too small"));
205 exit(EXIT_FAILURE);
206 }
207 #ifdef PDCURSES
208 resize_term(LINES, COLS);
209 #else
210 resizeterm(LINES, COLS);
211 #endif
213 screen.cols = COLS;
214 screen.rows = LINES;
216 title_bar_resize(&screen.title_bar, screen.cols);
218 /* main window */
219 screen.main_window.cols = screen.cols;
220 screen.main_window.rows = screen.rows-4;
221 wresize(screen.main_window.w, screen.main_window.rows, screen.cols);
222 wclear(screen.main_window.w);
224 /* progress window */
225 progress_bar_resize(&screen.progress_bar, screen.cols,
226 screen.rows - 2, 0);
227 progress_bar_paint(&screen.progress_bar);
229 /* status window */
230 status_bar_resize(&screen.status_bar, screen.cols, screen.rows - 1, 0);
231 status_bar_paint(&screen.status_bar, c->status, c->song);
233 screen.buf_size = screen.cols;
234 g_free(screen.buf);
235 screen.buf = g_malloc(screen.cols);
237 /* resize all screens */
238 screen_list_resize(screen.main_window.cols, screen.main_window.rows);
240 /* ? - without this the cursor becomes visible with aterm & Eterm */
241 curs_set(1);
242 curs_set(0);
244 screen_paint(c);
245 }
247 #ifndef NCMPC_MINI
248 static gboolean
249 welcome_timer_callback(gpointer data)
250 {
251 struct mpdclient *c = data;
253 #ifndef NCMPC_MINI
254 screen.welcome_source_id = 0;
255 #endif
257 paint_top_window(mode_fn->get_title != NULL
258 ? mode_fn->get_title(screen.buf, screen.buf_size)
259 : "",
260 c);
261 doupdate();
263 return false;
264 }
265 #endif
267 void
268 screen_init(struct mpdclient *c)
269 {
270 if (COLS < SCREEN_MIN_COLS || LINES < SCREEN_MIN_ROWS) {
271 fprintf(stderr, "%s\n", _("Error: Screen too small"));
272 exit(EXIT_FAILURE);
273 }
275 screen.cols = COLS;
276 screen.rows = LINES;
278 screen.buf = g_malloc(screen.cols);
279 screen.buf_size = screen.cols;
280 screen.findbuf = NULL;
282 #ifndef NCMPC_MINI
283 if (options.welcome_screen_list)
284 screen.welcome_source_id =
285 g_timeout_add(SCREEN_WELCOME_TIME * 1000,
286 welcome_timer_callback, c);
287 #endif
289 /* create top window */
290 title_bar_init(&screen.title_bar, screen.cols, 0, 0);
292 /* create main window */
293 window_init(&screen.main_window, screen.rows - 4, screen.cols, 2, 0);
295 if (!options.hardware_cursor)
296 leaveok(screen.main_window.w, TRUE);
298 keypad(screen.main_window.w, TRUE);
300 /* create progress window */
301 progress_bar_init(&screen.progress_bar, screen.cols,
302 screen.rows - 2, 0);
304 /* create status window */
305 status_bar_init(&screen.status_bar, screen.cols, screen.rows - 1, 0);
307 #ifdef ENABLE_COLORS
308 if (options.enable_colors) {
309 /* set background attributes */
310 wbkgd(stdscr, COLOR_PAIR(COLOR_LIST));
311 wbkgd(screen.main_window.w, COLOR_PAIR(COLOR_LIST));
312 wbkgd(screen.title_bar.window.w, COLOR_PAIR(COLOR_TITLE));
313 wbkgd(screen.progress_bar.window.w,
314 COLOR_PAIR(COLOR_PROGRESSBAR));
315 wbkgd(screen.status_bar.window.w, COLOR_PAIR(COLOR_STATUS));
316 colors_use(screen.progress_bar.window.w, COLOR_PROGRESSBAR);
317 }
318 #endif
320 /* initialize screens */
321 screen_list_init(screen.main_window.w,
322 screen.main_window.cols, screen.main_window.rows);
324 if (mode_fn->open != NULL)
325 mode_fn->open(c);
326 }
328 static void
329 screen_refresh(struct mpdclient *c, bool main_dirty)
330 {
331 /* update title/header window */
332 const char *title =
333 #ifndef NCMPC_MINI
334 screen.welcome_source_id == 0 &&
335 #endif
336 mode_fn->get_title != NULL
337 ? mode_fn->get_title(screen.buf, screen.buf_size)
338 : "";
339 assert(title != NULL);
340 paint_top_window(title, c);
342 /* paint the bottom window */
344 update_progress_window(c, true);
345 status_bar_paint(&screen.status_bar, c->status, c->song);
347 /* paint the main window */
349 if (main_dirty) {
350 wclear(screen.main_window.w);
351 if (mode_fn->paint != NULL)
352 mode_fn->paint();
353 }
355 /* move the cursor to the origin */
357 if (!options.hardware_cursor)
358 wmove(screen.main_window.w, 0, 0);
360 wnoutrefresh(screen.main_window.w);
362 /* tell curses to update */
363 doupdate();
364 }
366 void
367 screen_paint(struct mpdclient *c)
368 {
369 screen_refresh(c, true);
370 }
372 void
373 screen_update(struct mpdclient *c)
374 {
375 #ifndef NCMPC_MINI
376 static bool was_connected;
377 static bool initialized = false;
378 static bool repeat;
379 static bool random_enabled;
380 static bool single;
381 static bool consume;
382 static unsigned crossfade;
384 /* print a message if mpd status has changed */
385 if ((c->events & MPD_IDLE_OPTIONS) && c->status != NULL) {
386 if (!initialized) {
387 repeat = mpd_status_get_repeat(c->status);
388 random_enabled = mpd_status_get_random(c->status);
389 single = mpd_status_get_single(c->status);
390 consume = mpd_status_get_consume(c->status);
391 crossfade = mpd_status_get_crossfade(c->status);
392 initialized = true;
393 }
395 if (repeat != mpd_status_get_repeat(c->status))
396 screen_status_printf(mpd_status_get_repeat(c->status) ?
397 _("Repeat mode is on") :
398 _("Repeat mode is off"));
400 if (random_enabled != mpd_status_get_random(c->status))
401 screen_status_printf(mpd_status_get_random(c->status) ?
402 _("Random mode is on") :
403 _("Random mode is off"));
405 if (single != mpd_status_get_single(c->status))
406 screen_status_printf(mpd_status_get_single(c->status) ?
407 /* "single" mode means
408 that MPD will
409 automatically stop
410 after playing one
411 single song */
412 _("Single mode is on") :
413 _("Single mode is off"));
415 if (consume != mpd_status_get_consume(c->status))
416 screen_status_printf(mpd_status_get_consume(c->status) ?
417 /* "consume" mode means
418 that MPD removes each
419 song which has
420 finished playing */
421 _("Consume mode is on") :
422 _("Consume mode is off"));
424 if (crossfade != mpd_status_get_crossfade(c->status))
425 screen_status_printf(_("Crossfade %d seconds"),
426 mpd_status_get_crossfade(c->status));
428 repeat = mpd_status_get_repeat(c->status);
429 random_enabled = mpd_status_get_random(c->status);
430 single = mpd_status_get_single(c->status);
431 consume = mpd_status_get_consume(c->status);
432 crossfade = mpd_status_get_crossfade(c->status);
433 }
435 if ((c->events & MPD_IDLE_DATABASE) != 0 && was_connected &&
436 mpdclient_is_connected(c))
437 screen_status_printf(_("Database updated"));
438 was_connected = mpdclient_is_connected(c);
439 #endif
441 /* update the main window */
442 if (mode_fn->update != NULL)
443 mode_fn->update(c);
445 screen_refresh(c, false);
446 }
448 #ifdef HAVE_GETMOUSE
449 int
450 screen_get_mouse_event(struct mpdclient *c, unsigned long *bstate, int *row)
451 {
452 MEVENT event;
454 /* retrieve the mouse event from curses */
455 #ifdef PDCURSES
456 nc_getmouse(&event);
457 #else
458 getmouse(&event);
459 #endif
460 /* calculate the selected row in the list window */
461 *row = event.y - screen.title_bar.window.rows;
462 /* copy button state bits */
463 *bstate = event.bstate;
464 /* if button 2 was pressed switch screen */
465 if (event.bstate & BUTTON2_CLICKED) {
466 screen_cmd(c, CMD_SCREEN_NEXT);
467 return 1;
468 }
470 return 0;
471 }
472 #endif
474 void
475 screen_cmd(struct mpdclient *c, command_t cmd)
476 {
477 #ifndef NCMPC_MINI
478 if (screen.welcome_source_id != 0) {
479 g_source_remove(screen.welcome_source_id);
480 screen.welcome_source_id = 0;
481 }
482 #endif
484 if (mode_fn->cmd != NULL && mode_fn->cmd(c, cmd))
485 return;
487 if (handle_player_command(c, cmd))
488 return;
490 switch(cmd) {
491 case CMD_TOGGLE_FIND_WRAP:
492 options.find_wrap = !options.find_wrap;
493 screen_status_printf(options.find_wrap ?
494 _("Find mode: Wrapped") :
495 _("Find mode: Normal"));
496 break;
497 case CMD_TOGGLE_AUTOCENTER:
498 options.auto_center = !options.auto_center;
499 screen_status_printf(options.auto_center ?
500 _("Auto center mode: On") :
501 _("Auto center mode: Off"));
502 break;
503 case CMD_SCREEN_UPDATE:
504 screen_paint(c);
505 break;
506 case CMD_SCREEN_PREVIOUS:
507 screen_next_mode(c, -1);
508 break;
509 case CMD_SCREEN_NEXT:
510 screen_next_mode(c, 1);
511 break;
512 case CMD_SCREEN_PLAY:
513 screen_switch(&screen_queue, c);
514 break;
515 case CMD_SCREEN_FILE:
516 screen_switch(&screen_browse, c);
517 break;
518 #ifdef ENABLE_HELP_SCREEN
519 case CMD_SCREEN_HELP:
520 screen_switch(&screen_help, c);
521 break;
522 #endif
523 #ifdef ENABLE_SEARCH_SCREEN
524 case CMD_SCREEN_SEARCH:
525 screen_switch(&screen_search, c);
526 break;
527 #endif
528 #ifdef ENABLE_ARTIST_SCREEN
529 case CMD_SCREEN_ARTIST:
530 screen_switch(&screen_artist, c);
531 break;
532 #endif
533 #ifdef ENABLE_SONG_SCREEN
534 case CMD_SCREEN_SONG:
535 screen_switch(&screen_song, c);
536 break;
537 #endif
538 #ifdef ENABLE_KEYDEF_SCREEN
539 case CMD_SCREEN_KEYDEF:
540 screen_switch(&screen_keydef, c);
541 break;
542 #endif
543 #ifdef ENABLE_LYRICS_SCREEN
544 case CMD_SCREEN_LYRICS:
545 screen_switch(&screen_lyrics, c);
546 break;
547 #endif
548 #ifdef ENABLE_OUTPUTS_SCREEN
549 case CMD_SCREEN_OUTPUTS:
550 screen_switch(&screen_outputs, c);
551 break;
552 #endif
553 #ifdef ENABLE_CHAT_SCREEN
554 case CMD_SCREEN_CHAT:
555 screen_switch(&screen_chat, c);
556 break;
557 #endif
558 case CMD_SCREEN_SWAP:
559 screen_swap(c, NULL);
560 break;
562 default:
563 break;
564 }
565 }