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 int
550 screen_init(mpdclient_t *c)
551 {
552 gint i;
554 /* initialize the curses library */
555 initscr();
556 /* initialize color support */
557 colors_start();
558 /* tell curses not to do NL->CR/NL on output */
559 nonl();
560 /* use raw mode (ignore interrupt,quit,suspend, and flow control ) */
561 #ifdef ENABLE_RAW_MODE
562 raw();
563 #endif
564 /* don't echo input */
565 noecho();
566 /* set cursor invisible */
567 curs_set(0);
568 /* enable extra keys */
569 keypad(stdscr, TRUE);
570 /* return from getch() without blocking */
571 timeout(SCREEN_TIMEOUT);
572 /* initialize mouse support */
573 #ifdef HAVE_GETMOUSE
574 if( options.enable_mouse )
575 mousemask(ALL_MOUSE_EVENTS, NULL);
576 #endif
578 if( COLS<SCREEN_MIN_COLS || LINES<SCREEN_MIN_ROWS )
579 {
580 fprintf(stderr, _("Error: Screen to small!\n"));
581 exit(EXIT_FAILURE);
582 }
584 screen = g_malloc(sizeof(screen_t));
585 memset(screen, 0, sizeof(screen_t));
586 screen->mode = 0;
587 screen->cols = COLS;
588 screen->rows = LINES;
589 screen->buf = g_malloc(screen->cols);
590 screen->buf_size = screen->cols;
591 screen->findbuf = NULL;
592 screen->painted = 0;
593 screen->start_timestamp = time(NULL);
594 screen->input_timestamp = time(NULL);
595 screen->last_cmd = CMD_NONE;
597 /* create top window */
598 screen->top_window.rows = 2;
599 screen->top_window.cols = screen->cols;
600 screen->top_window.w = newwin(screen->top_window.rows,
601 screen->top_window.cols,
602 0, 0);
603 leaveok(screen->top_window.w, TRUE);
604 keypad(screen->top_window.w, TRUE);
606 /* create main window */
607 screen->main_window.rows = screen->rows-4;
608 screen->main_window.cols = screen->cols;
609 screen->main_window.w = newwin(screen->main_window.rows,
610 screen->main_window.cols,
611 2,
612 0);
614 // leaveok(screen->main_window.w, TRUE); temporary disabled
615 keypad(screen->main_window.w, TRUE);
617 /* create progress window */
618 screen->progress_window.rows = 1;
619 screen->progress_window.cols = screen->cols;
620 screen->progress_window.w = newwin(screen->progress_window.rows,
621 screen->progress_window.cols,
622 screen->rows-2,
623 0);
624 leaveok(screen->progress_window.w, TRUE);
626 /* create status window */
627 screen->status_window.rows = 1;
628 screen->status_window.cols = screen->cols;
629 screen->status_window.w = newwin(screen->status_window.rows,
630 screen->status_window.cols,
631 screen->rows-1,
632 0);
634 leaveok(screen->status_window.w, FALSE);
635 keypad(screen->status_window.w, TRUE);
637 if( options.enable_colors )
638 {
639 /* set background attributes */
640 wbkgd(stdscr, COLOR_PAIR(COLOR_LIST));
641 wbkgd(screen->main_window.w, COLOR_PAIR(COLOR_LIST));
642 wbkgd(screen->top_window.w, COLOR_PAIR(COLOR_TITLE));
643 wbkgd(screen->progress_window.w, COLOR_PAIR(COLOR_PROGRESSBAR));
644 wbkgd(screen->status_window.w, COLOR_PAIR(COLOR_STATUS));
645 colors_use(screen->progress_window.w, COLOR_PROGRESSBAR);
646 }
648 /* initialize screens */
649 i=0;
650 while( screens[i].get_mode_functions )
651 {
652 screen_functions_t *fn = screens[i].get_mode_functions();
654 if( fn && fn->init )
655 fn->init(screen->main_window.w,
656 screen->main_window.cols,
657 screen->main_window.rows);
659 i++;
660 }
662 #if 0
663 /* broken */
664 mode_fn = NULL;
665 switch_screen_mode(screen_get_id(options.screen_list[0]), c);
666 #else
667 mode_fn = get_screen_playlist();
668 #endif
670 if( mode_fn && mode_fn->open )
671 mode_fn->open(screen, c);
673 /* initialize wreadln */
674 wrln_wgetch = my_wgetch;
675 wrln_max_history_length = 16;
677 return 0;
678 }
680 void
681 screen_paint(mpdclient_t *c)
682 {
683 char *title = NULL;
685 if( mode_fn && mode_fn->get_title )
686 title = mode_fn->get_title(screen->buf,screen->buf_size);
688 D("screen_paint(%s)\n", title);
689 /* paint the title/header window */
690 if( title )
691 paint_top_window(title, c, 1);
692 else
693 paint_top_window("", c, 1);
695 /* paint the main window */
696 wclear(screen->main_window.w);
697 if( mode_fn && mode_fn->paint )
698 mode_fn->paint(screen, c);
700 paint_progress_window(c);
701 paint_status_window(c);
702 screen->painted = 1;
703 wmove(screen->main_window.w, 0, 0);
704 wnoutrefresh(screen->main_window.w);
706 /* tell curses to update */
707 doupdate();
708 }
710 void
711 screen_update(mpdclient_t *c)
712 {
713 static int repeat = -1;
714 static int random = -1;
715 static int crossfade = -1;
716 static int dbupdate = -1;
717 list_window_t *lw = NULL;
719 if( !screen->painted )
720 return screen_paint(c);
722 /* print a message if mpd status has changed */
723 if( repeat<0 )
724 {
725 repeat = c->status->repeat;
726 random = c->status->random;
727 crossfade = c->status->crossfade;
728 dbupdate = c->status->updatingDb;
729 }
730 if( repeat != c->status->repeat )
731 screen_status_printf(c->status->repeat ?
732 _("Repeat is on") :
733 _("Repeat is off"));
734 if( random != c->status->random )
735 screen_status_printf(c->status->random ?
736 _("Random is on") :
737 _("Random is off"));
739 if( crossfade != c->status->crossfade )
740 screen_status_printf(_("Crossfade %d seconds"), c->status->crossfade);
741 if( dbupdate && dbupdate != c->status->updatingDb )
742 {
743 screen_status_printf(_("Database updated!"));
744 mpdclient_browse_callback(c, BROWSE_DB_UPDATED, NULL);
745 }
747 repeat = c->status->repeat;
748 random = c->status->random;
749 crossfade = c->status->crossfade;
750 dbupdate = c->status->updatingDb;
752 /* update title/header window */
753 if( welcome && screen->last_cmd==CMD_NONE &&
754 time(NULL)-screen->start_timestamp <= SCREEN_WELCOME_TIME)
755 paint_top_window("", c, 0);
756 else if( mode_fn && mode_fn->get_title )
757 {
758 paint_top_window(mode_fn->get_title(screen->buf,screen->buf_size), c, 0);
759 welcome = FALSE;
760 }
761 else
762 paint_top_window("", c, 0);
764 /* update the main window */
765 if( mode_fn && mode_fn->paint )
766 mode_fn->update(screen, c);
768 if( mode_fn && mode_fn->get_lw )
769 lw = mode_fn->get_lw();
771 /* update progress window */
772 paint_progress_window(c);
774 /* update status window */
775 paint_status_window(c);
777 /* move the cursor to the selected row in the main window */
778 if( lw )
779 wmove(screen->main_window.w, LW_ROW(lw), 0);
780 else
781 wmove(screen->main_window.w, 0, 0);
782 wnoutrefresh(screen->main_window.w);
784 /* tell curses to update */
785 doupdate();
786 }
788 void
789 screen_idle(mpdclient_t *c)
790 {
791 if( c->song && seek_id == c->song->id &&
792 (screen->last_cmd == CMD_SEEK_FORWARD ||
793 screen->last_cmd == CMD_SEEK_BACKWARD) )
794 {
795 mpdclient_cmd_seek(c, seek_id, seek_target_time);
796 }
798 screen->last_cmd = CMD_NONE;
799 seek_id = -1;
800 }
802 #ifdef HAVE_GETMOUSE
803 int
804 screen_get_mouse_event(mpdclient_t *c,
805 list_window_t *lw, int lw_length,
806 unsigned long *bstate, int *row)
807 {
808 MEVENT event;
810 /* retreive the mouse event from ncurses */
811 getmouse(&event);
812 D("mouse: id=%d y=%d,x=%d,z=%d\n",event.id,event.y,event.x,event.z);
813 /* calculate the selected row in the list window */
814 *row = event.y - screen->top_window.rows;
815 /* copy button state bits */
816 *bstate = event.bstate;
817 /* if button 2 was pressed switch screen */
818 if( event.bstate & BUTTON2_CLICKED )
819 {
820 screen_cmd(c, CMD_SCREEN_NEXT);
821 return 1;
822 }
823 /* if the even occured above the list window move up */
824 if( *row<0 && lw )
825 {
826 if( event.bstate & BUTTON3_CLICKED )
827 list_window_first(lw);
828 else
829 list_window_previous_page(lw);
830 return 1;
831 }
832 /* if the even occured below the list window move down */
833 if( *row>=lw->rows && lw )
834 {
835 if( event.bstate & BUTTON3_CLICKED )
836 list_window_last(lw, lw_length);
837 else
838 list_window_next_page(lw, lw_length);
839 return 1;
840 }
841 return 0;
842 }
843 #endif
845 void
846 screen_cmd(mpdclient_t *c, command_t cmd)
847 {
848 screen->input_timestamp = time(NULL);
849 screen->last_cmd = cmd;
850 welcome = FALSE;
852 if( mode_fn && mode_fn->cmd && mode_fn->cmd(screen, c, cmd) )
853 return;
855 switch(cmd)
856 {
857 case CMD_PLAY:
858 mpdclient_cmd_play(c, MPD_PLAY_AT_BEGINNING);
859 break;
860 case CMD_PAUSE:
861 mpdclient_cmd_pause(c, !IS_PAUSED(c->status->state));
862 break;
863 case CMD_STOP:
864 mpdclient_cmd_stop(c);
865 break;
866 case CMD_SEEK_FORWARD:
867 if( !IS_STOPPED(c->status->state) )
868 {
869 if( c->song && seek_id != c->song->id )
870 {
871 seek_id = c->song->id;
872 seek_target_time = c->status->elapsedTime;
873 }
874 seek_target_time+=options.seek_time;
875 if( seek_target_time < c->status->totalTime )
876 break;
877 seek_target_time = c->status->totalTime;
878 /* seek_target_time=0; */
879 }
880 break;
881 /* fall through... */
882 case CMD_TRACK_NEXT:
883 if( !IS_STOPPED(c->status->state) )
884 mpdclient_cmd_next(c);
885 break;
886 case CMD_SEEK_BACKWARD:
887 if( !IS_STOPPED(c->status->state) )
888 {
889 if( seek_id != c->song->id )
890 {
891 seek_id = c->song->id;
892 seek_target_time = c->status->elapsedTime;
893 }
894 seek_target_time-=options.seek_time;
895 if( seek_target_time < 0 )
896 seek_target_time=0;
897 }
898 break;
899 case CMD_TRACK_PREVIOUS:
900 if( !IS_STOPPED(c->status->state) )
901 mpdclient_cmd_prev(c);
902 break;
903 case CMD_SHUFFLE:
904 if( mpdclient_cmd_shuffle(c) == 0 )
905 screen_status_message(_("Shuffled playlist!"));
906 break;
907 case CMD_CLEAR:
908 if( mpdclient_cmd_clear(c) == 0 )
909 screen_status_message(_("Cleared playlist!"));
910 break;
911 case CMD_REPEAT:
912 mpdclient_cmd_repeat(c, !c->status->repeat);
913 break;
914 case CMD_RANDOM:
915 mpdclient_cmd_random(c, !c->status->random);
916 break;
917 case CMD_CROSSFADE:
918 if( c->status->crossfade )
919 mpdclient_cmd_crossfade(c, 0);
920 else
921 mpdclient_cmd_crossfade(c, options.crossfade_time);
922 break;
923 case CMD_DB_UPDATE:
924 if( !c->status->updatingDb )
925 {
926 if( mpdclient_cmd_db_update_utf8(c,NULL)==0 )
927 screen_status_printf(_("Database update started!"));
928 }
929 else
930 screen_status_printf(_("Database update running..."));
931 break;
932 case CMD_VOLUME_UP:
933 if( c->status->volume!=MPD_STATUS_NO_VOLUME && c->status->volume<100 )
934 mpdclient_cmd_volume(c, ++c->status->volume);
935 break;
936 case CMD_VOLUME_DOWN:
937 if( c->status->volume!=MPD_STATUS_NO_VOLUME && c->status->volume>0 )
938 mpdclient_cmd_volume(c, --c->status->volume);
939 break;
940 case CMD_TOGGLE_FIND_WRAP:
941 options.find_wrap = !options.find_wrap;
942 screen_status_printf(options.find_wrap ?
943 _("Find mode: Wrapped") :
944 _("Find mode: Normal"));
945 break;
946 case CMD_TOGGLE_AUTOCENTER:
947 options.auto_center = !options.auto_center;
948 screen_status_printf(options.auto_center ?
949 _("Auto center mode: On") :
950 _("Auto center mode: Off"));
951 break;
952 case CMD_SCREEN_UPDATE:
953 screen->painted = 0;
954 break;
955 case CMD_SCREEN_PREVIOUS:
956 screen_next_mode(c, -1);
957 break;
958 case CMD_SCREEN_NEXT:
959 screen_next_mode(c, 1);
960 break;
961 case CMD_SCREEN_PLAY:
962 switch_screen_mode(SCREEN_PLAYLIST_ID, c);
963 break;
964 case CMD_SCREEN_FILE:
965 switch_screen_mode(SCREEN_BROWSE_ID, c);
966 break;
967 case CMD_SCREEN_HELP:
968 switch_screen_mode(SCREEN_HELP_ID, c);
969 break;
970 case CMD_SCREEN_SEARCH:
971 switch_screen_mode(SCREEN_SEARCH_ID, c);
972 break;
973 case CMD_SCREEN_ARTIST:
974 switch_screen_mode(SCREEN_ARTIST_ID, c);
975 break;
976 case CMD_SCREEN_KEYDEF:
977 switch_screen_mode(SCREEN_KEYDEF_ID, c);
978 break;
979 case CMD_SCREEN_CLOCK:
980 switch_screen_mode(SCREEN_CLOCK_ID, c);
981 break;
982 case CMD_SCREEN_LYRICS:
983 switch_screen_mode(SCREEN_LYRICS_ID, c);
984 break;
985 case CMD_QUIT:
986 exit(EXIT_SUCCESS);
987 default:
988 break;
989 }
990 }