diff options
author | portix <none@none> | 2012-10-03 15:33:09 +0200 |
---|---|---|
committer | portix <none@none> | 2012-10-03 15:33:09 +0200 |
commit | 68caad4ca45e45ab29a6ec412b585c9e260c80ef (patch) | |
tree | 31d43ecd91629fde9b0d6e2c3c194afe867e7cdb /src | |
parent | 77826cd15abfcb1b244063396c0b987ab3868324 (diff) | |
parent | 5b74b52416b90d9ade35b99f2d105a4120b0187d (diff) | |
download | dwb-68caad4ca45e45ab29a6ec412b585c9e260c80ef.zip |
Merging scratchpad into main
Diffstat (limited to 'src')
-rw-r--r-- | src/scratchpad.c | 35 | ||||
-rw-r--r-- | src/scripts.c | 49 | ||||
-rw-r--r-- | src/scripts.h | 2 |
3 files changed, 81 insertions, 5 deletions
diff --git a/src/scratchpad.c b/src/scratchpad.c index 4ae25848..00fc0974 100644 --- a/src/scratchpad.c +++ b/src/scratchpad.c @@ -17,6 +17,9 @@ */ #include "dwb.h" +#include "js.h" +#include "scripts.h" +#include <JavaScriptCore/JavaScript.h> GtkWidget *g_scratchpad; static gboolean @@ -30,6 +33,32 @@ navigation_cb(WebKitWebView *wv, WebKitWebFrame *frame, WebKitNetworkRequest *re return false; } + +static JSValueRef +sp_send(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argc, const JSValueRef argv[], JSValueRef* exception) { + if (argc > 0) + scripts_scratchpad_send(ctx, argv[0]); + return JSValueMakeUndefined(ctx); +} + +static JSValueRef +sp_get(JSContextRef ctx, JSObjectRef function, JSObjectRef this, size_t argc, const JSValueRef argv[], JSValueRef* exc) { + scripts_scratchpad_get(ctx, function, this, argc, argv, exc); + return JSValueMakeUndefined(ctx); +} + +static void +window_object_cb(WebKitWebView *wv, WebKitWebFrame *frame, JSContextRef ctx, JSObjectRef window) { + if (frame == webkit_web_view_get_main_frame(wv)) { + JSObjectRef func = JSObjectMakeFunctionWithCallback(ctx, NULL, sp_get); + js_set_property(ctx, window, "dwbGet", func, + kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly, NULL); + func = JSObjectMakeFunctionWithCallback(ctx, NULL, sp_send); + js_set_property(ctx, window, "dwbSend", func, + kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly, NULL); + } +} + void scratchpad_load(const char *text) { if (g_str_has_prefix(text, "file://")) @@ -43,22 +72,24 @@ scratchpad_show(void) { gtk_widget_show(g_scratchpad); gtk_widget_grab_focus(g_scratchpad); } + void scratchpad_hide(void) { gtk_widget_hide(g_scratchpad); dwb_focus_scroll(dwb.state.fview); } + static void scratchpad_init(void) { g_scratchpad = webkit_web_view_new(); g_signal_connect(g_scratchpad, "navigation-policy-decision-requested", G_CALLBACK(navigation_cb), NULL); + g_signal_connect(g_scratchpad, "window-object-cleared", G_CALLBACK(window_object_cb), NULL); gtk_widget_set_size_request(g_scratchpad, -1, 200); } + GtkWidget * scratchpad_get(void) { if (g_scratchpad == NULL) scratchpad_init(); return g_scratchpad; } - - diff --git a/src/scripts.c b/src/scripts.c index ec9b7f6c..593961c6 100644 --- a/src/scripts.c +++ b/src/scripts.c @@ -126,6 +126,8 @@ static JSStaticValue message_values[] = { static JSValueRef sp_show(JSContextRef ctx, JSObjectRef function, JSObjectRef this, size_t argc, const JSValueRef argv[], JSValueRef* exc); static JSValueRef sp_hide(JSContextRef ctx, JSObjectRef function, JSObjectRef this, size_t argc, const JSValueRef argv[], JSValueRef* exc); static JSValueRef sp_load(JSContextRef ctx, JSObjectRef function, JSObjectRef this, size_t argc, const JSValueRef argv[], JSValueRef* exc); +static JSValueRef sp_get(JSContextRef ctx, JSObjectRef function, JSObjectRef this, size_t argc, const JSValueRef argv[], JSValueRef* exc); +static JSValueRef sp_send(JSContextRef ctx, JSObjectRef function, JSObjectRef this, size_t argc, const JSValueRef argv[], JSValueRef* exc); static JSValueRef frame_inject(JSContextRef ctx, JSObjectRef function, JSObjectRef this, size_t argc, const JSValueRef argv[], JSValueRef* exc); static JSStaticFunction frame_functions[] = { @@ -166,6 +168,8 @@ static JSClassRef m_webview_class, m_frame_class, m_download_class, m_default_cl static gboolean m_commandline = false; static JSObjectRef m_array_contructor; static JSObjectRef m_completion_callback; +static JSObjectRef m_sp_scripts_cb; +static JSObjectRef m_sp_scratchpad_cb; static GQuark ref_quark; /* MISC {{{*/ @@ -498,10 +502,47 @@ sp_load(JSContextRef ctx, JSObjectRef function, JSObjectRef this, size_t argc, c g_free(text); } return JSValueMakeUndefined(ctx); - } - - +static JSObjectRef +sp_callback_create(JSContextRef ctx, size_t argc, const JSValueRef argv[], JSValueRef *exc) { + JSObjectRef ret = NULL; + if (argc > 0) { + ret = JSValueToObject(ctx, argv[0], exc); + if (ret == NULL || !JSObjectIsFunction(ctx, ret)) + ret = NULL; + } + return ret; +} +static JSValueRef +sp_get(JSContextRef ctx, JSObjectRef function, JSObjectRef this, size_t argc, const JSValueRef argv[], JSValueRef* exc) { + m_sp_scripts_cb = sp_callback_create(ctx, argc, argv, exc); + return JSValueMakeUndefined(ctx); +} +void +scripts_scratchpad_get(JSContextRef ctx, JSObjectRef function, JSObjectRef this, size_t argc, const JSValueRef argv[], JSValueRef* exc) { + m_sp_scratchpad_cb = sp_callback_create(ctx, argc, argv, exc); +} +void +sp_context_change(JSContextRef src_ctx, JSContextRef dest_ctx, JSObjectRef func, JSValueRef val) { + if (func != NULL) { + JSValueRef val_changed = js_context_change(src_ctx, dest_ctx, val, NULL); + JSValueRef argv[] = { val_changed == 0 ? JSValueMakeNull(dest_ctx) : val_changed }; + JSObjectCallAsFunction(dest_ctx, func, NULL, 1, argv, NULL); + } +} +// send from scripts context to scratchpad context +static JSValueRef +sp_send(JSContextRef ctx, JSObjectRef function, JSObjectRef this, size_t argc, const JSValueRef argv[], JSValueRef* exc) { + if (argc > 0) { + sp_context_change(m_global_context, webkit_web_frame_get_global_context(webkit_web_view_get_main_frame(WEBKIT_WEB_VIEW(scratchpad_get()))), m_sp_scratchpad_cb, argv[0]); + } + return JSValueMakeUndefined(ctx); +} +// send from scratchpad context to script context +void +scripts_scratchpad_send(JSContextRef ctx, JSValueRef val) { + sp_context_change(ctx, m_global_context, m_sp_scripts_cb, val); +} /* SOUP_MESSAGE {{{*/ /* soup_uri_to_js_object {{{*/ @@ -1901,6 +1942,8 @@ create_global_object() { { "show", sp_show, kJSDefaultAttributes }, { "hide", sp_hide, kJSDefaultAttributes }, { "load", sp_load, kJSDefaultAttributes }, + { "get", sp_get, kJSDefaultAttributes }, + { "send", sp_send, kJSDefaultAttributes }, { 0, 0, 0 }, }; cd.className = "Scratchpad"; diff --git a/src/scripts.h b/src/scripts.h index 6e823aa6..c860905a 100644 --- a/src/scripts.h +++ b/src/scripts.h @@ -70,6 +70,8 @@ void scripts_execute_scripts(char **scripts); DwbStatus scripts_eval_key(KeyMap *m, Arg *arg); gboolean scripts_execute_one(const char *script); void scripts_completion_activate(void); +void scripts_scratchpad_send(JSContextRef ctx, JSValueRef val); +void scripts_scratchpad_get(JSContextRef, JSObjectRef, JSObjectRef, size_t, const JSValueRef[], JSValueRef* ); #define EMIT_SCRIPT(sig) ((dwb.misc.script_signals & (1<<SCRIPTS_SIG_##sig))) #define SCRIPTS_EMIT_RETURN(signal, json) G_STMT_START \ |