From 96c481028c5b133e7e102c7e312840a9ff87e111 Mon Sep 17 00:00:00 2001 From: Ivan Mahonin Date: Mar 20 2024 08:23:05 +0000 Subject: fix stopping by signal --- diff --git a/app.c b/app.c index 239518c..c6403d7 100644 --- a/app.c +++ b/app.c @@ -155,39 +155,42 @@ int appRun(App *app) { LOGDBG("app: run: event loop"); int buttonDown = 0; unsigned int buttonDownMs = 0; + fd_set fds = {}; + int maxFd = fd > app->stopFd ? fd : app->stopFd; + XFlush(app->dpy); while(1) { int hasEvent = 0; XEvent event = {}; - // wait for next event - if (buttonDown == 1) { - if (XPending(app->dpy)) { - XNextEvent(app->dpy, &event); - hasEvent = 1; - } else { - // manually call select for the X-socket to use timeout - unsigned int dt = monotonicMs() - buttonDownMs; - if (dt < LONGPRESS_MS) { - dt = LONGPRESS_MS - dt; - struct timeval t = {}; - t.tv_sec = dt/1000; - t.tv_usec = dt%1000*1000; - - LOGDBG("app: run: use select fd=%d, timeout=%ums, sec=%ld, usec=%ld", fd, dt, t.tv_sec, t.tv_usec); - - fd_set fds = {}; - FD_ZERO(&fds); - FD_SET(fd, &fds); - XFlush(app->dpy); - select(fd + 1, &fds, NULL, NULL, &t); - - if (XPending(app->dpy)) { - XNextEvent(app->dpy, &event); - hasEvent = 1; - } - } - } + FD_ZERO(&fds); + FD_SET(fd, &fds); + FD_SET(app->stopFd, &fds); + + // wait for next event + if (XPending(app->dpy)) { + XNextEvent(app->dpy, &event); + hasEvent = 1; + } else + if (buttonDown != 1) { + // just wait for fds + select(maxFd + 1, &fds, NULL, NULL, NULL); } else { + // manually call select for the X-socket to use timeout + unsigned int dt = monotonicMs() - buttonDownMs; + if (dt < LONGPRESS_MS) { + dt = LONGPRESS_MS - dt; + struct timeval t = {}; + t.tv_sec = dt/1000; + t.tv_usec = dt%1000*1000; + + LOGDBG("app: run: use select fd=%d, timeout=%ums, sec=%ld, usec=%ld", fd, dt, t.tv_sec, t.tv_usec); + + XFlush(app->dpy); + select(maxFd + 1, &fds, NULL, NULL, &t); + } + } + + if (!hasEvent && XPending(app->dpy)) { XNextEvent(app->dpy, &event); hasEvent = 1; } @@ -293,6 +296,12 @@ int appRun(App *app) { } +void appStopBySignal(App *app) { + char x = 0; + write(app->stopFd, &x, 1); +} + + void appStop(App *app, int err) { LOGDBG("app: stop: err=%d", err); if (!app->run) { diff --git a/app.h b/app.h index 0c620a9..2d79676 100644 --- a/app.h +++ b/app.h @@ -21,6 +21,7 @@ struct App { Window win; Atom aWmDel; int sw, sh; + int stopFd; // dynamic fields int run;