158 lines
3.2 KiB
C++
158 lines
3.2 KiB
C++
#include "rktwebview_internal.h"
|
|
|
|
#include <malloc.h>
|
|
#include <string.h>
|
|
#include <chrono>
|
|
#include <stdint.h>
|
|
#include <unistd.h>
|
|
#include <fcntl.h>
|
|
#include "rktwebview_qt.h"
|
|
#include "webviewapp.h"
|
|
|
|
uint64_t current_ms() {
|
|
using namespace std::chrono;
|
|
return duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
|
|
}
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
// Main C Interface
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
static int pipefd[2];
|
|
static bool started = false;
|
|
static pid_t webview_process;
|
|
static bool cannot_fork_or_pipe = false;
|
|
|
|
Rktwebview_qt *handler = nullptr;
|
|
|
|
void rkt_webview_init()
|
|
{
|
|
/*
|
|
if (!started) {
|
|
if (pipe(pipefd) != -1) {
|
|
webview_process = fork();
|
|
|
|
if (webview_process < 0)
|
|
cannot_fork_or_pipe = true;
|
|
} else if (webview_process == 0) {
|
|
WebViewApp app;
|
|
|
|
|
|
|
|
started = true;
|
|
} else {
|
|
cannot_fork_or_pipe = true;
|
|
}
|
|
}
|
|
*/
|
|
|
|
if (handler == nullptr) {
|
|
handler = new Rktwebview_qt(&handler);
|
|
}
|
|
}
|
|
|
|
int rkt_webview_create(int parent)
|
|
{
|
|
//rkt_webview_init();
|
|
return handler->rktWebViewCreate(parent);
|
|
}
|
|
|
|
void rkt_webview_close(int wv)
|
|
{
|
|
handler->rktWebViewClose(wv);
|
|
}
|
|
|
|
result_t rkt_webview_set_url(int wv, const char *url)
|
|
{
|
|
result_t r = handler->rktSetUrl(wv, url);
|
|
return r;
|
|
}
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
// Supporting functions
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
void queue_init(queue_t **q)
|
|
{
|
|
*q = static_cast<queue_t *>(malloc(sizeof(queue_t)));
|
|
queue_t *Q = *q;
|
|
Q->length = 0;
|
|
Q->first = nullptr;
|
|
Q->last = nullptr;
|
|
}
|
|
|
|
void enqueue(queue_t *q, item_t item)
|
|
{
|
|
queue_item_t *itm = (queue_item_t *) malloc(sizeof(queue_item_t));
|
|
itm->item.context = item.context;
|
|
itm->item.data = strdup(item.data);
|
|
if (q->first == nullptr) {
|
|
q->first = itm;
|
|
q->last = itm;
|
|
itm->prev = nullptr;
|
|
itm->next = nullptr;
|
|
q->length = 1;
|
|
} else {
|
|
itm->prev = q->last;
|
|
itm->next = nullptr;
|
|
q->last->next = itm;
|
|
q->last = itm;
|
|
q->length += 1;
|
|
}
|
|
}
|
|
|
|
bool dequeue(queue_t *q, item_t *item)
|
|
{
|
|
if (q->length == 0) {
|
|
item->context = CONTEXT_INVALID;
|
|
item->data = nullptr;
|
|
return false;
|
|
} else {
|
|
queue_item_t *itm = q->first;
|
|
q->first = q->first->next;
|
|
q->length -= 1;
|
|
if (q->length == 0) {
|
|
q->first = nullptr;
|
|
q->last = nullptr;
|
|
}
|
|
item->context = itm->item.context;
|
|
item->data = itm->item.data;
|
|
free(itm);
|
|
return true;
|
|
}
|
|
}
|
|
|
|
int queue_length(queue_t *q)
|
|
{
|
|
return q->length;
|
|
}
|
|
|
|
void queue_destroy(queue_t *q)
|
|
{
|
|
item_t i;
|
|
while(dequeue(q, &i)) {
|
|
free(i.data);
|
|
}
|
|
free(q);
|
|
}
|
|
|
|
void free_item(item_t item)
|
|
{
|
|
free(item.data);
|
|
}
|
|
|
|
|
|
|
|
void rkt_webview_process_events(int for_ms)
|
|
{
|
|
//rkt_webview_init();
|
|
|
|
int64_t start_ms = current_ms();
|
|
int64_t end_ms = start_ms + for_ms;
|
|
|
|
while (current_ms() < end_ms) {
|
|
usleep(500); // sleep 0.5 ms
|
|
handler->doEvents();
|
|
}
|
|
}
|