Skip to content

Commit

Permalink
Drop Timer, use PSC_Timer instead
Browse files Browse the repository at this point in the history
Moved timer implementation to poser-core, where it can be integrated
with the event loop. This way, it can use notification by signals
instead of the clunky thread notification interface, also eliminating
the need for a dummy pipe to get the notification back to the main
thread. Furthermore, timers are now multiplexed, so the system limit of
timers (max 32) only applies to timers concurrently running, not total
instances.
  • Loading branch information
Zirias committed Jul 11, 2024
1 parent fdc9d5c commit d2f4f77
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 161 deletions.
16 changes: 7 additions & 9 deletions src/bin/xmoji/keyinjector.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "keyinjector.h"

#include "timer.h"
#include "unistr.h"
#include "x11adapter.h"

Expand All @@ -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];
Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -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;
}

Expand All @@ -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;
}

101 changes: 0 additions & 101 deletions src/bin/xmoji/timer.c

This file was deleted.

15 changes: 0 additions & 15 deletions src/bin/xmoji/timer.h

This file was deleted.

28 changes: 8 additions & 20 deletions src/bin/xmoji/tooltip.c
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
#include "tooltip.h"

#include "textlabel.h"
#include "timer.h"
#include "window.h"

#include <poser/core.h>

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)
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
}
}

6 changes: 2 additions & 4 deletions src/bin/xmoji/xmoji.mk
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ xmoji_MODULES= button \
textbox \
textlabel \
textrenderer \
timer \
tooltip \
unistr \
unistrbuilder \
Expand All @@ -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 \
Expand All @@ -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
Expand Down
26 changes: 14 additions & 12 deletions src/bin/xmoji/xselection.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "xselection.h"

#include "object.h"
#include "timer.h"
#include "unistr.h"
#include "window.h"
#include "x11adapter.h"
Expand All @@ -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;
Expand Down Expand Up @@ -62,7 +61,7 @@ struct XSelectionConvert
XSelection *selection;
XSelectionConvert *next;
Widget *requestor;
Timer *timeout;
PSC_Timer *timeout;
void *data;
size_t datalen;
XSelectionCallback received;
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit d2f4f77

Please sign in to comment.