Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better detection of focused window with certain yabai features #66

Closed
FelixKratz opened this issue Feb 12, 2024 · 2 comments
Closed

Better detection of focused window with certain yabai features #66

FelixKratz opened this issue Feb 12, 2024 · 2 comments

Comments

@FelixKratz
Copy link
Owner

FelixKratz commented Feb 12, 2024

The current heuristic applied to determine the focused user window is good enough in most cases, however, when JB is used in combination with some more advanced yabai features (e.g. focus_follows_mouse autofocus, or presenting multiple windows of the same app on different system sub-layers) this heuristic breaks apart.

The motivation to use the current heuristic is that it does not require using the slow accessibility API but only fast interactions with the WindowServer. The heuristic depends on the fact that the focused window is usually ordered on top of the application window list. This assumption breaks with the aforementioned yabai modifications applied. As far as I know this is as good as it gets for focused window detection without using the accessibility API, but if someone has another idea, let me know.

Current function for reference:

static inline uint32_t get_front_window(int cid) {
uint64_t active_sid = get_active_space_id(cid);
debug("Active space id: %d\n", active_sid);
CFArrayRef space_list_ref = cfarray_of_cfnumbers(&active_sid,
sizeof(uint64_t),
1,
kCFNumberSInt64Type);
ProcessSerialNumber psn;
_SLPSGetFrontProcess(&psn);
int target_cid;
SLSGetConnectionIDForPSN(cid, &psn, &target_cid);
uint64_t set_tags = 1;
uint64_t clear_tags = 0;
CFArrayRef window_list = SLSCopyWindowsWithOptionsAndTags(cid,
target_cid,
space_list_ref,
0x2,
&set_tags,
&clear_tags );
uint32_t wid = 0;
if (window_list) {
uint32_t window_count = CFArrayGetCount(window_list);
if (window_count > 0) {
CFTypeRef query = SLSWindowQueryWindows(cid, window_list, 0x0);
if (query) {
CFTypeRef iterator = SLSWindowQueryResultCopyWindows(query);
if (iterator && SLSWindowIteratorGetCount(iterator) > 0) {
while (SLSWindowIteratorAdvance(iterator)) {
if (window_suitable(iterator)) {
wid = SLSWindowIteratorGetWindowID(iterator);
break;
}
}
}
if (iterator) CFRelease(iterator);
CFRelease(query);
}
} else {
debug("Empty window list\n");
}
CFRelease(window_list);
}
CFRelease(space_list_ref);
return wid;
}

@FelixKratz FelixKratz added help wanted Extra attention is needed incompatibility Some features do not work as expected when specific other tools are running needs investigation Further investigations need to be performed labels Feb 12, 2024
@FelixKratz FelixKratz changed the title Better detection of focused window Better detection of focused window with yabai SIP disabled features Feb 12, 2024
@FelixKratz FelixKratz pinned this issue Feb 12, 2024
@FelixKratz FelixKratz changed the title Better detection of focused window with yabai SIP disabled features Better detection of focused window with yabai features Feb 12, 2024
@FelixKratz FelixKratz changed the title Better detection of focused window with yabai features Better detection of focused window with certain yabai features Feb 12, 2024
FelixKratz added a commit that referenced this issue Feb 12, 2024
@FelixKratz FelixKratz added available on master and removed help wanted Extra attention is needed incompatibility Some features do not work as expected when specific other tools are running needs investigation Further investigations need to be performed labels Feb 12, 2024
@FelixKratz
Copy link
Owner Author

FelixKratz commented Feb 12, 2024

On current master, launching borders from yabairc (or any other AX trusted process) will automatically enable a fallback method for resolving the focused window that is compatible with all yabai features. This fallback method can also be configured manually with the option ax_focus=<boolean>.

@crisidev
Copy link

This solved the issue I reported on #15! This is amazing, thanks!

@FelixKratz FelixKratz unpinned this issue Feb 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants