10 #include <gdk/gdkkeysyms.h>
12 #include <webkit/webkit.h>
15 #define DOWNLOAD_DIR "/tmp/tmp"
16 #define ZEA_LANGUAGE "en-US"
19 static void zea_adblock(WebKitWebView *, WebKitWebFrame *, WebKitWebResource *,
20 WebKitNetworkRequest *, WebKitNetworkResponse *, gpointer);
21 static void zea_destroy_client(GtkWidget *, gpointer);
22 static gboolean zea_do_download(WebKitWebView *, WebKitDownload *, gpointer);
23 static gboolean zea_download_request(WebKitWebView *, WebKitWebFrame *,
24 WebKitNetworkRequest *, gchar *,
25 WebKitWebPolicyDecision *, gpointer);
26 static Window zea_launch_tabbed(void);
27 static void zea_load_adblock(void);
28 static void zea_load_status_changed(GObject *obj, GParamSpec *pspec,
30 static gboolean zea_location_key(GtkWidget *, GdkEvent *, gpointer);
31 static void zea_new_client(const gchar *uri);
32 static gboolean zea_new_client_request(WebKitWebView *, WebKitWebFrame *,
33 WebKitNetworkRequest *,
34 WebKitWebNavigationAction *,
35 WebKitWebPolicyDecision *, gpointer);
36 static gboolean zea_remote_msg(GIOChannel *, GIOCondition, gpointer);
37 static void zea_search(gpointer, gint);
38 static void zea_setup_cooperation(void);
39 static void zea_scroll(GtkAdjustment *, gint, gdouble);
40 static void zea_title_changed(GObject *, GParamSpec *, gpointer);
41 static void zea_uri_changed(GObject *, GParamSpec *, gpointer);
42 static void zea_usage(void);
43 static void zea_web_view_hover(WebKitWebView *, gchar *, gchar *, gpointer);
44 static gboolean zea_web_view_key(GtkWidget *, GdkEvent *, gpointer);
47 static Window embed = 0;
48 static gint clients = 0;
49 static gdouble global_zoom = 1.0;
50 static gchar *search_text = NULL;
51 static gchar *first_uri = NULL;
52 static gboolean show_all_requests = FALSE;
53 static GSList *adblock_patterns = NULL;
54 static gboolean cooperative_instances = TRUE;
55 static int cooperative_pipe_fp = 0;
56 static gboolean alone = TRUE;
57 static gboolean launch_tabbed = TRUE;
58 static gboolean language_set = FALSE;
73 zea_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);
104 zea_destroy_client(GtkWidget *obj, gpointer data)
106 struct Client *c = (struct Client *)data;
110 webkit_web_view_stop_loading(WEBKIT_WEB_VIEW(c->web_view));
111 gtk_widget_destroy(c->web_view);
112 gtk_widget_destroy(c->scroll);
113 gtk_widget_destroy(c->status);
114 gtk_widget_destroy(c->location);
115 gtk_widget_destroy(c->vbox);
116 gtk_widget_destroy(c->win);
125 zea_do_download(WebKitWebView *web_view, WebKitDownload *download, gpointer data)
134 uri = webkit_download_get_uri(download);
139 ret = execlp("xterm", "xterm", "-hold", "-e", "wget", uri, NULL);
142 if (snprintf(id, 16, "%ld", embed) >= 16)
144 fprintf(stderr, "zea: id for xterm embed truncated!\n");
147 ret = execlp("xterm", "xterm", "-hold", "-into", id, "-e", "wget",
153 fprintf(stderr, "zea: exec'ing xterm for download");
163 zea_download_request(WebKitWebView *web_view, WebKitWebFrame *frame,
164 WebKitNetworkRequest *request, gchar *mime_type,
165 WebKitWebPolicyDecision *policy_decision,
172 if (!webkit_web_view_can_show_mime_type(web_view, mime_type))
174 webkit_web_policy_decision_download(policy_decision);
181 zea_location_key(GtkWidget *widget, GdkEvent *event, gpointer data)
183 struct Client *c = (struct Client *)data;
188 if (event->type == GDK_KEY_PRESS)
190 if (((GdkEventKey *)event)->keyval == GDK_KEY_Return)
192 gtk_widget_grab_focus(c->web_view);
193 t = gtk_entry_get_text(GTK_ENTRY(c->location));
194 if (t != NULL && t[0] == '/')
196 if (search_text != NULL)
198 search_text = g_strdup(t + 1); /* XXX whacky */
202 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), t);
211 zea_new_client(const gchar *uri)
213 if (cooperative_instances && !alone)
215 write(cooperative_pipe_fp, uri, strlen(uri));
216 write(cooperative_pipe_fp, "\n", 1);
220 struct Client *c = malloc(sizeof(struct Client));
223 fprintf(stderr, "zea: fatal: malloc failed\n");
230 c->win = gtk_plug_new(embed);
231 if (!gtk_plug_get_embedded(GTK_PLUG(c->win)))
233 fprintf(stderr, "zea: Can't plug-in to XID %ld.\n", embed);
234 gtk_widget_destroy(c->win);
241 c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
243 /* When using Gtk2, zea only shows a white area when run in
244 * suckless' tabbed. It appears we need to set a default window size
245 * for this to work. This is not needed when using Gtk3. */
246 gtk_window_set_default_size(GTK_WINDOW(c->win), 1024, 768);
248 g_signal_connect(G_OBJECT(c->win), "destroy",
249 G_CALLBACK(zea_destroy_client), c);
250 gtk_window_set_title(GTK_WINDOW(c->win), "zea");
252 c->web_view = webkit_web_view_new();
253 webkit_web_view_set_full_content_zoom(WEBKIT_WEB_VIEW(c->web_view), TRUE);
254 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), global_zoom);
255 g_signal_connect(G_OBJECT(c->web_view), "notify::title",
256 G_CALLBACK(zea_title_changed), c);
257 g_signal_connect(G_OBJECT(c->web_view), "notify::uri",
258 G_CALLBACK(zea_uri_changed), c);
259 g_signal_connect(G_OBJECT(c->web_view), "notify::load-status",
260 G_CALLBACK(zea_load_status_changed), c);
261 g_signal_connect(G_OBJECT(c->web_view),
262 "new-window-policy-decision-requested",
263 G_CALLBACK(zea_new_client_request), NULL);
264 g_signal_connect(G_OBJECT(c->web_view),
265 "mime-type-policy-decision-requested",
266 G_CALLBACK(zea_download_request), NULL);
267 g_signal_connect(G_OBJECT(c->web_view), "download-requested",
268 G_CALLBACK(zea_do_download), NULL);
269 g_signal_connect(G_OBJECT(c->web_view), "key-press-event",
270 G_CALLBACK(zea_web_view_key), c);
271 g_signal_connect(G_OBJECT(c->web_view), "hovering-over-link",
272 G_CALLBACK(zea_web_view_hover), c);
273 g_signal_connect(G_OBJECT(c->web_view), "resource-request-starting",
274 G_CALLBACK(zea_adblock), NULL);
278 g_object_set(webkit_get_default_session(), "accept-language",
283 c->scroll = gtk_scrolled_window_new(NULL, NULL);
285 gtk_container_add(GTK_CONTAINER(c->scroll), c->web_view);
287 c->location = gtk_entry_new();
288 g_signal_connect(G_OBJECT(c->location), "key-press-event",
289 G_CALLBACK(zea_location_key), c);
291 c->status = gtk_statusbar_new();
292 gtk_statusbar_set_has_resize_grip(GTK_STATUSBAR(c->status), FALSE);
294 c->vbox = gtk_vbox_new(FALSE, 2);
295 gtk_box_pack_start(GTK_BOX(c->vbox), c->location, FALSE, FALSE, 0);
296 gtk_container_add(GTK_CONTAINER(c->vbox), c->scroll);
297 gtk_box_pack_end(GTK_BOX(c->vbox), c->status, FALSE, FALSE, 0);
299 gtk_container_add(GTK_CONTAINER(c->win), c->vbox);
301 gtk_widget_grab_focus(c->web_view);
302 gtk_widget_show_all(c->win);
304 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), uri);
310 zea_new_client_request(WebKitWebView *web_view, WebKitWebFrame *frame,
311 WebKitNetworkRequest *request,
312 WebKitWebNavigationAction *navigation_action,
313 WebKitWebPolicyDecision *policy_decision,
318 (void)navigation_action;
321 webkit_web_policy_decision_ignore(policy_decision);
322 zea_new_client(webkit_network_request_get_uri(request));
328 zea_remote_msg(GIOChannel *channel, GIOCondition condition, gpointer data)
335 g_io_channel_read_line(channel, &uri, NULL, NULL, NULL);
346 zea_search(gpointer data, gint direction)
348 struct Client *c = (struct Client *)data;
350 if (search_text == NULL)
353 webkit_web_view_search_text(WEBKIT_WEB_VIEW(c->web_view), search_text,
354 FALSE, direction == 1, TRUE);
358 zea_setup_cooperation(void)
363 fifopath = g_build_filename(g_get_user_runtime_dir(), "zea.fifo", NULL);
365 if (!g_file_test(fifopath, G_FILE_TEST_EXISTS))
366 mkfifo(fifopath, 0600);
368 cooperative_pipe_fp = open(fifopath, O_WRONLY | O_NONBLOCK);
369 if (!cooperative_pipe_fp)
371 fprintf(stderr, "zea: Can't open FIFO at all.\n");
375 if (write(cooperative_pipe_fp, "", 0) == -1)
377 /* Could not do an empty write to the FIFO which means there's
378 * no one listening. */
379 close(cooperative_pipe_fp);
380 towatch = g_io_channel_new_file(fifopath, "r+", NULL);
381 g_io_add_watch(towatch, G_IO_IN, (GIOFunc)zea_remote_msg, NULL);
391 zea_scroll(GtkAdjustment *a, gint step_type, gdouble factor)
393 gdouble new, lower, upper, step;
394 lower = gtk_adjustment_get_lower(a);
395 upper = gtk_adjustment_get_upper(a) - gtk_adjustment_get_page_size(a) + lower;
397 step = gtk_adjustment_get_step_increment(a);
399 step = gtk_adjustment_get_page_increment(a);
400 new = gtk_adjustment_get_value(a) + factor * step;
401 new = new < lower ? lower : new;
402 new = new > upper ? upper : new;
403 gtk_adjustment_set_value(a, new);
407 zea_title_changed(GObject *obj, GParamSpec *pspec, gpointer data)
410 struct Client *c = (struct Client *)data;
415 t = webkit_web_view_get_title(WEBKIT_WEB_VIEW(c->web_view));
416 gtk_window_set_title(GTK_WINDOW(c->win), (t == NULL ? "zea" : t));
420 zea_uri_changed(GObject *obj, GParamSpec *pspec, gpointer data)
423 struct Client *c = (struct Client *)data;
428 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
429 gtk_entry_set_text(GTK_ENTRY(c->location), (t == NULL ? "zea" : t));
435 fprintf(stderr, "Usage: zea [OPTION]... <URI>...\n");
440 zea_launch_tabbed(void)
443 GIOChannel *tabbed_stdout_channel;
445 gchar *output = NULL;
446 char *argv[] = { "tabbed", "-c", "-d", NULL };
449 if (!g_spawn_async_with_pipes(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL,
450 NULL, NULL, NULL, &tabbed_stdout, NULL,
453 fprintf(stderr, "zea: Could not launch tabbed: %s\n", err->message);
458 tabbed_stdout_channel = g_io_channel_unix_new(tabbed_stdout);
459 g_io_channel_read_line(tabbed_stdout_channel, &output, NULL, NULL, NULL);
462 fprintf(stderr, "zea: Could not read XID from tabbed\n");
466 g_io_channel_shutdown(tabbed_stdout_channel, FALSE, NULL);
469 plug_into = strtol(output, NULL, 16);
475 zea_load_adblock(void)
479 GIOChannel *channel = NULL;
483 path = g_build_filename(g_get_user_config_dir(), "zea", "adblock.black", NULL);
484 channel = g_io_channel_new_file(path, "r", &err);
487 while (g_io_channel_read_line(channel, &buf, NULL, NULL, NULL)
488 == G_IO_STATUS_NORMAL)
491 re = g_regex_new(buf,
492 G_REGEX_CASELESS | G_REGEX_OPTIMIZE,
493 G_REGEX_MATCH_PARTIAL, &err);
496 fprintf(stderr, "zea: Could not compile regex: %s\n", buf);
500 adblock_patterns = g_slist_append(adblock_patterns, re);
509 zea_load_status_changed(GObject *obj, GParamSpec *pspec, gpointer data)
511 struct Client *c = (struct Client *)data;
516 if (webkit_web_view_get_load_status(WEBKIT_WEB_VIEW(c->web_view))
517 == WEBKIT_LOAD_FINISHED)
519 gtk_statusbar_pop(GTK_STATUSBAR(c->status), 1);
520 gtk_statusbar_push(GTK_STATUSBAR(c->status), 1, "Finished.");
524 gtk_statusbar_pop(GTK_STATUSBAR(c->status), 1);
525 gtk_statusbar_push(GTK_STATUSBAR(c->status), 1, "Loading...");
530 zea_web_view_hover(WebKitWebView *web_view, gchar *title, gchar *uri,
533 struct Client *c = (struct Client *)data;
538 gtk_statusbar_pop(GTK_STATUSBAR(c->status), 0);
540 gtk_statusbar_push(GTK_STATUSBAR(c->status), 0, uri);
544 zea_web_view_key(GtkWidget *widget, GdkEvent *event, gpointer data)
546 struct Client *c = (struct Client *)data;
550 if (event->type == GDK_KEY_PRESS)
552 if (((GdkEventKey *)event)->state & GDK_CONTROL_MASK)
554 if (((GdkEventKey *)event)->keyval == GDK_KEY_o)
556 gtk_widget_grab_focus(c->location);
559 else if (((GdkEventKey *)event)->keyval == GDK_KEY_h)
561 zea_scroll(gtk_scrolled_window_get_hadjustment(
562 GTK_SCROLLED_WINDOW(c->scroll)), 0, -1);
565 else if (((GdkEventKey *)event)->keyval == GDK_KEY_j)
567 zea_scroll(gtk_scrolled_window_get_vadjustment(
568 GTK_SCROLLED_WINDOW(c->scroll)), 0, 1);
571 else if (((GdkEventKey *)event)->keyval == GDK_KEY_k)
573 zea_scroll(gtk_scrolled_window_get_vadjustment(
574 GTK_SCROLLED_WINDOW(c->scroll)), 0, -1);
577 else if (((GdkEventKey *)event)->keyval == GDK_KEY_l)
579 zea_scroll(gtk_scrolled_window_get_hadjustment(
580 GTK_SCROLLED_WINDOW(c->scroll)), 0, 1);
583 else if (((GdkEventKey *)event)->keyval == GDK_KEY_f)
585 zea_scroll(gtk_scrolled_window_get_vadjustment(
586 GTK_SCROLLED_WINDOW(c->scroll)), 1, 0.5);
589 else if (((GdkEventKey *)event)->keyval == GDK_KEY_b)
591 zea_scroll(gtk_scrolled_window_get_vadjustment(
592 GTK_SCROLLED_WINDOW(c->scroll)), 1, -0.5);
595 else if (((GdkEventKey *)event)->keyval == GDK_KEY_n)
600 else if (((GdkEventKey *)event)->keyval == GDK_KEY_p)
605 else if (((GdkEventKey *)event)->keyval == GDK_KEY_g)
607 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view),
612 else if (((GdkEventKey *)event)->keyval == GDK_KEY_Escape)
614 webkit_web_view_stop_loading(WEBKIT_WEB_VIEW(c->web_view));
615 gtk_statusbar_pop(GTK_STATUSBAR(c->status), 1);
616 gtk_statusbar_push(GTK_STATUSBAR(c->status), 1, "Aborted.");
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 launch_tabbed = FALSE;
642 show_all_requests = TRUE;
645 cooperative_instances = FALSE;
648 launch_tabbed = FALSE;
659 zea_setup_cooperation();
661 if (launch_tabbed && !(cooperative_instances && !alone))
662 embed = zea_launch_tabbed();
664 first_uri = g_strdup(argv[optind]);
665 for (i = optind; i < argc; i++)
666 zea_new_client(argv[i]);
667 if (!cooperative_instances || alone)