Code

screen_interface: add method mouse(), replacing CMD_MOUSE_EVENT
[ncmpc.git] / src / screen_artist.c
index 1d279b9cffb2c9a38ea649b58139c2e333393527..672c93f33c366f0b35f206d86e369911bf07dd19 100644 (file)
@@ -1,25 +1,25 @@
 /* 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
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.
-
+ *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
-
+ *
  * You should have received a copy of the GNU General Public License along
  * with this program; if not, write to the Free Software Foundation, Inc.,
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
-*/
+ */
 
 #include "screen_artist.h"
 #include "screen_interface.h"
-#include "screen_message.h"
+#include "screen_status.h"
 #include "screen_find.h"
 #include "screen_browser.h"
 #include "screen.h"
@@ -28,6 +28,7 @@
 #include "mpdclient.h"
 #include "screen_browser.h"
 #include "filelist.h"
+#include "options.h"
 
 #include <assert.h>
 #include <string.h>
@@ -49,12 +50,10 @@ static gint
 compare_utf8(gconstpointer s1, gconstpointer s2)
 {
        const char *const*t1 = s1, *const*t2 = s2;
-       char *key1, *key2;
-       int n;
 
-       key1 = g_utf8_collate_key(*t1,-1);
-       key2 = g_utf8_collate_key(*t2,-1);
-       n = strcmp(key1,key2);
+       char *key1 = g_utf8_collate_key(*t1,-1);
+       char *key2 = g_utf8_collate_key(*t2,-1);
+       int n = strcmp(key1,key2);
        g_free(key1);
        g_free(key2);
        return n;
@@ -65,8 +64,6 @@ static const char *
 screen_artist_lw_callback(unsigned idx, void *data)
 {
        GPtrArray *list = data;
-       static char buf[BUFSIZE];
-       char *str, *str_utf8;
 
        if (mode == LIST_ALBUMS) {
                if (idx == 0)
@@ -79,32 +76,22 @@ screen_artist_lw_callback(unsigned idx, void *data)
 
        assert(idx < list->len);
 
-       str_utf8 = g_ptr_array_index(list, idx);
+       char *str_utf8 = g_ptr_array_index(list, idx);
        assert(str_utf8 != NULL);
 
-       str = utf8_to_locale(str_utf8);
+       char *str = utf8_to_locale(str_utf8);
+
+       static char buf[BUFSIZE];
        g_strlcpy(buf, str, sizeof(buf));
        g_free(str);
 
        return buf;
 }
 
-static void
-screen_artist_paint(void);
-
-static void
-artist_repaint(void)
-{
-       screen_artist_paint();
-       wrefresh(browser.lw->w);
-}
-
 static void
 string_array_free(GPtrArray *array)
 {
-       unsigned i;
-
-       for (i = 0; i < array->len; ++i) {
+       for (unsigned i = 0; i < array->len; ++i) {
                char *value = g_ptr_array_index(array, i);
                g_free(value);
        }
@@ -162,8 +149,7 @@ load_artist_list(struct mpdclient *c)
                mpd_search_commit(connection);
                recv_tag_values(connection, MPD_TAG_ARTIST, artist_list);
 
-               if (!mpd_response_finish(connection))
-                       mpdclient_handle_error(c);
+               mpdclient_finish_command(c);
        }
 
        /* sort list */
@@ -193,8 +179,7 @@ load_album_list(struct mpdclient *c)
 
                recv_tag_values(connection, MPD_TAG_ALBUM, album_list);
 
-               if (!mpd_response_finish(connection))
-                       mpdclient_handle_error(c);
+               mpdclient_finish_command(c);
        }
 
        /* sort list */
@@ -228,8 +213,7 @@ load_song_list(struct mpdclient *c)
 
                filelist_recv(browser.filelist, connection);
 
-               if (!mpd_response_finish(connection))
-                       mpdclient_handle_error(c);
+               mpdclient_finish_command(c);
        }
 
        /* fix highlights */
@@ -305,9 +289,10 @@ reload_lists(struct mpdclient *c)
 }
 
 static void
