+scroll_v(Client *c, const Arg *arg)
+{
+ scroll(gtk_scrolled_window_get_vadjustment(
+ GTK_SCROLLED_WINDOW(c->scroll)), arg);
+}
+
+void
+scroll(GtkAdjustment *a, const Arg *arg)
+{
+ gdouble v;
+
+ v = gtk_adjustment_get_value(a);
+ switch (arg->i) {
+ case +10000:
+ case -10000:
+ v += gtk_adjustment_get_page_increment(a) * (arg->i / 10000);
+ break;
+ case +20000:
+ case -20000:
+ default:
+ v += gtk_adjustment_get_step_increment(a) * arg->i;
+ }
+
+ v = MAX(v, 0.0);
+ v = MIN(v, gtk_adjustment_get_upper(a) -
+ gtk_adjustment_get_page_size(a));
+ gtk_adjustment_set_value(a, v);
+}
+
+void
+setatom(Client *c, int a, const char *v)
+{
+ XSync(dpy, False);
+ XChangeProperty(dpy, c->xid,
+ atoms[a], XA_STRING, 8, PropModeReplace,
+ (unsigned char *)v, strlen(v) + 1);
+}
+
+void
+setup(void)
+{
+ int i;
+ char *styledirfile, *stylepath;
+ SoupSession *s;
+ GError *error = NULL;
+
+ /* clean up any zombies immediately */
+ sigchld(0);
+ gtk_init(NULL, NULL);
+
+ dpy = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
+
+ /* 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 = buildfile(cookiefile);
+ scriptfile = buildfile(scriptfile);
+ cachefolder = buildpath(cachefolder);
+ if (stylefile == NULL) {
+ styledir = buildpath(styledir);
+ for (i = 0; i < LENGTH(styles); i++) {
+ if (regcomp(&(styles[i].re), styles[i].regex,
+ REG_EXTENDED)) {
+ fprintf(stderr,
+ "Could not compile regex: %s\n",
+ styles[i].regex);
+ styles[i].regex = NULL;
+ }
+ styledirfile = g_strconcat(styledir, "/",
+ styles[i].style, NULL);
+ stylepath = buildfile(styledirfile);
+ styles[i].style = g_strconcat("file://", stylepath,
+ NULL);
+ g_free(styledirfile);
+ g_free(stylepath);
+ }
+ g_free(styledir);
+ } else {
+ stylepath = buildfile(stylefile);
+ stylefile = g_strconcat("file://", stylepath, NULL);
+ g_free(stylepath);
+ }
+
+ /* request handler */
+ s = webkit_get_default_session();
+
+ /* cookie jar */
+ soup_session_add_feature(s,
+ SOUP_SESSION_FEATURE(cookiejar_new(cookiefile,
+ FALSE, cookiepolicy_get())));
+
+ /* disk cache */
+ if (enablediskcache) {
+ diskcache = soup_cache_new(cachefolder,
+ SOUP_CACHE_SINGLE_USER);
+ soup_cache_set_max_size(diskcache, diskcachebytes);
+ soup_cache_load(diskcache);
+ soup_session_add_feature(s, SOUP_SESSION_FEATURE(diskcache));
+ }
+
+ /* ssl */
+ tlsdb = g_tls_file_database_new(cafile, &error);
+
+ if (error) {
+ g_warning("Error loading SSL database %s: %s", cafile,
+ error->message);
+ g_error_free(error);
+ }
+ g_object_set(G_OBJECT(s), "tls-database", tlsdb, NULL);
+ g_object_set(G_OBJECT(s), "ssl-strict", strictssl, NULL);
+}
+
+void
+sigchld(int unused)
+{
+ if (signal(SIGCHLD, sigchld) == SIG_ERR)
+ die("Can't install SIGCHLD handler");
+ while (0 < waitpid(-1, NULL, WNOHANG));
+}
+
+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 *view, GParamSpec *pspec, Client *c)
+{
+ const gchar *t = webkit_web_view_get_title(view);
+ if (t) {
+ c->title = copystr(&c->title, t);
+ updatetitle(c);
+ }
+}
+
+void
+titlechangeleave(void *a, void *b, Client *c)
+{
+ c->linkhover = NULL;
+ updatetitle(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
+togglecookiepolicy(Client *c, const Arg *arg)
+{
+ SoupCookieJar *jar;
+ SoupCookieJarAcceptPolicy policy;
+
+ jar = SOUP_COOKIE_JAR(soup_session_get_feature(
+ webkit_get_default_session(),
+ SOUP_TYPE_COOKIE_JAR));
+ g_object_get(G_OBJECT(jar), "accept-policy", &policy, NULL);
+
+ policysel++;
+ if (policysel >= strlen(cookiepolicies))
+ policysel = 0;
+
+ g_object_set(G_OBJECT(jar), "accept-policy", cookiepolicy_get(), NULL);