Added callback for events.
This commit is contained in:
@@ -23,6 +23,7 @@ add_library(rktwebview SHARED
|
||||
json.cpp json.h
|
||||
utils.h
|
||||
utils.cpp
|
||||
memqueue.h memqueue.cpp
|
||||
)
|
||||
|
||||
add_executable(rktwebview_prg
|
||||
|
||||
2
memqueue.cpp
Normal file
2
memqueue.cpp
Normal file
@@ -0,0 +1,2 @@
|
||||
#include "memqueue.h"
|
||||
|
||||
72
memqueue.h
Normal file
72
memqueue.h
Normal file
@@ -0,0 +1,72 @@
|
||||
#ifndef MEMQUEUE_H
|
||||
#define MEMQUEUE_H
|
||||
|
||||
#include <string>
|
||||
#include <mutex>
|
||||
|
||||
class MemQueueItem
|
||||
{
|
||||
public:
|
||||
MemQueueItem *next;
|
||||
std::string _event;
|
||||
int _wv;
|
||||
};
|
||||
|
||||
class MemQueue
|
||||
{
|
||||
private:
|
||||
std::mutex _mutex;
|
||||
int _depth;
|
||||
MemQueueItem *_first;
|
||||
MemQueueItem *_last;
|
||||
|
||||
public:
|
||||
void enqueue(int w, std::string s) {
|
||||
_mutex.lock();
|
||||
MemQueueItem *item = new MemQueueItem();
|
||||
item->next = nullptr;
|
||||
item->_event = s;
|
||||
item->_wv = w;
|
||||
if (_last == nullptr) {
|
||||
_last = item;
|
||||
_first = item;
|
||||
} else {
|
||||
_last->next = item;
|
||||
_last = item;
|
||||
}
|
||||
_depth += 1;
|
||||
_mutex.unlock();
|
||||
}
|
||||
|
||||
bool dequeue(int &wv, std::string &s) {
|
||||
_mutex.lock();
|
||||
if (_first == nullptr) {
|
||||
_mutex.unlock();
|
||||
return false;
|
||||
}
|
||||
MemQueueItem *item = _first;
|
||||
_first = _first->next;
|
||||
if (_first == nullptr) {
|
||||
_last = nullptr;
|
||||
}
|
||||
s = item->_event;
|
||||
wv = item->_wv;
|
||||
free(item);
|
||||
_depth -= 1;
|
||||
_mutex.unlock();
|
||||
return true;
|
||||
}
|
||||
|
||||
int depth() {
|
||||
return _depth;
|
||||
}
|
||||
|
||||
public:
|
||||
MemQueue() {
|
||||
_depth = 0;
|
||||
_first = nullptr;
|
||||
_last = nullptr;
|
||||
}
|
||||
};
|
||||
|
||||
#endif // MEMQUEUE_H
|
||||
@@ -20,6 +20,7 @@
|
||||
#include "rktwebview.h"
|
||||
#include "rkt_protocol.h"
|
||||
#include "shmqueue.h"
|
||||
#include "memqueue.h"
|
||||
#include "json.h"
|
||||
#include "utils.h"
|
||||
|
||||
@@ -41,7 +42,6 @@ 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
|
||||
@@ -53,6 +53,12 @@ typedef struct {
|
||||
int function_calls;
|
||||
int events;
|
||||
std::string log_file;
|
||||
|
||||
void (*evt_cb)(int entries);
|
||||
MemQueue *evt_queue;
|
||||
std::thread *guard_thread;
|
||||
bool guard_go_on;
|
||||
|
||||
} Handle_t;
|
||||
|
||||
Handle_t *handler = nullptr;
|
||||
@@ -171,21 +177,19 @@ bool runRktWebview(Handle_t *handler)
|
||||
// Main C Interface
|
||||
/////////////////////////////////////////////////////////////////////
|
||||
|
||||
static bool guard_go_on = false;
|
||||
static std::thread *guard_thread = nullptr;
|
||||
#define EVT_GUARD_STOP -93273
|
||||
|
||||
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);
|
||||
while(handler->guard_go_on) {
|
||||
int wv;
|
||||
std::string data;
|
||||
if (handler->event_queue->dequeue(wv, data, true)) {
|
||||
if (wv != EVT_GUARD_STOP) {
|
||||
handler->evt_queue->enqueue(wv, data);
|
||||
handler->evt_cb(1);
|
||||
}
|
||||
waiting = w;
|
||||
}
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(5));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -199,11 +203,14 @@ void rkt_webview_cleanup()
|
||||
// Cleaning up when exiting the current custodian is not enough.
|
||||
|
||||
if (handler->valid) {
|
||||
if (handler->evt_cb != nullptr) {
|
||||
INFO0("Stopping evt_guard\n");
|
||||
guard_go_on = false;
|
||||
guard_thread->join();
|
||||
delete guard_thread;
|
||||
guard_thread = nullptr;
|
||||
handler->guard_go_on = false;
|
||||
handler->event_queue->enqueue(EVT_GUARD_STOP);
|
||||
handler->guard_thread->join();
|
||||
delete handler->guard_thread;
|
||||
handler->guard_thread = nullptr;
|
||||
}
|
||||
|
||||
INFO0("Sending quit message\n");
|
||||
handler->command_queue->enqueue(CMD_QUIT);
|
||||
@@ -225,6 +232,7 @@ void rkt_webview_cleanup()
|
||||
delete handler->command_result_queue;
|
||||
delete handler->command_queue;
|
||||
delete handler->shm;
|
||||
delete handler->evt_queue;
|
||||
delete handler;
|
||||
handler = nullptr;
|
||||
}
|
||||
@@ -253,7 +261,6 @@ 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);
|
||||
@@ -267,9 +274,10 @@ 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);
|
||||
handler->guard_thread = nullptr;
|
||||
handler->evt_cb = nullptr;
|
||||
handler->evt_queue = new MemQueue();
|
||||
handler->guard_go_on = false;
|
||||
} else {
|
||||
handler->function_calls++;
|
||||
}
|
||||
@@ -304,7 +312,12 @@ static inline bool validHandle()
|
||||
void rkt_webview_register_evt_callback(void (*f)(int))
|
||||
{
|
||||
rkt_webview_init();
|
||||
|
||||
handler->evt_cb = f;
|
||||
if (handler->guard_thread == nullptr) {
|
||||
handler->guard_go_on = true;
|
||||
handler->guard_thread = new std::thread(rkt_evt_guard);
|
||||
}
|
||||
}
|
||||
|
||||
rkt_wv_context_t rkt_webview_new_context(const char *boilerplate_js, const char *optional_server_cert_pem)
|
||||
@@ -563,12 +576,33 @@ result_t rkt_webview_message_box(rktwebview_t w, const char *title, const char *
|
||||
|
||||
int rkt_webview_events_waiting()
|
||||
{
|
||||
if (handler->evt_cb != nullptr) {
|
||||
return handler->evt_queue->depth();
|
||||
} else {
|
||||
return handler->event_queue->depth();
|
||||
}
|
||||
}
|
||||
|
||||
rkt_data_t *rkt_webview_get_event()
|
||||
{
|
||||
//fprintf(stderr, "rkt_webview_get_event\n");
|
||||
if (handler->evt_cb != nullptr) {
|
||||
int wv;
|
||||
std::string data;
|
||||
if (handler->evt_queue->dequeue(wv, data)) {
|
||||
rkt_data_t *d = reinterpret_cast<rkt_data_t *>(malloc(sizeof(rkt_data_t)));
|
||||
d->kind = rkt_data_kind_t::event;
|
||||
handler->events += 1;
|
||||
size_t ds = data.size();
|
||||
d->data.event.event = reinterpret_cast<char *>(malloc(ds + 1));
|
||||
memcpy(d->data.event.event, data.c_str(), ds);
|
||||
d->data.event.event[ds] = '\0';
|
||||
d->data.event.wv = wv;
|
||||
return d;
|
||||
} else {
|
||||
return nullptr;
|
||||
}
|
||||
} else {
|
||||
int wv;
|
||||
std::string data;
|
||||
if (handler->event_queue->dequeue(wv, data, false)) {
|
||||
@@ -588,6 +622,7 @@ rkt_data_t *rkt_webview_get_event()
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void rkt_webview_env(const char *env_cmds[])
|
||||
{
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
|
||||
void evt_cb(int n)
|
||||
{
|
||||
fprintf(stderr, "events waiting: %d\n", n);
|
||||
fprintf(stderr, "events waiting: %d - %d\n", n, rkt_webview_events_waiting());
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
|
||||
Reference in New Issue
Block a user