Code

screen_interface: add method mouse(), replacing CMD_MOUSE_EVENT
[ncmpc.git] / src / screen_browser.c
index 313f9da6688e31865becda4a5ce95d73c3df2e60..e0c37cbc2e0a48a04e1a66ee3be030b6aad6786a 100644 (file)
@@ -1,5 +1,5 @@
 /* ncmpc (Ncurses MPD Client)
- * (c) 2004-2010 The Music Player Daemon Project
+ * (c) 2004-2017 The Music Player Daemon Project
  * Project homepage: http://musicpd.org
  *
  * This program is free software; you can redistribute it and/or modify
@@ -22,7 +22,7 @@
 #include "screen_file.h"
 #include "screen_song.h"
 #include "screen_lyrics.h"
-#include "screen_message.h"
+#include "screen_status.h"
 #include "screen_find.h"
 #include "screen.h"
 #include "i18n.h"
@@ -52,9 +52,7 @@ void
 screen_browser_sync_highlights(struct filelist *fl,
                               const struct mpdclient_playlist *playlist)
 {
-       guint i;
-
-       for (i = 0; i < filelist_length(fl); ++i) {
+       for (unsigned i = 0; i < filelist_length(fl); ++i) {
                struct filelist_entry *entry = filelist_get(fl, i);
                const struct mpd_entity *entity = entry->entity;
 
@@ -79,16 +77,14 @@ browser_lw_callback(unsigned idx, void *data)
 {
        const struct filelist *fl = (const struct filelist *) data;
        static char buf[BUFSIZE];
-       const struct filelist_entry *entry;
-       const struct mpd_entity *entity;
 
        assert(fl != NULL);
        assert(idx < filelist_length(fl));
 
-       entry = filelist_get(fl, idx);
+       const struct filelist_entry *entry = filelist_get(fl, idx);
        assert(entry != NULL);
 
-       entity = entry->entity;
+       const struct mpd_entity *entity = entry->entity;
 
        if( entity == NULL )
                return "..";
@@ -169,7 +165,7 @@ enqueue_and_play(struct mpdclient *c, struct filelist_entry *entry)
                entry->flags |= HIGHLIGHT;
 #endif
                strfsong(buf, BUFSIZE, options.list_format, song);
-               screen_status_printf(_("Adding \'%s\' to playlist"), buf);
+               screen_status_printf(_("Adding \'%s\' to queue"), buf);
        }
 
        if (!mpd_run_play_id(connection, id)) {
@@ -231,12 +227,10 @@ static bool
 browser_handle_enter(struct screen_browser *browser, struct mpdclient *c)
 {
        struct filelist_entry *entry = browser_get_selected_entry(browser);
-       struct mpd_entity *entity;
-
        if (entry == NULL)
                return false;
 
-       entity = entry->entity;
+       struct mpd_entity *entity = entry->entity;
        if (entity == NULL)
                return false;
 
@@ -249,7 +243,7 @@ browser_handle_enter(struct screen_browser *browser, struct mpdclient *c)
 
 static bool
 browser_select_entry(struct mpdclient *c, struct filelist_entry *entry,
-                    G_GNUC_UNUSED gboolean toggle)
+                    gcc_unused gboolean toggle)
 {
        assert(entry != NULL);
        assert(entry->entity != NULL);
@@ -264,7 +258,7 @@ browser_select_entry(struct mpdclient *c, struct filelist_entry *entry,
                if (mpdclient_cmd_add_path(c, mpd_directory_get_path(dir))) {
                        char *tmp = utf8_to_locale(mpd_directory_get_path(dir));
 
-                       screen_status_printf(_("Adding \'%s\' to playlist"), tmp);
+                       screen_status_printf(_("Adding \'%s\' to queue"), tmp);
                        g_free(tmp);
                }
 
@@ -289,7 +283,7 @@ browser_select_entry(struct mpdclient *c, struct filelist_entry *entry,
                        char buf[BUFSIZE];
 
                        strfsong(buf, BUFSIZE, options.list_format, song);
-                       screen_status_printf(_("Adding \'%s\' to playlist"), buf);
+                       screen_status_printf(_("Adding \'%s\' to queue"), buf);
                }
 #ifndef NCMPC_MINI
        } else {
@@ -313,13 +307,11 @@ static bool
 browser_handle_select(struct screen_browser *browser, struct mpdclient *c)
 {
        struct list_window_range range;
-       struct filelist_entry *entry;
        bool success = false;
 
        list_window_get_range(browser->lw, &range);
        for (unsigned i = range.start; i < range.end; ++i) {
-               entry = browser_get_index(browser, i);
-
+               struct filelist_entry *entry = browser_get_index(browser, i);
                if (entry != NULL && entry->entity != NULL)
                        success = browser_select_entry(c, entry, TRUE);
        }
@@ -331,13 +323,11 @@ static bool
 browser_handle_add(struct screen_browser *browser, struct mpdclient *c)
 {
        struct list_window_range range;
-       struct filelist_entry *entry;
        bool success = false;
 
        list_window_get_range(browser->lw, &range);
        for (unsigned i = range.start; i < range.end; ++i) {
-               entry = browser_get_index(browser, i);
-
+               struct filelist_entry *entry = browser_get_index(browser, i);
                if (entry != NULL && entry->entity != NULL)
                        success = browser_select_entry(c, entry, FALSE) ||
                                success;
@@ -349,12 +339,10 @@ browser_handle_add(struct screen_browser *browser, struct mpdclient *c)
 static void
 browser_handle_select_all(struct screen_browser *browser, struct mpdclient *c)
 {
-       guint i;
-
        if (browser->filelist == NULL)
                return;
 
-       for (i = 0; i < filelist_length(browser->filelist); ++i) {
+       for (unsigned i = 0; i < filelist_length(browser->filelist); ++i) {
                struct filelist_entry *entry = filelist_get(browser->filelist, i);
 
                if (entry != NULL && entry->entity != NULL)
@@ -363,16 +351,15 @@ browser_handle_select_all(struct screen_browser *browser, struct mpdclient *c)
 }
 
 #ifdef HAVE_GETMOUSE
-static int
-browser_handle_mouse_event(struct screen_browser *browser, struct mpdclient *c)
+
+bool
+browser_mouse(struct screen_browser *browser,
+             struct mpdclient *c, gcc_unused int x, int row, mmask_t bstate)
 {
-       int row;
        unsigned prev_selected = browser->lw->selected;
-       unsigned long bstate;
 
-       if (screen_get_mouse_event(c, &bstate, &row) ||
-           list_window_mouse(browser->lw, bstate, row))
-               return 1;
+       if (list_window_mouse(browser->lw, bstate, row))
+               return true;
 
        list_window_set_cursor(browser->lw, browser->lw->start + row);
 
@@ -384,20 +371,19 @@ browser_handle_mouse_event(struct screen_browser *browser, struct mpdclient *c)
                        browser_handle_select(browser, c);
        }
 
-       return 1;
+       return true;
 }
+
 #endif
 
 static void
 screen_browser_paint_callback(WINDOW *w, unsigned i, unsigned y,
-                             unsigned width, bool selected, void *data);
+                             unsigned width, bool selected, const void *data);
 
 bool
 browser_cmd(struct screen_browser *browser,
            struct mpdclient *c, command_t cmd)
 {
-       const struct mpd_song *song;
-
        if (browser->filelist == NULL)
                return false;
 
@@ -405,6 +391,10 @@ browser_cmd(struct screen_browser *browser,
                return true;
 
        switch (cmd) {
+#if defined(ENABLE_SONG_SCREEN) || defined(ENABLE_LYRICS_SCREEN)
+               const struct mpd_song *song;
+#endif
+
        case CMD_LIST_FIND:
        case CMD_LIST_RFIND:
        case CMD_LIST_FIND_NEXT:
@@ -413,15 +403,10 @@ browser_cmd(struct screen_browser *browser,
                            browser->filelist);
                return true;
        case CMD_LIST_JUMP:
-               screen_jump(browser->lw, browser_lw_callback,
-                           screen_browser_paint_callback, browser->filelist);
-               return true;
-
-#ifdef HAVE_GETMOUSE
-       case CMD_MOUSE_EVENT:
-               browser_handle_mouse_event(browser, c);
+               screen_jump(browser->lw,
+                           browser_lw_callback, browser->filelist,
+                           screen_browser_paint_callback, browser);
                return true;
-#endif
 
 #ifdef ENABLE_SONG_SCREEN
        case CMD_SCREEN_SONG:
@@ -454,6 +439,8 @@ browser_cmd(struct screen_browser *browser,
                return false;
 
        switch (cmd) {
+               const struct mpd_song *song;
+
        case CMD_PLAY:
                browser_handle_enter(browser, c);
                return true;
@@ -511,35 +498,34 @@ screen_browser_paint_playlist(WINDOW *w, unsigned width,
 static void
 screen_browser_paint_callback(WINDOW *w, unsigned i,
                              unsigned y, unsigned width,
-                             bool selected, void *data)
+                             bool selected, const void *data)
 {
-       const struct filelist *fl = (const struct filelist *) data;
-       const struct filelist_entry *entry;
-       const struct mpd_entity *entity;
-       bool highlight;
-       const struct mpd_directory *directory;
-       const struct mpd_playlist *playlist;
-       char *p;
+       const struct screen_browser *browser = (const struct screen_browser *) data;
 
-       assert(fl != NULL);
-       assert(i < filelist_length(fl));
+       assert(browser != NULL);
+       assert(browser->filelist != NULL);
+       assert(i < filelist_length(browser->filelist));
 
-       entry = filelist_get(fl, i);
+       const struct filelist_entry *entry = filelist_get(browser->filelist, i);
        assert(entry != NULL);
 
-       entity = entry->entity;
+       const struct mpd_entity *entity = entry->entity;
        if (entity == NULL) {
                screen_browser_paint_directory(w, width, selected, "..");
                return;
        }
 
 #ifndef NCMPC_MINI
-       highlight = (entry->flags & HIGHLIGHT) != 0;
+       const bool highlight = (entry->flags & HIGHLIGHT) != 0;
 #else
-       highlight = false;
+       const bool highlight = false;
 #endif
 
        switch (mpd_entity_get_type(entity)) {
+               const struct mpd_directory *directory;
+               const struct mpd_playlist *playlist;
+               char *p;
+
        case MPD_ENTITY_TYPE_DIRECTORY:
                directory = mpd_entity_get_directory(entity);
                p = utf8_to_locale(g_basename(mpd_directory_get_path(directory)));
@@ -549,7 +535,7 @@ screen_browser_paint_callback(WINDOW *w, unsigned i,
 
        case MPD_ENTITY_TYPE_SONG:
                paint_song_row(w, y, width, selected, highlight,
-                              mpd_entity_get_song(entity), NULL);
+                              mpd_entity_get_song(entity), NULL, browser->song_format);
                break;
 
        case MPD_ENTITY_TYPE_PLAYLIST:
@@ -569,5 +555,5 @@ void
 screen_browser_paint(const struct screen_browser *browser)
 {
        list_window_paint2(browser->lw, screen_browser_paint_callback,
-                          browser->filelist);
+                          browser);
 }