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