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_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;
98 client_destroy(GtkWidget *widget, gpointer data)
100 struct Client *c = (struct Client *)data;
102 g_signal_handlers_disconnect_by_func(G_OBJECT(c->web_view),
103 changed_load_progress, c);
108 quit_if_nothing_active();
112 client_destroy_request(WebKitWebView *web_view, gpointer data)
114 struct Client *c = (struct Client *)data;
116 gtk_widget_destroy(c->win);
122 client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show)
125 WebKitWebContext *wc;
128 if (uri != NULL && cooperative_instances && !cooperative_alone)
130 f = ensure_uri_scheme(uri);
131 write(cooperative_pipe_fp, f, strlen(f));
132 write(cooperative_pipe_fp, "\n", 1);
137 c = calloc(1, sizeof(struct Client));
140 fprintf(stderr, __NAME__": fatal: calloc failed\n");
146 c->win = gtk_plug_new(embed);
147 if (!gtk_plug_get_embedded(GTK_PLUG(c->win)))
149 fprintf(stderr, __NAME__": Can't plug-in to XID %ld.\n", embed);
150 gtk_widget_destroy(c->win);
157 c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
159 gtk_window_set_default_size(GTK_WINDOW(c->win), 800, 600);
161 g_signal_connect(G_OBJECT(c->win), "destroy", G_CALLBACK(client_destroy), c);
162 gtk_window_set_title(GTK_WINDOW(c->win), __NAME__);
164 if (related_wv == NULL)
165 c->web_view = webkit_web_view_new();
167 c->web_view = webkit_web_view_new_with_related_view(related_wv);
168 wc = webkit_web_view_get_context(WEBKIT_WEB_VIEW(c->web_view));
170 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), global_zoom);
171 g_signal_connect(G_OBJECT(c->web_view), "notify::title",
172 G_CALLBACK(changed_title), c);
173 g_signal_connect(G_OBJECT(c->web_view), "notify::uri",
174 G_CALLBACK(changed_uri), c);
175 g_signal_connect(G_OBJECT(c->web_view), "notify::estimated-load-progress",
176 G_CALLBACK(changed_load_progress), c);
177 g_signal_connect(G_OBJECT(c->web_view), "create",
178 G_CALLBACK(client_new_request), NULL);
179 g_signal_connect(G_OBJECT(c->web_view), "context-menu",
180 G_CALLBACK(menu_web_view), c);
181 g_signal_connect(G_OBJECT(c->web_view), "close",
182 G_CALLBACK(client_destroy_request), c);
183 g_signal_connect(G_OBJECT(c->web_view), "decide-policy",
184 G_CALLBACK(decide_policy), NULL);
185 g_signal_connect(G_OBJECT(c->web_view), "key-press-event",
186 G_CALLBACK(key_web_view), c);
187 g_signal_connect(G_OBJECT(c->web_view), "button-release-event",
188 G_CALLBACK(key_web_view), c);
189 g_signal_connect(G_OBJECT(c->web_view), "scroll-event",
190 G_CALLBACK(key_web_view), c);
191 g_signal_connect(G_OBJECT(c->web_view), "mouse-target-changed",
192 G_CALLBACK(hover_web_view), c);
193 g_signal_connect(G_OBJECT(c->web_view), "web-process-crashed",
194 G_CALLBACK(crashed_web_view), c);
196 if (!initial_wc_setup_done)
198 if (accepted_language[0] != NULL)
199 webkit_web_context_set_preferred_languages(wc, accepted_language);
201 g_signal_connect(G_OBJECT(wc), "download-started",
202 G_CALLBACK(download_handle_start), NULL);
204 trust_user_certs(wc);
206 initial_wc_setup_done = TRUE;
209 if (user_agent != NULL)
210 g_object_set(G_OBJECT(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view))),
211 "user-agent", user_agent, NULL);
213 if (enable_console_to_stdout)
214 webkit_settings_set_enable_write_console_messages_to_stdout(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view)), TRUE);
217 webkit_settings_set_enable_webgl(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view)), TRUE);
219 c->location = gtk_entry_new();
220 g_signal_connect(G_OBJECT(c->location), "key-press-event",
221 G_CALLBACK(key_location), c);
222 g_signal_connect(G_OBJECT(c->location), "icon-release",
223 G_CALLBACK(icon_location), c);
224 /* XXX This is a workaround. Setting this to NULL (which is done in
225 * grab_feeds_finished() if no feed has been detected) adds a little
226 * padding left of the text. Not sure why. The point of this call
227 * right here is to have that padding right from the start. This
228 * avoids a graphical artifact. */
229 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
230 GTK_ENTRY_ICON_PRIMARY,
233 c->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
234 gtk_box_pack_start(GTK_BOX(c->vbox), c->location, FALSE, FALSE, 0);
235 gtk_box_pack_start(GTK_BOX(c->vbox), c->web_view, TRUE, TRUE, 0);
237 gtk_container_add(GTK_CONTAINER(c->win), c->vbox);
240 show_web_view(NULL, c);
242 g_signal_connect(G_OBJECT(c->web_view), "ready-to-show",
243 G_CALLBACK(show_web_view), c);
247 f = ensure_uri_scheme(uri);
248 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
254 return WEBKIT_WEB_VIEW(c->web_view);
258 client_new_request(WebKitWebView *web_view,
259 WebKitNavigationAction *navigation_action, gpointer data)
261 return client_new(NULL, web_view, FALSE);
265 cooperation_setup(void)
268 gchar *fifofilename, *fifopath;
270 fifofilename = g_strdup_printf("%s-%s", __NAME__".fifo", fifo_suffix);
271 fifopath = g_build_filename(g_get_user_runtime_dir(), fifofilename, NULL);
272 g_free(fifofilename);
274 if (!g_file_test(fifopath, G_FILE_TEST_EXISTS))
275 mkfifo(fifopath, 0600);
277 cooperative_pipe_fp = open(fifopath, O_WRONLY | O_NONBLOCK);
278 if (!cooperative_pipe_fp)
280 fprintf(stderr, __NAME__": Can't open FIFO at all.\n");
284 if (write(cooperative_pipe_fp, "", 0) == -1)
286 /* Could not do an empty write to the FIFO which means there's
287 * no one listening. */
288 close(cooperative_pipe_fp);
289 towatch = g_io_channel_new_file(fifopath, "r+", NULL);
290 g_io_add_watch(towatch, G_IO_IN, (GIOFunc)remote_msg, NULL);
293 cooperative_alone = FALSE;
300 changed_download_progress(GObject *obj, GParamSpec *pspec, gpointer data)
302 WebKitDownload *download = WEBKIT_DOWNLOAD(obj);
303 WebKitURIResponse *resp;
304 GtkToolItem *tb = GTK_TOOL_ITEM(data);
307 gchar *t, *filename, *base;
309 p = webkit_download_get_estimated_progress(download);
313 resp = webkit_download_get_response(download);
314 size_mb = webkit_uri_response_get_content_length(resp) / 1e6;
316 uri = webkit_download_get_destination(download);
317 filename = g_filename_from_uri(uri, NULL, NULL);
318 if (filename == NULL)
320 /* This really should not happen because WebKit uses that URI to
321 * write to a file... */
322 fprintf(stderr, __NAME__": Could not construct file name from URI!\n");
323 t = g_strdup_printf("%s (%.0f%% of %.1f MB)",
324 webkit_uri_response_get_uri(resp), p, size_mb);
328 base = g_path_get_basename(filename);
329 t = g_strdup_printf("%s (%.0f%% of %.1f MB)", base, p, size_mb);
333 gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), t);
338 changed_load_progress(GObject *obj, GParamSpec *pspec, gpointer data)
340 struct Client *c = (struct Client *)data;
343 "a = document.querySelectorAll('"
344 " html > head > link[rel=\"alternate\"][href][type=\"application/atom+xml\"],"
345 " html > head > link[rel=\"alternate\"][href][type=\"application/rss+xml\"]"
352 " for (i = 0; i < a.length; i++)"
354 " url = encodeURIComponent(a[i].href);"
355 " if ('title' in a[i] && a[i].title != '')"
356 " title = encodeURIComponent(a[i].title);"
359 " out += '<li><a href=\"' + url + '\">' + title + '</a></li>';"
364 p = webkit_web_view_get_estimated_load_progress(WEBKIT_WEB_VIEW(c->web_view));
369 /* The page has loaded fully. We now run the short JavaScript
370 * snippet above that operates on the DOM. It tries to grab all
371 * occurences of <link rel="alternate" ...>, i.e. RSS/Atom feed
373 webkit_web_view_run_javascript(WEBKIT_WEB_VIEW(c->web_view),
375 grab_feeds_finished, c);
377 run_user_scripts(WEBKIT_WEB_VIEW(c->web_view));
379 gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), p);
383 changed_title(GObject *obj, GParamSpec *pspec, gpointer data)
386 struct Client *c = (struct Client *)data;
388 u = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
389 t = webkit_web_view_get_title(WEBKIT_WEB_VIEW(c->web_view));
391 u = u == NULL ? __NAME__ : u;
392 u = u[0] == 0 ? __NAME__ : u;
394 t = t == NULL ? u : t;
395 t = t[0] == 0 ? u : t;
397 gtk_window_set_title(GTK_WINDOW(c->win), t);
401 changed_uri(GObject *obj, GParamSpec *pspec, gpointer data)
404 struct Client *c = (struct Client *)data;
407 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
409 /* When a web process crashes, we get a "notify::uri" signal, but we
410 * can no longer read a meaningful URI. It's just an empty string
411 * now. Not updating the location bar in this scenario is important,
412 * because we would override the "WEB PROCESS CRASHED" message. */
413 if (t != NULL && strlen(t) > 0)
415 gtk_entry_set_text(GTK_ENTRY(c->location), t);
417 if (history_file != NULL)
419 fp = fopen(history_file, "a");
422 fprintf(fp, "%s\n", t);
426 perror(__NAME__": Error opening history file");
432 crashed_web_view(WebKitWebView *web_view, gpointer data)
435 struct Client *c = (struct Client *)data;
437 t = g_strdup_printf("WEB PROCESS CRASHED: %s",
438 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(web_view)));
439 gtk_entry_set_text(GTK_ENTRY(c->location), t);
446 decide_policy(WebKitWebView *web_view, WebKitPolicyDecision *decision,
447 WebKitPolicyDecisionType type, gpointer data)
449 WebKitResponsePolicyDecision *r;
453 case WEBKIT_POLICY_DECISION_TYPE_RESPONSE:
454 r = WEBKIT_RESPONSE_POLICY_DECISION(decision);
455 if (!webkit_response_policy_decision_is_mime_type_supported(r))
456 webkit_policy_decision_download(decision);
458 webkit_policy_decision_use(decision);
461 /* Use whatever default there is. */
468 download_handle_finished(WebKitDownload *download, gpointer data)
474 download_handle_start(WebKitWebView *web_view, WebKitDownload *download,
477 g_signal_connect(G_OBJECT(download), "decide-destination",
478 G_CALLBACK(download_handle), data);
482 download_handle(WebKitDownload *download, gchar *suggested_filename, gpointer data)
484 gchar *sug_clean, *path, *path2 = NULL, *uri;
489 sug_clean = g_strdup(suggested_filename);
490 for (i = 0; i < strlen(sug_clean); i++)
491 if (sug_clean[i] == G_DIR_SEPARATOR)
494 path = g_build_filename(download_dir, sug_clean, NULL);
495 path2 = g_strdup(path);
496 while (g_file_test(path2, G_FILE_TEST_EXISTS) && suffix < 1000)
500 path2 = g_strdup_printf("%s.%d", path, suffix);
506 fprintf(stderr, __NAME__": Suffix reached limit for download.\n");
507 webkit_download_cancel(download);
511 uri = g_filename_to_uri(path2, NULL, NULL);
512 webkit_download_set_destination(download, uri);
515 tb = gtk_tool_button_new(NULL, NULL);
516 gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(tb), "gtk-delete");
517 gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), sug_clean);
518 gtk_toolbar_insert(GTK_TOOLBAR(dm.toolbar), tb, 0);
519 gtk_widget_show_all(dm.win);
521 g_signal_connect(G_OBJECT(download), "notify::estimated-progress",
522 G_CALLBACK(changed_download_progress), tb);
525 g_signal_connect(G_OBJECT(download), "finished",
526 G_CALLBACK(download_handle_finished), NULL);
528 g_object_ref(download);
529 g_signal_connect(G_OBJECT(tb), "clicked",
530 G_CALLBACK(downloadmanager_cancel), download);
537 /* Propagate -- to whom it may concern. */
542 downloadmanager_cancel(GtkToolButton *tb, gpointer data)
544 WebKitDownload *download = WEBKIT_DOWNLOAD(data);
546 webkit_download_cancel(download);
547 g_object_unref(download);
549 gtk_widget_destroy(GTK_WIDGET(tb));
553 downloadmanager_delete(GtkWidget *obj, gpointer data)
555 if (!quit_if_nothing_active())
556 gtk_widget_hide(dm.win);
562 downloadmanager_setup(void)
564 dm.win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
565 gtk_window_set_type_hint(GTK_WINDOW(dm.win), GDK_WINDOW_TYPE_HINT_DIALOG);
566 gtk_window_set_default_size(GTK_WINDOW(dm.win), 500, 250);
567 gtk_window_set_title(GTK_WINDOW(dm.win), __NAME__" - Download Manager");
568 g_signal_connect(G_OBJECT(dm.win), "delete-event",
569 G_CALLBACK(downloadmanager_delete), NULL);
570 g_signal_connect(G_OBJECT(dm.win), "key-press-event",
571 G_CALLBACK(key_downloadmanager), NULL);
573 dm.toolbar = gtk_toolbar_new();
574 gtk_orientable_set_orientation(GTK_ORIENTABLE(dm.toolbar),
575 GTK_ORIENTATION_VERTICAL);
576 gtk_toolbar_set_style(GTK_TOOLBAR(dm.toolbar), GTK_TOOLBAR_BOTH_HORIZ);
577 gtk_toolbar_set_show_arrow(GTK_TOOLBAR(dm.toolbar), FALSE);
579 dm.scroll = gtk_scrolled_window_new(NULL, NULL);
580 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(dm.scroll),
581 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
582 gtk_container_add(GTK_CONTAINER(dm.scroll), dm.toolbar);
584 gtk_container_add(GTK_CONTAINER(dm.win), dm.scroll);
588 ensure_uri_scheme(const gchar *t)
592 f = g_ascii_strdown(t, -1);
593 if (!g_str_has_prefix(f, "http:") &&
594 !g_str_has_prefix(f, "https:") &&
595 !g_str_has_prefix(f, "file:") &&
596 !g_str_has_prefix(f, "about:") &&
597 !g_str_has_prefix(f, "data:"))
600 fabs = realpath(t, NULL);
603 f = g_strdup_printf("file://%s", fabs);
607 f = g_strdup_printf("http://%s", t);
615 external_handler_run(GSimpleAction *simple, GVariant *param, gpointer data)
617 struct Client *c = (struct Client *)data;
618 gchar *argv[] = { "lariza-external-handler", "-u", NULL, NULL };
625 argv[2] = c->external_handler_uri;
626 if (!g_spawn_async(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL,
629 fprintf(stderr, __NAME__": Could not launch key handler: %s\n",
634 g_spawn_close_pid(pid);
638 grab_environment_configuration(void)
642 e = g_getenv(__NAME_UPPERCASE__"_ACCEPTED_LANGUAGE");
644 accepted_language[0] = g_strdup(e);
646 e = g_getenv(__NAME_UPPERCASE__"_DOWNLOAD_DIR");
648 download_dir = g_strdup(e);
650 e = g_getenv(__NAME_UPPERCASE__"_ENABLE_CONSOLE_TO_STDOUT");
652 enable_console_to_stdout = TRUE;
654 e = g_getenv(__NAME_UPPERCASE__"_ENABLE_EXPERIMENTAL_WEBGL");
658 e = g_getenv(__NAME_UPPERCASE__"_FIFO_SUFFIX");
660 fifo_suffix = g_strdup(e);
662 e = g_getenv(__NAME_UPPERCASE__"_HISTORY_FILE");
664 history_file = g_strdup(e);
666 e = g_getenv(__NAME_UPPERCASE__"_HOME_URI");
668 home_uri = g_strdup(e);
670 e = g_getenv(__NAME_UPPERCASE__"_USER_AGENT");
672 user_agent = g_strdup(e);
674 e = g_getenv(__NAME_UPPERCASE__"_ZOOM");
676 global_zoom = atof(e);
680 grab_feeds_finished(GObject *object, GAsyncResult *result, gpointer data)
682 struct Client *c = (struct Client *)data;
683 WebKitJavascriptResult *js_result;
685 JSCException *exception;
689 g_free(c->feed_html);
692 /* This was taken almost verbatim from the example in WebKit's
695 * https://webkitgtk.org/reference/webkit2gtk/stable/WebKitWebView.html#webkit-web-view-run-javascript-finish */
697 js_result = webkit_web_view_run_javascript_finish(WEBKIT_WEB_VIEW(object),
701 fprintf(stderr, __NAME__": Error running javascript: %s\n", err->message);
706 value = webkit_javascript_result_get_js_value(js_result);
707 if (jsc_value_is_string(value))
709 str_value = jsc_value_to_string(value);
710 exception = jsc_context_get_exception(jsc_value_get_context(value));
711 if (exception != NULL)
713 fprintf(stderr, __NAME__": Error running javascript: %s\n",
714 jsc_exception_get_message(exception));
717 c->feed_html = str_value;
719 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
720 GTK_ENTRY_ICON_PRIMARY,
721 "application-rss+xml-symbolic");
722 gtk_entry_set_icon_activatable(GTK_ENTRY(c->location),
723 GTK_ENTRY_ICON_PRIMARY,
728 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
729 GTK_ENTRY_ICON_PRIMARY,
733 webkit_javascript_result_unref(js_result);
737 hover_web_view(WebKitWebView *web_view, WebKitHitTestResult *ht, guint modifiers,
740 struct Client *c = (struct Client *)data;
742 if (!gtk_widget_is_focus(c->location))
744 if (webkit_hit_test_result_context_is_link(ht))
746 gtk_entry_set_text(GTK_ENTRY(c->location),
747 webkit_hit_test_result_get_link_uri(ht));
749 if (c->hover_uri != NULL)
750 g_free(c->hover_uri);
751 c->hover_uri = g_strdup(webkit_hit_test_result_get_link_uri(ht));
755 gtk_entry_set_text(GTK_ENTRY(c->location),
756 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view)));
758 if (c->hover_uri != NULL)
759 g_free(c->hover_uri);
766 icon_location(GtkEntry *entry, GtkEntryIconPosition icon_pos, GdkEvent *event,
769 struct Client *c = (struct Client *)data;
771 gchar *data_template =
776 " <meta charset=\"UTF-8\">"
777 " <title>Feeds</title>"
780 " <p>Feeds found on this page:</p>"
787 if (c->feed_html != NULL)
789 /* What we're actually trying to do is show a simple HTML page
790 * that lists all the feeds on the current page. The function
791 * webkit_web_view_load_html() looks like the proper way to do
792 * that. Sad thing is, it doesn't create a history entry, but
793 * instead simply replaces the content of the current page. This
794 * is not what we want.
796 * RFC 2397 [0] defines the data URI scheme [1]. We abuse this
797 * mechanism to show my custom HTML snippet *and* create a
800 * [0]: https://tools.ietf.org/html/rfc2397
801 * [1]: https://en.wikipedia.org/wiki/Data_URI_scheme */
802 d = g_strdup_printf(data_template, c->feed_html);
803 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), d);
809 key_common(GtkWidget *widget, GdkEvent *event, gpointer data)
811 struct Client *c = (struct Client *)data;
812 WebKitWebContext *wc = webkit_web_view_get_context(WEBKIT_WEB_VIEW(c->web_view));
815 if (event->type == GDK_KEY_PRESS)
817 if (((GdkEventKey *)event)->state & GDK_MOD1_MASK)
819 switch (((GdkEventKey *)event)->keyval)
821 case GDK_KEY_q: /* close window (left hand) */
822 gtk_widget_destroy(c->win);
824 case GDK_KEY_w: /* home (left hand) */
825 f = ensure_uri_scheme(home_uri);
826 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
829 case GDK_KEY_e: /* new tab (left hand) */
830 f = ensure_uri_scheme(home_uri);
831 client_new(f, NULL, TRUE);
834 case GDK_KEY_r: /* reload (left hand) */
835 webkit_web_view_reload_bypass_cache(WEBKIT_WEB_VIEW(
838 case GDK_KEY_d: /* download manager (left hand) */
839 gtk_widget_show_all(dm.win);
841 case GDK_KEY_2: /* search forward (left hand) */
842 case GDK_KEY_n: /* search forward (maybe both hands) */
845 case GDK_KEY_3: /* search backward (left hand) */
848 case GDK_KEY_l: /* location (BOTH hands) */
849 gtk_widget_grab_focus(c->location);
851 case GDK_KEY_k: /* initiate search (BOTH hands) */
852 gtk_widget_grab_focus(c->location);
853 gtk_entry_set_text(GTK_ENTRY(c->location), ":/");
854 gtk_editable_set_position(GTK_EDITABLE(c->location), -1);
856 case GDK_KEY_c: /* reload trusted certs (left hand) */
857 trust_user_certs(wc);
859 case GDK_KEY_x: /* launch external handler (left hand) */
860 if (c->external_handler_uri != NULL)
861 g_free(c->external_handler_uri);
862 c->external_handler_uri = g_strdup(
863 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view)));
864 external_handler_run(NULL, NULL, c);
868 /* navigate backward (left hand) */
869 else if (((GdkEventKey *)event)->keyval == GDK_KEY_F2)
871 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
874 /* navigate forward (left hand) */
875 else if (((GdkEventKey *)event)->keyval == GDK_KEY_F3)
877 webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
886 key_downloadmanager(GtkWidget *widget, GdkEvent *event, gpointer data)
888 if (event->type == GDK_KEY_PRESS)
890 if (((GdkEventKey *)event)->state & GDK_MOD1_MASK)
892 switch (((GdkEventKey *)event)->keyval)
894 case GDK_KEY_d: /* close window (left hand) */
896 downloadmanager_delete(dm.win, NULL);
906 key_location(GtkWidget *widget, GdkEvent *event, gpointer data)
908 struct Client *c = (struct Client *)data;
912 if (key_common(widget, event, data))
915 if (event->type == GDK_KEY_PRESS)
917 switch (((GdkEventKey *)event)->keyval)
919 case GDK_KEY_KP_Enter:
921 gtk_widget_grab_focus(c->web_view);
922 t = gtk_entry_get_text(GTK_ENTRY(c->location));
923 if (t != NULL && t[0] == ':' && t[1] == '/')
925 if (search_text != NULL)
927 search_text = g_strdup(t + 2);
930 else if (!keywords_try_search(WEBKIT_WEB_VIEW(c->web_view), t))
932 f = ensure_uri_scheme(t);
933 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
938 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
939 gtk_entry_set_text(GTK_ENTRY(c->location),
940 (t == NULL ? __NAME__ : t));
949 key_web_view(GtkWidget *widget, GdkEvent *event, gpointer data)
951 struct Client *c = (struct Client *)data;
955 if (key_common(widget, event, data))
958 if (event->type == GDK_KEY_PRESS)
960 if (((GdkEventKey *)event)->keyval == GDK_KEY_Escape)
962 webkit_web_view_stop_loading(WEBKIT_WEB_VIEW(c->web_view));
963 gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), 0);
966 else if (event->type == GDK_BUTTON_RELEASE)
968 switch (((GdkEventButton *)event)->button)
971 if (c->hover_uri != NULL)
973 client_new(c->hover_uri, NULL, TRUE);
978 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
981 webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
985 else if (event->type == GDK_SCROLL)
987 if (((GdkEventScroll *)event)->state & GDK_MOD1_MASK ||
988 ((GdkEventScroll *)event)->state & GDK_CONTROL_MASK)
990 gdk_event_get_scroll_deltas(event, &dx, &dy);
991 z = webkit_web_view_get_zoom_level(WEBKIT_WEB_VIEW(c->web_view));
993 z = dx != 0 ? global_zoom : z;
994 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), z);
1006 GIOChannel *channel = NULL;
1007 gchar *path = NULL, *buf = NULL;
1008 gchar **tokens = NULL;
1010 keywords = g_hash_table_new(g_str_hash, g_str_equal);
1012 path = g_build_filename(g_get_user_config_dir(), __NAME__, "keywordsearch",
1014 channel = g_io_channel_new_file(path, "r", &err);
1015 if (channel != NULL)
1017 while (g_io_channel_read_line(channel, &buf, NULL, NULL, NULL)
1018 == G_IO_STATUS_NORMAL)
1023 tokens = g_strsplit(buf, " ", 2);
1024 if (tokens[0] != NULL && tokens[1] != NULL)
1025 g_hash_table_insert(keywords, g_strdup(tokens[0]),
1026 g_strdup(tokens[1]));
1031 g_io_channel_shutdown(channel, FALSE, NULL);
1037 keywords_try_search(WebKitWebView *web_view, const gchar *t)
1039 gboolean ret = FALSE;
1040 gchar **tokens = NULL;
1041 gchar *val = NULL, *escaped = NULL, *uri = NULL;
1043 tokens = g_strsplit(t, " ", 2);
1044 if (tokens[0] != NULL && tokens[1] != NULL)
1046 val = g_hash_table_lookup(keywords, tokens[0]);
1049 escaped = g_uri_escape_string(tokens[1], NULL, TRUE);
1050 uri = g_strdup_printf((gchar *)val, escaped);
1051 webkit_web_view_load_uri(web_view, uri);
1063 menu_web_view(WebKitWebView *web_view, WebKitContextMenu *menu, GdkEvent *ev,
1064 WebKitHitTestResult *ht, gpointer data)
1066 struct Client *c = (struct Client *)data;
1067 GSimpleAction *action = NULL;
1068 WebKitContextMenuItem *mi = NULL;
1069 const gchar *uri = NULL;
1073 if (webkit_hit_test_result_context_is_link(ht))
1074 uri = webkit_hit_test_result_get_link_uri(ht);
1075 else if (webkit_hit_test_result_context_is_image(ht))
1076 uri = webkit_hit_test_result_get_image_uri(ht);
1077 else if (webkit_hit_test_result_context_is_media(ht))
1078 uri = webkit_hit_test_result_get_media_uri(ht);
1082 webkit_context_menu_append(menu, webkit_context_menu_item_new_separator());
1084 if (c->external_handler_uri != NULL)
1085 g_free(c->external_handler_uri);
1086 c->external_handler_uri = g_strdup(uri);
1087 action = g_simple_action_new("external_handler", NULL);
1088 g_signal_connect(G_OBJECT(action), "activate",
1089 G_CALLBACK(external_handler_run), data);
1090 mi = webkit_context_menu_item_new_from_gaction(G_ACTION(action),
1091 "Open with external handler",
1093 webkit_context_menu_append(menu, mi);
1094 g_object_unref(action);
1097 /* FALSE = Show the menu. (TRUE = Don't ever show it.) */
1102 quit_if_nothing_active(void)
1112 gtk_widget_show_all(dm.win);
1119 remote_msg(GIOChannel *channel, GIOCondition condition, gpointer data)
1123 g_io_channel_read_line(channel, &uri, NULL, NULL, NULL);
1127 client_new(uri, NULL, TRUE);
1134 run_user_scripts(WebKitWebView *web_view)
1136 gchar *base = NULL, *path = NULL, *contents = NULL;
1137 const gchar *entry = NULL;
1138 GDir *scriptdir = NULL;
1140 base = g_build_filename(g_get_user_config_dir(), __NAME__, "scripts", NULL);
1141 scriptdir = g_dir_open(base, 0, NULL);
1142 if (scriptdir != NULL)
1144 while ((entry = g_dir_read_name(scriptdir)) != NULL)
1146 path = g_build_filename(base, entry, NULL);
1147 if (g_str_has_suffix(path, ".js"))
1149 if (g_file_get_contents(path, &contents, NULL, NULL))
1151 webkit_web_view_run_javascript(web_view, contents, NULL, NULL, NULL);
1157 g_dir_close(scriptdir);
1164 search(gpointer data, gint direction)
1166 struct Client *c = (struct Client *)data;
1167 WebKitWebView *web_view = WEBKIT_WEB_VIEW(c->web_view);
1168 WebKitFindController *fc = webkit_web_view_get_find_controller(web_view);
1170 if (search_text == NULL)
1176 webkit_find_controller_search(fc, search_text,
1177 WEBKIT_FIND_OPTIONS_CASE_INSENSITIVE |
1178 WEBKIT_FIND_OPTIONS_WRAP_AROUND,
1182 webkit_find_controller_search_next(fc);
1185 webkit_find_controller_search_previous(fc);
1191 show_web_view(WebKitWebView *web_view, gpointer data)
1193 struct Client *c = (struct Client *)data;
1197 gtk_widget_grab_focus(c->web_view);
1198 gtk_widget_show_all(c->win);
1205 GIOChannel *tabbed_stdout_channel;
1207 gchar *output = NULL;
1208 char *argv[] = { "tabbed", "-c", "-d", "-p", "s1", "-n", __NAME__, NULL };
1211 if (!g_spawn_async_with_pipes(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL,
1212 NULL, NULL, NULL, &tabbed_stdout, NULL,
1215 fprintf(stderr, __NAME__": Could not launch tabbed: %s\n", err->message);
1220 tabbed_stdout_channel = g_io_channel_unix_new(tabbed_stdout);
1221 if (tabbed_stdout_channel == NULL)
1223 fprintf(stderr, __NAME__": Could open tabbed's stdout\n");
1226 g_io_channel_read_line(tabbed_stdout_channel, &output, NULL, NULL, NULL);
1227 g_io_channel_shutdown(tabbed_stdout_channel, FALSE, NULL);
1230 fprintf(stderr, __NAME__": Could not read XID from tabbed\n");
1234 plug_into = strtol(output, NULL, 16);
1237 fprintf(stderr, __NAME__": The XID from tabbed is 0\n");
1242 trust_user_certs(WebKitWebContext *wc)
1244 GTlsCertificate *cert;
1245 const gchar *basedir, *file, *absfile;
1248 basedir = g_build_filename(g_get_user_config_dir(), __NAME__, "certs", NULL);
1249 dir = g_dir_open(basedir, 0, NULL);
1252 file = g_dir_read_name(dir);
1253 while (file != NULL)
1255 absfile = g_build_filename(g_get_user_config_dir(), __NAME__, "certs",
1257 cert = g_tls_certificate_new_from_file(absfile, NULL);
1259 fprintf(stderr, __NAME__": Could not load trusted cert '%s'\n", file);
1261 webkit_web_context_allow_tls_certificate_for_host(wc, cert, file);
1262 file = g_dir_read_name(dir);
1270 main(int argc, char **argv)
1275 gtk_init(&argc, &argv);
1276 webkit_web_context_set_process_model(webkit_web_context_get_default(),
1277 WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
1279 grab_environment_configuration();
1281 while ((opt = getopt(argc, argv, "e:CT")) != -1)
1286 embed = atol(optarg);
1287 tabbed_automagic = FALSE;
1290 cooperative_instances = FALSE;
1293 tabbed_automagic = FALSE;
1296 fprintf(stderr, "Usage: "__NAME__" [OPTION]... [URI]...\n");
1302 if (cooperative_instances)
1303 cooperation_setup();
1304 downloadmanager_setup();
1306 if (tabbed_automagic && !(cooperative_instances && !cooperative_alone))
1307 embed = tabbed_launch();
1309 if (!cooperative_instances || cooperative_alone)
1311 c = g_build_filename(g_get_user_config_dir(), __NAME__, "web_extensions",
1313 webkit_web_context_set_web_extensions_directory(
1314 webkit_web_context_get_default(), c
1319 client_new(home_uri, NULL, TRUE);
1322 for (i = optind; i < argc; i++)
1323 client_new(argv[i], NULL, TRUE);
1326 if (!cooperative_instances || cooperative_alone)