closing bars when unfocusing them.
[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
19 #define LENGTH(x) (sizeof x / sizeof x[0])
20 #define CLEANMASK(mask)         (mask & ~(GDK_MOD2_MASK))
21
22 Display *dpy;
23 Atom urlprop;
24 typedef union Arg Arg;
25 union Arg {
26         const gboolean b;
27         const gint i;
28         const void *v;
29 } ;
30
31 typedef struct Client {
32         GtkWidget *win, *scroll, *vbox, *urlbar, *searchbar, *indicator;
33         WebKitWebView *view;
34         WebKitDownload *download;
35         gchar *title;
36         gint progress;
37         struct Client *next;
38 } Client;
39
40 typedef struct Cookie {
41         char *name;
42         char *value;
43         char *domain;
44         char *path;
45         struct Cookie *next;
46 } Cookie;
47
48 typedef enum {
49     BROWSER = 0x0001,
50     SEARCHBAR = 0x0010,
51     URLBAR = 0x0100,
52     ALWAYS = ~0,
53 } KeyFocus;
54
55 typedef struct {
56         guint mod;
57         guint keyval;
58         void (*func)(Client *c, const Arg *arg);
59         const Arg arg;
60         KeyFocus focus;
61 } Key;
62
63 SoupCookieJar *cookiejar;
64 SoupSession *session;
65 Client *clients = NULL;
66 Cookie *cookies = NULL;
67 gboolean embed = FALSE;
68 gboolean showxid = FALSE;
69 gboolean ignore_once = FALSE;
70 extern char *optarg;
71 extern gint optind;
72
73 static void cleanup(void);
74 static void clipboard(Client *c, const Arg *arg);
75 static gchar *copystr(gchar **str, const gchar *src);
76 static void destroyclient(Client *c);
77 static void destroywin(GtkWidget* w, Client *c);
78 static void die(char *str);
79 static void download(WebKitDownload *o, GParamSpec *pspec, Client *c);
80 static void drawindicator(Client *c);
81 static gboolean exposeindicator(GtkWidget *w, GdkEventExpose *e, Client *c);
82 static gboolean initdownload(WebKitWebView *view, WebKitDownload *o, Client *c);
83 static gchar *geturi(Client *c);
84 static void hidesearch(Client *c, const Arg *arg);
85 static void hideurl(Client *c, const Arg *arg);
86 static gboolean keypress(GtkWidget* w, GdkEventKey *ev, Client *c);
87 static void linkhover(WebKitWebView* page, const gchar* t, const gchar* l, Client *c);
88 static void loadcommit(WebKitWebView *view, WebKitWebFrame *f, Client *c);
89 static void loadstart(WebKitWebView *view, WebKitWebFrame *f, Client *c);
90 static void loadfile(Client *c, const gchar *f);
91 static void loaduri(Client *c, const Arg *arg);
92 static void navigate(Client *c, const Arg *arg);
93 static Client *newclient(void);
94 static WebKitWebView *newwindow(WebKitWebView  *v, WebKitWebFrame *f, Client *c);
95 static void pasteurl(GtkClipboard *clipboard, const gchar *text, gpointer d);
96 static GdkFilterReturn processx(GdkXEvent *xevent, GdkEvent *event, gpointer d);
97 static void print(Client *c, const Arg *arg);
98 static void proccookies(SoupMessage *m, Client *c);
99 static void progresschange(WebKitWebView *view, gint p, Client *c);
100 static void request(SoupSession *s, SoupMessage *m, Client *c);
101 static void reload(Client *c, const Arg *arg);
102 static void rereadcookies(void);
103 static void setcookie(char *name, char *val, char *dom, char *path, long exp);
104 static void setup(void);
105 static void titlechange(WebKitWebView* view, WebKitWebFrame* frame,
106                 const gchar* title, Client *c);
107 static void scroll(Client *c, const Arg *arg);
108 static void searchtext(Client *c, const Arg *arg);
109 static void source(Client *c, const Arg *arg);
110 static void showsearch(Client *c, const Arg *arg);
111 static void showurl(Client *c, const Arg *arg);
112 static void stop(Client *c, const Arg *arg);
113 static void titlechange(WebKitWebView* view, WebKitWebFrame* frame, const gchar* title, Client *c);
114 static gboolean unfocusbar(GtkWidget *w, GdkEventFocus *e, Client *c);
115 static void usage(void);
116 static void update(Client *c);
117 static void zoom(Client *c, const Arg *arg);
118
119 #include "config.h"
120
121 void
122 cleanup(void) {
123         while(clients)
124                 destroyclient(clients);
125 }
126
127 void
128 clipboard(Client *c, const Arg *arg) {
129         gboolean paste = *(gboolean *)arg;
130
131         if(paste)
132                 gtk_clipboard_request_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), pasteurl, c);
133         else
134                 gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), webkit_web_view_get_uri(c->view), -1);
135 }
136
137 gchar *
138 copystr(gchar **str, const gchar *src) {
139         gchar *tmp;
140         tmp = g_strdup(src);
141
142         if(str && *str) {
143                 g_free(*str);
144                 *str = tmp;
145         }
146         return tmp;
147 }
148
149 void
150 destroyclient(Client *c) {
151         Client *p;
152
153         gtk_widget_destroy(GTK_WIDGET(webkit_web_view_new()));
154         gtk_widget_destroy(c->scroll);
155         gtk_widget_destroy(c->urlbar);
156         gtk_widget_destroy(c->searchbar);
157         gtk_widget_destroy(c->vbox);
158         gtk_widget_destroy(c->win);
159         for(p = clients; p && p->next != c; p = p->next);
160         if(p)
161                 p->next = c->next;
162         else
163                 clients = c->next;
164         free(c);
165         if(clients == NULL)
166                 gtk_main_quit();
167 }
168
169 void
170 destroywin(GtkWidget* w, Client *c) {
171         destroyclient(c);
172 }
173
174 void
175 die(char *str) {
176         fputs(str, stderr);
177         exit(EXIT_FAILURE);
178 }
179
180 void
181 drawindicator(Client *c) {
182         gint width;
183         gchar *uri;
184         GtkWidget *w;
185         GdkGC *gc;
186         GdkColor fg;
187
188         uri = geturi(c);
189         w = c->indicator;
190         width = c->progress * w->allocation.width / 100;
191         gc = gdk_gc_new(w->window);
192         gdk_color_parse(strstr(uri, "https://") == uri ?
193                         progress_trust : progress, &fg);
194         gdk_gc_set_rgb_fg_color(gc, &fg);
195         gdk_draw_rectangle(w->window,
196                         w->style->bg_gc[GTK_WIDGET_STATE(w)],
197                         TRUE, 0, 0, w->allocation.width, w->allocation.height);
198         gdk_draw_rectangle(w->window, gc, TRUE, 0, 0, width,
199                         w->allocation.height);
200         g_object_unref(gc);
201 }
202
203 gboolean
204 exposeindicator(GtkWidget *w, GdkEventExpose *e, Client *c) {
205         drawindicator(c);
206         return TRUE;
207 }
208
209 void
210 download(WebKitDownload *o, GParamSpec *pspec, Client *c) {
211         WebKitDownloadStatus status;
212
213         status = webkit_download_get_status(c->download);
214         if(status == WEBKIT_DOWNLOAD_STATUS_STARTED || status == WEBKIT_DOWNLOAD_STATUS_CREATED) {
215                 c->progress = (gint)(webkit_download_get_progress(c->download)*100);
216         }
217         update(c);
218 }
219
220 gboolean
221 initdownload(WebKitWebView *view, WebKitDownload *o, Client *c) {
222         const gchar *home, *filename;
223         gchar *uri, *path, *html;
224
225         stop(c, NULL);
226         c->download = o;
227         home = g_get_home_dir();
228         filename = webkit_download_get_suggested_filename(o);
229         path = g_build_filename(home, ".surf", "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         if(!(c = calloc(1, sizeof(Client))))
377                 die("Cannot malloc!\n");
378         /* Window */
379         if(embed) {
380                 c->win = gtk_plug_new(0);
381         }
382         else {
383                 c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
384                 gtk_window_set_wmclass(GTK_WINDOW(c->win), "surf", "surf");
385         }
386         gtk_window_set_default_size(GTK_WINDOW(c->win), 800, 600);
387         g_signal_connect(G_OBJECT(c->win), "destroy", G_CALLBACK(destroywin), c);
388         g_signal_connect(G_OBJECT(c->win), "key-press-event", G_CALLBACK(keypress), c);
389
390         /* VBox */
391         c->vbox = gtk_vbox_new(FALSE, 0);
392
393         /* scrolled window */
394         c->scroll = gtk_scrolled_window_new(NULL, NULL);
395         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
396                         GTK_POLICY_NEVER, GTK_POLICY_NEVER);
397
398         /* webview */
399         c->view = WEBKIT_WEB_VIEW(webkit_web_view_new());
400         g_signal_connect(G_OBJECT(c->view), "title-changed", G_CALLBACK(titlechange), c);
401         g_signal_connect(G_OBJECT(c->view), "load-progress-changed", G_CALLBACK(progresschange), c);
402         g_signal_connect(G_OBJECT(c->view), "load-committed", G_CALLBACK(loadcommit), c);
403         g_signal_connect(G_OBJECT(c->view), "load-started", G_CALLBACK(loadstart), c);
404         g_signal_connect(G_OBJECT(c->view), "hovering-over-link", G_CALLBACK(linkhover), c);
405         g_signal_connect(G_OBJECT(c->view), "create-web-view", G_CALLBACK(newwindow), c);
406         g_signal_connect(G_OBJECT(c->view), "download-requested", G_CALLBACK(initdownload), c);
407         g_signal_connect_after(session, "request-started", G_CALLBACK(request), c);
408
409         /* urlbar */
410         c->urlbar = gtk_entry_new();
411         gtk_entry_set_has_frame(GTK_ENTRY(c->urlbar), FALSE);
412         g_signal_connect(G_OBJECT(c->urlbar), "focus-out-event", G_CALLBACK(unfocusbar), c);
413
414         /* searchbar */
415         c->searchbar = gtk_entry_new();
416         gtk_entry_set_has_frame(GTK_ENTRY(c->searchbar), FALSE);
417         g_signal_connect(G_OBJECT(c->searchbar), "focus-out-event", G_CALLBACK(unfocusbar), c);
418
419         /* indicator */
420         c->indicator = gtk_drawing_area_new();
421         gtk_widget_set_size_request(c->indicator, 0, 2);
422         g_signal_connect (G_OBJECT (c->indicator), "expose_event",
423                         G_CALLBACK (exposeindicator), c);
424
425         /* downloadbar */
426
427         /* Arranging */
428         gtk_container_add(GTK_CONTAINER(c->scroll), GTK_WIDGET(c->view));
429         gtk_container_add(GTK_CONTAINER(c->win), c->vbox);
430         gtk_container_add(GTK_CONTAINER(c->vbox), c->scroll);
431         gtk_container_add(GTK_CONTAINER(c->vbox), c->searchbar);
432         gtk_container_add(GTK_CONTAINER(c->vbox), c->urlbar);
433         gtk_container_add(GTK_CONTAINER(c->vbox), c->indicator);
434
435         /* Setup */
436         gtk_box_set_child_packing(GTK_BOX(c->vbox), c->urlbar, FALSE, FALSE, 0, GTK_PACK_START);
437         gtk_box_set_child_packing(GTK_BOX(c->vbox), c->searchbar, FALSE, FALSE, 0, GTK_PACK_START);
438         gtk_box_set_child_packing(GTK_BOX(c->vbox), c->indicator, FALSE, FALSE, 0, GTK_PACK_START);
439         gtk_box_set_child_packing(GTK_BOX(c->vbox), c->scroll, TRUE, TRUE, 0, GTK_PACK_START);
440         gtk_widget_grab_focus(GTK_WIDGET(c->view));
441         gtk_widget_hide_all(c->searchbar);
442         gtk_widget_hide_all(c->urlbar);
443         gtk_widget_show(c->vbox);
444         gtk_widget_show(c->indicator);
445         gtk_widget_show(c->scroll);
446         gtk_widget_show(GTK_WIDGET(c->view));
447         gtk_widget_show(c->win);
448         gdk_window_set_events(GTK_WIDGET(c->win)->window, GDK_ALL_EVENTS_MASK);
449         gdk_window_add_filter(GTK_WIDGET(c->win)->window, processx, c);
450         webkit_web_view_set_full_content_zoom(c->view, TRUE);
451         c->download = NULL;
452         c->title = NULL;
453         c->next = clients;
454         clients = c;
455         if(showxid) {
456                 gdk_display_sync(gtk_widget_get_display(c->win));
457                 printf("%u\n", (guint)GDK_WINDOW_XID(GTK_WIDGET(c->win)->window));
458                 fflush(NULL);
459         }
460         return c;
461 }
462
463 WebKitWebView *
464 newwindow(WebKitWebView  *v, WebKitWebFrame *f, Client *c) {
465         Client *n = newclient();
466         return n->view;
467 }
468
469  
470 void
471 pasteurl(GtkClipboard *clipboard, const gchar *text, gpointer d) {
472         Arg arg = {.v = text };
473         if(text != NULL)
474                 loaduri((Client *) d, &arg);
475 }
476
477 GdkFilterReturn
478 processx(GdkXEvent *e, GdkEvent *event, gpointer d) {
479         Client *c = (Client *)d;
480         XPropertyEvent *ev;
481         Atom adummy;
482         gint idummy;
483         unsigned long ldummy;
484         unsigned char *buf = NULL;
485         Arg arg;
486
487         if(((XEvent *)e)->type == PropertyNotify) {
488                 ev = &((XEvent *)e)->xproperty;
489                 if(ev->atom == urlprop && ev->state == PropertyNewValue) {
490                         if(ignore_once)
491                                ignore_once = FALSE;
492                         else {
493                                 XGetWindowProperty(dpy, ev->window, urlprop, 0L, BUFSIZ, False, XA_STRING,
494                                         &adummy, &idummy, &ldummy, &ldummy, &buf);
495                                 arg.v = buf;
496                                 loaduri(c, &arg);
497                                 XFree(buf);
498                         }
499                         return GDK_FILTER_REMOVE;
500                 }
501         }
502         return GDK_FILTER_CONTINUE;
503 }
504
505 void
506 print(Client *c, const Arg *arg) {
507         webkit_web_frame_print(webkit_web_view_get_main_frame(c->view));
508 }
509
510 void
511 proccookies(SoupMessage *m, Client *c) {
512         GSList *l;
513         SoupCookie *co;
514         long t;
515
516         rereadcookies();
517         for (l = soup_cookies_from_response(m); l; l = l->next){
518                 co = (SoupCookie *)l->data;
519                 t = co->expires ?  soup_date_to_time_t(co->expires) : 0;
520                 setcookie(co->name, co->value, co->domain, co->value, t);
521         }
522         g_slist_free(l);
523 }
524
525 void
526 progresschange(WebKitWebView* view, gint p, Client *c) {
527         c->progress = p;
528         update(c);
529 }
530
531 void
532 request(SoupSession *s, SoupMessage *m, Client *c) {
533         soup_message_add_header_handler(m, "got-headers", "Set-Cookie",
534                         G_CALLBACK(proccookies), c);
535 }
536
537 void
538 reload(Client *c, const Arg *arg) {
539         gboolean nocache = *(gboolean *)arg;
540         if(nocache)
541                  webkit_web_view_reload_bypass_cache(c->view);
542         else
543                  webkit_web_view_reload(c->view);
544 }
545
546 void
547 rereadcookies(void) {
548         const gchar *filename, *home;
549
550         home = g_get_home_dir();
551         filename = g_build_filename(home, ".surf", "cookies", NULL);
552 }
553
554 void
555 scroll(Client *c, const Arg *arg) {
556         gdouble v;
557         GtkAdjustment *a;
558
559         a = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(c->scroll));
560         v = gtk_adjustment_get_value(a);
561         v += gtk_adjustment_get_step_increment(a) * arg->i;
562         v = MAX(v, 0.0);
563         v = MIN(v, gtk_adjustment_get_upper(a) - gtk_adjustment_get_page_size(a));
564         gtk_adjustment_set_value(a, v);
565 }
566
567 void
568 setcookie(char *name, char *val, char *dom, char *path, long exp) {
569
570 }
571
572 void
573 setup(void) {
574         dpy = GDK_DISPLAY();
575         session = webkit_get_default_session();
576         urlprop = XInternAtom(dpy, "_SURF_URL", False);
577 }
578
579 void
580 showsearch(Client *c, const Arg *arg) {
581         hideurl(c, NULL);
582         gtk_widget_show(c->searchbar);
583         gtk_widget_grab_focus(c->searchbar);
584 }
585
586 void
587 source(Client *c, const Arg *arg) {
588         Arg a = { .b = FALSE };
589         /*gboolean s;
590
591         s = webkit_web_view_get_view_source_mode(c->view);
592         webkit_web_view_set_view_source_mode(c->view, c->source);*/
593         reload(c, &a);
594 }
595
596 void
597 searchtext(Client *c, const Arg *arg) {
598         gboolean forward = *(gboolean *)arg;
599         webkit_web_view_search_text(c->view,
600                         gtk_entry_get_text(GTK_ENTRY(c->searchbar)),
601                         FALSE,
602                         forward,
603                         TRUE);
604 }
605
606 void
607 showurl(Client *c, const Arg *arg) {
608         gchar *uri;
609
610         hidesearch(c, NULL);
611         uri = geturi(c);
612         gtk_entry_set_text(GTK_ENTRY(c->urlbar), uri);
613         gtk_widget_show(c->urlbar);
614         gtk_widget_grab_focus(c->urlbar);
615 }
616
617 void
618 stop(Client *c, const Arg *arg) {
619         if(c->download)
620                 webkit_download_cancel(c->download);
621         else
622                 webkit_web_view_stop_loading(c->view);
623         c->download = NULL;
624 }
625
626 void
627 titlechange(WebKitWebView *v, WebKitWebFrame *f, const gchar *t, Client *c) {
628         c->title = copystr(&c->title, t);
629         update(c);
630 }
631
632 gboolean
633 unfocusbar(GtkWidget *w, GdkEventFocus *e, Client *c) {
634         hidesearch(c, NULL);
635         hideurl(c, NULL);
636         return TRUE;
637 }
638
639 void
640 usage(void) {
641         fputs("surf - simple browser\n", stderr);
642         die("usage: surf [-e] [-x] [uri]\n");
643 }
644
645 void
646 update(Client *c) {
647         gchar *t;
648
649         if(c->progress == 100)
650                 t = g_strdup(c->title);
651         else
652                 t = g_strdup_printf("%s [%i%%]", c->title, c->progress);
653         drawindicator(c);
654         gtk_window_set_title(GTK_WINDOW(c->win), t);
655         g_free(t);
656
657 }
658
659 void
660 zoom(Client *c, const Arg *arg) {
661         if(arg->i < 0)          /* zoom out */
662                 webkit_web_view_zoom_out(c->view);
663         else if(arg->i > 0)     /* zoom in */
664                 webkit_web_view_zoom_in(c->view);
665         else                    /* reset */
666                 webkit_web_view_set_zoom_level(c->view, 1.0);
667 }
668
669 int main(int argc, char *argv[]) {
670         SoupSession *s;
671         Client *c;
672         gint o;
673         const gchar *home, *filename;
674         Arg arg;
675
676         gtk_init(NULL, NULL);
677         if (!g_thread_supported())
678                 g_thread_init(NULL);
679         setup();
680         while((o = getopt(argc, argv, "vhxeu:f:")) != -1)
681                 switch(o) {
682                 case 'x':
683                         showxid = TRUE;
684                         break;
685                 case 'e':
686                         showxid = TRUE;
687                         embed = TRUE;
688                         break;
689                 case 'v':
690                         die("surf-"VERSION", © 2009 surf engineers, see LICENSE for details\n");
691                         break;
692                 default:
693                         usage();
694                 }
695         if(optind + 1 == argc) {
696                 c = newclient();
697                 arg.v = argv[optind];
698                 if(strchr("./", argv[optind][0]) || strcmp("-", argv[optind]) == 0)
699                         loadfile(c, argv[optind]);
700                 else
701                         loaduri(c, &arg);
702
703         }
704         else if(optind != argc)
705                 usage();
706         if(!clients)
707                 newclient();
708
709         /* make dirs */
710         home = g_get_home_dir();
711         filename = g_build_filename(home, ".surf", "dl", NULL);
712         g_mkdir_with_parents(filename, 0755);
713
714         /* cookie persistance */
715         s = webkit_get_default_session();
716         filename = g_build_filename(home, ".surf", "cookies.jar", NULL);
717         cookiejar = soup_cookie_jar_text_new(filename, FALSE);
718         soup_session_add_feature(s, SOUP_SESSION_FEATURE(cookiejar));
719
720         gtk_main();
721         cleanup();
722         return EXIT_SUCCESS;
723 }