From 482f43e3d463e677dec8370309de4abe176b0015 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Fri, 21 Dec 2018 22:51:47 -0500 Subject: [PATCH] added key input processing --- inc/x11.h | 14 +++++++++++++- src/lib/x11.c | 15 ++++----------- src/pick.c | 22 ++++++++++++++++++++-- 3 files changed, 37 insertions(+), 14 deletions(-) diff --git a/inc/x11.h b/inc/x11.h index e2693fc..33fbcee 100644 --- a/inc/x11.h +++ b/inc/x11.h @@ -120,7 +120,7 @@ static void x11_show(XConf* x) { static void x11_event_loop(XConf* x, void (*redraw)(XConf* x)) { if (redraw) redraw(x); - for (XEvent e;;) { + for (XEvent e; x->running;) { XNextEvent(x->display, &e); if (x->eventfns[e.type]) x->eventfns[e.type](x, &e); @@ -173,4 +173,16 @@ static void x11_flip(XConf* x) { XFlush(x->display); } +static KeySym x11_getkey(XConf* x, XEvent* e) { + char buf[8]; + KeySym key; + Status status; + /* Read the key string */ + if (x->xic) + Xutf8LookupString(x->xic, &(e->xkey), buf, sizeof(buf), &key, &status); + else + XLookupString(&(e->xkey), buf, sizeof(buf), &key, 0); + return key; +} + #pragma GCC diagnostic pop diff --git a/src/lib/x11.c b/src/lib/x11.c index bccc0b0..4e58603 100644 --- a/src/lib/x11.c +++ b/src/lib/x11.c @@ -108,15 +108,8 @@ static uint32_t special_keys(uint32_t key) { return (!key ? RUNE_ERR : key); } -static uint32_t getkey(XEvent* e) { - char buf[8]; - KeySym key; - Status status; - /* Read the key string */ - if (X.xic) - Xutf8LookupString(X.xic, &(e->xkey), buf, sizeof(buf), &key, &status); - else - XLookupString(&(e->xkey), buf, sizeof(buf), &key, 0); +static uint32_t getkey(XConf* x, XEvent* e) { + KeySym key = x11_getkey(x, e); /* if it's ascii, just return it */ if (key >= 0x20 && key <= 0x7F) return (uint32_t)key; @@ -300,7 +293,7 @@ static void xkeypress(XConf* x, XEvent* e) { (void)x; Now = e->xkey.time; Focused = (e->xkey.y <= Divider ? TAGS : EDIT); - uint32_t key = getkey(e); + uint32_t key = getkey(x, e); if (key == RUNE_ERR) return; KeyBtnState = e->xkey.state; int mods = KeyBtnState & (ModCtrl|ModShift|ModAlt); @@ -439,7 +432,7 @@ static void xupdate(Job* job) { for (int status; waitpid(-1, &status, WNOHANG) > 0;); } if (nqueued || !job) { - /* force update the title */ + /* force update the title */ win_title(NULL); /* determine the size of the regions */ size_t maxtagrows = ((X.height/4) / X.tagfont->height); diff --git a/src/pick.c b/src/pick.c index 2113b65..5f5049b 100644 --- a/src/pick.c +++ b/src/pick.c @@ -108,8 +108,26 @@ static void score(void) { } static void xkeypress(XConf* x, XEvent* e) { - (void)x, (void)e; - score(); + KeySym key = x11_getkey(x, e); + if (key == XK_Return) { + x->running = false; + } else if (key == XK_Escape) { + x->running = false; + ChoiceIdx = SIZE_MAX; + } else if (key == XK_Up) { + if (ChoiceIdx <= vec_size(&Choices)) + ChoiceIdx++; + } else if (key == XK_Down) { + if (ChoiceIdx > 0) + ChoiceIdx--; + } else if (key == XK_BackSpace) { + if (QueryIdx > 0) + Query[--QueryIdx] = '\0'; + } else if (key >= 0x20 && key <= 0x7F) { + if (QueryIdx < sizeof(Query)-1) + Query[QueryIdx++] = key; + score(); + } } static void xbtnpress(XConf* x, XEvent* e) { -- 2.52.0