10 #include <gdk/gdkkeysyms.h>
12 #include <webkit/webkit.h>
15 #define DOWNLOAD_DIR "/tmp/tmp"
16 #define LANGUAGE "en-US"
19 static void adblock(WebKitWebView *, WebKitWebFrame *, WebKitWebResource *,
20 WebKitNetworkRequest *, WebKitNetworkResponse *, gpointer);
21 static void adblock_load(void);
22 static void client_destroy(GtkWidget *, gpointer);
23 static void client_new(const gchar *uri);
24 static gboolean client_new_request(WebKitWebView *, WebKitWebFrame *,
25 WebKitNetworkRequest *,
26 WebKitWebNavigationAction *,
27 WebKitWebPolicyDecision *, gpointer);
28 static void cooperation_setup(void);
29 static void changed_load_status(GObject *obj, GParamSpec *pspec,
31 static void changed_title(GObject *, GParamSpec *, gpointer);
32 static void changed_uri(GObject *, GParamSpec *, gpointer);
33 static gboolean download_request(WebKitWebView *, WebKitWebFrame *,
34 WebKitNetworkRequest *, gchar *,
35 WebKitWebPolicyDecision *, gpointer);
36 static gboolean download_wget(WebKitWebView *, WebKitDownload *, gpointer);
37 static gchar *ensure_http_prefix(const gchar *);
38 static void hover_web_view(WebKitWebView *, gchar *, gchar *, gpointer);
39 static gboolean key_location(GtkWidget *, GdkEvent *, gpointer);
40 static gboolean key_web_view(GtkWidget *, GdkEvent *, gpointer);
41 static gboolean remote_msg(GIOChannel *, GIOCondition, gpointer);
42 static void search(gpointer, gint);
43 static void scroll(GtkAdjustment *, gint, gdouble);
44 static Window tabbed_launch(void);
45 static void usage(void);
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 Window embed = 0;
65 static gchar *first_uri = NULL;
66 static gdouble global_zoom = 1.0;
67 static gboolean language_is_set = FALSE;
68 static gchar *search_text = NULL;
69 static gboolean show_all_requests = FALSE;
70 static gboolean tabbed_automagic = TRUE;
74 adblock(WebKitWebView *web_view, WebKitWebFrame *frame,
75 WebKitWebResource *resource, WebKitNetworkRequest *request,
76 WebKitNetworkResponse *response, gpointer data)
78 GSList *it = adblock_patterns;
87 uri = webkit_network_request_get_uri(request);
88 if (show_all_requests)
89 fprintf(stderr, "-> %s\n", uri);
93 if (g_regex_match((GRegex *)(it->data), uri, 0, NULL))
95 webkit_network_request_set_uri(request, "about:blank");
96 if (show_all_requests)
97 fprintf(stderr, "\tBLOCKED!\n");
100 it = g_slist_next(it);
109 GIOChannel *channel = NULL;
113 path = g_build_filename(g_get_user_config_dir(), __NAME__, "adblock.black",
115 channel = g_io_channel_new_file(path, "r", &err);
118 while (g_io_channel_read_line(channel, &buf, NULL, NULL, NULL)
119 == G_IO_STATUS_NORMAL)
122 re = g_regex_new(buf,
123 G_REGEX_CASELESS | G_REGEX_OPTIMIZE,
124 G_REGEX_MATCH_PARTIAL, &err);
127 fprintf(stderr, __NAME__": Could not compile regex: %s\n", buf);
131 adblock_patterns = g_slist_append(adblock_patterns, re);
140 client_destroy(GtkWidget *obj, gpointer data)
142 struct Client *c = (struct Client *)data;
146 webkit_web_view_stop_loading(WEBKIT_WEB_VIEW(c->web_view));
147 gtk_widget_destroy(c->web_view);
148 gtk_widget_destroy(c->scroll);
149 gtk_widget_destroy(c->status);
150 gtk_widget_destroy(c->location);
151 gtk_widget_destroy(c->vbox);
152 gtk_widget_destroy(c->win);
161 client_new(const gchar *uri)
164 gchar *capitalized_name = NULL, *f;
166 if (cooperative_instances && !cooperative_alone)
168 write(cooperative_pipe_fp, uri, strlen(uri));
169 write(cooperative_pipe_fp, "\n", 1);
173 c = malloc(sizeof(struct Client));
176 fprintf(stderr, __NAME__": fatal: malloc failed\n");
183 c->win = gtk_plug_new(embed);
184 if (!gtk_plug_get_embedded(GTK_PLUG(c->win)))
186 fprintf(stderr, __NAME__": Can't plug-in to XID %ld.\n", embed);
187 gtk_widget_destroy(c->win);
195 c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
197 capitalized_name = g_strdup(__NAME__);
198 capitalized_name[0] = g_ascii_toupper(capitalized_name[0]);
199 gtk_window_set_wmclass(GTK_WINDOW(c->win), __NAME__, capitalized_name);
200 g_free(capitalized_name);
203 /* When using Gtk2, it only shows a white area when run in suckless'
204 * tabbed. It appears we need to set a default window size for this
205 * to work. This is not needed when using Gtk3. */
206 gtk_window_set_default_size(GTK_WINDOW(c->win), 1024, 768);
208 g_signal_connect(G_OBJECT(c->win), "destroy",
209 G_CALLBACK(client_destroy), c);
210 gtk_window_set_title(GTK_WINDOW(c->win), __NAME__);
212 c->web_view = webkit_web_view_new();
213 webkit_web_view_set_full_content_zoom(WEBKIT_WEB_VIEW(c->web_view), TRUE);
214 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), global_zoom);
215 g_signal_connect(G_OBJECT(c->web_view), "notify::title",
216 G_CALLBACK(changed_title), c);
217 g_signal_connect(G_OBJECT(c->web_view), "notify::uri",
218 G_CALLBACK(changed_uri), c);
219 g_signal_connect(G_OBJECT(c->web_view), "notify::load-status",
220 G_CALLBACK(changed_load_status), c);
221 g_signal_connect(G_OBJECT(c->web_view),
222 "new-window-policy-decision-requested",
223 G_CALLBACK(client_new_request), NULL);
224 g_signal_connect(G_OBJECT(c->web_view),
225 "mime-type-policy-decision-requested",
226 G_CALLBACK(download_request), NULL);
227 g_signal_connect(G_OBJECT(c->web_view), "download-requested",
228 G_CALLBACK(download_wget), NULL);
229 g_signal_connect(G_OBJECT(c->web_view), "key-press-event",
230 G_CALLBACK(key_web_view), c);
231 g_signal_connect(G_OBJECT(c->web_view), "button-press-event",
232 G_CALLBACK(key_web_view), c);
233 g_signal_connect(G_OBJECT(c->web_view), "hovering-over-link",
234 G_CALLBACK(hover_web_view), c);
235 g_signal_connect(G_OBJECT(c->web_view), "resource-request-starting",
236 G_CALLBACK(adblock), NULL);
238 if (!language_is_set)
240 g_object_set(webkit_get_default_session(), "accept-language", LANGUAGE,
242 language_is_set = TRUE;
245 c->scroll = gtk_scrolled_window_new(NULL, NULL);
247 gtk_container_add(GTK_CONTAINER(c->scroll), c->web_view);
249 c->location = gtk_entry_new();
250 g_signal_connect(G_OBJECT(c->location), "key-press-event",
251 G_CALLBACK(key_location), c);
253 c->status = gtk_statusbar_new();
254 gtk_statusbar_set_has_resize_grip(GTK_STATUSBAR(c->status), FALSE);
256 c->vbox = gtk_vbox_new(FALSE, 2);
257 gtk_box_pack_start(GTK_BOX(c->vbox), c->location, FALSE, FALSE, 0);
258 gtk_container_add(GTK_CONTAINER(c->vbox), c->scroll);
259 gtk_box_pack_end(GTK_BOX(c->vbox), c->status, FALSE, FALSE, 0);
261 gtk_container_add(GTK_CONTAINER(c->win), c->vbox);
263 gtk_widget_grab_focus(c->web_view);
264 gtk_widget_show_all(c->win);
266 f = ensure_http_prefix(uri);
267 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
274 client_new_request(WebKitWebView *web_view, WebKitWebFrame *frame,
275 WebKitNetworkRequest *request,
276 WebKitWebNavigationAction *navigation_action,
277 WebKitWebPolicyDecision *policy_decision, gpointer user_data)
281 (void)navigation_action;
284 webkit_web_policy_decision_ignore(policy_decision);
285 client_new(webkit_network_request_get_uri(request));
291 cooperation_setup(void)
296 fifopath = g_build_filename(g_get_user_runtime_dir(), __NAME__".fifo", NULL);
298 if (!g_file_test(fifopath, G_FILE_TEST_EXISTS))
299 mkfifo(fifopath, 0600);
301 cooperative_pipe_fp = open(fifopath, O_WRONLY | O_NONBLOCK);
302 if (!cooperative_pipe_fp)
304 fprintf(stderr, __NAME__": Can't open FIFO at all.\n");
308 if (write(cooperative_pipe_fp, "", 0) == -1)
310 /* Could not do an empty write to the FIFO which means there's
311 * no one listening. */
312 close(cooperative_pipe_fp);
313 towatch = g_io_channel_new_file(fifopath, "r+", NULL);
314 g_io_add_watch(towatch, G_IO_IN, (GIOFunc)remote_msg, NULL);
317 cooperative_alone = FALSE;
324 changed_load_status(GObject *obj, GParamSpec *pspec, gpointer data)
326 struct Client *c = (struct Client *)data;
331 if (webkit_web_view_get_load_status(WEBKIT_WEB_VIEW(c->web_view))
332 == WEBKIT_LOAD_FINISHED)
334 gtk_statusbar_pop(GTK_STATUSBAR(c->status), 1);
335 gtk_statusbar_push(GTK_STATUSBAR(c->status), 1, "Finished.");
339 gtk_statusbar_pop(GTK_STATUSBAR(c->status), 1);
340 gtk_statusbar_push(GTK_STATUSBAR(c->status), 1, "Loading...");
345 changed_title(GObject *obj, GParamSpec *pspec, gpointer data)
348 struct Client *c = (struct Client *)data;
353 t = webkit_web_view_get_title(WEBKIT_WEB_VIEW(c->web_view));
354 gtk_window_set_title(GTK_WINDOW(c->win), (t == NULL ? __NAME__ : t));
358 changed_uri(GObject *obj, GParamSpec *pspec, gpointer data)
361 struct Client *c = (struct Client *)data;
366 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
367 gtk_entry_set_text(GTK_ENTRY(c->location), (t == NULL ? __NAME__ : t));
371 download_request(WebKitWebView *web_view, WebKitWebFrame *frame,
372 WebKitNetworkRequest *request, gchar *mime_type,
373 WebKitWebPolicyDecision *policy_decision, gpointer data)
379 if (!webkit_web_view_can_show_mime_type(web_view, mime_type))
381 webkit_web_policy_decision_download(policy_decision);
388 download_wget(WebKitWebView *web_view, WebKitDownload *download, gpointer data)
397 uri = webkit_download_get_uri(download);
402 ret = execlp("xterm", "xterm", "-hold", "-e", "wget", uri, NULL);
405 if (snprintf(id, 16, "%ld", embed) >= 16)
407 fprintf(stderr, __NAME__": id for xterm embed truncated!\n");
410 ret = execlp("xterm", "xterm", "-hold", "-into", id, "-e", "wget",
416 fprintf(stderr, __NAME__": exec'ing xterm for download");
426 ensure_http_prefix(const gchar *t)
430 f = g_ascii_strdown(t, -1);
431 if (!g_str_has_prefix(f, "http://") &&
432 !g_str_has_prefix(f, "https://"))
435 f = g_strdup_printf("http://%s", t);
443 hover_web_view(WebKitWebView *web_view, gchar *title, gchar *uri,
446 struct Client *c = (struct Client *)data;
451 gtk_statusbar_pop(GTK_STATUSBAR(c->status), 0);
453 gtk_statusbar_push(GTK_STATUSBAR(c->status), 0, uri);
457 key_location(GtkWidget *widget, GdkEvent *event, gpointer data)
459 struct Client *c = (struct Client *)data;
465 if (event->type == GDK_KEY_PRESS)
467 switch (((GdkEventKey *)event)->keyval)
470 gtk_widget_grab_focus(c->web_view);
471 t = gtk_entry_get_text(GTK_ENTRY(c->location));
472 if (t != NULL && t[0] == '/')
474 if (search_text != NULL)
476 search_text = g_strdup(t + 1); /* XXX whacky */
481 f = ensure_http_prefix(t);
482 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
487 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
488 gtk_entry_set_text(GTK_ENTRY(c->location),
489 (t == NULL ? __NAME__ : t));
498 key_web_view(GtkWidget *widget, GdkEvent *event, gpointer data)
500 struct Client *c = (struct Client *)data;
501 WebKitHitTestResultContext ht_context;
502 WebKitHitTestResult *ht_result = NULL;
507 if (event->type == GDK_KEY_PRESS)
509 if (((GdkEventKey *)event)->state & GDK_CONTROL_MASK)
511 switch (((GdkEventKey *)event)->keyval)
514 gtk_widget_grab_focus(c->location);
517 scroll(gtk_scrolled_window_get_hadjustment(
518 GTK_SCROLLED_WINDOW(c->scroll)), 0, -1);
521 scroll(gtk_scrolled_window_get_vadjustment(
522 GTK_SCROLLED_WINDOW(c->scroll)), 0, 1);
525 scroll(gtk_scrolled_window_get_vadjustment(
526 GTK_SCROLLED_WINDOW(c->scroll)), 0, -1);
529 scroll(gtk_scrolled_window_get_hadjustment(
530 GTK_SCROLLED_WINDOW(c->scroll)), 0, 1);
533 scroll(gtk_scrolled_window_get_vadjustment(
534 GTK_SCROLLED_WINDOW(c->scroll)), 1, 0.5);
537 scroll(gtk_scrolled_window_get_vadjustment(
538 GTK_SCROLLED_WINDOW(c->scroll)), 1, -0.5);
547 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view),
552 else if (((GdkEventKey *)event)->keyval == GDK_KEY_Escape)
554 webkit_web_view_stop_loading(WEBKIT_WEB_VIEW(c->web_view));
555 gtk_statusbar_pop(GTK_STATUSBAR(c->status), 1);
556 gtk_statusbar_push(GTK_STATUSBAR(c->status), 1, "Aborted.");
559 else if (event->type == GDK_BUTTON_PRESS)
561 switch (((GdkEventButton *)event)->button)
564 ht_result = webkit_web_view_get_hit_test_result(
565 WEBKIT_WEB_VIEW(c->web_view),
566 (GdkEventButton *)event);
567 g_object_get(ht_result, "context", &ht_context, NULL);
568 if (ht_context & WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK)
570 g_object_get(ht_result, "link-uri", &ht_uri, NULL);
572 g_object_unref(ht_result);
575 g_object_unref(ht_result);
578 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
581 webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
590 remote_msg(GIOChannel *channel, GIOCondition condition, gpointer data)
597 g_io_channel_read_line(channel, &uri, NULL, NULL, NULL);
608 search(gpointer data, gint direction)
610 struct Client *c = (struct Client *)data;
612 if (search_text == NULL)
615 webkit_web_view_search_text(WEBKIT_WEB_VIEW(c->web_view), search_text,
616 FALSE, direction == 1, TRUE);
620 scroll(GtkAdjustment *a, gint step_type, gdouble factor)
622 gdouble new, lower, upper, step;
623 lower = gtk_adjustment_get_lower(a);
624 upper = gtk_adjustment_get_upper(a) - gtk_adjustment_get_page_size(a) + lower;
626 step = gtk_adjustment_get_step_increment(a);
628 step = gtk_adjustment_get_page_increment(a);
629 new = gtk_adjustment_get_value(a) + factor * step;
630 new = new < lower ? lower : new;
631 new = new > upper ? upper : new;
632 gtk_adjustment_set_value(a, new);
639 GIOChannel *tabbed_stdout_channel;
641 gchar *output = NULL;
642 char *argv[] = { "tabbed", "-c", "-d", "-n", __NAME__, NULL };
645 if (!g_spawn_async_with_pipes(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL,
646 NULL, NULL, NULL, &tabbed_stdout, NULL,
649 fprintf(stderr, __NAME__": Could not launch tabbed: %s\n", err->message);
654 tabbed_stdout_channel = g_io_channel_unix_new(tabbed_stdout);
655 g_io_channel_read_line(tabbed_stdout_channel, &output, NULL, NULL, NULL);
658 fprintf(stderr, __NAME__": Could not read XID from tabbed\n");
662 g_io_channel_shutdown(tabbed_stdout_channel, FALSE, NULL);
665 plug_into = strtol(output, NULL, 16);
673 fprintf(stderr, "Usage: "__NAME__" [OPTION]... <URI>...\n");
679 main(int argc, char **argv)
683 gtk_init(&argc, &argv);
685 while ((opt = getopt(argc, argv, "z:e:rCT")) != -1)
690 global_zoom = atof(optarg);
693 embed = atol(optarg);
694 tabbed_automagic = FALSE;
697 show_all_requests = TRUE;
700 cooperative_instances = FALSE;
703 tabbed_automagic = FALSE;
716 if (tabbed_automagic && !(cooperative_instances && !cooperative_alone))
717 embed = tabbed_launch();
719 first_uri = g_strdup(argv[optind]);
720 for (i = optind; i < argc; i++)
722 if (!cooperative_instances || cooperative_alone)