window: Move button handler to widget
diff --git a/clients/desktop-shell.c b/clients/desktop-shell.c
index 9e46cc1..3aa535b 100644
--- a/clients/desktop-shell.c
+++ b/clients/desktop-shell.c
@@ -244,24 +244,29 @@
 }
 
 static void
-panel_button_handler(struct window *window,
-		     struct input *input, uint32_t time,
-		     int button, int state, void *data)
+panel_widget_button_handler(struct widget *widget,
+			    struct input *input, uint32_t time,
+			    int button, int state, void *data)
 {
 	struct panel *panel = data;
 	struct panel_widget *pi;
-	struct widget *focus;
 
-	focus = window_get_focus_widget(panel->window);
-	if (focus && button == BTN_LEFT) {
-		pi = widget_get_user_data(focus);
-		window_schedule_redraw(panel->window);
-		if (state == 0)
-			panel_activate_widget(panel, pi);
-	} else if (button == BTN_RIGHT) {
-		if (state)
-			show_menu(panel, input, time);
-	}
+	pi = widget_get_user_data(widget);
+	widget_schedule_redraw(widget);
+	if (state == 0)
+		panel_activate_widget(panel, pi);
+}
+
+static void
+panel_button_handler(struct widget *widget,
+		     struct input *input, uint32_t time,
+		     int button, int state, void *data)
+{
+	struct window *window = data;
+	struct panel *panel = window_get_user_data(window);
+
+	if (button == BTN_RIGHT && state)
+		show_menu(panel, input, time);
 }
 
 static void
@@ -291,7 +296,9 @@
 	window_set_redraw_handler(panel->window, panel_redraw_handler);
 	window_set_custom(panel->window);
 	window_set_user_data(panel->window, panel);
-	window_set_button_handler(panel->window, panel_button_handler);
+
+	widget_set_button_handler(window_get_widget(panel->window),
+				  panel_button_handler);
 
 	return panel;
 }
@@ -310,6 +317,7 @@
 	widget->widget = window_add_widget(panel->window, widget);
 	widget_set_enter_handler(widget->widget, panel_widget_enter_handler);
 	widget_set_leave_handler(widget->widget, panel_widget_leave_handler);
+	widget_set_button_handler(widget->widget, panel_widget_button_handler);
 }
 
 static void
@@ -414,16 +422,14 @@
 }
 
 static void
