Code

81b736468757535f225895c51eb2475d1c865b9c
[ncmpc.git] / src / screen.c
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 struct screen screen;
63 static const struct screen_functions *mode_fn = &screen_playlist;
64 static const struct screen_functions *mode_fn_prev = &screen_playlist;
66 gboolean
67 screen_is_visible(const struct screen_functions *sf)
68 {
69         return sf == mode_fn;
70 }
72 void
73 screen_switch(const struct screen_functions *sf, struct mpdclient *c)
74 {
75         assert(sf != NULL);
77         if (sf == mode_fn)
78                 return;
80         mode_fn_prev = mode_fn;
82         /* close the old mode */
83         if (mode_fn->close != NULL)
84                 mode_fn->close();
86         /* get functions for the new mode */
87         mode_fn = sf;
89         /* open the new mode */
90         if (mode_fn->open != NULL)
91                 mode_fn->open(c);
93         screen_paint(c);
94 }
96 void
97 screen_swap(struct mpdclient *c, const struct mpd_song *song)
98 {
99         if (song != NULL)
100         {
101                 if (false)
102                         { /* just a hack to make the ifdefs less ugly */ }
103 #ifdef ENABLE_SONG_SCREEN
104                 if (mode_fn_prev == &screen_song)
105                         screen_song_switch(c, song);
106 #endif
107 #ifdef ENABLE_LYRICS_SCREEN
108                 else if (mode_fn_prev == &screen_lyrics)
109                         screen_lyrics_switch(c, song, true);
110 #endif
111                 else
112                         screen_switch(mode_fn_prev, c);
113         }
114         else
115                 screen_switch(mode_fn_prev, c);
118 static int
119 find_configured_screen(const char *name)
121         unsigned i;
123         for (i = 0; options.screen_list[i] != NULL; ++i)
124                 if (strcmp(options.screen_list[i], name) == 0)
125                         return i;
127         return -1;
130 static void
131 screen_next_mode(struct mpdclient *c, int offset)
133         int max = g_strv_length(options.screen_list);
134         int current, next;
135         const struct screen_functions *sf;
137         /* find current screen */
138         current = find_configured_screen(screen_get_name(mode_fn));
139         next = current + offset;
140         if (next<0)
141                 next = max-1;
142         else if (next>=max)
143                 next = 0;
145         sf = screen_lookup_name(options.screen_list[next]);
146         if (sf != NULL)
147                 screen_switch(sf, c);
150 static void
151 paint_top_window(const char *header, const struct mpdclient *c)
153         title_bar_paint(&screen.title_bar, header, c->status);
156 static void
157 paint_progress_window(struct mpdclient *c)
159         unsigned elapsed, duration;
161         if (c->song != NULL && seek_id == (int)mpd_song_get_id(c->song))
162                 elapsed = seek_target_time;
163         else if (c->status != NULL)
164                 elapsed = mpd_status_get_elapsed_time(c->status);
165         else
166                 elapsed = 0;
168         duration = c->status != NULL &&
169                 (mpd_status_get_state(c->status) == MPD_STATE_PLAY ||
170                  mpd_status_get_state(c->status) == MPD_STATE_PAUSE)
171                 ? mpd_status_get_total_time(c->status)
172                 : 0;
174         if (progress_bar_set(&screen.progress_bar, elapsed, duration))
175                 progress_bar_paint(&screen.progress_bar);
178 void
179 screen_exit(void)
181         if (mode_fn->close != NULL)
182                 mode_fn->close();
184         screen_list_exit();
186         string_list_free(screen.find_history);
187         g_free(screen.buf);
188         g_free(screen.findbuf);
190         title_bar_deinit(&screen.title_bar);
191         delwin(screen.main_window.w);
192         progress_bar_deinit(&screen.progress_bar);
193         status_bar_deinit(&screen.status_bar);
195 #ifndef NCMPC_MINI
196         if (screen.welcome_source_id != 0)
197                 g_source_remove(screen.welcome_source_id);
198 #endif
201 void
202 screen_resize(struct mpdclient *c)
204         if (COLS<SCREEN_MIN_COLS || LINES<SCREEN_MIN_ROWS) {
205                 screen_exit();
206                 fprintf(stderr, "%s", _("Error: Screen too small"));
207                 exit(EXIT_FAILURE);
208         }
210         resizeterm(LINES, COLS);
212         screen.cols = COLS;
213         screen.rows = LINES;
215         title_bar_resize(&screen.title_bar, screen.cols);
217         /* main window */
218         screen.main_window.cols = screen.cols;
219         screen.main_window.rows = screen.rows-4;
220         wresize(screen.main_window.w, screen.main_window.rows, screen.cols);
221         wclear(screen.main_window.w);
223         /* progress window */
224         progress_bar_resize(&screen.progress_bar, screen.cols,
225                             screen.rows - 2, 0);
226         progress_bar_paint(&screen.progress_bar);
228         /* status window */
229         status_bar_resize(&screen.status_bar, screen.cols, screen.rows - 1, 0);
230         status_bar_paint(&screen.status_bar, c->status, c->song);
232         screen.buf_size = screen.cols;
233         g_free(screen.buf);
234         screen.buf = g_malloc(screen.cols);
236         /* resize all screens */
237         screen_list_resize(screen.main_window.cols, screen.main_window.rows);
239         /* ? - without this the cursor becomes visible with aterm & Eterm */
240         curs_set(1);
241         curs_set(0);
243         screen_paint(c);
246 static gboolean
247 welcome_timer_callback(gpointer data)
249         struct mpdclient *c = data;
251         screen.welcome_source_id = 0;
253         paint_top_window(mode_fn->get_title != NULL
254                          ? mode_fn->get_title(screen.buf, screen.buf_size)
255                          : "",
256                          c);
257         doupdate();
259         return false;
262 void
263 screen_init(struct mpdclient *c)
265         if (COLS < SCREEN_MIN_COLS || LINES < SCREEN_MIN_ROWS) {
266                 fprintf(stderr, "%s\n", _("Error: Screen too small"));
267                 exit(EXIT_FAILURE);
268         }
270         screen.cols = COLS;
271         screen.rows = LINES;
273         screen.buf  = g_malloc(screen.cols);
274         screen.buf_size = screen.cols;
275         screen.findbuf = NULL;
277 #ifndef NCMPC_MINI
278         if (options.welcome_screen_list)
279                 screen.welcome_source_id =
280                         g_timeout_add(SCREEN_WELCOME_TIME * 1000,
281                                       welcome_timer_callback, c);
282 #endif
284         /* create top window */
285         title_bar_init(&screen.title_bar, screen.cols, 0, 0);
287         /* create main window */
288         window_init(&screen.main_window, screen.rows - 4, screen.cols, 2, 0);
290         //  leaveok(screen.main_window.w, TRUE); temporary disabled
291         keypad(screen.main_window.w, TRUE);
293         /* create progress window */
294         progress_bar_init(&screen.progress_bar, screen.cols,
295                           screen.rows - 2, 0);
296         progress_bar_paint(&screen.progress_bar);
298         /* create status window */
299         status_bar_init(&screen.status_bar, screen.cols, screen.rows - 1, 0);
300         status_bar_paint(&screen.status_bar, c->status, c->song);
302 #ifdef ENABLE_COLORS
303         if (options.enable_colors) {
304                 /* set background attributes */
305                 wbkgd(stdscr, COLOR_PAIR(COLOR_LIST));
306                 wbkgd(screen.main_window.w,     COLOR_PAIR(COLOR_LIST));
307                 wbkgd(screen.title_bar.window.w, COLOR_PAIR(COLOR_TITLE));
308                 wbkgd(screen.progress_bar.window.w,
309                       COLOR_PAIR(COLOR_PROGRESSBAR));
310                 wbkgd(screen.status_bar.window.w, COLOR_PAIR(COLOR_STATUS));
311                 colors_use(screen.progress_bar.window.w, COLOR_PROGRESSBAR);
312         }
313 #endif
315         doupdate();
317         /* initialize screens */
318         screen_list_init(screen.main_window.w,
319                          screen.main_window.cols, screen.main_window.rows);
321         if (mode_fn->open != NULL)
322                 mode_fn->open(c);
325 void
326 screen_paint(struct mpdclient *c)
328         const char *title = NULL;
330         if (mode_fn->get_title != NULL)
331                 title = mode_fn->get_title(screen.buf, screen.buf_size);
333         /* paint the title/header window */
334         if( title )
335                 paint_top_window(title, c);
336         else
337                 paint_top_window("", c);
339         /* paint the bottom window */
341         paint_progress_window(c);
342         status_bar_paint(&screen.status_bar, c->status, c->song);
344         /* paint the main window */
346         wclear(screen.main_window.w);
347         if (mode_fn->paint != NULL)
348                 mode_fn->paint();
350         /* move the cursor to the origin */
352         if (!options.hardware_cursor)
353                 wmove(screen.main_window.w, 0, 0);
355         wnoutrefresh(screen.main_window.w);
357         /* tell curses to update */
358         doupdate();
361 void
362 screen_update(struct mpdclient *c)
364 #ifndef NCMPC_MINI
365         static bool initialized = false;
366         static bool repeat;
367         static bool random_enabled;
368         static bool single;
369         static bool consume;
370         static unsigned crossfade;
372         /* print a message if mpd status has changed */
373         if ((c->events & MPD_IDLE_OPTIONS) && c->status != NULL) {
374                 if (!initialized) {
375                         repeat = mpd_status_get_repeat(c->status);
376                         random_enabled = mpd_status_get_random(c->status);
377                         single = mpd_status_get_single(c->status);
378                         consume = mpd_status_get_consume(c->status);
379                         crossfade = mpd_status_get_crossfade(c->status);
380                         initialized = true;
381                 }
383                 if (repeat != mpd_status_get_repeat(c->status))
384                         screen_status_printf(mpd_status_get_repeat(c->status) ?
385                                              _("Repeat mode is on") :
386                                              _("Repeat mode is off"));
388                 if (random_enabled != mpd_status_get_random(c->status))
389                         screen_status_printf(mpd_status_get_random(c->status) ?
390                                              _("Random mode is on") :
391                                              _("Random mode is off"));
393                 if (single != mpd_status_get_single(c->status))
394                         screen_status_printf(mpd_status_get_single(c->status) ?
395                                              /* "single" mode means
396                                                 that MPD will
397                                                 automatically stop
398                                                 after playing one
399                                                 single song */
400                                              _("Single mode is on") :
401                                              _("Single mode is off"));
403                 if (consume != mpd_status_get_consume(c->status))
404                         screen_status_printf(mpd_status_get_consume(c->status) ?
405                                              /* "consume" mode means
406                                                 that MPD removes each
407                                                 song which has
408                                                 finished playing */
409                                              _("Consume mode is on") :
410                                              _("Consume mode is off"));
412                 if (crossfade != mpd_status_get_crossfade(c->status))
413                         screen_status_printf(_("Crossfade %d seconds"),
414                                              mpd_status_get_crossfade(c->status));
416                 repeat = mpd_status_get_repeat(c->status);
417                 random_enabled = mpd_status_get_random(c->status);
418                 single = mpd_status_get_single(c->status);
419                 consume = mpd_status_get_consume(c->status);
420                 crossfade = mpd_status_get_crossfade(c->status);
421         }
423         if (c->events & MPD_IDLE_DATABASE)
424                 screen_status_printf(_("Database updated"));
426         /* update title/header window */
427         if (screen.welcome_source_id != 0)
428                 paint_top_window("", c);
429         else
430 #endif
431         if (mode_fn->get_title != NULL) {
432                 paint_top_window(mode_fn->get_title(screen.buf,screen.buf_size), c);
433         } else
434                 paint_top_window("", c);
436         /* update progress window */
437         paint_progress_window(c);
439         /* update status window */
440         status_bar_paint(&screen.status_bar, c->status, c->song);
442         /* update the main window */
443         if (mode_fn->update != NULL)
444                 mode_fn->update(c);
446         /* move the cursor to the origin */
448         if (!options.hardware_cursor)
449                 wmove(screen.main_window.w, 0, 0);
451         wnoutrefresh(screen.main_window.w);
453         /* tell curses to update */
454         doupdate();
457 #ifdef HAVE_GETMOUSE
458 int
459 screen_get_mouse_event(struct mpdclient *c, unsigned long *bstate, int *row)
461         MEVENT event;
463         /* retrieve the mouse event from ncurses */
464         getmouse(&event);
465         /* calculate the selected row in the list window */
466         *row = event.y - screen.title_bar.window.rows;
467         /* copy button state bits */
468         *bstate = event.bstate;
469         /* if button 2 was pressed switch screen */
470         if (event.bstate & BUTTON2_CLICKED) {
471                 screen_cmd(c, CMD_SCREEN_NEXT);
472                 return 1;
473         }
475         return 0;
477 #endif
479 void
480 screen_cmd(struct mpdclient *c, command_t cmd)
482 #ifndef NCMPC_MINI
483         if (screen.welcome_source_id != 0) {
484                 g_source_remove(screen.welcome_source_id);
485                 screen.welcome_source_id = 0;
486         }
487 #endif
489         if (mode_fn->cmd != NULL && mode_fn->cmd(c, cmd))
490                 return;
492         if (handle_player_command(c, cmd))
493                 return;
495         switch(cmd) {
496         case CMD_TOGGLE_FIND_WRAP:
497                 options.find_wrap = !options.find_wrap;
498                 screen_status_printf(options.find_wrap ?
499                                      _("Find mode: Wrapped") :
500                                      _("Find mode: Normal"));
501                 break;
502         case CMD_TOGGLE_AUTOCENTER:
503                 options.auto_center = !options.auto_center;
504                 screen_status_printf(options.auto_center ?
505                                      _("Auto center mode: On") :
506                                      _("Auto center mode: Off"));
507                 break;
508         case CMD_SCREEN_UPDATE:
509                 screen_paint(c);
510                 break;
511         case CMD_SCREEN_PREVIOUS:
512                 screen_next_mode(c, -1);
513                 break;
514         case CMD_SCREEN_NEXT:
515                 screen_next_mode(c, 1);
516                 break;
517         case CMD_SCREEN_PLAY:
518                 screen_switch(&screen_playlist, c);
519                 break;
520         case CMD_SCREEN_FILE:
521                 screen_switch(&screen_browse, c);
522                 break;
523 #ifdef ENABLE_HELP_SCREEN
524         case CMD_SCREEN_HELP:
525                 screen_switch(&screen_help, c);
526                 break;
527 #endif
528 #ifdef ENABLE_SEARCH_SCREEN
529         case CMD_SCREEN_SEARCH:
530                 screen_switch(&screen_search, c);
531                 break;
532 #endif
533 #ifdef ENABLE_ARTIST_SCREEN
534         case CMD_SCREEN_ARTIST:
535                 screen_switch(&screen_artist, c);
536                 break;
537 #endif
538 #ifdef ENABLE_SONG_SCREEN
539         case CMD_SCREEN_SONG:
540                 screen_switch(&screen_song, c);
541                 break;
542 #endif
543 #ifdef ENABLE_KEYDEF_SCREEN
544         case CMD_SCREEN_KEYDEF:
545                 screen_switch(&screen_keydef, c);
546                 break;
547 #endif
548 #ifdef ENABLE_LYRICS_SCREEN
549         case CMD_SCREEN_LYRICS:
550                 screen_switch(&screen_lyrics, c);
551                 break;
552 #endif
553 #ifdef ENABLE_OUTPUTS_SCREEN
554         case CMD_SCREEN_OUTPUTS:
555                 screen_switch(&screen_outputs, c);
556                 break;
557         case CMD_SCREEN_SWAP:
558                 screen_swap(c, NULL);
559                 break;
560 #endif
562         default:
563                 break;
564         }