Bind to JavaScriptCore. Serve files through embedded web server.
This commit is contained in:
committed by
Chris Forbes
parent
a561dd376e
commit
69ee1399b9
@@ -10,85 +10,82 @@
|
||||
#include <string.h>
|
||||
#include <gtk/gtk.h>
|
||||
#include <webkit/webkit.h>
|
||||
#include <JavaScriptCore/JavaScript.h>
|
||||
|
||||
#include "server.h"
|
||||
|
||||
#define JS_STR(str) JSStringCreateWithUTF8CString(str)
|
||||
#define JS_FUNC(ctx, callback) JSObjectMakeFunctionWithCallback(ctx, NULL, \
|
||||
callback)
|
||||
|
||||
GtkWindow * window;
|
||||
WebKitWebView * browser;
|
||||
|
||||
gboolean window_delete(GtkWidget * widget, GdkEvent * event, gpointer user_data)
|
||||
gboolean window_delete(GtkWidget * widget, GdkEvent * event,
|
||||
gpointer user_data)
|
||||
{
|
||||
server_teardown();
|
||||
gtk_main_quit();
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
int get_file_uri(char const * filepath, char ** uri)
|
||||
JSValueRef js_log(JSContextRef ctx, JSObjectRef func, JSObjectRef this,
|
||||
size_t argc, const JSValueRef argv[],
|
||||
JSValueRef * exception)
|
||||
{
|
||||
FILE * output;
|
||||
char buffer[1024];
|
||||
size_t buffer_len;
|
||||
|
||||
sprintf(buffer, "readlink -f %s", filepath);
|
||||
|
||||
output = popen(buffer, "r");
|
||||
|
||||
if (!output)
|
||||
JSValueRef return_value = JSValueMakeNull(ctx);
|
||||
if (argc < 1)
|
||||
{
|
||||
g_warning("Could not find absolute path for %s", filepath);
|
||||
return FALSE;
|
||||
*exception = JSValueMakeString(ctx, JS_STR("Not enough args"));
|
||||
return return_value;
|
||||
}
|
||||
|
||||
fgets(buffer, sizeof(buffer), output);
|
||||
|
||||
pclose(output);
|
||||
|
||||
buffer_len = strlen(buffer);
|
||||
buffer[buffer_len - 1] = '\0';
|
||||
|
||||
*uri = g_filename_to_uri(buffer, NULL, NULL);
|
||||
|
||||
if (!*uri)
|
||||
if (JSValueIsString(ctx, argv[0]))
|
||||
{
|
||||
g_warning("Could not convert %s to URI", buffer);
|
||||
return FALSE;
|
||||
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;
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
int get_bridge_script(char ** script)
|
||||
JSValueRef js_exists_in_mod(JSContextRef ctx, JSObjectRef func,
|
||||
JSObjectRef this, size_t argc,
|
||||
const JSValueRef argv[], JSValueRef * exception)
|
||||
{
|
||||
FILE * f;
|
||||
long fileSize;
|
||||
char * buffer;
|
||||
size_t result;
|
||||
|
||||
f = fopen("bridge.js", "r");
|
||||
|
||||
if (!f)
|
||||
{
|
||||
g_critical("Could not open bridge.js");
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
fseek(f, 0, SEEK_END);
|
||||
fileSize = ftell(f);
|
||||
rewind(f);
|
||||
|
||||
buffer = (char *) malloc(sizeof(char) * fileSize);
|
||||
result = fread(buffer, 1, fileSize, f);
|
||||
|
||||
fclose(f);
|
||||
|
||||
*script = buffer;
|
||||
return TRUE;
|
||||
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)
|
||||
{
|
||||
char * uri, * script;
|
||||
|
||||
server_init(48764);
|
||||
|
||||
gtk_init(&argc, &argv);
|
||||
@@ -98,24 +95,13 @@ int main(int argc, char ** argv)
|
||||
gtk_window_set_default_size(window, 800, 600);
|
||||
|
||||
browser = WEBKIT_WEB_VIEW(webkit_web_view_new());
|
||||
g_signal_connect(browser, "window-object-cleared",
|
||||
G_CALLBACK(bind_js_bridge), 0);
|
||||
|
||||
gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(browser));
|
||||
|
||||
if (!get_bridge_script(&script))
|
||||
return 1;
|
||||
|
||||
webkit_web_view_execute_script(browser, script);
|
||||
|
||||
free(script);
|
||||
|
||||
if (!get_file_uri("../mods/cnc/mod.html", &uri))
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
webkit_web_view_load_uri(browser, uri);
|
||||
|
||||
free(uri);
|
||||
webkit_web_view_load_uri(browser,
|
||||
"http://localhost:48764/mods/cnc/mod.html");
|
||||
|
||||
gtk_widget_show_all(GTK_WIDGET(window));
|
||||
g_signal_connect(window, "delete-event", G_CALLBACK(window_delete), 0);
|
||||
|
||||
Reference in New Issue
Block a user