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 webkit_settings_set_enable_developer_extras(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view)), TRUE);
221 c->location = gtk_entry_new();
222 g_signal_connect(G_OBJECT(c->location), "key-press-event",
223 G_CALLBACK(key_location), c);
224 g_signal_connect(G_OBJECT(c->location), "icon-release",
225 G_CALLBACK(icon_location), c);
226 /* XXX This is a workaround. Setting this to NULL (which is done in
227 * grab_feeds_finished() if no feed has been detected) adds a little
228 * padding left of the text. Not sure why. The point of this call
229 * right here is to have that padding right from the start. This
230 * avoids a graphical artifact. */
231 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
232 GTK_ENTRY_ICON_PRIMARY,
235 c->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
236 gtk_box_pack_start(GTK_BOX(c->vbox), c->location, FALSE, FALSE, 0);
237 gtk_box_pack_start(GTK_BOX(c->vbox), c->web_view, TRUE, TRUE, 0);
239 gtk_container_add(GTK_CONTAINER(c->win), c->vbox);
242 show_web_view(NULL, c);
244 g_signal_connect(G_OBJECT(c->web_view), "ready-to-show",
245 G_CALLBACK(show_web_view), c);
249 f = ensure_uri_scheme(uri);
250 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
256 return WEBKIT_WEB_VIEW(c->web_view);
260 client_new_request(WebKitWebView *web_view,
261 WebKitNavigationAction *navigation_action, gpointer data)
263 return client_new(NULL, web_view, FALSE);
267 cooperation_setup(void)
270 gchar *fifofilename, *fifopath;
272 fifofilename = g_strdup_printf("%s-%s", __NAME__".fifo", fifo_suffix);
273 fifopath = g_build_filename(g_get_user_runtime_dir(), fifofilename, NULL);
274 g_free(fifofilename);
276 if (!g_file_test(fifopath, G_FILE_TEST_EXISTS))
277 mkfifo(fifopath, 0600);
279 cooperative_pipe_fp = open(fifopath, O_WRONLY | O_NONBLOCK);
280 if (!cooperative_pipe_fp)
282 fprintf(stderr, __NAME__": Can't open FIFO at all.\n");
286 if (write(cooperative_pipe_fp, "", 0) == -1)
288 /* Could not do an empty write to the FIFO which means there's
289 * no one listening. */
290 close(cooperative_pipe_fp);
291 towatch = g_io_channel_new_file(fifopath, "r+", NULL);
292 g_io_add_watch(towatch, G_IO_IN, (GIOFunc)remote_msg, NULL);
295 cooperative_alone = FALSE;
302 changed_download_progress(GObject *obj, GParamSpec *pspec, gpointer data)
304 WebKitDownload *download = WEBKIT_DOWNLOAD(obj);
305 WebKitURIResponse *resp;
306 GtkToolItem *tb = GTK_TOOL_ITEM(data);
309 gchar *t, *filename, *base;
311 p = webkit_download_get_estimated_progress(download);
315 resp = webkit_download_get_response(download);
316 size_mb = webkit_uri_response_get_content_length(resp) / 1e6;
318 uri = webkit_download_get_destination(download);
319 filename = g_filename_from_uri(uri, NULL, NULL);
320 if (filename == NULL)
322 /* This really should not happen because WebKit uses that URI to
323 * write to a file... */
324 fprintf(stderr, __NAME__": Could not construct file name from URI!\n");
325 t = g_strdup_printf("%s (%.0f%% of %.1f MB)",
326 webkit_uri_response_get_uri(resp), p, size_mb);
330 base = g_path_get_basename(filename);
331 t = g_strdup_printf("%s (%.0f%% of %.1f MB)", base, p, size_mb);
335 gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), t);
340 changed_load_progress(GObject *obj, GParamSpec *pspec, gpointer data)
342 struct Client *c = (struct Client *)data;
345 "a = document.querySelectorAll('"
346 " html > head > link[rel=\"alternate\"][href][type=\"application/atom+xml\"],"
347 " html > head > link[rel=\"alternate\"][href][type=\"application/rss+xml\"]"
354 " for (i = 0; i < a.length; i++)"
356 " url = encodeURIComponent(a[i].href);"
357 " if ('title' in a[i] && a[i].title != '')"
358 " title = encodeURIComponent(a[i].title);"
361 " out += '<li><a href=\"' + url + '\">' + title + '</a></li>';"
366 p = webkit_web_view_get_estimated_load_progress(WEBKIT_WEB_VIEW(c->web_view));
371 /* The page has loaded fully. We now run the short JavaScript
372 * snippet above that operates on the DOM. It tries to grab all
373 * occurences of <link rel="alternate" ...>, i.e. RSS/Atom feed
375 webkit_web_view_run_javascript(WEBKIT_WEB_VIEW(c->web_view),
377 grab_feeds_finished, c);
379 run_user_scripts(WEBKIT_WEB_VIEW(c->web_view));
381 gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), p);
385 changed_title(GObject *obj, GParamSpec *pspec, gpointer data)
388 struct Client *c = (struct Client *)data;
390 u = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
391 t = webkit_web_view_get_title(WEBKIT_WEB_VIEW(c->web_view));
393 u = u == NULL ? __NAME__ : u;
394 u = u[0] == 0 ? __NAME__ : u;
396 t = t == NULL ? u : t;
397 t = t[0] == 0 ? u : t;
399 gtk_window_set_title(GTK_WINDOW(c->win), t);
403 changed_uri(GObject *obj, GParamSpec *pspec, gpointer data)
406 struct Client *c = (struct Client *)data;
409 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
411 /* When a web process crashes, we get a "notify::uri" signal, but we
412 * can no longer read a meaningful URI. It's just an empty string
413 * now. Not updating the location bar in this scenario is important,
414 * because we would override the "WEB PROCESS CRASHED" message. */
415 if (t != NULL && strlen(t) > 0)
417 gtk_entry_set_text(GTK_ENTRY(c->location), t);
419 if (history_file != NULL)
421 fp = fopen(history_file, "a");
424 fprintf(fp, "%s\n", t);
428 perror(__NAME__": Error opening history file");
434 crashed_web_view(WebKitWebView *web_view, gpointer data)
437 struct Client *c = (struct Client *)data;
439 t = g_strdup_printf("WEB PROCESS CRASHED: %s",
440 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(web_view)));
441 gtk_entry_set_text(GTK_ENTRY(c->location), t);
448 decide_policy(WebKitWebView *web_view, WebKitPolicyDecision *decision,
449 WebKitPolicyDecisionType type, gpointer data)
451 WebKitResponsePolicyDecision *r;
455 case WEBKIT_POLICY_DECISION_TYPE_RESPONSE:
456 r = WEBKIT_RESPONSE_POLICY_DECISION(decision);
457 if (!webkit_response_policy_decision_is_mime_type_supported(r))
458 webkit_policy_decision_download(decision);
460 webkit_policy_decision_use(decision);
463 /* Use whatever default there is. */
470 download_handle_finished(WebKitDownload *download, gpointer data)
476 download_handle_start(WebKitWebView *web_view, WebKitDownload *download,
479 g_signal_connect(G_OBJECT(download), "decide-destination",
480 G_CALLBACK(download_handle), data);
484 download_handle(WebKitDownload *download, gchar *suggested_filename, gpointer data)
486 gchar *sug_clean, *path, *path2 = NULL, *uri;
491 sug_clean = g_strdup(suggested_filename);
492 for (i = 0; i < strlen(sug_clean); i++)
493 if (sug_clean[i] == G_DIR_SEPARATOR)
496 path = g_build_filename(download_dir, sug_clean, NULL);
497 path2 = g_strdup(path);
498 while (g_file_test(path2, G_FILE_TEST_EXISTS) && suffix < 1000)
502 path2 = g_strdup_printf("%s.%d", path, suffix);
508 fprintf(stderr, __NAME__": Suffix reached limit for download.\n");
509 webkit_download_cancel(download);
513 uri = g_filename_to_uri(path2, NULL, NULL);
514 webkit_download_set_destination(download, uri);
517 tb = gtk_tool_button_new(NULL, NULL);
518 gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(tb), "gtk-delete");
519 gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), sug_clean);
520 gtk_toolbar_insert(GTK_TOOLBAR(dm.toolbar), tb, 0);
521 gtk_widget_show_all(dm.win);
523 g_signal_connect(G_OBJECT(download), "notify::estimated-progress",
524 G_CALLBACK(changed_download_progress), tb);
527 g_signal_connect(G_OBJECT(download), "finished",
528 G_CALLBACK(download_handle_finished), NULL);
530 g_object_ref(download);
531 g_signal_connect(G_OBJECT(tb), "clicked",
532 G_CALLBACK(downloadmanager_cancel), download);
539 /* Propagate -- to whom it may concern. */
544 downloadmanager_cancel(GtkToolButton *tb, gpointer data)
546 WebKitDownload *download = WEBKIT_DOWNLOAD(data);
548 webkit_download_cancel(download);
549 g_object_unref(download);
551 gtk_widget_destroy(GTK_WIDGET(tb));
555 downloadmanager_delete(GtkWidget *obj, gpointer data)
557 if (!quit_if_nothing_active())
558 gtk_widget_hide(dm.win);
564 downloadmanager_setup(void)
566 dm.win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
567 gtk_window_set_type_hint(GTK_WINDOW(dm.win), GDK_WINDOW_TYPE_HINT_DIALOG);
568 gtk_window_set_default_size(GTK_WINDOW(dm.win), 500, 250);
569 gtk_window_set_title(GTK_WINDOW(dm.win), __NAME__" - Download Manager");
570 g_signal_connect(G_OBJECT(dm.win), "delete-event",
571 G_CALLBACK(downloadmanager_delete), NULL);
572 g_signal_connect(G_OBJECT(dm.win), "key-press-event",
573 G_CALLBACK(key_downloadmanager), NULL);
575 dm.toolbar = gtk_toolbar_new();
576 gtk_orientable_set_orientation(GTK_ORIENTABLE(dm.toolbar),
577 GTK_ORIENTATION_VERTICAL);
578 gtk_toolbar_set_style(GTK_TOOLBAR(dm.toolbar), GTK_TOOLBAR_BOTH_HORIZ);
579 gtk_toolbar_set_show_arrow(GTK_TOOLBAR(dm.toolbar), FALSE);
581 dm.scroll = gtk_scrolled_window_new(NULL, NULL);
582 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(dm.scroll),
583 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
584 gtk_container_add(GTK_CONTAINER(dm.scroll), dm.toolbar);
586 gtk_container_add(GTK_CONTAINER(dm.win), dm.scroll);
590 ensure_uri_scheme(const gchar *t)
594 f = g_ascii_strdown(t, -1);
595 if (!g_str_has_prefix(f, "http:") &&
596 !g_str_has_prefix(f, "https:") &&
597 !g_str_has_prefix(f, "file:") &&
598 !g_str_has_prefix(f, "about:") &&
599 !g_str_has_prefix(f, "data:"))
602 fabs = realpath(t, NULL);
605 f = g_strdup_printf("file://%s", fabs);
609 f = g_strdup_printf("http://%s", t);
617 external_handler_run(GSimpleAction *simple, GVariant *param, gpointer data)
619 struct Client *c = (struct Client *)data;
620 gchar *argv[] = { "lariza-external-handler", "-u", NULL, NULL };
627 argv[2] = c->external_handler_uri;
628 if (!g_spawn_async(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL,
631 fprintf(stderr, __NAME__": Could not launch key handler: %s\n",
636 g_spawn_close_pid(pid);
640 grab_environment_configuration(void)
644 e = g_getenv(__NAME_UPPERCASE__"_ACCEPTED_LANGUAGE");
646 accepted_language[0] = g_strdup(e);
648 e = g_getenv(__NAME_UPPERCASE__"_DOWNLOAD_DIR");
650 download_dir = g_strdup(e);
652 e = g_getenv(__NAME_UPPERCASE__"_ENABLE_CONSOLE_TO_STDOUT");
654 enable_console_to_stdout = TRUE;
656 e = g_getenv(__NAME_UPPERCASE__"_ENABLE_EXPERIMENTAL_WEBGL");
660 e = g_getenv(__NAME_UPPERCASE__"_FIFO_SUFFIX");
662 fifo_suffix = g_strdup(e);
664 e = g_getenv(__NAME_UPPERCASE__"_HISTORY_FILE");
666 history_file = g_strdup(e);
668 e = g_getenv(__NAME_UPPERCASE__"_HOME_URI");
670 home_uri = g_strdup(e);
672 e = g_getenv(__NAME_UPPERCASE__"_USER_AGENT");
674 user_agent = g_strdup(e);
676 e = g_getenv(__NAME_UPPERCASE__"_ZOOM");
678 global_zoom = atof(e);
682 grab_feeds_finished(GObject *object, GAsyncResult *result, gpointer data)
684 struct Client *c = (struct Client *)data;
685 WebKitJavascriptResult *js_result;
687 JSCException *exception;
691 g_free(c->feed_html);
694 /* This was taken almost verbatim from the example in WebKit's
697 * https://webkitgtk.org/reference/webkit2gtk/stable/WebKitWebView.html#webkit-web-view-run-javascript-finish */
699 js_result = webkit_web_view_run_javascript_finish(WEBKIT_WEB_VIEW(object),
703 fprintf(stderr, __NAME__": Error running javascript: %s\n", err->message);
708 value = webkit_javascript_result_get_js_value(js_result);
709 if (jsc_value_is_string(value))
711 str_value = jsc_value_to_string(value);
712 exception = jsc_context_get_exception(jsc_value_get_context(value));
713 if (exception != NULL)
715 fprintf(stderr, __NAME__": Error running javascript: %s\n",
716 jsc_exception_get_message(exception));
719 c->feed_html = str_value;
721 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
722 GTK_ENTRY_ICON_PRIMARY,
723 "application-rss+xml-symbolic");
724 gtk_entry_set_icon_activatable(GTK_ENTRY(c->location),
725 GTK_ENTRY_ICON_PRIMARY,
730 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
731 GTK_ENTRY_ICON_PRIMARY,
735 webkit_javascript_result_unref(js_result);
739 hover_web_view(WebKitWebView *web_view, WebKitHitTestResult *ht, guint modifiers,
742 struct Client *c = (struct Client *)data;
744 if (!gtk_widget_is_focus(c->location))
746 if (webkit_hit_test_result_context_is_link(ht))
748 gtk_entry_set_text(GTK_ENTRY(c->location),
749 webkit_hit_test_result_get_link_uri(ht));
751 if (c->hover_uri != NULL)
752 g_free(c->hover_uri);
753 c->hover_uri = g_strdup(webkit_hit_test_result_get_link_uri(ht));
757 gtk_entry_set_text(GTK_ENTRY(c->location),
758 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view)));
760 if (c->hover_uri != NULL)
761 g_free(c->hover_uri);
768 icon_location(GtkEntry *entry, GtkEntryIconPosition icon_pos, GdkEvent *event,
771 struct Client *c = (struct Client *)data;
773 gchar *data_template =
778 " <meta charset=\"UTF-8\">"
779 " <title>Feeds</title>"
782 " <p>Feeds found on this page:</p>"
789 if (c->feed_html != NULL)
791 /* What we're actually trying to do is show a simple HTML page
792 * that lists all the feeds on the current page. The function
793 * webkit_web_view_load_html() looks like the proper way to do
794 * that. Sad thing is, it doesn't create a history entry, but
795 * instead simply replaces the content of the current page. This
796 * is not what we want.
798 * RFC 2397 [0] defines the data URI scheme [1]. We abuse this
799 * mechanism to show my custom HTML snippet *and* create a
802 * [0]: https://tools.ietf.org/html/rfc2397
803 * [1]: https://en.wikipedia.org/wiki/Data_URI_scheme */
804 d = g_strdup_printf(data_template, c->feed_html);
805 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), d);
811 key_common(GtkWidget *widget, GdkEvent *event, gpointer data)
813 struct Client *c = (struct Client *)data;
814 WebKitWebContext *wc = webkit_web_view_get_context(WEBKIT_WEB_VIEW(c->web_view));
817 if (event->type == GDK_KEY_PRESS)
819 if (((GdkEventKey *)event)->state & GDK_MOD1_MASK)
821 switch (((GdkEventKey *)event)->keyval)
823 case GDK_KEY_q: /* close window (left hand) */
824 gtk_widget_destroy(c->win);
826 case GDK_KEY_w: /* home (left hand) */
827 f = ensure_uri_scheme(home_uri);
828 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
831 case GDK_KEY_e: /* new tab (left hand) */
832 f = ensure_uri_scheme(home_uri);
833 client_new(f, NULL, TRUE);
836 case GDK_KEY_r: /* reload (left hand) */
837 webkit_web_view_reload_bypass_cache(WEBKIT_WEB_VIEW(
840 case GDK_KEY_d: /* download manager (left hand) */
841 gtk_widget_show_all(dm.win);
843 case GDK_KEY_2: /* search forward (left hand) */
844 case GDK_KEY_n: /* search forward (maybe both hands) */
847 case GDK_KEY_3: /* search backward (left hand) */
850 case GDK_KEY_l: /* location (BOTH hands) */
851 gtk_widget_grab_focus(c->location);
853 case GDK_KEY_k: /* initiate search (BOTH hands) */
854 gtk_widget_grab_focus(c->location);
855 gtk_entry_set_text(GTK_ENTRY(c->location), ":/");
856 gtk_editable_set_position(GTK_EDITABLE(c->location), -1);
858 case GDK_KEY_c: /* reload trusted certs (left hand) */
859 trust_user_certs(wc);
861 case GDK_KEY_x: /* launch external handler (left hand) */
862 if (c->external_handler_uri != NULL)
863 g_free(c->external_handler_uri);
864 c->external_handler_uri = g_strdup(
865 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view)));
866 external_handler_run(NULL, NULL, c);
870 /* navigate backward (left hand) */
871 else if (((GdkEventKey *)event)->keyval == GDK_KEY_F2)
873 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
876 /* navigate forward (left hand) */
877 else if (((GdkEventKey *)event)->keyval == GDK_KEY_F3)
879 webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
888 key_downloadmanager(GtkWidget *widget, GdkEvent *event, gpointer data)
890 if (event->type == GDK_KEY_PRESS)
892 if (((GdkEventKey *)event)->state & GDK_MOD1_MASK)
894 switch (((GdkEventKey *)event)->keyval)
896 case GDK_KEY_d: /* close window (left hand) */
898 downloadmanager_delete(dm.win, NULL);
908 key_location(GtkWidget *widget, GdkEvent *event, gpointer data)
910 struct Client *c = (struct Client *)data;
914 if (key_common(widget, event, data))
917 if (event->type == GDK_KEY_PRESS)
919 switch (((GdkEventKey *)event)->keyval)
921 case GDK_KEY_KP_Enter:
923 gtk_widget_grab_focus(c->web_view);
924 t = gtk_entry_get_text(GTK_ENTRY(c->location));
925 if (t != NULL && t[0] == ':' && t[1] == '/')
927 if (search_text != NULL)
929 search_text = g_strdup(t + 2);
932 else if (!keywords_try_search(WEBKIT_WEB_VIEW(c->web_view), t))
934 f = ensure_uri_scheme(t);
935 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
940 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
941 gtk_entry_set_text(GTK_ENTRY(c->location),
942 (t == NULL ? __NAME__ : t));
951 key_web_view(GtkWidget *widget, GdkEvent *event, gpointer data)
953 struct Client *c = (struct Client *)data;
957 if (key_common(widget, event, data))
960 if (event->type == GDK_KEY_PRESS)
962 if (((GdkEventKey *)event)->keyval == GDK_KEY_Escape)
964 webkit_web_view_stop_loading(WEBKIT_WEB_VIEW(c->web_view));
965 gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), 0);
968 else if (event->type == GDK_BUTTON_RELEASE)
970 switch (((GdkEventButton *)event)->button)
973 if (c->hover_uri != NULL)
975 client_new(c->hover_uri, NULL, TRUE);
980 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
983 webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
987 else if (event->type == GDK_SCROLL)
989 if (((GdkEventScroll *)event)->state & GDK_MOD1_MASK ||
990 ((GdkEventScroll *)event)->state & GDK_CONTROL_MASK)
992 gdk_event_get_scroll_deltas(event, &dx, &dy);
993 z = webkit_web_view_get_zoom_level(WEBKIT_WEB_VIEW(c->web_view));
995 z = dx != 0 ? global_zoom : z;
996 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), z);
1008 GIOChannel *channel = NULL;
1009 gchar *path = NULL, *buf = NULL;
1010 gchar **tokens = NULL;
1012 keywords = g_hash_table_new(g_str_hash, g_str_equal);
1014 path = g_build_filename(g_get_user_config_dir(), __NAME__, "keywordsearch",
1016 channel = g_io_channel_new_file(path, "r", &err);
1017 if (channel != NULL)
1019 while (g_io_channel_read_line(channel, &buf, NULL, NULL, NULL)
1020 == G_IO_STATUS_NORMAL)
1025 tokens = g_strsplit(buf, " ", 2);
1026 if (tokens[0] != NULL && tokens[1] != NULL)
1027 g_hash_table_insert(keywords, g_strdup(tokens[0]),
1028 g_strdup(tokens[1]));
1033 g_io_channel_shutdown(channel, FALSE, NULL);
1039 keywords_try_search(WebKitWebView *web_view, const gchar *t)
1041 gboolean ret = FALSE;
1042 gchar **tokens = NULL;
1043 gchar *val = NULL, *escaped = NULL, *uri = NULL;
1045 tokens = g_strsplit(t, " ", 2);
1046 if (tokens[0] != NULL && tokens[1] != NULL)
1048 val = g_hash_table_lookup(keywords, tokens[0]);
1051 escaped = g_uri_escape_string(tokens[1], NULL, TRUE);
1052 uri = g_strdup_printf((gchar *)val, escaped);
1053 webkit_web_view_load_uri(web_view, uri);
1065 menu_web_view(WebKitWebView *web_view, WebKitContextMenu *menu, GdkEvent *ev,
1066 WebKitHitTestResult *ht, gpointer data)
1068 struct Client *c = (struct Client *)data;
1069 GSimpleAction *action = NULL;
1070 WebKitContextMenuItem *mi = NULL;
1071 const gchar *uri = NULL;
1075 if (webkit_hit_test_result_context_is_link(ht))
1076 uri = webkit_hit_test_result_get_link_uri(ht);
1077 else if (webkit_hit_test_result_context_is_image(ht))
1078 uri = webkit_hit_test_result_get_image_uri(ht);
1079 else if (webkit_hit_test_result_context_is_media(ht))
1080 uri = webkit_hit_test_result_get_media_uri(ht);
1084 webkit_context_menu_append(menu, webkit_context_menu_item_new_separator());
1086 if (c->external_handler_uri != NULL)
1087 g_free(c->external_handler_uri);
1088 c->external_handler_uri = g_strdup(uri);
1089 action = g_simple_action_new("external_handler", NULL);
1090 g_signal_connect(G_OBJECT(action), "activate",
1091 G_CALLBACK(external_handler_run), data);
1092 mi = webkit_context_menu_item_new_from_gaction(G_ACTION(action),
1093 "Open with external handler",
1095 webkit_context_menu_append(menu, mi);
1096 g_object_unref(action);
1099 /* FALSE = Show the menu. (TRUE = Don't ever show it.) */
1104 quit_if_nothing_active(void)
1114 gtk_widget_show_all(dm.win);
1121 remote_msg(GIOChannel *channel, GIOCondition condition, gpointer data)
1125 g_io_channel_read_line(channel, &uri, NULL, NULL, NULL);
1129 client_new(uri, NULL, TRUE);
1136 run_user_scripts(WebKitWebView *web_view)
1138 gchar *base = NULL, *path = NULL, *contents = NULL;
1139 const gchar *entry = NULL;
1140 GDir *scriptdir = NULL;
1142 base = g_build_filename(g_get_user_config_dir(), __NAME__, "scripts", NULL);
1143 scriptdir = g_dir_open(base, 0, NULL);
1144 if (scriptdir != NULL)
1146 while ((entry = g_dir_read_name(scriptdir)) != NULL)
1148 path = g_build_filename(base, entry, NULL);
1149 if (g_str_has_suffix(path, ".js"))
1151 if (g_file_get_contents(path, &contents, NULL, NULL))
1153 webkit_web_view_run_javascript(web_view, contents, NULL, NULL, NULL);
1159 g_dir_close(scriptdir);
1166 search(gpointer data, gint direction)
1168 struct Client *c = (struct Client *)data;
1169 WebKitWebView *web_view = WEBKIT_WEB_VIEW(c->web_view);
1170 WebKitFindController *fc = webkit_web_view_get_find_controller(web_view);
1172 if (search_text == NULL)
1178 webkit_find_controller_search(fc, search_text,
1179 WEBKIT_FIND_OPTIONS_CASE_INSENSITIVE |
1180 WEBKIT_FIND_OPTIONS_WRAP_AROUND,
1184 webkit_find_controller_search_next(fc);
1187 webkit_find_controller_search_previous(fc);
1193 show_web_view(WebKitWebView *web_view, gpointer data)
1195 struct Client *c = (struct Client *)data;
1199 gtk_widget_grab_focus(c->web_view);
1200 gtk_widget_show_all(c->win);
1207 GIOChannel *tabbed_stdout_channel;
1209 gchar *output = NULL;
1210 char *argv[] = { "tabbed", "-c", "-d", "-p", "s1", "-n", __NAME__, NULL };
1213 if (!g_spawn_async_with_pipes(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL,
1214 NULL, NULL, NULL, &tabbed_stdout, NULL,
1217 fprintf(stderr, __NAME__": Could not launch tabbed: %s\n", err->message);
1222 tabbed_stdout_channel = g_io_channel_unix_new(tabbed_stdout);
1223 if (tabbed_stdout_channel == NULL)
1225 fprintf(stderr, __NAME__": Could open tabbed's stdout\n");
1228 g_io_channel_read_line(tabbed_stdout_channel, &output, NULL, NULL, NULL);
1229 g_io_channel_shutdown(tabbed_stdout_channel, FALSE, NULL);
1232 fprintf(stderr, __NAME__": Could not read XID from tabbed\n");
1236 plug_into = strtol(output, NULL, 16);
1239 fprintf(stderr, __NAME__": The XID from tabbed is 0\n");
1244 trust_user_certs(WebKitWebContext *wc)
1246 GTlsCertificate *cert;
1247 const gchar *basedir, *file, *absfile;
1250 basedir = g_build_filename(g_get_user_config_dir(), __NAME__, "certs", NULL);
1251 dir = g_dir_open(basedir, 0, NULL);
1254 file = g_dir_read_name(dir);
1255 while (file != NULL)
1257 absfile = g_build_filename(g_get_user_config_dir(), __NAME__, "certs",
1259 cert = g_tls_certificate_new_from_file(absfile, NULL);
1261 fprintf(stderr, __NAME__": Could not load trusted cert '%s'\n", file);
1263 webkit_web_context_allow_tls_certificate_for_host(wc, cert, file);
1264 file = g_dir_read_name(dir);
1272 main(int argc, char **argv)
1277 gtk_init(&argc, &argv);
1278 webkit_web_context_set_process_model(webkit_web_context_get_default(),
1279 WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
1281 grab_environment_configuration();
1283 while ((opt = getopt(argc, argv, "e:CT")) != -1)
1288 embed = atol(optarg);
1289 tabbed_automagic = FALSE;
1292 cooperative_instances = FALSE;
1295 tabbed_automagic = FALSE;
1298 fprintf(stderr, "Usage: "__NAME__" [OPTION]... [URI]...\n");
1304 if (cooperative_instances)
1305 cooperation_setup();
1306 downloadmanager_setup();
1308 if (tabbed_automagic && !(cooperative_instances && !cooperative_alone))
1309 embed = tabbed_launch();
1311 if (!cooperative_instances || cooperative_alone)
1313 c = g_build_filename(g_get_user_config_dir(), __NAME__, "web_extensions",
1315 webkit_web_context_set_web_extensions_directory(
1316 webkit_web_context_get_default(), c
1321 client_new(home_uri, NULL, TRUE);
1324 for (i = optind; i < argc; i++)
1325 client_new(argv[i], NULL, TRUE);
1328 if (!cooperative_instances || cooperative_alone)