threading / event callback

This commit is contained in:
2026-04-10 10:16:13 +02:00
parent 79d18bdd5d
commit ef3883ed15
2 changed files with 37 additions and 0 deletions

View File

@@ -2,6 +2,7 @@
#include <chrono> #include <chrono>
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
#include <thread>
#ifdef _WIN32 #ifdef _WIN32
#include <windows.h> #include <windows.h>
@@ -40,6 +41,7 @@ typedef struct {
ShmQueue *command_queue; ShmQueue *command_queue;
ShmQueue *command_result_queue; ShmQueue *command_result_queue;
ShmQueue *event_queue; ShmQueue *event_queue;
void (*evt_cb)(int entries);
#ifdef _WIN32 #ifdef _WIN32
HANDLE rkt_webview_prg_pid; HANDLE rkt_webview_prg_pid;
#else #else
@@ -169,6 +171,23 @@ bool runRktWebview(Handle_t *handler)
// Main C Interface // 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() void rkt_webview_cleanup()
{ {
@@ -180,6 +199,12 @@ void rkt_webview_cleanup()
// Cleaning up when exiting the current custodian is not enough. // Cleaning up when exiting the current custodian is not enough.
if (handler->valid) { 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"); INFO0("Sending quit message\n");
handler->command_queue->enqueue(CMD_QUIT); handler->command_queue->enqueue(CMD_QUIT);
INFO0("Message sent\n"); INFO0("Message sent\n");
@@ -228,6 +253,7 @@ void rkt_webview_init()
handler->name = buf; handler->name = buf;
handler->function_calls = 0; handler->function_calls = 0;
handler->events = 0; handler->events = 0;
handler->evt_cb = nullptr;
handler->shm_size = SHM_SIZE; handler->shm_size = SHM_SIZE;
handler->shm = new Shm(handler->name.data(), handler->shm_size, true); 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); handler->rkt_webview_prg_started = runRktWebview(handler);
if (!handler->rkt_webview_prg_started) { handler->valid = false; } if (!handler->rkt_webview_prg_started) { handler->valid = false; }
#endif #endif
// Start event guard thread
guard_go_on = true;
guard_thread = new std::thread(rkt_evt_guard);
} else { } else {
handler->function_calls++; handler->function_calls++;
} }
@@ -272,6 +301,12 @@ static inline bool validHandle()
#define FAIL_CALL_JS if (!validHandle()) { return nullptr; } #define FAIL_CALL_JS if (!validHandle()) { return nullptr; }
#define FAIL_INFO FAIL_CALL_JS #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_wv_context_t rkt_webview_new_context(const char *boilerplate_js, const char *optional_server_cert_pem)
{ {
rkt_webview_init(); rkt_webview_init();

View File

@@ -13,6 +13,8 @@ extern "C" {
RKTWEBVIEW_EXPORT void rkt_webview_env(const char *env_cmds[]); RKTWEBVIEW_EXPORT void rkt_webview_env(const char *env_cmds[]);
RKTWEBVIEW_EXPORT void rkt_webview_init(); RKTWEBVIEW_EXPORT void rkt_webview_init();
RKTWEBVIEW_EXPORT void rkt_webview_cleanup(); 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 void rkt_webview_set_loglevel(rkt_webview_loglevel_t l);
RKTWEBVIEW_EXPORT rkt_data_t *rkt_webview_info(); RKTWEBVIEW_EXPORT rkt_data_t *rkt_webview_info();