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_CLOCK_ID 102
47 #define SCREEN_SEARCH_ID 103
48 #define SCREEN_LYRICS_ID 104
52 /* screens */
53 extern struct screen_functions *get_screen_playlist(void);
54 extern struct screen_functions *get_screen_browse(void);
55 extern struct screen_functions *get_screen_help(void);
56 extern struct screen_functions *get_screen_search(void);
57 extern struct screen_functions *get_screen_artist(void);
58 extern struct screen_functions *get_screen_keydef(void);
59 extern struct screen_functions *get_screen_clock(void);
60 extern struct screen_functions *get_screen_lyrics(void);
62 typedef struct screen_functions * (*screen_get_mode_functions_fn_t) (void);
64 static const struct
65 {
66 gint id;
67 const gchar *name;
68 screen_get_mode_functions_fn_t get_mode_functions;
69 } screens[] = {
70 { SCREEN_PLAYLIST_ID, "playlist", get_screen_playlist },
71 { SCREEN_BROWSE_ID, "browse", get_screen_browse },
72 #ifdef ENABLE_ARTIST_SCREEN
73 { SCREEN_ARTIST_ID, "artist", get_screen_artist },
74 #endif
75 { SCREEN_HELP_ID, "help", get_screen_help },
76 #ifdef ENABLE_SEARCH_SCREEN
77 { SCREEN_SEARCH_ID, "search", get_screen_search },
78 #endif
79 #ifdef ENABLE_KEYDEF_SCREEN
80 { SCREEN_KEYDEF_ID, "keydef", get_screen_keydef },
81 #endif
82 #ifdef ENABLE_CLOCK_SCREEN
83 { SCREEN_CLOCK_ID, "clock", get_screen_clock },
84 #endif
85 #ifdef ENABLE_LYRICS_SCREEN
86 { SCREEN_LYRICS_ID, "lyrics", get_screen_lyrics },
87 #endif
88 { G_MAXINT, NULL, NULL }
89 };
91 static gboolean welcome = TRUE;
92 static screen_t *screen = NULL;
93 static struct screen_functions *mode_fn = NULL;
94 static int seek_id = -1;
95 static int seek_target_time = 0;
97 gint
98 screen_get_id(const char *name)
99 {
100 gint i=0;
102 while (screens[i].name) {
103 if (strcmp(name, screens[i].name) == 0)
104 return screens[i].id;
105 i++;
106 }
107 return -1;
108 }
110 static gint
111 lookup_mode(gint id)
112 {
113 gint i=0;
115 while (screens[i].name) {
116 if (screens[i].id == id)
117 return i;
118 i++;
119 }
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 /* close the old mode */
137 if( mode_fn && mode_fn->close )
138 mode_fn->close();
140 /* get functions for the new mode */
141 new_mode = lookup_mode(id);
142 if (new_mode>=0 && screens[new_mode].get_mode_functions) {
143 D("switch_screen(%s)\n", screens[new_mode].name );
144 mode_fn = screens[new_mode].get_mode_functions();
145 screen->mode = new_mode;
146 }
148 screen->painted = 0;
150 /* open the new mode */
151 if (mode_fn && mode_fn->open)
152 mode_fn->open(screen, c);
153 }
155 static void
156 screen_next_mode(mpdclient_t *c, int offset)
157 {
158 int max = g_strv_length(options.screen_list);
159 int current, next;
160 int i;
162 /* find current screen */
163 current = -1;
164 i = 0;
165 while (options.screen_list[i]) {
166 if (strcmp(options.screen_list[i],
167 screens[screen->mode].name) == 0)
168 current = i;
169 i++;
170 }
172 next = current + offset;
173 if (next<0)
174 next = max-1;
175 else if (next>=max)
176 next = 0;
178 D("current mode: %d:%d next:%d\n", current, max, next);
179 switch_screen_mode(screen_get_id(options.screen_list[next]), c);
180 }
182 static void
183 paint_top_window2(const char *header, mpdclient_t *c)
184 {
185 char flags[5];
186 WINDOW *w = screen->top_window.w;
187 char buf[32];
189 if (header[0]) {
190 colors_use(w, COLOR_TITLE_BOLD);
191 mvwaddstr(w, 0, 0, header);
192 } else {
193 colors_use(w, COLOR_TITLE_BOLD);
194 waddstr(w, get_key_names(CMD_SCREEN_HELP, FALSE));
195 colors_use(w, COLOR_TITLE);
196 waddstr(w, _(":Help "));
197 colors_use(w, COLOR_TITLE_BOLD);
198 waddstr(w, get_key_names(CMD_SCREEN_PLAY, FALSE));
199 colors_use(w, COLOR_TITLE);
200 waddstr(w, _(":Playlist "));
201 colors_use(w, COLOR_TITLE_BOLD);
202 waddstr(w, get_key_names(CMD_SCREEN_FILE, FALSE));
203 colors_use(w, COLOR_TITLE);
204 waddstr(w, _(":Browse "));
205 #ifdef ENABLE_ARTIST_SCREEN
206 colors_use(w, COLOR_TITLE_BOLD);
207 waddstr(w, get_key_names(CMD_SCREEN_ARTIST, FALSE));
208 colors_use(w, COLOR_TITLE);
209 waddstr(w, _(":Artist "));
210 #endif
211 #ifdef ENABLE_SEARCH_SCREEN
212 colors_use(w, COLOR_TITLE_BOLD);
213 waddstr(w, get_key_names(CMD_SCREEN_SEARCH, FALSE));
214 colors_use(w, COLOR_TITLE);
215 waddstr(w, _(":Search "));
216 #endif
217 #ifdef ENABLE_LYRICS_SCREEN
218 colors_use(w, COLOR_TITLE_BOLD);
219 waddstr(w, get_key_names(CMD_SCREEN_LYRICS, FALSE));
220 colors_use(w, COLOR_TITLE);
221 waddstr(w, _(":Lyrics "));
222 #endif
223 }
224 if (c->status->volume==MPD_STATUS_NO_VOLUME) {
225 g_snprintf(buf, 32, _("Volume n/a "));
226 } else {
227 g_snprintf(buf, 32, _(" Volume %d%%"), c->status->volume);
228 }
229 colors_use(w, COLOR_TITLE);
230 mvwaddstr(w, 0, screen->top_window.cols-my_strlen(buf), buf);
232 flags[0] = 0;
233 if( c->status->repeat )
234 g_strlcat(flags, "r", sizeof(flags));
235 if( c->status->random )
236 g_strlcat(flags, "z", sizeof(flags));;
237 if( c->status->crossfade )
238 g_strlcat(flags, "x", sizeof(flags));
239 if( c->status->updatingDb )
240 g_strlcat(flags, "U", sizeof(flags));
241 colors_use(w, COLOR_LINE);
242 mvwhline(w, 1, 0, ACS_HLINE, screen->top_window.cols);
243 if (flags[0]) {
244 wmove(w,1,screen->top_window.cols-strlen(flags)-3);
245 waddch(w, '[');
246 colors_use(w, COLOR_LINE_BOLD);
247 waddstr(w, flags);
248 colors_use(w, COLOR_LINE);
249 waddch(w, ']');
250 }
251 wnoutrefresh(w);
252 }
254 static void
255 paint_top_window(const char *header, mpdclient_t *c, int full_repaint)
256 {
257 static int prev_volume = -1;
258 static size_t prev_header_len = -1;
259 WINDOW *w = screen->top_window.w;
261 if (prev_header_len!=my_strlen(header)) {
262 prev_header_len = my_strlen(header);
263 full_repaint = 1;
264 }
266 if (full_repaint) {
267 wmove(w, 0, 0);
268 wclrtoeol(w);
269 }
271 if (prev_volume!=c->status->volume || full_repaint)
272 paint_top_window2(header, c);
273 }
275 static void
276 paint_progress_window(mpdclient_t *c)
277 {
278 double p;
279 int width;
280 int elapsedTime = c->status->elapsedTime;
282 if (c->status==NULL || IS_STOPPED(c->status->state)) {
283 mvwhline(screen->progress_window.w, 0, 0, ACS_HLINE,
284 screen->progress_window.cols);
285 wnoutrefresh(screen->progress_window.w);
286 return;
287 }
289 if (c->song && seek_id == c->song->id)
290 elapsedTime = seek_target_time;
292 p = ((double) elapsedTime) / ((double) c->status->totalTime);
294 width = (int) (p * (double) screen->progress_window.cols);
295 mvwhline(screen->progress_window.w,
296 0, 0,
297 ACS_HLINE,
298 screen->progress_window.cols);
299 whline(screen->progress_window.w, '=', width-1);
300 mvwaddch(screen->progress_window.w, 0, width-1, 'O');
301 wnoutrefresh(screen->progress_window.w);
302 }
304 static void
305 paint_status_window(mpdclient_t *c)
306 {
307 WINDOW *w = screen->status_window.w;
308 mpd_Status *status = c->status;
309 mpd_Song *song = c->song;
310 int elapsedTime = 0;
311 const char *str = NULL;
312 int x = 0;
314 if( time(NULL) - screen->status_timestamp <= SCREEN_STATUS_MESSAGE_TIME )
315 return;
317 wmove(w, 0, 0);
318 wclrtoeol(w);
319 colors_use(w, COLOR_STATUS_BOLD);
321 switch(status->state) {
322 case MPD_STATUS_STATE_PLAY:
323 str = _("Playing:");
324 break;
325 case MPD_STATUS_STATE_PAUSE:
326 str = _("[Paused]");
327 break;
328 case MPD_STATUS_STATE_STOP:
329 default:
330 break;
331 }
333 if (str) {
334 waddstr(w, str);
335 x += my_strlen(str)+1;
336 }
338 /* create time string */
339 memset(screen->buf, 0, screen->buf_size);
340 if (IS_PLAYING(status->state) || IS_PAUSED(status->state)) {
341 if (status->totalTime > 0) {
342 /*checks the conf to see whether to display elapsed or remaining time */
343 if(!strcmp(options.timedisplay_type,"elapsed"))
344 elapsedTime = c->status->elapsedTime;
345 else if(!strcmp(options.timedisplay_type,"remaining"))
346 elapsedTime = (c->status->totalTime - c->status->elapsedTime);
348 if( c->song && seek_id == c->song->id )
349 elapsedTime = seek_target_time;
350 /*write out the time, using hours if time over 60 minutes*/
351 if (c->status->totalTime > 3600) {
352 g_snprintf(screen->buf, screen->buf_size,
353 " [%i:%02i:%02i/%i:%02i:%02i]",
354 elapsedTime/3600, (elapsedTime%3600)/60, elapsedTime%60,
355 status->totalTime/3600, (status->totalTime%3600)/60, status->totalTime%60);
356 } else {
357 g_snprintf(screen->buf, screen->buf_size,
358 " [%i:%02i/%i:%02i]",
359 elapsedTime/60, elapsedTime%60,
360 status->totalTime/60, status->totalTime%60 );
361 }
362 } else {
363 g_snprintf(screen->buf, screen->buf_size,
364 " [%d kbps]", status->bitRate );
365 }
366 } else {
367 time_t timep;
369 time(&timep);
370 strftime(screen->buf, screen->buf_size, "%X ",localtime(&timep));
371 }
373 /* display song */
374 if (IS_PLAYING(status->state) || IS_PAUSED(status->state)) {
375 char songname[MAX_SONGNAME_LENGTH];
376 int width = COLS-x-my_strlen(screen->buf);
378 if (song)
379 strfsong(songname, MAX_SONGNAME_LENGTH, STATUS_FORMAT, song);
380 else
381 songname[0] = '\0';
383 colors_use(w, COLOR_STATUS);
384 /* scroll if the song name is to long */
385 if (options.scroll && my_strlen(songname) > (size_t)width) {
386 static scroll_state_t st = { 0, 0 };
387 char *tmp = strscroll(songname, options.scroll_sep, width, &st);
389 g_strlcpy(songname, tmp, MAX_SONGNAME_LENGTH);
390 g_free(tmp);
391 }
392 //mvwaddnstr(w, 0, x, songname, width);
393 mvwaddstr(w, 0, x, songname);
394 }
396 /* display time string */
397 if (screen->buf[0]) {
398 x = screen->status_window.cols - strlen(screen->buf);
399 colors_use(w, COLOR_STATUS_TIME);
400 mvwaddstr(w, 0, x, screen->buf);
401 }
403 wnoutrefresh(w);
404 }
406 int
407 screen_exit(void)
408 {
409 endwin();
410 if (screen) {
411 gint i;
413 /* close and exit all screens (playlist,browse,help...) */
414 i=0;
415 while (screens[i].get_mode_functions) {
416 struct screen_functions *sf = screens[i].get_mode_functions();
418 if (sf && sf->close)
419 sf->close();
420 if (sf && sf->exit)
421 sf->exit();
423 i++;
424 }
426 string_list_free(screen->find_history);
427 g_free(screen->buf);
428 g_free(screen->findbuf);
430 g_free(screen);
431 screen = NULL;
432 }
433 return 0;
434 }
436 void
437 screen_resize(void)
438 {
439 gint i;
441 D("Resize rows %d->%d, cols %d->%d\n",screen->rows,LINES,screen->cols,COLS);
442 if (COLS<SCREEN_MIN_COLS || LINES<SCREEN_MIN_ROWS) {
443 screen_exit();
444 fprintf(stderr, _("Error: Screen to small!\n"));
445 exit(EXIT_FAILURE);
446 }
448 resizeterm(LINES, COLS);
450 screen->cols = COLS;
451 screen->rows = LINES;
453 /* top window */
454 screen->top_window.cols = screen->cols;
455 wresize(screen->top_window.w, 2, screen->cols);
457 /* main window */
458 screen->main_window.cols = screen->cols;
459 screen->main_window.rows = screen->rows-4;
460 wresize(screen->main_window.w, screen->main_window.rows, screen->cols);
461 wclear(screen->main_window.w);
463 /* progress window */
464 screen->progress_window.cols = screen->cols;
465 wresize(screen->progress_window.w, 1, screen->cols);
466 mvwin(screen->progress_window.w, screen->rows-2, 0);
468 /* status window */
469 screen->status_window.cols = screen->cols;
470 wresize(screen->status_window.w, 1, screen->cols);
471 mvwin(screen->status_window.w, screen->rows-1, 0);
473 screen->buf_size = screen->cols;
474 g_free(screen->buf);
475 screen->buf = g_malloc(screen->cols);
477 /* close and exit all screens (playlist,browse,help...) */
478 i=0;
479 while (screens[i].get_mode_functions) {
480 struct screen_functions *sf = screens[i].get_mode_functions();
482 if (sf && sf->resize)
483 sf->resize(screen->main_window.cols, screen->main_window.rows);
485 i++;
486 }
488 /* ? - without this the cursor becomes visible with aterm & Eterm */
489 curs_set(1);
490 curs_set(0);
492 screen->painted = 0;
493 }
495 void
496 screen_status_message(const char *msg)
497 {
498 WINDOW *w = screen->status_window.w;
500 wmove(w, 0, 0);
501 wclrtoeol(w);
502 colors_use(w, COLOR_STATUS_ALERT);
503 waddstr(w, msg);
504 wnoutrefresh(w);
505 screen->status_timestamp = time(NULL);
506 }
508 void
509 screen_status_printf(const char *format, ...)
510 {
511 char *msg;
512 va_list ap;
514 va_start(ap,format);
515 msg = g_strdup_vprintf(format,ap);
516 va_end(ap);
517 screen_status_message(msg);
518 g_free(msg);
519 }
521 void
522 ncurses_init(void)
523 {
525 /* initialize the curses library */
526 initscr();
527 /* initialize color support */
528 colors_start();
529 /* tell curses not to do NL->CR/NL on output */
530 nonl();
531 /* use raw mode (ignore interrupt,quit,suspend, and flow control ) */
532 #ifdef ENABLE_RAW_MODE
533 // raw();
534 #endif
535 /* don't echo input */
536 noecho();
537 /* set cursor invisible */
538 curs_set(0);
539 /* enable extra keys */
540 keypad(stdscr, TRUE);
541 /* return from getch() without blocking */
542 timeout(SCREEN_TIMEOUT);
543 /* initialize mouse support */
544 #ifdef HAVE_GETMOUSE
545 if( options.enable_mouse )
546 mousemask(ALL_MOUSE_EVENTS, NULL);
547 #endif
549 if( COLS<SCREEN_MIN_COLS || LINES<SCREEN_MIN_ROWS )
550 {
551 fprintf(stderr, _("Error: Screen to small!\n"));
552 exit(EXIT_FAILURE);
553 }
554 screen = g_malloc(sizeof(screen_t));
555 memset(screen, 0, sizeof(screen_t));
556 screen->mode = 0;
557 screen->cols = COLS;
558 screen->rows = LINES;
560 screen->buf = g_malloc(screen->cols);
561 screen->buf_size = screen->cols;
562 screen->findbuf = NULL;
563 screen->painted = 0;
564 screen->start_timestamp = time(NULL);
565 screen->input_timestamp = time(NULL);
566 screen->last_cmd = CMD_NONE;
568 /* create top window */
569 screen->top_window.rows = 2;
570 screen->top_window.cols = screen->cols;
571 screen->top_window.w = newwin(screen->top_window.rows,
572 screen->top_window.cols,
573 0, 0);
574 leaveok(screen->top_window.w, TRUE);
575 keypad(screen->top_window.w, TRUE);
577 /* create main window */
578 screen->main_window.rows = screen->rows-4;
579 screen->main_window.cols = screen->cols;
580 screen->main_window.w = newwin(screen->main_window.rows,
581 screen->main_window.cols,
582 2,
583 0);
585 // leaveok(screen->main_window.w, TRUE); temporary disabled
586 keypad(screen->main_window.w, TRUE);
588 /* create progress window */
589 screen->progress_window.rows = 1;
590 screen->progress_window.cols = screen->cols;
591 screen->progress_window.w = newwin(screen->progress_window.rows,
592 screen->progress_window.cols,
593 screen->rows-2,
594 0);
595 leaveok(screen->progress_window.w, TRUE);
597 /* create status window */
598 screen->status_window.rows = 1;
599 screen->status_window.cols = screen->cols;
600 screen->status_window.w = newwin(screen->status_window.rows,
601 screen->status_window.cols,
602 screen->rows-1,
603 0);
605 leaveok(screen->status_window.w, FALSE);
606 keypad(screen->status_window.w, TRUE);
608 if( options.enable_colors )
609 {
610 /* set background attributes */
611 wbkgd(stdscr, COLOR_PAIR(COLOR_LIST));
612 wbkgd(screen->main_window.w, COLOR_PAIR(COLOR_LIST));
613 wbkgd(screen->top_window.w, COLOR_PAIR(COLOR_TITLE));
614 wbkgd(screen->progress_window.w, COLOR_PAIR(COLOR_PROGRESSBAR));
615 wbkgd(screen->status_window.w, COLOR_PAIR(COLOR_STATUS));
616 colors_use(screen->progress_window.w, COLOR_PROGRESSBAR);
617 }
618 }
620 int
621 screen_init(mpdclient_t *c)
622 {
623 gint i;
625 /* initialize screens */
626 i=0;
627 while( screens[i].get_mode_functions )
628 {
629 struct screen_functions *fn = screens[i].get_mode_functions();
631 if( fn && fn->init )
632 fn->init(screen->main_window.w,
633 screen->main_window.cols,
634 screen->main_window.rows);
636 i++;
637 }
639 #if 0
640 /* broken */
641 mode_fn = NULL;
642 switch_screen_mode(screen_get_id(options.screen_list[0]), c);
643 #else
644 mode_fn = get_screen_playlist();
645 #endif
647 if( mode_fn && mode_fn->open )
648 mode_fn->open(screen, c);
650 /* initialize wreadln */
651 wrln_wgetch = my_wgetch;
652 wrln_max_history_length = 16;
654 return 0;
655 }
657 void
658 screen_paint(mpdclient_t *c)
659 {
660 const char *title = NULL;
662 if (mode_fn && mode_fn->get_title)
663 title = mode_fn->get_title(screen->buf, screen->buf_size);
665 D("screen_paint(%s)\n", title);
666 /* paint the title/header window */
667 if( title )
668 paint_top_window(title, c, 1);
669 else
670 paint_top_window("", c, 1);
672 /* paint the main window */
673 wclear(screen->main_window.w);
674 if( mode_fn && mode_fn->paint )
675 mode_fn->paint(screen, c);
677 paint_progress_window(c);
678 paint_status_window(c);
679 screen->painted = 1;
680 wmove(screen->main_window.w, 0, 0);
681 wnoutrefresh(screen->main_window.w);
683 /* tell curses to update */
684 doupdate();
685 }
687 void
688 screen_update(mpdclient_t *c)
689 {
690 static int repeat = -1;
691 static int random_enabled = -1;
692 static int crossfade = -1;
693 static int dbupdate = -1;
694 list_window_t *lw = NULL;
696 if( !screen->painted )
697 return screen_paint(c);
699 /* print a message if mpd status has changed */
700 if (repeat < 0) {
701 repeat = c->status->repeat;
702 random_enabled = c->status->random;
703 crossfade = c->status->crossfade;
704 dbupdate = c->status->updatingDb;
705 }
707 if (repeat != c->status->repeat)
708 screen_status_printf(c->status->repeat ?
709 _("Repeat is on") :
710 _("Repeat is off"));
712 if (random_enabled != c->status->random)
713 screen_status_printf(c->status->random ?
714 _("Random is on") :
715 _("Random is off"));
717 if (crossfade != c->status->crossfade)
718 screen_status_printf(_("Crossfade %d seconds"), c->status->crossfade);
720 if (dbupdate && dbupdate != c->status->updatingDb) {
721 screen_status_printf(_("Database updated!"));
722 mpdclient_browse_callback(c, BROWSE_DB_UPDATED, NULL);
723 }
725 repeat = c->status->repeat;
726 random_enabled = c->status->random;
727 crossfade = c->status->crossfade;
728 dbupdate = c->status->updatingDb;
730 /* update title/header window */
731 if (welcome && screen->last_cmd==CMD_NONE &&
732 time(NULL)-screen->start_timestamp <= SCREEN_WELCOME_TIME)
733 paint_top_window("", c, 0);
734 else if (mode_fn && mode_fn->get_title) {
735 paint_top_window(mode_fn->get_title(screen->buf,screen->buf_size), c, 0);
736 welcome = FALSE;
737 } else
738 paint_top_window("", c, 0);
740 /* update the main window */
741 if (mode_fn && mode_fn->paint)
742 mode_fn->update(screen, c);
744 if (mode_fn && mode_fn->get_lw)
745 lw = mode_fn->get_lw();
747 /* update progress window */
748 paint_progress_window(c);
750 /* update status window */
751 paint_status_window(c);
753 /* move the cursor to the selected row in the main window */
754 if (lw)
755 wmove(screen->main_window.w, LW_ROW(lw), 0);
756 else
757 wmove(screen->main_window.w, 0, 0);
758 wnoutrefresh(screen->main_window.w);
760 /* tell curses to update */
761 doupdate();
762 }
764 void
765 screen_idle(mpdclient_t *c)
766 {
767 if (c->song && seek_id == c->song->id &&
768 (screen->last_cmd == CMD_SEEK_FORWARD ||
769 screen->last_cmd == CMD_SEEK_BACKWARD))
770 mpdclient_cmd_seek(c, seek_id, seek_target_time);
772 screen->last_cmd = CMD_NONE;
773 seek_id = -1;
774 }
776 #ifdef HAVE_GETMOUSE
777 int
778 screen_get_mouse_event(mpdclient_t *c,
779 list_window_t *lw, int lw_length,
780 unsigned long *bstate, int *row)
781 {
782 MEVENT event;
784 /* retreive the mouse event from ncurses */
785 getmouse(&event);
786 D("mouse: id=%d y=%d,x=%d,z=%d\n",event.id,event.y,event.x,event.z);
787 /* calculate the selected row in the list window */
788 *row = event.y - screen->top_window.rows;
789 /* copy button state bits */
790 *bstate = event.bstate;
791 /* if button 2 was pressed switch screen */
792 if (event.bstate & BUTTON2_CLICKED) {
793 screen_cmd(c, CMD_SCREEN_NEXT);
794 return 1;
795 }
797 /* if the even occured above the list window move up */
798 if (*row < 0 && lw) {
799 if (event.bstate & BUTTON3_CLICKED)
800 list_window_first(lw);
801 else
802 list_window_previous_page(lw);
803 return 1;
804 }
806 /* if the even occured below the list window move down */
807 if ((unsigned)*row >= lw->rows && lw) {
808 if (event.bstate & BUTTON3_CLICKED)
809 list_window_last(lw, lw_length);
810 else
811 list_window_next_page(lw, lw_length);
812 return 1;
813 }
815 return 0;
816 }
817 #endif
819 void
820 screen_cmd(mpdclient_t *c, command_t cmd)
821 {
822 screen->input_timestamp = time(NULL);
823 screen->last_cmd = cmd;
824 welcome = FALSE;
826 if( mode_fn && mode_fn->cmd && mode_fn->cmd(screen, c, cmd) )
827 return;
829 switch(cmd) {
830 case CMD_PLAY:
831 mpdclient_cmd_play(c, MPD_PLAY_AT_BEGINNING);
832 break;
833 case CMD_PAUSE:
834 mpdclient_cmd_pause(c, !IS_PAUSED(c->status->state));
835 break;
836 case CMD_STOP:
837 mpdclient_cmd_stop(c);
838 break;
839 case CMD_SEEK_FORWARD:
840 if (!IS_STOPPED(c->status->state)) {
841 if (c->song && seek_id != c->song->id) {
842 seek_id = c->song->id;
843 seek_target_time = c->status->elapsedTime;
844 }
845 seek_target_time+=options.seek_time;
846 if (seek_target_time < c->status->totalTime)
847 break;
848 seek_target_time = c->status->totalTime;
849 /* seek_target_time=0; */
850 }
851 break;
852 /* fall through... */
853 case CMD_TRACK_NEXT:
854 if (!IS_STOPPED(c->status->state))
855 mpdclient_cmd_next(c);
856 break;
857 case CMD_SEEK_BACKWARD:
858 if (!IS_STOPPED(c->status->state)) {
859 if (seek_id != c->song->id) {
860 seek_id = c->song->id;
861 seek_target_time = c->status->elapsedTime;
862 }
863 seek_target_time-=options.seek_time;
864 if (seek_target_time < 0)
865 seek_target_time=0;
866 }
867 break;
868 case CMD_TRACK_PREVIOUS:
869 if (!IS_STOPPED(c->status->state))
870 mpdclient_cmd_prev(c);
871 break;
872 case CMD_SHUFFLE:
873 if (mpdclient_cmd_shuffle(c) == 0)
874 screen_status_message(_("Shuffled playlist!"));
875 break;
876 case CMD_CLEAR:
877 if (mpdclient_cmd_clear(c) == 0)
878 screen_status_message(_("Cleared playlist!"));
879 break;
880 case CMD_REPEAT:
881 mpdclient_cmd_repeat(c, !c->status->repeat);
882 break;
883 case CMD_RANDOM:
884 mpdclient_cmd_random(c, !c->status->random);
885 break;
886 case CMD_CROSSFADE:
887 if (c->status->crossfade)
888 mpdclient_cmd_crossfade(c, 0);
889 else
890 mpdclient_cmd_crossfade(c, options.crossfade_time);
891 break;
892 case CMD_DB_UPDATE:
893 if (!c->status->updatingDb) {
894 if( mpdclient_cmd_db_update_utf8(c,NULL)==0 )
895 screen_status_printf(_("Database update started!"));
896 } else
897 screen_status_printf(_("Database update running..."));
898 break;
899 case CMD_VOLUME_UP:
900 if( c->status->volume!=MPD_STATUS_NO_VOLUME && c->status->volume<100 )
901 mpdclient_cmd_volume(c, ++c->status->volume);
902 break;
903 case CMD_VOLUME_DOWN:
904 if( c->status->volume!=MPD_STATUS_NO_VOLUME && c->status->volume>0 )
905 mpdclient_cmd_volume(c, --c->status->volume);
906 break;
907 case CMD_TOGGLE_FIND_WRAP:
908 options.find_wrap = !options.find_wrap;
909 screen_status_printf(options.find_wrap ?
910 _("Find mode: Wrapped") :
911 _("Find mode: Normal"));
912 break;
913 case CMD_TOGGLE_AUTOCENTER:
914 options.auto_center = !options.auto_center;
915 screen_status_printf(options.auto_center ?
916 _("Auto center mode: On") :
917 _("Auto center mode: Off"));
918 break;
919 case CMD_SCREEN_UPDATE:
920 screen->painted = 0;
921 break;
922 case CMD_SCREEN_PREVIOUS:
923 screen_next_mode(c, -1);
924 break;
925 case CMD_SCREEN_NEXT:
926 screen_next_mode(c, 1);
927 break;
928 case CMD_SCREEN_PLAY:
929 switch_screen_mode(SCREEN_PLAYLIST_ID, c);
930 break;
931 case CMD_SCREEN_FILE:
932 switch_screen_mode(SCREEN_BROWSE_ID, c);
933 break;
934 case CMD_SCREEN_HELP:
935 switch_screen_mode(SCREEN_HELP_ID, c);
936 break;
937 case CMD_SCREEN_SEARCH:
938 switch_screen_mode(SCREEN_SEARCH_ID, c);
939 break;
940 case CMD_SCREEN_ARTIST:
941 switch_screen_mode(SCREEN_ARTIST_ID, c);
942 break;
943 case CMD_SCREEN_KEYDEF:
944 switch_screen_mode(SCREEN_KEYDEF_ID, c);
945 break;
946 case CMD_SCREEN_CLOCK:
947 switch_screen_mode(SCREEN_CLOCK_ID, c);
948 break;
949 case CMD_SCREEN_LYRICS:
950 switch_screen_mode(SCREEN_LYRICS_ID, c);
951 break;
952 case CMD_QUIT:
953 exit(EXIT_SUCCESS);
954 default:
955 break;
956 }
957 }