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

Fix Space#moveWindows on Sonoma 14.5. #350

Merged
merged 7 commits into from
Jun 9, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ Changelog

Release: dd.mm.yyyy

### Bug Fixes

- Fix `Space#moveWindows` on Sonoma 14.5 ([#348](https://github.com/kasper/phoenix/issues/348), [#349](https://github.com/kasper/phoenix/issues/349)).

4.0.0
-----

Expand Down
1 change: 1 addition & 0 deletions Phoenix/NSProcessInfo+PHExtension.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@

+ (BOOL)isOperatingSystemAtLeastBigSur;
+ (BOOL)isOperatingSystemAtLeastMonterey;
+ (BOOL)isOperatingSystemAtLeastSonoma145;

@end
5 changes: 5 additions & 0 deletions Phoenix/NSProcessInfo+PHExtension.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@ + (BOOL)isOperatingSystemAtLeastMonterey {
return [[self processInfo] isOperatingSystemAtLeastVersion:monterey];
}

+ (BOOL)isOperatingSystemAtLeastSonoma145 {
NSOperatingSystemVersion sonoma145 = {.majorVersion = 14, .minorVersion = 5, .patchVersion = 0};
return [[self processInfo] isOperatingSystemAtLeastVersion:sonoma145];
}

@end
45 changes: 43 additions & 2 deletions Phoenix/PHSpace.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

typedef NSUInteger CGSConnectionID;
typedef NSUInteger CGSSpaceID;
typedef NSUInteger CGSWorkspaceID;
typedef NSUInteger CGSMoveWindowCompatID;

typedef enum {
kCGSSpaceIncludesCurrent = 1 << 0,
Expand All @@ -36,6 +38,9 @@ @implementation PHSpace
static NSString *const CGSSpaceIDKey = @"ManagedSpaceID";
static NSString *const CGSSpacesKey = @"Spaces";

// An arbitrary ID we can use with CGSSpaceSetCompatID
static const CGSMoveWindowCompatID MoveWindowsCompatId = 0x79616265;
metakirby5 marked this conversation as resolved.
Show resolved Hide resolved

// XXX: Undocumented private API to get the CGSConnectionID for the default connection for this process
CGSConnectionID CGSMainConnectionID(void);

Expand All @@ -61,8 +66,18 @@ @implementation PHSpace
void CGSRemoveWindowsFromSpaces(CGSConnectionID connection, CFArrayRef windowIds, CFArrayRef spaceIds);

// XXX: Undocumented private API to move the given windows (CGWindowIDs) to the given space
metakirby5 marked this conversation as resolved.
Show resolved Hide resolved
// Only works prior to MacOS 14.5!
void CGSMoveWindowsToManagedSpace(CGSConnectionID connection, CFArrayRef windowIds, CGSSpaceID spaceId);

// XXX: Undocumented private API to set a space’s (legacy) compatId
CGError CGSSpaceSetCompatID(CGSConnectionID connection, CGSSpaceID spaceId, CGSMoveWindowCompatID compatID);
metakirby5 marked this conversation as resolved.
Show resolved Hide resolved

// XXX: Undocumented private API to move the given windows (CGWindowIDs) to the given space by its (legacy) compatId
CGError CGSSetWindowListWorkspace(CGSConnectionID connection,
CGWindowID windowIds[],
NSUInteger windowCount,
CGSMoveWindowCompatID compatId);

#pragma mark - Initialising

- (instancetype)initWithIdentifier:(NSUInteger)identifier {
Expand Down Expand Up @@ -223,8 +238,34 @@ - (void)removeWindows:(NSArray<PHWindow *> *)windows {
}

- (void)moveWindows:(NSArray<PHWindow *> *)windows {
CGSMoveWindowsToManagedSpace(
CGSMainConnectionID(), (__bridge CFArrayRef)[self identifiersForWindows:windows], self.identifier);
if (![NSProcessInfo isOperatingSystemAtLeastSonoma145]) {
CGSMoveWindowsToManagedSpace(
CGSMainConnectionID(), (__bridge CFArrayRef)[self identifiersForWindows:windows], self.identifier);
return;
}

metakirby5 marked this conversation as resolved.
Show resolved Hide resolved
// CGSMoveWindowsToManagedSpace is broken in MacOS 14.5, so use the legacy Compat ID API instead.
[self moveWindowsWithCompatId:windows];
}

/**
* - https://github.com/kasper/phoenix/issues/348
* - https://github.com/koekeishiya/yabai/issues/2240#issuecomment-2116326165
* - https://github.com/ianyh/Amethyst/issues/1643#issuecomment-2132519682
*/
- (void)moveWindowsWithCompatId:(NSArray<PHWindow *> *)windows {
metakirby5 marked this conversation as resolved.
Show resolved Hide resolved
NSArray<NSNumber *> *ids = [self identifiersForWindows:windows];
metakirby5 marked this conversation as resolved.
Show resolved Hide resolved
NSUInteger windowCount = [ids count];
NSMutableData *windowIdSequence = [[NSMutableData alloc] initWithCapacity:windowCount * sizeof(CGWindowID)];
for (id item in ids) {
metakirby5 marked this conversation as resolved.
Show resolved Hide resolved
CGWindowID value = [item unsignedIntValue];
metakirby5 marked this conversation as resolved.
Show resolved Hide resolved
[windowIdSequence appendBytes:&value length:sizeof(CGWindowID)];
}

CGSConnectionID connection = CGSMainConnectionID();
CGSSpaceSetCompatID(connection, self.identifier, MoveWindowsCompatId);
CGSSetWindowListWorkspace(connection, (CGWindowID *)[windowIdSequence bytes], windowCount, MoveWindowsCompatId);
CGSSpaceSetCompatID(connection, self.identifier, 0x0);
}

@end
Loading