X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=libi3%2Fipc_connect.c;h=f659a1a4720f566fe0083c5076e53d89a5969ba7;hb=a080551f2610ec207c1ea4cb0fd6d0aa085812ce;hp=f493b4f11e6a6d118951afc058b9632c6621cd5b;hpb=d9ca3e4274e117f6270d8f3b5b99a97073749b42;p=i3%2Fi3 diff --git a/libi3/ipc_connect.c b/libi3/ipc_connect.c index f493b4f1..f659a1a4 100644 --- a/libi3/ipc_connect.c +++ b/libi3/ipc_connect.c @@ -5,6 +5,8 @@ * © 2009 Michael Stapelberg and contributors (see also: LICENSE) * */ +#include "libi3.h" + #include #include #include @@ -14,14 +16,31 @@ #include #include -#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); + 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"); - + err(EXIT_FAILURE, "Could not connect to i3 on socket %s", path); + free(path); return sockfd; }