From: Nicolas Schodet Date: Sun, 22 Mar 2026 22:25:40 +0100 Subject: Use timeout instead of signal to make the clock tick Previous code called non-reentrant function from the signal handler. Remove the signal handler and replace it with a timeout when getting the next event. Bug-Debian: https://bugs.debian.org/696148 Bug: https://github.com/rosorio/buici-clock/issues/2 Forwarded: https://github.com/rosorio/buici-clock/pull/3 --- clock.cxx | 36 +++++------------------------------- xo/ldisplay.cxx | 40 +++++++++++++++++++++++++++++++++++++++- xo/ldisplay.h | 2 ++ 3 files changed, 46 insertions(+), 32 deletions(-) diff --git a/clock.cxx b/clock.cxx index 132618d..af70b3b 100644 --- a/clock.cxx +++ b/clock.cxx @@ -63,7 +63,6 @@ #include "ldisplay.h" #include "lwindow.h" #include "lres.h" -#include "signal.h" // Our signal wrapper #include "buici.xbm" // Our name as a bitmap @@ -92,7 +91,6 @@ extern int g_fOverrideRedirect; extern int g_showSecondHand; int do_clock (void); -void signal_alarm (LSignalHandle handle, void* pv); int x_after (Display*); time_t calc_delta (time_t); @@ -799,22 +797,6 @@ void WTopLevel::render_line (GC gc, Pixmap pixmap, double theta, } -void WTopLevel::setup_time (void) -{ - // -- Configure interval timer - LSignal::accept (SIGALRM, signal_alarm, (void*) this, 0, 0); - { - struct itimerval value; - memset (&value, 0, sizeof (value)); - value.it_interval.tv_sec = 0; - value.it_interval.tv_usec = 100000; - value.it_value.tv_usec = 1; - // value.it_value = value.it_interval; - setitimer (ITIMER_REAL, &value, NULL); - } -} - - void WTopLevel::shape (void) { int i; @@ -934,7 +916,6 @@ int do_clock (void) pWindow->allocate_colors (); // First, create the colormap - pWindow->setup_time (); pWindow->render (); pWindow->map (); @@ -943,7 +924,11 @@ int do_clock (void) // fprintf (stderr, "starting dispatch\n"); while (!g_fQuit) { - display.dispatch_next_event (); + display.dispatch_next_event_timeout (100000); + if (pWindow->render () || pWindow->is_expose ()) { + pWindow->draw (); + pWindow->display ()->flush (); + } } fprintf (stderr, "exiting\n"); @@ -957,14 +942,3 @@ int do_clock (void) return 0; } - -void signal_alarm (LSignalHandle /* handle */, void* pv) -{ - // fprintf (stderr, "tick\n"); - WTopLevel* pWindow = (WTopLevel*) pv; - if (pWindow->render () || pWindow->is_expose ()) { - pWindow->draw (); - pWindow->display ()->flush (); - } - // fprintf (stderr, "tock\n"); -} diff --git a/xo/ldisplay.cxx b/xo/ldisplay.cxx index f800543..b7c6be2 100644 --- a/xo/ldisplay.cxx +++ b/xo/ldisplay.cxx @@ -34,6 +34,9 @@ #include "ldisplay.h" #include "lwindow.h" +#include +#include + unsigned long LDisplay::color_distance (XColor& color, XColor& colorMatch) { unsigned long sh = 16 - m_pVisual->bits_per_rgb; @@ -57,9 +60,15 @@ void LDisplay::dispatch (XEvent* pEvent) } void LDisplay::dispatch_next_event (void) +{ + dispatch_next_event_timeout (-1); +} + +void LDisplay::dispatch_next_event_timeout (long usec) { XEvent event; - next_event (&event); + if (!next_event_timeout (&event, usec)) + return; // Update keyboard mapping if (event.type == MappingKeyboard) { @@ -83,6 +92,35 @@ LWindow* LDisplay::find_child (int id) return m_pWindowRoot ? m_pWindowRoot->find_sibling (id) : (LWindow*) NULL; } +bool LDisplay::next_event_timeout (XEvent* pEvent, long usec) +{ + fd_set fds; + int r, fd; + struct timeval tv; + + if (!m_pDisplay) + return false; + + if (usec < 0 || XPending (m_pDisplay)) { + XNextEvent (m_pDisplay, pEvent); + return true; + } + + fd = ConnectionNumber (m_pDisplay); + FD_ZERO (&fds); + FD_SET (fd, &fds); + tv.tv_sec = 0; + tv.tv_usec = usec; + + r = select (fd + 1, &fds, NULL, NULL, &tv); + + if (r > 0 && XPending (m_pDisplay)) { + XNextEvent (m_pDisplay, pEvent); + return true; + } + + return false; +} bool LDisplay::open (char* szDisplay) { diff --git a/xo/ldisplay.h b/xo/ldisplay.h index 5efddf8..3c3c417 100644 --- a/xo/ldisplay.h +++ b/xo/ldisplay.h @@ -111,6 +111,7 @@ public: unsigned long color_distance (XColor& color, XColor& colorMatch); void dispatch_next_event (void); + void dispatch_next_event_timeout (long usec); void dispatch (XEvent* pEvent); Colormap colormap (void) { return m_colormap; } @@ -151,6 +152,7 @@ public: bool hash_template (const char* szTemplate, LWindow* pWindow); void next_event (XEvent* pEvent) { m_pDisplay ? XNextEvent (m_pDisplay, pEvent) : 0; } + bool next_event_timeout (XEvent* pEvent, long usec); bool open (char* szDisplay = NULL); void unhash_window (LWindow* pWindow);