From df00b8cf23273c7396a9277c4add4814052a3146 Mon Sep 17 00:00:00 2001 From: Matthew Bowra-Dean Date: Thu, 25 Nov 2010 00:51:48 +1300 Subject: [PATCH] Some refactoring. --- OpenRA.Launcher.Gtk/bridge.c | 87 ++++++++++++++++++++++++++++++++++++ OpenRA.Launcher.Gtk/bridge.h | 11 +++++ OpenRA.Launcher.Gtk/main.c | 61 +------------------------ 3 files changed, 99 insertions(+), 60 deletions(-) create mode 100644 OpenRA.Launcher.Gtk/bridge.c create mode 100644 OpenRA.Launcher.Gtk/bridge.h diff --git a/OpenRA.Launcher.Gtk/bridge.c b/OpenRA.Launcher.Gtk/bridge.c new file mode 100644 index 0000000000..3811895d49 --- /dev/null +++ b/OpenRA.Launcher.Gtk/bridge.c @@ -0,0 +1,87 @@ +/* + * Copyright 2007-2010 The OpenRA Developers (see AUTHORS) + * This file is part of OpenRA, which is free software. It is made + * available to you under the terms of the GNU General Public License + * as published by the Free Software Foundation. For more information, + * see LICENSE. + */ + +#include +#include +#include + +#define JS_STR(str) JSStringCreateWithUTF8CString(str) +#define JS_FUNC(ctx, callback) JSObjectMakeFunctionWithCallback(ctx, NULL, \ + callback) + +JSValueRef js_log(JSContextRef ctx, JSObjectRef func, JSObjectRef this, + size_t argc, const JSValueRef argv[], + JSValueRef * exception) +{ + JSValueRef return_value = JSValueMakeNull(ctx); + if (argc < 1) + { + *exception = JSValueMakeString(ctx, JS_STR("Not enough args")); + return return_value; + } + + if (JSValueIsString(ctx, argv[0])) + { + char buffer[1024]; + JSStringRef s = JSValueToStringCopy(ctx, argv[0], NULL); + JSStringGetUTF8CString(s, buffer, 1024); + g_message("JS Log: %s", buffer); + return return_value; + } + else + { + *exception = JSValueMakeString(ctx, JS_STR("Tried to log something other than a string")); + return return_value; + } +} + +JSValueRef js_exists_in_mod(JSContextRef ctx, JSObjectRef func, + JSObjectRef this, size_t argc, + const JSValueRef argv[], JSValueRef * exception) +{ + if (argc < 2) + { + *exception = JSValueMakeString(ctx, JS_STR("Not enough args")); + return JSValueMakeNull(ctx); + } + + return JSValueMakeNumber(ctx, 1); +} + +void js_add_functions(JSGlobalContextRef ctx, JSObjectRef target, char ** names, + JSObjectCallAsFunctionCallback * callbacks, size_t count) +{ + int i; + for (i = 0; i < count; i++) + { + JSObjectRef func = JS_FUNC(ctx, callbacks[i]); + JSObjectSetProperty(ctx, target, JS_STR(names[i]), func, kJSPropertyAttributeNone, NULL); + } +} + +void bind_js_bridge(WebKitWebView * view, WebKitWebFrame * frame, + gpointer context, gpointer window_object, + gpointer user_data) +{ + JSGlobalContextRef js_ctx; + JSObjectRef window_obj, external_obj; + + int func_count = 2; + char * names[] = { "log", "existsInMod" }; + JSObjectCallAsFunctionCallback callbacks[] = { js_log, js_exists_in_mod }; + + js_ctx = (JSGlobalContextRef)context; + + external_obj = JSObjectMake(js_ctx, NULL, NULL); + + window_obj = (JSObjectRef)window_object; + JSObjectSetProperty(js_ctx, window_obj, JS_STR("external"), + external_obj, 0, NULL); + + js_add_functions(js_ctx, external_obj, names, callbacks, func_count); +} diff --git a/OpenRA.Launcher.Gtk/bridge.h b/OpenRA.Launcher.Gtk/bridge.h new file mode 100644 index 0000000000..fdf4042917 --- /dev/null +++ b/OpenRA.Launcher.Gtk/bridge.h @@ -0,0 +1,11 @@ +/* + * Copyright 2007-2010 The OpenRA Developers (see AUTHORS) + * This file is part of OpenRA, which is free software. It is made + * available to you under the terms of the GNU General Public License + * as published by the Free Software Foundation. For more information, + * see LICENSE. + */ + +void bind_js_bridge(WebKitWebView * view, WebKitWebFrame * frame, + gpointer context, gpointer window_object, + gpointer user_data); diff --git a/OpenRA.Launcher.Gtk/main.c b/OpenRA.Launcher.Gtk/main.c index 2231ebc26f..c62cfbad10 100644 --- a/OpenRA.Launcher.Gtk/main.c +++ b/OpenRA.Launcher.Gtk/main.c @@ -10,13 +10,9 @@ #include #include #include -#include #include "server.h" - -#define JS_STR(str) JSStringCreateWithUTF8CString(str) -#define JS_FUNC(ctx, callback) JSObjectMakeFunctionWithCallback(ctx, NULL, \ - callback) +#include "bridge.h" GtkWindow * window; WebKitWebView * browser; @@ -29,61 +25,6 @@ gboolean window_delete(GtkWidget * widget, GdkEvent * event, return FALSE; } -JSValueRef js_log(JSContextRef ctx, JSObjectRef func, JSObjectRef this, - size_t argc, const JSValueRef argv[], - JSValueRef * exception) -{ - JSValueRef return_value = JSValueMakeNull(ctx); - if (argc < 1) - { - *exception = JSValueMakeString(ctx, JS_STR("Not enough args")); - return return_value; - } - - if (JSValueIsString(ctx, argv[0])) - { - char buffer[1024]; - JSStringRef s = JSValueToStringCopy(ctx, argv[0], NULL); - JSStringGetUTF8CString(s, buffer, 1024); - g_message("JS Log: %s", buffer); - return return_value; - } - else - { - *exception = JSValueMakeString(ctx, JS_STR("Tried to log something other than a string")); - return return_value; - } -} - -JSValueRef js_exists_in_mod(JSContextRef ctx, JSObjectRef func, - JSObjectRef this, size_t argc, - const JSValueRef argv[], JSValueRef * exception) -{ - return JSValueMakeNumber(ctx, 1); -} - -void bind_js_bridge(WebKitWebView * view, WebKitWebFrame * frame, - gpointer context, gpointer window_object, - gpointer user_data) -{ - JSGlobalContextRef js_ctx; - JSObjectRef window_obj, external_obj, - log_function, exists_in_mod_function; - - js_ctx = (JSGlobalContextRef)context; - - external_obj = JSObjectMake(js_ctx, NULL, NULL); - log_function = JS_FUNC(js_ctx, js_log); - exists_in_mod_function = JS_FUNC(js_ctx, js_exists_in_mod); - window_obj = (JSObjectRef)window_object; - JSObjectSetProperty(js_ctx, window_obj, JS_STR("external"), - external_obj, 0, NULL); - JSObjectSetProperty(js_ctx, external_obj, JS_STR("log"), - log_function, 0, NULL); - JSObjectSetProperty(js_ctx, external_obj, JS_STR("existsInMod"), - exists_in_mod_function, 0, NULL); -} - int main(int argc, char ** argv) { server_init(48764);