Code

d9e7fdbe86ed0510a61ffd7d856e652b2e678a0b
[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"
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);
113 static int
114 find_configured_screen(const char *name)
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;
125 static void
126 screen_next_mode(struct mpdclient *c, int offset)
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);
145 static inline int
146 volume_length(int volume)
148         if (volume == 100)
149                 return 3;
150         if (volume >= 10 && volume < 100)
151                 return 2;
152         if (volume >= 0 && volume < 10)
153                 return 1;
154         return -1;
157 static void
158 paint_top_window(const char *header, const struct mpdclient *c)
160         title_bar_paint(&screen.title_bar, header, c->status);
163 static void
164 paint_progress_window(struct mpdclient *c)
166         unsigned elapsed, duration;
168         if (c->song != NULL && seek_id == (int)mpd_song_get_id(c->song))
169                 elapsed = seek_target_time;
170         else if (c->status != NULL)
171                 elapsed = mpd_status_get_elapsed_time(c->status);
172         else
173                 elapsed = 0;
175         duration = c->status != NULL &&
176                 !IS_STOPPED(mpd_status_get_state(c->status))
177                 ? mpd_status_get_total_time(c->status)
178                 : 0;
180         if (progress_bar_set(&screen.progress_bar, elapsed, duration))
181                 progress_bar_paint(&screen.progress_bar);
184 void
185 screen_exit(void)
187         if (mode_fn->close != NULL)
188                 mode_fn->close();
190         screen_list_exit();
192         string_list_free(screen.find_history);
193         g_free(screen.buf);
194         g_free(screen.findbuf);
196         title_bar_deinit(&screen.title_bar);
197         delwin(screen.main_window.w);
198         progress_bar_deinit(&screen.progress_bar);
199         status_bar_deinit(&screen.status_bar);
202 void
203 screen_resize(struct mpdclient *c)
205         if (COLS<SCREEN_MIN_COLS || LINES<SCREEN_MIN_ROWS) {
206                 screen_exit();
207                 fprintf(stderr, "%s", _("Error: Screen too small"));
208                 exit(EXIT_FAILURE);
209         }
211         resizeterm(LINES, COLS);
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);
247 void
248 screen_status_message(const char *msg)
250         status_bar_message(&screen.status_bar, msg);
253 void
254 screen_status_printf(const char *format, ...)
256         char *msg;
257         va_list ap;
259         va_start(ap,format);
260         msg = g_strdup_vprintf(format,ap);
261         va_end(ap);
262         screen_status_message(msg);
263         g_free(msg);
266 void
267 screen_init(struct mpdclient *c)
269         if (COLS < SCREEN_MIN_COLS || LINES < SCREEN_MIN_ROWS) {
270                 fprintf(stderr, "%s\n", _("Error: Screen too small"));
271                 exit(EXIT_FAILURE);
272         }
274         screen.cols = COLS;
275         screen.rows = LINES;
277         screen.buf  = g_malloc(screen.cols);
278         screen.buf_size = screen.cols;
279         screen.findbuf = NULL;
280         screen.start_timestamp = time(NULL);
282         /* create top window */
283         title_bar_init(&screen.title_bar, screen.cols, 0, 0);
285         /* create main window */
286         window_init(&screen.main_window, screen.rows - 4, screen.cols, 2, 0);
288         //  leaveok(screen.main_window.w, TRUE); temporary disabled
289         keypad(screen.main_window.w, TRUE);
291         /* create progress window */
292         progress_bar_init(&screen.progress_bar, screen.cols,
293                           screen.rows - 2, 0);
294         progress_bar_paint(&screen.progress_bar);
296         /* create status window */
297         status_bar_init(&screen.status_bar, screen.cols, screen.rows - 1, 0);
298         status_bar_paint(&screen.status_bar, c->status, c->song);
300 #ifdef ENABLE_COLORS
301         if (options.enable_colors) {
302                 /* set background attributes */
303                 wbkgd(stdscr, COLOR_PAIR(COLOR_LIST));
304                 wbkgd(screen.main_window.w,     COLOR_PAIR(COLOR_LIST));
305                 wbkgd(screen.title_bar.window.w, COLOR_PAIR(COLOR_TITLE));
306                 wbkgd(screen.progress_bar.window.w,
307                       COLOR_PAIR(COLOR_PROGRESSBAR));
308                 wbkgd(screen.status_bar.window.w, COLOR_PAIR(COLOR_STATUS));
309                 colors_use(screen.progress_bar.window.w, COLOR_PROGRESSBAR);
310         }
311 #endif
313         doupdate();
315         /* initialize screens */
316         screen_list_init(screen.main_window.w,
317                          screen.main_window.cols, screen.main_window.rows);
319         if (mode_fn->open != NULL)
320                 mode_fn->open(c);
323 void
324 screen_paint(struct mpdclient *c)
326         const char *title = NULL;
328         if (mode_fn->get_title != NULL)
329                 title = mode_fn->get_title(screen.buf, screen.buf_size);
331         /* paint the title/header window */
332         if( title )
333                 paint_top_window(title, c);
334         else
335                 paint_top_window("", c);
337         /* paint the bottom window */
339         paint_progress_window(c);
340         status_bar_paint(&screen.status_bar, c->status, c->song);
342         /* paint the main window */
344         wclear(screen.main_window.w);
345         if (mode_fn->paint != NULL)
346                 mode_fn->paint();
348         /* move the cursor to the origin */
350         if (!options.hardware_cursor)
351                 wmove(screen.main_window.w, 0, 0);
353         wnoutrefresh(screen.main_window.w);
355         /* tell curses to update */
356         doupdate();
359 void
360 screen_update(struct mpdclient *c)
362 #ifndef NCMPC_MINI
363         static bool initialized = false;
364         static bool repeat;
365         static bool random_enabled;
366         static bool single;
367         static bool consume;
368         static unsigned crossfade;
370         /* print a message if mpd status has changed */
371         if ((c->events & MPD_IDLE_OPTIONS) && c->status != NULL) {
372                 if (!initialized) {
373                         repeat = mpd_status_get_repeat(c->status);
374                         random_enabled = mpd_status_get_random(c->status);
375                         single = mpd_status_get_single(c->status);
376                         consume = mpd_status_get_consume(c->status);
377                         crossfade = mpd_status_get_crossfade(c->status);
378                         initialized = true;
379                 }
381                 if (repeat != mpd_status_get_repeat(c->status))
382                         screen_status_printf(mpd_status_get_repeat(c->status) ?
383                                              _("Repeat mode is on") :
384                                              _("Repeat mode is off"));
386                 if (random_enabled != mpd_status_get_random(c->status))
387                         screen_status_printf(mpd_status_get_random(c->status) ?
388                                              _("Random mode is on") :
389                                              _("Random mode is off"));
391                 if (single != mpd_status_get_single(c->status))
392                         screen_status_printf(mpd_status_get_single(c->status) ?
393                                              /* "single" mode means
394                                                 that MPD will
395                                                 automatically stop
396                                                 after playing one
397                                                 single song */
398                                              _("Single mode is on") :
399                                              _("Single mode is off"));
401                 if (consume != mpd_status_get_consume(c->status))
402                         screen_status_printf(mpd_status_get_consume(c->status) ?
403                                              /* "consume" mode means
404                                                 that MPD removes each
405                                                 song which has
406                                                 finished playing */
407                                              _("Consume mode is on") :
408                                              _("Consume mode is off"));
410                 if (crossfade != mpd_status_get_crossfade(c->status))
411                         screen_status_printf(_("Crossfade %d seconds"),
412                                              mpd_status_get_crossfade(c->status));
414                 repeat = mpd_status_get_repeat(c->status);
415                 random_enabled = mpd_status_get_random(c->status);
416                 single = mpd_status_get_single(c->status);
417                 consume = mpd_status_get_consume(c->status);
418                 crossfade = mpd_status_get_crossfade(c->status);
419         }
421         if (c->events & MPD_IDLE_DATABASE)
422                 screen_status_printf(_("Database updated"));
424         /* update title/header window */
425         if (welcome && options.welcome_screen_list &&
426             time(NULL)-screen.start_timestamp <= SCREEN_WELCOME_TIME)
427                 paint_top_window("", c);
428         else
429 #endif
430         if (mode_fn->get_title != NULL) {
431                 paint_top_window(mode_fn->get_title(screen.buf,screen.buf_size), c);
432 #ifndef NCMPC_MINI
433                 welcome = FALSE;
434 #endif
435         } else
436                 paint_top_window("", c);
438         /* update progress window */
439         paint_progress_window(c);
441         /* update status window */
442         status_bar_paint(&screen.status_bar, c->status, c->song);
444         /* update the main window */
445         if (mode_fn->update != NULL)
446                 mode_fn->update(c);
448         /* move the cursor to the origin */
450         if (!options.hardware_cursor)
451                 wmove(screen.main_window.w, 0, 0);
453         wnoutrefresh(screen.main_window.w);
455         /* tell curses to update */
456         doupdate();
459 #ifdef HAVE_GETMOUSE
460 int
461 screen_get_mouse_event(struct mpdclient *c, unsigned long *bstate, int *row)
463         MEVENT event;
465         /* retrieve the mouse event from ncurses */
466         getmouse(&event);
467         /* calculate the selected row in the list window */
468         *row = event.y - screen.title_bar.window.rows;
469         /* copy button state bits */
470         *bstate = event.bstate;
471         /* if button 2 was pressed switch screen */
472         if (event.bstate & BUTTON2_CLICKED) {
473                 screen_cmd(c, CMD_SCREEN_NEXT);
474                 return 1;
475         }
477         return 0;
479 #endif
481 void
482 screen_cmd(struct mpdclient *c, command_t cmd)
484 #ifndef NCMPC_MINI
485         welcome = FALSE;
486 #endif
488         if (mode_fn->cmd != NULL && mode_fn->cmd(c, cmd))
489                 return;
491         if (handle_player_command(c, cmd))
492                 return;
494         switch(cmd) {
495         case CMD_TOGGLE_FIND_WRAP:
496                 options.find_wrap = !options.find_wrap;
497                 screen_status_printf(options.find_wrap ?
498                                      _("Find mode: Wrapped") :
499                                      _("Find mode: Normal"));
500                 break;
501         case CMD_TOGGLE_AUTOCENTER:
502                 options.auto_center = !options.auto_center;
503                 screen_status_printf(options.auto_center ?
504                                      _("Auto center mode: On") :
505                                      _("Auto center mode: Off"));
506                 break;
507         case CMD_SCREEN_UPDATE:
508                 screen_paint(c);
509                 break;
510         case CMD_SCREEN_PREVIOUS:
511                 screen_next_mode(c, -1);
512                 break;
513         case CMD_SCREEN_NEXT:
514                 screen_next_mode(c, 1);
515                 break;
516         case CMD_SCREEN_PLAY:
517                 screen_switch(&screen_playlist, c);
518                 break;
519         case CMD_SCREEN_FILE:
520                 screen_switch(&screen_browse, c);
521                 break;
522 #ifdef ENABLE_HELP_SCREEN
523         case CMD_SCREEN_HELP:
524                 screen_switch(&screen_help, c);
525                 break;
526 #endif
527 #ifdef ENABLE_SEARCH_SCREEN
528         case CMD_SCREEN_SEARCH:
529                 screen_switch(&screen_search, c);
530                 break;
531 #endif
532 #ifdef ENABLE_ARTIST_SCREEN
533         case CMD_SCREEN_ARTIST:
534                 screen_switch(&screen_artist, c);
535                 break;
536 #endif
537 #ifdef ENABLE_SONG_SCREEN
538         case CMD_SCREEN_SONG:
539                 screen_switch(&screen_song, c);
540                 break;
541 #endif
542 #ifdef ENABLE_KEYDEF_SCREEN
543         case CMD_SCREEN_KEYDEF:
544                 screen_switch(&screen_keydef, c);
545                 break;
546 #endif
547 #ifdef ENABLE_LYRICS_SCREEN
548         case CMD_SCREEN_LYRICS:
549                 screen_switch(&screen_lyrics, c);
550                 break;
551 #endif
552 #ifdef ENABLE_OUTPUTS_SCREEN
553         case CMD_SCREEN_OUTPUTS:
554                 screen_switch(&screen_outputs, c);
555                 break;
556         case CMD_SCREEN_SWAP:
557                 screen_swap(c, NULL);
558                 break;
559 #endif
561         default:
562                 break;
563         }