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);
114 }
116 static int
117 find_configured_screen(const char *name)
118 {
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;
126 }
128 static void
129 screen_next_mode(mpdclient_t *c, int offset)
130 {
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);
146 }
148 #ifndef NCMPC_MINI
149 static void
150 print_hotkey(WINDOW *w, command_t cmd, const char *label)
151 {
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, ' ');
159 }
160 #endif
162 static inline int
163 get_volume(const struct mpd_Status *status)
164 {
165 return status != NULL
166 ? status->volume
167 : MPD_STATUS_NO_VOLUME;
168 }
170 static void
171 paint_top_window2(const char *header, mpdclient_t *c)
172 {
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);
239 }
241 static inline int
242 volume_length(int volume)
243 {
244 if (volume == 100)
245 return 3;
246 if (volume >= 10 && volume < 100)
247 return 2;
248 if (volume >= 0 && volume < 10)
249 return 1;
250 return -1;
251 }
253 static void
254 paint_top_window(const char *header, mpdclient_t *c, int full_repaint)
255 {
256 static int prev_volume = -1;
257 static unsigned prev_header_len = -1;
258 WINDOW *w = screen.top_window.w;
260 if (prev_header_len != utf8_width(header)) {
261 prev_header_len = utf8_width(header);
262 full_repaint = 1;
263 }
265 if (c->status &&
266 volume_length(prev_volume) != volume_length(c->status->volume))
267 full_repaint = 1;
269 if (full_repaint) {
270 wmove(w, 0, 0);
271 wclrtoeol(w);
272 }
274 if ((c->status != NULL && prev_volume != c->status->volume) ||
275 full_repaint)
276 paint_top_window2(header, c);
277 }
279 static void
280 paint_progress_window(mpdclient_t *c)
281 {
282 double p;
283 int width;
284 int elapsedTime;
286 if (c->status==NULL || IS_STOPPED(c->status->state)) {
287 mvwhline(screen.progress_window.w, 0, 0, ACS_HLINE,
288 screen.progress_window.cols);
289 wnoutrefresh(screen.progress_window.w);
290 return;
291 }
293 if (c->song && seek_id == c->song->id)
294 elapsedTime = seek_target_time;
295 else
296 elapsedTime = c->status->elapsedTime;
298 p = ((double) elapsedTime) / ((double) c->status->totalTime);
300 width = (int) (p * (double) screen.progress_window.cols);
301 mvwhline(screen.progress_window.w,
302 0, 0,
303 ACS_HLINE,
304 screen.progress_window.cols);
305 whline(screen.progress_window.w, '=', width-1);
306 mvwaddch(screen.progress_window.w, 0, width-1, 'O');
307 wnoutrefresh(screen.progress_window.w);
308 }
310 static void
311 paint_status_window(mpdclient_t *c)
312 {
313 WINDOW *w = screen.status_window.w;
314 mpd_Status *status = c->status;
315 int state;
316 mpd_Song *song = c->song;
317 int elapsedTime = 0;
318 #ifdef NCMPC_MINI
319 static char bitrate[1];
320 #else
321 char bitrate[16];
322 #endif
323 const char *str = NULL;
324 int x = 0;
326 if( time(NULL) - screen.status_timestamp <= options.status_message_time )
327 return;
329 wmove(w, 0, 0);
330 wclrtoeol(w);
331 colors_use(w, COLOR_STATUS_BOLD);
333 state = status == NULL ? MPD_STATUS_STATE_UNKNOWN : status->state;
335 switch (state) {
336 case MPD_STATUS_STATE_PLAY:
337 str = _("Playing:");
338 break;
339 case MPD_STATUS_STATE_PAUSE:
340 str = _("[Paused]");
341 break;
342 case MPD_STATUS_STATE_STOP:
343 default:
344 break;
345 }
347 if (str) {
348 waddstr(w, str);
349 x += utf8_width(str) + 1;
350 }
352 /* create time string */
353 memset(screen.buf, 0, screen.buf_size);
354 if (IS_PLAYING(state) || IS_PAUSED(state)) {
355 if (status->totalTime > 0) {
356 /*checks the conf to see whether to display elapsed or remaining time */
357 if(!strcmp(options.timedisplay_type,"elapsed"))
358 elapsedTime = c->status->elapsedTime;
359 else if(!strcmp(options.timedisplay_type,"remaining"))
360 elapsedTime = (c->status->totalTime - c->status->elapsedTime);
362 if( c->song && seek_id == c->song->id )
363 elapsedTime = seek_target_time;
365 /* display bitrate if visible-bitrate is true */
366 #ifndef NCMPC_MINI
367 if (options.visible_bitrate) {
368 g_snprintf(bitrate, 16,
369 " [%d kbps]", status->bitRate);
370 } else {
371 bitrate[0] = '\0';
372 }
373 #endif
375 /*write out the time, using hours if time over 60 minutes*/
376 if (c->status->totalTime > 3600) {
377 g_snprintf(screen.buf, screen.buf_size,
378 "%s [%i:%02i:%02i/%i:%02i:%02i]",
379 bitrate, elapsedTime/3600, (elapsedTime%3600)/60, elapsedTime%60,
380 status->totalTime/3600, (status->totalTime%3600)/60, status->totalTime%60);
381 } else {
382 g_snprintf(screen.buf, screen.buf_size,
383 "%s [%i:%02i/%i:%02i]",
384 bitrate, elapsedTime/60, elapsedTime%60,
385 status->totalTime/60, status->totalTime%60 );
386 }
387 #ifndef NCMPC_MINI
388 } else {
389 g_snprintf(screen.buf, screen.buf_size,
390 " [%d kbps]", status->bitRate );
391 #endif
392 }
393 #ifndef NCMPC_MINI
394 } else {
395 if (options.display_time) {
396 time_t timep;
398 time(&timep);
399 strftime(screen.buf, screen.buf_size, "%X ",localtime(&timep));
400 }
401 #endif
402 }
404 /* display song */
405 if (status != NULL && (IS_PLAYING(status->state) ||
406 IS_PAUSED(status->state))) {
407 char songname[MAX_SONGNAME_LENGTH];
408 #ifndef NCMPC_MINI
409 int width = COLS - x - utf8_width(screen.buf);
410 #endif
412 if (song)
413 strfsong(songname, MAX_SONGNAME_LENGTH,
414 options.status_format, song);
415 else
416 songname[0] = '\0';
418 colors_use(w, COLOR_STATUS);
419 /* scroll if the song name is to long */
420 #ifndef NCMPC_MINI
421 if (options.scroll && utf8_width(songname) > (unsigned)width) {
422 static scroll_state_t st = { 0, 0 };
423 char *tmp = strscroll(songname, options.scroll_sep, width, &st);
425 g_strlcpy(songname, tmp, MAX_SONGNAME_LENGTH);
426 g_free(tmp);
427 }
428 #endif
429 //mvwaddnstr(w, 0, x, songname, width);
430 mvwaddstr(w, 0, x, songname);
431 }
433 /* display time string */
434 if (screen.buf[0]) {
435 x = screen.status_window.cols - strlen(screen.buf);
436 colors_use(w, COLOR_STATUS_TIME);
437 mvwaddstr(w, 0, x, screen.buf);
438 }
440 wnoutrefresh(w);
441 }
443 void
444 screen_exit(void)
445 {
446 if (mode_fn->close != NULL)
447 mode_fn->close();
449 screen_list_exit();
451 string_list_free(screen.find_history);
452 g_free(screen.buf);
453 g_free(screen.findbuf);
455 delwin(screen.top_window.w);
456 delwin(screen.main_window.w);
457 delwin(screen.progress_window.w);
458 delwin(screen.status_window.w);
459 }
461 void
462 screen_resize(struct mpdclient *c)
463 {
464 if (COLS<SCREEN_MIN_COLS || LINES<SCREEN_MIN_ROWS) {
465 screen_exit();
466 fprintf(stderr, "%s", _("Error: Screen too small"));
467 exit(EXIT_FAILURE);
468 }
470 resizeterm(LINES, COLS);
472 screen.cols = COLS;
473 screen.rows = LINES;
475 /* top window */
476 screen.top_window.cols = screen.cols;
477 wresize(screen.top_window.w, 2, screen.cols);
479 /* main window */
480 screen.main_window.cols = screen.cols;
481 screen.main_window.rows = screen.rows-4;
482 wresize(screen.main_window.w, screen.main_window.rows, screen.cols);
483 wclear(screen.main_window.w);
485 /* progress window */
486 screen.progress_window.cols = screen.cols;
487 wresize(screen.progress_window.w, 1, screen.cols);
488 mvwin(screen.progress_window.w, screen.rows-2, 0);
490 /* status window */
491 screen.status_window.cols = screen.cols;
492 wresize(screen.status_window.w, 1, screen.cols);
493 mvwin(screen.status_window.w, screen.rows-1, 0);
495 screen.buf_size = screen.cols;
496 g_free(screen.buf);
497 screen.buf = g_malloc(screen.cols);
499 /* resize all screens */
500 screen_list_resize(screen.main_window.cols, screen.main_window.rows);
502 /* ? - without this the cursor becomes visible with aterm & Eterm */
503 curs_set(1);
504 curs_set(0);
506 screen_paint(c);
507 }
509 void
510 screen_status_message(const char *msg)
511 {
512 WINDOW *w = screen.status_window.w;
514 wmove(w, 0, 0);
515 wclrtoeol(w);
516 colors_use(w, COLOR_STATUS_ALERT);
517 waddstr(w, msg);
518 wnoutrefresh(w);
519 screen.status_timestamp = time(NULL);
520 }
522 void
523 screen_status_printf(const char *format, ...)
524 {
525 char *msg;
526 va_list ap;
528 va_start(ap,format);
529 msg = g_strdup_vprintf(format,ap);
530 va_end(ap);
531 screen_status_message(msg);
532 g_free(msg);
533 }
535 void
536 screen_init(mpdclient_t *c)
537 {
538 if (COLS < SCREEN_MIN_COLS || LINES < SCREEN_MIN_ROWS) {
539 fprintf(stderr, "%s\n", _("Error: Screen too small"));
540 exit(EXIT_FAILURE);
541 }
543 screen.cols = COLS;
544 screen.rows = LINES;
546 screen.buf = g_malloc(screen.cols);
547 screen.buf_size = screen.cols;
548 screen.findbuf = NULL;
549 screen.start_timestamp = time(NULL);
550 screen.last_cmd = CMD_NONE;
552 /* create top window */
553 screen.top_window.rows = 2;
554 screen.top_window.cols = screen.cols;
555 screen.top_window.w = newwin(screen.top_window.rows,
556 screen.top_window.cols,
557 0, 0);
558 leaveok(screen.top_window.w, TRUE);
559 keypad(screen.top_window.w, TRUE);
561 /* create main window */
562 screen.main_window.rows = screen.rows-4;
563 screen.main_window.cols = screen.cols;
564 screen.main_window.w = newwin(screen.main_window.rows,
565 screen.main_window.cols,
566 2,
567 0);
569 // leaveok(screen.main_window.w, TRUE); temporary disabled
570 keypad(screen.main_window.w, TRUE);
572 /* create progress window */
573 screen.progress_window.rows = 1;
574 screen.progress_window.cols = screen.cols;
575 screen.progress_window.w = newwin(screen.progress_window.rows,
576 screen.progress_window.cols,
577 screen.rows-2,
578 0);
579 leaveok(screen.progress_window.w, TRUE);
581 /* create status window */
582 screen.status_window.rows = 1;
583 screen.status_window.cols = screen.cols;
584 screen.status_window.w = newwin(screen.status_window.rows,
585 screen.status_window.cols,
586 screen.rows-1,
587 0);
589 leaveok(screen.status_window.w, FALSE);
590 keypad(screen.status_window.w, TRUE);
592 #ifdef ENABLE_COLORS
593 if (options.enable_colors) {
594 /* set background attributes */
595 wbkgd(stdscr, COLOR_PAIR(COLOR_LIST));
596 wbkgd(screen.main_window.w, COLOR_PAIR(COLOR_LIST));
597 wbkgd(screen.top_window.w, COLOR_PAIR(COLOR_TITLE));
598 wbkgd(screen.progress_window.w, COLOR_PAIR(COLOR_PROGRESSBAR));
599 wbkgd(screen.status_window.w, COLOR_PAIR(COLOR_STATUS));
600 colors_use(screen.progress_window.w, COLOR_PROGRESSBAR);
601 }
602 #endif
604 refresh();
606 /* initialize screens */
607 screen_list_init(screen.main_window.w,
608 screen.main_window.cols, screen.main_window.rows);
610 if (mode_fn->open != NULL)
611 mode_fn->open(c);
612 }
614 void
615 screen_paint(mpdclient_t *c)
616 {
617 const char *title = NULL;
619 if (mode_fn->get_title != NULL)
620 title = mode_fn->get_title(screen.buf, screen.buf_size);
622 /* paint the title/header window */
623 if( title )
624 paint_top_window(title, c, 1);
625 else
626 paint_top_window("", c, 1);
628 /* paint the bottom window */
630 paint_progress_window(c);
631 paint_status_window(c);
633 /* paint the main window */
635 wclear(screen.main_window.w);
636 if (mode_fn->paint != NULL)
637 mode_fn->paint();
639 /* move the cursor to the origin */
641 if (!options.hardware_cursor)
642 wmove(screen.main_window.w, 0, 0);
644 wnoutrefresh(screen.main_window.w);
646 /* tell curses to update */
647 doupdate();
648 }
650 void
651 screen_update(mpdclient_t *c)
652 {
653 #ifndef NCMPC_MINI
654 static int repeat = -1;
655 static int random_enabled = -1;
656 static int single = -1;
657 static int consume = -1;
658 static int crossfade = -1;
659 static int dbupdate = -1;
661 /* print a message if mpd status has changed */
662 if (c->status != NULL) {
663 if (repeat < 0) {
664 repeat = c->status->repeat;
665 random_enabled = c->status->random;
666 single = c->status->single;
667 consume = c->status->consume;
668 crossfade = c->status->crossfade;
669 dbupdate = c->status->updatingDb;
670 }
672 if (repeat != c->status->repeat)
673 screen_status_printf(c->status->repeat ?
674 _("Repeat mode is on") :
675 _("Repeat mode is off"));
677 if (random_enabled != c->status->random)
678 screen_status_printf(c->status->random ?
679 _("Random mode is on") :
680 _("Random mode is off"));
682 if (single != c->status->single)
683 screen_status_printf(c->status->single ?
684 /* "single" mode means
685 that MPD will
686 automatically stop
687 after playing one
688 single song */
689 _("Single mode is on") :
690 _("Single mode is off"));
692 if (consume != c->status->consume)
693 screen_status_printf(c->status->consume ?
694 /* "consume" mode means
695 that MPD removes each
696 song which has
697 finished playing */
698 _("Consume mode is on") :
699 _("Consume mode is off"));
701 if (crossfade != c->status->crossfade)
702 screen_status_printf(_("Crossfade %d seconds"), c->status->crossfade);
704 if (dbupdate && dbupdate != c->status->updatingDb) {
705 screen_status_printf(_("Database updated"));
706 }
708 repeat = c->status->repeat;
709 single = c->status->single;
710 consume = c->status->consume;
711 random_enabled = c->status->random;
712 crossfade = c->status->crossfade;
713 dbupdate = c->status->updatingDb;
714 }
716 /* update title/header window */
717 if (welcome && options.welcome_screen_list &&
718 screen.last_cmd==CMD_NONE &&
719 time(NULL)-screen.start_timestamp <= SCREEN_WELCOME_TIME)
720 paint_top_window("", c, 0);
721 else
722 #endif
723 if (mode_fn->get_title != NULL) {
724 paint_top_window(mode_fn->get_title(screen.buf,screen.buf_size), c, 0);
725 #ifndef NCMPC_MINI
726 welcome = FALSE;
727 #endif
728 } else
729 paint_top_window("", c, 0);
731 /* update progress window */
732 paint_progress_window(c);
734 /* update status window */
735 paint_status_window(c);
737 /* update the main window */
738 if (mode_fn->update != NULL)
739 mode_fn->update(c);
741 /* move the cursor to the origin */
743 if (!options.hardware_cursor)
744 wmove(screen.main_window.w, 0, 0);
746 wnoutrefresh(screen.main_window.w);
748 /* tell curses to update */
749 doupdate();
750 }
752 void
753 screen_idle(mpdclient_t *c)
754 {
755 if (c->song && seek_id == c->song->id &&
756 (screen.last_cmd == CMD_SEEK_FORWARD ||
757 screen.last_cmd == CMD_SEEK_BACKWARD))
758 mpdclient_cmd_seek(c, seek_id, seek_target_time);
760 screen.last_cmd = CMD_NONE;
761 seek_id = -1;
762 }
764 #ifdef HAVE_GETMOUSE
765 int
766 screen_get_mouse_event(mpdclient_t *c, unsigned long *bstate, int *row)
767 {
768 MEVENT event;
770 /* retrieve the mouse event from ncurses */
771 getmouse(&event);
772 /* calculate the selected row in the list window */
773 *row = event.y - screen.top_window.rows;
774 /* copy button state bits */
775 *bstate = event.bstate;
776 /* if button 2 was pressed switch screen */
777 if (event.bstate & BUTTON2_CLICKED) {
778 screen_cmd(c, CMD_SCREEN_NEXT);
779 return 1;
780 }
782 return 0;
783 }
784 #endif
786 static int
787 screen_client_cmd(mpdclient_t *c, command_t cmd)
788 {
789 if (c->connection == NULL || c->status == NULL)
790 return 0;
792 switch(cmd) {
793 /*
794 case CMD_PLAY:
795 mpdclient_cmd_play(c, MPD_PLAY_AT_BEGINNING);
796 break;
797 */
798 case CMD_PAUSE:
799 mpdclient_cmd_pause(c, !IS_PAUSED(c->status->state));
800 break;
801 case CMD_STOP:
802 mpdclient_cmd_stop(c);
803 break;
804 case CMD_CROP:
805 mpdclient_cmd_crop(c);
806 break;
807 case CMD_SEEK_FORWARD:
808 if (!IS_STOPPED(c->status->state)) {
809 if (c->song && seek_id != c->song->id) {
810 seek_id = c->song->id;
811 seek_target_time = c->status->elapsedTime;
812 }
813 seek_target_time+=options.seek_time;
814 if (seek_target_time < c->status->totalTime)
815 break;
816 seek_target_time = c->status->totalTime;
817 /* seek_target_time=0; */
818 }
819 break;
820 /* fall through... */
821 case CMD_TRACK_NEXT:
822 if (!IS_STOPPED(c->status->state))
823 mpdclient_cmd_next(c);
824 break;
825 case CMD_SEEK_BACKWARD:
826 if (!IS_STOPPED(c->status->state)) {
827 if (seek_id != c->song->id) {
828 seek_id = c->song->id;
829 seek_target_time = c->status->elapsedTime;
830 }
831 seek_target_time-=options.seek_time;
832 if (seek_target_time < 0)
833 seek_target_time=0;
834 }
835 break;
836 case CMD_TRACK_PREVIOUS:
837 if (!IS_STOPPED(c->status->state))
838 mpdclient_cmd_prev(c);
839 break;
840 case CMD_SHUFFLE:
841 if (mpdclient_cmd_shuffle(c) == 0)
842 screen_status_message(_("Shuffled playlist"));
843 break;
844 case CMD_CLEAR:
845 if (mpdclient_cmd_clear(c) == 0)
846 screen_status_message(_("Cleared playlist"));
847 break;
848 case CMD_REPEAT:
849 mpdclient_cmd_repeat(c, !c->status->repeat);
850 break;
851 case CMD_RANDOM:
852 mpdclient_cmd_random(c, !c->status->random);
853 break;
854 case CMD_SINGLE:
855 mpdclient_cmd_single(c, !c->status->single);
856 break;
857 case CMD_CONSUME:
858 mpdclient_cmd_consume(c, !c->status->consume);
859 break;
860 case CMD_CROSSFADE:
861 if (c->status->crossfade)
862 mpdclient_cmd_crossfade(c, 0);
863 else
864 mpdclient_cmd_crossfade(c, options.crossfade_time);
865 break;
866 case CMD_DB_UPDATE:
867 if (!c->status->updatingDb) {
868 if( mpdclient_cmd_db_update(c,NULL)==0 )
869 screen_status_printf(_("Database update started"));
870 } else
871 screen_status_printf(_("Database update running..."));
872 break;
873 case CMD_VOLUME_UP:
874 mpdclient_cmd_volume_up(c);
875 break;
876 case CMD_VOLUME_DOWN:
877 mpdclient_cmd_volume_down(c);
878 break;
880 default:
881 return 0;
882 }
884 return 1;
885 }
887 void
888 screen_cmd(mpdclient_t *c, command_t cmd)
889 {
890 screen.last_cmd = cmd;
891 #ifndef NCMPC_MINI
892 welcome = FALSE;
893 #endif
895 if (mode_fn->cmd != NULL && mode_fn->cmd(c, cmd))
896 return;
898 if (screen_client_cmd(c, cmd))
899 return;
901 switch(cmd) {
902 case CMD_TOGGLE_FIND_WRAP:
903 options.find_wrap = !options.find_wrap;
904 screen_status_printf(options.find_wrap ?
905 _("Find mode: Wrapped") :
906 _("Find mode: Normal"));
907 break;
908 case CMD_TOGGLE_AUTOCENTER:
909 options.auto_center = !options.auto_center;
910 screen_status_printf(options.auto_center ?
911 _("Auto center mode: On") :
912 _("Auto center mode: Off"));
913 break;
914 case CMD_SCREEN_UPDATE:
915 screen_paint(c);
916 break;
917 case CMD_SCREEN_PREVIOUS:
918 screen_next_mode(c, -1);
919 break;
920 case CMD_SCREEN_NEXT:
921 screen_next_mode(c, 1);
922 break;
923 case CMD_SCREEN_PLAY:
924 screen_switch(&screen_playlist, c);
925 break;
926 case CMD_SCREEN_FILE:
927 screen_switch(&screen_browse, c);
928 break;
929 #ifdef ENABLE_HELP_SCREEN
930 case CMD_SCREEN_HELP:
931 screen_switch(&screen_help, c);
932 break;
933 #endif
934 #ifdef ENABLE_SEARCH_SCREEN
935 case CMD_SCREEN_SEARCH:
936 screen_switch(&screen_search, c);
937 break;
938 #endif
939 #ifdef ENABLE_ARTIST_SCREEN
940 case CMD_SCREEN_ARTIST:
941 screen_switch(&screen_artist, c);
942 break;
943 #endif
944 #ifdef ENABLE_SONG_SCREEN
945 case CMD_SCREEN_SONG:
946 screen_switch(&screen_song, c);
947 break;
948 #endif
949 #ifdef ENABLE_KEYDEF_SCREEN
950 case CMD_SCREEN_KEYDEF:
951 screen_switch(&screen_keydef, c);
952 break;
953 #endif
954 #ifdef ENABLE_LYRICS_SCREEN
955 case CMD_SCREEN_LYRICS:
956 screen_switch(&screen_lyrics, c);
957 break;
958 #endif
959 #ifdef ENABLE_OUTPUTS_SCREEN
960 case CMD_SCREEN_OUTPUTS:
961 screen_switch(&screen_outputs, c);
962 break;
963 case CMD_SCREEN_SWAP:
964 screen_swap(c, NULL);
965 break;
966 #endif
968 default:
969 break;
970 }
971 }