Code

034855702475916311327b036615624b439f2497
[ncmpc.git] / src / screen.c
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 struct screen screen;
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)
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;
118 static gint
119 lookup_mode(gint id)
121         gint i=0;
123         while (screens[i].name) {
124                 if (screens[i].id == id)
125                         return i;
126                 i++;
127         }
128         return -1;
131 gint get_cur_mode_id(void)
133         return screens[screen.mode].id;
136 static void
137 switch_screen_mode(gint id, mpdclient_t *c)
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);
163 static int
164 find_configured_screen(const char *name)
166         unsigned i;
168         for (i = 0; options.screen_list[i] != NULL; ++i)
169                 if (strcmp(options.screen_list[i], name) == 0)
170                         return i;
172         return -1;
175 static void
176 screen_next_mode(mpdclient_t *c, int offset)
178         int max = g_strv_length(options.screen_list);
179         int current, next;
181         /* find current screen */
182         current = find_configured_screen(screens[screen.mode].name);
183         next = current + offset;
184         if (next<0)
185                 next = max-1;
186         else if (next>=max)
187                 next = 0;
189         D("current mode: %d:%d    next:%d\n", current, max, next);
190         switch_screen_mode(screen_get_id(options.screen_list[next]), c);
193 static void
194 paint_top_window2(const char *header, mpdclient_t *c)
196         char flags[5];
197         WINDOW *w = screen.top_window.w;
198         char buf[32];
200         if (header[0]) {
201                 colors_use(w, COLOR_TITLE_BOLD);
202                 mvwaddstr(w, 0, 0, header);
203         } else {
204                 colors_use(w, COLOR_TITLE_BOLD);
205                 waddstr(w, get_key_names(CMD_SCREEN_HELP, FALSE));
206                 colors_use(w, COLOR_TITLE);
207                 waddstr(w, _(":Help  "));
208                 colors_use(w, COLOR_TITLE_BOLD);
209                 waddstr(w, get_key_names(CMD_SCREEN_PLAY, FALSE));
210                 colors_use(w, COLOR_TITLE);
211                 waddstr(w, _(":Playlist  "));
212                 colors_use(w, COLOR_TITLE_BOLD);
213                 waddstr(w, get_key_names(CMD_SCREEN_FILE, FALSE));
214                 colors_use(w, COLOR_TITLE);
215                 waddstr(w, _(":Browse  "));
216 #ifdef ENABLE_ARTIST_SCREEN
217                 colors_use(w, COLOR_TITLE_BOLD);
218                 waddstr(w, get_key_names(CMD_SCREEN_ARTIST, FALSE));
219                 colors_use(w, COLOR_TITLE);
220                 waddstr(w, _(":Artist  "));
221 #endif
222 #ifdef ENABLE_SEARCH_SCREEN
223                 colors_use(w, COLOR_TITLE_BOLD);
224                 waddstr(w, get_key_names(CMD_SCREEN_SEARCH, FALSE));
225                 colors_use(w, COLOR_TITLE);
226                 waddstr(w, _(":Search  "));
227 #endif
228 #ifdef ENABLE_LYRICS_SCREEN
229                 colors_use(w, COLOR_TITLE_BOLD);
230                 waddstr(w, get_key_names(CMD_SCREEN_LYRICS, FALSE));
231                 colors_use(w, COLOR_TITLE);
232                 waddstr(w, _(":Lyrics  "));
233 #endif
234         }
235         if (c->status->volume==MPD_STATUS_NO_VOLUME) {
236                 g_snprintf(buf, 32, _("Volume n/a "));
237         } else {
238                 g_snprintf(buf, 32, _(" Volume %d%%"), c->status->volume);
239         }
240         colors_use(w, COLOR_TITLE);
241         mvwaddstr(w, 0, screen.top_window.cols-my_strlen(buf), buf);
243         flags[0] = 0;
244         if( c->status->repeat )
245                 g_strlcat(flags, "r", sizeof(flags));
246         if( c->status->random )
247                 g_strlcat(flags, "z", sizeof(flags));;
248         if( c->status->crossfade )
249                 g_strlcat(flags, "x", sizeof(flags));
250         if( c->status->updatingDb )
251                 g_strlcat(flags, "U", sizeof(flags));
252         colors_use(w, COLOR_LINE);
253         mvwhline(w, 1, 0, ACS_HLINE, screen.top_window.cols);
254         if (flags[0]) {
255                 wmove(w,1,screen.top_window.cols-strlen(flags)-3);
256                 waddch(w, '[');
257                 colors_use(w, COLOR_LINE_BOLD);
258                 waddstr(w, flags);
259                 colors_use(w, COLOR_LINE);
260                 waddch(w, ']');
261         }
262         wnoutrefresh(w);
265 static void
266 paint_top_window(const char *header, mpdclient_t *c, int full_repaint)
268         static int prev_volume = -1;
269         static size_t prev_header_len = -1;
270         WINDOW *w = screen.top_window.w;
272         if (prev_header_len!=my_strlen(header)) {
273                 prev_header_len = my_strlen(header);
274                 full_repaint = 1;
275         }
277         if (full_repaint) {
278                 wmove(w, 0, 0);
279                 wclrtoeol(w);
280         }
282         if (prev_volume!=c->status->volume || full_repaint)
283                 paint_top_window2(header, c);
286 static void
287 paint_progress_window(mpdclient_t *c)
289         double p;
290         int width;
291         int elapsedTime = c->status->elapsedTime;
293         if (c->status==NULL || IS_STOPPED(c->status->state)) {
294                 mvwhline(screen.progress_window.w, 0, 0, ACS_HLINE,
295                          screen.progress_window.cols);
296                 wnoutrefresh(screen.progress_window.w);
297                 return;
298         }
300         if (c->song && seek_id == c->song->id)
301                 elapsedTime = seek_target_time;
303         p = ((double) elapsedTime) / ((double) c->status->totalTime);
305         width = (int) (p * (double) screen.progress_window.cols);
306         mvwhline(screen.progress_window.w,
307                  0, 0,
308                  ACS_HLINE,
309                  screen.progress_window.cols);
310         whline(screen.progress_window.w, '=', width-1);
311         mvwaddch(screen.progress_window.w, 0, width-1, 'O');
312         wnoutrefresh(screen.progress_window.w);
315 static void
316 paint_status_window(mpdclient_t *c)
318         WINDOW *w = screen.status_window.w;
319         mpd_Status *status = c->status;
320         mpd_Song *song = c->song;
321         int elapsedTime = 0;
322         const char *str = NULL;
323         int x = 0;
325         if( time(NULL) - screen.status_timestamp <= SCREEN_STATUS_MESSAGE_TIME )
326                 return;
328         wmove(w, 0, 0);
329         wclrtoeol(w);
330         colors_use(w, COLOR_STATUS_BOLD);
332         switch(status->state) {
333         case MPD_STATUS_STATE_PLAY:
334                 str = _("Playing:");
335                 break;
336         case MPD_STATUS_STATE_PAUSE:
337                 str = _("[Paused]");
338                 break;
339         case MPD_STATUS_STATE_STOP:
340         default:
341                 break;
342         }
344         if (str) {
345                 waddstr(w, str);
346                 x += my_strlen(str)+1;
347         }
349         /* create time string */
350         memset(screen.buf, 0, screen.buf_size);
351         if (IS_PLAYING(status->state) || IS_PAUSED(status->state)) {
352                 if (status->totalTime > 0) {
353                         /*checks the conf to see whether to display elapsed or remaining time */
354                         if(!strcmp(options.timedisplay_type,"elapsed"))
355                                 elapsedTime = c->status->elapsedTime;
356                         else if(!strcmp(options.timedisplay_type,"remaining"))
357                                 elapsedTime = (c->status->totalTime - c->status->elapsedTime);
359                         if( c->song && seek_id == c->song->id )
360                                 elapsedTime = seek_target_time;
361                         /*write out the time, using hours if time over 60 minutes*/
362                         if (c->status->totalTime > 3600) {
363                                 g_snprintf(screen.buf, screen.buf_size,
364                                            " [%i:%02i:%02i/%i:%02i:%02i]",
365                                            elapsedTime/3600, (elapsedTime%3600)/60, elapsedTime%60,
366                                            status->totalTime/3600, (status->totalTime%3600)/60,  status->totalTime%60);
367                         } else {
368                                 g_snprintf(screen.buf, screen.buf_size,
369                                            " [%i:%02i/%i:%02i]",
370                                            elapsedTime/60, elapsedTime%60,
371                                            status->totalTime/60,   status->totalTime%60 );
372                         }
373                 } else {
374                         g_snprintf(screen.buf, screen.buf_size,
375                                    " [%d kbps]", status->bitRate );
376                 }
377         } else {
378                 time_t timep;
380                 time(&timep);
381                 strftime(screen.buf, screen.buf_size, "%X ",localtime(&timep));
382         }
384         /* display song */
385         if (IS_PLAYING(status->state) || IS_PAUSED(status->state)) {
386                 char songname[MAX_SONGNAME_LENGTH];
387                 int width = COLS-x-my_strlen(screen.buf);
389                 if (song)
390                         strfsong(songname, MAX_SONGNAME_LENGTH, STATUS_FORMAT, song);
391                 else
392                         songname[0] = '\0';
394                 colors_use(w, COLOR_STATUS);
395                 /* scroll if the song name is to long */
396                 if (options.scroll && my_strlen(songname) > (size_t)width) {
397                         static  scroll_state_t st = { 0, 0 };
398                         char *tmp = strscroll(songname, options.scroll_sep, width, &st);
400                         g_strlcpy(songname, tmp, MAX_SONGNAME_LENGTH);
401                         g_free(tmp);
402                 }
403                 //mvwaddnstr(w, 0, x, songname, width);
404                 mvwaddstr(w, 0, x, songname);
405         }
407         /* display time string */
408         if (screen.buf[0]) {
409                 x = screen.status_window.cols - strlen(screen.buf);
410                 colors_use(w, COLOR_STATUS_TIME);
411                 mvwaddstr(w, 0, x, screen.buf);
412         }
414         wnoutrefresh(w);
417 int
418 screen_exit(void)
420         gint i;
422         endwin();
424         /* close and exit all screens (playlist,browse,help...) */
425         i=0;
426         while (screens[i].functions) {
427                 const struct screen_functions *sf = screens[i].functions;
429                 if (sf->close)
430                         sf->close();
431                 if (sf->exit)
432                         sf->exit();
434                 i++;
435         }
437         string_list_free(screen.find_history);
438         g_free(screen.buf);
439         g_free(screen.findbuf);
441         return 0;
444 void
445 screen_resize(void)
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;
503 void
504 screen_status_message(const char *msg)
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);
516 void
517 screen_status_printf(const char *format, ...)
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);
529 void
530 ncurses_init(void)
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.mode = 0;
563         screen.cols = COLS;
564         screen.rows = LINES;
566         screen.buf  = g_malloc(screen.cols);
567         screen.buf_size = screen.cols;
568         screen.findbuf = NULL;
569         screen.painted = 0;
570         screen.start_timestamp = time(NULL);
571         screen.input_timestamp = time(NULL);
572         screen.last_cmd = CMD_NONE;
574         /* create top window */
575         screen.top_window.rows = 2;
576         screen.top_window.cols = screen.cols;
577         screen.top_window.w = newwin(screen.top_window.rows,
578                                       screen.top_window.cols,
579                                       0, 0);
580         leaveok(screen.top_window.w, TRUE);
581         keypad(screen.top_window.w, TRUE);
583         /* create main window */
584         screen.main_window.rows = screen.rows-4;
585         screen.main_window.cols = screen.cols;
586         screen.main_window.w = newwin(screen.main_window.rows,
587                                        screen.main_window.cols,
588                                        2,
589                                        0);
591         //  leaveok(screen.main_window.w, TRUE); temporary disabled
592         keypad(screen.main_window.w, TRUE);
594         /* create progress window */
595         screen.progress_window.rows = 1;
596         screen.progress_window.cols = screen.cols;
597         screen.progress_window.w = newwin(screen.progress_window.rows,
598                                            screen.progress_window.cols,
599                                            screen.rows-2,
600                                            0);
601         leaveok(screen.progress_window.w, TRUE);
603         /* create status window */
604         screen.status_window.rows = 1;
605         screen.status_window.cols = screen.cols;
606         screen.status_window.w = newwin(screen.status_window.rows,
607                                          screen.status_window.cols,
608                                          screen.rows-1,
609                                          0);
611         leaveok(screen.status_window.w, FALSE);
612         keypad(screen.status_window.w, TRUE);
614         if( options.enable_colors )
615                 {
616                         /* set background attributes */
617                         wbkgd(stdscr, COLOR_PAIR(COLOR_LIST));
618                         wbkgd(screen.main_window.w,     COLOR_PAIR(COLOR_LIST));
619                         wbkgd(screen.top_window.w,      COLOR_PAIR(COLOR_TITLE));
620                         wbkgd(screen.progress_window.w, COLOR_PAIR(COLOR_PROGRESSBAR));
621                         wbkgd(screen.status_window.w,   COLOR_PAIR(COLOR_STATUS));
622                         colors_use(screen.progress_window.w, COLOR_PROGRESSBAR);
623                 }
625         refresh();
628 int
629 screen_init(mpdclient_t *c)
631         gint i;
633         /* initialize screens */
634         i=0;
635         while (screens[i].functions) {
636                 const struct screen_functions *fn = screens[i].functions;
638                 if (fn->init)
639                         fn->init(screen.main_window.w,
640                                  screen.main_window.cols,
641                                  screen.main_window.rows);
643                 i++;
644         }
646         if (mode_fn->open != NULL)
647                 mode_fn->open(&screen, c);
649         /* initialize wreadln */
650         wrln_wgetch = my_wgetch;
651         wrln_max_history_length = 16;
653         return 0;
656 void
657 screen_paint(mpdclient_t *c)
659         const char *title = NULL;
661         if (mode_fn->get_title != NULL)
662                 title = mode_fn->get_title(screen.buf, screen.buf_size);
664         D("screen_paint(%s)\n", title);
665         /* paint the title/header window */
666         if( title )
667                 paint_top_window(title, c, 1);
668         else
669                 paint_top_window("", c, 1);
671         /* paint the main window */
672         wclear(screen.main_window.w);
673         if (mode_fn->paint != NULL)
674                 mode_fn->paint(&screen, c);
676         paint_progress_window(c);
677         paint_status_window(c);
678         screen.painted = 1;
679         wmove(screen.main_window.w, 0, 0);
680         wnoutrefresh(screen.main_window.w);
682         /* tell curses to update */
683         doupdate();
686 void
687 screen_update(mpdclient_t *c)
689         static int repeat = -1;
690         static int random_enabled = -1;
691         static int crossfade = -1;
692         static int dbupdate = -1;
694         if( !screen.painted )
695                 return screen_paint(c);
697         /* print a message if mpd status has changed */
698         if (repeat < 0) {
699                 repeat = c->status->repeat;
700                 random_enabled = c->status->random;
701                 crossfade = c->status->crossfade;
702                 dbupdate = c->status->updatingDb;
703         }
705         if (repeat != c->status->repeat)
706                 screen_status_printf(c->status->repeat ?
707                                      _("Repeat is on") :
708                                      _("Repeat is off"));
710         if (random_enabled != c->status->random)
711                 screen_status_printf(c->status->random ?
712                                      _("Random is on") :
713                                      _("Random is off"));
715         if (crossfade != c->status->crossfade)
716                 screen_status_printf(_("Crossfade %d seconds"), c->status->crossfade);
718         if (dbupdate && dbupdate != c->status->updatingDb) {
719                 screen_status_printf(_("Database updated!"));
720                 mpdclient_browse_callback(c, BROWSE_DB_UPDATED, NULL);
721         }
723         repeat = c->status->repeat;
724         random_enabled = c->status->random;
725         crossfade = c->status->crossfade;
726         dbupdate = c->status->updatingDb;
728         /* update title/header window */
729         if (welcome && screen.last_cmd==CMD_NONE &&
730             time(NULL)-screen.start_timestamp <= SCREEN_WELCOME_TIME)
731                 paint_top_window("", c, 0);
732         else if (mode_fn->get_title != NULL) {
733                 paint_top_window(mode_fn->get_title(screen.buf,screen.buf_size), c, 0);
734                 welcome = FALSE;
735         } else
736                 paint_top_window("", c, 0);
738         /* update the main window */
739         if (mode_fn->update != NULL)
740                 mode_fn->update(&screen, c);
742         /* update progress window */
743         paint_progress_window(c);
745         /* update status window */
746         paint_status_window(c);
748         /* move the cursor to the origin */
749         wmove(screen.main_window.w, 0, 0);
750         wnoutrefresh(screen.main_window.w);
752         /* tell curses to update */
753         doupdate();
756 void
757 screen_idle(mpdclient_t *c)
759         if (c->song && seek_id == c->song->id &&
760             (screen.last_cmd == CMD_SEEK_FORWARD ||
761              screen.last_cmd == CMD_SEEK_BACKWARD))
762                 mpdclient_cmd_seek(c, seek_id, seek_target_time);
764         screen.last_cmd = CMD_NONE;
765         seek_id = -1;
768 #ifdef HAVE_GETMOUSE
769 int
770 screen_get_mouse_event(mpdclient_t *c,
771                        list_window_t *lw, int lw_length,
772                        unsigned long *bstate, int *row)
774         MEVENT event;
776         /* retreive the mouse event from ncurses */
777         getmouse(&event);
778         D("mouse: id=%d  y=%d,x=%d,z=%d\n",event.id,event.y,event.x,event.z);
779         /* calculate the selected row in the list window */
780         *row = event.y - screen.top_window.rows;
781         /* copy button state bits */
782         *bstate = event.bstate;
783         /* if button 2 was pressed switch screen */
784         if (event.bstate & BUTTON2_CLICKED) {
785                 screen_cmd(c, CMD_SCREEN_NEXT);
786                 return 1;
787         }
789         /* if the even occured above the list window move up */
790         if (*row < 0 && lw) {
791                 if (event.bstate & BUTTON3_CLICKED)
792                         list_window_first(lw);
793                 else
794                         list_window_previous_page(lw);
795                 return 1;
796         }
798         /* if the even occured below the list window move down */
799         if ((unsigned)*row >= lw->rows && lw) {
800                 if (event.bstate & BUTTON3_CLICKED)
801                         list_window_last(lw, lw_length);
802                 else
803                         list_window_next_page(lw, lw_length);
804                 return 1;
805         }
807         return 0;
809 #endif
811 void
812 screen_cmd(mpdclient_t *c, command_t cmd)
814         screen.input_timestamp = time(NULL);
815         screen.last_cmd = cmd;
816         welcome = FALSE;
818         if (mode_fn->cmd != NULL && mode_fn->cmd(&screen, c, cmd))
819                 return;
821         switch(cmd) {
822         case CMD_PLAY:
823                 mpdclient_cmd_play(c, MPD_PLAY_AT_BEGINNING);
824                 break;
825         case CMD_PAUSE:
826                 mpdclient_cmd_pause(c, !IS_PAUSED(c->status->state));
827                 break;
828         case CMD_STOP:
829                 mpdclient_cmd_stop(c);
830                 break;
831         case CMD_SEEK_FORWARD:
832                 if (!IS_STOPPED(c->status->state)) {
833                         if (c->song && seek_id != c->song->id) {
834                                 seek_id = c->song->id;
835                                 seek_target_time = c->status->elapsedTime;
836                         }
837                         seek_target_time+=options.seek_time;
838                         if (seek_target_time < c->status->totalTime)
839                                 break;
840                         seek_target_time = c->status->totalTime;
841                         /* seek_target_time=0; */
842                 }
843                 break;
844                 /* fall through... */
845         case CMD_TRACK_NEXT:
846                 if (!IS_STOPPED(c->status->state))
847                         mpdclient_cmd_next(c);
848                 break;
849         case CMD_SEEK_BACKWARD:
850                 if (!IS_STOPPED(c->status->state)) {
851                         if (seek_id != c->song->id) {
852                                 seek_id = c->song->id;
853                                 seek_target_time = c->status->elapsedTime;
854                         }
855                         seek_target_time-=options.seek_time;
856                         if (seek_target_time < 0)
857                                 seek_target_time=0;
858                 }
859                 break;
860         case CMD_TRACK_PREVIOUS:
861                 if (!IS_STOPPED(c->status->state))
862                         mpdclient_cmd_prev(c);
863                 break;
864         case CMD_SHUFFLE:
865                 if (mpdclient_cmd_shuffle(c) == 0)
866                         screen_status_message(_("Shuffled playlist!"));
867                 break;
868         case CMD_CLEAR:
869                 if (mpdclient_cmd_clear(c) == 0)
870                         screen_status_message(_("Cleared playlist!"));
871                 break;
872         case CMD_REPEAT:
873                 mpdclient_cmd_repeat(c, !c->status->repeat);
874                 break;
875         case CMD_RANDOM:
876                 mpdclient_cmd_random(c, !c->status->random);
877                 break;
878         case CMD_CROSSFADE:
879                 if (c->status->crossfade)
880                         mpdclient_cmd_crossfade(c, 0);
881                 else
882                         mpdclient_cmd_crossfade(c, options.crossfade_time);
883                 break;
884         case CMD_DB_UPDATE:
885                 if (!c->status->updatingDb) {
886                         if( mpdclient_cmd_db_update_utf8(c,NULL)==0 )
887                                 screen_status_printf(_("Database update started!"));
888                 } else
889                         screen_status_printf(_("Database update running..."));
890                 break;
891         case CMD_VOLUME_UP:
892                 if( c->status->volume!=MPD_STATUS_NO_VOLUME && c->status->volume<100 )
893                         mpdclient_cmd_volume(c, ++c->status->volume);
894                 break;
895         case CMD_VOLUME_DOWN:
896                 if( c->status->volume!=MPD_STATUS_NO_VOLUME && c->status->volume>0 )
897                         mpdclient_cmd_volume(c, --c->status->volume);
898                 break;
899         case CMD_TOGGLE_FIND_WRAP:
900                 options.find_wrap = !options.find_wrap;
901                 screen_status_printf(options.find_wrap ?
902                                      _("Find mode: Wrapped") :
903                                      _("Find mode: Normal"));
904                 break;
905         case CMD_TOGGLE_AUTOCENTER:
906                 options.auto_center = !options.auto_center;
907                 screen_status_printf(options.auto_center ?
908                                      _("Auto center mode: On") :
909                                      _("Auto center mode: Off"));
910                 break;
911         case CMD_SCREEN_UPDATE:
912                 screen.painted = 0;
913                 break;
914         case CMD_SCREEN_PREVIOUS:
915                 screen_next_mode(c, -1);
916                 break;
917         case CMD_SCREEN_NEXT:
918                 screen_next_mode(c, 1);
919                 break;
920         case CMD_SCREEN_PLAY:
921                 switch_screen_mode(SCREEN_PLAYLIST_ID, c);
922                 break;
923         case CMD_SCREEN_FILE:
924                 switch_screen_mode(SCREEN_BROWSE_ID, c);
925                 break;
926         case CMD_SCREEN_HELP:
927                 switch_screen_mode(SCREEN_HELP_ID, c);
928                 break;
929         case CMD_SCREEN_SEARCH:
930                 switch_screen_mode(SCREEN_SEARCH_ID, c);
931                 break;
932         case CMD_SCREEN_ARTIST:
933                 switch_screen_mode(SCREEN_ARTIST_ID, c);
934                 break;
935         case CMD_SCREEN_KEYDEF:
936                 switch_screen_mode(SCREEN_KEYDEF_ID, c);
937                 break;
938         case CMD_SCREEN_CLOCK:
939                 switch_screen_mode(SCREEN_CLOCK_ID, c);
940                 break;
941         case CMD_SCREEN_LYRICS:
942                 switch_screen_mode(SCREEN_LYRICS_ID, c);
943                 break;
944         case CMD_QUIT:
945                 exit(EXIT_SUCCESS);
946         default:
947                 break;
948         }