From 4141aa9455e4b4a5b4a235475c70e8c100ec663e Mon Sep 17 00:00:00 2001
From: nullsystem <nullsystem.aongp@slmail.me>
Date: Sat, 6 Apr 2024 02:03:49 +0100
Subject: [PATCH] buttonbystate - allow config for release (and press)

- Adds "state" (enum wlr_button_state) to configure a button action on
  either press or release. This basically enables release to be used
  for button actions.
---
 config.def.h |  6 +++---
 dwl.c        | 22 ++++++++++++----------
 2 files changed, 15 insertions(+), 13 deletions(-)

diff --git a/config.def.h b/config.def.h
index 8847e58..cc989cf 100644
--- a/config.def.h
+++ b/config.def.h
@@ -165,7 +165,7 @@ static const Key keys[] = {
 };
 
 static const Button buttons[] = {
-	{ MODKEY, BTN_LEFT,   moveresize,     {.ui = CurMove} },
-	{ MODKEY, BTN_MIDDLE, togglefloating, {0} },
-	{ MODKEY, BTN_RIGHT,  moveresize,     {.ui = CurResize} },
+	{ MODKEY, BTN_LEFT,   moveresize,     {.ui = CurMove},   WLR_BUTTON_PRESSED },
+	{ MODKEY, BTN_MIDDLE, togglefloating, {0},               WLR_BUTTON_PRESSED },
+	{ MODKEY, BTN_RIGHT,  moveresize,     {.ui = CurResize}, WLR_BUTTON_PRESSED },
 };
diff --git a/dwl.c b/dwl.c
index bf763df..6b60ccf 100644
--- a/dwl.c
+++ b/dwl.c
@@ -99,6 +99,7 @@ typedef struct {
 	unsigned int button;
 	void (*func)(const Arg *);
 	const Arg arg;
+	enum wlr_button_state state;
 } Button;
 
 typedef struct Monitor Monitor;
@@ -595,16 +596,6 @@ buttonpress(struct wl_listener *listener, void *data)
 		xytonode(cursor->x, cursor->y, NULL, &c, NULL, NULL, NULL);
 		if (c && (!client_is_unmanaged(c) || client_wants_focus(c)))
 			focusclient(c, 1);
-
-		keyboard = wlr_seat_get_keyboard(seat);
-		mods = keyboard ? wlr_keyboard_get_modifiers(keyboard) : 0;
-		for (b = buttons; b < END(buttons); b++) {
-			if (CLEANMASK(mods) == CLEANMASK(b->mod) &&
-					event->button == b->button && b->func) {
-				b->func(&b->arg);
-				return;
-			}
-		}
 		break;
 	case WLR_BUTTON_RELEASED:
 		held_grab = NULL;
@@ -622,6 +613,17 @@ buttonpress(struct wl_listener *listener, void *data)
 		}
 		break;
 	}
+
+	keyboard = wlr_seat_get_keyboard(seat);
+	mods = keyboard ? wlr_keyboard_get_modifiers(keyboard) : 0;
+	for (b = buttons; b < END(buttons); b++) {
+		if (b->state == event->state && CLEANMASK(mods) == CLEANMASK(b->mod) &&
+				event->button == b->button && b->func) {
+			b->func(&b->arg);
+			return;
+		}
+	}
+
 	/* If the event wasn't handled by the compositor, notify the client with
 	 * pointer focus that a button press has occurred */
 	wlr_seat_pointer_notify_button(seat,
-- 
2.44.0

