From: Michael D. Lowis Date: Mon, 26 Mar 2018 15:43:42 +0000 (-0400) Subject: reworked key handling code X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=f93671664f6f5cdccd75e1f4df1800e604256f03;p=projs%2Ftide.git reworked key handling code --- diff --git a/gdbscript b/gdbscript new file mode 100644 index 0000000..c143244 --- /dev/null +++ b/gdbscript @@ -0,0 +1,3 @@ +set args testdocs/sherlock.txt +run +bt diff --git a/inc/edit.h b/inc/edit.h index 01f1e62..f0de098 100644 --- a/inc/edit.h +++ b/inc/edit.h @@ -210,7 +210,7 @@ struct Job { void (*readfn)(Job *job); }; -bool job_poll(int fd, int ms); +bool job_poll(int ms); void job_spawn(int fd, jobfn_t readfn, jobfn_t writefn, void* data); void job_create(char** cmd, jobfn_t readfn, jobfn_t writefn, void* data); void job_start(char** cmd, char* data, size_t ndata, View* dest); diff --git a/lib/job.c b/lib/job.c index 262fc5a..c094dd8 100644 --- a/lib/job.c +++ b/lib/job.c @@ -18,15 +18,8 @@ static void job_finish(Job* job); static struct pollfd JobFds[MAX_JOBS]; static Job* JobList = NULL; -bool job_poll(int fd, int ms) { +bool job_poll(int ms) { int njobs = 0; - /* add the X11 connection if we have one */ - if (fd > 0) { - JobFds[0].fd = fd; - JobFds[0].events = POLLIN; - JobFds[0].revents = 0; - njobs = 1; - } /* Add jobs from the job list */ for (Job *job = JobList; job && njobs < MAX_JOBS; job = job->next) { JobFds[njobs].fd = job->fd; @@ -44,7 +37,6 @@ bool job_poll(int fd, int ms) { job_process(JobFds[i].fd, JobFds[i].revents); /* reap zombie processes */ for (int status; waitpid(-1, &status, WNOHANG) > 0;); - // TODO: cleanup old jobs here... return (ret > 0); } @@ -72,6 +64,7 @@ static void job_process(int fd, int events) { Job* job = JobList; // Get job by fd while (job && job->fd != fd) job = job->next; + if (!job) return; if (job->readfn && (events & POLLIN)) job->readfn(job); if (job->writefn && (events & POLLOUT)) diff --git a/lib/x11.c b/lib/x11.c index 831b8d9..6350d9d 100644 --- a/lib/x11.c +++ b/lib/x11.c @@ -416,7 +416,7 @@ void win_loop(void) { XMapWindow(X.display, X.self); XFlush(X.display); job_spawn(ConnectionNumber(X.display), xupdate, 0, 0); - while (1) job_poll(-1, Timeout); + while (1) job_poll(Timeout); } void win_settext(WinRegion id, char* text) { @@ -706,63 +706,27 @@ static WinRegion getregion(size_t x, size_t y) { } static uint32_t special_keys(uint32_t key) { - switch (key) { - case XK_F1: return KEY_F1; - case XK_F2: return KEY_F2; - case XK_F3: return KEY_F3; - case XK_F4: return KEY_F4; - case XK_F5: return KEY_F5; - case XK_F6: return KEY_F6; - case XK_F7: return KEY_F7; - case XK_F8: return KEY_F8; - case XK_F9: return KEY_F9; - case XK_F10: return KEY_F10; - case XK_F11: return KEY_F11; - case XK_F12: return KEY_F12; - case XK_Insert: return KEY_INSERT; - case XK_Delete: return KEY_DELETE; - case XK_Home: return KEY_HOME; - case XK_End: return KEY_END; - case XK_Page_Up: return KEY_PGUP; - case XK_Page_Down: return KEY_PGDN; - case XK_Up: return KEY_UP; - case XK_Down: return KEY_DOWN; - case XK_Left: return KEY_LEFT; - case XK_Right: return KEY_RIGHT; - case XK_Escape: return KEY_ESCAPE; - case XK_BackSpace: return '\b'; - case XK_Tab: return '\t'; - case XK_Return: return '\r'; - case XK_Linefeed: return '\n'; - - /* modifiers should not trigger key presses */ - case XK_Scroll_Lock: - case XK_Shift_L: - case XK_Shift_R: - case XK_Control_L: - case XK_Control_R: - case XK_Caps_Lock: - case XK_Shift_Lock: - case XK_Meta_L: - case XK_Meta_R: - case XK_Alt_L: - case XK_Alt_R: - case XK_Super_L: - case XK_Super_R: - case XK_Hyper_L: - case XK_Hyper_R: - case XK_Menu: - return RUNE_ERR; - - /* if it ain't special, don't touch it */ - default: - return key; - } + static uint32_t keymap[256] = { + /* Function keys */ + [0xBE] = KEY_F1, [0xBF] = KEY_F2, [0xC0] = KEY_F3, [0xC1] = KEY_F4, + [0xC2] = KEY_F5, [0xC3] = KEY_F6, [0xC4] = KEY_F7, [0xC5] = KEY_F8, + [0xC6] = KEY_F9, [0xC7] = KEY_F10, [0xC8] = KEY_F11, [0xC9] = KEY_F12, + /* Navigation keys */ + [0x50] = KEY_HOME, [0x51] = KEY_LEFT, [0x52] = KEY_UP, + [0x53] = KEY_RIGHT, [0x54] = KEY_DOWN, [0x55] = KEY_PGUP, + [0x56] = KEY_PGDN, [0x57] = KEY_END, + /* Control keys */ + [0x08] = '\b', [0x09] = '\t', [0x0d] = '\r', [0x0a] = '\n', + /* Miscellaneous */ + [0x63] = KEY_INSERT, [0x1B] = KEY_ESCAPE, [0xFF] = KEY_DELETE, + }; + /* lookup the key by keysym */ + key = ((key & 0xFF00) == 0xFF00 ? keymap[key & 0xFF] : key); + return (!key ? RUNE_ERR : key); } static uint32_t getkey(XEvent* e) { - int32_t rune = RUNE_ERR; - size_t len = 0; + size_t i = 0, len = 0; char buf[8]; KeySym key; Status status; @@ -774,14 +738,8 @@ static uint32_t getkey(XEvent* e) { /* if it's ascii, just return it */ if (key >= 0x20 && key <= 0x7F) return (uint32_t)key; - /* decode it */ - if (len > 0) { - len = 0; - for (int i = 0; i < 8 && !utf8decode(&rune, &len, buf[i]); i++); - } /* translate special key codes into unicode codepoints */ - rune = special_keys(key); - return rune; + return special_keys(key); } static void xftcolor(XftColor* xc, int id) { @@ -909,5 +867,4 @@ static void xresize(XEvent* e) { } static void xexpose(XEvent* e) { - onredraw(X.width, X.height); }