Introduce new data structure for handling settings
[surf.git] / surf.c
diff --git a/surf.c b/surf.c
index 0dd67b6..43b1531 100644 (file)
--- a/surf.c
+++ b/surf.c
 
 #define LENGTH(x)               (sizeof(x) / sizeof(x[0]))
 #define CLEANMASK(mask)         (mask & (MODKEY|GDK_SHIFT_MASK))
+#define SETB(p, s)              [p] = (Parameter){ { .b = s }, }
+#define SETI(p, s)              [p] = (Parameter){ { .i = s }, }
+#define SETV(p, s)              [p] = (Parameter){ { .v = s }, }
+#define SETF(p, s)              [p] = (Parameter){ { .f = s }, }
+#define FSETB(p, s)             [p] = (Parameter){ { .b = s }, 1 }
+#define FSETI(p, s)             [p] = (Parameter){ { .i = s }, 1 }
+#define FSETV(p, s)             [p] = (Parameter){ { .v = s }, 1 }
+#define FSETF(p, s)             [p] = (Parameter){ { .f = s }, 1 }
 
 enum { AtomFind, AtomGo, AtomUri, AtomLast };
 
-enum {
+typedef enum {
        CaretBrowsing,
+       CookiePolicies,
+       DiskCache,
+       DNSPrefetch,
+       FontSize,
        FrameFlattening,
        Geolocation,
+       HideBackground,
+       Inspector,
        JavaScript,
+       KioskMode,
        LoadImages,
        Plugins,
+       PreferredLanguages,
+       RunInFullscreen,
        ScrollBars,
-};
+       ShowIndicators,
+       SpellChecking,
+       SpellLanguages,
+       StrictSSL,
+       Style,
+       ZoomLevel,
+       ParameterLast,
+} ParamName;
 
 enum {
        OnDoc   = WEBKIT_HIT_TEST_RESULT_CONTEXT_DOCUMENT,
@@ -58,9 +82,15 @@ enum {
 typedef union {
        int b;
        int i;
+       float f;
        const void *v;
 } Arg;
 
+typedef struct {
+       Arg val;
+       int force;
+} Parameter;
+
 typedef struct Client {
        GtkWidget *win;
        WebKitWebView *view;
@@ -91,6 +121,12 @@ typedef struct {
        unsigned int stopevent;
 } Button;
 
+typedef struct {
+       const char *uri;
+       Parameter config[ParameterLast];
+       regex_t re;
+} UriParameters;
+
 typedef struct {
        char *regex;
        char *style;
@@ -126,6 +162,7 @@ static void destroyclient(Client *c);
 static void cleanup(void);
 
 /* GTK/WebKit */
+static GdkDevice *getkbdevice(void);
 static WebKitWebView *newview(Client *c, WebKitWebView *rv);
 static GtkWidget *createview(WebKitWebView *v, WebKitNavigationAction *a,
                              Client *c);
@@ -160,8 +197,7 @@ static void reload(Client *c, const Arg *a);
 static void print(Client *c, const Arg *a);
 static void clipboard(Client *c, const Arg *a);
 static void zoom(Client *c, const Arg *a);
-static void scroll_v(Client *c, const Arg *a);
-static void scroll_h(Client *c, const Arg *a);
+static void scroll(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);
@@ -185,6 +221,7 @@ static int showxid;
 static int cookiepolicy;
 static Display *dpy;
 static Client *clients;
+static GdkDevice *gdkkb;
 static char *stylefile;
 static const char *useragent;
 char *argv0;
@@ -232,6 +269,8 @@ setup(void)
        scriptfile = buildfile(scriptfile);
        cachedir   = buildpath(cachedir);
 
+       gdkkb = getkbdevice();
+
        if (!stylefile) {
                styledir = buildpath(styledir);
                for (i = 0; i < LENGTH(styles); ++i) {
@@ -365,7 +404,6 @@ loaduri(Client *c, const Arg *a)
                reload(c, a);
        } else {
                webkit_web_view_load_uri(c->view, url);
-               c->title = geturi(c);
                updatetitle(c);
        }
 
@@ -661,6 +699,22 @@ cleanup(void)
        g_free(cachedir);
 }
 
+static GdkDevice *
+getkbdevice(void)
+{
+       GList *l, *gdl = gdk_device_manager_list_devices(
+                  gdk_display_get_device_manager(gdk_display_get_default()),
+                  GDK_DEVICE_TYPE_MASTER);
+       GdkDevice *gd = NULL;
+
+       for (l = gdl; l != NULL; l = l->next)
+               if (gdk_device_get_source(l->data) == GDK_SOURCE_KEYBOARD)
+                       gd = l->data;
+
+       g_list_free(gdl);
+       return gd;
+}
+
 WebKitWebView *
 newview(Client *c, WebKitWebView *rv)
 {
@@ -964,20 +1018,25 @@ createwindow(Client *c)
 void
 loadchanged(WebKitWebView *v, WebKitLoadEvent e, Client *c)
 {
+       const char *title = geturi(c);
+
        switch (e) {
        case WEBKIT_LOAD_STARTED:
+               setatom(c, AtomUri, title);
+               c->title = title;
                c->tlsflags = G_TLS_CERTIFICATE_VALIDATE_ALL + 1;
                break;
        case WEBKIT_LOAD_REDIRECTED:
-               setatom(c, AtomUri, geturi(c));
+               setatom(c, AtomUri, title);
+               c->title = title;
                break;
        case WEBKIT_LOAD_COMMITTED:
+               setatom(c, AtomUri, title);
+               c->title = title;
                if (!webkit_web_view_get_tls_info(c->view, NULL,
                    &(c->tlsflags)))
                        c->tlsflags = G_TLS_CERTIFICATE_VALIDATE_ALL + 1;
 
-               setatom(c, AtomUri, geturi(c));
-
                if (enablestyle)
                        setstyle(c, getstyle(geturi(c)));
                break;
@@ -1253,17 +1312,38 @@ zoom(Client *c, const Arg *a)
 }
 
 void
-scroll_v(Client *c, const Arg *a)
+scroll(Client *c, const Arg *a)
 {
-       evalscript(c, "window.scrollBy(0, %d * (window.innerHeight / 100))",
-                  a->i);
-}
+       GdkEvent *ev = gdk_event_new(GDK_KEY_PRESS);
 
-void
-scroll_h(Client *c, const Arg *a)
-{
-       evalscript(c, "window.scrollBy(%d * (window.innerWidth / 100), 0)",
-                  a->i);
+       gdk_event_set_device(ev, gdkkb);
+//     gdk_event_set_screen(ev, gdk_screen_get_default());
+       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;
+       }
+
+       gdk_event_put(ev);
 }
 
 void
@@ -1425,10 +1505,8 @@ clickexternplayer(Client *c, const Arg *a, WebKitHitTestResult *h)
 {
        Arg arg;
 
-       if (webkit_hit_test_result_get_context(h) & OnMedia) {
-               arg = (Arg)VIDEOPLAY(webkit_hit_test_result_get_media_uri(h));
-               spawn(c, &arg);
-       }
+       arg = (Arg)VIDEOPLAY(webkit_hit_test_result_get_media_uri(h));
+       spawn(c, &arg);
 }
 
 int