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 void hover_web_view(WebKitWebView *, gchar *, gchar *, gpointer);
38 static gboolean key_location(GtkWidget *, GdkEvent *, gpointer);
39 static gboolean key_web_view(GtkWidget *, GdkEvent *, gpointer);
40 static gboolean remote_msg(GIOChannel *, GIOCondition, gpointer);
41 static void search(gpointer, gint);
42 static void scroll(GtkAdjustment *, gint, gdouble);
43 static Window tabbed_launch(void);
44 static void usage(void);
58 static GSList *adblock_patterns = NULL;
59 static gint clients = 0;
60 static gboolean cooperative_alone = TRUE;
61 static gboolean cooperative_instances = TRUE;
62 static int cooperative_pipe_fp = 0;
63 static Window embed = 0;
64 static gchar *first_uri = NULL;
65 static gdouble global_zoom = 1.0;
66 static gboolean language_is_set = FALSE;
67 static gchar *search_text = NULL;
68 static gboolean show_all_requests = FALSE;
69 static gboolean tabbed_automagic = TRUE;
73 adblock(WebKitWebView *web_view, WebKitWebFrame *frame,
74 WebKitWebResource *resource, WebKitNetworkRequest *request,
75 WebKitNetworkResponse *response, gpointer data)
77 GSList *it = adblock_patterns;
86 uri = webkit_network_request_get_uri(request);
87 if (show_all_requests)
88 fprintf(stderr, "-> %s\n", uri);
92 if (g_regex_match((GRegex *)(it->data), uri, 0, NULL))
94 webkit_network_request_set_uri(request, "about:blank");
95 if (show_all_requests)
96 fprintf(stderr, "\tBLOCKED!\n");
99 it = g_slist_next(it);
108 GIOChannel *channel = NULL;
112 path = g_build_filename(g_get_user_config_dir(), __NAME__, "adblock.black",
114 channel = g_io_channel_new_file(path, "r", &err);
117 while (g_io_channel_read_line(channel, &buf, NULL, NULL, NULL)
118 == G_IO_STATUS_NORMAL)
121 re = g_regex_new(buf,
122 G_REGEX_CASELESS | G_REGEX_OPTIMIZE,
123 G_REGEX_MATCH_PARTIAL, &err);
126 fprintf(stderr, __NAME__": Could not compile regex: %s\n", buf);
130 adblock_patterns = g_slist_append(adblock_patterns, re);
139 client_destroy(GtkWidget *obj, gpointer data)
141 struct Client *c = (struct Client *)data;
145 webkit_web_view_stop_loading(WEBKIT_WEB_VIEW(c->web_view));
146 gtk_widget_destroy(c->web_view);
147 gtk_widget_destroy(c->scroll);
148 gtk_widget_destroy(c->status);
149 gtk_widget_destroy(c->location);
150 gtk_widget_destroy(c->vbox);
151 gtk_widget_destroy(c->win);
160 client_new(const gchar *uri)
162 if (cooperative_instances && !cooperative_alone)
164 write(cooperative_pipe_fp, uri, strlen(uri));
165 write(cooperative_pipe_fp, "\n", 1);
169 struct Client *c = malloc(sizeof(struct Client));
172 fprintf(stderr, __NAME__": fatal: malloc failed\n");
179 c->win = gtk_plug_new(embed);
180 if (!gtk_plug_get_embedded(GTK_PLUG(c->win)))
182 fprintf(stderr, __NAME__": Can't plug-in to XID %ld.\n", embed);
183 gtk_widget_destroy(c->win);
190 c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
192 /* When using Gtk2, it only shows a white area when run in suckless'
193 * tabbed. It appears we need to set a default window size for this
194 * to work. This is not needed when using Gtk3. */
195 gtk_window_set_default_size(GTK_WINDOW(c->win), 1024, 768);
197 g_signal_connect(G_OBJECT(c->win), "destroy",
198 G_CALLBACK(client_destroy), c);
199 gtk_window_set_title(GTK_WINDOW(c->win), __NAME__);
201 c->web_view = webkit_web_view_new();
202 webkit_web_view_set_full_content_zoom(WEBKIT_WEB_VIEW(c->web_view), TRUE);
203 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), global_zoom);
204 g_signal_connect(G_OBJECT(c->web_view), "notify::title",
205 G_CALLBACK(changed_title), c);
206 g_signal_connect(G_OBJECT(c->web_view), "notify::uri",
207 G_CALLBACK(changed_uri), c);
208 g_signal_connect(G_OBJECT(c->web_view), "notify::load-status",
209 G_CALLBACK(changed_load_status), c);
210 g_signal_connect(G_OBJECT(c->web_view),
211 "new-window-policy-decision-requested",
212 G_CALLBACK(client_new_request), NULL);
213 g_signal_connect(G_OBJECT(c->web_view),
214 "mime-type-policy-decision-requested",
215 G_CALLBACK(download_request), NULL);
216 g_signal_connect(G_OBJECT(c->web_view), "download-requested",
217 G_CALLBACK(download_wget), NULL);
218 g_signal_connect(G_OBJECT(c->web_view), "key-press-event",
219 G_CALLBACK(key_web_view), c);
220 g_signal_connect(G_OBJECT(c->web_view), "hovering-over-link",
221 G_CALLBACK(hover_web_view), c);
222 g_signal_connect(G_OBJECT(c->web_view), "resource-request-starting",
223 G_CALLBACK(adblock), NULL);
225 if (!language_is_set)
227 g_object_set(webkit_get_default_session(), "accept-language", LANGUAGE,
229 language_is_set = TRUE;
232 c->scroll = gtk_scrolled_window_new(NULL, NULL);
234 gtk_container_add(GTK_CONTAINER(c->scroll), c->web_view);
236 c->location = gtk_entry_new();
237 g_signal_connect(G_OBJECT(c->location), "key-press-event",
238 G_CALLBACK(key_location), c);
240 c->status = gtk_statusbar_new();
241 gtk_statusbar_set_has_resize_grip(GTK_STATUSBAR(c->status), FALSE);
243 c->vbox = gtk_vbox_new(FALSE, 2);
244 gtk_box_pack_start(GTK_BOX(c->vbox), c->location, FALSE, FALSE, 0);
245 gtk_container_add(GTK_CONTAINER(c->vbox), c->scroll);
246 gtk_box_pack_end(GTK_BOX(c->vbox), c->status, FALSE, FALSE, 0);
248 gtk_container_add(GTK_CONTAINER(c->win), c->vbox);
250 gtk_widget_grab_focus(c->web_view);
251 gtk_widget_show_all(c->win);
253 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), uri);
259 client_new_request(WebKitWebView *web_view, WebKitWebFrame *frame,
260 WebKitNetworkRequest *request,
261 WebKitWebNavigationAction *navigation_action,
262 WebKitWebPolicyDecision *policy_decision, gpointer user_data)
266 (void)navigation_action;
269 webkit_web_policy_decision_ignore(policy_decision);
270 client_new(webkit_network_request_get_uri(request));
276 cooperation_setup(void)
281 fifopath = g_build_filename(g_get_user_runtime_dir(), __NAME__".fifo", NULL);
283 if (!g_file_test(fifopath, G_FILE_TEST_EXISTS))
284 mkfifo(fifopath, 0600);
286 cooperative_pipe_fp = open(fifopath, O_WRONLY | O_NONBLOCK);
287 if (!cooperative_pipe_fp)
289 fprintf(stderr, __NAME__": Can't open FIFO at all.\n");
293 if (write(cooperative_pipe_fp, "", 0) == -1)
295 /* Could not do an empty write to the FIFO which means there's
296 * no one listening. */
297 close(cooperative_pipe_fp);
298 towatch = g_io_channel_new_file(fifopath, "r+", NULL);
299 g_io_add_watch(towatch, G_IO_IN, (GIOFunc)remote_msg, NULL);
302 cooperative_alone = FALSE;
309 changed_load_status(GObject *obj, GParamSpec *pspec, gpointer data)
311 struct Client *c = (struct Client *)data;
316 if (webkit_web_view_get_load_status(WEBKIT_WEB_VIEW(c->web_view))
317 == WEBKIT_LOAD_FINISHED)
319 gtk_statusbar_pop(GTK_STATUSBAR(c->status), 1);
320 gtk_statusbar_push(GTK_STATUSBAR(c->status), 1, "Finished.");
324 gtk_statusbar_pop(GTK_STATUSBAR(c->status), 1);
325 gtk_statusbar_push(GTK_STATUSBAR(c->status), 1, "Loading...");
330 changed_title(GObject *obj, GParamSpec *pspec, gpointer data)
333 struct Client *c = (struct Client *)data;
338 t = webkit_web_view_get_title(WEBKIT_WEB_VIEW(c->web_view));
339 gtk_window_set_title(GTK_WINDOW(c->win), (t == NULL ? __NAME__ : t));
343 changed_uri(GObject *obj, GParamSpec *pspec, gpointer data)
346 struct Client *c = (struct Client *)data;
351 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
352 gtk_entry_set_text(GTK_ENTRY(c->location), (t == NULL ? __NAME__ : t));
356 download_request(WebKitWebView *web_view, WebKitWebFrame *frame,
357 WebKitNetworkRequest *request, gchar *mime_type,
358 WebKitWebPolicyDecision *policy_decision, gpointer data)
364 if (!webkit_web_view_can_show_mime_type(web_view, mime_type))
366 webkit_web_policy_decision_download(policy_decision);
373 download_wget(WebKitWebView *web_view, WebKitDownload *download, gpointer data)
382 uri = webkit_download_get_uri(download);
387 ret = execlp("xterm", "xterm", "-hold", "-e", "wget", uri, NULL);
390 if (snprintf(id, 16, "%ld", embed) >= 16)
392 fprintf(stderr, __NAME__": id for xterm embed truncated!\n");
395 ret = execlp("xterm", "xterm", "-hold", "-into", id, "-e", "wget",
401 fprintf(stderr, __NAME__": exec'ing xterm for download");
411 hover_web_view(WebKitWebView *web_view, gchar *title, gchar *uri,
414 struct Client *c = (struct Client *)data;
419 gtk_statusbar_pop(GTK_STATUSBAR(c->status), 0);
421 gtk_statusbar_push(GTK_STATUSBAR(c->status), 0, uri);
425 key_location(GtkWidget *widget, GdkEvent *event, gpointer data)
427 struct Client *c = (struct Client *)data;
432 if (event->type == GDK_KEY_PRESS)
434 if (((GdkEventKey *)event)->keyval == GDK_KEY_Return)
436 gtk_widget_grab_focus(c->web_view);
437 t = gtk_entry_get_text(GTK_ENTRY(c->location));
438 if (t != NULL && t[0] == '/')
440 if (search_text != NULL)
442 search_text = g_strdup(t + 1); /* XXX whacky */
446 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), t);
455 key_web_view(GtkWidget *widget, GdkEvent *event, gpointer data)
457 struct Client *c = (struct Client *)data;
461 if (event->type == GDK_KEY_PRESS)
463 if (((GdkEventKey *)event)->state & GDK_CONTROL_MASK)
465 if (((GdkEventKey *)event)->keyval == GDK_KEY_o)
467 gtk_widget_grab_focus(c->location);
470 else if (((GdkEventKey *)event)->keyval == GDK_KEY_h)
472 scroll(gtk_scrolled_window_get_hadjustment(
473 GTK_SCROLLED_WINDOW(c->scroll)), 0, -1);
476 else if (((GdkEventKey *)event)->keyval == GDK_KEY_j)
478 scroll(gtk_scrolled_window_get_vadjustment(
479 GTK_SCROLLED_WINDOW(c->scroll)), 0, 1);
482 else if (((GdkEventKey *)event)->keyval == GDK_KEY_k)
484 scroll(gtk_scrolled_window_get_vadjustment(
485 GTK_SCROLLED_WINDOW(c->scroll)), 0, -1);
488 else if (((GdkEventKey *)event)->keyval == GDK_KEY_l)
490 scroll(gtk_scrolled_window_get_hadjustment(
491 GTK_SCROLLED_WINDOW(c->scroll)), 0, 1);
494 else if (((GdkEventKey *)event)->keyval == GDK_KEY_f)
496 scroll(gtk_scrolled_window_get_vadjustment(
497 GTK_SCROLLED_WINDOW(c->scroll)), 1, 0.5);
500 else if (((GdkEventKey *)event)->keyval == GDK_KEY_b)
502 scroll(gtk_scrolled_window_get_vadjustment(
503 GTK_SCROLLED_WINDOW(c->scroll)), 1, -0.5);
506 else if (((GdkEventKey *)event)->keyval == GDK_KEY_n)
511 else if (((GdkEventKey *)event)->keyval == GDK_KEY_p)
516 else if (((GdkEventKey *)event)->keyval == GDK_KEY_g)
518 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view),
523 else if (((GdkEventKey *)event)->keyval == GDK_KEY_Escape)
525 webkit_web_view_stop_loading(WEBKIT_WEB_VIEW(c->web_view));
526 gtk_statusbar_pop(GTK_STATUSBAR(c->status), 1);
527 gtk_statusbar_push(GTK_STATUSBAR(c->status), 1, "Aborted.");
535 remote_msg(GIOChannel *channel, GIOCondition condition, gpointer data)
542 g_io_channel_read_line(channel, &uri, NULL, NULL, NULL);
553 search(gpointer data, gint direction)
555 struct Client *c = (struct Client *)data;
557 if (search_text == NULL)
560 webkit_web_view_search_text(WEBKIT_WEB_VIEW(c->web_view), search_text,
561 FALSE, direction == 1, TRUE);
565 scroll(GtkAdjustment *a, gint step_type, gdouble factor)
567 gdouble new, lower, upper, step;
568 lower = gtk_adjustment_get_lower(a);
569 upper = gtk_adjustment_get_upper(a) - gtk_adjustment_get_page_size(a) + lower;
571 step = gtk_adjustment_get_step_increment(a);
573 step = gtk_adjustment_get_page_increment(a);
574 new = gtk_adjustment_get_value(a) + factor * step;
575 new = new < lower ? lower : new;
576 new = new > upper ? upper : new;
577 gtk_adjustment_set_value(a, new);
584 GIOChannel *tabbed_stdout_channel;
586 gchar *output = NULL;
587 char *argv[] = { "tabbed", "-c", "-d", NULL };
590 if (!g_spawn_async_with_pipes(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL,
591 NULL, NULL, NULL, &tabbed_stdout, NULL,
594 fprintf(stderr, __NAME__": Could not launch tabbed: %s\n", err->message);
599 tabbed_stdout_channel = g_io_channel_unix_new(tabbed_stdout);
600 g_io_channel_read_line(tabbed_stdout_channel, &output, NULL, NULL, NULL);
603 fprintf(stderr, __NAME__": Could not read XID from tabbed\n");
607 g_io_channel_shutdown(tabbed_stdout_channel, FALSE, NULL);
610 plug_into = strtol(output, NULL, 16);
618 fprintf(stderr, "Usage: "__NAME__" [OPTION]... <URI>...\n");
624 main(int argc, char **argv)
628 gtk_init(&argc, &argv);
630 while ((opt = getopt(argc, argv, "z:e:rCT")) != -1)
635 global_zoom = atof(optarg);
638 embed = atol(optarg);
639 tabbed_automagic = FALSE;
642 show_all_requests = TRUE;
645 cooperative_instances = FALSE;
648 tabbed_automagic = FALSE;
661 if (tabbed_automagic && !(cooperative_instances && !cooperative_alone))
662 embed = tabbed_launch();
664 first_uri = g_strdup(argv[optind]);
665 for (i = optind; i < argc; i++)
667 if (!cooperative_instances || cooperative_alone)