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