Code

use the glib main event loop
[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 const struct screen_functions screen_playlist;
54 extern const struct screen_functions screen_browse;
55 #ifdef ENABLE_ARTIST_SCREEN
56 extern const struct screen_functions screen_artist;
57 #endif
58 extern const struct screen_functions screen_help;
59 #ifdef ENABLE_SEARCH_SCREEN
60 extern const struct screen_functions screen_search;
61 #endif
62 #ifdef ENABLE_KEYDEF_SCREEN
63 extern const struct screen_functions screen_keydef;
64 #endif
65 #ifdef ENABLE_CLOCK_SCREEN
66 extern const struct screen_functions screen_clock;
67 #endif
68 extern const struct screen_functions screen_lyrics;
70 typedef struct screen_functions * (*screen_get_mode_functions_fn_t) (void);
72 static const struct
73 {
74         gint id;
75         const gchar *name;
76         const struct screen_functions *functions;
77 } screens[] = {
78         { SCREEN_PLAYLIST_ID, "playlist", &screen_playlist },
79         { SCREEN_BROWSE_ID, "browse", &screen_browse },
80 #ifdef ENABLE_ARTIST_SCREEN
81         { SCREEN_ARTIST_ID, "artist", &screen_artist },
82 #endif
83         { SCREEN_HELP_ID, "help", &screen_help },
84 #ifdef ENABLE_SEARCH_SCREEN
85         { SCREEN_SEARCH_ID, "search", &screen_search },
86 #endif
87 #ifdef ENABLE_KEYDEF_SCREEN
88         { SCREEN_KEYDEF_ID, "keydef", &screen_keydef },
89 #endif
90 #ifdef ENABLE_CLOCK_SCREEN
91         { SCREEN_CLOCK_ID, "clock", &screen_clock },
92 #endif
93 #ifdef ENABLE_LYRICS_SCREEN
94         { SCREEN_LYRICS_ID, "lyrics", &screen_lyrics },
95 #endif
96 };
98 #define NUM_SCREENS (sizeof(screens) / sizeof(screens[0]))
100 static gboolean welcome = TRUE;
101 static struct screen screen;
102 static const struct screen_functions *mode_fn = &screen_playlist;
103 static int seek_id = -1;
104 static int seek_target_time = 0;
106 gint
107 screen_get_id(const char *name)
109         guint i;
111         for (i = 0; i < NUM_SCREENS; ++i)
112                 if (strcmp(name, screens[i].name) == 0)
113                         return screens[i].id;
115         return -1;
118 static gint
119 lookup_mode(gint id)
121         guint i;
123         for (i = 0; i < NUM_SCREENS; ++i)
124                 if (screens[i].id == id)
125                         return i;
127         return -1;
130 gint get_cur_mode_id(void)
132         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         new_mode = lookup_mode(id);
144         if (new_mode < 0)
145                 return;
147         /* close the old mode */
148         if (mode_fn->close != NULL)
149                 mode_fn->close();
151         /* get functions for the new mode */
152         D("switch_screen(%s)\n", screens[new_mode].name );
153         mode_fn = screens[new_mode].functions;
154         screen.mode = new_mode;
155         screen.painted = 0;
157         /* open the new mode */
158         if (mode_fn->open != NULL)
159                 mode_fn->open(&screen, c);
162 static int
163 find_configured_screen(const char *name)
165         unsigned i;
167         for (i = 0; options.screen_list[i] != NULL; ++i)
168                 if (strcmp(options.screen_list[i], name) == 0)
169                         return i;
171         return -1;
174 static void
175 screen_next_mode(mpdclient_t *c, int offset)
177         int max = g_strv_length(options.screen_list);
178         int current, next;
180         /* find current screen */
181         current = find_configured_screen(screens[screen.mode].name);
182         next = current + offset;
183         if (next<0)
184                 next = max-1;
185         else if (next>=max)
186                 next = 0;
188         D("current mode: %d:%d    next:%d\n", current, max, next);
189         switch_screen_mode(screen_get_id(options.screen_list[next]), c);
192 static void
193 paint_top_window2(const char *header, mpdclient_t *c)
195         char flags[5];
196         WINDOW *w = screen.top_window.w;
197         char buf[32];
199         if (header[0]) {
200                 colors_use(w, COLOR_TITLE_BOLD);
201                 mvwaddstr(w, 0, 0, header);
202         } else {
203                 colors_use(w, COLOR_TITLE_BOLD);
204                 waddstr(w, get_key_names(CMD_SCREEN_HELP, FALSE));
205                 colors_use(w, COLOR_TITLE);
206                 waddstr(w, _(":Help  "));
207                 colors_use(w, COLOR_TITLE_BOLD);
208                 waddstr(w, get_key_names(CMD_SCREEN_PLAY, FALSE));
209                 colors_use(w, COLOR_TITLE);
210                 waddstr(w, _(":Playlist  "));
211                 colors_use(w, COLOR_TITLE_BOLD);
212                 waddstr(w, get_key_names(CMD_SCREEN_FILE, FALSE));
213                 colors_use(w, COLOR_TITLE);
214                 waddstr(w, _(":Browse  "));
215 #ifdef ENABLE_ARTIST_SCREEN
216                 colors_use(w, COLOR_TITLE_BOLD);
217                 waddstr(w, get_key_names(CMD_SCREEN_ARTIST, FALSE));
218                 colors_use(w, COLOR_TITLE);
219                 waddstr(w, _(":Artist  "));
220 #endif
221 #ifdef ENABLE_SEARCH_SCREEN
222                 colors_use(w, COLOR_TITLE_BOLD);
223                 waddstr(w, get_key_names(CMD_SCREEN_SEARCH, FALSE));
224                 colors_use(w, COLOR_TITLE);
225                 waddstr(w, _(":Search  "));
226 #endif
227 #ifdef ENABLE_LYRICS_SCREEN
228                 colors_use(w, COLOR_TITLE_BOLD);
229                 waddstr(w, get_key_names(CMD_SCREEN_LYRICS, FALSE));
230                 colors_use(w, COLOR_TITLE);
231                 waddstr(w, _(":Lyrics  "));
232 #endif
233         }
234         if (c->status == NULL || c->status->volume == MPD_STATUS_NO_VOLUME) {
235                 g_snprintf(buf, 32, _("Volume n/a "));
236         } else {
237                 g_snprintf(buf, 32, _(" Volume %d%%"), c->status->volume);
238         }
239         colors_use(w, COLOR_TITLE);
240         mvwaddstr(w, 0, screen.top_window.cols-my_strlen(buf), buf);
242         flags[0] = 0;
243         if (c->status != NULL) {
244                 if (c->status->repeat)
245                         g_strlcat(flags, "r", sizeof(flags));
246                 if (c->status->random)
247                         g_strlcat(flags, "z", sizeof(flags));;
248                 if (c->status->crossfade)
249                         g_strlcat(flags, "x", sizeof(flags));
250                 if (c->status->updatingDb)
251                         g_strlcat(flags, "U", sizeof(flags));
252         }
254         colors_use(w, COLOR_LINE);
255         mvwhline(w, 1, 0, ACS_HLINE, screen.top_window.cols);
256         if (flags[0]) {
257                 wmove(w,1,screen.top_window.cols-strlen(flags)-3);
258                 waddch(w, '[');
259                 colors_use(w, COLOR_LINE_BOLD);
260                 waddstr(w, flags);
261                 colors_use(w, COLOR_LINE);
262                 waddch(w, ']');
263         }
264         wnoutrefresh(w);
267 static void
268 paint_top_window(const char *header, mpdclient_t *c, int full_repaint)
270         static int prev_volume = -1;
271         static size_t prev_header_len = -1;
272         WINDOW *w = screen.top_window.w;
274         if (prev_header_len!=my_strlen(header)) {
275                 prev_header_len = my_strlen(header);
276                 full_repaint = 1;
277         }
279         if (full_repaint) {
280                 wmove(w, 0, 0);
281                 wclrtoeol(w);
282         }
284         if ((c->status != NULL && prev_volume != c->status->volume) ||
285             full_repaint)
286                 paint_top_window2(header, c);
289 static void
290 paint_progress_window(mpdclient_t *c)
292         double p;
293         int width;
294         int elapsedTime;
296         if (c->status==NULL || IS_STOPPED(c->status->state)) {
297                 mvwhline(screen.progress_window.w, 0, 0, ACS_HLINE,
298                          screen.progress_window.cols);
299                 wnoutrefresh(screen.progress_window.w);
300                 return;
301         }
303         if (c->song && seek_id == c->song->id)
304                 elapsedTime = seek_target_time;
305         else
306                 elapsedTime = c->status->elapsedTime;
308         p = ((double) elapsedTime) / ((double) c->status->totalTime);
310         width = (int) (p * (double) screen.progress_window.cols);
311         mvwhline(screen.progress_window.w,
312                  0, 0,
313                  ACS_HLINE,
314                  screen.progress_window.cols);
315         whline(screen.progress_window.w, '=', width-1);
316         mvwaddch(screen.progress_window.w, 0, width-1, 'O');
317         wnoutrefresh(screen.progress_window.w);
320 static void
321 paint_status_window(mpdclient_t *c)
323         WINDOW *w = screen.status_window.w;
324         mpd_Status *status = c->status;
325         mpd_Song *song = c->song;
326         int elapsedTime = 0;
327         const char *str = NULL;
328         int x = 0;
330         if( time(NULL) - screen.status_timestamp <= SCREEN_STATUS_MESSAGE_TIME )
331                 return;
333         wmove(w, 0, 0);
334         wclrtoeol(w);
335         colors_use(w, COLOR_STATUS_BOLD);
337         switch (status == NULL ? MPD_STATUS_STATE_STOP : status->state) {
338         case MPD_STATUS_STATE_PLAY:
339                 str = _("Playing:");
340                 break;
341         case MPD_STATUS_STATE_PAUSE:
342                 str = _("[Paused]");
343                 break;
344         case MPD_STATUS_STATE_STOP:
345         default:
346                 break;
347         }
349         if (str) {
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 (status != NULL && (IS_PLAYING(status->state) ||
357                                IS_PAUSED(status->state))) {
358                 if (status->totalTime > 0) {
359                         /*checks the conf to see whether to display elapsed or remaining time */
360                         if(!strcmp(options.timedisplay_type,"elapsed"))
361                                 elapsedTime = c->status->elapsedTime;
362                         else if(!strcmp(options.timedisplay_type,"remaining"))
363                                 elapsedTime = (c->status->totalTime - c->status->elapsedTime);
365                         if( c->song && seek_id == c->song->id )
366                                 elapsedTime = seek_target_time;
367                         /*write out the time, using hours if time over 60 minutes*/
368                         if (c->status->totalTime > 3600) {
369                                 g_snprintf(screen.buf, screen.buf_size,
370                                            " [%i:%02i:%02i/%i:%02i:%02i]",
371                                            elapsedTime/3600, (elapsedTime%3600)/60, elapsedTime%60,
372                                            status->totalTime/3600, (status->totalTime%3600)/60,  status->totalTime%60);
373                         } else {
374                                 g_snprintf(screen.buf, screen.buf_size,
375                                            " [%i:%02i/%i:%02i]",
376                                            elapsedTime/60, elapsedTime%60,
377                                            status->totalTime/60,   status->totalTime%60 );
378                         }
379                 } else {
380                         g_snprintf(screen.buf, screen.buf_size,
381                                    " [%d kbps]", status->bitRate );
382                 }
383         } else {
384                 time_t timep;
386                 time(&timep);
387                 strftime(screen.buf, screen.buf_size, "%X ",localtime(&timep));
388         }
390         /* display song */
391         if (status != NULL && (IS_PLAYING(status->state) ||
392                                IS_PAUSED(status->state))) {
393                 char songname[MAX_SONGNAME_LENGTH];
394                 int width = COLS-x-my_strlen(screen.buf);
396                 if (song)
397                         strfsong(songname, MAX_SONGNAME_LENGTH, STATUS_FORMAT, song);
398                 else
399                         songname[0] = '\0';
401                 colors_use(w, COLOR_STATUS);
402                 /* scroll if the song name is to long */
403                 if (options.scroll && my_strlen(songname) > (size_t)width) {
404                         static  scroll_state_t st = { 0, 0 };
405                         char *tmp = strscroll(songname, options.scroll_sep, width, &st);
407                         g_strlcpy(songname, tmp, MAX_SONGNAME_LENGTH);
408                         g_free(tmp);
409                 }
410                 //mvwaddnstr(w, 0, x, songname, width);
411                 mvwaddstr(w, 0, x, songname);
412         }
414         /* display time string */
415         if (screen.buf[0]) {
416                 x = screen.status_window.cols - strlen(screen.buf);
417                 colors_use(w, COLOR_STATUS_TIME);
418                 mvwaddstr(w, 0, x, screen.buf);
419         }
421         wnoutrefresh(w);
424 void
425 screen_exit(void)
427         guint i;
429         endwin();
431         if (mode_fn->close != NULL)
432                 mode_fn->close();
434         /* close and exit all screens (playlist,browse,help...) */
435         for (i = 0; i < NUM_SCREENS; ++i) {
436                 const struct screen_functions *sf = screens[i].functions;
438                 if (sf->exit)
439                         sf->exit();
440         }
442         string_list_free(screen.find_history);
443         g_free(screen.buf);
444         g_free(screen.findbuf);
447 void
448 screen_resize(void)
450         guint i;
452         D("Resize rows %d->%d, cols %d->%d\n",screen.rows,LINES,screen.cols,COLS);
453         if (COLS<SCREEN_MIN_COLS || LINES<SCREEN_MIN_ROWS) {
454                 screen_exit();
455                 fprintf(stderr, _("Error: Screen to small!\n"));
456                 exit(EXIT_FAILURE);
457         }
459         resizeterm(LINES, COLS);
461         screen.cols = COLS;
462         screen.rows = LINES;
464         /* top window */
465         screen.top_window.cols = screen.cols;
466         wresize(screen.top_window.w, 2, screen.cols);
468         /* main window */
469         screen.main_window.cols = screen.cols;
470         screen.main_window.rows = screen.rows-4;
471         wresize(screen.main_window.w, screen.main_window.rows, screen.cols);
472         wclear(screen.main_window.w);
474         /* progress window */
475         screen.progress_window.cols = screen.cols;
476         wresize(screen.progress_window.w, 1, screen.cols);
477         mvwin(screen.progress_window.w, screen.rows-2, 0);
479         /* status window */
480         screen.status_window.cols = screen.cols;
481         wresize(screen.status_window.w, 1, screen.cols);
482         mvwin(screen.status_window.w, screen.rows-1, 0);
484         screen.buf_size = screen.cols;
485         g_free(screen.buf);
486         screen.buf = g_malloc(screen.cols);
488         /* close and exit all screens (playlist,browse,help...) */
489         for (i = 0; i < NUM_SCREENS; ++i) {
490                 const struct screen_functions *sf = screens[i].functions;
492                 if (sf->resize)
493                         sf->resize(screen.main_window.cols, screen.main_window.rows);
494         }
497         /* ? - without this the cursor becomes visible with aterm & Eterm */
498         curs_set(1);
499         curs_set(0);
501         screen.painted = 0;
504 void
505 screen_status_message(const char *msg)
507         WINDOW *w = screen.status_window.w;
509         wmove(w, 0, 0);
510         wclrtoeol(w);
511         colors_use(w, COLOR_STATUS_ALERT);
512         waddstr(w, msg);
513         wnoutrefresh(w);
514         screen.status_timestamp = time(NULL);
517 void
518 screen_status_printf(const char *format, ...)
520         char *msg;
521         va_list ap;
523         va_start(ap,format);
524         msg = g_strdup_vprintf(format,ap);
525         va_end(ap);
526         screen_status_message(msg);
527         g_free(msg);
530 void
531 ncurses_init(void)
534         /* initialize the curses library */
535         initscr();
536         /* initialize color support */
537         colors_start();
538         /* tell curses not to do NL->CR/NL on output */
539         nonl();
540         /*  use raw mode (ignore interrupt,quit,suspend, and flow control ) */
541 #ifdef ENABLE_RAW_MODE
542         //  raw();
543 #endif
544         /* don't echo input */
545         noecho();
546         /* set cursor invisible */
547         curs_set(0);
548         /* enable extra keys */
549         keypad(stdscr, TRUE);
550         /* initialize mouse support */
551 #ifdef HAVE_GETMOUSE
552         if (options.enable_mouse)
553                 mousemask(ALL_MOUSE_EVENTS, NULL);
554 #endif
556         if (COLS < SCREEN_MIN_COLS || LINES < SCREEN_MIN_ROWS) {
557                 fprintf(stderr, _("Error: Screen to small!\n"));
558                 exit(EXIT_FAILURE);
559         }
561         screen.mode = 0;
562         screen.cols = COLS;
563         screen.rows = LINES;
565         screen.buf  = g_malloc(screen.cols);
566         screen.buf_size = screen.cols;
567         screen.findbuf = NULL;
568         screen.painted = 0;
569         screen.start_timestamp = time(NULL);
570         screen.input_timestamp = time(NULL);
571         screen.last_cmd = CMD_NONE;
573         /* create top window */
574         screen.top_window.rows = 2;
575         screen.top_window.cols = screen.cols;
576         screen.top_window.w = newwin(screen.top_window.rows,
577                                       screen.top_window.cols,
578                                       0, 0);
579         leaveok(screen.top_window.w, TRUE);
580         keypad(screen.top_window.w, TRUE);
582         /* create main window */
583         screen.main_window.rows = screen.rows-4;
584         screen.main_window.cols = screen.cols;
585         screen.main_window.w = newwin(screen.main_window.rows,
586                                        screen.main_window.cols,
587                                        2,
588                                        0);
590         //  leaveok(screen.main_window.w, TRUE); temporary disabled
591         keypad(screen.main_window.w, TRUE);
593         /* create progress window */
594         screen.progress_window.rows = 1;
595         screen.progress_window.cols = screen.cols;
596         screen.progress_window.w = newwin(screen.progress_window.rows,
597                                            screen.progress_window.cols,
598                                            screen.rows-2,
599                                            0);
600         leaveok(screen.progress_window.w, TRUE);
602         /* create status window */
603         screen.status_window.rows = 1;
604         screen.status_window.cols = screen.cols;
605         screen.status_window.w = newwin(screen.status_window.rows,
606                                          screen.status_window.cols,
607                                          screen.rows-1,
608                                          0);
610         leaveok(screen.status_window.w, FALSE);
611         keypad(screen.status_window.w, TRUE);
613         if (options.enable_colors) {
614                 /* set background attributes */
615                 wbkgd(stdscr, COLOR_PAIR(COLOR_LIST));
616                 wbkgd(screen.main_window.w,     COLOR_PAIR(COLOR_LIST));
617                 wbkgd(screen.top_window.w,      COLOR_PAIR(COLOR_TITLE));
618                 wbkgd(screen.progress_window.w, COLOR_PAIR(COLOR_PROGRESSBAR));
619                 wbkgd(screen.status_window.w,   COLOR_PAIR(COLOR_STATUS));
620                 colors_use(screen.progress_window.w, COLOR_PROGRESSBAR);
621         }
623         refresh();
626 void
627 screen_init(mpdclient_t *c)
629         guint i;
631         /* initialize screens */
632         for (i = 0; i < NUM_SCREENS; ++i) {
633                 const struct screen_functions *fn = screens[i].functions;
635                 if (fn->init)
636                         fn->init(screen.main_window.w,
637                                  screen.main_window.cols,
638                                  screen.main_window.rows);
639         }
641         if (mode_fn->open != NULL)
642                 mode_fn->open(&screen, c);
644         /* initialize wreadln */
645         wrln_wgetch = my_wgetch;
646         wrln_max_history_length = 16;
649 void
650 screen_paint(mpdclient_t *c)
652         const char *title = NULL;
654         if (mode_fn->get_title != NULL)
655                 title = mode_fn->get_title(screen.buf, screen.buf_size);
657         D("screen_paint(%s)\n", title);
658         /* paint the title/header window */
659         if( title )
660                 paint_top_window(title, c, 1);
661         else
662                 paint_top_window("", c, 1);
664         /* paint the main window */
665         wclear(screen.main_window.w);
666         if (mode_fn->paint != NULL)
667                 mode_fn->paint(&screen, c);
669         paint_progress_window(c);
670         paint_status_window(c);
671         screen.painted = 1;
672         wmove(screen.main_window.w, 0, 0);
673         wnoutrefresh(screen.main_window.w);
675         /* tell curses to update */
676         doupdate();
679 void
680 screen_update(mpdclient_t *c)
682         static int repeat = -1;
683         static int random_enabled = -1;
684         static int crossfade = -1;
685         static int dbupdate = -1;
687         if( !screen.painted )
688                 return screen_paint(c);
690         /* print a message if mpd status has changed */
691         if (c->status != NULL) {
692                 if (repeat < 0) {
693                         repeat = c->status->repeat;
694                         random_enabled = c->status->random;
695                         crossfade = c->status->crossfade;
696                         dbupdate = c->status->updatingDb;
697                 }
699                 if (repeat != c->status->repeat)
700                         screen_status_printf(c->status->repeat ?
701                                              _("Repeat is on") :
702                                              _("Repeat is off"));
704                 if (random_enabled != c->status->random)
705                         screen_status_printf(c->status->random ?
706                                              _("Random is on") :
707                                              _("Random is off"));
709                 if (crossfade != c->status->crossfade)
710                         screen_status_printf(_("Crossfade %d seconds"), c->status->crossfade);
712                 if (dbupdate && dbupdate != c->status->updatingDb) {
713                         screen_status_printf(_("Database updated!"));
714                         mpdclient_browse_callback(c, BROWSE_DB_UPDATED, NULL);
715                 }
717                 repeat = c->status->repeat;
718                 random_enabled = c->status->random;
719                 crossfade = c->status->crossfade;
720                 dbupdate = c->status->updatingDb;
721         }
723         /* update title/header window */
724         if (welcome && screen.last_cmd==CMD_NONE &&
725             time(NULL)-screen.start_timestamp <= SCREEN_WELCOME_TIME)
726                 paint_top_window("", c, 0);
727         else if (mode_fn->get_title != NULL) {
728                 paint_top_window(mode_fn->get_title(screen.buf,screen.buf_size), c, 0);
729                 welcome = FALSE;
730         } else
731                 paint_top_window("", c, 0);
733         /* update the main window */
734         if (mode_fn->update != NULL)
735                 mode_fn->update(&screen, c);
737         /* update progress window */
738         paint_progress_window(c);
740         /* update status window */
741         paint_status_window(c);
743         /* move the cursor to the origin */
744         wmove(screen.main_window.w, 0, 0);
745         wnoutrefresh(screen.main_window.w);
747         /* tell curses to update */
748         doupdate();
751 void
752 screen_idle(mpdclient_t *c)
754         if (c->song && seek_id == c->song->id &&
755             (screen.last_cmd == CMD_SEEK_FORWARD ||
756              screen.last_cmd == CMD_SEEK_BACKWARD))
757                 mpdclient_cmd_seek(c, seek_id, seek_target_time);
759         screen.last_cmd = CMD_NONE;
760         seek_id = -1;
763 #ifdef HAVE_GETMOUSE
764 int
765 screen_get_mouse_event(mpdclient_t *c,
766                        list_window_t *lw, int lw_length,
767                        unsigned long *bstate, int *row)
769         MEVENT event;
771         /* retreive the mouse event from ncurses */
772         getmouse(&event);
773         D("mouse: id=%d  y=%d,x=%d,z=%d\n",event.id,event.y,event.x,event.z);
774         /* calculate the selected row in the list window */
775         *row = event.y - screen.top_window.rows;
776         /* copy button state bits */
777         *bstate = event.bstate;
778         /* if button 2 was pressed switch screen */
779         if (event.bstate & BUTTON2_CLICKED) {
780                 screen_cmd(c, CMD_SCREEN_NEXT);
781                 return 1;
782         }
784         /* if the even occured above the list window move up */
785         if (*row < 0 && lw) {
786                 if (event.bstate & BUTTON3_CLICKED)
787                         list_window_first(lw);
788                 else
789                         list_window_previous_page(lw);
790                 return 1;
791         }
793         /* if the even occured below the list window move down */
794         if ((unsigned)*row >= lw->rows && lw) {
795                 if (event.bstate & BUTTON3_CLICKED)
796                         list_window_last(lw, lw_length);
797                 else
798                         list_window_next_page(lw, lw_length);
799                 return 1;
800         }
802         return 0;
804 #endif
806 static int
807 screen_client_cmd(mpdclient_t *c, command_t cmd)
809         if (c->connection == NULL || c->status == NULL)
810                 return 0;
812         switch(cmd) {
813         case CMD_PLAY:
814                 mpdclient_cmd_play(c, MPD_PLAY_AT_BEGINNING);
815                 break;
816         case CMD_PAUSE:
817                 mpdclient_cmd_pause(c, !IS_PAUSED(c->status->state));
818                 break;
819         case CMD_STOP:
820                 mpdclient_cmd_stop(c);
821                 break;
822         case CMD_SEEK_FORWARD:
823                 if (!IS_STOPPED(c->status->state)) {
824                         if (c->song && seek_id != c->song->id) {
825                                 seek_id = c->song->id;
826                                 seek_target_time = c->status->elapsedTime;
827                         }
828                         seek_target_time+=options.seek_time;
829                         if (seek_target_time < c->status->totalTime)
830                                 break;
831                         seek_target_time = c->status->totalTime;
832                         /* seek_target_time=0; */
833                 }
834                 break;
835                 /* fall through... */
836         case CMD_TRACK_NEXT:
837                 if (!IS_STOPPED(c->status->state))
838                         mpdclient_cmd_next(c);
839                 break;
840         case CMD_SEEK_BACKWARD:
841                 if (!IS_STOPPED(c->status->state)) {
842                         if (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 < 0)
848                                 seek_target_time=0;
849                 }
850                 break;
851         case CMD_TRACK_PREVIOUS:
852                 if (!IS_STOPPED(c->status->state))
853                         mpdclient_cmd_prev(c);
854                 break;
855         case CMD_SHUFFLE:
856                 if (mpdclient_cmd_shuffle(c) == 0)
857                         screen_status_message(_("Shuffled playlist!"));
858                 break;
859         case CMD_CLEAR:
860                 if (mpdclient_cmd_clear(c) == 0)
861                         screen_status_message(_("Cleared playlist!"));
862                 break;
863         case CMD_REPEAT:
864                 mpdclient_cmd_repeat(c, !c->status->repeat);
865                 break;
866         case CMD_RANDOM:
867                 mpdclient_cmd_random(c, !c->status->random);
868                 break;
869         case CMD_CROSSFADE:
870                 if (c->status->crossfade)
871                         mpdclient_cmd_crossfade(c, 0);
872                 else
873                         mpdclient_cmd_crossfade(c, options.crossfade_time);
874                 break;
875         case CMD_DB_UPDATE:
876                 if (!c->status->updatingDb) {
877                         if( mpdclient_cmd_db_update_utf8(c,NULL)==0 )
878                                 screen_status_printf(_("Database update started!"));
879                 } else
880                         screen_status_printf(_("Database update running..."));
881                 break;
882         case CMD_VOLUME_UP:
883                 if( c->status->volume!=MPD_STATUS_NO_VOLUME && c->status->volume<100 )
884                         mpdclient_cmd_volume(c, ++c->status->volume);
885                 break;
886         case CMD_VOLUME_DOWN:
887                 if( c->status->volume!=MPD_STATUS_NO_VOLUME && c->status->volume>0 )
888                         mpdclient_cmd_volume(c, --c->status->volume);
889                 break;
891         default:
892                 return 0;
893         }
895         return 1;
898 void
899 screen_cmd(mpdclient_t *c, command_t cmd)
901         screen.input_timestamp = time(NULL);
902         screen.last_cmd = cmd;
903         welcome = FALSE;
905         if (mode_fn->cmd != NULL && mode_fn->cmd(&screen, c, cmd))
906                 return;
908         if (screen_client_cmd(c, cmd))
909                 return;
911         switch(cmd) {
912         case CMD_TOGGLE_FIND_WRAP:
913                 options.find_wrap = !options.find_wrap;
914                 screen_status_printf(options.find_wrap ?
915                                      _("Find mode: Wrapped") :
916                                      _("Find mode: Normal"));
917                 break;
918         case CMD_TOGGLE_AUTOCENTER:
919                 options.auto_center = !options.auto_center;
920                 screen_status_printf(options.auto_center ?
921                                      _("Auto center mode: On") :
922                                      _("Auto center mode: Off"));
923                 break;
924         case CMD_SCREEN_UPDATE:
925                 screen.painted = 0;
926                 break;
927         case CMD_SCREEN_PREVIOUS:
928                 screen_next_mode(c, -1);
929                 break;
930         case CMD_SCREEN_NEXT:
931                 screen_next_mode(c, 1);
932                 break;
933         case CMD_SCREEN_PLAY:
934                 switch_screen_mode(SCREEN_PLAYLIST_ID, c);
935                 break;
936         case CMD_SCREEN_FILE:
937                 switch_screen_mode(SCREEN_BROWSE_ID, c);
938                 break;
939         case CMD_SCREEN_HELP:
940                 switch_screen_mode(SCREEN_HELP_ID, c);
941                 break;
942         case CMD_SCREEN_SEARCH:
943                 switch_screen_mode(SCREEN_SEARCH_ID, c);
944                 break;
945         case CMD_SCREEN_ARTIST:
946                 switch_screen_mode(SCREEN_ARTIST_ID, c);
947                 break;
948         case CMD_SCREEN_KEYDEF:
949                 switch_screen_mode(SCREEN_KEYDEF_ID, c);
950                 break;
951         case CMD_SCREEN_CLOCK:
952                 switch_screen_mode(SCREEN_CLOCK_ID, c);
953                 break;
954         case CMD_SCREEN_LYRICS:
955                 switch_screen_mode(SCREEN_LYRICS_ID, c);
956                 break;
957         case CMD_QUIT:
958                 exit(EXIT_SUCCESS);
959         default:
960                 break;
961         }