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