Code

screen: don't pass screen pointer to method update()
[ncmpc.git] / src / screen.c
1 /*
2  * $Id$
3  *
4  * (c) 2004 by Kalle Wallin <kaw@linux.se>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  */
21 #include "screen.h"
22 #include "screen_utils.h"
23 #include "config.h"
24 #include "ncmpc.h"
25 #include "support.h"
26 #include "mpdclient.h"
27 #include "utils.h"
28 #include "command.h"
29 #include "options.h"
30 #include "colors.h"
31 #include "strfsong.h"
32 #include "wreadln.h"
34 #include <stdlib.h>
35 #include <unistd.h>
36 #include <stdarg.h>
37 #include <string.h>
38 #include <time.h>
39 #include <locale.h>
41 #define SCREEN_PLAYLIST_ID     0
42 #define SCREEN_BROWSE_ID       1
43 #define SCREEN_ARTIST_ID       2
44 #define SCREEN_HELP_ID         100
45 #define SCREEN_KEYDEF_ID       101
46 #define SCREEN_SEARCH_ID       103
47 #define SCREEN_LYRICS_ID           104
51 /* screens */
52 extern const struct screen_functions screen_playlist;
53 extern const struct screen_functions screen_browse;
54 #ifdef ENABLE_ARTIST_SCREEN
55 extern const struct screen_functions screen_artist;
56 #endif
57 extern const struct screen_functions screen_help;
58 #ifdef ENABLE_SEARCH_SCREEN
59 extern const struct screen_functions screen_search;
60 #endif
61 #ifdef ENABLE_KEYDEF_SCREEN
62 extern const struct screen_functions screen_keydef;
63 #endif
64 extern const struct screen_functions screen_lyrics;
66 typedef struct screen_functions * (*screen_get_mode_functions_fn_t) (void);
68 static const struct
69 {
70         gint id;
71         const gchar *name;
72         const struct screen_functions *functions;
73 } screens[] = {
74         { SCREEN_PLAYLIST_ID, "playlist", &screen_playlist },
75         { SCREEN_BROWSE_ID, "browse", &screen_browse },
76 #ifdef ENABLE_ARTIST_SCREEN
77         { SCREEN_ARTIST_ID, "artist", &screen_artist },
78 #endif
79         { SCREEN_HELP_ID, "help", &screen_help },
80 #ifdef ENABLE_SEARCH_SCREEN
81         { SCREEN_SEARCH_ID, "search", &screen_search },
82 #endif
83 #ifdef ENABLE_KEYDEF_SCREEN
84         { SCREEN_KEYDEF_ID, "keydef", &screen_keydef },
85 #endif
86 #ifdef ENABLE_LYRICS_SCREEN
87         { SCREEN_LYRICS_ID, "lyrics", &screen_lyrics },
88 #endif
89 };
91 #define NUM_SCREENS (sizeof(screens) / sizeof(screens[0]))
93 static gboolean welcome = TRUE;
94 static struct screen screen;
95 static const struct screen_functions *mode_fn = &screen_playlist;
96 static int seek_id = -1;
97 static int seek_target_time = 0;
99 gint
100 screen_get_id(const char *name)
102         guint i;
104         for (i = 0; i < NUM_SCREENS; ++i)
105                 if (strcmp(name, screens[i].name) == 0)
106                         return screens[i].id;
108         return -1;
111 static gint
112 lookup_mode(gint id)
114         guint i;
116         for (i = 0; i < NUM_SCREENS; ++i)
117                 if (screens[i].id == id)
118                         return i;
120         return -1;
123 gint get_cur_mode_id(void)
125         return screens[screen.mode].id;
128 static void
129 switch_screen_mode(gint id, mpdclient_t *c)
131         gint new_mode;
133         if( id == screens[screen.mode].id )
134                 return;
136         new_mode = lookup_mode(id);
137         if (new_mode < 0)
138                 return;
140         /* close the old mode */
141         if (mode_fn->close != NULL)
142                 mode_fn->close();
144         /* get functions for the new mode */
145         D("switch_screen(%s)\n", screens[new_mode].name );
146         mode_fn = screens[new_mode].functions;
147         screen.mode = new_mode;
148         screen.painted = 0;
150         /* open the new mode */
151         if (mode_fn->open != NULL)
152                 mode_fn->open(&screen, c);
155 static int
156 find_configured_screen(const char *name)
158         unsigned i;
160         for (i = 0; options.screen_list[i] != NULL; ++i)
161                 if (strcmp(options.screen_list[i], name) == 0)
162                         return i;
164         return -1;
167 static void
168 screen_next_mode(mpdclient_t *c, int offset)
170         int max = g_strv_length(options.screen_list);
171         int current, next;
173         /* find current screen */
174         current = find_configured_screen(screens[screen.mode].name);
175         next = current + offset;
176         if (next<0)
177                 next = max-1;
178         else if (next>=max)
179                 next = 0;
181         D("current mode: %d:%d    next:%d\n", current, max, next);
182         switch_screen_mode(screen_get_id(options.screen_list[next]), c);
185 static void
186 paint_top_window2(const char *header, mpdclient_t *c)
188         char flags[5];
189         WINDOW *w = screen.top_window.w;
190         char buf[32];
192         if (header[0]) {
193                 colors_use(w, COLOR_TITLE_BOLD);
194                 mvwaddstr(w, 0, 0, header);
195         } else {
196                 colors_use(w, COLOR_TITLE_BOLD);
197                 waddstr(w, get_key_names(CMD_SCREEN_HELP, FALSE));
198                 colors_use(w, COLOR_TITLE);
199                 waddstr(w, _(":Help  "));
200                 colors_use(w, COLOR_TITLE_BOLD);
201                 waddstr(w, get_key_names(CMD_SCREEN_PLAY, FALSE));
202                 colors_use(w, COLOR_TITLE);
203                 waddstr(w, _(":Playlist  "));
204                 colors_use(w, COLOR_TITLE_BOLD);
205                 waddstr(w, get_key_names(CMD_SCREEN_FILE, FALSE));
206                 colors_use(w, COLOR_TITLE);
207                 waddstr(w, _(":Browse  "));
208 #ifdef ENABLE_ARTIST_SCREEN
209                 colors_use(w, COLOR_TITLE_BOLD);
210                 waddstr(w, get_key_names(CMD_SCREEN_ARTIST, FALSE));
211                 colors_use(w, COLOR_TITLE);
212                 waddstr(w, _(":Artist  "));
213 #endif
214 #ifdef ENABLE_SEARCH_SCREEN
215                 colors_use(w, COLOR_TITLE_BOLD);
216                 waddstr(w, get_key_names(CMD_SCREEN_SEARCH, FALSE));
217                 colors_use(w, COLOR_TITLE);
218                 waddstr(w, _(":Search  "));
219 #endif
220 #ifdef ENABLE_LYRICS_SCREEN
221                 colors_use(w, COLOR_TITLE_BOLD);
222                 waddstr(w, get_key_names(CMD_SCREEN_LYRICS, FALSE));
223                 colors_use(w, COLOR_TITLE);
224                 waddstr(w, _(":Lyrics  "));
225 #endif
226         }
227         if (c->status == NULL || c->status->volume == MPD_STATUS_NO_VOLUME) {
228                 g_snprintf(buf, 32, _("Volume n/a "));
229         } else {
230                 g_snprintf(buf, 32, _(" Volume %d%%"), c->status->volume);
231         }
232         colors_use(w, COLOR_TITLE);
233         mvwaddstr(w, 0, screen.top_window.cols-my_strlen(buf), buf);
235         flags[0] = 0;
236         if (c->status != NULL) {
237                 if (c->status->repeat)
238                         g_strlcat(flags, "r", sizeof(flags));
239                 if (c->status->random)
240                         g_strlcat(flags, "z", sizeof(flags));;
241                 if (c->status->crossfade)
242                         g_strlcat(flags, "x", sizeof(flags));
243                 if (c->status->updatingDb)
244                         g_strlcat(flags, "U", sizeof(flags));
245         }
247         colors_use(w, COLOR_LINE);
248         mvwhline(w, 1, 0, ACS_HLINE, screen.top_window.cols);
249         if (flags[0]) {
250                 wmove(w,1,screen.top_window.cols-strlen(flags)-3);
251                 waddch(w, '[');
252                 colors_use(w, COLOR_LINE_BOLD);
253                 waddstr(w, flags);
254                 colors_use(w, COLOR_LINE);
255                 waddch(w, ']');
256         }
257         wnoutrefresh(w);
260 static void
261 paint_top_window(const char *header, mpdclient_t *c, int full_repaint)
263         static int prev_volume = -1;
264         static size_t prev_header_len = -1;
265         WINDOW *w = screen.top_window.w;
267         if (prev_header_len!=my_strlen(header)) {
268                 prev_header_len = my_strlen(header);
269                 full_repaint = 1;
270         }
272         if (full_repaint) {
273                 wmove(w, 0, 0);
274                 wclrtoeol(w);
275         }
277         if ((c->status != NULL && prev_volume != c->status->volume) ||
278             full_repaint)
279                 paint_top_window2(header, c);
282 static void
283 paint_progress_window(mpdclient_t *c)
285         double p;
286         int width;
287         int elapsedTime;
289         if (c->status==NULL || IS_STOPPED(c->status->state)) {
290                 mvwhline(screen.progress_window.w, 0, 0, ACS_HLINE,
291                          screen.progress_window.cols);
292                 wnoutrefresh(screen.progress_window.w);
293                 return;
294         }
296         if (c->song && seek_id == c->song->id)
297                 elapsedTime = seek_target_time;
298         else
299                 elapsedTime = c->status->elapsedTime;
301         p = ((double) elapsedTime) / ((double) c->status->totalTime);
303         width = (int) (p * (double) screen.progress_window.cols);
304         mvwhline(screen.progress_window.w,
305                  0, 0,
306                  ACS_HLINE,
307                  screen.progress_window.cols);
308         whline(screen.progress_window.w, '=', width-1);
309         mvwaddch(screen.progress_window.w, 0, width-1, 'O');
310         wnoutrefresh(screen.progress_window.w);
313 static void
314 paint_status_window(mpdclient_t *c)
316         WINDOW *w = screen.status_window.w;
317         mpd_Status *status = c->status;
318         mpd_Song *song = c->song;
319         int elapsedTime = 0;
320         char bitrate[16];
321         const char *str = NULL;
322         int x = 0;
324         if( time(NULL) - screen.status_timestamp <= SCREEN_STATUS_MESSAGE_TIME )
325                 return;
327         wmove(w, 0, 0);
328         wclrtoeol(w);
329         colors_use(w, COLOR_STATUS_BOLD);
331         switch (status == NULL ? MPD_STATUS_STATE_STOP : status->state) {
332         case MPD_STATUS_STATE_PLAY:
333                 str = _("Playing:");
334                 break;
335         case MPD_STATUS_STATE_PAUSE:
336                 str = _("[Paused]");
337                 break;
338         case MPD_STATUS_STATE_STOP:
339         default:
340                 break;
341         }
343         if (str) {
344                 waddstr(w, str);
345                 x += my_strlen(str)+1;
346         }
348         /* create time string */
349         memset(screen.buf, 0, screen.buf_size);
350         if (status != NULL && (IS_PLAYING(status->state) ||
351                                IS_PAUSED(status->state))) {
352                 if (status->totalTime > 0) {
353                         /*checks the conf to see whether to display elapsed or remaining time */
354                         if(!strcmp(options.timedisplay_type,"elapsed"))
355                                 elapsedTime = c->status->elapsedTime;
356                         else if(!strcmp(options.timedisplay_type,"remaining"))
357                                 elapsedTime = (c->status->totalTime - c->status->elapsedTime);
359                         if( c->song && seek_id == c->song->id )
360                                 elapsedTime = seek_target_time;
362                         /* display bitrate if visible-bitrate is true */
363                         if (options.visible_bitrate) {
364                                 g_snprintf(bitrate, 16,
365                                            " [%d kbps]", status->bitRate);
366                         } else {
367                                 bitrate[0] = '\0';
368                         }
370                         /*write out the time, using hours if time over 60 minutes*/
371                         if (c->status->totalTime > 3600) {
372                                 g_snprintf(screen.buf, screen.buf_size,
373                                            "%s [%i:%02i:%02i/%i:%02i:%02i]",
374                                            bitrate, elapsedTime/3600, (elapsedTime%3600)/60, elapsedTime%60,
375                                            status->totalTime/3600, (status->totalTime%3600)/60,  status->totalTime%60);
376                         } else {
377                                 g_snprintf(screen.buf, screen.buf_size,
378                                            "%s [%i:%02i/%i:%02i]",
379                                            bitrate, elapsedTime/60, elapsedTime%60,
380                                            status->totalTime/60,   status->totalTime%60 );
381                         }
382                 } else {
383                         g_snprintf(screen.buf, screen.buf_size,
384                                    " [%d kbps]", status->bitRate );
385                 }
386         } else {
387                 time_t timep;
389                 time(&timep);
390                 strftime(screen.buf, screen.buf_size, "%X ",localtime(&timep));
391         }
393         /* display song */
394         if (status != NULL && (IS_PLAYING(status->state) ||
395                                IS_PAUSED(status->state))) {
396                 char songname[MAX_SONGNAME_LENGTH];
397                 int width = COLS-x-my_strlen(screen.buf);
399                 if (song)
400                         strfsong(songname, MAX_SONGNAME_LENGTH, STATUS_FORMAT, song);
401                 else
402                         songname[0] = '\0';
404                 colors_use(w, COLOR_STATUS);
405                 /* scroll if the song name is to long */
406                 if (options.scroll && my_strlen(songname) > (size_t)width) {
407                         static  scroll_state_t st = { 0, 0 };
408                         char *tmp = strscroll(songname, options.scroll_sep, width, &st);
410                         g_strlcpy(songname, tmp, MAX_SONGNAME_LENGTH);
411                         g_free(tmp);
412                 }
413                 //mvwaddnstr(w, 0, x, songname, width);
414                 mvwaddstr(w, 0, x, songname);
415         }
417         /* display time string */
418         if (screen.buf[0]) {
419                 x = screen.status_window.cols - strlen(screen.buf);
420                 colors_use(w, COLOR_STATUS_TIME);
421                 mvwaddstr(w, 0, x, screen.buf);
422         }
424         wnoutrefresh(w);
427 void
428 screen_exit(void)
430         guint i;
432         if (mode_fn->close != NULL)
433                 mode_fn->close();
435         /* close and exit all screens (playlist,browse,help...) */
436         for (i = 0; i < NUM_SCREENS; ++i) {
437                 const struct screen_functions *sf = screens[i].functions;
439                 if (sf->exit)
440                         sf->exit();
441         }
443         string_list_free(screen.find_history);
444         g_free(screen.buf);
445         g_free(screen.findbuf);
448 void
449 screen_resize(void)
451         guint i;
453         D("Resize rows %d->%d, cols %d->%d\n",screen.rows,LINES,screen.cols,COLS);
454         if (COLS<SCREEN_MIN_COLS || LINES<SCREEN_MIN_ROWS) {
455                 screen_exit();
456                 fprintf(stderr, _("Error: Screen to small!\n"));
457                 exit(EXIT_FAILURE);
458         }
460         resizeterm(LINES, COLS);
462         screen.cols = COLS;
463         screen.rows = LINES;
465         /* top window */
466         screen.top_window.cols = screen.cols;
467         wresize(screen.top_window.w, 2, screen.cols);
469         /* main window */
470         screen.main_window.cols = screen.cols;
471         screen.main_window.rows = screen.rows-4;
472         wresize(screen.main_window.w, screen.main_window.rows, screen.cols);
473         wclear(screen.main_window.w);
475         /* progress window */
476         screen.progress_window.cols = screen.cols;
477         wresize(screen.progress_window.w, 1, screen.cols);
478         mvwin(screen.progress_window.w, screen.rows-2, 0);
480         /* status window */
481         screen.status_window.cols = screen.cols;
482         wresize(screen.status_window.w, 1, screen.cols);
483         mvwin(screen.status_window.w, screen.rows-1, 0);
485         screen.buf_size = screen.cols;
486         g_free(screen.buf);
487         screen.buf = g_malloc(screen.cols);
489         /* close and exit all screens (playlist,browse,help...) */
490         for (i = 0; i < NUM_SCREENS; ++i) {
491                 const struct screen_functions *sf = screens[i].functions;
493                 if (sf->resize)
494                         sf->resize(screen.main_window.cols, screen.main_window.rows);
495         }
498         /* ? - without this the cursor becomes visible with aterm & Eterm */
499         curs_set(1);
500         curs_set(0);
502         screen.painted = 0;
505 void
506 screen_status_message(const char *msg)
508         WINDOW *w = screen.status_window.w;
510         wmove(w, 0, 0);
511         wclrtoeol(w);
512         colors_use(w, COLOR_STATUS_ALERT);
513         waddstr(w, msg);
514         wnoutrefresh(w);
515         screen.status_timestamp = time(NULL);
518 void
519 screen_status_printf(const char *format, ...)
521         char *msg;
522         va_list ap;
524         va_start(ap,format);
525         msg = g_strdup_vprintf(format,ap);
526         va_end(ap);
527         screen_status_message(msg);
528         g_free(msg);
531 void
532 screen_init(mpdclient_t *c)
534         guint i;
536         if (COLS < SCREEN_MIN_COLS || LINES < SCREEN_MIN_ROWS) {
537                 fprintf(stderr, _("Error: Screen to small!\n"));
538                 exit(EXIT_FAILURE);
539         }
541         screen.mode = 0;
542         screen.cols = COLS;
543         screen.rows = LINES;
545         screen.buf  = g_malloc(screen.cols);
546         screen.buf_size = screen.cols;
547         screen.findbuf = NULL;
548         screen.painted = 0;
549         screen.start_timestamp = time(NULL);
550         screen.last_cmd = CMD_NONE;
552         /* create top window */
553         screen.top_window.rows = 2;
554         screen.top_window.cols = screen.cols;
555         screen.top_window.w = newwin(screen.top_window.rows,
556                                       screen.top_window.cols,
557                                       0, 0);
558         leaveok(screen.top_window.w, TRUE);
559         keypad(screen.top_window.w, TRUE);
561         /* create main window */
562         screen.main_window.rows = screen.rows-4;
563         screen.main_window.cols = screen.cols;
564         screen.main_window.w = newwin(screen.main_window.rows,
565                                        screen.main_window.cols,
566                                        2,
567                                        0);
569         //  leaveok(screen.main_window.w, TRUE); temporary disabled
570         keypad(screen.main_window.w, TRUE);
572         /* create progress window */
573         screen.progress_window.rows = 1;
574         screen.progress_window.cols = screen.cols;
575         screen.progress_window.w = newwin(screen.progress_window.rows,
576                                            screen.progress_window.cols,
577                                            screen.rows-2,
578                                            0);
579         leaveok(screen.progress_window.w, TRUE);
581         /* create status window */
582         screen.status_window.rows = 1;
583         screen.status_window.cols = screen.cols;
584         screen.status_window.w = newwin(screen.status_window.rows,
585                                          screen.status_window.cols,
586                                          screen.rows-1,
587                                          0);
589         leaveok(screen.status_window.w, FALSE);
590         keypad(screen.status_window.w, TRUE);
592         if (options.enable_colors) {
593                 /* set background attributes */
594                 wbkgd(stdscr, COLOR_PAIR(COLOR_LIST));
595                 wbkgd(screen.main_window.w,     COLOR_PAIR(COLOR_LIST));
596                 wbkgd(screen.top_window.w,      COLOR_PAIR(COLOR_TITLE));
597                 wbkgd(screen.progress_window.w, COLOR_PAIR(COLOR_PROGRESSBAR));
598                 wbkgd(screen.status_window.w,   COLOR_PAIR(COLOR_STATUS));
599                 colors_use(screen.progress_window.w, COLOR_PROGRESSBAR);
600         }
602         refresh();
604         /* initialize screens */
605         for (i = 0; i < NUM_SCREENS; ++i) {
606                 const struct screen_functions *fn = screens[i].functions;
608                 if (fn->init)
609                         fn->init(screen.main_window.w,
610                                  screen.main_window.cols,
611                                  screen.main_window.rows);
612         }
614         if (mode_fn->open != NULL)
615                 mode_fn->open(&screen, c);
617         /* initialize wreadln */
618         wrln_wgetch = my_wgetch;
619         wrln_max_history_length = 16;
622 void
623 screen_paint(mpdclient_t *c)
625         const char *title = NULL;
627         if (mode_fn->get_title != NULL)
628                 title = mode_fn->get_title(screen.buf, screen.buf_size);
630         D("screen_paint(%s)\n", title);
631         /* paint the title/header window */
632         if( title )
633                 paint_top_window(title, c, 1);
634         else
635                 paint_top_window("", c, 1);
637         /* paint the main window */
638         wclear(screen.main_window.w);
639         if (mode_fn->paint != NULL)
640                 mode_fn->paint(c);
642         paint_progress_window(c);
643         paint_status_window(c);
644         screen.painted = 1;
645         wmove(screen.main_window.w, 0, 0);
646         wnoutrefresh(screen.main_window.w);
648         /* tell curses to update */
649         doupdate();
652 void
653 screen_update(mpdclient_t *c)
655         static int repeat = -1;
656         static int random_enabled = -1;
657         static int crossfade = -1;
658         static int dbupdate = -1;
660         if( !screen.painted )
661                 return screen_paint(c);
663         /* print a message if mpd status has changed */
664         if (c->status != NULL) {
665                 if (repeat < 0) {
666                         repeat = c->status->repeat;
667                         random_enabled = c->status->random;
668                         crossfade = c->status->crossfade;
669                         dbupdate = c->status->updatingDb;
670                 }
672                 if (repeat != c->status->repeat)
673                         screen_status_printf(c->status->repeat ?
674                                              _("Repeat is on") :
675                                              _("Repeat is off"));
677                 if (random_enabled != c->status->random)
678                         screen_status_printf(c->status->random ?
679                                              _("Random is on") :
680                                              _("Random is off"));
682                 if (crossfade != c->status->crossfade)
683                         screen_status_printf(_("Crossfade %d seconds"), c->status->crossfade);
685                 if (dbupdate && dbupdate != c->status->updatingDb) {
686                         screen_status_printf(_("Database updated!"));
687                         mpdclient_browse_callback(c, BROWSE_DB_UPDATED, NULL);
688                 }
690                 repeat = c->status->repeat;
691                 random_enabled = c->status->random;
692                 crossfade = c->status->crossfade;
693                 dbupdate = c->status->updatingDb;
694         }
696         /* update title/header window */
697         if (welcome && options.welcome_screen_list &&
698             screen.last_cmd==CMD_NONE &&
699             time(NULL)-screen.start_timestamp <= SCREEN_WELCOME_TIME)
700                 paint_top_window("", c, 0);
701         else if (mode_fn->get_title != NULL) {
702                 paint_top_window(mode_fn->get_title(screen.buf,screen.buf_size), c, 0);
703                 welcome = FALSE;
704         } else
705                 paint_top_window("", c, 0);
707         /* update the main window */
708         if (mode_fn->update != NULL)
709                 mode_fn->update(c);
711         /* update progress window */
712         paint_progress_window(c);
714         /* update status window */
715         paint_status_window(c);
717         /* move the cursor to the origin */
718         wmove(screen.main_window.w, 0, 0);
719         wnoutrefresh(screen.main_window.w);
721         /* tell curses to update */
722         doupdate();
725 void
726 screen_idle(mpdclient_t *c)
728         if (c->song && seek_id == c->song->id &&
729             (screen.last_cmd == CMD_SEEK_FORWARD ||
730              screen.last_cmd == CMD_SEEK_BACKWARD))
731                 mpdclient_cmd_seek(c, seek_id, seek_target_time);
733         screen.last_cmd = CMD_NONE;
734         seek_id = -1;
737 #ifdef HAVE_GETMOUSE
738 int
739 screen_get_mouse_event(mpdclient_t *c, unsigned long *bstate, int *row)
741         MEVENT event;
743         /* retreive the mouse event from ncurses */
744         getmouse(&event);
745         D("mouse: id=%d  y=%d,x=%d,z=%d\n",event.id,event.y,event.x,event.z);
746         /* calculate the selected row in the list window */
747         *row = event.y - screen.top_window.rows;
748         /* copy button state bits */
749         *bstate = event.bstate;
750         /* if button 2 was pressed switch screen */
751         if (event.bstate & BUTTON2_CLICKED) {
752                 screen_cmd(c, CMD_SCREEN_NEXT);
753                 return 1;
754         }
756         return 0;
758 #endif
760 static int
761 screen_client_cmd(mpdclient_t *c, command_t cmd)
763         if (c->connection == NULL || c->status == NULL)
764                 return 0;
766         switch(cmd) {
767                 /*
768         case CMD_PLAY:
769                 mpdclient_cmd_play(c, MPD_PLAY_AT_BEGINNING);
770                 break;
771                 */
772         case CMD_PAUSE:
773                 mpdclient_cmd_pause(c, !IS_PAUSED(c->status->state));
774                 break;
775         case CMD_STOP:
776                 mpdclient_cmd_stop(c);
777                 break;
778         case CMD_CROP:
779                 mpdclient_cmd_crop(c);
780                 break;
781         case CMD_SEEK_FORWARD:
782                 if (!IS_STOPPED(c->status->state)) {
783                         if (c->song && seek_id != c->song->id) {
784                                 seek_id = c->song->id;
785                                 seek_target_time = c->status->elapsedTime;
786                         }
787                         seek_target_time+=options.seek_time;
788                         if (seek_target_time < c->status->totalTime)
789                                 break;
790                         seek_target_time = c->status->totalTime;
791                         /* seek_target_time=0; */
792                 }
793                 break;
794                 /* fall through... */
795         case CMD_TRACK_NEXT:
796                 if (!IS_STOPPED(c->status->state))
797                         mpdclient_cmd_next(c);
798                 break;
799         case CMD_SEEK_BACKWARD:
800                 if (!IS_STOPPED(c->status->state)) {
801                         if (seek_id != c->song->id) {
802                                 seek_id = c->song->id;
803                                 seek_target_time = c->status->elapsedTime;
804                         }
805                         seek_target_time-=options.seek_time;
806                         if (seek_target_time < 0)
807                                 seek_target_time=0;
808                 }
809                 break;
810         case CMD_TRACK_PREVIOUS:
811                 if (!IS_STOPPED(c->status->state))
812                         mpdclient_cmd_prev(c);
813                 break;
814         case CMD_SHUFFLE:
815                 if (mpdclient_cmd_shuffle(c) == 0)
816                         screen_status_message(_("Shuffled playlist!"));
817                 break;
818         case CMD_CLEAR:
819                 if (mpdclient_cmd_clear(c) == 0)
820                         screen_status_message(_("Cleared playlist!"));
821                 break;
822         case CMD_REPEAT:
823                 mpdclient_cmd_repeat(c, !c->status->repeat);
824                 break;
825         case CMD_RANDOM:
826                 mpdclient_cmd_random(c, !c->status->random);
827                 break;
828         case CMD_CROSSFADE:
829                 if (c->status->crossfade)
830                         mpdclient_cmd_crossfade(c, 0);
831                 else
832                         mpdclient_cmd_crossfade(c, options.crossfade_time);
833                 break;
834         case CMD_DB_UPDATE:
835                 if (!c->status->updatingDb) {
836                         if( mpdclient_cmd_db_update_utf8(c,NULL)==0 )
837                                 screen_status_printf(_("Database update started!"));
838                 } else
839                         screen_status_printf(_("Database update running..."));
840                 break;
841         case CMD_VOLUME_UP:
842                 if( c->status->volume!=MPD_STATUS_NO_VOLUME && c->status->volume<100 )
843                         mpdclient_cmd_volume(c, ++c->status->volume);
844                 break;
845         case CMD_VOLUME_DOWN:
846                 if( c->status->volume!=MPD_STATUS_NO_VOLUME && c->status->volume>0 )
847                         mpdclient_cmd_volume(c, --c->status->volume);
848                 break;
850         default:
851                 return 0;
852         }
854         return 1;
857 void
858 screen_cmd(mpdclient_t *c, command_t cmd)
860         screen.last_cmd = cmd;
861         welcome = FALSE;
863         if (mode_fn->cmd != NULL && mode_fn->cmd(&screen, c, cmd))
864                 return;
866         if (screen_client_cmd(c, cmd))
867                 return;
869         switch(cmd) {
870         case CMD_TOGGLE_FIND_WRAP:
871                 options.find_wrap = !options.find_wrap;
872                 screen_status_printf(options.find_wrap ?
873                                      _("Find mode: Wrapped") :
874                                      _("Find mode: Normal"));
875                 break;
876         case CMD_TOGGLE_AUTOCENTER:
877                 options.auto_center = !options.auto_center;
878                 screen_status_printf(options.auto_center ?
879                                      _("Auto center mode: On") :
880                                      _("Auto center mode: Off"));
881                 break;
882         case CMD_SCREEN_UPDATE:
883                 screen.painted = 0;
884                 break;
885         case CMD_SCREEN_PREVIOUS:
886                 screen_next_mode(c, -1);
887                 break;
888         case CMD_SCREEN_NEXT:
889                 screen_next_mode(c, 1);
890                 break;
891         case CMD_SCREEN_PLAY:
892                 switch_screen_mode(SCREEN_PLAYLIST_ID, c);
893                 break;
894         case CMD_SCREEN_FILE:
895                 switch_screen_mode(SCREEN_BROWSE_ID, c);
896                 break;
897         case CMD_SCREEN_HELP:
898                 switch_screen_mode(SCREEN_HELP_ID, c);
899                 break;
900         case CMD_SCREEN_SEARCH:
901                 switch_screen_mode(SCREEN_SEARCH_ID, c);
902                 break;
903         case CMD_SCREEN_ARTIST:
904                 switch_screen_mode(SCREEN_ARTIST_ID, c);
905                 break;
906         case CMD_SCREEN_KEYDEF:
907                 switch_screen_mode(SCREEN_KEYDEF_ID, c);
908                 break;
909         case CMD_SCREEN_LYRICS:
910                 switch_screen_mode(SCREEN_LYRICS_ID, c);
911                 break;
912         default:
913                 break;
914         }