Browse Source

Factor out scheme only queue push

master
Wesley Kerfoot 5 years ago
parent
commit
3e80f09952
  1. 66
      browser.c
  2. 22
      browser.h
  3. 4
      schemekit.scm

66
browser.c

@ -13,30 +13,18 @@ load_modules(void) {
scm_c_use_module("ice-9 hash-table"); scm_c_use_module("ice-9 hash-table");
} }
struct QueueData {
GAsyncQueue *queue;
WebKitWebView *webView;
};
struct BrowserMessage {
enum BrowserEvent {
LOAD = 0,
CLOSE = 1,
EMPTY = 2
} event;
void *data;
};
static gboolean static gboolean
eventCallback(void *data) { eventCallback(void *data) {
struct QueueData *qdata = data; struct QueueData *qdata = data;
struct BrowserMessage *msg = g_async_queue_timeout_pop(qdata->gtk_qu, 10);
struct BrowserMessage *msg = g_async_queue_timeout_pop(qdata->queue, 10);
if (msg != NULL) { if (msg != NULL) {
switch (msg->event) { switch (msg->event) {
case LOAD: case LOAD:
printf("Got a load event\n"); printf("Got a load event\n");
printf("Messaging Guile\n");
printf(msg->data); printf(msg->data);
qu_push(msg->event, msg->data, qdata->guile_qu);
webkit_web_view_load_uri(qdata->webView, msg->data); webkit_web_view_load_uri(qdata->webView, msg->data);
break; break;
case CLOSE: case CLOSE:
@ -51,16 +39,14 @@ eventCallback(void *data) {
return TRUE; return TRUE;
} }
static SCM static int
qu_push(SCM scm_msg_type, qu_push(enum BrowserEvent msg_type,
SCM scm_message, char *message,
SCM scm_qu) { GAsyncQueue *g_queue) {
struct BrowserMessage *msg = malloc( sizeof(*msg) ); struct BrowserMessage *msg = malloc( sizeof(*msg) );
int msg_type = scm_to_int(scm_msg_type); msg->data = message;
msg->data = scm_to_locale_string(scm_message);
switch (msg_type) { switch (msg_type) {
case LOAD: case LOAD:
msg->event = LOAD; msg->event = LOAD;
@ -73,9 +59,22 @@ qu_push(SCM scm_msg_type,
break; break;
} }
GAsyncQueue *g_queue = scm_to_pointer(scm_qu);
g_async_queue_push(g_queue, msg); g_async_queue_push(g_queue, msg);
return 1;
}
static SCM
scm_qu_push(SCM scm_msg_type,
SCM scm_message,
SCM scm_qu) {
enum BrowserEvent msg_type = scm_to_int(scm_msg_type);
char *data = scm_to_locale_string(scm_message);
GAsyncQueue *g_queue = scm_to_pointer(scm_qu);
qu_push(msg_type, data, g_queue);
return SCM_BOOL_T; return SCM_BOOL_T;
} }
@ -162,9 +161,10 @@ closeWebViewCb(WebKitWebView *webView,
} }
SCM SCM
launch_webkit(SCM qu) { launch_webkit(SCM scm_gtk_qu, SCM scm_guile_qu) {
WebKitWebView *webView = make_webview(); WebKitWebView *webView = make_webview();
GAsyncQueue *g_queue = scm_to_pointer(qu); GAsyncQueue *gtk_qu = scm_to_pointer(scm_gtk_qu);
GAsyncQueue *guile_qu = scm_to_pointer(scm_guile_qu);
/* Get a default webkit context for modifying the cache policy */ /* Get a default webkit context for modifying the cache policy */
WebKitWebContext *webkit_ctx = webkit_web_context_get_default(); WebKitWebContext *webkit_ctx = webkit_web_context_get_default();
@ -182,7 +182,8 @@ launch_webkit(SCM qu) {
struct QueueData queue_data; struct QueueData queue_data;
queue_data.queue = g_queue; queue_data.gtk_qu = gtk_qu;
queue_data.guile_qu = guile_qu;
queue_data.webView = webView; queue_data.webView = webView;
g_idle_add(eventCallback, &queue_data); g_idle_add(eventCallback, &queue_data);
@ -220,12 +221,17 @@ run_repl(void *data, int argc, char **argv) {
load_modules(); load_modules();
SCM current_module = scm_current_module(); SCM current_module = scm_current_module();
GAsyncQueue *message_qu = g_async_queue_new(); GAsyncQueue *browser_event_qu = g_async_queue_new();
GAsyncQueue *guile_event_qu = g_async_queue_new();
/* Used for passing messages to the GTK thread */
scm_c_define("gtk-qu", scm_from_pointer(browser_event_qu, NULL));
scm_c_define("message-qu", scm_from_pointer(message_qu, NULL)); /* Used for passing messages back to the Guile thread */
scm_c_define("guile-qu", scm_from_pointer(guile_event_qu, NULL));
scm_c_define_gsubr("launch-webkit-blocking", 1, 0, 0, launch_webkit); scm_c_define_gsubr("launch-webkit-blocking", 2, 0, 0, launch_webkit);
scm_c_define_gsubr("qu-push", 3, 0, 0, qu_push); scm_c_define_gsubr("qu-push", 3, 0, 0, scm_qu_push);
scm_c_primitive_load("./schemekit.scm"); scm_c_primitive_load("./schemekit.scm");

22
browser.h

@ -1,2 +1,24 @@
struct QueueData {
GAsyncQueue *gtk_qu;
GAsyncQueue *guile_qu;
WebKitWebView *webView;
};
enum BrowserEvent {
LOAD = 0,
CLOSE = 1,
EMPTY = 2
};
struct BrowserMessage {
enum BrowserEvent event;
void *data;
};
static SCM scm_ref(const char *); static SCM scm_ref(const char *);
static int read_config_val(char * const); static int read_config_val(char * const);
static SCM qu_push(enum BrowserEvent, char *, GAsyncQueue *);

4
schemekit.scm

@ -15,8 +15,8 @@
(qu-push (qu-push
load-event load-event
url url
message-qu)) gtk-qu))
(call-with-new-thread (call-with-new-thread
(lambda () (lambda ()
(launch-webkit-blocking message-qu))) (launch-webkit-blocking gtk-qu guile-qu)))

Loading…
Cancel
Save