Code

Merge remote branches 'jn/cosmetics', 'jn/doxygen' and 'jn/renames'
[ncmpc.git] / src / screen.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2010 The Music Player Daemon Project
3  * Project homepage: http://musicpd.org
4  *
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.
9  *
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.
14  *
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_status.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_queue.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_queue;
64 static const struct screen_functions *mode_fn_prev = &screen_queue;
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 update_progress_window(struct mpdclient *c, bool repaint)
159         unsigned elapsed, duration;
161         if (c->status == NULL)
162                 elapsed = 0;
163         else if (seek_id >= 0 && seek_id == mpd_status_get_song_id(c->status))
164                 elapsed = seek_target_time;
165         else
166                 elapsed = mpd_status_get_elapsed_time(c->status);
168         duration = mpdclient_is_playing(c)
169                 ? mpd_status_get_total_time(c->status)
170                 : 0;
172         if (progress_bar_set(&screen.progress_bar, elapsed, duration) ||
173             repaint)
174                 progress_bar_paint(&screen.progress_bar);
177 void
178 screen_exit(void)
180         if (mode_fn->close != NULL)
181                 mode_fn->close();
183         screen_list_exit();
185         string_list_free(screen.find_history);
186         g_free(screen.buf);
187         g_free(screen.findbuf);
189         title_bar_deinit(&screen.title_bar);
190         delwin(screen.main_window.w);
191         progress_bar_deinit(&screen.progress_bar);
192         status_bar_deinit(&screen.status_bar);
194 #ifndef NCMPC_MINI
195         if (screen.welcome_source_id != 0)
196                 g_source_remove(screen.welcome_source_id);
197 #endif
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\n", _("Error: Screen too small"));
206                 exit(EXIT_FAILURE);
207         }
208 #ifdef PDCURSES
209         resize_term(LINES, COLS);
210 #else 
211         resizeterm(LINES, COLS);
212 #endif
214         screen.cols = COLS;
215         screen.rows = LINES;
217         title_bar_resize(&screen.title_bar, screen.cols);
219         /* main window */
220         screen.main_window.cols = screen.cols;
221         screen.main_window.rows = screen.rows-4;
222         wresize(screen.main_window.w, screen.main_window.rows, screen.cols);
223         wclear(screen.main_window.w);
225         /* progress window */
226         progress_bar_resize(&screen.progress_bar, screen.cols,
227                             screen.rows - 2, 0);
228         progress_bar_paint(&screen.progress_bar);
230         /* status window */
231         status_bar_resize(&screen.status_bar, screen.cols, screen.rows - 1, 0);
232         status_bar_paint(&screen.status_bar, c->status, c->song);
234         screen.buf_size = screen.cols;
235         g_free(screen.buf);
236         screen.buf = g_malloc(screen.cols);
238         /* resize all screens */
239         screen_list_resize(screen.main_window.cols, screen.main_window.rows);
241         /* ? - without this the cursor becomes visible with aterm & Eterm */
242         curs_set(1);
243         curs_set(0);
245         screen_paint(c);
248 #ifndef NCMPC_MINI
249 static gboolean
250 welcome_timer_callback(gpointer data)
252         struct mpdclient *c = data;
254 #ifndef NCMPC_MINI
255         screen.welcome_source_id = 0;
256 #endif
258         paint_top_window(mode_fn->get_title != NULL
259                          ? mode_fn->get_title(screen.buf, screen.buf_size)
260                          : "",
261                          c);
262         doupdate();
264         return false;
266 #endif
268 void
269 screen_init(struct mpdclient *c)
271         if (COLS < SCREEN_MIN_COLS || LINES < SCREEN_MIN_ROWS) {
272                 fprintf(stderr, "%s\n", _("Error: Screen too small"));
273                 exit(EXIT_FAILURE);
274         }
276         screen.cols = COLS;
277         screen.rows = LINES;
279         screen.buf  = g_malloc(screen.cols);
280         screen.buf_size = screen.cols;
281         screen.findbuf = NULL;
283 #ifndef NCMPC_MINI
284         if (options.welcome_screen_list)
285                 screen.welcome_source_id =
286                         g_timeout_add(SCREEN_WELCOME_TIME * 1000,
287                                       welcome_timer_callback, c);
288 #endif
290         /* create top window */
291         title_bar_init(&screen.title_bar, screen.cols, 0, 0);
293         /* create main window */
294         window_init(&screen.main_window, screen.rows - 4, screen.cols, 2, 0);
296         if (!options.hardware_cursor)
297                 leaveok(screen.main_window.w, TRUE);
299         keypad(screen.main_window.w, TRUE);
301         /* create progress window */
302         progress_bar_init(&screen.progress_bar, screen.cols,
303                           screen.rows - 2, 0);
304         progress_bar_paint(&screen.progress_bar);
306         /* create status window */
307         status_bar_init(&screen.status_bar, screen.cols, screen.rows - 1, 0);
308         status_bar_paint(&screen.status_bar, c->status, c->song);
310 #ifdef ENABLE_COLORS
311         if (options.enable_colors) {
312                 /* set background attributes */
313                 wbkgd(stdscr, COLOR_PAIR(COLOR_LIST));
314                 wbkgd(screen.main_window.w,     COLOR_PAIR(COLOR_LIST));
315                 wbkgd(screen.title_bar.window.w, COLOR_PAIR(COLOR_TITLE));
316                 wbkgd(screen.progress_bar.window.w,
317                       COLOR_PAIR(COLOR_PROGRESSBAR));
318                 wbkgd(screen.status_bar.window.w, COLOR_PAIR(COLOR_STATUS));
319                 colors_use(screen.progress_bar.window.w, COLOR_PROGRESSBAR);
320         }
321 #endif
323         doupdate();
325         /* initialize screens */
326         screen_list_init(screen.main_window.w,
327                          screen.main_window.cols, screen.main_window.rows);
329         if (mode_fn->open != NULL)
330                 mode_fn->open(c);
333 void
334 screen_paint(struct mpdclient *c)
336         const char *title = NULL;
338         if (mode_fn->get_title != NULL)
339                 title = mode_fn->get_title(screen.buf, screen.buf_size);
341         /* paint the title/header window */
342         if( title )
343                 paint_top_window(title, c);
344         else
345                 paint_top_window("", c);
347         /* paint the bottom window */
349         update_progress_window(c, true);
350         status_bar_paint(&screen.status_bar, c->status, c->song);
352         /* paint the main window */
354         wclear(screen.main_window.w);
355         if (mode_fn->paint != NULL)
356                 mode_fn->paint();
358         /* move the cursor to the origin */
360         if (!options.hardware_cursor)
361                 wmove(screen.main_window.w, 0, 0);
363         wnoutrefresh(screen.main_window.w);
365         /* tell curses to update */
366         doupdate();
369 void
370 screen_update(struct mpdclient *c)
372 #ifndef NCMPC_MINI
373         static bool was_connected;
374         static bool initialized = false;
375         static bool repeat;
376         static bool random_enabled;
377         static bool single;
378         static bool consume;
379         static unsigned crossfade;
381         /* print a message if mpd status has changed */
382         if ((c->events & MPD_IDLE_OPTIONS) && c->status != NULL) {
383                 if (!initialized) {
384                         repeat = mpd_status_get_repeat(c->status);
385                         random_enabled = mpd_status_get_random(c->status);
386                         single = mpd_status_get_single(c->status);
387                         consume = mpd_status_get_consume(c->status);
388                         crossfade = mpd_status_get_crossfade(c->status);
389                         initialized = true;
390                 }
392                 if (repeat != mpd_status_get_repeat(c->status))
393                         screen_status_printf(mpd_status_get_repeat(c->status) ?
394                                              _("Repeat mode is on") :
395                                              _("Repeat mode is off"));
397                 if (random_enabled != mpd_status_get_random(c->status))
398                         screen_status_printf(mpd_status_get_random(c->status) ?
399                                              _("Random mode is on") :
400                                              _("Random mode is off"));
402                 if (single != mpd_status_get_single(c->status))
403                         screen_status_printf(mpd_status_get_single(c->status) ?
404                                              /* "single" mode means
405                                                 that MPD will
406                                                 automatically stop
407                                                 after playing one
408                                                 single song */
409                                              _("Single mode is on") :
410                                              _("Single mode is off"));
412                 if (consume != mpd_status_get_consume(c->status))
413                         screen_status_printf(mpd_status_get_consume(c->status) ?
414                                              /* "consume" mode means
415                                                 that MPD removes each
416                                                 song which has
417                                                 finished playing */
418                                              _("Consume mode is on") :
419                                              _("Consume mode is off"));
421                 if (crossfade != mpd_status_get_crossfade(c->status))
422                         screen_status_printf(_("Crossfade %d seconds"),
423                                              mpd_status_get_crossfade(c->status));
425                 repeat = mpd_status_get_repeat(c->status);
426                 random_enabled = mpd_status_get_random(c->status);
427                 single = mpd_status_get_single(c->status);
428                 consume = mpd_status_get_consume(c->status);
429                 crossfade = mpd_status_get_crossfade(c->status);
430         }
432         if ((c->events & MPD_IDLE_DATABASE) != 0 && was_connected &&
433             mpdclient_is_connected(c))
434                 screen_status_printf(_("Database updated"));
435         was_connected = mpdclient_is_connected(c);
437         /* update title/header window */
438         if (screen.welcome_source_id != 0)
439                 paint_top_window("", c);
440         else
441 #endif
442         if (mode_fn->get_title != NULL) {
443                 paint_top_window(mode_fn->get_title(screen.buf,screen.buf_size), c);
444         } else
445                 paint_top_window("", c);
447         /* update progress window */
448         update_progress_window(c, false);
450         /* update status window */
451         status_bar_paint(&screen.status_bar, c->status, c->song);
453         /* update the main window */
454         if (mode_fn->update != NULL)
455                 mode_fn->update(c);
457         /* move the cursor to the origin */
459         if (!options.hardware_cursor)
460                 wmove(screen.main_window.w, 0, 0);
462         wnoutrefresh(screen.main_window.w);
464         /* tell curses to update */
465         doupdate();
468 #ifdef HAVE_GETMOUSE
469 int
470 screen_get_mouse_event(struct mpdclient *c, unsigned long *bstate, int *row)
472         MEVENT event;
474         /* retrieve the mouse event from curses */
475 #ifdef PDCURSES
476         nc_getmouse(&event);
477 #else
478         getmouse(&event);
479 #endif
480         /* calculate the selected row in the list window */
481         *row = event.y - screen.title_bar.window.rows;
482         /* copy button state bits */
483         *bstate = event.bstate;
484         /* if button 2 was pressed switch screen */
485         if (event.bstate & BUTTON2_CLICKED) {
486                 screen_cmd(c, CMD_SCREEN_NEXT);
487                 return 1;
488         }
490         return 0;
492 #endif
494 void
495 screen_cmd(struct mpdclient *c, command_t cmd)
497 #ifndef NCMPC_MINI
498         if (screen.welcome_source_id != 0) {
499                 g_source_remove(screen.welcome_source_id);
500                 screen.welcome_source_id = 0;
501         }
502 #endif
504         if (mode_fn->cmd != NULL && mode_fn->cmd(c, cmd))
505                 return;
507         if (handle_player_command(c, cmd))
508                 return;
510         switch(cmd) {
511         case CMD_TOGGLE_FIND_WRAP:
512                 options.find_wrap = !options.find_wrap;
513                 screen_status_printf(options.find_wrap ?
514                                      _("Find mode: Wrapped") :
515                                      _("Find mode: Normal"));
516                 break;
517         case CMD_TOGGLE_AUTOCENTER:
518                 options.auto_center = !options.auto_center;
519                 screen_status_printf(options.auto_center ?
520                                      _("Auto center mode: On") :
521                                      _("Auto center mode: Off"));
522                 break;
523         case CMD_SCREEN_UPDATE:
524                 screen_paint(c);
525                 break;
526         case CMD_SCREEN_PREVIOUS:
527                 screen_next_mode(c, -1);
528                 break;
529         case CMD_SCREEN_NEXT:
530                 screen_next_mode(c, 1);
531                 break;
532         case CMD_SCREEN_PLAY:
533                 screen_switch(&screen_queue, c);
534                 break;
535         case CMD_SCREEN_FILE:
536                 screen_switch(&screen_browse, c);
537                 break;
538 #ifdef ENABLE_HELP_SCREEN
539         case CMD_SCREEN_HELP:
540                 screen_switch(&screen_help, c);
541                 break;
542 #endif
543 #ifdef ENABLE_SEARCH_SCREEN
544         case CMD_SCREEN_SEARCH:
545                 screen_switch(&screen_search, c);
546                 break;
547 #endif
548 #ifdef ENABLE_ARTIST_SCREEN
549         case CMD_SCREEN_ARTIST:
550                 screen_switch(&screen_artist, c);
551                 break;
552 #endif
553 #ifdef ENABLE_SONG_SCREEN
554         case CMD_SCREEN_SONG:
555                 screen_switch(&screen_song, c);
556                 break;
557 #endif
558 #ifdef ENABLE_KEYDEF_SCREEN
559         case CMD_SCREEN_KEYDEF:
560                 screen_switch(&screen_keydef, c);
561                 break;
562 #endif
563 #ifdef ENABLE_LYRICS_SCREEN
564         case CMD_SCREEN_LYRICS:
565                 screen_switch(&screen_lyrics, c);
566                 break;
567 #endif
568 #ifdef ENABLE_OUTPUTS_SCREEN
569         case CMD_SCREEN_OUTPUTS:
570                 screen_switch(&screen_outputs, c);
571                 break;
572 #endif
573         case CMD_SCREEN_SWAP:
574                 screen_swap(c, NULL);
575                 break;
577         default:
578                 break;
579         }