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