diff --git a/RELEASE-NOTES-next b/RELEASE-NOTES-next index 1eec3658f..3863a6ea5 100644 --- a/RELEASE-NOTES-next +++ b/RELEASE-NOTES-next @@ -14,6 +14,7 @@ strongly encouraged to upgrade. • i3-nagbar: position on focused monitor by default • i3-nagbar: add option to position on primary monitor • alternate focusing tab/stack children-parent containers by clicking on their titlebars + • i3bar: use first bar config by default ┌────────────────────────────┐ │ Bugfixes │ diff --git a/i3bar/include/configuration.h b/i3bar/include/configuration.h index f39d24c9b..e39fe99a9 100644 --- a/i3bar/include/configuration.h +++ b/i3bar/include/configuration.h @@ -74,6 +74,13 @@ extern config_t config; */ void parse_config_json(char *json); +/** + * Start parsing the received bar configuration list. The only usecase right + * now is to automatically get the first bar id. + * + */ +void parse_get_first_i3bar_config(char *json); + /** * free()s the color strings as soon as they are not needed anymore. * diff --git a/i3bar/include/ipc.h b/i3bar/include/ipc.h index 5056e5f37..2ce072150 100644 --- a/i3bar/include/ipc.h +++ b/i3bar/include/ipc.h @@ -18,7 +18,7 @@ * socket_path must be a valid path to the ipc_socket of i3 * */ -int init_connection(const char *socket_path); +void init_connection(const char *socket_path); /* * Destroy the connection to i3. diff --git a/i3bar/src/config.c b/i3bar/src/config.c index 5d4bf88a4..10847cd16 100644 --- a/i3bar/src/config.c +++ b/i3bar/src/config.c @@ -367,14 +367,12 @@ static yajl_callbacks outputs_callbacks = { * */ void parse_config_json(char *json) { - yajl_handle handle; - yajl_status state; - handle = yajl_alloc(&outputs_callbacks, NULL, NULL); + yajl_handle handle = yajl_alloc(&outputs_callbacks, NULL, NULL); TAILQ_INIT(&(config.bindings)); TAILQ_INIT(&(config.tray_outputs)); - state = yajl_parse(handle, (const unsigned char *)json, strlen(json)); + yajl_status state = yajl_parse(handle, (const unsigned char *)json, strlen(json)); /* FIXME: Proper error handling for JSON parsing */ switch (state) { @@ -390,6 +388,25 @@ void parse_config_json(char *json) { yajl_free(handle); } +static int i3bar_config_string_cb(void *params_, const unsigned char *val, size_t _len) { + sasprintf(&config.bar_id, "%.*s", (int)_len, val); + return 0; /* Stop parsing */ +} + +/* + * Start parsing the received bar configuration list. The only usecase right + * now is to automatically get the first bar id. + * + */ +void parse_get_first_i3bar_config(char *json) { + yajl_callbacks configs_callbacks = { + .yajl_string = i3bar_config_string_cb, + }; + yajl_handle handle = yajl_alloc(&configs_callbacks, NULL, NULL); + yajl_parse(handle, (const unsigned char *)json, strlen(json)); + yajl_free(handle); +} + /* * free()s the color strings as soon as they are not needed anymore. * diff --git a/i3bar/src/ipc.c b/i3bar/src/ipc.c index f2c105ac3..ec5fc06a7 100644 --- a/i3bar/src/ipc.c +++ b/i3bar/src/ipc.c @@ -85,6 +85,20 @@ static void got_output_reply(char *reply) { * */ static void got_bar_config(char *reply) { + if (!config.bar_id) { + DLOG("Received bar list \"%s\"\n", reply); + parse_get_first_i3bar_config(reply); + + if (!config.bar_id) { + ELOG("No bar configuration found, please configure a bar block in your i3 config file.\n"); + exit(EXIT_FAILURE); + } + + LOG("Using first bar config: %s. Use --bar_id to manually select a different bar configuration.\n", config.bar_id); + i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_BAR_CONFIG, config.bar_id); + return; + } + DLOG("Received bar config \"%s\"\n", reply); /* We initiate the main function by requesting infos about the outputs and * workspaces. Everything else (creating the bars, showing the right workspace- @@ -328,13 +342,12 @@ int i3_send_msg(uint32_t type, const char *payload) { * socket_path must be a valid path to the ipc_socket of i3 * */ -int init_connection(const char *socket_path) { +void init_connection(const char *socket_path) { sock_path = socket_path; int sockfd = ipc_connect(socket_path); i3_connection = smalloc(sizeof(ev_io)); ev_io_init(i3_connection, &got_data, sockfd, EV_READ); ev_io_start(main_loop, i3_connection); - return 1; } /* diff --git a/i3bar/src/main.c b/i3bar/src/main.c index ca9a06d6d..5d7a2b995 100644 --- a/i3bar/src/main.c +++ b/i3bar/src/main.c @@ -56,9 +56,9 @@ static char *expand_path(char *path) { } static void print_usage(char *elf_name) { - printf("Usage: %s -b bar_id [-s sock_path] [-t] [-h] [-v] [-V]\n", elf_name); + printf("Usage: %s [-b bar_id] [-s sock_path] [-t] [-h] [-v] [-V]\n", elf_name); printf("\n"); - printf("-b, --bar_id \tBar ID for which to get the configuration\n"); + printf("-b, --bar_id \tBar ID for which to get the configuration, defaults to the first bar from the i3 config\n"); printf("-s, --socket \tConnect to i3 via \n"); printf("-t, --transparency Enable transparency (RGBA colors)\n"); printf("-h, --help Display this help message and exit\n"); @@ -133,13 +133,6 @@ int main(int argc, char **argv) { } } - if (!config.bar_id) { - /* TODO: maybe we want -f which will automatically ask i3 for the first - * configured bar (and error out if there are too many)? */ - ELOG("No bar_id passed. Please let i3 start i3bar or specify --bar_id\n"); - exit(EXIT_FAILURE); - } - main_loop = ev_default_loop(0); /* needed in init_xcb_early */ char *atom_sock_path = init_xcb_early(); @@ -166,10 +159,13 @@ int main(int argc, char **argv) { init_dpi(); init_outputs(); - if (init_connection(socket_path)) { - /* Request the bar configuration. When it arrives, we fill the config array. */ - i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_BAR_CONFIG, config.bar_id); - } + + init_connection(socket_path); + /* Request the bar configuration. When it arrives, we fill the config + * array. In case that config.bar_id is empty, we will receive a list of + * available configs and then request the configuration for the first bar. + * See got_bar_config for more. */ + i3_send_msg(I3_IPC_MESSAGE_TYPE_GET_BAR_CONFIG, config.bar_id); free(socket_path); /* We listen to SIGTERM/QUIT/INT and try to exit cleanly, by stopping the main loop. diff --git a/man/i3bar.man b/man/i3bar.man index 580e683fe..479e10fcb 100644 --- a/man/i3bar.man +++ b/man/i3bar.man @@ -9,7 +9,7 @@ i3bar - xcb-based status- and workspace-bar == SYNOPSIS -*i3bar* *-b* 'bar_id' [*-s* 'sock_path'] [*-t*] [*-h*] [*-v*] [*-V*] +*i3bar* [*-b* 'bar_id'] [*-s* 'sock_path'] [*-t*] [*-h*] [*-v*] [*-V*] == WARNING @@ -25,7 +25,8 @@ You have been warned! Overwrites the path to the i3 IPC socket. *-b, --bar_id* 'bar_id':: -Specifies the bar ID for which to get the configuration from i3. +Specifies the bar ID for which to get the configuration from i3. By default, +i3bar will use the first bar block as configured in i3. *-t, --transparency*:: Enable transparency (RGBA colors)