+void
+newwindow(Client *c, const Arg *arg, gboolean noembed)
+{
+ guint i = 0;
+ const char *cmd[18], *uri;
+ const Arg a = { .v = (void *)cmd };
+ char tmp[64];
+
+ cmd[i++] = argv0;
+ cmd[i++] = "-a";
+ cmd[i++] = cookiepolicies;
+ if (!enablescrollbars)
+ cmd[i++] = "-b";
+ if (embed && !noembed) {
+ cmd[i++] = "-e";
+ snprintf(tmp, LENGTH(tmp), "%u", (int)embed);
+ cmd[i++] = tmp;
+ }
+ if (!allowgeolocation)
+ cmd[i++] = "-g";
+ if (!loadimages)
+ cmd[i++] = "-i";
+ if (kioskmode)
+ cmd[i++] = "-k";
+ if (!enableplugins)
+ cmd[i++] = "-p";
+ if (!enablescripts)
+ cmd[i++] = "-s";
+ if (showxid)
+ cmd[i++] = "-x";
+ if (enablediskcache)
+ cmd[i++] = "-D";
+ cmd[i++] = "-c";
+ cmd[i++] = cookiefile;
+ cmd[i++] = "--";
+ uri = arg->v ? (char *)arg->v : c->linkhover;
+ if (uri)
+ cmd[i++] = uri;
+ cmd[i++] = NULL;
+ spawn(NULL, &a);
+}
+
+gboolean
+contextmenu(WebKitWebView *view, GtkWidget *menu, WebKitHitTestResult *target,
+ gboolean keyboard, Client *c)
+{
+ GList *items = gtk_container_get_children(GTK_CONTAINER(GTK_MENU(menu)));
+
+ for (GList *l = items; l; l = l->next)
+ g_signal_connect(l->data, "activate", G_CALLBACK(menuactivate), c);
+
+ g_list_free(items);
+ return FALSE;
+}
+
+void
+menuactivate(GtkMenuItem *item, Client *c)
+{
+ /*
+ * context-menu-action-2000 open link
+ * context-menu-action-1 open link in window
+ * context-menu-action-2 download linked file
+ * context-menu-action-3 copy link location
+ * context-menu-action-7 copy image address
+ * context-menu-action-13 reload
+ * context-menu-action-10 back
+ * context-menu-action-11 forward
+ * context-menu-action-12 stop
+ */
+
+ const gchar *name, *uri;
+ GtkClipboard *prisel, *clpbrd;
+
+ name = gtk_actionable_get_action_name(GTK_ACTIONABLE(item));
+ if (name == NULL)
+ return;
+
+ if (!g_strcmp0(name, "context-menu-action-3")) {
+ prisel = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
+ gtk_clipboard_set_text(prisel, c->linkhover, -1);
+ } else if (!g_strcmp0(name, "context-menu-action-7")) {
+ prisel = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
+ clpbrd = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
+ uri = gtk_clipboard_wait_for_text(clpbrd);
+ if (uri)
+ gtk_clipboard_set_text(prisel, uri, -1);
+ }
+}
+
+void
+pasteuri(GtkClipboard *clipboard, const char *text, gpointer d)
+{
+ Arg arg = {.v = text };
+ if (text != NULL)
+ loaduri((Client *) d, &arg);
+}
+
+void
+print(Client *c, const Arg *arg)
+{
+ webkit_web_frame_print(webkit_web_view_get_main_frame(c->view));
+}
+
+GdkFilterReturn
+processx(GdkXEvent *e, GdkEvent *event, gpointer d)
+{
+ Client *c = (Client *)d;
+ XPropertyEvent *ev;
+ Arg arg;
+
+ if (((XEvent *)e)->type == PropertyNotify) {
+ ev = &((XEvent *)e)->xproperty;
+ if (ev->state == PropertyNewValue) {
+ if (ev->atom == atoms[AtomFind]) {
+ arg.b = TRUE;
+ find(c, &arg);
+
+ return GDK_FILTER_REMOVE;
+ } else if (ev->atom == atoms[AtomGo]) {
+ arg.v = getatom(c, AtomGo);
+ loaduri(c, &arg);
+
+ return GDK_FILTER_REMOVE;
+ }
+ }
+ }
+ return GDK_FILTER_CONTINUE;
+}
+
+void
+progresschange(WebKitWebView *view, GParamSpec *pspec, Client *c)
+{
+ c->progress = webkit_web_view_get_progress(c->view) * 100;
+ updatetitle(c);
+}
+
+void
+linkopen(Client *c, const Arg *arg)
+{
+ newwindow(NULL, arg, 1);
+}
+
+void
+linkopenembed(Client *c, const Arg *arg)
+{
+ newwindow(NULL, arg, 0);
+}
+
+void
+reload(Client *c, const Arg *arg)
+{
+ gboolean nocache = *(gboolean *)arg;
+ if (nocache)
+ webkit_web_view_reload_bypass_cache(c->view);
+ else
+ webkit_web_view_reload(c->view);
+}
+
+void
+scroll_h(Client *c, const Arg *arg)
+{
+ scroll(gtk_scrolled_window_get_hadjustment(
+ GTK_SCROLLED_WINDOW(c->scroll)), arg);
+}
+
+void
+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);
+
+ updatetitle(c);
+ /* Do not reload. */
+}
+
+void
+togglegeolocation(Client *c, const Arg *arg)
+{
+ Arg a = { .b = FALSE };
+
+ allowgeolocation ^= 1;
+ reload(c, &a);
+}
+
+void
+twitch(Client *c, const Arg *arg)
+{
+ GtkAdjustment *a;
+ gdouble v;
+
+ a = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(
+ c->scroll));
+
+ v = gtk_adjustment_get_value(a);
+
+ v += 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
+togglescrollbars(Client *c, const Arg *arg)
+{
+ GtkPolicyType vspolicy;
+ Arg a;
+
+ gtk_scrolled_window_get_policy(GTK_SCROLLED_WINDOW(c->scroll), NULL,
+ &vspolicy);
+
+ if (vspolicy == GTK_POLICY_AUTOMATIC) {
+ gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
+ GTK_POLICY_NEVER,
+ GTK_POLICY_NEVER);
+ } else {
+ gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
+ GTK_POLICY_AUTOMATIC,
+ GTK_POLICY_AUTOMATIC);
+ a.i = +1;
+ twitch(c, &a);
+ a.i = -1;
+ twitch(c, &a);
+ }
+}
+
+void
+togglestyle(Client *c, const Arg *arg)
+{
+ enablestyle = !enablestyle;
+ setstyle(c, enablestyle ? getstyle(geturi(c)) : "");
+
+ updatetitle(c);
+}
+
+void
+gettogglestat(Client *c)
+{
+ gboolean value;
+ int p = 0;
+ WebKitWebSettings *settings = webkit_web_view_get_settings(c->view);
+
+ togglestat[p++] = cookiepolicy_set(cookiepolicy_get());
+
+ g_object_get(G_OBJECT(settings), "enable-caret-browsing", &value,
+ NULL);
+ togglestat[p++] = value? 'C': 'c';
+
+ togglestat[p++] = allowgeolocation? 'G': 'g';
+
+ togglestat[p++] = enablediskcache? 'D': 'd';
+
+ g_object_get(G_OBJECT(settings), "auto-load-images", &value, NULL);
+ togglestat[p++] = value? 'I': 'i';
+
+ g_object_get(G_OBJECT(settings), "enable-scripts", &value, NULL);
+ togglestat[p++] = value? 'S': 's';
+
+ g_object_get(G_OBJECT(settings), "enable-plugins", &value, NULL);
+ togglestat[p++] = value? 'V': 'v';
+
+ togglestat[p++] = enablestyle ? 'M': 'm';
+
+ togglestat[p] = '\0';
+}
+
+void
+getpagestat(Client *c)
+{
+ const char *uri = geturi(c);
+
+ if (strstr(uri, "https://") == uri)
+ pagestat[0] = c->sslfailed ? 'U' : 'T';
+ else
+ pagestat[0] = '-';
+
+ pagestat[1] = '\0';
+}
+
+void
+updatetitle(Client *c)
+{
+ char *t;
+
+ if (showindicators) {
+ gettogglestat(c);
+ getpagestat(c);
+
+ if (c->linkhover) {
+ t = g_strdup_printf("%s:%s | %s", togglestat, pagestat,
+ c->linkhover);
+ } else if (c->progress != 100) {
+ t = g_strdup_printf("[%i%%] %s:%s | %s", c->progress,
+ togglestat, pagestat,
+ c->title == NULL ? "" : c->title);
+ } else {
+ t = g_strdup_printf("%s:%s | %s", togglestat, pagestat,
+ c->title == NULL ? "" : c->title);
+ }
+
+ gtk_window_set_title(GTK_WINDOW(c->win), t);
+ g_free(t);
+ } else {
+ gtk_window_set_title(GTK_WINDOW(c->win), (c->title == NULL) ?
+ "" : c->title);
+ }
+}
+
+void
+updatewinid(Client *c)
+{
+ snprintf(winid, LENGTH(winid), "%lu", c->xid);
+}
+
+void
+usage(void)
+{
+ die("usage: %s [-bBdDfFgGiIkKmMnNpPsSvx] [-a cookiepolicies ] "
+ "[-c cookiefile] [-e xid] [-r scriptfile] [-t stylefile] "
+ "[-u useragent] [-z zoomlevel] [uri]\n", basename(argv0));
+}
+
+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;