]> git.armaanb.net Git - chorizo.git/blob - browser.c
User configuration using environment variables
[chorizo.git] / browser.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <fcntl.h>
6 #include <string.h>
7
8 #include <gtk/gtk.h>
9 #include <gdk/gdkx.h>
10 #include <gdk/gdkkeysyms.h>
11 #include <gio/gio.h>
12 #include <webkit/webkit.h>
13
14
15 static void adblock(WebKitWebView *, WebKitWebFrame *, WebKitWebResource *,
16                     WebKitNetworkRequest *, WebKitNetworkResponse *, gpointer);
17 static void adblock_load(void);
18 static void client_destroy(GtkWidget *, gpointer);
19 static void client_new(const gchar *uri);
20 static gboolean client_new_request(WebKitWebView *, WebKitWebFrame *,
21                                    WebKitNetworkRequest *,
22                                    WebKitWebNavigationAction *,
23                                    WebKitWebPolicyDecision *, gpointer);
24 static void cooperation_setup(void);
25 static void changed_load_status(GObject *obj, GParamSpec *pspec,
26                                 gpointer data);
27 static void changed_title(GObject *, GParamSpec *, gpointer);
28 static void changed_uri(GObject *, GParamSpec *, gpointer);
29 static gboolean download_request(WebKitWebView *, WebKitWebFrame *,
30                                  WebKitNetworkRequest *, gchar *,
31                                  WebKitWebPolicyDecision *, gpointer);
32 static gboolean download_wget(WebKitWebView *, WebKitDownload *, 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);
43
44
45 struct Client
46 {
47         GtkWidget *win;
48         GtkWidget *vbox;
49         GtkWidget *location;
50         GtkWidget *status;
51         GtkWidget *scroll;
52         GtkWidget *web_view;
53 };
54
55
56 static gchar *accepted_language = "en-US";
57 static GSList *adblock_patterns = NULL;
58 static gint clients = 0;
59 static gboolean cooperative_alone = TRUE;
60 static gboolean cooperative_instances = TRUE;
61 static int cooperative_pipe_fp = 0;
62 static gchar *download_dir = "/tmp";
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;
70
71
72 void
73 adblock(WebKitWebView *web_view, WebKitWebFrame *frame,
74         WebKitWebResource *resource, WebKitNetworkRequest *request,
75         WebKitNetworkResponse *response, gpointer data)
76 {
77         GSList *it = adblock_patterns;
78         const gchar *uri;
79
80         (void)web_view;
81         (void)frame;
82         (void)resource;
83         (void)response;
84         (void)data;
85
86         uri = webkit_network_request_get_uri(request);
87         if (show_all_requests)
88                 fprintf(stderr, "-> %s\n", uri);
89
90         while (it)
91         {
92                 if (g_regex_match((GRegex *)(it->data), uri, 0, NULL))
93                 {
94                         webkit_network_request_set_uri(request, "about:blank");
95                         if (show_all_requests)
96                                 fprintf(stderr, "\tBLOCKED!\n");
97                         return;
98                 }
99                 it = g_slist_next(it);
100         }
101 }
102
103 void
104 adblock_load(void)
105 {
106         GRegex *re = NULL;
107         GError *err = NULL;
108         GIOChannel *channel = NULL;
109         gchar *path = NULL;
110         gchar *buf = NULL;
111
112         path = g_build_filename(g_get_user_config_dir(), __NAME__, "adblock.black",
113                                 NULL);
114         channel = g_io_channel_new_file(path, "r", &err);
115         if (channel != NULL)
116         {
117                 while (g_io_channel_read_line(channel, &buf, NULL, NULL, NULL)
118                        == G_IO_STATUS_NORMAL)
119                 {
120                         g_strstrip(buf);
121                         re = g_regex_new(buf,
122                                          G_REGEX_CASELESS | G_REGEX_OPTIMIZE,
123                                          G_REGEX_MATCH_PARTIAL, &err);
124                         if (err != NULL)
125                         {
126                                 fprintf(stderr, __NAME__": Could not compile regex: %s\n", buf);
127                                 g_error_free(err);
128                                 err = NULL;
129                         }
130                         adblock_patterns = g_slist_append(adblock_patterns, re);
131
132                         g_free(buf);
133                 }
134         }
135         g_free(path);
136 }
137
138 void
139 client_destroy(GtkWidget *obj, gpointer data)
140 {
141         struct Client *c = (struct Client *)data;
142
143         (void)obj;
144
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);
152         free(c);
153
154         clients--;
155         if (clients == 0)
156                 gtk_main_quit();
157 }
158
159 void
160 client_new(const gchar *uri)
161 {
162         struct Client *c;
163         gchar *capitalized_name = NULL, *f;
164
165         if (cooperative_instances && !cooperative_alone)
166         {
167                 write(cooperative_pipe_fp, uri, strlen(uri));
168                 write(cooperative_pipe_fp, "\n", 1);
169                 return;
170         }
171
172         c = malloc(sizeof(struct Client));
173         if (!c)
174         {
175                 fprintf(stderr, __NAME__": fatal: malloc failed\n");
176                 exit(EXIT_FAILURE);
177         }
178
179         c->win = NULL;
180         if (embed != 0)
181         {
182                 c->win = gtk_plug_new(embed);
183                 if (!gtk_plug_get_embedded(GTK_PLUG(c->win)))
184                 {
185                         fprintf(stderr, __NAME__": Can't plug-in to XID %ld.\n", embed);
186                         gtk_widget_destroy(c->win);
187                         c->win = NULL;
188                         embed = 0;
189                 }
190         }
191
192         if (c->win == NULL)
193         {
194                 c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
195
196                 capitalized_name = g_strdup(__NAME__);
197                 capitalized_name[0] = g_ascii_toupper(capitalized_name[0]);
198                 gtk_window_set_wmclass(GTK_WINDOW(c->win), __NAME__, capitalized_name);
199                 g_free(capitalized_name);
200         }
201
202         /* When using Gtk2, it only shows a white area when run in suckless'
203          * tabbed. It appears we need to set a default window size for this
204          * to work. This is not needed when using Gtk3. */
205         gtk_window_set_default_size(GTK_WINDOW(c->win), 1024, 768);
206
207         g_signal_connect(G_OBJECT(c->win), "destroy",
208                          G_CALLBACK(client_destroy), c);
209         gtk_window_set_title(GTK_WINDOW(c->win), __NAME__);
210
211         c->web_view = webkit_web_view_new();
212         webkit_web_view_set_full_content_zoom(WEBKIT_WEB_VIEW(c->web_view), TRUE);
213         webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), global_zoom);
214         g_signal_connect(G_OBJECT(c->web_view), "notify::title",
215                          G_CALLBACK(changed_title), c);
216         g_signal_connect(G_OBJECT(c->web_view), "notify::uri",
217                          G_CALLBACK(changed_uri), c);
218         g_signal_connect(G_OBJECT(c->web_view), "notify::load-status",
219                          G_CALLBACK(changed_load_status), c);
220         g_signal_connect(G_OBJECT(c->web_view),
221                          "new-window-policy-decision-requested",
222                          G_CALLBACK(client_new_request), NULL);
223         g_signal_connect(G_OBJECT(c->web_view),
224                          "mime-type-policy-decision-requested",
225                          G_CALLBACK(download_request), NULL);
226         g_signal_connect(G_OBJECT(c->web_view), "download-requested",
227                          G_CALLBACK(download_wget), NULL);
228         g_signal_connect(G_OBJECT(c->web_view), "key-press-event",
229                          G_CALLBACK(key_web_view), c);
230         g_signal_connect(G_OBJECT(c->web_view), "button-release-event",
231                          G_CALLBACK(key_web_view), c);
232         g_signal_connect(G_OBJECT(c->web_view), "hovering-over-link",
233                          G_CALLBACK(hover_web_view), c);
234         g_signal_connect(G_OBJECT(c->web_view), "resource-request-starting",
235                          G_CALLBACK(adblock), NULL);
236
237         if (!language_is_set)
238         {
239                 g_object_set(webkit_get_default_session(), "accept-language",
240                              accepted_language, NULL);
241                 language_is_set = TRUE;
242         }
243
244         c->scroll = gtk_scrolled_window_new(NULL, NULL);
245
246         gtk_container_add(GTK_CONTAINER(c->scroll), c->web_view);
247
248         c->location = gtk_entry_new();
249         g_signal_connect(G_OBJECT(c->location), "key-press-event",
250                          G_CALLBACK(key_location), c);
251
252         c->status = gtk_statusbar_new();
253         gtk_statusbar_set_has_resize_grip(GTK_STATUSBAR(c->status), FALSE);
254
255         c->vbox = gtk_vbox_new(FALSE, 2);
256         gtk_box_pack_start(GTK_BOX(c->vbox), c->location, FALSE, FALSE, 0);
257         gtk_container_add(GTK_CONTAINER(c->vbox), c->scroll);
258         gtk_box_pack_end(GTK_BOX(c->vbox), c->status, FALSE, FALSE, 0);
259
260         gtk_container_add(GTK_CONTAINER(c->win), c->vbox);
261
262         gtk_widget_grab_focus(c->web_view);
263         gtk_widget_show_all(c->win);
264
265         f = ensure_url_scheme(uri);
266         webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
267         g_free(f);
268
269         clients++;
270 }
271
272 gboolean
273 client_new_request(WebKitWebView *web_view, WebKitWebFrame *frame,
274                    WebKitNetworkRequest *request,
275                    WebKitWebNavigationAction *navigation_action,
276                    WebKitWebPolicyDecision *policy_decision, gpointer user_data)
277 {
278         (void)web_view;
279         (void)frame;
280         (void)navigation_action;
281         (void)user_data;
282
283         webkit_web_policy_decision_ignore(policy_decision);
284         client_new(webkit_network_request_get_uri(request));
285
286         return TRUE;
287 }
288
289 void
290 cooperation_setup(void)
291 {
292         GIOChannel *towatch;
293         gchar *fifopath;
294
295         fifopath = g_build_filename(g_get_user_runtime_dir(), __NAME__".fifo", NULL);
296
297         if (!g_file_test(fifopath, G_FILE_TEST_EXISTS))
298                 mkfifo(fifopath, 0600);
299
300         cooperative_pipe_fp = open(fifopath, O_WRONLY | O_NONBLOCK);
301         if (!cooperative_pipe_fp)
302         {
303                 fprintf(stderr, __NAME__": Can't open FIFO at all.\n");
304         }
305         else
306         {
307                 if (write(cooperative_pipe_fp, "", 0) == -1)
308                 {
309                         /* Could not do an empty write to the FIFO which means there's
310                          * no one listening. */
311                         close(cooperative_pipe_fp);
312                         towatch = g_io_channel_new_file(fifopath, "r+", NULL);
313                         g_io_add_watch(towatch, G_IO_IN, (GIOFunc)remote_msg, NULL);
314                 }
315                 else
316                         cooperative_alone = FALSE;
317         }
318
319         g_free(fifopath);
320 }
321
322 void
323 changed_load_status(GObject *obj, GParamSpec *pspec, gpointer data)
324 {
325         struct Client *c = (struct Client *)data;
326
327         (void)obj;
328         (void)pspec;
329
330         if (webkit_web_view_get_load_status(WEBKIT_WEB_VIEW(c->web_view))
331             == WEBKIT_LOAD_FINISHED)
332         {
333                 gtk_statusbar_pop(GTK_STATUSBAR(c->status), 1);
334                 gtk_statusbar_push(GTK_STATUSBAR(c->status), 1, "Finished.");
335         }
336         else
337         {
338                 gtk_statusbar_pop(GTK_STATUSBAR(c->status), 1);
339                 gtk_statusbar_push(GTK_STATUSBAR(c->status), 1, "Loading...");
340         }
341 }
342
343 void
344 changed_title(GObject *obj, GParamSpec *pspec, gpointer data)
345 {
346         const gchar *t;
347         struct Client *c = (struct Client *)data;
348
349         (void)obj;
350         (void)pspec;
351
352         t = webkit_web_view_get_title(WEBKIT_WEB_VIEW(c->web_view));
353         gtk_window_set_title(GTK_WINDOW(c->win), (t == NULL ? __NAME__ : t));
354 }
355
356 void
357 changed_uri(GObject *obj, GParamSpec *pspec, gpointer data)
358 {
359         const gchar *t;
360         struct Client *c = (struct Client *)data;
361
362         (void)obj;
363         (void)pspec;
364
365         t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
366         gtk_entry_set_text(GTK_ENTRY(c->location), (t == NULL ? __NAME__ : t));
367 }
368
369 gboolean
370 download_request(WebKitWebView *web_view, WebKitWebFrame *frame,
371                  WebKitNetworkRequest *request, gchar *mime_type,
372                  WebKitWebPolicyDecision *policy_decision, gpointer data)
373 {
374         (void)frame;
375         (void)request;
376         (void)data;
377
378         if (!webkit_web_view_can_show_mime_type(web_view, mime_type))
379         {
380                 webkit_web_policy_decision_download(policy_decision);
381                 return TRUE;
382         }
383         return FALSE;
384 }
385
386 gboolean
387 download_wget(WebKitWebView *web_view, WebKitDownload *download, gpointer data)
388 {
389         const gchar *uri;
390         char id[16] = "";
391         gint ret;
392
393         (void)web_view;
394         (void)data;
395
396         uri = webkit_download_get_uri(download);
397         if (fork() == 0)
398         {
399                 chdir(download_dir);
400                 if (embed == 0)
401                         ret = execlp("xterm", "xterm", "-hold", "-e", "wget", uri, NULL);
402                 else
403                 {
404                         if (snprintf(id, 16, "%ld", embed) >= 16)
405                         {
406                                 fprintf(stderr, __NAME__": id for xterm embed truncated!\n");
407                                 exit(EXIT_FAILURE);
408                         }
409                         ret = execlp("xterm", "xterm", "-hold", "-into", id, "-e", "wget",
410                                      uri, NULL);
411                 }
412
413                 if (ret == -1)
414                 {
415                         fprintf(stderr, __NAME__": exec'ing xterm for download");
416                         perror(" failed");
417                         exit(EXIT_FAILURE);
418                 }
419         }
420
421         return FALSE;
422 }
423
424 gchar *
425 ensure_url_scheme(const gchar *t)
426 {
427         gchar *f;
428
429         f = g_ascii_strdown(t, -1);
430         if (!g_str_has_prefix(f, "http:") &&
431                 !g_str_has_prefix(f, "https:") &&
432                 !g_str_has_prefix(f, "file:") &&
433                 !g_str_has_prefix(f, "about:"))
434         {
435                 g_free(f);
436                 f = g_strdup_printf("http://%s", t);
437                 return f;
438         }
439         else
440                 return g_strdup(t);
441 }
442
443 void
444 grab_environment_configuration(void)
445 {
446         gchar *u, *v;
447         const gchar *e;
448
449         u = g_ascii_strup(__NAME__, -1);
450
451         v = g_strdup_printf("%s_ACCEPTED_LANGUAGE", u);
452         e = g_getenv(v);
453         if (e != NULL)
454         {
455                 accepted_language = g_strdup(e);
456         }
457         g_free(v);
458
459         v = g_strdup_printf("%s_DOWNLOAD_DIR", u);
460         e = g_getenv(v);
461         if (e != NULL)
462         {
463                 download_dir = g_strdup(e);
464         }
465         g_free(v);
466
467         v = g_strdup_printf("%s_ZOOM", u);
468         e = g_getenv(v);
469         if (e != NULL)
470         {
471                 global_zoom = atof(e);
472         }
473         g_free(v);
474
475         g_free(u);
476 }
477
478 void
479 hover_web_view(WebKitWebView *web_view, gchar *title, gchar *uri,
480                    gpointer data)
481 {
482         struct Client *c = (struct Client *)data;
483
484         (void)web_view;
485         (void)title;
486
487         gtk_statusbar_pop(GTK_STATUSBAR(c->status), 0);
488         if (uri != NULL)
489                 gtk_statusbar_push(GTK_STATUSBAR(c->status), 0, uri);
490 }
491
492 gboolean
493 key_location(GtkWidget *widget, GdkEvent *event, gpointer data)
494 {
495         struct Client *c = (struct Client *)data;
496         const gchar *t;
497         gchar *f;
498
499         (void)widget;
500
501         if (event->type == GDK_KEY_PRESS)
502         {
503                 switch (((GdkEventKey *)event)->keyval)
504                 {
505                         case GDK_KEY_Return:
506                                 gtk_widget_grab_focus(c->web_view);
507                                 t = gtk_entry_get_text(GTK_ENTRY(c->location));
508                                 if (t != NULL && t[0] == '/')
509                                 {
510                                         if (search_text != NULL)
511                                                 g_free(search_text);
512                                         search_text = g_strdup(t + 1);  /* XXX whacky */
513                                         search(c, 1);
514                                 }
515                                 else
516                                 {
517                                         f = ensure_url_scheme(t);
518                                         webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
519                                         g_free(f);
520                                 }
521                                 return TRUE;
522                         case GDK_KEY_Escape:
523                                 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
524                                 gtk_entry_set_text(GTK_ENTRY(c->location),
525                                                    (t == NULL ? __NAME__ : t));
526                                 return TRUE;
527                 }
528         }
529
530         return FALSE;
531 }
532
533 gboolean
534 key_web_view(GtkWidget *widget, GdkEvent *event, gpointer data)
535 {
536         struct Client *c = (struct Client *)data;
537         WebKitHitTestResultContext ht_context;
538         WebKitHitTestResult *ht_result = NULL;
539         char *ht_uri =  NULL;
540
541         (void)widget;
542
543         if (event->type == GDK_KEY_PRESS)
544         {
545                 if (((GdkEventKey *)event)->state & GDK_CONTROL_MASK)
546                 {
547                         switch (((GdkEventKey *)event)->keyval)
548                         {
549                                 case GDK_KEY_o:
550                                         gtk_widget_grab_focus(c->location);
551                                         return TRUE;
552                                 case GDK_KEY_h:
553                                         scroll(gtk_scrolled_window_get_hadjustment(
554                                                GTK_SCROLLED_WINDOW(c->scroll)), 0, -1);
555                                         return TRUE;
556                                 case GDK_KEY_j:
557                                         scroll(gtk_scrolled_window_get_vadjustment(
558                                                GTK_SCROLLED_WINDOW(c->scroll)), 0, 1);
559                                         return TRUE;
560                                 case GDK_KEY_k:
561                                         scroll(gtk_scrolled_window_get_vadjustment(
562                                                GTK_SCROLLED_WINDOW(c->scroll)), 0, -1);
563                                         return TRUE;
564                                 case GDK_KEY_l:
565                                         scroll(gtk_scrolled_window_get_hadjustment(
566                                                GTK_SCROLLED_WINDOW(c->scroll)), 0, 1);
567                                         return TRUE;
568                                 case GDK_KEY_f:
569                                         scroll(gtk_scrolled_window_get_vadjustment(
570                                                GTK_SCROLLED_WINDOW(c->scroll)), 1, 0.5);
571                                         return TRUE;
572                                 case GDK_KEY_b:
573                                         scroll(gtk_scrolled_window_get_vadjustment(
574                                                GTK_SCROLLED_WINDOW(c->scroll)), 1, -0.5);
575                                         return TRUE;
576                                 case GDK_KEY_n:
577                                         search(c, 1);
578                                         return TRUE;
579                                 case GDK_KEY_p:
580                                         search(c, -1);
581                                         return TRUE;
582                                 case GDK_KEY_g:
583                                         webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view),
584                                                                  first_uri);
585                                         return TRUE;
586                         }
587                 }
588                 else if (((GdkEventKey *)event)->keyval == GDK_KEY_Escape)
589                 {
590                         webkit_web_view_stop_loading(WEBKIT_WEB_VIEW(c->web_view));
591                         gtk_statusbar_pop(GTK_STATUSBAR(c->status), 1);
592                         gtk_statusbar_push(GTK_STATUSBAR(c->status), 1, "Aborted.");
593                 }
594         }
595         else if (event->type == GDK_BUTTON_RELEASE)
596         {
597                 switch (((GdkEventButton *)event)->button)
598                 {
599                         case 2:
600                                 ht_result = webkit_web_view_get_hit_test_result(
601                                                                    WEBKIT_WEB_VIEW(c->web_view),
602                                                                        (GdkEventButton *)event);
603                                 g_object_get(ht_result, "context", &ht_context, NULL);
604                                 if (ht_context & WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK)
605                                 {
606                                         g_object_get(ht_result, "link-uri", &ht_uri, NULL);
607                                         client_new(ht_uri);
608                                         g_object_unref(ht_result);
609                                         return TRUE;
610                                 }
611                                 g_object_unref(ht_result);
612                                 return FALSE;
613                         case 8:
614                                 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
615                                 return TRUE;
616                         case 9:
617                                 webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
618                                 return TRUE;
619                 }
620         }
621
622         return FALSE;
623 }
624
625 gboolean
626 remote_msg(GIOChannel *channel, GIOCondition condition, gpointer data)
627 {
628         gchar *uri = NULL;
629
630         (void)condition;
631         (void)data;
632
633         g_io_channel_read_line(channel, &uri, NULL, NULL, NULL);
634         if (uri)
635         {
636                 g_strstrip(uri);
637                 client_new(uri);
638                 g_free(uri);
639         }
640         return TRUE;
641 }
642
643 void
644 search(gpointer data, gint direction)
645 {
646         struct Client *c = (struct Client *)data;
647
648         if (search_text == NULL)
649                 return;
650
651         webkit_web_view_search_text(WEBKIT_WEB_VIEW(c->web_view), search_text,
652                                     FALSE, direction == 1, TRUE);
653 }
654
655 void
656 scroll(GtkAdjustment *a, gint step_type, gdouble factor)
657 {
658         gdouble new, lower, upper, step;
659         lower = gtk_adjustment_get_lower(a);
660         upper = gtk_adjustment_get_upper(a) - gtk_adjustment_get_page_size(a) + lower;
661         if (step_type == 0)
662                 step = gtk_adjustment_get_step_increment(a);
663         else
664                 step = gtk_adjustment_get_page_increment(a);
665         new = gtk_adjustment_get_value(a) + factor * step;
666         new = new < lower ? lower : new;
667         new = new > upper ? upper : new;
668         gtk_adjustment_set_value(a, new);
669 }
670
671 Window
672 tabbed_launch(void)
673 {
674         gint tabbed_stdout;
675         GIOChannel *tabbed_stdout_channel;
676         GError *err = NULL;
677         gchar *output = NULL;
678         char *argv[] = { "tabbed", "-c", "-d", "-p", "s1", "-n", __NAME__, NULL };
679         Window plug_into;
680
681         if (!g_spawn_async_with_pipes(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL,
682                                       NULL, NULL, NULL, &tabbed_stdout, NULL,
683                                       &err))
684         {
685                 fprintf(stderr, __NAME__": Could not launch tabbed: %s\n", err->message);
686                 g_error_free(err);
687                 return 0;
688         }
689
690         tabbed_stdout_channel = g_io_channel_unix_new(tabbed_stdout);
691         g_io_channel_read_line(tabbed_stdout_channel, &output, NULL, NULL, NULL);
692         if (output == NULL)
693         {
694                 fprintf(stderr, __NAME__": Could not read XID from tabbed\n");
695                 return 0;
696         }
697
698         g_io_channel_shutdown(tabbed_stdout_channel, FALSE, NULL);
699
700         g_strstrip(output);
701         plug_into = strtol(output, NULL, 16);
702         g_free(output);
703         return plug_into;
704 }
705
706 void
707 usage(void)
708 {
709         fprintf(stderr, "Usage: "__NAME__" [OPTION]... <URI>...\n");
710         exit(EXIT_FAILURE);
711 }
712
713
714 int
715 main(int argc, char **argv)
716 {
717         int opt, i;
718
719         gtk_init(&argc, &argv);
720
721         grab_environment_configuration();
722
723         while ((opt = getopt(argc, argv, "e:rCT")) != -1)
724         {
725                 switch (opt)
726                 {
727                         case 'e':
728                                 embed = atol(optarg);
729                                 tabbed_automagic = FALSE;
730                                 break;
731                         case 'r':
732                                 show_all_requests = TRUE;
733                                 break;
734                         case 'C':
735                                 cooperative_instances = FALSE;
736                                 break;
737                         case 'T':
738                                 tabbed_automagic = FALSE;
739                                 break;
740                         default:
741                                 usage();
742                 }
743         }
744
745         if (optind >= argc)
746                 usage();
747
748         adblock_load();
749         cooperation_setup();
750
751         if (tabbed_automagic && !(cooperative_instances && !cooperative_alone))
752                 embed = tabbed_launch();
753
754         first_uri = g_strdup(argv[optind]);
755         for (i = optind; i < argc; i++)
756                 client_new(argv[i]);
757         if (!cooperative_instances || cooperative_alone)
758                 gtk_main();
759         exit(EXIT_SUCCESS);
760 }