Code

screen: free ncurses windows with delwin()
[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 "strfsong.h"
32 #ifndef NCMPC_MINI
33 #include "hscroll.h"
34 #endif
36 #include <stdlib.h>
37 #include <unistd.h>
38 #include <stdarg.h>
39 #include <string.h>
40 #include <time.h>
41 #include <locale.h>
43 #ifndef NCMPC_MINI
44 /** welcome message time [s] */
45 static const GTime SCREEN_WELCOME_TIME = 10;
46 #endif
48 /* minimum window size */
49 static const int SCREEN_MIN_COLS = 14;
50 static const int SCREEN_MIN_ROWS = 5;
52 /* screens */
54 #ifndef NCMPC_MINI
55 static gboolean welcome = TRUE;
56 #endif
58 struct screen screen;
59 static const struct screen_functions *mode_fn = &screen_playlist;
60 static const struct screen_functions *mode_fn_prev = &screen_playlist;
61 static int seek_id = -1;
62 static int seek_target_time = 0;
64 gboolean
65 screen_is_visible(const struct screen_functions *sf)
66 {
67         return sf == mode_fn;
68 }
70 void
71 screen_switch(const struct screen_functions *sf, struct mpdclient *c)
72 {
73         assert(sf != NULL);
75         if (sf == mode_fn)
76                 return;
78         mode_fn_prev = mode_fn;
80         /* close the old mode */
81         if (mode_fn->close != NULL)
82                 mode_fn->close();
84         /* get functions for the new mode */
85         mode_fn = sf;
87         /* open the new mode */
88         if (mode_fn->open != NULL)
89                 mode_fn->open(c);
91         screen_paint(c);
92 }
94 void
95 screen_swap(struct mpdclient *c, const struct mpd_song *song)
96 {
97         if (song != NULL)
98         {
99                 if (false)
100                         { /* just a hack to make the ifdefs less ugly */ }
101 #ifdef ENABLE_SONG_SCREEN
102                 if (mode_fn_prev == &screen_song)
103                         screen_song_switch(c, song);
104 #endif
105 #ifdef ENABLE_LYRICS_SCREEN
106                 else if (mode_fn_prev == &screen_lyrics)
107                         screen_lyrics_switch(c, song);
108 #endif
109                 else
110                         screen_switch(mode_fn_prev, c);
111         }
112         else
113                 screen_switch(mode_fn_prev, c);
116 static int
117 find_configured_screen(const char *name)
119         unsigned i;
121         for (i = 0; options.screen_list[i] != NULL; ++i)
122                 if (strcmp(options.screen_list[i], name) == 0)
123                         return i;
125         return -1;
128 static void
129 screen_next_mode(mpdclient_t *c, int offset)
131         int max = g_strv_length(options.screen_list);
132         int current, next;
133         const struct screen_functions *sf;
135         /* find current screen */
136         current = find_configured_screen(screen_get_name(mode_fn));
137         next = current + offset;
138         if (next<0)
139                 next = max-1;
140         else if (next>=max)
141                 next = 0;
143         sf = screen_lookup_name(options.screen_list[next]);
144         if (sf != NULL)
145                 screen_switch(sf, c);
148 #ifndef NCMPC_MINI
149 static void
150 print_hotkey(WINDOW *w, command_t cmd, const char *label)
152         colors_use(w, COLOR_TITLE_BOLD);
153         waddstr(w, get_key_names(cmd, FALSE));
154         colors_use(w, COLOR_TITLE);
155         waddch(w, ':');
156         waddstr(w, label);
157         waddch(w, ' ');
158         waddch(w, ' ');
160 #endif
162 static inline int
163 get_volume(const struct mpd_Status *status)
165         return status != NULL
166                 ? status->volume
167                 : MPD_STATUS_NO_VOLUME;
170 static void
171 paint_top_window2(const char *header, mpdclient_t *c)
173         int volume;
174         char flags[5];
175         WINDOW *w = screen.top_window.w;
176         char buf[32];
178         if (header[0]) {
179                 colors_use(w, COLOR_TITLE_BOLD);
180                 mvwaddstr(w, 0, 0, header);
181 #ifndef NCMPC_MINI
182         } else {
183 #ifdef ENABLE_HELP_SCREEN
184                 print_hotkey(w, CMD_SCREEN_HELP, _("Help"));
185 #endif
186                 print_hotkey(w, CMD_SCREEN_PLAY, _("Playlist"));
187                 print_hotkey(w, CMD_SCREEN_FILE, _("Browse"));
188 #ifdef ENABLE_ARTIST_SCREEN
189                 print_hotkey(w, CMD_SCREEN_ARTIST, _("Artist"));
190 #endif
191 #ifdef ENABLE_SEARCH_SCREEN
192                 print_hotkey(w, CMD_SCREEN_SEARCH, _("Search"));
193 #endif
194 #ifdef ENABLE_LYRICS_SCREEN
195                 print_hotkey(w, CMD_SCREEN_LYRICS, _("Lyrics"));
196 #endif
197 #ifdef ENABLE_OUTPUTS_SCREEN
198                 print_hotkey(w, CMD_SCREEN_OUTPUTS, _("Outputs"));
199 #endif
200 #endif
201         }
203         volume = get_volume(c->status);
204         if (volume == MPD_STATUS_NO_VOLUME)
205                 g_snprintf(buf, 32, _("Volume n/a"));
206         else
207                 g_snprintf(buf, 32, _("Volume %d%%"), volume);
209         colors_use(w, COLOR_TITLE);
210         mvwaddstr(w, 0, screen.top_window.cols - utf8_width(buf), buf);
212         flags[0] = 0;
213         if (c->status != NULL) {
214                 if (c->status->repeat)
215                         g_strlcat(flags, "r", sizeof(flags));
216                 if (c->status->random)
217                         g_strlcat(flags, "z", sizeof(flags));
218                 if (c->status->single)
219                         g_strlcat(flags, "s", sizeof(flags));
220                 if (c->status->consume)
221                         g_strlcat(flags, "c", sizeof(flags));
222                 if (c->status->crossfade)
223                         g_strlcat(flags, "x", sizeof(flags));
224                 if (c->status->updatingDb)
225                         g_strlcat(flags, "U", sizeof(flags));
226         }
228         colors_use(w, COLOR_LINE);
229         mvwhline(w, 1, 0, ACS_HLINE, screen.top_window.cols);
230         if (flags[0]) {
231                 wmove(w,1,screen.top_window.cols-strlen(flags)-3);
232                 waddch(w, '[');
233                 colors_use(w, COLOR_LINE_BOLD);
234                 waddstr(w, flags);
235                 colors_use(w, COLOR_LINE);
236                 waddch(w, ']');
237         }
238         wnoutrefresh(w);
241 static void
242 paint_top_window(const char *header, mpdclient_t *c, int full_repaint)
244         static int prev_volume = -1;
245         static unsigned prev_header_len = -1;
246         WINDOW *w = screen.top_window.w;
248         if (prev_header_len != utf8_width(header)) {
249                 prev_header_len = utf8_width(header);
250                 full_repaint = 1;
251         }
253         if (full_repaint) {
254                 wmove(w, 0, 0);
255                 wclrtoeol(w);
256         }
258         if ((c->status != NULL && prev_volume != c->status->volume) ||
259             full_repaint)
260                 paint_top_window2(header, c);
263 static void
264 paint_progress_window(mpdclient_t *c)
266         double p;
267         int width;
268         int elapsedTime;
270         if (c->status==NULL || IS_STOPPED(c->status->state)) {
271                 mvwhline(screen.progress_window.w, 0, 0, ACS_HLINE,
272                          screen.progress_window.cols);
273                 wnoutrefresh(screen.progress_window.w);
274                 return;
275         }
277         if (c->song && seek_id == c->song->id)
278                 elapsedTime = seek_target_time;
279         else
280                 elapsedTime = c->status->elapsedTime;
282         p = ((double) elapsedTime) / ((double) c->status->totalTime);
284         width = (int) (p * (double) screen.progress_window.cols);
285         mvwhline(screen.progress_window.w,
286                  0, 0,
287                  ACS_HLINE,
288                  screen.progress_window.cols);
289         whline(screen.progress_window.w, '=', width-1);
290         mvwaddch(screen.progress_window.w, 0, width-1, 'O');
291         wnoutrefresh(screen.progress_window.w);
294 static void
295 paint_status_window(mpdclient_t *c)
297         WINDOW *w = screen.status_window.w;
298         mpd_Status *status = c->status;
299         int state;
300         mpd_Song *song = c->song;
301         int elapsedTime = 0;
302 #ifdef NCMPC_MINI
303         static char bitrate[1];
304 #else
305         char bitrate[16];
306 #endif
307         const char *str = NULL;
308         int x = 0;
310         if( time(NULL) - screen.status_timestamp <= options.status_message_time )
311                 return;
313         wmove(w, 0, 0);
314         wclrtoeol(w);
315         colors_use(w, COLOR_STATUS_BOLD);
317         state = status == NULL ? MPD_STATUS_STATE_UNKNOWN : status->state;
319         switch (state) {
320         case MPD_STATUS_STATE_PLAY:
321                 str = _("Playing:");
322                 break;
323         case MPD_STATUS_STATE_PAUSE:
324                 str = _("[Paused]");
325                 break;
326         case MPD_STATUS_STATE_STOP:
327         default:
328                 break;
329         }
331         if (str) {
332                 waddstr(w, str);
333                 x += utf8_width(str) + 1;
334         }
336         /* create time string */
337         memset(screen.buf, 0, screen.buf_size);
338         if (IS_PLAYING(state) || IS_PAUSED(state)) {
339                 if (status->totalTime > 0) {
340                         /*checks the conf to see whether to display elapsed or remaining time */
341                         if(!strcmp(options.timedisplay_type,"elapsed"))
342                                 elapsedTime = c->status->elapsedTime;
343                         else if(!strcmp(options.timedisplay_type,"remaining"))
344                                 elapsedTime = (c->status->totalTime - c->status->elapsedTime);
346                         if( c->song && seek_id == c->song->id )
347                                 elapsedTime = seek_target_time;
349                         /* display bitrate if visible-bitrate is true */
350 #ifndef NCMPC_MINI
351                         if (options.visible_bitrate) {
352                                 g_snprintf(bitrate, 16,
353                                            " [%d kbps]", status->bitRate);
354                         } else {
355                                 bitrate[0] = '\0';
356                         }
357 #endif
359                         /*write out the time, using hours if time over 60 minutes*/
360                         if (c->status->totalTime > 3600) {
361                                 g_snprintf(screen.buf, screen.buf_size,
362                                            "%s [%i:%02i:%02i/%i:%02i:%02i]",
363                                            bitrate, elapsedTime/3600, (elapsedTime%3600)/60, elapsedTime%60,
364                                            status->totalTime/3600, (status->totalTime%3600)/60,  status->totalTime%60);
365                         } else {
366                                 g_snprintf(screen.buf, screen.buf_size,
367                                            "%s [%i:%02i/%i:%02i]",
368                                            bitrate, elapsedTime/60, elapsedTime%60,
369                                            status->totalTime/60,   status->totalTime%60 );
370                         }
371 #ifndef NCMPC_MINI
372                 } else {
373                         g_snprintf(screen.buf, screen.buf_size,
374                                    " [%d kbps]", status->bitRate );
375 #endif
376                 }
377 #ifndef NCMPC_MINI
378         } else {
379                 if (options.display_time) {
380                         time_t timep;
382                         time(&timep);
383                         strftime(screen.buf, screen.buf_size, "%X ",localtime(&timep));
384                 }
385 #endif
386         }
388         /* display song */
389         if (status != NULL && (IS_PLAYING(status->state) ||
390                                IS_PAUSED(status->state))) {
391                 char songname[MAX_SONGNAME_LENGTH];
392 #ifndef NCMPC_MINI
393                 int width = COLS - x - utf8_width(screen.buf);
394 #endif
396                 if (song)
397                         strfsong(songname, MAX_SONGNAME_LENGTH,
398                                  options.status_format, song);
399                 else
400                         songname[0] = '\0';
402                 colors_use(w, COLOR_STATUS);
403                 /* scroll if the song name is to long */
404 #ifndef NCMPC_MINI
405                 if (options.scroll && utf8_width(songname) > (unsigned)width) {
406                         static  scroll_state_t st = { 0, 0 };
407                         char *tmp = strscroll(songname, options.scroll_sep, width, &st);
409                         g_strlcpy(songname, tmp, MAX_SONGNAME_LENGTH);
410                         g_free(tmp);
411                 }
412 #endif
413                 //mvwaddnstr(w, 0, x, songname, width);
414                 mvwaddstr(w, 0, x, songname);
415         }
417         /* display time string */
418         if (screen.buf[0]) {
419                 x = screen.status_window.cols - strlen(screen.buf);
420                 colors_use(w, COLOR_STATUS_TIME);
421                 mvwaddstr(w, 0, x, screen.buf);
422         }
424         wnoutrefresh(w);
427 void
428 screen_exit(void)
430         if (mode_fn->close != NULL)
431                 mode_fn->close();
433         screen_list_exit();
435         string_list_free(screen.find_history);
436         g_free(screen.buf);
437         g_free(screen.findbuf);
439         delwin(screen.top_window.w);
440         delwin(screen.main_window.w);
441         delwin(screen.progress_window.w);
442         delwin(screen.status_window.w);
445 void
446 screen_resize(struct mpdclient *c)
448         if (COLS<SCREEN_MIN_COLS || LINES<SCREEN_MIN_ROWS) {
449                 screen_exit();
450                 fprintf(stderr, "%s", _("Error: Screen too small"));
451                 exit(EXIT_FAILURE);
452         }
454         resizeterm(LINES, COLS);
456         screen.cols = COLS;
457         screen.rows = LINES;
459         /* top window */
460         screen.top_window.cols = screen.cols;
461         wresize(screen.top_window.w, 2, screen.cols);
463         /* main window */
464         screen.main_window.cols = screen.cols;
465         screen.main_window.rows = screen.rows-4;
466         wresize(screen.main_window.w, screen.main_window.rows, screen.cols);
467         wclear(screen.main_window.w);
469         /* progress window */
470         screen.progress_window.cols = screen.cols;
471         wresize(screen.progress_window.w, 1, screen.cols);
472         mvwin(screen.progress_window.w, screen.rows-2, 0);
474         /* status window */
475         screen.status_window.cols = screen.cols;
476         wresize(screen.status_window.w, 1, screen.cols);
477         mvwin(screen.status_window.w, screen.rows-1, 0);
479         screen.buf_size = screen.cols;
480         g_free(screen.buf);
481         screen.buf = g_malloc(screen.cols);
483         /* resize all screens */
484         screen_list_resize(screen.main_window.cols, screen.main_window.rows);
486         /* ? - without this the cursor becomes visible with aterm & Eterm */
487         curs_set(1);
488         curs_set(0);
490         screen_paint(c);
493 void
494 screen_status_message(const char *msg)
496         WINDOW *w = screen.status_window.w;
498         wmove(w, 0, 0);
499         wclrtoeol(w);
500         colors_use(w, COLOR_STATUS_ALERT);
501         waddstr(w, msg);
502         wnoutrefresh(w);
503         screen.status_timestamp = time(NULL);
506 void
507 screen_status_printf(const char *format, ...)
509         char *msg;
510         va_list ap;
512         va_start(ap,format);
513         msg = g_strdup_vprintf(format,ap);
514         va_end(ap);
515         screen_status_message(msg);
516         g_free(msg);
519 void
520 screen_init(mpdclient_t *c)
522         if (COLS < SCREEN_MIN_COLS || LINES < SCREEN_MIN_ROWS) {
523                 fprintf(stderr, "%s\n", _("Error: Screen too small"));
524                 exit(EXIT_FAILURE);
525         }
527         screen.cols = COLS;
528         screen.rows = LINES;
530         screen.buf  = g_malloc(screen.cols);
531         screen.buf_size = screen.cols;
532         screen.findbuf = NULL;
533         screen.start_timestamp = time(NULL);
534         screen.last_cmd = CMD_NONE;
536         /* create top window */
537         screen.top_window.rows = 2;
538         screen.top_window.cols = screen.cols;
539         screen.top_window.w = newwin(screen.top_window.rows,
540                                       screen.top_window.cols,
541                                       0, 0);
542         leaveok(screen.top_window.w, TRUE);
543         keypad(screen.top_window.w, TRUE);
545         /* create main window */
546         screen.main_window.rows = screen.rows-4;
547         screen.main_window.cols = screen.cols;
548         screen.main_window.w = newwin(screen.main_window.rows,
549                                        screen.main_window.cols,
550                                        2,
551                                        0);
553         //  leaveok(screen.main_window.w, TRUE); temporary disabled
554         keypad(screen.main_window.w, TRUE);
556         /* create progress window */
557         screen.progress_window.rows = 1;
558         screen.progress_window.cols = screen.cols;
559         screen.progress_window.w = newwin(screen.progress_window.rows,
560                                            screen.progress_window.cols,
561                                            screen.rows-2,
562                                            0);
563         leaveok(screen.progress_window.w, TRUE);
565         /* create status window */
566         screen.status_window.rows = 1;
567         screen.status_window.cols = screen.cols;
568         screen.status_window.w = newwin(screen.status_window.rows,
569                                          screen.status_window.cols,
570                                          screen.rows-1,
571                                          0);
573         leaveok(screen.status_window.w, FALSE);
574         keypad(screen.status_window.w, TRUE);
576 #ifdef ENABLE_COLORS
577         if (options.enable_colors) {
578                 /* set background attributes */
579                 wbkgd(stdscr, COLOR_PAIR(COLOR_LIST));
580                 wbkgd(screen.main_window.w,     COLOR_PAIR(COLOR_LIST));
581                 wbkgd(screen.top_window.w,      COLOR_PAIR(COLOR_TITLE));
582                 wbkgd(screen.progress_window.w, COLOR_PAIR(COLOR_PROGRESSBAR));
583                 wbkgd(screen.status_window.w,   COLOR_PAIR(COLOR_STATUS));
584                 colors_use(screen.progress_window.w, COLOR_PROGRESSBAR);
585         }
586 #endif
588         refresh();
590         /* initialize screens */
591         screen_list_init(screen.main_window.w,
592                          screen.main_window.cols, screen.main_window.rows);
594         if (mode_fn->open != NULL)
595                 mode_fn->open(c);
598 void
599 screen_paint(mpdclient_t *c)
601         const char *title = NULL;
603         if (mode_fn->get_title != NULL)
604                 title = mode_fn->get_title(screen.buf, screen.buf_size);
606         /* paint the title/header window */
607         if( title )
608                 paint_top_window(title, c, 1);
609         else
610                 paint_top_window("", c, 1);
612         /* paint the bottom window */
614         paint_progress_window(c);
615         paint_status_window(c);
617         /* paint the main window */
619         wclear(screen.main_window.w);
620         if (mode_fn->paint != NULL)
621                 mode_fn->paint();
623         /* move the cursor to the origin */
625         if (!options.hardware_cursor)
626                 wmove(screen.main_window.w, 0, 0);
628         wnoutrefresh(screen.main_window.w);
630         /* tell curses to update */
631         doupdate();
634 void
635 screen_update(mpdclient_t *c)
637 #ifndef NCMPC_MINI
638         static int repeat = -1;
639         static int random_enabled = -1;
640         static int single = -1;
641         static int consume = -1;
642         static int crossfade = -1;
643         static int dbupdate = -1;
645         /* print a message if mpd status has changed */
646         if (c->status != NULL) {
647                 if (repeat < 0) {
648                         repeat = c->status->repeat;
649                         random_enabled = c->status->random;
650                         single = c->status->single;
651                         consume = c->status->consume;
652                         crossfade = c->status->crossfade;
653                         dbupdate = c->status->updatingDb;
654                 }
656                 if (repeat != c->status->repeat)
657                         screen_status_printf(c->status->repeat ?
658                                              _("Repeat mode is on") :
659                                              _("Repeat mode is off"));
661                 if (random_enabled != c->status->random)
662                         screen_status_printf(c->status->random ?
663                                              _("Random mode is on") :
664                                              _("Random mode is off"));
666                 if (single != c->status->single)
667                         screen_status_printf(c->status->single ?
668                                              /* "single" mode means
669                                                 that MPD will
670                                                 automatically stop
671                                                 after playing one
672                                                 single song */
673                                              _("Single mode is on") :
674                                              _("Single mode is off"));
676                 if (consume != c->status->consume)
677                         screen_status_printf(c->status->consume ?
678                                              /* "consume" mode means
679                                                 that MPD removes each
680                                                 song which has
681                                                 finished playing */
682                                              _("Consume mode is on") :
683                                              _("Consume mode is off"));
685                 if (crossfade != c->status->crossfade)
686                         screen_status_printf(_("Crossfade %d seconds"), c->status->crossfade);
688                 if (dbupdate && dbupdate != c->status->updatingDb) {
689                         screen_status_printf(_("Database updated"));
690                 }
692                 repeat = c->status->repeat;
693                 single = c->status->single;
694                 consume = c->status->consume;
695                 random_enabled = c->status->random;
696                 crossfade = c->status->crossfade;
697                 dbupdate = c->status->updatingDb;
698         }
700         /* update title/header window */
701         if (welcome && options.welcome_screen_list &&
702             screen.last_cmd==CMD_NONE &&
703             time(NULL)-screen.start_timestamp <= SCREEN_WELCOME_TIME)
704                 paint_top_window("", c, 0);
705         else
706 #endif
707         if (mode_fn->get_title != NULL) {
708                 paint_top_window(mode_fn->get_title(screen.buf,screen.buf_size), c, 0);
709 #ifndef NCMPC_MINI
710                 welcome = FALSE;
711 #endif
712         } else
713                 paint_top_window("", c, 0);
715         /* update progress window */
716         paint_progress_window(c);
718         /* update status window */
719         paint_status_window(c);
721         /* update the main window */
722         if (mode_fn->update != NULL)
723                 mode_fn->update(c);
725         /* move the cursor to the origin */
727         if (!options.hardware_cursor)
728                 wmove(screen.main_window.w, 0, 0);
730         wnoutrefresh(screen.main_window.w);
732         /* tell curses to update */
733         doupdate();
736 void
737 screen_idle(mpdclient_t *c)
739         if (c->song && seek_id == c->song->id &&
740             (screen.last_cmd == CMD_SEEK_FORWARD ||
741              screen.last_cmd == CMD_SEEK_BACKWARD))
742                 mpdclient_cmd_seek(c, seek_id, seek_target_time);
744         screen.last_cmd = CMD_NONE;
745         seek_id = -1;
748 #ifdef HAVE_GETMOUSE
749 int
750 screen_get_mouse_event(mpdclient_t *c, unsigned long *bstate, int *row)
752         MEVENT event;
754         /* retrieve the mouse event from ncurses */
755         getmouse(&event);
756         /* calculate the selected row in the list window */
757         *row = event.y - screen.top_window.rows;
758         /* copy button state bits */
759         *bstate = event.bstate;
760         /* if button 2 was pressed switch screen */
761         if (event.bstate & BUTTON2_CLICKED) {
762                 screen_cmd(c, CMD_SCREEN_NEXT);
763                 return 1;
764         }
766         return 0;
768 #endif
770 static int
771 screen_client_cmd(mpdclient_t *c, command_t cmd)
773         if (c->connection == NULL || c->status == NULL)
774                 return 0;
776         switch(cmd) {
777                 /*
778         case CMD_PLAY:
779                 mpdclient_cmd_play(c, MPD_PLAY_AT_BEGINNING);
780                 break;
781                 */
782         case CMD_PAUSE:
783                 mpdclient_cmd_pause(c, !IS_PAUSED(c->status->state));
784                 break;
785         case CMD_STOP:
786                 mpdclient_cmd_stop(c);
787                 break;
788         case CMD_CROP:
789                 mpdclient_cmd_crop(c);
790                 break;
791         case CMD_SEEK_FORWARD:
792                 if (!IS_STOPPED(c->status->state)) {
793                         if (c->song && seek_id != c->song->id) {
794                                 seek_id = c->song->id;
795                                 seek_target_time = c->status->elapsedTime;
796                         }
797                         seek_target_time+=options.seek_time;
798                         if (seek_target_time < c->status->totalTime)
799                                 break;
800                         seek_target_time = c->status->totalTime;
801                         /* seek_target_time=0; */
802                 }
803                 break;
804                 /* fall through... */
805         case CMD_TRACK_NEXT:
806                 if (!IS_STOPPED(c->status->state))
807                         mpdclient_cmd_next(c);
808                 break;
809         case CMD_SEEK_BACKWARD:
810                 if (!IS_STOPPED(c->status->state)) {
811                         if (seek_id != c->song->id) {
812                                 seek_id = c->song->id;
813                                 seek_target_time = c->status->elapsedTime;
814                         }
815                         seek_target_time-=options.seek_time;
816                         if (seek_target_time < 0)
817                                 seek_target_time=0;
818                 }
819                 break;
820         case CMD_TRACK_PREVIOUS:
821                 if (!IS_STOPPED(c->status->state))
822                         mpdclient_cmd_prev(c);
823                 break;
824         case CMD_SHUFFLE:
825                 if (mpdclient_cmd_shuffle(c) == 0)
826                         screen_status_message(_("Shuffled playlist"));
827                 break;
828         case CMD_CLEAR:
829                 if (mpdclient_cmd_clear(c) == 0)
830                         screen_status_message(_("Cleared playlist"));
831                 break;
832         case CMD_REPEAT:
833                 mpdclient_cmd_repeat(c, !c->status->repeat);
834                 break;
835         case CMD_RANDOM:
836                 mpdclient_cmd_random(c, !c->status->random);
837                 break;
838         case CMD_SINGLE:
839                 mpdclient_cmd_single(c, !c->status->single);
840                 break;
841         case CMD_CONSUME:
842                 mpdclient_cmd_consume(c, !c->status->consume);
843                 break;
844         case CMD_CROSSFADE:
845                 if (c->status->crossfade)
846                         mpdclient_cmd_crossfade(c, 0);
847                 else
848                         mpdclient_cmd_crossfade(c, options.crossfade_time);
849                 break;
850         case CMD_DB_UPDATE:
851                 if (!c->status->updatingDb) {
852                         if( mpdclient_cmd_db_update(c,NULL)==0 )
853                                 screen_status_printf(_("Database update started"));
854                 } else
855                         screen_status_printf(_("Database update running..."));
856                 break;
857         case CMD_VOLUME_UP:
858                 mpdclient_cmd_volume_up(c);
859                 break;
860         case CMD_VOLUME_DOWN:
861                 mpdclient_cmd_volume_down(c);
862                 break;
864         default:
865                 return 0;
866         }
868         return 1;
871 void
872 screen_cmd(mpdclient_t *c, command_t cmd)
874         screen.last_cmd = cmd;
875 #ifndef NCMPC_MINI
876         welcome = FALSE;
877 #endif
879         if (mode_fn->cmd != NULL && mode_fn->cmd(c, cmd))
880                 return;
882         if (screen_client_cmd(c, cmd))
883                 return;
885         switch(cmd) {
886         case CMD_TOGGLE_FIND_WRAP:
887                 options.find_wrap = !options.find_wrap;
888                 screen_status_printf(options.find_wrap ?
889                                      _("Find mode: Wrapped") :
890                                      _("Find mode: Normal"));
891                 break;
892         case CMD_TOGGLE_AUTOCENTER:
893                 options.auto_center = !options.auto_center;
894                 screen_status_printf(options.auto_center ?
895                                      _("Auto center mode: On") :
896                                      _("Auto center mode: Off"));
897                 break;
898         case CMD_SCREEN_UPDATE:
899                 screen_paint(c);
900                 break;
901         case CMD_SCREEN_PREVIOUS:
902                 screen_next_mode(c, -1);
903                 break;
904         case CMD_SCREEN_NEXT:
905                 screen_next_mode(c, 1);
906                 break;
907         case CMD_SCREEN_PLAY:
908                 screen_switch(&screen_playlist, c);
909                 break;
910         case CMD_SCREEN_FILE:
911                 screen_switch(&screen_browse, c);
912                 break;
913 #ifdef ENABLE_HELP_SCREEN
914         case CMD_SCREEN_HELP:
915                 screen_switch(&screen_help, c);
916                 break;
917 #endif
918 #ifdef ENABLE_SEARCH_SCREEN
919         case CMD_SCREEN_SEARCH:
920                 screen_switch(&screen_search, c);
921                 break;
922 #endif
923 #ifdef ENABLE_ARTIST_SCREEN
924         case CMD_SCREEN_ARTIST:
925                 screen_switch(&screen_artist, c);
926                 break;
927 #endif
928 #ifdef ENABLE_SONG_SCREEN
929         case CMD_SCREEN_SONG:
930                 screen_switch(&screen_song, c);
931                 break;
932 #endif
933 #ifdef ENABLE_KEYDEF_SCREEN
934         case CMD_SCREEN_KEYDEF:
935                 screen_switch(&screen_keydef, c);
936                 break;
937 #endif
938 #ifdef ENABLE_LYRICS_SCREEN
939         case CMD_SCREEN_LYRICS:
940                 screen_switch(&screen_lyrics, c);
941                 break;
942 #endif
943 #ifdef ENABLE_OUTPUTS_SCREEN
944         case CMD_SCREEN_OUTPUTS:
945                 screen_switch(&screen_outputs, c);
946                 break;
947         case CMD_SCREEN_SWAP:
948                 screen_swap(c, NULL);
949                 break;
950 #endif
952         default:
953                 break;
954         }