Browse Source

Fix warnings, add bi-directional message queue

master
Wesley Kerfoot 5 years ago
parent
commit
2027bdee5b
  1. 47
      browser.c
  2. 2
      browser.h
  3. 2
      build.sh
  4. 11
      scheme_functions.h
  5. 11
      schemekit.scm

47
browser.c

@ -4,6 +4,7 @@
#include <libguile.h> #include <libguile.h>
#include <libguile/strings.h> #include <libguile/strings.h>
/* Our includes */
#include "browser.h" #include "browser.h"
#include "scheme_functions.h" #include "scheme_functions.h"
@ -15,17 +16,16 @@ load_modules(void) {
} }
static gboolean static gboolean
eventCallback(void *data) { messageEvent(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->gtk_qu, 10);
if (msg != NULL) { if (msg != NULL) {
printf("%d\n", msg->event);
printf("Got an event\n");
switch (msg->event) { switch (msg->event) {
case LOAD: case LOAD:
printf("Got a load event\n"); printf("Loading %s\n", (char*)msg->data);
printf("Messaging Guile\n");
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:
@ -45,9 +45,10 @@ qu_push(enum BrowserEvent msg_type,
char *message, char *message,
GAsyncQueue *g_queue) { GAsyncQueue *g_queue) {
struct BrowserMessage *msg = malloc( sizeof(*msg) ); struct BrowserMessage *msg = malloc( sizeof (*msg) );
msg->data = message; msg->data = message;
switch (msg_type) { switch (msg_type) {
case LOAD: case LOAD:
msg->event = LOAD; msg->event = LOAD;
@ -59,9 +60,7 @@ qu_push(enum BrowserEvent msg_type,
msg->event = EMPTY; msg->event = EMPTY;
break; break;
} }
g_async_queue_push(g_queue, msg); g_async_queue_push(g_queue, msg);
return 1; return 1;
} }
@ -104,7 +103,6 @@ make_webview() {
webkit_settings_set_media_playback_requires_user_gesture(settings, conf_val("media-gestures")); webkit_settings_set_media_playback_requires_user_gesture(settings, conf_val("media-gestures"));
webkit_settings_set_enable_media_stream(settings, conf_val("media-stream")); webkit_settings_set_enable_media_stream(settings, conf_val("media-stream"));
webkit_settings_set_enable_encrypted_media(settings, conf_val("encrypted-media"));
webkit_settings_set_enable_media_capabilities(settings, conf_val("media-capabilities")); webkit_settings_set_enable_media_capabilities(settings, conf_val("media-capabilities"));
webkit_settings_set_enable_mediasource(settings, conf_val("media-source")); webkit_settings_set_enable_mediasource(settings, conf_val("media-source"));
@ -141,8 +139,30 @@ closeWebViewCb(WebKitWebView *webView,
return TRUE; return TRUE;
} }
static void
webViewChanged(WebKitWebView *web_view,
WebKitLoadEvent load_event,
struct QueueData *qdata) {
switch (load_event) {
case WEBKIT_LOAD_STARTED:
case WEBKIT_LOAD_REDIRECTED:
case WEBKIT_LOAD_COMMITTED:
/* The load is being performed. Current URI is
* the final one and it won't change unless a new
* load is requested or a navigation within the
* same page is performed */
break;
case WEBKIT_LOAD_FINISHED:
qu_push(LOAD, (char*)webkit_web_view_get_uri(web_view), qdata->guile_qu);
/* Load finished, we can now stop the spinner */
break;
}
}
SCM SCM
launch_webkit(SCM scm_gtk_qu, SCM scm_guile_qu) { launch_webkit(SCM scm_gtk_qu,
SCM scm_guile_qu) {
WebKitWebView *webView = make_webview(); WebKitWebView *webView = make_webview();
GAsyncQueue *gtk_qu = scm_to_pointer(scm_gtk_qu); GAsyncQueue *gtk_qu = scm_to_pointer(scm_gtk_qu);
GAsyncQueue *guile_qu = scm_to_pointer(scm_guile_qu); GAsyncQueue *guile_qu = scm_to_pointer(scm_guile_qu);
@ -163,11 +183,12 @@ launch_webkit(SCM scm_gtk_qu, SCM scm_guile_qu) {
struct QueueData queue_data; struct QueueData queue_data;
/* Initialize the queue data */
queue_data.gtk_qu = gtk_qu; queue_data.gtk_qu = gtk_qu;
queue_data.guile_qu = guile_qu; queue_data.guile_qu = guile_qu;
queue_data.webView = webView; queue_data.webView = webView;
g_idle_add(eventCallback, &queue_data); g_idle_add(messageEvent, &queue_data);
/* Create an 800x600 window that will contain the browser instance */ /* Create an 800x600 window that will contain the browser instance */
GtkWidget *main_window = gtk_window_new(GTK_WINDOW_TOPLEVEL); GtkWidget *main_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
@ -181,6 +202,7 @@ launch_webkit(SCM scm_gtk_qu, SCM scm_guile_qu) {
/* closed, the program will exit */ /* closed, the program will exit */
g_signal_connect(main_window, "destroy", G_CALLBACK(destroyWindowCb), NULL); g_signal_connect(main_window, "destroy", G_CALLBACK(destroyWindowCb), NULL);
g_signal_connect(webView, "close", G_CALLBACK(closeWebViewCb), main_window); g_signal_connect(webView, "close", G_CALLBACK(closeWebViewCb), main_window);
g_signal_connect(webView, "load-changed", G_CALLBACK(webViewChanged), &queue_data);
/* Load a web page into the browser instance */ /* Load a web page into the browser instance */
webkit_web_view_load_uri(webView, "http://google.com/"); webkit_web_view_load_uri(webView, "http://google.com/");
@ -200,8 +222,6 @@ launch_webkit(SCM scm_gtk_qu, SCM scm_guile_qu) {
static void static void
run_repl(void *data, int argc, char **argv) { run_repl(void *data, int argc, char **argv) {
load_modules(); load_modules();
SCM current_module = scm_current_module();
GAsyncQueue *browser_event_qu = g_async_queue_new(); GAsyncQueue *browser_event_qu = g_async_queue_new();
GAsyncQueue *guile_event_qu = g_async_queue_new(); GAsyncQueue *guile_event_qu = g_async_queue_new();
@ -213,6 +233,7 @@ run_repl(void *data, int argc, char **argv) {
scm_c_define_gsubr("launch-webkit-blocking", 2, 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, scm_qu_push); scm_c_define_gsubr("qu-push", 3, 0, 0, scm_qu_push);
scm_c_define_gsubr("qu-pop", 1, 0, 0, scm_qu_pop);
scm_c_primitive_load("./schemekit.scm"); scm_c_primitive_load("./schemekit.scm");

2
browser.h

@ -18,7 +18,7 @@ struct BrowserMessage {
static SCM scm_ref(const char *); static SCM scm_ref(const char *);
static int read_config_val(char * const); static int conf_val(char * const);
static int qu_push(enum BrowserEvent, char *, GAsyncQueue *); static int qu_push(enum BrowserEvent, char *, GAsyncQueue *);
static SCM scm_qu_push(SCM, SCM, SCM); static SCM scm_qu_push(SCM, SCM, SCM);

2
build.sh

@ -1,2 +1,2 @@
#! /usr/bin/bash #! /usr/bin/bash
gcc $(pkg-config --cflags --libs guile-2.2 gtk+-3.0 webkit2gtk-4.0) browser.c -o schemekit gcc -Wall $(pkg-config --cflags --libs guile-2.2 gtk+-3.0 webkit2gtk-4.0) browser.c -o schemekit

11
scheme_functions.h

@ -1,9 +1,16 @@
static SCM static SCM
scm_qu_pop(SCM scm_qu) { scm_qu_pop(SCM scm_qu) {
GAsyncQueue *g_queue = scm_to_pointer(scm_qu); GAsyncQueue *g_queue = scm_to_pointer(scm_qu);
struct BrowserMessage *msg = g_async_queue_timeout_pop(g_queue, 10); struct BrowserMessage *msg = g_async_queue_timeout_pop(g_queue, 10);
if (msg != NULL) {
printf(msg->data);
return scm_from_locale_string(msg->data);
}
else {
return SCM_BOOL_F;
}
} }
static SCM static SCM
@ -25,5 +32,3 @@ scm_ref(const char *var_name) {
/* Lookup and de-reference a Scheme value */ /* Lookup and de-reference a Scheme value */
return scm_variable_ref(scm_c_lookup(var_name)); return scm_variable_ref(scm_c_lookup(var_name));
} }

11
schemekit.scm

@ -20,3 +20,14 @@
(call-with-new-thread (call-with-new-thread
(lambda () (lambda ()
(launch-webkit-blocking gtk-qu guile-qu))) (launch-webkit-blocking gtk-qu guile-qu)))
(define (handle-events)
(begin
(sleep 1)
(let ((msg (qu-pop guile-qu)))
(if msg
(display (format "~a loaded\n" msg))
'())
(handle-events))))
(call-with-new-thread handle-events)

Loading…
Cancel
Save