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