| 1 |
From: Nicolas Schodet <[email protected]> |
| 2 |
Date: Sun, 22 Mar 2026 22:25:40 +0100 |
| 3 |
Subject: Use timeout instead of signal to make the clock tick |
| 4 |
|
| 5 |
Previous code called non-reentrant function from the signal handler. |
| 6 |
Remove the signal handler and replace it with a timeout when getting the |
| 7 |
next event. |
| 8 |
|
| 9 |
Bug-Debian: https://bugs.debian.org/696148 |
| 10 |
Bug: https://github.com/rosorio/buici-clock/issues/2 |
| 11 |
Forwarded: https://github.com/rosorio/buici-clock/pull/3 |
| 12 |
--- |
| 13 |
clock.cxx | 36 +++++------------------------------- |
| 14 |
xo/ldisplay.cxx | 40 +++++++++++++++++++++++++++++++++++++++- |
| 15 |
xo/ldisplay.h | 2 ++ |
| 16 |
3 files changed, 46 insertions(+), 32 deletions(-) |
| 17 |
|
| 18 |
diff --git a/clock.cxx b/clock.cxx |
| 19 |
index 132618d..af70b3b 100644 |
| 20 |
--- a/clock.cxx |
| 21 |
+++ b/clock.cxx |
| 22 |
@@ -63,7 +63,6 @@ |
| 23 |
#include "ldisplay.h" |
| 24 |
#include "lwindow.h" |
| 25 |
#include "lres.h" |
| 26 |
-#include "signal.h" // Our signal wrapper |
| 27 |
|
| 28 |
#include "buici.xbm" // Our name as a bitmap |
| 29 |
|
| 30 |
@@ -92,7 +91,6 @@ extern int g_fOverrideRedirect; |
| 31 |
extern int g_showSecondHand; |
| 32 |
|
| 33 |
int do_clock (void); |
| 34 |
-void signal_alarm (LSignalHandle handle, void* pv); |
| 35 |
int x_after (Display*); |
| 36 |
time_t calc_delta (time_t); |
| 37 |
|
| 38 |
@@ -799,22 +797,6 @@ void WTopLevel::render_line (GC gc, Pixmap pixmap, double theta, |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
-void WTopLevel::setup_time (void) |
| 43 |
-{ |
| 44 |
- // -- Configure interval timer |
| 45 |
- LSignal::accept (SIGALRM, signal_alarm, (void*) this, 0, 0); |
| 46 |
- { |
| 47 |
- struct itimerval value; |
| 48 |
- memset (&value, 0, sizeof (value)); |
| 49 |
- value.it_interval.tv_sec = 0; |
| 50 |
- value.it_interval.tv_usec = 100000; |
| 51 |
- value.it_value.tv_usec = 1; |
| 52 |
- // value.it_value = value.it_interval; |
| 53 |
- setitimer (ITIMER_REAL, &value, NULL); |
| 54 |
- } |
| 55 |
-} |
| 56 |
- |
| 57 |
- |
| 58 |
void WTopLevel::shape (void) |
| 59 |
{ |
| 60 |
int i; |
| 61 |
@@ -934,7 +916,6 @@ int do_clock (void) |
| 62 |
|
| 63 |
pWindow->allocate_colors (); // First, create the colormap |
| 64 |
|
| 65 |
- pWindow->setup_time (); |
| 66 |
pWindow->render (); |
| 67 |
pWindow->map (); |
| 68 |
|
| 69 |
@@ -943,7 +924,11 @@ int do_clock (void) |
| 70 |
// fprintf (stderr, "starting dispatch\n"); |
| 71 |
|
| 72 |
while (!g_fQuit) { |
| 73 |
- display.dispatch_next_event (); |
| 74 |
+ display.dispatch_next_event_timeout (100000); |
| 75 |
+ if (pWindow->render () || pWindow->is_expose ()) { |
| 76 |
+ pWindow->draw (); |
| 77 |
+ pWindow->display ()->flush (); |
| 78 |
+ } |
| 79 |
} |
| 80 |
|
| 81 |
fprintf (stderr, "exiting\n"); |
| 82 |
@@ -957,14 +942,3 @@ int do_clock (void) |
| 83 |
|
| 84 |
return 0; |
| 85 |
} |
| 86 |
- |
| 87 |
-void signal_alarm (LSignalHandle /* handle */, void* pv) |
| 88 |
-{ |
| 89 |
- // fprintf (stderr, "tick\n"); |
| 90 |
- WTopLevel* pWindow = (WTopLevel*) pv; |
| 91 |
- if (pWindow->render () || pWindow->is_expose ()) { |
| 92 |
- pWindow->draw (); |
| 93 |
- pWindow->display ()->flush (); |
| 94 |
- } |
| 95 |
- // fprintf (stderr, "tock\n"); |
| 96 |
-} |
| 97 |
diff --git a/xo/ldisplay.cxx b/xo/ldisplay.cxx |
| 98 |
index f800543..b7c6be2 100644 |
| 99 |
--- a/xo/ldisplay.cxx |
| 100 |
+++ b/xo/ldisplay.cxx |
| 101 |
@@ -34,6 +34,9 @@ |
| 102 |
#include "ldisplay.h" |
| 103 |
#include "lwindow.h" |
| 104 |
|
| 105 |
+#include <sys/select.h> |
| 106 |
+#include <sys/time.h> |
| 107 |
+ |
| 108 |
unsigned long LDisplay::color_distance (XColor& color, XColor& colorMatch) |
| 109 |
{ |
| 110 |
unsigned long sh = 16 - m_pVisual->bits_per_rgb; |
| 111 |
@@ -57,9 +60,15 @@ void LDisplay::dispatch (XEvent* pEvent) |
| 112 |
} |
| 113 |
|
| 114 |
void LDisplay::dispatch_next_event (void) |
| 115 |
+{ |
| 116 |
+ dispatch_next_event_timeout (-1); |
| 117 |
+} |
| 118 |
+ |
| 119 |
+void LDisplay::dispatch_next_event_timeout (long usec) |
| 120 |
{ |
| 121 |
XEvent event; |
| 122 |
- next_event (&event); |
| 123 |
+ if (!next_event_timeout (&event, usec)) |
| 124 |
+ return; |
| 125 |
|
| 126 |
// Update keyboard mapping |
| 127 |
if (event.type == MappingKeyboard) { |
| 128 |
@@ -83,6 +92,35 @@ LWindow* LDisplay::find_child (int id) |
| 129 |
return m_pWindowRoot ? m_pWindowRoot->find_sibling (id) : (LWindow*) NULL; |
| 130 |
} |
| 131 |
|
| 132 |
+bool LDisplay::next_event_timeout (XEvent* pEvent, long usec) |
| 133 |
+{ |
| 134 |
+ fd_set fds; |
| 135 |
+ int r, fd; |
| 136 |
+ struct timeval tv; |
| 137 |
+ |
| 138 |
+ if (!m_pDisplay) |
| 139 |
+ return false; |
| 140 |
+ |
| 141 |
+ if (usec < 0 || XPending (m_pDisplay)) { |
| 142 |
+ XNextEvent (m_pDisplay, pEvent); |
| 143 |
+ return true; |
| 144 |
+ } |
| 145 |
+ |
| 146 |
+ fd = ConnectionNumber (m_pDisplay); |
| 147 |
+ FD_ZERO (&fds); |
| 148 |
+ FD_SET (fd, &fds); |
| 149 |
+ tv.tv_sec = 0; |
| 150 |
+ tv.tv_usec = usec; |
| 151 |
+ |
| 152 |
+ r = select (fd + 1, &fds, NULL, NULL, &tv); |
| 153 |
+ |
| 154 |
+ if (r > 0 && XPending (m_pDisplay)) { |
| 155 |
+ XNextEvent (m_pDisplay, pEvent); |
| 156 |
+ return true; |
| 157 |
+ } |
| 158 |
+ |
| 159 |
+ return false; |
| 160 |
+} |
| 161 |
|
| 162 |
bool LDisplay::open (char* szDisplay) |
| 163 |
{ |
| 164 |
diff --git a/xo/ldisplay.h b/xo/ldisplay.h |
| 165 |
index 5efddf8..3c3c417 100644 |
| 166 |
--- a/xo/ldisplay.h |
| 167 |
+++ b/xo/ldisplay.h |
| 168 |
@@ -111,6 +111,7 @@ public: |
| 169 |
|
| 170 |
unsigned long color_distance (XColor& color, XColor& colorMatch); |
| 171 |
void dispatch_next_event (void); |
| 172 |
+ void dispatch_next_event_timeout (long usec); |
| 173 |
void dispatch (XEvent* pEvent); |
| 174 |
Colormap colormap (void) { |
| 175 |
return m_colormap; } |
| 176 |
@@ -151,6 +152,7 @@ public: |
| 177 |
bool hash_template (const char* szTemplate, LWindow* pWindow); |
| 178 |
void next_event (XEvent* pEvent) { |
| 179 |
m_pDisplay ? XNextEvent (m_pDisplay, pEvent) : 0; } |
| 180 |
+ bool next_event_timeout (XEvent* pEvent, long usec); |
| 181 |
bool open (char* szDisplay = NULL); |
| 182 |
void unhash_window (LWindow* pWindow); |
| 183 |
|