Code

screen: removed screen_functions.get_lw()
[ncmpc.git] / src / screen.c
1 /*
2  * $Id$
3  *
4  * (c) 2004 by Kalle Wallin <kaw@linux.se>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  */
21 #include "screen.h"
22 #include "screen_utils.h"
23 #include "config.h"
24 #include "ncmpc.h"
25 #include "support.h"
26 #include "mpdclient.h"
27 #include "utils.h"
28 #include "command.h"
29 #include "options.h"
30 #include "colors.h"
31 #include "strfsong.h"
32 #include "wreadln.h"
34 #include <stdlib.h>
35 #include <unistd.h>
36 #include <stdarg.h>
37 #include <string.h>
38 #include <time.h>
39 #include <locale.h>
41 #define SCREEN_PLAYLIST_ID     0
42 #define SCREEN_BROWSE_ID       1
43 #define SCREEN_ARTIST_ID       2
44 #define SCREEN_HELP_ID         100
45 #define SCREEN_KEYDEF_ID       101
46 #define SCREEN_CLOCK_ID        102
47 #define SCREEN_SEARCH_ID       103
48 #define SCREEN_LYRICS_ID           104
52 /* screens */
53 extern const struct screen_functions screen_playlist;
54 extern const struct screen_functions screen_browse;
55 #ifdef ENABLE_ARTIST_SCREEN
56 extern const struct screen_functions screen_artist;
57 #endif
58 extern const struct screen_functions screen_help;
59 #ifdef ENABLE_SEARCH_SCREEN
60 extern const struct screen_functions screen_search;
61 #endif
62 #ifdef ENABLE_KEYDEF_SCREEN
63 extern const struct screen_functions screen_keydef;
64 #endif
65 #ifdef ENABLE_CLOCK_SCREEN
66 extern const struct screen_functions screen_clock;
67 #endif
68 extern const struct screen_functions screen_lyrics;
70 typedef struct screen_functions * (*screen_get_mode_functions_fn_t) (void);
72 static const struct
73 {
74         gint id;
75         const gchar *name;
76         const struct screen_functions *functions;
77 } screens[] = {
78         { SCREEN_PLAYLIST_ID, "playlist", &screen_playlist },
79         { SCREEN_BROWSE_ID, "browse", &screen_browse },
80 #ifdef ENABLE_ARTIST_SCREEN
81         { SCREEN_ARTIST_ID, "artist", &screen_artist },
82 #endif
83         { SCREEN_HELP_ID, "help", &screen_help },
84 #ifdef ENABLE_SEARCH_SCREEN
85         { SCREEN_SEARCH_ID, "search", &screen_search },
86 #endif
87 #ifdef ENABLE_KEYDEF_SCREEN
88         { SCREEN_KEYDEF_ID, "keydef", &screen_keydef },
89 #endif
90 #ifdef ENABLE_CLOCK_SCREEN
91         { SCREEN_CLOCK_ID, "clock", &screen_clock },
92 #endif
93 #ifdef ENABLE_LYRICS_SCREEN
94         { SCREEN_LYRICS_ID, "lyrics", &screen_lyrics },
95 #endif
96         { G_MAXINT, NULL, NULL }
97 };
99 static gboolean welcome = TRUE;
100 static screen_t *screen = NULL;
101 static const struct screen_functions *mode_fn = NULL;
102 static int seek_id = -1;
103 static int seek_target_time = 0;
105 gint
106 screen_get_id(const char *name)
108         gint i=0;
110         while (screens[i].name) {
111                 if (strcmp(name, screens[i].name) == 0)
112                         return screens[i].id;
113                 i++;
114         }
115         return -1;
118 static gint
119 lookup_mode(gint id)
121         gint i=0;
123         while (screens[i].name) {
124                 if (screens[i].id == id)
125                         return i;
126                 i++;
127         }
128         return -1;
131 gint get_cur_mode_id(void)
133         return screens[screen->mode].id;
136 static void
137 switch_screen_mode(gint id, mpdclient_t *c)
139         gint new_mode;
141         if( id == screens[screen->mode].id )
142                 return;
144         /* close the old mode */
145         if( mode_fn && mode_fn->close )
146                 mode_fn->close();
148         /* get functions for the new mode */
149         new_mode = lookup_mode(id);
150         if (new_mode >= 0 && screens[new_mode].functions) {
151                 D("switch_screen(%s)\n", screens[new_mode].name );
152                 mode_fn = screens[new_mode].functions;
153                 screen->mode = new_mode;
154         }
156         screen->painted = 0;
158         /* open the new mode */
159         if (mode_fn && mode_fn->open)
160                 mode_fn->open(screen, c);
163 static void
164 screen_next_mode(mpdclient_t *c, int offset)
166         int max = g_strv_length(options.screen_list);
167         int current, next;
168         int i;
170         /* find current screen */
171         current = -1;
172         i = 0;
173         while (options.screen_list[i]) {
174                 if (strcmp(options.screen_list[i],
175                            screens[screen->mode].name) == 0)
176                         current = i;
177                 i++;
178         }
180         next = current + offset;
181         if (next<0)
182                 next = max-1;
183         else if (next>=max)
184                 next = 0;
186         D("current mode: %d:%d    next:%d\n", current, max, next);
187         switch_screen_mode(screen_get_id(options.screen_list[next]), c);
190 static void
191 paint_top_window2(const char *header, mpdclient_t *c)
193         char flags[5];
194         WINDOW *w = screen->top_window.w;
195         char buf[32];
197         if (header[0]) {
198                 colors_use(w, COLOR_TITLE_BOLD);
199                 mvwaddstr(w, 0, 0, header);
200         } else {
201                 colors_use(w, COLOR_TITLE_BOLD);
202                 waddstr(w, get_key_names(CMD_SCREEN_HELP, FALSE));
203                 colors_use(w, COLOR_TITLE);
204                 waddstr(w, _(":Help  "));
205                 colors_use(w, COLOR_TITLE_BOLD);
206                 waddstr(w, get_key_names(CMD_SCREEN_PLAY, FALSE));
207                 colors_use(w, COLOR_TITLE);
208                 waddstr(w, _(":Playlist  "));
209                 colors_use(w, COLOR_TITLE_BOLD);
210                 waddstr(w, get_key_names(CMD_SCREEN_FILE, FALSE));
211                 colors_use(w, COLOR_TITLE);
212                 waddstr(w, _(":Browse  "));
213 #ifdef ENABLE_ARTIST_SCREEN
214                 colors_use(w, COLOR_TITLE_BOLD);
215                 waddstr(w, get_key_names(CMD_SCREEN_ARTIST, FALSE));
216                 colors_use(w, COLOR_TITLE);
217                 waddstr(w, _(":Artist  "));
218 #endif
219 #ifdef ENABLE_SEARCH_SCREEN
220                 colors_use(w, COLOR_TITLE_BOLD);
221                 waddstr(w, get_key_names(CMD_SCREEN_SEARCH, FALSE));
222                 colors_use(w, COLOR_TITLE);
223                 waddstr(w, _(":Search  "));
224 #endif
225 #ifdef ENABLE_LYRICS_SCREEN
226                 colors_use(w, COLOR_TITLE_BOLD);
227                 waddstr(w, get_key_names(CMD_SCREEN_LYRICS, FALSE));
228                 colors_use(w, COLOR_TITLE);
229                 waddstr(w, _(":Lyrics  "));
230 #endif
231         }
232         if (c->status->volume==MPD_STATUS_NO_VOLUME) {
233                 g_snprintf(buf, 32, _("Volume n/a "));
234         } else {
235                 g_snprintf(buf, 32, _(" Volume %d%%"), c->status->volume);
236         }
237         colors_use(w, COLOR_TITLE);
238         mvwaddstr(w, 0, screen->top_window.cols-my_strlen(buf), buf);
240         flags[0] = 0;
241         if( c->status->repeat )
242                 g_strlcat(flags, "r", sizeof(flags));
243         if( c->status->random )
244                 g_strlcat(flags, "z", sizeof(flags));;
245         if( c->status->crossfade )
246                 g_strlcat(flags, "x", sizeof(flags));
247         if( c->status->updatingDb )
248                 g_strlcat(flags, "U", sizeof(flags));
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 (prev_volume!=c->status->volume || full_repaint)
280                 paint_top_window2(header, c);
283 static void
284 paint_progress_window(mpdclient_t *c)
286         double p;
287         int width;
288         int elapsedTime = c->status->elapsedTime;
290         if (c->status==NULL || IS_STOPPED(c->status->state)) {
291                 mvwhline(screen->progress_window.w, 0, 0, ACS_HLINE,
292                          screen->progress_window.cols);
293                 wnoutrefresh(screen->progress_window.w);
294                 return;
295         }
297         if (c->song && seek_id == c->song->id)
298                 elapsedTime = seek_target_time;
300         p = ((double) elapsedTime) / ((double) c->status->totalTime);
302         width = (int) (p * (double) screen->progress_window.cols);
303         mvwhline(screen->progress_window.w,
304                  0, 0,
305                  ACS_HLINE,
306                  screen->progress_window.cols);
307         whline(screen->progress_window.w, '=', width-1);
308         mvwaddch(screen->progress_window.w, 0, width-1, 'O');
309         wnoutrefresh(screen->progress_window.w);
312 static void
313 paint_status_window(mpdclient_t *c)
315         WINDOW *w = screen->status_window.w;
316         mpd_Status *status = c->status;
317         mpd_Song *song = c->song;
318         int elapsedTime = 0;
319         const char *str = NULL;
320         int x = 0;
322         if( time(NULL) - screen->status_timestamp <= SCREEN_STATUS_MESSAGE_TIME )
323                 return;
325         wmove(w, 0, 0);
326         wclrtoeol(w);
327         colors_use(w, COLOR_STATUS_BOLD);
329         switch(status->state) {
330         case MPD_STATUS_STATE_PLAY:
331                 str = _("Playing:");
332                 break;
333         case MPD_STATUS_STATE_PAUSE:
334                 str = _("[Paused]");
335                 break;
336         case MPD_STATUS_STATE_STOP:
337         default:
338                 break;
339         }
341         if (str) {
342                 waddstr(w, str);
343                 x += my_strlen(str)+1;
344         }
346         /* create time string */
347         memset(screen->buf, 0, screen->buf_size);
348         if (IS_PLAYING(status->state) || IS_PAUSED(status->state)) {
349                 if (status->totalTime > 0) {
350                         /*checks the conf to see whether to display elapsed or remaining time */
351                         if(!strcmp(options.timedisplay_type,"elapsed"))
352                                 elapsedTime = c->status->elapsedTime;
353                         else if(!strcmp(options.timedisplay_type,"remaining"))
354                                 elapsedTime = (c->status->totalTime - c->status->elapsedTime);
356                         if( c->song && seek_id == c->song->id )
357                                 elapsedTime = seek_target_time;
358                         /*write out the time, using hours if time over 60 minutes*/
359                         if (c->status->totalTime > 3600) {
360                                 g_snprintf(screen->buf, screen->buf_size,
361                                            " [%i:%02i:%02i/%i:%02i:%02i]",
362                                            elapsedTime/3600, (elapsedTime%3600)/60, elapsedTime%60,
363                                            status->totalTime/3600, (status->totalTime%3600)/60,  status->totalTime%60);
364                         } else {
365                                 g_snprintf(screen->buf, screen->buf_size,
366                                            " [%i:%02i/%i:%02i]",
367                                            elapsedTime/60, elapsedTime%60,
368                                            status->totalTime/60,   status->totalTime%60 );
369                         }
370                 } else {
371                         g_snprintf(screen->buf, screen->buf_size,
372                                    " [%d kbps]", status->bitRate );
373                 }
374         } else {
375                 time_t timep;
377                 time(&timep);
378                 strftime(screen->buf, screen->buf_size, "%X ",localtime(&timep));
379         }
381         /* display song */
382         if (IS_PLAYING(status->state) || IS_PAUSED(status->state)) {
383                 char songname[MAX_SONGNAME_LENGTH];
384                 int width = COLS-x-my_strlen(screen->buf);
386                 if (song)
387                         strfsong(songname, MAX_SONGNAME_LENGTH, STATUS_FORMAT, song);
388                 else
389                         songname[0] = '\0';
391                 colors_use(w, COLOR_STATUS);
392                 /* scroll if the song name is to long */
393                 if (options.scroll && my_strlen(songname) > (size_t)width) {
394                         static  scroll_state_t st = { 0, 0 };
395                         char *tmp = strscroll(songname, options.scroll_sep, width, &st);
397                         g_strlcpy(songname, tmp, MAX_SONGNAME_LENGTH);
398                         g_free(tmp);
399                 }
400                 //mvwaddnstr(w, 0, x, songname, width);
401                 mvwaddstr(w, 0, x, songname);
402         }
404         /* display time string */
405         if (screen->buf[0]) {
406                 x = screen->status_window.cols - strlen(screen->buf);
407                 colors_use(w, COLOR_STATUS_TIME);
408                 mvwaddstr(w, 0, x, screen->buf);
409         }
411         wnoutrefresh(w);
414 int
415 screen_exit(void)
417         endwin();
418         if (screen) {
419                 gint i;
421                 /* close and exit all screens (playlist,browse,help...) */
422                 i=0;
423                 while (screens[i].functions) {
424                         const struct screen_functions *sf = screens[i].functions;
426                         if (sf->close)
427                                 sf->close();
428                         if (sf->exit)
429                                 sf->exit();
431                         i++;
432                 }
434                 string_list_free(screen->find_history);
435                 g_free(screen->buf);
436                 g_free(screen->findbuf);
438                 g_free(screen);
439                 screen = NULL;
440         }
441         return 0;
444 void
445 screen_resize(void)
447         gint i;
449         D("Resize rows %d->%d, cols %d->%d\n",screen->rows,LINES,screen->cols,COLS);
450         if (COLS<SCREEN_MIN_COLS || LINES<SCREEN_MIN_ROWS) {
451                 screen_exit();
452                 fprintf(stderr, _("Error: Screen to small!\n"));
453                 exit(EXIT_FAILURE);
454         }
456         resizeterm(LINES, COLS);
458         screen->cols = COLS;
459         screen->rows = LINES;
461         /* top window */
462         screen->top_window.cols = screen->cols;
463         wresize(screen->top_window.w, 2, screen->cols);
465         /* main window */
466         screen->main_window.cols = screen->cols;
467         screen->main_window.rows = screen->rows-4;
468         wresize(screen->main_window.w, screen->main_window.rows, screen->cols);
469         wclear(screen->main_window.w);
471         /* progress window */
472         screen->progress_window.cols = screen->cols;
473         wresize(screen->progress_window.w, 1, screen->cols);
474         mvwin(screen->progress_window.w, screen->rows-2, 0);
476         /* status window */
477         screen->status_window.cols = screen->cols;
478         wresize(screen->status_window.w, 1, screen->cols);
479         mvwin(screen->status_window.w, screen->rows-1, 0);
481         screen->buf_size = screen->cols;
482         g_free(screen->buf);
483         screen->buf = g_malloc(screen->cols);
485         /* close and exit all screens (playlist,browse,help...) */
486         i=0;
487         while (screens[i].functions) {
488                 const struct screen_functions *sf = screens[i].functions;
490                 if (sf->resize)
491                         sf->resize(screen->main_window.cols, screen->main_window.rows);
493                 i++;
494         }
496         /* ? - without this the cursor becomes visible with aterm & Eterm */
497         curs_set(1);
498         curs_set(0);
500         screen->painted = 0;
503 void
504 screen_status_message(const char *msg)
506         WINDOW *w = screen->status_window.w;
508         wmove(w, 0, 0);
509         wclrtoeol(w);
510         colors_use(w, COLOR_STATUS_ALERT);
511         waddstr(w, msg);
512         wnoutrefresh(w);
513         screen->status_timestamp = time(NULL);
516 void
517 screen_status_printf(const char *format, ...)
519         char *msg;
520         va_list ap;
522         va_start(ap,format);
523         msg = g_strdup_vprintf(format,ap);
524         va_end(ap);
525         screen_status_message(msg);
526         g_free(msg);
529 void
530 ncurses_init(void)
533         /* initialize the curses library */
534         initscr();
535         /* initialize color support */
536         colors_start();
537         /* tell curses not to do NL->CR/NL on output */
538         nonl();
539         /*  use raw mode (ignore interrupt,quit,suspend, and flow control ) */
540 #ifdef ENABLE_RAW_MODE
541         //  raw();
542 #endif
543         /* don't echo input */
544         noecho();
545         /* set cursor invisible */
546         curs_set(0);
547         /* enable extra keys */
548         keypad(stdscr, TRUE);
549         /* return from getch() without blocking */
550         timeout(SCREEN_TIMEOUT);
551         /* initialize mouse support */
552 #ifdef HAVE_GETMOUSE
553         if( options.enable_mouse )
554                 mousemask(ALL_MOUSE_EVENTS, NULL);
555 #endif
557         if( COLS<SCREEN_MIN_COLS || LINES<SCREEN_MIN_ROWS )
558                 {
559                         fprintf(stderr, _("Error: Screen to small!\n"));
560                         exit(EXIT_FAILURE);
561                 }
562         screen = g_malloc(sizeof(screen_t));
563         memset(screen, 0, sizeof(screen_t));
564         screen->mode = 0;
565         screen->cols = COLS;
566         screen->rows = LINES;
568         screen->buf  = g_malloc(screen->cols);
569         screen->buf_size = screen->cols;
570         screen->findbuf = NULL;
571         screen->painted = 0;
572         screen->start_timestamp = time(NULL);
573         screen->input_timestamp = time(NULL);
574         screen->last_cmd = CMD_NONE;
576         /* create top window */
577         screen->top_window.rows = 2;
578         screen->top_window.cols = screen->cols;
579         screen->top_window.w = newwin(screen->top_window.rows,
580                                       screen->top_window.cols,
581                                       0, 0);
582         leaveok(screen->top_window.w, TRUE);
583         keypad(screen->top_window.w, TRUE);
585         /* create main window */
586         screen->main_window.rows = screen->rows-4;
587         screen->main_window.cols = screen->cols;
588         screen->main_window.w = newwin(screen->main_window.rows,
589                                        screen->main_window.cols,
590                                        2,
591                                        0);
593         //  leaveok(screen->main_window.w, TRUE); temporary disabled
594         keypad(screen->main_window.w, TRUE);
596         /* create progress window */
597         screen->progress_window.rows = 1;
598         screen->progress_window.cols = screen->cols;
599         screen->progress_window.w = newwin(screen->progress_window.rows,
600                                            screen->progress_window.cols,
601                                            screen->rows-2,
602                                            0);
603         leaveok(screen->progress_window.w, TRUE);
605         /* create status window */
606         screen->status_window.rows = 1;
607         screen->status_window.cols = screen->cols;
608         screen->status_window.w = newwin(screen->status_window.rows,
609                                          screen->status_window.cols,
610                                          screen->rows-1,
611                                          0);
613         leaveok(screen->status_window.w, FALSE);
614         keypad(screen->status_window.w, TRUE);
616         if( options.enable_colors )
617                 {
618                         /* set background attributes */
619                         wbkgd(stdscr, COLOR_PAIR(COLOR_LIST));
620                         wbkgd(screen->main_window.w,     COLOR_PAIR(COLOR_LIST));
621                         wbkgd(screen->top_window.w,      COLOR_PAIR(COLOR_TITLE));
622                         wbkgd(screen->progress_window.w, COLOR_PAIR(COLOR_PROGRESSBAR));
623                         wbkgd(screen->status_window.w,   COLOR_PAIR(COLOR_STATUS));
624                         colors_use(screen->progress_window.w, COLOR_PROGRESSBAR);
625                 }
627         refresh();
630 int
631 screen_init(mpdclient_t *c)
633         gint i;
635         /* initialize screens */
636         i=0;
637         while (screens[i].functions) {
638                 const struct screen_functions *fn = screens[i].functions;
640                 if (fn->init)
641                         fn->init(screen->main_window.w,
642                                  screen->main_window.cols,
643                                  screen->main_window.rows);
645                 i++;
646         }
648 #if 0
649         /* broken */
650         mode_fn = NULL;
651         switch_screen_mode(screen_get_id(options.screen_list[0]), c);
652 #else
653         mode_fn = &screen_playlist;
654 #endif
656         if( mode_fn && mode_fn->open )
657                 mode_fn->open(screen, c);
659         /* initialize wreadln */
660         wrln_wgetch = my_wgetch;
661         wrln_max_history_length = 16;
663         return 0;
666 void
667 screen_paint(mpdclient_t *c)
669         const char *title = NULL;
671         if (mode_fn && mode_fn->get_title)
672                 title = mode_fn->get_title(screen->buf, screen->buf_size);
674         D("screen_paint(%s)\n", title);
675         /* paint the title/header window */
676         if( title )
677                 paint_top_window(title, c, 1);
678         else
679                 paint_top_window("", c, 1);
681         /* paint the main window */
682         wclear(screen->main_window.w);
683         if( mode_fn && mode_fn->paint )
684                 mode_fn->paint(screen, c);
686         paint_progress_window(c);
687         paint_status_window(c);
688         screen->painted = 1;
689         wmove(screen->main_window.w, 0, 0);
690         wnoutrefresh(screen->main_window.w);
692         /* tell curses to update */
693         doupdate();
696 void
697 screen_update(mpdclient_t *c)
699         static int repeat = -1;
700         static int random_enabled = -1;
701         static int crossfade = -1;
702         static int dbupdate = -1;
704         if( !screen->painted )
705                 return screen_paint(c);
707         /* print a message if mpd status has changed */
708         if (repeat < 0) {
709                 repeat = c->status->repeat;
710                 random_enabled = c->status->random;
711                 crossfade = c->status->crossfade;
712                 dbupdate = c->status->updatingDb;
713         }
715         if (repeat != c->status->repeat)
716                 screen_status_printf(c->status->repeat ?
717                                      _("Repeat is on") :
718                                      _("Repeat is off"));
720         if (random_enabled != c->status->random)
721                 screen_status_printf(c->status->random ?
722                                      _("Random is on") :
723                                      _("Random is off"));
725         if (crossfade != c->status->crossfade)
726                 screen_status_printf(_("Crossfade %d seconds"), c->status->crossfade);
728         if (dbupdate && dbupdate != c->status->updatingDb) {
729                 screen_status_printf(_("Database updated!"));
730                 mpdclient_browse_callback(c, BROWSE_DB_UPDATED, NULL);
731         }
733         repeat = c->status->repeat;
734         random_enabled = c->status->random;
735         crossfade = c->status->crossfade;
736         dbupdate = c->status->updatingDb;
738         /* update title/header window */
739         if (welcome && screen->last_cmd==CMD_NONE &&
740             time(NULL)-screen->start_timestamp <= SCREEN_WELCOME_TIME)
741                 paint_top_window("", c, 0);
742         else if (mode_fn && mode_fn->get_title) {
743                 paint_top_window(mode_fn->get_title(screen->buf,screen->buf_size), c, 0);
744                 welcome = FALSE;
745         } else
746                 paint_top_window("", c, 0);
748         /* update the main window */
749         if (mode_fn && mode_fn->paint)
750                 mode_fn->update(screen, c);
752         /* update progress window */
753         paint_progress_window(c);
755         /* update status window */
756         paint_status_window(c);
758         /* move the cursor to the origin */
759         wmove(screen->main_window.w, 0, 0);
760         wnoutrefresh(screen->main_window.w);
762         /* tell curses to update */
763         doupdate();
766 void
767 screen_idle(mpdclient_t *c)
769         if (c->song && seek_id == c->song->id &&
770             (screen->last_cmd == CMD_SEEK_FORWARD ||
771              screen->last_cmd == CMD_SEEK_BACKWARD))
772                 mpdclient_cmd_seek(c, seek_id, seek_target_time);
774         screen->last_cmd = CMD_NONE;
775         seek_id = -1;
778 #ifdef HAVE_GETMOUSE
779 int
780 screen_get_mouse_event(mpdclient_t *c,
781                        list_window_t *lw, int lw_length,
782                        unsigned long *bstate, int *row)
784         MEVENT event;
786         /* retreive the mouse event from ncurses */
787         getmouse(&event);
788         D("mouse: id=%d  y=%d,x=%d,z=%d\n",event.id,event.y,event.x,event.z);
789         /* calculate the selected row in the list window */
790         *row = event.y - screen->top_window.rows;
791         /* copy button state bits */
792         *bstate = event.bstate;
793         /* if button 2 was pressed switch screen */
794         if (event.bstate & BUTTON2_CLICKED) {
795                 screen_cmd(c, CMD_SCREEN_NEXT);
796                 return 1;
797         }
799         /* if the even occured above the list window move up */
800         if (*row < 0 && lw) {
801                 if (event.bstate & BUTTON3_CLICKED)
802                         list_window_first(lw);
803                 else
804                         list_window_previous_page(lw);
805                 return 1;
806         }
808         /* if the even occured below the list window move down */
809         if ((unsigned)*row >= lw->rows && lw) {
810                 if (event.bstate & BUTTON3_CLICKED)
811                         list_window_last(lw, lw_length);
812                 else
813                         list_window_next_page(lw, lw_length);
814                 return 1;
815         }
817         return 0;
819 #endif
821 void
822 screen_cmd(mpdclient_t *c, command_t cmd)
824         screen->input_timestamp = time(NULL);
825         screen->last_cmd = cmd;
826         welcome = FALSE;
828         if( mode_fn && mode_fn->cmd && mode_fn->cmd(screen, c, cmd) )
829                 return;
831         switch(cmd) {
832         case CMD_PLAY:
833                 mpdclient_cmd_play(c, MPD_PLAY_AT_BEGINNING);
834                 break;
835         case CMD_PAUSE:
836                 mpdclient_cmd_pause(c, !IS_PAUSED(c->status->state));
837                 break;
838         case CMD_STOP:
839                 mpdclient_cmd_stop(c);
840                 break;
841         case CMD_SEEK_FORWARD:
842                 if (!IS_STOPPED(c->status->state)) {
843                         if (c->song && seek_id != c->song->id) {
844                                 seek_id = c->song->id;
845                                 seek_target_time = c->status->elapsedTime;
846                         }
847                         seek_target_time+=options.seek_time;
848                         if (seek_target_time < c->status->totalTime)
849                                 break;
850                         seek_target_time = c->status->totalTime;
851                         /* seek_target_time=0; */
852                 }
853                 break;
854                 /* fall through... */
855         case CMD_TRACK_NEXT:
856                 if (!IS_STOPPED(c->status->state))
857                         mpdclient_cmd_next(c);
858                 break;
859         case CMD_SEEK_BACKWARD:
860                 if (!IS_STOPPED(c->status->state)) {
861                         if (seek_id != c->song->id) {
862                                 seek_id = c->song->id;
863                                 seek_target_time = c->status->elapsedTime;
864                         }
865                         seek_target_time-=options.seek_time;
866                         if (seek_target_time < 0)
867                                 seek_target_time=0;
868                 }
869                 break;
870         case CMD_TRACK_PREVIOUS:
871                 if (!IS_STOPPED(c->status->state))
872                         mpdclient_cmd_prev(c);
873                 break;
874         case CMD_SHUFFLE:
875                 if (mpdclient_cmd_shuffle(c) == 0)
876                         screen_status_message(_("Shuffled playlist!"));
877                 break;
878         case CMD_CLEAR:
879                 if (mpdclient_cmd_clear(c) == 0)
880                         screen_status_message(_("Cleared playlist!"));
881                 break;
882         case CMD_REPEAT:
883                 mpdclient_cmd_repeat(c, !c->status->repeat);
884                 break;
885         case CMD_RANDOM:
886                 mpdclient_cmd_random(c, !c->status->random);
887                 break;
888         case CMD_CROSSFADE:
889                 if (c->status->crossfade)
890                         mpdclient_cmd_crossfade(c, 0);
891                 else
892                         mpdclient_cmd_crossfade(c, options.crossfade_time);
893                 break;
894         case CMD_DB_UPDATE:
895                 if (!c->status->updatingDb) {
896                         if( mpdclient_cmd_db_update_utf8(c,NULL)==0 )
897                                 screen_status_printf(_("Database update started!"));
898                 } else
899                         screen_status_printf(_("Database update running..."));
900                 break;
901         case CMD_VOLUME_UP:
902                 if( c->status->volume!=MPD_STATUS_NO_VOLUME && c->status->volume<100 )
903                         mpdclient_cmd_volume(c, ++c->status->volume);
904                 break;
905         case CMD_VOLUME_DOWN:
906                 if( c->status->volume!=MPD_STATUS_NO_VOLUME && c->status->volume>0 )
907                         mpdclient_cmd_volume(c, --c->status->volume);
908                 break;
909         case CMD_TOGGLE_FIND_WRAP:
910                 options.find_wrap = !options.find_wrap;
911                 screen_status_printf(options.find_wrap ?
912                                      _("Find mode: Wrapped") :
913                                      _("Find mode: Normal"));
914                 break;
915         case CMD_TOGGLE_AUTOCENTER:
916                 options.auto_center = !options.auto_center;
917                 screen_status_printf(options.auto_center ?
918                                      _("Auto center mode: On") :
919                                      _("Auto center mode: Off"));
920                 break;
921         case CMD_SCREEN_UPDATE:
922                 screen->painted = 0;
923                 break;
924         case CMD_SCREEN_PREVIOUS:
925                 screen_next_mode(c, -1);
926                 break;
927         case CMD_SCREEN_NEXT:
928                 screen_next_mode(c, 1);
929                 break;
930         case CMD_SCREEN_PLAY:
931                 switch_screen_mode(SCREEN_PLAYLIST_ID, c);
932                 break;
933         case CMD_SCREEN_FILE:
934                 switch_screen_mode(SCREEN_BROWSE_ID, c);
935                 break;
936         case CMD_SCREEN_HELP:
937                 switch_screen_mode(SCREEN_HELP_ID, c);
938                 break;
939         case CMD_SCREEN_SEARCH:
940                 switch_screen_mode(SCREEN_SEARCH_ID, c);
941                 break;
942         case CMD_SCREEN_ARTIST:
943                 switch_screen_mode(SCREEN_ARTIST_ID, c);
944                 break;
945         case CMD_SCREEN_KEYDEF:
946                 switch_screen_mode(SCREEN_KEYDEF_ID, c);
947                 break;
948         case CMD_SCREEN_CLOCK:
949                 switch_screen_mode(SCREEN_CLOCK_ID, c);
950                 break;
951         case CMD_SCREEN_LYRICS:
952                 switch_screen_mode(SCREEN_LYRICS_ID, c);
953                 break;
954         case CMD_QUIT:
955                 exit(EXIT_SUCCESS);
956         default:
957                 break;
958         }