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 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_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);
215 webkit_settings_set_enable_developer_extras(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view)), TRUE);
217 c->location = gtk_entry_new();
218 g_signal_connect(G_OBJECT(c->location), "key-press-event",
219 G_CALLBACK(key_location), c);
220 g_signal_connect(G_OBJECT(c->location), "icon-release",
221 G_CALLBACK(icon_location), c);
222 /* XXX This is a workaround. Setting this to NULL (which is done in
223 * grab_feeds_finished() if no feed has been detected) adds a little
224 * padding left of the text. Not sure why. The point of this call
225 * right here is to have that padding right from the start. This
226 * avoids a graphical artifact. */
227 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
228 GTK_ENTRY_ICON_PRIMARY,
231 c->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
232 gtk_box_pack_start(GTK_BOX(c->vbox), c->location, FALSE, FALSE, 0);
233 gtk_box_pack_start(GTK_BOX(c->vbox), c->web_view, TRUE, TRUE, 0);
235 gtk_container_add(GTK_CONTAINER(c->win), c->vbox);
238 show_web_view(NULL, c);
240 g_signal_connect(G_OBJECT(c->web_view), "ready-to-show",
241 G_CALLBACK(show_web_view), c);
245 f = ensure_uri_scheme(uri);
246 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
252 return WEBKIT_WEB_VIEW(c->web_view);
256 client_new_request(WebKitWebView *web_view,
257 WebKitNavigationAction *navigation_action, gpointer data)
259 return client_new(NULL, web_view, FALSE);
263 cooperation_setup(void)
266 gchar *fifofilename, *fifopath;
268 fifofilename = g_strdup_printf("%s-%s", __NAME__".fifo", fifo_suffix);
269 fifopath = g_build_filename(g_get_user_runtime_dir(), fifofilename, NULL);
270 g_free(fifofilename);
272 if (!g_file_test(fifopath, G_FILE_TEST_EXISTS))
273 mkfifo(fifopath, 0600);
275 cooperative_pipe_fp = open(fifopath, O_WRONLY | O_NONBLOCK);
276 if (!cooperative_pipe_fp)
278 fprintf(stderr, __NAME__": Can't open FIFO at all.\n");
282 if (write(cooperative_pipe_fp, "", 0) == -1)
284 /* Could not do an empty write to the FIFO which means there's
285 * no one listening. */
286 close(cooperative_pipe_fp);
287 towatch = g_io_channel_new_file(fifopath, "r+", NULL);
288 g_io_add_watch(towatch, G_IO_IN, (GIOFunc)remote_msg, NULL);
291 cooperative_alone = FALSE;
298 changed_download_progress(GObject *obj, GParamSpec *pspec, gpointer data)
300 WebKitDownload *download = WEBKIT_DOWNLOAD(obj);
301 WebKitURIResponse *resp;
302 GtkToolItem *tb = GTK_TOOL_ITEM(data);
305 gchar *t, *filename, *base;
307 p = webkit_download_get_estimated_progress(download);
311 resp = webkit_download_get_response(download);
312 size_mb = webkit_uri_response_get_content_length(resp) / 1e6;
314 uri = webkit_download_get_destination(download);
315 filename = g_filename_from_uri(uri, NULL, NULL);
316 if (filename == NULL)
318 /* This really should not happen because WebKit uses that URI to
319 * write to a file... */
320 fprintf(stderr, __NAME__": Could not construct file name from URI!\n");
321 t = g_strdup_printf("%s (%.0f%% of %.1f MB)",
322 webkit_uri_response_get_uri(resp), p, size_mb);
326 base = g_path_get_basename(filename);
327 t = g_strdup_printf("%s (%.0f%% of %.1f MB)", base, p, size_mb);
331 gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), t);
336 changed_load_progress(GObject *obj, GParamSpec *pspec, gpointer data)
338 struct Client *c = (struct Client *)data;
341 "a = document.querySelectorAll('"
342 " html > head > link[rel=\"alternate\"][href][type=\"application/atom+xml\"],"
343 " html > head > link[rel=\"alternate\"][href][type=\"application/rss+xml\"]"
350 " for (i = 0; i < a.length; i++)"
352 " url = encodeURIComponent(a[i].href);"
353 " if ('title' in a[i] && a[i].title != '')"
354 " title = encodeURIComponent(a[i].title);"
357 " out += '<li><a href=\"' + url + '\">' + title + '</a></li>';"
362 p = webkit_web_view_get_estimated_load_progress(WEBKIT_WEB_VIEW(c->web_view));
367 /* The page has loaded fully. We now run the short JavaScript
368 * snippet above that operates on the DOM. It tries to grab all
369 * occurences of <link rel="alternate" ...>, i.e. RSS/Atom feed
371 webkit_web_view_run_javascript(WEBKIT_WEB_VIEW(c->web_view),
373 grab_feeds_finished, c);
375 run_user_scripts(WEBKIT_WEB_VIEW(c->web_view));
377 gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), p);
381 changed_title(GObject *obj, GParamSpec *pspec, gpointer data)
384 struct Client *c = (struct Client *)data;
386 u = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
387 t = webkit_web_view_get_title(WEBKIT_WEB_VIEW(c->web_view));
389 u = u == NULL ? __NAME__ : u;
390 u = u[0] == 0 ? __NAME__ : u;
392 t = t == NULL ? u : t;
393 t = t[0] == 0 ? u : t;
395 gtk_window_set_title(GTK_WINDOW(c->win), t);
399 changed_uri(GObject *obj, GParamSpec *pspec, gpointer data)
402 struct Client *c = (struct Client *)data;
405 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
407 /* When a web process crashes, we get a "notify::uri" signal, but we
408 * can no longer read a meaningful URI. It's just an empty string
409 * now. Not updating the location bar in this scenario is important,
410 * because we would override the "WEB PROCESS CRASHED" message. */
411 if (t != NULL && strlen(t) > 0)
413 gtk_entry_set_text(GTK_ENTRY(c->location), t);
415 if (history_file != NULL)
417 fp = fopen(history_file, "a");
420 fprintf(fp, "%s\n", t);
424 perror(__NAME__": Error opening history file");
430 crashed_web_view(WebKitWebView *web_view, gpointer data)
433 struct Client *c = (struct Client *)data;
435 t = g_strdup_printf("WEB PROCESS CRASHED: %s",
436 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(web_view)));
437 gtk_entry_set_text(GTK_ENTRY(c->location), t);
444 decide_policy(WebKitWebView *web_view, WebKitPolicyDecision *decision,
445 WebKitPolicyDecisionType type, gpointer data)
447 WebKitResponsePolicyDecision *r;
451 case WEBKIT_POLICY_DECISION_TYPE_RESPONSE:
452 r = WEBKIT_RESPONSE_POLICY_DECISION(decision);
453 if (!webkit_response_policy_decision_is_mime_type_supported(r))
454 webkit_policy_decision_download(decision);
456 webkit_policy_decision_use(decision);
459 /* Use whatever default there is. */
466 download_handle_finished(WebKitDownload *download, gpointer data)
472 download_handle_start(WebKitWebView *web_view, WebKitDownload *download,
475 g_signal_connect(G_OBJECT(download), "decide-destination",
476 G_CALLBACK(download_handle), data);
480 download_handle(WebKitDownload *download, gchar *suggested_filename, gpointer data)
482 gchar *sug_clean, *path, *path2 = NULL, *uri;
487 sug_clean = g_strdup(suggested_filename);
488 for (i = 0; i < strlen(sug_clean); i++)
489 if (sug_clean[i] == G_DIR_SEPARATOR)
492 path = g_build_filename(download_dir, sug_clean, NULL);
493 path2 = g_strdup(path);
494 while (g_file_test(path2, G_FILE_TEST_EXISTS) && suffix < 1000)
498 path2 = g_strdup_printf("%s.%d", path, suffix);
504 fprintf(stderr, __NAME__": Suffix reached limit for download.\n");
505 webkit_download_cancel(download);
509 uri = g_filename_to_uri(path2, NULL, NULL);
510 webkit_download_set_destination(download, uri);
513 tb = gtk_tool_button_new(NULL, NULL);
514 gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(tb), "gtk-delete");
515 gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), sug_clean);
516 gtk_toolbar_insert(GTK_TOOLBAR(dm.toolbar), tb, 0);
517 gtk_widget_show_all(dm.win);
519 g_signal_connect(G_OBJECT(download), "notify::estimated-progress",
520 G_CALLBACK(changed_download_progress), tb);
523 g_signal_connect(G_OBJECT(download), "finished",
524 G_CALLBACK(download_handle_finished), NULL);
526 g_object_ref(download);
527 g_signal_connect(G_OBJECT(tb), "clicked",
528 G_CALLBACK(downloadmanager_cancel), download);
535 /* Propagate -- to whom it may concern. */
540 downloadmanager_cancel(GtkToolButton *tb, gpointer data)
542 WebKitDownload *download = WEBKIT_DOWNLOAD(data);
544 webkit_download_cancel(download);
545 g_object_unref(download);
547 gtk_widget_destroy(GTK_WIDGET(tb));
551 downloadmanager_delete(GtkWidget *obj, gpointer data)
553 if (!quit_if_nothing_active())
554 gtk_widget_hide(dm.win);
560 downloadmanager_setup(void)
562 dm.win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
563 gtk_window_set_type_hint(GTK_WINDOW(dm.win), GDK_WINDOW_TYPE_HINT_DIALOG);
564 gtk_window_set_default_size(GTK_WINDOW(dm.win), 500, 250);
565 gtk_window_set_title(GTK_WINDOW(dm.win), __NAME__" - Download Manager");
566 g_signal_connect(G_OBJECT(dm.win), "delete-event",
567 G_CALLBACK(downloadmanager_delete), NULL);
568 g_signal_connect(G_OBJECT(dm.win), "key-press-event",
569 G_CALLBACK(key_downloadmanager), NULL);
571 dm.toolbar = gtk_toolbar_new();
572 gtk_orientable_set_orientation(GTK_ORIENTABLE(dm.toolbar),
573 GTK_ORIENTATION_VERTICAL);
574 gtk_toolbar_set_style(GTK_TOOLBAR(dm.toolbar), GTK_TOOLBAR_BOTH_HORIZ);
575 gtk_toolbar_set_show_arrow(GTK_TOOLBAR(dm.toolbar), FALSE);
577 dm.scroll = gtk_scrolled_window_new(NULL, NULL);
578 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(dm.scroll),
579 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
580 gtk_container_add(GTK_CONTAINER(dm.scroll), dm.toolbar);
582 gtk_container_add(GTK_CONTAINER(dm.win), dm.scroll);
586 ensure_uri_scheme(const gchar *t)
590 f = g_ascii_strdown(t, -1);
591 if (!g_str_has_prefix(f, "http:") &&
592 !g_str_has_prefix(f, "https:") &&
593 !g_str_has_prefix(f, "file:") &&
594 !g_str_has_prefix(f, "about:") &&
595 !g_str_has_prefix(f, "data:"))
598 fabs = realpath(t, NULL);
601 f = g_strdup_printf("file://%s", fabs);
605 f = g_strdup_printf("http://%s", t);
613 external_handler_run(GSimpleAction *simple, GVariant *param, gpointer data)
615 struct Client *c = (struct Client *)data;
616 gchar *argv[] = { "lariza-external-handler", "-u", NULL, NULL };
623 argv[2] = c->external_handler_uri;
624 if (!g_spawn_async(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL,
627 fprintf(stderr, __NAME__": Could not launch key handler: %s\n",
632 g_spawn_close_pid(pid);
636 grab_environment_configuration(void)
640 e = g_getenv(__NAME_UPPERCASE__"_ACCEPTED_LANGUAGE");
642 accepted_language[0] = g_strdup(e);
644 e = g_getenv(__NAME_UPPERCASE__"_DOWNLOAD_DIR");
646 download_dir = g_strdup(e);
648 e = g_getenv(__NAME_UPPERCASE__"_ENABLE_CONSOLE_TO_STDOUT");
650 enable_console_to_stdout = TRUE;
652 e = g_getenv(__NAME_UPPERCASE__"_FIFO_SUFFIX");
654 fifo_suffix = g_strdup(e);
656 e = g_getenv(__NAME_UPPERCASE__"_HISTORY_FILE");
658 history_file = g_strdup(e);
660 e = g_getenv(__NAME_UPPERCASE__"_HOME_URI");
662 home_uri = g_strdup(e);
664 e = g_getenv(__NAME_UPPERCASE__"_USER_AGENT");
666 user_agent = g_strdup(e);
668 e = g_getenv(__NAME_UPPERCASE__"_ZOOM");
670 global_zoom = atof(e);
674 grab_feeds_finished(GObject *object, GAsyncResult *result, gpointer data)
676 struct Client *c = (struct Client *)data;
677 WebKitJavascriptResult *js_result;
679 JSCException *exception;
683 g_free(c->feed_html);
686 /* This was taken almost verbatim from the example in WebKit's
689 * https://webkitgtk.org/reference/webkit2gtk/stable/WebKitWebView.html#webkit-web-view-run-javascript-finish */
691 js_result = webkit_web_view_run_javascript_finish(WEBKIT_WEB_VIEW(object),
695 fprintf(stderr, __NAME__": Error running javascript: %s\n", err->message);
700 value = webkit_javascript_result_get_js_value(js_result);
701 if (jsc_value_is_string(value))
703 str_value = jsc_value_to_string(value);
704 exception = jsc_context_get_exception(jsc_value_get_context(value));
705 if (exception != NULL)
707 fprintf(stderr, __NAME__": Error running javascript: %s\n",
708 jsc_exception_get_message(exception));
711 c->feed_html = str_value;
713 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
714 GTK_ENTRY_ICON_PRIMARY,
715 "application-rss+xml-symbolic");
716 gtk_entry_set_icon_activatable(GTK_ENTRY(c->location),
717 GTK_ENTRY_ICON_PRIMARY,
722 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
723 GTK_ENTRY_ICON_PRIMARY,
727 webkit_javascript_result_unref(js_result);
731 hover_web_view(WebKitWebView *web_view, WebKitHitTestResult *ht, guint modifiers,
734 struct Client *c = (struct Client *)data;
736 if (!gtk_widget_is_focus(c->location))
738 if (webkit_hit_test_result_context_is_link(ht))
740 gtk_entry_set_text(GTK_ENTRY(c->location),
741 webkit_hit_test_result_get_link_uri(ht));
743 if (c->hover_uri != NULL)
744 g_free(c->hover_uri);
745 c->hover_uri = g_strdup(webkit_hit_test_result_get_link_uri(ht));
749 gtk_entry_set_text(GTK_ENTRY(c->location),
750 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view)));
752 if (c->hover_uri != NULL)
753 g_free(c->hover_uri);
760 icon_location(GtkEntry *entry, GtkEntryIconPosition icon_pos, GdkEvent *event,
763 struct Client *c = (struct Client *)data;
765 gchar *data_template =
770 " <meta charset=\"UTF-8\">"
771 " <title>Feeds</title>"
774 " <p>Feeds found on this page:</p>"
781 if (c->feed_html != NULL)
783 /* What we're actually trying to do is show a simple HTML page
784 * that lists all the feeds on the current page. The function
785 * webkit_web_view_load_html() looks like the proper way to do
786 * that. Sad thing is, it doesn't create a history entry, but
787 * instead simply replaces the content of the current page. This
788 * is not what we want.
790 * RFC 2397 [0] defines the data URI scheme [1]. We abuse this
791 * mechanism to show my custom HTML snippet *and* create a
794 * [0]: https://tools.ietf.org/html/rfc2397
795 * [1]: https://en.wikipedia.org/wiki/Data_URI_scheme */
796 d = g_strdup_printf(data_template, c->feed_html);
797 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), d);
803 key_common(GtkWidget *widget, GdkEvent *event, gpointer data)
805 struct Client *c = (struct Client *)data;
806 WebKitWebContext *wc = webkit_web_view_get_context(WEBKIT_WEB_VIEW(c->web_view));
809 if (event->type == GDK_KEY_PRESS)
811 if (((GdkEventKey *)event)->state & GDK_MOD1_MASK)
813 switch (((GdkEventKey *)event)->keyval)
815 case GDK_KEY_q: /* close window (left hand) */
816 gtk_widget_destroy(c->win);
818 case GDK_KEY_w: /* home (left hand) */
819 f = ensure_uri_scheme(home_uri);
820 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
823 case GDK_KEY_e: /* new tab (left hand) */
824 f = ensure_uri_scheme(home_uri);
825 client_new(f, NULL, TRUE);
828 case GDK_KEY_r: /* reload (left hand) */
829 webkit_web_view_reload_bypass_cache(WEBKIT_WEB_VIEW(
832 case GDK_KEY_d: /* download manager (left hand) */
833 gtk_widget_show_all(dm.win);
835 case GDK_KEY_2: /* search forward (left hand) */
836 case GDK_KEY_n: /* search forward (maybe both hands) */
839 case GDK_KEY_3: /* search backward (left hand) */
842 case GDK_KEY_l: /* location (BOTH hands) */
843 gtk_widget_grab_focus(c->location);
845 case GDK_KEY_k: /* initiate search (BOTH hands) */
846 gtk_widget_grab_focus(c->location);
847 gtk_entry_set_text(GTK_ENTRY(c->location), ":/");
848 gtk_editable_set_position(GTK_EDITABLE(c->location), -1);
850 case GDK_KEY_c: /* reload trusted certs (left hand) */
851 trust_user_certs(wc);
853 case GDK_KEY_x: /* launch external handler (left hand) */
854 if (c->external_handler_uri != NULL)
855 g_free(c->external_handler_uri);
856 c->external_handler_uri = g_strdup(
857 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view)));
858 external_handler_run(NULL, NULL, c);
862 /* navigate backward (left hand) */
863 else if (((GdkEventKey *)event)->keyval == GDK_KEY_F2)
865 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
868 /* navigate forward (left hand) */
869 else if (((GdkEventKey *)event)->keyval == GDK_KEY_F3)
871 webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
880 key_downloadmanager(GtkWidget *widget, GdkEvent *event, gpointer data)
882 if (event->type == GDK_KEY_PRESS)
884 if (((GdkEventKey *)event)->state & GDK_MOD1_MASK)
886 switch (((GdkEventKey *)event)->keyval)
888 case GDK_KEY_d: /* close window (left hand) */
890 downloadmanager_delete(dm.win, NULL);
900 key_location(GtkWidget *widget, GdkEvent *event, gpointer data)
902 struct Client *c = (struct Client *)data;
906 if (key_common(widget, event, data))
909 if (event->type == GDK_KEY_PRESS)
911 switch (((GdkEventKey *)event)->keyval)
913 case GDK_KEY_KP_Enter:
915 gtk_widget_grab_focus(c->web_view);
916 t = gtk_entry_get_text(GTK_ENTRY(c->location));
917 if (t != NULL && t[0] == ':' && t[1] == '/')
919 if (search_text != NULL)
921 search_text = g_strdup(t + 2);
924 else if (!keywords_try_search(WEBKIT_WEB_VIEW(c->web_view), t))
926 f = ensure_uri_scheme(t);
927 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
932 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
933 gtk_entry_set_text(GTK_ENTRY(c->location),
934 (t == NULL ? __NAME__ : t));
943 key_web_view(GtkWidget *widget, GdkEvent *event, gpointer data)
945 struct Client *c = (struct Client *)data;
949 if (key_common(widget, event, data))
952 if (event->type == GDK_KEY_PRESS)
954 if (((GdkEventKey *)event)->keyval == GDK_KEY_Escape)
956 webkit_web_view_stop_loading(WEBKIT_WEB_VIEW(c->web_view));
957 gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), 0);
960 else if (event->type == GDK_BUTTON_RELEASE)
962 switch (((GdkEventButton *)event)->button)
965 if (c->hover_uri != NULL)
967 client_new(c->hover_uri, NULL, TRUE);
972 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
975 webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
979 else if (event->type == GDK_SCROLL)
981 if (((GdkEventScroll *)event)->state & GDK_MOD1_MASK ||
982 ((GdkEventScroll *)event)->state & GDK_CONTROL_MASK)
984 gdk_event_get_scroll_deltas(event, &dx, &dy);
985 z = webkit_web_view_get_zoom_level(WEBKIT_WEB_VIEW(c->web_view));
987 z = dx != 0 ? global_zoom : z;
988 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), z);
1000 GIOChannel *channel = NULL;
1001 gchar *path = NULL, *buf = NULL;
1002 gchar **tokens = NULL;
1004 keywords = g_hash_table_new(g_str_hash, g_str_equal);
1006 path = g_build_filename(g_get_user_config_dir(), __NAME__, "keywordsearch",
1008 channel = g_io_channel_new_file(path, "r", &err);
1009 if (channel != NULL)
1011 while (g_io_channel_read_line(channel, &buf, NULL, NULL, NULL)
1012 == G_IO_STATUS_NORMAL)
1017 tokens = g_strsplit(buf, " ", 2);
1018 if (tokens[0] != NULL && tokens[1] != NULL)
1019 g_hash_table_insert(keywords, g_strdup(tokens[0]),
1020 g_strdup(tokens[1]));
1025 g_io_channel_shutdown(channel, FALSE, NULL);
1031 keywords_try_search(WebKitWebView *web_view, const gchar *t)
1033 gboolean ret = FALSE;
1034 gchar **tokens = NULL;
1035 gchar *val = NULL, *escaped = NULL, *uri = NULL;
1037 tokens = g_strsplit(t, " ", 2);
1038 if (tokens[0] != NULL && tokens[1] != NULL)
1040 val = g_hash_table_lookup(keywords, tokens[0]);
1043 escaped = g_uri_escape_string(tokens[1], NULL, TRUE);
1044 uri = g_strdup_printf((gchar *)val, escaped);
1045 webkit_web_view_load_uri(web_view, uri);
1057 menu_web_view(WebKitWebView *web_view, WebKitContextMenu *menu, GdkEvent *ev,
1058 WebKitHitTestResult *ht, gpointer data)
1060 struct Client *c = (struct Client *)data;
1061 GSimpleAction *action = NULL;
1062 WebKitContextMenuItem *mi = NULL;
1063 const gchar *uri = NULL;
1067 if (webkit_hit_test_result_context_is_link(ht))
1068 uri = webkit_hit_test_result_get_link_uri(ht);
1069 else if (webkit_hit_test_result_context_is_image(ht))
1070 uri = webkit_hit_test_result_get_image_uri(ht);
1071 else if (webkit_hit_test_result_context_is_media(ht))
1072 uri = webkit_hit_test_result_get_media_uri(ht);
1076 webkit_context_menu_append(menu, webkit_context_menu_item_new_separator());
1078 if (c->external_handler_uri != NULL)
1079 g_free(c->external_handler_uri);
1080 c->external_handler_uri = g_strdup(uri);
1081 action = g_simple_action_new("external_handler", NULL);
1082 g_signal_connect(G_OBJECT(action), "activate",
1083 G_CALLBACK(external_handler_run), data);
1084 mi = webkit_context_menu_item_new_from_gaction(G_ACTION(action),
1085 "Open with external handler",
1087 webkit_context_menu_append(menu, mi);
1088 g_object_unref(action);
1091 /* FALSE = Show the menu. (TRUE = Don't ever show it.) */
1096 quit_if_nothing_active(void)
1106 gtk_widget_show_all(dm.win);
1113 remote_msg(GIOChannel *channel, GIOCondition condition, gpointer data)
1117 g_io_channel_read_line(channel, &uri, NULL, NULL, NULL);
1121 client_new(uri, NULL, TRUE);
1128 run_user_scripts(WebKitWebView *web_view)
1130 gchar *base = NULL, *path = NULL, *contents = NULL;
1131 const gchar *entry = NULL;
1132 GDir *scriptdir = NULL;
1134 base = g_build_filename(g_get_user_config_dir(), __NAME__, "user-scripts", NULL);
1135 scriptdir = g_dir_open(base, 0, NULL);
1136 if (scriptdir != NULL)
1138 while ((entry = g_dir_read_name(scriptdir)) != NULL)
1140 path = g_build_filename(base, entry, NULL);
1141 if (g_str_has_suffix(path, ".js"))
1143 if (g_file_get_contents(path, &contents, NULL, NULL))
1145 webkit_web_view_run_javascript(web_view, contents, NULL, NULL, NULL);
1151 g_dir_close(scriptdir);
1158 search(gpointer data, gint direction)
1160 struct Client *c = (struct Client *)data;
1161 WebKitWebView *web_view = WEBKIT_WEB_VIEW(c->web_view);
1162 WebKitFindController *fc = webkit_web_view_get_find_controller(web_view);
1164 if (search_text == NULL)
1170 webkit_find_controller_search(fc, search_text,
1171 WEBKIT_FIND_OPTIONS_CASE_INSENSITIVE |
1172 WEBKIT_FIND_OPTIONS_WRAP_AROUND,
1176 webkit_find_controller_search_next(fc);
1179 webkit_find_controller_search_previous(fc);
1185 show_web_view(WebKitWebView *web_view, gpointer data)
1187 struct Client *c = (struct Client *)data;
1191 gtk_widget_grab_focus(c->web_view);
1192 gtk_widget_show_all(c->win);
1199 GIOChannel *tabbed_stdout_channel;
1201 gchar *output = NULL;
1202 char *argv[] = { "tabbed", "-c", "-d", "-p", "s1", "-n", __NAME__, NULL };
1205 if (!g_spawn_async_with_pipes(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL,
1206 NULL, NULL, NULL, &tabbed_stdout, NULL,
1209 fprintf(stderr, __NAME__": Could not launch tabbed: %s\n", err->message);
1214 tabbed_stdout_channel = g_io_channel_unix_new(tabbed_stdout);
1215 if (tabbed_stdout_channel == NULL)
1217 fprintf(stderr, __NAME__": Could open tabbed's stdout\n");
1220 g_io_channel_read_line(tabbed_stdout_channel, &output, NULL, NULL, NULL);
1221 g_io_channel_shutdown(tabbed_stdout_channel, FALSE, NULL);
1224 fprintf(stderr, __NAME__": Could not read XID from tabbed\n");
1228 plug_into = strtol(output, NULL, 16);
1231 fprintf(stderr, __NAME__": The XID from tabbed is 0\n");
1236 trust_user_certs(WebKitWebContext *wc)
1238 GTlsCertificate *cert;
1239 const gchar *basedir, *file, *absfile;
1242 basedir = g_build_filename(g_get_user_config_dir(), __NAME__, "certs", NULL);
1243 dir = g_dir_open(basedir, 0, NULL);
1246 file = g_dir_read_name(dir);
1247 while (file != NULL)
1249 absfile = g_build_filename(g_get_user_config_dir(), __NAME__, "certs",
1251 cert = g_tls_certificate_new_from_file(absfile, NULL);
1253 fprintf(stderr, __NAME__": Could not load trusted cert '%s'\n", file);
1255 webkit_web_context_allow_tls_certificate_for_host(wc, cert, file);
1256 file = g_dir_read_name(dir);
1264 main(int argc, char **argv)
1269 gtk_init(&argc, &argv);
1270 webkit_web_context_set_process_model(webkit_web_context_get_default(),
1271 WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
1273 grab_environment_configuration();
1275 while ((opt = getopt(argc, argv, "e:CT")) != -1)
1280 embed = atol(optarg);
1281 tabbed_automagic = FALSE;
1284 cooperative_instances = FALSE;
1287 tabbed_automagic = FALSE;
1290 fprintf(stderr, "Usage: "__NAME__" [OPTION]... [URI]...\n");
1296 if (cooperative_instances)
1297 cooperation_setup();
1298 downloadmanager_setup();
1300 if (tabbed_automagic && !(cooperative_instances && !cooperative_alone))
1301 embed = tabbed_launch();
1303 if (!cooperative_instances || cooperative_alone)
1305 c = g_build_filename(g_get_user_config_dir(), __NAME__, "web_extensions",
1307 webkit_web_context_set_web_extensions_directory(
1308 webkit_web_context_get_default(), c
1313 client_new(home_uri, NULL, TRUE);
1316 for (i = optind; i < argc; i++)
1317 client_new(argv[i], NULL, TRUE);
1320 if (!cooperative_instances || cooperative_alone)