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