10 #include <gdk/gdkkeysyms.h>
12 #include <webkit/webkit.h>
15 static void adblock(WebKitWebView *, WebKitWebFrame *, WebKitWebResource *,
16 WebKitNetworkRequest *, WebKitNetworkResponse *, gpointer);
17 static void adblock_load(void);
18 static void client_destroy(GtkWidget *obj, gpointer data);
19 static gboolean client_destroy_request(WebKitWebView *, gpointer);
20 static WebKitWebView *client_new(const gchar *uri);
21 static WebKitWebView *client_new_request(WebKitWebView *, WebKitWebFrame *,
23 static void cooperation_setup(void);
24 static void changed_load_progress(GObject *obj, GParamSpec *pspec,
26 static void changed_title(GObject *, GParamSpec *, gpointer);
27 static void changed_uri(GObject *, GParamSpec *, gpointer);
28 static gboolean download_handle(WebKitWebView *, WebKitDownload *, gpointer);
29 static gboolean download_reset_indicator(gpointer);
30 static gboolean download_request(WebKitWebView *, WebKitWebFrame *,
31 WebKitNetworkRequest *, gchar *,
32 WebKitWebPolicyDecision *, gpointer);
33 static void downloadmanager_cancel(GtkToolButton *, gpointer data);
34 static void downloadmanager_progress(GObject *obj, GParamSpec *pspec,
36 static void downloadmanager_setup(void);
37 static gchar *ensure_url_scheme(const gchar *);
38 static void grab_environment_configuration(void);
39 static void hover_web_view(WebKitWebView *, gchar *, gchar *, gpointer);
40 static gboolean key_location(GtkWidget *, GdkEvent *, gpointer);
41 static gboolean key_web_view(GtkWidget *, GdkEvent *, gpointer);
42 static gboolean remote_msg(GIOChannel *, GIOCondition, gpointer);
43 static void search(gpointer, gint);
44 static void scroll(GtkAdjustment *, gint, gdouble);
45 static Window tabbed_launch(void);
46 static void usage(void);
61 struct DownloadManager
69 static gchar *accepted_language = "en-US";
70 static GSList *adblock_patterns = NULL;
71 static gint clients = 0;
72 static gboolean cooperative_alone = TRUE;
73 static gboolean cooperative_instances = TRUE;
74 static int cooperative_pipe_fp = 0;
75 static gchar *download_dir = "/tmp";
76 static gint downloads_indicated = 0;
77 static Window embed = 0;
78 static gchar *first_uri = NULL;
79 static gdouble global_zoom = 1.0;
80 static gboolean language_is_set = FALSE;
81 static gchar *search_text = NULL;
82 static gboolean show_all_requests = FALSE;
83 static gboolean tabbed_automagic = TRUE;
87 adblock(WebKitWebView *web_view, WebKitWebFrame *frame,
88 WebKitWebResource *resource, WebKitNetworkRequest *request,
89 WebKitNetworkResponse *response, gpointer data)
91 GSList *it = adblock_patterns;
100 uri = webkit_network_request_get_uri(request);
101 if (show_all_requests)
102 fprintf(stderr, " -> %s\n", uri);
106 if (g_regex_match((GRegex *)(it->data), uri, 0, NULL))
108 webkit_network_request_set_uri(request, "about:blank");
109 if (show_all_requests)
110 fprintf(stderr, " BLOCKED!\n");
113 it = g_slist_next(it);
122 GIOChannel *channel = NULL;
126 path = g_build_filename(g_get_user_config_dir(), __NAME__, "adblock.black",
128 channel = g_io_channel_new_file(path, "r", &err);
131 while (g_io_channel_read_line(channel, &buf, NULL, NULL, NULL)
132 == G_IO_STATUS_NORMAL)
135 re = g_regex_new(buf,
136 G_REGEX_CASELESS | G_REGEX_OPTIMIZE,
137 G_REGEX_MATCH_PARTIAL, &err);
140 fprintf(stderr, __NAME__": Could not compile regex: %s\n", buf);
144 adblock_patterns = g_slist_append(adblock_patterns, re);
153 client_destroy(GtkWidget *obj, gpointer data)
155 struct Client *c = (struct Client *)data;
168 client_destroy_request(WebKitWebView *web_view, gpointer data)
170 struct Client *c = (struct Client *)data;
174 gtk_widget_destroy(c->win);
180 client_new(const gchar *uri)
185 if (uri != NULL && cooperative_instances && !cooperative_alone)
187 write(cooperative_pipe_fp, uri, strlen(uri));
188 write(cooperative_pipe_fp, "\n", 1);
192 c = malloc(sizeof(struct Client));
195 fprintf(stderr, __NAME__": fatal: malloc failed\n");
202 c->win = gtk_plug_new(embed);
203 if (!gtk_plug_get_embedded(GTK_PLUG(c->win)))
205 fprintf(stderr, __NAME__": Can't plug-in to XID %ld.\n", embed);
206 gtk_widget_destroy(c->win);
214 c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
215 gtk_window_set_wmclass(GTK_WINDOW(c->win), __NAME__, __NAME_CAPITALIZED__);
218 /* When using Gtk2, it only shows a white area when run in suckless'
219 * tabbed. It appears we need to set a default window size for this
220 * to work. This is not needed when using Gtk3. */
221 gtk_window_set_default_size(GTK_WINDOW(c->win), 1024, 768);
223 g_signal_connect(G_OBJECT(c->win), "destroy", G_CALLBACK(client_destroy), c);
224 gtk_window_set_title(GTK_WINDOW(c->win), __NAME__);
226 c->web_view = webkit_web_view_new();
228 /* XXX I really do want to enable this option. However, I get
229 * reproducable crashes with it enabled. I've seen bug reports from
230 * 2010 about this... WebKit crashes in libpixman, so maybe it's not
232 * Yeah, well. I'll turn it off for now. */
233 /*webkit_web_view_set_full_content_zoom(WEBKIT_WEB_VIEW(c->web_view), TRUE);*/
235 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), global_zoom);
236 g_signal_connect(G_OBJECT(c->web_view), "notify::title",
237 G_CALLBACK(changed_title), c);
238 g_signal_connect(G_OBJECT(c->web_view), "notify::uri",
239 G_CALLBACK(changed_uri), c);
240 g_signal_connect(G_OBJECT(c->web_view), "notify::progress",
241 G_CALLBACK(changed_load_progress), c);
242 g_signal_connect(G_OBJECT(c->web_view), "create-web-view",
243 G_CALLBACK(client_new_request), NULL);
244 g_signal_connect(G_OBJECT(c->web_view), "close-web-view",
245 G_CALLBACK(client_destroy_request), c);
246 g_signal_connect(G_OBJECT(c->web_view),
247 "mime-type-policy-decision-requested",
248 G_CALLBACK(download_request), NULL);
249 g_signal_connect(G_OBJECT(c->web_view), "download-requested",
250 G_CALLBACK(download_handle), c);
251 g_signal_connect(G_OBJECT(c->web_view), "key-press-event",
252 G_CALLBACK(key_web_view), c);
253 g_signal_connect(G_OBJECT(c->web_view), "button-press-event",
254 G_CALLBACK(key_web_view), c);
255 g_signal_connect(G_OBJECT(c->web_view), "hovering-over-link",
256 G_CALLBACK(hover_web_view), c);
257 g_signal_connect(G_OBJECT(c->web_view), "resource-request-starting",
258 G_CALLBACK(adblock), NULL);
260 if (!language_is_set)
262 g_object_set(webkit_get_default_session(), "accept-language",
263 accepted_language, NULL);
264 language_is_set = TRUE;
267 c->scroll = gtk_scrolled_window_new(NULL, NULL);
269 gtk_container_add(GTK_CONTAINER(c->scroll), c->web_view);
271 c->location = gtk_entry_new();
272 g_signal_connect(G_OBJECT(c->location), "key-press-event",
273 G_CALLBACK(key_location), c);
275 c->progress = gtk_progress_bar_new();
276 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(c->progress), 0);
278 c->status = gtk_progress_bar_new();
279 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(c->status), 0);
280 gtk_widget_set_size_request(c->status, 20, -1);
282 c->top_box = gtk_hbox_new(FALSE, 0);
283 gtk_box_pack_start(GTK_BOX(c->top_box), c->status, FALSE, FALSE, 2);
284 gtk_box_pack_start(GTK_BOX(c->top_box), c->location, TRUE, TRUE, 0);
285 gtk_box_pack_end(GTK_BOX(c->top_box), c->progress, FALSE, TRUE, 2);
287 c->vbox = gtk_vbox_new(FALSE, 0);
288 gtk_box_pack_start(GTK_BOX(c->vbox), c->top_box, FALSE, FALSE, 2);
289 gtk_container_add(GTK_CONTAINER(c->vbox), c->scroll);
291 gtk_container_add(GTK_CONTAINER(c->win), c->vbox);
293 gtk_widget_grab_focus(c->web_view);
294 gtk_widget_show_all(c->win);
298 f = ensure_url_scheme(uri);
299 if (show_all_requests)
300 fprintf(stderr, "====> %s\n", uri);
301 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
307 return WEBKIT_WEB_VIEW(c->web_view);
311 client_new_request(WebKitWebView *web_view, WebKitWebFrame *frame, gpointer data)
317 return client_new(NULL);
321 cooperation_setup(void)
326 fifopath = g_build_filename(g_get_user_runtime_dir(), __NAME__".fifo", NULL);
328 if (!g_file_test(fifopath, G_FILE_TEST_EXISTS))
329 mkfifo(fifopath, 0600);
331 cooperative_pipe_fp = open(fifopath, O_WRONLY | O_NONBLOCK);
332 if (!cooperative_pipe_fp)
334 fprintf(stderr, __NAME__": Can't open FIFO at all.\n");
338 if (write(cooperative_pipe_fp, "", 0) == -1)
340 /* Could not do an empty write to the FIFO which means there's
341 * no one listening. */
342 close(cooperative_pipe_fp);
343 towatch = g_io_channel_new_file(fifopath, "r+", NULL);
344 g_io_add_watch(towatch, G_IO_IN, (GIOFunc)remote_msg, NULL);
347 cooperative_alone = FALSE;
354 changed_load_progress(GObject *obj, GParamSpec *pspec, gpointer data)
356 struct Client *c = (struct Client *)data;
362 p = webkit_web_view_get_progress(WEBKIT_WEB_VIEW(c->web_view));
363 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(c->progress), p);
367 changed_title(GObject *obj, GParamSpec *pspec, gpointer data)
370 struct Client *c = (struct Client *)data;
375 t = webkit_web_view_get_title(WEBKIT_WEB_VIEW(c->web_view));
376 gtk_window_set_title(GTK_WINDOW(c->win), (t == NULL ? __NAME__ : t));
380 changed_uri(GObject *obj, GParamSpec *pspec, gpointer data)
383 struct Client *c = (struct Client *)data;
388 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
389 gtk_entry_set_text(GTK_ENTRY(c->location), (t == NULL ? __NAME__ : t));
393 download_handle(WebKitWebView *web_view, WebKitDownload *download, gpointer data)
395 struct Client *c = (struct Client *)data;
396 gchar *path, *path2 = NULL, *uri;
404 path = g_build_filename(download_dir,
405 webkit_download_get_suggested_filename(download),
407 path2 = g_strdup(path);
408 while (g_file_test(path2, G_FILE_TEST_EXISTS) && suffix < 1000)
412 path2 = g_strdup_printf("%s.%d", path, suffix);
418 fprintf(stderr, __NAME__": Suffix reached limit for download.\n");
423 uri = g_filename_to_uri(path2, NULL, NULL);
424 webkit_download_set_destination_uri(download, uri);
428 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(c->status), 1);
429 downloads_indicated++;
430 g_timeout_add(500, download_reset_indicator, c);
432 tb = gtk_tool_button_new_from_stock(GTK_STOCK_DELETE);
433 gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb),
434 webkit_download_get_suggested_filename(download));
435 gtk_toolbar_insert(GTK_TOOLBAR(dm.toolbar), tb, 0);
436 gtk_widget_show_all(dm.toolbar);
438 g_signal_connect(G_OBJECT(download), "notify::progress",
439 G_CALLBACK(downloadmanager_progress), tb);
441 g_object_ref(download);
442 g_signal_connect(G_OBJECT(tb), "clicked",
443 G_CALLBACK(downloadmanager_cancel), download);
453 download_reset_indicator(gpointer data)
455 struct Client *c = (struct Client *)data;
457 downloads_indicated--;
458 if (downloads_indicated == 0)
459 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(c->status), 0);
465 download_request(WebKitWebView *web_view, WebKitWebFrame *frame,
466 WebKitNetworkRequest *request, gchar *mime_type,
467 WebKitWebPolicyDecision *policy_decision, gpointer data)
473 if (!webkit_web_view_can_show_mime_type(web_view, mime_type))
475 webkit_web_policy_decision_download(policy_decision);
482 downloadmanager_cancel(GtkToolButton *tb, gpointer data)
484 WebKitDownload *download = WEBKIT_DOWNLOAD(data);
486 webkit_download_cancel(download);
487 g_object_unref(download);
489 gtk_widget_destroy(GTK_WIDGET(tb));
493 downloadmanager_progress(GObject *obj, GParamSpec *pspec, gpointer data)
495 WebKitDownload *download = WEBKIT_DOWNLOAD(obj);
496 GtkToolItem *tb = GTK_TOOL_ITEM(data);
502 p = webkit_download_get_progress(download) * 100;
503 t = g_strdup_printf("%s (%.0f%%)",
504 webkit_download_get_suggested_filename(download),
506 gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), t);
511 downloadmanager_setup(void)
513 dm.win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
514 gtk_window_set_type_hint(GTK_WINDOW(dm.win), GDK_WINDOW_TYPE_HINT_DIALOG);
515 gtk_window_set_default_size(GTK_WINDOW(dm.win), 500, 250);
516 gtk_window_set_title(GTK_WINDOW(dm.win), __NAME__" - Download Manager");
517 g_signal_connect(G_OBJECT(dm.win), "delete-event",
518 G_CALLBACK(gtk_widget_hide_on_delete), NULL);
520 dm.toolbar = gtk_toolbar_new();
521 gtk_orientable_set_orientation(GTK_ORIENTABLE(dm.toolbar),
522 GTK_ORIENTATION_VERTICAL);
523 gtk_toolbar_set_style(GTK_TOOLBAR(dm.toolbar), GTK_TOOLBAR_BOTH_HORIZ);
524 gtk_toolbar_set_show_arrow(GTK_TOOLBAR(dm.toolbar), FALSE);
526 dm.scroll = gtk_scrolled_window_new(NULL, NULL);
527 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(dm.scroll),
528 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
529 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(dm.scroll),
532 gtk_container_add(GTK_CONTAINER(dm.win), dm.scroll);
536 ensure_url_scheme(const gchar *t)
540 f = g_ascii_strdown(t, -1);
541 if (!g_str_has_prefix(f, "http:") &&
542 !g_str_has_prefix(f, "https:") &&
543 !g_str_has_prefix(f, "file:") &&
544 !g_str_has_prefix(f, "about:"))
547 f = g_strdup_printf("http://%s", t);
555 grab_environment_configuration(void)
559 e = g_getenv(__NAME_UPPERCASE__"_ACCEPTED_LANGUAGE");
561 accepted_language = g_strdup(e);
563 e = g_getenv(__NAME_UPPERCASE__"_DOWNLOAD_DIR");
565 download_dir = g_strdup(e);
567 e = g_getenv(__NAME_UPPERCASE__"_ZOOM");
569 global_zoom = atof(e);
573 hover_web_view(WebKitWebView *web_view, gchar *title, gchar *uri, gpointer data)
575 struct Client *c = (struct Client *)data;
580 if (!gtk_widget_is_focus(c->location))
583 gtk_entry_set_text(GTK_ENTRY(c->location),
584 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view)));
586 gtk_entry_set_text(GTK_ENTRY(c->location), uri);
591 key_location(GtkWidget *widget, GdkEvent *event, gpointer data)
593 struct Client *c = (struct Client *)data;
599 if (event->type == GDK_KEY_PRESS)
601 switch (((GdkEventKey *)event)->keyval)
604 gtk_widget_grab_focus(c->web_view);
605 t = gtk_entry_get_text(GTK_ENTRY(c->location));
606 if (t != NULL && t[0] == '/')
608 if (search_text != NULL)
610 search_text = g_strdup(t + 1); /* XXX whacky */
615 f = ensure_url_scheme(t);
616 if (show_all_requests)
617 fprintf(stderr, "====> %s\n", f);
618 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
623 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
624 gtk_entry_set_text(GTK_ENTRY(c->location),
625 (t == NULL ? __NAME__ : t));
628 if (((GdkEventKey *)event)->state & GDK_CONTROL_MASK)
630 gtk_widget_show_all(dm.win);
642 key_web_view(GtkWidget *widget, GdkEvent *event, gpointer data)
644 struct Client *c = (struct Client *)data;
645 WebKitHitTestResultContext ht_context;
646 WebKitHitTestResult *ht_result = NULL;
647 gchar *ht_uri = NULL, *f;
651 if (event->type == GDK_KEY_PRESS)
653 if (((GdkEventKey *)event)->state & GDK_CONTROL_MASK)
655 switch (((GdkEventKey *)event)->keyval)
658 gtk_widget_grab_focus(c->location);
661 scroll(gtk_scrolled_window_get_hadjustment(
662 GTK_SCROLLED_WINDOW(c->scroll)), 0, -1);
665 scroll(gtk_scrolled_window_get_vadjustment(
666 GTK_SCROLLED_WINDOW(c->scroll)), 0, 1);
669 scroll(gtk_scrolled_window_get_vadjustment(
670 GTK_SCROLLED_WINDOW(c->scroll)), 0, -1);
673 scroll(gtk_scrolled_window_get_hadjustment(
674 GTK_SCROLLED_WINDOW(c->scroll)), 0, 1);
677 scroll(gtk_scrolled_window_get_vadjustment(
678 GTK_SCROLLED_WINDOW(c->scroll)), 1, 0.5);
681 scroll(gtk_scrolled_window_get_vadjustment(
682 GTK_SCROLLED_WINDOW(c->scroll)), 1, -0.5);
691 f = ensure_url_scheme(first_uri);
692 if (show_all_requests)
693 fprintf(stderr, "====> %s\n", f);
694 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
698 gtk_widget_show_all(dm.win);
702 else if (((GdkEventKey *)event)->keyval == GDK_KEY_Escape)
704 webkit_web_view_stop_loading(WEBKIT_WEB_VIEW(c->web_view));
705 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(c->progress), 0);
708 else if (event->type == GDK_BUTTON_PRESS)
710 switch (((GdkEventButton *)event)->button)
713 ht_result = webkit_web_view_get_hit_test_result(
714 WEBKIT_WEB_VIEW(c->web_view),
715 (GdkEventButton *)event);
716 g_object_get(ht_result, "context", &ht_context, NULL);
717 if (ht_context & WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK)
719 g_object_get(ht_result, "link-uri", &ht_uri, NULL);
721 g_object_unref(ht_result);
724 g_object_unref(ht_result);
727 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
730 webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
739 remote_msg(GIOChannel *channel, GIOCondition condition, gpointer data)
746 g_io_channel_read_line(channel, &uri, NULL, NULL, NULL);
757 search(gpointer data, gint direction)
759 struct Client *c = (struct Client *)data;
761 if (search_text == NULL)
764 webkit_web_view_search_text(WEBKIT_WEB_VIEW(c->web_view), search_text,
765 FALSE, direction == 1, TRUE);
769 scroll(GtkAdjustment *a, gint step_type, gdouble factor)
771 gdouble new, lower, upper, step;
772 lower = gtk_adjustment_get_lower(a);
773 upper = gtk_adjustment_get_upper(a) - gtk_adjustment_get_page_size(a) + lower;
775 step = gtk_adjustment_get_step_increment(a);
777 step = gtk_adjustment_get_page_increment(a);
778 new = gtk_adjustment_get_value(a) + factor * step;
779 new = new < lower ? lower : new;
780 new = new > upper ? upper : new;
781 gtk_adjustment_set_value(a, new);
788 GIOChannel *tabbed_stdout_channel;
790 gchar *output = NULL;
791 char *argv[] = { "tabbed", "-c", "-d", "-p", "s1", "-n", __NAME__, NULL };
794 if (!g_spawn_async_with_pipes(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL,
795 NULL, NULL, NULL, &tabbed_stdout, NULL,
798 fprintf(stderr, __NAME__": Could not launch tabbed: %s\n", err->message);
803 tabbed_stdout_channel = g_io_channel_unix_new(tabbed_stdout);
804 g_io_channel_read_line(tabbed_stdout_channel, &output, NULL, NULL, NULL);
807 fprintf(stderr, __NAME__": Could not read XID from tabbed\n");
811 g_io_channel_shutdown(tabbed_stdout_channel, FALSE, NULL);
814 plug_into = strtol(output, NULL, 16);
822 fprintf(stderr, "Usage: "__NAME__" [OPTION]... <URI>...\n");
828 main(int argc, char **argv)
832 gtk_init(&argc, &argv);
834 grab_environment_configuration();
836 while ((opt = getopt(argc, argv, "e:rCT")) != -1)
841 embed = atol(optarg);
842 tabbed_automagic = FALSE;
845 show_all_requests = TRUE;
848 cooperative_instances = FALSE;
851 tabbed_automagic = FALSE;
863 downloadmanager_setup();
865 if (tabbed_automagic && !(cooperative_instances && !cooperative_alone))
866 embed = tabbed_launch();
868 first_uri = g_strdup(argv[optind]);
869 for (i = optind; i < argc; i++)
871 if (!cooperative_instances || cooperative_alone)