Code

0b335efa903f27bab16bdb1531ead40bddef391d
[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 struct screen_functions *get_screen_playlist(void);
54 extern struct screen_functions *get_screen_browse(void);
55 extern struct screen_functions *get_screen_help(void);
56 extern struct screen_functions *get_screen_search(void);
57 extern struct screen_functions *get_screen_artist(void);
58 extern struct screen_functions *get_screen_keydef(void);
59 extern struct screen_functions *get_screen_clock(void);
60 extern struct screen_functions *get_screen_lyrics(void);
62 typedef struct screen_functions * (*screen_get_mode_functions_fn_t) (void);
64 static const struct
65 {
66         gint id;
67         const gchar *name;
68         screen_get_mode_functions_fn_t get_mode_functions;
69 } screens[] = {
70         { SCREEN_PLAYLIST_ID, "playlist", get_screen_playlist },
71         { SCREEN_BROWSE_ID,   "browse",   get_screen_browse },
72 #ifdef ENABLE_ARTIST_SCREEN
73         { SCREEN_ARTIST_ID,   "artist",   get_screen_artist },
74 #endif
75         { SCREEN_HELP_ID,     "help",     get_screen_help },
76 #ifdef ENABLE_SEARCH_SCREEN
77         { SCREEN_SEARCH_ID,   "search",   get_screen_search },
78 #endif
79 #ifdef ENABLE_KEYDEF_SCREEN
80         { SCREEN_KEYDEF_ID,   "keydef",   get_screen_keydef },
81 #endif
82 #ifdef ENABLE_CLOCK_SCREEN
83         { SCREEN_CLOCK_ID,    "clock",    get_screen_clock },
84 #endif
85 #ifdef ENABLE_LYRICS_SCREEN
86         { SCREEN_LYRICS_ID,    "lyrics",    get_screen_lyrics },
87 #endif
88         { G_MAXINT, NULL,      NULL }
89 };
91 static gboolean welcome = TRUE;
92 static screen_t *screen = NULL;
93 static struct screen_functions *mode_fn = NULL;
94 static int seek_id = -1;
95 static int seek_target_time = 0;
97 gint
98 screen_get_id(const char *name)
99 {
100         gint i=0;
102         while (screens[i].name) {
103                 if (strcmp(name, screens[i].name) == 0)
104                         return screens[i].id;
105                 i++;
106         }
107         return -1;
110 static gint
111 lookup_mode(gint id)
113         gint i=0;
115         while (screens[i].name) {
116                 if (screens[i].id == id)
117                         return i;
118                 i++;
119         }
120         return -1;
123 gint get_cur_mode_id(void)
125         return screens[screen->mode].id;
128 static void
129 switch_screen_mode(gint id, mpdclient_t *c)
131         gint new_mode;
133         if( id == screens[screen->mode].id )
134                 return;
136         /* close the old mode */
137         if( mode_fn && mode_fn->close )
138                 mode_fn->close();
140         /* get functions for the new mode */
141         new_mode = lookup_mode(id);
142         if (new_mode>=0 && screens[new_mode].get_mode_functions) {
143                 D("switch_screen(%s)\n", screens[new_mode].name );
144                 mode_fn = screens[new_mode].get_mode_functions();
145                 screen->mode = new_mode;
146         }
148         screen->painted = 0;
150         /* open the new mode */
151         if (mode_fn && mode_fn->open)
152                 mode_fn->open(screen, c);
155 static void
156 screen_next_mode(mpdclient_t *c, int offset)
158         int max = g_strv_length(options.screen_list);
159         int current, next;
160         int i;
162         /* find current screen */
163         current = -1;
164         i = 0;
165         while (options.screen_list[i]) {
166                 if (strcmp(options.screen_list[i],
167                            screens[screen->mode].name) == 0)
168                         current = i;
169                 i++;
170         }
172         next = current + offset;
173         if (next<0)
174                 next = max-1;
175         else if (next>=max)
176                 next = 0;
178         D("current mode: %d:%d    next:%d\n", current, max, next);
179         switch_screen_mode(screen_get_id(options.screen_list[next]), c);
182 static void
183 paint_top_window2(const char *header, mpdclient_t *c)
185         char flags[5];
186         WINDOW *w = screen->top_window.w;
187         char buf[32];
189         if (header[0]) {
190                 colors_use(w, COLOR_TITLE_BOLD);
191                 mvwaddstr(w, 0, 0, header);
192         } else {
193                 colors_use(w, COLOR_TITLE_BOLD);
194                 waddstr(w, get_key_names(CMD_SCREEN_HELP, FALSE));
195                 colors_use(w, COLOR_TITLE);
196                 waddstr(w, _(":Help  "));
197                 colors_use(w, COLOR_TITLE_BOLD);
198                 waddstr(w, get_key_names(CMD_SCREEN_PLAY, FALSE));
199                 colors_use(w, COLOR_TITLE);
200                 waddstr(w, _(":Playlist  "));
201                 colors_use(w, COLOR_TITLE_BOLD);
202                 waddstr(w, get_key_names(CMD_SCREEN_FILE, FALSE));
203                 colors_use(w, COLOR_TITLE);
204                 waddstr(w, _(":Browse  "));
205 #ifdef ENABLE_ARTIST_SCREEN
206                 colors_use(w, COLOR_TITLE_BOLD);
207                 waddstr(w, get_key_names(CMD_SCREEN_ARTIST, FALSE));
208                 colors_use(w, COLOR_TITLE);
209                 waddstr(w, _(":Artist  "));
210 #endif
211 #ifdef ENABLE_SEARCH_SCREEN
212                 colors_use(w, COLOR_TITLE_BOLD);
213                 waddstr(w, get_key_names(CMD_SCREEN_SEARCH, FALSE));
214                 colors_use(w, COLOR_TITLE);
215                 waddstr(w, _(":Search  "));
216 #endif
217 #ifdef ENABLE_LYRICS_SCREEN
218                 colors_use(w, COLOR_TITLE_BOLD);
219                 waddstr(w, get_key_names(CMD_SCREEN_LYRICS, FALSE));
220                 colors_use(w, COLOR_TITLE);
221                 waddstr(w, _(":Lyrics  "));
222 #endif
223         }
224         if (c->status->volume==MPD_STATUS_NO_VOLUME) {
225                 g_snprintf(buf, 32, _("Volume n/a "));
226         } else {
227                 g_snprintf(buf, 32, _(" Volume %d%%"), c->status->volume);
228         }
229         colors_use(w, COLOR_TITLE);
230         mvwaddstr(w, 0, screen->top_window.cols-my_strlen(buf), buf);
232         flags[0] = 0;
233         if( c->status->repeat )
234                 g_strlcat(flags, "r", sizeof(flags));
235         if( c->status->random )
236                 g_strlcat(flags, "z", sizeof(flags));;
237         if( c->status->crossfade )
238                 g_strlcat(flags, "x", sizeof(flags));
239         if( c->status->updatingDb )
240                 g_strlcat(flags, "U", sizeof(flags));
241         colors_use(w, COLOR_LINE);
242         mvwhline(w, 1, 0, ACS_HLINE, screen->top_window.cols);
243         if (flags[0]) {
244                 wmove(w,1,screen->top_window.cols-strlen(flags)-3);
245                 waddch(w, '[');
246                 colors_use(w, COLOR_LINE_BOLD);
247                 waddstr(w, flags);
248                 colors_use(w, COLOR_LINE);
249                 waddch(w, ']');
250         }
251         wnoutrefresh(w);
254 static void
255 paint_top_window(const char *header, mpdclient_t *c, int full_repaint)
257         static int prev_volume = -1;
258         static size_t prev_header_len = -1;
259         WINDOW *w = screen->top_window.w;
261         if (prev_header_len!=my_strlen(header)) {
262                 prev_header_len = my_strlen(header);
263                 full_repaint = 1;
264         }
266         if (full_repaint) {
267                 wmove(w, 0, 0);
268                 wclrtoeol(w);
269         }
271         if (prev_volume!=c->status->volume || full_repaint)
272                 paint_top_window2(header, c);
275 static void
276 paint_progress_window(mpdclient_t *c)
278         double p;
279         int width;
280         int elapsedTime = c->status->elapsedTime;
282         if (c->status==NULL || IS_STOPPED(c->status->state)) {
283                 mvwhline(screen->progress_window.w, 0, 0, ACS_HLINE,
284                          screen->progress_window.cols);
285                 wnoutrefresh(screen->progress_window.w);
286                 return;
287         }
289         if (c->song && seek_id == c->song->id)
290                 elapsedTime = seek_target_time;
292         p = ((double) elapsedTime) / ((double) c->status->totalTime);
294         width = (int) (p * (double) screen->progress_window.cols);
295         mvwhline(screen->progress_window.w,
296                  0, 0,
297                  ACS_HLINE,
298                  screen->progress_window.cols);
299         whline(screen->progress_window.w, '=', width-1);
300         mvwaddch(screen->progress_window.w, 0, width-1, 'O');
301         wnoutrefresh(screen->progress_window.w);
304 static void
305 paint_status_window(mpdclient_t *c)
307         WINDOW *w = screen->status_window.w;
308         mpd_Status *status = c->status;
309         mpd_Song *song = c->song;
310         int elapsedTime = 0;
311         const char *str = NULL;
312         int x = 0;
314         if( time(NULL) - screen->status_timestamp <= SCREEN_STATUS_MESSAGE_TIME )
315                 return;
317         wmove(w, 0, 0);
318         wclrtoeol(w);
319         colors_use(w, COLOR_STATUS_BOLD);
321         switch(status->state) {
322         case MPD_STATUS_STATE_PLAY:
323                 str = _("Playing:");
324                 break;
325         case MPD_STATUS_STATE_PAUSE:
326                 str = _("[Paused]");
327                 break;
328         case MPD_STATUS_STATE_STOP:
329         default:
330                 break;
331         }
333         if (str) {
334                 waddstr(w, str);
335                 x += my_strlen(str)+1;
336         }
338         /* create time string */
339         memset(screen->buf, 0, screen->buf_size);
340         if (IS_PLAYING(status->state) || IS_PAUSED(status->state)) {
341                 if (status->totalTime > 0) {
342                         /*checks the conf to see whether to display elapsed or remaining time */
343                         if(!strcmp(options.timedisplay_type,"elapsed"))
344                                 elapsedTime = c->status->elapsedTime;
345                         else if(!strcmp(options.timedisplay_type,"remaining"))
346                                 elapsedTime = (c->status->totalTime - c->status->elapsedTime);
348                         if( c->song && seek_id == c->song->id )
349                                 elapsedTime = seek_target_time;
350                         /*write out the time, using hours if time over 60 minutes*/
351                         if (c->status->totalTime > 3600) {
352                                 g_snprintf(screen->buf, screen->buf_size,
353                                            " [%i:%02i:%02i/%i:%02i:%02i]",
354                                            elapsedTime/3600, (elapsedTime%3600)/60, elapsedTime%60,
355                                            status->totalTime/3600, (status->totalTime%3600)/60,  status->totalTime%60);
356                         } else {
357                                 g_snprintf(screen->buf, screen->buf_size,
358                                            " [%i:%02i/%i:%02i]",
359                                            elapsedTime/60, elapsedTime%60,
360                                            status->totalTime/60,   status->totalTime%60 );
361                         }
362                 } else {
363                         g_snprintf(screen->buf, screen->buf_size,
364                                    " [%d kbps]", status->bitRate );
365                 }
366         } else {
367                 time_t timep;
369                 time(&timep);
370                 strftime(screen->buf, screen->buf_size, "%X ",localtime(&timep));
371         }
373         /* display song */
374         if (IS_PLAYING(status->state) || IS_PAUSED(status->state)) {
375                 char songname[MAX_SONGNAME_LENGTH];
376                 int width = COLS-x-my_strlen(screen->buf);
378                 if (song)
379                         strfsong(songname, MAX_SONGNAME_LENGTH, STATUS_FORMAT, song);
380                 else
381                         songname[0] = '\0';
383                 colors_use(w, COLOR_STATUS);
384                 /* scroll if the song name is to long */
385                 if (options.scroll && my_strlen(songname) > (size_t)width) {
386                         static  scroll_state_t st = { 0, 0 };
387                         char *tmp = strscroll(songname, options.scroll_sep, width, &st);
389                         g_strlcpy(songname, tmp, MAX_SONGNAME_LENGTH);
390                         g_free(tmp);
391                 }
392                 //mvwaddnstr(w, 0, x, songname, width);
393                 mvwaddstr(w, 0, x, songname);
394         }
396         /* display time string */
397         if (screen->buf[0]) {
398                 x = screen->status_window.cols - strlen(screen->buf);
399                 colors_use(w, COLOR_STATUS_TIME);
400                 mvwaddstr(w, 0, x, screen->buf);
401         }
403         wnoutrefresh(w);
406 int
407 screen_exit(void)
409         endwin();
410         if (screen) {
411                 gint i;
413                 /* close and exit all screens (playlist,browse,help...) */
414                 i=0;
415                 while (screens[i].get_mode_functions) {
416                         struct screen_functions *sf = screens[i].get_mode_functions();
418                         if (sf && sf->close)
419                                 sf->close();
420                         if (sf && sf->exit)
421                                 sf->exit();
423                         i++;
424                 }
426                 string_list_free(screen->find_history);
427                 g_free(screen->buf);
428                 g_free(screen->findbuf);
430                 g_free(screen);
431                 screen = NULL;
432         }
433         return 0;
436 void
437 screen_resize(void)
439         gint i;
441         D("Resize rows %d->%d, cols %d->%d\n",screen->rows,LINES,screen->cols,COLS);
442         if (COLS<SCREEN_MIN_COLS || LINES<SCREEN_MIN_ROWS) {
443                 screen_exit();
444                 fprintf(stderr, _("Error: Screen to small!\n"));
445                 exit(EXIT_FAILURE);
446         }
448         resizeterm(LINES, COLS);
450         screen->cols = COLS;
451         screen->rows = LINES;
453         /* top window */
454         screen->top_window.cols = screen->cols;
455         wresize(screen->top_window.w, 2, screen->cols);
457         /* main window */
458         screen->main_window.cols = screen->cols;
459         screen->main_window.rows = screen->rows-4;
460         wresize(screen->main_window.w, screen->main_window.rows, screen->cols);
461         wclear(screen->main_window.w);
463         /* progress window */
464         screen->progress_window.cols = screen->cols;
465         wresize(screen->progress_window.w, 1, screen->cols);
466         mvwin(screen->progress_window.w, screen->rows-2, 0);
468         /* status window */
469         screen->status_window.cols = screen->cols;
470         wresize(screen->status_window.w, 1, screen->cols);
471         mvwin(screen->status_window.w, screen->rows-1, 0);
473         screen->buf_size = screen->cols;
474         g_free(screen->buf);
475         screen->buf = g_malloc(screen->cols);
477         /* close and exit all screens (playlist,browse,help...) */
478         i=0;
479         while (screens[i].get_mode_functions) {
480                 struct screen_functions *sf = screens[i].get_mode_functions();
482                 if (sf && sf->resize)
483                         sf->resize(screen->main_window.cols, screen->main_window.rows);
485                 i++;
486         }
488         /* ? - without this the cursor becomes visible with aterm & Eterm */
489         curs_set(1);
490         curs_set(0);
492         screen->painted = 0;
495 void
496 screen_status_message(const char *msg)
498         WINDOW *w = screen->status_window.w;
500         wmove(w, 0, 0);
501         wclrtoeol(w);
502         colors_use(w, COLOR_STATUS_ALERT);
503         waddstr(w, msg);
504         wnoutrefresh(w);
505         screen->status_timestamp = time(NULL);
508 void
509 screen_status_printf(const char *format, ...)
511         char *msg;
512         va_list ap;
514         va_start(ap,format);
515         msg = g_strdup_vprintf(format,ap);
516         va_end(ap);
517         screen_status_message(msg);
518         g_free(msg);
521 void
522 ncurses_init(void)
525         /* initialize the curses library */
526         initscr();
527         /* initialize color support */
528         colors_start();
529         /* tell curses not to do NL->CR/NL on output */
530         nonl();
531         /*  use raw mode (ignore interrupt,quit,suspend, and flow control ) */
532 #ifdef ENABLE_RAW_MODE
533         //  raw();
534 #endif
535         /* don't echo input */
536         noecho();
537         /* set cursor invisible */
538         curs_set(0);
539         /* enable extra keys */
540         keypad(stdscr, TRUE);
541         /* return from getch() without blocking */
542         timeout(SCREEN_TIMEOUT);
543         /* initialize mouse support */
544 #ifdef HAVE_GETMOUSE
545         if( options.enable_mouse )
546                 mousemask(ALL_MOUSE_EVENTS, NULL);
547 #endif
549         if( COLS<SCREEN_MIN_COLS || LINES<SCREEN_MIN_ROWS )
550                 {
551                         fprintf(stderr, _("Error: Screen to small!\n"));
552                         exit(EXIT_FAILURE);
553                 }
554         screen = g_malloc(sizeof(screen_t));
555         memset(screen, 0, sizeof(screen_t));
556         screen->mode = 0;
557         screen->cols = COLS;
558         screen->rows = LINES;
560         screen->buf  = g_malloc(screen->cols);
561         screen->buf_size = screen->cols;
562         screen->findbuf = NULL;
563         screen->painted = 0;
564         screen->start_timestamp = time(NULL);
565         screen->input_timestamp = time(NULL);
566         screen->last_cmd = CMD_NONE;
568         /* create top window */
569         screen->top_window.rows = 2;
570         screen->top_window.cols = screen->cols;
571         screen->top_window.w = newwin(screen->top_window.rows,
572                                       screen->top_window.cols,
573                                       0, 0);
574         leaveok(screen->top_window.w, TRUE);
575         keypad(screen->top_window.w, TRUE);
577         /* create main window */
578         screen->main_window.rows = screen->rows-4;
579         screen->main_window.cols = screen->cols;
580         screen->main_window.w = newwin(screen->main_window.rows,
581                                        screen->main_window.cols,
582                                        2,
583                                        0);
585         //  leaveok(screen->main_window.w, TRUE); temporary disabled
586         keypad(screen->main_window.w, TRUE);
588         /* create progress window */
589         screen->progress_window.rows = 1;
590         screen->progress_window.cols = screen->cols;
591         screen->progress_window.w = newwin(screen->progress_window.rows,
592                                            screen->progress_window.cols,
593                                            screen->rows-2,
594                                            0);
595         leaveok(screen->progress_window.w, TRUE);
597         /* create status window */
598         screen->status_window.rows = 1;
599         screen->status_window.cols = screen->cols;
600         screen->status_window.w = newwin(screen->status_window.rows,
601                                          screen->status_window.cols,
602                                          screen->rows-1,
603                                          0);
605         leaveok(screen->status_window.w, FALSE);
606         keypad(screen->status_window.w, TRUE);
608         if( options.enable_colors )
609                 {
610                         /* set background attributes */
611                         wbkgd(stdscr, COLOR_PAIR(COLOR_LIST));
612                         wbkgd(screen->main_window.w,     COLOR_PAIR(COLOR_LIST));
613                         wbkgd(screen->top_window.w,      COLOR_PAIR(COLOR_TITLE));
614                         wbkgd(screen->progress_window.w, COLOR_PAIR(COLOR_PROGRESSBAR));
615                         wbkgd(screen->status_window.w,   COLOR_PAIR(COLOR_STATUS));
616                         colors_use(screen->progress_window.w, COLOR_PROGRESSBAR);
617                 }
619         refresh();
622 int
623 screen_init(mpdclient_t *c)
625         gint i;
627         /* initialize screens */
628         i=0;
629         while (screens[i].get_mode_functions) {
630                 struct screen_functions *fn = screens[i].get_mode_functions();
632                 if (fn && fn->init)
633                         fn->init(screen->main_window.w,
634                                  screen->main_window.cols,
635                                  screen->main_window.rows);
637                 i++;
638         }
640 #if 0
641         /* broken */
642         mode_fn = NULL;
643         switch_screen_mode(screen_get_id(options.screen_list[0]), c);
644 #else
645         mode_fn = get_screen_playlist();
646 #endif
648         if( mode_fn && mode_fn->open )
649                 mode_fn->open(screen, c);
651         /* initialize wreadln */
652         wrln_wgetch = my_wgetch;
653         wrln_max_history_length = 16;
655         return 0;
658 void
659 screen_paint(mpdclient_t *c)
661         const char *title = NULL;
663         if (mode_fn && mode_fn->get_title)
664                 title = mode_fn->get_title(screen->buf, screen->buf_size);
666         D("screen_paint(%s)\n", title);
667         /* paint the title/header window */
668         if( title )
669                 paint_top_window(title, c, 1);
670         else
671                 paint_top_window("", c, 1);
673         /* paint the main window */
674         wclear(screen->main_window.w);
675         if( mode_fn && mode_fn->paint )
676                 mode_fn->paint(screen, c);
678         paint_progress_window(c);
679         paint_status_window(c);
680         screen->painted = 1;
681         wmove(screen->main_window.w, 0, 0);
682         wnoutrefresh(screen->main_window.w);
684         /* tell curses to update */
685         doupdate();
688 void
689 screen_update(mpdclient_t *c)
691         static int repeat = -1;
692         static int random_enabled = -1;
693         static int crossfade = -1;
694         static int dbupdate = -1;
695         list_window_t *lw = NULL;
697         if( !screen->painted )
698                 return screen_paint(c);
700         /* print a message if mpd status has changed */
701         if (repeat < 0) {
702                 repeat = c->status->repeat;
703                 random_enabled = c->status->random;
704                 crossfade = c->status->crossfade;
705                 dbupdate = c->status->updatingDb;
706         }
708         if (repeat != c->status->repeat)
709                 screen_status_printf(c->status->repeat ?
710                                      _("Repeat is on") :
711                                      _("Repeat is off"));
713         if (random_enabled != c->status->random)
714                 screen_status_printf(c->status->random ?
715                                      _("Random is on") :
716                                      _("Random is off"));
718         if (crossfade != c->status->crossfade)
719                 screen_status_printf(_("Crossfade %d seconds"), c->status->crossfade);
721         if (dbupdate && dbupdate != c->status->updatingDb) {
722                 screen_status_printf(_("Database updated!"));
723                 mpdclient_browse_callback(c, BROWSE_DB_UPDATED, NULL);
724         }
726         repeat = c->status->repeat;
727         random_enabled = c->status->random;
728         crossfade = c->status->crossfade;
729         dbupdate = c->status->updatingDb;
731         /* update title/header window */
732         if (welcome && screen->last_cmd==CMD_NONE &&
733             time(NULL)-screen->start_timestamp <= SCREEN_WELCOME_TIME)
734                 paint_top_window("", c, 0);
735         else if (mode_fn && mode_fn->get_title) {
736                 paint_top_window(mode_fn->get_title(screen->buf,screen->buf_size), c, 0);
737                 welcome = FALSE;
738         } else
739                 paint_top_window("", c, 0);
741         /* update the main window */
742         if (mode_fn && mode_fn->paint)
743                 mode_fn->update(screen, c);
745         if (mode_fn && mode_fn->get_lw)
746                 lw = mode_fn->get_lw();
748         /* update progress window */
749         paint_progress_window(c);
751         /* update status window */
752         paint_status_window(c);
754         /* move the cursor to the selected row in the main window */
755         if (lw)
756                 wmove(screen->main_window.w, LW_ROW(lw), 0);
757         else
758                 wmove(screen->main_window.w, 0, 0);
759         wnoutrefresh(screen->main_window.w);
761         /* tell curses to update */
762         doupdate();
765 void
766 screen_idle(mpdclient_t *c)
768         if (c->song && seek_id == c->song->id &&
769             (screen->last_cmd == CMD_SEEK_FORWARD ||
770              screen->last_cmd == CMD_SEEK_BACKWARD))
771                 mpdclient_cmd_seek(c, seek_id, seek_target_time);
773         screen->last_cmd = CMD_NONE;
774         seek_id = -1;
777 #ifdef HAVE_GETMOUSE
778 int
779 screen_get_mouse_event(mpdclient_t *c,
780                        list_window_t *lw, int lw_length,
781                        unsigned long *bstate, int *row)
783         MEVENT event;
785         /* retreive the mouse event from ncurses */
786         getmouse(&event);
787         D("mouse: id=%d  y=%d,x=%d,z=%d\n",event.id,event.y,event.x,event.z);
788         /* calculate the selected row in the list window */
789         *row = event.y - screen->top_window.rows;
790         /* copy button state bits */
791         *bstate = event.bstate;
792         /* if button 2 was pressed switch screen */
793         if (event.bstate & BUTTON2_CLICKED) {
794                 screen_cmd(c, CMD_SCREEN_NEXT);
795                 return 1;
796         }
798         /* if the even occured above the list window move up */
799         if (*row < 0 && lw) {
800                 if (event.bstate & BUTTON3_CLICKED)
801                         list_window_first(lw);
802                 else
803                         list_window_previous_page(lw);
804                 return 1;
805         }
807         /* if the even occured below the list window move down */
808         if ((unsigned)*row >= lw->rows && lw) {
809                 if (event.bstate & BUTTON3_CLICKED)
810                         list_window_last(lw, lw_length);
811                 else
812                         list_window_next_page(lw, lw_length);
813                 return 1;
814         }
816         return 0;
818 #endif
820 void
821 screen_cmd(mpdclient_t *c, command_t cmd)
823         screen->input_timestamp = time(NULL);
824         screen->last_cmd = cmd;
825         welcome = FALSE;
827         if( mode_fn && mode_fn->cmd && mode_fn->cmd(screen, c, cmd) )
828                 return;
830         switch(cmd) {
831         case CMD_PLAY:
832                 mpdclient_cmd_play(c, MPD_PLAY_AT_BEGINNING);
833                 break;
834         case CMD_PAUSE:
835                 mpdclient_cmd_pause(c, !IS_PAUSED(c->status->state));
836                 break;
837         case CMD_STOP:
838                 mpdclient_cmd_stop(c);
839                 break;
840         case CMD_SEEK_FORWARD:
841                 if (!IS_STOPPED(c->status->state)) {
842                         if (c->song && seek_id != c->song->id) {
843                                 seek_id = c->song->id;
844                                 seek_target_time = c->status->elapsedTime;
845                         }
846                         seek_target_time+=options.seek_time;
847                         if (seek_target_time < c->status->totalTime)
848                                 break;
849                         seek_target_time = c->status->totalTime;
850                         /* seek_target_time=0; */
851                 }
852                 break;
853                 /* fall through... */
854         case CMD_TRACK_NEXT:
855                 if (!IS_STOPPED(c->status->state))
856                         mpdclient_cmd_next(c);
857                 break;
858         case CMD_SEEK_BACKWARD:
859                 if (!IS_STOPPED(c->status->state)) {
860                         if (seek_id != c->song->id) {
861                                 seek_id = c->song->id;
862                                 seek_target_time = c->status->elapsedTime;
863                         }
864                         seek_target_time-=options.seek_time;
865                         if (seek_target_time < 0)
866                                 seek_target_time=0;
867                 }
868                 break;
869         case CMD_TRACK_PREVIOUS:
870                 if (!IS_STOPPED(c->status->state))
871                         mpdclient_cmd_prev(c);
872                 break;
873         case CMD_SHUFFLE:
874                 if (mpdclient_cmd_shuffle(c) == 0)
875                         screen_status_message(_("Shuffled playlist!"));
876                 break;
877         case CMD_CLEAR:
878                 if (mpdclient_cmd_clear(c) == 0)
879                         screen_status_message(_("Cleared playlist!"));
880                 break;
881         case CMD_REPEAT:
882                 mpdclient_cmd_repeat(c, !c->status->repeat);
883                 break;
884         case CMD_RANDOM:
885                 mpdclient_cmd_random(c, !c->status->random);
886                 break;
887         case CMD_CROSSFADE:
888                 if (c->status->crossfade)
889                         mpdclient_cmd_crossfade(c, 0);
890                 else
891                         mpdclient_cmd_crossfade(c, options.crossfade_time);
892                 break;
893         case CMD_DB_UPDATE:
894                 if (!c->status->updatingDb) {
895                         if( mpdclient_cmd_db_update_utf8(c,NULL)==0 )
896                                 screen_status_printf(_("Database update started!"));
897                 } else
898                         screen_status_printf(_("Database update running..."));
899                 break;
900         case CMD_VOLUME_UP:
901                 if( c->status->volume!=MPD_STATUS_NO_VOLUME && c->status->volume<100 )
902                         mpdclient_cmd_volume(c, ++c->status->volume);
903                 break;
904         case CMD_VOLUME_DOWN:
905                 if( c->status->volume!=MPD_STATUS_NO_VOLUME && c->status->volume>0 )
906                         mpdclient_cmd_volume(c, --c->status->volume);
907                 break;
908         case CMD_TOGGLE_FIND_WRAP:
909                 options.find_wrap = !options.find_wrap;
910                 screen_status_printf(options.find_wrap ?
911                                      _("Find mode: Wrapped") :
912                                      _("Find mode: Normal"));
913                 break;
914         case CMD_TOGGLE_AUTOCENTER:
915                 options.auto_center = !options.auto_center;
916                 screen_status_printf(options.auto_center ?
917                                      _("Auto center mode: On") :
918                                      _("Auto center mode: Off"));
919                 break;
920         case CMD_SCREEN_UPDATE:
921                 screen->painted = 0;
922                 break;
923         case CMD_SCREEN_PREVIOUS:
924                 screen_next_mode(c, -1);
925                 break;
926         case CMD_SCREEN_NEXT:
927                 screen_next_mode(c, 1);
928                 break;
929         case CMD_SCREEN_PLAY:
930                 switch_screen_mode(SCREEN_PLAYLIST_ID, c);
931                 break;
932         case CMD_SCREEN_FILE:
933                 switch_screen_mode(SCREEN_BROWSE_ID, c);
934                 break;
935         case CMD_SCREEN_HELP:
936                 switch_screen_mode(SCREEN_HELP_ID, c);
937                 break;
938         case CMD_SCREEN_SEARCH:
939                 switch_screen_mode(SCREEN_SEARCH_ID, c);
940                 break;
941         case CMD_SCREEN_ARTIST:
942                 switch_screen_mode(SCREEN_ARTIST_ID, c);
943                 break;
944         case CMD_SCREEN_KEYDEF:
945                 switch_screen_mode(SCREEN_KEYDEF_ID, c);
946                 break;
947         case CMD_SCREEN_CLOCK:
948                 switch_screen_mode(SCREEN_CLOCK_ID, c);
949                 break;
950         case CMD_SCREEN_LYRICS:
951                 switch_screen_mode(SCREEN_LYRICS_ID, c);
952                 break;
953         case CMD_QUIT:
954                 exit(EXIT_SUCCESS);
955         default:
956                 break;
957         }