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 int x = 0;
327 if( time(NULL) - screen->status_timestamp <= SCREEN_STATUS_MESSAGE_TIME )
328 return;
330 wmove(w, 0, 0);
331 wclrtoeol(w);
332 colors_use(w, COLOR_STATUS_BOLD);
334 switch(status->state)
335 {
336 case MPD_STATUS_STATE_PLAY:
337 str = _("Playing:");
338 break;
339 case MPD_STATUS_STATE_PAUSE:
340 str = _("[Paused]");
341 break;
342 case MPD_STATUS_STATE_STOP:
343 default:
344 break;
345 }
347 if( str )
348 {
349 waddstr(w, str);
350 x += my_strlen(str)+1;
351 }
353 /* create time string */
354 memset(screen->buf, 0, screen->buf_size);
355 if( IS_PLAYING(status->state) || IS_PAUSED(status->state) )
356 {
357 if( status->totalTime > 0 )
358 {
359 /*checks the conf to see whether to display elapsed or remaining time */
360 if(!strcmp(options.timedisplay_type,"elapsed"))
361 elapsedTime = c->status->elapsedTime;
362 else if(!strcmp(options.timedisplay_type,"remaining"))
363 elapsedTime = (c->status->totalTime - c->status->elapsedTime);
365 if( c->song && seek_id == c->song->id )
366 elapsedTime = seek_target_time;
367 /*write out the time, using hours if time over 60 minutes*/
368 if (c->status->totalTime > 3600)
369 {
370 g_snprintf(screen->buf, screen->buf_size,
371 " [%i:%02i:%02i/%i:%02i:%02i]",
372 elapsedTime/3600, (elapsedTime%3600)/60, elapsedTime%60,
373 status->totalTime/3600, (status->totalTime%3600)/60, status->totalTime%60);
374 }
375 else
376 {
377 g_snprintf(screen->buf, screen->buf_size,
378 " [%i:%02i/%i:%02i]",
379 elapsedTime/60, elapsedTime%60,
380 status->totalTime/60, status->totalTime%60 );
381 }
382 }
383 else
384 {
385 g_snprintf(screen->buf, screen->buf_size,
386 " [%d kbps]", status->bitRate );
387 }
388 }
389 else
390 {
391 time_t timep;
393 time(&timep);
394 strftime(screen->buf, screen->buf_size, "%X ",localtime(&timep));
395 }
397 /* display song */
398 if( (IS_PLAYING(status->state) || IS_PAUSED(status->state)) )
399 {
400 char songname[MAX_SONGNAME_LENGTH];
401 int width = COLS-x-my_strlen(screen->buf);
403 if( song )
404 strfsong(songname, MAX_SONGNAME_LENGTH, STATUS_FORMAT, song);
405 else
406 songname[0] = '\0';
408 colors_use(w, COLOR_STATUS);
409 /* scroll if the song name is to long */
410 if( options.scroll && my_strlen(songname) > width )
411 {
412 static scroll_state_t st = { 0, 0 };
413 char *tmp = strscroll(songname, options.scroll_sep, width, &st);
415 g_strlcpy(songname, tmp, MAX_SONGNAME_LENGTH);
416 g_free(tmp);
417 }
418 //mvwaddnstr(w, 0, x, songname, width);
419 mvwaddstr(w, 0, x, songname);
420 }
422 /* display time string */
423 if( screen->buf[0] )
424 {
425 x = screen->status_window.cols - strlen(screen->buf);
426 colors_use(w, COLOR_STATUS_TIME);
427 mvwaddstr(w, 0, x, screen->buf);
428 }
430 wnoutrefresh(w);
431 }
433 int
434 screen_exit(void)
435 {
436 endwin();
437 if( screen )
438 {
439 gint i;
441 /* close and exit all screens (playlist,browse,help...) */
442 i=0;
443 while( screens[i].get_mode_functions )
444 {
445 screen_functions_t *mode_fn = screens[i].get_mode_functions();
447 if( mode_fn && mode_fn->close )
448 mode_fn->close();
449 if( mode_fn && mode_fn->exit )
450 mode_fn->exit();
452 i++;
453 }
455 string_list_free(screen->find_history);
456 g_free(screen->buf);
457 g_free(screen->findbuf);
459 g_free(screen);
460 screen = NULL;
461 }
462 return 0;
463 }
465 void
466 screen_resize(void)
467 {
468 gint i;
470 D("Resize rows %d->%d, cols %d->%d\n",screen->rows,LINES,screen->cols,COLS);
471 if( COLS<SCREEN_MIN_COLS || LINES<SCREEN_MIN_ROWS )
472 {
473 screen_exit();
474 fprintf(stderr, _("Error: Screen to small!\n"));
475 exit(EXIT_FAILURE);
476 }
478 resizeterm(LINES, COLS);
480 screen->cols = COLS;
481 screen->rows = LINES;
483 /* top window */
484 screen->top_window.cols = screen->cols;
485 wresize(screen->top_window.w, 2, screen->cols);
487 /* main window */
488 screen->main_window.cols = screen->cols;
489 screen->main_window.rows = screen->rows-4;
490 wresize(screen->main_window.w, screen->main_window.rows, screen->cols);
491 wclear(screen->main_window.w);
493 /* progress window */
494 screen->progress_window.cols = screen->cols;
495 wresize(screen->progress_window.w, 1, screen->cols);
496 mvwin(screen->progress_window.w, screen->rows-2, 0);
498 /* status window */
499 screen->status_window.cols = screen->cols;
500 wresize(screen->status_window.w, 1, screen->cols);
501 mvwin(screen->status_window.w, screen->rows-1, 0);
503 screen->buf_size = screen->cols;
504 g_free(screen->buf);
505 screen->buf = g_malloc(screen->cols);
507 /* close and exit all screens (playlist,browse,help...) */
508 i=0;
509 while( screens[i].get_mode_functions )
510 {
511 screen_functions_t *mode_fn = screens[i].get_mode_functions();
513 if( mode_fn && mode_fn->resize )
514 mode_fn->resize(screen->main_window.cols, screen->main_window.rows);
516 i++;
517 }
519 /* ? - without this the cursor becomes visible with aterm & Eterm */
520 curs_set(1);
521 curs_set(0);
523 screen->painted = 0;
524 }
526 void
527 screen_status_message(char *msg)
528 {
529 WINDOW *w = screen->status_window.w;
531 wmove(w, 0, 0);
532 wclrtoeol(w);
533 colors_use(w, COLOR_STATUS_ALERT);
534 waddstr(w, msg);
535 wnoutrefresh(w);
536 screen->status_timestamp = time(NULL);
537 }
539 void
540 screen_status_printf(char *format, ...)
541 {
542 char *msg;
543 va_list ap;
545 va_start(ap,format);
546 msg = g_strdup_vprintf(format,ap);
547 va_end(ap);
548 screen_status_message(msg);
549 g_free(msg);
550 }
552 void
553 ncurses_init()
554 {
556 /* initialize the curses library */
557 initscr();
558 /* initialize color support */
559 colors_start();
560 /* tell curses not to do NL->CR/NL on output */
561 nonl();
562 /* use raw mode (ignore interrupt,quit,suspend, and flow control ) */
563 #ifdef ENABLE_RAW_MODE
564 // raw();
565 #endif
566 /* don't echo input */
567 noecho();
568 /* set cursor invisible */
569 curs_set(0);
570 /* enable extra keys */
571 keypad(stdscr, TRUE);
572 /* return from getch() without blocking */
573 timeout(SCREEN_TIMEOUT);
574 /* initialize mouse support */
575 #ifdef HAVE_GETMOUSE
576 if( options.enable_mouse )
577 mousemask(ALL_MOUSE_EVENTS, NULL);
578 #endif
580 if( COLS<SCREEN_MIN_COLS || LINES<SCREEN_MIN_ROWS )
581 {
582 fprintf(stderr, _("Error: Screen to small!\n"));
583 exit(EXIT_FAILURE);
584 }
585 screen = g_malloc(sizeof(screen_t));
586 memset(screen, 0, sizeof(screen_t));
587 screen->mode = 0;
588 screen->cols = COLS;
589 screen->rows = LINES;
591 screen->buf = g_malloc(screen->cols);
592 screen->buf_size = screen->cols;
593 screen->findbuf = NULL;
594 screen->painted = 0;
595 screen->start_timestamp = time(NULL);
596 screen->input_timestamp = time(NULL);
597 screen->last_cmd = CMD_NONE;
599 /* create top window */
600 screen->top_window.rows = 2;
601 screen->top_window.cols = screen->cols;
602 screen->top_window.w = newwin(screen->top_window.rows,
603 screen->top_window.cols,
604 0, 0);
605 leaveok(screen->top_window.w, TRUE);
606 keypad(screen->top_window.w, TRUE);
608 /* create main window */
609 screen->main_window.rows = screen->rows-4;
610 screen->main_window.cols = screen->cols;
611 screen->main_window.w = newwin(screen->main_window.rows,
612 screen->main_window.cols,
613 2,
614 0);
616 // leaveok(screen->main_window.w, TRUE); temporary disabled
617 keypad(screen->main_window.w, TRUE);
619 /* create progress window */
620 screen->progress_window.rows = 1;
621 screen->progress_window.cols = screen->cols;
622 screen->progress_window.w = newwin(screen->progress_window.rows,
623 screen->progress_window.cols,
624 screen->rows-2,
625 0);
626 leaveok(screen->progress_window.w, TRUE);
628 /* create status window */
629 screen->status_window.rows = 1;
630 screen->status_window.cols = screen->cols;
631 screen->status_window.w = newwin(screen->status_window.rows,
632 screen->status_window.cols,
633 screen->rows-1,
634 0);
636 leaveok(screen->status_window.w, FALSE);
637 keypad(screen->status_window.w, TRUE);
639 if( options.enable_colors )
640 {
641 /* set background attributes */
642 wbkgd(stdscr, COLOR_PAIR(COLOR_LIST));
643 wbkgd(screen->main_window.w, COLOR_PAIR(COLOR_LIST));
644 wbkgd(screen->top_window.w, COLOR_PAIR(COLOR_TITLE));
645 wbkgd(screen->progress_window.w, COLOR_PAIR(COLOR_PROGRESSBAR));
646 wbkgd(screen->status_window.w, COLOR_PAIR(COLOR_STATUS));
647 colors_use(screen->progress_window.w, COLOR_PROGRESSBAR);
648 }
649 }
651 screen_init(mpdclient_t *c)
652 {
653 gint i;
655 /* initialize screens */
656 i=0;
657 while( screens[i].get_mode_functions )
658 {
659 screen_functions_t *fn = screens[i].get_mode_functions();
661 if( fn && fn->init )
662 fn->init(screen->main_window.w,
663 screen->main_window.cols,
664 screen->main_window.rows);
666 i++;
667 }
669 #if 0
670 /* broken */
671 mode_fn = NULL;
672 switch_screen_mode(screen_get_id(options.screen_list[0]), c);
673 #else
674 mode_fn = get_screen_playlist();
675 #endif
677 if( mode_fn && mode_fn->open )
678 mode_fn->open(screen, c);
680 /* initialize wreadln */
681 wrln_wgetch = my_wgetch;
682 wrln_max_history_length = 16;
684 return 0;
685 }
687 void
688 screen_paint(mpdclient_t *c)
689 {
690 char *title = NULL;
692 if( mode_fn && mode_fn->get_title )
693 title = mode_fn->get_title(screen->buf,screen->buf_size);
695 D("screen_paint(%s)\n", title);
696 /* paint the title/header window */
697 if( title )
698 paint_top_window(title, c, 1);
699 else
700 paint_top_window("", c, 1);
702 /* paint the main window */
703 wclear(screen->main_window.w);
704 if( mode_fn && mode_fn->paint )
705 mode_fn->paint(screen, c);
707 paint_progress_window(c);
708 paint_status_window(c);
709 screen->painted = 1;
710 wmove(screen->main_window.w, 0, 0);
711 wnoutrefresh(screen->main_window.w);
713 /* tell curses to update */
714 doupdate();
715 }
717 void
718 screen_update(mpdclient_t *c)
719 {
720 static int repeat = -1;
721 static int random = -1;
722 static int crossfade = -1;
723 static int dbupdate = -1;
724 list_window_t *lw = NULL;
726 if( !screen->painted )
727 return screen_paint(c);
729 /* print a message if mpd status has changed */
730 if( repeat<0 )
731 {
732 repeat = c->status->repeat;
733 random = c->status->random;
734 crossfade = c->status->crossfade;
735 dbupdate = c->status->updatingDb;
736 }
737 if( repeat != c->status->repeat )
738 screen_status_printf(c->status->repeat ?
739 _("Repeat is on") :
740 _("Repeat is off"));
741 if( random != c->status->random )
742 screen_status_printf(c->status->random ?
743 _("Random is on") :
744 _("Random is off"));
746 if( crossfade != c->status->crossfade )
747 screen_status_printf(_("Crossfade %d seconds"), c->status->crossfade);
748 if( dbupdate && dbupdate != c->status->updatingDb )
749 {
750 screen_status_printf(_("Database updated!"));
751 mpdclient_browse_callback(c, BROWSE_DB_UPDATED, NULL);
752 }
754 repeat = c->status->repeat;
755 random = c->status->random;
756 crossfade = c->status->crossfade;
757 dbupdate = c->status->updatingDb;
759 /* update title/header window */
760 if( welcome && screen->last_cmd==CMD_NONE &&
761 time(NULL)-screen->start_timestamp <= SCREEN_WELCOME_TIME)
762 paint_top_window("", c, 0);
763 else if( mode_fn && mode_fn->get_title )
764 {
765 paint_top_window(mode_fn->get_title(screen->buf,screen->buf_size), c, 0);
766 welcome = FALSE;
767 }
768 else
769 paint_top_window("", c, 0);
771 /* update the main window */
772 if( mode_fn && mode_fn->paint )
773 mode_fn->update(screen, c);
775 if( mode_fn && mode_fn->get_lw )
776 lw = mode_fn->get_lw();
778 /* update progress window */
779 paint_progress_window(c);
781 /* update status window */
782 paint_status_window(c);
784 /* move the cursor to the selected row in the main window */
785 if( lw )
786 wmove(screen->main_window.w, LW_ROW(lw), 0);
787 else
788 wmove(screen->main_window.w, 0, 0);
789 wnoutrefresh(screen->main_window.w);
791 /* tell curses to update */
792 doupdate();
793 }
795 void
796 screen_idle(mpdclient_t *c)
797 {
798 if( c->song && seek_id == c->song->id &&
799 (screen->last_cmd == CMD_SEEK_FORWARD ||
800 screen->last_cmd == CMD_SEEK_BACKWARD) )
801 {
802 mpdclient_cmd_seek(c, seek_id, seek_target_time);
803 }
805 screen->last_cmd = CMD_NONE;
806 seek_id = -1;
807 }
809 #ifdef HAVE_GETMOUSE
810 int
811 screen_get_mouse_event(mpdclient_t *c,
812 list_window_t *lw, int lw_length,
813 unsigned long *bstate, int *row)
814 {
815 MEVENT event;
817 /* retreive the mouse event from ncurses */
818 getmouse(&event);
819 D("mouse: id=%d y=%d,x=%d,z=%d\n",event.id,event.y,event.x,event.z);
820 /* calculate the selected row in the list window */
821 *row = event.y - screen->top_window.rows;
822 /* copy button state bits */
823 *bstate = event.bstate;
824 /* if button 2 was pressed switch screen */
825 if( event.bstate & BUTTON2_CLICKED )
826 {
827 screen_cmd(c, CMD_SCREEN_NEXT);
828 return 1;
829 }
830 /* if the even occured above the list window move up */
831 if( *row<0 && lw )
832 {
833 if( event.bstate & BUTTON3_CLICKED )
834 list_window_first(lw);
835 else
836 list_window_previous_page(lw);
837 return 1;
838 }
839 /* if the even occured below the list window move down */
840 if( *row>=lw->rows && lw )
841 {
842 if( event.bstate & BUTTON3_CLICKED )
843 list_window_last(lw, lw_length);
844 else
845 list_window_next_page(lw, lw_length);
846 return 1;
847 }
848 return 0;
849 }
850 #endif
852 void
853 screen_cmd(mpdclient_t *c, command_t cmd)
854 {
855 screen->input_timestamp = time(NULL);
856 screen->last_cmd = cmd;
857 welcome = FALSE;
859 if( mode_fn && mode_fn->cmd && mode_fn->cmd(screen, c, cmd) )
860 return;
862 switch(cmd)
863 {
864 case CMD_PLAY:
865 mpdclient_cmd_play(c, MPD_PLAY_AT_BEGINNING);
866 break;
867 case CMD_PAUSE:
868 mpdclient_cmd_pause(c, !IS_PAUSED(c->status->state));
869 break;
870 case CMD_STOP:
871 mpdclient_cmd_stop(c);
872 break;
873 case CMD_SEEK_FORWARD:
874 if( !IS_STOPPED(c->status->state) )
875 {
876 if( c->song && seek_id != c->song->id )
877 {
878 seek_id = c->song->id;
879 seek_target_time = c->status->elapsedTime;
880 }
881 seek_target_time+=options.seek_time;
882 if( seek_target_time < c->status->totalTime )
883 break;
884 seek_target_time = c->status->totalTime;
885 /* seek_target_time=0; */
886 }
887 break;
888 /* fall through... */
889 case CMD_TRACK_NEXT:
890 if( !IS_STOPPED(c->status->state) )
891 mpdclient_cmd_next(c);
892 break;
893 case CMD_SEEK_BACKWARD:
894 if( !IS_STOPPED(c->status->state) )
895 {
896 if( seek_id != c->song->id )
897 {
898 seek_id = c->song->id;
899 seek_target_time = c->status->elapsedTime;
900 }
901 seek_target_time-=options.seek_time;
902 if( seek_target_time < 0 )
903 seek_target_time=0;
904 }
905 break;
906 case CMD_TRACK_PREVIOUS:
907 if( !IS_STOPPED(c->status->state) )
908 mpdclient_cmd_prev(c);
909 break;
910 case CMD_SHUFFLE:
911 if( mpdclient_cmd_shuffle(c) == 0 )
912 screen_status_message(_("Shuffled playlist!"));
913 break;
914 case CMD_CLEAR:
915 if( mpdclient_cmd_clear(c) == 0 )
916 screen_status_message(_("Cleared playlist!"));
917 break;
918 case CMD_REPEAT:
919 mpdclient_cmd_repeat(c, !c->status->repeat);
920 break;
921 case CMD_RANDOM:
922 mpdclient_cmd_random(c, !c->status->random);
923 break;
924 case CMD_CROSSFADE:
925 if( c->status->crossfade )
926 mpdclient_cmd_crossfade(c, 0);
927 else
928 mpdclient_cmd_crossfade(c, options.crossfade_time);
929 break;
930 case CMD_DB_UPDATE:
931 if( !c->status->updatingDb )
932 {
933 if( mpdclient_cmd_db_update_utf8(c,NULL)==0 )
934 screen_status_printf(_("Database update started!"));
935 }
936 else
937 screen_status_printf(_("Database update running..."));
938 break;
939 case CMD_VOLUME_UP:
940 if( c->status->volume!=MPD_STATUS_NO_VOLUME && c->status->volume<100 )
941 mpdclient_cmd_volume(c, ++c->status->volume);
942 break;
943 case CMD_VOLUME_DOWN:
944 if( c->status->volume!=MPD_STATUS_NO_VOLUME && c->status->volume>0 )
945 mpdclient_cmd_volume(c, --c->status->volume);
946 break;
947 case CMD_TOGGLE_FIND_WRAP:
948 options.find_wrap = !options.find_wrap;
949 screen_status_printf(options.find_wrap ?
950 _("Find mode: Wrapped") :
951 _("Find mode: Normal"));
952 break;
953 case CMD_TOGGLE_AUTOCENTER:
954 options.auto_center = !options.auto_center;
955 screen_status_printf(options.auto_center ?
956 _("Auto center mode: On") :
957 _("Auto center mode: Off"));
958 break;
959 case CMD_SCREEN_UPDATE:
960 screen->painted = 0;
961 break;
962 case CMD_SCREEN_PREVIOUS:
963 screen_next_mode(c, -1);
964 break;
965 case CMD_SCREEN_NEXT:
966 screen_next_mode(c, 1);
967 break;
968 case CMD_SCREEN_PLAY:
969 switch_screen_mode(SCREEN_PLAYLIST_ID, c);
970 break;
971 case CMD_SCREEN_FILE:
972 switch_screen_mode(SCREEN_BROWSE_ID, c);
973 break;
974 case CMD_SCREEN_HELP:
975 switch_screen_mode(SCREEN_HELP_ID, c);
976 break;
977 case CMD_SCREEN_SEARCH:
978 switch_screen_mode(SCREEN_SEARCH_ID, c);
979 break;
980 case CMD_SCREEN_ARTIST:
981 switch_screen_mode(SCREEN_ARTIST_ID, c);
982 break;
983 case CMD_SCREEN_KEYDEF:
984 switch_screen_mode(SCREEN_KEYDEF_ID, c);
985 break;
986 case CMD_SCREEN_CLOCK:
987 switch_screen_mode(SCREEN_CLOCK_ID, c);
988 break;
989 case CMD_SCREEN_LYRICS:
990 switch_screen_mode(SCREEN_LYRICS_ID, c);
991 break;
992 case CMD_QUIT:
993 exit(EXIT_SUCCESS);
994 default:
995 break;
996 }
997 }