+
+ dpy = GDK_DISPLAY();
+
+ /* atoms */
+ atoms[AtomFind] = XInternAtom(dpy, "_SURF_FIND", False);
+ atoms[AtomGo] = XInternAtom(dpy, "_SURF_GO", False);
+ atoms[AtomUri] = XInternAtom(dpy, "_SURF_URI", False);
+
+ /* dirs and files */
+ cookiefile = buildpath(cookiefile);
+ scriptfile = buildpath(scriptfile);
+ stylefile = buildpath(stylefile);
+
+ /* request handler */
+ s = webkit_get_default_session();
+
+ /* cookie jar */
+ soup_session_add_feature(s, SOUP_SESSION_FEATURE(cookiejar_new(cookiefile, FALSE)));
+
+ /* ssl */
+ g_object_set(G_OBJECT(s), "ssl-ca-file", cafile, NULL);
+ g_object_set(G_OBJECT(s), "ssl-strict", strictssl, 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);
+ usingproxy = 1;
+ }
+}
+
+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
+eval(Client *c, const Arg *arg) {
+ WebKitWebFrame *frame = webkit_web_view_get_main_frame(c->view);
+ evalscript(webkit_web_frame_get_global_context(frame),
+ ((char **)arg->v)[0], "");
+}
+
+void
+stop(Client *c, const Arg *arg) {
+ webkit_web_view_stop_loading(c->view);
+}
+
+void
+titlechange(WebKitWebView *v, WebKitWebFrame *f, const char *t, Client *c) {
+ c->title = copystr(&c->title, t);
+ update(c);
+}
+
+void
+toggle(Client *c, const Arg *arg) {
+ WebKitWebSettings *settings;
+ char *name = (char *)arg->v;
+ gboolean value;
+ Arg a = { .b = FALSE };
+
+ settings = webkit_web_view_get_settings(c->view);
+ g_object_get(G_OBJECT(settings), name, &value, NULL);
+ g_object_set(G_OBJECT(settings), name, !value, NULL);
+
+ reload(c,&a);
+}
+
+void
+gettogglestat(Client *c){
+ gboolean value;
+ WebKitWebSettings *settings = webkit_web_view_get_settings(c->view);
+
+ g_object_get(G_OBJECT(settings), "enable-caret-browsing",
+ &value, NULL);
+ togglestat[0] = value? 'C': 'c';
+
+ g_object_get(G_OBJECT(settings), "auto-load-images", &value, NULL);
+ togglestat[1] = value? 'I': 'i';
+
+ g_object_get(G_OBJECT(settings), "enable-scripts", &value, NULL);
+ togglestat[2] = value? 'S': 's';
+
+ g_object_get(G_OBJECT(settings), "enable-plugins", &value, NULL);
+ togglestat[3] = value? 'V': 'v';
+
+ togglestat[4] = '\0';
+}
+
+
+void
+update(Client *c) {
+ char *t;
+
+ gettogglestat(c);
+
+ if(c->linkhover) {
+ t = g_strdup_printf("%s| %s", togglestat, c->linkhover);
+ } else if(c->progress != 100) {
+ drawindicator(c);
+ gtk_widget_show(c->indicator);
+ t = g_strdup_printf("[%i%%] %s| %s", c->progress, togglestat,
+ c->title);
+ } else {
+ gtk_widget_hide_all(c->indicator);
+ t = g_strdup_printf("%s| %s", togglestat, c->title);
+ }
+
+ gtk_window_set_title(GTK_WINDOW(c->win), t);
+ g_free(t);
+}
+
+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 [-c cookiefile] [-e xid] [-i] [-p] [-r scriptfile]"
+ " [-s] [-t stylefile] [-u useragent] [-v] [-x] [uri]\n");
+}
+
+void
+windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame, JSContextRef js, JSObjectRef win, Client *c) {
+ runscript(frame);
+}
+
+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[]) {
+ Arg arg;
+
+ memset(&arg, 0, sizeof(arg));
+
+ /* command line args */
+ ARGBEGIN {
+ case 'c':
+ cookiefile = EARGF(usage());
+ break;
+ case 'e':
+ embed = strtol(EARGF(usage()), NULL, 0);
+ break;
+ case 'i':
+ loadimages = 0;
+ break;
+ case 'p':
+ enableplugins = 0;
+ break;
+ case 'r':
+ scriptfile = EARGF(usage());
+ break;
+ case 's':
+ enablescripts = 0;
+ break;
+ case 't':
+ stylefile = EARGF(usage());
+ break;
+ case 'u':
+ useragent = EARGF(usage());
+ break;
+ case 'x':
+ showxid = TRUE;
+ break;
+ case 'v':
+ die("surf-"VERSION", ©2009-2012 surf engineers, see LICENSE for details\n");
+ default:
+ usage();
+ } ARGEND;
+ if(argc > 0)
+ arg.v = argv[0];
+