diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index f7d44ec54..a40f096d2 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -2,6 +2,7 @@ add_library(sway-common ipc-client.c list.c log.c + util.c readline.c stringop.c ) diff --git a/common/util.c b/common/util.c new file mode 100644 index 000000000..ed6d033fe --- /dev/null +++ b/common/util.c @@ -0,0 +1,15 @@ +#include "util.h" + +int wrap(int i, int max) { + return ((i % max) + max) % max; +} + +int numlen(int n) { + if (n >= 1000000) return 7; + if (n >= 100000) return 6; + if (n >= 10000) return 5; + if (n >= 1000) return 4; + if (n >= 100) return 3; + if (n >= 10) return 2; + return 1; +} diff --git a/include/config.h b/include/config.h index 04528e270..0f3ce5506 100644 --- a/include/config.h +++ b/include/config.h @@ -72,6 +72,13 @@ struct bar_config { * In "show" mode, it will always be shown on top of the active workspace. */ char *hidden_state; + /** + * Id name used to identify the bar through IPC. + * + * Defaults to bar-x, where x corresponds to the position of the + * embedding bar block in the config file (bar-0, bar-1, ...). + */ + char *id; uint32_t modifier; enum desktop_shell_panel_position position; char *status_command; diff --git a/include/util.h b/include/util.h index 8e65e6d61..9cb861dd9 100644 --- a/include/util.h +++ b/include/util.h @@ -6,4 +6,9 @@ */ int wrap(int i, int max); +/** + * Count number of digits in int + */ +int numlen(int n); + #endif diff --git a/sway/CMakeLists.txt b/sway/CMakeLists.txt index aa553492d..894163b86 100644 --- a/sway/CMakeLists.txt +++ b/sway/CMakeLists.txt @@ -21,7 +21,6 @@ add_executable(sway main.c output.c resize.c - util.c workspace.c ) diff --git a/sway/commands.c b/sway/commands.c index 604e10aa5..c565adbb7 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -15,6 +15,7 @@ #include "layout.h" #include "focus.h" #include "log.h" +#include "util.h" #include "workspace.h" #include "commands.h" #include "container.h" @@ -1124,9 +1125,20 @@ static struct cmd_results *cmd_bar(int argc, char **argv) { bar->tray_padding = config->bar.tray_padding; list_add(config->bars, bar); + // set bar id + int i; + for (i = 0; i < config->bars->length; ++i) { + if (bar == config->bars->items[i]) { + const int len = 5 + numlen(i); // "bar-" + i + \0 + bar->id = malloc(len * sizeof(char)); + snprintf(bar->id, len, "bar-%d", i); + break; + } + } + // Set current bar config->current_bar = bar; - sway_log(L_DEBUG, "Configuring bar"); + sway_log(L_DEBUG, "Configuring bar %s", bar->id); return cmd_results_new(CMD_BLOCK_BAR, NULL, NULL); } @@ -1534,7 +1546,7 @@ static struct cmd_results *bar_cmd_position(int argc, char **argv) { return error; } - sway_log(L_DEBUG, "Setting bar position '%s'", argv[0]); + sway_log(L_DEBUG, "Setting bar position '%s' for bar: %s", argv[0], config->current_bar->id); return cmd_results_new(CMD_SUCCESS, NULL, NULL); } @@ -1550,10 +1562,10 @@ static struct cmd_results *bar_cmd_strip_workspace_numbers(int argc, char **argv if (strcasecmp("yes", argv[0]) == 0) { config->current_bar->strip_workspace_numbers = true; - sway_log(L_DEBUG, "Stripping workspace numbers on bar"); + sway_log(L_DEBUG, "Stripping workspace numbers on bar: %s", config->current_bar->id); } else if (strcasecmp("no", argv[0]) == 0) { config->current_bar->strip_workspace_numbers = false; - sway_log(L_DEBUG, "Enabling workspace numbers on bar"); + sway_log(L_DEBUG, "Enabling workspace numbers on bar: %s", config->current_bar->id); } else { error = cmd_results_new(CMD_INVALID, "strip_workspace_numbers", "Invalid value %s", argv[0]); return error; @@ -1587,7 +1599,7 @@ static struct cmd_results *bar_cmd_tray_padding(int argc, char **argv) { "Unknown unit %s", argv[1]); } config->current_bar->tray_padding = padding; - sway_log(L_DEBUG, "Enabling tray padding of %d px", padding); + sway_log(L_DEBUG, "Enabling tray padding of %d px on bar: %s", padding, config->current_bar->id); return cmd_results_new(CMD_SUCCESS, NULL, NULL); } @@ -1603,10 +1615,10 @@ static struct cmd_results *bar_cmd_workspace_buttons(int argc, char **argv) { if (strcasecmp("yes", argv[0]) == 0) { config->current_bar->workspace_buttons = true; - sway_log(L_DEBUG, "Enabling workspace buttons on bar"); + sway_log(L_DEBUG, "Enabling workspace buttons on bar: %s", config->current_bar->id); } else if (strcasecmp("no", argv[0]) == 0) { config->current_bar->workspace_buttons = false; - sway_log(L_DEBUG, "Disabling workspace buttons on bar"); + sway_log(L_DEBUG, "Disabling workspace buttons on bar: %s", config->current_bar->id); } else { error = cmd_results_new(CMD_INVALID, "workspace_buttons", "Invalid value %s", argv[0]); return error; diff --git a/sway/util.c b/sway/util.c deleted file mode 100644 index 9a59ddf91..000000000 --- a/sway/util.c +++ /dev/null @@ -1,5 +0,0 @@ -#include "util.h" - -int wrap(int i, int max) { - return ((i % max) + max) % max; -} diff --git a/swaygrab/main.c b/swaygrab/main.c index 681a6da42..2c6cf2dd6 100644 --- a/swaygrab/main.c +++ b/swaygrab/main.c @@ -8,21 +8,12 @@ #include #include "log.h" #include "ipc-client.h" +#include "util.h" void sway_terminate(void) { exit(EXIT_FAILURE); } -int numlen(int n) { - if (n >= 1000000) return 7; - if (n >= 100000) return 6; - if (n >= 10000) return 5; - if (n >= 1000) return 4; - if (n >= 100) return 3; - if (n >= 10) return 2; - return 1; -} - void grab_and_apply_magick(const char *file, const char *output, int socketfd, int raw) { uint32_t len = strlen(output);