+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[26], *uri;
+ const Arg arg = { .v = cmd };
+
+ cmd[i++] = argv0;
+ cmd[i++] = "-a";
+ cmd[i++] = cookiepolicies;
+ cmd[i++] = enablescrollbars ? "-B" : "-b";
+ if (cookiefile && g_strcmp0(cookiefile, "")) {
+ cmd[i++] = "-c";
+ cmd[i++] = cookiefile;
+ }
+ cmd[i++] = enablecache ? "-D" : "-d";
+ if (embed && !noembed) {
+ cmd[i++] = "-e";
+ snprintf(tmp, LENGTH(tmp), "%lu", embed);
+ cmd[i++] = tmp;
+ }
+ cmd[i++] = runinfullscreen ? "-F" : "-f";
+ cmd[i++] = allowgeolocation ? "-G" : "-g";
+ cmd[i++] = loadimages ? "-I" : "-i";
+ cmd[i++] = kioskmode ? "-K" : "-k";
+ cmd[i++] = enablestyle ? "-M" : "-m";
+ cmd[i++] = enableinspector ? "-N" : "-n";
+ cmd[i++] = enableplugins ? "-P" : "-p";
+ if (scriptfile && g_strcmp0(scriptfile, "")) {
+ cmd[i++] = "-r";
+ cmd[i++] = scriptfile;
+ }
+ cmd[i++] = enablescripts ? "-S" : "-s";
+ if (stylefile && g_strcmp0(stylefile, "")) {
+ cmd[i++] = "-t";
+ cmd[i++] = stylefile;
+ }
+ if (fulluseragent && g_strcmp0(fulluseragent, "")) {
+ cmd[i++] = "-u";
+ cmd[i++] = fulluseragent;
+ }
+ if (showxid)
+ cmd[i++] = "-x";
+ /* do not keep zoom level */
+ cmd[i++] = "--";
+ if ((uri = a->v))
+ cmd[i++] = uri;
+ cmd[i] = NULL;
+
+ spawn(c, &arg);
+}
+
+void
+spawn(Client *c, const Arg *a)
+{
+ if (fork() == 0) {
+ if (dpy)
+ close(ConnectionNumber(dpy));
+ setsid();
+ execvp(((char **)a->v)[0], (char **)a->v);
+ fprintf(stderr, "surf: execvp %s", ((char **)a->v)[0]);
+ perror(" failed");
+ exit(0);
+ }
+}
+
+void
+destroyclient(Client *c)
+{
+ Client *p;
+
+ webkit_web_view_stop_loading(c->view);
+ /* Not needed, has already been called
+ gtk_widget_destroy(c->win);
+ */
+
+ for (p = clients; p && p->next != c; p = p->next)
+ ;
+ if (p)
+ p->next = c->next;
+ else
+ clients = c->next;
+ free(c);
+}
+
+void
+cleanup(void)
+{
+ while (clients)
+ destroyclient(clients);
+ g_free(cookiefile);
+ g_free(scriptfile);
+ g_free(stylefile);
+ g_free(cachedir);
+}
+
+WebKitWebView *
+newview(Client *c, WebKitWebView *rv)
+{
+ WebKitWebView *v;
+ WebKitSettings *settings;
+ WebKitUserContentManager *contentmanager;
+ WebKitWebContext *context;
+
+ /* Webview */
+ if (rv) {
+ v = WEBKIT_WEB_VIEW(
+ webkit_web_view_new_with_related_view(rv));
+ } else {
+ settings = webkit_settings_new_with_settings(
+ "auto-load-images", loadimages,
+ "default-font-size", defaultfontsize,
+ "enable-caret-browsing", enablecaretbrowsing,
+ "enable-developer-extras", enableinspector,
+ "enable-dns-prefetching", enablednsprefetching,
+ "enable-frame-flattening", enableframeflattening,
+ "enable-html5-database", enablecache,
+ "enable-html5-local-storage", enablecache,
+ "enable-javascript", enablescripts,
+ "enable-plugins", enableplugins,
+ NULL);
+/* For mor interesting settings, have a look at
+ * http://webkitgtk.org/reference/webkit2gtk/stable/WebKitSettings.html */
+
+ if (strcmp(fulluseragent, "")) {
+ webkit_settings_set_user_agent(settings, fulluseragent);
+ } else if (surfuseragent) {
+ webkit_settings_set_user_agent_with_application_details(
+ settings, "Surf", VERSION);
+ }
+ useragent = webkit_settings_get_user_agent(settings);
+
+ contentmanager = webkit_user_content_manager_new();
+
+ context = webkit_web_context_new_with_website_data_manager(
+ webkit_website_data_manager_new(
+ "base-cache-directory", cachedir,
+ "base-data-directory", cachedir,
+ NULL));
+
+ /* rendering process model, can be a shared unique one
+ * or one for each view */
+ webkit_web_context_set_process_model(context,
+ WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
+ /* ssl */
+ webkit_web_context_set_tls_errors_policy(context, strictssl ?
+ WEBKIT_TLS_ERRORS_POLICY_FAIL :
+ WEBKIT_TLS_ERRORS_POLICY_IGNORE);
+ /* disk cache */
+ webkit_web_context_set_cache_model(context, enablecache ?
+ WEBKIT_CACHE_MODEL_WEB_BROWSER :
+ WEBKIT_CACHE_MODEL_DOCUMENT_VIEWER);
+
+ /* Currently only works with text file to be compatible with curl */
+ webkit_cookie_manager_set_persistent_storage(
+ webkit_web_context_get_cookie_manager(context), cookiefile,
+ WEBKIT_COOKIE_PERSISTENT_STORAGE_TEXT);
+ /* cookie policy */
+ webkit_cookie_manager_set_accept_policy(
+ webkit_web_context_get_cookie_manager(context),
+ cookiepolicy_get());
+
+ g_signal_connect(G_OBJECT(context), "download-started",
+ G_CALLBACK(downloadstarted), c);
+
+ v = g_object_new(WEBKIT_TYPE_WEB_VIEW,
+ "settings", settings,
+ "user-content-manager", contentmanager,
+ "web-context", context,
+ NULL);
+ }
+
+ g_signal_connect(G_OBJECT(v), "notify::title",
+ G_CALLBACK(titlechanged), c);
+ g_signal_connect(G_OBJECT(v), "mouse-target-changed",
+ G_CALLBACK(mousetargetchanged), c);
+ g_signal_connect(G_OBJECT(v), "permission-request",
+ G_CALLBACK(permissionrequested), c);
+ g_signal_connect(G_OBJECT(v), "create",
+ G_CALLBACK(createview), c);
+ g_signal_connect(G_OBJECT(v), "ready-to-show",
+ G_CALLBACK(showview), c);
+ g_signal_connect(G_OBJECT(v), "decide-policy",
+ G_CALLBACK(decidepolicy), c);
+ g_signal_connect(G_OBJECT(v), "load-changed",
+ G_CALLBACK(loadchanged), c);
+ g_signal_connect(G_OBJECT(v), "notify::estimated-load-progress",
+ G_CALLBACK(progresschanged), c);
+ g_signal_connect(G_OBJECT(v), "button-release-event",
+ G_CALLBACK(buttonreleased), c);
+ g_signal_connect(G_OBJECT(v), "close",
+ G_CALLBACK(closeview), c);
+
+ return v;
+}
+
+GtkWidget *
+createview(WebKitWebView *v, WebKitNavigationAction *a, Client *c)
+{
+ Client *n;
+
+ switch (webkit_navigation_action_get_navigation_type(a)) {
+ case WEBKIT_NAVIGATION_TYPE_OTHER: /* fallthrough */
+ /*
+ * popup windows of type “other” are almost always triggered
+ * by user gesture, so inverse the logic here
+ */
+/* instead of this, compare destination uri to mouse-over uri for validating window */
+ if (webkit_navigation_action_is_user_gesture(a))
+ return NULL;
+ case WEBKIT_NAVIGATION_TYPE_LINK_CLICKED: /* fallthrough */
+ case WEBKIT_NAVIGATION_TYPE_FORM_SUBMITTED: /* fallthrough */
+ case WEBKIT_NAVIGATION_TYPE_BACK_FORWARD: /* fallthrough */
+ case WEBKIT_NAVIGATION_TYPE_RELOAD: /* fallthrough */
+ case WEBKIT_NAVIGATION_TYPE_FORM_RESUBMITTED:
+ n = newclient(c);
+ break;
+ default:
+ return NULL;
+ }
+
+ return GTK_WIDGET(n->view);