-unlock_dialog_button_handler(struct window *window,
+unlock_dialog_button_handler(struct widget *widget,
 			     struct input *input, uint32_t time,
 			     int button, int state, void *data)
 {
 	struct unlock_dialog *dialog = data;
 	struct desktop *desktop = dialog->desktop;
-	struct widget *focus;
 
-	focus = window_get_focus_widget(dialog->window);
-	if (focus && button == BTN_LEFT) {
+	if (button == BTN_LEFT) {
 		if (state == 0 && !dialog->closing) {
 			display_defer(desktop->display, &desktop->unlock_task);
 			dialog->closing = 1;
@@ -480,12 +486,13 @@
 	window_set_redraw_handler(dialog->window, unlock_dialog_redraw_handler);
 	window_set_keyboard_focus_handler(dialog->window,
 					  unlock_dialog_keyboard_focus_handler);
-	window_set_button_handler(dialog->window, unlock_dialog_button_handler);
 	dialog->button = window_add_widget(dialog->window, NULL);
 	widget_set_enter_handler(dialog->button,
 				 unlock_dialog_widget_enter_handler);
 	widget_set_leave_handler(dialog->button,
 				 unlock_dialog_widget_leave_handler);
+	widget_set_button_handler(dialog->button,
+				  unlock_dialog_button_handler);
 
 	desktop_shell_set_lock_surface(desktop->shell,
 	       window_get_wl_shell_surface(dialog->window));
diff --git a/clients/dnd.c b/clients/dnd.c
index c557e71..f8cd7ef 100644
--- a/clients/dnd.c
+++ b/clients/dnd.c
@@ -361,11 +361,12 @@
 }
 
 static void
-dnd_button_handler(struct window *window,
+dnd_button_handler(struct widget *widget,
 		   struct input *input, uint32_t time,
 		   int button, int state, void *data)
 {
-	struct dnd *dnd = data;
+	struct window *window = data;
+	struct dnd *dnd = window_get_user_data(window);
 	int32_t x, y;
 	struct item *item;
 	struct rectangle allocation;
@@ -537,13 +538,13 @@
 	window_set_redraw_handler(dnd->window, redraw_handler);
 	window_set_keyboard_focus_handler(dnd->window,
 					  keyboard_focus_handler);
-	window_set_button_handler(dnd->window, dnd_button_handler);
 	window_set_data_handler(dnd->window, dnd_data_handler);
 	window_set_drop_handler(dnd->window, dnd_drop_handler);
 
 	widget = window_get_widget(dnd->window);
 	widget_set_enter_handler(widget, dnd_enter_handler);
 	widget_set_motion_handler(widget, dnd_motion_handler);
+	widget_set_button_handler(widget, dnd_button_handler);
 
 	width = 4 * (item_width + item_padding) + item_padding;
 	height = 4 * (item_height + item_padding) + item_padding;
diff --git a/clients/eventdemo.c b/clients/eventdemo.c
index 34a4dfa..54daf77 100644
--- a/clients/eventdemo.c
+++ b/clients/eventdemo.c
@@ -222,7 +222,7 @@
  * \param data user data associated to the window
  */
 static void
-button_handler(struct window *window, struct input *input, uint32_t time,
+button_handler(struct widget *widget, struct input *input, uint32_t time,
 	       int button, int state, void *data)
 {
 	int32_t x, y;
@@ -314,7 +314,8 @@
 	window_set_key_handler(e->window, key_handler);
 
 	/* Set the callback button handler for the window */
-	window_set_button_handler(e->window, button_handler);
+	widget_set_button_handler(window_get_widget(e->window),
+				  button_handler);
 
 	/* Set the callback motion handler for the window */
 	widget_set_motion_handler(window_get_widget(e->window),
diff --git a/clients/flower.c b/clients/flower.c
index 4b6a872..18e2015 100644
--- a/clients/flower.c
+++ b/clients/flower.c
@@ -105,10 +105,12 @@
 }
 
 static void
-button_handler(struct window *window,
+button_handler(struct widget *widget,
 	       struct input *input, uint32_t time,
 	       int button, int state, void *data)
 {
+	struct window *window = data;
+
 	if (state)
 		window_move(window, input, time);
 }
@@ -156,7 +158,8 @@
 
 	widget_set_motion_handler(window_get_widget(flower.window),
 				  motion_handler);
-	window_set_button_handler(flower.window, button_handler);
+	widget_set_button_handler(window_get_widget(flower.window),
+				  button_handler);
 	window_set_user_data(flower.window, &flower);
 	display_run(d);
 
diff --git a/clients/resizor.c b/clients/resizor.c
index 1037824..2818e26 100644
--- a/clients/resizor.c
+++ b/clients/resizor.c
@@ -190,11 +190,12 @@
 }
 
 static void
-button_handler(struct window *window,
+button_handler(struct widget *widget,
 	       struct input *input, uint32_t time,
 	       int button, int state, void *data)
 {
-	struct resizor *resizor = data;
+	struct window *window = data;
+	struct resizor *resizor = window_get_user_data(window);
 
 	switch (button) {
 	case BTN_RIGHT:
@@ -232,7 +233,8 @@
 	height = resizor->height.current + 0.5;
 
 	window_set_child_size(resizor->window, resizor->width, height);
-	window_set_button_handler(resizor->window, button_handler);
+	widget_set_button_handler(window_get_widget(resizor->window),
+				  button_handler);
 
 	resizor_draw(resizor);
 
diff --git a/clients/tablet-shell.c b/clients/tablet-shell.c
index 0de35ce..479aa88 100644
--- a/clients/tablet-shell.c
+++ b/clients/tablet-shell.c
@@ -198,11 +198,12 @@
 }
 
 static void
-lockscreen_button_handler(struct window *window,
+lockscreen_button_handler(struct widget *widget,
 			  struct input *input, uint32_t time,
 			  int button, int state, void *data)
 {
-	struct tablet_shell *shell = data;
+	struct window *window = data;
+	struct tablet_shell *shell = window_get_user_data(window);
 
 	window_destroy(shell->lockscreen);
 	shell->lockscreen = NULL;
@@ -219,7 +220,7 @@
 	window_set_user_data(shell->lockscreen, shell);
 	window_set_decoration(shell->lockscreen, 0);
 	window_set_custom(shell->lockscreen);
-	window_set_button_handler(shell->lockscreen,
+	widget_set_button_handler(window_get_widget(shell->lockscreen),
 				  lockscreen_button_handler);
 
 	tablet_shell_set_lockscreen(shell->tablet_shell,
diff --git a/clients/terminal.c b/clients/terminal.c
index c83b630..9ec8190 100644
--- a/clients/terminal.c
+++ b/clients/terminal.c
@@ -2224,11 +2224,12 @@
 }
 
 static void
-button_handler(struct window *window,
+button_handler(struct widget *widget,
 	       struct input *input, uint32_t time,
 	       int button, int state, void *data)
 {
-	struct terminal *terminal = data;
+	struct window *window = data;
+	struct terminal *terminal = window_get_user_data(window);
 
 	switch (button) {
 	case 272:
@@ -2298,7 +2299,8 @@
 	window_set_key_handler(terminal->window, key_handler);
 	window_set_keyboard_focus_handler(terminal->window,
 					  keyboard_focus_handler);
-	window_set_button_handler(terminal->window, button_handler);
+	widget_set_button_handler(window_get_widget(terminal->window),
+				  button_handler);
 
 	widget_set_motion_handler(window_get_widget(terminal->window),
 				  motion_handler);
diff --git a/clients/view.c b/clients/view.c
index 9d8a99f..1a345e6 100644
--- a/clients/view.c
+++ b/clients/view.c
@@ -154,7 +154,8 @@
 button_handler(struct window *window, struct input *input, uint32_t time,
                int button, int state, void *data)
 {
-        struct view *view = data;
+	struct window *window = data;
+a        struct view *view = window_get_user_data(window);
 
         if(!state)
                 return;
@@ -252,7 +253,8 @@
 	window_set_key_handler(view->window, key_handler);
 	window_set_keyboard_focus_handler(view->window,
 					  keyboard_focus_handler);
-	window_set_button_handler(view->window, button_handler);
+	widget_set_button_handler(window_get_widget(view->window),
+				  button_handler);
 	view->page = 0;
 
 	view->fullscreen = fullscreen;
diff --git a/clients/window.c b/clients/window.c
index 6e92e76..70642a4 100644
--- a/clients/window.c
+++ b/clients/window.c
@@ -130,7 +130,6 @@
 	window_resize_handler_t resize_handler;
 	window_redraw_handler_t redraw_handler;
 	window_key_handler_t key_handler;
-	window_button_handler_t button_handler;
 	window_keyboard_focus_handler_t keyboard_focus_handler;
 	window_data_handler_t data_handler;
 	window_drop_handler_t drop_handler;
@@ -153,6 +152,7 @@
 	widget_enter_handler_t enter_handler;
 	widget_leave_handler_t leave_handler;
 	widget_motion_handler_t motion_handler;
+	widget_button_handler_t button_handler;
 	void *user_data;
 };
 
@@ -1127,6 +1127,13 @@
 }
 
 void
+widget_set_button_handler(struct widget *widget,
+			  widget_button_handler_t handler)
+{
+	widget->button_handler = handler;
+}
+
+void
 widget_schedule_redraw(struct widget *widget)
 {
 	window_schedule_redraw(widget->window);
@@ -1349,6 +1356,7 @@
 
 	location = get_pointer_location(window, input->sx, input->sy);
 
+	widget = window->focus_widget;
 	if (window->display->shell && button == BTN_LEFT && state == 1) {
 		switch (location) {
 		case WINDOW_TITLEBAR:
@@ -1373,11 +1381,11 @@
 						location);
 			break;
 		case WINDOW_CLIENT_AREA:
-			if (window->button_handler)
-				(*window->button_handler)(window,
+			if (widget && widget->button_handler)
+				(*widget->button_handler)(widget,
 							  input, time,
 							  button, state,
-							  window->user_data);
+							  widget->user_data);
 			break;
 		}
 	} else if (button == BTN_RIGHT && state == 1) {
@@ -1393,19 +1401,19 @@
 			window_schedule_redraw(window->menu);
 			break;
 		case WINDOW_CLIENT_AREA:
-			if (window->button_handler)
-				(*window->button_handler)(window,
+			if (widget && widget->button_handler)
+				(*widget->button_handler)(widget,
 							  input, time,
 							  button, state,
-							  window->user_data);
+							  widget->user_data);
 			break;
 		}
 	} else {
-		if (window->button_handler)
-			(*window->button_handler)(window,
+		if (widget && widget->button_handler)
+			(*widget->button_handler)(widget,
 						  input, time,
 						  button, state,
-						  window->user_data);
+						  widget->user_data);
 	}
 
 	if (window->focus_widget &&
@@ -2043,13 +2051,6 @@
 }
 
 void
-window_set_button_handler(struct window *window,
-			  window_button_handler_t handler)
-{
-	window->button_handler = handler;
-}
-
-void
 window_set_keyboard_focus_handler(struct window *window,
 				  window_keyboard_focus_handler_t handler)
 {
@@ -2241,12 +2242,13 @@
 }
 
 static void
-menu_button_handler(struct window *window,
+menu_button_handler(struct widget *widget,
 		    struct input *input, uint32_t time,
 		    int button, int state, void *data)
 
 {
-	struct menu *menu = data;
+	struct window *window = data;
+	struct menu *menu = window_get_user_data(window);
 
 	/* Either relase after press-drag-release or click-motion-click. */
 	if (state == 0 && time - menu->time > 500) {
@@ -2333,13 +2335,13 @@
 				   window->parent->shell_surface,
 				   window->x, window->y, 0);
 
-	window_set_button_handler(window, menu_button_handler);
 	window_set_redraw_handler(window, menu_redraw_handler);
 	window_set_user_data(window, menu);
 
 	widget_set_enter_handler(window->widget, menu_enter_handler);
 	widget_set_leave_handler(window->widget, menu_leave_handler);
 	widget_set_motion_handler(window->widget, menu_motion_handler);
+	widget_set_button_handler(window->widget, menu_button_handler);
 
 	return window;
 }
diff --git a/clients/window.h b/clients/window.h
index cf1b0bc..97a1c6a 100644
--- a/clients/window.h
+++ b/clients/window.h
@@ -168,15 +168,6 @@
 typedef void (*window_keyboard_focus_handler_t)(struct window *window,
 						struct input *device, void *data);
 
-typedef void (*window_button_handler_t)(struct window *window,
-					struct input *input, uint32_t time,
-					int button, int state, void *data);
-
-typedef int (*window_motion_handler_t)(struct window *window,
-				       struct input *input, uint32_t time,
-				       int32_t x, int32_t y,
-				       int32_t sx, int32_t sy, void *data);
-
 typedef void (*window_data_handler_t)(struct window *window,
 				      struct input *input, uint32_t time,
 				      int32_t x, int32_t y,
@@ -197,6 +188,9 @@
 typedef int (*widget_motion_handler_t)(struct widget *widget,
 				       struct input *input, uint32_t time,
 				       int32_t x, int32_t y, void *data);
+typedef void (*widget_button_handler_t)(struct widget *widget,
+					struct input *input, uint32_t time,
+					int button, int state, void *data);
 
 struct window *
 window_create(struct display *display, int32_t width, int32_t height);
@@ -317,14 +311,6 @@
 		       window_key_handler_t handler);
 
 void
-window_set_button_handler(struct window *window,
-			  window_button_handler_t handler);
-
-void
-window_set_motion_handler(struct window *window,
-			  window_motion_handler_t handler);
-
-void
 window_set_keyboard_focus_handler(struct window *window,
 				  window_keyboard_focus_handler_t handler);
 
@@ -366,6 +352,10 @@
 widget_set_motion_handler(struct widget *widget,
 			  widget_motion_handler_t handler);
 void
+widget_set_button_handler(struct widget *widget,
+			  widget_button_handler_t handler);
+
+void
 widget_schedule_redraw(struct widget *widget);
 
 void