Code

screen: move mode_fn into struct screen
[ncmpc.git] / src / screen.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2017 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"
42 #include "screen_chat.h"
44 #include <mpd/client.h>
46 #include <stdlib.h>
47 #include <unistd.h>
48 #include <string.h>
49 #include <time.h>
50 #include <locale.h>
52 #ifndef NCMPC_MINI
53 /** welcome message time [s] */
54 static const GTime SCREEN_WELCOME_TIME = 10;
55 #endif
57 /* minimum window size */
58 static const unsigned SCREEN_MIN_COLS = 14;
59 static const unsigned SCREEN_MIN_ROWS = 5;
61 /* screens */
63 struct screen screen = {
64         .current_page = &screen_queue,
65 };
67 static const struct screen_functions *mode_fn_prev = &screen_queue;
69 void
70 screen_switch(const struct screen_functions *sf, struct mpdclient *c)
71 {
72         assert(sf != NULL);
74         if (sf == screen.current_page)
75                 return;
77         mode_fn_prev = screen.current_page;
79         /* close the old mode */
80         if (screen.current_page->close != NULL)
81                 screen.current_page->close();
83         /* get functions for the new mode */
84         screen.current_page = sf;
86         /* open the new mode */
87         if (sf->open != NULL)
88                 sf->open(c);
90         screen_paint(c, true);
91 }
93 void
94 screen_swap(struct mpdclient *c, const struct mpd_song *song)
95 {
96         if (song != NULL)
97         {
98                 if (false)
99                         { /* just a hack to make the ifdefs less ugly */ }
100 #ifdef ENABLE_SONG_SCREEN
101                 if (mode_fn_prev == &screen_song)
102                         screen_song_switch(c, song);
103 #endif
104 #ifdef ENABLE_LYRICS_SCREEN
105                 else if (mode_fn_prev == &screen_lyrics)
106                         screen_lyrics_switch(c, song, true);
107 #endif
108                 else
109                         screen_switch(mode_fn_prev, c);
110         }
111         else
112                 screen_switch(mode_fn_prev, c);
115 static int
116 find_configured_screen(const char *name)
118         unsigned i;
120         for (i = 0; options.screen_list[i] != NULL; ++i)
121                 if (strcmp(options.screen_list[i], name) == 0)
122                         return i;
124         return -1;
127 static void
128 screen_next_mode(struct mpdclient *c, int offset)
130         int max = g_strv_length(options.screen_list);
132         /* find current screen */
133         int current = find_configured_screen(screen_get_name(screen.current_page));
134         int next = current + offset;
135         if (next<0)
136                 next = max-1;
137         else if (next>=max)
138                 next = 0;
140         const struct screen_functions *sf =
141                 screen_lookup_name(options.screen_list[next]);
142         if (sf != NULL)
143                 screen_switch(sf, c);
146 static void
147 paint_top_window(const struct mpdclient *c)
149         const char *title =
150 #ifndef NCMPC_MINI
151                 screen.welcome_source_id == 0 &&
152 #endif
153                 screen.current_page->get_title != NULL
154                 ? screen.current_page->get_title(screen.buf, screen.buf_size)
155                 : "";
156         assert(title != NULL);
158         title_bar_paint(&screen.title_bar, title, c->status);
161 static void
162 update_progress_window(struct mpdclient *c, bool repaint)
164         unsigned elapsed;
165         if (c->status == NULL)
166                 elapsed = 0;
167         else if (seek_id >= 0 && seek_id == mpd_status_get_song_id(c->status))
168                 elapsed = seek_target_time;
169         else
170                 elapsed = mpd_status_get_elapsed_time(c->status);
172         unsigned duration = mpdclient_is_playing(c)
173                 ? mpd_status_get_total_time(c->status)
174                 : 0;
176         if (progress_bar_set(&screen.progress_bar, elapsed, duration) ||
177             repaint)
178                 progress_bar_paint(&screen.progress_bar);
181 void
182 screen_exit(void)
184         if (screen.current_page->close != NULL)
185                 screen.current_page->close();
187         screen_list_exit();
189         string_list_free(screen.find_history);
190         g_free(screen.buf);
191         g_free(screen.findbuf);
193         title_bar_deinit(&screen.title_bar);
194         delwin(screen.main_window.w);
195         progress_bar_deinit(&screen.progress_bar);
196         status_bar_deinit(&screen.status_bar);
198 #ifndef NCMPC_MINI
199         if (screen.welcome_source_id != 0)
200                 g_source_remove(screen.welcome_source_id);
201 #endif
204 void
205 screen_resize(struct mpdclient *c)
207         const unsigned cols = COLS, rows = LINES;
208         if (cols < SCREEN_MIN_COLS || rows < SCREEN_MIN_ROWS) {
209                 screen_exit();
210                 fprintf(stderr, "%s\n", _("Error: Screen too small"));
211                 exit(EXIT_FAILURE);
212         }
214 #ifdef PDCURSES
215         resize_term(rows, cols);
216 #else 
217         resizeterm(rows, cols);
218 #endif
220         title_bar_resize(&screen.title_bar, cols);
222         /* main window */
223         screen.main_window.cols = cols;
224         screen.main_window.rows = rows - 4;
225         wresize(screen.main_window.w, screen.main_window.rows, cols);
227         /* progress window */
228         progress_bar_resize(&screen.progress_bar, cols, rows - 2, 0);
230         /* status window */
231         status_bar_resize(&screen.status_bar, cols, rows - 1, 0);
233         screen.buf_size = cols;
234         g_free(screen.buf);
235         screen.buf = g_malloc(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, true);
247 #ifndef NCMPC_MINI
248 static gboolean
249 welcome_timer_callback(gpointer data)
251         struct mpdclient *c = data;
253         screen.welcome_source_id = 0;
255         paint_top_window(c);
256         doupdate();
258         return false;
260 #endif
262 void
263 screen_init(struct mpdclient *c)
265         const unsigned cols = COLS, rows = LINES;
266         if (cols < SCREEN_MIN_COLS || rows < SCREEN_MIN_ROWS) {
267                 fprintf(stderr, "%s\n", _("Error: Screen too small"));
268                 exit(EXIT_FAILURE);
269         }
271         screen.buf = g_malloc(cols);
272         screen.buf_size = cols;
273         screen.findbuf = NULL;
275 #ifndef NCMPC_MINI
276         if (options.welcome_screen_list)
277                 screen.welcome_source_id =
278                         g_timeout_add_seconds(SCREEN_WELCOME_TIME,
279                                               welcome_timer_callback, c);
280 #endif
282         /* create top window */
283         title_bar_init(&screen.title_bar, cols, 0, 0);
285         /* create main window */
286         window_init(&screen.main_window, rows - 4, cols, 2, 0);
288         if (!options.hardware_cursor)
289                 leaveok(screen.main_window.w, TRUE);
291         keypad(screen.main_window.w, TRUE);
293         /* create progress window */
294         progress_bar_init(&screen.progress_bar, cols, rows - 2, 0);
296         /* create status window */
297         status_bar_init(&screen.status_bar, cols, rows - 1, 0);
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         /* initialize screens */
313         screen_list_init(screen.main_window.w,
314                          screen.main_window.cols, screen.main_window.rows);
316         if (screen.current_page->open != NULL)
317                 screen.current_page->open(c);
320 void
321 screen_paint(struct mpdclient *c, bool main_dirty)
323         /* update title/header window */
324         paint_top_window(c);
326         /* paint the bottom window */
328         update_progress_window(c, true);
329         status_bar_paint(&screen.status_bar, c->status, c->song);
331         /* paint the main window */
333         if (main_dirty) {
334                 wclear(screen.main_window.w);
335                 if (screen.current_page->paint != NULL)
336                         screen.current_page->paint();
337         }
339         /* move the cursor to the origin */
341         if (!options.hardware_cursor)
342                 wmove(screen.main_window.w, 0, 0);
344         wnoutrefresh(screen.main_window.w);
346         /* tell curses to update */
347         doupdate();
350 void
351 screen_update(struct mpdclient *c)
353 #ifndef NCMPC_MINI
354         static bool was_connected;
355         static bool initialized = false;
356         static bool repeat;
357         static bool random_enabled;
358         static bool single;
359         static bool consume;
360         static unsigned crossfade;
362         /* print a message if mpd status has changed */
363         if ((c->events & MPD_IDLE_OPTIONS) && c->status != NULL) {
364                 if (!initialized) {
365                         repeat = mpd_status_get_repeat(c->status);
366                         random_enabled = mpd_status_get_random(c->status);
367                         single = mpd_status_get_single(c->status);
368                         consume = mpd_status_get_consume(c->status);
369                         crossfade = mpd_status_get_crossfade(c->status);
370                         initialized = true;
371                 }
373                 if (repeat != mpd_status_get_repeat(c->status))
374                         screen_status_printf(mpd_status_get_repeat(c->status) ?
375                                              _("Repeat mode is on") :
376                                              _("Repeat mode is off"));
378                 if (random_enabled != mpd_status_get_random(c->status))
379                         screen_status_printf(mpd_status_get_random(c->status) ?
380                                              _("Random mode is on") :
381                                              _("Random mode is off"));
383                 if (single != mpd_status_get_single(c->status))
384                         screen_status_printf(mpd_status_get_single(c->status) ?
385                                              /* "single" mode means
386                                                 that MPD will
387                                                 automatically stop
388                                                 after playing one
389                                                 single song */
390                                              _("Single mode is on") :
391                                              _("Single mode is off"));
393                 if (consume != mpd_status_get_consume(c->status))
394                         screen_status_printf(mpd_status_get_consume(c->status) ?
395                                              /* "consume" mode means
396                                                 that MPD removes each
397                                                 song which has
398                                                 finished playing */
399                                              _("Consume mode is on") :
400                                              _("Consume mode is off"));
402                 if (crossfade != mpd_status_get_crossfade(c->status))
403                         screen_status_printf(_("Crossfade %d seconds"),
404                                              mpd_status_get_crossfade(c->status));
406                 repeat = mpd_status_get_repeat(c->status);
407                 random_enabled = mpd_status_get_random(c->status);
408                 single = mpd_status_get_single(c->status);
409                 consume = mpd_status_get_consume(c->status);
410                 crossfade = mpd_status_get_crossfade(c->status);
411         }
413         if ((c->events & MPD_IDLE_DATABASE) != 0 && was_connected &&
414             mpdclient_is_connected(c))
415                 screen_status_printf(_("Database updated"));
416         was_connected = mpdclient_is_connected(c);
417 #endif
419         /* update the main window */
420         if (screen.current_page->update != NULL)
421                 screen.current_page->update(c);
423         screen_paint(c, false);
426 #ifdef HAVE_GETMOUSE
427 int
428 screen_get_mouse_event(struct mpdclient *c, unsigned long *bstate, int *row)
430         MEVENT event;
432         /* retrieve the mouse event from curses */
433 #ifdef PDCURSES
434         nc_getmouse(&event);
435 #else
436         getmouse(&event);
437 #endif
438         /* calculate the selected row in the list window */
439         *row = event.y - screen.title_bar.window.rows;
440         /* copy button state bits */
441         *bstate = event.bstate;
442         /* if button 2 was pressed switch screen */
443         if (event.bstate & BUTTON2_CLICKED) {
444                 screen_cmd(c, CMD_SCREEN_NEXT);
445                 return 1;
446         }
448         return 0;
450 #endif
452 void
453 screen_cmd(struct mpdclient *c, command_t cmd)
455 #ifndef NCMPC_MINI
456         if (screen.welcome_source_id != 0) {
457                 g_source_remove(screen.welcome_source_id);
458                 screen.welcome_source_id = 0;
459         }
460 #endif
462         if (screen.current_page->cmd != NULL &&
463             screen.current_page->cmd(c, cmd))
464                 return;
466         if (handle_player_command(c, cmd))
467                 return;
469         switch(cmd) {
470         case CMD_TOGGLE_FIND_WRAP:
471                 options.find_wrap = !options.find_wrap;
472                 screen_status_printf(options.find_wrap ?
473                                      _("Find mode: Wrapped") :
474                                      _("Find mode: Normal"));
475                 break;
476         case CMD_TOGGLE_AUTOCENTER:
477                 options.auto_center = !options.auto_center;
478                 screen_status_printf(options.auto_center ?
479                                      _("Auto center mode: On") :
480                                      _("Auto center mode: Off"));
481                 break;
482         case CMD_SCREEN_UPDATE:
483                 screen_paint(c, true);
484                 break;
485         case CMD_SCREEN_PREVIOUS:
486                 screen_next_mode(c, -1);
487                 break;
488         case CMD_SCREEN_NEXT:
489                 screen_next_mode(c, 1);
490                 break;
491         case CMD_SCREEN_PLAY:
492                 screen_switch(&screen_queue, c);
493                 break;
494         case CMD_SCREEN_FILE:
495                 screen_switch(&screen_browse, c);
496                 break;
497 #ifdef ENABLE_HELP_SCREEN
498         case CMD_SCREEN_HELP:
499                 screen_switch(&screen_help, c);
500                 break;
501 #endif
502 #ifdef ENABLE_SEARCH_SCREEN
503         case CMD_SCREEN_SEARCH:
504                 screen_switch(&screen_search, c);
505                 break;
506 #endif
507 #ifdef ENABLE_ARTIST_SCREEN
508         case CMD_SCREEN_ARTIST:
509                 screen_switch(&screen_artist, c);
510                 break;
511 #endif
512 #ifdef ENABLE_SONG_SCREEN
513         case CMD_SCREEN_SONG:
514                 screen_switch(&screen_song, c);
515                 break;
516 #endif
517 #ifdef ENABLE_KEYDEF_SCREEN
518         case CMD_SCREEN_KEYDEF:
519                 screen_switch(&screen_keydef, c);
520                 break;
521 #endif
522 #ifdef ENABLE_LYRICS_SCREEN
523         case CMD_SCREEN_LYRICS:
524                 screen_switch(&screen_lyrics, c);
525                 break;
526 #endif
527 #ifdef ENABLE_OUTPUTS_SCREEN
528         case CMD_SCREEN_OUTPUTS:
529                 screen_switch(&screen_outputs, c);
530                 break;
531 #endif
532 #ifdef ENABLE_CHAT_SCREEN
533         case CMD_SCREEN_CHAT:
534                 screen_switch(&screen_chat, c);
535                 break;
536 #endif
537         case CMD_SCREEN_SWAP:
538                 screen_swap(c, NULL);
539                 break;
541         default:
542                 break;
543         }