Code

c2c1e7fdaeff5c69fdbb8fabea8b175592c35e83
[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 #ifdef ENABLE_RAW_MODE
469   raw();
470 #endif
471   /* don't echo input */
472   noecho();    
473   /* set cursor invisible */     
474   curs_set(0);     
475   /* enable extra keys */
476   keypad(stdscr, TRUE);  
477   /* return from getch() without blocking */
478   timeout(SCREEN_TIMEOUT);
479   /* initialize mouse support */
480 #ifdef HAVE_GETMOUSE
481   mousemask(ALL_MOUSE_EVENTS, NULL);
482 #endif
484   if( COLS<SCREEN_MIN_COLS || LINES<SCREEN_MIN_ROWS )
485     {
486       fprintf(stderr, _("Error: Screen to small!\n"));
487       exit(EXIT_FAILURE);
488     }
490   screen = g_malloc(sizeof(screen_t));
491   memset(screen, 0, sizeof(screen_t));
492   screen->mode = SCREEN_PLAY_WINDOW;
493   screen->cols = COLS;
494   screen->rows = LINES;
495   screen->buf  = g_malloc(screen->cols);
496   screen->buf_size = screen->cols;
497   screen->findbuf = NULL;
498   screen->painted = 0;
499   screen->start_timestamp = time(NULL);
500   screen->input_timestamp = time(NULL);
501   screen->last_cmd = CMD_NONE;
503   /* create top window */
504   screen->top_window.rows = 2;
505   screen->top_window.cols = screen->cols;
506   screen->top_window.w = newwin(screen->top_window.rows, 
507                                   screen->top_window.cols,
508                                   0, 0);
509   leaveok(screen->top_window.w, TRUE);
510   keypad(screen->top_window.w, TRUE);  
512   /* create main window */
513   screen->main_window.rows = screen->rows-4;
514   screen->main_window.cols = screen->cols;
515   screen->main_window.w = newwin(screen->main_window.rows, 
516                                  screen->main_window.cols,
517                                  2, 
518                                  0);
520   //  leaveok(screen->main_window.w, TRUE); temporary disabled
521   keypad(screen->main_window.w, TRUE);  
523   /* create progress window */
524   screen->progress_window.rows = 1;
525   screen->progress_window.cols = screen->cols;
526   screen->progress_window.w = newwin(screen->progress_window.rows, 
527                                      screen->progress_window.cols,
528                                      screen->rows-2, 
529                                      0);
530   leaveok(screen->progress_window.w, TRUE);
531   
532   /* create status window */
533   screen->status_window.rows = 1;
534   screen->status_window.cols = screen->cols;
535   screen->status_window.w = newwin(screen->status_window.rows, 
536                                    screen->status_window.cols,
537                                    screen->rows-1, 
538                                    0);
540   leaveok(screen->status_window.w, FALSE);
541   keypad(screen->status_window.w, TRUE);  
543   if( options.enable_colors )
544     {
545       /* set background attributes */
546       wbkgd(stdscr, COLOR_PAIR(COLOR_LIST)); 
547       wbkgd(screen->main_window.w,     COLOR_PAIR(COLOR_LIST));
548       wbkgd(screen->top_window.w,      COLOR_PAIR(COLOR_TITLE));
549       wbkgd(screen->progress_window.w, COLOR_PAIR(COLOR_PROGRESSBAR));
550       wbkgd(screen->status_window.w,   COLOR_PAIR(COLOR_STATUS));
551       colors_use(screen->progress_window.w, COLOR_PROGRESSBAR);
552     }
554   /* initialize screens */
555   screen->screen_list = NULL;
556   screen->screen_list = g_list_append(screen->screen_list, 
557                                       (gpointer) get_screen_playlist());
558   screen->screen_list = g_list_append(screen->screen_list, 
559                                       (gpointer) get_screen_browse());
560   screen->screen_list = g_list_append(screen->screen_list, 
561                                       (gpointer) get_screen_help());
562 #ifdef ENABLE_KEYDEF_SCREEN
563   screen->screen_list = g_list_append(screen->screen_list, 
564                                       (gpointer) get_screen_keydef());
565 #endif
566 #ifdef ENABLE_CLOCK_SCREEN
567   screen->screen_list = g_list_append(screen->screen_list, 
568                                       (gpointer) get_screen_clock());
569 #endif
571   list = screen->screen_list;
572   while( list )
573     {
574       screen_functions_t *fn = list->data;
575       
576       if( fn && fn->init )
577         fn->init(screen->main_window.w, 
578                  screen->main_window.cols,
579                  screen->main_window.rows);
580       
581       list = list->next;
582     }
584   mode_fn = get_screen_playlist();
585   if( mode_fn && mode_fn->open )
586     mode_fn->open(screen, c);
588   /* initialize wreadln */
589   wrln_resize_callback = screen_resize;
590   wrln_max_history_length = 16;
592   return 0;
595 void 
596 screen_paint(mpdclient_t *c)
598   D("screen_paint()\n");
599   /* paint the title/header window */
600   if( mode_fn && mode_fn->get_title )
601     paint_top_window(mode_fn->get_title(screen->buf,screen->buf_size), c, 1);
602   else
603     paint_top_window("", c, 1);
605   /* paint the main window */
606   wclear(screen->main_window.w);
607   if( mode_fn && mode_fn->paint )
608     mode_fn->paint(screen, c);
609   
610   paint_progress_window(c);
611   paint_status_window(c);
612   screen->painted = 1;
613   wmove(screen->main_window.w, 0, 0);  
614   wnoutrefresh(screen->main_window.w);
616   /* tell curses to update */
617   doupdate();
620 void 
621 screen_update(mpdclient_t *c)
623   static int repeat = -1;
624   static int random = -1;
625   static int crossfade = -1;
626   static int dbupdate = -1;
627   list_window_t *lw = NULL;
629   if( !screen->painted )
630     return screen_paint(c);
632   /* print a message if mpd status has changed */
633   if( repeat<0 )
634     {
635       repeat = c->status->repeat;
636       random = c->status->random;
637       crossfade = c->status->crossfade;
638       dbupdate = c->status->updatingDb;
639     }
640   if( repeat != c->status->repeat )
641     screen_status_printf(c->status->repeat ? 
642                          _("Repeat is on") :
643                          _("Repeat is off"));
644   if( random != c->status->random )
645     screen_status_printf(c->status->random ?
646                          _("Random is on") :
647                          _("Random is off"));
648                          
649   if( crossfade != c->status->crossfade )
650     screen_status_printf(_("Crossfade %d seconds"), c->status->crossfade);
651   if( dbupdate && dbupdate != c->status->updatingDb )
652     {
653       screen_status_printf(_("Database updated!"));
654       mpdclient_browse_callback(c, BROWSE_DB_UPDATED, NULL);
655     }
657   repeat = c->status->repeat;
658   random = c->status->random;
659   crossfade = c->status->crossfade;
660   dbupdate = c->status->updatingDb;
662   /* update title/header window */
663   if( welcome && screen->last_cmd==CMD_NONE &&
664       time(NULL)-screen->start_timestamp <= SCREEN_WELCOME_TIME)
665     paint_top_window("", c, 0);
666   else if( mode_fn && mode_fn->get_title )
667     {
668       paint_top_window(mode_fn->get_title(screen->buf,screen->buf_size), c, 0);
669       welcome = FALSE;
670     }
671   else
672     paint_top_window("", c, 0);
674   /* update the main window */
675   if( mode_fn && mode_fn->paint )
676     mode_fn->update(screen, c);
678   if( mode_fn && mode_fn->get_lw )
679     lw = mode_fn->get_lw();
681   /* update progress window */
682   paint_progress_window(c);
684   /* update status window */
685   paint_status_window(c);
687   /* move the cursor to the selected row in the main window */
688   if( lw )
689     wmove(screen->main_window.w, LW_ROW(lw), 0);   
690   else
691     wmove(screen->main_window.w, 0, 0);   
692   wnoutrefresh(screen->main_window.w);
694   /* tell curses to update */
695   doupdate();
698 void
699 screen_idle(mpdclient_t *c)
701   if( c->song && seek_id ==  c->song->id &&
702       (screen->last_cmd == CMD_SEEK_FORWARD || 
703        screen->last_cmd == CMD_SEEK_BACKWARD) )
704     {
705       mpdclient_cmd_seek(c, seek_id, seek_target_time);
706     }
708   screen->last_cmd = CMD_NONE;
709   seek_id = -1;
712 #ifdef HAVE_GETMOUSE
713 int
714 screen_get_mouse_event(mpdclient_t *c,
715                        list_window_t *lw, int lw_length, 
716                        unsigned long *bstate, int *row)
718   MEVENT event;
720   /* retreive the mouse event from ncurses */
721   getmouse(&event);
722   D("mouse: id=%d  y=%d,x=%d,z=%d\n",event.id,event.y,event.x,event.z);
723   /* calculate the selected row in the list window */
724   *row = event.y - screen->top_window.rows;
725   /* copy button state bits */
726   *bstate = event.bstate;
727   /* if button 2 was pressed switch screen */
728   if( event.bstate & BUTTON2_CLICKED )
729     {
730       screen_cmd(c, CMD_SCREEN_NEXT);
731       return 1;
732     }
733   /* if the even occured above the list window move up */
734   if( *row<0 && lw )
735     {
736       if( event.bstate & BUTTON3_CLICKED )
737         list_window_first(lw);
738       else
739         list_window_previous_page(lw);
740       return 1;
741     }
742    /* if the even occured below the list window move down */
743   if( *row>=lw->rows && lw )
744     {
745       if( event.bstate & BUTTON3_CLICKED )
746         list_window_last(lw, lw_length);
747       else
748         list_window_next_page(lw, lw_length);
749       return 1;
750     } 
751   return 0;
753 #endif
755 void 
756 screen_cmd(mpdclient_t *c, command_t cmd)
758   screen_mode_t new_mode = screen->mode;
760   screen->input_timestamp = time(NULL);
761   screen->last_cmd = cmd;
762   welcome = FALSE;
764   if( mode_fn && mode_fn->cmd && mode_fn->cmd(screen, c, cmd) )
765     return;
767   switch(cmd)
768     {
769     case CMD_PLAY:
770       mpdclient_cmd_play(c, MPD_PLAY_AT_BEGINNING);
771       break;
772     case CMD_PAUSE:
773       mpdclient_cmd_pause(c, !IS_PAUSED(c->status->state));
774       break;
775     case CMD_STOP:
776       mpdclient_cmd_stop(c);
777       break;
778     case CMD_SEEK_FORWARD:
779       if( !IS_STOPPED(c->status->state) )
780         {
781           if( c->song && seek_id != c->song->id )
782             {
783               seek_id = c->song->id;
784               seek_target_time = c->status->elapsedTime;
785             }
786           seek_target_time++;
787           if( seek_target_time < c->status->totalTime )
788             break;
789           seek_target_time = c->status->totalTime;
790           /* seek_target_time=0; */
791         }
792       break;
793       /* fall through... */
794     case CMD_TRACK_NEXT:
795       if( !IS_STOPPED(c->status->state) )
796         mpdclient_cmd_next(c);
797       break;
798     case CMD_SEEK_BACKWARD:
799       if( !IS_STOPPED(c->status->state) )
800         {
801           if( seek_id != c->song->id )
802             {
803               seek_id = c->song->id;
804               seek_target_time = c->status->elapsedTime;
805             }
806           seek_target_time--;
807           if( seek_target_time < 0 )
808             seek_target_time=0;
809         }
810       break;
811     case CMD_TRACK_PREVIOUS:
812       if( !IS_STOPPED(c->status->state) )
813         mpdclient_cmd_prev(c);
814       break;   
815     case CMD_SHUFFLE:
816       if( mpdclient_cmd_shuffle(c) == 0 )
817         screen_status_message(_("Shuffled playlist!"));
818       break;
819     case CMD_CLEAR:
820       if( mpdclient_cmd_clear(c) == 0 )
821         screen_status_message(_("Cleared playlist!"));
822       break;
823     case CMD_REPEAT:
824       mpdclient_cmd_repeat(c, !c->status->repeat);
825       break;
826     case CMD_RANDOM:
827       mpdclient_cmd_random(c, !c->status->random);
828       break;
829     case CMD_CROSSFADE:
830       mpdclient_cmd_crossfade(c, c->status->crossfade ? 0 : CROSSFADE_TIME);
831       break;
832     case CMD_DB_UPDATE:
833       if( !c->status->updatingDb )
834         {
835           if( mpdclient_cmd_db_update_utf8(c,NULL)==0 )
836             screen_status_printf(_("Database update started!"));
837         }
838       else
839         screen_status_printf(_("Database update running..."));
840       break;
841     case CMD_VOLUME_UP:
842       if( c->status->volume!=MPD_STATUS_NO_VOLUME && c->status->volume<100 )
843         mpdclient_cmd_volume(c, ++c->status->volume);
844       break;
845     case CMD_VOLUME_DOWN:
846       if( c->status->volume!=MPD_STATUS_NO_VOLUME && c->status->volume>0 )
847         mpdclient_cmd_volume(c, --c->status->volume);
848       break;
849     case CMD_TOGGLE_FIND_WRAP:
850       options.find_wrap = !options.find_wrap;
851       screen_status_printf(options.find_wrap ? 
852                            _("Find mode: Wrapped") :
853                            _("Find mode: Normal"));
854       break;
855     case CMD_TOGGLE_AUTOCENTER:
856       options.auto_center = !options.auto_center;
857       screen_status_printf(options.auto_center ?
858                            _("Auto center mode: On") :
859                            _("Auto center mode: Off"));
860       break;
861     case CMD_SCREEN_UPDATE:
862       screen->painted = 0;
863       break;
864     case CMD_SCREEN_PREVIOUS:
865       if( screen->mode > SCREEN_PLAY_WINDOW )
866         new_mode = screen->mode - 1;
867       else
868         new_mode = SCREEN_HELP_WINDOW-1;
869       switch_screen_mode(new_mode, c);
870       break;
871     case CMD_SCREEN_NEXT:
872       new_mode = screen->mode + 1;
873       if( new_mode >= SCREEN_HELP_WINDOW )
874         new_mode = SCREEN_PLAY_WINDOW;
875       switch_screen_mode(new_mode, c);
876       break;
877     case CMD_SCREEN_PLAY:
878       switch_screen_mode(SCREEN_PLAY_WINDOW, c);
879       break;
880     case CMD_SCREEN_FILE:
881       switch_screen_mode(SCREEN_FILE_WINDOW, c);
882       break;
883     case CMD_SCREEN_SEARCH:
884       switch_screen_mode(SCREEN_SEARCH_WINDOW, c);
885       break;
886     case CMD_SCREEN_HELP:
887       switch_screen_mode(SCREEN_HELP_WINDOW, c);
888       break;
889 #ifdef ENABLE_KEYDEF_SCREEN 
890     case CMD_SCREEN_KEYDEF:
891       switch_screen_mode(SCREEN_KEYDEF_WINDOW, c);
892       break;
893 #endif
894 #ifdef ENABLE_CLOCK_SCREEN 
895     case CMD_SCREEN_CLOCK:
896       switch_screen_mode(SCREEN_CLOCK_WINDOW, c);
897       break;
898 #endif
899     case CMD_QUIT:
900       exit(EXIT_SUCCESS);
901     default:
902       break;
903     }