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