Code

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