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