Code

Added basic ncurses mouse support
[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 <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 "command.h"
35 #include "options.h"
36 #include "colors.h"
37 #include "strfsong.h"
38 #include "wreadln.h"
39 #include "screen.h"
40 #include "screen_utils.h"
42 #define ENABLE_STATUS_LINE_CLOCK
43 #define ENABLE_SCROLLING
45 #define CROSSFADE_TIME 10
47 #define STATUS_MESSAGE_TIMEOUT 3
48 #define STATUS_LINE_MAX_SIZE   512
50 /* screens */
51 extern screen_functions_t *get_screen_playlist(void);
52 extern screen_functions_t *get_screen_browse(void);
53 extern screen_functions_t *get_screen_help(void);
55 #ifdef ENABLE_KEYDEF_SCREEN
56 extern screen_functions_t *get_screen_keydef(void);
57 #endif
58 #ifdef ENABLE_CLOCK_SCREEN
59 extern screen_functions_t *get_screen_clock(void);
60 #endif
62 static gboolean welcome = TRUE;
63 static screen_t *screen = NULL;
64 static screen_functions_t *mode_fn = NULL;
65 static int seek_id = -1;
66 static int seek_target_time = 0;
69 static void
70 switch_screen_mode(screen_mode_t new_mode, mpdclient_t *c)
71 {
72   if( new_mode == screen->mode )
73     return;
75   /* close the old mode */
76   if( mode_fn && mode_fn->close )
77     mode_fn->close();
79   /* get functions for the new mode */
80   switch(new_mode)
81     {
82     case SCREEN_PLAY_WINDOW:
83       mode_fn = get_screen_playlist();
84       break;
85     case SCREEN_FILE_WINDOW:
86       mode_fn = get_screen_browse();
87       break;
88     case SCREEN_HELP_WINDOW:
89       mode_fn = get_screen_help();
90       break;
91 #ifdef ENABLE_KEYDEF_SCREEN
92     case SCREEN_KEYDEF_WINDOW:
93       mode_fn = get_screen_keydef();
94       break;
95 #endif
96 #ifdef ENABLE_CLOCK_SCREEN
97     case SCREEN_CLOCK_WINDOW:
98       mode_fn = get_screen_clock();
99       break;
100 #endif
102     default:
103       break;
104     }
106  screen->mode = new_mode;
107  screen->painted = 0;
109  /* open the new mode */
110  if( mode_fn && mode_fn->open )
111    mode_fn->open(screen, c);
115 static void
116 paint_top_window(char *header, mpdclient_t *c, int clear)
118   char flags[4];
119   static int prev_volume = -1;
120   static int prev_header_len = -1;
121   WINDOW *w = screen->top_window.w;
123   if(prev_header_len!=strlen(header))
124     {
125       prev_header_len = strlen(header);
126       clear = 1;
127     }
129   if(clear)
130     {
131       wmove(w, 0, 0);
132       wclrtoeol(w);
133     }
135   if(prev_volume!=c->status->volume || clear)
136     {
137       char buf[32];
139       if( header[0] )
140         {
141           colors_use(w, COLOR_TITLE_BOLD);
142           mvwaddstr(w, 0, 0, header);
143         }
144       else
145         {
146           colors_use(w, COLOR_TITLE_BOLD);
147           waddstr(w, get_key_names(CMD_SCREEN_HELP, FALSE));
148           colors_use(w, COLOR_TITLE);
149           waddstr(w, _(":Help  "));
150           colors_use(w, COLOR_TITLE_BOLD);
151           waddstr(w, get_key_names(CMD_SCREEN_PLAY, FALSE));
152           colors_use(w, COLOR_TITLE);
153           waddstr(w, _(":Playlist  "));
154           colors_use(w, COLOR_TITLE_BOLD);
155           waddstr(w, get_key_names(CMD_SCREEN_FILE, FALSE));
156           colors_use(w, COLOR_TITLE);
157           waddstr(w, _(":Browse"));
158         }
159       if( c->status->volume==MPD_STATUS_NO_VOLUME )
160         {
161           snprintf(buf, 32, _("Volume n/a "));
162         }
163       else
164         {
165           snprintf(buf, 32, _(" Volume %d%%"), c->status->volume); 
166         }
167       colors_use(w, COLOR_TITLE);
168       mvwaddstr(w, 0, screen->top_window.cols-strlen(buf), buf);
170       flags[0] = 0;
171       if( c->status->repeat )
172         strcat(flags, "r");
173       if( c->status->random )
174         strcat(flags, "z");
175       if( c->status->crossfade )
176         strcat(flags, "x");
177       if( c->status->updatingDb )
178         strcat(flags, "U");
179       colors_use(w, COLOR_LINE);
180       mvwhline(w, 1, 0, ACS_HLINE, screen->top_window.cols);
181       if( flags[0] )
182         {
183           wmove(w,1,screen->top_window.cols-strlen(flags)-3);
184           waddch(w, '[');
185           colors_use(w, COLOR_LINE_BOLD);
186           waddstr(w, flags);
187           colors_use(w, COLOR_LINE);
188           waddch(w, ']');
189         }
190       wnoutrefresh(w);
191     }
194 static void
195 paint_progress_window(mpdclient_t *c)
197   double p;
198   int width;
199   int elapsedTime = c->status->elapsedTime;
201   if( c->status==NULL || IS_STOPPED(c->status->state) )
202     {
203       mvwhline(screen->progress_window.w, 0, 0, ACS_HLINE, 
204                screen->progress_window.cols);
205       wnoutrefresh(screen->progress_window.w);
206       return;
207     }
209   if( c->song && seek_id == c->song->id )
210     elapsedTime = seek_target_time;
212   p = ((double) elapsedTime) / ((double) c->status->totalTime);
213   
214   width = (int) (p * (double) screen->progress_window.cols);
215   mvwhline(screen->progress_window.w, 
216            0, 0,
217            ACS_HLINE, 
218            screen->progress_window.cols);
219   whline(screen->progress_window.w, '=', width-1);
220   mvwaddch(screen->progress_window.w, 0, width-1, 'O');
221   wnoutrefresh(screen->progress_window.w);
224 static void 
225 paint_status_window(mpdclient_t *c)
227   WINDOW *w = screen->status_window.w;
228   mpd_Status *status = c->status;
229   mpd_Song *song   = c->song;
230   int elapsedTime = c->status->elapsedTime;
231   char *str = NULL;
232   int x = 0;
234   if( time(NULL) - screen->status_timestamp <= STATUS_MESSAGE_TIMEOUT )
235     return;
236   
237   
238   wmove(w, 0, 0);
239   wclrtoeol(w);
240   colors_use(w, COLOR_STATUS_BOLD);
241   
242   switch(status->state)
243     {
244     case MPD_STATUS_STATE_PLAY:
245       str = _("Playing:");
246       break;
247     case MPD_STATUS_STATE_PAUSE:
248       str = _("[Paused]");
249       break;
250     case MPD_STATUS_STATE_STOP:
251     default:
252       break;
253     }
255   if( str )
256     {
257       waddstr(w, str);
258       x += strlen(str)+1;
259     }
261   /* create time string */
262   memset(screen->buf, 0, screen->buf_size);
263   if( IS_PLAYING(status->state) || IS_PAUSED(status->state) )
264     {
265       if( status->totalTime > 0 )
266         {
267           if( c->song && seek_id == c->song->id )
268             elapsedTime = seek_target_time;
269           snprintf(screen->buf, screen->buf_size, 
270                    " [%i:%02i/%i:%02i]",
271                    elapsedTime/60, elapsedTime%60,
272                    status->totalTime/60,   status->totalTime%60 );
273         }
274       else
275         {
276           snprintf(screen->buf, screen->buf_size,  " [%d kbps]", status->bitRate );
277         }
278     }
279 #ifdef ENABLE_STATUS_LINE_CLOCK
280   else
281     {
282       time_t timep;
284       time(&timep);
285       strftime(screen->buf, screen->buf_size, "%X ",localtime(&timep));
286     }
287 #endif
289   /* display song */
290   if( (IS_PLAYING(status->state) || IS_PAUSED(status->state)) )
291     {
292       char songname[STATUS_LINE_MAX_SIZE];
293       int width = COLS-x-strlen(screen->buf);
295       if( song )
296         strfsong(songname, STATUS_LINE_MAX_SIZE, STATUS_FORMAT, song);
297       else
298         songname[0] = '\0';
300       colors_use(w, COLOR_STATUS);
301 #ifdef ENABLE_SCROLLING
302       if( strlen(songname) > width )
303         {
304           static  scroll_state_t st = { 0, 0 };
305           char *tmp = strscroll(songname, " *** ", width, &st);
307           strcpy(songname, tmp);
308           g_free(tmp);    
309         }
310 #endif
311       mvwaddnstr(w, 0, x, songname, width);
312     } 
314   /* distplay time string */
315   if( screen->buf[0] )
316     {
317       x = screen->status_window.cols - strlen(screen->buf);
318       colors_use(w, COLOR_STATUS_TIME);
319       mvwaddstr(w, 0, x, screen->buf);
320     }
322   wnoutrefresh(w);
325 GList *
326 screen_free_string_list(GList *list)
328   GList *l = g_list_first(list);
329   
330   while(l)
331     {
332       g_free(l->data);
333       l->data = NULL;
334       l=l->next;
335     }
336   g_list_free(list);
337   return NULL;
340 int
341 screen_exit(void)
343   endwin();
344   if( screen )
345     {
346       GList *list = g_list_first(screen->screen_list);
348       /* close and exit all screens (playlist,browse,help...) */
349       while( list )
350         {
351           screen_functions_t *mode_fn = list->data;
353           if( mode_fn && mode_fn->close )
354             mode_fn->close();
355           if( mode_fn && mode_fn->exit )
356             mode_fn->exit();
357           list->data = NULL;
358           list=list->next;
359         }
360       g_list_free(screen->screen_list);
361       screen_free_string_list(screen->find_history);
362       g_free(screen->buf);
363       g_free(screen->findbuf);
364       
365       g_free(screen);
366       screen = NULL;
367     }
368   return 0;
371 void
372 screen_resize(void)
374   GList *list;
376   D("Resize rows %d->%d, cols %d->%d\n",screen->rows,LINES,screen->cols,COLS);
377   if( COLS<SCREEN_MIN_COLS || LINES<SCREEN_MIN_ROWS )
378     {
379       screen_exit();
380       fprintf(stderr, _("Error: Screen to small!\n"));
381       exit(EXIT_FAILURE);
382     }
384   resizeterm(LINES, COLS);
386   screen->cols = COLS;
387   screen->rows = LINES;
389   /* top window */
390   screen->top_window.cols = screen->cols;
391   wresize(screen->top_window.w, 2, screen->cols);
393   /* main window */
394   screen->main_window.cols = screen->cols;
395   screen->main_window.rows = screen->rows-4;
396   wresize(screen->main_window.w, screen->main_window.rows, screen->cols);
397   wclear(screen->main_window.w);
399   /* progress window */
400   screen->progress_window.cols = screen->cols;
401   wresize(screen->progress_window.w, 1, screen->cols);
402   mvwin(screen->progress_window.w, screen->rows-2, 0);
404   /* status window */
405   screen->status_window.cols = screen->cols;
406   wresize(screen->status_window.w, 1, screen->cols);
407   mvwin(screen->status_window.w, screen->rows-1, 0);
409   screen->buf_size = screen->cols;
410   g_free(screen->buf);
411   screen->buf = g_malloc(screen->cols);
413   list = g_list_first(screen->screen_list);
414   while( list )
415     {
416       screen_functions_t *mode_fn = list->data;
418       if( mode_fn && mode_fn->resize )
419         mode_fn->resize(screen->main_window.cols, screen->main_window.rows);
421       list=list->next;
422     }
424   /* ? - without this the cursor becomes visible with aterm & Eterm */
425   curs_set(1);
426   curs_set(0);     
428   screen->painted = 0;
431 void 
432 screen_status_message(char *msg)
434   WINDOW *w = screen->status_window.w;
436   wmove(w, 0, 0);
437   wclrtoeol(w);
438   colors_use(w, COLOR_STATUS_ALERT);
439   waddstr(w, msg);
440   wnoutrefresh(w);
441   screen->status_timestamp = time(NULL);
444 void 
445 screen_status_printf(char *format, ...)
447   char buffer[STATUS_LINE_MAX_SIZE];
448   va_list ap;
449   
450   va_start(ap,format);
451   vsnprintf(buffer,sizeof(buffer),format,ap);
452   va_end(ap);
453   screen_status_message(buffer);
456 int
457 screen_init(mpdclient_t *c)
459   GList *list;
461   /* initialize the curses library */
462   initscr();
463   /* initialize color support */
464   colors_start();
465   /* tell curses not to do NL->CR/NL on output */
466   nonl();          
467   /*  use raw mode (ignore interrupt,quit,suspend, and flow control ) */
468   raw();
469   /* don't echo input */
470   noecho();    
471   /* set cursor invisible */     
472   curs_set(0);     
473   /* enable extra keys */
474   keypad(stdscr, TRUE);  
475   /* return from getch() without blocking */
476   timeout(SCREEN_TIMEOUT);
477   /* initialize mouse support */
478 #ifdef HAVE_GETMOUSE
479   mousemask(ALL_MOUSE_EVENTS, NULL);
480 #endif
482   if( COLS<SCREEN_MIN_COLS || LINES<SCREEN_MIN_ROWS )
483     {
484       fprintf(stderr, _("Error: Screen to small!\n"));
485       exit(EXIT_FAILURE);
486     }
488   screen = g_malloc(sizeof(screen_t));
489   memset(screen, 0, sizeof(screen_t));
490   screen->mode = SCREEN_PLAY_WINDOW;
491   screen->cols = COLS;
492   screen->rows = LINES;
493   screen->buf  = g_malloc(screen->cols);
494   screen->buf_size = screen->cols;
495   screen->findbuf = NULL;
496   screen->painted = 0;
497   screen->start_timestamp = time(NULL);
498   screen->input_timestamp = time(NULL);
499   screen->last_cmd = CMD_NONE;
501   /* create top window */
502   screen->top_window.rows = 2;
503   screen->top_window.cols = screen->cols;
504   screen->top_window.w = newwin(screen->top_window.rows, 
505                                   screen->top_window.cols,
506                                   0, 0);
507   leaveok(screen->top_window.w, TRUE);
508   keypad(screen->top_window.w, TRUE);  
510   /* create main window */
511   screen->main_window.rows = screen->rows-4;
512   screen->main_window.cols = screen->cols;
513   screen->main_window.w = newwin(screen->main_window.rows, 
514                                  screen->main_window.cols,
515                                  2, 
516                                  0);
518   //  leaveok(screen->main_window.w, TRUE); temporary disabled
519   keypad(screen->main_window.w, TRUE);  
521   /* create progress window */
522   screen->progress_window.rows = 1;
523   screen->progress_window.cols = screen->cols;
524   screen->progress_window.w = newwin(screen->progress_window.rows, 
525                                      screen->progress_window.cols,
526                                      screen->rows-2, 
527                                      0);
528   leaveok(screen->progress_window.w, TRUE);
529   
530   /* create status window */
531   screen->status_window.rows = 1;
532   screen->status_window.cols = screen->cols;
533   screen->status_window.w = newwin(screen->status_window.rows, 
534                                    screen->status_window.cols,
535                                    screen->rows-1, 
536                                    0);
538   leaveok(screen->status_window.w, FALSE);
539   keypad(screen->status_window.w, TRUE);  
541   if( options.enable_colors )
542     {
543       /* set background attributes */
544       wbkgd(stdscr, COLOR_PAIR(COLOR_LIST)); 
545       wbkgd(screen->main_window.w,     COLOR_PAIR(COLOR_LIST));
546       wbkgd(screen->top_window.w,      COLOR_PAIR(COLOR_TITLE));
547       wbkgd(screen->progress_window.w, COLOR_PAIR(COLOR_PROGRESSBAR));
548       wbkgd(screen->status_window.w,   COLOR_PAIR(COLOR_STATUS));
549       colors_use(screen->progress_window.w, COLOR_PROGRESSBAR);
550     }
552   /* initialize screens */
553   screen->screen_list = NULL;
554   screen->screen_list = g_list_append(screen->screen_list, 
555                                       (gpointer) get_screen_playlist());
556   screen->screen_list = g_list_append(screen->screen_list, 
557                                       (gpointer) get_screen_browse());
558   screen->screen_list = g_list_append(screen->screen_list, 
559                                       (gpointer) get_screen_help());
560 #ifdef ENABLE_KEYDEF_SCREEN
561   screen->screen_list = g_list_append(screen->screen_list, 
562                                       (gpointer) get_screen_keydef());
563 #endif
564 #ifdef ENABLE_CLOCK_SCREEN
565   screen->screen_list = g_list_append(screen->screen_list, 
566                                       (gpointer) get_screen_clock());
567 #endif
569   list = screen->screen_list;
570   while( list )
571     {
572       screen_functions_t *fn = list->data;
573       
574       if( fn && fn->init )
575         fn->init(screen->main_window.w, 
576                  screen->main_window.cols,
577                  screen->main_window.rows);
578       
579       list = list->next;
580     }
582   mode_fn = get_screen_playlist();
583   if( mode_fn && mode_fn->open )
584     mode_fn->open(screen, c);
586   /* initialize wreadln */
587   wrln_resize_callback = screen_resize;
588   wrln_max_history_length = 16;
590   return 0;
593 void 
594 screen_paint(mpdclient_t *c)
596   D("screen_paint()\n");
597   /* paint the title/header window */
598   if( mode_fn && mode_fn->get_title )
599     paint_top_window(mode_fn->get_title(screen->buf,screen->buf_size), c, 1);
600   else
601     paint_top_window("", c, 1);
603   /* paint the main window */
604   wclear(screen->main_window.w);
605   if( mode_fn && mode_fn->paint )
606     mode_fn->paint(screen, c);
607   
608   paint_progress_window(c);
609   paint_status_window(c);
610   screen->painted = 1;
611   wmove(screen->main_window.w, 0, 0);  
612   wnoutrefresh(screen->main_window.w);
614   /* tell curses to update */
615   doupdate();
618 void 
619 screen_update(mpdclient_t *c)
621   static int repeat = -1;
622   static int random = -1;
623   static int crossfade = -1;
624   static int dbupdate = -1;
625   list_window_t *lw = NULL;
627   if( !screen->painted )
628     return screen_paint(c);
630   /* print a message if mpd status has changed */
631   if( repeat<0 )
632     {
633       repeat = c->status->repeat;
634       random = c->status->random;
635       crossfade = c->status->crossfade;
636       dbupdate = c->status->updatingDb;
637     }
638   if( repeat != c->status->repeat )
639     screen_status_printf(c->status->repeat ? 
640                          _("Repeat is on") :
641                          _("Repeat is off"));
642   if( random != c->status->random )
643     screen_status_printf(c->status->random ?
644                          _("Random is on") :
645                          _("Random is off"));
646                          
647   if( crossfade != c->status->crossfade )
648     screen_status_printf(_("Crossfade %d seconds"), c->status->crossfade);
649   if( dbupdate && dbupdate != c->status->updatingDb )
650     {
651       screen_status_printf(_("Database updated!"));
652       mpdclient_browse_callback(c, BROWSE_DB_UPDATED, NULL);
653     }
655   repeat = c->status->repeat;
656   random = c->status->random;
657   crossfade = c->status->crossfade;
658   dbupdate = c->status->updatingDb;
660   /* update title/header window */
661   if( welcome && screen->last_cmd==CMD_NONE &&
662       time(NULL)-screen->start_timestamp <= SCREEN_WELCOME_TIME)
663     paint_top_window("", c, 0);
664   else if( mode_fn && mode_fn->get_title )
665     {
666       paint_top_window(mode_fn->get_title(screen->buf,screen->buf_size), c, 0);
667       welcome = FALSE;
668     }
669   else
670     paint_top_window("", c, 0);
672   /* update the main window */
673   if( mode_fn && mode_fn->paint )
674     mode_fn->update(screen, c);
676   if( mode_fn && mode_fn->get_lw )
677     lw = mode_fn->get_lw();
679   /* update progress window */
680   paint_progress_window(c);
682   /* update status window */
683   paint_status_window(c);
685   /* move the cursor to the selected row in the main window */
686   if( lw )
687     wmove(screen->main_window.w, LW_ROW(lw), 0);   
688   else
689     wmove(screen->main_window.w, 0, 0);   
690   wnoutrefresh(screen->main_window.w);
692   /* tell curses to update */
693   doupdate();
696 void
697 screen_idle(mpdclient_t *c)
699   if( c->song && seek_id ==  c->song->id &&
700       (screen->last_cmd == CMD_SEEK_FORWARD || 
701        screen->last_cmd == CMD_SEEK_BACKWARD) )
702     {
703       mpdclient_cmd_seek(c, seek_id, seek_target_time);
704     }
706   screen->last_cmd = CMD_NONE;
707   seek_id = -1;
710 #ifdef HAVE_GETMOUSE
711 int
712 screen_get_mouse_event(mpdclient_t *c,
713                        list_window_t *lw, int lw_length, 
714                        unsigned long *bstate, int *row)
716   MEVENT event;
718   /* retreive the mouse event from ncurses */
719   getmouse(&event);
720   D("mouse: id=%d  y=%d,x=%d,z=%d\n",event.id,event.y,event.x,event.z);
721   /* calculate the selected row in the list window */
722   *row = event.y - screen->top_window.rows;
723   /* copy button state bits */
724   *bstate = event.bstate;
725   /* if button 2 was pressed switch screen */
726   if( event.bstate & BUTTON2_CLICKED )
727     {
728       screen_cmd(c, CMD_SCREEN_NEXT);
729       return 1;
730     }
731   /* if the even occured above the list window move up */
732   if( *row<0 && lw )
733     {
734       if( event.bstate & BUTTON3_CLICKED )
735         list_window_first(lw);
736       else
737         list_window_previous_page(lw);
738       return 1;
739     }
740    /* if the even occured below the list window move down */
741   if( *row>=lw->rows && lw )
742     {
743       if( event.bstate & BUTTON3_CLICKED )
744         list_window_last(lw, lw_length);
745       else
746         list_window_next_page(lw, lw_length);
747       return 1;
748     } 
749   return 0;
751 #endif
753 void 
754 screen_cmd(mpdclient_t *c, command_t cmd)
756   screen_mode_t new_mode = screen->mode;
758   screen->input_timestamp = time(NULL);
759   screen->last_cmd = cmd;
760   welcome = FALSE;
762   if( mode_fn && mode_fn->cmd && mode_fn->cmd(screen, c, cmd) )
763     return;
765   switch(cmd)
766     {
767     case CMD_PLAY:
768       mpdclient_cmd_play(c, MPD_PLAY_AT_BEGINNING);
769       break;
770     case CMD_PAUSE:
771       mpdclient_cmd_pause(c, !IS_PAUSED(c->status->state));
772       break;
773     case CMD_STOP:
774       mpdclient_cmd_stop(c);
775       break;
776     case CMD_SEEK_FORWARD:
777       if( !IS_STOPPED(c->status->state) )
778         {
779           if( c->song && seek_id != c->song->id )
780             {
781               seek_id = c->song->id;
782               seek_target_time = c->status->elapsedTime;
783             }
784           seek_target_time++;
785           if( seek_target_time < c->status->totalTime )
786             break;
787           seek_target_time = c->status->totalTime;
788           /* seek_target_time=0; */
789         }
790       break;
791       /* fall through... */
792     case CMD_TRACK_NEXT:
793       if( !IS_STOPPED(c->status->state) )
794         mpdclient_cmd_next(c);
795       break;
796     case CMD_SEEK_BACKWARD:
797       if( !IS_STOPPED(c->status->state) )
798         {
799           if( seek_id != c->song->id )
800             {
801               seek_id = c->song->id;
802               seek_target_time = c->status->elapsedTime;
803             }
804           seek_target_time--;
805           if( seek_target_time < 0 )
806             seek_target_time=0;
807         }
808       break;
809     case CMD_TRACK_PREVIOUS:
810       if( !IS_STOPPED(c->status->state) )
811         mpdclient_cmd_prev(c);
812       break;   
813     case CMD_SHUFFLE:
814       if( mpdclient_cmd_shuffle(c) == 0 )
815         screen_status_message(_("Shuffled playlist!"));
816       break;
817     case CMD_CLEAR:
818       if( mpdclient_cmd_clear(c) == 0 )
819         screen_status_message(_("Cleared playlist!"));
820       break;
821     case CMD_REPEAT:
822       mpdclient_cmd_repeat(c, !c->status->repeat);
823       break;
824     case CMD_RANDOM:
825       mpdclient_cmd_random(c, !c->status->random);
826       break;
827     case CMD_CROSSFADE:
828       mpdclient_cmd_crossfade(c, c->status->crossfade ? 0 : CROSSFADE_TIME);
829       break;
830     case CMD_DB_UPDATE:
831       if( !c->status->updatingDb )
832         {
833           if( mpdclient_cmd_db_update_utf8(c,NULL)==0 )
834             screen_status_printf(_("Database update started!"));
835         }
836       else
837         screen_status_printf(_("Database update running..."));
838       break;
839     case CMD_VOLUME_UP:
840       if( c->status->volume!=MPD_STATUS_NO_VOLUME && c->status->volume<100 )
841         mpdclient_cmd_volume(c, ++c->status->volume);
842       break;
843     case CMD_VOLUME_DOWN:
844       if( c->status->volume!=MPD_STATUS_NO_VOLUME && c->status->volume>0 )
845         mpdclient_cmd_volume(c, --c->status->volume);
846       break;
847     case CMD_TOGGLE_FIND_WRAP:
848       options.find_wrap = !options.find_wrap;
849       screen_status_printf(options.find_wrap ? 
850                            _("Find mode: Wrapped") :
851                            _("Find mode: Normal"));
852       break;
853     case CMD_TOGGLE_AUTOCENTER:
854       options.auto_center = !options.auto_center;
855       screen_status_printf(options.auto_center ?
856                            _("Auto center mode: On") :
857                            _("Auto center mode: Off"));
858       break;
859     case CMD_SCREEN_UPDATE:
860       screen->painted = 0;
861       break;
862     case CMD_SCREEN_PREVIOUS:
863       if( screen->mode > SCREEN_PLAY_WINDOW )
864         new_mode = screen->mode - 1;
865       else
866         new_mode = SCREEN_HELP_WINDOW-1;
867       switch_screen_mode(new_mode, c);
868       break;
869     case CMD_SCREEN_NEXT:
870       new_mode = screen->mode + 1;
871       if( new_mode >= SCREEN_HELP_WINDOW )
872         new_mode = SCREEN_PLAY_WINDOW;
873       switch_screen_mode(new_mode, c);
874       break;
875     case CMD_SCREEN_PLAY:
876       switch_screen_mode(SCREEN_PLAY_WINDOW, c);
877       break;
878     case CMD_SCREEN_FILE:
879       switch_screen_mode(SCREEN_FILE_WINDOW, c);
880       break;
881     case CMD_SCREEN_SEARCH:
882       switch_screen_mode(SCREEN_SEARCH_WINDOW, c);
883       break;
884     case CMD_SCREEN_HELP:
885       switch_screen_mode(SCREEN_HELP_WINDOW, c);
886       break;
887 #ifdef ENABLE_KEYDEF_SCREEN 
888     case CMD_SCREEN_KEYDEF:
889       switch_screen_mode(SCREEN_KEYDEF_WINDOW, c);
890       break;
891 #endif
892 #ifdef ENABLE_CLOCK_SCREEN 
893     case CMD_SCREEN_CLOCK:
894       switch_screen_mode(SCREEN_CLOCK_WINDOW, c);
895       break;
896 #endif
897     case CMD_QUIT:
898       exit(EXIT_SUCCESS);
899     default:
900       break;
901     }