6 #include <webkit2/webkit2.h>
11 WebKitWebView *client_new_request(WebKitWebView *, WebKitNavigationAction *,
13 gboolean crashed_web_view(WebKitWebView *, gpointer);
14 gboolean decide_policy(WebKitWebView *, WebKitPolicyDecision *,
15 WebKitPolicyDecisionType, gpointer);
16 gboolean key_location(GtkWidget *, GdkEvent *, gpointer);
17 gboolean key_tablabel(GtkWidget *, GdkEvent *, gpointer);
18 gboolean key_web_view(GtkWidget *, GdkEvent *, gpointer);
19 gboolean remote_msg(GIOChannel *, GIOCondition, gpointer);
20 gchar *ensure_uri_scheme(const gchar *);
21 void changed_favicon(GObject *, GParamSpec *, gpointer);
22 void changed_load_progress(GObject *, GParamSpec *, gpointer);
23 void changed_title(GObject *, GParamSpec *, gpointer);
24 void changed_uri(GObject *, GParamSpec *, gpointer);
25 void grab_feeds_finished(GObject *, GAsyncResult *, gpointer);
26 void hover_web_view(WebKitWebView *, WebKitHitTestResult *, guint, gpointer);
27 void icon_location(GtkEntry *, GtkEntryIconPosition, GdkEvent *, gpointer);
28 void mainwindow_title(gint);
29 void notebook_switch_page(GtkNotebook *, GtkWidget *, guint, gpointer);
30 void show_web_view(WebKitWebView *, gpointer);
31 void trust_user_certs(WebKitWebContext *);
40 WebKitSettings *settings;
41 gboolean focus_new_tab;
42 gchar *external_handler_uri;
52 struct Configuration {
53 gboolean cooperative_alone;
54 gboolean noncooperative_instances;
60 struct Client **client_arr;
62 int cooperative_pipe_fp = 0;
68 togglejs(GtkButton *jsbutton, gpointer data)
70 struct Client *c = (struct Client *)data;
71 webkit_settings_set_enable_javascript(
73 !webkit_settings_get_enable_javascript(c->settings));
74 webkit_web_view_set_settings(WEBKIT_WEB_VIEW(c->web_view), c->settings);
78 client_destroy(GtkWidget *widget, gpointer data)
80 struct Client *c = (struct Client *)data;
82 g_signal_handlers_disconnect_by_func(G_OBJECT(c->web_view),
83 changed_load_progress, c);
85 idx = gtk_notebook_page_num(GTK_NOTEBOOK(mw.notebook), c->vbox);
87 fprintf(stderr, "chorizo: Tab index was -1, bamboozled\n");
89 gtk_notebook_remove_page(GTK_NOTEBOOK(mw.notebook), idx);
91 if (!cfg.private && WEBKIT_IS_WEB_VIEW(c->web_view)) {
92 int len = sizeof(closed_tabs) / sizeof(char *);
94 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
95 closed_tabs = (char **)realloc(closed_tabs,
96 len * sizeof(closed_tabs[0]));
97 closed_tabs[len] = strdup(uri);
103 quit_if_nothing_active();
107 set_uri(const char *uri, struct Client *c)
109 if (!gtk_widget_is_focus(c->location))
110 gtk_entry_set_text(GTK_ENTRY(c->location),
111 (uri != NULL) ? uri : "");
115 client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show,
120 GtkWidget *evbox, *tabbox;
121 if (uri != NULL && !cfg.noncooperative_instances &&
122 !cfg.cooperative_alone) {
123 f = ensure_uri_scheme(uri);
124 write(cooperative_pipe_fp, f, strlen(f));
125 write(cooperative_pipe_fp, "\n", 1);
129 c = calloc(1, sizeof(struct Client));
131 fprintf(stderr, "chorizo: fatal: calloc failed\n");
134 c->focus_new_tab = focus_tab;
136 if (related_wv == NULL) {
137 WebKitUserContentManager *ucm =
138 webkit_user_content_manager_new();
139 WebKitUserScript *wkscript;
140 WebKitUserStyleSheet *wkstyle;
141 gchar *path = NULL, *source, *base;
142 const gchar *entry = NULL;
144 base = g_build_filename(g_get_user_data_dir(), "chorizo",
145 "user-scripts", NULL);
146 dir = g_dir_open(base, 0, NULL);
148 while ((entry = g_dir_read_name(dir)) != NULL) {
149 path = g_build_filename(base, entry, NULL);
150 if (g_str_has_suffix(path, ".js")) {
151 g_file_get_contents(path, &source, NULL,
153 wkscript = webkit_user_script_new(
155 WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES,
156 WEBKIT_USER_SCRIPT_INJECT_AT_DOCUMENT_START,
158 webkit_user_content_manager_add_script(
160 webkit_user_script_unref(wkscript);
168 base = g_build_filename(g_get_user_data_dir(), "chorizo",
169 "user-styles", NULL);
170 dir = g_dir_open(base, 0, NULL);
172 while ((entry = g_dir_read_name(dir)) != NULL) {
173 path = g_build_filename(base, entry, NULL);
174 if (g_str_has_suffix(path, ".css")) {
175 g_file_get_contents(path, &source, NULL,
177 wkstyle = webkit_user_style_sheet_new(
179 WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES,
180 WEBKIT_USER_STYLE_LEVEL_USER,
182 webkit_user_content_manager_add_style_sheet(
184 webkit_user_style_sheet_unref(wkstyle);
194 webkit_web_view_new_with_user_content_manager(ucm);
196 c->web_view = webkit_web_view_new_with_related_view(related_wv);
200 webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view));
201 webkit_settings_set_enable_javascript(c->settings, cfg_js_default);
203 webkit_settings_set_enable_write_console_messages_to_stdout(
206 webkit_settings_set_enable_developer_extras(c->settings, TRUE);
208 g_signal_connect(G_OBJECT(c->web_view), "notify::favicon",
209 G_CALLBACK(changed_favicon), c);
210 g_signal_connect(G_OBJECT(c->web_view), "notify::title",
211 G_CALLBACK(changed_title), c);
212 g_signal_connect(G_OBJECT(c->web_view), "notify::uri",
213 G_CALLBACK(changed_uri), c);
214 g_signal_connect(G_OBJECT(c->web_view),
215 "notify::estimated-load-progress",
216 G_CALLBACK(changed_load_progress), c);
217 g_signal_connect(G_OBJECT(c->web_view), "create",
218 G_CALLBACK(client_new_request), NULL);
219 g_signal_connect(G_OBJECT(c->web_view), "close",
220 G_CALLBACK(client_destroy), c);
221 g_signal_connect(G_OBJECT(c->web_view), "decide-policy",
222 G_CALLBACK(decide_policy), NULL);
223 g_signal_connect(G_OBJECT(c->web_view), "key-press-event",
224 G_CALLBACK(key_web_view), c);
225 g_signal_connect(G_OBJECT(c->web_view), "button-release-event",
226 G_CALLBACK(key_web_view), c);
227 g_signal_connect(G_OBJECT(c->web_view), "scroll-event",
228 G_CALLBACK(key_web_view), c);
229 g_signal_connect(G_OBJECT(c->web_view), "mouse-target-changed",
230 G_CALLBACK(hover_web_view), c);
231 g_signal_connect(G_OBJECT(c->web_view), "web-process-crashed",
232 G_CALLBACK(crashed_web_view), c);
234 GtkWidget *locbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
235 c->jsbutton = gtk_toggle_button_new_with_label("JS");
236 gtk_widget_set_tooltip_text(c->jsbutton, "Toggle JavaScript execution");
237 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(c->jsbutton),
239 g_signal_connect(G_OBJECT(c->jsbutton), "toggled", G_CALLBACK(togglejs),
242 c->location = gtk_entry_new();
243 gtk_box_pack_start(GTK_BOX(locbox), c->location, TRUE, TRUE, 0);
246 GtkWidget *privindicator = gtk_label_new("Private mode");
247 gtk_widget_set_tooltip_text(
249 "You are in private mode. No history, caches, or "
250 "cookies will be saved beyond this session.");
251 gtk_box_pack_end(GTK_BOX(locbox), privindicator, FALSE, FALSE,
254 gtk_box_pack_start(GTK_BOX(locbox), c->jsbutton, FALSE, FALSE, 5);
256 g_signal_connect(G_OBJECT(c->location), "key-press-event",
257 G_CALLBACK(key_location), c);
258 g_signal_connect(G_OBJECT(c->location), "icon-release",
259 G_CALLBACK(icon_location), c);
261 * XXX This is a workaround. Setting this to NULL (which is done in
262 * grab_feeds_finished() if no feed has been detected) adds a little
263 * padding left of the text. Not sure why. The point of this call
264 * right here is to have that padding right from the start. This
265 * avoids a graphical artifact.
268 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
269 GTK_ENTRY_ICON_SECONDARY, NULL);
271 c->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
272 gtk_box_pack_start(GTK_BOX(c->vbox), locbox, FALSE, FALSE, 0);
273 gtk_box_pack_start(GTK_BOX(c->vbox), c->web_view, TRUE, TRUE, 0);
274 gtk_container_set_focus_child(GTK_CONTAINER(c->vbox), c->web_view);
276 c->tabicon = gtk_image_new_from_icon_name("text-html",
277 GTK_ICON_SIZE_SMALL_TOOLBAR);
279 c->tablabel = gtk_label_new("chorizo");
280 gtk_label_set_ellipsize(GTK_LABEL(c->tablabel), PANGO_ELLIPSIZE_END);
281 gtk_label_set_width_chars(GTK_LABEL(c->tablabel), 20);
282 gtk_widget_set_has_tooltip(c->tablabel, TRUE);
285 * XXX I don't own a HiDPI screen, so I don't know if scale_factor
286 * does the right thing.
288 tabbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL,
289 5 * gtk_widget_get_scale_factor(mw.win));
290 gtk_box_pack_start(GTK_BOX(tabbox), c->tabicon, FALSE, FALSE, 0);
291 gtk_box_pack_start(GTK_BOX(tabbox), c->tablabel, TRUE, TRUE, 0);
293 evbox = gtk_event_box_new();
294 gtk_container_add(GTK_CONTAINER(evbox), tabbox);
295 g_signal_connect(G_OBJECT(evbox), "button-release-event",
296 G_CALLBACK(key_tablabel), c);
298 gtk_widget_add_events(evbox, GDK_SCROLL_MASK);
299 g_signal_connect(G_OBJECT(evbox), "scroll-event",
300 G_CALLBACK(key_tablabel), c);
302 //For easy access, store a reference to our label.
303 g_object_set_data(G_OBJECT(evbox), "chorizo-tab-label", c->tablabel);
306 * This only shows the event box and the label inside, nothing else.
307 * Needed because the evbox/label is "internal" to the notebook and
308 * not part of the normal "widget tree" (IIUC).
310 gtk_widget_show_all(evbox);
312 int page = gtk_notebook_get_current_page(GTK_NOTEBOOK(mw.notebook)) + 1;
313 gtk_notebook_insert_page(GTK_NOTEBOOK(mw.notebook), c->vbox, evbox,
315 gtk_notebook_set_tab_reorderable(GTK_NOTEBOOK(mw.notebook), c->vbox,
319 show_web_view(NULL, c);
321 g_signal_connect(G_OBJECT(c->web_view), "ready-to-show",
322 G_CALLBACK(show_web_view), c);
325 f = ensure_uri_scheme(uri);
326 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
332 client_arr = realloc(client_arr, (clients + 1) * sizeof(client_arr[0]));
333 client_arr[clients] = c;
335 if (clients == 1 || uri == NULL)
336 gtk_widget_grab_focus(c->location);
338 return WEBKIT_WEB_VIEW(c->web_view);
342 client_new_request(WebKitWebView *web_view,
343 WebKitNavigationAction *navigation_action, gpointer data)
345 return client_new(NULL, web_view, FALSE, FALSE);
349 mkdirp(const char *dir, mode_t mode)
354 snprintf(tmp, sizeof(tmp), "%s", dir);
356 if (tmp[len - 1] == '/')
358 for (p = tmp + 1; *p; p++)
368 cooperation_setup(void)
373 gchar *priv = (cfg.private) ? "-private" : "";
374 const gchar *fifo_suffix_env = g_getenv("CHORIZO_FIFO_SUFFIX");
375 const gchar *fifo_suffix = (fifo_suffix_env) ? fifo_suffix_env : "";
376 fifofilename = g_strdup_printf("%s%s%s%s", "chorizo", priv, ".fifo",
378 fifopath = g_build_filename(g_get_user_runtime_dir(), "chorizo",
380 mkdirp(dirname(fifopath), 0600);
381 g_free(fifofilename);
383 if (!g_file_test(fifopath, G_FILE_TEST_EXISTS))
384 mkfifo(fifopath, 0600);
386 cooperative_pipe_fp = open(fifopath, O_WRONLY | O_NONBLOCK);
387 if (!cooperative_pipe_fp) {
388 fprintf(stderr, "chorizo: Can't open FIFO at all.\n");
390 if (write(cooperative_pipe_fp, "", 0) == -1) {
392 * Could not do an empty write to the FIFO which
393 * means there's no one listening.
395 close(cooperative_pipe_fp);
396 towatch = g_io_channel_new_file(fifopath, "r+", NULL);
397 g_io_add_watch(towatch, G_IO_IN, (GIOFunc)remote_msg,
400 cfg.cooperative_alone = FALSE;
406 changed_load_progress(GObject *obj, GParamSpec *pspec, gpointer data)
408 struct Client *c = (struct Client *)data;
411 "a = document.querySelectorAll('"
413 "link[rel=\"alternate\"][href][type=\"application/atom+xml\"],"
415 "link[rel=\"alternate\"][href][type=\"application/rss+xml\"]"
421 " for (i = 0; i < a.length; i++) {"
422 " url = encodeURIComponent(a[i].href);"
423 " if ('title' in a[i] && a[i].title != '')"
424 " title = encodeURIComponent(a[i].title);"
427 " out += '<li><a href=\"' + url + '\">' + title + "
432 p = webkit_web_view_get_estimated_load_progress(
433 WEBKIT_WEB_VIEW(c->web_view));
438 * The page has loaded fully. We now run the short JavaScript
439 * snippet above that operates on the DOM. It tries to grab
440 * all occurences of <link rel="alternate" ...>, i.e.
441 * RSS/Atom feed references.
443 webkit_web_view_run_javascript(WEBKIT_WEB_VIEW(c->web_view),
445 grab_feeds_finished, c);
447 gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), p);
451 changed_favicon(GObject *obj, GParamSpec *pspec, gpointer data)
453 struct Client *c = (struct Client *)data;
455 int w, h, w_should, h_should;
456 GdkPixbuf *pb, *pb_scaled;
457 f = webkit_web_view_get_favicon(WEBKIT_WEB_VIEW(c->web_view));
459 gtk_image_set_from_icon_name(GTK_IMAGE(c->tabicon), "text-html",
460 GTK_ICON_SIZE_SMALL_TOOLBAR);
462 w = cairo_image_surface_get_width(f);
463 h = cairo_image_surface_get_height(f);
464 pb = gdk_pixbuf_get_from_surface(f, 0, 0, w, h);
466 w_should = 16 * gtk_widget_get_scale_factor(c->tabicon);
467 h_should = 16 * gtk_widget_get_scale_factor(c->tabicon);
468 pb_scaled = gdk_pixbuf_scale_simple(
469 pb, w_should, h_should, GDK_INTERP_BILINEAR);
470 gtk_image_set_from_pixbuf(GTK_IMAGE(c->tabicon),
473 g_object_unref(pb_scaled);
480 changed_title(GObject *obj, GParamSpec *pspec, gpointer data)
483 struct Client *c = (struct Client *)data;
484 u = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
485 t = webkit_web_view_get_title(WEBKIT_WEB_VIEW(c->web_view));
487 u = u == NULL ? "chorizo" : u;
488 u = u[0] == 0 ? "chorizo" : u;
490 t = t == NULL ? u : t;
491 t = t[0] == 0 ? u : t;
493 gchar *name = malloc(strlen(t) + 4);
495 webkit_web_view_get_is_muted(WEBKIT_WEB_VIEW(c->web_view));
496 gchar *muted = (mute) ? "[m] " : "";
497 sprintf(name, "%s%s", muted, t);
498 gtk_label_set_text(GTK_LABEL(c->tablabel), name);
501 gtk_widget_set_tooltip_text(c->tablabel, t);
503 gtk_notebook_get_current_page(GTK_NOTEBOOK(mw.notebook)));
507 changed_uri(GObject *obj, GParamSpec *pspec, gpointer data)
510 struct Client *c = (struct Client *)data;
512 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
515 * When a web process crashes, we get a "notify::uri" signal, but we
516 * can no longer read a meaningful URI. It's just an empty string
517 * now. Not updating the location bar in this scenario is important,
518 * because we would override the "WEB PROCESS CRASHED" message.
520 if (t != NULL && strlen(t) > 0) {
523 //No g_get_user_state_dir unfortunately
524 gchar *state_env = getenv("XDG_STATE_DIR");
525 gchar *state_dir = (state_env) ?
527 g_build_filename(g_get_home_dir(),
531 gchar *history_file =
532 g_build_filename(state_dir, "history", NULL);
534 mkdirp(state_dir, 0700);
535 fp = fopen(history_file, "a");
537 fprintf(fp, "%s\n", t);
540 perror("chorizo: Error opening history file");
543 g_free(history_file);
548 crashed_web_view(WebKitWebView *web_view, gpointer data)
551 struct Client *c = (struct Client *)data;
552 t = g_strdup_printf("WEB PROCESS CRASHED: %s",
553 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(web_view)));
554 gtk_entry_set_text(GTK_ENTRY(c->location), t);
560 decide_policy(WebKitWebView *web_view, WebKitPolicyDecision *decision,
561 WebKitPolicyDecisionType type, gpointer data)
563 WebKitResponsePolicyDecision *r;
565 case WEBKIT_POLICY_DECISION_TYPE_RESPONSE:
566 r = WEBKIT_RESPONSE_POLICY_DECISION(decision);
567 if (!webkit_response_policy_decision_is_mime_type_supported(r))
568 webkit_policy_decision_download(decision);
570 webkit_policy_decision_use(decision);
573 //Use whatever default there is.
580 ensure_uri_scheme(const gchar *t)
583 f = g_ascii_strdown(t, -1);
584 if (!g_str_has_prefix(f, "http:") && !g_str_has_prefix(f, "https:") &&
585 !g_str_has_prefix(f, "file:") && !g_str_has_prefix(f, "about:") &&
586 !g_str_has_prefix(f, "data:") && !g_str_has_prefix(f, "webkit:")) {
588 fabs = realpath(t, NULL);
590 f = g_strdup_printf("file://%s", fabs);
593 f = g_strdup_printf("http://%s", t);
601 grab_feeds_finished(GObject *object, GAsyncResult *result, gpointer data)
603 struct Client *c = (struct Client *)data;
604 WebKitJavascriptResult *js_result;
606 JSCException *exception;
609 g_free(c->feed_html);
613 * This was taken almost verbatim from the example in WebKit's
616 * https://webkitgtk.org/reference/webkit2gtk/stable/WebKitWebView.html
619 js_result = webkit_web_view_run_javascript_finish(
620 WEBKIT_WEB_VIEW(object), result, &err);
622 fprintf(stderr, "chorizo: Error running javascript: %s\n",
627 value = webkit_javascript_result_get_js_value(js_result);
628 if (jsc_value_is_string(value)) {
629 str_value = jsc_value_to_string(value);
631 jsc_context_get_exception(jsc_value_get_context(value));
632 if (exception != NULL) {
634 "chorizo: Error running javascript: %s\n",
635 jsc_exception_get_message(exception));
637 c->feed_html = str_value;
640 gtk_entry_set_icon_from_icon_name(
641 GTK_ENTRY(c->location), GTK_ENTRY_ICON_SECONDARY,
642 "application-rss+xml-symbolic");
643 gtk_entry_set_icon_activatable(GTK_ENTRY(c->location),
644 GTK_ENTRY_ICON_SECONDARY, TRUE);
646 gtk_entry_set_icon_from_icon_name(
647 GTK_ENTRY(c->location), GTK_ENTRY_ICON_SECONDARY, NULL);
650 webkit_javascript_result_unref(js_result);
654 hover_web_view(WebKitWebView *web_view, WebKitHitTestResult *ht,
655 guint modifiers, gpointer data)
657 struct Client *c = (struct Client *)data;
659 g_free(c->hover_uri);
661 if (webkit_hit_test_result_context_is_link(ht)) {
662 to_show = webkit_hit_test_result_get_link_uri(ht);
663 c->hover_uri = g_strdup(to_show);
665 to_show = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
669 if (!gtk_widget_is_focus(c->location))
674 icon_location(GtkEntry *entry, GtkEntryIconPosition icon_pos, GdkEvent *event,
677 struct Client *c = (struct Client *)data;
679 gchar *data_template = "data:text/html,"
683 " <meta charset=\"UTF-8\">"
684 " <title>Feeds</title>"
687 " <p>Feeds found on this page:</p>"
693 if (c->feed_html != NULL) {
695 * What we're actually trying to do is show a simple HTML
696 * page that lists all the feeds on the current page. The
697 * function webkit_web_view_load_html() looks like the proper
698 * way to do that. Sad thing is, it doesn't create a history
699 * entry, but instead simply replaces the content of the
700 * current page. This is not what we want.
702 * RFC 2397 [0] defines the data URI scheme [1]. We abuse this
703 * mechanism to show my custom HTML snippet* and*create a
706 * [0]: https://tools.ietf.org/html/rfc2397 [1]:
707 * https://en.wikipedia.org/wiki/Data_URI_scheme
710 d = g_strdup_printf(data_template, c->feed_html);
711 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), d);
717 init_default_web_context(void)
720 WebKitWebContext *wc;
721 WebKitCookieManager *cm;
722 wc = (cfg.private) ? webkit_web_context_new_ephemeral() :
723 webkit_web_context_get_default();
725 p = g_build_filename(g_get_user_config_dir(), "chorizo", "adblock",
727 webkit_web_context_set_sandbox_enabled(wc, TRUE);
728 webkit_web_context_add_path_to_sandbox(wc, p, TRUE);
731 WebKitProcessModel model =
732 WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES;
733 webkit_web_context_set_process_model(wc, model);
735 p = g_build_filename(g_get_user_data_dir(), "chorizo", "web-extensions",
737 webkit_web_context_set_web_extensions_directory(wc, p);
740 char *xdg_down = getenv("XDG_DOWNLOAD_DIR");
741 g_signal_connect(G_OBJECT(wc), "download-started",
742 G_CALLBACK(download_start),
743 (xdg_down) ? xdg_down : "/var/tmp");
745 trust_user_certs(wc);
747 cm = webkit_web_context_get_cookie_manager(wc);
748 webkit_cookie_manager_set_accept_policy(cm, cfg_cookie_policy);
751 webkit_web_context_set_favicon_database_directory(wc, NULL);
753 gchar *fname = g_build_filename("/", g_get_user_data_dir(),
754 "chorizo", "cookies.db", NULL);
755 mkdirp(dirname(fname), 0700);
756 WebKitCookiePersistentStorage type =
757 WEBKIT_COOKIE_PERSISTENT_STORAGE_SQLITE;
758 webkit_cookie_manager_set_persistent_storage(cm, fname, type);
761 webkit_web_context_set_spell_checking_enabled(wc, TRUE);
765 search(gpointer data, gint direction)
767 struct Client *c = (struct Client *)data;
768 WebKitWebView *web_view = WEBKIT_WEB_VIEW(c->web_view);
769 WebKitFindController *fc =
770 webkit_web_view_get_find_controller(web_view);
771 if (search_text == NULL)
776 webkit_find_controller_search(
778 WEBKIT_FIND_OPTIONS_CASE_INSENSITIVE |
779 WEBKIT_FIND_OPTIONS_WRAP_AROUND,
783 webkit_find_controller_search_next(fc);
786 webkit_find_controller_search_previous(fc);
789 webkit_find_controller_search_finish(fc);
795 search_init(struct Client *c, int direction)
797 gtk_widget_grab_focus(c->location);
798 const gchar *contents = gtk_entry_get_text(GTK_ENTRY(c->location));
799 if (strcspn(contents, "s/")) {
800 gtk_entry_set_text(GTK_ENTRY(c->location), "s/");
801 gtk_editable_set_position(GTK_EDITABLE(c->location), -1);
805 search(c, direction);
809 key_common(GtkWidget *widget, GdkEvent *event, gpointer data)
811 struct Client *c = (struct Client *)data;
813 if (event->type == GDK_KEY_PRESS) {
814 if (((GdkEventKey *)event)->state & GDK_CONTROL_MASK) {
815 const char *uri = webkit_web_view_get_uri(
816 WEBKIT_WEB_VIEW(c->web_view));
817 int key = ((GdkEventKey *)event)->keyval;
818 if (GDK_KEY_y == key) {
819 downloadmanager_show();
821 } else if (GDK_KEY_h == key) {
822 webkit_web_view_go_back(
823 WEBKIT_WEB_VIEW(c->web_view));
825 } else if (GDK_KEY_l == key) {
826 webkit_web_view_go_forward(
827 WEBKIT_WEB_VIEW(c->web_view));
829 } else if (GDK_KEY_o == key) {
830 gtk_widget_grab_focus(c->location);
832 } else if (GDK_KEY_Print == key) {
833 WebKitPrintOperation *operation =
834 webkit_print_operation_new(
835 WEBKIT_WEB_VIEW(c->web_view));
836 GtkWidget *toplevel =
837 gtk_widget_get_toplevel(mw.win);
838 webkit_print_operation_run_dialog(
839 operation, GTK_WINDOW(toplevel));
841 } else if (GDK_KEY_g == key) {
843 gtk_widget_grab_focus(c->web_view);
844 gtk_editable_set_position(
845 GTK_EDITABLE(c->location), -1);
848 GTK_ENTRY(c->location), uri);
849 webkit_web_view_run_javascript(
850 WEBKIT_WEB_VIEW(c->web_view),
851 "window.getSelection().removeAllRanges();"
852 "document.activeElement.blur();",
855 } else if (GDK_KEY_r == key) {
856 webkit_web_view_reload_bypass_cache(
857 WEBKIT_WEB_VIEW(c->web_view));
859 } else if (GDK_KEY_j == key) {
860 for (int i = 0; i <= cfg_scroll_lines - 1;
862 event->key.keyval = GDK_KEY_Down;
863 gdk_event_put(event);
866 } else if (GDK_KEY_k == key) {
867 for (int i = 0; i <= cfg_scroll_lines - 1;
869 event->key.keyval = GDK_KEY_Up;
870 gdk_event_put(event);
873 } else if (GDK_KEY_f == key) {
874 event->key.keyval = GDK_KEY_Page_Down;
875 gdk_event_put(event);
877 } else if (GDK_KEY_b == key) {
878 event->key.keyval = GDK_KEY_Page_Up;
879 gdk_event_put(event);
881 } else if (GDK_KEY_s == key) {
884 } else if (GDK_KEY_r == key) {
887 } else if (GDK_KEY_q == key) {
888 client_destroy(NULL, c);
890 } else if (GDK_KEY_1 == key) {
891 gtk_notebook_set_current_page(
892 GTK_NOTEBOOK(mw.notebook), 0);
894 } else if (GDK_KEY_2 == key) {
895 gtk_notebook_set_current_page(
896 GTK_NOTEBOOK(mw.notebook), 1);
898 } else if (GDK_KEY_3 == key) {
899 gtk_notebook_set_current_page(
900 GTK_NOTEBOOK(mw.notebook), 2);
902 } else if (GDK_KEY_4 == key) {
903 gtk_notebook_set_current_page(
904 GTK_NOTEBOOK(mw.notebook), 3);
906 } else if (GDK_KEY_5 == key) {
907 gtk_notebook_set_current_page(
908 GTK_NOTEBOOK(mw.notebook), 4);
910 } else if (GDK_KEY_6 == key) {
911 gtk_notebook_set_current_page(
912 GTK_NOTEBOOK(mw.notebook), 5);
914 } else if (GDK_KEY_7 == key) {
915 gtk_notebook_set_current_page(
916 GTK_NOTEBOOK(mw.notebook), 6);
918 } else if (GDK_KEY_8 == key) {
919 gtk_notebook_set_current_page(
920 GTK_NOTEBOOK(mw.notebook), 7);
922 } else if (GDK_KEY_9 == key) {
923 gtk_notebook_set_current_page(
924 GTK_NOTEBOOK(mw.notebook), 8);
926 } else if (GDK_KEY_u == key) {
927 gtk_notebook_prev_page(
928 GTK_NOTEBOOK(mw.notebook));
930 } else if (GDK_KEY_m == key) {
931 gboolean muted = webkit_web_view_get_is_muted(
932 WEBKIT_WEB_VIEW(c->web_view));
933 webkit_web_view_set_is_muted(
934 WEBKIT_WEB_VIEW(c->web_view), !muted);
935 changed_title(G_OBJECT(c->web_view), NULL, c);
937 } else if (GDK_KEY_t == key) {
938 client_new(cfg_home_uri, NULL, TRUE, TRUE);
940 } else if (GDK_KEY_bracketleft == key) {
943 int len = sizeof(closed_tabs) / sizeof(char *);
946 client_new(closed_tabs[len], NULL, TRUE, TRUE);
947 free(closed_tabs[len]);
948 closed_tabs = (char **)realloc(
950 (len - 1) * sizeof(closed_tabs[0]));
952 } else if (GDK_KEY_i == key) {
953 gtk_notebook_next_page(
954 GTK_NOTEBOOK(mw.notebook));
956 } else if (GDK_KEY_p == key) {
958 webkit_settings_get_enable_javascript(
960 webkit_settings_set_enable_javascript(
962 webkit_web_view_set_settings(
963 WEBKIT_WEB_VIEW(c->web_view),
965 gtk_toggle_button_set_active(
966 GTK_TOGGLE_BUTTON(c->jsbutton), !on);
967 } else if (GDK_KEY_d == key) {
968 gtk_widget_grab_focus(c->location);
969 gtk_entry_set_text(GTK_ENTRY(c->location),
971 gtk_editable_set_position(
972 GTK_EDITABLE(c->location), -1);
974 } else if (GDK_KEY_equal == key) {
975 now = webkit_web_view_get_zoom_level(
976 WEBKIT_WEB_VIEW(c->web_view));
977 webkit_web_view_set_zoom_level(
978 WEBKIT_WEB_VIEW(c->web_view),
981 } else if (GDK_KEY_minus == key) {
982 now = webkit_web_view_get_zoom_level(
983 WEBKIT_WEB_VIEW(c->web_view));
984 webkit_web_view_set_zoom_level(
985 WEBKIT_WEB_VIEW(c->web_view),
988 } else if (GDK_KEY_0 == key) {
989 webkit_web_view_set_zoom_level(
990 WEBKIT_WEB_VIEW(c->web_view), 1);
998 key_location(GtkWidget *widget, GdkEvent *event, gpointer data)
1000 struct Client *c = (struct Client *)data;
1002 if (key_common(widget, event, data))
1005 if (event->type == GDK_KEY_PRESS) {
1006 int key = ((GdkEventKey *)event)->keyval;
1007 if ((GDK_KEY_KP_Enter == key) || (GDK_KEY_Return == key)) {
1008 gtk_widget_grab_focus(c->web_view);
1009 t = gtk_entry_get_text(GTK_ENTRY(c->location));
1010 if (t != NULL && t[0] == 's' && t[1] == '/') {
1011 if (search_text != NULL)
1012 g_free(search_text);
1013 search_text = g_strdup(t + 2);
1015 } else if (t != NULL && t[0] == 'w' && t[1] == '/') {
1016 int len = strlen(cfg_search_engine) +
1018 gchar *f = malloc(len + 1);
1019 snprintf(f, len + 1, "%s%s", cfg_search_engine,
1021 webkit_web_view_load_uri(
1022 WEBKIT_WEB_VIEW(c->web_view), f);
1025 webkit_web_view_load_uri(
1026 WEBKIT_WEB_VIEW(c->web_view),
1027 ensure_uri_scheme(t));
1030 } else if (GDK_KEY_Escape == key) {
1031 t = webkit_web_view_get_uri(
1032 WEBKIT_WEB_VIEW(c->web_view));
1033 gtk_entry_set_text(GTK_ENTRY(c->location),
1034 (t == NULL) ? "" : t);
1041 key_tablabel(GtkWidget *widget, GdkEvent *event, gpointer data)
1043 GdkScrollDirection direction;
1044 if (event->type == GDK_BUTTON_RELEASE) {
1045 switch (((GdkEventButton *)event)->button) {
1047 client_destroy(NULL, data);
1050 } else if (event->type == GDK_SCROLL) {
1051 gdk_event_get_scroll_direction(event, &direction);
1052 switch (direction) {
1054 gtk_notebook_prev_page(GTK_NOTEBOOK(mw.notebook));
1056 case GDK_SCROLL_DOWN:
1057 gtk_notebook_next_page(GTK_NOTEBOOK(mw.notebook));
1067 key_web_view(GtkWidget *widget, GdkEvent *event, gpointer data)
1069 struct Client *c = (struct Client *)data;
1072 if (key_common(widget, event, data))
1075 if (event->type == GDK_KEY_PRESS) {
1076 if (((GdkEventKey *)event)->keyval == GDK_KEY_Escape) {
1077 webkit_web_view_stop_loading(
1078 WEBKIT_WEB_VIEW(c->web_view));
1079 gtk_entry_set_progress_fraction(GTK_ENTRY(c->location),
1082 } else if (event->type == GDK_BUTTON_RELEASE) {
1083 GdkModifierType modifiers =
1084 gtk_accelerator_get_default_mod_mask();
1085 switch (((GdkEventButton *)event)->button) {
1087 if ((((GdkEventButton *)event)->state & modifiers) ==
1089 c->hover_uri != NULL) {
1090 client_new(c->hover_uri, NULL, TRUE, FALSE);
1095 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
1098 webkit_web_view_go_forward(
1099 WEBKIT_WEB_VIEW(c->web_view));
1102 } else if (event->type == GDK_SCROLL) {
1103 event->scroll.delta_y *= cfg_scroll_lines;
1104 if (((GdkEventScroll *)event)->state & GDK_CONTROL_MASK) {
1105 gdk_event_get_scroll_deltas(event, &dx, &dy);
1106 z = webkit_web_view_get_zoom_level(
1107 WEBKIT_WEB_VIEW(c->web_view));
1109 z = dx != 0 ? 1 : z;
1110 webkit_web_view_set_zoom_level(
1111 WEBKIT_WEB_VIEW(c->web_view), z);
1119 mainwindow_setup(void)
1121 mw.win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1122 gtk_window_set_default_size(GTK_WINDOW(mw.win), 800, 600);
1123 g_signal_connect(G_OBJECT(mw.win), "destroy", gtk_main_quit, NULL);
1125 gchar *priv = (cfg.private) ? "-private" : "";
1126 gchar *title = malloc(strlen(priv) + 7);
1127 sprintf(title, "%s%s", "chorizo", priv);
1128 gtk_window_set_title(GTK_WINDOW(mw.win), title);
1131 mw.notebook = gtk_notebook_new();
1132 gtk_notebook_set_scrollable(GTK_NOTEBOOK(mw.notebook), TRUE);
1133 gtk_container_add(GTK_CONTAINER(mw.win), mw.notebook);
1134 g_signal_connect(G_OBJECT(mw.notebook), "switch-page",
1135 G_CALLBACK(notebook_switch_page), NULL);
1139 mainwindow_title(gint idx)
1141 GtkWidget *child, *widg, *tablabel;
1143 child = gtk_notebook_get_nth_page(GTK_NOTEBOOK(mw.notebook), idx);
1147 widg = gtk_notebook_get_tab_label(GTK_NOTEBOOK(mw.notebook), child);
1148 tablabel = (GtkWidget *)g_object_get_data(G_OBJECT(widg),
1149 "chorizo-tab-label");
1150 text = gtk_label_get_text(GTK_LABEL(tablabel));
1151 gtk_window_set_title(GTK_WINDOW(mw.win), text);
1155 notebook_switch_page(GtkNotebook *nb, GtkWidget *p, guint idx, gpointer data)
1157 mainwindow_title(idx);
1160 quit_if_nothing_active(void)
1163 if (downloads == 0) {
1167 downloadmanager_show();
1173 remote_msg(GIOChannel *channel, GIOCondition condition, gpointer data)
1176 g_io_channel_read_line(channel, &uri, NULL, NULL, NULL);
1179 client_new(uri, NULL, TRUE, TRUE);
1186 show_web_view(WebKitWebView *web_view, gpointer data)
1188 struct Client *c = (struct Client *)data;
1192 gtk_widget_show_all(mw.win);
1194 if (c->focus_new_tab) {
1195 idx = gtk_notebook_page_num(GTK_NOTEBOOK(mw.notebook), c->vbox);
1197 gtk_notebook_set_current_page(GTK_NOTEBOOK(mw.notebook),
1200 gtk_widget_grab_focus(c->web_view);
1205 trust_user_certs(WebKitWebContext *wc)
1207 GTlsCertificate *cert;
1208 gchar *basedir, *absfile;
1211 basedir = g_build_filename(g_get_user_data_dir(), "chorizo", "certs",
1213 dir = g_dir_open(basedir, 0, NULL);
1216 file = g_dir_read_name(dir);
1217 while (file != NULL) {
1218 absfile = g_build_filename(g_get_user_data_dir(),
1219 "chorizo", "certs", file,
1221 cert = g_tls_certificate_new_from_file(absfile, NULL);
1225 "chorizo: Could not load trusted cert '%s'\n",
1228 webkit_web_context_allow_tls_certificate_for_host(
1230 file = g_dir_read_name(dir);
1237 main(int argc, char **argv)
1242 cfg.noncooperative_instances = FALSE;
1243 cfg.cooperative_alone = TRUE;
1245 while ((opt = getopt(argc, argv, "cpvV")) != -1) {
1248 cfg.noncooperative_instances = TRUE;
1257 printf("%s %s\n", "chorizo", VERSION);
1261 "Usage: chorizo [OPTION]... [URI]...\n");
1266 gtk_init(&argc, &argv);
1268 //Keep clipboard contents after program closes
1269 gtk_clipboard_store(gtk_clipboard_get_for_display(
1270 gdk_display_get_default(), GDK_SELECTION_CLIPBOARD));
1272 if (!cfg.noncooperative_instances)
1273 cooperation_setup();
1275 if (cfg.noncooperative_instances || cfg.cooperative_alone)
1276 init_default_web_context();
1278 downloadmanager_setup();
1281 client_arr = malloc(sizeof(struct Client *));
1282 if (optind >= argc) {
1283 client_new(cfg_home_uri, NULL, TRUE, TRUE);
1285 for (i = optind; i < argc; i++)
1286 client_new(argv[i], NULL, TRUE, TRUE);
1289 if (cfg.noncooperative_instances || cfg.cooperative_alone) {
1293 for (int i = 0; i < clients; i++) {
1294 free(&(client_arr[i]));