Code

removed the clock screen
[ncmpc.git] / src / screen.c
1 /*
2  * $Id$
3  *
4  * (c) 2004 by Kalle Wallin <kaw@linux.se>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  */
21 #include "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         const char *str = NULL;
321         int x = 0;
323         if( time(NULL) - screen.status_timestamp <= SCREEN_STATUS_MESSAGE_TIME )
324                 return;
326         wmove(w, 0, 0);
327         wclrtoeol(w);
328         colors_use(w, COLOR_STATUS_BOLD);
330         switch (status == NULL ? MPD_STATUS_STATE_STOP : status->state) {
331         case MPD_STATUS_STATE_PLAY:
332                 str = _("Playing:");
333                 break;
334         case MPD_STATUS_STATE_PAUSE:
335                 str = _("[Paused]");
336                 break;
337         case MPD_STATUS_STATE_STOP:
338         default:
339                 break;
340         }
342         if (str) {
343                 waddstr(w, str);
344                 x += my_strlen(str)+1;
345         }
347         /* create time string */
348         memset(screen.buf, 0, screen.buf_size);
349         if (status != NULL && (IS_PLAYING(status->state) ||
350                                IS_PAUSED(status->state))) {
351                 if (status->totalTime > 0) {
352                         /*checks the conf to see whether to display elapsed or remaining time */
353                         if(!strcmp(options.timedisplay_type,"elapsed"))
354                                 elapsedTime = c->status->elapsedTime;
355                         else if(!strcmp(options.timedisplay_type,"remaining"))
356                                 elapsedTime = (c->status->totalTime - c->status->elapsedTime);
358                         if( c->song && seek_id == c->song->id )
359                                 elapsedTime = seek_target_time;
360                         /*write out the time, using hours if time over 60 minutes*/
361                         if (c->status->totalTime > 3600) {
362                                 g_snprintf(screen.buf, screen.buf_size,
363                                            " [%i:%02i:%02i/%i:%02i:%02i]",
364                                            elapsedTime/3600, (elapsedTime%3600)/60, elapsedTime%60,
365                                            status->totalTime/3600, (status->totalTime%3600)/60,  status->totalTime%60);
366                         } else {
367                                 g_snprintf(screen.buf, screen.buf_size,
368                                            " [%i:%02i/%i:%02i]",
369                                            elapsedTime/60, elapsedTime%60,
370                                            status->totalTime/60,   status->totalTime%60 );
371                         }
372                 } else {
373                         g_snprintf(screen.buf, screen.buf_size,
374                                    " [%d kbps]", status->bitRate );
375                 }
376         } else {
377                 time_t timep;
379                 time(&timep);
380                 strftime(screen.buf, screen.buf_size, "%X ",localtime(&timep));
381         }
383         /* display song */
384         if (status != NULL && (IS_PLAYING(status->state) ||
385                                IS_PAUSED(status->state))) {
386                 char songname[MAX_SONGNAME_LENGTH];
387                 int width = COLS-x-my_strlen(screen.buf);
389                 if (song)
390                         strfsong(songname, MAX_SONGNAME_LENGTH, STATUS_FORMAT, song);
391                 else
392                         songname[0] = '\0';
394                 colors_use(w, COLOR_STATUS);
395                 /* scroll if the song name is to long */
396                 if (options.scroll && my_strlen(songname) > (size_t)width) {
397                         static  scroll_state_t st = { 0, 0 };
398                         char *tmp = strscroll(songname, options.scroll_sep, width, &st);
400                         g_strlcpy(songname, tmp, MAX_SONGNAME_LENGTH);
401                         g_free(tmp);
402                 }
403                 //mvwaddnstr(w, 0, x, songname, width);
404                 mvwaddstr(w, 0, x, songname);
405         }
407         /* display time string */
408         if (screen.buf[0]) {
409                 x = screen.status_window.cols - strlen(screen.buf);
410                 colors_use(w, COLOR_STATUS_TIME);
411                 mvwaddstr(w, 0, x, screen.buf);
412         }
414         wnoutrefresh(w);
417 void
418 screen_exit(void)
420         guint i;
422         endwin();
424         if (mode_fn->close != NULL)
425                 mode_fn->close();
427         /* close and exit all screens (playlist,browse,help...) */
428         for (i = 0; i < NUM_SCREENS; ++i) {
429                 const struct screen_functions *sf = screens[i].functions;
431                 if (sf->exit)
432                         sf->exit();
433         }
435         string_list_free(screen.find_history);
436         g_free(screen.buf);
437         g_free(screen.findbuf);
440 void
441 screen_resize(void)
443         guint i;
445         D("Resize rows %d->%d, cols %d->%d\n",screen.rows,LINES,screen.cols,COLS);
446         if (COLS<SCREEN_MIN_COLS || LINES<SCREEN_MIN_ROWS) {
447                 screen_exit();
448                 fprintf(stderr, _("Error: Screen to small!\n"));
449                 exit(EXIT_FAILURE);
450         }
452         resizeterm(LINES, COLS);
454         screen.cols = COLS;
455         screen.rows = LINES;
457         /* top window */
458         screen.top_window.cols = screen.cols;
459         wresize(screen.top_window.w, 2, screen.cols);
461         /* main window */
462         screen.main_window.cols = screen.cols;
463         screen.main_window.rows = screen.rows-4;
464         wresize(screen.main_window.w, screen.main_window.rows, screen.cols);
465         wclear(screen.main_window.w);
467         /* progress window */
468         screen.progress_window.cols = screen.cols;
469         wresize(screen.progress_window.w, 1, screen.cols);
470         mvwin(screen.progress_window.w, screen.rows-2, 0);
472         /* status window */
473         screen.status_window.cols = screen.cols;
474         wresize(screen.status_window.w, 1, screen.cols);
475         mvwin(screen.status_window.w, screen.rows-1, 0);
477         screen.buf_size = screen.cols;
478         g_free(screen.buf);
479         screen.buf = g_malloc(screen.cols);
481         /* close and exit all screens (playlist,browse,help...) */
482         for (i = 0; i < NUM_SCREENS; ++i) {
483                 const struct screen_functions *sf = screens[i].functions;
485                 if (sf->resize)
486                         sf->resize(screen.main_window.cols, screen.main_window.rows);
487         }
490         /* ? - without this the cursor becomes visible with aterm & Eterm */
491         curs_set(1);
492         curs_set(0);
494         screen.painted = 0;
497 void
498 screen_status_message(const char *msg)
500         WINDOW *w = screen.status_window.w;
502         wmove(w, 0, 0);
503         wclrtoeol(w);
504         colors_use(w, COLOR_STATUS_ALERT);
505         waddstr(w, msg);
506         wnoutrefresh(w);
507         screen.status_timestamp = time(NULL);
510 void
511 screen_status_printf(const char *format, ...)
513         char *msg;
514         va_list ap;
516         va_start(ap,format);
517         msg = g_strdup_vprintf(format,ap);
518         va_end(ap);
519         screen_status_message(msg);
520         g_free(msg);
523 void
524 ncurses_init(void)
527         /* initialize the curses library */
528         initscr();
529         /* initialize color support */
530         colors_start();
531         /* tell curses not to do NL->CR/NL on output */
532         nonl();
533         /*  use raw mode (ignore interrupt,quit,suspend, and flow control ) */
534 #ifdef ENABLE_RAW_MODE
535         //  raw();
536 #endif
537         /* don't echo input */
538         noecho();
539         /* set cursor invisible */
540         curs_set(0);
541         /* enable extra keys */
542         keypad(stdscr, TRUE);
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                 fprintf(stderr, _("Error: Screen to small!\n"));
551                 exit(EXIT_FAILURE);
552         }
554         screen.mode = 0;
555         screen.cols = COLS;
556         screen.rows = LINES;
558         screen.buf  = g_malloc(screen.cols);
559         screen.buf_size = screen.cols;
560         screen.findbuf = NULL;
561         screen.painted = 0;
562         screen.start_timestamp = time(NULL);
563         screen.input_timestamp = time(NULL);
564         screen.last_cmd = CMD_NONE;
566         /* create top window */
567         screen.top_window.rows = 2;
568         screen.top_window.cols = screen.cols;
569         screen.top_window.w = newwin(screen.top_window.rows,
570                                       screen.top_window.cols,
571                                       0, 0);
572         leaveok(screen.top_window.w, TRUE);
573         keypad(screen.top_window.w, TRUE);
575         /* create main window */
576         screen.main_window.rows = screen.rows-4;
577         screen.main_window.cols = screen.cols;
578         screen.main_window.w = newwin(screen.main_window.rows,
579                                        screen.main_window.cols,
580                                        2,
581                                        0);
583         //  leaveok(screen.main_window.w, TRUE); temporary disabled
584         keypad(screen.main_window.w, TRUE);
586         /* create progress window */
587         screen.progress_window.rows = 1;
588         screen.progress_window.cols = screen.cols;
589         screen.progress_window.w = newwin(screen.progress_window.rows,
590                                            screen.progress_window.cols,
591                                            screen.rows-2,
592                                            0);
593         leaveok(screen.progress_window.w, TRUE);
595         /* create status window */
596         screen.status_window.rows = 1;
597         screen.status_window.cols = screen.cols;
598         screen.status_window.w = newwin(screen.status_window.rows,
599                                          screen.status_window.cols,
600                                          screen.rows-1,
601                                          0);
603         leaveok(screen.status_window.w, FALSE);
604         keypad(screen.status_window.w, TRUE);
606         if (options.enable_colors) {
607                 /* set background attributes */
608                 wbkgd(stdscr, COLOR_PAIR(COLOR_LIST));
609                 wbkgd(screen.main_window.w,     COLOR_PAIR(COLOR_LIST));
610                 wbkgd(screen.top_window.w,      COLOR_PAIR(COLOR_TITLE));
611                 wbkgd(screen.progress_window.w, COLOR_PAIR(COLOR_PROGRESSBAR));
612                 wbkgd(screen.status_window.w,   COLOR_PAIR(COLOR_STATUS));
613                 colors_use(screen.progress_window.w, COLOR_PROGRESSBAR);
614         }
616         refresh();
619 void
620 screen_init(mpdclient_t *c)
622         guint i;
624         /* initialize screens */
625         for (i = 0; i < NUM_SCREENS; ++i) {
626                 const struct screen_functions *fn = screens[i].functions;
628                 if (fn->init)
629                         fn->init(screen.main_window.w,
630                                  screen.main_window.cols,
631                                  screen.main_window.rows);
632         }
634         if (mode_fn->open != NULL)
635                 mode_fn->open(&screen, c);
637         /* initialize wreadln */
638         wrln_wgetch = my_wgetch;
639         wrln_max_history_length = 16;
642 void
643 screen_paint(mpdclient_t *c)
645         const char *title = NULL;
647         if (mode_fn->get_title != NULL)
648                 title = mode_fn->get_title(screen.buf, screen.buf_size);
650         D("screen_paint(%s)\n", title);
651         /* paint the title/header window */
652         if( title )
653                 paint_top_window(title, c, 1);
654         else
655                 paint_top_window("", c, 1);
657         /* paint the main window */
658         wclear(screen.main_window.w);
659         if (mode_fn->paint != NULL)
660                 mode_fn->paint(&screen, c);
662         paint_progress_window(c);
663         paint_status_window(c);
664         screen.painted = 1;
665         wmove(screen.main_window.w, 0, 0);
666         wnoutrefresh(screen.main_window.w);
668         /* tell curses to update */
669         doupdate();
672 void
673 screen_update(mpdclient_t *c)
675         static int repeat = -1;
676         static int random_enabled = -1;
677         static int crossfade = -1;
678         static int dbupdate = -1;
680         if( !screen.painted )
681                 return screen_paint(c);
683         /* print a message if mpd status has changed */
684         if (c->status != NULL) {
685                 if (repeat < 0) {
686                         repeat = c->status->repeat;
687                         random_enabled = c->status->random;
688                         crossfade = c->status->crossfade;
689                         dbupdate = c->status->updatingDb;
690                 }
692                 if (repeat != c->status->repeat)
693                         screen_status_printf(c->status->repeat ?
694                                              _("Repeat is on") :
695                                              _("Repeat is off"));
697                 if (random_enabled != c->status->random)
698                         screen_status_printf(c->status->random ?
699                                              _("Random is on") :
700                                              _("Random is off"));
702                 if (crossfade != c->status->crossfade)
703                         screen_status_printf(_("Crossfade %d seconds"), c->status->crossfade);
705                 if (dbupdate && dbupdate != c->status->updatingDb) {
706                         screen_status_printf(_("Database updated!"));
707                         mpdclient_browse_callback(c, BROWSE_DB_UPDATED, NULL);
708                 }
710                 repeat = c->status->repeat;
711                 random_enabled = c->status->random;
712                 crossfade = c->status->crossfade;
713                 dbupdate = c->status->updatingDb;
714         }
716         /* update title/header window */
717         if (welcome && screen.last_cmd==CMD_NONE &&
718             time(NULL)-screen.start_timestamp <= SCREEN_WELCOME_TIME)
719                 paint_top_window("", c, 0);
720         else if (mode_fn->get_title != NULL) {
721                 paint_top_window(mode_fn->get_title(screen.buf,screen.buf_size), c, 0);
722                 welcome = FALSE;
723         } else
724                 paint_top_window("", c, 0);
726         /* update the main window */
727         if (mode_fn->update != NULL)
728                 mode_fn->update(&screen, c);
730         /* update progress window */
731         paint_progress_window(c);
733         /* update status window */
734         paint_status_window(c);
736         /* move the cursor to the origin */
737         wmove(screen.main_window.w, 0, 0);
738         wnoutrefresh(screen.main_window.w);
740         /* tell curses to update */
741         doupdate();
744 void
745 screen_idle(mpdclient_t *c)
747         if (c->song && seek_id == c->song->id &&
748             (screen.last_cmd == CMD_SEEK_FORWARD ||
749              screen.last_cmd == CMD_SEEK_BACKWARD))
750                 mpdclient_cmd_seek(c, seek_id, seek_target_time);
752         screen.last_cmd = CMD_NONE;
753         seek_id = -1;
756 #ifdef HAVE_GETMOUSE
757 int
758 screen_get_mouse_event(mpdclient_t *c,
759                        list_window_t *lw, int lw_length,
760                        unsigned long *bstate, int *row)
762         MEVENT event;
764         /* retreive the mouse event from ncurses */
765         getmouse(&event);
766         D("mouse: id=%d  y=%d,x=%d,z=%d\n",event.id,event.y,event.x,event.z);
767         /* calculate the selected row in the list window */
768         *row = event.y - screen.top_window.rows;
769         /* copy button state bits */
770         *bstate = event.bstate;
771         /* if button 2 was pressed switch screen */
772         if (event.bstate & BUTTON2_CLICKED) {
773                 screen_cmd(c, CMD_SCREEN_NEXT);
774                 return 1;
775         }
777         /* if the even occured above the list window move up */
778         if (*row < 0 && lw) {
779                 if (event.bstate & BUTTON3_CLICKED)
780                         list_window_first(lw);
781                 else
782                         list_window_previous_page(lw);
783                 return 1;
784         }
786         /* if the even occured below the list window move down */
787         if ((unsigned)*row >= lw->rows && lw) {
788                 if (event.bstate & BUTTON3_CLICKED)
789                         list_window_last(lw, lw_length);
790                 else
791                         list_window_next_page(lw, lw_length);
792                 return 1;
793         }
795         return 0;
797 #endif
799 static int
800 screen_client_cmd(mpdclient_t *c, command_t cmd)
802         if (c->connection == NULL || c->status == NULL)
803                 return 0;
805         switch(cmd) {
806                 /*
807         case CMD_PLAY:
808                 mpdclient_cmd_play(c, MPD_PLAY_AT_BEGINNING);
809                 break;
810                 */
811         case CMD_PAUSE:
812                 mpdclient_cmd_pause(c, !IS_PAUSED(c->status->state));
813                 break;
814         case CMD_STOP:
815                 mpdclient_cmd_stop(c);
816                 break;
817         case CMD_SEEK_FORWARD:
818                 if (!IS_STOPPED(c->status->state)) {
819                         if (c->song && seek_id != c->song->id) {
820                                 seek_id = c->song->id;
821                                 seek_target_time = c->status->elapsedTime;
822                         }
823                         seek_target_time+=options.seek_time;
824                         if (seek_target_time < c->status->totalTime)
825                                 break;
826                         seek_target_time = c->status->totalTime;
827                         /* seek_target_time=0; */
828                 }
829                 break;
830                 /* fall through... */
831         case CMD_TRACK_NEXT:
832                 if (!IS_STOPPED(c->status->state))
833                         mpdclient_cmd_next(c);
834                 break;
835         case CMD_SEEK_BACKWARD:
836                 if (!IS_STOPPED(c->status->state)) {
837                         if (seek_id != c->song->id) {
838                                 seek_id = c->song->id;
839                                 seek_target_time = c->status->elapsedTime;
840                         }
841                         seek_target_time-=options.seek_time;
842                         if (seek_target_time < 0)
843                                 seek_target_time=0;
844                 }
845                 break;
846         case CMD_TRACK_PREVIOUS:
847                 if (!IS_STOPPED(c->status->state))
848                         mpdclient_cmd_prev(c);
849                 break;
850         case CMD_SHUFFLE:
851                 if (mpdclient_cmd_shuffle(c) == 0)
852                         screen_status_message(_("Shuffled playlist!"));
853                 break;
854         case CMD_CLEAR:
855                 if (mpdclient_cmd_clear(c) == 0)
856                         screen_status_message(_("Cleared playlist!"));
857                 break;
858         case CMD_REPEAT:
859                 mpdclient_cmd_repeat(c, !c->status->repeat);
860                 break;
861         case CMD_RANDOM:
862                 mpdclient_cmd_random(c, !c->status->random);
863                 break;
864         case CMD_CROSSFADE:
865                 if (c->status->crossfade)
866                         mpdclient_cmd_crossfade(c, 0);
867                 else
868                         mpdclient_cmd_crossfade(c, options.crossfade_time);
869                 break;
870         case CMD_DB_UPDATE:
871                 if (!c->status->updatingDb) {
872                         if( mpdclient_cmd_db_update_utf8(c,NULL)==0 )
873                                 screen_status_printf(_("Database update started!"));
874                 } else
875                         screen_status_printf(_("Database update running..."));
876                 break;
877         case CMD_VOLUME_UP:
878                 if( c->status->volume!=MPD_STATUS_NO_VOLUME && c->status->volume<100 )
879                         mpdclient_cmd_volume(c, ++c->status->volume);
880                 break;
881         case CMD_VOLUME_DOWN:
882                 if( c->status->volume!=MPD_STATUS_NO_VOLUME && c->status->volume>0 )
883                         mpdclient_cmd_volume(c, --c->status->volume);
884                 break;
886         default:
887                 return 0;
888         }
890         return 1;
893 void
894 screen_cmd(mpdclient_t *c, command_t cmd)
896         screen.input_timestamp = time(NULL);
897         screen.last_cmd = cmd;
898         welcome = FALSE;
900         if (mode_fn->cmd != NULL && mode_fn->cmd(&screen, c, cmd))
901                 return;
903         if (screen_client_cmd(c, cmd))
904                 return;
906         switch(cmd) {
907         case CMD_TOGGLE_FIND_WRAP:
908                 options.find_wrap = !options.find_wrap;
909                 screen_status_printf(options.find_wrap ?
910                                      _("Find mode: Wrapped") :
911                                      _("Find mode: Normal"));
912                 break;
913         case CMD_TOGGLE_AUTOCENTER:
914                 options.auto_center = !options.auto_center;
915                 screen_status_printf(options.auto_center ?
916                                      _("Auto center mode: On") :
917                                      _("Auto center mode: Off"));
918                 break;
919         case CMD_SCREEN_UPDATE:
920                 screen.painted = 0;
921                 break;
922         case CMD_SCREEN_PREVIOUS:
923                 screen_next_mode(c, -1);
924                 break;
925         case CMD_SCREEN_NEXT:
926                 screen_next_mode(c, 1);
927                 break;
928         case CMD_SCREEN_PLAY:
929                 switch_screen_mode(SCREEN_PLAYLIST_ID, c);
930                 break;
931         case CMD_SCREEN_FILE:
932                 switch_screen_mode(SCREEN_BROWSE_ID, c);
933                 break;
934         case CMD_SCREEN_HELP:
935                 switch_screen_mode(SCREEN_HELP_ID, c);
936                 break;
937         case CMD_SCREEN_SEARCH:
938                 switch_screen_mode(SCREEN_SEARCH_ID, c);
939                 break;
940         case CMD_SCREEN_ARTIST:
941                 switch_screen_mode(SCREEN_ARTIST_ID, c);
942                 break;
943         case CMD_SCREEN_KEYDEF:
944                 switch_screen_mode(SCREEN_KEYDEF_ID, c);
945                 break;
946         case CMD_SCREEN_LYRICS:
947                 switch_screen_mode(SCREEN_LYRICS_ID, c);
948                 break;
949         default:
950                 break;
951         }