improving urlbar hiding.
[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 focusview(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         g_signal_connect(G_OBJECT(c->view), "focus-in-event", G_CALLBACK(focusview), c);
423
424         /* urlbar */
425         c->urlbar = gtk_entry_new();
426         gtk_entry_set_has_frame(GTK_ENTRY(c->urlbar), FALSE);
427
428         /* searchbar */
429         c->searchbar = gtk_entry_new();
430         gtk_entry_set_has_frame(GTK_ENTRY(c->searchbar), FALSE);
431
432         /* indicator */
433         c->indicator = gtk_drawing_area_new();
434         gtk_widget_set_size_request(c->indicator, 0, 2);
435         g_signal_connect (G_OBJECT (c->indicator), "expose_event",
436                         G_CALLBACK (exposeindicator), c);
437
438         /* Arranging */
439         gtk_container_add(GTK_CONTAINER(c->scroll), GTK_WIDGET(c->view));
440         gtk_container_add(GTK_CONTAINER(c->win), c->vbox);
441         gtk_container_add(GTK_CONTAINER(c->vbox), c->scroll);
442         gtk_container_add(GTK_CONTAINER(c->vbox), c->searchbar);
443         gtk_container_add(GTK_CONTAINER(c->vbox), c->urlbar);
444         gtk_container_add(GTK_CONTAINER(c->vbox), c->indicator);
445
446         /* Setup */
447         gtk_box_set_child_packing(GTK_BOX(c->vbox), c->urlbar, FALSE, FALSE, 0, GTK_PACK_START);
448         gtk_box_set_child_packing(GTK_BOX(c->vbox), c->searchbar, FALSE, FALSE, 0, GTK_PACK_START);
449         gtk_box_set_child_packing(GTK_BOX(c->vbox), c->indicator, FALSE, FALSE, 0, GTK_PACK_START);
450         gtk_box_set_child_packing(GTK_BOX(c->vbox), c->scroll, TRUE, TRUE, 0, GTK_PACK_START);
451         gtk_widget_grab_focus(GTK_WIDGET(c->view));
452         gtk_widget_hide_all(c->searchbar);
453         gtk_widget_hide_all(c->urlbar);
454         gtk_widget_show(c->vbox);
455         gtk_widget_show(c->indicator);
456         gtk_widget_show(c->scroll);
457         gtk_widget_show(GTK_WIDGET(c->view));
458         gtk_widget_show(c->win);
459         gdk_window_set_events(GTK_WIDGET(c->win)->window, GDK_ALL_EVENTS_MASK);
460         gdk_window_add_filter(GTK_WIDGET(c->win)->window, processx, c);
461         webkit_web_view_set_full_content_zoom(c->view, TRUE);
462         settings = webkit_web_view_get_settings(c->view);
463         g_object_set(G_OBJECT(settings), "user-agent", useragent, NULL);
464         uri = g_strconcat("file://", stylefile, NULL);
465         g_object_set(G_OBJECT(settings), "user-stylesheet-uri", uri, NULL);
466         g_free(uri);
467
468         c->download = NULL;
469         c->title = NULL;
470         c->next = clients;
471         clients = c;
472         if(showxid) {
473                 gdk_display_sync(gtk_widget_get_display(c->win));
474                 printf("%u\n", (guint)GDK_WINDOW_XID(GTK_WIDGET(c->win)->window));
475                 fflush(NULL);
476         }
477         return c;
478 }
479
480 void
481 newproc(const gchar *url) {
482         guint i = 0, urlindex;
483         const gchar *cmd[7];
484         const Arg arg = { .v = (void *)cmd };
485         gchar tmp[64];
486
487         cmd[i++] = progname;
488         if(embed) {
489                 cmd[i++] = "-e";
490                 snprintf(tmp, LENGTH(tmp), "%u\n", (int)embed);
491                 cmd[i++] = tmp;
492         }
493         if(showxid) {
494                 cmd[i++] = "-x";
495         }
496         cmd[i++] = "--";
497         urlindex = i;
498         if(url)
499                 cmd[i++] = url;
500         cmd[i++] = NULL;
501         spawn(NULL, &arg);
502 }
503
504 WebKitWebView *
505 createwindow(WebKitWebView  *v, WebKitWebFrame *f, Client *c) {
506         Client *n = newclient();
507         return n->view;
508 }
509
510 void
511 pasteurl(GtkClipboard *clipboard, const gchar *text, gpointer d) {
512         Arg arg = {.v = text };
513         if(text != NULL)
514                 loaduri((Client *) d, &arg);
515 }
516
517 GdkFilterReturn
518 processx(GdkXEvent *e, GdkEvent *event, gpointer d) {
519         Client *c = (Client *)d;
520         XPropertyEvent *ev;
521         Atom adummy;
522         gint idummy;
523         unsigned long ldummy;
524         unsigned char *buf = NULL;
525         Arg arg;
526
527         if(((XEvent *)e)->type == PropertyNotify) {
528                 ev = &((XEvent *)e)->xproperty;
529                 if(ev->atom == urlprop && ev->state == PropertyNewValue) {
530                         if(ignore_once)
531                                ignore_once = FALSE;
532                         else {
533                                 XGetWindowProperty(dpy, ev->window, urlprop, 0L, BUFSIZ, False, XA_STRING,
534                                         &adummy, &idummy, &ldummy, &ldummy, &buf);
535                                 arg.v = buf;
536                                 loaduri(c, &arg);
537                                 XFree(buf);
538                         }
539                         return GDK_FILTER_REMOVE;
540                 }
541         }
542         return GDK_FILTER_CONTINUE;
543 }
544
545 void
546 print(Client *c, const Arg *arg) {
547         webkit_web_frame_print(webkit_web_view_get_main_frame(c->view));
548 }
549
550 void
551 proccookies(SoupMessage *m, Client *c) {
552         GSList *l;
553         SoupCookie *co;
554         long t;
555
556         rereadcookies();
557         for (l = soup_cookies_from_response(m); l; l = l->next){
558                 co = (SoupCookie *)l->data;
559                 t = co->expires ?  soup_date_to_time_t(co->expires) : 0;
560                 setcookie(co->name, co->value, co->domain, co->value, t);
561         }
562         g_slist_free(l);
563 }
564
565 void
566 progresschange(WebKitWebView* view, gint p, Client *c) {
567         c->progress = p;
568         update(c);
569 }
570
571 void
572 request(SoupSession *s, SoupMessage *m, Client *c) {
573         soup_message_add_header_handler(m, "got-headers", "Set-Cookie",
574                         G_CALLBACK(proccookies), c);
575 }
576
577 void
578 reload(Client *c, const Arg *arg) {
579         gboolean nocache = *(gboolean *)arg;
580         if(nocache)
581                  webkit_web_view_reload_bypass_cache(c->view);
582         else
583                  webkit_web_view_reload(c->view);
584 }
585
586 void
587 rereadcookies(void) {
588
589 }
590
591 void
592 scroll(Client *c, const Arg *arg) {
593         gdouble v;
594         GtkAdjustment *a;
595
596         a = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(c->scroll));
597         v = gtk_adjustment_get_value(a);
598         v += gtk_adjustment_get_step_increment(a) * arg->i;
599         v = MAX(v, 0.0);
600         v = MIN(v, gtk_adjustment_get_upper(a) - gtk_adjustment_get_page_size(a));
601         gtk_adjustment_set_value(a, v);
602 }
603
604 void
605 sigchld(int unused) {
606         if(signal(SIGCHLD, sigchld) == SIG_ERR)
607                 die("Can't install SIGCHLD handler");
608         while(0 < waitpid(-1, NULL, WNOHANG));
609 }
610
611 void
612 setcookie(char *name, char *val, char *dom, char *path, long exp) {
613
614 }
615
616 void
617 setup(void) {
618         SoupSession *s;
619
620         /* clean up any zombies immediately */
621         sigchld(0);
622         gtk_init(NULL, NULL);
623         if (!g_thread_supported())
624                 g_thread_init(NULL);
625
626         dpy = GDK_DISPLAY();
627         session = webkit_get_default_session();
628         urlprop = XInternAtom(dpy, "_SURF_URL", False);
629
630         /* create dirs and files */
631         cookiefile = buildpath(cookiefile);
632         dldir = buildpath(dldir);
633         scriptfile = buildpath(scriptfile);
634         stylefile = buildpath(stylefile);
635
636         /* cookie persistance */
637         s = webkit_get_default_session();
638         cookiejar = soup_cookie_jar_text_new(cookiefile, FALSE);
639         soup_session_add_feature(s, SOUP_SESSION_FEATURE(cookiejar));
640 }
641
642 void
643 showsearch(Client *c, const Arg *arg) {
644         hideurl(c, NULL);
645         gtk_widget_show(c->searchbar);
646         gtk_widget_grab_focus(c->searchbar);
647 }
648
649 void
650 source(Client *c, const Arg *arg) {
651         Arg a = { .b = FALSE };
652         gboolean s;
653
654         s = webkit_web_view_get_view_source_mode(c->view);
655         webkit_web_view_set_view_source_mode(c->view, !s);
656         reload(c, &a);
657 }
658
659 void
660 searchtext(Client *c, const Arg *arg) {
661         const gchar *text;
662         gboolean forward = *(gboolean *)arg;
663         text = gtk_entry_get_text(GTK_ENTRY(c->searchbar));
664         webkit_web_view_search_text(c->view, text, FALSE, forward, TRUE);
665         webkit_web_view_mark_text_matches(c->view, text, FALSE, 0);
666 }
667
668 void
669 showurl(Client *c, const Arg *arg) {
670         gchar *uri;
671
672         hidesearch(c, NULL);
673         uri = geturi(c);
674         gtk_entry_set_text(GTK_ENTRY(c->urlbar), uri);
675         gtk_widget_show(c->urlbar);
676         gtk_widget_grab_focus(c->urlbar);
677 }
678
679 void
680 stop(Client *c, const Arg *arg) {
681         if(c->download)
682                 webkit_download_cancel(c->download);
683         else
684                 webkit_web_view_stop_loading(c->view);
685         c->download = NULL;
686 }
687
688 void
689 spawn(Client *c, const Arg *arg) {
690         if(fork() == 0) {
691                 if(dpy)
692                         close(ConnectionNumber(dpy));
693                 setsid();
694                 execvp(((char **)arg->v)[0], (char **)arg->v);
695                 fprintf(stderr, "tabbed: execvp %s", ((char **)arg->v)[0]);
696                 perror(" failed");
697                 exit(0);
698         }
699 }
700
701 void
702 titlechange(WebKitWebView *v, WebKitWebFrame *f, const gchar *t, Client *c) {
703         c->title = copystr(&c->title, t);
704         update(c);
705 }
706
707 gboolean
708 focusview(GtkWidget *w, GdkEventFocus *e, Client *c) {
709         hidesearch(c, NULL);
710         hideurl(c, NULL);
711         return FALSE;
712 }
713
714 void
715 usage(void) {
716         fputs("surf - simple browser\n", stderr);
717         die("usage: surf [-e Window] [-x] [uri]\n");
718 }
719
720 void
721 update(Client *c) {
722         gchar *t;
723
724         if(c->progress == 100)
725                 t = g_strdup(c->title);
726         else
727                 t = g_strdup_printf("%s [%i%%]", c->title, c->progress);
728         drawindicator(c);
729         gtk_window_set_title(GTK_WINDOW(c->win), t);
730         g_free(t);
731
732 }
733
734 void
735 updatewinid(Client *c) {
736         snprintf(winid, LENGTH(winid), "%u",
737                         (int)GDK_WINDOW_XID(GTK_WIDGET(c->win)->window));
738 }
739
740 void
741 windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame, JSContextRef js, JSObjectRef win, Client *c) {
742         JSStringRef jsscript;
743         gchar *script;
744         JSValueRef exception = NULL;
745         GError *error;
746         
747         if(g_file_get_contents(scriptfile, &script, NULL, &error)) {
748                 jsscript = JSStringCreateWithUTF8CString(script);
749                 JSEvaluateScript(js, jsscript, JSContextGetGlobalObject(js), NULL, 0, &exception);
750         }
751 }
752
753 void
754 zoom(Client *c, const Arg *arg) {
755         if(arg->i < 0)          /* zoom out */
756                 webkit_web_view_zoom_out(c->view);
757         else if(arg->i > 0)     /* zoom in */
758                 webkit_web_view_zoom_in(c->view);
759         else                    /* reset */
760                 webkit_web_view_set_zoom_level(c->view, 1.0);
761 }
762
763 int main(int argc, char *argv[]) {
764         int i;
765         Arg arg;
766
767         progname = argv[0];
768         /* command line args */
769         for(i = 1, arg.v = NULL; i < argc && argv[i][0] == '-'; i++) {
770                 if(!strcmp(argv[i], "-x"))
771                         showxid = TRUE;
772                 else if(!strcmp(argv[i], "-e")) {
773                         if(++i < argc)
774                                 embed = atoi(argv[i]);
775                         else
776                                 usage();
777                 }
778                 else if(!strcmp(argv[i], "--")) {
779                         i++;
780                         break;
781                 }
782                 else if(!strcmp(argv[i], "-v"))
783                         die("surf-"VERSION", © 2009 surf engineers, see LICENSE for details\n");
784                 else
785                         usage();
786         }
787         if(i < argc)
788                 arg.v = argv[i];
789         setup();
790         newclient();
791         if(arg.v) {
792                 loaduri(clients, &arg);
793         }
794         gtk_main();
795         cleanup();
796         return EXIT_SUCCESS;
797 }