Skip to content

Commit c1e8ac6

Browse files
committed
feat: merge libtimer into libptk
BREAKING CHANGE: lcui_set_timeout -> ptk_set_timeout, lcui_set_interval -> ptk_set_interval
1 parent 85f8f9f commit c1e8ac6

File tree

13 files changed

+61
-243
lines changed

13 files changed

+61
-243
lines changed

lib/ptk/include/ptk/events.h

+14
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,20 @@ PTK_PUBLIC void ptk_set_event_dispatcher(ptk_event_dispatcher_t dispatcher);
7070
PTK_PUBLIC int ptk_post_event(ptk_event_t *e);
7171
PTK_PUBLIC void ptk_process_events(void);
7272

73+
PTK_PUBLIC int ptk_set_timeout(long ms, ptk_timer_cb cb, void *cb_arg);
74+
PTK_PUBLIC int ptk_set_interval(long ms, ptk_timer_cb cb, void *cb_arg);
75+
PTK_PUBLIC int ptk_clear_timer(int timer_id);
76+
77+
PTK_INLINE int ptk_clear_timeout(int timer_id)
78+
{
79+
return ptk_clear_timer(timer_id);
80+
}
81+
82+
PTK_INLINE int ptk_clear_interval(int timer_id)
83+
{
84+
return ptk_clear_timer(timer_id);
85+
}
86+
7387
PTK_END_DECLS
7488

7589
#endif

lib/ptk/include/ptk/types.h

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <pandagl/types.h>
1717

1818
typedef pd_context_t ptk_window_paint_t;
19+
typedef void (*ptk_timer_cb)(void *);
1920

2021
typedef enum {
2122
PTK_APP_ID_UNKNOWN,

lib/ptk/src/events.c

+22
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ static struct {
2020
/** list_t<app_listener_t> */
2121
list_t listeners;
2222

23+
timer_list_t *timers;
24+
2325
ptk_event_dispatcher_t dispatcher;
2426
} ptk_events;
2527

@@ -144,6 +146,23 @@ void ptk_tick(void)
144146
ptk_process_event(&tick_event);
145147
}
146148

149+
int ptk_clear_timer(int timer_id)
150+
{
151+
return timer_destroy(ptk_events.timers, timer_id);
152+
}
153+
154+
int ptk_set_timeout(long ms, ptk_timer_cb cb, void *cb_arg)
155+
{
156+
return timer_list_add_timeout(ptk_events.timers, ms, cb,
157+
cb_arg);
158+
}
159+
160+
int ptk_set_interval(long ms, ptk_timer_cb cb, void *cb_arg)
161+
{
162+
return timer_list_add_interval(ptk_events.timers, ms, cb,
163+
cb_arg);
164+
}
165+
147166
int ptk_process_event(ptk_event_t *e)
148167
{
149168
int count = 0;
@@ -157,6 +176,7 @@ int ptk_process_event(ptk_event_t *e)
157176
++count;
158177
}
159178
}
179+
timer_list_process(ptk_events.timers);
160180
if (ptk_events.dispatcher) {
161181
ptk_events.dispatcher(e);
162182
}
@@ -182,11 +202,13 @@ void ptk_set_event_dispatcher(ptk_event_dispatcher_t dispatcher)
182202

183203
void ptk_events_init(void)
184204
{
205+
ptk_events.timers = timer_list_create();
185206
list_create(&ptk_events.queue);
186207
}
187208

188209
void ptk_events_destroy(void)
189210
{
190211
ptk_set_event_dispatcher(NULL);
191212
list_destroy(&ptk_events.queue, free);
213+
timer_list_destroy(ptk_events.timers);
192214
}

lib/ptk/src/linux/x11clipboard.c

+12-16
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
#include "x11clipboard.h"
3939

4040
#if defined(PTK_LINUX) && defined(PTK_HAS_LIBX11)
41-
#include <thread.h>
4241

4342
#define CLIPBOARD_TIMEOUT 1000
4443

@@ -63,8 +62,7 @@ static struct ptk_x11clipboard_module_t {
6362
Atom xa_targets;
6463
Atom xa_text;
6564

66-
thread_t observer_thread;
67-
bool observer_thread_active;
65+
int timer;
6866
} ptk_x11clipboard;
6967

