Code

d3a826523b548380a9638d539113d31d764ebc38
[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_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", _("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 #ifndef NCMPC_MINI
246 static gboolean
247 welcome_timer_callback(gpointer data)
249         struct mpdclient *c = data;
251 #ifndef NCMPC_MINI
252         screen.welcome_source_id = 0;
253 #endif
255         paint_top_window(mode_fn->get_title != NULL
256                          ? mode_fn->get_title(screen.buf, screen.buf_size)
257                          : "",
258                          c);
259         doupdate();
261         return false;
263 #endif
265 void
266 screen_init(struct mpdclient *c)
268         if (COLS < SCREEN_MIN_COLS || LINES < SCREEN_MIN_ROWS) {
269                 fprintf(stderr, "%s\n", _("Error: Screen too small"));
270                 exit(EXIT_FAILURE);
271         }
273         screen.cols = COLS;
274         screen.rows = LINES;
276         screen.buf  = g_malloc(screen.cols);
277         screen.buf_size = screen.cols;
278         screen.findbuf = NULL;
280 #ifndef NCMPC_MINI
281         if (options.welcome_screen_list)
282                 screen.welcome_source_id =
283                         g_timeout_add(SCREEN_WELCOME_TIME * 1000,
284                                       welcome_timer_callback, c);
285 #endif
287         /* create top window */
288         title_bar_init(&screen.title_bar, screen.cols, 0, 0);
290         /* create main window */
291         window_init(&screen.main_window, screen.rows - 4, screen.cols, 2, 0);
293         //  leaveok(screen.main_window.w, TRUE); temporary disabled
294         keypad(screen.main_window.w, TRUE);
296         /* create progress window */
297         progress_bar_init(&screen.progress_bar, screen.cols,
298                           screen.rows - 2, 0);
299         progress_bar_paint(&screen.progress_bar);
301         /* create status window */
302         status_bar_init(&screen.status_bar, screen.cols, screen.rows - 1, 0);
303         status_bar_paint(&screen.status_bar, c->status, c->song);
305 #ifdef ENABLE_COLORS
306         if (options.enable_colors) {
307                 /* set background attributes */
308                 wbkgd(stdscr, COLOR_PAIR(COLOR_LIST));
309                 wbkgd(screen.main_window.w,     COLOR_PAIR(COLOR_LIST));
310                 wbkgd(screen.title_bar.window.w, COLOR_PAIR(COLOR_TITLE));
311                 wbkgd(screen.progress_bar.window.w,
312                       COLOR_PAIR(COLOR_PROGRESSBAR));
313                 wbkgd(screen.status_bar.window.w, COLOR_PAIR(COLOR_STATUS));
314                 colors_use(screen.progress_bar.window.w, COLOR_PROGRESSBAR);
315         }
316 #endif
318         doupdate();
320         /* initialize screens */
321         screen_list_init(screen.main_window.w,
322                          screen.main_window.cols, screen.main_window.rows);
324         if (mode_fn->open != NULL)
325                 mode_fn->open(c);
328 void
329 screen_paint(struct mpdclient *c)
331         const char *title = NULL;
333         if (mode_fn->get_title != NULL)
334                 title = mode_fn->get_title(screen.buf, screen.buf_size);
336         /* paint the title/header window */
337         if( title )
338                 paint_top_window(title, c);
339         else
340                 paint_top_window("", c);
342         /* paint the bottom window */
344         update_progress_window(c, true);
345         status_bar_paint(&screen.status_bar, c->status, c->song);
347         /* paint the main window */
349         wclear(screen.main_window.w);
350         if (mode_fn->paint != NULL)
351                 mode_fn->paint();
353         /* move the cursor to the origin */
355         if (!options.hardware_cursor)
356                 wmove(screen.main_window.w, 0, 0);
358         wnoutrefresh(screen.main_window.w);
360         /* tell curses to update */
361         doupdate();
364 void
365 screen_update(struct mpdclient *c)
367 #ifndef NCMPC_MINI
368         static bool was_connected;
369         static bool initialized = false;
370         static bool repeat;
371         static bool random_enabled;
372         static bool single;
373         static bool consume;
374         static unsigned crossfade;
376         /* print a message if mpd status has changed */
377         if ((c->events & MPD_IDLE_OPTIONS) && c->status != NULL) {
378                 if (!initialized) {
379                         repeat = mpd_status_get_repeat(c->status);
380                         random_enabled = mpd_status_get_random(c->status);
381                         single = mpd_status_get_single(c->status);
382                         consume = mpd_status_get_consume(c->status);
383                         crossfade = mpd_status_get_crossfade(c->status);
384                         initialized = true;
385                 }
387                 if (repeat != mpd_status_get_repeat(c->status))
388                         screen_status_printf(mpd_status_get_repeat(c->status) ?
389                                              _("Repeat mode is on") :
390                                              _("Repeat mode is off"));
392                 if (random_enabled != mpd_status_get_random(c->status))
393                         screen_status_printf(mpd_status_get_random(c->status) ?
394                                              _("Random mode is on") :
395                                              _("Random mode is off"));
397                 if (single != mpd_status_get_single(c->status))
398                         screen_status_printf(mpd_status_get_single(c->status) ?
399                                              /* "single" mode means
400                                                 that MPD will
401                                                 automatically stop
402                                                 after playing one
403                                                 single song */
404                                              _("Single mode is on") :
405                                              _("Single mode is off"));
407                 if (consume != mpd_status_get_consume(c->status))
408                         screen_status_printf(mpd_status_get_consume(c->status) ?
409                                              /* "consume" mode means
410                                                 that MPD removes each
411                                                 song which has
412                                                 finished playing */
413                                              _("Consume mode is on") :
414                                              _("Consume mode is off"));
416                 if (crossfade != mpd_status_get_crossfade(c->status))
417                         screen_status_printf(_("Crossfade %d seconds"),
418                                              mpd_status_get_crossfade(c->status));
420                 repeat = mpd_status_get_repeat(c->status);
421                 random_enabled = mpd_status_get_random(c->status);
422                 single = mpd_status_get_single(c->status);
423                 consume = mpd_status_get_consume(c->status);
424                 crossfade = mpd_status_get_crossfade(c->status);
425         }
427         if ((c->events & MPD_IDLE_DATABASE) != 0 && was_connected &&
428             mpdclient_is_connected(c))
429                 screen_status_printf(_("Database updated"));
430         was_connected = mpdclient_is_connected(c);
432         /* update title/header window */
433         if (screen.welcome_source_id != 0)
434                 paint_top_window("", c);
435         else
436 #endif
437         if (mode_fn->get_title != NULL) {
438                 paint_top_window(mode_fn->get_title(screen.buf,screen.buf_size), c);
439         } else
440                 paint_top_window("", c);
442         /* update progress window */
443         update_progress_window(c, false);
445         /* update status window */
446         status_bar_paint(&screen.status_bar, c->status, c->song);
448         /* update the main window */
449         if (mode_fn->update != NULL)
450                 mode_fn->update(c);
452         /* move the cursor to the origin */
454         if (!options.hardware_cursor)
455                 wmove(screen.main_window.w, 0, 0);
457         wnoutrefresh(screen.main_window.w);
459         /* tell curses to update */
460         doupdate();
463 #ifdef HAVE_GETMOUSE
464 int
465 screen_get_mouse_event(struct mpdclient *c, unsigned long *bstate, int *row)
467         MEVENT event;
469         /* retrieve the mouse event from ncurses */
470         getmouse(&event);
471         /* calculate the selected row in the list window */
472         *row = event.y - screen.title_bar.window.rows;
473         /* copy button state bits */
474         *bstate = event.bstate;
475         /* if button 2 was pressed switch screen */
476         if (event.bstate & BUTTON2_CLICKED) {
477                 screen_cmd(c, CMD_SCREEN_NEXT);
478                 return 1;
479         }
481         return 0;
483 #endif
485 void
486 screen_cmd(struct mpdclient *c, command_t cmd)
488 #ifndef NCMPC_MINI
489         if (screen.welcome_source_id != 0) {
490                 g_source_remove(screen.welcome_source_id);
491                 screen.welcome_source_id = 0;
492         }
493 #endif
495         if (mode_fn->cmd != NULL && mode_fn->cmd(c, cmd))
496                 return;
498         if (handle_player_command(c, cmd))
499                 return;
501         switch(cmd) {
502         case CMD_TOGGLE_FIND_WRAP:
503                 options.find_wrap = !options.find_wrap;
504                 screen_status_printf(options.find_wrap ?
505                                      _("Find mode: Wrapped") :
506                                      _("Find mode: Normal"));
507                 break;
508         case CMD_TOGGLE_AUTOCENTER:
509                 options.auto_center = !options.auto_center;
510                 screen_status_printf(options.auto_center ?
511                                      _("Auto center mode: On") :
512                                      _("Auto center mode: Off"));
513                 break;
514         case CMD_SCREEN_UPDATE:
515                 screen_paint(c);
516                 break;
517         case CMD_SCREEN_PREVIOUS:
518                 screen_next_mode(c, -1);
519                 break;
520         case CMD_SCREEN_NEXT:
521                 screen_next_mode(c, 1);
522                 break;
523         case CMD_SCREEN_PLAY:
524                 screen_switch(&screen_queue, c);
525                 break;
526         case CMD_SCREEN_FILE:
527                 screen_switch(&screen_browse, c);
528                 break;
529 #ifdef ENABLE_HELP_SCREEN
530         case CMD_SCREEN_HELP:
531                 screen_switch(&screen_help, c);
532                 break;
533 #endif
534 #ifdef ENABLE_SEARCH_SCREEN
535         case CMD_SCREEN_SEARCH:
536                 screen_switch(&screen_search, c);
537                 break;
538 #endif
539 #ifdef ENABLE_ARTIST_SCREEN
540         case CMD_SCREEN_ARTIST:
541                 screen_switch(&screen_artist, c);
542                 break;
543 #endif
544 #ifdef ENABLE_SONG_SCREEN
545         case CMD_SCREEN_SONG:
546                 screen_switch(&screen_song, c);
547                 break;
548 #endif
549 #ifdef ENABLE_KEYDEF_SCREEN
550         case CMD_SCREEN_KEYDEF:
551                 screen_switch(&screen_keydef, c);
552                 break;
553 #endif
554 #ifdef ENABLE_LYRICS_SCREEN
555         case CMD_SCREEN_LYRICS:
556                 screen_switch(&screen_lyrics, c);
557                 break;
558 #endif
559 #ifdef ENABLE_OUTPUTS_SCREEN
560         case CMD_SCREEN_OUTPUTS:
561                 screen_switch(&screen_outputs, c);
562                 break;
563         case CMD_SCREEN_SWAP:
564                 screen_swap(c, NULL);
565                 break;
566 #endif
568         default:
569                 break;
570         }