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>
master
Devin J. Pohly 7 years ago
parent dbe8676d7d
commit ed132e1127
  1. 6
      config.def.h
  2. 58
      st.c
  3. 18
      st.h
  4. 48
      x.c

@ -209,13 +209,13 @@ Shortcut shortcuts[] = {
* If you want keys other than the X11 function keys (0xFD00 - 0xFFFF) * If you want keys other than the X11 function keys (0xFD00 - 0xFFFF)
* to be mapped below, add them to this array. * 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, * State bits to ignore when matching key or button events. By default,
* numlock (Mod2Mask) and keyboard layout (XK_SWITCH_MOD) are ignored. * 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). * 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 * This is the huge key array which defines all compatibility to the Linux
* world. Please decide about changes wisely. * world. Please decide about changes wisely.
*/ */
static Key key[] = { Key key[] = {
/* keysym mask string appkey appcursor crlf */ /* keysym mask string appkey appcursor crlf */
{ XK_KP_Home, ShiftMask, "\033[2J", 0, -1, 0}, { XK_KP_Home, ShiftMask, "\033[2J", 0, -1, 0},
{ XK_KP_Home, ShiftMask, "\033[1;2H", 0, +1, 0}, { XK_KP_Home, ShiftMask, "\033[1;2H", 0, +1, 0},

58
st.c

@ -110,16 +110,6 @@ typedef struct {
int narg; /* nb of args */ int narg; /* nb of args */
} STREscape; } 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 */ /* function definitions used in config.h */
static void clipcopy(const Arg *); static void clipcopy(const Arg *);
static void clippaste(const Arg *); static void clippaste(const Arg *);
@ -223,6 +213,8 @@ size_t colornamelen = LEN(colorname);
size_t mshortcutslen = LEN(mshortcuts); size_t mshortcutslen = LEN(mshortcuts);
size_t shortcutslen = LEN(shortcuts); size_t shortcutslen = LEN(shortcuts);
size_t selmaskslen = LEN(selmasks); size_t selmaskslen = LEN(selmasks);
size_t keyslen = LEN(key);
size_t mappedkeyslen = LEN(mappedkeys);
ssize_t ssize_t
xwrite(int fd, const char *s, size_t len) xwrite(int fd, const char *s, size_t len)
@ -2550,54 +2542,8 @@ redraw(void)
draw(); draw();
} }
int
match(uint mask, uint state)
{
return mask == XK_ANY_MOD || mask == (state & ~ignoremod);
}
void void
numlock(const Arg *dummy) numlock(const Arg *dummy)
{ {
term.numlock ^= 1; 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

@ -176,6 +176,16 @@ typedef struct {
const Arg arg; const Arg arg;
} Shortcut; } 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 die(const char *, ...);
void redraw(void); void redraw(void);
@ -184,7 +194,6 @@ void tnew(int, int);
void tresize(int, int); void tresize(int, int);
void tsetdirt(int, int); void tsetdirt(int, int);
void tsetdirtattr(int); void tsetdirtattr(int);
int match(uint, uint);
void ttynew(void); void ttynew(void);
size_t ttyread(void); size_t ttyread(void);
void ttyresize(int, int); void ttyresize(int, int);
@ -193,9 +202,7 @@ void ttywrite(const char *, size_t);
void resettitle(void); void resettitle(void);
char *kmap(KeySym, uint);
void selclear(void); void selclear(void);
void selinit(void); void selinit(void);
void selnormalize(void); void selnormalize(void);
int selected(int, int); int selected(int, int);
@ -255,7 +262,12 @@ extern MouseShortcut mshortcuts[];
extern size_t mshortcutslen; extern size_t mshortcutslen;
extern Shortcut shortcuts[]; extern Shortcut shortcuts[];
extern size_t shortcutslen; extern size_t shortcutslen;
extern KeySym mappedkeys[];
extern size_t mappedkeyslen;
extern uint ignoremod;
extern uint forceselmod; extern uint forceselmod;
extern Key key[];
extern size_t keyslen;
extern uint selmasks[]; extern uint selmasks[];
extern size_t selmaskslen; extern size_t selmaskslen;
extern char ascii_printable[]; extern char ascii_printable[];

48
x.c

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

Loading…
Cancel
Save