make i3bar use libi3’s root_atom_contents()

This removes code duplication, which will be useful for a subsequent
commit.

Furthermore, we now don’t open X11 connections unnecessarily in some
corner cases.
This commit is contained in:
Michael Stapelberg 2013-11-22 15:48:45 +01:00
parent ca39289e3e
commit d3beff2339
9 changed files with 36 additions and 48 deletions

View File

@ -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)))

View File

@ -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. Lets 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);

View File

@ -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);

View File

@ -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)

View File

@ -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") ||

View File

@ -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 doesnt 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

View File

@ -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 doesnt 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,6 +63,7 @@ char *root_atom_contents(const char *atomname) {
(char*)xcb_get_property_value(prop_reply)) == -1)
return NULL;
}
if (provided_conn == NULL)
xcb_disconnect(conn);
return content;
}

View File

@ -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");

View File

@ -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;