7068
void ptk_x11clipboard_notify(ptk_clipboard_t *cb)
@@ -96,25 +94,18 @@ void ptk_x11clipboard_execute_action(void)
9694
clipboard_data.text = wstr;
9795
clipboard_data.len = len;
9896
clipboard_data.image = NULL;
99-
ptk_x11clipboard.observer_thread_active = false;
97+
ptk_clear_timeout(ptk_x11clipboard.timer);
10098
ptk_x11clipboard_notify(&clipboard_data);
99+
ptk_x11clipboard.timer = 0;
101100
free(wstr);
102101
}
103102

104103
void ptk_x11clipboard_request_timeout(void *arg)
105104
{
106-
int ms;
107105
ptk_clipboard_t clipboard_data = { 0 };
108106

109-
for (ms = 0;
110-
ms <= CLIPBOARD_TIMEOUT && ptk_x11clipboard.observer_thread_active;
111-
ms += 100) {
112-
sleep_ms(100);
113-
}
114-
if (ptk_x11clipboard.observer_thread_active) {
115-
logger_debug("action timed out\n");
116-
ptk_x11clipboard_notify(&clipboard_data);
117-
}
107+
logger_debug("action timed out\n");
108+
ptk_x11clipboard_notify(&clipboard_data);
118109
}
119110

120111
/**
@@ -198,6 +189,8 @@ int ptk_x11clipboard_request_text(ptk_clipboard_callback_t callback, void *arg)
198189
// investigation
199190
thread_create(&ptk_x11clipboard.observer_thread,
200191
ptk_x11clipboard_request_timeout, NULL);
192+
ptk_set_timeout(CLIPBOARD_TIMEOUT, ptk_x11clipboard_request_timeout,
193+
NULL);
201194
XConvertSelection(display, ptk_x11clipboard.xclipboard,
202195
ptk_x11clipboard.xutf8_string, XSEL_DATA, window,
203196
CurrentTime);
@@ -322,6 +315,7 @@ void ptk_x11clipboard_init(void)
322315
ptk_x11clipboard.xa_string = XA_STRING;
323316
ptk_x11clipboard.xa_targets = XInternAtom(display, "TARGETS", false);
324317
ptk_x11clipboard.xa_text = XInternAtom(display, "TEXT", false);
318+
ptk_x11clipboard.timer = 0;
325319

326320
ptk_on_native_event(SelectionNotify, ptk_x11clipboard_on_notify, NULL);
327321
ptk_on_native_event(SelectionClear, ptk_x11clipboard_on_clear, NULL);
@@ -335,8 +329,10 @@ void ptk_x11clipboard_destroy(void)
335329
ptk_off_native_event(SelectionNotify, ptk_x11clipboard_on_notify);
336330
ptk_off_native_event(SelectionClear, ptk_x11clipboard_on_clear);
337331
ptk_off_native_event(SelectionRequest, ptk_x11clipboard_on_request);
338-
ptk_x11clipboard.observer_thread_active = false;
339-
thread_join(ptk_x11clipboard.observer_thread, NULL);
332+
if (ptk_x11clipboard.timer) {
333+
ptk_clear_timeout(ptk_x11clipboard.timer);
334+
}
335+
ptk_x11clipboard.timer = 0;
340336
}
341337

342338
#endif

lib/timer/include/timer.h

-55
This file was deleted.

lib/timer/src/timer.c

-92
This file was deleted.

lib/timer/src/timer.h.in

-55
This file was deleted.

lib/timer/xmake.lua

-12
This file was deleted.

src/lcui.c

-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ static int lcui_dispatch_app_event(ptk_event_t *e)
9797
return 0;
9898
}
9999
worker_run_task(lcui_app.main_worker);
100-
lcui_process_timers();
101100
lcui_dispatch_ui_event(e);
102101
lcui_update_ui();
103102
ptk_steptimer_tick(&lcui_app.timer, lcui_app_on_tick, NULL);

0 commit comments

Comments
 (0)