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