+ buf[0] = '\0';
+ XFree(p);
+
+ return buf;
+}
+
+void
+updatetitle(Client *c)
+{
+ char *title;
+ const char *name = c->overtitle ? c->overtitle :
+ c->title ? c->title : "";
+
+ if (curconfig[ShowIndicators].val.b) {
+ gettogglestats(c);
+ getpagestats(c);
+
+ if (c->progress != 100)
+ title = g_strdup_printf("[%i%%] %s:%s | %s",
+ c->progress, togglestats, pagestats, name);
+ else
+ title = g_strdup_printf("%s:%s | %s",
+ togglestats, pagestats, name);
+
+ gtk_window_set_title(GTK_WINDOW(c->win), title);
+ g_free(title);
+ } else {
+ gtk_window_set_title(GTK_WINDOW(c->win), name);
+ }
+}
+
+void
+gettogglestats(Client *c)
+{
+ togglestats[0] = cookiepolicy_set(cookiepolicy_get());
+ togglestats[1] = curconfig[CaretBrowsing].val.b ? 'C' : 'c';
+ togglestats[2] = curconfig[Geolocation].val.b ? 'G' : 'g';
+ togglestats[3] = curconfig[DiskCache].val.b ? 'D' : 'd';
+ togglestats[4] = curconfig[LoadImages].val.b ? 'I' : 'i';
+ togglestats[5] = curconfig[JavaScript].val.b ? 'S' : 's';
+ togglestats[6] = curconfig[Plugins].val.b ? 'V' : 'v';
+ togglestats[7] = curconfig[Style].val.b ? 'M' : 'm';
+ togglestats[8] = curconfig[FrameFlattening].val.b ? 'F' : 'f';
+ togglestats[9] = curconfig[Certificate].val.b ? 'X' : 'x';
+ togglestats[10] = curconfig[StrictTLS].val.b ? 'T' : 't';
+ togglestats[11] = '\0';
+}
+
+void
+getpagestats(Client *c)
+{
+ if (c->https)
+ pagestats[0] = (c->tlserr || c->insecure) ? 'U' : 'T';
+ else
+ pagestats[0] = '-';
+ pagestats[1] = '\0';
+}
+
+WebKitCookieAcceptPolicy
+cookiepolicy_get(void)
+{
+ switch (((char *)curconfig[CookiePolicies].val.v)[cookiepolicy]) {
+ case 'a':
+ return WEBKIT_COOKIE_POLICY_ACCEPT_NEVER;
+ case '@':
+ return WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY;
+ default: /* fallthrough */
+ case 'A':
+ return WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS;
+ }
+}
+
+char
+cookiepolicy_set(const WebKitCookieAcceptPolicy p)
+{
+ switch (p) {
+ case WEBKIT_COOKIE_POLICY_ACCEPT_NEVER:
+ return 'a';
+ case WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY:
+ return '@';
+ default: /* fallthrough */
+ case WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS:
+ return 'A';
+ }
+}
+
+void
+seturiparameters(Client *c, const char *uri)
+{
+ Parameter *newconfig = NULL;
+ int i;
+
+ for (i = 0; i < LENGTH(uriparams); ++i) {
+ if (uriparams[i].uri &&
+ !regexec(&(uriparams[i].re), uri, 0, NULL, 0)) {
+ newconfig = uriparams[i].config;
+ break;
+ }
+ }
+
+ if (!newconfig)
+ newconfig = defconfig;
+ if (newconfig == curconfig)
+ return;
+
+ for (i = 0; i < ParameterLast; ++i) {
+ if (defconfig[i].force)
+ continue;
+ if (newconfig[i].force)
+ setparameter(c, 0, i, &newconfig[i].val);
+ else if (curconfig[i].force)
+ setparameter(c, 0, i, &defconfig[i].val);
+ }
+
+ curconfig = newconfig;
+}
+
+void
+setparameter(Client *c, int refresh, ParamName p, const Arg *a)
+{
+ GdkRGBA bgcolor = { 0 };
+ WebKitSettings *s = webkit_web_view_get_settings(c->view);
+
+ switch (p) {
+ case AcceleratedCanvas:
+ webkit_settings_set_enable_accelerated_2d_canvas(s, a->b);
+ break;
+ case CaretBrowsing:
+ webkit_settings_set_enable_caret_browsing(s, a->b);
+ refresh = 0;
+ break;
+ case Certificate:
+ if (a->b)
+ setcert(c, geturi(c));
+ return; /* do not update */
+ case CookiePolicies:
+ webkit_cookie_manager_set_accept_policy(
+ webkit_web_context_get_cookie_manager(
+ webkit_web_view_get_context(c->view)),
+ cookiepolicy_get());
+ refresh = 0;
+ break;
+ case DiskCache:
+ webkit_web_context_set_cache_model(
+ webkit_web_view_get_context(c->view), a->b ?
+ WEBKIT_CACHE_MODEL_WEB_BROWSER :
+ WEBKIT_CACHE_MODEL_DOCUMENT_VIEWER);
+ return; /* do not update */
+ case DefaultCharset:
+ webkit_settings_set_default_charset(s, a->v);
+ return; /* do not update */
+ case DNSPrefetch:
+ webkit_settings_set_enable_dns_prefetching(s, a->b);
+ return; /* do not update */
+ case FontSize:
+ webkit_settings_set_default_font_size(s, a->i);
+ return; /* do not update */
+ case FrameFlattening:
+ webkit_settings_set_enable_frame_flattening(s, a->b);
+ break;
+ case Geolocation:
+ refresh = 0;
+ break;
+ case HideBackground:
+ if (a->b)
+ webkit_web_view_set_background_color(c->view, &bgcolor);
+ return; /* do not update */
+ case Inspector:
+ webkit_settings_set_enable_developer_extras(s, a->b);
+ return; /* do not update */
+ case Java:
+ webkit_settings_set_enable_java(s, a->b);
+ return; /* do not update */
+ case JavaScript:
+ webkit_settings_set_enable_javascript(s, a->b);
+ break;
+ case KioskMode:
+ return; /* do nothing */
+ case LoadImages:
+ webkit_settings_set_auto_load_images(s, a->b);
+ break;
+ case MediaManualPlay:
+ webkit_settings_set_media_playback_requires_user_gesture(s, a->b);
+ break;
+ case Plugins:
+ webkit_settings_set_enable_plugins(s, a->b);
+ break;
+ case PreferredLanguages:
+ return; /* do nothing */
+ case RunInFullscreen:
+ return; /* do nothing */
+ case ScrollBars:
+ /* Disabled until we write some WebKitWebExtension for
+ * manipulating the DOM directly.
+ enablescrollbars = !enablescrollbars;
+ evalscript(c, "document.documentElement.style.overflow = '%s'",
+ enablescrollbars ? "auto" : "hidden");
+ */
+ return; /* do not update */
+ case ShowIndicators:
+ break;
+ case SmoothScrolling:
+ webkit_settings_set_enable_smooth_scrolling(s, a->b);
+ return; /* do not update */
+ case SiteQuirks:
+ webkit_settings_set_enable_site_specific_quirks(s, a->b);
+ break;
+ case SpellChecking:
+ webkit_web_context_set_spell_checking_enabled(
+ webkit_web_view_get_context(c->view), a->b);
+ return; /* do not update */
+ case SpellLanguages:
+ return; /* do nothing */
+ case StrictTLS:
+ webkit_web_context_set_tls_errors_policy(
+ webkit_web_view_get_context(c->view), a->b ?
+ WEBKIT_TLS_ERRORS_POLICY_FAIL :
+ WEBKIT_TLS_ERRORS_POLICY_IGNORE);
+ break;
+ case Style:
+ if (a->b)
+ setstyle(c, getstyle(geturi(c)));
+ else
+ webkit_user_content_manager_remove_all_style_sheets(
+ webkit_web_view_get_user_content_manager(c->view));
+ refresh = 0;
+ break;
+ case ZoomLevel:
+ webkit_web_view_set_zoom_level(c->view, a->f);
+ return; /* do not update */
+ default:
+ return; /* do nothing */
+ }
+
+ updatetitle(c);
+ if (refresh)
+ reload(c, a);
+}
+
+const char *
+getcert(const char *uri)
+{
+ int i;
+
+ for (i = 0; i < LENGTH(certs); ++i) {
+ if (certs[i].regex &&
+ !regexec(&(certs[i].re), uri, 0, NULL, 0))
+ return certs[i].file;
+ }
+
+ return NULL;
+}
+
+void
+setcert(Client *c, const char *uri)
+{
+ const char *file = getcert(uri);
+ char *host;
+ GTlsCertificate *cert;
+
+ if (!file)
+ return;
+
+ if (!(cert = g_tls_certificate_new_from_file(file, NULL))) {
+ fprintf(stderr, "Could not read certificate file: %s\n", file);
+ return;
+ }
+
+ if ((uri = strstr(uri, "https://"))) {
+ uri += sizeof("https://") - 1;
+ host = g_strndup(uri, strchr(uri, '/') - uri);
+ webkit_web_context_allow_tls_certificate_for_host(
+ webkit_web_view_get_context(c->view), cert, host);
+ g_free(host);
+ }
+
+ g_object_unref(cert);
+
+}
+
+const char *
+getstyle(const char *uri)
+{
+ int i;
+
+ if (stylefile)
+ return stylefile;
+
+ for (i = 0; i < LENGTH(styles); ++i) {
+ if (styles[i].regex &&
+ !regexec(&(styles[i].re), uri, 0, NULL, 0))
+ return styles[i].file;
+ }
+
+ return "";
+}
+
+void
+setstyle(Client *c, const char *file)
+{
+ gchar *style;
+
+ if (!g_file_get_contents(file, &style, NULL, NULL)) {
+ fprintf(stderr, "Could not read style file: %s\n", file);
+ return;
+ }
+
+ webkit_user_content_manager_add_style_sheet(
+ webkit_web_view_get_user_content_manager(c->view),
+ webkit_user_style_sheet_new(style,
+ WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES,
+ WEBKIT_USER_STYLE_LEVEL_USER,
+ NULL, NULL));
+
+ g_free(style);
+}
+
+void
+runscript(Client *c)
+{
+ gchar *script;
+ gsize l;
+
+ if (g_file_get_contents(scriptfile, &script, &l, NULL) && l)
+ evalscript(c, script);
+ g_free(script);
+}
+
+void
+evalscript(Client *c, const char *jsstr, ...)
+{
+ va_list ap;
+ gchar *script;
+
+ va_start(ap, jsstr);
+ script = g_strdup_vprintf(jsstr, ap);
+ va_end(ap);
+
+ webkit_web_view_run_javascript(c->view, script, NULL, NULL, NULL);
+ g_free(script);
+}
+
+void
+updatewinid(Client *c)
+{
+ snprintf(winid, LENGTH(winid), "%lu", c->xid);
+}
+
+void
+handleplumb(Client *c, const char *uri)
+{
+ Arg a = (Arg)PLUMB(uri);
+ spawn(c, &a);
+}
+
+void
+newwindow(Client *c, const Arg *a, int noembed)
+{
+ int i = 0;
+ char tmp[64];
+ const char *cmd[29], *uri;
+ const Arg arg = { .v = cmd };
+
+ cmd[i++] = argv0;
+ cmd[i++] = "-a";
+ cmd[i++] = curconfig[CookiePolicies].val.v;
+ cmd[i++] = curconfig[ScrollBars].val.b ? "-B" : "-b";
+ if (cookiefile && g_strcmp0(cookiefile, "")) {
+ cmd[i++] = "-c";
+ cmd[i++] = cookiefile;
+ }
+ if (stylefile && g_strcmp0(stylefile, "")) {
+ cmd[i++] = "-C";
+ cmd[i++] = stylefile;