commit e200d2a

uint  ·  2026-06-09 19:22:29 +0000 UTC
parent 92fc832
Track mouse button pressed? state
4 files changed,  +36, -5
M maus.h
M maus.h
+2, -2
 1@@ -74,8 +74,8 @@ typedef struct {
 2 
 3 		struct {
 4 			struct {
 5-				uint8_t  button;
 6-				bool     pressed;
 7+				MausMouseButton button;
 8+				bool            pressed;
 9 			} button;
10 
11 			struct {
+9, -0
 1@@ -65,6 +65,15 @@ typedef enum {
 2 	MAUS_CURSOR_STATE_FREE,
 3 } MausCursorState;
 4 
 5+typedef enum {
 6+	MAUS_MOUSE_BUTTON_NONE = 0,
 7+	MAUS_MOUSE_BUTTON_LEFT,
 8+	MAUS_MOUSE_BUTTON_MIDDLE,
 9+	MAUS_MOUSE_BUTTON_RIGHT,
10+	MAUS_MOUSE_BUTTON_SCROLL_UP,
11+	MAUS_MOUSE_BUTTON_SCROLL_DOWN,
12+	MAUS_MOUSE_BUTTON_LAST,
13+} MausMouseButton;
14 
15 #endif /* MAUS_KEYS_H */
16 
+24, -3
 1@@ -1,4 +1,3 @@
 2-#include <X11/X.h>
 3 #include <stdbool.h>
 4 #include <stdlib.h>
 5 #include <sys/shm.h>
 6@@ -11,6 +10,7 @@
 7 #include <X11/extensions/XShm.h>
 8 
 9 #include "maus.h"
10+#include "maus_input.h"
11 
12 typedef struct {
13 	KeySym  x11;
14@@ -53,6 +53,7 @@ static bool fb_create_shm(Maus* mw);
15 static void fb_destroy(Maus* mw);
16 static bool handle_event(XEvent* xev, MausEvent* ev, Maus* mw);
17 static MausKey keysym_to_mauskey(KeySym sym);
18+static MausMouseButton mouse_button_to_maus(int btn);
19 static int xerr(Display* dpy, XErrorEvent* ev);
20 
21 static void build_keymap(Maus* mw)
22@@ -217,6 +218,8 @@ static void fb_destroy(Maus* mw)
23 static bool handle_event(XEvent* xev, MausEvent* ev, Maus* mw)
24 {
25 	uint32_t code = 0;
26+	unsigned int mb;
27+	MausMouseButton mbtype;
28 	MausKey key = MAUS_KEY_NONE;
29 
30 	switch (xev->type) {
31@@ -260,14 +263,20 @@ static bool handle_event(XEvent* xev, MausEvent* ev, Maus* mw)
32 
33 		case ButtonPress:
34 			ev->type = MAUS_EV_MOUSE_BUTTON;
35-			ev->mouse.button.button = xev->xbutton.button;
36+			mb = xev->xbutton.button;
37+			mbtype = mouse_button_to_maus(mb);
38+			ev->mouse.button.button = mbtype;
39 			ev->mouse.button.pressed = true;
40+			mw->mouse_buttons[mbtype] = true;
41 			return true;
42 
43 		case ButtonRelease:
44 			ev->type = MAUS_EV_MOUSE_BUTTON;
45-			ev->mouse.button.button = xev->xbutton.button;
46+			mb = xev->xbutton.button;
47+			mbtype = mouse_button_to_maus(mb);
48+			ev->mouse.button.button = mbtype;
49 			ev->mouse.button.pressed = false;
50+			mw->mouse_buttons[mbtype] = false;
51 			return true;
52 
53 		case MotionNotify:
54@@ -435,6 +444,18 @@ bool maus_event_poll(Maus* mw, MausEvent* ev)
55 	return false;
56 }
57 
58+static MausMouseButton mouse_button_to_maus(int btn)
59+{
60+	switch (btn) {
61+		case Button1: return MAUS_MOUSE_BUTTON_LEFT;
62+		case Button2: return MAUS_MOUSE_BUTTON_MIDDLE;
63+		case Button3: return MAUS_MOUSE_BUTTON_RIGHT;
64+		case Button4: return MAUS_MOUSE_BUTTON_SCROLL_UP;
65+		case Button5: return MAUS_MOUSE_BUTTON_SCROLL_DOWN;
66+		default:      return MAUS_MOUSE_BUTTON_NONE;
67+	}
68+}
69+
70 static int xerr(Display* dpy, XErrorEvent* ev)
71 {
72 	(void) dpy;
+1, -0
1@@ -40,6 +40,7 @@ typedef struct {
2 	MausKey        keymap[MAUS_KEYCODE_LAST];    /* X11 keycode->MausKey */
3 
4 	MausCursor     cursor;
5+	bool           mouse_buttons[MAUS_MOUSE_BUTTON_LAST];
6 } Maus;
7 
8 #endif /* MAUS_X11_H */