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