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 run_user_scripts(WebKitWebView *);
52 static void search(gpointer, gint);
53 static void show_web_view(WebKitWebView *, gpointer);
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_console_to_stdout = FALSE;
83 static gchar *fifo_suffix = "main";
84 static gdouble global_zoom = 1.0;
85 static gchar *history_file = NULL;
86 static gchar *home_uri = "about:blank";
87 static gboolean initial_wc_setup_done = FALSE;
88 static GHashTable *keywords = NULL;
89 static gchar *search_text = NULL;
90 static gchar *user_agent = NULL;
94 client_destroy(GtkWidget *widget, gpointer data)
96 struct Client *c = (struct Client *)data;
98 g_signal_handlers_disconnect_by_func(G_OBJECT(c->web_view),
99 changed_load_progress, c);
104 quit_if_nothing_active();
108 client_destroy_request(WebKitWebView *web_view, gpointer data)
110 struct Client *c = (struct Client *)data;
112 gtk_widget_destroy(c->win);
118 client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show)
121 WebKitWebContext *wc;
124 if (uri != NULL && cooperative_instances && !cooperative_alone)
126 f = ensure_uri_scheme(uri);
127 write(cooperative_pipe_fp, f, strlen(f));
128 write(cooperative_pipe_fp, "\n", 1);
133 c = calloc(1, sizeof(struct Client));
136 fprintf(stderr, __NAME__": fatal: calloc failed\n");
141 c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
143 gtk_window_set_default_size(GTK_WINDOW(c->win), 800, 600);
145 g_signal_connect(G_OBJECT(c->win), "destroy", G_CALLBACK(client_destroy), c);
146 gtk_window_set_title(GTK_WINDOW(c->win), __NAME__);
148 if (related_wv == NULL)
149 c->web_view = webkit_web_view_new();
151 c->web_view = webkit_web_view_new_with_related_view(related_wv);
152 wc = webkit_web_view_get_context(WEBKIT_WEB_VIEW(c->web_view));
154 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), global_zoom);
155 g_signal_connect(G_OBJECT(c->web_view), "notify::title",
156 G_CALLBACK(changed_title), c);
157 g_signal_connect(G_OBJECT(c->web_view), "notify::uri",
158 G_CALLBACK(changed_uri), c);
159 g_signal_connect(G_OBJECT(c->web_view), "notify::estimated-load-progress",
160 G_CALLBACK(changed_load_progress), c);
161 g_signal_connect(G_OBJECT(c->web_view), "create",
162 G_CALLBACK(client_new_request), NULL);
163 g_signal_connect(G_OBJECT(c->web_view), "context-menu",
164 G_CALLBACK(menu_web_view), c);
165 g_signal_connect(G_OBJECT(c->web_view), "close",
166 G_CALLBACK(client_destroy_request), c);
167 g_signal_connect(G_OBJECT(c->web_view), "decide-policy",
168 G_CALLBACK(decide_policy), NULL);
169 g_signal_connect(G_OBJECT(c->web_view), "key-press-event",
170 G_CALLBACK(key_web_view), c);
171 g_signal_connect(G_OBJECT(c->web_view), "button-release-event",
172 G_CALLBACK(key_web_view), c);
173 g_signal_connect(G_OBJECT(c->web_view), "scroll-event",
174 G_CALLBACK(key_web_view), c);
175 g_signal_connect(G_OBJECT(c->web_view), "mouse-target-changed",
176 G_CALLBACK(hover_web_view), c);
177 g_signal_connect(G_OBJECT(c->web_view), "web-process-crashed",
178 G_CALLBACK(crashed_web_view), c);
180 if (!initial_wc_setup_done)
182 if (accepted_language[0] != NULL)
183 webkit_web_context_set_preferred_languages(wc, accepted_language);
185 g_signal_connect(G_OBJECT(wc), "download-started",
186 G_CALLBACK(download_handle_start), NULL);
188 trust_user_certs(wc);
190 initial_wc_setup_done = TRUE;
193 if (user_agent != NULL)
194 g_object_set(G_OBJECT(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view))),
195 "user-agent", user_agent, NULL);
197 if (enable_console_to_stdout)
198 webkit_settings_set_enable_write_console_messages_to_stdout(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view)), TRUE);
200 webkit_settings_set_enable_developer_extras(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view)), TRUE);
202 c->location = gtk_entry_new();
203 g_signal_connect(G_OBJECT(c->location), "key-press-event",
204 G_CALLBACK(key_location), c);
205 g_signal_connect(G_OBJECT(c->location), "icon-release",
206 G_CALLBACK(icon_location), c);
207 /* XXX This is a workaround. Setting this to NULL (which is done in
208 * grab_feeds_finished() if no feed has been detected) adds a little
209 * padding left of the text. Not sure why. The point of this call
210 * right here is to have that padding right from the start. This
211 * avoids a graphical artifact. */
212 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
213 GTK_ENTRY_ICON_PRIMARY,
216 c->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
217 gtk_box_pack_start(GTK_BOX(c->vbox), c->location, FALSE, FALSE, 0);
218 gtk_box_pack_start(GTK_BOX(c->vbox), c->web_view, TRUE, TRUE, 0);
220 gtk_container_add(GTK_CONTAINER(c->win), c->vbox);
223 show_web_view(NULL, c);
225 g_signal_connect(G_OBJECT(c->web_view), "ready-to-show",
226 G_CALLBACK(show_web_view), c);
230 f = ensure_uri_scheme(uri);
231 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
237 return WEBKIT_WEB_VIEW(c->web_view);
241 client_new_request(WebKitWebView *web_view,
242 WebKitNavigationAction *navigation_action, gpointer data)
244 return client_new(NULL, web_view, FALSE);
248 cooperation_setup(void)
251 gchar *fifofilename, *fifopath;
253 fifofilename = g_strdup_printf("%s-%s", __NAME__".fifo", fifo_suffix);
254 fifopath = g_build_filename(g_get_user_runtime_dir(), fifofilename, NULL);
255 g_free(fifofilename);
257 if (!g_file_test(fifopath, G_FILE_TEST_EXISTS))
258 mkfifo(fifopath, 0600);
260 cooperative_pipe_fp = open(fifopath, O_WRONLY | O_NONBLOCK);
261 if (!cooperative_pipe_fp)
263 fprintf(stderr, __NAME__": Can't open FIFO at all.\n");
267 if (write(cooperative_pipe_fp, "", 0) == -1)
269 /* Could not do an empty write to the FIFO which means there's
270 * no one listening. */
271 close(cooperative_pipe_fp);
272 towatch = g_io_channel_new_file(fifopath, "r+", NULL);
273 g_io_add_watch(towatch, G_IO_IN, (GIOFunc)remote_msg, NULL);
276 cooperative_alone = FALSE;
283 changed_download_progress(GObject *obj, GParamSpec *pspec, gpointer data)
285 WebKitDownload *download = WEBKIT_DOWNLOAD(obj);
286 WebKitURIResponse *resp;
287 GtkToolItem *tb = GTK_TOOL_ITEM(data);
290 gchar *t, *filename, *base;
292 p = webkit_download_get_estimated_progress(download);
296 resp = webkit_download_get_response(download);
297 size_mb = webkit_uri_response_get_content_length(resp) / 1e6;
299 uri = webkit_download_get_destination(download);
300 filename = g_filename_from_uri(uri, NULL, NULL);
301 if (filename == NULL)
303 /* This really should not happen because WebKit uses that URI to
304 * write to a file... */
305 fprintf(stderr, __NAME__": Could not construct file name from URI!\n");
306 t = g_strdup_printf("%s (%.0f%% of %.1f MB)",
307 webkit_uri_response_get_uri(resp), p, size_mb);
311 base = g_path_get_basename(filename);
312 t = g_strdup_printf("%s (%.0f%% of %.1f MB)", base, p, size_mb);
316 gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), t);
321 changed_load_progress(GObject *obj, GParamSpec *pspec, gpointer data)
323 struct Client *c = (struct Client *)data;
326 "a = document.querySelectorAll('"
327 " html > head > link[rel=\"alternate\"][href][type=\"application/atom+xml\"],"
328 " html > head > link[rel=\"alternate\"][href][type=\"application/rss+xml\"]"
335 " for (i = 0; i < a.length; i++)"
337 " url = encodeURIComponent(a[i].href);"
338 " if ('title' in a[i] && a[i].title != '')"
339 " title = encodeURIComponent(a[i].title);"
342 " out += '<li><a href=\"' + url + '\">' + title + '</a></li>';"
347 p = webkit_web_view_get_estimated_load_progress(WEBKIT_WEB_VIEW(c->web_view));
352 /* The page has loaded fully. We now run the short JavaScript
353 * snippet above that operates on the DOM. It tries to grab all
354 * occurences of <link rel="alternate" ...>, i.e. RSS/Atom feed
356 webkit_web_view_run_javascript(WEBKIT_WEB_VIEW(c->web_view),
358 grab_feeds_finished, c);
360 run_user_scripts(WEBKIT_WEB_VIEW(c->web_view));
362 gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), p);
366 changed_title(GObject *obj, GParamSpec *pspec, gpointer data)
369 struct Client *c = (struct Client *)data;
371 u = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
372 t = webkit_web_view_get_title(WEBKIT_WEB_VIEW(c->web_view));
374 u = u == NULL ? __NAME__ : u;
375 u = u[0] == 0 ? __NAME__ : u;
377 t = t == NULL ? u : t;
378 t = t[0] == 0 ? u : t;
380 gtk_window_set_title(GTK_WINDOW(c->win), t);
384 changed_uri(GObject *obj, GParamSpec *pspec, gpointer data)
387 struct Client *c = (struct Client *)data;
390 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
392 /* When a web process crashes, we get a "notify::uri" signal, but we
393 * can no longer read a meaningful URI. It's just an empty string
394 * now. Not updating the location bar in this scenario is important,
395 * because we would override the "WEB PROCESS CRASHED" message. */
396 if (t != NULL && strlen(t) > 0)
398 gtk_entry_set_text(GTK_ENTRY(c->location), t);
400 if (history_file != NULL)
402 fp = fopen(history_file, "a");
405 fprintf(fp, "%s\n", t);
409 perror(__NAME__": Error opening history file");
415 crashed_web_view(WebKitWebView *web_view, gpointer data)
418 struct Client *c = (struct Client *)data;
420 t = g_strdup_printf("WEB PROCESS CRASHED: %s",
421 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(web_view)));
422 gtk_entry_set_text(GTK_ENTRY(c->location), t);
429 decide_policy(WebKitWebView *web_view, WebKitPolicyDecision *decision,
430 WebKitPolicyDecisionType type, gpointer data)
432 WebKitResponsePolicyDecision *r;
436 case WEBKIT_POLICY_DECISION_TYPE_RESPONSE:
437 r = WEBKIT_RESPONSE_POLICY_DECISION(decision);
438 if (!webkit_response_policy_decision_is_mime_type_supported(r))
439 webkit_policy_decision_download(decision);
441 webkit_policy_decision_use(decision);
444 /* Use whatever default there is. */
451 download_handle_finished(WebKitDownload *download, gpointer data)
457 download_handle_start(WebKitWebView *web_view, WebKitDownload *download,
460 g_signal_connect(G_OBJECT(download), "decide-destination",
461 G_CALLBACK(download_handle), data);
465 download_handle(WebKitDownload *download, gchar *suggested_filename, gpointer data)
467 gchar *sug_clean, *path, *path2 = NULL, *uri;
472 sug_clean = g_strdup(suggested_filename);
473 for (i = 0; i < strlen(sug_clean); i++)
474 if (sug_clean[i] == G_DIR_SEPARATOR)
477 path = g_build_filename(download_dir, sug_clean, NULL);
478 path2 = g_strdup(path);
479 while (g_file_test(path2, G_FILE_TEST_EXISTS) && suffix < 1000)
483 path2 = g_strdup_printf("%s.%d", path, suffix);
489 fprintf(stderr, __NAME__": Suffix reached limit for download.\n");
490 webkit_download_cancel(download);
494 uri = g_filename_to_uri(path2, NULL, NULL);
495 webkit_download_set_destination(download, uri);
498 tb = gtk_tool_button_new(NULL, NULL);
499 gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(tb), "gtk-delete");
500 gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), sug_clean);
501 gtk_toolbar_insert(GTK_TOOLBAR(dm.toolbar), tb, 0);
502 gtk_widget_show_all(dm.win);
504 g_signal_connect(G_OBJECT(download), "notify::estimated-progress",
505 G_CALLBACK(changed_download_progress), tb);
508 g_signal_connect(G_OBJECT(download), "finished",
509 G_CALLBACK(download_handle_finished), NULL);
511 g_object_ref(download);
512 g_signal_connect(G_OBJECT(tb), "clicked",
513 G_CALLBACK(downloadmanager_cancel), download);
520 /* Propagate -- to whom it may concern. */
525 downloadmanager_cancel(GtkToolButton *tb, gpointer data)
527 WebKitDownload *download = WEBKIT_DOWNLOAD(data);
529 webkit_download_cancel(download);
530 g_object_unref(download);
532 gtk_widget_destroy(GTK_WIDGET(tb));
536 downloadmanager_delete(GtkWidget *obj, gpointer data)
538 if (!quit_if_nothing_active())
539 gtk_widget_hide(dm.win);
545 downloadmanager_setup(void)
547 dm.win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
548 gtk_window_set_type_hint(GTK_WINDOW(dm.win), GDK_WINDOW_TYPE_HINT_DIALOG);
549 gtk_window_set_default_size(GTK_WINDOW(dm.win), 500, 250);
550 gtk_window_set_title(GTK_WINDOW(dm.win), __NAME__" - Download Manager");
551 g_signal_connect(G_OBJECT(dm.win), "delete-event",
552 G_CALLBACK(downloadmanager_delete), NULL);
553 g_signal_connect(G_OBJECT(dm.win), "key-press-event",
554 G_CALLBACK(key_downloadmanager), NULL);
556 dm.toolbar = gtk_toolbar_new();
557 gtk_orientable_set_orientation(GTK_ORIENTABLE(dm.toolbar),
558 GTK_ORIENTATION_VERTICAL);
559 gtk_toolbar_set_style(GTK_TOOLBAR(dm.toolbar), GTK_TOOLBAR_BOTH_HORIZ);
560 gtk_toolbar_set_show_arrow(GTK_TOOLBAR(dm.toolbar), FALSE);
562 dm.scroll = gtk_scrolled_window_new(NULL, NULL);
563 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(dm.scroll),
564 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
565 gtk_container_add(GTK_CONTAINER(dm.scroll), dm.toolbar);
567 gtk_container_add(GTK_CONTAINER(dm.win), dm.scroll);
571 ensure_uri_scheme(const gchar *t)
575 f = g_ascii_strdown(t, -1);
576 if (!g_str_has_prefix(f, "http:") &&
577 !g_str_has_prefix(f, "https:") &&
578 !g_str_has_prefix(f, "file:") &&
579 !g_str_has_prefix(f, "about:") &&
580 !g_str_has_prefix(f, "data:"))
583 fabs = realpath(t, NULL);
586 f = g_strdup_printf("file://%s", fabs);
590 f = g_strdup_printf("http://%s", t);
598 external_handler_run(GSimpleAction *simple, GVariant *param, gpointer data)
600 struct Client *c = (struct Client *)data;
601 gchar *argv[] = { "lariza-external-handler", "-u", NULL, NULL };
608 argv[2] = c->external_handler_uri;
609 if (!g_spawn_async(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL,
612 fprintf(stderr, __NAME__": Could not launch key handler: %s\n",
617 g_spawn_close_pid(pid);
621 grab_environment_configuration(void)
625 e = g_getenv(__NAME_UPPERCASE__"_ACCEPTED_LANGUAGE");
627 accepted_language[0] = g_strdup(e);
629 e = g_getenv(__NAME_UPPERCASE__"_DOWNLOAD_DIR");
631 download_dir = g_strdup(e);
633 e = g_getenv(__NAME_UPPERCASE__"_ENABLE_CONSOLE_TO_STDOUT");
635 enable_console_to_stdout = TRUE;
637 e = g_getenv(__NAME_UPPERCASE__"_FIFO_SUFFIX");
639 fifo_suffix = g_strdup(e);
641 e = g_getenv(__NAME_UPPERCASE__"_HISTORY_FILE");
643 history_file = g_strdup(e);
645 e = g_getenv(__NAME_UPPERCASE__"_HOME_URI");
647 home_uri = g_strdup(e);
649 e = g_getenv(__NAME_UPPERCASE__"_USER_AGENT");
651 user_agent = g_strdup(e);
653 e = g_getenv(__NAME_UPPERCASE__"_ZOOM");
655 global_zoom = atof(e);
659 grab_feeds_finished(GObject *object, GAsyncResult *result, gpointer data)
661 struct Client *c = (struct Client *)data;
662 WebKitJavascriptResult *js_result;
664 JSCException *exception;
668 g_free(c->feed_html);
671 /* This was taken almost verbatim from the example in WebKit's
674 * https://webkitgtk.org/reference/webkit2gtk/stable/WebKitWebView.html#webkit-web-view-run-javascript-finish */
676 js_result = webkit_web_view_run_javascript_finish(WEBKIT_WEB_VIEW(object),
680 fprintf(stderr, __NAME__": Error running javascript: %s\n", err->message);
685 value = webkit_javascript_result_get_js_value(js_result);
686 if (jsc_value_is_string(value))
688 str_value = jsc_value_to_string(value);
689 exception = jsc_context_get_exception(jsc_value_get_context(value));
690 if (exception != NULL)
692 fprintf(stderr, __NAME__": Error running javascript: %s\n",
693 jsc_exception_get_message(exception));
696 c->feed_html = str_value;
698 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
699 GTK_ENTRY_ICON_PRIMARY,
700 "application-rss+xml-symbolic");
701 gtk_entry_set_icon_activatable(GTK_ENTRY(c->location),
702 GTK_ENTRY_ICON_PRIMARY,
707 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
708 GTK_ENTRY_ICON_PRIMARY,
712 webkit_javascript_result_unref(js_result);
716 hover_web_view(WebKitWebView *web_view, WebKitHitTestResult *ht, guint modifiers,
719 struct Client *c = (struct Client *)data;
721 if (!gtk_widget_is_focus(c->location))
723 if (webkit_hit_test_result_context_is_link(ht))
725 gtk_entry_set_text(GTK_ENTRY(c->location),
726 webkit_hit_test_result_get_link_uri(ht));
728 if (c->hover_uri != NULL)
729 g_free(c->hover_uri);
730 c->hover_uri = g_strdup(webkit_hit_test_result_get_link_uri(ht));
734 gtk_entry_set_text(GTK_ENTRY(c->location),
735 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view)));
737 if (c->hover_uri != NULL)
738 g_free(c->hover_uri);
745 icon_location(GtkEntry *entry, GtkEntryIconPosition icon_pos, GdkEvent *event,
748 struct Client *c = (struct Client *)data;
750 gchar *data_template =
755 " <meta charset=\"UTF-8\">"
756 " <title>Feeds</title>"
759 " <p>Feeds found on this page:</p>"
766 if (c->feed_html != NULL)
768 /* What we're actually trying to do is show a simple HTML page
769 * that lists all the feeds on the current page. The function
770 * webkit_web_view_load_html() looks like the proper way to do
771 * that. Sad thing is, it doesn't create a history entry, but
772 * instead simply replaces the content of the current page. This
773 * is not what we want.
775 * RFC 2397 [0] defines the data URI scheme [1]. We abuse this
776 * mechanism to show my custom HTML snippet *and* create a
779 * [0]: https://tools.ietf.org/html/rfc2397
780 * [1]: https://en.wikipedia.org/wiki/Data_URI_scheme */
781 d = g_strdup_printf(data_template, c->feed_html);
782 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), d);
788 key_common(GtkWidget *widget, GdkEvent *event, gpointer data)
790 struct Client *c = (struct Client *)data;
791 WebKitWebContext *wc = webkit_web_view_get_context(WEBKIT_WEB_VIEW(c->web_view));
794 if (event->type == GDK_KEY_PRESS)
796 if (((GdkEventKey *)event)->state & GDK_MOD1_MASK)
798 switch (((GdkEventKey *)event)->keyval)
800 case GDK_KEY_q: /* close window (left hand) */
801 gtk_widget_destroy(c->win);
803 case GDK_KEY_w: /* home (left hand) */
804 f = ensure_uri_scheme(home_uri);
805 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
808 case GDK_KEY_e: /* new tab (left hand) */
809 f = ensure_uri_scheme(home_uri);
810 client_new(f, NULL, TRUE);
813 case GDK_KEY_r: /* reload (left hand) */
814 webkit_web_view_reload_bypass_cache(WEBKIT_WEB_VIEW(
817 case GDK_KEY_d: /* download manager (left hand) */
818 gtk_widget_show_all(dm.win);
820 case GDK_KEY_2: /* search forward (left hand) */
821 case GDK_KEY_n: /* search forward (maybe both hands) */
824 case GDK_KEY_3: /* search backward (left hand) */
827 case GDK_KEY_l: /* location (BOTH hands) */
828 gtk_widget_grab_focus(c->location);
830 case GDK_KEY_k: /* initiate search (BOTH hands) */
831 gtk_widget_grab_focus(c->location);
832 gtk_entry_set_text(GTK_ENTRY(c->location), ":/");
833 gtk_editable_set_position(GTK_EDITABLE(c->location), -1);
835 case GDK_KEY_c: /* reload trusted certs (left hand) */
836 trust_user_certs(wc);
838 case GDK_KEY_x: /* launch external handler (left hand) */
839 if (c->external_handler_uri != NULL)
840 g_free(c->external_handler_uri);
841 c->external_handler_uri = g_strdup(
842 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view)));
843 external_handler_run(NULL, NULL, c);
847 /* navigate backward (left hand) */
848 else if (((GdkEventKey *)event)->keyval == GDK_KEY_F2)
850 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
853 /* navigate forward (left hand) */
854 else if (((GdkEventKey *)event)->keyval == GDK_KEY_F3)
856 webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
865 key_downloadmanager(GtkWidget *widget, GdkEvent *event, gpointer data)
867 if (event->type == GDK_KEY_PRESS)
869 if (((GdkEventKey *)event)->state & GDK_MOD1_MASK)
871 switch (((GdkEventKey *)event)->keyval)
873 case GDK_KEY_d: /* close window (left hand) */
875 downloadmanager_delete(dm.win, NULL);
885 key_location(GtkWidget *widget, GdkEvent *event, gpointer data)
887 struct Client *c = (struct Client *)data;
891 if (key_common(widget, event, data))
894 if (event->type == GDK_KEY_PRESS)
896 switch (((GdkEventKey *)event)->keyval)
898 case GDK_KEY_KP_Enter:
900 gtk_widget_grab_focus(c->web_view);
901 t = gtk_entry_get_text(GTK_ENTRY(c->location));
902 if (t != NULL && t[0] == ':' && t[1] == '/')
904 if (search_text != NULL)
906 search_text = g_strdup(t + 2);
909 else if (!keywords_try_search(WEBKIT_WEB_VIEW(c->web_view), t))
911 f = ensure_uri_scheme(t);
912 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
917 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
918 gtk_entry_set_text(GTK_ENTRY(c->location),
919 (t == NULL ? __NAME__ : t));
928 key_web_view(GtkWidget *widget, GdkEvent *event, gpointer data)
930 struct Client *c = (struct Client *)data;
934 if (key_common(widget, event, data))
937 if (event->type == GDK_KEY_PRESS)
939 if (((GdkEventKey *)event)->keyval == GDK_KEY_Escape)
941 webkit_web_view_stop_loading(WEBKIT_WEB_VIEW(c->web_view));
942 gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), 0);
945 else if (event->type == GDK_BUTTON_RELEASE)
947 switch (((GdkEventButton *)event)->button)
950 if (c->hover_uri != NULL)
952 client_new(c->hover_uri, NULL, TRUE);
957 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
960 webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
964 else if (event->type == GDK_SCROLL)
966 if (((GdkEventScroll *)event)->state & GDK_MOD1_MASK ||
967 ((GdkEventScroll *)event)->state & GDK_CONTROL_MASK)
969 gdk_event_get_scroll_deltas(event, &dx, &dy);
970 z = webkit_web_view_get_zoom_level(WEBKIT_WEB_VIEW(c->web_view));
972 z = dx != 0 ? global_zoom : z;
973 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), z);
985 GIOChannel *channel = NULL;
986 gchar *path = NULL, *buf = NULL;
987 gchar **tokens = NULL;
989 keywords = g_hash_table_new(g_str_hash, g_str_equal);
991 path = g_build_filename(g_get_user_config_dir(), __NAME__, "keywordsearch",
993 channel = g_io_channel_new_file(path, "r", &err);
996 while (g_io_channel_read_line(channel, &buf, NULL, NULL, NULL)
997 == G_IO_STATUS_NORMAL)
1002 tokens = g_strsplit(buf, " ", 2);
1003 if (tokens[0] != NULL && tokens[1] != NULL)
1004 g_hash_table_insert(keywords, g_strdup(tokens[0]),
1005 g_strdup(tokens[1]));
1010 g_io_channel_shutdown(channel, FALSE, NULL);
1016 keywords_try_search(WebKitWebView *web_view, const gchar *t)
1018 gboolean ret = FALSE;
1019 gchar **tokens = NULL;
1020 gchar *val = NULL, *escaped = NULL, *uri = NULL;
1022 tokens = g_strsplit(t, " ", 2);
1023 if (tokens[0] != NULL && tokens[1] != NULL)
1025 val = g_hash_table_lookup(keywords, tokens[0]);
1028 escaped = g_uri_escape_string(tokens[1], NULL, TRUE);
1029 uri = g_strdup_printf((gchar *)val, escaped);
1030 webkit_web_view_load_uri(web_view, uri);
1042 menu_web_view(WebKitWebView *web_view, WebKitContextMenu *menu, GdkEvent *ev,
1043 WebKitHitTestResult *ht, gpointer data)
1045 struct Client *c = (struct Client *)data;
1046 GSimpleAction *action = NULL;
1047 WebKitContextMenuItem *mi = NULL;
1048 const gchar *uri = NULL;
1052 if (webkit_hit_test_result_context_is_link(ht))
1053 uri = webkit_hit_test_result_get_link_uri(ht);
1054 else if (webkit_hit_test_result_context_is_image(ht))
1055 uri = webkit_hit_test_result_get_image_uri(ht);
1056 else if (webkit_hit_test_result_context_is_media(ht))
1057 uri = webkit_hit_test_result_get_media_uri(ht);
1061 webkit_context_menu_append(menu, webkit_context_menu_item_new_separator());
1063 if (c->external_handler_uri != NULL)
1064 g_free(c->external_handler_uri);
1065 c->external_handler_uri = g_strdup(uri);
1066 action = g_simple_action_new("external_handler", NULL);
1067 g_signal_connect(G_OBJECT(action), "activate",
1068 G_CALLBACK(external_handler_run), data);
1069 mi = webkit_context_menu_item_new_from_gaction(G_ACTION(action),
1070 "Open with external handler",
1072 webkit_context_menu_append(menu, mi);
1073 g_object_unref(action);
1076 /* FALSE = Show the menu. (TRUE = Don't ever show it.) */
1081 quit_if_nothing_active(void)
1091 gtk_widget_show_all(dm.win);
1098 remote_msg(GIOChannel *channel, GIOCondition condition, gpointer data)
1102 g_io_channel_read_line(channel, &uri, NULL, NULL, NULL);
1106 client_new(uri, NULL, TRUE);
1113 run_user_scripts(WebKitWebView *web_view)
1115 gchar *base = NULL, *path = NULL, *contents = NULL;
1116 const gchar *entry = NULL;
1117 GDir *scriptdir = NULL;
1119 base = g_build_filename(g_get_user_config_dir(), __NAME__, "user-scripts", NULL);
1120 scriptdir = g_dir_open(base, 0, NULL);
1121 if (scriptdir != NULL)
1123 while ((entry = g_dir_read_name(scriptdir)) != NULL)
1125 path = g_build_filename(base, entry, NULL);
1126 if (g_str_has_suffix(path, ".js"))
1128 if (g_file_get_contents(path, &contents, NULL, NULL))
1130 webkit_web_view_run_javascript(web_view, contents, NULL, NULL, NULL);
1136 g_dir_close(scriptdir);
1143 search(gpointer data, gint direction)
1145 struct Client *c = (struct Client *)data;
1146 WebKitWebView *web_view = WEBKIT_WEB_VIEW(c->web_view);
1147 WebKitFindController *fc = webkit_web_view_get_find_controller(web_view);
1149 if (search_text == NULL)
1155 webkit_find_controller_search(fc, search_text,
1156 WEBKIT_FIND_OPTIONS_CASE_INSENSITIVE |
1157 WEBKIT_FIND_OPTIONS_WRAP_AROUND,
1161 webkit_find_controller_search_next(fc);
1164 webkit_find_controller_search_previous(fc);
1170 show_web_view(WebKitWebView *web_view, gpointer data)
1172 struct Client *c = (struct Client *)data;
1176 gtk_widget_grab_focus(c->web_view);
1177 gtk_widget_show_all(c->win);
1181 trust_user_certs(WebKitWebContext *wc)
1183 GTlsCertificate *cert;
1184 const gchar *basedir, *file, *absfile;
1187 basedir = g_build_filename(g_get_user_config_dir(), __NAME__, "certs", NULL);
1188 dir = g_dir_open(basedir, 0, NULL);
1191 file = g_dir_read_name(dir);
1192 while (file != NULL)
1194 absfile = g_build_filename(g_get_user_config_dir(), __NAME__, "certs",
1196 cert = g_tls_certificate_new_from_file(absfile, NULL);
1198 fprintf(stderr, __NAME__": Could not load trusted cert '%s'\n", file);
1200 webkit_web_context_allow_tls_certificate_for_host(wc, cert, file);
1201 file = g_dir_read_name(dir);
1209 main(int argc, char **argv)
1214 gtk_init(&argc, &argv);
1215 webkit_web_context_set_process_model(webkit_web_context_get_default(),
1216 WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
1218 grab_environment_configuration();
1220 while ((opt = getopt(argc, argv, "C")) != -1)
1225 cooperative_instances = FALSE;
1228 fprintf(stderr, "Usage: "__NAME__" [OPTION]... [URI]...\n");
1234 if (cooperative_instances)
1235 cooperation_setup();
1236 downloadmanager_setup();
1238 if (!cooperative_instances || cooperative_alone)
1240 c = g_build_filename(g_get_user_config_dir(), __NAME__, "web_extensions",
1242 webkit_web_context_set_web_extensions_directory(
1243 webkit_web_context_get_default(), c
1248 client_new(home_uri, NULL, TRUE);
1251 for (i = optind; i < argc; i++)
1252 client_new(argv[i], NULL, TRUE);
1255 if (!cooperative_instances || cooperative_alone)