3 #include <webkit2/webkit2.h>
7 void client_destroy(GtkWidget *, gpointer);
8 WebKitWebView *client_new(const gchar *, WebKitWebView *, gboolean, gboolean);
9 WebKitWebView *client_new_request(WebKitWebView *, WebKitNavigationAction *,
11 void cooperation_setup(void);
12 void changed_load_progress(GObject *, GParamSpec *, gpointer);
13 void changed_favicon(GObject *, GParamSpec *, gpointer);
14 void changed_title(GObject *, GParamSpec *, gpointer);
15 void changed_uri(GObject *, GParamSpec *, gpointer);
16 gboolean crashed_web_view(WebKitWebView *, gpointer);
17 gboolean decide_policy(WebKitWebView *, WebKitPolicyDecision *,
18 WebKitPolicyDecisionType, gpointer);
19 gchar *ensure_uri_scheme(const gchar *);
20 void grab_environment_configuration(void);
21 void grab_feeds_finished(GObject *, GAsyncResult *, gpointer);
22 void hover_web_view(WebKitWebView *, WebKitHitTestResult *, guint, gpointer);
23 void icon_location(GtkEntry *, GtkEntryIconPosition, GdkEvent *, gpointer);
24 void init_default_web_context(void);
25 gboolean key_common(GtkWidget *, GdkEvent *, gpointer);
26 gboolean key_location(GtkWidget *, GdkEvent *, gpointer);
27 gboolean key_tablabel(GtkWidget *, GdkEvent *, gpointer);
28 gboolean key_web_view(GtkWidget *, GdkEvent *, gpointer);
29 void mainwindow_setup(void);
30 void mainwindow_title(gint);
31 void notebook_switch_page(GtkNotebook *, GtkWidget *, guint, gpointer);
32 gboolean remote_msg(GIOChannel *, GIOCondition, gpointer);
33 void run_user_scripts(WebKitWebView *);
34 void search(gpointer, gint);
35 void show_web_view(WebKitWebView *, gpointer);
36 void trust_user_certs(WebKitWebContext *);
37 GKeyFile *get_ini(void);
41 gchar *external_handler_uri;
49 gboolean focus_new_tab;
52 struct Configuration {
53 WebKitCookieAcceptPolicy cookie_policy;
54 const gchar *accepted_language[2];
55 gboolean cooperative_alone;
56 gboolean cooperative_instances;
57 gboolean enable_console_to_stdout;
58 gboolean javascript_disabled;
59 gboolean spellcheck_disabled;
65 gchar *spellcheck_language;
78 int cooperative_pipe_fp = 0;
82 client_destroy(GtkWidget *widget, gpointer data) {
83 struct Client *c = (struct Client *)data;
86 g_signal_handlers_disconnect_by_func(G_OBJECT(c->web_view),
87 changed_load_progress, c);
89 idx = gtk_notebook_page_num(GTK_NOTEBOOK(mw.notebook), c->vbox);
91 fprintf(stderr, __NAME__ ": Tab index was -1, bamboozled\n");
93 gtk_notebook_remove_page(GTK_NOTEBOOK(mw.notebook), idx);
98 quit_if_nothing_active();
102 client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show,
103 gboolean focus_tab) {
106 GtkWidget *evbox, *tabbox;
108 if (uri != NULL && cfg.cooperative_instances && !cfg.cooperative_alone) {
109 f = ensure_uri_scheme(uri);
110 write(cooperative_pipe_fp, f, strlen(f));
111 write(cooperative_pipe_fp, "\n", 1);
116 c = calloc(1, sizeof(struct Client));
118 fprintf(stderr, __NAME__ ": fatal: calloc failed\n");
122 c->focus_new_tab = focus_tab;
124 if (related_wv == NULL)
125 c->web_view = webkit_web_view_new();
127 c->web_view = webkit_web_view_new_with_related_view(related_wv);
129 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view),
131 WebKitSettings *settings =
132 webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view));
133 webkit_settings_set_enable_javascript(settings, !cfg.javascript_disabled);
134 g_signal_connect(G_OBJECT(c->web_view), "notify::favicon",
135 G_CALLBACK(changed_favicon), c);
136 g_signal_connect(G_OBJECT(c->web_view), "notify::title",
137 G_CALLBACK(changed_title), c);
138 g_signal_connect(G_OBJECT(c->web_view), "notify::uri",
139 G_CALLBACK(changed_uri), c);
140 g_signal_connect(G_OBJECT(c->web_view), "notify::estimated-load-progress",
141 G_CALLBACK(changed_load_progress), c);
142 g_signal_connect(G_OBJECT(c->web_view), "create",
143 G_CALLBACK(client_new_request), NULL);
144 g_signal_connect(G_OBJECT(c->web_view), "close", G_CALLBACK(client_destroy),
146 g_signal_connect(G_OBJECT(c->web_view), "decide-policy",
147 G_CALLBACK(decide_policy), NULL);
148 g_signal_connect(G_OBJECT(c->web_view), "key-press-event",
149 G_CALLBACK(key_web_view), c);
150 g_signal_connect(G_OBJECT(c->web_view), "button-release-event",
151 G_CALLBACK(key_web_view), c);
152 g_signal_connect(G_OBJECT(c->web_view), "scroll-event",
153 G_CALLBACK(key_web_view), c);
154 g_signal_connect(G_OBJECT(c->web_view), "mouse-target-changed",
155 G_CALLBACK(hover_web_view), c);
156 g_signal_connect(G_OBJECT(c->web_view), "web-process-crashed",
157 G_CALLBACK(crashed_web_view), c);
159 if (cfg.user_agent != NULL) {
160 g_object_set(settings, "user-agent", cfg.user_agent, NULL);
163 if (cfg.enable_console_to_stdout)
164 webkit_settings_set_enable_write_console_messages_to_stdout(settings,
167 webkit_settings_set_enable_developer_extras(settings, TRUE);
169 c->location = gtk_entry_new();
170 g_signal_connect(G_OBJECT(c->location), "key-press-event",
171 G_CALLBACK(key_location), c);
172 g_signal_connect(G_OBJECT(c->location), "icon-release",
173 G_CALLBACK(icon_location), c);
174 /* XXX This is a workaround. Setting this to NULL (which is done in
175 * grab_feeds_finished() if no feed has been detected) adds a little
176 * padding left of the text. Not sure why. The point of this call
177 * right here is to have that padding right from the start. This
178 * avoids a graphical artifact. */
179 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
180 GTK_ENTRY_ICON_PRIMARY, NULL);
182 c->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
183 gtk_box_pack_start(GTK_BOX(c->vbox), c->location, FALSE, FALSE, 0);
184 gtk_box_pack_start(GTK_BOX(c->vbox), c->web_view, TRUE, TRUE, 0);
185 gtk_container_set_focus_child(GTK_CONTAINER(c->vbox), c->web_view);
188 gtk_image_new_from_icon_name("text-html", GTK_ICON_SIZE_SMALL_TOOLBAR);
190 c->tablabel = gtk_label_new(__NAME__);
191 gtk_label_set_ellipsize(GTK_LABEL(c->tablabel), PANGO_ELLIPSIZE_END);
192 gtk_label_set_width_chars(GTK_LABEL(c->tablabel), cfg.tab_width_chars);
193 gtk_widget_set_has_tooltip(c->tablabel, TRUE);
195 /* XXX I don't own a HiDPI screen, so I don't know if scale_factor
196 * does the right thing. */
197 tabbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL,
198 5 * gtk_widget_get_scale_factor(mw.win));
199 gtk_box_pack_start(GTK_BOX(tabbox), c->tabicon, FALSE, FALSE, 0);
200 gtk_box_pack_start(GTK_BOX(tabbox), c->tablabel, TRUE, TRUE, 0);
202 evbox = gtk_event_box_new();
203 gtk_container_add(GTK_CONTAINER(evbox), tabbox);
204 g_signal_connect(G_OBJECT(evbox), "button-release-event",
205 G_CALLBACK(key_tablabel), c);
207 gtk_widget_add_events(evbox, GDK_SCROLL_MASK);
208 g_signal_connect(G_OBJECT(evbox), "scroll-event", G_CALLBACK(key_tablabel),
211 /* For easy access, store a reference to our label. */
212 g_object_set_data(G_OBJECT(evbox), "chorizo-tab-label", c->tablabel);
214 /* This only shows the event box and the label inside, nothing else.
215 * Needed because the evbox/label is "internal" to the notebook and
216 * not part of the normal "widget tree" (IIUC). */
217 gtk_widget_show_all(evbox);
219 int page = gtk_notebook_get_current_page(GTK_NOTEBOOK(mw.notebook)) + 1;
220 gtk_notebook_insert_page(GTK_NOTEBOOK(mw.notebook), c->vbox, evbox, page);
221 gtk_notebook_set_tab_reorderable(GTK_NOTEBOOK(mw.notebook), c->vbox, TRUE);
224 show_web_view(NULL, c);
226 g_signal_connect(G_OBJECT(c->web_view), "ready-to-show",
227 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) {
243 return client_new(NULL, web_view, FALSE, FALSE);
247 cooperation_setup(void) {
249 gchar *fifofilename, *fifopath;
251 fifofilename = g_strdup_printf("%s-%s", __NAME__ ".fifo", cfg.fifo_suffix);
252 fifopath = g_build_filename(g_get_user_runtime_dir(), fifofilename, NULL);
253 g_free(fifofilename);
255 if (!g_file_test(fifopath, G_FILE_TEST_EXISTS))
256 mkfifo(fifopath, 0600);
258 cooperative_pipe_fp = open(fifopath, O_WRONLY | O_NONBLOCK);
259 if (!cooperative_pipe_fp) {
260 fprintf(stderr, __NAME__ ": Can't open FIFO at all.\n");
262 if (write(cooperative_pipe_fp, "", 0) == -1) {
263 /* Could not do an empty write to the FIFO which means
264 there's no one listening. */
265 close(cooperative_pipe_fp);
266 towatch = g_io_channel_new_file(fifopath, "r+", NULL);
267 g_io_add_watch(towatch, G_IO_IN, (GIOFunc)remote_msg, NULL);
269 cfg.cooperative_alone = FALSE;
277 changed_load_progress(GObject *obj, GParamSpec *pspec, gpointer data) {
278 struct Client *c = (struct Client *)data;
281 "a = document.querySelectorAll('"
283 "link[rel=\"alternate\"][href][type=\"application/atom+xml\"],"
285 "link[rel=\"alternate\"][href][type=\"application/rss+xml\"]"
291 " for (i = 0; i < a.length; i++) {"
292 " url = encodeURIComponent(a[i].href);"
293 " if ('title' in a[i] && a[i].title != '')"
294 " title = encodeURIComponent(a[i].title);"
297 " out += '<li><a href=\"' + url + '\">' + title + "
303 p = webkit_web_view_get_estimated_load_progress(
304 WEBKIT_WEB_VIEW(c->web_view));
308 /* The page has loaded fully. We now run the short JavaScript
309 * snippet above that operates on the DOM. It tries to grab all
310 * occurences of <link rel="alternate" ...>, i.e. RSS/Atom feed
312 webkit_web_view_run_javascript(WEBKIT_WEB_VIEW(c->web_view), grab_feeds,
313 NULL, grab_feeds_finished, c);
315 run_user_scripts(WEBKIT_WEB_VIEW(c->web_view));
317 gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), p);
321 changed_favicon(GObject *obj, GParamSpec *pspec, gpointer data) {
322 struct Client *c = (struct Client *)data;
324 int w, h, w_should, h_should;
325 GdkPixbuf *pb, *pb_scaled;
327 f = webkit_web_view_get_favicon(WEBKIT_WEB_VIEW(c->web_view));
329 gtk_image_set_from_icon_name(GTK_IMAGE(c->tabicon), "text-html",
330 GTK_ICON_SIZE_SMALL_TOOLBAR);
332 w = cairo_image_surface_get_width(f);
333 h = cairo_image_surface_get_height(f);
334 pb = gdk_pixbuf_get_from_surface(f, 0, 0, w, h);
336 w_should = 16 * gtk_widget_get_scale_factor(c->tabicon);
337 h_should = 16 * gtk_widget_get_scale_factor(c->tabicon);
338 pb_scaled = gdk_pixbuf_scale_simple(pb, w_should, h_should,
339 GDK_INTERP_BILINEAR);
340 gtk_image_set_from_pixbuf(GTK_IMAGE(c->tabicon), pb_scaled);
342 g_object_unref(pb_scaled);
349 changed_title(GObject *obj, GParamSpec *pspec, gpointer data) {
351 struct Client *c = (struct Client *)data;
353 u = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
354 t = webkit_web_view_get_title(WEBKIT_WEB_VIEW(c->web_view));
356 u = u == NULL ? __NAME__ : u;
357 u = u[0] == 0 ? __NAME__ : u;
359 t = t == NULL ? u : t;
360 t = t[0] == 0 ? u : t;
362 gchar *name = malloc(strlen(t) + 4);
363 gboolean mute = webkit_web_view_get_is_muted(WEBKIT_WEB_VIEW(c->web_view));
364 gchar *muted = (mute) ? "[m] " : "";
365 sprintf(name, "%s%s", muted, t);
367 gtk_label_set_text(GTK_LABEL(c->tablabel), name);
369 gtk_widget_set_tooltip_text(c->tablabel, t);
370 mainwindow_title(gtk_notebook_get_current_page(GTK_NOTEBOOK(mw.notebook)));
374 changed_uri(GObject *obj, GParamSpec *pspec, gpointer data) {
376 struct Client *c = (struct Client *)data;
379 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
381 /* When a web process crashes, we get a "notify::uri" signal, but we
382 * can no longer read a meaningful URI. It's just an empty string
383 * now. Not updating the location bar in this scenario is important,
384 * because we would override the "WEB PROCESS CRASHED" message. */
385 if (t != NULL && strlen(t) > 0) {
386 gtk_entry_set_text(GTK_ENTRY(c->location), t);
388 if (cfg.history_file != NULL) {
389 fp = fopen(cfg.history_file, "a");
391 fprintf(fp, "%s\n", t);
394 perror(__NAME__ ": Error opening history file");
401 crashed_web_view(WebKitWebView *web_view, gpointer data) {
403 struct Client *c = (struct Client *)data;
405 t = g_strdup_printf("WEB PROCESS CRASHED: %s",
406 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(web_view)));
407 gtk_entry_set_text(GTK_ENTRY(c->location), t);
414 decide_policy(WebKitWebView *web_view, WebKitPolicyDecision *decision,
415 WebKitPolicyDecisionType type, gpointer data) {
416 WebKitResponsePolicyDecision *r;
419 case WEBKIT_POLICY_DECISION_TYPE_RESPONSE:
420 r = WEBKIT_RESPONSE_POLICY_DECISION(decision);
421 if (!webkit_response_policy_decision_is_mime_type_supported(r))
422 webkit_policy_decision_download(decision);
424 webkit_policy_decision_use(decision);
427 /* Use whatever default there is. */
434 ensure_uri_scheme(const gchar *t) {
437 f = g_ascii_strdown(t, -1);
438 if (!g_str_has_prefix(f, "http:") && !g_str_has_prefix(f, "https:") &&
439 !g_str_has_prefix(f, "file:") && !g_str_has_prefix(f, "about:") &&
440 !g_str_has_prefix(f, "data:") && !g_str_has_prefix(f, "webkit:")) {
442 fabs = realpath(t, NULL);
444 f = g_strdup_printf("file://%s", fabs);
447 f = g_strdup_printf("http://%s", t);
456 cfg.accepted_language[0] = NULL;
457 cfg.accepted_language[1] = NULL;
458 cfg.cooperative_alone = TRUE;
459 cfg.cooperative_alone = TRUE;
460 cfg.fifo_suffix = "main";
462 const char *e = g_getenv(__NAME_UPPERCASE__ "_FIFO_SUFFIX");
464 cfg.fifo_suffix = g_strdup(e);
466 char *xdg_down = getenv("XDG_DOWNLOAD_DIR");
467 cfg.download_dir = (xdg_down) ? xdg_down : "/var/tmp";
470 cfg.accepted_language[0] =
471 g_key_file_get_string(config, "browser", "accepted_language", NULL);
473 g_key_file_get_string(config, "browser", "history_file", NULL);
474 cfg.home_uri = g_key_file_get_string(config, "browser", "homepage", NULL);
475 cfg.home_uri = (cfg.home_uri) ? cfg.home_uri : "about:blank";
476 cfg.enable_console_to_stdout =
477 g_key_file_get_boolean(config, "browser", "console_to_stdout", NULL);
479 g_key_file_get_string(config, "browser", "user_agent", NULL);
480 cfg.javascript_disabled =
481 g_key_file_get_boolean(config, "browser", "javascript_disabled", NULL);
482 char *input_cookie_policy =
483 g_key_file_get_string(config, "browser", "cookie_policy", NULL);
484 cfg.cookie_policy = WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY;
485 if (input_cookie_policy) {
486 if (strcmp(input_cookie_policy, "all") == 0) {
487 cfg.cookie_policy = WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS;
488 } else if (strcmp(input_cookie_policy, "none") == 0) {
489 cfg.cookie_policy = WEBKIT_COOKIE_POLICY_ACCEPT_NEVER;
493 cfg.tab_width_chars =
494 g_key_file_get_integer(config, "ui", "tab_width", NULL);
495 cfg.tab_width_chars = (cfg.tab_width_chars) ? cfg.tab_width_chars : 20;
496 cfg.global_zoom = g_key_file_get_double(config, "ui", "zoom_level", NULL);
497 cfg.global_zoom = (cfg.global_zoom) ? cfg.global_zoom : 1.0;
499 g_key_file_get_string(config, "ui", "search_engine", NULL);
501 (cfg.search_engine) ? cfg.search_engine : "https://duckduckgo.com?q=";
502 cfg.spellcheck_disabled =
503 g_key_file_get_boolean(config, "ui", "spellcheck_disabled", NULL);
504 cfg.spellcheck_language =
505 g_key_file_get_string(config, "ui", "spellcheck_language", NULL);
506 cfg.spellcheck_language =
507 (cfg.spellcheck_language) ? cfg.spellcheck_language : "en_US";
509 g_key_file_get_integer(config, "ui", "scroll_lines", NULL);
510 cfg.scroll_lines = (cfg.scroll_lines) ? cfg.scroll_lines : 3;
514 grab_feeds_finished(GObject *object, GAsyncResult *result, gpointer data) {
515 struct Client *c = (struct Client *)data;
516 WebKitJavascriptResult *js_result;
518 JSCException *exception;
522 g_free(c->feed_html);
525 /* This was taken almost verbatim from the example in WebKit's
528 * https://webkitgtk.org/reference/webkit2gtk/stable/WebKitWebView.html */
530 js_result = webkit_web_view_run_javascript_finish(WEBKIT_WEB_VIEW(object),
533 fprintf(stderr, __NAME__ ": Error running javascript: %s\n",
539 value = webkit_javascript_result_get_js_value(js_result);
540 if (jsc_value_is_string(value)) {
541 str_value = jsc_value_to_string(value);
542 exception = jsc_context_get_exception(jsc_value_get_context(value));
543 if (exception != NULL) {
544 fprintf(stderr, __NAME__ ": Error running javascript: %s\n",
545 jsc_exception_get_message(exception));
547 c->feed_html = str_value;
550 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
551 GTK_ENTRY_ICON_PRIMARY,
552 "application-rss+xml-symbolic");
553 gtk_entry_set_icon_activatable(GTK_ENTRY(c->location),
554 GTK_ENTRY_ICON_PRIMARY, TRUE);
556 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
557 GTK_ENTRY_ICON_PRIMARY, NULL);
560 webkit_javascript_result_unref(js_result);
564 hover_web_view(WebKitWebView *web_view, WebKitHitTestResult *ht,
565 guint modifiers, gpointer data) {
566 struct Client *c = (struct Client *)data;
569 g_free(c->hover_uri);
571 if (webkit_hit_test_result_context_is_link(ht)) {
572 to_show = webkit_hit_test_result_get_link_uri(ht);
573 c->hover_uri = g_strdup(to_show);
575 to_show = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
579 if (!gtk_widget_is_focus(c->location))
580 gtk_entry_set_text(GTK_ENTRY(c->location), to_show);
584 icon_location(GtkEntry *entry, GtkEntryIconPosition icon_pos, GdkEvent *event,
586 struct Client *c = (struct Client *)data;
588 gchar *data_template = "data:text/html,"
592 " <meta charset=\"UTF-8\">"
593 " <title>Feeds</title>"
596 " <p>Feeds found on this page:</p>"
603 if (c->feed_html != NULL) {
604 /* What we're actually trying to do is show a simple HTML page
605 * that lists all the feeds on the current page. The function
606 * webkit_web_view_load_html() looks like the proper way to do
607 * that. Sad thing is, it doesn't create a history entry, but
608 * instead simply replaces the content of the current page. This
609 * is not what we want.
611 * RFC 2397 [0] defines the data URI scheme [1]. We abuse this
612 * mechanism to show my custom HTML snippet *and* create a
615 * [0]: https://tools.ietf.org/html/rfc2397
616 * [1]: https://en.wikipedia.org/wiki/Data_URI_scheme */
617 d = g_strdup_printf(data_template, c->feed_html);
618 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), d);
624 init_default_web_context(void) {
626 WebKitWebContext *wc;
627 WebKitCookieManager *cm;
629 wc = webkit_web_context_get_default();
631 p = g_build_filename(g_get_user_config_dir(), __NAME__, "adblock", NULL);
632 webkit_web_context_set_sandbox_enabled(wc, TRUE);
633 webkit_web_context_add_path_to_sandbox(wc, p, TRUE);
636 WebKitProcessModel model =
637 WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES;
638 webkit_web_context_set_process_model(wc, model);
640 p = g_build_filename(g_get_user_data_dir(), __NAME__, "web_extensions",
642 webkit_web_context_set_web_extensions_directory(wc, p);
645 if (cfg.accepted_language[0] != NULL)
646 webkit_web_context_set_preferred_languages(wc, cfg.accepted_language);
648 g_signal_connect(G_OBJECT(wc), "download-started",
649 G_CALLBACK(download_start), &cfg);
651 trust_user_certs(wc);
653 cm = webkit_web_context_get_cookie_manager(wc);
654 webkit_cookie_manager_set_accept_policy(cm, cfg.cookie_policy);
656 webkit_web_context_set_favicon_database_directory(wc, NULL);
657 gchar *fname = g_build_filename("/", g_get_user_cache_dir(), __NAME__,
660 WebKitCookiePersistentStorage type =
661 WEBKIT_COOKIE_PERSISTENT_STORAGE_SQLITE;
662 webkit_cookie_manager_set_persistent_storage(cm, fname, type);
664 const gchar *const languages[2] = {(const gchar *)cfg.spellcheck_language,
666 webkit_web_context_set_spell_checking_languages(wc, languages);
667 webkit_web_context_set_spell_checking_enabled(wc, !cfg.spellcheck_disabled);
671 search(gpointer data, gint direction) {
672 struct Client *c = (struct Client *)data;
673 WebKitWebView *web_view = WEBKIT_WEB_VIEW(c->web_view);
674 WebKitFindController *fc = webkit_web_view_get_find_controller(web_view);
676 if (search_text == NULL)
681 webkit_find_controller_search(fc, search_text,
682 WEBKIT_FIND_OPTIONS_CASE_INSENSITIVE |
683 WEBKIT_FIND_OPTIONS_WRAP_AROUND,
687 webkit_find_controller_search_next(fc);
690 webkit_find_controller_search_previous(fc);
693 webkit_find_controller_search_finish(fc);
699 search_init(struct Client *c, int direction) {
700 gtk_widget_grab_focus(c->location);
701 const gchar *contents = gtk_entry_get_text(GTK_ENTRY(c->location));
702 if (strcspn(contents, "s/")) {
703 gtk_entry_set_text(GTK_ENTRY(c->location), "s/");
705 gtk_editable_set_position(GTK_EDITABLE(c->location), -1);
708 search(c, direction);
712 def_key(char *key, unsigned int def) {
713 char *conf = g_key_file_get_string(config, "keybindings", key, NULL);
714 return (conf) ? gdk_keyval_from_name((conf) ? conf : NULL) : def;
718 key_common(GtkWidget *widget, GdkEvent *event, gpointer data) {
719 struct Client *c = (struct Client *)data;
723 if (event->type == GDK_KEY_PRESS) {
724 if (((GdkEventKey *)event)->state & GDK_CONTROL_MASK) {
725 WebKitSettings *settings =
726 webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view));
727 gboolean js = webkit_settings_get_enable_javascript(settings);
728 int key = ((GdkEventKey *)event)->keyval;
729 if (def_key("download_manager", GDK_KEY_y) == key) {
730 downloadmanager_show();
732 } else if (def_key("history_back", GDK_KEY_h) == key) {
733 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
735 } else if (def_key("history_forwards", GDK_KEY_l) == key) {
736 webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
738 } else if (def_key("location", GDK_KEY_t) == key) {
739 gtk_widget_grab_focus(c->location);
741 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
742 const char *goal = (uri) ? uri : "https://";
743 gtk_entry_set_text(GTK_ENTRY(c->location), goal);
744 gtk_editable_set_position(GTK_EDITABLE(c->location), -1);
746 } else if (def_key("print", GDK_KEY_Print) == key) {
747 WebKitPrintOperation *operation =
748 webkit_print_operation_new(WEBKIT_WEB_VIEW(c->web_view));
749 GtkWidget *toplevel = gtk_widget_get_toplevel(mw.win);
750 webkit_print_operation_run_dialog(operation,
751 GTK_WINDOW(toplevel));
753 } else if (def_key("quit", GDK_KEY_g) == key) {
755 gtk_widget_grab_focus(c->web_view);
757 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
758 gtk_entry_set_text(GTK_ENTRY(c->location), uri);
759 gtk_editable_set_position(GTK_EDITABLE(c->location), -1);
760 webkit_web_view_run_javascript(
761 WEBKIT_WEB_VIEW(c->web_view),
762 "window.getSelection().removeAllRanges();"
763 "document.activeElement.blur();",
766 } else if (def_key("reload", GDK_KEY_e) == key) {
767 webkit_web_view_reload_bypass_cache(
768 WEBKIT_WEB_VIEW(c->web_view));
770 } else if (def_key("scroll_line_down", GDK_KEY_j) == key) {
771 for (int i = 0; i <= cfg.scroll_lines - 1; i++) {
772 event->key.keyval = GDK_KEY_Down;
773 gdk_event_put(event);
776 } else if (def_key("scroll_line_up", GDK_KEY_k) == key) {
777 for (int i = 0; i <= cfg.scroll_lines - 1; i++) {
778 event->key.keyval = GDK_KEY_Up;
779 gdk_event_put(event);
782 } else if (def_key("scroll_page_down", GDK_KEY_f) == key) {
783 event->key.keyval = GDK_KEY_Page_Down;
784 gdk_event_put(event);
786 } else if (def_key("scroll_page_up", GDK_KEY_b) == key) {
787 event->key.keyval = GDK_KEY_Page_Up;
788 gdk_event_put(event);
790 } else if (def_key("search_forwards", GDK_KEY_s) == key) {
793 } else if (def_key("search_backwards", GDK_KEY_r) == key) {
796 } else if (def_key("tab_close", GDK_KEY_q) == key) {
797 client_destroy(NULL, c);
799 } else if (def_key("tab_switch_1", GDK_KEY_1) == key) {
800 gtk_notebook_set_current_page(GTK_NOTEBOOK(mw.notebook), 0);
802 } else if (def_key("tab_switch_2", GDK_KEY_2) == key) {
803 gtk_notebook_set_current_page(GTK_NOTEBOOK(mw.notebook), 1);
805 } else if (def_key("tab_switch_3", GDK_KEY_3) == key) {
806 gtk_notebook_set_current_page(GTK_NOTEBOOK(mw.notebook), 2);
808 } else if (def_key("tab_switch_4", GDK_KEY_4) == key) {
809 gtk_notebook_set_current_page(GTK_NOTEBOOK(mw.notebook), 3);
811 } else if (def_key("tab_switch_5", GDK_KEY_5) == key) {
812 gtk_notebook_set_current_page(GTK_NOTEBOOK(mw.notebook), 4);
814 } else if (def_key("tab_switch_6", GDK_KEY_6) == key) {
815 gtk_notebook_set_current_page(GTK_NOTEBOOK(mw.notebook), 5);
817 } else if (def_key("tab_switch_7", GDK_KEY_7) == key) {
818 gtk_notebook_set_current_page(GTK_NOTEBOOK(mw.notebook), 6);
820 } else if (def_key("tab_switch_8", GDK_KEY_8) == key) {
821 gtk_notebook_set_current_page(GTK_NOTEBOOK(mw.notebook), 7);
823 } else if (def_key("tab_switch_9", GDK_KEY_9) == key) {
824 gtk_notebook_set_current_page(GTK_NOTEBOOK(mw.notebook), 8);
826 } else if (def_key("tab_previous", GDK_KEY_u) == key) {
827 gtk_notebook_prev_page(GTK_NOTEBOOK(mw.notebook));
829 } else if (def_key("tab_mute", GDK_KEY_m) == key) {
831 webkit_web_view_get_is_muted(WEBKIT_WEB_VIEW(c->web_view));
832 webkit_web_view_set_is_muted(WEBKIT_WEB_VIEW(c->web_view),
834 changed_title(G_OBJECT(c->web_view), NULL, c);
836 } else if (def_key("tab_new", GDK_KEY_w) == key) {
837 f = ensure_uri_scheme(cfg.home_uri);
838 client_new(f, NULL, TRUE, TRUE);
841 } else if (def_key("tab_next", GDK_KEY_i) == key) {
842 gtk_notebook_next_page(GTK_NOTEBOOK(mw.notebook));
844 } else if (def_key("toggle_js", GDK_KEY_o) == key) {
845 webkit_settings_set_enable_javascript(settings,
846 (js) ? FALSE : TRUE);
847 webkit_web_view_set_settings(WEBKIT_WEB_VIEW(c->web_view),
850 } else if (def_key("web_search", GDK_KEY_d) == key) {
851 gtk_widget_grab_focus(c->location);
852 gtk_entry_set_text(GTK_ENTRY(c->location), "w/");
853 gtk_editable_set_position(GTK_EDITABLE(c->location), -1);
855 } else if (def_key("zoom_in", GDK_KEY_equal) == key) {
856 now = webkit_web_view_get_zoom_level(
857 WEBKIT_WEB_VIEW(c->web_view));
858 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view),
861 } else if (def_key("zoom_out", GDK_KEY_minus) == key) {
862 now = webkit_web_view_get_zoom_level(
863 WEBKIT_WEB_VIEW(c->web_view));
864 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view),
867 } else if (def_key("zoom_reset", GDK_KEY_0) == key) {
868 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view),
878 key_location(GtkWidget *widget, GdkEvent *event, gpointer data) {
879 struct Client *c = (struct Client *)data;
883 if (key_common(widget, event, data))
886 if (event->type == GDK_KEY_PRESS) {
887 int key = ((GdkEventKey *)event)->keyval;
888 if ((GDK_KEY_KP_Enter == key) || (GDK_KEY_Return == key)) {
889 gtk_widget_grab_focus(c->web_view);
890 t = gtk_entry_get_text(GTK_ENTRY(c->location));
891 if (t != NULL && t[0] == 's' && t[1] == '/') {
892 if (search_text != NULL)
894 search_text = g_strdup(t + 2);
896 } else if (t != NULL && t[0] == 'w' && t[1] == '/') {
897 const char *engine = cfg.search_engine;
898 int len = strlen(engine) + strlen(t) - 2;
899 char *f = (char *)malloc(len);
900 snprintf(f, len + 1, "%s%s", engine, t + 2);
901 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
904 f = ensure_uri_scheme(t);
905 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
909 } else if (GDK_KEY_Escape == key) {
910 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
911 gtk_entry_set_text(GTK_ENTRY(c->location), (t == NULL) ? "" : t);
919 key_tablabel(GtkWidget *widget, GdkEvent *event, gpointer data) {
920 GdkScrollDirection direction;
922 if (event->type == GDK_BUTTON_RELEASE) {
923 switch (((GdkEventButton *)event)->button) {
925 client_destroy(NULL, data);
928 } else if (event->type == GDK_SCROLL) {
929 gdk_event_get_scroll_direction(event, &direction);
932 gtk_notebook_prev_page(GTK_NOTEBOOK(mw.notebook));
934 case GDK_SCROLL_DOWN:
935 gtk_notebook_next_page(GTK_NOTEBOOK(mw.notebook));
946 key_web_view(GtkWidget *widget, GdkEvent *event, gpointer data) {
947 struct Client *c = (struct Client *)data;
951 if (key_common(widget, event, data))
954 if (event->type == GDK_KEY_PRESS) {
955 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);
959 } else if (event->type == GDK_BUTTON_RELEASE) {
960 switch (((GdkEventButton *)event)->button) {
962 if (c->hover_uri != NULL) {
963 client_new(c->hover_uri, NULL, TRUE, FALSE);
968 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
971 webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
974 } else if (event->type == GDK_SCROLL) {
975 event->scroll.delta_y *= cfg.scroll_lines;
976 if (((GdkEventScroll *)event)->state & GDK_CONTROL_MASK) {
977 gdk_event_get_scroll_deltas(event, &dx, &dy);
978 z = webkit_web_view_get_zoom_level(WEBKIT_WEB_VIEW(c->web_view));
980 z = dx != 0 ? cfg.global_zoom : z;
981 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), z);
990 mainwindow_setup(void) {
991 mw.win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
992 gtk_window_set_default_size(GTK_WINDOW(mw.win), 800, 600);
993 g_signal_connect(G_OBJECT(mw.win), "destroy", gtk_main_quit, NULL);
994 gtk_window_set_title(GTK_WINDOW(mw.win), __NAME__);
996 mw.notebook = gtk_notebook_new();
997 gtk_notebook_set_scrollable(GTK_NOTEBOOK(mw.notebook), TRUE);
998 gtk_container_add(GTK_CONTAINER(mw.win), mw.notebook);
999 g_signal_connect(G_OBJECT(mw.notebook), "switch-page",
1000 G_CALLBACK(notebook_switch_page), NULL);
1004 mainwindow_title(gint idx) {
1005 GtkWidget *child, *widg, *tablabel;
1008 child = gtk_notebook_get_nth_page(GTK_NOTEBOOK(mw.notebook), idx);
1012 widg = gtk_notebook_get_tab_label(GTK_NOTEBOOK(mw.notebook), child);
1014 (GtkWidget *)g_object_get_data(G_OBJECT(widg), "chorizo-tab-label");
1015 text = gtk_label_get_text(GTK_LABEL(tablabel));
1016 gtk_window_set_title(GTK_WINDOW(mw.win), text);
1020 notebook_switch_page(GtkNotebook *nb, GtkWidget *p, guint idx, gpointer data) {
1021 mainwindow_title(idx);
1025 quit_if_nothing_active(void) {
1027 if (downloads == 0) {
1031 downloadmanager_show();
1039 remote_msg(GIOChannel *channel, GIOCondition condition, gpointer data) {
1042 g_io_channel_read_line(channel, &uri, NULL, NULL, NULL);
1045 client_new(uri, NULL, TRUE, TRUE);
1052 run_user_scripts(WebKitWebView *web_view) {
1053 gchar *base = NULL, *path = NULL, *contents = NULL;
1054 const gchar *entry = NULL;
1055 GDir *scriptdir = NULL;
1058 g_build_filename(g_get_user_data_dir(), __NAME__, "user-scripts", NULL);
1059 scriptdir = g_dir_open(base, 0, NULL);
1060 if (scriptdir != NULL) {
1061 while ((entry = g_dir_read_name(scriptdir)) != NULL) {
1062 path = g_build_filename(base, entry, NULL);
1063 char *jscmd = malloc(strlen(path) + 36);
1064 sprintf(jscmd, "console.log(\"Running userscript %s\");", path);
1065 if (g_str_has_suffix(path, ".js")) {
1066 if (g_file_get_contents(path, &contents, NULL, NULL)) {
1067 webkit_web_view_run_javascript(web_view, jscmd, NULL, NULL,
1069 webkit_web_view_run_javascript(web_view, contents, NULL,
1077 g_dir_close(scriptdir);
1084 show_web_view(WebKitWebView *web_view, gpointer data) {
1085 struct Client *c = (struct Client *)data;
1090 gtk_widget_show_all(mw.win);
1092 if (c->focus_new_tab) {
1093 idx = gtk_notebook_page_num(GTK_NOTEBOOK(mw.notebook), c->vbox);
1095 gtk_notebook_set_current_page(GTK_NOTEBOOK(mw.notebook), idx);
1097 gtk_widget_grab_focus(c->web_view);
1102 trust_user_certs(WebKitWebContext *wc) {
1103 GTlsCertificate *cert;
1104 const gchar *basedir, *file, *absfile;
1107 basedir = g_build_filename(g_get_user_data_dir(), __NAME__, "certs", NULL);
1108 dir = g_dir_open(basedir, 0, NULL);
1110 file = g_dir_read_name(dir);
1111 while (file != NULL) {
1112 absfile = g_build_filename(g_get_user_data_dir(), __NAME__, "certs",
1114 cert = g_tls_certificate_new_from_file(absfile, NULL);
1116 fprintf(stderr, __NAME__ ": Could not load trusted cert '%s'\n",
1119 webkit_web_context_allow_tls_certificate_for_host(wc, cert,
1121 file = g_dir_read_name(dir);
1129 GKeyFileFlags flags = G_KEY_FILE_NONE;
1130 config = g_key_file_new();
1133 if (!g_key_file_load_from_file(config,
1134 g_build_filename(g_get_user_config_dir(),
1135 __NAME__, "chorizo.ini",
1138 // Load global config
1139 if (!g_key_file_load_from_file(config, "/etc/chorizo.ini", flags,
1141 fprintf(stderr, "Could not load chorizo.ini");
1148 main(int argc, char **argv) {
1151 while ((opt = getopt(argc, argv, "Cv")) != -1) {
1154 cfg.cooperative_instances = FALSE;
1157 printf("%s %s\n", __NAME__, VERSION);
1160 fprintf(stderr, "Usage: " __NAME__ " [OPTION]... [URI]...\n");
1165 gtk_init(&argc, &argv);
1168 if (cfg.cooperative_instances)
1169 cooperation_setup();
1171 if (!cfg.cooperative_instances || cfg.cooperative_alone)
1172 init_default_web_context();
1174 downloadmanager_setup();
1177 if (optind >= argc) {
1178 client_new(cfg.home_uri, NULL, TRUE, TRUE);
1180 for (i = optind; i < argc; i++)
1181 client_new(argv[i], NULL, TRUE, TRUE);
1184 if (!cfg.cooperative_instances || cfg.cooperative_alone)