Skip to content

Commit

Permalink
feat: merge libtimer into libptk
Browse files Browse the repository at this point in the history
BREAKING CHANGE: lcui_set_timeout -> ptk_set_timeout, lcui_set_interval -> ptk_set_interval
  • Loading branch information
lc-soft committed Sep 23, 2024
1 parent 85f8f9f commit c1e8ac6
Show file tree
Hide file tree
Showing 13 changed files with 61 additions and 243 deletions.
14 changes: 14 additions & 0 deletions lib/ptk/include/ptk/events.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,20 @@ PTK_PUBLIC void ptk_set_event_dispatcher(ptk_event_dispatcher_t dispatcher);
PTK_PUBLIC int ptk_post_event(ptk_event_t *e);
PTK_PUBLIC void ptk_process_events(void);

PTK_PUBLIC int ptk_set_timeout(long ms, ptk_timer_cb cb, void *cb_arg);
PTK_PUBLIC int ptk_set_interval(long ms, ptk_timer_cb cb, void *cb_arg);
PTK_PUBLIC int ptk_clear_timer(int timer_id);

PTK_INLINE int ptk_clear_timeout(int timer_id)
{
return ptk_clear_timer(timer_id);
}

PTK_INLINE int ptk_clear_interval(int timer_id)
{
return ptk_clear_timer(timer_id);
}

PTK_END_DECLS

#endif
1 change: 1 addition & 0 deletions lib/ptk/include/ptk/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <pandagl/types.h>

typedef pd_context_t ptk_window_paint_t;
typedef void (*ptk_timer_cb)(void *);

typedef enum {
PTK_APP_ID_UNKNOWN,
Expand Down
22 changes: 22 additions & 0 deletions lib/ptk/src/events.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ static struct {
/** list_t<app_listener_t> */
list_t listeners;

timer_list_t *timers;

ptk_event_dispatcher_t dispatcher;
} ptk_events;

Expand Down Expand Up @@ -144,6 +146,23 @@ void ptk_tick(void)
ptk_process_event(&tick_event);
}

int ptk_clear_timer(int timer_id)
{
return timer_destroy(ptk_events.timers, timer_id);
}

int ptk_set_timeout(long ms, ptk_timer_cb cb, void *cb_arg)
{
return timer_list_add_timeout(ptk_events.timers, ms, cb,
cb_arg);
}

int ptk_set_interval(long ms, ptk_timer_cb cb, void *cb_arg)
{
return timer_list_add_interval(ptk_events.timers, ms, cb,
cb_arg);
}

int ptk_process_event(ptk_event_t *e)
{
int count = 0;
Expand All @@ -157,6 +176,7 @@ int ptk_process_event(ptk_event_t *e)
++count;
}
}
timer_list_process(ptk_events.timers);
if (ptk_events.dispatcher) {
ptk_events.dispatcher(e);
}
Expand All @@ -182,11 +202,13 @@ void ptk_set_event_dispatcher(ptk_event_dispatcher_t dispatcher)

void ptk_events_init(void)
{
ptk_events.timers = timer_list_create();
list_create(&ptk_events.queue);
}

void ptk_events_destroy(void)
{
ptk_set_event_dispatcher(NULL);
list_destroy(&ptk_events.queue, free);
timer_list_destroy(ptk_events.timers);
}
28 changes: 12 additions & 16 deletions lib/ptk/src/linux/x11clipboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
#include "x11clipboard.h"

#if defined(PTK_LINUX) && defined(PTK_HAS_LIBX11)
#include <thread.h>

#define CLIPBOARD_TIMEOUT 1000

Expand All @@ -63,8 +62,7 @@ static struct ptk_x11clipboard_module_t {
Atom xa_targets;
Atom xa_text;

thread_t observer_thread;
bool observer_thread_active;
int timer;
} ptk_x11clipboard;

void ptk_x11clipboard_notify(ptk_clipboard_t *cb)
Expand Down Expand Up @@ -96,25 +94,18 @@ void ptk_x11clipboard_execute_action(void)
clipboard_data.text = wstr;
clipboard_data.len = len;
clipboard_data.image = NULL;
ptk_x11clipboard.observer_thread_active = false;
ptk_clear_timeout(ptk_x11clipboard.timer);
ptk_x11clipboard_notify(&clipboard_data);
ptk_x11clipboard.timer = 0;
free(wstr);
}

void ptk_x11clipboard_request_timeout(void *arg)
{
int ms;
ptk_clipboard_t clipboard_data = { 0 };

for (ms = 0;
ms <= CLIPBOARD_TIMEOUT && ptk_x11clipboard.observer_thread_active;
ms += 100) {
sleep_ms(100);
}
if (ptk_x11clipboard.observer_thread_active) {
logger_debug("action timed out\n");
ptk_x11clipboard_notify(&clipboard_data);
}
logger_debug("action timed out\n");
ptk_x11clipboard_notify(&clipboard_data);
}

/**
Expand Down Expand Up @@ -198,6 +189,8 @@ int ptk_x11clipboard_request_text(ptk_clipboard_callback_t callback, void *arg)
// investigation
thread_create(&ptk_x11clipboard.observer_thread,
ptk_x11clipboard_request_timeout, NULL);
ptk_set_timeout(CLIPBOARD_TIMEOUT, ptk_x11clipboard_request_timeout,
NULL);
XConvertSelection(display, ptk_x11clipboard.xclipboard,
ptk_x11clipboard.xutf8_string, XSEL_DATA, window,
CurrentTime);
Expand Down Expand Up @@ -322,6 +315,7 @@ void ptk_x11clipboard_init(void)
ptk_x11clipboard.xa_string = XA_STRING;
ptk_x11clipboard.xa_targets = XInternAtom(display, "TARGETS", false);
ptk_x11clipboard.xa_text = XInternAtom(display, "TEXT", false);
ptk_x11clipboard.timer = 0;

ptk_on_native_event(SelectionNotify, ptk_x11clipboard_on_notify, NULL);
ptk_on_native_event(SelectionClear, ptk_x11clipboard_on_clear, NULL);
Expand All @@ -335,8 +329,10 @@ void ptk_x11clipboard_destroy(void)
ptk_off_native_event(SelectionNotify, ptk_x11clipboard_on_notify);
ptk_off_native_event(SelectionClear, ptk_x11clipboard_on_clear);
ptk_off_native_event(SelectionRequest, ptk_x11clipboard_on_request);
ptk_x11clipboard.observer_thread_active = false;
thread_join(ptk_x11clipboard.observer_thread, NULL);
if (ptk_x11clipboard.timer) {
ptk_clear_timeout(ptk_x11clipboard.timer);
}
ptk_x11clipboard.timer = 0;
}

#endif
55 changes: 0 additions & 55 deletions lib/timer/include/timer.h

This file was deleted.

92 changes: 0 additions & 92 deletions lib/timer/src/timer.c

This file was deleted.

55 changes: 0 additions & 55 deletions lib/timer/src/timer.h.in

This file was deleted.

12 changes: 0 additions & 12 deletions lib/timer/xmake.lua

This file was deleted.

1 change: 0 additions & 1 deletion src/lcui.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ static int lcui_dispatch_app_event(ptk_event_t *e)
return 0;
}
worker_run_task(lcui_app.main_worker);
lcui_process_timers();
lcui_dispatch_ui_event(e);
lcui_update_ui();
ptk_steptimer_tick(&lcui_app.timer, lcui_app_on_tick, NULL);
Expand Down
Loading

0 comments on commit c1e8ac6

Please sign in to comment.