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