Move key-matching functions into x.c

Modifiers and keysyms are specific to X, and the functions match and
kmap are only used in x.c.  Needed to global-ize the key arrays and
lengths from config.h (for now).

Signed-off-by: Devin J. Pohly <djpohly@gmail.com>
This commit is contained in:
Devin J. Pohly 2017-10-11 08:47:14 -05:00
parent dbe8676d7d
commit ed132e1127
4 changed files with 68 additions and 62 deletions

View File

@ -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).
@ -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},

58
st.c
View File

@ -110,16 +110,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 clipcopy(const Arg *);
static void clippaste(const Arg *);
@ -223,6 +213,8 @@ size_t colornamelen = LEN(colorname);
size_t mshortcutslen = LEN(mshortcuts);
size_t shortcutslen = LEN(shortcuts);
size_t selmaskslen = LEN(selmasks);
size_t keyslen = LEN(key);
size_t mappedkeyslen = LEN(mappedkeys);
ssize_t
xwrite(int fd, const char *s, size_t len)
@ -2550,54 +2542,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;
}

18
st.h
View File

@ -176,6 +176,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);
@ -184,7 +194,6 @@ void tnew(int, 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);
@ -193,9 +202,7 @@ void ttywrite(const char *, size_t);
void resettitle(void);
char *kmap(KeySym, uint);
void selclear(void);
void selinit(void);
void selnormalize(void);
int selected(int, int);
@ -255,7 +262,12 @@ extern MouseShortcut mshortcuts[];
extern size_t mshortcutslen;
extern Shortcut shortcuts[];
extern size_t shortcutslen;
extern KeySym mappedkeys[];
extern size_t mappedkeyslen;
extern uint ignoremod;
extern uint forceselmod;
extern Key key[];
extern size_t keyslen;
extern uint selmasks[];
extern size_t selmaskslen;
extern char ascii_printable[];

48
x.c
View File

@ -116,6 +116,8 @@ static void selrequest(XEvent *);
static void selcopy(Time);
static void getbuttoninfo(XEvent *);
static void mousereport(XEvent *);
static char *kmap(KeySym, uint);
static int match(uint, uint);
static void run(void);
static void usage(void);
@ -1603,6 +1605,52 @@ focus(XEvent *ev)
}
}
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 + keyslen; 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
kpress(XEvent *ev)
{