11 #include <gdk/gdkkeysyms.h>
13 #include <webkit2/webkit2.h>
14 #include <JavaScriptCore/JavaScript.h>
17 gboolean button_tablabel(GtkWidget *, GdkEvent *, gpointer);
18 void client_destroy(GtkWidget *, gpointer);
19 WebKitWebView *client_new(const gchar *, WebKitWebView *, gboolean,
21 WebKitWebView *client_new_request(WebKitWebView *, WebKitNavigationAction *,
23 void cooperation_setup(void);
24 void changed_download_progress(GObject *, GParamSpec *, gpointer);
25 void changed_load_progress(GObject *, GParamSpec *, gpointer);
26 void changed_favicon(GObject *, GParamSpec *, gpointer);
27 void changed_title(GObject *, GParamSpec *, gpointer);
28 void changed_uri(GObject *, GParamSpec *, gpointer);
29 gboolean crashed_web_view(WebKitWebView *, gpointer);
30 gboolean decide_policy(WebKitWebView *, WebKitPolicyDecision *,
31 WebKitPolicyDecisionType, gpointer);
32 gboolean download_handle(WebKitDownload *, gchar *, gpointer);
33 void download_handle_start(WebKitWebView *, WebKitDownload *, gpointer);
34 void downloadmanager_cancel(GtkToolButton *, gpointer);
35 gboolean downloadmanager_delete(GtkWidget *, gpointer);
36 void downloadmanager_setup(void);
37 gchar *ensure_uri_scheme(const gchar *);
38 void external_handler_run(GSimpleAction *, GVariant *, gpointer);
39 void grab_environment_configuration(void);
40 void grab_feeds_finished(GObject *, GAsyncResult *, gpointer);
41 void hover_web_view(WebKitWebView *, WebKitHitTestResult *, guint, gpointer);
42 void icon_location(GtkEntry *, GtkEntryIconPosition, GdkEvent *, gpointer);
43 gboolean key_common(GtkWidget *, GdkEvent *, gpointer);
44 gboolean key_downloadmanager(GtkWidget *, GdkEvent *, gpointer);
45 gboolean key_location(GtkWidget *, GdkEvent *, gpointer);
46 gboolean key_web_view(GtkWidget *, GdkEvent *, gpointer);
47 void mainwindow_setup(void);
48 gboolean menu_web_view(WebKitWebView *, WebKitContextMenu *, GdkEvent *,
49 WebKitHitTestResult *, gpointer);
50 gboolean quit_if_nothing_active(void);
51 gboolean remote_msg(GIOChannel *, GIOCondition, gpointer);
52 void run_user_scripts(WebKitWebView *);
53 void search(gpointer, gint);
54 void show_web_view(WebKitWebView *, gpointer);
55 void trust_user_certs(WebKitWebContext *);
60 gchar *external_handler_uri;
68 gboolean focus_new_tab;
77 struct DownloadManager
85 const gchar *accepted_language[2] = { NULL, NULL };
86 gint clients = 0, downloads = 0;
87 gboolean cooperative_alone = TRUE;
88 gboolean cooperative_instances = TRUE;
89 int cooperative_pipe_fp = 0;
90 gchar *download_dir = "/var/tmp";
91 gboolean enable_console_to_stdout = FALSE;
92 gchar *fifo_suffix = "main";
93 gdouble global_zoom = 1.0;
94 gchar *history_file = NULL;
95 gchar *home_uri = "about:blank";
96 gboolean initial_wc_setup_done = FALSE;
97 gchar *search_text = NULL;
98 GtkPositionType tab_pos = GTK_POS_TOP;
99 gint tab_width_chars = 20;
100 gchar *user_agent = NULL;
104 button_tablabel(GtkWidget *widget, GdkEvent *event, gpointer data)
106 if (event->type == GDK_BUTTON_RELEASE)
108 switch (((GdkEventButton *)event)->button)
111 client_destroy(NULL, data);
119 client_destroy(GtkWidget *widget, gpointer data)
121 struct Client *c = (struct Client *)data;
124 g_signal_handlers_disconnect_by_func(G_OBJECT(c->web_view),
125 changed_load_progress, c);
127 idx = gtk_notebook_page_num(GTK_NOTEBOOK(mw.notebook), c->vbox);
129 fprintf(stderr, __NAME__": Tab index was -1, bamboozled\n");
131 gtk_notebook_remove_page(GTK_NOTEBOOK(mw.notebook), idx);
136 quit_if_nothing_active();
140 client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show,
144 WebKitWebContext *wc;
146 GtkWidget *evbox, *tabbox;
148 if (uri != NULL && cooperative_instances && !cooperative_alone)
150 f = ensure_uri_scheme(uri);
151 write(cooperative_pipe_fp, f, strlen(f));
152 write(cooperative_pipe_fp, "\n", 1);
157 c = calloc(1, sizeof(struct Client));
160 fprintf(stderr, __NAME__": fatal: calloc failed\n");
164 c->focus_new_tab = focus_tab;
166 if (related_wv == NULL)
167 c->web_view = webkit_web_view_new();
169 c->web_view = webkit_web_view_new_with_related_view(related_wv);
170 wc = webkit_web_view_get_context(WEBKIT_WEB_VIEW(c->web_view));
172 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), global_zoom);
173 g_signal_connect(G_OBJECT(c->web_view), "notify::favicon",
174 G_CALLBACK(changed_favicon), c);
175 g_signal_connect(G_OBJECT(c->web_view), "notify::title",
176 G_CALLBACK(changed_title), c);
177 g_signal_connect(G_OBJECT(c->web_view), "notify::uri",
178 G_CALLBACK(changed_uri), c);
179 g_signal_connect(G_OBJECT(c->web_view), "notify::estimated-load-progress",
180 G_CALLBACK(changed_load_progress), c);
181 g_signal_connect(G_OBJECT(c->web_view), "create",
182 G_CALLBACK(client_new_request), NULL);
183 g_signal_connect(G_OBJECT(c->web_view), "context-menu",
184 G_CALLBACK(menu_web_view), c);
185 g_signal_connect(G_OBJECT(c->web_view), "close",
186 G_CALLBACK(client_destroy), c);
187 g_signal_connect(G_OBJECT(c->web_view), "decide-policy",
188 G_CALLBACK(decide_policy), NULL);
189 g_signal_connect(G_OBJECT(c->web_view), "key-press-event",
190 G_CALLBACK(key_web_view), c);
191 g_signal_connect(G_OBJECT(c->web_view), "button-release-event",
192 G_CALLBACK(key_web_view), c);
193 g_signal_connect(G_OBJECT(c->web_view), "scroll-event",
194 G_CALLBACK(key_web_view), c);
195 g_signal_connect(G_OBJECT(c->web_view), "mouse-target-changed",
196 G_CALLBACK(hover_web_view), c);
197 g_signal_connect(G_OBJECT(c->web_view), "web-process-crashed",
198 G_CALLBACK(crashed_web_view), c);
200 if (!initial_wc_setup_done)
202 if (accepted_language[0] != NULL)
203 webkit_web_context_set_preferred_languages(wc, accepted_language);
205 g_signal_connect(G_OBJECT(wc), "download-started",
206 G_CALLBACK(download_handle_start), NULL);
208 trust_user_certs(wc);
210 webkit_web_context_set_favicon_database_directory(wc, NULL);
212 initial_wc_setup_done = TRUE;
215 if (user_agent != NULL)
216 g_object_set(G_OBJECT(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view))),
217 "user-agent", user_agent, NULL);
219 if (enable_console_to_stdout)
220 webkit_settings_set_enable_write_console_messages_to_stdout(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view)), TRUE);
222 webkit_settings_set_enable_developer_extras(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view)), TRUE);
224 c->location = gtk_entry_new();
225 g_signal_connect(G_OBJECT(c->location), "key-press-event",
226 G_CALLBACK(key_location), c);
227 g_signal_connect(G_OBJECT(c->location), "icon-release",
228 G_CALLBACK(icon_location), c);
229 /* XXX This is a workaround. Setting this to NULL (which is done in
230 * grab_feeds_finished() if no feed has been detected) adds a little
231 * padding left of the text. Not sure why. The point of this call
232 * right here is to have that padding right from the start. This
233 * avoids a graphical artifact. */
234 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
235 GTK_ENTRY_ICON_PRIMARY,
238 c->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
239 gtk_box_pack_start(GTK_BOX(c->vbox), c->location, FALSE, FALSE, 0);
240 gtk_box_pack_start(GTK_BOX(c->vbox), c->web_view, TRUE, TRUE, 0);
242 c->tabicon = gtk_image_new_from_icon_name("text-html", GTK_ICON_SIZE_SMALL_TOOLBAR);
244 c->tablabel = gtk_label_new(__NAME__);
245 gtk_label_set_ellipsize(GTK_LABEL(c->tablabel), PANGO_ELLIPSIZE_END);
246 gtk_label_set_width_chars(GTK_LABEL(c->tablabel), tab_width_chars);
247 gtk_widget_set_has_tooltip(c->tablabel, TRUE);
249 /* XXX I don't own a HiDPI screen, so I don't know if scale_factor
250 * does the right thing. */
251 tabbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL,
252 5 * gtk_widget_get_scale_factor(mw.win));
253 gtk_box_pack_start(GTK_BOX(tabbox), c->tabicon, FALSE, FALSE, 0);
254 gtk_box_pack_start(GTK_BOX(tabbox), c->tablabel, TRUE, TRUE, 0);
256 evbox = gtk_event_box_new();
257 gtk_container_add(GTK_CONTAINER(evbox), tabbox);
258 g_signal_connect(G_OBJECT(evbox), "button-release-event",
259 G_CALLBACK(button_tablabel), c);
261 /* This only shows the event box and the label inside, nothing else.
262 * Needed because the evbox/label is "internal" to the notebook and
263 * not part of the normal "widget tree" (IIUC). */
264 gtk_widget_show_all(evbox);
266 gtk_notebook_insert_page(GTK_NOTEBOOK(mw.notebook), c->vbox, evbox,
267 gtk_notebook_get_current_page(GTK_NOTEBOOK(mw.notebook)) + 1);
268 gtk_notebook_set_tab_reorderable(GTK_NOTEBOOK(mw.notebook), c->vbox, TRUE);
271 show_web_view(NULL, c);
273 g_signal_connect(G_OBJECT(c->web_view), "ready-to-show",
274 G_CALLBACK(show_web_view), c);
278 f = ensure_uri_scheme(uri);
279 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
285 return WEBKIT_WEB_VIEW(c->web_view);
289 client_new_request(WebKitWebView *web_view,
290 WebKitNavigationAction *navigation_action, gpointer data)
292 return client_new(NULL, web_view, FALSE, FALSE);
296 cooperation_setup(void)
299 gchar *fifofilename, *fifopath;
301 fifofilename = g_strdup_printf("%s-%s", __NAME__".fifo", fifo_suffix);
302 fifopath = g_build_filename(g_get_user_runtime_dir(), fifofilename, NULL);
303 g_free(fifofilename);
305 if (!g_file_test(fifopath, G_FILE_TEST_EXISTS))
306 mkfifo(fifopath, 0600);
308 cooperative_pipe_fp = open(fifopath, O_WRONLY | O_NONBLOCK);
309 if (!cooperative_pipe_fp)
311 fprintf(stderr, __NAME__": Can't open FIFO at all.\n");
315 if (write(cooperative_pipe_fp, "", 0) == -1)
317 /* Could not do an empty write to the FIFO which means there's
318 * no one listening. */
319 close(cooperative_pipe_fp);
320 towatch = g_io_channel_new_file(fifopath, "r+", NULL);
321 g_io_add_watch(towatch, G_IO_IN, (GIOFunc)remote_msg, NULL);
324 cooperative_alone = FALSE;
331 changed_download_progress(GObject *obj, GParamSpec *pspec, gpointer data)
333 WebKitDownload *download = WEBKIT_DOWNLOAD(obj);
334 WebKitURIResponse *resp;
335 GtkToolItem *tb = GTK_TOOL_ITEM(data);
338 gchar *t, *filename, *base;
340 p = webkit_download_get_estimated_progress(download);
344 resp = webkit_download_get_response(download);
345 size_mb = webkit_uri_response_get_content_length(resp) / 1e6;
347 uri = webkit_download_get_destination(download);
348 filename = g_filename_from_uri(uri, NULL, NULL);
349 if (filename == NULL)
351 /* This really should not happen because WebKit uses that URI to
352 * write to a file... */
353 fprintf(stderr, __NAME__": Could not construct file name from URI!\n");
354 t = g_strdup_printf("%s (%.0f%% of %.1f MB)",
355 webkit_uri_response_get_uri(resp), p, size_mb);
359 base = g_path_get_basename(filename);
360 t = g_strdup_printf("%s (%.0f%% of %.1f MB)", base, p, size_mb);
364 gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), t);
369 changed_load_progress(GObject *obj, GParamSpec *pspec, gpointer data)
371 struct Client *c = (struct Client *)data;
374 "a = document.querySelectorAll('"
375 " html > head > link[rel=\"alternate\"][href][type=\"application/atom+xml\"],"
376 " html > head > link[rel=\"alternate\"][href][type=\"application/rss+xml\"]"
383 " for (i = 0; i < a.length; i++)"
385 " url = encodeURIComponent(a[i].href);"
386 " if ('title' in a[i] && a[i].title != '')"
387 " title = encodeURIComponent(a[i].title);"
390 " out += '<li><a href=\"' + url + '\">' + title + '</a></li>';"
395 p = webkit_web_view_get_estimated_load_progress(WEBKIT_WEB_VIEW(c->web_view));
400 /* The page has loaded fully. We now run the short JavaScript
401 * snippet above that operates on the DOM. It tries to grab all
402 * occurences of <link rel="alternate" ...>, i.e. RSS/Atom feed
404 webkit_web_view_run_javascript(WEBKIT_WEB_VIEW(c->web_view),
406 grab_feeds_finished, c);
408 run_user_scripts(WEBKIT_WEB_VIEW(c->web_view));
410 gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), p);
414 changed_favicon(GObject *obj, GParamSpec *pspec, gpointer data)
416 struct Client *c = (struct Client *)data;
418 int w, h, w_should, h_should;
419 GdkPixbuf *pb, *pb_scaled;
421 f = webkit_web_view_get_favicon(WEBKIT_WEB_VIEW(c->web_view));
424 gtk_image_set_from_icon_name(GTK_IMAGE(c->tabicon), "text-html",
425 GTK_ICON_SIZE_SMALL_TOOLBAR);
429 w = cairo_image_surface_get_width(f);
430 h = cairo_image_surface_get_height(f);
431 pb = gdk_pixbuf_get_from_surface(f, 0, 0, w, h);
434 w_should = 16 * gtk_widget_get_scale_factor(c->tabicon);
435 h_should = 16 * gtk_widget_get_scale_factor(c->tabicon);
436 pb_scaled = gdk_pixbuf_scale_simple(pb, w_should, h_should,
437 GDK_INTERP_BILINEAR);
438 gtk_image_set_from_pixbuf(GTK_IMAGE(c->tabicon), pb_scaled);
440 g_object_unref(pb_scaled);
447 changed_title(GObject *obj, GParamSpec *pspec, gpointer data)
450 struct Client *c = (struct Client *)data;
452 u = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
453 t = webkit_web_view_get_title(WEBKIT_WEB_VIEW(c->web_view));
455 u = u == NULL ? __NAME__ : u;
456 u = u[0] == 0 ? __NAME__ : u;
458 t = t == NULL ? u : t;
459 t = t[0] == 0 ? u : t;
461 gtk_label_set_text(GTK_LABEL(c->tablabel), t);
462 gtk_widget_set_tooltip_text(c->tablabel, t);
466 changed_uri(GObject *obj, GParamSpec *pspec, gpointer data)
469 struct Client *c = (struct Client *)data;
472 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
474 /* When a web process crashes, we get a "notify::uri" signal, but we
475 * can no longer read a meaningful URI. It's just an empty string
476 * now. Not updating the location bar in this scenario is important,
477 * because we would override the "WEB PROCESS CRASHED" message. */
478 if (t != NULL && strlen(t) > 0)
480 gtk_entry_set_text(GTK_ENTRY(c->location), t);
482 if (history_file != NULL)
484 fp = fopen(history_file, "a");
487 fprintf(fp, "%s\n", t);
491 perror(__NAME__": Error opening history file");
497 crashed_web_view(WebKitWebView *web_view, gpointer data)
500 struct Client *c = (struct Client *)data;
502 t = g_strdup_printf("WEB PROCESS CRASHED: %s",
503 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(web_view)));
504 gtk_entry_set_text(GTK_ENTRY(c->location), t);
511 decide_policy(WebKitWebView *web_view, WebKitPolicyDecision *decision,
512 WebKitPolicyDecisionType type, gpointer data)
514 WebKitResponsePolicyDecision *r;
518 case WEBKIT_POLICY_DECISION_TYPE_RESPONSE:
519 r = WEBKIT_RESPONSE_POLICY_DECISION(decision);
520 if (!webkit_response_policy_decision_is_mime_type_supported(r))
521 webkit_policy_decision_download(decision);
523 webkit_policy_decision_use(decision);
526 /* Use whatever default there is. */
533 download_handle_finished(WebKitDownload *download, gpointer data)
539 download_handle_start(WebKitWebView *web_view, WebKitDownload *download,
542 g_signal_connect(G_OBJECT(download), "decide-destination",
543 G_CALLBACK(download_handle), data);
547 download_handle(WebKitDownload *download, gchar *suggested_filename, gpointer data)
549 gchar *sug_clean, *path, *path2 = NULL, *uri;
554 sug_clean = g_strdup(suggested_filename);
555 for (i = 0; i < strlen(sug_clean); i++)
556 if (sug_clean[i] == G_DIR_SEPARATOR)
559 path = g_build_filename(download_dir, sug_clean, NULL);
560 path2 = g_strdup(path);
561 while (g_file_test(path2, G_FILE_TEST_EXISTS) && suffix < 1000)
565 path2 = g_strdup_printf("%s.%d", path, suffix);
571 fprintf(stderr, __NAME__": Suffix reached limit for download.\n");
572 webkit_download_cancel(download);
576 uri = g_filename_to_uri(path2, NULL, NULL);
577 webkit_download_set_destination(download, uri);
580 tb = gtk_tool_button_new(NULL, NULL);
581 gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(tb), "gtk-delete");
582 gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), sug_clean);
583 gtk_toolbar_insert(GTK_TOOLBAR(dm.toolbar), tb, 0);
584 gtk_widget_show_all(dm.win);
586 g_signal_connect(G_OBJECT(download), "notify::estimated-progress",
587 G_CALLBACK(changed_download_progress), tb);
590 g_signal_connect(G_OBJECT(download), "finished",
591 G_CALLBACK(download_handle_finished), NULL);
593 g_object_ref(download);
594 g_signal_connect(G_OBJECT(tb), "clicked",
595 G_CALLBACK(downloadmanager_cancel), download);
602 /* Propagate -- to whom it may concern. */
607 downloadmanager_cancel(GtkToolButton *tb, gpointer data)
609 WebKitDownload *download = WEBKIT_DOWNLOAD(data);
611 webkit_download_cancel(download);
612 g_object_unref(download);
614 gtk_widget_destroy(GTK_WIDGET(tb));
618 downloadmanager_delete(GtkWidget *obj, gpointer data)
620 if (!quit_if_nothing_active())
621 gtk_widget_hide(dm.win);
627 downloadmanager_setup(void)
629 dm.win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
630 gtk_window_set_type_hint(GTK_WINDOW(dm.win), GDK_WINDOW_TYPE_HINT_DIALOG);
631 gtk_window_set_default_size(GTK_WINDOW(dm.win), 500, 250);
632 gtk_window_set_title(GTK_WINDOW(dm.win), __NAME__" - Download Manager");
633 g_signal_connect(G_OBJECT(dm.win), "delete-event",
634 G_CALLBACK(downloadmanager_delete), NULL);
635 g_signal_connect(G_OBJECT(dm.win), "key-press-event",
636 G_CALLBACK(key_downloadmanager), NULL);
638 dm.toolbar = gtk_toolbar_new();
639 gtk_orientable_set_orientation(GTK_ORIENTABLE(dm.toolbar),
640 GTK_ORIENTATION_VERTICAL);
641 gtk_toolbar_set_style(GTK_TOOLBAR(dm.toolbar), GTK_TOOLBAR_BOTH_HORIZ);
642 gtk_toolbar_set_show_arrow(GTK_TOOLBAR(dm.toolbar), FALSE);
644 dm.scroll = gtk_scrolled_window_new(NULL, NULL);
645 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(dm.scroll),
646 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
647 gtk_container_add(GTK_CONTAINER(dm.scroll), dm.toolbar);
649 gtk_container_add(GTK_CONTAINER(dm.win), dm.scroll);
653 ensure_uri_scheme(const gchar *t)
657 f = g_ascii_strdown(t, -1);
658 if (!g_str_has_prefix(f, "http:") &&
659 !g_str_has_prefix(f, "https:") &&
660 !g_str_has_prefix(f, "file:") &&
661 !g_str_has_prefix(f, "about:") &&
662 !g_str_has_prefix(f, "data:"))
665 fabs = realpath(t, NULL);
668 f = g_strdup_printf("file://%s", fabs);
672 f = g_strdup_printf("http://%s", t);
680 external_handler_run(GSimpleAction *simple, GVariant *param, gpointer data)
682 struct Client *c = (struct Client *)data;
683 gchar *argv[] = { "lariza-external-handler", "-u", NULL, NULL };
690 argv[2] = c->external_handler_uri;
691 if (!g_spawn_async(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL,
694 fprintf(stderr, __NAME__": Could not launch key handler: %s\n",
699 g_spawn_close_pid(pid);
703 grab_environment_configuration(void)
707 e = g_getenv(__NAME_UPPERCASE__"_ACCEPTED_LANGUAGE");
709 accepted_language[0] = g_strdup(e);
711 e = g_getenv(__NAME_UPPERCASE__"_DOWNLOAD_DIR");
713 download_dir = g_strdup(e);
715 e = g_getenv(__NAME_UPPERCASE__"_ENABLE_CONSOLE_TO_STDOUT");
717 enable_console_to_stdout = TRUE;
719 e = g_getenv(__NAME_UPPERCASE__"_FIFO_SUFFIX");
721 fifo_suffix = g_strdup(e);
723 e = g_getenv(__NAME_UPPERCASE__"_HISTORY_FILE");
725 history_file = g_strdup(e);
727 e = g_getenv(__NAME_UPPERCASE__"_HOME_URI");
729 home_uri = g_strdup(e);
731 e = g_getenv(__NAME_UPPERCASE__"_TAB_POS");
734 if (strcmp(e, "top") == 0)
735 tab_pos = GTK_POS_TOP;
736 if (strcmp(e, "right") == 0)
737 tab_pos = GTK_POS_RIGHT;
738 if (strcmp(e, "bottom") == 0)
739 tab_pos = GTK_POS_BOTTOM;
740 if (strcmp(e, "left") == 0)
741 tab_pos = GTK_POS_LEFT;
744 e = g_getenv(__NAME_UPPERCASE__"_TAB_WIDTH_CHARS");
746 tab_width_chars = atoi(e);
748 e = g_getenv(__NAME_UPPERCASE__"_USER_AGENT");
750 user_agent = g_strdup(e);
752 e = g_getenv(__NAME_UPPERCASE__"_ZOOM");
754 global_zoom = atof(e);
758 grab_feeds_finished(GObject *object, GAsyncResult *result, gpointer data)
760 struct Client *c = (struct Client *)data;
761 WebKitJavascriptResult *js_result;
763 JSCException *exception;
767 g_free(c->feed_html);
770 /* This was taken almost verbatim from the example in WebKit's
773 * https://webkitgtk.org/reference/webkit2gtk/stable/WebKitWebView.html#webkit-web-view-run-javascript-finish */
775 js_result = webkit_web_view_run_javascript_finish(WEBKIT_WEB_VIEW(object),
779 fprintf(stderr, __NAME__": Error running javascript: %s\n", err->message);
784 value = webkit_javascript_result_get_js_value(js_result);
785 if (jsc_value_is_string(value))
787 str_value = jsc_value_to_string(value);
788 exception = jsc_context_get_exception(jsc_value_get_context(value));
789 if (exception != NULL)
791 fprintf(stderr, __NAME__": Error running javascript: %s\n",
792 jsc_exception_get_message(exception));
795 c->feed_html = str_value;
797 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
798 GTK_ENTRY_ICON_PRIMARY,
799 "application-rss+xml-symbolic");
800 gtk_entry_set_icon_activatable(GTK_ENTRY(c->location),
801 GTK_ENTRY_ICON_PRIMARY,
806 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
807 GTK_ENTRY_ICON_PRIMARY,
811 webkit_javascript_result_unref(js_result);
815 hover_web_view(WebKitWebView *web_view, WebKitHitTestResult *ht, guint modifiers,
818 struct Client *c = (struct Client *)data;
820 if (!gtk_widget_is_focus(c->location))
822 if (webkit_hit_test_result_context_is_link(ht))
824 gtk_entry_set_text(GTK_ENTRY(c->location),
825 webkit_hit_test_result_get_link_uri(ht));
827 if (c->hover_uri != NULL)
828 g_free(c->hover_uri);
829 c->hover_uri = g_strdup(webkit_hit_test_result_get_link_uri(ht));
833 gtk_entry_set_text(GTK_ENTRY(c->location),
834 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view)));
836 if (c->hover_uri != NULL)
837 g_free(c->hover_uri);
844 icon_location(GtkEntry *entry, GtkEntryIconPosition icon_pos, GdkEvent *event,
847 struct Client *c = (struct Client *)data;
849 gchar *data_template =
854 " <meta charset=\"UTF-8\">"
855 " <title>Feeds</title>"
858 " <p>Feeds found on this page:</p>"
865 if (c->feed_html != NULL)
867 /* What we're actually trying to do is show a simple HTML page
868 * that lists all the feeds on the current page. The function
869 * webkit_web_view_load_html() looks like the proper way to do
870 * that. Sad thing is, it doesn't create a history entry, but
871 * instead simply replaces the content of the current page. This
872 * is not what we want.
874 * RFC 2397 [0] defines the data URI scheme [1]. We abuse this
875 * mechanism to show my custom HTML snippet *and* create a
878 * [0]: https://tools.ietf.org/html/rfc2397
879 * [1]: https://en.wikipedia.org/wiki/Data_URI_scheme */
880 d = g_strdup_printf(data_template, c->feed_html);
881 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), d);
887 key_common(GtkWidget *widget, GdkEvent *event, gpointer data)
889 struct Client *c = (struct Client *)data;
890 WebKitWebContext *wc = webkit_web_view_get_context(WEBKIT_WEB_VIEW(c->web_view));
893 if (event->type == GDK_KEY_PRESS)
895 if (((GdkEventKey *)event)->state & GDK_MOD1_MASK)
897 switch (((GdkEventKey *)event)->keyval)
899 case GDK_KEY_q: /* close window (left hand) */
900 client_destroy(NULL, c);
902 case GDK_KEY_w: /* home (left hand) */
903 f = ensure_uri_scheme(home_uri);
904 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
907 case GDK_KEY_e: /* new tab (left hand) */
908 f = ensure_uri_scheme(home_uri);
909 client_new(f, NULL, TRUE, TRUE);
912 case GDK_KEY_r: /* reload (left hand) */
913 webkit_web_view_reload_bypass_cache(WEBKIT_WEB_VIEW(
916 case GDK_KEY_d: /* download manager (left hand) */
917 gtk_widget_show_all(dm.win);
919 case GDK_KEY_2: /* search forward (left hand) */
920 case GDK_KEY_n: /* search forward (maybe both hands) */
923 case GDK_KEY_3: /* search backward (left hand) */
926 case GDK_KEY_l: /* location (BOTH hands) */
927 gtk_widget_grab_focus(c->location);
929 case GDK_KEY_k: /* initiate search (BOTH hands) */
930 gtk_widget_grab_focus(c->location);
931 gtk_entry_set_text(GTK_ENTRY(c->location), ":/");
932 gtk_editable_set_position(GTK_EDITABLE(c->location), -1);
934 case GDK_KEY_c: /* reload trusted certs (left hand) */
935 trust_user_certs(wc);
937 case GDK_KEY_x: /* launch external handler (left hand) */
938 if (c->external_handler_uri != NULL)
939 g_free(c->external_handler_uri);
940 c->external_handler_uri = g_strdup(
941 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view)));
942 external_handler_run(NULL, NULL, c);
944 case GDK_KEY_a: /* go one tab to the left (left hand) */
945 gtk_notebook_prev_page(GTK_NOTEBOOK(mw.notebook));
947 case GDK_KEY_s: /* go one tab to the right (left hand) */
948 gtk_notebook_next_page(GTK_NOTEBOOK(mw.notebook));
952 /* navigate backward (left hand) */
953 else if (((GdkEventKey *)event)->keyval == GDK_KEY_F2)
955 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
958 /* navigate forward (left hand) */
959 else if (((GdkEventKey *)event)->keyval == GDK_KEY_F3)
961 webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
970 key_downloadmanager(GtkWidget *widget, GdkEvent *event, gpointer data)
972 if (event->type == GDK_KEY_PRESS)
974 if (((GdkEventKey *)event)->state & GDK_MOD1_MASK)
976 switch (((GdkEventKey *)event)->keyval)
978 case GDK_KEY_d: /* close window (left hand) */
980 downloadmanager_delete(dm.win, NULL);
990 key_location(GtkWidget *widget, GdkEvent *event, gpointer data)
992 struct Client *c = (struct Client *)data;
996 if (key_common(widget, event, data))
999 if (event->type == GDK_KEY_PRESS)
1001 switch (((GdkEventKey *)event)->keyval)
1003 case GDK_KEY_KP_Enter:
1004 case GDK_KEY_Return:
1005 gtk_widget_grab_focus(c->web_view);
1006 t = gtk_entry_get_text(GTK_ENTRY(c->location));
1007 if (t != NULL && t[0] == ':' && t[1] == '/')
1009 if (search_text != NULL)
1010 g_free(search_text);
1011 search_text = g_strdup(t + 2);
1016 f = ensure_uri_scheme(t);
1017 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
1021 case GDK_KEY_Escape:
1022 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
1023 gtk_entry_set_text(GTK_ENTRY(c->location),
1024 (t == NULL ? __NAME__ : t));
1033 key_web_view(GtkWidget *widget, GdkEvent *event, gpointer data)
1035 struct Client *c = (struct Client *)data;
1039 if (key_common(widget, event, data))
1042 if (event->type == GDK_KEY_PRESS)
1044 if (((GdkEventKey *)event)->keyval == GDK_KEY_Escape)
1046 webkit_web_view_stop_loading(WEBKIT_WEB_VIEW(c->web_view));
1047 gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), 0);
1050 else if (event->type == GDK_BUTTON_RELEASE)
1052 switch (((GdkEventButton *)event)->button)
1055 if (c->hover_uri != NULL)
1057 client_new(c->hover_uri, NULL, TRUE, FALSE);
1062 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
1065 webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
1069 else if (event->type == GDK_SCROLL)
1071 if (((GdkEventScroll *)event)->state & GDK_MOD1_MASK ||
1072 ((GdkEventScroll *)event)->state & GDK_CONTROL_MASK)
1074 gdk_event_get_scroll_deltas(event, &dx, &dy);
1075 z = webkit_web_view_get_zoom_level(WEBKIT_WEB_VIEW(c->web_view));
1077 z = dx != 0 ? global_zoom : z;
1078 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), z);
1087 mainwindow_setup(void)
1089 mw.win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1090 gtk_window_set_default_size(GTK_WINDOW(mw.win), 800, 600);
1091 g_signal_connect(G_OBJECT(mw.win), "destroy", gtk_main_quit, NULL);
1092 gtk_window_set_title(GTK_WINDOW(mw.win), __NAME__);
1094 mw.notebook = gtk_notebook_new();
1095 gtk_notebook_set_scrollable(GTK_NOTEBOOK(mw.notebook), TRUE);
1096 gtk_notebook_set_tab_pos(GTK_NOTEBOOK(mw.notebook), tab_pos);
1097 gtk_container_add(GTK_CONTAINER(mw.win), mw.notebook);
1101 menu_web_view(WebKitWebView *web_view, WebKitContextMenu *menu, GdkEvent *ev,
1102 WebKitHitTestResult *ht, gpointer data)
1104 struct Client *c = (struct Client *)data;
1105 GSimpleAction *action = NULL;
1106 WebKitContextMenuItem *mi = NULL;
1107 const gchar *uri = NULL;
1111 if (webkit_hit_test_result_context_is_link(ht))
1112 uri = webkit_hit_test_result_get_link_uri(ht);
1113 else if (webkit_hit_test_result_context_is_image(ht))
1114 uri = webkit_hit_test_result_get_image_uri(ht);
1115 else if (webkit_hit_test_result_context_is_media(ht))
1116 uri = webkit_hit_test_result_get_media_uri(ht);
1120 webkit_context_menu_append(menu, webkit_context_menu_item_new_separator());
1122 if (c->external_handler_uri != NULL)
1123 g_free(c->external_handler_uri);
1124 c->external_handler_uri = g_strdup(uri);
1125 action = g_simple_action_new("external_handler", NULL);
1126 g_signal_connect(G_OBJECT(action), "activate",
1127 G_CALLBACK(external_handler_run), data);
1128 mi = webkit_context_menu_item_new_from_gaction(G_ACTION(action),
1129 "Open with external handler",
1131 webkit_context_menu_append(menu, mi);
1132 g_object_unref(action);
1135 /* FALSE = Show the menu. (TRUE = Don't ever show it.) */
1140 quit_if_nothing_active(void)
1150 gtk_widget_show_all(dm.win);
1157 remote_msg(GIOChannel *channel, GIOCondition condition, gpointer data)
1161 g_io_channel_read_line(channel, &uri, NULL, NULL, NULL);
1165 client_new(uri, NULL, TRUE, TRUE);
1172 run_user_scripts(WebKitWebView *web_view)
1174 gchar *base = NULL, *path = NULL, *contents = NULL;
1175 const gchar *entry = NULL;
1176 GDir *scriptdir = NULL;
1178 base = g_build_filename(g_get_user_config_dir(), __NAME__, "user-scripts", NULL);
1179 scriptdir = g_dir_open(base, 0, NULL);
1180 if (scriptdir != NULL)
1182 while ((entry = g_dir_read_name(scriptdir)) != NULL)
1184 path = g_build_filename(base, entry, NULL);
1185 if (g_str_has_suffix(path, ".js"))
1187 if (g_file_get_contents(path, &contents, NULL, NULL))
1189 webkit_web_view_run_javascript(web_view, contents, NULL, NULL, NULL);
1195 g_dir_close(scriptdir);
1202 search(gpointer data, gint direction)
1204 struct Client *c = (struct Client *)data;
1205 WebKitWebView *web_view = WEBKIT_WEB_VIEW(c->web_view);
1206 WebKitFindController *fc = webkit_web_view_get_find_controller(web_view);
1208 if (search_text == NULL)
1214 webkit_find_controller_search(fc, search_text,
1215 WEBKIT_FIND_OPTIONS_CASE_INSENSITIVE |
1216 WEBKIT_FIND_OPTIONS_WRAP_AROUND,
1220 webkit_find_controller_search_next(fc);
1223 webkit_find_controller_search_previous(fc);
1229 show_web_view(WebKitWebView *web_view, gpointer data)
1231 struct Client *c = (struct Client *)data;
1236 gtk_widget_show_all(mw.win);
1238 if (c->focus_new_tab)
1240 idx = gtk_notebook_page_num(GTK_NOTEBOOK(mw.notebook), c->vbox);
1242 gtk_notebook_set_current_page(GTK_NOTEBOOK(mw.notebook), idx);
1244 gtk_widget_grab_focus(c->web_view);
1249 trust_user_certs(WebKitWebContext *wc)
1251 GTlsCertificate *cert;
1252 const gchar *basedir, *file, *absfile;
1255 basedir = g_build_filename(g_get_user_config_dir(), __NAME__, "certs", NULL);
1256 dir = g_dir_open(basedir, 0, NULL);
1259 file = g_dir_read_name(dir);
1260 while (file != NULL)
1262 absfile = g_build_filename(g_get_user_config_dir(), __NAME__, "certs",
1264 cert = g_tls_certificate_new_from_file(absfile, NULL);
1266 fprintf(stderr, __NAME__": Could not load trusted cert '%s'\n", file);
1268 webkit_web_context_allow_tls_certificate_for_host(wc, cert, file);
1269 file = g_dir_read_name(dir);
1277 main(int argc, char **argv)
1282 gtk_init(&argc, &argv);
1283 webkit_web_context_set_process_model(webkit_web_context_get_default(),
1284 WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
1286 grab_environment_configuration();
1288 while ((opt = getopt(argc, argv, "C")) != -1)
1293 cooperative_instances = FALSE;
1296 fprintf(stderr, "Usage: "__NAME__" [OPTION]... [URI]...\n");
1301 if (cooperative_instances)
1302 cooperation_setup();
1303 downloadmanager_setup();
1307 if (!cooperative_instances || cooperative_alone)
1309 c = g_build_filename(g_get_user_config_dir(), __NAME__, "web_extensions",
1311 webkit_web_context_set_web_extensions_directory(
1312 webkit_web_context_get_default(), c
1317 client_new(home_uri, NULL, TRUE, TRUE);
1320 for (i = optind; i < argc; i++)
1321 client_new(argv[i], NULL, TRUE, TRUE);
1324 if (!cooperative_instances || cooperative_alone)