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