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