From d2299c8379f31d30ecd110663e8f0f52b2226f87 Mon Sep 17 00:00:00 2001 From: Felix Kratz Date: Mon, 15 Jan 2024 21:34:33 +0100 Subject: [PATCH] lookup adid directly from active display uuid --- src/bar_manager.c | 8 ++++---- src/display.c | 25 +++++++++++++++++++++++-- src/display.h | 5 +++-- src/event.c | 10 +++++----- src/message.c | 2 +- 5 files changed, 36 insertions(+), 14 deletions(-) diff --git a/src/bar_manager.c b/src/bar_manager.c index 2792487..7867152 100644 --- a/src/bar_manager.c +++ b/src/bar_manager.c @@ -37,7 +37,7 @@ void bar_manager_init(struct bar_manager* bar_manager) { bar_manager->topmost = false; bar_manager->notch_width = 200; bar_manager->notch_offset = 0; - bar_manager->active_adid = display_arrangement(display_active_display_id()); + bar_manager->active_adid = display_active_display_adid(); bar_manager->might_need_clipping = false; bar_manager->sticky = false; @@ -603,7 +603,7 @@ void bar_manager_begin(struct bar_manager* bar_manager) { bar_manager->active_displays |= 1 << bar_manager->bars[i]->adid; } - bar_manager->active_adid = display_arrangement(display_active_display_id()); + bar_manager->active_adid = display_active_display_adid(); bar_manager->needs_ordering = true; } @@ -763,7 +763,7 @@ void bar_manager_display_added(struct bar_manager* bar_manager, uint32_t did) { } void bar_manager_display_changed(struct bar_manager* bar_manager) { - bar_manager->active_adid = display_arrangement(display_active_display_id()); + bar_manager->active_adid = display_active_display_adid(); bar_manager_freeze(bar_manager); bar_manager_reset(bar_manager); @@ -978,7 +978,7 @@ void bar_manager_handle_space_change(struct bar_manager* bar_manager, bool force } void bar_manager_handle_display_change(struct bar_manager* bar_manager) { - bar_manager->active_adid = display_arrangement(display_active_display_id()); + bar_manager->active_adid = display_active_display_adid(); struct env_vars env_vars; env_vars_init(&env_vars); char adid_str[3]; diff --git a/src/display.c b/src/display.c index 7d30624..0ea7eee 100644 --- a/src/display.c +++ b/src/display.c @@ -135,8 +135,7 @@ uint32_t display_main_display_id(void) { } static CFStringRef display_active_display_uuid(void) { - CFStringRef menubar = SLSCopyActiveMenuBarDisplayIdentifier(g_connection); - return menubar; + return SLSCopyActiveMenuBarDisplayIdentifier(g_connection); } uint32_t display_active_display_id(void) { @@ -192,6 +191,28 @@ uint32_t display_arrangement_display_id(int arrangement) { return result; } +uint32_t display_active_display_adid(void) { + CFStringRef uuid = display_active_display_uuid(); + CFArrayRef displays = SLSCopyManagedDisplays(g_connection); + if (!displays) { + CFRelease(uuid); + return 0; + } + + int result = 0; + int displays_count = CFArrayGetCount(displays); + + for (int i = 0; i < displays_count; ++i) { + if (CFEqual(CFArrayGetValueAtIndex(displays, i), uuid)) { + result = i + 1; + break; + } + } + CFRelease(displays); + CFRelease(uuid); + return result; +} + bool display_menu_bar_visible(void) { int status = 0; SLSGetMenuBarAutohideEnabled(g_connection, &status); diff --git a/src/display.h b/src/display.h index 7cbdeb4..b0f24c4 100644 --- a/src/display.h +++ b/src/display.h @@ -30,18 +30,19 @@ extern CGError SLSSetMenuBarAutohideEnabled(int cid, bool enabled); uint32_t display_main_display_id(void); uint32_t display_active_display_id(void); +uint32_t display_active_display_adid(void); uint32_t display_arrangement_display_id(int arrangement); bool display_menu_bar_visible(void); CGRect display_menu_bar_rect(uint32_t did); uint32_t display_active_display_count(void); -uint32_t *display_active_display_list(uint32_t *count); +uint32_t* display_active_display_list(uint32_t* count); bool display_begin(void); bool display_end(void); CFStringRef display_uuid(uint32_t did); CGRect display_bounds(uint32_t did); uint64_t display_space_id(uint32_t did); -uint64_t *display_space_list(uint32_t did, int *count); +uint64_t* display_space_list(uint32_t did, int* count); int display_arrangement(uint32_t did); void forced_brightness_event(); diff --git a/src/event.c b/src/event.c index 421fd45..963b1ba 100644 --- a/src/event.c +++ b/src/event.c @@ -72,7 +72,7 @@ static void event_mouse_up(void* context) { CGEventType type = CGEventGetType(context); uint32_t mouse_button_code = CGEventGetIntegerValueField(context, kCGMouseEventButtonNumber); uint32_t modifier_keys = CGEventGetFlags(context); - uint32_t adid = display_arrangement(display_active_display_id()); + uint32_t adid = display_active_display_adid(); struct bar_item* bar_item = bar_manager_get_item_by_wid(&g_bar_manager, wid, @@ -109,7 +109,7 @@ static void event_mouse_up(void* context) { static void event_mouse_dragged(void* context) { CGPoint point = CGEventGetLocation(context); uint32_t wid = get_wid_from_cg_event(context); - uint32_t adid = display_arrangement(display_active_display_id()); + uint32_t adid = display_active_display_adid(); struct bar_item* bar_item = bar_manager_get_item_by_wid(&g_bar_manager, wid, @@ -160,7 +160,7 @@ static void event_mouse_entered(void* context) { return; } - uint32_t adid = display_arrangement(display_active_display_id()); + uint32_t adid = display_active_display_adid(); struct bar_item* bar_item = bar_manager_get_item_by_wid(&g_bar_manager, wid, @@ -175,7 +175,7 @@ static void event_mouse_entered(void* context) { } static void event_mouse_exited(void* context) { - uint32_t adid = display_arrangement(display_active_display_id()); + uint32_t adid = display_active_display_adid(); uint32_t wid = get_wid_from_cg_event(context); struct bar* bar,* bar_target; @@ -245,7 +245,7 @@ static void event_mouse_scrolled(void* context) { int scroll_delta = CGEventGetIntegerValueField(context, kCGScrollWheelEventDeltaAxis1); - uint32_t adid = display_arrangement(display_active_display_id()); + uint32_t adid = display_active_display_adid(); uint64_t event_time = clock_gettime_nsec_np(CLOCK_MONOTONIC_RAW_APPROX); if (g_scroll_info.timestamp + SCROLL_TIMEOUT > event_time) { diff --git a/src/message.c b/src/message.c index 94eccb2..91cea45 100644 --- a/src/message.c +++ b/src/message.c @@ -359,7 +359,7 @@ static bool handle_domain_bar(FILE *rsp, struct token domain, char *message) { struct token state = get_token(&message); uint32_t adid = 0; if (token_equals(state, "current")) { - adid = display_arrangement(display_active_display_id()); + adid = display_active_display_adid(); if (adid > 0 && adid <= g_bar_manager.bar_count) needs_refresh = bar_manager_set_hidden(&g_bar_manager,