Code

screen: eliminated state macros
[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                 (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);
182 void
183 screen_exit(void)
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);
200 void
201 screen_resize(struct mpdclient *c)
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);
245 void
246 screen_status_message(const char *msg)
248         status_bar_message(&screen.status_bar, msg);
251 void
252 screen_status_printf(const char *format, ...)
254         char *msg;
255         va_list ap;
257         va_start(ap,format);
258         msg = g_strdup_vprintf(format,ap);
259         va_end(ap);
260         screen_status_message(msg);
261         g_free(msg);
264 void
265 screen_init(struct mpdclient *c)
267         if (COLS < SCREEN_MIN_COLS || LINES < SCREEN_MIN_ROWS) {
268                 fprintf(stderr, "%s\n", _("Error: Screen too small"));
269                 exit(EXIT_FAILURE);
270         }
272         screen.cols = COLS;
273         screen.rows = LINES;
275         screen.buf  = g_malloc(screen.cols);
276         screen.buf_size = screen.cols;
277         screen.findbuf = NULL;
278         screen.start_timestamp = time(NULL);
280         /* create top window */
281         title_bar_init(&screen.title_bar, screen.cols, 0, 0);
283         /* create main window */
284         window_init(&screen.main_window, screen.rows - 4, screen.cols, 2, 0);
286         //  leaveok(screen.main_window.w, TRUE); temporary disabled
287         keypad(screen.main_window.w, TRUE);
289         /* create progress window */
290         progress_bar_init(&screen.progress_bar, screen.cols,
291                           screen.rows - 2, 0);
292         progress_bar_paint(&screen.progress_bar);
294         /* create status window */
295         status_bar_init(&screen.status_bar, screen.cols, screen.rows - 1, 0);
296         status_bar_paint(&screen.status_bar, c->status, c->song);
298 #ifdef ENABLE_COLORS
299         if (options.enable_colors) {
300                 /* set background attributes */
301                 wbkgd(stdscr, COLOR_PAIR(COLOR_LIST));
302                 wbkgd(screen.main_window.w,     COLOR_PAIR(COLOR_LIST));
303                 wbkgd(screen.title_bar.window.w, COLOR_PAIR(COLOR_TITLE));
304                 wbkgd(screen.progress_bar.window.w,
305                       COLOR_PAIR(COLOR_PROGRESSBAR));
306                 wbkgd(screen.status_bar.window.w, COLOR_PAIR(COLOR_STATUS));
307                 colors_use(screen.progress_bar.window.w, COLOR_PROGRESSBAR);
308         }
309 #endif
311         doupdate();
313         /* initialize screens */
314         screen_list_init(screen.main_window.w,
315                          screen.main_window.cols, screen.main_window.rows);
317         if (mode_fn->open != NULL)
318                 mode_fn->open(c);
321 void
322 screen_paint(struct mpdclient *c)
324         const char *title = NULL;
326         if (mode_fn->get_title != NULL)
327                 title = mode_fn->get_title(screen.buf, screen.buf_size);
329         /* paint the title/header window */
330         if( title )
331                 paint_top_window(title, c);
332         else
333                 paint_top_window("", c);
335         /* paint the bottom window */
337         paint_progress_window(c);
338         status_bar_paint(&screen.status_bar, c->status, c->song);
340         /* paint the main window */
342         wclear(screen.main_window.w);
343         if (mode_fn->paint != NULL)
344                 mode_fn->paint();
346         /* move the cursor to the origin */
348         if (!options.hardware_cursor)
349                 wmove(screen.main_window.w, 0, 0);
351         wnoutrefresh(screen.main_window.w);
353         /* tell curses to update */
354         doupdate();
357 void
358 screen_update(struct mpdclient *c)
360 #ifndef NCMPC_MINI
361         static bool initialized = false;
362         static bool repeat;
363         static bool random_enabled;
364         static bool single;
365         static bool consume;
366         static unsigned crossfade;
368         /* print a message if mpd status has changed */
369         if ((c->events & MPD_IDLE_OPTIONS) && c->status != NULL) {
370                 if (!initialized) {
371                         repeat = mpd_status_get_repeat(c->status);
372                         random_enabled = mpd_status_get_random(c->status);
373                         single = mpd_status_get_single(c->status);
374                         consume = mpd_status_get_consume(c->status);
375                         crossfade = mpd_status_get_crossfade(c->status);
376                         initialized = true;
377                 }
379                 if (repeat != mpd_status_get_repeat(c->status))
380                         screen_status_printf(mpd_status_get_repeat(c->status) ?
381                                              _("Repeat mode is on") :
382                                              _("Repeat mode is off"));
384                 if (random_enabled != mpd_status_get_random(c->status))
385                         screen_status_printf(mpd_status_get_random(c->status) ?
386                                              _("Random mode is on") :
387                                              _("Random mode is off"));
389                 if (single != mpd_status_get_single(c->status))
390                         screen_status_printf(mpd_status_get_single(c->status) ?
391                                              /* "single" mode means
392                                                 that MPD will
393                                                 automatically stop
394                                                 after playing one
395                                                 single song */
396                                              _("Single mode is on") :
397                                              _("Single mode is off"));
399                 if (consume != mpd_status_get_consume(c->status))
400                         screen_status_printf(mpd_status_get_consume(c->status) ?
401                                              /* "consume" mode means
402                                                 that MPD removes each
403                                                 song which has
404                                                 finished playing */
405                                              _("Consume mode is on") :
406                                              _("Consume mode is off"));
408                 if (crossfade != mpd_status_get_crossfade(c->status))
409                         screen_status_printf(_("Crossfade %d seconds"),
410                                              mpd_status_get_crossfade(c->status));
412                 repeat = mpd_status_get_repeat(c->status);
413                 random_enabled = mpd_status_get_random(c->status);
414                 single = mpd_status_get_single(c->status);
415                 consume = mpd_status_get_consume(c->status);
416                 crossfade = mpd_status_get_crossfade(c->status);
417         }
419         if (c->events & MPD_IDLE_DATABASE)
420                 screen_status_printf(_("Database updated"));
422         /* update title/header window */
423         if (welcome && options.welcome_screen_list &&
424             time(NULL)-screen.start_timestamp <= SCREEN_WELCOME_TIME)
425                 paint_top_window("", c);
426         else
427 #endif
428         if (mode_fn->get_title != NULL) {
429                 paint_top_window(mode_fn->get_title(screen.buf,screen.buf_size), c);
430 #ifndef NCMPC_MINI
431                 welcome = FALSE;
432 #endif
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         welcome = FALSE;
484 #endif
486         if (mode_fn->cmd != NULL && mode_fn->cmd(c, cmd))
487                 return;
489         if (handle_player_command(c, cmd))
490                 return;
492         switch(cmd) {
493         case CMD_TOGGLE_FIND_WRAP:
494                 options.find_wrap = !options.find_wrap;
495                 screen_status_printf(options.find_wrap ?
496                                      _("Find mode: Wrapped") :
497                                      _("Find mode: Normal"));
498                 break;
499         case CMD_TOGGLE_AUTOCENTER:
500                 options.auto_center = !options.auto_center;
501                 screen_status_printf(options.auto_center ?
502                                      _("Auto center mode: On") :
503                                      _("Auto center mode: Off"));
504                 break;
505         case CMD_SCREEN_UPDATE:
506                 screen_paint(c);
507                 break;
508         case CMD_SCREEN_PREVIOUS:
509                 screen_next_mode(c, -1);
510                 break;
511         case CMD_SCREEN_NEXT:
512                 screen_next_mode(c, 1);
513                 break;
514         case CMD_SCREEN_PLAY:
515                 screen_switch(&screen_playlist, c);
516                 break;
517         case CMD_SCREEN_FILE:
518                 screen_switch(&screen_browse, c);
519                 break;
520 #ifdef ENABLE_HELP_SCREEN
521         case CMD_SCREEN_HELP:
522                 screen_switch(&screen_help, c);
523                 break;
524 #endif
525 #ifdef ENABLE_SEARCH_SCREEN
526         case CMD_SCREEN_SEARCH:
527                 screen_switch(&screen_search, c);
528                 break;
529 #endif
530 #ifdef ENABLE_ARTIST_SCREEN
531         case CMD_SCREEN_ARTIST:
532                 screen_switch(&screen_artist, c);
533                 break;
534 #endif
535 #ifdef ENABLE_SONG_SCREEN
536         case CMD_SCREEN_SONG:
537                 screen_switch(&screen_song, c);
538                 break;
539 #endif
540 #ifdef ENABLE_KEYDEF_SCREEN
541         case CMD_SCREEN_KEYDEF:
542                 screen_switch(&screen_keydef, c);
543                 break;
544 #endif
545 #ifdef ENABLE_LYRICS_SCREEN
546         case CMD_SCREEN_LYRICS:
547                 screen_switch(&screen_lyrics, c);
548                 break;
549 #endif
550 #ifdef ENABLE_OUTPUTS_SCREEN
551         case CMD_SCREEN_OUTPUTS:
552                 screen_switch(&screen_outputs, c);
553                 break;
554         case CMD_SCREEN_SWAP:
555                 screen_swap(c, NULL);
556                 break;
557 #endif
559         default:
560                 break;
561         }