From ef3883ed1598799b5934d74cb962e73f1211fdc2 Mon Sep 17 00:00:00 2001 From: Hans Dijkema Date: Fri, 10 Apr 2026 10:16:13 +0200 Subject: [PATCH] threading / event callback --- rktwebview.cpp | 35 +++++++++++++++++++++++++++++++++++ rktwebview.h | 2 ++ 2 files changed, 37 insertions(+) diff --git a/rktwebview.cpp b/rktwebview.cpp index c9a680a..5782645 100644 --- a/rktwebview.cpp +++ b/rktwebview.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #ifdef _WIN32 #include @@ -40,6 +41,7 @@ typedef struct { ShmQueue *command_queue; ShmQueue *command_result_queue; ShmQueue *event_queue; + void (*evt_cb)(int entries); #ifdef _WIN32 HANDLE rkt_webview_prg_pid; #else @@ -169,6 +171,23 @@ bool runRktWebview(Handle_t *handler) // Main C Interface ///////////////////////////////////////////////////////////////////// +static bool guard_go_on = false; +static std::thread *guard_thread = nullptr; + +void rkt_evt_guard(void) +{ + int waiting = rkt_webview_events_waiting(); + while(guard_go_on) { + int w = rkt_webview_events_waiting(); + if (w != waiting) { + if (handler->evt_cb != nullptr) { + handler->evt_cb(w); + } + waiting = w; + } + std::this_thread::sleep_for(std::chrono::milliseconds(5)); + } +} void rkt_webview_cleanup() { @@ -180,6 +199,12 @@ void rkt_webview_cleanup() // Cleaning up when exiting the current custodian is not enough. if (handler->valid) { + INFO0("Stopping evt_guard\n"); + guard_go_on = false; + guard_thread->join(); + delete guard_thread; + guard_thread = nullptr; + INFO0("Sending quit message\n"); handler->command_queue->enqueue(CMD_QUIT); INFO0("Message sent\n"); @@ -228,6 +253,7 @@ void rkt_webview_init() handler->name = buf; handler->function_calls = 0; handler->events = 0; + handler->evt_cb = nullptr; handler->shm_size = SHM_SIZE; handler->shm = new Shm(handler->name.data(), handler->shm_size, true); @@ -241,6 +267,9 @@ void rkt_webview_init() handler->rkt_webview_prg_started = runRktWebview(handler); if (!handler->rkt_webview_prg_started) { handler->valid = false; } #endif + // Start event guard thread + guard_go_on = true; + guard_thread = new std::thread(rkt_evt_guard); } else { handler->function_calls++; } @@ -272,6 +301,12 @@ static inline bool validHandle() #define FAIL_CALL_JS if (!validHandle()) { return nullptr; } #define FAIL_INFO FAIL_CALL_JS +void rkt_webview_register_evt_callback(void (*f)(int)) +{ + rkt_webview_init(); + handler->evt_cb = f; +} + rkt_wv_context_t rkt_webview_new_context(const char *boilerplate_js, const char *optional_server_cert_pem) { rkt_webview_init(); diff --git a/rktwebview.h b/rktwebview.h index c2e2ce8..f83bf9c 100644 --- a/rktwebview.h +++ b/rktwebview.h @@ -13,6 +13,8 @@ extern "C" { RKTWEBVIEW_EXPORT void rkt_webview_env(const char *env_cmds[]); RKTWEBVIEW_EXPORT void rkt_webview_init(); RKTWEBVIEW_EXPORT void rkt_webview_cleanup(); +RKTWEBVIEW_EXPORT void rkt_webview_register_evt_callback(void (*f)(int)); + RKTWEBVIEW_EXPORT void rkt_webview_set_loglevel(rkt_webview_loglevel_t l); RKTWEBVIEW_EXPORT rkt_data_t *rkt_webview_info();