Code

screen: moved list_window mouse code to list_window.c
[ncmpc.git] / src / screen.c
index 3395255233ae9d4f3c7484448a18d8ab208f50ff..a76438b9c6f9b095f016fbaea11d6f364b5cf6c2 100644 (file)
@@ -43,7 +43,6 @@
 #define SCREEN_ARTIST_ID       2
 #define SCREEN_HELP_ID         100
 #define SCREEN_KEYDEF_ID       101
-#define SCREEN_CLOCK_ID        102
 #define SCREEN_SEARCH_ID       103
 #define SCREEN_LYRICS_ID          104
 
@@ -62,9 +61,6 @@ extern const struct screen_functions screen_search;
 #ifdef ENABLE_KEYDEF_SCREEN
 extern const struct screen_functions screen_keydef;
 #endif
-#ifdef ENABLE_CLOCK_SCREEN
-extern const struct screen_functions screen_clock;
-#endif
 extern const struct screen_functions screen_lyrics;
 
 typedef struct screen_functions * (*screen_get_mode_functions_fn_t) (void);
@@ -87,15 +83,13 @@ static const struct
 #ifdef ENABLE_KEYDEF_SCREEN
        { SCREEN_KEYDEF_ID, "keydef", &screen_keydef },
 #endif
-#ifdef ENABLE_CLOCK_SCREEN
-       { SCREEN_CLOCK_ID, "clock", &screen_clock },
-#endif
 #ifdef ENABLE_LYRICS_SCREEN
        { SCREEN_LYRICS_ID, "lyrics", &screen_lyrics },
 #endif
-       { G_MAXINT, NULL, NULL }
 };
 
+#define NUM_SCREENS (sizeof(screens) / sizeof(screens[0]))
+
 static gboolean welcome = TRUE;
 static struct screen screen;
 static const struct screen_functions *mode_fn = &screen_playlist;
@@ -105,26 +99,24 @@ static int seek_target_time = 0;
 gint
 screen_get_id(const char *name)
 {
-       gint i=0;
+       guint i;
 
-       while (screens[i].name) {
+       for (i = 0; i < NUM_SCREENS; ++i)
                if (strcmp(name, screens[i].name) == 0)
                        return screens[i].id;
-               i++;
-       }
+
        return -1;
 }
 
 static gint
 lookup_mode(gint id)
 {
-       gint i=0;
+       guint i;
 
-       while (screens[i].name) {
+       for (i = 0; i < NUM_SCREENS; ++i)
                if (screens[i].id == id)
                        return i;
-               i++;
-       }
+
        return -1;
 }
 
@@ -141,18 +133,18 @@ switch_screen_mode(gint id, mpdclient_t *c)
        if( id == screens[screen.mode].id )
                return;
 
+       new_mode = lookup_mode(id);
+       if (new_mode < 0)
+               return;
+
        /* close the old mode */
        if (mode_fn->close != NULL)
                mode_fn->close();
 
        /* get functions for the new mode */
-       new_mode = lookup_mode(id);
-       if (new_mode >= 0 && screens[new_mode].functions) {
-               D("switch_screen(%s)\n", screens[new_mode].name );
-               mode_fn = screens[new_mode].functions;
-               screen.mode = new_mode;
-       }
-
+       D("switch_screen(%s)\n", screens[new_mode].name );
+       mode_fn = screens[new_mode].functions;
+       screen.mode = new_mode;
        screen.painted = 0;
 
        /* open the new mode */
@@ -232,7 +224,7 @@ paint_top_window2(const char *header, mpdclient_t *c)
                waddstr(w, _(":Lyrics  "));
 #endif
        }
