Code

screen: moved list_window mouse code to list_window.c
authorMax Kellermann <max@duempel.org>
Mon, 22 Sep 2008 08:04:46 +0000 (10:04 +0200)
committerMax Kellermann <max@duempel.org>
Mon, 22 Sep 2008 08:04:46 +0000 (10:04 +0200)
Move the portion of screen_get_mouse_event() which handles list_window
clicks to list_window.c.  The code contained a NULL pointer
dereference, which is now fixed.

src/list_window.c
src/list_window.h
src/screen.c
src/screen.h
src/screen_browser.c
src/screen_play.c

index 5ab8f9c9c125c5ecc628fd050a6de932de327fb8..215a531dd4907a8bcba373c084db1c7706182f55 100644 (file)
@@ -25,6 +25,7 @@
 #include "command.h"
 #include "colors.h"
 
+#include <assert.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <string.h>
@@ -362,6 +363,35 @@ list_window_scroll_cmd(struct list_window *lw, unsigned rows, command_t cmd)
        return 1;
 }
 
+#ifdef HAVE_GETMOUSE
+int
+list_window_mouse(struct list_window *lw, unsigned rows,
+                 unsigned long bstate, int y)
+{
+       assert(lw != NULL);
+
+       /* if the even occured above the list window move up */
+       if (y < 0) {
+               if (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)y >= rows) {
+               if (bstate & BUTTON3_CLICKED)
+                       list_window_last(lw, rows);
+               else
+                       list_window_next_page(lw, rows);
+               return 1;
+       }
+
+       return 0;
+}
+#endif
+
 list_window_state_t *
 list_window_init_state(void)
 {
index ca93971dde2aa5146242a1b1f9c2ed79a96a804e..78593948add8eae7438f33ced05e8278d40c0fdf 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef LIST_WINDOW_H
 #define LIST_WINDOW_H
 
+#include "../config.h"
 #include "command.h"
 
 #include <ncurses.h>
@@ -56,6 +57,16 @@ int list_window_cmd(struct list_window *lw, unsigned rows, command_t cmd);
 int
 list_window_scroll_cmd(struct list_window *lw, unsigned rows, command_t cmd);
 
+#ifdef HAVE_GETMOUSE
+/**
+ * The mouse was clicked.  Check if the list should be scrolled
+ * Returns non-zero if the mouse event has been handled.
+ */
+int
+list_window_mouse(struct list_window *lw, unsigned rows,
+                 unsigned long bstate, int y);
+#endif
+
 void
 list_window_center(struct list_window *lw, unsigned rows, unsigned n);
 
index 92ce29e484a786dd91f5c5087522c7bfd3292014..a76438b9c6f9b095f016fbaea11d6f364b5cf6c2 100644 (file)
@@ -737,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;
 
@@ -756,24 +754,6 @@ 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
index ca0dd2a0cdccfefe6e5c941de20a1cb33d9c337e..6f98d1e4d5a238f4b6db32f0a28c2b807a3ae4fe 100644 (file)
@@ -13,8 +13,6 @@
 
 #define MAX_SONGNAME_LENGTH   512
 
-struct list_window;
-
 struct window {
        WINDOW *w;
        int rows, cols;
@@ -83,8 +81,6 @@ gint screen_get_id(const char *name);
 
 
 gint get_cur_mode_id(void);
-int screen_get_mouse_event(mpdclient_t *c,
-                          struct list_window *lw, int lw_length, 
-                          unsigned long *bstate, int *row);
+int screen_get_mouse_event(mpdclient_t *c, unsigned long *bstate, int *row);
 
 #endif
index 4c8bc2ed6cccfd5421a36d51af7aff9be09bdc45..bbf69c5227aa3d3d44bd8033701171f9fd8dbe15 100644 (file)
@@ -423,7 +423,8 @@ browser_handle_mouse_event(struct screen_browser *browser, mpdclient_t *c)
        else
                length = 0;
 
-       if( screen_get_mouse_event(c, browser->lw, length, &bstate, &row) )
+       if (screen_get_mouse_event(c, &bstate, &row) ||
+           list_window_mouse(browser->lw, length, bstate, row))
                return 1;
 
        browser->lw->selected = browser->lw->start + row;
index cd43982c334de9a4252e4b7cba546c3cd7f24dcf..11d4754783d15c855bda56c9e35bdf3f8433eff0 100644 (file)
@@ -422,7 +422,8 @@ handle_mouse_event(mpd_unused screen_t *screen, mpdclient_t *c)
        unsigned selected;
        unsigned long bstate;
 
-       if (screen_get_mouse_event(c, lw, c->playlist.list->len, &bstate, &row))
+       if (screen_get_mouse_event(c, &bstate, &row) ||
+           list_window_mouse(lw, c->playlist.list->len, bstate, row))
                return 1;
 
        if (bstate & BUTTON1_DOUBLE_CLICKED) {