Code

screen: don't compile disabled sources
[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 #ifdef ENABLE_LYRICS_SCREEN
65 extern const struct screen_functions screen_lyrics;
66 #endif
68 typedef struct screen_functions * (*screen_get_mode_functions_fn_t) (void);
70 static const struct
71 {
72         gint id;
73         const gchar *name;
74         const struct screen_functions *functions;
75 } screens[] = {
76         { SCREEN_PLAYLIST_ID, "playlist", &screen_playlist },
77         { SCREEN_BROWSE_ID, "browse", &screen_browse },
78 #ifdef ENABLE_ARTIST_SCREEN
79         { SCREEN_ARTIST_ID, "artist", &screen_artist },
80 #endif
81         { SCREEN_HELP_ID, "help", &screen_help },
82 #ifdef ENABLE_SEARCH_SCREEN
83         { SCREEN_SEARCH_ID, "search", &screen_search },
84 #endif
85 #ifdef ENABLE_KEYDEF_SCREEN
86         { SCREEN_KEYDEF_ID, "keydef", &screen_keydef },
87 #endif
88 #ifdef ENABLE_LYRICS_SCREEN
89         { SCREEN_LYRICS_ID, "lyrics", &screen_lyrics },
90 #endif
91 };
93 #define NUM_SCREENS (sizeof(screens) / sizeof(screens[0]))
95 static gboolean welcome = TRUE;
96 static struct screen screen;
97 static const struct screen_functions *mode_fn = &screen_playlist;
98 static int seek_id = -1;
99 static int seek_target_time = 0;
101 gint
102 screen_get_id(const char *name)
104         guint i;
106         for (i = 0; i < NUM_SCREENS; ++i)
107                 if (strcmp(name, screens[i].name) == 0)
108                         return screens[i].id;
110         return -1;
113 static gint
114 lookup_mode(gint id)
116         guint i;
118         for (i = 0; i < NUM_SCREENS; ++i)
119                 if (screens[i].id == id)
120                         return i;
122         return -1;
125 gint get_cur_mode_id(void)
127         return screens[screen.mode].id;
130 static void
131 switch_screen_mode(gint id, mpdclient_t *c)
133         gint new_mode;
135         if( id == screens[screen.mode].id )
136                 return;
138         new_mode = lookup_mode(id);
139         if (new_mode < 0)
140                 return;
142         /* close the old mode */
143         if (mode_fn->close != NULL)
144                 mode_fn->close();
146         /* get functions for the new mode */
147         D("switch_screen(%s)\n", screens[new_mode].name );
148         mode_fn = screens[new_mode].functions;
149         screen.mode = new_mode;
150         screen.painted = 0;
152         /* open the new mode */
153         if (mode_fn->open != NULL)
154                 mode_fn->open(&screen, c);
157 static int
158 find_configured_screen(const char *name)
160         unsigned i;
162         for (i = 0; options.screen_list[i] != NULL; ++i)
163                 if (strcmp(options.screen_list[i], name) == 0)
164                         return i;
166         return -1;
169 static void
170 screen_next_mode(mpdclient_t *c, int offset)
172         int max = g_strv_length(options.screen_list);
173         int current, next;
175         /* find current screen */
176         current = find_configured_screen(screens[screen.mode].name);
177         next = current + offset;
178         if (next<0)
179                 next = max-1;
180         else if (next>=max)
181                 next = 0;
183         D("current mode: %d:%d    next:%d\n", current, max, next);
184         switch_screen_mode(screen_get_id(options.screen_list[next]), c);
187 static void
188 paint_top_window2(const char *header, mpdclient_t *c)
190         char flags[5];
191         WINDOW *w = screen.top_window.w;
192         char buf[32];
194         if (header[0]) {
195                 colors_use(w, COLOR_TITLE_BOLD);
196                 mvwaddstr(w, 0, 0, header);
197         } else {
198                 colors_use(w, COLOR_TITLE_BOLD);
199                 waddstr(w, get_key_names(CMD_SCREEN_HELP, FALSE));
200                 colors_use(w, COLOR_TITLE);
201                 waddstr(w, _(":Help  "));
202                 colors_use(w, COLOR_TITLE_BOLD);
203                 waddstr(w, get_key_names(CMD_SCREEN_PLAY, FALSE));
204                 colors_use(w, COLOR_TITLE);
205                 waddstr(w, _(":Playlist  "));
206                 colors_use(w, COLOR_TITLE_BOLD);
207                 waddstr(w, get_key_names(CMD_SCREEN_FILE, FALSE));
208                 colors_use(w, COLOR_TITLE);
209                 waddstr(w, _(":Browse  "));
210 #ifdef ENABLE_ARTIST_SCREEN
211                 colors_use(w, COLOR_TITLE_BOLD);
212                 waddstr(w, get_key_names(CMD_SCREEN_ARTIST, FALSE));
213                 colors_use(w, COLOR_TITLE);
214                 waddstr(w, _(":Artist  "));
215 #endif
216 #ifdef ENABLE_SEARCH_SCREEN
217                 colors_use(w, COLOR_TITLE_BOLD);
218                 waddstr(w, get_key_names(CMD_SCREEN_SEARCH, FALSE));
219                 colors_use(w, COLOR_TITLE);
220                 waddstr(w, _(":Search  "));
221 #endif
222 #ifdef ENABLE_LYRICS_SCREEN
223                 colors_use(w, COLOR_TITLE_BOLD);
224                 waddstr(w, get_key_names(CMD_SCREEN_LYRICS, FALSE));
225                 colors_use(w, COLOR_TITLE);
226                 waddstr(w, _(":Lyrics  "));
227 #endif
228         }
229         if (c->status == NULL || c->status->volume == MPD_STATUS_NO_VOLUME) {
230                 g_snprintf(buf, 32, _("Volume n/a "));
231         } else {
232                 g_snprintf(buf, 32, _(" Volume %d%%"), c->status->volume);
233         }
234         colors_use(w, COLOR_TITLE);
235         mvwaddstr(w, 0, screen.top_window.cols-my_strlen(buf), buf);
237         flags[0] = 0;
238         if (c->status != NULL) {
239                 if (c->status->repeat)
240                         g_strlcat(flags, "r", sizeof(flags));
241                 if (c->status->random)
242                         g_strlcat(flags, "z", sizeof(flags));;
243                 if (c->status->crossfade)
244                         g_strlcat(flags, "x", sizeof(flags));
245                 if (c->status->updatingDb)
246                         g_strlcat(flags, "U", sizeof(flags));
247         }
249         colors_use(w, COLOR_LINE);
250         mvwhline(w, 1, 0, ACS_HLINE, screen.top_window.cols);
251         if (flags[0]) {
252                 wmove(w,1,screen.top_window.cols-strlen(flags)-3);
253                 waddch(w, '[');
254                 colors_use(w, COLOR_LINE_BOLD);
255                 waddstr(w, flags);
256                 colors_use(w, COLOR_LINE);
257                 waddch(w, ']');
258         }
259         wnoutrefresh(w);
262 static void
263 paint_top_window(const char *header, mpdclient_t *c, int full_repaint)
265         static int prev_volume = -1;
266         static size_t prev_header_len = -1;
267         WINDOW *w = screen.top_window.w;
269         if (prev_header_len!=my_strlen(header)) {
270                 prev_header_len = my_strlen(header);
271                 full_repaint = 1;
272         }
274         if (full_repaint) {
275                 wmove(w, 0, 0);
276                 wclrtoeol(w);
277         }
279         if ((c->status != NULL && prev_volume != c->status->volume) ||
280             full_repaint)
281                 paint_top_window2(header, c);
284 static void
285 paint_progress_window(mpdclient_t *c)
287         double p;
288         int width;
289         int elapsedTime;
291         if (c->status==NULL || IS_STOPPED(c->status->state)) {
292                 mvwhline(screen.progress_window.w, 0, 0, ACS_HLINE,
293                          screen.progress_window.cols);
294                 wnoutrefresh(screen.progress_window.w);
295                 return;
296         }
298         if (c->song && seek_id == c->song->id)
299                 elapsedTime = seek_target_time;
300         else
301                 elapsedTime = c->status->elapsedTime;
303         p = ((double) elapsedTime) / ((double) c->status->totalTime);
305         width = (int) (p * (double) screen.progress_window.cols);
306         mvwhline(screen.progress_window.w,
307                  0, 0,
308                  ACS_HLINE,
309                  screen.progress_window.cols);
310         whline(screen.progress_window.w, '=', width-1);
311         mvwaddch(screen.progress_window.w, 0, width-1, 'O');
312         wnoutrefresh(screen.progress_window.w);
315 static void
316 paint_status_window(mpdclient_t *c)
318         WINDOW *w = screen.status_window.w;
319         mpd_Status *status = c->status;
320         mpd_Song *song = c->song;
321         int elapsedTime = 0;
322         char bitrate[16];
323         const char *str = NULL;
324         int x = 0;
326         if( time(NULL) - screen.status_timestamp <= SCREEN_STATUS_MESSAGE_TIME )
327                 return;
329         wmove(w, 0, 0);
330         wclrtoeol(w);
331         colors_use(w, COLOR_STATUS_BOLD);
333         switch (status == NULL ? MPD_STATUS_STATE_STOP : status->state) {
334         case MPD_STATUS_STATE_PLAY:
335                 str = _("Playing:");
336                 break;
337         case MPD_STATUS_STATE_PAUSE:
338                 str = _("[Paused]");
339                 break;
340         case MPD_STATUS_STATE_STOP:
341         default:
342                 break;
343         }
345         if (str) {
346                 waddstr(w, str);
347                 x += my_strlen(str)+1;
348         }
350         /* create time string */
351         memset(screen.buf, 0, screen.buf_size);
352         if (status != NULL && (IS_PLAYING(status->state) ||
353                                IS_PAUSED(status->state))) {
354                 if (status->totalTime > 0) {
355                         /*checks the conf to see whether to display elapsed or remaining time */
356                         if(!strcmp(options.timedisplay_type,"elapsed"))
357                                 elapsedTime = c->status->elapsedTime;
358                         else if(!strcmp(options.timedisplay_type,"remaining"))
359                                 elapsedTime = (c->status->totalTime - c->status->elapsedTime);
361                         if( c->song && seek_id == c->song->id )
362                                 elapsedTime = seek_target_time;
364                         /* display bitrate if visible-bitrate is true */
365                         if (options.visible_bitrate) {
366                                 g_snprintf(bitrate, 16,
367                                            " [%d kbps]", status->bitRate);
368                         } else {
369                                 bitrate[0] = '\0';
370                         }
372                         /*write out the time, using hours if time over 60 minutes*/
373                         if (c->status->totalTime > 3600) {
374                                 g_snprintf(screen.buf, screen.buf_size,
375                                            "%s [%i:%02i:%02i/%i:%02i:%02i]",
376                                            bitrate, elapsedTime/3600, (elapsedTime%3600)/60, elapsedTime%60,
377                                            status->totalTime/3600, (status->totalTime%3600)/60,  status->totalTime%60);
378                         } else {
379                                 g_snprintf(screen.buf, screen.buf_size,
380                                            "%s [%i:%02i/%i:%02i]",
381                                            bitrate, elapsedTime/60, elapsedTime%60,
382                                            status->totalTime/60,   status->totalTime%60 );
383                         }
384                 } else {
385                         g_snprintf(screen.buf, screen.buf_size,
386                                    " [%d kbps]", status->bitRate );
387                 }
388         } else {
389                 time_t timep;
391                 time(&timep);
392                 strftime(screen.buf, screen.buf_size, "%X ",localtime(&timep));
393         }
395         /* display song */
396         if (status != NULL && (IS_PLAYING(status->state) ||
397                                IS_PAUSED(status->state))) {
398                 char songname[MAX_SONGNAME_LENGTH];
399                 int width = COLS-x-my_strlen(screen.buf);
401                 if (song)
402                         strfsong(songname, MAX_SONGNAME_LENGTH, STATUS_FORMAT, song);
403                 else
404                         songname[0] = '\0';
406                 colors_use(w, COLOR_STATUS);
407                 /* scroll if the song name is to long */
408                 if (options.scroll && my_strlen(songname) > (size_t)width) {
409                         static  scroll_state_t st = { 0, 0 };
410                         char *tmp = strscroll(songname, options.scroll_sep, width, &st);
412                         g_strlcpy(songname, tmp, MAX_SONGNAME_LENGTH);
413                         g_free(tmp);
414                 }
415                 //mvwaddnstr(w, 0, x, songname, width);
416                 mvwaddstr(w, 0, x, songname);
417         }
419         /* display time string */
420         if (screen.buf[0]) {
421                 x = screen.status_window.cols - strlen(screen.buf);
422                 colors_use(w, COLOR_STATUS_TIME);
423                 mvwaddstr(w, 0, x, screen.buf);
424         }
426         wnoutrefresh(w);
429 void
430 screen_exit(void)
432         guint i;
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 screen_init(mpdclient_t *c)
536         guint i;
538         if (COLS < SCREEN_MIN_COLS || LINES < SCREEN_MIN_ROWS) {
539                 fprintf(stderr, _("Error: Screen to small!\n"));
540                 exit(EXIT_FAILURE);
541         }
543         screen.mode = 0;
544         screen.cols = COLS;
545         screen.rows = LINES;
547         screen.buf  = g_malloc(screen.cols);
548         screen.buf_size = screen.cols;
549         screen.findbuf = NULL;
550         screen.painted = 0;
551         screen.start_timestamp = time(NULL);
552         screen.last_cmd = CMD_NONE;
554         /* create top window */
555         screen.top_window.rows = 2;
556         screen.top_window.cols = screen.cols;
557         screen.top_window.w = newwin(screen.top_window.rows,
558                                       screen.top_window.cols,
559                                       0, 0);
560         leaveok(screen.top_window.w, TRUE);
561         keypad(screen.top_window.w, TRUE);
563         /* create main window */
564         screen.main_window.rows = screen.rows-4;
565         screen.main_window.cols = screen.cols;
566         screen.main_window.w = newwin(screen.main_window.rows,
567                                        screen.main_window.cols,
568                                        2,
569                                        0);
571         //  leaveok(screen.main_window.w, TRUE); temporary disabled
572         keypad(screen.main_window.w, TRUE);
574         /* create progress window */
575         screen.progress_window.rows = 1;
576         screen.progress_window.cols = screen.cols;
577         screen.progress_window.w = newwin(screen.progress_window.rows,
578                                            screen.progress_window.cols,
579                                            screen.rows-2,
580                                            0);
581         leaveok(screen.progress_window.w, TRUE);
583         /* create status window */
584         screen.status_window.rows = 1;
585         screen.status_window.cols = screen.cols;
586         screen.status_window.w = newwin(screen.status_window.rows,
587                                          screen.status_window.cols,
588                                          screen.rows-1,
589                                          0);
591         leaveok(screen.status_window.w, FALSE);
592         keypad(screen.status_window.w, TRUE);
594         if (options.enable_colors) {
595                 /* set background attributes */
596                 wbkgd(stdscr, COLOR_PAIR(COLOR_LIST));
597                 wbkgd(screen.main_window.w,     COLOR_PAIR(COLOR_LIST));
598                 wbkgd(screen.top_window.w,      COLOR_PAIR(COLOR_TITLE));
599                 wbkgd(screen.progress_window.w, COLOR_PAIR(COLOR_PROGRESSBAR));
600                 wbkgd(screen.status_window.w,   COLOR_PAIR(COLOR_STATUS));
601                 colors_use(screen.progress_window.w, COLOR_PROGRESSBAR);
602         }
604         refresh();
606         /* initialize screens */
607         for (i = 0; i < NUM_SCREENS; ++i) {
608                 const struct screen_functions *fn = screens[i].functions;
610                 if (fn->init)
611                         fn->init(screen.main_window.w,
612                                  screen.main_window.cols,
613                                  screen.main_window.rows);
614         }
616         if (mode_fn->open != NULL)
617                 mode_fn->open(&screen, c);
619         /* initialize wreadln */
620         wrln_wgetch = my_wgetch;
621         wrln_max_history_length = 16;
624 void
625 screen_paint(mpdclient_t *c)
627         const char *title = NULL;
629         if (mode_fn->get_title != NULL)
630                 title = mode_fn->get_title(screen.buf, screen.buf_size);
632         D("screen_paint(%s)\n", title);
633         /* paint the title/header window */
634         if( title )
635                 paint_top_window(title, c, 1);
636         else
637                 paint_top_window("", c, 1);
639         /* paint the main window */
640         wclear(screen.main_window.w);
641         if (mode_fn->paint != NULL)
642                 mode_fn->paint(c);
644         paint_progress_window(c);
645         paint_status_window(c);
646         screen.painted = 1;
647         wmove(screen.main_window.w, 0, 0);
648         wnoutrefresh(screen.main_window.w);
650         /* tell curses to update */
651         doupdate();
654 void
655 screen_update(mpdclient_t *c)
657         static int repeat = -1;
658         static int random_enabled = -1;
659         static int crossfade = -1;
660         static int dbupdate = -1;
662         if( !screen.painted )
663                 return screen_paint(c);
665         /* print a message if mpd status has changed */
666         if (c->status != NULL) {
667                 if (repeat < 0) {
668                         repeat = c->status->repeat;
669                         random_enabled = c->status->random;
670                         crossfade = c->status->crossfade;
671                         dbupdate = c->status->updatingDb;
672                 }
674                 if (repeat != c->status->repeat)
675                         screen_status_printf(c->status->repeat ?
676                                              _("Repeat is on") :
677                                              _("Repeat is off"));
679                 if (random_enabled != c->status->random)
680                         screen_status_printf(c->status->random ?
681                                              _("Random is on") :
682                                              _("Random is off"));
684                 if (crossfade != c->status->crossfade)
685                         screen_status_printf(_("Crossfade %d seconds"), c->status->crossfade);
687                 if (dbupdate && dbupdate != c->status->updatingDb) {
688                         screen_status_printf(_("Database updated!"));
689                         mpdclient_browse_callback(c, BROWSE_DB_UPDATED, NULL);
690                 }
692                 repeat = c->status->repeat;
693                 random_enabled = c->status->random;
694                 crossfade = c->status->crossfade;
695                 dbupdate = c->status->updatingDb;
696         }
698         /* update title/header window */
699         if (welcome && options.welcome_screen_list &&
700             screen.last_cmd==CMD_NONE &&
701             time(NULL)-screen.start_timestamp <= SCREEN_WELCOME_TIME)
702                 paint_top_window("", c, 0);
703         else if (mode_fn->get_title != NULL) {
704                 paint_top_window(mode_fn->get_title(screen.buf,screen.buf_size), c, 0);
705                 welcome = FALSE;
706         } else
707                 paint_top_window("", c, 0);
709         /* update the main window */
710         if (mode_fn->update != NULL)
711                 mode_fn->update(c);
713         /* update progress window */
714         paint_progress_window(c);
716         /* update status window */
717         paint_status_window(c);
719         /* move the cursor to the origin */
720         wmove(screen.main_window.w, 0, 0);
721         wnoutrefresh(screen.main_window.w);
723         /* tell curses to update */
724         doupdate();
727 void
728 screen_idle(mpdclient_t *c)
730         if (c->song && seek_id == c->song->id &&
731             (screen.last_cmd == CMD_SEEK_FORWARD ||
732              screen.last_cmd == CMD_SEEK_BACKWARD))
733                 mpdclient_cmd_seek(c, seek_id, seek_target_time);
735         screen.last_cmd = CMD_NONE;
736         seek_id = -1;
739 #ifdef HAVE_GETMOUSE
740 int
741 screen_get_mouse_event(mpdclient_t *c, unsigned long *bstate, int *row)
743         MEVENT event;
745         /* retreive the mouse event from ncurses */
746         getmouse(&event);
747         D("mouse: id=%d  y=%d,x=%d,z=%d\n",event.id,event.y,event.x,event.z);
748         /* calculate the selected row in the list window */
749         *row = event.y - screen.top_window.rows;
750         /* copy button state bits */
751         *bstate = event.bstate;
752         /* if button 2 was pressed switch screen */
753         if (event.bstate & BUTTON2_CLICKED) {
754                 screen_cmd(c, CMD_SCREEN_NEXT);
755                 return 1;
756         }
758         return 0;
760 #endif
762 static int
763 screen_client_cmd(mpdclient_t *c, command_t cmd)
765         if (c->connection == NULL || c->status == NULL)
766                 return 0;
768         switch(cmd) {
769                 /*
770         case CMD_PLAY:
771                 mpdclient_cmd_play(c, MPD_PLAY_AT_BEGINNING);
772                 break;
773                 */
774         case CMD_PAUSE:
775                 mpdclient_cmd_pause(c, !IS_PAUSED(c->status->state));
776                 break;
777         case CMD_STOP:
778                 mpdclient_cmd_stop(c);
779                 break;
780         case CMD_CROP:
781                 mpdclient_cmd_crop(c);
782                 break;
783         case CMD_SEEK_FORWARD:
784                 if (!IS_STOPPED(c->status->state)) {
785                         if (c->song && seek_id != c->song->id) {
786                                 seek_id = c->song->id;
787                                 seek_target_time = c->status->elapsedTime;
788                         }
789                         seek_target_time+=options.seek_time;
790                         if (seek_target_time < c->status->totalTime)
791                                 break;
792                         seek_target_time = c->status->totalTime;
793                         /* seek_target_time=0; */
794                 }
795                 break;
796                 /* fall through... */
797         case CMD_TRACK_NEXT:
798                 if (!IS_STOPPED(c->status->state))
799                         mpdclient_cmd_next(c);
800                 break;
801         case CMD_SEEK_BACKWARD:
802                 if (!IS_STOPPED(c->status->state)) {
803                         if (seek_id != c->song->id) {
804                                 seek_id = c->song->id;
805                                 seek_target_time = c->status->elapsedTime;
806                         }
807                         seek_target_time-=options.seek_time;
808                         if (seek_target_time < 0)
809                                 seek_target_time=0;
810                 }
811                 break;
812         case CMD_TRACK_PREVIOUS:
813                 if (!IS_STOPPED(c->status->state))
814                         mpdclient_cmd_prev(c);
815                 break;
816         case CMD_SHUFFLE:
817                 if (mpdclient_cmd_shuffle(c) == 0)
818                         screen_status_message(_("Shuffled playlist!"));
819                 break;
820         case CMD_CLEAR:
821                 if (mpdclient_cmd_clear(c) == 0)
822                         screen_status_message(_("Cleared playlist!"));
823                 break;
824         case CMD_REPEAT:
825                 mpdclient_cmd_repeat(c, !c->status->repeat);
826                 break;
827         case CMD_RANDOM:
828                 mpdclient_cmd_random(c, !c->status->random);
829                 break;
830         case CMD_CROSSFADE:
831                 if (c->status->crossfade)
832                         mpdclient_cmd_crossfade(c, 0);
833                 else
834                         mpdclient_cmd_crossfade(c, options.crossfade_time);
835                 break;
836         case CMD_DB_UPDATE:
837                 if (!c->status->updatingDb) {
838                         if( mpdclient_cmd_db_update_utf8(c,NULL)==0 )
839                                 screen_status_printf(_("Database update started!"));
840                 } else
841                         screen_status_printf(_("Database update running..."));
842                 break;
843         case CMD_VOLUME_UP:
844                 if( c->status->volume!=MPD_STATUS_NO_VOLUME && c->status->volume<100 )
845                         mpdclient_cmd_volume(c, ++c->status->volume);
846                 break;
847         case CMD_VOLUME_DOWN:
848                 if( c->status->volume!=MPD_STATUS_NO_VOLUME && c->status->volume>0 )
849                         mpdclient_cmd_volume(c, --c->status->volume);
850                 break;
852         default:
853                 return 0;
854         }
856         return 1;
859 void
860 screen_cmd(mpdclient_t *c, command_t cmd)
862         screen.last_cmd = cmd;
863         welcome = FALSE;
865         if (mode_fn->cmd != NULL && mode_fn->cmd(&screen, c, cmd))
866                 return;
868         if (screen_client_cmd(c, cmd))
869                 return;
871         switch(cmd) {
872         case CMD_TOGGLE_FIND_WRAP:
873                 options.find_wrap = !options.find_wrap;
874                 screen_status_printf(options.find_wrap ?
875                                      _("Find mode: Wrapped") :
876                                      _("Find mode: Normal"));
877                 break;
878         case CMD_TOGGLE_AUTOCENTER:
879                 options.auto_center = !options.auto_center;
880                 screen_status_printf(options.auto_center ?
881                                      _("Auto center mode: On") :
882                                      _("Auto center mode: Off"));
883                 break;
884         case CMD_SCREEN_UPDATE:
885                 screen.painted = 0;
886                 break;
887         case CMD_SCREEN_PREVIOUS:
888                 screen_next_mode(c, -1);
889                 break;
890         case CMD_SCREEN_NEXT:
891                 screen_next_mode(c, 1);
892                 break;
893         case CMD_SCREEN_PLAY:
894                 switch_screen_mode(SCREEN_PLAYLIST_ID, c);
895                 break;
896         case CMD_SCREEN_FILE:
897                 switch_screen_mode(SCREEN_BROWSE_ID, c);
898                 break;
899         case CMD_SCREEN_HELP:
900                 switch_screen_mode(SCREEN_HELP_ID, c);
901                 break;
902         case CMD_SCREEN_SEARCH:
903                 switch_screen_mode(SCREEN_SEARCH_ID, c);
904                 break;
905         case CMD_SCREEN_ARTIST:
906                 switch_screen_mode(SCREEN_ARTIST_ID, c);
907                 break;
908         case CMD_SCREEN_KEYDEF:
909                 switch_screen_mode(SCREEN_KEYDEF_ID, c);
910                 break;
911         case CMD_SCREEN_LYRICS:
912                 switch_screen_mode(SCREEN_LYRICS_ID, c);
913                 break;
914         default:
915                 break;
916         }