-       if (c->status->volume==MPD_STATUS_NO_VOLUME) {
+       if (c->status == NULL || c->status->volume == MPD_STATUS_NO_VOLUME) {
                g_snprintf(buf, 32, _("Volume n/a "));
        } else {
                g_snprintf(buf, 32, _(" Volume %d%%"), c->status->volume);
@@ -241,14 +233,17 @@ paint_top_window2(const char *header, mpdclient_t *c)
        mvwaddstr(w, 0, screen.top_window.cols-my_strlen(buf), buf);
 
        flags[0] = 0;
-       if( c->status->repeat )
-               g_strlcat(flags, "r", sizeof(flags));
-       if( c->status->random )
-               g_strlcat(flags, "z", sizeof(flags));;
-       if( c->status->crossfade )
-               g_strlcat(flags, "x", sizeof(flags));
-       if( c->status->updatingDb )
-               g_strlcat(flags, "U", sizeof(flags));
+       if (c->status != NULL) {
+               if (c->status->repeat)
+                       g_strlcat(flags, "r", sizeof(flags));
+               if (c->status->random)
+                       g_strlcat(flags, "z", sizeof(flags));;
+               if (c->status->crossfade)
+                       g_strlcat(flags, "x", sizeof(flags));
+               if (c->status->updatingDb)
+                       g_strlcat(flags, "U", sizeof(flags));
+       }
+
        colors_use(w, COLOR_LINE);
        mvwhline(w, 1, 0, ACS_HLINE, screen.top_window.cols);
        if (flags[0]) {
@@ -279,7 +274,8 @@ paint_top_window(const char *header, mpdclient_t *c, int full_repaint)
                wclrtoeol(w);
        }
 
-       if (prev_volume!=c->status->volume || full_repaint)
+       if ((c->status != NULL && prev_volume != c->status->volume) ||
+           full_repaint)
                paint_top_window2(header, c);
 }
 
@@ -288,7 +284,7 @@ paint_progress_window(mpdclient_t *c)
 {
        double p;
        int width;
-       int elapsedTime = c->status->elapsedTime;
+       int elapsedTime;
 
        if (c->status==NULL || IS_STOPPED(c->status->state)) {
                mvwhline(screen.progress_window.w, 0, 0, ACS_HLINE,
@@ -299,6 +295,8 @@ paint_progress_window(mpdclient_t *c)
 
        if (c->song && seek_id == c->song->id)
                elapsedTime = seek_target_time;
+       else
+               elapsedTime = c->status->elapsedTime;
 
        p = ((double) elapsedTime) / ((double) c->status->totalTime);
 
@@ -319,6 +317,7 @@ paint_status_window(mpdclient_t *c)
        mpd_Status *status = c->status;
        mpd_Song *song = c->song;
        int elapsedTime = 0;
+       char bitrate[16];
        const char *str = NULL;
        int x = 0;
 
@@ -329,7 +328,7 @@ paint_status_window(mpdclient_t *c)
        wclrtoeol(w);
        colors_use(w, COLOR_STATUS_BOLD);
 
-       switch(status->state) {
+       switch (status == NULL ? MPD_STATUS_STATE_STOP : status->state) {
        case MPD_STATUS_STATE_PLAY:
                str = _("Playing:");
                break;
@@ -348,7 +347,8 @@ paint_status_window(mpdclient_t *c)
 
        /* create time string */
        memset(screen.buf, 0, screen.buf_size);
-       if (IS_PLAYING(status->state) || IS_PAUSED(status->state)) {
+       if (status != NULL && (IS_PLAYING(status->state) ||
+                              IS_PAUSED(status->state))) {
                if (status->totalTime > 0) {
                        /*checks the conf to see whether to display elapsed or remaining time */
                        if(!strcmp(options.timedisplay_type,"elapsed"))
@@ -358,16 +358,25 @@ paint_status_window(mpdclient_t *c)
 
                        if( c->song && seek_id == c->song->id )
                                elapsedTime = seek_target_time;
+
+                       /* display bitrate if visible-bitrate is true */
+                       if (options.visible_bitrate) {
+                               g_snprintf(bitrate, 16,
+                                          " [%d kbps]", status->bitRate);
+                       } else {
+                               bitrate[0] = '\0';
+                       }
+
                        /*write out the time, using hours if time over 60 minutes*/
                        if (c->status->totalTime > 3600) {
                                g_snprintf(screen.buf, screen.buf_size,
-                                          " [%i:%02i:%02i/%i:%02i:%02i]",
-                                          elapsedTime/3600, (elapsedTime%3600)/60, elapsedTime%60,
+                                          "%s [%i:%02i:%02i/%i:%02i:%02i]",
+                                          bitrate, elapsedTime/3600, (elapsedTime%3600)/60, elapsedTime%60,
                                           status->totalTime/3600, (status->totalTime%3600)/60,  status->totalTime%60);
                        } else {
                                g_snprintf(screen.buf, screen.buf_size,
-                                          " [%i:%02i/%i:%02i]",
-                                          elapsedTime/60, elapsedTime%60,
+                                          "%s [%i:%02i/%i:%02i]",
+                                          bitrate, elapsedTime/60, elapsedTime%60,
                                           status->totalTime/60,   status->totalTime%60 );
                        }
                } else {
@@ -382,7 +391,8 @@ paint_status_window(mpdclient_t *c)
        }
 
        /* display song */
-       if (IS_PLAYING(status->state) || IS_PAUSED(status->state)) {
+       if (status != NULL && (IS_PLAYING(status->state) ||
+                              IS_PAUSED(status->state))) {
                char songname[MAX_SONGNAME_LENGTH];
                int width = COLS-x-my_strlen(screen.buf);
 
@@ -414,38 +424,31 @@ paint_status_window(mpdclient_t *c)
        wnoutrefresh(w);
 }
 
-int
+void
 screen_exit(void)
 {
-       gint i;
-
-       endwin();
+       guint i;
 
        if (mode_fn->close != NULL)
                mode_fn->close();
 
        /* close and exit all screens (playlist,browse,help...) */
-       i=0;
-       while (screens[i].functions) {
+       for (i = 0; i < NUM_SCREENS; ++i) {
                const struct screen_functions *sf = screens[i].functions;
 
                if (sf->exit)
                        sf->exit();
-
-               i++;
        }
 
        string_list_free(screen.find_history);
        g_free(screen.buf);
        g_free(screen.findbuf);
-
-       return 0;
 }
 
 void
 screen_resize(void)
 {
-       gint i;
+       guint i;
 
        D("Resize rows %d->%d, cols %d->%d\n",screen.rows,LINES,screen.cols,COLS);
        if (COLS<SCREEN_MIN_COLS || LINES<SCREEN_MIN_ROWS) {
@@ -484,16 +487,14 @@ screen_resize(void)
        screen.buf = g_malloc(screen.cols);
 
        /* close and exit all screens (playlist,browse,help...) */
-       i=0;
-       while (screens[i].functions) {
+       for (i = 0; i < NUM_SCREENS; ++i) {
                const struct screen_functions *sf = screens[i].functions;
 
                if (sf->resize)
                        sf->resize(screen.main_window.cols, screen.main_window.rows);
-
-               i++;
        }
 
+
        /* ? - without this the cursor becomes visible with aterm & Eterm */
        curs_set(1);
        curs_set(0);
@@ -528,38 +529,15 @@ screen_status_printf(const char *format, ...)
 }
 
 void
-ncurses_init(void)
+screen_init(mpdclient_t *c)
 {
+       guint i;
 
-       /* initialize the curses library */
-       initscr();
-       /* initialize color support */
-       colors_start();
-       /* tell curses not to do NL->CR/NL on output */
-       nonl();
-       /*  use raw mode (ignore interrupt,quit,suspend, and flow control ) */
-#ifdef ENABLE_RAW_MODE
-       //  raw();
-#endif
-       /* don't echo input */
-       noecho();
-       /* set cursor invisible */
-       curs_set(0);
-       /* enable extra keys */
-       keypad(stdscr, TRUE);
-       /* return from getch() without blocking */
-       timeout(SCREEN_TIMEOUT);
-       /* initialize mouse support */
-#ifdef HAVE_GETMOUSE
-       if( options.enable_mouse )
-               mousemask(ALL_MOUSE_EVENTS, NULL);
-#endif
+       if (COLS < SCREEN_MIN_COLS || LINES < SCREEN_MIN_ROWS) {
+               fprintf(stderr, _("Error: Screen to small!\n"));
+               exit(EXIT_FAILURE);
+       }
 
-       if( COLS<SCREEN_MIN_COLS || LINES<SCREEN_MIN_ROWS )
-               {
-                       fprintf(stderr, _("Error: Screen to small!\n"));
-                       exit(EXIT_FAILURE);
-               }
        screen.mode = 0;
        screen.cols = COLS;
        screen.rows = LINES;
@@ -612,36 +590,26 @@ ncurses_init(void)
        leaveok(screen.status_window.w, FALSE);
        keypad(screen.status_window.w, TRUE);
 
-       if( options.enable_colors )
-               {
-                       /* set background attributes */
-                       wbkgd(stdscr, COLOR_PAIR(COLOR_LIST));
-                       wbkgd(screen.main_window.w,     COLOR_PAIR(COLOR_LIST));
-                       wbkgd(screen.top_window.w,      COLOR_PAIR(COLOR_TITLE));
-                       wbkgd(screen.progress_window.w, COLOR_PAIR(COLOR_PROGRESSBAR));
-                       wbkgd(screen.status_window.w,   COLOR_PAIR(COLOR_STATUS));
-                       colors_use(screen.progress_window.w, COLOR_PROGRESSBAR);
-               }
+       if (options.enable_colors) {
+               /* set background attributes */
+               wbkgd(stdscr, COLOR_PAIR(COLOR_LIST));
+               wbkgd(screen.main_window.w,     COLOR_PAIR(COLOR_LIST));
+               wbkgd(screen.top_window.w,      COLOR_PAIR(COLOR_TITLE));
+               wbkgd(screen.progress_window.w, COLOR_PAIR(COLOR_PROGRESSBAR));
+               wbkgd(screen.status_window.w,   COLOR_PAIR(COLOR_STATUS));
+               colors_use(screen.progress_window.w, COLOR_PROGRESSBAR);
+       }
 
        refresh();
-}
-
-int
-screen_init(mpdclient_t *c)
-{
-       gint i;
 
        /* initialize screens */
-       i=0;
-       while (screens[i].functions) {
+       for (i = 0; i < NUM_SCREENS; ++i) {
                const struct screen_functions *fn = screens[i].functions;
 
                if (fn->init)
                        fn->init(screen.main_window.w,
                                 screen.main_window.cols,
                                 screen.main_window.rows);
-
-               i++;
        }
 
        if (mode_fn->open != NULL)
@@ -650,8 +618,6 @@ screen_init(mpdclient_t *c)
        /* initialize wreadln */
        wrln_wgetch = my_wgetch;
        wrln_max_history_length = 16;
-
-       return 0;
 }
 
 void
@@ -696,38 +662,41 @@ screen_update(mpdclient_t *c)
                return screen_paint(c);
 
        /* print a message if mpd status has changed */
-       if (repeat < 0) {
-               repeat = c->status->repeat;
-               random_enabled = c->status->random;
-               crossfade = c->status->crossfade;
-               dbupdate = c->status->updatingDb;
-       }
+       if (c->status != NULL) {
+               if (repeat < 0) {
+                       repeat = c->status->repeat;
+                       random_enabled = c->status->random;
+                       crossfade = c->status->crossfade;
+                       dbupdate = c->status->updatingDb;
+               }
 
-       if (repeat != c->status->repeat)
-               screen_status_printf(c->status->repeat ?
-                                    _("Repeat is on") :
-                                    _("Repeat is off"));
+               if (repeat != c->status->repeat)
+                       screen_status_printf(c->status->repeat ?
+                                            _("Repeat is on") :
+                                            _("Repeat is off"));
 
-       if (random_enabled != c->status->random)
-               screen_status_printf(c->status->random ?
-                                    _("Random is on") :
-                                    _("Random is off"));
+               if (random_enabled != c->status->random)
+                       screen_status_printf(c->status->random ?
+                                            _("Random is on") :
+                                            _("Random is off"));
 
-       if (crossfade != c->status->crossfade)
-               screen_status_printf(_("Crossfade %d seconds"), c->status->crossfade);
+               if (crossfade != c->status->crossfade)
+                       screen_status_printf(_("Crossfade %d seconds"), c->status->crossfade);
 
-       if (dbupdate && dbupdate != c->status->updatingDb) {
-               screen_status_printf(_("Database updated!"));
-               mpdclient_browse_callback(c, BROWSE_DB_UPDATED, NULL);
-       }
+               if (dbupdate && dbupdate != c->status->updatingDb) {
+                       screen_status_printf(_("Database updated!"));
+                       mpdclient_browse_callback(c, BROWSE_DB_UPDATED, NULL);
+               }
 
-       repeat = c->status->repeat;
-       random_enabled = c->status->random;
-       crossfade = c->status->crossfade;
-       dbupdate = c->status->updatingDb;
+               repeat = c->status->repeat;
+               random_enabled = c->status->random;
+               crossfade = c->status->crossfade;
+               dbupdate = c->status->updatingDb;
+       }
 
        /* update title/header window */
-       if (welcome && screen.last_cmd==CMD_NONE &&
+       if (welcome && options.welcome_screen_list &&
+           screen.last_cmd==CMD_NONE &&
            time(NULL)-screen.start_timestamp <= SCREEN_WELCOME_TIME)
                paint_top_window("", c, 0);
        else if (mode_fn->get_title != NULL) {
@@ -768,9 +737,7 @@ screen_idle(mpdclient_t *c)
 
 #ifdef HAVE_GETMOUSE
 int
-screen_get_mouse_event(mpdclient_t *c,
-                      list_window_t *lw, int lw_length,
-                      unsigned long *bstate, int *row)
+screen_get_mouse_event(mpdclient_t *c, unsigned long *bstate, int *row)
 {
        MEVENT event;
 
@@ -787,48 +754,31 @@ screen_get_mouse_event(mpdclient_t *c,
                return 1;
        }
 
-       /* if the even occured above the list window move up */
-       if (*row < 0 && lw) {
-               if (event.bstate & BUTTON3_CLICKED)
-                       list_window_first(lw);
-               else
-                       list_window_previous_page(lw);
-               return 1;
-       }
-
-       /* if the even occured below the list window move down */
-       if ((unsigned)*row >= lw->rows && lw) {
-               if (event.bstate & BUTTON3_CLICKED)
-                       list_window_last(lw, lw_length);
-               else
-                       list_window_next_page(lw, lw_length);
-               return 1;
-       }
-
        return 0;
 }
 #endif
 
-void
-screen_cmd(mpdclient_t *c, command_t cmd)
+static int
+screen_client_cmd(mpdclient_t *c, command_t cmd)
 {
-       screen.input_timestamp = time(NULL);
-       screen.last_cmd = cmd;
-       welcome = FALSE;
-
-       if (mode_fn->cmd != NULL && mode_fn->cmd(&screen, c, cmd))
-               return;
+       if (c->connection == NULL || c->status == NULL)
+               return 0;
 
        switch(cmd) {
+               /*
        case CMD_PLAY:
                mpdclient_cmd_play(c, MPD_PLAY_AT_BEGINNING);
                break;
+               */
        case CMD_PAUSE:
                mpdclient_cmd_pause(c, !IS_PAUSED(c->status->state));
                break;
        case CMD_STOP:
                mpdclient_cmd_stop(c);
                break;
+       case CMD_CROP:
+               mpdclient_cmd_crop(c);
+               break;
        case CMD_SEEK_FORWARD:
                if (!IS_STOPPED(c->status->state)) {
                        if (c->song && seek_id != c->song->id) {
@@ -897,6 +847,28 @@ screen_cmd(mpdclient_t *c, command_t cmd)
                if( c->status->volume!=MPD_STATUS_NO_VOLUME && c->status->volume>0 )
                        mpdclient_cmd_volume(c, --c->status->volume);
                break;
+
+       default:
+               return 0;
+       }
+
+       return 1;
+}
+
+void
+screen_cmd(mpdclient_t *c, command_t cmd)
+{
+       screen.input_timestamp = time(NULL);
+       screen.last_cmd = cmd;
+       welcome = FALSE;
+
+       if (mode_fn->cmd != NULL && mode_fn->cmd(&screen, c, cmd))
+               return;
+
+       if (screen_client_cmd(c, cmd))
+               return;
+
+       switch(cmd) {
        case CMD_TOGGLE_FIND_WRAP:
                options.find_wrap = !options.find_wrap;
                screen_status_printf(options.find_wrap ?
@@ -936,14 +908,9 @@ screen_cmd(mpdclient_t *c, command_t cmd)
        case CMD_SCREEN_KEYDEF:
                switch_screen_mode(SCREEN_KEYDEF_ID, c);
                break;
-       case CMD_SCREEN_CLOCK:
-               switch_screen_mode(SCREEN_CLOCK_ID, c);
-               break;
        case CMD_SCREEN_LYRICS:
                switch_screen_mode(SCREEN_LYRICS_ID, c);
                break;
-       case CMD_QUIT:
-               exit(EXIT_SUCCESS);
        default:
                break;
        }