diff --git a/i3-config-wizard/main.c b/i3-config-wizard/main.c index ffc3df93..880b80ed 100644 --- a/i3-config-wizard/main.c +++ b/i3-config-wizard/main.c @@ -791,17 +791,17 @@ int main(int argc, char *argv[]) { close(fd); unlink(config_path); + int screen; + if ((conn = xcb_connect(NULL, &screen)) == NULL || + xcb_connection_has_error(conn)) + errx(1, "Cannot open display\n"); + if (socket_path == NULL) - socket_path = root_atom_contents("I3_SOCKET_PATH"); + socket_path = root_atom_contents("I3_SOCKET_PATH", conn, screen); if (socket_path == NULL) socket_path = "/tmp/i3-ipc.sock"; - int screens; - if ((conn = xcb_connect(NULL, &screens)) == NULL || - xcb_connection_has_error(conn)) - errx(1, "Cannot open display\n"); - keysyms = xcb_key_symbols_alloc(conn); xcb_get_modifier_mapping_cookie_t modmap_cookie; modmap_cookie = xcb_get_modifier_mapping(conn); @@ -813,7 +813,7 @@ int main(int argc, char *argv[]) { #include "atoms.xmacro" #undef xmacro - root_screen = xcb_aux_get_screen(conn, screens); + root_screen = xcb_aux_get_screen(conn, screen); root = root_screen->root; if (!(modmap_reply = xcb_get_modifier_mapping_reply(conn, modmap_cookie, NULL))) diff --git a/i3-dump-log/main.c b/i3-dump-log/main.c index 852a5cf7..acb7fcb1 100644 --- a/i3-dump-log/main.c +++ b/i3-dump-log/main.c @@ -90,7 +90,7 @@ int main(int argc, char *argv[]) { } } - char *shmname = root_atom_contents("I3_SHMLOG_PATH"); + char *shmname = root_atom_contents("I3_SHMLOG_PATH", NULL, 0); if (shmname == NULL) { /* Something failed. Let’s invest a little effort to find out what it * is. This is hugely helpful for users who want to debug i3 but are @@ -109,7 +109,7 @@ int main(int argc, char *argv[]) { fprintf(stderr, "FYI: The DISPLAY environment variable is set to \"%s\".\n", getenv("DISPLAY")); exit(1); } - if (root_atom_contents("I3_CONFIG_PATH") != NULL) { + if (root_atom_contents("I3_CONFIG_PATH", conn, screen) != NULL) { fprintf(stderr, "i3-dump-log: ERROR: i3 is running, but SHM logging is not enabled.\n\n"); if (!is_debug_build()) { fprintf(stderr, "You seem to be using a release version of i3:\n %s\n\n", I3_VERSION); diff --git a/i3-input/main.c b/i3-input/main.c index 49db4df2..da95c903 100644 --- a/i3-input/main.c +++ b/i3-input/main.c @@ -368,23 +368,23 @@ int main(int argc, char *argv[]) { printf("using format \"%s\"\n", format); + int screen; + conn = xcb_connect(NULL, &screen); + if (!conn || xcb_connection_has_error(conn)) + die("Cannot open display\n"); + if (socket_path == NULL) - socket_path = root_atom_contents("I3_SOCKET_PATH"); + socket_path = root_atom_contents("I3_SOCKET_PATH", conn, screen); if (socket_path == NULL) socket_path = "/tmp/i3-ipc.sock"; sockfd = ipc_connect(socket_path); - int screens; - conn = xcb_connect(NULL, &screens); - if (!conn || xcb_connection_has_error(conn)) - die("Cannot open display\n"); - /* Request the current InputFocus to restore when i3-input exits. */ focus_cookie = xcb_get_input_focus(conn); - root_screen = xcb_aux_get_screen(conn, screens); + root_screen = xcb_aux_get_screen(conn, screen); root = root_screen->root; symbols = xcb_key_symbols_alloc(conn); diff --git a/i3-msg/main.c b/i3-msg/main.c index a1428fb8..935edc04 100644 --- a/i3-msg/main.c +++ b/i3-msg/main.c @@ -188,7 +188,7 @@ int main(int argc, char *argv[]) { } if (socket_path == NULL) - socket_path = root_atom_contents("I3_SOCKET_PATH"); + socket_path = root_atom_contents("I3_SOCKET_PATH", NULL, 0); /* Fall back to the default socket path */ if (socket_path == NULL) diff --git a/i3bar/src/xcb.c b/i3bar/src/xcb.c index cec7ab82..0c8de65e 100644 --- a/i3bar/src/xcb.c +++ b/i3bar/src/xcb.c @@ -960,26 +960,7 @@ char *init_xcb_early() { /* Now we get the atoms and save them in a nice data structure */ get_atoms(); - xcb_get_property_cookie_t path_cookie; - path_cookie = xcb_get_property_unchecked(xcb_connection, - 0, - xcb_root, - atoms[I3_SOCKET_PATH], - XCB_GET_PROPERTY_TYPE_ANY, - 0, PATH_MAX); - - /* We check, if i3 set its socket-path */ - xcb_get_property_reply_t *path_reply = xcb_get_property_reply(xcb_connection, - path_cookie, - NULL); - char *path = NULL; - if (path_reply) { - int len = xcb_get_property_value_length(path_reply); - if (len != 0) { - path = strndup(xcb_get_property_value(path_reply), len); - } - } - + char *path = root_atom_contents("I3_SOCKET_PATH", xcb_connection, screen); if (xcb_request_failed(sl_pm_cookie, "Could not allocate statusline-buffer") || xcb_request_failed(clear_ctx_cookie, "Could not allocate statusline-buffer-clearcontext") || diff --git a/include/libi3.h b/include/libi3.h index b0141f1d..9ba78004 100644 --- a/include/libi3.h +++ b/include/libi3.h @@ -84,11 +84,14 @@ void errorlog(char *fmt, ...); * Try to get the contents of the given atom (for example I3_SOCKET_PATH) from * the X11 root window and return NULL if it doesn’t work. * + * If the provided XCB connection is NULL, a new connection will be + * established. + * * The memory for the contents is dynamically allocated and has to be * free()d by the caller. * */ -char *root_atom_contents(const char *atomname); +char *root_atom_contents(const char *atomname, xcb_connection_t *provided_conn, int screen); /** * Safe-wrapper around malloc which exits if malloc returns NULL (meaning that diff --git a/libi3/root_atom_contents.c b/libi3/root_atom_contents.c index cabaaf2c..697441eb 100644 --- a/libi3/root_atom_contents.c +++ b/libi3/root_atom_contents.c @@ -19,19 +19,22 @@ * Try to get the contents of the given atom (for example I3_SOCKET_PATH) from * the X11 root window and return NULL if it doesn’t work. * + * If the provided XCB connection is NULL, a new connection will be + * established. + * * The memory for the contents is dynamically allocated and has to be * free()d by the caller. * */ -char *root_atom_contents(const char *atomname) { - xcb_connection_t *conn; +char *root_atom_contents(const char *atomname, xcb_connection_t *provided_conn, int screen) { xcb_intern_atom_cookie_t atom_cookie; xcb_intern_atom_reply_t *atom_reply; - int screen; char *content; + xcb_connection_t *conn = provided_conn; - if ((conn = xcb_connect(NULL, &screen)) == NULL || - xcb_connection_has_error(conn)) + if (provided_conn == NULL && + ((conn = xcb_connect(NULL, &screen)) == NULL || + xcb_connection_has_error(conn))) return NULL; atom_cookie = xcb_intern_atom(conn, 0, strlen(atomname), atomname); @@ -60,7 +63,8 @@ char *root_atom_contents(const char *atomname) { (char*)xcb_get_property_value(prop_reply)) == -1) return NULL; } - xcb_disconnect(conn); + if (provided_conn == NULL) + xcb_disconnect(conn); return content; } diff --git a/src/display_version.c b/src/display_version.c index 0901ae07..5fa4f8e2 100644 --- a/src/display_version.c +++ b/src/display_version.c @@ -68,11 +68,11 @@ static yajl_callbacks version_callbacks = { * */ void display_running_version(void) { - char *socket_path = root_atom_contents("I3_SOCKET_PATH"); + char *socket_path = root_atom_contents("I3_SOCKET_PATH", conn, conn_screen); if (socket_path == NULL) exit(EXIT_SUCCESS); - char *pid_from_atom = root_atom_contents("I3_PID"); + char *pid_from_atom = root_atom_contents("I3_PID", conn, conn_screen); if (pid_from_atom == NULL) { /* If I3_PID is not set, the running version is older than 4.2-200. */ printf("\nRunning version: < 4.2-200\n"); diff --git a/src/main.c b/src/main.c index aac73883..2b397181 100644 --- a/src/main.c +++ b/src/main.c @@ -352,7 +352,7 @@ int main(int argc, char *argv[]) { break; } else if (strcmp(long_options[option_index].name, "get-socketpath") == 0 || strcmp(long_options[option_index].name, "get_socketpath") == 0) { - char *socket_path = root_atom_contents("I3_SOCKET_PATH"); + char *socket_path = root_atom_contents("I3_SOCKET_PATH", NULL, 0); if (socket_path) { printf("%s\n", socket_path); exit(EXIT_SUCCESS); @@ -442,7 +442,7 @@ int main(int argc, char *argv[]) { optind++; } DLOG("Command is: %s (%zd bytes)\n", payload, strlen(payload)); - char *socket_path = root_atom_contents("I3_SOCKET_PATH"); + char *socket_path = root_atom_contents("I3_SOCKET_PATH", NULL, 0); if (!socket_path) { ELOG("Could not get i3 IPC socket path\n"); return 1;