Code

added option "welcome-screen-list"
[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_SEARCH_ID       103
47 #define SCREEN_LYRICS_ID           104
51 /* screens */
52 extern const struct screen_functions screen_playlist;
53 extern const struct screen_functions screen_browse;
54 #ifdef ENABLE_ARTIST_SCREEN
55 extern const struct screen_functions screen_artist;
56 #endif
57 extern const struct screen_functions screen_help;
58 #ifdef ENABLE_SEARCH_SCREEN
59 extern const struct screen_functions screen_search;
60 #endif
61 #ifdef ENABLE_KEYDEF_SCREEN
62 extern const struct screen_functions screen_keydef;
63 #endif
64 extern const struct screen_functions screen_lyrics;
66 typedef struct screen_functions * (*screen_get_mode_functions_fn_t) (void);
68 static const struct
69 {
70         gint id;
71         const gchar *name;
72         const struct screen_functions *functions;
73 } screens[] = {
74         { SCREEN_PLAYLIST_ID, "playlist", &screen_playlist },
75         { SCREEN_BROWSE_ID, "browse", &screen_browse },
76 #ifdef ENABLE_ARTIST_SCREEN
77         { SCREEN_ARTIST_ID, "artist", &screen_artist },
78 #endif
79         { SCREEN_HELP_ID, "help", &screen_help },
80 #ifdef ENABLE_SEARCH_SCREEN
81         { SCREEN_SEARCH_ID, "search", &screen_search },
82 #endif
83 #ifdef ENABLE_KEYDEF_SCREEN
84         { SCREEN_KEYDEF_ID, "keydef", &screen_keydef },
85 #endif
86 #ifdef ENABLE_LYRICS_SCREEN
87         { SCREEN_LYRICS_ID, "lyrics", &screen_lyrics },
88 #endif
89 };
91 #define NUM_SCREENS (sizeof(screens) / sizeof(screens[0]))
93 static gboolean welcome = TRUE;
94 static struct screen screen;
95 static const struct screen_functions *mode_fn = &screen_playlist;
96 static int seek_id = -1;
97 static int seek_target_time = 0;
99 gint
100 screen_get_id(const char *name)
102         guint i;
104         for (i = 0; i < NUM_SCREENS; ++i)
105                 if (strcmp(name, screens[i].name) == 0)
106                         return screens[i].id;
108         return -1;
111 static gint
112 lookup_mode(gint id)
114         guint i;
116         for (i = 0; i < NUM_SCREENS; ++i)
117                 if (screens[i].id == id)
118                         return i;
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         new_mode = lookup_mode(id);
137         if (new_mode < 0)
138                 return;
140         /* close the old mode */
141         if (mode_fn->close != NULL)
142                 mode_fn->close();
144         /* get functions for the new mode */
145         D("switch_screen(%s)\n", screens[new_mode].name );
146         mode_fn = screens[new_mode].functions;
147         screen.mode = new_mode;
148         screen.painted = 0;
150         /* open the new mode */
151         if (mode_fn->open != NULL)
152                 mode_fn->open(&screen, c);
155 static int
156 find_configured_screen(const char *name)
158         unsigned i;
160         for (i = 0; options.screen_list[i] != NULL; ++i)
161                 if (strcmp(options.screen_list[i], name) == 0)
162                         return i;
164         return -1;
167 static void
168 screen_next_mode(mpdclient_t *c, int offset)
170         int max = g_strv_length(options.screen_list);
171         int current, next;
173         /* find current screen */
174         current = find_configured_screen(screens[screen.mode].name);
175         next = current + offset;
176         if (next<0)
177                 next = max-1;
178         else if (next>=max)
179                 next = 0;
181         D("current mode: %d:%d    next:%d\n", current, max, next);
182         switch_screen_mode(screen_get_id(options.screen_list[next]), c);
185 static void
186 paint_top_window2(const char *header, mpdclient_t *c)
188         char flags[5];
189         WINDOW *w = screen.top_window.w;
190         char buf[32];
192         if (header[0]) {
193                 colors_use(w, COLOR_TITLE_BOLD);
194                 mvwaddstr(w, 0, 0, header);
195         } else {
196                 colors_use(w, COLOR_TITLE_BOLD);
197                 waddstr(w, get_key_names(CMD_SCREEN_HELP, FALSE));
198                 colors_use(w, COLOR_TITLE);
199                 waddstr(w, _(":Help  "));
200                 colors_use(w, COLOR_TITLE_BOLD);
201                 waddstr(w, get_key_names(CMD_SCREEN_PLAY, FALSE));
202                 colors_use(w, COLOR_TITLE);
203                 waddstr(w, _(":Playlist  "));
204                 colors_use(w, COLOR_TITLE_BOLD);
205                 waddstr(w, get_key_names(CMD_SCREEN_FILE, FALSE));
206                 colors_use(w, COLOR_TITLE);
207                 waddstr(w, _(":Browse  "));
208 #ifdef ENABLE_ARTIST_SCREEN
209                 colors_use(w, COLOR_TITLE_BOLD);
210                 waddstr(w, get_key_names(CMD_SCREEN_ARTIST, FALSE));
211                 colors_use(w, COLOR_TITLE);
212                 waddstr(w, _(":Artist  "));
213 #endif
214 #ifdef ENABLE_SEARCH_SCREEN
215                 colors_use(w, COLOR_TITLE_BOLD);
216                 waddstr(w, get_key_names(CMD_SCREEN_SEARCH, FALSE));
217                 colors_use(w, COLOR_TITLE);
218                 waddstr(w, _(":Search  "));
219 #endif
220 #ifdef ENABLE_LYRICS_SCREEN
221                 colors_use(w, COLOR_TITLE_BOLD);
222                 waddstr(w, get_key_names(CMD_SCREEN_LYRICS, FALSE));
223                 colors_use(w, COLOR_TITLE);
224                 waddstr(w, _(":Lyrics  "));
225 #endif
226         }
227         if (c->status == NULL || c->status->volume == MPD_STATUS_NO_VOLUME) {
228                 g_snprintf(buf, 32, _("Volume n/a "));
229         } else {
230                 g_snprintf(buf, 32, _(" Volume %d%%"), c->status->volume);
231         }
232         colors_use(w, COLOR_TITLE);
233         mvwaddstr(w, 0, screen.top_window.cols-my_strlen(buf), buf);
235         flags[0] = 0;
236         if (c->status != NULL) {
237                 if (c->status->repeat)
238                         g_strlcat(flags, "r", sizeof(flags));
239                 if (c->status->random)
240                         g_strlcat(flags, "z", sizeof(flags));;
241                 if (c->status->crossfade)
242                         g_strlcat(flags, "x", sizeof(flags));
243                 if (c->status->updatingDb)
244                         g_strlcat(flags, "U", sizeof(flags));
245         }
247         colors_use(w, COLOR_LINE);
248         mvwhline(w, 1, 0, ACS_HLINE, screen.top_window.cols);
249         if (flags[0]) {
250                 wmove(w,1,screen.top_window.cols-strlen(flags)-3);
251                 waddch(w, '[');
252                 colors_use(w, COLOR_LINE_BOLD);
253                 waddstr(w, flags);
254                 colors_use(w, COLOR_LINE);
255                 waddch(w, ']');
256         }
257         wnoutrefresh(w);
260 static void
261 paint_top_window(const char *header, mpdclient_t *c, int full_repaint)
263         static int prev_volume = -1;
264         static size_t prev_header_len = -1;
265         WINDOW *w = screen.top_window.w;
267         if (prev_header_len!=my_strlen(header)) {
268                 prev_header_len = my_strlen(header);
269                 full_repaint = 1;
270         }
272         if (full_repaint) {
273                 wmove(w, 0, 0);
274                 wclrtoeol(w);
275         }
277         if ((c->status != NULL && prev_volume != c->status->volume) ||
278             full_repaint)
279                 paint_top_window2(header, c);
282 static void
283 paint_progress_window(mpdclient_t *c)
285         double p;
286         int width;
287         int elapsedTime;
289         if (c->status==NULL || IS_STOPPED(c->status->state)) {
290                 mvwhline(screen.progress_window.w, 0, 0, ACS_HLINE,
291                          screen.progress_window.cols);
292                 wnoutrefresh(screen.progress_window.w);
293                 return;
294         }
296         if (c->song && seek_id == c->song->id)
297                 elapsedTime = seek_target_time;
298         else
299                 elapsedTime = c->status->elapsedTime;
301         p = ((double) elapsedTime) / ((double) c->status->totalTime);
303         width = (int) (p * (double) screen.progress_window.cols);
304         mvwhline(screen.progress_window.w,
305                  0, 0,
306                  ACS_HLINE,
307                  screen.progress_window.cols);
308         whline(screen.progress_window.w, '=', width-1);
309         mvwaddch(screen.progress_window.w, 0, width-1, 'O');
310         wnoutrefresh(screen.progress_window.w);
313 static void
314 paint_status_window(mpdclient_t *c)
316         WINDOW *w = screen.status_window.w;
317         mpd_Status *status = c->status;
318         mpd_Song *song = c->song;
319         int elapsedTime = 0;
320         char bitrate[16];
321         const char *str = NULL;
322         int x = 0;
324         if( time(NULL) - screen.status_timestamp <= SCREEN_STATUS_MESSAGE_TIME )
325                 return;
327         wmove(w, 0, 0);
328         wclrtoeol(w);
329         colors_use(w, COLOR_STATUS_BOLD);
331         switch (status == NULL ? MPD_STATUS_STATE_STOP : status->state) {
332         case MPD_STATUS_STATE_PLAY:
333                 str = _("Playing:");
334                 break;
335         case MPD_STATUS_STATE_PAUSE:
336                 str = _("[Paused]");
337                 break;
338         case MPD_STATUS_STATE_STOP:
339         default:
340                 break;
341         }
343         if (str) {
344                 waddstr(w, str);
345                 x += my_strlen(str)+1;
346         }
348         /* create time string */
349         memset(screen.buf, 0, screen.buf_size);
350         if (status != NULL && (IS_PLAYING(status->state) ||
351                                IS_PAUSED(status->state))) {
352                 if (status->totalTime > 0) {
353                         /*checks the conf to see whether to display elapsed or remaining time */
354                         if(!strcmp(options.timedisplay_type,"elapsed"))
355                                 elapsedTime = c->status->elapsedTime;
356                         else if(!strcmp(options.timedisplay_type,"remaining"))
357                                 elapsedTime = (c->status->totalTime - c->status->elapsedTime);
359                         if( c->song && seek_id == c->song->id )
360                                 elapsedTime = seek_target_time;
362                         /* display bitrate if visible-bitrate is true */
363                         if (options.visible_bitrate) {
364                                 g_snprintf(bitrate, 16,
365                                            " [%d kbps]", status->bitRate);
366                         } else {
367                                 bitrate[0] = '\0';
368                         }
370                         /*write out the time, using hours if time over 60 minutes*/
371                         if (c->status->totalTime > 3600) {
372                                 g_snprintf(screen.buf, screen.buf_size,
373                                            "%s [%i:%02i:%02i/%i:%02i:%02i]",
374                                            bitrate, elapsedTime/3600, (elapsedTime%3600)/60, elapsedTime%60,
375                                            status->totalTime/3600, (status->totalTime%3600)/60,  status->totalTime%60);
376                         } else {
377                                 g_snprintf(screen.buf, screen.buf_size,
378                                            "%s [%i:%02i/%i:%02i]",
379                                            bitrate, elapsedTime/60, elapsedTime%60,
380                                            status->totalTime/60,   status->totalTime%60 );
381                         }
382                 } else {
383                         g_snprintf(screen.buf, screen.buf_size,
384                                    " [%d kbps]", status->bitRate );
385                 }
386         } else {
387                 time_t timep;
389                 time(&timep);
390                 strftime(screen.buf, screen.buf_size, "%X ",localtime(&timep));
391         }
393         /* display song */
394         if (status != NULL && (IS_PLAYING(status->state) ||
395                                IS_PAUSED(status->state))) {
396                 char songname[MAX_SONGNAME_LENGTH];
397                 int width = COLS-x-my_strlen(screen.buf);
399                 if (song)
400                         strfsong(songname, MAX_SONGNAME_LENGTH, STATUS_FORMAT, song);
401                 else
402                         songname[0] = '\0';
404                 colors_use(w, COLOR_STATUS);
405                 /* scroll if the song name is to long */
406                 if (options.scroll && my_strlen(songname) > (size_t)width) {
407                         static  scroll_state_t st = { 0, 0 };
408                         char *tmp = strscroll(songname, options.scroll_sep, width, &st);
410                         g_strlcpy(songname, tmp, MAX_SONGNAME_LENGTH);
411                         g_free(tmp);
412                 }
413                 //mvwaddnstr(w, 0, x, songname, width);
414                 mvwaddstr(w, 0, x, songname);
415         }
417         /* display time string */
418         if (screen.buf[0]) {
419                 x = screen.status_window.cols - strlen(screen.buf);
420                 colors_use(w, COLOR_STATUS_TIME);
421                 mvwaddstr(w, 0, x, screen.buf);
422         }
424         wnoutrefresh(w);
427 void
428 screen_exit(void)
430         guint i;
432         endwin();
434         if (mode_fn->close != NULL)
435                 mode_fn->close();
437         /* close and exit all screens (playlist,browse,help...) */
438         for (i = 0; i < NUM_SCREENS; ++i) {
439                 const struct screen_functions *sf = screens[i].functions;
441                 if (sf->exit)
442                         sf->exit();
443         }
445         string_list_free(screen.find_history);
446         g_free(screen.buf);
447         g_free(screen.findbuf);
450 void
451 screen_resize(void)
453         guint i;
455         D("Resize rows %d->%d, cols %d->%d\n",screen.rows,LINES,screen.cols,COLS);
456         if (COLS<SCREEN_MIN_COLS || LINES<SCREEN_MIN_ROWS) {
457                 screen_exit();
458                 fprintf(stderr, _("Error: Screen to small!\n"));
459                 exit(EXIT_FAILURE);
460         }
462         resizeterm(LINES, COLS);
464         screen.cols = COLS;
465         screen.rows = LINES;
467         /* top window */
468         screen.top_window.cols = screen.cols;
469         wresize(screen.top_window.w, 2, screen.cols);
471         /* main window */
472         screen.main_window.cols = screen.cols;
473         screen.main_window.rows = screen.rows-4;
474         wresize(screen.main_window.w, screen.main_window.rows, screen.cols);
475         wclear(screen.main_window.w);
477         /* progress window */
478         screen.progress_window.cols = screen.cols;
479         wresize(screen.progress_window.w, 1, screen.cols);
480         mvwin(screen.progress_window.w, screen.rows-2, 0);
482         /* status window */
483         screen.status_window.cols = screen.cols;
484         wresize(screen.status_window.w, 1, screen.cols);
485         mvwin(screen.status_window.w, screen.rows-1, 0);
487         screen.buf_size = screen.cols;
488         g_free(screen.buf);
489         screen.buf = g_malloc(screen.cols);
491         /* close and exit all screens (playlist,browse,help...) */
492         for (i = 0; i < NUM_SCREENS; ++i) {
493                 const struct screen_functions *sf = screens[i].functions;
495                 if (sf->resize)
496                         sf->resize(screen.main_window.cols, screen.main_window.rows);
497         }
500         /* ? - without this the cursor becomes visible with aterm & Eterm */
501         curs_set(1);
502         curs_set(0);
504         screen.painted = 0;
507 void
508 screen_status_message(const char *msg)
510         WINDOW *w = screen.status_window.w;
512         wmove(w, 0, 0);
513         wclrtoeol(w);
514         colors_use(w, COLOR_STATUS_ALERT);
515         waddstr(w, msg);
516         wnoutrefresh(w);
517         screen.status_timestamp = time(NULL);
520 void
521 screen_status_printf(const char *format, ...)
523         char *msg;
524         va_list ap;
526         va_start(ap,format);
527         msg = g_strdup_vprintf(format,ap);
528         va_end(ap);
529         screen_status_message(msg);
530         g_free(msg);
533 void
534 ncurses_init(void)
537         /* initialize the curses library */
538         initscr();
539         /* initialize color support */
540         colors_start();
541         /* tell curses not to do NL->CR/NL on output */
542         nonl();
543         /*  use raw mode (ignore interrupt,quit,suspend, and flow control ) */
544 #ifdef ENABLE_RAW_MODE
545         //  raw();
546 #endif
547         /* don't echo input */
548         noecho();
549         /* set cursor invisible */
550         curs_set(0);
551         /* enable extra keys */
552         keypad(stdscr, TRUE);
553         /* initialize mouse support */
554 #ifdef HAVE_GETMOUSE
555         if (options.enable_mouse)
556                 mousemask(ALL_MOUSE_EVENTS, NULL);
557 #endif
559         if (COLS < SCREEN_MIN_COLS || LINES < SCREEN_MIN_ROWS) {
560                 fprintf(stderr, _("Error: Screen to small!\n"));
561                 exit(EXIT_FAILURE);
562         }
564         screen.mode = 0;
565         screen.cols = COLS;
566         screen.rows = LINES;
568         screen.buf  = g_malloc(screen.cols);
569         screen.buf_size = screen.cols;
570         screen.findbuf = NULL;
571         screen.painted = 0;
572         screen.start_timestamp = time(NULL);
573         screen.input_timestamp = time(NULL);
574         screen.last_cmd = CMD_NONE;
576         /* create top window */
577         screen.top_window.rows = 2;
578         screen.top_window.cols = screen.cols;
579         screen.top_window.w = newwin(screen.top_window.rows,
580                                       screen.top_window.cols,
581                                       0, 0);
582         leaveok(screen.top_window.w, TRUE);
583         keypad(screen.top_window.w, TRUE);
585         /* create main window */
586         screen.main_window.rows = screen.rows-4;
587         screen.main_window.cols = screen.cols;
588         screen.main_window.w = newwin(screen.main_window.rows,
589                                        screen.main_window.cols,
590                                        2,
591                                        0);
593         //  leaveok(screen.main_window.w, TRUE); temporary disabled
594         keypad(screen.main_window.w, TRUE);
596         /* create progress window */
597         screen.progress_window.rows = 1;
598         screen.progress_window.cols = screen.cols;
599         screen.progress_window.w = newwin(screen.progress_window.rows,
600                                            screen.progress_window.cols,
601                                            screen.rows-2,
602                                            0);
603         leaveok(screen.progress_window.w, TRUE);
605         /* create status window */
606         screen.status_window.rows = 1;
607         screen.status_window.cols = screen.cols;
608         screen.status_window.w = newwin(screen.status_window.rows,
609                                          screen.status_window.cols,
610                                          screen.rows-1,
611                                          0);
613         leaveok(screen.status_window.w, FALSE);
614         keypad(screen.status_window.w, TRUE);
616         if (options.enable_colors) {
617                 /* set background attributes */
618                 wbkgd(stdscr, COLOR_PAIR(COLOR_LIST));
619                 wbkgd(screen.main_window.w,     COLOR_PAIR(COLOR_LIST));
620                 wbkgd(screen.top_window.w,      COLOR_PAIR(COLOR_TITLE));
621                 wbkgd(screen.progress_window.w, COLOR_PAIR(COLOR_PROGRESSBAR));
622                 wbkgd(screen.status_window.w,   COLOR_PAIR(COLOR_STATUS));
623                 colors_use(screen.progress_window.w, COLOR_PROGRESSBAR);
624         }
626         refresh();
629 void
630 screen_init(mpdclient_t *c)
632         guint i;
634         /* initialize screens */
635         for (i = 0; i < NUM_SCREENS; ++i) {
636                 const struct screen_functions *fn = screens[i].functions;
638                 if (fn->init)
639                         fn->init(screen.main_window.w,
640                                  screen.main_window.cols,
641                                  screen.main_window.rows);
642         }
644         if (mode_fn->open != NULL)
645                 mode_fn->open(&screen, c);
647         /* initialize wreadln */
648         wrln_wgetch = my_wgetch;
649         wrln_max_history_length = 16;
652 void
653 screen_paint(mpdclient_t *c)
655         const char *title = NULL;
657         if (mode_fn->get_title != NULL)
658                 title = mode_fn->get_title(screen.buf, screen.buf_size);
660         D("screen_paint(%s)\n", title);
661         /* paint the title/header window */
662         if( title )
663                 paint_top_window(title, c, 1);
664         else
665                 paint_top_window("", c, 1);
667         /* paint the main window */
668         wclear(screen.main_window.w);
669         if (mode_fn->paint != NULL)
670                 mode_fn->paint(&screen, c);
672         paint_progress_window(c);
673         paint_status_window(c);
674         screen.painted = 1;
675         wmove(screen.main_window.w, 0, 0);
676         wnoutrefresh(screen.main_window.w);
678         /* tell curses to update */
679         doupdate();
682 void
683 screen_update(mpdclient_t *c)
685         static int repeat = -1;
686         static int random_enabled = -1;
687         static int crossfade = -1;
688         static int dbupdate = -1;
690         if( !screen.painted )
691                 return screen_paint(c);
693         /* print a message if mpd status has changed */
694         if (c->status != NULL) {
695                 if (repeat < 0) {
696                         repeat = c->status->repeat;
697                         random_enabled = c->status->random;
698                         crossfade = c->status->crossfade;
699                         dbupdate = c->status->updatingDb;
700                 }
702                 if (repeat != c->status->repeat)
703                         screen_status_printf(c->status->repeat ?
704                                              _("Repeat is on") :
705                                              _("Repeat is off"));
707                 if (random_enabled != c->status->random)
708                         screen_status_printf(c->status->random ?
709                                              _("Random is on") :
710                                              _("Random is off"));
712                 if (crossfade != c->status->crossfade)
713                         screen_status_printf(_("Crossfade %d seconds"), c->status->crossfade);
715                 if (dbupdate && dbupdate != c->status->updatingDb) {
716                         screen_status_printf(_("Database updated!"));
717                         mpdclient_browse_callback(c, BROWSE_DB_UPDATED, NULL);
718                 }
720                 repeat = c->status->repeat;
721                 random_enabled = c->status->random;
722                 crossfade = c->status->crossfade;
723                 dbupdate = c->status->updatingDb;
724         }
726         /* update title/header window */
727         if (welcome && options.welcome_screen_list &&
728             screen.last_cmd==CMD_NONE &&
729             time(NULL)-screen.start_timestamp <= SCREEN_WELCOME_TIME)
730                 paint_top_window("", c, 0);
731         else if (mode_fn->get_title != NULL) {
732                 paint_top_window(mode_fn->get_title(screen.buf,screen.buf_size), c, 0);
733                 welcome = FALSE;
734         } else
735                 paint_top_window("", c, 0);
737         /* update the main window */
738         if (mode_fn->update != NULL)
739                 mode_fn->update(&screen, c);
741         /* update progress window */
742         paint_progress_window(c);
744         /* update status window */
745         paint_status_window(c);
747         /* move the cursor to the origin */
748         wmove(screen.main_window.w, 0, 0);
749         wnoutrefresh(screen.main_window.w);
751         /* tell curses to update */
752         doupdate();
755 void
756 screen_idle(mpdclient_t *c)
758         if (c->song && seek_id == c->song->id &&
759             (screen.last_cmd == CMD_SEEK_FORWARD ||
760              screen.last_cmd == CMD_SEEK_BACKWARD))
761                 mpdclient_cmd_seek(c, seek_id, seek_target_time);
763         screen.last_cmd = CMD_NONE;
764         seek_id = -1;
767 #ifdef HAVE_GETMOUSE
768 int
769 screen_get_mouse_event(mpdclient_t *c,
770                        list_window_t *lw, int lw_length,
771                        unsigned long *bstate, int *row)
773         MEVENT event;
775         /* retreive the mouse event from ncurses */
776         getmouse(&event);
777         D("mouse: id=%d  y=%d,x=%d,z=%d\n",event.id,event.y,event.x,event.z);
778         /* calculate the selected row in the list window */
779         *row = event.y - screen.top_window.rows;
780         /* copy button state bits */
781         *bstate = event.bstate;
782         /* if button 2 was pressed switch screen */
783         if (event.bstate & BUTTON2_CLICKED) {
784                 screen_cmd(c, CMD_SCREEN_NEXT);
785                 return 1;
786         }
788         /* if the even occured above the list window move up */
789         if (*row < 0 && lw) {
790                 if (event.bstate & BUTTON3_CLICKED)
791                         list_window_first(lw);
792                 else
793                         list_window_previous_page(lw);
794                 return 1;
795         }
797         /* if the even occured below the list window move down */
798         if ((unsigned)*row >= lw->rows && lw) {
799                 if (event.bstate & BUTTON3_CLICKED)
800                         list_window_last(lw, lw_length);
801                 else
802                         list_window_next_page(lw, lw_length);
803                 return 1;
804         }
806         return 0;
808 #endif
810 static int
811 screen_client_cmd(mpdclient_t *c, command_t cmd)
813         if (c->connection == NULL || c->status == NULL)
814                 return 0;
816         switch(cmd) {
817                 /*
818         case CMD_PLAY:
819                 mpdclient_cmd_play(c, MPD_PLAY_AT_BEGINNING);
820                 break;
821                 */
822         case CMD_PAUSE:
823                 mpdclient_cmd_pause(c, !IS_PAUSED(c->status->state));
824                 break;
825         case CMD_STOP:
826                 mpdclient_cmd_stop(c);
827                 break;
828         case CMD_CROP:
829                 mpdclient_cmd_crop(c);
830                 break;
831         case CMD_SEEK_FORWARD:
832                 if (!IS_STOPPED(c->status->state)) {
833                         if (c->song && seek_id != c->song->id) {
834                                 seek_id = c->song->id;
835                                 seek_target_time = c->status->elapsedTime;
836                         }
837                         seek_target_time+=options.seek_time;
838                         if (seek_target_time < c->status->totalTime)
839                                 break;
840                         seek_target_time = c->status->totalTime;
841                         /* seek_target_time=0; */
842                 }
843                 break;
844                 /* fall through... */
845         case CMD_TRACK_NEXT:
846                 if (!IS_STOPPED(c->status->state))
847                         mpdclient_cmd_next(c);
848                 break;
849         case CMD_SEEK_BACKWARD:
850                 if (!IS_STOPPED(c->status->state)) {
851                         if (seek_id != c->song->id) {
852                                 seek_id = c->song->id;
853                                 seek_target_time = c->status->elapsedTime;
854                         }
855                         seek_target_time-=options.seek_time;
856                         if (seek_target_time < 0)
857                                 seek_target_time=0;
858                 }
859                 break;
860         case CMD_TRACK_PREVIOUS:
861                 if (!IS_STOPPED(c->status->state))
862                         mpdclient_cmd_prev(c);
863                 break;
864         case CMD_SHUFFLE:
865                 if (mpdclient_cmd_shuffle(c) == 0)
866                         screen_status_message(_("Shuffled playlist!"));
867                 break;
868         case CMD_CLEAR:
869                 if (mpdclient_cmd_clear(c) == 0)
870                         screen_status_message(_("Cleared playlist!"));
871                 break;
872         case CMD_REPEAT:
873                 mpdclient_cmd_repeat(c, !c->status->repeat);
874                 break;
875         case CMD_RANDOM:
876                 mpdclient_cmd_random(c, !c->status->random);
877                 break;
878         case CMD_CROSSFADE:
879                 if (c->status->crossfade)
880                         mpdclient_cmd_crossfade(c, 0);
881                 else
882                         mpdclient_cmd_crossfade(c, options.crossfade_time);
883                 break;
884         case CMD_DB_UPDATE:
885                 if (!c->status->updatingDb) {
886                         if( mpdclient_cmd_db_update_utf8(c,NULL)==0 )
887                                 screen_status_printf(_("Database update started!"));
888                 } else
889                         screen_status_printf(_("Database update running..."));
890                 break;
891         case CMD_VOLUME_UP:
892                 if( c->status->volume!=MPD_STATUS_NO_VOLUME && c->status->volume<100 )
893                         mpdclient_cmd_volume(c, ++c->status->volume);
894                 break;
895         case CMD_VOLUME_DOWN:
896                 if( c->status->volume!=MPD_STATUS_NO_VOLUME && c->status->volume>0 )
897                         mpdclient_cmd_volume(c, --c->status->volume);
898                 break;
900         default:
901                 return 0;
902         }
904         return 1;
907 void
908 screen_cmd(mpdclient_t *c, command_t cmd)
910         screen.input_timestamp = time(NULL);
911         screen.last_cmd = cmd;
912         welcome = FALSE;
914         if (mode_fn->cmd != NULL && mode_fn->cmd(&screen, c, cmd))
915                 return;
917         if (screen_client_cmd(c, cmd))
918                 return;
920         switch(cmd) {
921         case CMD_TOGGLE_FIND_WRAP:
922                 options.find_wrap = !options.find_wrap;
923                 screen_status_printf(options.find_wrap ?
924                                      _("Find mode: Wrapped") :
925                                      _("Find mode: Normal"));
926                 break;
927         case CMD_TOGGLE_AUTOCENTER:
928                 options.auto_center = !options.auto_center;
929                 screen_status_printf(options.auto_center ?
930                                      _("Auto center mode: On") :
931                                      _("Auto center mode: Off"));
932                 break;
933         case CMD_SCREEN_UPDATE:
934                 screen.painted = 0;
935                 break;
936         case CMD_SCREEN_PREVIOUS:
937                 screen_next_mode(c, -1);
938                 break;
939         case CMD_SCREEN_NEXT:
940                 screen_next_mode(c, 1);
941                 break;
942         case CMD_SCREEN_PLAY:
943                 switch_screen_mode(SCREEN_PLAYLIST_ID, c);
944                 break;
945         case CMD_SCREEN_FILE:
946                 switch_screen_mode(SCREEN_BROWSE_ID, c);
947                 break;
948         case CMD_SCREEN_HELP:
949                 switch_screen_mode(SCREEN_HELP_ID, c);
950                 break;
951         case CMD_SCREEN_SEARCH:
952                 switch_screen_mode(SCREEN_SEARCH_ID, c);
953                 break;
954         case CMD_SCREEN_ARTIST:
955                 switch_screen_mode(SCREEN_ARTIST_ID, c);
956                 break;
957         case CMD_SCREEN_KEYDEF:
958                 switch_screen_mode(SCREEN_KEYDEF_ID, c);
959                 break;
960         case CMD_SCREEN_LYRICS:
961                 switch_screen_mode(SCREEN_LYRICS_ID, c);
962                 break;
963         default:
964                 break;
965         }