797eae2ac9a2d69fbb8801b7f25152cd5b827730
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 const struct screen_functions screen_playlist;
54 extern const struct screen_functions screen_browse;
55 #ifdef ENABLE_ARTIST_SCREEN
56 extern const struct screen_functions screen_artist;
57 #endif
58 extern const struct screen_functions screen_help;
59 #ifdef ENABLE_SEARCH_SCREEN
60 extern const struct screen_functions screen_search;
61 #endif
62 #ifdef ENABLE_KEYDEF_SCREEN
63 extern const struct screen_functions screen_keydef;
64 #endif
65 #ifdef ENABLE_CLOCK_SCREEN
66 extern const struct screen_functions screen_clock;
67 #endif
68 extern const struct screen_functions screen_lyrics;
70 typedef struct screen_functions * (*screen_get_mode_functions_fn_t) (void);
72 static const struct
73 {
74 gint id;
75 const gchar *name;
76 const struct screen_functions *functions;
77 } screens[] = {
78 { SCREEN_PLAYLIST_ID, "playlist", &screen_playlist },
79 { SCREEN_BROWSE_ID, "browse", &screen_browse },
80 #ifdef ENABLE_ARTIST_SCREEN
81 { SCREEN_ARTIST_ID, "artist", &screen_artist },
82 #endif
83 { SCREEN_HELP_ID, "help", &screen_help },
84 #ifdef ENABLE_SEARCH_SCREEN
85 { SCREEN_SEARCH_ID, "search", &screen_search },
86 #endif
87 #ifdef ENABLE_KEYDEF_SCREEN
88 { SCREEN_KEYDEF_ID, "keydef", &screen_keydef },
89 #endif
90 #ifdef ENABLE_CLOCK_SCREEN
91 { SCREEN_CLOCK_ID, "clock", &screen_clock },
92 #endif
93 #ifdef ENABLE_LYRICS_SCREEN
94 { SCREEN_LYRICS_ID, "lyrics", &screen_lyrics },
95 #endif
96 { G_MAXINT, NULL, NULL }
97 };
99 static gboolean welcome = TRUE;
100 static screen_t *screen = NULL;
101 static const struct screen_functions *mode_fn = &screen_playlist;
102 static int seek_id = -1;
103 static int seek_target_time = 0;
105 gint
106 screen_get_id(const char *name)
107 {
108 gint i=0;
110 while (screens[i].name) {
111 if (strcmp(name, screens[i].name) == 0)
112 return screens[i].id;
113 i++;
114 }
115 return -1;
116 }
118 static gint
119 lookup_mode(gint id)
120 {
121 gint i=0;
123 while (screens[i].name) {
124 if (screens[i].id == id)
125 return i;
126 i++;
127 }
128 return -1;
129 }
131 gint get_cur_mode_id(void)
132 {
133 return screens[screen->mode].id;
134 }
136 static void
137 switch_screen_mode(gint id, mpdclient_t *c)
138 {
139 gint new_mode;
141 if( id == screens[screen->mode].id )
142 return;
144 /* close the old mode */
145 if (mode_fn->close != NULL)
146 mode_fn->close();
148 /* get functions for the new mode */
149 new_mode = lookup_mode(id);
150 if (new_mode >= 0 && screens[new_mode].functions) {
151 D("switch_screen(%s)\n", screens[new_mode].name );
152 mode_fn = screens[new_mode].functions;
153 screen->mode = new_mode;
154 }
156 screen->painted = 0;
158 /* open the new mode */
159 if (mode_fn->open != NULL)
160 mode_fn->open(screen, c);
161 }
163 static void
164 screen_next_mode(mpdclient_t *c, int offset)
165 {
166 int max = g_strv_length(options.screen_list);
167 int current, next;
168 int i;
170 /* find current screen */
171 current = -1;
172 i = 0;
173 while (options.screen_list[i]) {
174 if (strcmp(options.screen_list[i],
175 screens[screen->mode].name) == 0)
176 current = i;
177 i++;
178 }
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_window2(const char *header, mpdclient_t *c)
192 {
193 char flags[5];
194 WINDOW *w = screen->top_window.w;
195 char buf[32];
197 if (header[0]) {
198 colors_use(w, COLOR_TITLE_BOLD);
199 mvwaddstr(w, 0, 0, header);
200 } else {
201 colors_use(w, COLOR_TITLE_BOLD);
202 waddstr(w, get_key_names(CMD_SCREEN_HELP, FALSE));
203 colors_use(w, COLOR_TITLE);
204 waddstr(w, _(":Help "));
205 colors_use(w, COLOR_TITLE_BOLD);
206 waddstr(w, get_key_names(CMD_SCREEN_PLAY, FALSE));
207 colors_use(w, COLOR_TITLE);
208 waddstr(w, _(":Playlist "));
209 colors_use(w, COLOR_TITLE_BOLD);
210 waddstr(w, get_key_names(CMD_SCREEN_FILE, FALSE));
211 colors_use(w, COLOR_TITLE);
212 waddstr(w, _(":Browse "));
213 #ifdef ENABLE_ARTIST_SCREEN
214 colors_use(w, COLOR_TITLE_BOLD);
215 waddstr(w, get_key_names(CMD_SCREEN_ARTIST, FALSE));
216 colors_use(w, COLOR_TITLE);
217 waddstr(w, _(":Artist "));
218 #endif
219 #ifdef ENABLE_SEARCH_SCREEN
220 colors_use(w, COLOR_TITLE_BOLD);
221 waddstr(w, get_key_names(CMD_SCREEN_SEARCH, FALSE));
222 colors_use(w, COLOR_TITLE);
223 waddstr(w, _(":Search "));
224 #endif
225 #ifdef ENABLE_LYRICS_SCREEN
226 colors_use(w, COLOR_TITLE_BOLD);
227 waddstr(w, get_key_names(CMD_SCREEN_LYRICS, FALSE));
228 colors_use(w, COLOR_TITLE);
229 waddstr(w, _(":Lyrics "));
230 #endif
231 }
232 if (c->status->volume==MPD_STATUS_NO_VOLUME) {
233 g_snprintf(buf, 32, _("Volume n/a "));
234 } else {
235 g_snprintf(buf, 32, _(" Volume %d%%"), c->status->volume);
236 }
237 colors_use(w, COLOR_TITLE);
238 mvwaddstr(w, 0, screen->top_window.cols-my_strlen(buf), buf);
240 flags[0] = 0;
241 if( c->status->repeat )
242 g_strlcat(flags, "r", sizeof(flags));
243 if( c->status->random )
244 g_strlcat(flags, "z", sizeof(flags));;
245 if( c->status->crossfade )
246 g_strlcat(flags, "x", sizeof(flags));
247 if( c->status->updatingDb )
248 g_strlcat(flags, "U", sizeof(flags));
249 colors_use(w, COLOR_LINE);
250 mvwhline(w, 1, 0, ACS_HLINE, screen->top_window.cols);
251 if (flags[0]) {
252 wmove(w,1,screen->top_window.cols-strlen(flags)-3);
253 waddch(w, '[');
254 colors_use(w, COLOR_LINE_BOLD);
255 waddstr(w, flags);
256 colors_use(w, COLOR_LINE);
257 waddch(w, ']');
258 }
259 wnoutrefresh(w);
260 }
262 static void
263 paint_top_window(const char *header, mpdclient_t *c, int full_repaint)
264 {
265 static int prev_volume = -1;
266 static size_t prev_header_len = -1;
267 WINDOW *w = screen->top_window.w;
269 if (prev_header_len!=my_strlen(header)) {
270 prev_header_len = my_strlen(header);
271 full_repaint = 1;
272 }
274 if (full_repaint) {
275 wmove(w, 0, 0);
276 wclrtoeol(w);
277 }
279 if (prev_volume!=c->status->volume || full_repaint)
280 paint_top_window2(header, c);
281 }
283 static void
284 paint_progress_window(mpdclient_t *c)
285 {
286 double p;
287 int width;
288 int elapsedTime = c->status->elapsedTime;
290 if (c->status==NULL || IS_STOPPED(c->status->state)) {
291 mvwhline(screen->progress_window.w, 0, 0, ACS_HLINE,
292 screen->progress_window.cols);
293 wnoutrefresh(screen->progress_window.w);
294 return;
295 }
297 if (c->song && seek_id == c->song->id)
298 elapsedTime = seek_target_time;
300 p = ((double) elapsedTime) / ((double) c->status->totalTime);
302 width = (int) (p * (double) screen->progress_window.cols);
303 mvwhline(screen->progress_window.w,
304 0, 0,
305 ACS_HLINE,
306 screen->progress_window.cols);
307 whline(screen->progress_window.w, '=', width-1);
308 mvwaddch(screen->progress_window.w, 0, width-1, 'O');
309 wnoutrefresh(screen->progress_window.w);
310 }
312 static void
313 paint_status_window(mpdclient_t *c)
314 {
315 WINDOW *w = screen->status_window.w;
316 mpd_Status *status = c->status;
317 mpd_Song *song = c->song;
318 int elapsedTime = 0;
319 const char *str = NULL;
320 int x = 0;
322 if( time(NULL) - screen->status_timestamp <= SCREEN_STATUS_MESSAGE_TIME )
323 return;
325 wmove(w, 0, 0);
326 wclrtoeol(w);
327 colors_use(w, COLOR_STATUS_BOLD);
329 switch(status->state) {
330 case MPD_STATUS_STATE_PLAY:
331 str = _("Playing:");
332 break;
333 case MPD_STATUS_STATE_PAUSE:
334 str = _("[Paused]");
335 break;
336 case MPD_STATUS_STATE_STOP:
337 default:
338 break;
339 }
341 if (str) {
342 waddstr(w, str);
343 x += my_strlen(str)+1;
344 }
346 /* create time string */
347 memset(screen->buf, 0, screen->buf_size);
348 if (IS_PLAYING(status->state) || IS_PAUSED(status->state)) {
349 if (status->totalTime > 0) {
350 /*checks the conf to see whether to display elapsed or remaining time */
351 if(!strcmp(options.timedisplay_type,"elapsed"))
352 elapsedTime = c->status->elapsedTime;
353 else if(!strcmp(options.timedisplay_type,"remaining"))
354 elapsedTime = (c->status->totalTime - c->status->elapsedTime);
356 if( c->song && seek_id == c->song->id )
357 elapsedTime = seek_target_time;
358 /*write out the time, using hours if time over 60 minutes*/
359 if (c->status->totalTime > 3600) {
360 g_snprintf(screen->buf, screen->buf_size,
361 " [%i:%02i:%02i/%i:%02i:%02i]",
362 elapsedTime/3600, (elapsedTime%3600)/60, elapsedTime%60,
363 status->totalTime/3600, (status->totalTime%3600)/60, status->totalTime%60);
364 } else {
365 g_snprintf(screen->buf, screen->buf_size,
366 " [%i:%02i/%i:%02i]",
367 elapsedTime/60, elapsedTime%60,
368 status->totalTime/60, status->totalTime%60 );
369 }
370 } else {
371 g_snprintf(screen->buf, screen->buf_size,
372 " [%d kbps]", status->bitRate );
373 }
374 } else {
375 time_t timep;
377 time(&timep);
378 strftime(screen->buf, screen->buf_size, "%X ",localtime(&timep));
379 }
381 /* display song */
382 if (IS_PLAYING(status->state) || IS_PAUSED(status->state)) {
383 char songname[MAX_SONGNAME_LENGTH];
384 int width = COLS-x-my_strlen(screen->buf);
386 if (song)
387 strfsong(songname, MAX_SONGNAME_LENGTH, STATUS_FORMAT, song);
388 else
389 songname[0] = '\0';
391 colors_use(w, COLOR_STATUS);
392 /* scroll if the song name is to long */
393 if (options.scroll && my_strlen(songname) > (size_t)width) {
394 static scroll_state_t st = { 0, 0 };
395 char *tmp = strscroll(songname, options.scroll_sep, width, &st);
397 g_strlcpy(songname, tmp, MAX_SONGNAME_LENGTH);
398 g_free(tmp);
399 }
400 //mvwaddnstr(w, 0, x, songname, width);
401 mvwaddstr(w, 0, x, songname);
402 }
404 /* display time string */
405 if (screen->buf[0]) {
406 x = screen->status_window.cols - strlen(screen->buf);
407 colors_use(w, COLOR_STATUS_TIME);
408 mvwaddstr(w, 0, x, screen->buf);
409 }
411 wnoutrefresh(w);
412 }
414 int
415 screen_exit(void)
416 {
417 endwin();
418 if (screen) {
419 gint i;
421 /* close and exit all screens (playlist,browse,help...) */
422 i=0;
423 while (screens[i].functions) {
424 const struct screen_functions *sf = screens[i].functions;
426 if (sf->close)
427 sf->close();
428 if (sf->exit)
429 sf->exit();
431 i++;
432 }
434 string_list_free(screen->find_history);
435 g_free(screen->buf);
436 g_free(screen->findbuf);
438 g_free(screen);
439 screen = NULL;
440 }
441 return 0;
442 }
444 void
445 screen_resize(void)
446 {
447 gint i;
449 D("Resize rows %d->%d, cols %d->%d\n",screen->rows,LINES,screen->cols,COLS);
450 if (COLS<SCREEN_MIN_COLS || LINES<SCREEN_MIN_ROWS) {
451 screen_exit();
452 fprintf(stderr, _("Error: Screen to small!\n"));
453 exit(EXIT_FAILURE);
454 }
456 resizeterm(LINES, COLS);
458 screen->cols = COLS;
459 screen->rows = LINES;
461 /* top window */
462 screen->top_window.cols = screen->cols;
463 wresize(screen->top_window.w, 2, screen->cols);
465 /* main window */
466 screen->main_window.cols = screen->cols;
467 screen->main_window.rows = screen->rows-4;
468 wresize(screen->main_window.w, screen->main_window.rows, screen->cols);
469 wclear(screen->main_window.w);
471 /* progress window */
472 screen->progress_window.cols = screen->cols;
473 wresize(screen->progress_window.w, 1, screen->cols);
474 mvwin(screen->progress_window.w, screen->rows-2, 0);
476 /* status window */
477 screen->status_window.cols = screen->cols;
478 wresize(screen->status_window.w, 1, screen->cols);
479 mvwin(screen->status_window.w, screen->rows-1, 0);
481 screen->buf_size = screen->cols;
482 g_free(screen->buf);
483 screen->buf = g_malloc(screen->cols);
485 /* close and exit all screens (playlist,browse,help...) */
486 i=0;
487 while (screens[i].functions) {
488 const struct screen_functions *sf = screens[i].functions;
490 if (sf->resize)
491 sf->resize(screen->main_window.cols, screen->main_window.rows);
493 i++;
494 }
496 /* ? - without this the cursor becomes visible with aterm & Eterm */
497 curs_set(1);
498 curs_set(0);
500 screen->painted = 0;
501 }
503 void
504 screen_status_message(const char *msg)
505 {
506 WINDOW *w = screen->status_window.w;
508 wmove(w, 0, 0);
509 wclrtoeol(w);
510 colors_use(w, COLOR_STATUS_ALERT);
511 waddstr(w, msg);
512 wnoutrefresh(w);
513 screen->status_timestamp = time(NULL);
514 }
516 void
517 screen_status_printf(const char *format, ...)
518 {
519 char *msg;
520 va_list ap;
522 va_start(ap,format);
523 msg = g_strdup_vprintf(format,ap);
524 va_end(ap);
525 screen_status_message(msg);
526 g_free(msg);
527 }
529 void
530 ncurses_init(void)
531 {
533 /* initialize the curses library */
534 initscr();
535 /* initialize color support */
536 colors_start();
537 /* tell curses not to do NL->CR/NL on output */
538 nonl();
539 /* use raw mode (ignore interrupt,quit,suspend, and flow control ) */
540 #ifdef ENABLE_RAW_MODE
541 // raw();
542 #endif
543 /* don't echo input */
544 noecho();
545 /* set cursor invisible */
546 curs_set(0);
547 /* enable extra keys */
548 keypad(stdscr, TRUE);
549 /* return from getch() without blocking */
550 timeout(SCREEN_TIMEOUT);
551 /* initialize mouse support */
552 #ifdef HAVE_GETMOUSE
553 if( options.enable_mouse )
554 mousemask(ALL_MOUSE_EVENTS, NULL);
555 #endif
557 if( COLS<SCREEN_MIN_COLS || LINES<SCREEN_MIN_ROWS )
558 {
559 fprintf(stderr, _("Error: Screen to small!\n"));
560 exit(EXIT_FAILURE);
561 }
562 screen = g_malloc(sizeof(screen_t));
563 memset(screen, 0, sizeof(screen_t));
564 screen->mode = 0;
565 screen->cols = COLS;
566 screen->rows = LINES;
568 screen->buf = g_malloc(screen->cols);
569 screen->buf_size = screen->cols;
570 screen->findbuf = NULL;
571 screen->painted = 0;
572 screen->start_timestamp = time(NULL);
573 screen->input_timestamp = time(NULL);
574 screen->last_cmd = CMD_NONE;
576 /* create top window */
577 screen->top_window.rows = 2;
578 screen->top_window.cols = screen->cols;
579 screen->top_window.w = newwin(screen->top_window.rows,
580 screen->top_window.cols,
581 0, 0);
582 leaveok(screen->top_window.w, TRUE);
583 keypad(screen->top_window.w, TRUE);
585 /* create main window */
586 screen->main_window.rows = screen->rows-4;
587 screen->main_window.cols = screen->cols;
588 screen->main_window.w = newwin(screen->main_window.rows,
589 screen->main_window.cols,
590 2,
591 0);
593 // leaveok(screen->main_window.w, TRUE); temporary disabled
594 keypad(screen->main_window.w, TRUE);
596 /* create progress window */
597 screen->progress_window.rows = 1;
598 screen->progress_window.cols = screen->cols;
599 screen->progress_window.w = newwin(screen->progress_window.rows,
600 screen->progress_window.cols,
601 screen->rows-2,
602 0);
603 leaveok(screen->progress_window.w, TRUE);
605 /* create status window */
606 screen->status_window.rows = 1;
607 screen->status_window.cols = screen->cols;
608 screen->status_window.w = newwin(screen->status_window.rows,
609 screen->status_window.cols,
610 screen->rows-1,
611 0);
613 leaveok(screen->status_window.w, FALSE);
614 keypad(screen->status_window.w, TRUE);
616 if( options.enable_colors )
617 {
618 /* set background attributes */
619 wbkgd(stdscr, COLOR_PAIR(COLOR_LIST));
620 wbkgd(screen->main_window.w, COLOR_PAIR(COLOR_LIST));
621 wbkgd(screen->top_window.w, COLOR_PAIR(COLOR_TITLE));
622 wbkgd(screen->progress_window.w, COLOR_PAIR(COLOR_PROGRESSBAR));
623 wbkgd(screen->status_window.w, COLOR_PAIR(COLOR_STATUS));
624 colors_use(screen->progress_window.w, COLOR_PROGRESSBAR);
625 }
627 refresh();
628 }
630 int
631 screen_init(mpdclient_t *c)
632 {
633 gint i;
635 /* initialize screens */
636 i=0;
637 while (screens[i].functions) {
638 const struct screen_functions *fn = screens[i].functions;
640 if (fn->init)
641 fn->init(screen->main_window.w,
642 screen->main_window.cols,
643 screen->main_window.rows);
645 i++;
646 }
648 if (mode_fn->open != NULL)
649 mode_fn->open(screen, c);
651 /* initialize wreadln */
652 wrln_wgetch = my_wgetch;
653 wrln_max_history_length = 16;
655 return 0;
656 }
658 void
659 screen_paint(mpdclient_t *c)
660 {
661 const char *title = NULL;
663 if (mode_fn->get_title != NULL)
664 title = mode_fn->get_title(screen->buf, screen->buf_size);
666 D("screen_paint(%s)\n", title);
667 /* paint the title/header window */
668 if( title )
669 paint_top_window(title, c, 1);
670 else
671 paint_top_window("", c, 1);
673 /* paint the main window */
674 wclear(screen->main_window.w);
675 if (mode_fn->paint != NULL)
676 mode_fn->paint(screen, c);
678 paint_progress_window(c);
679 paint_status_window(c);
680 screen->painted = 1;
681 wmove(screen->main_window.w, 0, 0);
682 wnoutrefresh(screen->main_window.w);
684 /* tell curses to update */
685 doupdate();
686 }
688 void
689 screen_update(mpdclient_t *c)
690 {
691 static int repeat = -1;
692 static int random_enabled = -1;
693 static int crossfade = -1;
694 static int dbupdate = -1;
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->get_title != NULL) {
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->update != NULL)
742 mode_fn->update(screen, c);
744 /* update progress window */
745 paint_progress_window(c);
747 /* update status window */
748 paint_status_window(c);
750 /* move the cursor to the origin */
751 wmove(screen->main_window.w, 0, 0);
752 wnoutrefresh(screen->main_window.w);
754 /* tell curses to update */
755 doupdate();
756 }
758 void
759 screen_idle(mpdclient_t *c)
760 {
761 if (c->song && seek_id == c->song->id &&
762 (screen->last_cmd == CMD_SEEK_FORWARD ||
763 screen->last_cmd == CMD_SEEK_BACKWARD))
764 mpdclient_cmd_seek(c, seek_id, seek_target_time);
766 screen->last_cmd = CMD_NONE;
767 seek_id = -1;
768 }
770 #ifdef HAVE_GETMOUSE
771 int
772 screen_get_mouse_event(mpdclient_t *c,
773 list_window_t *lw, int lw_length,
774 unsigned long *bstate, int *row)
775 {
776 MEVENT event;
778 /* retreive the mouse event from ncurses */
779 getmouse(&event);
780 D("mouse: id=%d y=%d,x=%d,z=%d\n",event.id,event.y,event.x,event.z);
781 /* calculate the selected row in the list window */
782 *row = event.y - screen->top_window.rows;
783 /* copy button state bits */
784 *bstate = event.bstate;
785 /* if button 2 was pressed switch screen */
786 if (event.bstate & BUTTON2_CLICKED) {
787 screen_cmd(c, CMD_SCREEN_NEXT);
788 return 1;
789 }
791 /* if the even occured above the list window move up */
792 if (*row < 0 && lw) {
793 if (event.bstate & BUTTON3_CLICKED)
794 list_window_first(lw);
795 else
796 list_window_previous_page(lw);
797 return 1;
798 }
800 /* if the even occured below the list window move down */
801 if ((unsigned)*row >= lw->rows && lw) {
802 if (event.bstate & BUTTON3_CLICKED)
803 list_window_last(lw, lw_length);
804 else
805 list_window_next_page(lw, lw_length);
806 return 1;
807 }
809 return 0;
810 }
811 #endif
813 void
814 screen_cmd(mpdclient_t *c, command_t cmd)
815 {
816 screen->input_timestamp = time(NULL);
817 screen->last_cmd = cmd;
818 welcome = FALSE;
820 if (mode_fn->cmd != NULL && mode_fn->cmd(screen, c, cmd))
821 return;
823 switch(cmd) {
824 case CMD_PLAY:
825 mpdclient_cmd_play(c, MPD_PLAY_AT_BEGINNING);
826 break;
827 case CMD_PAUSE:
828 mpdclient_cmd_pause(c, !IS_PAUSED(c->status->state));
829 break;
830 case CMD_STOP:
831 mpdclient_cmd_stop(c);
832 break;
833 case CMD_SEEK_FORWARD:
834 if (!IS_STOPPED(c->status->state)) {
835 if (c->song && seek_id != c->song->id) {
836 seek_id = c->song->id;
837 seek_target_time = c->status->elapsedTime;
838 }
839 seek_target_time+=options.seek_time;
840 if (seek_target_time < c->status->totalTime)
841 break;
842 seek_target_time = c->status->totalTime;
843 /* seek_target_time=0; */
844 }
845 break;
846 /* fall through... */
847 case CMD_TRACK_NEXT:
848 if (!IS_STOPPED(c->status->state))
849 mpdclient_cmd_next(c);
850 break;
851 case CMD_SEEK_BACKWARD:
852 if (!IS_STOPPED(c->status->state)) {
853 if (seek_id != c->song->id) {
854 seek_id = c->song->id;
855 seek_target_time = c->status->elapsedTime;
856 }
857 seek_target_time-=options.seek_time;
858 if (seek_target_time < 0)
859 seek_target_time=0;
860 }
861 break;
862 case CMD_TRACK_PREVIOUS:
863 if (!IS_STOPPED(c->status->state))
864 mpdclient_cmd_prev(c);
865 break;
866 case CMD_SHUFFLE:
867 if (mpdclient_cmd_shuffle(c) == 0)
868 screen_status_message(_("Shuffled playlist!"));
869 break;
870 case CMD_CLEAR:
871 if (mpdclient_cmd_clear(c) == 0)
872 screen_status_message(_("Cleared playlist!"));
873 break;
874 case CMD_REPEAT:
875 mpdclient_cmd_repeat(c, !c->status->repeat);
876 break;
877 case CMD_RANDOM:
878 mpdclient_cmd_random(c, !c->status->random);
879 break;
880 case CMD_CROSSFADE:
881 if (c->status->crossfade)
882 mpdclient_cmd_crossfade(c, 0);
883 else
884 mpdclient_cmd_crossfade(c, options.crossfade_time);
885 break;
886 case CMD_DB_UPDATE:
887 if (!c->status->updatingDb) {
888 if( mpdclient_cmd_db_update_utf8(c,NULL)==0 )
889 screen_status_printf(_("Database update started!"));
890 } else
891 screen_status_printf(_("Database update running..."));
892 break;
893 case CMD_VOLUME_UP:
894 if( c->status->volume!=MPD_STATUS_NO_VOLUME && c->status->volume<100 )
895 mpdclient_cmd_volume(c, ++c->status->volume);
896 break;
897 case CMD_VOLUME_DOWN:
898 if( c->status->volume!=MPD_STATUS_NO_VOLUME && c->status->volume>0 )
899 mpdclient_cmd_volume(c, --c->status->volume);
900 break;
901 case CMD_TOGGLE_FIND_WRAP:
902 options.find_wrap = !options.find_wrap;
903 screen_status_printf(options.find_wrap ?
904 _("Find mode: Wrapped") :
905 _("Find mode: Normal"));
906 break;
907 case CMD_TOGGLE_AUTOCENTER:
908 options.auto_center = !options.auto_center;
909 screen_status_printf(options.auto_center ?
910 _("Auto center mode: On") :
911 _("Auto center mode: Off"));
912 break;
913 case CMD_SCREEN_UPDATE:
914 screen->painted = 0;
915 break;
916 case CMD_SCREEN_PREVIOUS:
917 screen_next_mode(c, -1);
918 break;
919 case CMD_SCREEN_NEXT:
920 screen_next_mode(c, 1);
921 break;
922 case CMD_SCREEN_PLAY:
923 switch_screen_mode(SCREEN_PLAYLIST_ID, c);
924 break;
925 case CMD_SCREEN_FILE:
926 switch_screen_mode(SCREEN_BROWSE_ID, c);
927 break;
928 case CMD_SCREEN_HELP:
929 switch_screen_mode(SCREEN_HELP_ID, c);
930 break;
931 case CMD_SCREEN_SEARCH:
932 switch_screen_mode(SCREEN_SEARCH_ID, c);
933 break;
934 case CMD_SCREEN_ARTIST:
935 switch_screen_mode(SCREEN_ARTIST_ID, c);
936 break;
937 case CMD_SCREEN_KEYDEF:
938 switch_screen_mode(SCREEN_KEYDEF_ID, c);
939 break;
940 case CMD_SCREEN_CLOCK:
941 switch_screen_mode(SCREEN_CLOCK_ID, c);
942 break;
943 case CMD_SCREEN_LYRICS:
944 switch_screen_mode(SCREEN_LYRICS_ID, c);
945 break;
946 case CMD_QUIT:
947 exit(EXIT_SUCCESS);
948 default:
949 break;
950 }
951 }