From bd496a05bc3e246144e7a1eece839fb0fe0f8594 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Sun, 25 Mar 2018 23:22:25 -0400 Subject: [PATCH] started reworking event loop --- inc/edit.h | 1 + lib/job.c | 22 ++++++++++++---------- lib/x11.c | 41 +++++++++++++++++++++++++++++++++-------- tide.c | 2 ++ 4 files changed, 48 insertions(+), 18 deletions(-) diff --git a/inc/edit.h b/inc/edit.h index 08cfa11..01f1e62 100644 --- a/inc/edit.h +++ b/inc/edit.h @@ -211,6 +211,7 @@ struct Job { }; bool job_poll(int fd, 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 bc6451d..d660174 100644 --- a/lib/job.c +++ b/lib/job.c @@ -48,18 +48,20 @@ bool job_poll(int fd, int ms) { return (ret > 0); } +void job_spawn(int fd, jobfn_t readfn, jobfn_t writefn, void* data) { + Job *job = calloc(1, sizeof(job)); + job->fd = fd; + job->readfn = readfn; + job->writefn = writefn; + job->data = data; + job->next = JobList; + JobList = job; +} + void job_create(char** cmd, jobfn_t readfn, jobfn_t writefn, void* data) { int fd = -1, pid = -1; - if (job_execute(cmd, &fd, &pid) > 0) { - Job *job = calloc(1, sizeof(job)); - job->fd = fd; - job->pid = pid; - job->readfn = readfn; - job->writefn = writefn; - job->data = data; - job->next = JobList; - JobList = job; - } + if (job_execute(cmd, &fd, &pid) > 0) + job_spawn(fd, readfn, writefn, data); } void job_start(char** cmd, char* data, size_t ndata, View* dest) { diff --git a/lib/x11.c b/lib/x11.c index eac2b88..6ce53ad 100644 --- a/lib/x11.c +++ b/lib/x11.c @@ -401,7 +401,27 @@ void win_save(char* path) { buf_save(&(view->buffer)); } +static void xupdate(Job* job) { + bool pending = job_poll(ConnectionNumber(X.display), Timeout); + int nevents = XEventsQueued(X.display, QueuedAfterFlush); + if (pending || nevents) { + for (XEvent e; XPending(X.display);) { + XNextEvent(X.display, &e); + if (!XFilterEvent(&e, None) && EventHandlers[e.type]) + (EventHandlers[e.type])(&e); + } + onredraw(X.width, X.height); + XCopyArea(X.display, X.pixmap, X.self, X.gc, 0, 0, X.width, X.height, 0, 0); + } + XFlush(X.display); +} + void win_loop(void) { +#if 0 + XMapWindow(X.display, X.self); + job_spawn(ConnectionNumber(X.display), xupdate, 0, 0); + while (1) job_poll(-1, Timeout); +#else XMapWindow(X.display, X.self); while (Running) { bool pending = job_poll(ConnectionNumber(X.display), Timeout); @@ -426,8 +446,11 @@ void win_loop(void) { job_start((char*[]){ "xcpd", NULL }, text, len, NULL); while (job_poll(-1, 100)); } +#endif } + + void win_settext(WinRegion id, char* text) { View* view = win_view(id); view->buffer.gapstart = view->buffer.bufstart; @@ -545,6 +568,16 @@ static void onredraw(int width, int height) { Row* row = view_getrow(view, y); draw_glyphs(Regions[i].x, Regions[i].y + ((y+1) * fheight), row->cols, row->rlen, row->len); } + + /* place the cursor on screen */ + if (Regions[i].csrx != SIZE_MAX && Regions[i].csry != SIZE_MAX) { + x11_draw_rect( + Regions[i].clrcsr.fg, + Regions[i].x + (Regions[i].csrx * fwidth), + Regions[i].y + (Regions[i].csry * fheight), + 1, fheight); + } + } /* draw the scroll region */ @@ -556,14 +589,6 @@ static void onredraw(int width, int height) { x11_draw_rect(clr_scroll.bg, 0, Regions[SCROLL].y - 2, Regions[SCROLL].width, thumbreg); x11_draw_rect(clr_scroll.fg, 0, thumboff, Regions[SCROLL].width, thumbsz); - /* place the cursor on screen */ - if (Regions[Focused].csrx != SIZE_MAX && Regions[Focused].csry != SIZE_MAX) { - x11_draw_rect( - Regions[Focused].clrcsr.fg, - Regions[Focused].x + (Regions[Focused].csrx * fwidth), - Regions[Focused].y + (Regions[Focused].csry * fheight), - 1, fheight); - } } static void oninput(int mods, Rune key) { diff --git a/tide.c b/tide.c index c7cf300..97006d5 100644 --- a/tide.c +++ b/tide.c @@ -634,6 +634,7 @@ void onlayout(void) { void onshutdown(void) { quit(); + exit(0); } static void oninput(Rune rune) { @@ -689,6 +690,7 @@ void edit_relative(char* path) { free(origdir); } + #ifndef TEST int main(int argc, char** argv) { /* setup the shell */ -- 2.49.0