Code

screen: move screen_paint() to screen_paint.c
[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 void
147 screen_exit(void)
149         if (screen.current_page->close != NULL)
150                 screen.current_page->close();
152         screen_list_exit();
154         string_list_free(screen.find_history);
155         g_free(screen.buf);
156         g_free(screen.findbuf);
158         title_bar_deinit(&screen.title_bar);
159         delwin(screen.main_window.w);
160         progress_bar_deinit(&screen.progress_bar);
161         status_bar_deinit(&screen.status_bar);
163 #ifndef NCMPC_MINI
164         if (screen.welcome_source_id != 0)
165                 g_source_remove(screen.welcome_source_id);
166 #endif
169 void
170 screen_resize(struct mpdclient *c)
172         const unsigned cols = COLS, rows = LINES;
173         if (cols < SCREEN_MIN_COLS || rows < SCREEN_MIN_ROWS) {
174                 screen_exit();
175                 fprintf(stderr, "%s\n", _("Error: Screen too small"));
176                 exit(EXIT_FAILURE);
177         }
179 #ifdef PDCURSES
180         resize_term(rows, cols);
181 #else 
182         resizeterm(rows, cols);
183 #endif
185         title_bar_resize(&screen.title_bar, cols);
187         /* main window */
188         screen.main_window.cols = cols;
189         screen.main_window.rows = rows - 4;
190         wresize(screen.main_window.w, screen.main_window.rows, cols);
192         /* progress window */
193         progress_bar_resize(&screen.progress_bar, cols, rows - 2, 0);
195         /* status window */
196         status_bar_resize(&screen.status_bar, cols, rows - 1, 0);
198         screen.buf_size = cols;
199         g_free(screen.buf);
200         screen.buf = g_malloc(cols);
202         /* resize all screens */
203         screen_list_resize(screen.main_window.cols, screen.main_window.rows);
205         /* ? - without this the cursor becomes visible with aterm & Eterm */
206         curs_set(1);
207         curs_set(0);
209         screen_paint(c, true);
212 #ifndef NCMPC_MINI
213 static gboolean
214 welcome_timer_callback(gpointer data)
216         struct mpdclient *c = data;
218         screen.welcome_source_id = 0;
220         paint_top_window(c);
221         doupdate();
223         return false;
225 #endif
227 void
228 screen_init(struct mpdclient *c)
230         const unsigned cols = COLS, rows = LINES;
231         if (cols < SCREEN_MIN_COLS || rows < SCREEN_MIN_ROWS) {
232                 fprintf(stderr, "%s\n", _("Error: Screen too small"));
233                 exit(EXIT_FAILURE);
234         }
236         screen.buf = g_malloc(cols);
237         screen.buf_size = cols;
238         screen.findbuf = NULL;
240 #ifndef NCMPC_MINI
241         if (options.welcome_screen_list)
242                 screen.welcome_source_id =
243                         g_timeout_add_seconds(SCREEN_WELCOME_TIME,
244                                               welcome_timer_callback, c);
245 #endif
247         /* create top window */
248         title_bar_init(&screen.title_bar, cols, 0, 0);
250         /* create main window */
251         window_init(&screen.main_window, rows - 4, cols, 2, 0);
253         if (!options.hardware_cursor)
254                 leaveok(screen.main_window.w, TRUE);
256         keypad(screen.main_window.w, TRUE);
258         /* create progress window */
259         progress_bar_init(&screen.progress_bar, cols, rows - 2, 0);
261         /* create status window */
262         status_bar_init(&screen.status_bar, cols, rows - 1, 0);
264 #ifdef ENABLE_COLORS
265         if (options.enable_colors) {
266                 /* set background attributes */
267                 wbkgd(stdscr, COLOR_PAIR(COLOR_LIST));
268                 wbkgd(screen.main_window.w,     COLOR_PAIR(COLOR_LIST));
269                 wbkgd(screen.title_bar.window.w, COLOR_PAIR(COLOR_TITLE));
270                 wbkgd(screen.progress_bar.window.w,
271                       COLOR_PAIR(COLOR_PROGRESSBAR));
272                 wbkgd(screen.status_bar.window.w, COLOR_PAIR(COLOR_STATUS));
273                 colors_use(screen.progress_bar.window.w, COLOR_PROGRESSBAR);
274         }
275 #endif
277         /* initialize screens */
278         screen_list_init(screen.main_window.w,
279                          screen.main_window.cols, screen.main_window.rows);
281         if (screen.current_page->open != NULL)
282                 screen.current_page->open(c);
285 void
286 screen_update(struct mpdclient *c)
288 #ifndef NCMPC_MINI
289         static bool was_connected;
290         static bool initialized = false;
291         static bool repeat;
292         static bool random_enabled;
293         static bool single;
294         static bool consume;
295         static unsigned crossfade;
297         /* print a message if mpd status has changed */
298         if ((c->events & MPD_IDLE_OPTIONS) && c->status != NULL) {
299                 if (!initialized) {
300                         repeat = mpd_status_get_repeat(c->status);
301                         random_enabled = mpd_status_get_random(c->status);
302                         single = mpd_status_get_single(c->status);
303                         consume = mpd_status_get_consume(c->status);
304                         crossfade = mpd_status_get_crossfade(c->status);
305                         initialized = true;
306                 }
308                 if (repeat != mpd_status_get_repeat(c->status))
309                         screen_status_printf(mpd_status_get_repeat(c->status) ?
310                                              _("Repeat mode is on") :
311                                              _("Repeat mode is off"));
313                 if (random_enabled != mpd_status_get_random(c->status))
314                         screen_status_printf(mpd_status_get_random(c->status) ?
315                                              _("Random mode is on") :
316                                              _("Random mode is off"));
318                 if (single != mpd_status_get_single(c->status))
319                         screen_status_printf(mpd_status_get_single(c->status) ?
320                                              /* "single" mode means
321                                                 that MPD will
322                                                 automatically stop
323                                                 after playing one
324                                                 single song */
325                                              _("Single mode is on") :
326                                              _("Single mode is off"));
328                 if (consume != mpd_status_get_consume(c->status))
329                         screen_status_printf(mpd_status_get_consume(c->status) ?
330                                              /* "consume" mode means
331                                                 that MPD removes each
332                                                 song which has
333                                                 finished playing */
334                                              _("Consume mode is on") :
335                                              _("Consume mode is off"));
337                 if (crossfade != mpd_status_get_crossfade(c->status))
338                         screen_status_printf(_("Crossfade %d seconds"),
339                                              mpd_status_get_crossfade(c->status));
341                 repeat = mpd_status_get_repeat(c->status);
342                 random_enabled = mpd_status_get_random(c->status);
343                 single = mpd_status_get_single(c->status);
344                 consume = mpd_status_get_consume(c->status);
345                 crossfade = mpd_status_get_crossfade(c->status);
346         }
348         if ((c->events & MPD_IDLE_DATABASE) != 0 && was_connected &&
349             mpdclient_is_connected(c))
350                 screen_status_printf(_("Database updated"));
351         was_connected = mpdclient_is_connected(c);
352 #endif
354         /* update the main window */
355         if (screen.current_page->update != NULL)
356                 screen.current_page->update(c);
358         screen_paint(c, false);
361 #ifdef HAVE_GETMOUSE
362 int
363 screen_get_mouse_event(struct mpdclient *c, unsigned long *bstate, int *row)
365         MEVENT event;
367         /* retrieve the mouse event from curses */
368 #ifdef PDCURSES
369         nc_getmouse(&event);
370 #else
371         getmouse(&event);
372 #endif
373         /* calculate the selected row in the list window */
374         *row = event.y - screen.title_bar.window.rows;
375         /* copy button state bits */
376         *bstate = event.bstate;
377         /* if button 2 was pressed switch screen */
378         if (event.bstate & BUTTON2_CLICKED) {
379                 screen_cmd(c, CMD_SCREEN_NEXT);
380                 return 1;
381         }
383         return 0;
385 #endif
387 void
388 screen_cmd(struct mpdclient *c, command_t cmd)
390 #ifndef NCMPC_MINI
391         if (screen.welcome_source_id != 0) {
392                 g_source_remove(screen.welcome_source_id);
393                 screen.welcome_source_id = 0;
394         }
395 #endif
397         if (screen.current_page->cmd != NULL &&
398             screen.current_page->cmd(c, cmd))
399                 return;
401         if (handle_player_command(c, cmd))
402                 return;
404         switch(cmd) {
405         case CMD_TOGGLE_FIND_WRAP:
406                 options.find_wrap = !options.find_wrap;
407                 screen_status_printf(options.find_wrap ?
408                                      _("Find mode: Wrapped") :
409                                      _("Find mode: Normal"));
410                 break;
411         case CMD_TOGGLE_AUTOCENTER:
412                 options.auto_center = !options.auto_center;
413                 screen_status_printf(options.auto_center ?
414                                      _("Auto center mode: On") :
415                                      _("Auto center mode: Off"));
416                 break;
417         case CMD_SCREEN_UPDATE:
418                 screen_paint(c, true);
419                 break;
420         case CMD_SCREEN_PREVIOUS:
421                 screen_next_mode(c, -1);
422                 break;
423         case CMD_SCREEN_NEXT:
424                 screen_next_mode(c, 1);
425                 break;
426         case CMD_SCREEN_PLAY:
427                 screen_switch(&screen_queue, c);
428                 break;
429         case CMD_SCREEN_FILE:
430                 screen_switch(&screen_browse, c);
431                 break;
432 #ifdef ENABLE_HELP_SCREEN
433         case CMD_SCREEN_HELP:
434                 screen_switch(&screen_help, c);
435                 break;
436 #endif
437 #ifdef ENABLE_SEARCH_SCREEN
438         case CMD_SCREEN_SEARCH:
439                 screen_switch(&screen_search, c);
440                 break;
441 #endif
442 #ifdef ENABLE_ARTIST_SCREEN
443         case CMD_SCREEN_ARTIST:
444                 screen_switch(&screen_artist, c);
445                 break;
446 #endif
447 #ifdef ENABLE_SONG_SCREEN
448         case CMD_SCREEN_SONG:
449                 screen_switch(&screen_song, c);
450                 break;
451 #endif
452 #ifdef ENABLE_KEYDEF_SCREEN
453         case CMD_SCREEN_KEYDEF:
454                 screen_switch(&screen_keydef, c);
455                 break;
456 #endif
457 #ifdef ENABLE_LYRICS_SCREEN
458         case CMD_SCREEN_LYRICS:
459                 screen_switch(&screen_lyrics, c);
460                 break;
461 #endif
462 #ifdef ENABLE_OUTPUTS_SCREEN
463         case CMD_SCREEN_OUTPUTS:
464                 screen_switch(&screen_outputs, c);
465                 break;
466 #endif
467 #ifdef ENABLE_CHAT_SCREEN
468         case CMD_SCREEN_CHAT:
469                 screen_switch(&screen_chat, c);
470                 break;
471 #endif
472         case CMD_SCREEN_SWAP:
473                 screen_swap(c, NULL);
474                 break;
476         default:
477                 break;
478         }