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 "screen.h"
21 #include "screen_interface.h"
22 #include "screen_list.h"
23 #include "screen_utils.h"
24 #include "config.h"
25 #include "i18n.h"
26 #include "charset.h"
27 #include "mpdclient.h"
28 #include "utils.h"
29 #include "options.h"
30 #include "colors.h"
31 #include "player_command.h"
33 #include <mpd/client.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 #include <stdarg.h>
38 #include <string.h>
39 #include <time.h>
40 #include <locale.h>
42 #ifndef NCMPC_MINI
43 /** welcome message time [s] */
44 static const GTime SCREEN_WELCOME_TIME = 10;
45 #endif
47 /* minimum window size */
48 static const int SCREEN_MIN_COLS = 14;
49 static const int SCREEN_MIN_ROWS = 5;
51 /* screens */
53 #ifndef NCMPC_MINI
54 static gboolean welcome = TRUE;
55 #endif
57 struct screen screen;
58 static const struct screen_functions *mode_fn = &screen_playlist;
59 static const struct screen_functions *mode_fn_prev = &screen_playlist;
61 gboolean
62 screen_is_visible(const struct screen_functions *sf)
63 {
64 return sf == mode_fn;
65 }
67 void
68 screen_switch(const struct screen_functions *sf, struct mpdclient *c)
69 {
70 assert(sf != NULL);
72 if (sf == mode_fn)
73 return;
75 mode_fn_prev = mode_fn;
77 /* close the old mode */
78 if (mode_fn->close != NULL)
79 mode_fn->close();
81 /* get functions for the new mode */
82 mode_fn = sf;
84 /* open the new mode */
85 if (mode_fn->open != NULL)
86 mode_fn->open(c);
88 screen_paint(c);
89 }
91 void
92 screen_swap(struct mpdclient *c, const struct mpd_song *song)
93 {
94 if (song != NULL)
95 {
96 if (false)
97 { /* just a hack to make the ifdefs less ugly */ }
98 #ifdef ENABLE_SONG_SCREEN
99 if (mode_fn_prev == &screen_song)
100 screen_song_switch(c, song);
101 #endif
102 #ifdef ENABLE_LYRICS_SCREEN
103 else if (mode_fn_prev == &screen_lyrics)
104 screen_lyrics_switch(c, song, true);
105 #endif
106 else
107 screen_switch(mode_fn_prev, c);
108 }
109 else
110 screen_switch(mode_fn_prev, c);
111 }
113 static int
114 find_configured_screen(const char *name)
115 {
116 unsigned i;
118 for (i = 0; options.screen_list[i] != NULL; ++i)
119 if (strcmp(options.screen_list[i], name) == 0)
120 return i;
122 return -1;
123 }
125 static void
126 screen_next_mode(struct mpdclient *c, int offset)
127 {
128 int max = g_strv_length(options.screen_list);
129 int current, next;
130 const struct screen_functions *sf;
132 /* find current screen */
133 current = find_configured_screen(screen_get_name(mode_fn));
134 next = current + offset;
135 if (next<0)
136 next = max-1;
137 else if (next>=max)
138 next = 0;
140 sf = screen_lookup_name(options.screen_list[next]);
141 if (sf != NULL)
142 screen_switch(sf, c);
143 }
145 static void
146 paint_top_window(const char *header, const struct mpdclient *c)
147 {
148 title_bar_paint(&screen.title_bar, header, c->status);
149 }
151 static void
152 paint_progress_window(struct mpdclient *c)
153 {
154 unsigned elapsed, duration;
156 if (c->song != NULL && seek_id == (int)mpd_song_get_id(c->song))
157 elapsed = seek_target_time;
158 else if (c->status != NULL)
159 elapsed = mpd_status_get_elapsed_time(c->status);
160 else
161 elapsed = 0;
163 duration = c->status != NULL &&
164 !IS_STOPPED(mpd_status_get_state(c->status))
165 ? mpd_status_get_total_time(c->status)
166 : 0;
168 if (progress_bar_set(&screen.progress_bar, elapsed, duration))
169 progress_bar_paint(&screen.progress_bar);
170 }
172 void
173 screen_exit(void)
174 {
175 if (mode_fn->close != NULL)
176 mode_fn->close();
178 screen_list_exit();
180 string_list_free(screen.find_history);
181 g_free(screen.buf);
182 g_free(screen.findbuf);
184 title_bar_deinit(&screen.title_bar);
185 delwin(screen.main_window.w);
186 progress_bar_deinit(&screen.progress_bar);
187 status_bar_deinit(&screen.status_bar);
188 }
190 void
191 screen_resize(struct mpdclient *c)
192 {
193 if (COLS<SCREEN_MIN_COLS || LINES<SCREEN_MIN_ROWS) {
194 screen_exit();
195 fprintf(stderr, "%s", _("Error: Screen too small"));
196 exit(EXIT_FAILURE);
197 }
199 resizeterm(LINES, COLS);
201 screen.cols = COLS;
202 screen.rows = LINES;
204 title_bar_resize(&screen.title_bar, screen.cols);
206 /* main window */
207 screen.main_window.cols = screen.cols;
208 screen.main_window.rows = screen.rows-4;
209 wresize(screen.main_window.w, screen.main_window.rows, screen.cols);
210 wclear(screen.main_window.w);
212 /* progress window */
213 progress_bar_resize(&screen.progress_bar, screen.cols,
214 screen.rows - 2, 0);
215 progress_bar_paint(&screen.progress_bar);
217 /* status window */
218 status_bar_resize(&screen.status_bar, screen.cols, screen.rows - 1, 0);
219 status_bar_paint(&screen.status_bar, c->status, c->song);
221 screen.buf_size = screen.cols;
222 g_free(screen.buf);
223 screen.buf = g_malloc(screen.cols);
225 /* resize all screens */
226 screen_list_resize(screen.main_window.cols, screen.main_window.rows);
228 /* ? - without this the cursor becomes visible with aterm & Eterm */
229 curs_set(1);
230 curs_set(0);
232 screen_paint(c);
233 }
235 void
236 screen_status_message(const char *msg)
237 {
238 status_bar_message(&screen.status_bar, msg);
239 }
241 void
242 screen_status_printf(const char *format, ...)
243 {
244 char *msg;
245 va_list ap;
247 va_start(ap,format);
248 msg = g_strdup_vprintf(format,ap);
249 va_end(ap);
250 screen_status_message(msg);
251 g_free(msg);
252 }
254 void
255 screen_init(struct mpdclient *c)
256 {
257 if (COLS < SCREEN_MIN_COLS || LINES < SCREEN_MIN_ROWS) {
258 fprintf(stderr, "%s\n", _("Error: Screen too small"));
259 exit(EXIT_FAILURE);
260 }
262 screen.cols = COLS;
263 screen.rows = LINES;
265 screen.buf = g_malloc(screen.cols);
266 screen.buf_size = screen.cols;
267 screen.findbuf = NULL;
268 screen.start_timestamp = time(NULL);
270 /* create top window */
271 title_bar_init(&screen.title_bar, screen.cols, 0, 0);
273 /* create main window */
274 window_init(&screen.main_window, screen.rows - 4, screen.cols, 2, 0);
276 // leaveok(screen.main_window.w, TRUE); temporary disabled
277 keypad(screen.main_window.w, TRUE);
279 /* create progress window */
280 progress_bar_init(&screen.progress_bar, screen.cols,
281 screen.rows - 2, 0);
282 progress_bar_paint(&screen.progress_bar);
284 /* create status window */
285 status_bar_init(&screen.status_bar, screen.cols, screen.rows - 1, 0);
286 status_bar_paint(&screen.status_bar, c->status, c->song);
288 #ifdef ENABLE_COLORS
289 if (options.enable_colors) {
290 /* set background attributes */
291 wbkgd(stdscr, COLOR_PAIR(COLOR_LIST));
292 wbkgd(screen.main_window.w, COLOR_PAIR(COLOR_LIST));
293 wbkgd(screen.title_bar.window.w, COLOR_PAIR(COLOR_TITLE));
294 wbkgd(screen.progress_bar.window.w,
295 COLOR_PAIR(COLOR_PROGRESSBAR));
296 wbkgd(screen.status_bar.window.w, COLOR_PAIR(COLOR_STATUS));
297 colors_use(screen.progress_bar.window.w, COLOR_PROGRESSBAR);
298 }
299 #endif
301 doupdate();
303 /* initialize screens */
304 screen_list_init(screen.main_window.w,
305 screen.main_window.cols, screen.main_window.rows);
307 if (mode_fn->open != NULL)
308 mode_fn->open(c);
309 }
311 void
312 screen_paint(struct mpdclient *c)
313 {
314 const char *title = NULL;
316 if (mode_fn->get_title != NULL)
317 title = mode_fn->get_title(screen.buf, screen.buf_size);
319 /* paint the title/header window */
320 if( title )
321 paint_top_window(title, c);
322 else
323 paint_top_window("", c);
325 /* paint the bottom window */
327 paint_progress_window(c);
328 status_bar_paint(&screen.status_bar, c->status, c->song);
330 /* paint the main window */
332 wclear(screen.main_window.w);
333 if (mode_fn->paint != NULL)
334 mode_fn->paint();
336 /* move the cursor to the origin */
338 if (!options.hardware_cursor)
339 wmove(screen.main_window.w, 0, 0);
341 wnoutrefresh(screen.main_window.w);
343 /* tell curses to update */
344 doupdate();
345 }
347 void
348 screen_update(struct mpdclient *c)
349 {
350 #ifndef NCMPC_MINI
351 static bool initialized = false;
352 static bool repeat;
353 static bool random_enabled;
354 static bool single;
355 static bool consume;
356 static unsigned crossfade;
358 /* print a message if mpd status has changed */
359 if ((c->events & MPD_IDLE_OPTIONS) && c->status != NULL) {
360 if (!initialized) {
361 repeat = mpd_status_get_repeat(c->status);
362 random_enabled = mpd_status_get_random(c->status);
363 single = mpd_status_get_single(c->status);
364 consume = mpd_status_get_consume(c->status);
365 crossfade = mpd_status_get_crossfade(c->status);
366 initialized = true;
367 }
369 if (repeat != mpd_status_get_repeat(c->status))
370 screen_status_printf(mpd_status_get_repeat(c->status) ?
371 _("Repeat mode is on") :
372 _("Repeat mode is off"));
374 if (random_enabled != mpd_status_get_random(c->status))
375 screen_status_printf(mpd_status_get_random(c->status) ?
376 _("Random mode is on") :
377 _("Random mode is off"));
379 if (single != mpd_status_get_single(c->status))
380 screen_status_printf(mpd_status_get_single(c->status) ?
381 /* "single" mode means
382 that MPD will
383 automatically stop
384 after playing one
385 single song */
386 _("Single mode is on") :
387 _("Single mode is off"));
389 if (consume != mpd_status_get_consume(c->status))
390 screen_status_printf(mpd_status_get_consume(c->status) ?
391 /* "consume" mode means
392 that MPD removes each
393 song which has
394 finished playing */
395 _("Consume mode is on") :
396 _("Consume mode is off"));
398 if (crossfade != mpd_status_get_crossfade(c->status))
399 screen_status_printf(_("Crossfade %d seconds"),
400 mpd_status_get_crossfade(c->status));
402 repeat = mpd_status_get_repeat(c->status);
403 random_enabled = mpd_status_get_random(c->status);
404 single = mpd_status_get_single(c->status);
405 consume = mpd_status_get_consume(c->status);
406 crossfade = mpd_status_get_crossfade(c->status);
407 }
409 if (c->events & MPD_IDLE_DATABASE)
410 screen_status_printf(_("Database updated"));
412 /* update title/header window */
413 if (welcome && options.welcome_screen_list &&
414 time(NULL)-screen.start_timestamp <= SCREEN_WELCOME_TIME)
415 paint_top_window("", c);
416 else
417 #endif
418 if (mode_fn->get_title != NULL) {
419 paint_top_window(mode_fn->get_title(screen.buf,screen.buf_size), c);
420 #ifndef NCMPC_MINI
421 welcome = FALSE;
422 #endif
423 } else
424 paint_top_window("", c);
426 /* update progress window */
427 paint_progress_window(c);
429 /* update status window */
430 status_bar_paint(&screen.status_bar, c->status, c->song);
432 /* update the main window */
433 if (mode_fn->update != NULL)
434 mode_fn->update(c);
436 /* move the cursor to the origin */
438 if (!options.hardware_cursor)
439 wmove(screen.main_window.w, 0, 0);
441 wnoutrefresh(screen.main_window.w);
443 /* tell curses to update */
444 doupdate();
445 }
447 #ifdef HAVE_GETMOUSE
448 int
449 screen_get_mouse_event(struct mpdclient *c, unsigned long *bstate, int *row)
450 {
451 MEVENT event;
453 /* retrieve the mouse event from ncurses */
454 getmouse(&event);
455 /* calculate the selected row in the list window */
456 *row = event.y - screen.title_bar.window.rows;
457 /* copy button state bits */
458 *bstate = event.bstate;
459 /* if button 2 was pressed switch screen */
460 if (event.bstate & BUTTON2_CLICKED) {
461 screen_cmd(c, CMD_SCREEN_NEXT);
462 return 1;
463 }
465 return 0;
466 }
467 #endif
469 void
470 screen_cmd(struct mpdclient *c, command_t cmd)
471 {
472 #ifndef NCMPC_MINI
473 welcome = FALSE;
474 #endif
476 if (mode_fn->cmd != NULL && mode_fn->cmd(c, cmd))
477 return;
479 if (handle_player_command(c, cmd))
480 return;
482 switch(cmd) {
483 case CMD_TOGGLE_FIND_WRAP:
484 options.find_wrap = !options.find_wrap;
485 screen_status_printf(options.find_wrap ?
486 _("Find mode: Wrapped") :
487 _("Find mode: Normal"));
488 break;
489 case CMD_TOGGLE_AUTOCENTER:
490 options.auto_center = !options.auto_center;
491 screen_status_printf(options.auto_center ?
492 _("Auto center mode: On") :
493 _("Auto center mode: Off"));
494 break;
495 case CMD_SCREEN_UPDATE:
496 screen_paint(c);
497 break;
498 case CMD_SCREEN_PREVIOUS:
499 screen_next_mode(c, -1);
500 break;
501 case CMD_SCREEN_NEXT:
502 screen_next_mode(c, 1);
503 break;
504 case CMD_SCREEN_PLAY:
505 screen_switch(&screen_playlist, c);
506 break;
507 case CMD_SCREEN_FILE:
508 screen_switch(&screen_browse, c);
509 break;
510 #ifdef ENABLE_HELP_SCREEN
511 case CMD_SCREEN_HELP:
512 screen_switch(&screen_help, c);
513 break;
514 #endif
515 #ifdef ENABLE_SEARCH_SCREEN
516 case CMD_SCREEN_SEARCH:
517 screen_switch(&screen_search, c);
518 break;
519 #endif
520 #ifdef ENABLE_ARTIST_SCREEN
521 case CMD_SCREEN_ARTIST:
522 screen_switch(&screen_artist, c);
523 break;
524 #endif
525 #ifdef ENABLE_SONG_SCREEN
526 case CMD_SCREEN_SONG:
527 screen_switch(&screen_song, c);
528 break;
529 #endif
530 #ifdef ENABLE_KEYDEF_SCREEN
531 case CMD_SCREEN_KEYDEF:
532 screen_switch(&screen_keydef, c);
533 break;
534 #endif
535 #ifdef ENABLE_LYRICS_SCREEN
536 case CMD_SCREEN_LYRICS:
537 screen_switch(&screen_lyrics, c);
538 break;
539 #endif
540 #ifdef ENABLE_OUTPUTS_SCREEN
541 case CMD_SCREEN_OUTPUTS:
542 screen_switch(&screen_outputs, c);
543 break;
544 case CMD_SCREEN_SWAP:
545 screen_swap(c, NULL);
546 break;
547 #endif
549 default:
550 break;
551 }
552 }