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