Code

cf995383cf361dc0dae0f14e0b517f46ed44675d
[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 "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"
32 #include "screen_help.h"
33 #include "screen_play.h"
34 #include "screen_file.h"
35 #include "screen_artist.h"
36 #include "screen_search.h"
37 #include "screen_song.h"
38 #include "screen_keydef.h"
39 #include "screen_lyrics.h"
40 #include "screen_outputs.h"
42 #include <mpd/client.h>
44 #include <stdlib.h>
45 #include <unistd.h>
46 #include <stdarg.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)
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);
122 static int
123 find_configured_screen(const char *name)
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;
134 static void
135 screen_next_mode(struct mpdclient *c, int offset)
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);
154 static void
155 paint_top_window(const char *header, const struct mpdclient *c)
157         title_bar_paint(&screen.title_bar, header, c->status);
160 static void
161 paint_progress_window(struct mpdclient *c)
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                 !IS_STOPPED(mpd_status_get_state(c->status))
174                 ? mpd_status_get_total_time(c->status)
175                 : 0;
177         if (progress_bar_set(&screen.progress_bar, elapsed, duration))
178                 progress_bar_paint(&screen.progress_bar);
181 void
182 screen_exit(void)
184         if (mode_fn->close != NULL)
185                 mode_fn->close();
187         screen_list_exit();
189         string_list_free(screen.find_history);
190         g_free(screen.buf);
191         g_free(screen.findbuf);
193         title_bar_deinit(&screen.title_bar);
194         delwin(screen.main_window.w);
195         progress_bar_deinit(&screen.progress_bar);
196         status_bar_deinit(&screen.status_bar);
199 void
200 screen_resize(struct mpdclient *c)
202         if (COLS<SCREEN_MIN_COLS || LINES<SCREEN_MIN_ROWS) {
203                 screen_exit();
204                 fprintf(stderr, "%s", _("Error: Screen too small"));
205                 exit(EXIT_FAILURE);
206         }
208         resizeterm(LINES, COLS);
210         screen.cols = COLS;
211         screen.rows = LINES;
213         title_bar_resize(&screen.title_bar, screen.cols);
215         /* main window */
216         screen.main_window.cols = screen.cols;
217         screen.main_window.rows = screen.rows-4;
218         wresize(screen.main_window.w, screen.main_window.rows, screen.cols);
219         wclear(screen.main_window.w);
221         /* progress window */
222         progress_bar_resize(&screen.progress_bar, screen.cols,
223                             screen.rows - 2, 0);
224         progress_bar_paint(&screen.progress_bar);
226         /* status window */
227         status_bar_resize(&screen.status_bar, screen.cols, screen.rows - 1, 0);
228         status_bar_paint(&screen.status_bar, c->status, c->song);
230         screen.buf_size = screen.cols;
231         g_free(screen.buf);
232         screen.buf = g_malloc(screen.cols);
234         /* resize all screens */
235         screen_list_resize(screen.main_window.cols, screen.main_window.rows);
237         /* ? - without this the cursor becomes visible with aterm & Eterm */
238         curs_set(1);
239         curs_set(0);
241         screen_paint(c);
244 void
245 screen_status_message(const char *msg)
247         status_bar_message(&screen.status_bar, msg);
250 void
251 screen_status_printf(const char *format, ...)
253         char *msg;
254         va_list ap;
256         va_start(ap,format);
257         msg = g_strdup_vprintf(format,ap);
258         va_end(ap);
259         screen_status_message(msg);
260         g_free(msg);
263 void
264 screen_init(struct mpdclient *c)
266         if (COLS < SCREEN_MIN_COLS || LINES < SCREEN_MIN_ROWS) {
267                 fprintf(stderr, "%s\n", _("Error: Screen too small"));
268                 exit(EXIT_FAILURE);
269         }
271         screen.cols = COLS;
272         screen.rows = LINES;
274         screen.buf  = g_malloc(screen.cols);
275         screen.buf_size = screen.cols;
276         screen.findbuf = NULL;
277         screen.start_timestamp = time(NULL);
279         /* create top window */
280         title_bar_init(&screen.title_bar, screen.cols, 0, 0);
282         /* create main window */
283         window_init(&screen.main_window, screen.rows - 4, screen.cols, 2, 0);
285         //  leaveok(screen.main_window.w, TRUE); temporary disabled
286         keypad(screen.main_window.w, TRUE);
288         /* create progress window */
289         progress_bar_init(&screen.progress_bar, screen.cols,
290                           screen.rows - 2, 0);
291         progress_bar_paint(&screen.progress_bar);
293         /* create status window */
294         status_bar_init(&screen.status_bar, screen.cols, screen.rows - 1, 0);
295         status_bar_paint(&screen.status_bar, c->status, c->song);
297 #ifdef ENABLE_COLORS
298         if (options.enable_colors) {
299                 /* set background attributes */
300                 wbkgd(stdscr, COLOR_PAIR(COLOR_LIST));
301                 wbkgd(screen.main_window.w,     COLOR_PAIR(COLOR_LIST));
302                 wbkgd(screen.title_bar.window.w, COLOR_PAIR(COLOR_TITLE));
303                 wbkgd(screen.progress_bar.window.w,
304                       COLOR_PAIR(COLOR_PROGRESSBAR));
305                 wbkgd(screen.status_bar.window.w, COLOR_PAIR(COLOR_STATUS));
306                 colors_use(screen.progress_bar.window.w, COLOR_PROGRESSBAR);
307         }
308 #endif
310         doupdate();
312         /* initialize screens */
313         screen_list_init(screen.main_window.w,
314                          screen.main_window.cols, screen.main_window.rows);
316         if (mode_fn->open != NULL)
317                 mode_fn->open(c);
320 void
321 screen_paint(struct mpdclient *c)
323         const char *title = NULL;
325         if (mode_fn->get_title != NULL)
326                 title = mode_fn->get_title(screen.buf, screen.buf_size);
328         /* paint the title/header window */
329         if( title )
330                 paint_top_window(title, c);
331         else
332                 paint_top_window("", c);
334         /* paint the bottom window */
336         paint_progress_window(c);
337         status_bar_paint(&screen.status_bar, c->status, c->song);
339         /* paint the main window */
341         wclear(screen.main_window.w);
342         if (mode_fn->paint != NULL)
343                 mode_fn->paint();
345         /* move the cursor to the origin */
347         if (!options.hardware_cursor)
348                 wmove(screen.main_window.w, 0, 0);
350         wnoutrefresh(screen.main_window.w);
352         /* tell curses to update */
353         doupdate();
356 void
357 screen_update(struct mpdclient *c)
359 #ifndef NCMPC_MINI
360         static bool initialized = false;
361         static bool repeat;
362         static bool random_enabled;
363         static bool single;
364         static bool consume;
365         static unsigned crossfade;
367         /* print a message if mpd status has changed */
368         if ((c->events & MPD_IDLE_OPTIONS) && c->status != NULL) {
369                 if (!initialized) {
370                         repeat = mpd_status_get_repeat(c->status);
371                         random_enabled = mpd_status_get_random(c->status);
372                         single = mpd_status_get_single(c->status);
373                         consume = mpd_status_get_consume(c->status);
374                         crossfade = mpd_status_get_crossfade(c->status);
375                         initialized = true;
376                 }
378                 if (repeat != mpd_status_get_repeat(c->status))
379                         screen_status_printf(mpd_status_get_repeat(c->status) ?
380                                              _("Repeat mode is on") :
381                                              _("Repeat mode is off"));
383                 if (random_enabled != mpd_status_get_random(c->status))
384                         screen_status_printf(mpd_status_get_random(c->status) ?
385                                              _("Random mode is on") :
386                                              _("Random mode is off"));
388                 if (single != mpd_status_get_single(c->status))
389                         screen_status_printf(mpd_status_get_single(c->status) ?
390                                              /* "single" mode means
391                                                 that MPD will
392                                                 automatically stop
393                                                 after playing one
394                                                 single song */
395                                              _("Single mode is on") :
396                                              _("Single mode is off"));
398                 if (consume != mpd_status_get_consume(c->status))
399                         screen_status_printf(mpd_status_get_consume(c->status) ?
400                                              /* "consume" mode means
401                                                 that MPD removes each
402                                                 song which has
403                                                 finished playing */
404                                              _("Consume mode is on") :
405                                              _("Consume mode is off"));
407                 if (crossfade != mpd_status_get_crossfade(c->status))
408                         screen_status_printf(_("Crossfade %d seconds"),
409                                              mpd_status_get_crossfade(c->status));
411                 repeat = mpd_status_get_repeat(c->status);
412                 random_enabled = mpd_status_get_random(c->status);
413                 single = mpd_status_get_single(c->status);
414                 consume = mpd_status_get_consume(c->status);
415                 crossfade = mpd_status_get_crossfade(c->status);
416         }
418         if (c->events & MPD_IDLE_DATABASE)
419                 screen_status_printf(_("Database updated"));
421         /* update title/header window */
422         if (welcome && options.welcome_screen_list &&
423             time(NULL)-screen.start_timestamp <= SCREEN_WELCOME_TIME)
424                 paint_top_window("", c);
425         else
426 #endif
427         if (mode_fn->get_title != NULL) {
428                 paint_top_window(mode_fn->get_title(screen.buf,screen.buf_size), c);
429 #ifndef NCMPC_MINI
430                 welcome = FALSE;
431 #endif
432         } else
433                 paint_top_window("", c);
435         /* update progress window */
436         paint_progress_window(c);
438         /* update status window */
439         status_bar_paint(&screen.status_bar, c->status, c->song);
441         /* update the main window */
442         if (mode_fn->update != NULL)
443                 mode_fn->update(c);
445         /* move the cursor to the origin */
447         if (!options.hardware_cursor)
448                 wmove(screen.main_window.w, 0, 0);
450         wnoutrefresh(screen.main_window.w);
452         /* tell curses to update */
453         doupdate();
456 #ifdef HAVE_GETMOUSE
457 int
458 screen_get_mouse_event(struct mpdclient *c, unsigned long *bstate, int *row)
460         MEVENT event;
462         /* retrieve the mouse event from ncurses */
463         getmouse(&event);
464         /* calculate the selected row in the list window */
465         *row = event.y - screen.title_bar.window.rows;
466         /* copy button state bits */
467         *bstate = event.bstate;
468         /* if button 2 was pressed switch screen */
469         if (event.bstate & BUTTON2_CLICKED) {
470                 screen_cmd(c, CMD_SCREEN_NEXT);
471                 return 1;
472         }
474         return 0;
476 #endif
478 void
479 screen_cmd(struct mpdclient *c, command_t cmd)
481 #ifndef NCMPC_MINI
482         welcome = FALSE;
483 #endif
485         if (mode_fn->cmd != NULL && mode_fn->cmd(c, cmd))
486                 return;
488         if (handle_player_command(c, cmd))
489                 return;
491         switch(cmd) {
492         case CMD_TOGGLE_FIND_WRAP:
493                 options.find_wrap = !options.find_wrap;
494                 screen_status_printf(options.find_wrap ?
495                                      _("Find mode: Wrapped") :
496                                      _("Find mode: Normal"));
497                 break;
498         case CMD_TOGGLE_AUTOCENTER:
499                 options.auto_center = !options.auto_center;
500                 screen_status_printf(options.auto_center ?
501                                      _("Auto center mode: On") :
502                                      _("Auto center mode: Off"));
503                 break;
504         case CMD_SCREEN_UPDATE:
505                 screen_paint(c);
506                 break;
507         case CMD_SCREEN_PREVIOUS:
508                 screen_next_mode(c, -1);
509                 break;
510         case CMD_SCREEN_NEXT:
511                 screen_next_mode(c, 1);
512                 break;
513         case CMD_SCREEN_PLAY:
514                 screen_switch(&screen_playlist, c);
515                 break;
516         case CMD_SCREEN_FILE:
517                 screen_switch(&screen_browse, c);
518                 break;
519 #ifdef ENABLE_HELP_SCREEN
520         case CMD_SCREEN_HELP:
521                 screen_switch(&screen_help, c);
522                 break;
523 #endif
524 #ifdef ENABLE_SEARCH_SCREEN
525         case CMD_SCREEN_SEARCH:
526                 screen_switch(&screen_search, c);
527                 break;
528 #endif
529 #ifdef ENABLE_ARTIST_SCREEN
530         case CMD_SCREEN_ARTIST:
531                 screen_switch(&screen_artist, c);
532                 break;
533 #endif
534 #ifdef ENABLE_SONG_SCREEN
535         case CMD_SCREEN_SONG:
536                 screen_switch(&screen_song, c);
537                 break;
538 #endif
539 #ifdef ENABLE_KEYDEF_SCREEN
540         case CMD_SCREEN_KEYDEF:
541                 screen_switch(&screen_keydef, c);
542                 break;
543 #endif
544 #ifdef ENABLE_LYRICS_SCREEN
545         case CMD_SCREEN_LYRICS:
546                 screen_switch(&screen_lyrics, c);
547                 break;
548 #endif
549 #ifdef ENABLE_OUTPUTS_SCREEN
550         case CMD_SCREEN_OUTPUTS:
551                 screen_switch(&screen_outputs, c);
552                 break;
553         case CMD_SCREEN_SWAP:
554                 screen_swap(c, NULL);
555                 break;
556 #endif
558         default:
559                 break;
560         }