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 "screen_message.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_play.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"
43 #include <mpd/client.h>
45 #include <stdlib.h>
46 #include <unistd.h>
47 #include <string.h>
48 #include <time.h>
49 #include <locale.h>
51 #ifndef NCMPC_MINI
52 /** welcome message time [s] */
53 static const GTime SCREEN_WELCOME_TIME = 10;
54 #endif
56 /* minimum window size */
57 static const int SCREEN_MIN_COLS = 14;
58 static const int SCREEN_MIN_ROWS = 5;
60 /* screens */
62 #ifndef NCMPC_MINI
63 static gboolean welcome = TRUE;
64 #endif
66 struct screen screen;
67 static const struct screen_functions *mode_fn = &screen_playlist;
68 static const struct screen_functions *mode_fn_prev = &screen_playlist;
70 gboolean
71 screen_is_visible(const struct screen_functions *sf)
72 {
73 return sf == mode_fn;
74 }
76 void
77 screen_switch(const struct screen_functions *sf, struct mpdclient *c)
78 {
79 assert(sf != NULL);
81 if (sf == mode_fn)
82 return;
84 mode_fn_prev = mode_fn;
86 /* close the old mode */
87 if (mode_fn->close != NULL)
88 mode_fn->close();
90 /* get functions for the new mode */
91 mode_fn = sf;
93 /* open the new mode */
94 if (mode_fn->open != NULL)
95 mode_fn->open(c);
97 screen_paint(c);
98 }
100 void
101 screen_swap(struct mpdclient *c, const struct mpd_song *song)
102 {
103 if (song != NULL)
104 {
105 if (false)
106 { /* just a hack to make the ifdefs less ugly */ }
107 #ifdef ENABLE_SONG_SCREEN
108 if (mode_fn_prev == &screen_song)
109 screen_song_switch(c, song);
110 #endif
111 #ifdef ENABLE_LYRICS_SCREEN
112 else if (mode_fn_prev == &screen_lyrics)
113 screen_lyrics_switch(c, song, true);
114 #endif
115 else
116 screen_switch(mode_fn_prev, c);
117 }
118 else
119 screen_switch(mode_fn_prev, c);
120 }
122 static int
123 find_configured_screen(const char *name)
124 {
125 unsigned i;
127 for (i = 0; options.screen_list[i] != NULL; ++i)
128 if (strcmp(options.screen_list[i], name) == 0)
129 return i;
131 return -1;
132 }
134 static void
135 screen_next_mode(struct mpdclient *c, int offset)
136 {
137 int max = g_strv_length(options.screen_list);
138 int current, next;
139 const struct screen_functions *sf;
141 /* find current screen */
142 current = find_configured_screen(screen_get_name(mode_fn));
143 next = current + offset;
144 if (next<0)
145 next = max-1;
146 else if (next>=max)
147 next = 0;
149 sf = screen_lookup_name(options.screen_list[next]);
150 if (sf != NULL)
151 screen_switch(sf, c);
152 }
154 static void
155 paint_top_window(const char *header, const struct mpdclient *c)
156 {
157 title_bar_paint(&screen.title_bar, header, c->status);
158 }
160 static void
161 paint_progress_window(struct mpdclient *c)
162 {
163 unsigned elapsed, duration;
165 if (c->song != NULL && seek_id == (int)mpd_song_get_id(c->song))
166 elapsed = seek_target_time;
167 else if (c->status != NULL)
168 elapsed = mpd_status_get_elapsed_time(c->status);
169 else
170 elapsed = 0;
172 duration = c->status != NULL &&
173 (mpd_status_get_state(c->status) == MPD_STATE_PLAY ||
174 mpd_status_get_state(c->status) == MPD_STATE_PAUSE)
175 ? mpd_status_get_total_time(c->status)
176 : 0;
178 if (progress_bar_set(&screen.progress_bar, elapsed, duration))
179 progress_bar_paint(&screen.progress_bar);
180 }
182 void
183 screen_exit(void)
184 {
185 if (mode_fn->close != NULL)
186 mode_fn->close();
188 screen_list_exit();
190 string_list_free(screen.find_history);
191 g_free(screen.buf);
192 g_free(screen.findbuf);
194 title_bar_deinit(&screen.title_bar);
195 delwin(screen.main_window.w);
196 progress_bar_deinit(&screen.progress_bar);
197 status_bar_deinit(&screen.status_bar);
198 }
200 void
201 screen_resize(struct mpdclient *c)
202 {
203 if (COLS<SCREEN_MIN_COLS || LINES<SCREEN_MIN_ROWS) {
204 screen_exit();
205 fprintf(stderr, "%s", _("Error: Screen too small"));
206 exit(EXIT_FAILURE);
207 }
209 resizeterm(LINES, COLS);
211 screen.cols = COLS;
212 screen.rows = LINES;
214 title_bar_resize(&screen.title_bar, screen.cols);
216 /* main window */
217 screen.main_window.cols = screen.cols;
218 screen.main_window.rows = screen.rows-4;
219 wresize(screen.main_window.w, screen.main_window.rows, screen.cols);
220 wclear(screen.main_window.w);
222 /* progress window */
223 progress_bar_resize(&screen.progress_bar, screen.cols,
224 screen.rows - 2, 0);
225 progress_bar_paint(&screen.progress_bar);
227 /* status window */
228 status_bar_resize(&screen.status_bar, screen.cols, screen.rows - 1, 0);
229 status_bar_paint(&screen.status_bar, c->status, c->song);
231 screen.buf_size = screen.cols;
232 g_free(screen.buf);
233 screen.buf = g_malloc(screen.cols);
235 /* resize all screens */
236 screen_list_resize(screen.main_window.cols, screen.main_window.rows);
238 /* ? - without this the cursor becomes visible with aterm & Eterm */
239 curs_set(1);
240 curs_set(0);
242 screen_paint(c);
243 }
245 void
246 screen_init(struct mpdclient *c)
247 {
248 if (COLS < SCREEN_MIN_COLS || LINES < SCREEN_MIN_ROWS) {
249 fprintf(stderr, "%s\n", _("Error: Screen too small"));
250 exit(EXIT_FAILURE);
251 }
253 screen.cols = COLS;
254 screen.rows = LINES;
256 screen.buf = g_malloc(screen.cols);
257 screen.buf_size = screen.cols;
258 screen.findbuf = NULL;
259 screen.start_timestamp = time(NULL);
261 /* create top window */
262 title_bar_init(&screen.title_bar, screen.cols, 0, 0);
264 /* create main window */
265 window_init(&screen.main_window, screen.rows - 4, screen.cols, 2, 0);
267 // leaveok(screen.main_window.w, TRUE); temporary disabled
268 keypad(screen.main_window.w, TRUE);
270 /* create progress window */
271 progress_bar_init(&screen.progress_bar, screen.cols,
272 screen.rows - 2, 0);
273 progress_bar_paint(&screen.progress_bar);
275 /* create status window */
276 status_bar_init(&screen.status_bar, screen.cols, screen.rows - 1, 0);
277 status_bar_paint(&screen.status_bar, c->status, c->song);
279 #ifdef ENABLE_COLORS
280 if (options.enable_colors) {
281 /* set background attributes */
282 wbkgd(stdscr, COLOR_PAIR(COLOR_LIST));
283 wbkgd(screen.main_window.w, COLOR_PAIR(COLOR_LIST));
284 wbkgd(screen.title_bar.window.w, COLOR_PAIR(COLOR_TITLE));
285 wbkgd(screen.progress_bar.window.w,
286 COLOR_PAIR(COLOR_PROGRESSBAR));
287 wbkgd(screen.status_bar.window.w, COLOR_PAIR(COLOR_STATUS));
288 colors_use(screen.progress_bar.window.w, COLOR_PROGRESSBAR);
289 }
290 #endif
292 doupdate();
294 /* initialize screens */
295 screen_list_init(screen.main_window.w,
296 screen.main_window.cols, screen.main_window.rows);
298 if (mode_fn->open != NULL)
299 mode_fn->open(c);
300 }
302 void
303 screen_paint(struct mpdclient *c)
304 {
305 const char *title = NULL;
307 if (mode_fn->get_title != NULL)
308 title = mode_fn->get_title(screen.buf, screen.buf_size);
310 /* paint the title/header window */
311 if( title )
312 paint_top_window(title, c);
313 else
314 paint_top_window("", c);
316 /* paint the bottom window */
318 paint_progress_window(c);
319 status_bar_paint(&screen.status_bar, c->status, c->song);
321 /* paint the main window */
323 wclear(screen.main_window.w);
324 if (mode_fn->paint != NULL)
325 mode_fn->paint();
327 /* move the cursor to the origin */
329 if (!options.hardware_cursor)
330 wmove(screen.main_window.w, 0, 0);
332 wnoutrefresh(screen.main_window.w);
334 /* tell curses to update */
335 doupdate();
336 }
338 void
339 screen_update(struct mpdclient *c)
340 {
341 #ifndef NCMPC_MINI
342 static bool initialized = false;
343 static bool repeat;
344 static bool random_enabled;
345 static bool single;
346 static bool consume;
347 static unsigned crossfade;
349 /* print a message if mpd status has changed */
350 if ((c->events & MPD_IDLE_OPTIONS) && c->status != NULL) {
351 if (!initialized) {
352 repeat = mpd_status_get_repeat(c->status);
353 random_enabled = mpd_status_get_random(c->status);
354 single = mpd_status_get_single(c->status);
355 consume = mpd_status_get_consume(c->status);
356 crossfade = mpd_status_get_crossfade(c->status);
357 initialized = true;
358 }
360 if (repeat != mpd_status_get_repeat(c->status))
361 screen_status_printf(mpd_status_get_repeat(c->status) ?
362 _("Repeat mode is on") :
363 _("Repeat mode is off"));
365 if (random_enabled != mpd_status_get_random(c->status))
366 screen_status_printf(mpd_status_get_random(c->status) ?
367 _("Random mode is on") :
368 _("Random mode is off"));
370 if (single != mpd_status_get_single(c->status))
371 screen_status_printf(mpd_status_get_single(c->status) ?
372 /* "single" mode means
373 that MPD will
374 automatically stop
375 after playing one
376 single song */
377 _("Single mode is on") :
378 _("Single mode is off"));
380 if (consume != mpd_status_get_consume(c->status))
381 screen_status_printf(mpd_status_get_consume(c->status) ?
382 /* "consume" mode means
383 that MPD removes each
384 song which has
385 finished playing */
386 _("Consume mode is on") :
387 _("Consume mode is off"));
389 if (crossfade != mpd_status_get_crossfade(c->status))
390 screen_status_printf(_("Crossfade %d seconds"),
391 mpd_status_get_crossfade(c->status));
393 repeat = mpd_status_get_repeat(c->status);
394 random_enabled = mpd_status_get_random(c->status);
395 single = mpd_status_get_single(c->status);
396 consume = mpd_status_get_consume(c->status);
397 crossfade = mpd_status_get_crossfade(c->status);
398 }
400 if (c->events & MPD_IDLE_DATABASE)
401 screen_status_printf(_("Database updated"));
403 /* update title/header window */
404 if (welcome && options.welcome_screen_list &&
405 time(NULL)-screen.start_timestamp <= SCREEN_WELCOME_TIME)
406 paint_top_window("", c);
407 else
408 #endif
409 if (mode_fn->get_title != NULL) {
410 paint_top_window(mode_fn->get_title(screen.buf,screen.buf_size), c);
411 #ifndef NCMPC_MINI
412 welcome = FALSE;
413 #endif
414 } else
415 paint_top_window("", c);
417 /* update progress window */
418 paint_progress_window(c);
420 /* update status window */
421 status_bar_paint(&screen.status_bar, c->status, c->song);
423 /* update the main window */
424 if (mode_fn->update != NULL)
425 mode_fn->update(c);
427 /* move the cursor to the origin */
429 if (!options.hardware_cursor)
430 wmove(screen.main_window.w, 0, 0);
432 wnoutrefresh(screen.main_window.w);
434 /* tell curses to update */
435 doupdate();
436 }
438 #ifdef HAVE_GETMOUSE
439 int
440 screen_get_mouse_event(struct mpdclient *c, unsigned long *bstate, int *row)
441 {
442 MEVENT event;
444 /* retrieve the mouse event from ncurses */
445 getmouse(&event);
446 /* calculate the selected row in the list window */
447 *row = event.y - screen.title_bar.window.rows;
448 /* copy button state bits */
449 *bstate = event.bstate;
450 /* if button 2 was pressed switch screen */
451 if (event.bstate & BUTTON2_CLICKED) {
452 screen_cmd(c, CMD_SCREEN_NEXT);
453 return 1;
454 }
456 return 0;
457 }
458 #endif
460 void
461 screen_cmd(struct mpdclient *c, command_t cmd)
462 {
463 #ifndef NCMPC_MINI
464 welcome = FALSE;
465 #endif
467 if (mode_fn->cmd != NULL && mode_fn->cmd(c, cmd))
468 return;
470 if (handle_player_command(c, cmd))
471 return;
473 switch(cmd) {
474 case CMD_TOGGLE_FIND_WRAP:
475 options.find_wrap = !options.find_wrap;
476 screen_status_printf(options.find_wrap ?
477 _("Find mode: Wrapped") :
478 _("Find mode: Normal"));
479 break;
480 case CMD_TOGGLE_AUTOCENTER:
481 options.auto_center = !options.auto_center;
482 screen_status_printf(options.auto_center ?
483 _("Auto center mode: On") :
484 _("Auto center mode: Off"));
485 break;
486 case CMD_SCREEN_UPDATE:
487 screen_paint(c);
488 break;
489 case CMD_SCREEN_PREVIOUS:
490 screen_next_mode(c, -1);
491 break;
492 case CMD_SCREEN_NEXT:
493 screen_next_mode(c, 1);
494 break;
495 case CMD_SCREEN_PLAY:
496 screen_switch(&screen_playlist, c);
497 break;
498 case CMD_SCREEN_FILE:
499 screen_switch(&screen_browse, c);
500 break;
501 #ifdef ENABLE_HELP_SCREEN
502 case CMD_SCREEN_HELP:
503 screen_switch(&screen_help, c);
504 break;
505 #endif
506 #ifdef ENABLE_SEARCH_SCREEN
507 case CMD_SCREEN_SEARCH:
508 screen_switch(&screen_search, c);
509 break;
510 #endif
511 #ifdef ENABLE_ARTIST_SCREEN
512 case CMD_SCREEN_ARTIST:
513 screen_switch(&screen_artist, c);
514 break;
515 #endif
516 #ifdef ENABLE_SONG_SCREEN
517 case CMD_SCREEN_SONG:
518 screen_switch(&screen_song, c);
519 break;
520 #endif
521 #ifdef ENABLE_KEYDEF_SCREEN
522 case CMD_SCREEN_KEYDEF:
523 screen_switch(&screen_keydef, c);
524 break;
525 #endif
526 #ifdef ENABLE_LYRICS_SCREEN
527 case CMD_SCREEN_LYRICS:
528 screen_switch(&screen_lyrics, c);
529 break;
530 #endif
531 #ifdef ENABLE_OUTPUTS_SCREEN
532 case CMD_SCREEN_OUTPUTS:
533 screen_switch(&screen_outputs, c);
534 break;
535 case CMD_SCREEN_SWAP:
536 screen_swap(c, NULL);
537 break;
538 #endif
540 default:
541 break;
542 }
543 }