making config-files configurable in config.h
[surf.git] / surf.c
1 /* See LICENSE file for copyright and license details.
2  *
3  * To understand surf, start reading main().
4  */
5 #include <X11/X.h>
6 #include <X11/Xatom.h>
7 #include <gtk/gtk.h>
8 #include <gdk/gdkx.h>
9 #include <gdk/gdk.h>
10 #include <gdk/gdkkeysyms.h>
11 #include <string.h>
12 #include <unistd.h>
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <webkit/webkit.h>
16 #include <glib/gstdio.h>
17 #include <JavaScriptCore/JavaScript.h>
18
19 #define LENGTH(x)               (sizeof x / sizeof x[0])
20 #define CLEANMASK(mask)         (mask & ~(GDK_MOD2_MASK))
21
22 typedef union Arg Arg;
23 union Arg {
24         const gboolean b;
25         const gint i;
26         const void *v;
27 };
28
29 typedef struct Client {
30         GtkWidget *win, *scroll, *vbox, *urlbar, *searchbar, *indicator;
31         WebKitWebView *view;
32         WebKitDownload *download;
33         gchar *title;
34         gint progress;
35         struct Client *next;
36 } Client;
37
38 typedef struct Cookie {
39         char *name;
40         char *value;
41         char *domain;
42         char *path;
43         struct Cookie *next;
44 } Cookie;
45
46 typedef enum {
47         Browser = 0x0001,
48         SearchBar = 0x0010,
49         UrlBar = 0x0100,
50         Any = ~0,
51 } KeyFocus;
52
53 typedef struct {
54         guint mod;
55         guint keyval;
56         void (*func)(Client *c, const Arg *arg);
57         const Arg arg;
58         KeyFocus focus;
59 } Key;
60
61 static Display *dpy;
62 static Atom urlprop;
63 static SoupCookieJar *cookiejar;
64 static SoupSession *session;
65 static Client *clients = NULL;
66 /*static Cookie *cookies = NULL;*/
67 static GdkNativeWindow embed = 0;
68 static gboolean showxid = FALSE;
69 static gboolean ignore_once = FALSE;
70
71 static gchar *buildpath(const gchar *path);
72 static void cleanup(void);
73 static void clipboard(Client *c, const Arg *arg);
74 static gchar *copystr(gchar **str, const gchar *src);
75 static void destroyclient(Client *c);
76 static void destroywin(GtkWidget* w, Client *c);
77 static void die(char *str);
78 static void download(WebKitDownload *o, GParamSpec *pspec, Client *c);
79 static void drawindicator(Client *c);
80 static gboolean exposeindicator(GtkWidget *w, GdkEventExpose *e, Client *c);
81 static gboolean initdownload(WebKitWebView *view, WebKitDownload *o, Client *c);
82 static gchar *geturi(Client *c);
83 static void hidesearch(Client *c, const Arg *arg);
84 static void hideurl(Client *c, const Arg *arg);
85 static gboolean keypress(GtkWidget* w, GdkEventKey *ev, Client *c);
86 static void linkhover(WebKitWebView* page, const gchar* t, const gchar* l, Client *c);
87 static void loadcommit(WebKitWebView *view, WebKitWebFrame *f, Client *c);
88 static void loadstart(WebKitWebView *view, WebKitWebFrame *f, Client *c);
89 static void loadfile(Client *c, const gchar *f);
90 static void loaduri(Client *c, const Arg *arg);
91 static void navigate(Client *c, const Arg *arg);
92 static Client *newclient(void);
93 static WebKitWebView *newwindow(WebKitWebView  *v, WebKitWebFrame *f, Client *c);
94 static void pasteurl(GtkClipboard *clipboard, const gchar *text, gpointer d);
95 static GdkFilterReturn processx(GdkXEvent *xevent, GdkEvent *event, gpointer d);
96 static void print(Client *c, const Arg *arg);
97 static void proccookies(SoupMessage *m, Client *c);
98 static void progresschange(WebKitWebView *view, gint p, Client *c);
99 static void request(SoupSession *s, SoupMessage *m, Client *c);
100 static void reload(Client *c, const Arg *arg);
101 static void rereadcookies(void);
102 static void setcookie(char *name, char *val, char *dom, char *path, long exp);
103 static void setup(void);
104 static void titlechange(WebKitWebView* view, WebKitWebFrame* frame, const gchar* title, Client *c);
105 static void scroll(Client *c, const Arg *arg);
106 static void searchtext(Client *c, const Arg *arg);
107 static void source(Client *c, const Arg *arg);
108 static void showsearch(Client *c, const Arg *arg);
109 static void showurl(Client *c, const Arg *arg);
110 static void stop(Client *c, const Arg *arg);
111 static void titlechange(WebKitWebView* view, WebKitWebFrame* frame, const gchar* title, Client *c);
112 static gboolean unfocusbar(GtkWidget *w, GdkEventFocus *e, Client *c);
113 static void usage(void);
114 static void update(Client *c);
115 static void windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame, JSContextRef js, JSObjectRef win, Client *c);
116 static void zoom(Client *c, const Arg *arg);
117
118 /* configuration, allows nested code to access above variables */
119 #include "config.h"
120
121 gchar *
122 buildpath(const gchar *path) {
123         gchar *apath, *p;
124         FILE *f;
125
126         /* creating directory */
127         if(path[0] == '/')
128                 apath = g_strdup(path);
129         else
130                 apath = g_strconcat(g_get_home_dir(), "/", path, NULL);
131         if((p = strrchr(apath, '/'))) {
132                 *p = '\0';
133                 g_mkdir_with_parents(apath, 0755);
134                 *p = '/';
135         }
136         /* creating file (gives error when apath ends with "/") */
137         if((f = g_fopen(apath, "a")))
138                 fclose(f);
139         puts(apath);
140         return apath;
141 }
142
143 void
144 cleanup(void) {
145         while(clients)
146                 destroyclient(clients);
147         g_free(stylefile);
148         g_free(scriptfile);
149         g_free(dldir);
150 }
151
152 void
153 clipboard(Client *c, const Arg *arg) {
154         gboolean paste = *(gboolean *)arg;
155
156         if(paste)
157                 gtk_clipboard_request_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), pasteurl, c);
158         else
159                 gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), webkit_web_view_get_uri(c->view), -1);
160 }
161
162 gchar *
163 copystr(gchar **str, const gchar *src) {
164         gchar *tmp;
165         tmp = g_strdup(src);
166
167         if(str && *str) {
168                 g_free(*str);
169                 *str = tmp;
170         }
171         return tmp;
172 }
173
174 void
175 destroyclient(Client *c) {
176         Client *p;
177
178         gtk_widget_destroy(GTK_WIDGET(webkit_web_view_new()));
179         gtk_widget_destroy(c->scroll);
180         gtk_widget_destroy(c->urlbar);
181         gtk_widget_destroy(c->searchbar);
182         gtk_widget_destroy(c->vbox);
183         gtk_widget_destroy(c->win);
184         for(p = clients; p && p->next != c; p = p->next);
185         if(p)
186                 p->next = c->next;
187         else
188                 clients = c->next;
189         free(c);
190         if(clients == NULL)
191                 gtk_main_quit();
192 }
193
194 void
195 destroywin(GtkWidget* w, Client *c) {
196         destroyclient(c);
197 }
198
199 void
200 die(char *str) {
201         fputs(str, stderr);
202         exit(EXIT_FAILURE);
203 }
204
205 void
206 drawindicator(Client *c) {
207         gint width;
208         gchar *uri;
209         GtkWidget *w;
210         GdkGC *gc;
211         GdkColor fg;
212
213         uri = geturi(c);
214         w = c->indicator;
215         width = c->progress * w->allocation.width / 100;
216         gc = gdk_gc_new(w->window);
217         gdk_color_parse(strstr(uri, "https://") == uri ?
218                         progress_trust : progress, &fg);
219         gdk_gc_set_rgb_fg_color(gc, &fg);
220         gdk_draw_rectangle(w->window,
221                         w->style->bg_gc[GTK_WIDGET_STATE(w)],
222                         TRUE, 0, 0, w->allocation.width, w->allocation.height);
223         gdk_draw_rectangle(w->window, gc, TRUE, 0, 0, width,
224                         w->allocation.height);
225         g_object_unref(gc);
226 }
227
228 gboolean
229 exposeindicator(GtkWidget *w, GdkEventExpose *e, Client *c) {
230         drawindicator(c);
231         return TRUE;
232 }
233
234 void
235 download(WebKitDownload *o, GParamSpec *pspec, Client *c) {
236         WebKitDownloadStatus status;
237
238         status = webkit_download_get_status(c->download);
239         if(status == WEBKIT_DOWNLOAD_STATUS_STARTED || status == WEBKIT_DOWNLOAD_STATUS_CREATED) {
240                 c->progress = (gint)(webkit_download_get_progress(c->download)*100);
241         }
242         update(c);
243 }
244
245 gboolean
246 initdownload(WebKitWebView *view, WebKitDownload *o, Client *c) {
247         const gchar *filename;
248         gchar *uri, *html;
249
250         stop(c, NULL);
251         c->download = o;
252         filename = webkit_download_get_suggested_filename(o);
253         uri = g_strconcat("file://", dldir, "/", filename, NULL);
254         webkit_download_set_destination_uri(c->download, uri);
255         c->progress = 0;
256         g_free(uri);
257         html = g_strdup_printf("Download <b>%s</b>...", filename);
258         webkit_web_view_load_html_string(c->view, html,
259                         webkit_download_get_uri(c->download));
260         g_signal_connect(c->download, "notify::progress", G_CALLBACK(download), c);
261         g_signal_connect(c->download, "notify::status", G_CALLBACK(download), c);
262         webkit_download_start(c->download);
263         
264         c->title = copystr(&c->title, filename);
265         update(c);
266         g_free(html);
267         return TRUE;
268 }
269
270 gchar *
271 geturi(Client *c) {
272         gchar *uri;
273
274         if(!(uri = (gchar *)webkit_web_view_get_uri(c->view)))
275                 uri = copystr(NULL, "about:blank");
276         return uri;
277 }
278
279 void
280 hidesearch(Client *c, const Arg *arg) {
281         gtk_widget_hide(c->searchbar);
282         gtk_widget_grab_focus(GTK_WIDGET(c->view));
283 }
284
285 void
286 hideurl(Client *c, const Arg *arg) {
287         gtk_widget_hide(c->urlbar);
288         gtk_widget_grab_focus(GTK_WIDGET(c->view));
289 }
290
291 gboolean
292 keypress(GtkWidget* w, GdkEventKey *ev, Client *c) {
293         guint i, focus;
294         gboolean processed = FALSE;
295
296         if(ev->type != GDK_KEY_PRESS)
297                 return FALSE;
298         if(GTK_WIDGET_HAS_FOCUS(c->searchbar))
299                 focus = SearchBar;
300         else if(GTK_WIDGET_HAS_FOCUS(c->urlbar))
301                 focus = UrlBar;
302         else
303                 focus = Browser;
304         for(i = 0; i < LENGTH(keys); i++) {
305                 if(focus & keys[i].focus
306                                 && gdk_keyval_to_lower(ev->keyval) == keys[i].keyval
307                                 && CLEANMASK(ev->state) == keys[i].mod
308                                 && keys[i].func) {
309                         keys[i].func(c, &(keys[i].arg));
310                         processed = TRUE;
311                 }
312         }
313         return processed;
314 }
315
316 void
317 linkhover(WebKitWebView* page, const gchar* t, const gchar* l, Client *c) {
318         if(l)
319                 gtk_window_set_title(GTK_WINDOW(c->win), l);
320         else
321                 update(c);
322 }
323
324 void
325 loadcommit(WebKitWebView *view, WebKitWebFrame *f, Client *c) {
326         gchar *uri;
327
328         ignore_once = TRUE;
329         uri = geturi(c);
330         XChangeProperty(dpy, GDK_WINDOW_XID(GTK_WIDGET(c->win)->window), urlprop,
331                         XA_STRING, 8, PropModeReplace, (unsigned char *)uri,
332                         strlen(uri) + 1);
333 }
334
335 void
336 loadstart(WebKitWebView *view, WebKitWebFrame *f, Client *c) {
337         c->progress = 0;
338         update(c);
339 }
340
341 void
342 loadfile(Client *c, const gchar *f) {
343         GIOChannel *chan = NULL;
344         GError *e = NULL;
345         GString *code;
346         gchar *line, *uri;
347         Arg arg;
348
349         if(strcmp(f, "-") == 0) {
350                 chan = g_io_channel_unix_new(STDIN_FILENO);
351                 if (chan) {
352                         code = g_string_new("");
353                         while(g_io_channel_read_line(chan, &line, NULL, NULL,
354                                                 &e) == G_IO_STATUS_NORMAL) {
355                                 g_string_append(code, line);
356                                 g_free(line);
357                         }
358                         webkit_web_view_load_html_string(c->view, code->str,
359                                         "file://.");
360                         g_io_channel_shutdown(chan, FALSE, NULL);
361                         g_string_free(code, TRUE);
362                 }
363                 arg.v = uri = g_strdup("stdin");
364         }
365         else {
366                 arg.v = uri = g_strdup_printf("file://%s", f);
367                 loaduri(c, &arg);
368         }
369         c->title = copystr(&c->title, uri);
370         update(c);
371         g_free(uri);
372 }
373
374 void
375 loaduri(Client *c, const Arg *arg) {
376         gchar *u;
377         const gchar *uri = (gchar *)arg->v;
378         if(!uri)
379                 uri = gtk_entry_get_text(GTK_ENTRY(c->urlbar));
380         u = g_strrstr(uri, "://") ? g_strdup(uri)
381                 : g_strdup_printf("http://%s", uri);
382         webkit_web_view_load_uri(c->view, u);
383         c->progress = 0;
384         c->title = copystr(&c->title, u);
385         g_free(u);
386         update(c);
387 }
388
389 void
390 navigate(Client *c, const Arg *arg) {
391         gint steps = *(gint *)arg;
392         webkit_web_view_go_back_or_forward(c->view, steps);
393 }
394
395 Client *
396 newclient(void) {
397         Client *c;
398         WebKitWebSettings *settings;
399         gchar *uri;
400
401         if(!(c = calloc(1, sizeof(Client))))
402                 die("Cannot malloc!\n");
403         /* Window */
404         if(embed) {
405                 c->win = gtk_plug_new(embed);
406         }
407         else {
408                 c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
409                 gtk_window_set_wmclass(GTK_WINDOW(c->win), "surf", "surf");
410         }
411         gtk_window_set_default_size(GTK_WINDOW(c->win), 800, 600);
412         g_signal_connect(G_OBJECT(c->win), "destroy", G_CALLBACK(destroywin), c);
413         g_signal_connect(G_OBJECT(c->win), "key-press-event", G_CALLBACK(keypress), c);
414
415         /* VBox */
416         c->vbox = gtk_vbox_new(FALSE, 0);
417
418         /* scrolled window */
419         c->scroll = gtk_scrolled_window_new(NULL, NULL);
420         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
421                         GTK_POLICY_NEVER, GTK_POLICY_NEVER);
422
423         /* webview */
424         c->view = WEBKIT_WEB_VIEW(webkit_web_view_new());
425         g_signal_connect(G_OBJECT(c->view), "title-changed", G_CALLBACK(titlechange), c);
426         g_signal_connect(G_OBJECT(c->view), "load-progress-changed", G_CALLBACK(progresschange), c);
427         g_signal_connect(G_OBJECT(c->view), "load-committed", G_CALLBACK(loadcommit), c);
428         g_signal_connect(G_OBJECT(c->view), "load-started", G_CALLBACK(loadstart), c);
429         g_signal_connect(G_OBJECT(c->view), "hovering-over-link", G_CALLBACK(linkhover), c);
430         g_signal_connect(G_OBJECT(c->view), "create-web-view", G_CALLBACK(newwindow), c);
431         g_signal_connect(G_OBJECT(c->view), "download-requested", G_CALLBACK(initdownload), c);
432         g_signal_connect(G_OBJECT(c->view), "window-object-cleared", G_CALLBACK(windowobjectcleared), c);
433         g_signal_connect_after(session, "request-started", G_CALLBACK(request), c);
434
435         /* urlbar */
436         c->urlbar = gtk_entry_new();
437         gtk_entry_set_has_frame(GTK_ENTRY(c->urlbar), FALSE);
438         g_signal_connect(G_OBJECT(c->urlbar), "focus-out-event", G_CALLBACK(unfocusbar), c);
439
440         /* searchbar */
441         c->searchbar = gtk_entry_new();
442         gtk_entry_set_has_frame(GTK_ENTRY(c->searchbar), FALSE);
443         g_signal_connect(G_OBJECT(c->searchbar), "focus-out-event", G_CALLBACK(unfocusbar), c);
444
445         /* indicator */
446         c->indicator = gtk_drawing_area_new();
447         gtk_widget_set_size_request(c->indicator, 0, 2);
448         g_signal_connect (G_OBJECT (c->indicator), "expose_event",
449                         G_CALLBACK (exposeindicator), c);
450
451         /* Arranging */
452         gtk_container_add(GTK_CONTAINER(c->scroll), GTK_WIDGET(c->view));
453         gtk_container_add(GTK_CONTAINER(c->win), c->vbox);
454         gtk_container_add(GTK_CONTAINER(c->vbox), c->scroll);
455         gtk_container_add(GTK_CONTAINER(c->vbox), c->searchbar);
456         gtk_container_add(GTK_CONTAINER(c->vbox), c->urlbar);
457         gtk_container_add(GTK_CONTAINER(c->vbox), c->indicator);
458
459         /* Setup */
460         gtk_box_set_child_packing(GTK_BOX(c->vbox), c->urlbar, FALSE, FALSE, 0, GTK_PACK_START);
461         gtk_box_set_child_packing(GTK_BOX(c->vbox), c->searchbar, FALSE, FALSE, 0, GTK_PACK_START);
462         gtk_box_set_child_packing(GTK_BOX(c->vbox), c->indicator, FALSE, FALSE, 0, GTK_PACK_START);
463         gtk_box_set_child_packing(GTK_BOX(c->vbox), c->scroll, TRUE, TRUE, 0, GTK_PACK_START);
464         gtk_widget_grab_focus(GTK_WIDGET(c->view));
465         gtk_widget_hide_all(c->searchbar);
466         gtk_widget_hide_all(c->urlbar);
467         gtk_widget_show(c->vbox);
468         gtk_widget_show(c->indicator);
469         gtk_widget_show(c->scroll);
470         gtk_widget_show(GTK_WIDGET(c->view));
471         gtk_widget_show(c->win);
472         gdk_window_set_events(GTK_WIDGET(c->win)->window, GDK_ALL_EVENTS_MASK);
473         gdk_window_add_filter(GTK_WIDGET(c->win)->window, processx, c);
474         webkit_web_view_set_full_content_zoom(c->view, TRUE);
475         settings = webkit_web_view_get_settings(c->view);
476         g_object_set(G_OBJECT(settings), "user-agent", "surf", NULL);
477         uri = g_strconcat("file://", stylefile, NULL);
478         g_object_set(G_OBJECT(settings), "user-stylesheet-uri", uri, NULL);
479         g_free(uri);
480
481         c->download = NULL;
482         c->title = NULL;
483         c->next = clients;
484         clients = c;
485         if(showxid) {
486                 gdk_display_sync(gtk_widget_get_display(c->win));
487                 printf("%u\n", (guint)GDK_WINDOW_XID(GTK_WIDGET(c->win)->window));
488                 fflush(NULL);
489         }
490         return c;
491 }
492
493 WebKitWebView *
494 newwindow(WebKitWebView  *v, WebKitWebFrame *f, Client *c) {
495         Client *n = newclient();
496         return n->view;
497 }
498
499  
500 void
501 pasteurl(GtkClipboard *clipboard, const gchar *text, gpointer d) {
502         Arg arg = {.v = text };
503         if(text != NULL)
504                 loaduri((Client *) d, &arg);
505 }
506
507 GdkFilterReturn
508 processx(GdkXEvent *e, GdkEvent *event, gpointer d) {
509         Client *c = (Client *)d;
510         XPropertyEvent *ev;
511         Atom adummy;
512         gint idummy;
513         unsigned long ldummy;
514         unsigned char *buf = NULL;
515         Arg arg;
516
517         if(((XEvent *)e)->type == PropertyNotify) {
518                 ev = &((XEvent *)e)->xproperty;
519                 if(ev->atom == urlprop && ev->state == PropertyNewValue) {
520                         if(ignore_once)
521                                ignore_once = FALSE;
522                         else {
523                                 XGetWindowProperty(dpy, ev->window, urlprop, 0L, BUFSIZ, False, XA_STRING,
524                                         &adummy, &idummy, &ldummy, &ldummy, &buf);
525                                 arg.v = buf;
526                                 loaduri(c, &arg);
527                                 XFree(buf);
528                         }
529                         return GDK_FILTER_REMOVE;
530                 }
531         }
532         return GDK_FILTER_CONTINUE;
533 }
534
535 void
536 print(Client *c, const Arg *arg) {
537         webkit_web_frame_print(webkit_web_view_get_main_frame(c->view));
538 }
539
540 void
541 proccookies(SoupMessage *m, Client *c) {
542         GSList *l;
543         SoupCookie *co;
544         long t;
545
546         rereadcookies();
547         for (l = soup_cookies_from_response(m); l; l = l->next){
548                 co = (SoupCookie *)l->data;
549                 t = co->expires ?  soup_date_to_time_t(co->expires) : 0;
550                 setcookie(co->name, co->value, co->domain, co->value, t);
551         }
552         g_slist_free(l);
553 }
554
555 void
556 progresschange(WebKitWebView* view, gint p, Client *c) {
557         c->progress = p;
558         update(c);
559 }
560
561 void
562 request(SoupSession *s, SoupMessage *m, Client *c) {
563         soup_message_add_header_handler(m, "got-headers", "Set-Cookie",
564                         G_CALLBACK(proccookies), c);
565 }
566
567 void
568 reload(Client *c, const Arg *arg) {
569         gboolean nocache = *(gboolean *)arg;
570         if(nocache)
571                  webkit_web_view_reload_bypass_cache(c->view);
572         else
573                  webkit_web_view_reload(c->view);
574 }
575
576 void
577 rereadcookies(void) {
578 }
579
580 void
581 scroll(Client *c, const Arg *arg) {
582         gdouble v;
583         GtkAdjustment *a;
584
585         a = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(c->scroll));
586         v = gtk_adjustment_get_value(a);
587         v += gtk_adjustment_get_step_increment(a) * arg->i;
588         v = MAX(v, 0.0);
589         v = MIN(v, gtk_adjustment_get_upper(a) - gtk_adjustment_get_page_size(a));
590         gtk_adjustment_set_value(a, v);
591 }
592
593 void
594 setcookie(char *name, char *val, char *dom, char *path, long exp) {
595
596 }
597
598 void
599 setup(void) {
600         SoupSession *s;
601
602         gtk_init(NULL, NULL);
603         if (!g_thread_supported())
604                 g_thread_init(NULL);
605
606         dpy = GDK_DISPLAY();
607         session = webkit_get_default_session();
608         urlprop = XInternAtom(dpy, "_SURF_URL", False);
609
610         /* create dirs and files */
611         cookiefile = buildpath(cookiefile);
612         dldir = buildpath(dldir);
613         scriptfile = buildpath(scriptfile);
614         stylefile = buildpath(stylefile);
615
616         /* cookie persistance */
617         s = webkit_get_default_session();
618         cookiejar = soup_cookie_jar_text_new(cookiefile, FALSE);
619         soup_session_add_feature(s, SOUP_SESSION_FEATURE(cookiejar));
620 }
621
622 void
623 showsearch(Client *c, const Arg *arg) {
624         hideurl(c, NULL);
625         gtk_widget_show(c->searchbar);
626         gtk_widget_grab_focus(c->searchbar);
627 }
628
629 void
630 source(Client *c, const Arg *arg) {
631         Arg a = { .b = FALSE };
632         /*gboolean s;
633
634         s = webkit_web_view_get_view_source_mode(c->view);
635         webkit_web_view_set_view_source_mode(c->view, c->source);*/
636         reload(c, &a);
637 }
638
639 void
640 searchtext(Client *c, const Arg *arg) {
641         gboolean forward = *(gboolean *)arg;
642         webkit_web_view_search_text(c->view,
643                         gtk_entry_get_text(GTK_ENTRY(c->searchbar)),
644                         FALSE,
645                         forward,
646                         TRUE);
647 }
648
649 void
650 showurl(Client *c, const Arg *arg) {
651         gchar *uri;
652
653         hidesearch(c, NULL);
654         uri = geturi(c);
655         gtk_entry_set_text(GTK_ENTRY(c->urlbar), uri);
656         gtk_widget_show(c->urlbar);
657         gtk_widget_grab_focus(c->urlbar);
658 }
659
660 void
661 stop(Client *c, const Arg *arg) {
662         if(c->download)
663                 webkit_download_cancel(c->download);
664         else
665                 webkit_web_view_stop_loading(c->view);
666         c->download = NULL;
667 }
668
669 void
670 titlechange(WebKitWebView *v, WebKitWebFrame *f, const gchar *t, Client *c) {
671         c->title = copystr(&c->title, t);
672         update(c);
673 }
674
675 gboolean
676 unfocusbar(GtkWidget *w, GdkEventFocus *e, Client *c) {
677         hidesearch(c, NULL);
678         hideurl(c, NULL);
679         return FALSE;
680 }
681
682 void
683 usage(void) {
684         fputs("surf - simple browser\n", stderr);
685         die("usage: surf [-e Window] [-x] [uri]\n");
686 }
687
688 void
689 update(Client *c) {
690         gchar *t;
691
692         if(c->progress == 100)
693                 t = g_strdup(c->title);
694         else
695                 t = g_strdup_printf("%s [%i%%]", c->title, c->progress);
696         drawindicator(c);
697         gtk_window_set_title(GTK_WINDOW(c->win), t);
698         g_free(t);
699
700 }
701
702 void
703 windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame, JSContextRef js, JSObjectRef win, Client *c) {
704         JSStringRef jsscript;
705         gchar *script;
706         JSValueRef exception = NULL;
707         GError *error;
708         
709         if(g_file_get_contents(scriptfile, &script, NULL, &error)) {
710                 jsscript = JSStringCreateWithUTF8CString(script);
711                 JSEvaluateScript(js, jsscript, JSContextGetGlobalObject(js), NULL, 0, &exception);
712         }
713 }
714
715 void
716 zoom(Client *c, const Arg *arg) {
717         if(arg->i < 0)          /* zoom out */
718                 webkit_web_view_zoom_out(c->view);
719         else if(arg->i > 0)     /* zoom in */
720                 webkit_web_view_zoom_in(c->view);
721         else                    /* reset */
722                 webkit_web_view_set_zoom_level(c->view, 1.0);
723 }
724
725 int main(int argc, char *argv[]) {
726         int i;
727         Arg arg;
728         Client *c;
729
730         /* command line args */
731         for(i = 1, arg.v = NULL; i < argc; i++) {
732                 if(!strcmp(argv[i], "-x"))
733                         showxid = TRUE;
734                 else if(!strcmp(argv[i], "-e")) {
735                         if(++i < argc)
736                                 embed = atoi(argv[i]);
737                         else
738                                 usage();
739                 }
740                 else if(!strcmp(argv[i], "--"))
741                         break;
742                 else if(!strcmp(argv[i], "-v"))
743                         die("surf-"VERSION", © 2009 surf engineers, see LICENSE for details\n");
744                 else if(argv[i][0] == '-')
745                         usage();
746                 else
747                         arg.v = argv[i];
748         }
749         setup();
750         c = newclient();
751         if(arg.v) {
752                 if(strchr("./", ((char *)arg.v)[0]) || strcmp("-", (char *)arg.v) == 0)
753                         loadfile(c, (char *)arg.v);
754                 else
755                         loaduri(c, &arg);
756         }
757         gtk_main();
758         cleanup();
759         return EXIT_SUCCESS;
760 }