From 084c9b642e5258bd242d2275853a117381b316cc Mon Sep 17 00:00:00 2001 From: Felix Kratz Date: Thu, 13 Apr 2023 22:23:37 +0200 Subject: [PATCH] add possibility for comma separated integer list in --bar display=... (#354) --- src/bar_manager.c | 29 ++++++++++++++++++++--------- src/bar_manager.h | 7 +++++-- src/message.c | 23 +++++++++++++++++++---- src/misc/defines.h | 6 +++--- 4 files changed, 47 insertions(+), 18 deletions(-) diff --git a/src/bar_manager.c b/src/bar_manager.c index c1fe5ff..bb23891 100644 --- a/src/bar_manager.c +++ b/src/bar_manager.c @@ -24,7 +24,7 @@ void bar_manager_init(struct bar_manager* bar_manager) { bar_manager->bars = NULL; bar_manager->bar_count = 0; bar_manager->bar_item_count = 0; - bar_manager->display = DISPLAY_ALL; + bar_manager->displays = DISPLAY_ALL_PATTERN; bar_manager->position = POSITION_TOP; bar_manager->shadow = false; bar_manager->blur_radius = 0; @@ -206,9 +206,9 @@ bool bar_manager_set_position(struct bar_manager* bar_manager, char pos) { return true; } -bool bar_manager_set_display(struct bar_manager* bar_manager, char display) { - if (bar_manager->display == display) return false; - bar_manager->display = display; +bool bar_manager_set_displays(struct bar_manager* bar_manager, uint32_t displays) { + if (bar_manager->displays == displays) return false; + bar_manager->displays = displays; bar_manager_reset(bar_manager); return true; @@ -548,7 +548,7 @@ void bar_manager_reset(struct bar_manager* bar_manager) { } void bar_manager_begin(struct bar_manager* bar_manager) { - if (bar_manager->display == DISPLAY_MAIN) { + if (bar_manager->displays == DISPLAY_MAIN_PATTERN) { uint32_t did = display_main_display_id(); bar_manager->bar_count = 1; bar_manager->bars = (struct bar **) realloc( @@ -560,16 +560,27 @@ void bar_manager_begin(struct bar_manager* bar_manager) { bar_manager->bars[0]->adid = display_arrangement(did); } else { - bar_manager->bar_count = display_active_display_count(); + uint32_t display_count = display_active_display_count(); + uint32_t bar_count = 0; + for (uint32_t index = 1; index <= display_count; index++) { + if (!(bar_manager->displays & 1 << (index - 1))) continue; + bar_count++; + } + + bar_manager->bar_count = bar_count; bar_manager->bars = (struct bar **) realloc( bar_manager->bars, sizeof(struct bar *) * bar_manager->bar_count); memset(bar_manager->bars, 0, sizeof(struct bar*) * bar_manager->bar_count); - for (uint32_t index = 1; index <= bar_manager->bar_count; index++) { + + uint32_t bar_index = 0; + for (uint32_t index = 1; index <= display_count; index++) { + if (!(bar_manager->displays & 1 << (index - 1))) continue; uint32_t did = display_arrangement_display_id(index); - bar_manager->bars[index - 1] = bar_create(did); - bar_manager->bars[index - 1]->adid = index; + bar_manager->bars[bar_index] = bar_create(did); + bar_manager->bars[bar_index]->adid = index; + bar_index++; } } diff --git a/src/bar_manager.h b/src/bar_manager.h index efd4e71..6dff22f 100644 --- a/src/bar_manager.h +++ b/src/bar_manager.h @@ -6,6 +6,9 @@ #define CLOCK_CALLBACK(name) void name(CFRunLoopTimerRef timer, void *context) typedef CLOCK_CALLBACK(clock_callback); +#define DISPLAY_MAIN_PATTERN 0 +#define DISPLAY_ALL_PATTERN UINT32_MAX + struct bar_manager { CFRunLoopTimerRef clock; @@ -21,7 +24,7 @@ struct bar_manager { bool bar_needs_update; bool bar_needs_resize; - char display; + uint32_t displays; char position; int margin; @@ -64,7 +67,7 @@ bool bar_manager_set_background_blur(struct bar_manager* bar_manager, uint32_t r bool bar_manager_set_position(struct bar_manager* bar_manager, char pos); bool bar_manager_set_spaces(struct bar_manager* bar_manager, bool value); bool bar_manager_set_spaces_for_all_displays(struct bar_manager* bar_manager, bool value); -bool bar_manager_set_display(struct bar_manager* bar_manager, char display); +bool bar_manager_set_displays(struct bar_manager* bar_manager, uint32_t displays); bool bar_manager_set_hidden(struct bar_manager* bar_manager, uint32_t sid, bool hidden); bool bar_manager_set_topmost(struct bar_manager* bar_manager, bool topmost); bool bar_manager_set_sticky(struct bar_manager *bar_manager, bool sticky); diff --git a/src/message.c b/src/message.c index c214d9f..aee2562 100644 --- a/src/message.c +++ b/src/message.c @@ -382,10 +382,25 @@ static bool handle_domain_bar(FILE *rsp, struct token domain, char *message) { evaluate_boolean_state(token, g_bar_manager.sticky)); } else if (token_equals(command, PROPERTY_DISPLAY)) { - struct token position = get_token(&message); - if (position.length > 0) - needs_refresh = bar_manager_set_display(&g_bar_manager, - position.text[0]); + struct token display = get_token(&message); + + uint32_t display_pattern = 0; + uint32_t count; + char** list = token_split(display, ',', &count); + if (list && count > 0) { + for (int i = 0; i < count; i++) { + if (strcmp(list[i], ARGUMENT_DISPLAY_ALL) == 0) { + display_pattern = DISPLAY_ALL_PATTERN; + } else if (strcmp(list[i], ARGUMENT_DISPLAY_MAIN) == 0) { + display_pattern = DISPLAY_MAIN_PATTERN; + } + else { + display_pattern |= 1 << (strtoul(list[i], NULL, 0) - 1); + } + } + free(list); + } + needs_refresh = bar_manager_set_displays(&g_bar_manager, display_pattern); } else if (token_equals(command, PROPERTY_POSITION)) { struct token position = get_token(&message); if (position.length > 0) diff --git a/src/misc/defines.h b/src/misc/defines.h index 3b1ee13..872a508 100644 --- a/src/misc/defines.h +++ b/src/misc/defines.h @@ -140,6 +140,9 @@ #define ARGUMENT_COMMON_VAL_BEFORE "before" #define ARGUMENT_COMMON_VAL_AFTER "after" +#define ARGUMENT_DISPLAY_MAIN "main" +#define ARGUMENT_DISPLAY_ALL "all" + #define ARGUMENT_UPDATES_WHEN_SHOWN "when_shown" #define ARGUMENT_DYNAMIC "dynamic" @@ -152,9 +155,6 @@ #define POSITION_CENTER_LEFT 'q' #define POSITION_CENTER_RIGHT 'e' -#define DISPLAY_MAIN 'm' -#define DISPLAY_ALL 'a' - #define TYPE_GRAPH "graph" #define TYPE_SPACE "space" #define TYPE_ALIAS "alias"