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