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 grab_environment_configuration(void);
38 static void grab_feeds_finished(GObject *, GAsyncResult *, gpointer);
39 static void hover_web_view(WebKitWebView *, WebKitHitTestResult *, guint, gpointer);
40 static void icon_location(GtkEntry *, GtkEntryIconPosition, GdkEvent *, gpointer);
41 static gboolean key_common(GtkWidget *, GdkEvent *, gpointer);
42 static gboolean key_downloadmanager(GtkWidget *, GdkEvent *, gpointer);
43 static gboolean key_location(GtkWidget *, GdkEvent *, gpointer);
44 static gboolean key_web_view(GtkWidget *, GdkEvent *, gpointer);
45 static void keywords_load(void);
46 static gboolean keywords_try_search(WebKitWebView *, const gchar *);
47 static gboolean menu_web_view(WebKitWebView *, WebKitContextMenu *, GdkEvent *,
48 WebKitHitTestResult *, gpointer);
49 static gboolean quit_if_nothing_active(void);
50 static gboolean remote_msg(GIOChannel *, GIOCondition, gpointer);
51 static void search(gpointer, gint);
52 static void show_web_view(WebKitWebView *, gpointer);
53 static Window tabbed_launch(void);
54 static void trust_user_certs(WebKitWebContext *);
59 gchar *external_handler_uri;
68 struct DownloadManager
76 static const gchar *accepted_language[2] = { NULL, NULL };
77 static gint clients = 0, downloads = 0;
78 static gboolean cooperative_alone = TRUE;
79 static gboolean cooperative_instances = TRUE;
80 static int cooperative_pipe_fp = 0;
81 static gchar *download_dir = "/var/tmp";
82 static gboolean enable_webgl = FALSE;
83 static gboolean enable_console_to_stdout = FALSE;
84 static Window embed = 0;
85 static gchar *fifo_suffix = "main";
86 static gdouble global_zoom = 1.0;
87 static gchar *history_file = NULL;
88 static gchar *home_uri = "about:blank";
89 static gboolean initial_wc_setup_done = FALSE;
90 static GHashTable *keywords = NULL;
91 static gchar *search_text = NULL;
92 static gboolean tabbed_automagic = TRUE;
93 static gchar *user_agent = NULL;
97 client_destroy(GtkWidget *widget, gpointer data)
99 struct Client *c = (struct Client *)data;
101 g_signal_handlers_disconnect_by_func(G_OBJECT(c->web_view),
102 changed_load_progress, c);
107 quit_if_nothing_active();
111 client_destroy_request(WebKitWebView *web_view, gpointer data)
113 struct Client *c = (struct Client *)data;
115 gtk_widget_destroy(c->win);
121 client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show)
124 WebKitWebContext *wc;
127 if (uri != NULL && cooperative_instances && !cooperative_alone)
129 f = ensure_uri_scheme(uri);
130 write(cooperative_pipe_fp, f, strlen(f));
131 write(cooperative_pipe_fp, "\n", 1);
136 c = calloc(1, sizeof(struct Client));
139 fprintf(stderr, __NAME__": fatal: calloc failed\n");
145 c->win = gtk_plug_new(embed);
146 if (!gtk_plug_get_embedded(GTK_PLUG(c->win)))
148 fprintf(stderr, __NAME__": Can't plug-in to XID %ld.\n", embed);
149 gtk_widget_destroy(c->win);
156 c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
158 gtk_window_set_default_size(GTK_WINDOW(c->win), 800, 600);
160 g_signal_connect(G_OBJECT(c->win), "destroy", G_CALLBACK(client_destroy), c);
161 gtk_window_set_title(GTK_WINDOW(c->win), __NAME__);
163 if (related_wv == NULL)
164 c->web_view = webkit_web_view_new();
166 c->web_view = webkit_web_view_new_with_related_view(related_wv);
167 wc = webkit_web_view_get_context(WEBKIT_WEB_VIEW(c->web_view));
169 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), global_zoom);
170 g_signal_connect(G_OBJECT(c->web_view), "notify::title",
171 G_CALLBACK(changed_title), c);
172 g_signal_connect(G_OBJECT(c->web_view), "notify::uri",
173 G_CALLBACK(changed_uri), c);
174 g_signal_connect(G_OBJECT(c->web_view), "notify::estimated-load-progress",
175 G_CALLBACK(changed_load_progress), c);
176 g_signal_connect(G_OBJECT(c->web_view), "create",
177 G_CALLBACK(client_new_request), NULL);
178 g_signal_connect(G_OBJECT(c->web_view), "context-menu",
179 G_CALLBACK(menu_web_view), c);
180 g_signal_connect(G_OBJECT(c->web_view), "close",
181 G_CALLBACK(client_destroy_request), c);
182 g_signal_connect(G_OBJECT(c->web_view), "decide-policy",
183 G_CALLBACK(decide_policy), NULL);
184 g_signal_connect(G_OBJECT(c->web_view), "key-press-event",
185 G_CALLBACK(key_web_view), c);
186 g_signal_connect(G_OBJECT(c->web_view), "button-release-event",
187 G_CALLBACK(key_web_view), c);
188 g_signal_connect(G_OBJECT(c->web_view), "scroll-event",
189 G_CALLBACK(key_web_view), c);
190 g_signal_connect(G_OBJECT(c->web_view), "mouse-target-changed",
191 G_CALLBACK(hover_web_view), c);
192 g_signal_connect(G_OBJECT(c->web_view), "web-process-crashed",
193 G_CALLBACK(crashed_web_view), c);
195 if (!initial_wc_setup_done)
197 if (accepted_language[0] != NULL)
198 webkit_web_context_set_preferred_languages(wc, accepted_language);
200 g_signal_connect(G_OBJECT(wc), "download-started",
201 G_CALLBACK(download_handle_start), NULL);
203 trust_user_certs(wc);
205 initial_wc_setup_done = TRUE;
208 if (user_agent != NULL)
209 g_object_set(G_OBJECT(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view))),
210 "user-agent", user_agent, NULL);
212 if (enable_console_to_stdout)
213 webkit_settings_set_enable_write_console_messages_to_stdout(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view)), TRUE);
216 webkit_settings_set_enable_webgl(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view)), TRUE);
218 c->location = gtk_entry_new();
219 g_signal_connect(G_OBJECT(c->location), "key-press-event",
220 G_CALLBACK(key_location), c);
221 g_signal_connect(G_OBJECT(c->location), "icon-release",
222 G_CALLBACK(icon_location), c);
223 /* XXX This is a workaround. Setting this to NULL (which is done in
224 * grab_feeds_finished() if no feed has been detected) adds a little
225 * padding left of the text. Not sure why. The point of this call
226 * right here is to have that padding right from the start. This
227 * avoids a graphical artifact. */
228 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
229 GTK_ENTRY_ICON_PRIMARY,
232 c->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
233 gtk_box_pack_start(GTK_BOX(c->vbox), c->location, FALSE, FALSE, 0);
234 gtk_box_pack_start(GTK_BOX(c->vbox), c->web_view, TRUE, TRUE, 0);
236 gtk_container_add(GTK_CONTAINER(c->win), c->vbox);
239 show_web_view(NULL, c);
241 g_signal_connect(G_OBJECT(c->web_view), "ready-to-show",
242 G_CALLBACK(show_web_view), c);
246 f = ensure_uri_scheme(uri);
247 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
253 return WEBKIT_WEB_VIEW(c->web_view);
257 client_new_request(WebKitWebView *web_view,
258 WebKitNavigationAction *navigation_action, gpointer data)
260 return client_new(NULL, web_view, FALSE);
264 cooperation_setup(void)
267 gchar *fifofilename, *fifopath;
269 fifofilename = g_strdup_printf("%s-%s", __NAME__".fifo", fifo_suffix);
270 fifopath = g_build_filename(g_get_user_runtime_dir(), fifofilename, NULL);
271 g_free(fifofilename);
273 if (!g_file_test(fifopath, G_FILE_TEST_EXISTS))
274 mkfifo(fifopath, 0600);
276 cooperative_pipe_fp = open(fifopath, O_WRONLY | O_NONBLOCK);
277 if (!cooperative_pipe_fp)
279 fprintf(stderr, __NAME__": Can't open FIFO at all.\n");
283 if (write(cooperative_pipe_fp, "", 0) == -1)
285 /* Could not do an empty write to the FIFO which means there's
286 * no one listening. */
287 close(cooperative_pipe_fp);
288 towatch = g_io_channel_new_file(fifopath, "r+", NULL);
289 g_io_add_watch(towatch, G_IO_IN, (GIOFunc)remote_msg, NULL);
292 cooperative_alone = FALSE;
299 changed_download_progress(GObject *obj, GParamSpec *pspec, gpointer data)
301 WebKitDownload *download = WEBKIT_DOWNLOAD(obj);
302 WebKitURIResponse *resp;
303 GtkToolItem *tb = GTK_TOOL_ITEM(data);
306 gchar *t, *filename, *base;
308 p = webkit_download_get_estimated_progress(download);
312 resp = webkit_download_get_response(download);
313 size_mb = webkit_uri_response_get_content_length(resp) / 1e6;
315 uri = webkit_download_get_destination(download);
316 filename = g_filename_from_uri(uri, NULL, NULL);
317 if (filename == NULL)
319 /* This really should not happen because WebKit uses that URI to
320 * write to a file... */
321 fprintf(stderr, __NAME__": Could not construct file name from URI!\n");
322 t = g_strdup_printf("%s (%.0f%% of %.1f MB)",
323 webkit_uri_response_get_uri(resp), p, size_mb);
327 base = g_path_get_basename(filename);
328 t = g_strdup_printf("%s (%.0f%% of %.1f MB)", base, p, size_mb);
332 gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), t);
337 changed_load_progress(GObject *obj, GParamSpec *pspec, gpointer data)
339 struct Client *c = (struct Client *)data;
342 "a = document.querySelectorAll('"
343 " html > head > link[rel=\"alternate\"][href][type=\"application/atom+xml\"],"
344 " html > head > link[rel=\"alternate\"][href][type=\"application/rss+xml\"]"
351 " for (i = 0; i < a.length; i++)"
353 " url = encodeURIComponent(a[i].href);"
354 " if ('title' in a[i] && a[i].title != '')"
355 " title = encodeURIComponent(a[i].title);"
358 " out += '<li><a href=\"' + url + '\">' + title + '</a></li>';"
363 p = webkit_web_view_get_estimated_load_progress(WEBKIT_WEB_VIEW(c->web_view));
368 /* The page has loaded fully. We now run the short JavaScript
369 * snippet above that operates on the DOM. It tries to grab all
370 * occurences of <link rel="alternate" ...>, i.e. RSS/Atom feed
372 webkit_web_view_run_javascript(WEBKIT_WEB_VIEW(c->web_view),
374 grab_feeds_finished, c);
376 gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), p);
380 changed_title(GObject *obj, GParamSpec *pspec, gpointer data)
383 struct Client *c = (struct Client *)data;
385 u = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
386 t = webkit_web_view_get_title(WEBKIT_WEB_VIEW(c->web_view));
388 u = u == NULL ? __NAME__ : u;
389 u = u[0] == 0 ? __NAME__ : u;
391 t = t == NULL ? u : t;
392 t = t[0] == 0 ? u : t;
394 gtk_window_set_title(GTK_WINDOW(c->win), t);
398 changed_uri(GObject *obj, GParamSpec *pspec, gpointer data)
401 struct Client *c = (struct Client *)data;
404 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
406 /* When a web process crashes, we get a "notify::uri" signal, but we
407 * can no longer read a meaningful URI. It's just an empty string
408 * now. Not updating the location bar in this scenario is important,
409 * because we would override the "WEB PROCESS CRASHED" message. */
410 if (t != NULL && strlen(t) > 0)
412 gtk_entry_set_text(GTK_ENTRY(c->location), t);
414 if (history_file != NULL)
416 fp = fopen(history_file, "a");
419 fprintf(fp, "%s\n", t);
423 perror(__NAME__": Error opening history file");
429 crashed_web_view(WebKitWebView *web_view, gpointer data)
432 struct Client *c = (struct Client *)data;
434 t = g_strdup_printf("WEB PROCESS CRASHED: %s",
435 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(web_view)));
436 gtk_entry_set_text(GTK_ENTRY(c->location), t);
443 decide_policy(WebKitWebView *web_view, WebKitPolicyDecision *decision,
444 WebKitPolicyDecisionType type, gpointer data)
446 WebKitResponsePolicyDecision *r;
450 case WEBKIT_POLICY_DECISION_TYPE_RESPONSE:
451 r = WEBKIT_RESPONSE_POLICY_DECISION(decision);
452 if (!webkit_response_policy_decision_is_mime_type_supported(r))
453 webkit_policy_decision_download(decision);
455 webkit_policy_decision_use(decision);
458 /* Use whatever default there is. */
465 download_handle_finished(WebKitDownload *download, gpointer data)
471 download_handle_start(WebKitWebView *web_view, WebKitDownload *download,
474 g_signal_connect(G_OBJECT(download), "decide-destination",
475 G_CALLBACK(download_handle), data);
479 download_handle(WebKitDownload *download, gchar *suggested_filename, gpointer data)
481 gchar *sug_clean, *path, *path2 = NULL, *uri;
486 sug_clean = g_strdup(suggested_filename);
487 for (i = 0; i < strlen(sug_clean); i++)
488 if (sug_clean[i] == G_DIR_SEPARATOR)
491 path = g_build_filename(download_dir, sug_clean, NULL);
492 path2 = g_strdup(path);
493 while (g_file_test(path2, G_FILE_TEST_EXISTS) && suffix < 1000)
497 path2 = g_strdup_printf("%s.%d", path, suffix);
503 fprintf(stderr, __NAME__": Suffix reached limit for download.\n");
504 webkit_download_cancel(download);
508 uri = g_filename_to_uri(path2, NULL, NULL);
509 webkit_download_set_destination(download, uri);
512 tb = gtk_tool_button_new(NULL, NULL);
513 gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(tb), "gtk-delete");
514 gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), sug_clean);
515 gtk_toolbar_insert(GTK_TOOLBAR(dm.toolbar), tb, 0);
516 gtk_widget_show_all(dm.win);
518 g_signal_connect(G_OBJECT(download), "notify::estimated-progress",
519 G_CALLBACK(changed_download_progress), tb);
522 g_signal_connect(G_OBJECT(download), "finished",
523 G_CALLBACK(download_handle_finished), NULL);
525 g_object_ref(download);
526 g_signal_connect(G_OBJECT(tb), "clicked",
527 G_CALLBACK(downloadmanager_cancel), download);
534 /* Propagate -- to whom it may concern. */
539 downloadmanager_cancel(GtkToolButton *tb, gpointer data)
541 WebKitDownload *download = WEBKIT_DOWNLOAD(data);
543 webkit_download_cancel(download);
544 g_object_unref(download);
546 gtk_widget_destroy(GTK_WIDGET(tb));
550 downloadmanager_delete(GtkWidget *obj, gpointer data)
552 if (!quit_if_nothing_active())
553 gtk_widget_hide(dm.win);
559 downloadmanager_setup(void)
561 dm.win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
562 gtk_window_set_type_hint(GTK_WINDOW(dm.win), GDK_WINDOW_TYPE_HINT_DIALOG);
563 gtk_window_set_default_size(GTK_WINDOW(dm.win), 500, 250);
564 gtk_window_set_title(GTK_WINDOW(dm.win), __NAME__" - Download Manager");
565 g_signal_connect(G_OBJECT(dm.win), "delete-event",
566 G_CALLBACK(downloadmanager_delete), NULL);
567 g_signal_connect(G_OBJECT(dm.win), "key-press-event",
568 G_CALLBACK(key_downloadmanager), NULL);
570 dm.toolbar = gtk_toolbar_new();
571 gtk_orientable_set_orientation(GTK_ORIENTABLE(dm.toolbar),
572 GTK_ORIENTATION_VERTICAL);
573 gtk_toolbar_set_style(GTK_TOOLBAR(dm.toolbar), GTK_TOOLBAR_BOTH_HORIZ);
574 gtk_toolbar_set_show_arrow(GTK_TOOLBAR(dm.toolbar), FALSE);
576 dm.scroll = gtk_scrolled_window_new(NULL, NULL);
577 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(dm.scroll),
578 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
579 gtk_container_add(GTK_CONTAINER(dm.scroll), dm.toolbar);
581 gtk_container_add(GTK_CONTAINER(dm.win), dm.scroll);
585 ensure_uri_scheme(const gchar *t)
589 f = g_ascii_strdown(t, -1);
590 if (!g_str_has_prefix(f, "http:") &&
591 !g_str_has_prefix(f, "https:") &&
592 !g_str_has_prefix(f, "file:") &&
593 !g_str_has_prefix(f, "about:") &&
594 !g_str_has_prefix(f, "data:"))
597 fabs = realpath(t, NULL);
600 f = g_strdup_printf("file://%s", fabs);
604 f = g_strdup_printf("http://%s", t);
612 external_handler_run(GSimpleAction *simple, GVariant *param, gpointer data)
614 struct Client *c = (struct Client *)data;
615 gchar *argv[] = { "lariza-external-handler", "-u", NULL, NULL };
622 argv[2] = c->external_handler_uri;
623 if (!g_spawn_async(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL,
626 fprintf(stderr, __NAME__": Could not launch key handler: %s\n",
631 g_spawn_close_pid(pid);
635 grab_environment_configuration(void)
639 e = g_getenv(__NAME_UPPERCASE__"_ACCEPTED_LANGUAGE");
641 accepted_language[0] = g_strdup(e);
643 e = g_getenv(__NAME_UPPERCASE__"_DOWNLOAD_DIR");
645 download_dir = g_strdup(e);
647 e = g_getenv(__NAME_UPPERCASE__"_ENABLE_CONSOLE_TO_STDOUT");
649 enable_console_to_stdout = TRUE;
651 e = g_getenv(__NAME_UPPERCASE__"_ENABLE_EXPERIMENTAL_WEBGL");
655 e = g_getenv(__NAME_UPPERCASE__"_FIFO_SUFFIX");
657 fifo_suffix = g_strdup(e);
659 e = g_getenv(__NAME_UPPERCASE__"_HISTORY_FILE");
661 history_file = g_strdup(e);
663 e = g_getenv(__NAME_UPPERCASE__"_HOME_URI");
665 home_uri = g_strdup(e);
667 e = g_getenv(__NAME_UPPERCASE__"_USER_AGENT");
669 user_agent = g_strdup(e);
671 e = g_getenv(__NAME_UPPERCASE__"_ZOOM");
673 global_zoom = atof(e);
677 grab_feeds_finished(GObject *object, GAsyncResult *result, gpointer data)
679 struct Client *c = (struct Client *)data;
680 WebKitJavascriptResult *js_result;
682 JSCException *exception;
686 g_free(c->feed_html);
689 /* This was taken almost verbatim from the example in WebKit's
692 * https://webkitgtk.org/reference/webkit2gtk/stable/WebKitWebView.html#webkit-web-view-run-javascript-finish */
694 js_result = webkit_web_view_run_javascript_finish(WEBKIT_WEB_VIEW(object),
698 fprintf(stderr, __NAME__": Error running javascript: %s\n", err->message);
703 value = webkit_javascript_result_get_js_value(js_result);
704 if (jsc_value_is_string(value))
706 str_value = jsc_value_to_string(value);
707 exception = jsc_context_get_exception(jsc_value_get_context(value));
708 if (exception != NULL)
710 fprintf(stderr, __NAME__": Error running javascript: %s\n",
711 jsc_exception_get_message(exception));
714 c->feed_html = str_value;
716 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
717 GTK_ENTRY_ICON_PRIMARY,
718 "application-rss+xml-symbolic");
719 gtk_entry_set_icon_activatable(GTK_ENTRY(c->location),
720 GTK_ENTRY_ICON_PRIMARY,
725 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
726 GTK_ENTRY_ICON_PRIMARY,
730 webkit_javascript_result_unref(js_result);
734 hover_web_view(WebKitWebView *web_view, WebKitHitTestResult *ht, guint modifiers,
737 struct Client *c = (struct Client *)data;
739 if (!gtk_widget_is_focus(c->location))
741 if (webkit_hit_test_result_context_is_link(ht))
743 gtk_entry_set_text(GTK_ENTRY(c->location),
744 webkit_hit_test_result_get_link_uri(ht));
746 if (c->hover_uri != NULL)
747 g_free(c->hover_uri);
748 c->hover_uri = g_strdup(webkit_hit_test_result_get_link_uri(ht));
752 gtk_entry_set_text(GTK_ENTRY(c->location),
753 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view)));
755 if (c->hover_uri != NULL)
756 g_free(c->hover_uri);
763 icon_location(GtkEntry *entry, GtkEntryIconPosition icon_pos, GdkEvent *event,
766 struct Client *c = (struct Client *)data;
768 gchar *data_template =
773 " <meta charset=\"UTF-8\">"
774 " <title>Feeds</title>"
777 " <p>Feeds found on this page:</p>"
784 if (c->feed_html != NULL)
786 /* What we're actually trying to do is show a simple HTML page
787 * that lists all the feeds on the current page. The function
788 * webkit_web_view_load_html() looks like the proper way to do
789 * that. Sad thing is, it doesn't create a history entry, but
790 * instead simply replaces the content of the current page. This
791 * is not what we want.
793 * RFC 2397 [0] defines the data URI scheme [1]. We abuse this
794 * mechanism to show my custom HTML snippet *and* create a
797 * [0]: https://tools.ietf.org/html/rfc2397
798 * [1]: https://en.wikipedia.org/wiki/Data_URI_scheme */
799 d = g_strdup_printf(data_template, c->feed_html);
800 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), d);
806 key_common(GtkWidget *widget, GdkEvent *event, gpointer data)
808 struct Client *c = (struct Client *)data;
809 WebKitWebContext *wc = webkit_web_view_get_context(WEBKIT_WEB_VIEW(c->web_view));
812 if (event->type == GDK_KEY_PRESS)
814 if (((GdkEventKey *)event)->state & GDK_MOD1_MASK)
816 switch (((GdkEventKey *)event)->keyval)
818 case GDK_KEY_q: /* close window (left hand) */
819 gtk_widget_destroy(c->win);
821 case GDK_KEY_w: /* home (left hand) */
822 f = ensure_uri_scheme(home_uri);
823 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
826 case GDK_KEY_e: /* new tab (left hand) */
827 f = ensure_uri_scheme(home_uri);
828 client_new(f, NULL, TRUE);
831 case GDK_KEY_r: /* reload (left hand) */
832 webkit_web_view_reload_bypass_cache(WEBKIT_WEB_VIEW(
835 case GDK_KEY_d: /* download manager (left hand) */
836 gtk_widget_show_all(dm.win);
838 case GDK_KEY_2: /* search forward (left hand) */
839 case GDK_KEY_n: /* search forward (maybe both hands) */
842 case GDK_KEY_3: /* search backward (left hand) */
845 case GDK_KEY_l: /* location (BOTH hands) */
846 gtk_widget_grab_focus(c->location);
848 case GDK_KEY_k: /* initiate search (BOTH hands) */
849 gtk_widget_grab_focus(c->location);
850 gtk_entry_set_text(GTK_ENTRY(c->location), ":/");
851 gtk_editable_set_position(GTK_EDITABLE(c->location), -1);
853 case GDK_KEY_c: /* reload trusted certs (left hand) */
854 trust_user_certs(wc);
856 case GDK_KEY_x: /* launch external handler (left hand) */
857 if (c->external_handler_uri != NULL)
858 g_free(c->external_handler_uri);
859 c->external_handler_uri = g_strdup(
860 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view)));
861 external_handler_run(NULL, NULL, c);
865 /* navigate backward (left hand) */
866 else if (((GdkEventKey *)event)->keyval == GDK_KEY_F2)
868 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
871 /* navigate forward (left hand) */
872 else if (((GdkEventKey *)event)->keyval == GDK_KEY_F3)
874 webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
883 key_downloadmanager(GtkWidget *widget, GdkEvent *event, gpointer data)
885 if (event->type == GDK_KEY_PRESS)
887 if (((GdkEventKey *)event)->state & GDK_MOD1_MASK)
889 switch (((GdkEventKey *)event)->keyval)
891 case GDK_KEY_d: /* close window (left hand) */
893 downloadmanager_delete(dm.win, NULL);
903 key_location(GtkWidget *widget, GdkEvent *event, gpointer data)
905 struct Client *c = (struct Client *)data;
909 if (key_common(widget, event, data))
912 if (event->type == GDK_KEY_PRESS)
914 switch (((GdkEventKey *)event)->keyval)
916 case GDK_KEY_KP_Enter:
918 gtk_widget_grab_focus(c->web_view);
919 t = gtk_entry_get_text(GTK_ENTRY(c->location));
920 if (t != NULL && t[0] == ':' && t[1] == '/')
922 if (search_text != NULL)
924 search_text = g_strdup(t + 2);
927 else if (!keywords_try_search(WEBKIT_WEB_VIEW(c->web_view), t))
929 f = ensure_uri_scheme(t);
930 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
935 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
936 gtk_entry_set_text(GTK_ENTRY(c->location),
937 (t == NULL ? __NAME__ : t));
946 key_web_view(GtkWidget *widget, GdkEvent *event, gpointer data)
948 struct Client *c = (struct Client *)data;
952 if (key_common(widget, event, data))
955 if (event->type == GDK_KEY_PRESS)
957 if (((GdkEventKey *)event)->keyval == GDK_KEY_Escape)
959 webkit_web_view_stop_loading(WEBKIT_WEB_VIEW(c->web_view));
960 gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), 0);
963 else if (event->type == GDK_BUTTON_RELEASE)
965 switch (((GdkEventButton *)event)->button)
968 if (c->hover_uri != NULL)
970 client_new(c->hover_uri, NULL, TRUE);
975 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
978 webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
982 else if (event->type == GDK_SCROLL)
984 if (((GdkEventScroll *)event)->state & GDK_MOD1_MASK ||
985 ((GdkEventScroll *)event)->state & GDK_CONTROL_MASK)
987 gdk_event_get_scroll_deltas(event, &dx, &dy);
988 z = webkit_web_view_get_zoom_level(WEBKIT_WEB_VIEW(c->web_view));
990 z = dx != 0 ? global_zoom : z;
991 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), z);
1003 GIOChannel *channel = NULL;
1004 gchar *path = NULL, *buf = NULL;
1005 gchar **tokens = NULL;
1007 keywords = g_hash_table_new(g_str_hash, g_str_equal);
1009 path = g_build_filename(g_get_user_config_dir(), __NAME__, "keywordsearch",
1011 channel = g_io_channel_new_file(path, "r", &err);
1012 if (channel != NULL)
1014 while (g_io_channel_read_line(channel, &buf, NULL, NULL, NULL)
1015 == G_IO_STATUS_NORMAL)
1020 tokens = g_strsplit(buf, " ", 2);
1021 if (tokens[0] != NULL && tokens[1] != NULL)
1022 g_hash_table_insert(keywords, g_strdup(tokens[0]),
1023 g_strdup(tokens[1]));
1028 g_io_channel_shutdown(channel, FALSE, NULL);
1034 keywords_try_search(WebKitWebView *web_view, const gchar *t)
1036 gboolean ret = FALSE;
1037 gchar **tokens = NULL;
1038 gchar *val = NULL, *escaped = NULL, *uri = NULL;
1040 tokens = g_strsplit(t, " ", 2);
1041 if (tokens[0] != NULL && tokens[1] != NULL)
1043 val = g_hash_table_lookup(keywords, tokens[0]);
1046 escaped = g_uri_escape_string(tokens[1], NULL, TRUE);
1047 uri = g_strdup_printf((gchar *)val, escaped);
1048 webkit_web_view_load_uri(web_view, uri);
1060 menu_web_view(WebKitWebView *web_view, WebKitContextMenu *menu, GdkEvent *ev,
1061 WebKitHitTestResult *ht, gpointer data)
1063 struct Client *c = (struct Client *)data;
1064 GSimpleAction *action = NULL;
1065 WebKitContextMenuItem *mi = NULL;
1066 const gchar *uri = NULL;
1070 if (webkit_hit_test_result_context_is_link(ht))
1071 uri = webkit_hit_test_result_get_link_uri(ht);
1072 else if (webkit_hit_test_result_context_is_image(ht))
1073 uri = webkit_hit_test_result_get_image_uri(ht);
1074 else if (webkit_hit_test_result_context_is_media(ht))
1075 uri = webkit_hit_test_result_get_media_uri(ht);
1079 webkit_context_menu_append(menu, webkit_context_menu_item_new_separator());
1081 if (c->external_handler_uri != NULL)
1082 g_free(c->external_handler_uri);
1083 c->external_handler_uri = g_strdup(uri);
1084 action = g_simple_action_new("external_handler", NULL);
1085 g_signal_connect(G_OBJECT(action), "activate",
1086 G_CALLBACK(external_handler_run), data);
1087 mi = webkit_context_menu_item_new_from_gaction(G_ACTION(action),
1088 "Open with external handler",
1090 webkit_context_menu_append(menu, mi);
1091 g_object_unref(action);
1094 /* FALSE = Show the menu. (TRUE = Don't ever show it.) */
1099 quit_if_nothing_active(void)
1109 gtk_widget_show_all(dm.win);
1116 remote_msg(GIOChannel *channel, GIOCondition condition, gpointer data)
1120 g_io_channel_read_line(channel, &uri, NULL, NULL, NULL);
1124 client_new(uri, NULL, TRUE);
1131 search(gpointer data, gint direction)
1133 struct Client *c = (struct Client *)data;
1134 WebKitWebView *web_view = WEBKIT_WEB_VIEW(c->web_view);
1135 WebKitFindController *fc = webkit_web_view_get_find_controller(web_view);
1137 if (search_text == NULL)
1143 webkit_find_controller_search(fc, search_text,
1144 WEBKIT_FIND_OPTIONS_CASE_INSENSITIVE |
1145 WEBKIT_FIND_OPTIONS_WRAP_AROUND,
1149 webkit_find_controller_search_next(fc);
1152 webkit_find_controller_search_previous(fc);
1158 show_web_view(WebKitWebView *web_view, gpointer data)
1160 struct Client *c = (struct Client *)data;
1164 gtk_widget_grab_focus(c->web_view);
1165 gtk_widget_show_all(c->win);
1172 GIOChannel *tabbed_stdout_channel;
1174 gchar *output = NULL;
1175 char *argv[] = { "tabbed", "-c", "-d", "-p", "s1", "-n", __NAME__, NULL };
1178 if (!g_spawn_async_with_pipes(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL,
1179 NULL, NULL, NULL, &tabbed_stdout, NULL,
1182 fprintf(stderr, __NAME__": Could not launch tabbed: %s\n", err->message);
1187 tabbed_stdout_channel = g_io_channel_unix_new(tabbed_stdout);
1188 if (tabbed_stdout_channel == NULL)
1190 fprintf(stderr, __NAME__": Could open tabbed's stdout\n");
1193 g_io_channel_read_line(tabbed_stdout_channel, &output, NULL, NULL, NULL);
1194 g_io_channel_shutdown(tabbed_stdout_channel, FALSE, NULL);
1197 fprintf(stderr, __NAME__": Could not read XID from tabbed\n");
1201 plug_into = strtol(output, NULL, 16);
1204 fprintf(stderr, __NAME__": The XID from tabbed is 0\n");
1209 trust_user_certs(WebKitWebContext *wc)
1211 GTlsCertificate *cert;
1212 const gchar *basedir, *file, *absfile;
1215 basedir = g_build_filename(g_get_user_config_dir(), __NAME__, "certs", NULL);
1216 dir = g_dir_open(basedir, 0, NULL);
1219 file = g_dir_read_name(dir);
1220 while (file != NULL)
1222 absfile = g_build_filename(g_get_user_config_dir(), __NAME__, "certs",
1224 cert = g_tls_certificate_new_from_file(absfile, NULL);
1226 fprintf(stderr, __NAME__": Could not load trusted cert '%s'\n", file);
1228 webkit_web_context_allow_tls_certificate_for_host(wc, cert, file);
1229 file = g_dir_read_name(dir);
1237 main(int argc, char **argv)
1242 gtk_init(&argc, &argv);
1243 webkit_web_context_set_process_model(webkit_web_context_get_default(),
1244 WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
1246 grab_environment_configuration();
1248 while ((opt = getopt(argc, argv, "e:CT")) != -1)
1253 embed = atol(optarg);
1254 tabbed_automagic = FALSE;
1257 cooperative_instances = FALSE;
1260 tabbed_automagic = FALSE;
1263 fprintf(stderr, "Usage: "__NAME__" [OPTION]... [URI]...\n");
1269 if (cooperative_instances)
1270 cooperation_setup();
1271 downloadmanager_setup();
1273 if (tabbed_automagic && !(cooperative_instances && !cooperative_alone))
1274 embed = tabbed_launch();
1276 if (!cooperative_instances || cooperative_alone)
1278 c = g_build_filename(g_get_user_config_dir(), __NAME__, "web_extensions",
1280 webkit_web_context_set_web_extensions_directory(
1281 webkit_web_context_get_default(), c
1286 client_new(home_uri, NULL, TRUE);
1289 for (i = optind; i < argc; i++)
1290 client_new(argv[i], NULL, TRUE);
1293 if (!cooperative_instances || cooperative_alone)