Skip to content

Commit 9918917

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 9918917

22 files changed

+82
-270
lines changed

examples/fabric/src/main.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,15 @@ void ui_fabric_on_init(ui_widget_t *w)
112112
ui_widget_on(w, "mousedown", ui_fabric_on_mousedown, NULL);
113113
ui_widget_on(w, "mousemove", ui_fabric_on_mousemove, NULL);
114114
ui_widget_on(w, "mouseup", ui_fabric_on_mouseup, NULL);
115-
data->timer = lcui_set_interval(LCUI_MAX_FRAME_MSEC,
115+
data->timer = ptk_set_interval(LCUI_MAX_FRAME_MSEC,
116116
(timer_callback)ui_fabric_on_frame, w);
117117
}
118118

119119
void ui_fabric_on_destroy(ui_widget_t *w)
120120
{
121121
ui_fabric_t *data;
122122
data = ui_widget_get_data(w, ui_fabric_proto);
123-
lcui_destroy_timer(data->timer);
123+
ptk_clear_timer(data->timer);
124124
ui_fabric_proto->proto->destroy(w);
125125
}
126126

include/LCUI.h

-1
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,4 @@
2020
#include <thread.h>
2121
#include <ptk.h>
2222
#include <worker.h>
23-
#include <timer.h>
2423
#include <LCUI/app.h>

lib/ptk/include/ptk/events.h

+15
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,21 @@ 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+
PTK_PUBLIC int ptk_reset_timer(int timer_id, long ms);
77+
78+
PTK_INLINE int ptk_clear_timeout(int timer_id)
79+
{
80+
return ptk_clear_timer(timer_id);
81+
}
82+
83+
PTK_INLINE int ptk_clear_interval(int timer_id)
84+
{
85+
return ptk_clear_timer(timer_id);
86+
}
87+
7388
PTK_END_DECLS
7489

7590
#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

+27
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,28 @@ void ptk_tick(void)
144146
ptk_process_event(&tick_event);
145147
}
146148

149+
int ptk_reset_timer(int timer_id, long ms)
150+
{
151+
return timer_reset(ptk_events.timers, timer_id, ms);
152+
}
153+
154+
int ptk_clear_timer(int timer_id)
155+
{
156+
return timer_destroy(ptk_events.timers, timer_id);
157+
}
158+
159+
int ptk_set_timeout(long ms, ptk_timer_cb cb, void *cb_arg)
160+
{
161+
return timer_list_add_timeout(ptk_events.timers, ms, cb,
162+
cb_arg);
163+
}
164+
165+
int ptk_set_interval(long ms, ptk_timer_cb cb, void *cb_arg)
166+
{
167+
return timer_list_add_interval(ptk_events.timers, ms, cb,
168+
cb_arg);
169+
}
170+
147171
int ptk_process_event(ptk_event_t *e)
148172
{
149173
int count = 0;
@@ -157,6 +181,7 @@ int ptk_process_event(ptk_event_t *e)
157181
++count;
158182
}
159183
}
184+
timer_list_process(ptk_events.timers);
160185
if (ptk_events.dispatcher) {
161186
ptk_events.dispatcher(e);
162187
}
@@ -182,11 +207,13 @@ void ptk_set_event_dispatcher(ptk_event_dispatcher_t dispatcher)
182207

183208
void ptk_events_init(void)
184209
{
210+
ptk_events.timers = timer_list_create();
185211
list_create(&ptk_events.queue);
186212
}
187213

188214
void ptk_events_destroy(void)
189215
{
190216
ptk_set_event_dispatcher(NULL);
191217
list_destroy(&ptk_events.queue, free);
218+
timer_list_destroy(ptk_events.timers);
192219
}

lib/ptk/src/linux/x11clipboard.c

+12-19
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
/**
@@ -189,15 +180,14 @@ int ptk_x11clipboard_request_text(ptk_clipboard_callback_t callback, void *arg)
189180
logger_debug("Clipboard owner not found\n");
190181
return 1;
191182
}
192-
ptk_x11clipboard.observer_thread_active = true;
193183
// @WhoAteDaCake
194184
// TODO: needs error handling if we can't access the clipboard?
195185
// TODO: some other implementations will try XA_STRING if no text was
196186
// retrieved from UTF8_STRING, however, our implementation is not
197187
// synchronous, so not sure how it would work, it needs further
198188
// investigation
199-
thread_create(&ptk_x11clipboard.observer_thread,
200-
ptk_x11clipboard_request_timeout, NULL);
189+
ptk_set_timeout(CLIPBOARD_TIMEOUT, ptk_x11clipboard_request_timeout,
190+
NULL);
201191
XConvertSelection(display, ptk_x11clipboard.xclipboard,
202192
ptk_x11clipboard.xutf8_string, XSEL_DATA, window,
203193
CurrentTime);
@@ -322,6 +312,7 @@ void ptk_x11clipboard_init(void)
322312
ptk_x11clipboard.xa_string = XA_STRING;
323313
ptk_x11clipboard.xa_targets = XInternAtom(display, "TARGETS", false);
324314
ptk_x11clipboard.xa_text = XInternAtom(display, "TEXT", false);
315+
ptk_x11clipboard.timer = 0;
325316

326317
ptk_on_native_event(SelectionNotify, ptk_x11clipboard_on_notify, NULL);
327318
ptk_on_native_event(SelectionClear, ptk_x11clipboard_on_clear, NULL);
@@ -335,8 +326,10 @@ void ptk_x11clipboard_destroy(void)
335326
ptk_off_native_event(SelectionNotify, ptk_x11clipboard_on_notify);
336327
ptk_off_native_event(SelectionClear, ptk_x11clipboard_on_clear);
337328
ptk_off_native_event(SelectionRequest, ptk_x11clipboard_on_request);
338-
ptk_x11clipboard.observer_thread_active = false;
339-
thread_join(ptk_x11clipboard.observer_thread, NULL);
329+
if (ptk_x11clipboard.timer) {
330+
ptk_clear_timeout(ptk_x11clipboard.timer);
331+
}
332+
ptk_x11clipboard.timer = 0;
340333
}
341334

342335
#endif

lib/timer/include/timer.h

-55
This file was deleted.

lib/timer/src/timer.c

-92
This file was deleted.

0 commit comments

Comments
 (0)