Fix GdkDisplay assignment in setup()
[surf.git] / surf.c
diff --git a/surf.c b/surf.c
index 1c194a3..64c5f5f 100644 (file)
--- a/surf.c
+++ b/surf.c
@@ -144,6 +144,8 @@ static void setup(void);
 static void sigchld(int unused);
 static char *buildfile(const char *path);
 static char *buildpath(const char *path);
+static const char *getuserhomedir(const char *user);
+static const char *getcurrentuserhomedir(void);
 static Client *newclient(Client *c);
 static void loaduri(Client *c, const Arg *a);
 static const char *geturi(Client *c);
@@ -168,7 +170,6 @@ 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);
@@ -257,13 +258,15 @@ die(const char *errstr, ...)
 void
 setup(void)
 {
+       GdkDisplay *gdpy;
        int i, j;
 
        /* clean up any zombies immediately */
        sigchld(0);
        gtk_init(NULL, NULL);
 
-       dpy = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
+       gdpy = gdk_display_get_default();
+       dpy = GDK_DISPLAY_XDISPLAY(gdpy);
 
        curconfig = defconfig;
 
@@ -277,7 +280,7 @@ setup(void)
        scriptfile = buildfile(scriptfile);
        cachedir   = buildpath(cachedir);
 
-       gdkkb = getkbdevice();
+       gdkkb = gdk_seat_get_keyboard(gdk_display_get_default_seat(gdpy));
 
        if (!stylefile) {
                styledir = buildpath(styledir);
@@ -350,29 +353,59 @@ buildfile(const char *path)
        return fpath;
 }
 
+static const char*
+getuserhomedir(const char *user)
+{
+       struct passwd *pw = getpwnam(user);
+
+       if (!pw)
+               die("Can't get user %s login information.\n", user);
+
+       return pw->pw_dir;
+}
+
+static const char*
+getcurrentuserhomedir(void)
+{
+       const char *homedir;
+       const char *user;
+       struct passwd *pw;
+
+       homedir = getenv("HOME");
+       if (homedir)
+               return homedir;
+
+       user = getenv("USER");
+       if (user)
+               return getuserhomedir(user);
+
+       pw = getpwuid(getuid());
+       if (!pw)
+               die("Can't get current user home directory\n");
+
+       return pw->pw_dir;
+}
+
 char *
 buildpath(const char *path)
 {
-       struct passwd *pw;
        char *apath, *name, *p, *fpath;
+       const char *homedir;
 
        if (path[0] == '~') {
                if (path[1] == '/' || path[1] == '\0') {
                        p = (char *)&path[1];
-                       pw = getpwuid(getuid());
+                       homedir = getcurrentuserhomedir();
                } else {
                        if ((p = strchr(path, '/')))
                                name = g_strndup(&path[1], --p - path);
                        else
                                name = g_strdup(&path[1]);
 
-                       if (!(pw = getpwnam(name))) {
-                               die("Can't get user %s home directory: %s.\n",
-                                   name, path);
-                       }
+                       homedir = getuserhomedir(name);
                        g_free(name);
                }
-               apath = g_build_filename(pw->pw_dir, p, NULL);
+               apath = g_build_filename(homedir, p, NULL);
        } else {
                apath = g_strdup(path);
        }
@@ -415,7 +448,10 @@ loaduri(Client *c, const Arg *a)
        if (g_strcmp0(uri, "") == 0)
                return;
 
-       if (g_strrstr(uri, "://") || g_str_has_prefix(uri, "about:")) {
+       if (g_str_has_prefix(uri, "http://")  ||
+           g_str_has_prefix(uri, "https://") ||
+           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);
@@ -840,22 +876,6 @@ 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)
 {
@@ -1084,8 +1104,7 @@ showview(WebKitWebView *v, Client *c)
        GdkWindow *gwin;
 
        c->finder = webkit_web_view_get_find_controller(c->view);
-       if (curconfig[Inspector].val.b)
-               c->inspector = webkit_web_view_get_inspector(c->view);
+       c->inspector = webkit_web_view_get_inspector(c->view);
 
        c->win = createwindow(c);
 
@@ -1168,20 +1187,18 @@ loadchanged(WebKitWebView *v, WebKitLoadEvent e, Client *c)
                setatom(c, AtomUri, title);
                c->title = title;
                c->tlsflags = G_TLS_CERTIFICATE_VALIDATE_ALL + 1;
+               seturiparameters(c, geturi(c));
                break;
        case WEBKIT_LOAD_REDIRECTED:
                setatom(c, AtomUri, title);
                c->title = title;
+               seturiparameters(c, geturi(c));
                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));
-               seturiparameters(c, geturi(c));
                break;
        case WEBKIT_LOAD_FINISHED:
                /* Disabled until we write some WebKitWebExtension for
@@ -1462,7 +1479,6 @@ scroll(Client *c, const Arg *a)
        GdkEvent *ev = gdk_event_new(GDK_KEY_PRESS);
 
        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;
@@ -1535,12 +1551,10 @@ togglecookiepolicy(Client *c, const Arg *a)
 void
 toggleinspector(Client *c, const Arg *a)
 {
-       if (curconfig[Inspector].val.b) {
-               if (webkit_web_inspector_is_attached(c->inspector))
-                       webkit_web_inspector_close(c->inspector);
-               else
-                       webkit_web_inspector_show(c->inspector);
-       }
+       if (webkit_web_inspector_is_attached(c->inspector))
+               webkit_web_inspector_close(c->inspector);
+       else if (curconfig[Inspector].val.b)
+               webkit_web_inspector_show(c->inspector);
 }
 
 void