From 74bc6e77290aaee69f97cce18f6a21e64af2627b Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Fri, 23 Mar 2018 23:06:31 -0400 Subject: [PATCH] restructure event loop --- lib/x11.c | 92 ++++++++++++++++++------------------------------------- 1 file changed, 29 insertions(+), 63 deletions(-) diff --git a/lib/x11.c b/lib/x11.c index e643b8a..b1017f9 100644 --- a/lib/x11.c +++ b/lib/x11.c @@ -223,63 +223,8 @@ void x11_dialog(char* name, int height, int width) { XChangeProperty(X.display, X.self, WindowType, XA_ATOM, 32, PropModeReplace, (unsigned char*)&DialogType, 1); } -void x11_show(void) { - /* simulate an initial resize and map the window */ - XConfigureEvent ce; - ce.type = ConfigureNotify; - ce.width = X.width; - ce.height = X.height; - XSendEvent(X.display, X.self, False, StructureNotifyMask, (XEvent *)&ce); - XMapWindow(X.display, X.self); -} - -bool x11_running(void) { - return Running; -} - -void x11_flip(void) { - onredraw(X.width, X.height); - XCopyArea(X.display, X.pixmap, X.self, X.gc, 0, 0, X.width, X.height, 0, 0); - x11_flush(); -} - -void x11_flush(void) { - XFlush(X.display); -} - -void x11_finish(void) { - XCloseDisplay(X.display); - /* we're exiting now. If we own the clipboard, make sure it persists */ - if (Selections[CLIPBOARD].text) { - char* text = Selections[CLIPBOARD].text; - size_t len = strlen(text); - job_start((char*[]){ "xcpd", NULL }, text, len, NULL); - while (job_poll(-1, 100)); - } -} - /******************************************************************************/ -void x11_handle_event(XEvent* e) { - if (EventHandlers[e->type]) - (EventHandlers[e->type])(e); -} - -int x11_events_queued(void) { - return XEventsQueued(X.display, QueuedAfterFlush); -} - -void x11_events_take(void) { - XEvent e; - int nevents; - XGetMotionEvents(X.display, X.self, CurrentTime, CurrentTime, &nevents); - while (XPending(X.display)) { - XNextEvent(X.display, &e); - if (!XFilterEvent(&e, None) && EventHandlers[e.type]) - (EventHandlers[e.type])(&e); - } -} - XFont x11_font_load(char* name) { struct XFont* font = calloc(1, sizeof(struct XFont)); /* init the library and the base font pattern */ @@ -472,18 +417,39 @@ void win_save(char* path) { } void win_loop(void) { - x11_show(); - while (x11_running()) { + /* simulate an initial resize and map the window */ + XConfigureEvent ce; + ce.type = ConfigureNotify; + ce.width = X.width; + ce.height = X.height; + XSendEvent(X.display, X.self, False, StructureNotifyMask, (XEvent *)&ce); + XMapWindow(X.display, X.self); + + while (Running) { bool pending = job_poll(ConnectionNumber(X.display), Timeout); - int nevents = x11_events_queued(); + int nevents = XEventsQueued(X.display, QueuedAfterFlush); if (pending || nevents) { - x11_events_take(); - if (x11_running()) - x11_flip(); + XGetMotionEvents(X.display, X.self, CurrentTime, CurrentTime, &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); } - x11_flush(); + XFlush(X.display); + } + XCloseDisplay(X.display); + + /* we're exiting now. If we own the clipboard, make sure it persists */ + if (Selections[CLIPBOARD].text) { + char* text = Selections[CLIPBOARD].text; + size_t len = strlen(text); + job_start((char*[]){ "xcpd", NULL }, text, len, NULL); + while (job_poll(-1, 100)); } - x11_finish(); + } void win_settext(WinRegion id, char* text) { -- 2.51.0