diff --git a/src/bin/xmoji/keyinjector.c b/src/bin/xmoji/keyinjector.c index 073cbc4..1db18d9 100644 --- a/src/bin/xmoji/keyinjector.c +++ b/src/bin/xmoji/keyinjector.c @@ -1,6 +1,5 @@ #include "keyinjector.h" -#include "timer.h" #include "unistr.h" #include "x11adapter.h" @@ -19,8 +18,7 @@ typedef struct InjectJob unsigned symspercode; } InjectJob; -static Timer *timer; -static unsigned resetms; +static PSC_Timer *timer; static InjectorFlags injectflags; static InjectJob queue[MAXQUEUELEN]; @@ -47,7 +45,7 @@ static void resetkmap(void *receiver, void *sender, void *args) (void)sender; (void)args; - Timer_stop(timer); + PSC_Timer_stop(timer); xcb_connection_t *c = X11Adapter_connection(); const xcb_setup_t *setup = xcb_get_setup(c); CHECK(xcb_change_keyboard_mapping(c, queue[queuefront].len, @@ -139,7 +137,7 @@ static void doinject(void *obj, unsigned sequence, queue[queuefront].len = len; queue[queuefront].symspercode = kmap->keysyms_per_keycode; - Timer_start(timer, resetms); + PSC_Timer_start(timer); } static void injectnext(void) @@ -157,10 +155,10 @@ void KeyInjector_init(unsigned ms, InjectorFlags flags) { if (!timer) { - timer = Timer_create(); - PSC_Event_register(Timer_expired(timer), 0, resetkmap, 0); + timer = PSC_Timer_create(); + PSC_Timer_setMs(timer, ms); + PSC_Event_register(PSC_Timer_expired(timer), 0, resetkmap, 0); } - resetms = ms; injectflags = flags; } @@ -175,7 +173,7 @@ void KeyInjector_inject(const UniStr *str) void KeyInjector_done(void) { if (!timer) return; - Timer_destroy(timer); + PSC_Timer_destroy(timer); timer = 0; } diff --git a/src/bin/xmoji/timer.c b/src/bin/xmoji/timer.c deleted file mode 100644 index 68c79f7..0000000 --- a/src/bin/xmoji/timer.c +++ /dev/null @@ -1,101 +0,0 @@ -#define _POSIX_C_SOURCE 200112L - -#include "timer.h" - -#include "suppress.h" - -#include -#include -#include -#include -#include -#include -#include - -struct Timer -{ - PSC_Event *expired; - timer_t timerid; - int pipe[2]; -}; - -static void expired(void *receiver, void *sender, void *args) -{ - (void)sender; - (void)args; - - Timer *self = receiver; - char dummy; - while (read(self->pipe[0], &dummy, 1) == 1) - { - PSC_Event_raise(self->expired, 0, 0); - } -} - -static void expirefunc(union sigval sv) -{ - Timer *self = sv.sival_ptr; - static const char dummy = 0; -SUPPRESS(unused-result) - write(self->pipe[1], &dummy, 1); -ENDSUPPRESS -} - -Timer *Timer_create(void) -{ - Timer *self = PSC_malloc(sizeof *self); - memset(self, 0, sizeof *self); - if (pipe(self->pipe) < 0) goto error; - fcntl(self->pipe[0], F_SETFL, fcntl(self->pipe[0], F_GETFL) | O_NONBLOCK); - PSC_Event_register(PSC_Service_readyRead(), self, expired, self->pipe[0]); - PSC_Service_registerRead(self->pipe[0]); - struct sigevent ev = { - .sigev_notify = SIGEV_THREAD, - .sigev_signo = 0, - .sigev_value = { .sival_ptr = self }, - .sigev_notify_function = expirefunc, - .sigev_notify_attributes = 0 - }; - if (timer_create(CLOCK_MONOTONIC, &ev, &self->timerid) < 0) goto error; - self->expired = PSC_Event_create(self); - return self; - -error: - Timer_destroy(self); - return 0; -} - -PSC_Event *Timer_expired(Timer *self) -{ - return self->expired; -} - -void Timer_start(Timer *self, unsigned interval_ms) -{ - struct itimerspec itv = { - .it_interval = { - .tv_sec = interval_ms / 1000U, - .tv_nsec = 1000000U * (interval_ms % 1000U) }, - .it_value = { - .tv_sec = interval_ms / 1000U, - .tv_nsec = 1000000U * (interval_ms % 1000U) } - }; - timer_settime(self->timerid, 0, &itv, 0); -} - -void Timer_destroy(Timer *self) -{ - if (!self) return; - PSC_Event_destroy(self->expired); - if (self->timerid) timer_delete(self->timerid); - if (self->pipe[1]) - { - PSC_Service_unregisterRead(self->pipe[0]); - PSC_Event_unregister(PSC_Service_readyRead(), self, expired, - self->pipe[0]); - close(self->pipe[1]); - close(self->pipe[0]); - } - free(self); -} - diff --git a/src/bin/xmoji/timer.h b/src/bin/xmoji/timer.h deleted file mode 100644 index 1202768..0000000 --- a/src/bin/xmoji/timer.h +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef XMOJI_TIMER_H -#define XMOJI_TIMER_H - -#include - -C_CLASS_DECL(PSC_Event); -C_CLASS_DECL(Timer); - -Timer *Timer_create(void); -PSC_Event *Timer_expired(Timer *self) CMETHOD; -void Timer_start(Timer *self, unsigned interval_ms) CMETHOD; -#define Timer_stop(t) Timer_start(t, 0) -void Timer_destroy(Timer *self); - -#endif diff --git a/src/bin/xmoji/tooltip.c b/src/bin/xmoji/tooltip.c index 8587da0..8105525 100644 --- a/src/bin/xmoji/tooltip.c +++ b/src/bin/xmoji/tooltip.c @@ -1,20 +1,16 @@ #include "tooltip.h" #include "textlabel.h" -#include "timer.h" #include "window.h" #include -static Timer *timer; -unsigned timerref; - struct Tooltip { TextLabel *label; Widget *parent; Window *window; - unsigned delay; + PSC_Timer *timer; }; static void timeout(void *receiver, void *sender, void *args) @@ -23,8 +19,7 @@ static void timeout(void *receiver, void *sender, void *args) (void)args; Tooltip *self = receiver; - Timer_stop(timer); - PSC_Event_unregister(Timer_expired(timer), self, timeout, 0); + PSC_Timer_stop(self->timer); if (self->window) { Window_showTooltip(self->window, self->label, self->parent); @@ -33,15 +28,13 @@ static void timeout(void *receiver, void *sender, void *args) Tooltip *Tooltip_create(const UniStr *text, Widget *parent, unsigned delay) { - if (!timerref++) - { - timer = Timer_create(); - } Tooltip *self = PSC_malloc(sizeof *self); self->label = TextLabel_create(0, 0); self->parent = parent; self->window = 0; - self->delay = delay ? delay : 2000; + self->timer = PSC_Timer_create(); + PSC_Timer_setMs(self->timer, delay ? delay : 2000); + PSC_Event_register(PSC_Timer_expired(self->timer), self, timeout, 0); TextLabel_setText(self->label, text); TextLabel_setColor(self->label, COLOR_TOOLTIP); @@ -54,25 +47,20 @@ Tooltip *Tooltip_create(const UniStr *text, Widget *parent, unsigned delay) void Tooltip_activate(Tooltip *self, Window *window) { self->window = window; - PSC_Event_register(Timer_expired(timer), self, timeout, 0); - Timer_start(timer, self->delay); + PSC_Timer_start(self->timer); } void Tooltip_cancel(Tooltip *self) { self->window = 0; - Timer_stop(timer); - PSC_Event_unregister(Timer_expired(timer), self, timeout, 0); + PSC_Timer_stop(self->timer); } void Tooltip_destroy(Tooltip *self) { if (!self) return; + PSC_Timer_destroy(self->timer); Object_destroy(self->label); free(self); - if (!--timerref) - { - Timer_destroy(timer); - } } diff --git a/src/bin/xmoji/xmoji.mk b/src/bin/xmoji/xmoji.mk index 18027a8..26fca2d 100644 --- a/src/bin/xmoji/xmoji.mk +++ b/src/bin/xmoji/xmoji.mk @@ -28,7 +28,6 @@ xmoji_MODULES= button \ textbox \ textlabel \ textrenderer \ - timer \ tooltip \ unistr \ unistrbuilder \ @@ -46,8 +45,7 @@ xmoji_BIN2CSTR_FILES= icon256.h:icons/256x256/xmoji.png \ icon32.h:icons/32x32/xmoji.png \ icon16.h:icons/16x16/xmoji.png xmoji_EMOJIGEN_FILES= emojidata.h:contrib/emoji-test.txt -xmoji_LIBS= m \ - rt +xmoji_LIBS= m xmoji_PKGDEPS= fontconfig \ freetype2 >= 24.2.18 \ harfbuzz \ @@ -68,7 +66,7 @@ endif ifeq ($(BUNDLED_POSER),1) xmoji_STATICDEPS+= posercore xmoji_PRECFLAGS+= -I./poser/include -xmoji_LIBS+= posercore +xmoji_LIBS+= posercore rt xmoji_LDFLAGS+= -pthread else xmoji_PKGDEPS+= posercore diff --git a/src/bin/xmoji/xselection.c b/src/bin/xmoji/xselection.c index 51af484..e15951b 100644 --- a/src/bin/xmoji/xselection.c +++ b/src/bin/xmoji/xselection.c @@ -1,7 +1,6 @@ #include "xselection.h" #include "object.h" -#include "timer.h" #include "unistr.h" #include "window.h" #include "x11adapter.h" @@ -25,7 +24,7 @@ struct XSelectionRequest XSelectionRequest *next; XSelectionRequest *parent; XSelectionRequest *subreqs; - Timer *timeout; + PSC_Timer *timeout; void *data; size_t datalen; size_t datapos; @@ -62,7 +61,7 @@ struct XSelectionConvert XSelection *selection; XSelectionConvert *next; Widget *requestor; - Timer *timeout; + PSC_Timer *timeout; void *data; size_t datalen; XSelectionCallback received; @@ -171,7 +170,7 @@ static void XSelectionRequest_reject(XSelection *selection, static void XSelectionRequest_done(XSelectionRequest *self, int destroy) { if (destroy && !self) return; - Timer_destroy(self->timeout); + PSC_Timer_destroy(self->timeout); free(self->data); if (self->sendincr) { @@ -413,10 +412,11 @@ static void XSelectionRequest_start(XSelectionRequest *self) (unsigned)Window_id(self->selection->w)); PSC_Event_register(X11Adapter_propertyNotify(), self, XSelectionRequest_propertyChanged, self->requestor); - self->timeout = Timer_create(); - PSC_Event_register(Timer_expired(self->timeout), self, + self->timeout = PSC_Timer_create(); + PSC_Timer_setMs(self->timeout, REQUESTTIMEOUT); + PSC_Event_register(PSC_Timer_expired(self->timeout), self, XSelectionRequest_timedout, 0); - Timer_start(self->timeout, REQUESTTIMEOUT); + PSC_Timer_start(self->timeout); AWAIT(xcb_change_property(c, XCB_PROP_MODE_REPLACE, self->requestor, self->property, A(INCR), 32, 1, &incr), self, XSelectionRequest_checkError); @@ -629,7 +629,7 @@ static void XSelectionConvert_done(XSelectionConvert *self, int destroy) } XSelectionConvert *next = self->next; Object_destroy(self->requestor); - Timer_destroy(self->timeout); + PSC_Timer_destroy(self->timeout); Window_returnProperty(self->selection->w, self->property); if (self->recvincr) { @@ -707,9 +707,10 @@ static void XSelectionConvert_readProperty(void *obj, unsigned sequence, } if (prop->type == A(INCR)) { + PSC_Timer_setMs(self->timeout, REQUESTTIMEOUT); PSC_Event_register(Window_propertyChanged(self->selection->w), self, XSelectionConvert_readIncr, self->property); - Timer_start(self->timeout, REQUESTTIMEOUT); + PSC_Timer_start(self->timeout); self->recvincr = 1; if (self->next) XSelectionConvert_start(self->next); return; @@ -837,9 +838,10 @@ static void XSelectionConvert_convert(XSelectionConvert *self) static void XSelectionConvert_start(XSelectionConvert *self) { - PSC_Event_register(Timer_expired(self->timeout), self, + PSC_Timer_setMs(self->timeout, CONVERTTIMEOUT); + PSC_Event_register(PSC_Timer_expired(self->timeout), self, XSelectionConvert_timedout, 0); - Timer_start(self->timeout, CONVERTTIMEOUT); + PSC_Timer_start(self->timeout); self->target = A(TARGETS); XSelectionConvert_convert(self); } @@ -889,7 +891,7 @@ void XSelection_request(XSelection *self, XSelectionType type, conversion->selection = self; conversion->next = 0; conversion->requestor = Object_ref(widget); - conversion->timeout = Timer_create(); + conversion->timeout = PSC_Timer_create(); conversion->data = 0; conversion->datalen = 0; conversion->received = received;