-screen_artist_init(WINDOW *w, int cols, int rows)
+screen_artist_init(WINDOW *w, unsigned cols, unsigned rows)
 {
        browser.lw = list_window_init(w, cols, rows);
+       browser.song_format = options.list_format;
        artist = NULL;
        album = NULL;
 }
@@ -328,7 +313,7 @@ screen_artist_open(struct mpdclient *c)
 }
 
 static void
-screen_artist_resize(int cols, int rows)
+screen_artist_resize(unsigned cols, unsigned rows)
 {
        list_window_resize(browser.lw, cols, rows);
 }
@@ -338,10 +323,10 @@ screen_artist_resize(int cols, int rows)
  */
 static void
 paint_artist_callback(WINDOW *w, unsigned i,
-                     G_GNUC_UNUSED unsigned y, unsigned width,
-                     bool selected, void *data)
+                     gcc_unused unsigned y, unsigned width,
+                     bool selected, const void *data)
 {
-       GPtrArray *list = data;
+       const GPtrArray *list = data;
        char *p = utf8_to_locale(g_ptr_array_index(list, i));
 
        screen_browser_paint_directory(w, width, selected, p);
@@ -356,10 +341,10 @@ paint_artist_callback(WINDOW *w, unsigned i,
  */
 static void
 paint_album_callback(WINDOW *w, unsigned i,
-                    G_GNUC_UNUSED unsigned y, unsigned width,
-                    bool selected, void *data)
+                    gcc_unused unsigned y, unsigned width,
+                    bool selected, const void *data)
 {
-       GPtrArray *list = data;
+       const GPtrArray *list = data;
        const char *p;
        char *q = NULL;
 
@@ -394,9 +379,9 @@ screen_artist_paint(void)
 static const char *
 screen_artist_get_title(char *str, size_t size)
 {
-       char *s1, *s2;
-
        switch(mode) {
+               char *s1, *s2;
+
        case LIST_ARTISTS:
                g_snprintf(str, size, _("All artists"));
                break;
@@ -445,25 +430,24 @@ screen_artist_update(struct mpdclient *c)
                         | MPD_IDLE_QUEUE
 #endif
                         ))
-               artist_repaint();
+               screen_artist_paint();
 }
 
-/* _artist is actually only used in the ALBUM case to distinguish albums with
+/* add_query - Add all songs satisfying specified criteria.
+   _artist is actually only used in the ALBUM case to distinguish albums with
    the same name from different artists. */
 static void
 add_query(struct mpdclient *c, enum mpd_tag_type table, const char *_filter,
          const char *_artist)
 {
        struct mpd_connection *connection = mpdclient_get_connection(c);
-       char *str;
-       struct filelist *addlist;
 
        assert(_filter != NULL);
 
        if (connection == NULL)
                return;
 
-       str = utf8_to_locale(_filter);
+       char *str = utf8_to_locale(_filter);
        if (table == MPD_TAG_ALBUM)
                screen_status_printf(_("Adding album %s..."), str);
        else
@@ -478,12 +462,10 @@ add_query(struct mpdclient *c, enum mpd_tag_type table, const char *_filter,
                                              MPD_TAG_ARTIST, _artist);
        mpd_search_commit(connection);
 
-       addlist = filelist_new_recv(connection);
+       struct filelist *addlist = filelist_new_recv(connection);
 
-       if (mpd_response_finish(connection))
+       if (mpdclient_finish_command(c))
                mpdclient_filelist_add_all(c, addlist);
-       else
-               mpdclient_handle_error(c);
 
        filelist_free(addlist);
 }
@@ -520,13 +502,13 @@ string_array_find(GPtrArray *array, const char *value)
 static bool
 screen_artist_cmd(struct mpdclient *c, command_t cmd)
 {
-       struct list_window_range range;
-       char *selected;
-       char *old;
-       char *old_ptr;
-       int idx;
-
        switch(cmd) {
+               struct list_window_range range;
+               char *selected;
+               char *old;
+               char *old_ptr;
+               int idx;
+
        case CMD_PLAY:
                switch (mode) {
                case LIST_ARTISTS:
@@ -538,7 +520,7 @@ screen_artist_cmd(struct mpdclient *c, command_t cmd)
                        open_album_list(c, g_strdup(selected));
                        list_window_reset(browser.lw);
 
-                       artist_repaint();
+                       screen_artist_paint();
                        return true;
 
                case LIST_ALBUMS:
@@ -568,7 +550,7 @@ screen_artist_cmd(struct mpdclient *c, command_t cmd)
                                list_window_reset(browser.lw);
                        }
 
-                       artist_repaint();
+                       screen_artist_paint();
                        return true;
 
                case LIST_SONGS:
@@ -591,7 +573,7 @@ screen_artist_cmd(struct mpdclient *c, command_t cmd)
                                        list_window_center(browser.lw, idx);
                                }
 
-                               artist_repaint();
+                               screen_artist_paint();
                                return true;
                        }
                        break;
@@ -640,7 +622,7 @@ screen_artist_cmd(struct mpdclient *c, command_t cmd)
                        break;
                }
 
