+
+ dpy = GDK_DISPLAY();
+ s = webkit_get_default_session();
+ uriprop = XInternAtom(dpy, "_SURF_URI", False);
+ findprop = XInternAtom(dpy, "_SURF_FIND", False);
+
+ /* dirs and files */
+ cookiefile = buildpath(cookiefile);
+ dldir = buildpath(dldir);
+ scriptfile = buildpath(scriptfile);
+ stylefile = buildpath(stylefile);
+
+ /* request handler */
+ s = webkit_get_default_session();
+ soup_session_remove_feature_by_type(s, soup_cookie_get_type());
+ soup_session_remove_feature_by_type(s, soup_cookie_jar_get_type());
+ g_signal_connect_after(G_OBJECT(s), "request-started", G_CALLBACK(newrequest), NULL);
+
+ /* proxy */
+ if((proxy = getenv("http_proxy")) && strcmp(proxy, "")) {
+ new_proxy = g_strrstr(proxy, "http://") ? g_strdup(proxy) :
+ g_strdup_printf("http://%s", proxy);
+
+ puri = soup_uri_new(new_proxy);
+ g_object_set(G_OBJECT(s), "proxy-uri", puri, NULL);
+ soup_uri_free(puri);
+ g_free(new_proxy);
+ }
+}
+
+void
+sigchld(int unused) {
+ if(signal(SIGCHLD, sigchld) == SIG_ERR)
+ die("Can't install SIGCHLD handler");
+ while(0 < waitpid(-1, NULL, WNOHANG));
+}
+
+void
+source(Client *c, const Arg *arg) {
+ Arg a = { .b = FALSE };
+ gboolean s;
+
+ s = webkit_web_view_get_view_source_mode(c->view);
+ webkit_web_view_set_view_source_mode(c->view, !s);
+ reload(c, &a);
+}
+
+void
+spawn(Client *c, const Arg *arg) {
+ if(fork() == 0) {
+ if(dpy)
+ close(ConnectionNumber(dpy));
+ setsid();
+ execvp(((char **)arg->v)[0], (char **)arg->v);
+ fprintf(stderr, "surf: execvp %s", ((char **)arg->v)[0]);
+ perror(" failed");
+ exit(0);
+ }
+}
+
+void
+stop(Client *c, const Arg *arg) {
+ if(c->download)
+ webkit_download_cancel(c->download);
+ else
+ webkit_web_view_stop_loading(c->view);
+ c->download = NULL;
+}
+
+void
+titlechange(WebKitWebView *v, WebKitWebFrame *f, const char *t, Client *c) {
+ c->title = copystr(&c->title, t);
+ update(c);
+}
+
+void
+update(Client *c) {
+ char *t;
+
+ if(c->progress != 100)
+ t = g_strdup_printf("[%i%%] %s", c->progress, c->title);
+ else if(c->linkhover)
+ t = g_strdup(c->linkhover);
+ else
+ t = g_strdup(c->title);
+ drawindicator(c);
+ gtk_window_set_title(GTK_WINDOW(c->win), t);
+ g_free(t);
+}
+
+void
+updatedownload(WebKitDownload *o, GParamSpec *pspec, Client *c) {
+ WebKitDownloadStatus status;
+
+ status = webkit_download_get_status(c->download);
+ if(status == WEBKIT_DOWNLOAD_STATUS_STARTED || status == WEBKIT_DOWNLOAD_STATUS_CREATED) {
+ c->progress = (gint)(webkit_download_get_progress(c->download)*100);
+ }
+ else
+ stop(c, NULL);
+ update(c);
+}
+
+void
+updatewinid(Client *c) {
+ snprintf(winid, LENGTH(winid), "%u",
+ (int)GDK_WINDOW_XID(GTK_WIDGET(c->win)->window));
+}
+
+void
+usage(void) {
+ fputs("surf - simple browser\n", stderr);
+ die("usage: surf [-e xid] [-i] [-p] [-s] [-v] [-x] [uri]\n");
+}
+
+void
+windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame, JSContextRef js, JSObjectRef win, Client *c) {
+ runscript(frame, js);
+}
+
+void
+zoom(Client *c, const Arg *arg) {
+ c->zoomed = TRUE;
+ if(arg->i < 0) /* zoom out */
+ webkit_web_view_zoom_out(c->view);
+ else if(arg->i > 0) /* zoom in */
+ webkit_web_view_zoom_in(c->view);
+ else { /* reset */
+ c->zoomed = FALSE;
+ webkit_web_view_set_zoom_level(c->view, 1.0);
+ }
+}
+
+int
+main(int argc, char *argv[]) {
+ int i;
+ Arg arg;
+
+ progname = argv[0];
+ /* command line args */
+ for(i = 1, arg.v = NULL; i < argc && argv[i][0] == '-' &&
+ argv[i][1] != '\0' && argv[i][2] == '\0'; i++) {
+ if(!strcmp(argv[i], "--")) {
+ i++;