commit 657d974

uint  ·  2026-06-12 16:43:25 +0000 UTC
parent ffec88f
Store translated text output to key.text attribute, remove translated keymap
2 files changed,  +34, -60
M maus.h
M maus.h
+1, -1
 1@@ -70,6 +70,7 @@ typedef struct {
 2 		struct {
 3 			uint32_t  code; /* raw, backend keycode */
 4 			MausKey   key;  /* logical, mapped key */
 5+			char      text; /* translated text from key */
 6 			bool      pressed;
 7 		} key;
 8 
 9@@ -109,7 +110,6 @@ typedef struct {
10 
11 	bool           key_codes[MAUS_KEYCODE_LAST]; /* physical keys */
12 	bool           key_syms[MAUS_KEY_LAST];      /* logical keys */
13-	MausKey        keymap[MAUS_KEYCODE_LAST];    /* X11 keycode->MausKey */
14 
15 	MausCursor     cursor;
16 	bool           mouse_buttons[MAUS_MOUSE_BUTTON_LAST];
+33, -59
  1@@ -21,7 +21,6 @@ typedef struct {
  2 	MausKey maus;
  3 } KeyMapEntry;
  4 
  5-static void build_keymap(Maus* mw);
  6 static bool fb_create(Maus* mw);
  7 static bool fb_create_shm(Maus* mw);
  8 /* TODO? static bool fb_create_ximage(Maus* mw); */
  9@@ -60,42 +59,6 @@ static const KeyMapEntry keymap[] = {
 10 
 11 static int xerrored;
 12 
 13-static void build_keymap(Maus* mw)
 14-{
 15-	MausBackend* be = &mw->backend;
 16-	int min_code = 0;
 17-	int max_code = 0;
 18-	int syms_per_code = 0;
 19-
 20-	/* can be called multiple times during lieftime
 21-	   so initial calloc may not be enough */
 22-	for (int i = 0; i < MAUS_KEYCODE_LAST; i++)
 23-		mw->keymap[i] = MAUS_KEY_NONE;
 24-
 25-	XDisplayKeycodes(be->display, &min_code, &max_code);
 26-	KeySym* syms = XGetKeyboardMapping(
 27-		be->display, min_code,
 28-		max_code - min_code + 1, &syms_per_code
 29-	);
 30-	if (!syms)
 31-		return;
 32-	if (syms_per_code <= 0) {
 33-		XFree(syms);
 34-		return;
 35-	}
 36-
 37-	for (int code = min_code; code <= max_code && code < MAUS_KEYCODE_LAST; code++) {
 38-		for (int col = 0; col < syms_per_code; col++) {
 39-			KeySym sym = syms[(code - min_code) * syms_per_code + col];
 40-			mw->keymap[code] = keysym_to_mauskey(sym);
 41-			if (mw->keymap[code] != MAUS_KEY_NONE)
 42-				break;
 43-		}
 44-	}
 45-
 46-	XFree(syms);
 47-}
 48-
 49 static bool fb_create(Maus* mw)
 50 {
 51 	MausBackend* be = &mw->backend;
 52@@ -228,7 +191,6 @@ static bool handle_event(XEvent* xev, MausEvent* ev, Maus* mw)
 53 	uint32_t code = 0;
 54 	unsigned int mb;
 55 	MausMouseButton mbtype;
 56-	MausKey key = MAUS_KEY_NONE;
 57 
 58 	switch (xev->type) {
 59 		case ClientMessage:
 60@@ -240,34 +202,51 @@ static bool handle_event(XEvent* xev, MausEvent* ev, Maus* mw)
 61 
 62 		case MappingNotify:
 63 			XRefreshKeyboardMapping(&xev->xmapping);
 64-			build_keymap(mw);
 65+			return true;
 66 			break;
 67 
 68-		case KeyPress:
 69+		case KeyPress: {
 70 			ev->type = MAUS_EV_KEY;
 71 			code = xev->xkey.keycode;
 72-			key = code < MAUS_KEYCODE_LAST ? mw->keymap[code] : MAUS_KEY_NONE;
 73 			ev->key.code = code;
 74-			ev->key.key = key;
 75 			ev->key.pressed = true;
 76 			if (code < MAUS_KEYCODE_LAST)
 77-				mw->key_codes[code] = true;
 78-			if (key != MAUS_KEY_NONE)
 79-				mw->key_syms[key] = true;
 80+			        mw->key_codes[code] = true;
 81+
 82+			/* grab the raw, base sym */
 83+			KeySym sym = XLookupKeysym(&xev->xkey, 0);
 84+			ev->key.key = keysym_to_mauskey(sym);
 85+			if (ev->key.key != MAUS_KEY_NONE)
 86+			        mw->key_syms[ev->key.key] = true;
 87+
 88+			char buf[8] = {0};
 89+			int len = XLookupString(&xev->xkey, buf, sizeof(buf), NULL, NULL);
 90+			if (len > 0)
 91+			        ev->key.text = (buf[0] == '\r') ? '\n' : buf[0];
 92+			else
 93+			        ev->key.text = 0;
 94+
 95 			return true;
 96+		}
 97 
 98-		case KeyRelease:
 99+		case KeyRelease: {
100 			ev->type = MAUS_EV_KEY;
101 			code = xev->xkey.keycode;
102-			key = code < MAUS_KEYCODE_LAST ? mw->keymap[code] : MAUS_KEY_NONE;
103 			ev->key.code = code;
104-			ev->key.key = key;
105 			ev->key.pressed = false;
106+			ev->key.text = 0;
107+
108 			if (code < MAUS_KEYCODE_LAST)
109-				mw->key_codes[code] = false;
110-			if (key != MAUS_KEY_NONE)
111-				mw->key_syms[key] = false;
112+			        mw->key_codes[code] = false;
113+
114+			/* unset base sym set on `KeyPress` */
115+			KeySym sym = XLookupKeysym(&xev->xkey, 0);
116+			ev->key.key = keysym_to_mauskey(sym);
117+			if (ev->key.key != MAUS_KEY_NONE)
118+			        mw->key_syms[ev->key.key] = false;
119+
120 			return true;
121+		}
122 
123 		case ButtonPress:
124 			ev->type = MAUS_EV_MOUSE_BUTTON;
125@@ -341,12 +320,9 @@ static bool handle_event(XEvent* xev, MausEvent* ev, Maus* mw)
126 
127 static MausKey keysym_to_mauskey(KeySym sym)
128 {
129-	if (sym >= XK_0 && sym <= XK_9)
130-		return MAUS_KEY_0 + (sym - XK_0);
131-	if (sym >= XK_A && sym <= XK_Z)
132-		return MAUS_KEY_A + (sym - XK_A);
133-	if (sym >= XK_a && sym <= XK_z)
134-		return MAUS_KEY_A + (sym - XK_a);
135+	if (sym >= 0x20 && sym <= 0x7E)
136+		return (MausKey)sym;
137+
138 	if (sym >= XK_F1 && sym <= XK_F12)
139 		return MAUS_KEY_F1 + (sym - XK_F1);
140 	if (sym >= XK_KP_0 && sym <= XK_KP_9)
141@@ -557,8 +533,6 @@ Maus* maus_init(const char* title, int x, int y, int width, int height)
142 	mw->y = y;
143 	mw->fb = NULL;
144 
145-	build_keymap(mw);
146-	
147 	be->gc = 0;
148 	be->image = NULL;
149 	be->shmat = false;