1 /*
2 * $Id$
3 *
4 * (c) 2004 by Kalle Wallin <kaw@linux.se>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 */
21 #include "screen.h"
22 #include "screen_utils.h"
23 #include "config.h"
24 #include "ncmpc.h"
25 #include "support.h"
26 #include "mpdclient.h"
27 #include "utils.h"
28 #include "command.h"
29 #include "options.h"
30 #include "colors.h"
31 #include "strfsong.h"
32 #include "wreadln.h"
34 #include <stdlib.h>
35 #include <unistd.h>
36 #include <stdarg.h>
37 #include <string.h>
38 #include <time.h>
39 #include <locale.h>
41 #define SCREEN_PLAYLIST_ID 0
42 #define SCREEN_BROWSE_ID 1
43 #define SCREEN_ARTIST_ID 2
44 #define SCREEN_HELP_ID 100
45 #define SCREEN_KEYDEF_ID 101
46 #define SCREEN_SEARCH_ID 103
47 #define SCREEN_LYRICS_ID 104
51 /* screens */
52 extern const struct screen_functions screen_playlist;
53 extern const struct screen_functions screen_browse;
54 #ifdef ENABLE_ARTIST_SCREEN
55 extern const struct screen_functions screen_artist;
56 #endif
57 extern const struct screen_functions screen_help;
58 #ifdef ENABLE_SEARCH_SCREEN
59 extern const struct screen_functions screen_search;
60 #endif
61 #ifdef ENABLE_KEYDEF_SCREEN
62 extern const struct screen_functions screen_keydef;
63 #endif
64 extern const struct screen_functions screen_lyrics;
66 typedef struct screen_functions * (*screen_get_mode_functions_fn_t) (void);
68 static const struct
69 {
70 gint id;
71 const gchar *name;
72 const struct screen_functions *functions;
73 } screens[] = {
74 { SCREEN_PLAYLIST_ID, "playlist", &screen_playlist },
75 { SCREEN_BROWSE_ID, "browse", &screen_browse },
76 #ifdef ENABLE_ARTIST_SCREEN
77 { SCREEN_ARTIST_ID, "artist", &screen_artist },
78 #endif
79 { SCREEN_HELP_ID, "help", &screen_help },
80 #ifdef ENABLE_SEARCH_SCREEN
81 { SCREEN_SEARCH_ID, "search", &screen_search },
82 #endif
83 #ifdef ENABLE_KEYDEF_SCREEN
84 { SCREEN_KEYDEF_ID, "keydef", &screen_keydef },
85 #endif
86 #ifdef ENABLE_LYRICS_SCREEN
87 { SCREEN_LYRICS_ID, "lyrics", &screen_lyrics },
88 #endif
89 };
91 #define NUM_SCREENS (sizeof(screens) / sizeof(screens[0]))
93 static gboolean welcome = TRUE;
94 static struct screen screen;
95 static const struct screen_functions *mode_fn = &screen_playlist;
96 static int seek_id = -1;
97 static int seek_target_time = 0;
99 gint
100 screen_get_id(const char *name)
101 {
102 guint i;
104 for (i = 0; i < NUM_SCREENS; ++i)
105 if (strcmp(name, screens[i].name) == 0)
106 return screens[i].id;
108 return -1;
109 }
111 static gint
112 lookup_mode(gint id)
113 {
114 guint i;
116 for (i = 0; i < NUM_SCREENS; ++i)
117 if (screens[i].id == id)
118 return i;
120 return -1;
121 }
123 gint get_cur_mode_id(void)
124 {
125 return screens[screen.mode].id;
126 }
128 static void
129 switch_screen_mode(gint id, mpdclient_t *c)
130 {
131 gint new_mode;
133 if( id == screens[screen.mode].id )
134 return;
136 new_mode = lookup_mode(id);
137 if (new_mode < 0)
138 return;
140 /* close the old mode */
141 if (mode_fn->close != NULL)
142 mode_fn->close();
144 /* get functions for the new mode */
145 D("switch_screen(%s)\n", screens[new_mode].name );
146 mode_fn = screens[new_mode].functions;
147 screen.mode = new_mode;
148 screen.painted = 0;
150 /* open the new mode */
151 if (mode_fn->open != NULL)
152 mode_fn->open(&screen, c);
153 }
155 static int
156 find_configured_screen(const char *name)
157 {
158 unsigned i;
160 for (i = 0; options.screen_list[i] != NULL; ++i)
161 if (strcmp(options.screen_list[i], name) == 0)
162 return i;
164 return -1;
165 }
167 static void
168 screen_next_mode(mpdclient_t *c, int offset)
169 {
170 int max = g_strv_length(options.screen_list);
171 int current, next;
173 /* find current screen */
174 current = find_configured_screen(screens[screen.mode].name);
175 next = current + offset;
176 if (next<0)
177 next = max-1;
178 else if (next>=max)
179 next = 0;
181 D("current mode: %d:%d next:%d\n", current, max, next);
182 switch_screen_mode(screen_get_id(options.screen_list[next]), c);
183 }
185 static void
186 paint_top_window2(const char *header, mpdclient_t *c)
187 {
188 char flags[5];
189 WINDOW *w = screen.top_window.w;
190 char buf[32];
192 if (header[0]) {
193 colors_use(w, COLOR_TITLE_BOLD);
194 mvwaddstr(w, 0, 0, header);
195 } else {
196 colors_use(w, COLOR_TITLE_BOLD);
197 waddstr(w, get_key_names(CMD_SCREEN_HELP, FALSE));
198 colors_use(w, COLOR_TITLE);
199 waddstr(w, _(":Help "));
200 colors_use(w, COLOR_TITLE_BOLD);
201 waddstr(w, get_key_names(CMD_SCREEN_PLAY, FALSE));
202 colors_use(w, COLOR_TITLE);
203 waddstr(w, _(":Playlist "));
204 colors_use(w, COLOR_TITLE_BOLD);
205 waddstr(w, get_key_names(CMD_SCREEN_FILE, FALSE));
206 colors_use(w, COLOR_TITLE);
207 waddstr(w, _(":Browse "));
208 #ifdef ENABLE_ARTIST_SCREEN
209 colors_use(w, COLOR_TITLE_BOLD);
210 waddstr(w, get_key_names(CMD_SCREEN_ARTIST, FALSE));
211 colors_use(w, COLOR_TITLE);
212 waddstr(w, _(":Artist "));
213 #endif
214 #ifdef ENABLE_SEARCH_SCREEN
215 colors_use(w, COLOR_TITLE_BOLD);
216 waddstr(w, get_key_names(CMD_SCREEN_SEARCH, FALSE));
217 colors_use(w, COLOR_TITLE);
218 waddstr(w, _(":Search "));
219 #endif
220 #ifdef ENABLE_LYRICS_SCREEN
221 colors_use(w, COLOR_TITLE_BOLD);
222 waddstr(w, get_key_names(CMD_SCREEN_LYRICS, FALSE));
223 colors_use(w, COLOR_TITLE);
224 waddstr(w, _(":Lyrics "));
225 #endif
226 }
227 if (c->status == NULL || c->status->volume == MPD_STATUS_NO_VOLUME) {
228 g_snprintf(buf, 32, _("Volume n/a "));
229 } else {
230 g_snprintf(buf, 32, _(" Volume %d%%"), c->status->volume);
231 }
232 colors_use(w, COLOR_TITLE);
233 mvwaddstr(w, 0, screen.top_window.cols-my_strlen(buf), buf);
235 flags[0] = 0;
236 if (c->status != NULL) {
237 if (c->status->repeat)
238 g_strlcat(flags, "r", sizeof(flags));
239 if (c->status->random)
240 g_strlcat(flags, "z", sizeof(flags));;
241 if (c->status->crossfade)
242 g_strlcat(flags, "x", sizeof(flags));
243 if (c->status->updatingDb)
244 g_strlcat(flags, "U", sizeof(flags));
245 }
247 colors_use(w, COLOR_LINE);
248 mvwhline(w, 1, 0, ACS_HLINE, screen.top_window.cols);
249 if (flags[0]) {
250 wmove(w,1,screen.top_window.cols-strlen(flags)-3);
251 waddch(w, '[');
252 colors_use(w, COLOR_LINE_BOLD);
253 waddstr(w, flags);
254 colors_use(w, COLOR_LINE);
255 waddch(w, ']');
256 }
257 wnoutrefresh(w);
258 }
260 static void
261 paint_top_window(const char *header, mpdclient_t *c, int full_repaint)
262 {
263 static int prev_volume = -1;
264 static size_t prev_header_len = -1;
265 WINDOW *w = screen.top_window.w;
267 if (prev_header_len!=my_strlen(header)) {
268 prev_header_len = my_strlen(header);
269 full_repaint = 1;
270 }
272 if (full_repaint) {
273 wmove(w, 0, 0);
274 wclrtoeol(w);
275 }
277 if ((c->status != NULL && prev_volume != c->status->volume) ||
278 full_repaint)
279 paint_top_window2(header, c);
280 }
282 static void
283 paint_progress_window(mpdclient_t *c)
284 {
285 double p;
286 int width;
287 int elapsedTime;
289 if (c->status==NULL || IS_STOPPED(c->status->state)) {
290 mvwhline(screen.progress_window.w, 0, 0, ACS_HLINE,
291 screen.progress_window.cols);
292 wnoutrefresh(screen.progress_window.w);
293 return;
294 }
296 if (c->song && seek_id == c->song->id)
297 elapsedTime = seek_target_time;
298 else
299 elapsedTime = c->status->elapsedTime;
301 p = ((double) elapsedTime) / ((double) c->status->totalTime);
303 width = (int) (p * (double) screen.progress_window.cols);
304 mvwhline(screen.progress_window.w,
305 0, 0,
306 ACS_HLINE,
307 screen.progress_window.cols);
308 whline(screen.progress_window.w, '=', width-1);
309 mvwaddch(screen.progress_window.w, 0, width-1, 'O');
310 wnoutrefresh(screen.progress_window.w);
311 }
313 static void
314 paint_status_window(mpdclient_t *c)
315 {
316 WINDOW *w = screen.status_window.w;
317 mpd_Status *status = c->status;
318 mpd_Song *song = c->song;
319 int elapsedTime = 0;
320 char bitrate[16];
321 const char *str = NULL;
322 int x = 0;
324 if( time(NULL) - screen.status_timestamp <= SCREEN_STATUS_MESSAGE_TIME )
325 return;
327 wmove(w, 0, 0);
328 wclrtoeol(w);
329 colors_use(w, COLOR_STATUS_BOLD);
331 switch (status == NULL ? MPD_STATUS_STATE_STOP : status->state) {
332 case MPD_STATUS_STATE_PLAY:
333 str = _("Playing:");
334 break;
335 case MPD_STATUS_STATE_PAUSE:
336 str = _("[Paused]");
337 break;
338 case MPD_STATUS_STATE_STOP:
339 default:
340 break;
341 }
343 if (str) {
344 waddstr(w, str);
345 x += my_strlen(str)+1;
346 }
348 /* create time string */
349 memset(screen.buf, 0, screen.buf_size);
350 if (status != NULL && (IS_PLAYING(status->state) ||
351 IS_PAUSED(status->state))) {
352 if (status->totalTime > 0) {
353 /*checks the conf to see whether to display elapsed or remaining time */
354 if(!strcmp(options.timedisplay_type,"elapsed"))
355 elapsedTime = c->status->elapsedTime;
356 else if(!strcmp(options.timedisplay_type,"remaining"))
357 elapsedTime = (c->status->totalTime - c->status->elapsedTime);
359 if( c->song && seek_id == c->song->id )
360 elapsedTime = seek_target_time;
362 /* display bitrate if visible-bitrate is true */
363 if (options.visible_bitrate) {
364 g_snprintf(bitrate, 16,
365 " [%d kbps]", status->bitRate);
366 } else {
367 bitrate[0] = '\0';
368 }
370 /*write out the time, using hours if time over 60 minutes*/
371 if (c->status->totalTime > 3600) {
372 g_snprintf(screen.buf, screen.buf_size,
373 "%s [%i:%02i:%02i/%i:%02i:%02i]",
374 bitrate, elapsedTime/3600, (elapsedTime%3600)/60, elapsedTime%60,
375 status->totalTime/3600, (status->totalTime%3600)/60, status->totalTime%60);
376 } else {
377 g_snprintf(screen.buf, screen.buf_size,
378 "%s [%i:%02i/%i:%02i]",
379 bitrate, elapsedTime/60, elapsedTime%60,
380 status->totalTime/60, status->totalTime%60 );
381 }
382 } else {
383 g_snprintf(screen.buf, screen.buf_size,
384 " [%d kbps]", status->bitRate );
385 }
386 } else {
387 time_t timep;
389 time(&timep);
390 strftime(screen.buf, screen.buf_size, "%X ",localtime(&timep));
391 }
393 /* display song */
394 if (status != NULL && (IS_PLAYING(status->state) ||
395 IS_PAUSED(status->state))) {
396 char songname[MAX_SONGNAME_LENGTH];
397 int width = COLS-x-my_strlen(screen.buf);
399 if (song)
400 strfsong(songname, MAX_SONGNAME_LENGTH, STATUS_FORMAT, song);
401 else
402 songname[0] = '\0';
404 colors_use(w, COLOR_STATUS);
405 /* scroll if the song name is to long */
406 if (options.scroll && my_strlen(songname) > (size_t)width) {
407 static scroll_state_t st = { 0, 0 };
408 char *tmp = strscroll(songname, options.scroll_sep, width, &st);
410 g_strlcpy(songname, tmp, MAX_SONGNAME_LENGTH);
411 g_free(tmp);
412 }
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);
425 }
427 void
428 screen_exit(void)
429 {
430 guint i;
432 if (mode_fn->close != NULL)
433 mode_fn->close();
435 /* close and exit all screens (playlist,browse,help...) */
436 for (i = 0; i < NUM_SCREENS; ++i) {
437 const struct screen_functions *sf = screens[i].functions;
439 if (sf->exit)
440 sf->exit();
441 }
443 string_list_free(screen.find_history);
444 g_free(screen.buf);
445 g_free(screen.findbuf);
446 }
448 void
449 screen_resize(void)
450 {
451 guint i;
453 D("Resize rows %d->%d, cols %d->%d\n",screen.rows,LINES,screen.cols,COLS);
454 if (COLS<SCREEN_MIN_COLS || LINES<SCREEN_MIN_ROWS) {
455 screen_exit();
456 fprintf(stderr, _("Error: Screen to small!\n"));
457 exit(EXIT_FAILURE);
458 }
460 resizeterm(LINES, COLS);
462 screen.cols = COLS;
463 screen.rows = LINES;
465 /* top window */
466 screen.top_window.cols = screen.cols;
467 wresize(screen.top_window.w, 2, screen.cols);
469 /* main window */
470 screen.main_window.cols = screen.cols;
471 screen.main_window.rows = screen.rows-4;
472 wresize(screen.main_window.w, screen.main_window.rows, screen.cols);
473 wclear(screen.main_window.w);
475 /* progress window */
476 screen.progress_window.cols = screen.cols;
477 wresize(screen.progress_window.w, 1, screen.cols);
478 mvwin(screen.progress_window.w, screen.rows-2, 0);
480 /* status window */
481 screen.status_window.cols = screen.cols;
482 wresize(screen.status_window.w, 1, screen.cols);
483 mvwin(screen.status_window.w, screen.rows-1, 0);
485 screen.buf_size = screen.cols;
486 g_free(screen.buf);
487 screen.buf = g_malloc(screen.cols);
489 /* close and exit all screens (playlist,browse,help...) */
490 for (i = 0; i < NUM_SCREENS; ++i) {
491 const struct screen_functions *sf = screens[i].functions;
493 if (sf->resize)
494 sf->resize(screen.main_window.cols, screen.main_window.rows);
495 }
498 /* ? - without this the cursor becomes visible with aterm & Eterm */
499 curs_set(1);
500 curs_set(0);
502 screen.painted = 0;
503 }
505 void
506 screen_status_message(const char *msg)
507 {
508 WINDOW *w = screen.status_window.w;
510 wmove(w, 0, 0);
511 wclrtoeol(w);
512 colors_use(w, COLOR_STATUS_ALERT);
513 waddstr(w, msg);
514 wnoutrefresh(w);
515 screen.status_timestamp = time(NULL);
516 }
518 void
519 screen_status_printf(const char *format, ...)
520 {
521 char *msg;
522 va_list ap;
524 va_start(ap,format);
525 msg = g_strdup_vprintf(format,ap);
526 va_end(ap);
527 screen_status_message(msg);
528 g_free(msg);
529 }
531 void
532 screen_init(mpdclient_t *c)
533 {
534 guint i;
536 if (COLS < SCREEN_MIN_COLS || LINES < SCREEN_MIN_ROWS) {
537 fprintf(stderr, _("Error: Screen to small!\n"));
538 exit(EXIT_FAILURE);
539 }
541 screen.mode = 0;
542 screen.cols = COLS;
543 screen.rows = LINES;
545 screen.buf = g_malloc(screen.cols);
546 screen.buf_size = screen.cols;
547 screen.findbuf = NULL;
548 screen.painted = 0;
549 screen.start_timestamp = time(NULL);
550 screen.input_timestamp = time(NULL);
551 screen.last_cmd = CMD_NONE;
553 /* create top window */
554 screen.top_window.rows = 2;
555 screen.top_window.cols = screen.cols;
556 screen.top_window.w = newwin(screen.top_window.rows,
557 screen.top_window.cols,
558 0, 0);
559 leaveok(screen.top_window.w, TRUE);
560 keypad(screen.top_window.w, TRUE);
562 /* create main window */
563 screen.main_window.rows = screen.rows-4;
564 screen.main_window.cols = screen.cols;
565 screen.main_window.w = newwin(screen.main_window.rows,
566 screen.main_window.cols,
567 2,
568 0);
570 // leaveok(screen.main_window.w, TRUE); temporary disabled
571 keypad(screen.main_window.w, TRUE);
573 /* create progress window */
574 screen.progress_window.rows = 1;
575 screen.progress_window.cols = screen.cols;
576 screen.progress_window.w = newwin(screen.progress_window.rows,
577 screen.progress_window.cols,
578 screen.rows-2,
579 0);
580 leaveok(screen.progress_window.w, TRUE);
582 /* create status window */
583 screen.status_window.rows = 1;
584 screen.status_window.cols = screen.cols;
585 screen.status_window.w = newwin(screen.status_window.rows,
586 screen.status_window.cols,
587 screen.rows-1,
588 0);
590 leaveok(screen.status_window.w, FALSE);
591 keypad(screen.status_window.w, TRUE);
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 }
603 refresh();
605 /* initialize screens */
606 for (i = 0; i < NUM_SCREENS; ++i) {
607 const struct screen_functions *fn = screens[i].functions;
609 if (fn->init)
610 fn->init(screen.main_window.w,
611 screen.main_window.cols,
612 screen.main_window.rows);
613 }
615 if (mode_fn->open != NULL)
616 mode_fn->open(&screen, c);
618 /* initialize wreadln */
619 wrln_wgetch = my_wgetch;
620 wrln_max_history_length = 16;
621 }
623 void
624 screen_paint(mpdclient_t *c)
625 {
626 const char *title = NULL;
628 if (mode_fn->get_title != NULL)
629 title = mode_fn->get_title(screen.buf, screen.buf_size);
631 D("screen_paint(%s)\n", title);
632 /* paint the title/header window */
633 if( title )
634 paint_top_window(title, c, 1);
635 else
636 paint_top_window("", c, 1);
638 /* paint the main window */
639 wclear(screen.main_window.w);
640 if (mode_fn->paint != NULL)
641 mode_fn->paint(&screen, c);
643 paint_progress_window(c);
644 paint_status_window(c);
645 screen.painted = 1;
646 wmove(screen.main_window.w, 0, 0);
647 wnoutrefresh(screen.main_window.w);
649 /* tell curses to update */
650 doupdate();
651 }
653 void
654 screen_update(mpdclient_t *c)
655 {
656 static int repeat = -1;
657 static int random_enabled = -1;
658 static int crossfade = -1;
659 static int dbupdate = -1;
661 if( !screen.painted )
662 return screen_paint(c);
664 /* print a message if mpd status has changed */
665 if (c->status != NULL) {
666 if (repeat < 0) {
667 repeat = c->status->repeat;
668 random_enabled = c->status->random;
669 crossfade = c->status->crossfade;
670 dbupdate = c->status->updatingDb;
671 }
673 if (repeat != c->status->repeat)
674 screen_status_printf(c->status->repeat ?
675 _("Repeat is on") :
676 _("Repeat is off"));
678 if (random_enabled != c->status->random)
679 screen_status_printf(c->status->random ?
680 _("Random is on") :
681 _("Random is off"));
683 if (crossfade != c->status->crossfade)
684 screen_status_printf(_("Crossfade %d seconds"), c->status->crossfade);
686 if (dbupdate && dbupdate != c->status->updatingDb) {
687 screen_status_printf(_("Database updated!"));
688 mpdclient_browse_callback(c, BROWSE_DB_UPDATED, NULL);
689 }
691 repeat = c->status->repeat;
692 random_enabled = c->status->random;
693 crossfade = c->status->crossfade;
694 dbupdate = c->status->updatingDb;
695 }
697 /* update title/header window */
698 if (welcome && options.welcome_screen_list &&
699 screen.last_cmd==CMD_NONE &&
700 time(NULL)-screen.start_timestamp <= SCREEN_WELCOME_TIME)
701 paint_top_window("", c, 0);
702 else if (mode_fn->get_title != NULL) {
703 paint_top_window(mode_fn->get_title(screen.buf,screen.buf_size), c, 0);
704 welcome = FALSE;
705 } else
706 paint_top_window("", c, 0);
708 /* update the main window */
709 if (mode_fn->update != NULL)
710 mode_fn->update(&screen, c);
712 /* update progress window */
713 paint_progress_window(c);
715 /* update status window */
716 paint_status_window(c);
718 /* move the cursor to the origin */
719 wmove(screen.main_window.w, 0, 0);
720 wnoutrefresh(screen.main_window.w);
722 /* tell curses to update */
723 doupdate();
724 }
726 void
727 screen_idle(mpdclient_t *c)
728 {
729 if (c->song && seek_id == c->song->id &&
730 (screen.last_cmd == CMD_SEEK_FORWARD ||
731 screen.last_cmd == CMD_SEEK_BACKWARD))
732 mpdclient_cmd_seek(c, seek_id, seek_target_time);
734 screen.last_cmd = CMD_NONE;
735 seek_id = -1;
736 }
738 #ifdef HAVE_GETMOUSE
739 int
740 screen_get_mouse_event(mpdclient_t *c, unsigned long *bstate, int *row)
741 {
742 MEVENT event;
744 /* retreive the mouse event from ncurses */
745 getmouse(&event);
746 D("mouse: id=%d y=%d,x=%d,z=%d\n",event.id,event.y,event.x,event.z);
747 /* calculate the selected row in the list window */
748 *row = event.y - screen.top_window.rows;
749 /* copy button state bits */
750 *bstate = event.bstate;
751 /* if button 2 was pressed switch screen */
752 if (event.bstate & BUTTON2_CLICKED) {
753 screen_cmd(c, CMD_SCREEN_NEXT);
754 return 1;
755 }
757 return 0;
758 }
759 #endif
761 static int
762 screen_client_cmd(mpdclient_t *c, command_t cmd)
763 {
764 if (c->connection == NULL || c->status == NULL)
765 return 0;
767 switch(cmd) {
768 /*
769 case CMD_PLAY:
770 mpdclient_cmd_play(c, MPD_PLAY_AT_BEGINNING);
771 break;
772 */
773 case CMD_PAUSE:
774 mpdclient_cmd_pause(c, !IS_PAUSED(c->status->state));
775 break;
776 case CMD_STOP:
777 mpdclient_cmd_stop(c);
778 break;
779 case CMD_CROP:
780 mpdclient_cmd_crop(c);
781 break;
782 case CMD_SEEK_FORWARD:
783 if (!IS_STOPPED(c->status->state)) {
784 if (c->song && seek_id != c->song->id) {
785 seek_id = c->song->id;
786 seek_target_time = c->status->elapsedTime;
787 }
788 seek_target_time+=options.seek_time;
789 if (seek_target_time < c->status->totalTime)
790 break;
791 seek_target_time = c->status->totalTime;
792 /* seek_target_time=0; */
793 }
794 break;
795 /* fall through... */
796 case CMD_TRACK_NEXT:
797 if (!IS_STOPPED(c->status->state))
798 mpdclient_cmd_next(c);
799 break;
800 case CMD_SEEK_BACKWARD:
801 if (!IS_STOPPED(c->status->state)) {
802 if (seek_id != c->song->id) {
803 seek_id = c->song->id;
804 seek_target_time = c->status->elapsedTime;
805 }
806 seek_target_time-=options.seek_time;
807 if (seek_target_time < 0)
808 seek_target_time=0;
809 }
810 break;
811 case CMD_TRACK_PREVIOUS:
812 if (!IS_STOPPED(c->status->state))
813 mpdclient_cmd_prev(c);
814 break;
815 case CMD_SHUFFLE:
816 if (mpdclient_cmd_shuffle(c) == 0)
817 screen_status_message(_("Shuffled playlist!"));
818 break;
819 case CMD_CLEAR:
820 if (mpdclient_cmd_clear(c) == 0)
821 screen_status_message(_("Cleared playlist!"));
822 break;
823 case CMD_REPEAT:
824 mpdclient_cmd_repeat(c, !c->status->repeat);
825 break;
826 case CMD_RANDOM:
827 mpdclient_cmd_random(c, !c->status->random);
828 break;
829 case CMD_CROSSFADE:
830 if (c->status->crossfade)
831 mpdclient_cmd_crossfade(c, 0);
832 else
833 mpdclient_cmd_crossfade(c, options.crossfade_time);
834 break;
835 case CMD_DB_UPDATE:
836 if (!c->status->updatingDb) {
837 if( mpdclient_cmd_db_update_utf8(c,NULL)==0 )
838 screen_status_printf(_("Database update started!"));
839 } else
840 screen_status_printf(_("Database update running..."));
841 break;
842 case CMD_VOLUME_UP:
843 if( c->status->volume!=MPD_STATUS_NO_VOLUME && c->status->volume<100 )
844 mpdclient_cmd_volume(c, ++c->status->volume);
845 break;
846 case CMD_VOLUME_DOWN:
847 if( c->status->volume!=MPD_STATUS_NO_VOLUME && c->status->volume>0 )
848 mpdclient_cmd_volume(c, --c->status->volume);
849 break;
851 default:
852 return 0;
853 }
855 return 1;
856 }
858 void
859 screen_cmd(mpdclient_t *c, command_t cmd)
860 {
861 screen.input_timestamp = time(NULL);
862 screen.last_cmd = cmd;
863 welcome = FALSE;
865 if (mode_fn->cmd != NULL && mode_fn->cmd(&screen, c, cmd))
866 return;
868 if (screen_client_cmd(c, cmd))
869 return;
871 switch(cmd) {
872 case CMD_TOGGLE_FIND_WRAP:
873 options.find_wrap = !options.find_wrap;
874 screen_status_printf(options.find_wrap ?
875 _("Find mode: Wrapped") :
876 _("Find mode: Normal"));
877 break;
878 case CMD_TOGGLE_AUTOCENTER:
879 options.auto_center = !options.auto_center;
880 screen_status_printf(options.auto_center ?
881 _("Auto center mode: On") :
882 _("Auto center mode: Off"));
883 break;
884 case CMD_SCREEN_UPDATE:
885 screen.painted = 0;
886 break;
887 case CMD_SCREEN_PREVIOUS:
888 screen_next_mode(c, -1);
889 break;
890 case CMD_SCREEN_NEXT:
891 screen_next_mode(c, 1);
892 break;
893 case CMD_SCREEN_PLAY:
894 switch_screen_mode(SCREEN_PLAYLIST_ID, c);
895 break;
896 case CMD_SCREEN_FILE:
897 switch_screen_mode(SCREEN_BROWSE_ID, c);
898 break;
899 case CMD_SCREEN_HELP:
900 switch_screen_mode(SCREEN_HELP_ID, c);
901 break;
902 case CMD_SCREEN_SEARCH:
903 switch_screen_mode(SCREEN_SEARCH_ID, c);
904 break;
905 case CMD_SCREEN_ARTIST:
906 switch_screen_mode(SCREEN_ARTIST_ID, c);
907 break;
908 case CMD_SCREEN_KEYDEF:
909 switch_screen_mode(SCREEN_KEYDEF_ID, c);
910 break;
911 case CMD_SCREEN_LYRICS:
912 switch_screen_mode(SCREEN_LYRICS_ID, c);
913 break;
914 default:
915 break;
916 }
917 }