Code

screen: moved code to status_bar.c
[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_list.h"
22 #include "screen_utils.h"
23 #include "config.h"
24 #include "i18n.h"
25 #include "charset.h"
26 #include "mpdclient.h"
27 #include "utils.h"
28 #include "options.h"
29 #include "colors.h"
30 #include "player_command.h"
32 #include <mpd/client.h>
34 #include <stdlib.h>
35 #include <unistd.h>
36 #include <stdarg.h>
37 #include <string.h>
38 #include <time.h>
39 #include <locale.h>
41 #ifndef NCMPC_MINI
42 /** welcome message time [s] */
43 static const GTime SCREEN_WELCOME_TIME = 10;
44 #endif
46 /* minimum window size */
47 static const int SCREEN_MIN_COLS = 14;
48 static const int SCREEN_MIN_ROWS = 5;
50 /* screens */
52 #ifndef NCMPC_MINI
53 static gboolean welcome = TRUE;
54 #endif
56 struct screen screen;
57 static const struct screen_functions *mode_fn = &screen_playlist;
58 static const struct screen_functions *mode_fn_prev = &screen_playlist;
60 gboolean
61 screen_is_visible(const struct screen_functions *sf)
62 {
63         return sf == mode_fn;
64 }
66 void
67 screen_switch(const struct screen_functions *sf, struct mpdclient *c)
68 {
69         assert(sf != NULL);
71         if (sf == mode_fn)
72                 return;
74         mode_fn_prev = mode_fn;
76         /* close the old mode */
77         if (mode_fn->close != NULL)
78                 mode_fn->close();
80         /* get functions for the new mode */
81         mode_fn = sf;
83         /* open the new mode */
84         if (mode_fn->open != NULL)
85                 mode_fn->open(c);
87         screen_paint(c);
88 }
90 void
91 screen_swap(struct mpdclient *c, const struct mpd_song *song)
92 {
93         if (song != NULL)
94         {
95                 if (false)
96                         { /* just a hack to make the ifdefs less ugly */ }
97 #ifdef ENABLE_SONG_SCREEN
98                 if (mode_fn_prev == &screen_song)
99                         screen_song_switch(c, song);
100 #endif
101 #ifdef ENABLE_LYRICS_SCREEN
102                 else if (mode_fn_prev == &screen_lyrics)
103                         screen_lyrics_switch(c, song, true);
104 #endif
105                 else
106                         screen_switch(mode_fn_prev, c);
107         }
108         else
109                 screen_switch(mode_fn_prev, c);
112 static int
113 find_configured_screen(const char *name)
115         unsigned i;
117         for (i = 0; options.screen_list[i] != NULL; ++i)
118                 if (strcmp(options.screen_list[i], name) == 0)
119                         return i;
121         return -1;
124 static void
125 screen_next_mode(struct mpdclient *c, int offset)
127         int max = g_strv_length(options.screen_list);
128         int current, next;
129         const struct screen_functions *sf;
131         /* find current screen */
132         current = find_configured_screen(screen_get_name(mode_fn));
133         next = current + offset;
134         if (next<0)
135                 next = max-1;
136         else if (next>=max)
137                 next = 0;
139         sf = screen_lookup_name(options.screen_list[next]);
140         if (sf != NULL)
141                 screen_switch(sf, c);
144 static inline int
145 volume_length(int volume)
147         if (volume == 100)
148                 return 3;
149         if (volume >= 10 && volume < 100)
150                 return 2;
151         if (volume >= 0 && volume < 10)
152                 return 1;
153         return -1;
156 static void
157 paint_top_window(const char *header, const struct mpdclient *c)
159         title_bar_paint(&screen.title_bar, header, c->status);
162 static void
163 paint_progress_window(struct mpdclient *c)
165         unsigned elapsed, duration;
167         if (c->song != NULL && seek_id == (int)mpd_song_get_id(c->song))
168                 elapsed = seek_target_time;
169         else if (c->status != NULL)
170                 elapsed = mpd_status_get_elapsed_time(c->status);
171         else
172                 elapsed = 0;
174         duration = c->status != NULL &&
175                 !IS_STOPPED(mpd_status_get_state(c->status))
176                 ? mpd_status_get_total_time(c->status)
177                 : 0;
179         if (progress_bar_set(&screen.progress_bar, elapsed, duration))
180                 progress_bar_paint(&screen.progress_bar);
183 void
184 screen_exit(void)
186         if (mode_fn->close != NULL)
187                 mode_fn->close();
189         screen_list_exit();
191         string_list_free(screen.find_history);
192         g_free(screen.buf);
193         g_free(screen.findbuf);
195         title_bar_deinit(&screen.title_bar);
196         delwin(screen.main_window.w);
197         progress_bar_deinit(&screen.progress_bar);
198         status_bar_deinit(&screen.status_bar);
201 void
202 screen_resize(struct mpdclient *c)
204         if (COLS<SCREEN_MIN_COLS || LINES<SCREEN_MIN_ROWS) {
205                 screen_exit();
206                 fprintf(stderr, "%s", _("Error: Screen too small"));
207                 exit(EXIT_FAILURE);
208         }
210         resizeterm(LINES, COLS);
212         screen.cols = COLS;
213         screen.rows = LINES;
215         title_bar_resize(&screen.title_bar, screen.cols);
217         /* main window */
218         screen.main_window.cols = screen.cols;
219         screen.main_window.rows = screen.rows-4;
220         wresize(screen.main_window.w, screen.main_window.rows, screen.cols);
221         wclear(screen.main_window.w);
223         /* progress window */
224         progress_bar_resize(&screen.progress_bar, screen.cols,
225                             screen.rows - 2, 0);
226         progress_bar_paint(&screen.progress_bar);
228         /* status window */
229         status_bar_resize(&screen.status_bar, screen.cols, screen.rows - 1, 0);
230         status_bar_paint(&screen.status_bar, c->status, c->song);
232         screen.buf_size = screen.cols;
233         g_free(screen.buf);
234         screen.buf = g_malloc(screen.cols);
236         /* resize all screens */
237         screen_list_resize(screen.main_window.cols, screen.main_window.rows);
239         /* ? - without this the cursor becomes visible with aterm & Eterm */
240         curs_set(1);
241         curs_set(0);
243         screen_paint(c);
246 void
247 screen_status_message(const char *msg)
249         status_bar_message(&screen.status_bar, msg);
252 void
253 screen_status_printf(const char *format, ...)
255         char *msg;
256         va_list ap;
258         va_start(ap,format);
259         msg = g_strdup_vprintf(format,ap);
260         va_end(ap);
261         screen_status_message(msg);
262         g_free(msg);
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;
279         screen.start_timestamp = time(NULL);
281         /* create top window */
282         title_bar_init(&screen.title_bar, screen.cols, 0, 0);
284         /* create main window */
285         window_init(&screen.main_window, screen.rows - 4, screen.cols, 2, 0);
287         //  leaveok(screen.main_window.w, TRUE); temporary disabled
288         keypad(screen.main_window.w, TRUE);
290         /* create progress window */
291         progress_bar_init(&screen.progress_bar, screen.cols,
292                           screen.rows - 2, 0);
293         progress_bar_paint(&screen.progress_bar);
295         /* create status window */
296         status_bar_init(&screen.status_bar, screen.cols, screen.rows - 1, 0);
297         status_bar_paint(&screen.status_bar, c->status, c->song);
299 #ifdef ENABLE_COLORS
300         if (options.enable_colors) {
301                 /* set background attributes */
302                 wbkgd(stdscr, COLOR_PAIR(COLOR_LIST));
303                 wbkgd(screen.main_window.w,     COLOR_PAIR(COLOR_LIST));
304                 wbkgd(screen.title_bar.window.w, COLOR_PAIR(COLOR_TITLE));
305                 wbkgd(screen.progress_bar.window.w,
306                       COLOR_PAIR(COLOR_PROGRESSBAR));
307                 wbkgd(screen.status_bar.window.w, COLOR_PAIR(COLOR_STATUS));
308                 colors_use(screen.progress_bar.window.w, COLOR_PROGRESSBAR);
309         }
310 #endif
312         doupdate();
314         /* initialize screens */
315         screen_list_init(screen.main_window.w,
316                          screen.main_window.cols, screen.main_window.rows);
318         if (mode_fn->open != NULL)
319                 mode_fn->open(c);
322 void
323 screen_paint(struct mpdclient *c)
325         const char *title = NULL;
327         if (mode_fn->get_title != NULL)
328                 title = mode_fn->get_title(screen.buf, screen.buf_size);
330         /* paint the title/header window */
331         if( title )
332                 paint_top_window(title, c);
333         else
334                 paint_top_window("", c);
336         /* paint the bottom window */
338         paint_progress_window(c);
339         status_bar_paint(&screen.status_bar, c->status, c->song);
341         /* paint the main window */
343         wclear(screen.main_window.w);
344         if (mode_fn->paint != NULL)
345                 mode_fn->paint();
347         /* move the cursor to the origin */
349         if (!options.hardware_cursor)
350                 wmove(screen.main_window.w, 0, 0);
352         wnoutrefresh(screen.main_window.w);
354         /* tell curses to update */
355         doupdate();
358 void
359 screen_update(struct mpdclient *c)
361 #ifndef NCMPC_MINI
362         static bool initialized = false;
363         static bool repeat;
364         static bool random_enabled;
365         static bool single;
366         static bool consume;
367         static unsigned crossfade;
368         static unsigned dbupdate;
370         /* print a message if mpd status has changed */
371         if (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                         dbupdate = mpd_status_get_update_id(c->status);
379                         initialized = true;
380                 }
382                 if (repeat != mpd_status_get_repeat(c->status))
383                         screen_status_printf(mpd_status_get_repeat(c->status) ?
384                                              _("Repeat mode is on") :
385                                              _("Repeat mode is off"));
387                 if (random_enabled != mpd_status_get_random(c->status))
388                         screen_status_printf(mpd_status_get_random(c->status) ?
389                                              _("Random mode is on") :
390                                              _("Random mode is off"));
392                 if (single != mpd_status_get_single(c->status))
393                         screen_status_printf(mpd_status_get_single(c->status) ?
394                                              /* "single" mode means
395                                                 that MPD will
396                                                 automatically stop
397                                                 after playing one
398                                                 single song */
399                                              _("Single mode is on") :
400                                              _("Single mode is off"));
402                 if (consume != mpd_status_get_consume(c->status))
403                         screen_status_printf(mpd_status_get_consume(c->status) ?
404                                              /* "consume" mode means
405                                                 that MPD removes each
406                                                 song which has
407                                                 finished playing */
408                                              _("Consume mode is on") :
409                                              _("Consume mode is off"));
411                 if (crossfade != mpd_status_get_crossfade(c->status))
412                         screen_status_printf(_("Crossfade %d seconds"),
413                                              mpd_status_get_crossfade(c->status));
415                 if (dbupdate != 0 &&
416                     dbupdate != mpd_status_get_update_id(c->status)) {
417                         screen_status_printf(_("Database updated"));
418                 }
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                 dbupdate = mpd_status_get_update_id(c->status);
426         }
428         /* update title/header window */
429         if (welcome && options.welcome_screen_list &&
430             time(NULL)-screen.start_timestamp <= SCREEN_WELCOME_TIME)
431                 paint_top_window("", c);
432         else
433 #endif
434         if (mode_fn->get_title != NULL) {
435                 paint_top_window(mode_fn->get_title(screen.buf,screen.buf_size), c);
436 #ifndef NCMPC_MINI
437                 welcome = FALSE;
438 #endif
439         } else
440                 paint_top_window("", c);
442         /* update progress window */
443         paint_progress_window(c);
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         welcome = FALSE;
490 #endif
492         if (mode_fn->cmd != NULL && mode_fn->cmd(c, cmd))
493                 return;
495         if (handle_player_command(c, cmd))
496                 return;
498         switch(cmd) {
499         case CMD_TOGGLE_FIND_WRAP:
500                 options.find_wrap = !options.find_wrap;
501                 screen_status_printf(options.find_wrap ?
502                                      _("Find mode: Wrapped") :
503                                      _("Find mode: Normal"));
504                 break;
505         case CMD_TOGGLE_AUTOCENTER:
506                 options.auto_center = !options.auto_center;
507                 screen_status_printf(options.auto_center ?
508                                      _("Auto center mode: On") :
509                                      _("Auto center mode: Off"));
510                 break;
511         case CMD_SCREEN_UPDATE:
512                 screen_paint(c);
513                 break;
514         case CMD_SCREEN_PREVIOUS:
515                 screen_next_mode(c, -1);
516                 break;
517         case CMD_SCREEN_NEXT:
518                 screen_next_mode(c, 1);
519                 break;
520         case CMD_SCREEN_PLAY:
521                 screen_switch(&screen_playlist, c);
522                 break;
523         case CMD_SCREEN_FILE:
524                 screen_switch(&screen_browse, c);
525                 break;
526 #ifdef ENABLE_HELP_SCREEN
527         case CMD_SCREEN_HELP:
528                 screen_switch(&screen_help, c);
529                 break;
530 #endif
531 #ifdef ENABLE_SEARCH_SCREEN
532         case CMD_SCREEN_SEARCH:
533                 screen_switch(&screen_search, c);
534                 break;
535 #endif
536 #ifdef ENABLE_ARTIST_SCREEN
537         case CMD_SCREEN_ARTIST:
538                 screen_switch(&screen_artist, c);
539                 break;
540 #endif
541 #ifdef ENABLE_SONG_SCREEN
542         case CMD_SCREEN_SONG:
543                 screen_switch(&screen_song, c);
544                 break;
545 #endif
546 #ifdef ENABLE_KEYDEF_SCREEN
547         case CMD_SCREEN_KEYDEF:
548                 screen_switch(&screen_keydef, c);
549                 break;
550 #endif
551 #ifdef ENABLE_LYRICS_SCREEN
552         case CMD_SCREEN_LYRICS:
553                 screen_switch(&screen_lyrics, c);
554                 break;
555 #endif
556 #ifdef ENABLE_OUTPUTS_SCREEN
557         case CMD_SCREEN_OUTPUTS:
558                 screen_switch(&screen_outputs, c);
559                 break;
560         case CMD_SCREEN_SWAP:
561                 screen_swap(c, NULL);
562                 break;
563 #endif
565         default:
566                 break;
567         }