]> git.sur5r.net Git - i3/i3/blobdiff - libi3/ipc_connect.c
Update badges in README
[i3/i3] / libi3 / ipc_connect.c
index d27e04678e0e50c511addf5b2c8639181227ed04..f659a1a4720f566fe0083c5076e53d89a5969ba7 100644 (file)
@@ -2,9 +2,11 @@
  * vim:ts=4:sw=4:expandtab
  *
  * i3 - an improved dynamic tiling window manager
- * © 2009-2013 Michael Stapelberg and contributors (see also: LICENSE)
+ * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
  *
  */
+#include "libi3.h"
+
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <sys/un.h>
 #include <unistd.h>
 #include <fcntl.h>
 
-#include "libi3.h"
-
 /*
  * Connects to the i3 IPC socket and returns the file descriptor for the
  * socket. die()s if anything goes wrong.
  *
  */
 int ipc_connect(const char *socket_path) {
+    char *path = NULL;
+    if (socket_path != NULL) {
+        path = sstrdup(socket_path);
+    }
+
+    if (path == NULL) {
+        if ((path = getenv("I3SOCK")) != NULL) {
+            path = sstrdup(path);
+        }
+    }
+
+    if (path == NULL) {
+        path = root_atom_contents("I3_SOCKET_PATH", NULL, 0);
+    }
+
+    if (path == NULL) {
+        path = sstrdup("/tmp/i3-ipc.sock");
+    }
+
     int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0);
     if (sockfd == -1)
         err(EXIT_FAILURE, "Could not create socket");
@@ -31,9 +50,9 @@ int ipc_connect(const char *socket_path) {
     struct sockaddr_un addr;
     memset(&addr, 0, sizeof(struct sockaddr_un));
     addr.sun_family = AF_LOCAL;
-    strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1);
-    if (connect(sockfd, (const struct sockaddr*)&addr, sizeof(struct sockaddr_un)) < 0)
-        err(EXIT_FAILURE, "Could not connect to i3");
-
+    strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);
+    if (connect(sockfd, (const struct sockaddr *)&addr, sizeof(struct sockaddr_un)) < 0)
+        err(EXIT_FAILURE, "Could not connect to i3 on socket %s", path);
+    free(path);
     return sockfd;
 }