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 gchar *ensure_url_scheme(const gchar *);
34 static void grab_environment_configuration(void);
35 static void hover_web_view(WebKitWebView *, gchar *, gchar *, gpointer);
36 static gboolean key_location(GtkWidget *, GdkEvent *, gpointer);
37 static gboolean key_web_view(GtkWidget *, GdkEvent *, gpointer);
38 static gboolean remote_msg(GIOChannel *, GIOCondition, gpointer);
39 static void search(gpointer, gint);
40 static void scroll(GtkAdjustment *, gint, gdouble);
41 static Window tabbed_launch(void);
42 static void usage(void);
58 static gchar *accepted_language = "en-US";
59 static GSList *adblock_patterns = NULL;
60 static gint clients = 0;
61 static gboolean cooperative_alone = TRUE;
62 static gboolean cooperative_instances = TRUE;
63 static int cooperative_pipe_fp = 0;
64 static gchar *download_dir = "/tmp";
65 static gint downloads_indicated = 0;
66 static Window embed = 0;
67 static gchar *first_uri = NULL;
68 static gdouble global_zoom = 1.0;
69 static gboolean language_is_set = FALSE;
70 static gchar *search_text = NULL;
71 static gboolean show_all_requests = FALSE;
72 static gboolean tabbed_automagic = TRUE;
76 adblock(WebKitWebView *web_view, WebKitWebFrame *frame,
77 WebKitWebResource *resource, WebKitNetworkRequest *request,
78 WebKitNetworkResponse *response, gpointer data)
80 GSList *it = adblock_patterns;
89 uri = webkit_network_request_get_uri(request);
90 if (show_all_requests)
91 fprintf(stderr, " -> %s\n", uri);
95 if (g_regex_match((GRegex *)(it->data), uri, 0, NULL))
97 webkit_network_request_set_uri(request, "about:blank");
98 if (show_all_requests)
99 fprintf(stderr, " BLOCKED!\n");
102 it = g_slist_next(it);
111 GIOChannel *channel = NULL;
115 path = g_build_filename(g_get_user_config_dir(), __NAME__, "adblock.black",
117 channel = g_io_channel_new_file(path, "r", &err);
120 while (g_io_channel_read_line(channel, &buf, NULL, NULL, NULL)
121 == G_IO_STATUS_NORMAL)
124 re = g_regex_new(buf,
125 G_REGEX_CASELESS | G_REGEX_OPTIMIZE,
126 G_REGEX_MATCH_PARTIAL, &err);
129 fprintf(stderr, __NAME__": Could not compile regex: %s\n", buf);
133 adblock_patterns = g_slist_append(adblock_patterns, re);
142 client_destroy(GtkWidget *obj, gpointer data)
144 struct Client *c = (struct Client *)data;
157 client_destroy_request(WebKitWebView *web_view, gpointer data)
159 struct Client *c = (struct Client *)data;
163 gtk_widget_destroy(c->win);
169 client_new(const gchar *uri)
174 if (uri != NULL && cooperative_instances && !cooperative_alone)
176 write(cooperative_pipe_fp, uri, strlen(uri));
177 write(cooperative_pipe_fp, "\n", 1);
181 c = malloc(sizeof(struct Client));
184 fprintf(stderr, __NAME__": fatal: malloc failed\n");
191 c->win = gtk_plug_new(embed);
192 if (!gtk_plug_get_embedded(GTK_PLUG(c->win)))
194 fprintf(stderr, __NAME__": Can't plug-in to XID %ld.\n", embed);
195 gtk_widget_destroy(c->win);
203 c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
204 gtk_window_set_wmclass(GTK_WINDOW(c->win), __NAME__, __NAME_CAPITALIZED__);
207 /* When using Gtk2, it only shows a white area when run in suckless'
208 * tabbed. It appears we need to set a default window size for this
209 * to work. This is not needed when using Gtk3. */
210 gtk_window_set_default_size(GTK_WINDOW(c->win), 1024, 768);
212 g_signal_connect(G_OBJECT(c->win), "destroy", G_CALLBACK(client_destroy), c);
213 gtk_window_set_title(GTK_WINDOW(c->win), __NAME__);
215 c->web_view = webkit_web_view_new();
217 /* XXX I really do want to enable this option. However, I get
218 * reproducable crashes with it enabled. I've seen bug reports from
219 * 2010 about this... WebKit crashes in libpixman, so maybe it's not
221 * Yeah, well. I'll turn it off for now. */
222 /*webkit_web_view_set_full_content_zoom(WEBKIT_WEB_VIEW(c->web_view), TRUE);*/
224 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), global_zoom);
225 g_signal_connect(G_OBJECT(c->web_view), "notify::title",
226 G_CALLBACK(changed_title), c);
227 g_signal_connect(G_OBJECT(c->web_view), "notify::uri",
228 G_CALLBACK(changed_uri), c);
229 g_signal_connect(G_OBJECT(c->web_view), "notify::progress",
230 G_CALLBACK(changed_load_progress), c);
231 g_signal_connect(G_OBJECT(c->web_view), "create-web-view",
232 G_CALLBACK(client_new_request), NULL);
233 g_signal_connect(G_OBJECT(c->web_view), "close-web-view",
234 G_CALLBACK(client_destroy_request), c);
235 g_signal_connect(G_OBJECT(c->web_view),
236 "mime-type-policy-decision-requested",
237 G_CALLBACK(download_request), NULL);
238 g_signal_connect(G_OBJECT(c->web_view), "download-requested",
239 G_CALLBACK(download_handle), c);
240 g_signal_connect(G_OBJECT(c->web_view), "key-press-event",
241 G_CALLBACK(key_web_view), c);
242 g_signal_connect(G_OBJECT(c->web_view), "button-press-event",
243 G_CALLBACK(key_web_view), c);
244 g_signal_connect(G_OBJECT(c->web_view), "hovering-over-link",
245 G_CALLBACK(hover_web_view), c);
246 g_signal_connect(G_OBJECT(c->web_view), "resource-request-starting",
247 G_CALLBACK(adblock), NULL);
249 if (!language_is_set)
251 g_object_set(webkit_get_default_session(), "accept-language",
252 accepted_language, NULL);
253 language_is_set = TRUE;
256 c->scroll = gtk_scrolled_window_new(NULL, NULL);
258 gtk_container_add(GTK_CONTAINER(c->scroll), c->web_view);
260 c->location = gtk_entry_new();
261 g_signal_connect(G_OBJECT(c->location), "key-press-event",
262 G_CALLBACK(key_location), c);
264 c->progress = gtk_progress_bar_new();
265 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(c->progress), 0);
267 c->status = gtk_progress_bar_new();
268 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(c->status), 0);
269 gtk_widget_set_size_request(c->status, 20, -1);
271 c->top_box = gtk_hbox_new(FALSE, 0);
272 gtk_box_pack_start(GTK_BOX(c->top_box), c->status, FALSE, FALSE, 2);
273 gtk_box_pack_start(GTK_BOX(c->top_box), c->location, TRUE, TRUE, 0);
274 gtk_box_pack_end(GTK_BOX(c->top_box), c->progress, FALSE, TRUE, 2);
276 c->vbox = gtk_vbox_new(FALSE, 0);
277 gtk_box_pack_start(GTK_BOX(c->vbox), c->top_box, FALSE, FALSE, 2);
278 gtk_container_add(GTK_CONTAINER(c->vbox), c->scroll);
280 gtk_container_add(GTK_CONTAINER(c->win), c->vbox);
282 gtk_widget_grab_focus(c->web_view);
283 gtk_widget_show_all(c->win);
287 f = ensure_url_scheme(uri);
288 if (show_all_requests)
289 fprintf(stderr, "====> %s\n", uri);
290 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
296 return WEBKIT_WEB_VIEW(c->web_view);
300 client_new_request(WebKitWebView *web_view, WebKitWebFrame *frame, gpointer data)
306 return client_new(NULL);
310 cooperation_setup(void)
315 fifopath = g_build_filename(g_get_user_runtime_dir(), __NAME__".fifo", NULL);
317 if (!g_file_test(fifopath, G_FILE_TEST_EXISTS))
318 mkfifo(fifopath, 0600);
320 cooperative_pipe_fp = open(fifopath, O_WRONLY | O_NONBLOCK);
321 if (!cooperative_pipe_fp)
323 fprintf(stderr, __NAME__": Can't open FIFO at all.\n");
327 if (write(cooperative_pipe_fp, "", 0) == -1)
329 /* Could not do an empty write to the FIFO which means there's
330 * no one listening. */
331 close(cooperative_pipe_fp);
332 towatch = g_io_channel_new_file(fifopath, "r+", NULL);
333 g_io_add_watch(towatch, G_IO_IN, (GIOFunc)remote_msg, NULL);
336 cooperative_alone = FALSE;
343 changed_load_progress(GObject *obj, GParamSpec *pspec, gpointer data)
345 struct Client *c = (struct Client *)data;
351 p = webkit_web_view_get_progress(WEBKIT_WEB_VIEW(c->web_view));
352 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(c->progress), p);
356 changed_title(GObject *obj, GParamSpec *pspec, gpointer data)
359 struct Client *c = (struct Client *)data;
364 t = webkit_web_view_get_title(WEBKIT_WEB_VIEW(c->web_view));
365 gtk_window_set_title(GTK_WINDOW(c->win), (t == NULL ? __NAME__ : t));
369 changed_uri(GObject *obj, GParamSpec *pspec, gpointer data)
372 struct Client *c = (struct Client *)data;
377 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
378 gtk_entry_set_text(GTK_ENTRY(c->location), (t == NULL ? __NAME__ : t));
382 download_handle(WebKitWebView *web_view, WebKitDownload *download, gpointer data)
384 struct Client *c = (struct Client *)data;
385 gchar *path, *path2 = NULL, *uri;
392 path = g_build_filename(download_dir,
393 webkit_download_get_suggested_filename(download),
395 path2 = g_strdup(path);
396 while (g_file_test(path2, G_FILE_TEST_EXISTS) && suffix < 1000)
400 path2 = g_strdup_printf("%s.%d", path, suffix);
406 fprintf(stderr, __NAME__": Suffix reached limit for download.\n");
411 uri = g_filename_to_uri(path2, NULL, NULL);
412 webkit_download_set_destination_uri(download, uri);
416 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(c->status), 1);
417 downloads_indicated++;
418 g_timeout_add(500, download_reset_indicator, c);
428 download_reset_indicator(gpointer data)
430 struct Client *c = (struct Client *)data;
432 downloads_indicated--;
433 if (downloads_indicated == 0)
434 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(c->status), 0);
440 download_request(WebKitWebView *web_view, WebKitWebFrame *frame,
441 WebKitNetworkRequest *request, gchar *mime_type,
442 WebKitWebPolicyDecision *policy_decision, gpointer data)
448 if (!webkit_web_view_can_show_mime_type(web_view, mime_type))
450 webkit_web_policy_decision_download(policy_decision);
457 ensure_url_scheme(const gchar *t)
461 f = g_ascii_strdown(t, -1);
462 if (!g_str_has_prefix(f, "http:") &&
463 !g_str_has_prefix(f, "https:") &&
464 !g_str_has_prefix(f, "file:") &&
465 !g_str_has_prefix(f, "about:"))
468 f = g_strdup_printf("http://%s", t);
476 grab_environment_configuration(void)
480 e = g_getenv(__NAME_UPPERCASE__"_ACCEPTED_LANGUAGE");
482 accepted_language = g_strdup(e);
484 e = g_getenv(__NAME_UPPERCASE__"_DOWNLOAD_DIR");
486 download_dir = g_strdup(e);
488 e = g_getenv(__NAME_UPPERCASE__"_ZOOM");
490 global_zoom = atof(e);
494 hover_web_view(WebKitWebView *web_view, gchar *title, gchar *uri, gpointer data)
496 struct Client *c = (struct Client *)data;
501 if (!gtk_widget_is_focus(c->location))
504 gtk_entry_set_text(GTK_ENTRY(c->location),
505 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view)));
507 gtk_entry_set_text(GTK_ENTRY(c->location), uri);
512 key_location(GtkWidget *widget, GdkEvent *event, gpointer data)
514 struct Client *c = (struct Client *)data;
520 if (event->type == GDK_KEY_PRESS)
522 switch (((GdkEventKey *)event)->keyval)
525 gtk_widget_grab_focus(c->web_view);
526 t = gtk_entry_get_text(GTK_ENTRY(c->location));
527 if (t != NULL && t[0] == '/')
529 if (search_text != NULL)
531 search_text = g_strdup(t + 1); /* XXX whacky */
536 f = ensure_url_scheme(t);
537 if (show_all_requests)
538 fprintf(stderr, "====> %s\n", f);
539 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
544 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
545 gtk_entry_set_text(GTK_ENTRY(c->location),
546 (t == NULL ? __NAME__ : t));
555 key_web_view(GtkWidget *widget, GdkEvent *event, gpointer data)
557 struct Client *c = (struct Client *)data;
558 WebKitHitTestResultContext ht_context;
559 WebKitHitTestResult *ht_result = NULL;
560 gchar *ht_uri = NULL, *f;
564 if (event->type == GDK_KEY_PRESS)
566 if (((GdkEventKey *)event)->state & GDK_CONTROL_MASK)
568 switch (((GdkEventKey *)event)->keyval)
571 gtk_widget_grab_focus(c->location);
574 scroll(gtk_scrolled_window_get_hadjustment(
575 GTK_SCROLLED_WINDOW(c->scroll)), 0, -1);
578 scroll(gtk_scrolled_window_get_vadjustment(
579 GTK_SCROLLED_WINDOW(c->scroll)), 0, 1);
582 scroll(gtk_scrolled_window_get_vadjustment(
583 GTK_SCROLLED_WINDOW(c->scroll)), 0, -1);
586 scroll(gtk_scrolled_window_get_hadjustment(
587 GTK_SCROLLED_WINDOW(c->scroll)), 0, 1);
590 scroll(gtk_scrolled_window_get_vadjustment(
591 GTK_SCROLLED_WINDOW(c->scroll)), 1, 0.5);
594 scroll(gtk_scrolled_window_get_vadjustment(
595 GTK_SCROLLED_WINDOW(c->scroll)), 1, -0.5);
604 f = ensure_url_scheme(first_uri);
605 if (show_all_requests)
606 fprintf(stderr, "====> %s\n", f);
607 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
612 else if (((GdkEventKey *)event)->keyval == GDK_KEY_Escape)
614 webkit_web_view_stop_loading(WEBKIT_WEB_VIEW(c->web_view));
615 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(c->progress), 0);
618 else if (event->type == GDK_BUTTON_PRESS)
620 switch (((GdkEventButton *)event)->button)
623 ht_result = webkit_web_view_get_hit_test_result(
624 WEBKIT_WEB_VIEW(c->web_view),
625 (GdkEventButton *)event);
626 g_object_get(ht_result, "context", &ht_context, NULL);
627 if (ht_context & WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK)
629 g_object_get(ht_result, "link-uri", &ht_uri, NULL);
631 g_object_unref(ht_result);
634 g_object_unref(ht_result);
637 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
640 webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
649 remote_msg(GIOChannel *channel, GIOCondition condition, gpointer data)
656 g_io_channel_read_line(channel, &uri, NULL, NULL, NULL);
667 search(gpointer data, gint direction)
669 struct Client *c = (struct Client *)data;
671 if (search_text == NULL)
674 webkit_web_view_search_text(WEBKIT_WEB_VIEW(c->web_view), search_text,
675 FALSE, direction == 1, TRUE);
679 scroll(GtkAdjustment *a, gint step_type, gdouble factor)
681 gdouble new, lower, upper, step;
682 lower = gtk_adjustment_get_lower(a);
683 upper = gtk_adjustment_get_upper(a) - gtk_adjustment_get_page_size(a) + lower;
685 step = gtk_adjustment_get_step_increment(a);
687 step = gtk_adjustment_get_page_increment(a);
688 new = gtk_adjustment_get_value(a) + factor * step;
689 new = new < lower ? lower : new;
690 new = new > upper ? upper : new;
691 gtk_adjustment_set_value(a, new);
698 GIOChannel *tabbed_stdout_channel;
700 gchar *output = NULL;
701 char *argv[] = { "tabbed", "-c", "-d", "-p", "s1", "-n", __NAME__, NULL };
704 if (!g_spawn_async_with_pipes(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL,
705 NULL, NULL, NULL, &tabbed_stdout, NULL,
708 fprintf(stderr, __NAME__": Could not launch tabbed: %s\n", err->message);
713 tabbed_stdout_channel = g_io_channel_unix_new(tabbed_stdout);
714 g_io_channel_read_line(tabbed_stdout_channel, &output, NULL, NULL, NULL);
717 fprintf(stderr, __NAME__": Could not read XID from tabbed\n");
721 g_io_channel_shutdown(tabbed_stdout_channel, FALSE, NULL);
724 plug_into = strtol(output, NULL, 16);
732 fprintf(stderr, "Usage: "__NAME__" [OPTION]... <URI>...\n");
738 main(int argc, char **argv)
742 gtk_init(&argc, &argv);
744 grab_environment_configuration();
746 while ((opt = getopt(argc, argv, "e:rCT")) != -1)
751 embed = atol(optarg);
752 tabbed_automagic = FALSE;
755 show_all_requests = TRUE;
758 cooperative_instances = FALSE;
761 tabbed_automagic = FALSE;
774 if (tabbed_automagic && !(cooperative_instances && !cooperative_alone))
775 embed = tabbed_launch();
777 first_uri = g_strdup(argv[optind]);
778 for (i = optind; i < argc; i++)
780 if (!cooperative_instances || cooperative_alone)