11 #include <gdk/gdkkeysyms.h>
13 #include <webkit2/webkit2.h>
14 #include <JavaScriptCore/JavaScript.h>
17 static void client_destroy(GtkWidget *, gpointer);
18 static gboolean client_destroy_request(WebKitWebView *, gpointer);
19 static WebKitWebView *client_new(const gchar *, WebKitWebView *, gboolean);
20 static WebKitWebView *client_new_request(WebKitWebView *, WebKitNavigationAction *,
22 static void cooperation_setup(void);
23 static void changed_download_progress(GObject *, GParamSpec *, gpointer);
24 static void changed_load_progress(GObject *, GParamSpec *, gpointer);
25 static void changed_title(GObject *, GParamSpec *, gpointer);
26 static void changed_uri(GObject *, GParamSpec *, gpointer);
27 static gboolean crashed_web_view(WebKitWebView *, gpointer);
28 static gboolean decide_policy(WebKitWebView *, WebKitPolicyDecision *,
29 WebKitPolicyDecisionType, gpointer);
30 static gboolean download_handle(WebKitDownload *, gchar *, gpointer);
31 static void download_handle_start(WebKitWebView *, WebKitDownload *, gpointer);
32 static void downloadmanager_cancel(GtkToolButton *, gpointer);
33 static gboolean downloadmanager_delete(GtkWidget *, gpointer);
34 static void downloadmanager_setup(void);
35 static gchar *ensure_uri_scheme(const gchar *);
36 static void external_handler_run(GSimpleAction *, GVariant *, gpointer);
37 static void feed_icon(gpointer, gchar *);
38 static void grab_environment_configuration(void);
39 static void grab_feeds_finished(GObject *, GAsyncResult *, gpointer);
40 static void hover_web_view(WebKitWebView *, WebKitHitTestResult *, guint, gpointer);
41 static void icon_location(GtkEntry *, GtkEntryIconPosition, GdkEvent *, gpointer);
42 static gboolean key_common(GtkWidget *, GdkEvent *, gpointer);
43 static gboolean key_downloadmanager(GtkWidget *, GdkEvent *, gpointer);
44 static gboolean key_location(GtkWidget *, GdkEvent *, gpointer);
45 static gboolean key_web_view(GtkWidget *, GdkEvent *, gpointer);
46 static void keywords_load(void);
47 static gboolean keywords_try_search(WebKitWebView *, const gchar *);
48 static gboolean menu_web_view(WebKitWebView *, WebKitContextMenu *, GdkEvent *,
49 WebKitHitTestResult *, gpointer);
50 static gboolean quit_if_nothing_active(void);
51 static gboolean remote_msg(GIOChannel *, GIOCondition, gpointer);
52 static void search(gpointer, gint);
53 static void show_web_view(WebKitWebView *, gpointer);
54 static Window tabbed_launch(void);
55 static void trust_user_certs(WebKitWebContext *);
60 gchar *external_handler_uri;
69 struct DownloadManager
77 static const gchar *accepted_language[2] = { NULL, NULL };
78 static gint clients = 0, downloads = 0;
79 static gboolean cooperative_alone = TRUE;
80 static gboolean cooperative_instances = TRUE;
81 static int cooperative_pipe_fp = 0;
82 static gchar *download_dir = "/var/tmp";
83 static gboolean enable_webgl = FALSE;
84 static gboolean enable_console_to_stdout = FALSE;
85 static Window embed = 0;
86 static gchar *fifo_suffix = "main";
87 static gdouble global_zoom = 1.0;
88 static gchar *history_file = NULL;
89 static gchar *home_uri = "about:blank";
90 static gboolean initial_wc_setup_done = FALSE;
91 static GHashTable *keywords = NULL;
92 static gchar *search_text = NULL;
93 static gboolean tabbed_automagic = TRUE;
94 static gchar *user_agent = NULL;
96 static gchar *feed_html_header =
100 " <title>Feeds</title>"
103 " <p>Feeds found on this page:</p>"
107 static gchar *feed_html_footer =
113 static gchar *grab_feeds =
114 "a = document.querySelectorAll('"
115 " html > head > link[rel=\"alternate\"][href][type=\"application/atom+xml\"],"
116 " html > head > link[rel=\"alternate\"][href][type=\"application/rss+xml\"]"
123 " for (i = 0; i < a.length; i++)"
125 " url = encodeURIComponent(a[i].href);"
126 " if ('title' in a[i] && a[i].title != '')"
127 " title = encodeURIComponent(a[i].title);"
130 " out += '<li><a href=\"' + url + '\">' + title + '</a></li>';"
138 client_destroy(GtkWidget *widget, gpointer data)
140 struct Client *c = (struct Client *)data;
142 g_signal_handlers_disconnect_by_func(G_OBJECT(c->web_view),
143 changed_load_progress, c);
148 quit_if_nothing_active();
152 client_destroy_request(WebKitWebView *web_view, gpointer data)
154 struct Client *c = (struct Client *)data;
156 gtk_widget_destroy(c->win);
162 client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show)
165 WebKitWebContext *wc;
168 if (uri != NULL && cooperative_instances && !cooperative_alone)
170 f = ensure_uri_scheme(uri);
171 write(cooperative_pipe_fp, f, strlen(f));
172 write(cooperative_pipe_fp, "\n", 1);
177 c = calloc(1, sizeof(struct Client));
180 fprintf(stderr, __NAME__": fatal: calloc failed\n");
186 c->win = gtk_plug_new(embed);
187 if (!gtk_plug_get_embedded(GTK_PLUG(c->win)))
189 fprintf(stderr, __NAME__": Can't plug-in to XID %ld.\n", embed);
190 gtk_widget_destroy(c->win);
197 c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
199 gtk_window_set_default_size(GTK_WINDOW(c->win), 800, 600);
201 g_signal_connect(G_OBJECT(c->win), "destroy", G_CALLBACK(client_destroy), c);
202 gtk_window_set_title(GTK_WINDOW(c->win), __NAME__);
204 if (related_wv == NULL)
205 c->web_view = webkit_web_view_new();
207 c->web_view = webkit_web_view_new_with_related_view(related_wv);
208 wc = webkit_web_view_get_context(WEBKIT_WEB_VIEW(c->web_view));
210 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), global_zoom);
211 g_signal_connect(G_OBJECT(c->web_view), "notify::title",
212 G_CALLBACK(changed_title), c);
213 g_signal_connect(G_OBJECT(c->web_view), "notify::uri",
214 G_CALLBACK(changed_uri), c);
215 g_signal_connect(G_OBJECT(c->web_view), "notify::estimated-load-progress",
216 G_CALLBACK(changed_load_progress), c);
217 g_signal_connect(G_OBJECT(c->web_view), "create",
218 G_CALLBACK(client_new_request), NULL);
219 g_signal_connect(G_OBJECT(c->web_view), "context-menu",
220 G_CALLBACK(menu_web_view), c);
221 g_signal_connect(G_OBJECT(c->web_view), "close",
222 G_CALLBACK(client_destroy_request), c);
223 g_signal_connect(G_OBJECT(c->web_view), "decide-policy",
224 G_CALLBACK(decide_policy), NULL);
225 g_signal_connect(G_OBJECT(c->web_view), "key-press-event",
226 G_CALLBACK(key_web_view), c);
227 g_signal_connect(G_OBJECT(c->web_view), "button-release-event",
228 G_CALLBACK(key_web_view), c);
229 g_signal_connect(G_OBJECT(c->web_view), "scroll-event",
230 G_CALLBACK(key_web_view), c);
231 g_signal_connect(G_OBJECT(c->web_view), "mouse-target-changed",
232 G_CALLBACK(hover_web_view), c);
233 g_signal_connect(G_OBJECT(c->web_view), "web-process-crashed",
234 G_CALLBACK(crashed_web_view), c);
236 if (!initial_wc_setup_done)
238 if (accepted_language[0] != NULL)
239 webkit_web_context_set_preferred_languages(wc, accepted_language);
241 g_signal_connect(G_OBJECT(wc), "download-started",
242 G_CALLBACK(download_handle_start), NULL);
244 trust_user_certs(wc);
246 initial_wc_setup_done = TRUE;
249 if (user_agent != NULL)
250 g_object_set(G_OBJECT(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view))),
251 "user-agent", user_agent, NULL);
253 if (enable_console_to_stdout)
254 webkit_settings_set_enable_write_console_messages_to_stdout(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view)), TRUE);
257 webkit_settings_set_enable_webgl(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view)), TRUE);
259 c->location = gtk_entry_new();
260 g_signal_connect(G_OBJECT(c->location), "key-press-event",
261 G_CALLBACK(key_location), c);
262 g_signal_connect(G_OBJECT(c->location), "icon-release",
263 G_CALLBACK(icon_location), c);
265 c->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
266 gtk_box_pack_start(GTK_BOX(c->vbox), c->location, FALSE, FALSE, 0);
267 gtk_box_pack_start(GTK_BOX(c->vbox), c->web_view, TRUE, TRUE, 0);
269 gtk_container_add(GTK_CONTAINER(c->win), c->vbox);
272 show_web_view(NULL, c);
274 g_signal_connect(G_OBJECT(c->web_view), "ready-to-show",
275 G_CALLBACK(show_web_view), c);
279 f = ensure_uri_scheme(uri);
280 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
286 return WEBKIT_WEB_VIEW(c->web_view);
290 client_new_request(WebKitWebView *web_view,
291 WebKitNavigationAction *navigation_action, gpointer data)
293 return client_new(NULL, web_view, FALSE);
297 cooperation_setup(void)
300 gchar *fifofilename, *fifopath;
302 fifofilename = g_strdup_printf("%s-%s", __NAME__".fifo", fifo_suffix);
303 fifopath = g_build_filename(g_get_user_runtime_dir(), fifofilename, NULL);
304 g_free(fifofilename);
306 if (!g_file_test(fifopath, G_FILE_TEST_EXISTS))
307 mkfifo(fifopath, 0600);
309 cooperative_pipe_fp = open(fifopath, O_WRONLY | O_NONBLOCK);
310 if (!cooperative_pipe_fp)
312 fprintf(stderr, __NAME__": Can't open FIFO at all.\n");
316 if (write(cooperative_pipe_fp, "", 0) == -1)
318 /* Could not do an empty write to the FIFO which means there's
319 * no one listening. */
320 close(cooperative_pipe_fp);
321 towatch = g_io_channel_new_file(fifopath, "r+", NULL);
322 g_io_add_watch(towatch, G_IO_IN, (GIOFunc)remote_msg, NULL);
325 cooperative_alone = FALSE;
332 changed_download_progress(GObject *obj, GParamSpec *pspec, gpointer data)
334 WebKitDownload *download = WEBKIT_DOWNLOAD(obj);
335 WebKitURIResponse *resp;
336 GtkToolItem *tb = GTK_TOOL_ITEM(data);
339 gchar *t, *filename, *base;
341 p = webkit_download_get_estimated_progress(download);
345 resp = webkit_download_get_response(download);
346 size_mb = webkit_uri_response_get_content_length(resp) / 1e6;
348 uri = webkit_download_get_destination(download);
349 filename = g_filename_from_uri(uri, NULL, NULL);
350 if (filename == NULL)
352 /* This really should not happen because WebKit uses that URI to
353 * write to a file... */
354 fprintf(stderr, __NAME__": Could not construct file name from URI!\n");
355 t = g_strdup_printf("%s (%.0f%% of %.1f MB)",
356 webkit_uri_response_get_uri(resp), p, size_mb);
360 base = g_path_get_basename(filename);
361 t = g_strdup_printf("%s (%.0f%% of %.1f MB)", base, p, size_mb);
365 gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), t);
370 changed_load_progress(GObject *obj, GParamSpec *pspec, gpointer data)
372 struct Client *c = (struct Client *)data;
375 p = webkit_web_view_get_estimated_load_progress(WEBKIT_WEB_VIEW(c->web_view));
380 /* The page has loaded fully. We now run a short JavaScript
381 * snippet that operates on the DOM. It tries to grab all
382 * occurences of <link rel="alternate" ...>, i.e. RSS/Atom feed
384 webkit_web_view_run_javascript(WEBKIT_WEB_VIEW(c->web_view),
386 grab_feeds_finished, c);
388 gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), p);
392 changed_title(GObject *obj, GParamSpec *pspec, gpointer data)
395 struct Client *c = (struct Client *)data;
397 u = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
398 t = webkit_web_view_get_title(WEBKIT_WEB_VIEW(c->web_view));
400 u = u == NULL ? __NAME__ : u;
401 u = u[0] == 0 ? __NAME__ : u;
403 t = t == NULL ? u : t;
404 t = t[0] == 0 ? u : t;
406 gtk_window_set_title(GTK_WINDOW(c->win), t);
410 changed_uri(GObject *obj, GParamSpec *pspec, gpointer data)
413 struct Client *c = (struct Client *)data;
416 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
418 /* When a web process crashes, we get a "notify::uri" signal, but we
419 * can no longer read a meaningful URI. It's just an empty string
420 * now. Not updating the location bar in this scenario is important,
421 * because we would override the "WEB PROCESS CRASHED" message. */
422 if (t != NULL && strlen(t) > 0)
424 gtk_entry_set_text(GTK_ENTRY(c->location), t);
426 if (history_file != NULL)
428 fp = fopen(history_file, "a");
431 fprintf(fp, "%s\n", t);
435 perror(__NAME__": Error opening history file");
441 crashed_web_view(WebKitWebView *web_view, gpointer data)
444 struct Client *c = (struct Client *)data;
446 t = g_strdup_printf("WEB PROCESS CRASHED: %s",
447 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(web_view)));
448 gtk_entry_set_text(GTK_ENTRY(c->location), t);
455 decide_policy(WebKitWebView *web_view, WebKitPolicyDecision *decision,
456 WebKitPolicyDecisionType type, gpointer data)
458 WebKitResponsePolicyDecision *r;
462 case WEBKIT_POLICY_DECISION_TYPE_RESPONSE:
463 r = WEBKIT_RESPONSE_POLICY_DECISION(decision);
464 if (!webkit_response_policy_decision_is_mime_type_supported(r))
465 webkit_policy_decision_download(decision);
467 webkit_policy_decision_use(decision);
470 /* Use whatever default there is. */
477 download_handle_finished(WebKitDownload *download, gpointer data)
483 download_handle_start(WebKitWebView *web_view, WebKitDownload *download,
486 g_signal_connect(G_OBJECT(download), "decide-destination",
487 G_CALLBACK(download_handle), data);
491 download_handle(WebKitDownload *download, gchar *suggested_filename, gpointer data)
493 gchar *sug_clean, *path, *path2 = NULL, *uri;
498 sug_clean = g_strdup(suggested_filename);
499 for (i = 0; i < strlen(sug_clean); i++)
500 if (sug_clean[i] == G_DIR_SEPARATOR)
503 path = g_build_filename(download_dir, sug_clean, NULL);
504 path2 = g_strdup(path);
505 while (g_file_test(path2, G_FILE_TEST_EXISTS) && suffix < 1000)
509 path2 = g_strdup_printf("%s.%d", path, suffix);
515 fprintf(stderr, __NAME__": Suffix reached limit for download.\n");
516 webkit_download_cancel(download);
520 uri = g_filename_to_uri(path2, NULL, NULL);
521 webkit_download_set_destination(download, uri);
524 tb = gtk_tool_button_new(NULL, NULL);
525 gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(tb), "gtk-delete");
526 gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), sug_clean);
527 gtk_toolbar_insert(GTK_TOOLBAR(dm.toolbar), tb, 0);
528 gtk_widget_show_all(dm.win);
530 g_signal_connect(G_OBJECT(download), "notify::estimated-progress",
531 G_CALLBACK(changed_download_progress), tb);
534 g_signal_connect(G_OBJECT(download), "finished",
535 G_CALLBACK(download_handle_finished), NULL);
537 g_object_ref(download);
538 g_signal_connect(G_OBJECT(tb), "clicked",
539 G_CALLBACK(downloadmanager_cancel), download);
546 /* Propagate -- to whom it may concern. */
551 downloadmanager_cancel(GtkToolButton *tb, gpointer data)
553 WebKitDownload *download = WEBKIT_DOWNLOAD(data);
555 webkit_download_cancel(download);
556 g_object_unref(download);
558 gtk_widget_destroy(GTK_WIDGET(tb));
562 downloadmanager_delete(GtkWidget *obj, gpointer data)
564 if (!quit_if_nothing_active())
565 gtk_widget_hide(dm.win);
571 downloadmanager_setup(void)
573 dm.win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
574 gtk_window_set_type_hint(GTK_WINDOW(dm.win), GDK_WINDOW_TYPE_HINT_DIALOG);
575 gtk_window_set_default_size(GTK_WINDOW(dm.win), 500, 250);
576 gtk_window_set_title(GTK_WINDOW(dm.win), __NAME__" - Download Manager");
577 g_signal_connect(G_OBJECT(dm.win), "delete-event",
578 G_CALLBACK(downloadmanager_delete), NULL);
579 g_signal_connect(G_OBJECT(dm.win), "key-press-event",
580 G_CALLBACK(key_downloadmanager), NULL);
582 dm.toolbar = gtk_toolbar_new();
583 gtk_orientable_set_orientation(GTK_ORIENTABLE(dm.toolbar),
584 GTK_ORIENTATION_VERTICAL);
585 gtk_toolbar_set_style(GTK_TOOLBAR(dm.toolbar), GTK_TOOLBAR_BOTH_HORIZ);
586 gtk_toolbar_set_show_arrow(GTK_TOOLBAR(dm.toolbar), FALSE);
588 dm.scroll = gtk_scrolled_window_new(NULL, NULL);
589 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(dm.scroll),
590 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
591 gtk_container_add(GTK_CONTAINER(dm.scroll), dm.toolbar);
593 gtk_container_add(GTK_CONTAINER(dm.win), dm.scroll);
597 ensure_uri_scheme(const gchar *t)
601 f = g_ascii_strdown(t, -1);
602 if (!g_str_has_prefix(f, "http:") &&
603 !g_str_has_prefix(f, "https:") &&
604 !g_str_has_prefix(f, "file:") &&
605 !g_str_has_prefix(f, "about:") &&
606 !g_str_has_prefix(f, "data:"))
609 fabs = realpath(t, NULL);
612 f = g_strdup_printf("file://%s", fabs);
616 f = g_strdup_printf("http://%s", t);
624 external_handler_run(GSimpleAction *simple, GVariant *param, gpointer data)
626 struct Client *c = (struct Client *)data;
627 gchar *argv[] = { "lariza-external-handler", "-u", NULL, NULL };
634 argv[2] = c->external_handler_uri;
635 if (!g_spawn_async(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL,
638 fprintf(stderr, __NAME__": Could not launch key handler: %s\n",
643 g_spawn_close_pid(pid);
646 void feed_icon(gpointer user_data, gchar *feed_html)
648 struct Client *c = (struct Client *)user_data;
650 if (feed_html != NULL)
652 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
653 GTK_ENTRY_ICON_PRIMARY,
654 "application-rss+xml-symbolic");
655 gtk_entry_set_icon_activatable(GTK_ENTRY(c->location),
656 GTK_ENTRY_ICON_PRIMARY,
659 if (c->feed_html != NULL)
660 g_free(c->feed_html);
661 c->feed_html = g_strdup(feed_html);
665 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
666 GTK_ENTRY_ICON_PRIMARY,
672 grab_environment_configuration(void)
676 e = g_getenv(__NAME_UPPERCASE__"_ACCEPTED_LANGUAGE");
678 accepted_language[0] = g_strdup(e);
680 e = g_getenv(__NAME_UPPERCASE__"_DOWNLOAD_DIR");
682 download_dir = g_strdup(e);
684 e = g_getenv(__NAME_UPPERCASE__"_ENABLE_CONSOLE_TO_STDOUT");
686 enable_console_to_stdout = TRUE;
688 e = g_getenv(__NAME_UPPERCASE__"_ENABLE_EXPERIMENTAL_WEBGL");
692 e = g_getenv(__NAME_UPPERCASE__"_FIFO_SUFFIX");
694 fifo_suffix = g_strdup(e);
696 e = g_getenv(__NAME_UPPERCASE__"_HISTORY_FILE");
698 history_file = g_strdup(e);
700 e = g_getenv(__NAME_UPPERCASE__"_HOME_URI");
702 home_uri = g_strdup(e);
704 e = g_getenv(__NAME_UPPERCASE__"_USER_AGENT");
706 user_agent = g_strdup(e);
708 e = g_getenv(__NAME_UPPERCASE__"_ZOOM");
710 global_zoom = atof(e);
714 grab_feeds_finished(GObject *object, GAsyncResult *result, gpointer data)
716 WebKitJavascriptResult *js_result;
718 JSGlobalContextRef context;
720 JSStringRef js_str_value;
724 /* This was taken almost verbatim from the example in WebKit's
727 js_result = webkit_web_view_run_javascript_finish(WEBKIT_WEB_VIEW(object),
731 fprintf(stderr, __NAME__": Error running javascript: %s\n", err->message);
736 context = webkit_javascript_result_get_global_context(js_result);
737 value = webkit_javascript_result_get_value(js_result);
739 if (JSValueIsString(context, value))
741 js_str_value = JSValueToStringCopy(context, value, NULL);
742 str_length = JSStringGetMaximumUTF8CStringSize(js_str_value);
743 str_value = (gchar *)g_malloc(str_length);
744 JSStringGetUTF8CString(js_str_value, str_value, str_length);
745 JSStringRelease(js_str_value);
746 feed_icon(data, str_value);
750 feed_icon(data, NULL);
752 webkit_javascript_result_unref(js_result);
756 hover_web_view(WebKitWebView *web_view, WebKitHitTestResult *ht, guint modifiers,
759 struct Client *c = (struct Client *)data;
761 if (!gtk_widget_is_focus(c->location))
763 if (webkit_hit_test_result_context_is_link(ht))
765 gtk_entry_set_text(GTK_ENTRY(c->location),
766 webkit_hit_test_result_get_link_uri(ht));
768 if (c->hover_uri != NULL)
769 g_free(c->hover_uri);
770 c->hover_uri = g_strdup(webkit_hit_test_result_get_link_uri(ht));
774 gtk_entry_set_text(GTK_ENTRY(c->location),
775 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view)));
777 if (c->hover_uri != NULL)
778 g_free(c->hover_uri);
785 icon_location(GtkEntry *entry, GtkEntryIconPosition icon_pos, GdkEvent *event, gpointer data)
787 struct Client *c = (struct Client *)data;
790 if (c->feed_html != NULL)
792 /* What we're actually trying to do is show a simple HTML page
793 * that lists all the feeds on the current page. The function
794 * webkit_web_view_load_html() looks like the proper way to do
795 * that. Sad thing is, it doesn't create a history entry, but
796 * instead simply replaces the content of the current page. This
797 * is not what we want.
799 * RFC 2397 [0] defines the data URI scheme [1]. We abuse this
800 * mechanism to show my custom HTML snippet *and* create a
803 * [0]: https://tools.ietf.org/html/rfc2397
804 * [1]: https://en.wikipedia.org/wiki/Data_URI_scheme */
805 d = g_strdup_printf("data:text/html,%s%s%s",
809 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), d);
815 key_common(GtkWidget *widget, GdkEvent *event, gpointer data)
817 struct Client *c = (struct Client *)data;
818 WebKitWebContext *wc = webkit_web_view_get_context(WEBKIT_WEB_VIEW(c->web_view));
821 if (event->type == GDK_KEY_PRESS)
823 if (((GdkEventKey *)event)->state & GDK_MOD1_MASK)
825 switch (((GdkEventKey *)event)->keyval)
827 case GDK_KEY_q: /* close window (left hand) */
828 gtk_widget_destroy(c->win);
830 case GDK_KEY_w: /* home (left hand) */
831 f = ensure_uri_scheme(home_uri);
832 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
835 case GDK_KEY_e: /* new tab (left hand) */
836 f = ensure_uri_scheme(home_uri);
837 client_new(f, NULL, TRUE);
840 case GDK_KEY_r: /* reload (left hand) */
841 webkit_web_view_reload_bypass_cache(WEBKIT_WEB_VIEW(
844 case GDK_KEY_d: /* download manager (left hand) */
845 gtk_widget_show_all(dm.win);
847 case GDK_KEY_2: /* search forward (left hand) */
848 case GDK_KEY_n: /* search forward (maybe both hands) */
851 case GDK_KEY_3: /* search backward (left hand) */
854 case GDK_KEY_l: /* location (BOTH hands) */
855 gtk_widget_grab_focus(c->location);
857 case GDK_KEY_k: /* initiate search (BOTH hands) */
858 gtk_widget_grab_focus(c->location);
859 gtk_entry_set_text(GTK_ENTRY(c->location), ":/");
860 gtk_editable_set_position(GTK_EDITABLE(c->location), -1);
862 case GDK_KEY_c: /* reload trusted certs (left hand) */
863 trust_user_certs(wc);
865 case GDK_KEY_x: /* launch external handler (left hand) */
866 if (c->external_handler_uri != NULL)
867 g_free(c->external_handler_uri);
868 c->external_handler_uri = g_strdup(
869 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view)));
870 external_handler_run(NULL, NULL, c);
874 /* navigate backward (left hand) */
875 else if (((GdkEventKey *)event)->keyval == GDK_KEY_F2)
877 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
880 /* navigate forward (left hand) */
881 else if (((GdkEventKey *)event)->keyval == GDK_KEY_F3)
883 webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
892 key_downloadmanager(GtkWidget *widget, GdkEvent *event, gpointer data)
894 if (event->type == GDK_KEY_PRESS)
896 if (((GdkEventKey *)event)->state & GDK_MOD1_MASK)
898 switch (((GdkEventKey *)event)->keyval)
900 case GDK_KEY_d: /* close window (left hand) */
902 downloadmanager_delete(dm.win, NULL);
912 key_location(GtkWidget *widget, GdkEvent *event, gpointer data)
914 struct Client *c = (struct Client *)data;
918 if (key_common(widget, event, data))
921 if (event->type == GDK_KEY_PRESS)
923 switch (((GdkEventKey *)event)->keyval)
925 case GDK_KEY_KP_Enter:
927 gtk_widget_grab_focus(c->web_view);
928 t = gtk_entry_get_text(GTK_ENTRY(c->location));
929 if (t != NULL && t[0] == ':' && t[1] == '/')
931 if (search_text != NULL)
933 search_text = g_strdup(t + 2);
936 else if (!keywords_try_search(WEBKIT_WEB_VIEW(c->web_view), t))
938 f = ensure_uri_scheme(t);
939 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
944 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
945 gtk_entry_set_text(GTK_ENTRY(c->location),
946 (t == NULL ? __NAME__ : t));
955 key_web_view(GtkWidget *widget, GdkEvent *event, gpointer data)
957 struct Client *c = (struct Client *)data;
961 if (key_common(widget, event, data))
964 if (event->type == GDK_KEY_PRESS)
966 if (((GdkEventKey *)event)->keyval == GDK_KEY_Escape)
968 webkit_web_view_stop_loading(WEBKIT_WEB_VIEW(c->web_view));
969 gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), 0);
972 else if (event->type == GDK_BUTTON_RELEASE)
974 switch (((GdkEventButton *)event)->button)
977 if (c->hover_uri != NULL)
979 client_new(c->hover_uri, NULL, TRUE);
984 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
987 webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
991 else if (event->type == GDK_SCROLL)
993 if (((GdkEventScroll *)event)->state & GDK_MOD1_MASK ||
994 ((GdkEventScroll *)event)->state & GDK_CONTROL_MASK)
996 gdk_event_get_scroll_deltas(event, &dx, &dy);
997 z = webkit_web_view_get_zoom_level(WEBKIT_WEB_VIEW(c->web_view));
999 z = dx != 0 ? global_zoom : z;
1000 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), z);
1012 GIOChannel *channel = NULL;
1013 gchar *path = NULL, *buf = NULL;
1014 gchar **tokens = NULL;
1016 keywords = g_hash_table_new(g_str_hash, g_str_equal);
1018 path = g_build_filename(g_get_user_config_dir(), __NAME__, "keywordsearch",
1020 channel = g_io_channel_new_file(path, "r", &err);
1021 if (channel != NULL)
1023 while (g_io_channel_read_line(channel, &buf, NULL, NULL, NULL)
1024 == G_IO_STATUS_NORMAL)
1029 tokens = g_strsplit(buf, " ", 2);
1030 if (tokens[0] != NULL && tokens[1] != NULL)
1031 g_hash_table_insert(keywords, g_strdup(tokens[0]),
1032 g_strdup(tokens[1]));
1037 g_io_channel_shutdown(channel, FALSE, NULL);
1043 keywords_try_search(WebKitWebView *web_view, const gchar *t)
1045 gboolean ret = FALSE;
1046 gchar **tokens = NULL;
1047 gchar *val = NULL, *escaped = NULL, *uri = NULL;
1049 tokens = g_strsplit(t, " ", 2);
1050 if (tokens[0] != NULL && tokens[1] != NULL)
1052 val = g_hash_table_lookup(keywords, tokens[0]);
1055 escaped = g_uri_escape_string(tokens[1], NULL, TRUE);
1056 uri = g_strdup_printf((gchar *)val, escaped);
1057 webkit_web_view_load_uri(web_view, uri);
1069 menu_web_view(WebKitWebView *web_view, WebKitContextMenu *menu, GdkEvent *ev,
1070 WebKitHitTestResult *ht, gpointer data)
1072 struct Client *c = (struct Client *)data;
1073 GSimpleAction *action = NULL;
1074 WebKitContextMenuItem *mi = NULL;
1075 const gchar *uri = NULL;
1079 if (webkit_hit_test_result_context_is_link(ht))
1080 uri = webkit_hit_test_result_get_link_uri(ht);
1081 else if (webkit_hit_test_result_context_is_image(ht))
1082 uri = webkit_hit_test_result_get_image_uri(ht);
1083 else if (webkit_hit_test_result_context_is_media(ht))
1084 uri = webkit_hit_test_result_get_media_uri(ht);
1088 webkit_context_menu_append(menu, webkit_context_menu_item_new_separator());
1090 if (c->external_handler_uri != NULL)
1091 g_free(c->external_handler_uri);
1092 c->external_handler_uri = g_strdup(uri);
1093 action = g_simple_action_new("external_handler", NULL);
1094 g_signal_connect(G_OBJECT(action), "activate",
1095 G_CALLBACK(external_handler_run), data);
1096 mi = webkit_context_menu_item_new_from_gaction(G_ACTION(action),
1097 "Open with external handler",
1099 webkit_context_menu_append(menu, mi);
1100 g_object_unref(action);
1103 /* FALSE = Show the menu. (TRUE = Don't ever show it.) */
1108 quit_if_nothing_active(void)
1118 gtk_widget_show_all(dm.win);
1125 remote_msg(GIOChannel *channel, GIOCondition condition, gpointer data)
1129 g_io_channel_read_line(channel, &uri, NULL, NULL, NULL);
1133 client_new(uri, NULL, TRUE);
1140 search(gpointer data, gint direction)
1142 struct Client *c = (struct Client *)data;
1143 WebKitWebView *web_view = WEBKIT_WEB_VIEW(c->web_view);
1144 WebKitFindController *fc = webkit_web_view_get_find_controller(web_view);
1146 if (search_text == NULL)
1152 webkit_find_controller_search(fc, search_text,
1153 WEBKIT_FIND_OPTIONS_CASE_INSENSITIVE |
1154 WEBKIT_FIND_OPTIONS_WRAP_AROUND,
1158 webkit_find_controller_search_next(fc);
1161 webkit_find_controller_search_previous(fc);
1167 show_web_view(WebKitWebView *web_view, gpointer data)
1169 struct Client *c = (struct Client *)data;
1173 gtk_widget_grab_focus(c->web_view);
1174 gtk_widget_show_all(c->win);
1181 GIOChannel *tabbed_stdout_channel;
1183 gchar *output = NULL;
1184 char *argv[] = { "tabbed", "-c", "-d", "-p", "s1", "-n", __NAME__, NULL };
1187 if (!g_spawn_async_with_pipes(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL,
1188 NULL, NULL, NULL, &tabbed_stdout, NULL,
1191 fprintf(stderr, __NAME__": Could not launch tabbed: %s\n", err->message);
1196 tabbed_stdout_channel = g_io_channel_unix_new(tabbed_stdout);
1197 if (tabbed_stdout_channel == NULL)
1199 fprintf(stderr, __NAME__": Could open tabbed's stdout\n");
1202 g_io_channel_read_line(tabbed_stdout_channel, &output, NULL, NULL, NULL);
1203 g_io_channel_shutdown(tabbed_stdout_channel, FALSE, NULL);
1206 fprintf(stderr, __NAME__": Could not read XID from tabbed\n");
1210 plug_into = strtol(output, NULL, 16);
1213 fprintf(stderr, __NAME__": The XID from tabbed is 0\n");
1218 trust_user_certs(WebKitWebContext *wc)
1220 GTlsCertificate *cert;
1221 const gchar *basedir, *file, *absfile;
1224 basedir = g_build_filename(g_get_user_config_dir(), __NAME__, "certs", NULL);
1225 dir = g_dir_open(basedir, 0, NULL);
1228 file = g_dir_read_name(dir);
1229 while (file != NULL)
1231 absfile = g_build_filename(g_get_user_config_dir(), __NAME__, "certs",
1233 cert = g_tls_certificate_new_from_file(absfile, NULL);
1235 fprintf(stderr, __NAME__": Could not load trusted cert '%s'\n", file);
1237 webkit_web_context_allow_tls_certificate_for_host(wc, cert, file);
1238 file = g_dir_read_name(dir);
1246 main(int argc, char **argv)
1251 gtk_init(&argc, &argv);
1252 webkit_web_context_set_process_model(webkit_web_context_get_default(),
1253 WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
1255 grab_environment_configuration();
1257 while ((opt = getopt(argc, argv, "e:CT")) != -1)
1262 embed = atol(optarg);
1263 tabbed_automagic = FALSE;
1266 cooperative_instances = FALSE;
1269 tabbed_automagic = FALSE;
1272 fprintf(stderr, "Usage: "__NAME__" [OPTION]... [URI]...\n");
1278 if (cooperative_instances)
1279 cooperation_setup();
1280 downloadmanager_setup();
1282 if (tabbed_automagic && !(cooperative_instances && !cooperative_alone))
1283 embed = tabbed_launch();
1285 if (!cooperative_instances || cooperative_alone)
1287 c = g_build_filename(g_get_user_config_dir(), __NAME__, "web_extensions",
1289 webkit_web_context_set_web_extensions_directory(
1290 webkit_web_context_get_default(), c
1295 client_new(home_uri, NULL, TRUE);
1298 for (i = optind; i < argc; i++)
1299 client_new(argv[i], NULL, TRUE);
1302 if (!cooperative_instances || cooperative_alone)