-               artist_repaint();
+               screen_artist_paint();
                break;
 
        case CMD_GO_ROOT_DIRECTORY:
@@ -657,7 +639,7 @@ screen_artist_cmd(struct mpdclient *c, command_t cmd)
                        break;
                }
 
-               artist_repaint();
+               screen_artist_paint();
                break;
 
        case CMD_SELECT:
@@ -709,13 +691,13 @@ screen_artist_cmd(struct mpdclient *c, command_t cmd)
                case LIST_ARTISTS:
                        screen_find(browser.lw, cmd,
                                    screen_artist_lw_callback, artist_list);
-                       artist_repaint();
+                       screen_artist_paint();
                        return true;
 
                case LIST_ALBUMS:
                        screen_find(browser.lw, cmd,
                                    screen_artist_lw_callback, album_list);
-                       artist_repaint();
+                       screen_artist_paint();
                        return true;
 
                case LIST_SONGS:
@@ -725,15 +707,17 @@ screen_artist_cmd(struct mpdclient *c, command_t cmd)
        case CMD_LIST_JUMP:
                switch (mode) {
                case LIST_ARTISTS:
-                       screen_jump(browser.lw, screen_artist_lw_callback,
+                       screen_jump(browser.lw,
+                                   screen_artist_lw_callback, artist_list,
                                    paint_artist_callback, artist_list);
-                       artist_repaint();
+                       screen_artist_paint();
                        return true;
 
                case LIST_ALBUMS:
-                       screen_jump(browser.lw, screen_artist_lw_callback,
+                       screen_jump(browser.lw,
+                                   screen_artist_lw_callback, album_list,
                                    paint_album_callback, album_list);
-                       artist_repaint();
+                       screen_artist_paint();
                        return true;
 
                case LIST_SONGS:
@@ -749,12 +733,27 @@ screen_artist_cmd(struct mpdclient *c, command_t cmd)
 
        if (screen_artist_lw_cmd(c, cmd)) {
                if (screen_is_visible(&screen_artist))
-                       artist_repaint();
+                       screen_artist_paint();
+               return true;
+       }
+
+       return false;
+}
+
+#ifdef HAVE_GETMOUSE
+static bool
+screen_artist_mouse(struct mpdclient *c, int x, int y, mmask_t bstate)
+{
+       if (browser_mouse(&browser, c, x, y, bstate)) {
+               if (screen_is_visible(&screen_artist))
+                       screen_artist_paint();
+
                return true;
        }
 
        return false;
 }
+#endif
 
 const struct screen_functions screen_artist = {
        .init = screen_artist_init,
@@ -764,5 +763,8 @@ const struct screen_functions screen_artist = {
        .paint = screen_artist_paint,
        .update = screen_artist_update,
        .cmd = screen_artist_cmd,
+#ifdef HAVE_GETMOUSE
+       .mouse = screen_artist_mouse,
+#endif
        .get_title = screen_artist_get_title,
 };