diff --git a/browser.c b/browser.c index efbf9f7..b3e2cbd 100644 --- a/browser.c +++ b/browser.c @@ -4,6 +4,7 @@ #include #include +/* Our includes */ #include "browser.h" #include "scheme_functions.h" @@ -15,17 +16,16 @@ load_modules(void) { } static gboolean -eventCallback(void *data) { +messageEvent(void *data) { struct QueueData *qdata = data; struct BrowserMessage *msg = g_async_queue_timeout_pop(qdata->gtk_qu, 10); if (msg != NULL) { + printf("%d\n", msg->event); + printf("Got an event\n"); switch (msg->event) { case LOAD: - printf("Got a load event\n"); - printf("Messaging Guile\n"); - printf(msg->data); - qu_push(msg->event, msg->data, qdata->guile_qu); + printf("Loading %s\n", (char*)msg->data); webkit_web_view_load_uri(qdata->webView, msg->data); break; case CLOSE: @@ -45,9 +45,10 @@ qu_push(enum BrowserEvent msg_type, char *message, GAsyncQueue *g_queue) { - struct BrowserMessage *msg = malloc( sizeof(*msg) ); + struct BrowserMessage *msg = malloc( sizeof (*msg) ); msg->data = message; + switch (msg_type) { case LOAD: msg->event = LOAD; @@ -59,9 +60,7 @@ qu_push(enum BrowserEvent msg_type, msg->event = EMPTY; break; } - g_async_queue_push(g_queue, msg); - return 1; } @@ -104,7 +103,6 @@ make_webview() { 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_encrypted_media(settings, conf_val("encrypted-media")); webkit_settings_set_enable_media_capabilities(settings, conf_val("media-capabilities")); webkit_settings_set_enable_mediasource(settings, conf_val("media-source")); @@ -141,8 +139,30 @@ closeWebViewCb(WebKitWebView *webView, 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 -launch_webkit(SCM scm_gtk_qu, SCM scm_guile_qu) { +launch_webkit(SCM scm_gtk_qu, + SCM scm_guile_qu) { + WebKitWebView *webView = make_webview(); GAsyncQueue *gtk_qu = scm_to_pointer(scm_gtk_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; + /* Initialize the queue data */ queue_data.gtk_qu = gtk_qu; queue_data.guile_qu = guile_qu; 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 */ 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 */ g_signal_connect(main_window, "destroy", G_CALLBACK(destroyWindowCb), NULL); 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 */ 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 run_repl(void *data, int argc, char **argv) { load_modules(); - SCM current_module = scm_current_module(); - GAsyncQueue *browser_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("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"); diff --git a/browser.h b/browser.h index 78a9408..9d3fde9 100644 --- a/browser.h +++ b/browser.h @@ -18,7 +18,7 @@ struct BrowserMessage { 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 SCM scm_qu_push(SCM, SCM, SCM); diff --git a/build.sh b/build.sh index 7b2e8cd..d898b64 100755 --- a/build.sh +++ b/build.sh @@ -1,2 +1,2 @@ #! /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 diff --git a/scheme_functions.h b/scheme_functions.h index d52a6e5..e2403dc 100644 --- a/scheme_functions.h +++ b/scheme_functions.h @@ -1,9 +1,16 @@ - static SCM scm_qu_pop(SCM scm_qu) { GAsyncQueue *g_queue = scm_to_pointer(scm_qu); + 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 @@ -25,5 +32,3 @@ scm_ref(const char *var_name) { /* Lookup and de-reference a Scheme value */ return scm_variable_ref(scm_c_lookup(var_name)); } - - diff --git a/schemekit.scm b/schemekit.scm index d768437..b86ff3e 100644 --- a/schemekit.scm +++ b/schemekit.scm @@ -20,3 +20,14 @@ (call-with-new-thread (lambda () (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)