threading / event callback
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
#include <chrono>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <thread>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user