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 25, 2024
1 parent 85f8f9f commit 9918917
Show file tree
Hide file tree
Showing 22 changed files with 82 additions and 270 deletions.
4 changes: 2 additions & 2 deletions examples/fabric/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,15 @@ void ui_fabric_on_init(ui_widget_t *w)
ui_widget_on(w, "mousedown", ui_fabric_on_mousedown, NULL);
ui_widget_on(w, "mousemove", ui_fabric_on_mousemove, NULL);
ui_widget_on(w, "mouseup", ui_fabric_on_mouseup, NULL);
data->timer = lcui_set_interval(LCUI_MAX_FRAME_MSEC,
data->timer = ptk_set_interval(LCUI_MAX_FRAME_MSEC,
(timer_callback)ui_fabric_on_frame, w);
}

void ui_fabric_on_destroy(ui_widget_t *w)
{
ui_fabric_t *data;
data = ui_widget_get_data(w, ui_fabric_proto);
lcui_destroy_timer(data->timer);
ptk_clear_timer(data->timer);
ui_fabric_proto->proto->destroy(w);
}

Expand Down
1 change: 0 additions & 1 deletion include/LCUI.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,4 @@
#include <thread.h>
#include <ptk.h>
#include <worker.h>
#include <timer.h>
#include <LCUI/app.h>
15 changes: 15 additions & 0 deletions lib/ptk/include/ptk/events.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,21 @@ 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_PUBLIC int ptk_reset_timer(int timer_id, long ms);

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
27 changes: 27 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,28 @@ void ptk_tick(void)
ptk_process_event(&tick_event);
}

int ptk_reset_timer(int timer_id, long ms)
{
return timer_reset(ptk_events.timers, timer_id, ms);
}

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 +181,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 +207,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);
}
31 changes: 12 additions & 19 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 @@ -189,15 +180,14 @@ int ptk_x11clipboard_request_text(ptk_clipboard_callback_t callback, void *arg)
logger_debug("Clipboard owner not found\n");
return 1;
}
ptk_x11clipboard.observer_thread_active = true;
// @WhoAteDaCake
// TODO: needs error handling if we can't access the clipboard?
// TODO: some other implementations will try XA_STRING if no text was
// retrieved from UTF8_STRING, however, our implementation is not
// synchronous, so not sure how it would work, it needs further
// 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 +312,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 +326,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.

Loading

0 comments on commit 9918917

Please sign in to comment.