Send message size inside messages through pipes
[surf.git] / surf.c
diff --git a/surf.c b/surf.c
index 61448fb..2b54e3c 100644 (file)
--- a/surf.c
+++ b/surf.c
@@ -11,7 +11,6 @@
 #include <pwd.h>
 #include <regex.h>
 #include <signal.h>
-#include <stdarg.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <webkit2/webkit2.h>
 #include <X11/X.h>
 #include <X11/Xatom.h>
+#include <glib.h>
 
 #include "arg.h"
+#include "common.h"
 
 #define LENGTH(x)               (sizeof(x) / sizeof(x[0]))
 #define CLEANMASK(mask)         (mask & (MODKEY|GDK_SHIFT_MASK))
@@ -79,6 +80,7 @@ typedef enum {
        SpellLanguages,
        StrictTLS,
        Style,
+       WebGL,
        ZoomLevel,
        ParameterLast
 } ParamName;
@@ -103,6 +105,7 @@ typedef struct Client {
        GTlsCertificate *cert, *failedcert;
        GTlsCertificateFlags tlserr;
        Window xid;
+       unsigned long pageid;
        int progress, fullscreen, https, insecure, errorpage;
        const char *title, *overtitle, *targeturi;
        const char *needle;
@@ -139,12 +142,12 @@ typedef struct {
 
 /* Surf */
 static void usage(void);
-static void die(const char *errstr, ...);
 static void setup(void);
 static void sigchld(int unused);
 static void sighup(int unused);
 static char *buildfile(const char *path);
 static char *buildpath(const char *path);
+static char *untildepath(const char *path);
 static const char *getuserhomedir(const char *user);
 static const char *getcurrentuserhomedir(void);
 static Client *newclient(Client *c);
@@ -169,6 +172,7 @@ static void updatewinid(Client *c);
 static void handleplumb(Client *c, const char *uri);
 static void newwindow(Client *c, const Arg *a, int noembed);
 static void spawn(Client *c, const Arg *a);
+static void msgext(Client *c, char type, const Arg *a);
 static void destroyclient(Client *c);
 static void cleanup(void);
 
@@ -181,6 +185,7 @@ static gboolean buttonreleased(GtkWidget *w, GdkEvent *e, Client *c);
 static GdkFilterReturn processx(GdkXEvent *xevent, GdkEvent *event,
                                 gpointer d);
 static gboolean winevent(GtkWidget *w, GdkEvent *e, Client *c);
+static gboolean readpipe(GIOChannel *s, GIOCondition ioc, gpointer unused);
 static void showview(WebKitWebView *v, Client *c);
 static GtkWidget *createwindow(Client *c);
 static gboolean loadfailedtls(WebKitWebView *v, gchar *uri,
@@ -204,6 +209,9 @@ static void downloadstarted(WebKitWebContext *wc, WebKitDownload *d,
                             Client *c);
 static void responsereceived(WebKitDownload *d, GParamSpec *ps, Client *c);
 static void download(Client *c, WebKitURIResponse *r);
+static void webprocessterminated(WebKitWebView *v,
+                                 WebKitWebProcessTerminationReason r,
+                                 Client *c);
 static void closeview(WebKitWebView *v, Client *c);
 static void destroywin(GtkWidget* w, Client *c);
 
@@ -214,7 +222,8 @@ static void print(Client *c, const Arg *a);
 static void showcert(Client *c, const Arg *a);
 static void clipboard(Client *c, const Arg *a);
 static void zoom(Client *c, const Arg *a);
-static void scroll(Client *c, const Arg *a);
+static void scrollv(Client *c, const Arg *a);
+static void scrollh(Client *c, const Arg *a);
 static void navigate(Client *c, const Arg *a);
 static void stop(Client *c, const Arg *a);
 static void toggle(Client *c, const Arg *a);
@@ -241,6 +250,8 @@ static GdkDevice *gdkkb;
 static char *stylefile;
 static const char *useragent;
 static Parameter *curconfig;
+static int modparams[ParameterLast];
+static int pipein[2], pipeout[2];
 char *argv0;
 
 static ParamName loadtransient[] = {
@@ -298,20 +309,10 @@ usage(void)
            "[-r scriptfile] [-u useragent] [-z zoomlevel] [uri]\n");
 }
 
-void
-die(const char *errstr, ...)
-{
-       va_list ap;
-
-       va_start(ap, errstr);
-       vfprintf(stderr, errstr, ap);
-       va_end(ap);
-       exit(1);
-}
-
 void
 setup(void)
 {
+       GIOChannel *gchanin;
        GdkDisplay *gdpy;
        int i, j;
 
@@ -342,6 +343,16 @@ setup(void)
 
        gdkkb = gdk_seat_get_keyboard(gdk_display_get_default_seat(gdpy));
 
+       if (pipe(pipeout) < 0 || pipe(pipein) < 0) {
+               fputs("Unable to create pipes\n", stderr);
+       } else {
+               gchanin = g_io_channel_unix_new(pipein[0]);
+               g_io_channel_set_encoding(gchanin, NULL, NULL);
+               g_io_channel_set_close_on_unref(gchanin, TRUE);
+               g_io_add_watch(gchanin, G_IO_IN, readpipe, NULL);
+       }
+
+
        for (i = 0; i < LENGTH(certs); ++i) {
                if (!regcomp(&(certs[i].re), certs[i].regex, REG_EXTENDED)) {
                        certs[i].file = g_strconcat(certdir, "/", certs[i].file,
@@ -468,26 +479,12 @@ getcurrentuserhomedir(void)
 char *
 buildpath(const char *path)
 {
-       char *apath, *name, *p, *fpath;
-       const char *homedir;
+       char *apath, *fpath;
 
-       if (path[0] == '~') {
-               if (path[1] == '/' || path[1] == '\0') {
-                       p = (char *)&path[1];
-                       homedir = getcurrentuserhomedir();
-               } else {
-                       if ((p = strchr(path, '/')))
-                               name = g_strndup(&path[1], --p - path);
-                       else
-                               name = g_strdup(&path[1]);
-
-                       homedir = getuserhomedir(name);
-                       g_free(name);
-               }
-               apath = g_build_filename(homedir, p, NULL);
-       } else {
+       if (path[0] == '~')
+               apath = untildepath(path);
+       else
                apath = g_strdup(path);
-       }
 
        /* creating directory */
        if (g_mkdir_with_parents(apath, 0700) < 0)
@@ -499,6 +496,28 @@ buildpath(const char *path)
        return fpath;
 }
 
+char *
+untildepath(const char *path)
+{
+       char *apath, *name, *p;
+       const char *homedir;
+
+       if (path[1] == '/' || path[1] == '\0') {
+               p = (char *)&path[1];
+               homedir = getcurrentuserhomedir();
+       } else {
+               if ((p = strchr(path, '/')))
+                       name = g_strndup(&path[1], p - (path + 1));
+               else
+                       name = g_strdup(&path[1]);
+
+               homedir = getuserhomedir(name);
+               g_free(name);
+       }
+       apath = g_build_filename(homedir, p, NULL);
+       return apath;
+}
+
 Client *
 newclient(Client *rc)
 {
@@ -520,7 +539,7 @@ void
 loaduri(Client *c, const Arg *a)
 {
        struct stat st;
-       char *url, *path;
+       char *url, *path, *apath;
        const char *uri = a->v;
 
        if (g_strcmp0(uri, "") == 0)
@@ -531,11 +550,19 @@ loaduri(Client *c, const Arg *a)
            g_str_has_prefix(uri, "file://")  ||
            g_str_has_prefix(uri, "about:")) {
                url = g_strdup(uri);
-       } else if (!stat(uri, &st) && (path = realpath(uri, NULL))) {
-               url = g_strdup_printf("file://%s", path);
-               free(path);
        } else {
-               url = g_strdup_printf("http://%s", uri);
+               if (uri[0] == '~')
+                       apath = untildepath(uri);
+               else
+                       apath = (char *)uri;
+               if (!stat(apath, &st) && (path = realpath(apath, NULL))) {
+                       url = g_strdup_printf("file://%s", path);
+                       free(path);
+               } else {
+                       url = g_strdup_printf("http://%s", uri);
+               }
+               if (apath != uri)
+                       free(apath);
        }
 
        setatom(c, AtomUri, url);
@@ -673,41 +700,31 @@ cookiepolicy_set(const WebKitCookieAcceptPolicy p)
 void
 seturiparameters(Client *c, const char *uri, ParamName *params)
 {
-       Parameter *config, *newconfig = NULL;
+       Parameter *config, *uriconfig = NULL;
        int i, p;
 
        for (i = 0; i < LENGTH(uriparams); ++i) {
                if (uriparams[i].uri &&
                    !regexec(&(uriparams[i].re), uri, 0, NULL, 0)) {
-                       newconfig = uriparams[i].config;
+                       uriconfig = uriparams[i].config;
                        break;
                }
        }
 
-       if (!newconfig)
-               newconfig = defconfig;
+       curconfig = uriconfig ? uriconfig : defconfig;
 
        for (i = 0; (p = params[i]) != ParameterLast; ++i) {
                switch(p) {
+               default: /* FALLTHROUGH */
+                       if (!(defconfig[p].prio < curconfig[p].prio ||
+                           defconfig[p].prio < modparams[p]))
+                               continue;
                case Certificate:
                case CookiePolicies:
                case Style:
-                       config = (newconfig[p].prio > defconfig[p].prio) ?
-                                newconfig : defconfig;
-                       break;
-               default:
-                       if (newconfig[p].prio > defconfig[p].prio)
-                               config = newconfig;
-                       else if (curconfig[p].prio > defconfig[p].prio)
-                               config = defconfig;
-                       else
-                               continue;
+                       setparameter(c, 0, p, &curconfig[p].val);
                }
-
-               setparameter(c, 0, p, &config[p].val);
        }
-
-       curconfig = newconfig;
 }
 
 void
@@ -716,6 +733,8 @@ setparameter(Client *c, int refresh, ParamName p, const Arg *a)
        GdkRGBA bgcolor = { 0 };
        WebKitSettings *s = webkit_web_view_get_settings(c->view);
 
+       modparams[p] = curconfig[p].prio;
+
        switch (p) {
        case AcceleratedCanvas:
                webkit_settings_set_enable_accelerated_2d_canvas(s, a->i);
@@ -827,6 +846,9 @@ setparameter(Client *c, int refresh, ParamName p, const Arg *a)
                        setstyle(c, getstyle(geturi(c)));
                refresh = 0;
                break;
+       case WebGL:
+               webkit_settings_set_enable_webgl(s, a->i);
+               break;
        case ZoomLevel:
                webkit_web_view_set_zoom_level(c->view, a->f);
                return; /* do not update */
@@ -924,7 +946,7 @@ runscript(Client *c)
        gsize l;
 
        if (g_file_get_contents(scriptfile, &script, &l, NULL) && l)
-               evalscript(c, script);
+               evalscript(c, "%s", script);
        g_free(script);
 }
 
@@ -1016,6 +1038,8 @@ spawn(Client *c, const Arg *a)
        if (fork() == 0) {
                if (dpy)
                        close(ConnectionNumber(dpy));
+               close(pipein[0]);
+               close(pipeout[1]);
                setsid();
                execvp(((char **)a->v)[0], (char **)a->v);
                fprintf(stderr, "%s: execvp %s", argv0, ((char **)a->v)[0]);
@@ -1048,6 +1072,9 @@ cleanup(void)
 {
        while (clients)
                destroyclient(clients);
+
+       close(pipein[0]);
+       close(pipeout[1]);
        g_free(cookiefile);
        g_free(scriptfile);
        g_free(stylefile);
@@ -1060,13 +1087,13 @@ newview(Client *c, WebKitWebView *rv)
 {
        WebKitWebView *v;
        WebKitSettings *settings;
-       WebKitUserContentManager *contentmanager;
        WebKitWebContext *context;
+       WebKitCookieManager *cookiemanager;
+       WebKitUserContentManager *contentmanager;
 
        /* Webview */
        if (rv) {
-               v = WEBKIT_WEB_VIEW(
-                   webkit_web_view_new_with_related_view(rv));
+               v = WEBKIT_WEB_VIEW(webkit_web_view_new_with_related_view(rv));
        } else {
                settings = webkit_settings_new_with_settings(
                   "allow-file-access-from-file-urls", curconfig[FileURLsCrossAccess].val.i,
@@ -1086,6 +1113,7 @@ newview(Client *c, WebKitWebView *rv)
                   "enable-accelerated-2d-canvas", curconfig[AcceleratedCanvas].val.i,
                   "enable-site-specific-quirks", curconfig[SiteQuirks].val.i,
                   "enable-smooth-scrolling", curconfig[SmoothScrolling].val.i,
+                  "enable-webgl", curconfig[WebGL].val.i,
                   "media-playback-requires-user-gesture", curconfig[MediaManualPlay].val.i,
                   NULL);
 /* For more interesting settings, have a look at
@@ -1107,6 +1135,8 @@ newview(Client *c, WebKitWebView *rv)
                          "base-data-directory", cachedir,
                          NULL));
 
+               cookiemanager = webkit_web_context_get_cookie_manager(context);
+
                /* rendering process model, can be a shared unique one
                 * or one for each view */
                webkit_web_context_set_process_model(context,
@@ -1121,12 +1151,10 @@ newview(Client *c, WebKitWebView *rv)
                    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);
+               webkit_cookie_manager_set_persistent_storage(cookiemanager,
+                   cookiefile, WEBKIT_COOKIE_PERSISTENT_STORAGE_TEXT);
                /* cookie policy */
-               webkit_cookie_manager_set_accept_policy(
-                   webkit_web_context_get_cookie_manager(context),
+               webkit_cookie_manager_set_accept_policy(cookiemanager,
                    cookiepolicy_get());
                /* languages */
                webkit_web_context_set_preferred_languages(context,
@@ -1172,13 +1200,51 @@ newview(Client *c, WebKitWebView *rv)
                         G_CALLBACK(permissionrequested), c);
        g_signal_connect(G_OBJECT(v), "ready-to-show",
                         G_CALLBACK(showview), c);
+       g_signal_connect(G_OBJECT(v), "web-process-terminated",
+                        G_CALLBACK(webprocessterminated), c);
 
        return v;
 }
 
+static gboolean
+readpipe(GIOChannel *s, GIOCondition ioc, gpointer unused)
+{
+       static char msg[MSGBUFSZ], msgsz;
+       GError *gerr = NULL;
+
+       if (g_io_channel_read_chars(s, msg, sizeof(msg), NULL, &gerr) !=
+           G_IO_STATUS_NORMAL) {
+               fprintf(stderr, "surf: error reading pipe: %s\n",
+                       gerr->message);
+               g_error_free(gerr);
+               return TRUE;
+       }
+       if ((msgsz = msg[0]) < 3) {
+               fprintf(stderr, "surf: message too short: %d\n", msgsz);
+               return TRUE;
+       }
+
+       switch (msg[2]) {
+       case 'i':
+               close(pipein[1]);
+               close(pipeout[0]);
+               break;
+       }
+
+       return TRUE;
+}
+
 void
 initwebextensions(WebKitWebContext *wc, Client *c)
 {
+       GVariant *gv;
+
+       if (!pipeout[0] || !pipein[1])
+               return;
+
+       gv = g_variant_new("(ii)", pipeout[0], pipein[1]);
+
+       webkit_web_context_set_web_extensions_initialization_user_data(wc, gv);
        webkit_web_context_set_web_extensions_directory(wc, WEBEXTDIR);
 }
 
@@ -1305,6 +1371,7 @@ showview(WebKitWebView *v, Client *c)
        c->finder = webkit_web_view_get_find_controller(c->view);
        c->inspector = webkit_web_view_get_inspector(c->view);
 
+       c->pageid = webkit_web_view_get_page_id(c->view);
        c->win = createwindow(c);
 
        gtk_container_add(GTK_CONTAINER(c->win), GTK_WIDGET(c->view));
@@ -1317,6 +1384,7 @@ showview(WebKitWebView *v, Client *c)
        if (showxid) {
                gdk_display_sync(gtk_widget_get_display(c->win));
                puts(winid);
+               fflush(stdout);
        }
 
        if (curconfig[HideBackground].val.i)
@@ -1353,8 +1421,7 @@ createwindow(Client *c)
                gtk_window_set_wmclass(GTK_WINDOW(w), wmstr, "Surf");
                g_free(wmstr);
 
-               wmstr = g_strdup_printf("%s[%lu]", "Surf",
-                       webkit_web_view_get_page_id(c->view));
+               wmstr = g_strdup_printf("%s[%lu]", "Surf", c->pageid);
                gtk_window_set_role(GTK_WINDOW(w), wmstr);
                g_free(wmstr);
 
@@ -1675,6 +1742,15 @@ download(Client *c, WebKitURIResponse *r)
        spawn(c, &a);
 }
 
+void
+webprocessterminated(WebKitWebView *v, WebKitWebProcessTerminationReason r,
+                     Client *c)
+{
+       fprintf(stderr, "web process terminated: %s\n",
+               r == WEBKIT_WEB_PROCESS_CRASHED ? "crashed" : "no memory");
+       closeview(v, c);
+}
+
 void
 closeview(WebKitWebView *v, Client *c)
 {
@@ -1766,38 +1842,33 @@ zoom(Client *c, const Arg *a)
        curconfig[ZoomLevel].val.f = webkit_web_view_get_zoom_level(c->view);
 }
 
-void
-scroll(Client *c, const Arg *a)
+static void
+msgext(Client *c, char type, const Arg *a)
 {
-       GdkEvent *ev = gdk_event_new(GDK_KEY_PRESS);
+       static char msg[MSGBUFSZ];
+       int ret;
 
-       gdk_event_set_device(ev, gdkkb);
-       ev->key.window = gtk_widget_get_window(GTK_WIDGET(c->win));
-       ev->key.state = GDK_CONTROL_MASK;
-       ev->key.time = GDK_CURRENT_TIME;
-
-       switch (a->i) {
-       case 'd':
-               ev->key.keyval = GDK_KEY_Down;
-               break;
-       case 'D':
-               ev->key.keyval = GDK_KEY_Page_Down;
-               break;
-       case 'l':
-               ev->key.keyval = GDK_KEY_Left;
-               break;
-       case 'r':
-               ev->key.keyval = GDK_KEY_Right;
-               break;
-       case 'U':
-               ev->key.keyval = GDK_KEY_Page_Up;
-               break;
-       case 'u':
-               ev->key.keyval = GDK_KEY_Up;
-               break;
+       if ((ret = snprintf(msg, sizeof(msg), "%c%c%c%c",
+                           4, c->pageid, type, a->i))
+           >= sizeof(msg)) {
+               fprintf(stderr, "surf: message too long: %d\n", ret);
+               return;
        }
 
-       gdk_event_put(ev);
+       if (pipeout[1] && write(pipeout[1], msg, sizeof(msg)) < 0)
+               fprintf(stderr, "surf: error sending: %.*s\n", ret-2, msg+2);
+}
+
+void
+scrollv(Client *c, const Arg *a)
+{
+       msgext(c, 'v', a);
+}
+
+void
+scrollh(Client *c, const Arg *a)
+{
+       msgext(c, 'h', a);
 }
 
 void