[hackers] [st][PATCH 14/23] Move key mapping into x.c

From: Devin J. Pohly <djpohly_AT_gmail.com>
Date: Sun, 24 Sep 2017 16:11:36 -0500

---
 config.def.h |  6 +++---
 st.c         | 58 ++--------------------------------------------------------
 st.h         | 17 +++++++++++++++--
 x.c          | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 68 insertions(+), 61 deletions(-)
diff --git a/config.def.h b/config.def.h
index dd94be2..18cb31c 100644
--- a/config.def.h
+++ b/config.def.h
_AT_@ -209,13 +209,13 @@ Shortcut shortcuts[] = {
  * If you want keys other than the X11 function keys (0xFD00 - 0xFFFF)
  * to be mapped below, add them to this array.
  */
-static KeySym mappedkeys[] = { -1 };
+KeySym mappedkeys[] = { -1 };
 
 /*
  * State bits to ignore when matching key or button events.  By default,
  * numlock (Mod2Mask) and keyboard layout (XK_SWITCH_MOD) are ignored.
  */
-static uint ignoremod = Mod2Mask|XK_SWITCH_MOD;
+uint ignoremod = Mod2Mask|XK_SWITCH_MOD;
 
 /*
  * Override mouse-select while mask is active (when MODE_MOUSE is set).
_AT_@ -228,7 +228,7 @@ uint forceselmod = ShiftMask;
  * This is the huge key array which defines all compatibility to the Linux
  * world. Please decide about changes wisely.
  */
-static Key key[] = {
+Key key[] = {
 	/* keysym           mask            string      appkey appcursor crlf */
 	{ XK_KP_Home,       ShiftMask,      "\033[2J",       0,   -1,    0},
 	{ XK_KP_Home,       ShiftMask,      "\033[1;2H",     0,   +1,    0},
diff --git a/st.c b/st.c
index 9b92bc6..2f0f8ec 100644
--- a/st.c
+++ b/st.c
_AT_@ -107,16 +107,6 @@ typedef struct {
 	int narg;              /* nb of args */
 } STREscape;
 
-typedef struct {
-	KeySym k;
-	uint mask;
-	char *s;
-	/* three valued logic variables: 0 indifferent, 1 on, -1 off */
-	signed char appkey;    /* application keypad */
-	signed char appcursor; /* application cursor */
-	signed char crlf;      /* crlf mode          */
-} Key;
-
 /* function definitions used in config.h */
 static void numlock(const Arg *);
 static void printsel(const Arg *);
_AT_@ -215,6 +205,8 @@ static Rune utfmax[UTF_SIZ + 1] = {0x10FFFF, 0x7F, 0x7FF, 0xFFFF, 0x10FFFF};
 size_t colornamelen = LEN(colorname);
 size_t mshortcutslen = LEN(mshortcuts);
 size_t shortcutslen = LEN(shortcuts);
+size_t mappedkeyslen = LEN(mappedkeys);
+size_t keylen = LEN(key);
 size_t selmaskslen = LEN(selmasks);
 
 ssize_t
_AT_@ -2496,54 +2488,8 @@ redraw(void)
 	draw();
 }
 
-int
-match(uint mask, uint state)
-{
-	return mask == XK_ANY_MOD || mask == (state & ~ignoremod);
-}
-
 void
 numlock(const Arg *dummy)
 {
 	term.numlock ^= 1;
 }
-
-char*
-kmap(KeySym k, uint state)
-{
-	Key *kp;
-	int i;
-
-	/* Check for mapped keys out of X11 function keys. */
-	for (i = 0; i < LEN(mappedkeys); i++) {
-		if (mappedkeys[i] == k)
-			break;
-	}
-	if (i == LEN(mappedkeys)) {
-		if ((k & 0xFFFF) < 0xFD00)
-			return NULL;
-	}
-
-	for (kp = key; kp < key + LEN(key); kp++) {
-		if (kp->k != k)
-			continue;
-
-		if (!match(kp->mask, state))
-			continue;
-
-		if (IS_SET(MODE_APPKEYPAD) ? kp->appkey < 0 : kp->appkey > 0)
-			continue;
-		if (term.numlock && kp->appkey == 2)
-			continue;
-
-		if (IS_SET(MODE_APPCURSOR) ? kp->appcursor < 0 : kp->appcursor > 0)
-			continue;
-
-		if (IS_SET(MODE_CRLF) ? kp->crlf < 0 : kp->crlf > 0)
-			continue;
-
-		return kp->s;
-	}
-
-	return NULL;
-}
diff --git a/st.h b/st.h
index 4b21153..8d75687 100644
--- a/st.h
+++ b/st.h
_AT_@ -179,6 +179,16 @@ typedef struct {
 	const Arg arg;
 } Shortcut;
 
+typedef struct {
+	KeySym k;
+	uint mask;
+	char *s;
+	/* three valued logic variables: 0 indifferent, 1 on, -1 off */
+	signed char appkey;    /* application keypad */
+	signed char appcursor; /* application cursor */
+	signed char crlf;      /* crlf mode          */
+} Key;
+
 void die(const char *, ...);
 void redraw(void);
 
_AT_@ -187,7 +197,6 @@ void tnew(int, int, unsigned int);
 void tresize(int, int);
 void tsetdirt(int, int);
 void tsetdirtattr(int);
-int match(uint, uint);
 void ttynew(void);
 size_t ttyread(void);
 void ttyresize(int, int);
_AT_@ -196,7 +205,6 @@ void ttywrite(const char *, size_t);
 
 void resettitle(void);
 
-char *kmap(KeySym, uint);
 void selclear(void);
 
 void selnormalize(void);
_AT_@ -254,6 +262,11 @@ extern MouseShortcut mshortcuts[];
 extern size_t mshortcutslen;
 extern Shortcut shortcuts[];
 extern size_t shortcutslen;
+extern KeySym mappedkeys[];
+extern size_t mappedkeyslen;
+extern Key key[];
+extern size_t keylen;
+extern uint ignoremod;
 extern uint forceselmod;
 extern uint selmasks[];
 extern size_t selmaskslen;
diff --git a/x.c b/x.c
index c2faa9c..45278e0 100644
--- a/x.c
+++ b/x.c
_AT_@ -116,6 +116,8 @@ static void selrequest(XEvent *);
 
 static void selcopy(Time);
 static void selinit(void);
+static int match(uint, uint);
+static char *kmap(KeySym, uint);
 
 static int x2col(int);
 static int y2row(int);
_AT_@ -1725,6 +1727,52 @@ resize(XEvent *e)
 	ttyresize(win.tw, win.th);
 }
 
+int
+match(uint mask, uint state)
+{
+	return mask == XK_ANY_MOD || mask == (state & ~ignoremod);
+}
+
+char *
+kmap(KeySym k, uint state)
+{
+	Key *kp;
+	int i;
+
+	/* Check for mapped keys out of X11 function keys. */
+	for (i = 0; i < mappedkeyslen; i++) {
+		if (mappedkeys[i] == k)
+			break;
+	}
+	if (i == mappedkeyslen) {
+		if ((k & 0xFFFF) < 0xFD00)
+			return NULL;
+	}
+
+	for (kp = key; kp < key + keylen; kp++) {
+		if (kp->k != k)
+			continue;
+
+		if (!match(kp->mask, state))
+			continue;
+
+		if (IS_SET(MODE_APPKEYPAD) ? kp->appkey < 0 : kp->appkey > 0)
+			continue;
+		if (term.numlock && kp->appkey == 2)
+			continue;
+
+		if (IS_SET(MODE_APPCURSOR) ? kp->appcursor < 0 : kp->appcursor > 0)
+			continue;
+
+		if (IS_SET(MODE_CRLF) ? kp->crlf < 0 : kp->crlf > 0)
+			continue;
+
+		return kp->s;
+	}
+
+	return NULL;
+}
+
 void
 run(void)
 {
-- 
2.14.1
Received on Sun Sep 24 2017 - 23:11:36 CEST

This archive was generated by hypermail 2.3.0 : Sun Sep 24 2017 - 23:25:34 CEST