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