]> git.armaanb.net Git - chorizo.git/blob - browser.c
Shorten grab_environment_configuration()
[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 *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                 gtk_window_set_wmclass(GTK_WINDOW(c->win), __NAME__, __NAME_CAPITALIZED__);
196         }
197
198         /* When using Gtk2, it only shows a white area when run in suckless'
199          * tabbed. It appears we need to set a default window size for this
200          * to work. This is not needed when using Gtk3. */
201         gtk_window_set_default_size(GTK_WINDOW(c->win), 1024, 768);
202
203         g_signal_connect(G_OBJECT(c->win), "destroy",
204                          G_CALLBACK(client_destroy), c);
205         gtk_window_set_title(GTK_WINDOW(c->win), __NAME__);
206
207         c->web_view = webkit_web_view_new();
208
209         /* XXX I really do want to enable this option. However, I get
210          * reproducable crashes with it enabled. I've seen bug reports from
211          * 2010 about this... WebKit crashes in libpixman, so maybe it's not
212          * a WebKit issue.
213          * Yeah, well. I'll turn it off for now. */
214         /*webkit_web_view_set_full_content_zoom(WEBKIT_WEB_VIEW(c->web_view), TRUE);*/
215
216         webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), global_zoom);
217         g_signal_connect(G_OBJECT(c->web_view), "notify::title",
218                          G_CALLBACK(changed_title), c);
219         g_signal_connect(G_OBJECT(c->web_view), "notify::uri",
220                          G_CALLBACK(changed_uri), c);
221         g_signal_connect(G_OBJECT(c->web_view), "notify::load-status",
222                          G_CALLBACK(changed_load_status), c);
223         g_signal_connect(G_OBJECT(c->web_view),
224                          "new-window-policy-decision-requested",
225                          G_CALLBACK(client_new_request), NULL);
226         g_signal_connect(G_OBJECT(c->web_view),
227                          "mime-type-policy-decision-requested",
228                          G_CALLBACK(download_request), NULL);
229         g_signal_connect(G_OBJECT(c->web_view), "download-requested",
230                          G_CALLBACK(download_wget), NULL);
231         g_signal_connect(G_OBJECT(c->web_view), "key-press-event",
232                          G_CALLBACK(key_web_view), c);
233         g_signal_connect(G_OBJECT(c->web_view), "button-press-event",
234                          G_CALLBACK(key_web_view), c);
235         g_signal_connect(G_OBJECT(c->web_view), "hovering-over-link",
236                          G_CALLBACK(hover_web_view), c);
237         g_signal_connect(G_OBJECT(c->web_view), "resource-request-starting",
238                          G_CALLBACK(adblock), NULL);
239
240         if (!language_is_set)
241         {
242                 g_object_set(webkit_get_default_session(), "accept-language",
243                              accepted_language, NULL);
244                 language_is_set = TRUE;
245         }
246
247         c->scroll = gtk_scrolled_window_new(NULL, NULL);
248
249         gtk_container_add(GTK_CONTAINER(c->scroll), c->web_view);
250
251         c->location = gtk_entry_new();
252         g_signal_connect(G_OBJECT(c->location), "key-press-event",
253                          G_CALLBACK(key_location), c);
254
255         c->status = gtk_statusbar_new();
256         gtk_statusbar_set_has_resize_grip(GTK_STATUSBAR(c->status), FALSE);
257
258         c->vbox = gtk_vbox_new(FALSE, 2);
259         gtk_box_pack_start(GTK_BOX(c->vbox), c->location, FALSE, FALSE, 0);
260         gtk_container_add(GTK_CONTAINER(c->vbox), c->scroll);
261         gtk_box_pack_end(GTK_BOX(c->vbox), c->status, FALSE, FALSE, 0);
262
263         gtk_container_add(GTK_CONTAINER(c->win), c->vbox);
264
265         gtk_widget_grab_focus(c->web_view);
266         gtk_widget_show_all(c->win);
267
268         f = ensure_url_scheme(uri);
269         webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
270         g_free(f);
271
272         clients++;
273 }
274
275 gboolean
276 client_new_request(WebKitWebView *web_view, WebKitWebFrame *frame,
277                    WebKitNetworkRequest *request,
278                    WebKitWebNavigationAction *navigation_action,
279                    WebKitWebPolicyDecision *policy_decision, gpointer user_data)
280 {
281         (void)web_view;
282         (void)frame;
283         (void)navigation_action;
284         (void)user_data;
285
286         webkit_web_policy_decision_ignore(policy_decision);
287         client_new(webkit_network_request_get_uri(request));
288
289         return TRUE;
290 }
291
292 void
293 cooperation_setup(void)
294 {
295         GIOChannel *towatch;
296         gchar *fifopath;
297
298         fifopath = g_build_filename(g_get_user_runtime_dir(), __NAME__".fifo", NULL);
299
300         if (!g_file_test(fifopath, G_FILE_TEST_EXISTS))
301                 mkfifo(fifopath, 0600);
302
303         cooperative_pipe_fp = open(fifopath, O_WRONLY | O_NONBLOCK);
304         if (!cooperative_pipe_fp)
305         {
306                 fprintf(stderr, __NAME__": Can't open FIFO at all.\n");
307         }
308         else
309         {
310                 if (write(cooperative_pipe_fp, "", 0) == -1)
311                 {
312                         /* Could not do an empty write to the FIFO which means there's
313                          * no one listening. */
314                         close(cooperative_pipe_fp);
315                         towatch = g_io_channel_new_file(fifopath, "r+", NULL);
316                         g_io_add_watch(towatch, G_IO_IN, (GIOFunc)remote_msg, NULL);
317                 }
318                 else
319                         cooperative_alone = FALSE;
320         }
321
322         g_free(fifopath);
323 }
324
325 void
326 changed_load_status(GObject *obj, GParamSpec *pspec, gpointer data)
327 {
328         struct Client *c = (struct Client *)data;
329
330         (void)obj;
331         (void)pspec;
332
333         if (webkit_web_view_get_load_status(WEBKIT_WEB_VIEW(c->web_view))
334             == WEBKIT_LOAD_FINISHED)
335         {
336                 gtk_statusbar_pop(GTK_STATUSBAR(c->status), 1);
337                 gtk_statusbar_push(GTK_STATUSBAR(c->status), 1, "Finished.");
338         }
339         else
340         {
341                 gtk_statusbar_pop(GTK_STATUSBAR(c->status), 1);
342                 gtk_statusbar_push(GTK_STATUSBAR(c->status), 1, "Loading...");
343         }
344 }
345
346 void
347 changed_title(GObject *obj, GParamSpec *pspec, gpointer data)
348 {
349         const gchar *t;
350         struct Client *c = (struct Client *)data;
351
352         (void)obj;
353         (void)pspec;
354
355         t = webkit_web_view_get_title(WEBKIT_WEB_VIEW(c->web_view));
356         gtk_window_set_title(GTK_WINDOW(c->win), (t == NULL ? __NAME__ : t));
357 }
358
359 void
360 changed_uri(GObject *obj, GParamSpec *pspec, gpointer data)
361 {
362         const gchar *t;
363         struct Client *c = (struct Client *)data;
364
365         (void)obj;
366         (void)pspec;
367
368         t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
369         gtk_entry_set_text(GTK_ENTRY(c->location), (t == NULL ? __NAME__ : t));
370 }
371
372 gboolean
373 download_request(WebKitWebView *web_view, WebKitWebFrame *frame,
374                  WebKitNetworkRequest *request, gchar *mime_type,
375                  WebKitWebPolicyDecision *policy_decision, gpointer data)
376 {
377         (void)frame;
378         (void)request;
379         (void)data;
380
381         if (!webkit_web_view_can_show_mime_type(web_view, mime_type))
382         {
383                 webkit_web_policy_decision_download(policy_decision);
384                 return TRUE;
385         }
386         return FALSE;
387 }
388
389 gboolean
390 download_wget(WebKitWebView *web_view, WebKitDownload *download, gpointer data)
391 {
392         const gchar *uri;
393         char id[16] = "";
394         gint ret;
395
396         (void)web_view;
397         (void)data;
398
399         uri = webkit_download_get_uri(download);
400         if (fork() == 0)
401         {
402                 chdir(download_dir);
403                 if (embed == 0)
404                         ret = execlp("xterm", "xterm", "-hold", "-e", "wget", uri, NULL);
405                 else
406                 {
407                         if (snprintf(id, 16, "%ld", embed) >= 16)
408                         {
409                                 fprintf(stderr, __NAME__": id for xterm embed truncated!\n");
410                                 exit(EXIT_FAILURE);
411                         }
412                         ret = execlp("xterm", "xterm", "-hold", "-into", id, "-e", "wget",
413                                      uri, NULL);
414                 }
415
416                 if (ret == -1)
417                 {
418                         fprintf(stderr, __NAME__": exec'ing xterm for download");
419                         perror(" failed");
420                         exit(EXIT_FAILURE);
421                 }
422         }
423
424         return FALSE;
425 }
426
427 gchar *
428 ensure_url_scheme(const gchar *t)
429 {
430         gchar *f;
431
432         f = g_ascii_strdown(t, -1);
433         if (!g_str_has_prefix(f, "http:") &&
434                 !g_str_has_prefix(f, "https:") &&
435                 !g_str_has_prefix(f, "file:") &&
436                 !g_str_has_prefix(f, "about:"))
437         {
438                 g_free(f);
439                 f = g_strdup_printf("http://%s", t);
440                 return f;
441         }
442         else
443                 return g_strdup(t);
444 }
445
446 void
447 grab_environment_configuration(void)
448 {
449         const gchar *e;
450
451         e = g_getenv(__NAME_UPPERCASE__"_ACCEPTED_LANGUAGE");
452         if (e != NULL)
453                 accepted_language = g_strdup(e);
454
455         e = g_getenv(__NAME_UPPERCASE__"_DOWNLOAD_DIR");
456         if (e != NULL)
457                 download_dir = g_strdup(e);
458
459         e = g_getenv(__NAME_UPPERCASE__"_ZOOM");
460         if (e != NULL)
461                 global_zoom = atof(e);
462 }
463
464 void
465 hover_web_view(WebKitWebView *web_view, gchar *title, gchar *uri,
466                    gpointer data)
467 {
468         struct Client *c = (struct Client *)data;
469
470         (void)web_view;
471         (void)title;
472
473         gtk_statusbar_pop(GTK_STATUSBAR(c->status), 0);
474         if (uri != NULL)
475                 gtk_statusbar_push(GTK_STATUSBAR(c->status), 0, uri);
476 }
477
478 gboolean
479 key_location(GtkWidget *widget, GdkEvent *event, gpointer data)
480 {
481         struct Client *c = (struct Client *)data;
482         const gchar *t;
483         gchar *f;
484
485         (void)widget;
486
487         if (event->type == GDK_KEY_PRESS)
488         {
489                 switch (((GdkEventKey *)event)->keyval)
490                 {
491                         case GDK_KEY_Return:
492                                 gtk_widget_grab_focus(c->web_view);
493                                 t = gtk_entry_get_text(GTK_ENTRY(c->location));
494                                 if (t != NULL && t[0] == '/')
495                                 {
496                                         if (search_text != NULL)
497                                                 g_free(search_text);
498                                         search_text = g_strdup(t + 1);  /* XXX whacky */
499                                         search(c, 1);
500                                 }
501                                 else
502                                 {
503                                         f = ensure_url_scheme(t);
504                                         webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
505                                         g_free(f);
506                                 }
507                                 return TRUE;
508                         case GDK_KEY_Escape:
509                                 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
510                                 gtk_entry_set_text(GTK_ENTRY(c->location),
511                                                    (t == NULL ? __NAME__ : t));
512                                 return TRUE;
513                 }
514         }
515
516         return FALSE;
517 }
518
519 gboolean
520 key_web_view(GtkWidget *widget, GdkEvent *event, gpointer data)
521 {
522         struct Client *c = (struct Client *)data;
523         WebKitHitTestResultContext ht_context;
524         WebKitHitTestResult *ht_result = NULL;
525         char *ht_uri =  NULL;
526
527         (void)widget;
528
529         if (event->type == GDK_KEY_PRESS)
530         {
531                 if (((GdkEventKey *)event)->state & GDK_CONTROL_MASK)
532                 {
533                         switch (((GdkEventKey *)event)->keyval)
534                         {
535                                 case GDK_KEY_o:
536                                         gtk_widget_grab_focus(c->location);
537                                         return TRUE;
538                                 case GDK_KEY_h:
539                                         scroll(gtk_scrolled_window_get_hadjustment(
540                                                GTK_SCROLLED_WINDOW(c->scroll)), 0, -1);
541                                         return TRUE;
542                                 case GDK_KEY_j:
543                                         scroll(gtk_scrolled_window_get_vadjustment(
544                                                GTK_SCROLLED_WINDOW(c->scroll)), 0, 1);
545                                         return TRUE;
546                                 case GDK_KEY_k:
547                                         scroll(gtk_scrolled_window_get_vadjustment(
548                                                GTK_SCROLLED_WINDOW(c->scroll)), 0, -1);
549                                         return TRUE;
550                                 case GDK_KEY_l:
551                                         scroll(gtk_scrolled_window_get_hadjustment(
552                                                GTK_SCROLLED_WINDOW(c->scroll)), 0, 1);
553                                         return TRUE;
554                                 case GDK_KEY_f:
555                                         scroll(gtk_scrolled_window_get_vadjustment(
556                                                GTK_SCROLLED_WINDOW(c->scroll)), 1, 0.5);
557                                         return TRUE;
558                                 case GDK_KEY_b:
559                                         scroll(gtk_scrolled_window_get_vadjustment(
560                                                GTK_SCROLLED_WINDOW(c->scroll)), 1, -0.5);
561                                         return TRUE;
562                                 case GDK_KEY_n:
563                                         search(c, 1);
564                                         return TRUE;
565                                 case GDK_KEY_p:
566                                         search(c, -1);
567                                         return TRUE;
568                                 case GDK_KEY_g:
569                                         webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view),
570                                                                  first_uri);
571                                         return TRUE;
572                         }
573                 }
574                 else if (((GdkEventKey *)event)->keyval == GDK_KEY_Escape)
575                 {
576                         webkit_web_view_stop_loading(WEBKIT_WEB_VIEW(c->web_view));
577                         gtk_statusbar_pop(GTK_STATUSBAR(c->status), 1);
578                         gtk_statusbar_push(GTK_STATUSBAR(c->status), 1, "Aborted.");
579                 }
580         }
581         else if (event->type == GDK_BUTTON_PRESS)
582         {
583                 switch (((GdkEventButton *)event)->button)
584                 {
585                         case 2:
586                                 ht_result = webkit_web_view_get_hit_test_result(
587                                                                    WEBKIT_WEB_VIEW(c->web_view),
588                                                                        (GdkEventButton *)event);
589                                 g_object_get(ht_result, "context", &ht_context, NULL);
590                                 if (ht_context & WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK)
591                                 {
592                                         g_object_get(ht_result, "link-uri", &ht_uri, NULL);
593                                         client_new(ht_uri);
594                                         g_object_unref(ht_result);
595                                         return TRUE;
596                                 }
597                                 g_object_unref(ht_result);
598                                 return FALSE;
599                         case 8:
600                                 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
601                                 return TRUE;
602                         case 9:
603                                 webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
604                                 return TRUE;
605                 }
606         }
607
608         return FALSE;
609 }
610
611 gboolean
612 remote_msg(GIOChannel *channel, GIOCondition condition, gpointer data)
613 {
614         gchar *uri = NULL;
615
616         (void)condition;
617         (void)data;
618
619         g_io_channel_read_line(channel, &uri, NULL, NULL, NULL);
620         if (uri)
621         {
622                 g_strstrip(uri);
623                 client_new(uri);
624                 g_free(uri);
625         }
626         return TRUE;
627 }
628
629 void
630 search(gpointer data, gint direction)
631 {
632         struct Client *c = (struct Client *)data;
633
634         if (search_text == NULL)
635                 return;
636
637         webkit_web_view_search_text(WEBKIT_WEB_VIEW(c->web_view), search_text,
638                                     FALSE, direction == 1, TRUE);
639 }
640
641 void
642 scroll(GtkAdjustment *a, gint step_type, gdouble factor)
643 {
644         gdouble new, lower, upper, step;
645         lower = gtk_adjustment_get_lower(a);
646         upper = gtk_adjustment_get_upper(a) - gtk_adjustment_get_page_size(a) + lower;
647         if (step_type == 0)
648                 step = gtk_adjustment_get_step_increment(a);
649         else
650                 step = gtk_adjustment_get_page_increment(a);
651         new = gtk_adjustment_get_value(a) + factor * step;
652         new = new < lower ? lower : new;
653         new = new > upper ? upper : new;
654         gtk_adjustment_set_value(a, new);
655 }
656
657 Window
658 tabbed_launch(void)
659 {
660         gint tabbed_stdout;
661         GIOChannel *tabbed_stdout_channel;
662         GError *err = NULL;
663         gchar *output = NULL;
664         char *argv[] = { "tabbed", "-c", "-d", "-p", "s1", "-n", __NAME__, NULL };
665         Window plug_into;
666
667         if (!g_spawn_async_with_pipes(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL,
668                                       NULL, NULL, NULL, &tabbed_stdout, NULL,
669                                       &err))
670         {
671                 fprintf(stderr, __NAME__": Could not launch tabbed: %s\n", err->message);
672                 g_error_free(err);
673                 return 0;
674         }
675
676         tabbed_stdout_channel = g_io_channel_unix_new(tabbed_stdout);
677         g_io_channel_read_line(tabbed_stdout_channel, &output, NULL, NULL, NULL);
678         if (output == NULL)
679         {
680                 fprintf(stderr, __NAME__": Could not read XID from tabbed\n");
681                 return 0;
682         }
683
684         g_io_channel_shutdown(tabbed_stdout_channel, FALSE, NULL);
685
686         g_strstrip(output);
687         plug_into = strtol(output, NULL, 16);
688         g_free(output);
689         return plug_into;
690 }
691
692 void
693 usage(void)
694 {
695         fprintf(stderr, "Usage: "__NAME__" [OPTION]... <URI>...\n");
696         exit(EXIT_FAILURE);
697 }
698
699
700 int
701 main(int argc, char **argv)
702 {
703         int opt, i;
704
705         gtk_init(&argc, &argv);
706
707         grab_environment_configuration();
708
709         while ((opt = getopt(argc, argv, "e:rCT")) != -1)
710         {
711                 switch (opt)
712                 {
713                         case 'e':
714                                 embed = atol(optarg);
715                                 tabbed_automagic = FALSE;
716                                 break;
717                         case 'r':
718                                 show_all_requests = TRUE;
719                                 break;
720                         case 'C':
721                                 cooperative_instances = FALSE;
722                                 break;
723                         case 'T':
724                                 tabbed_automagic = FALSE;
725                                 break;
726                         default:
727                                 usage();
728                 }
729         }
730
731         if (optind >= argc)
732                 usage();
733
734         adblock_load();
735         cooperation_setup();
736
737         if (tabbed_automagic && !(cooperative_instances && !cooperative_alone))
738                 embed = tabbed_launch();
739
740         first_uri = g_strdup(argv[optind]);
741         for (i = optind; i < argc; i++)
742                 client_new(argv[i]);
743         if (!cooperative_instances || cooperative_alone)
744                 gtk_main();
745         exit(EXIT_SUCCESS);
746 }