Skip to content

Commit

Permalink
Fix Space#moveWindows on Sonoma 14.5.
Browse files Browse the repository at this point in the history
  • Loading branch information
metakirby5 committed Jun 3, 2024
1 parent b8f8f27 commit d1c46ea
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
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)canMoveWindowsToManagedSpace;

@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)canMoveWindowsToManagedSpace {
NSOperatingSystemVersion sonoma145 = {.majorVersion = 14, .minorVersion = 5, .patchVersion = 0};
return ![[self processInfo] isOperatingSystemAtLeastVersion:sonoma145];
}

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

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

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

// An arbitrary ID we can use CGSMoveWindowsToManagedSpace with.
const NSUInteger MoveWindowsCompatId = 0x79616265;

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

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

// XXX: Undocumented private API to move the given windows (CGWindowIDs) to the given space
// Only works prior to MacOS 14.5!
void CGSMoveWindowsToManagedSpace(CGSConnectionID connection, CFArrayRef windowIds, CGSSpaceID spaceId);

/**
* The two functions below serve as a workaround to CGSMoveWindowsToManagedSpace breaking in MacOS 14.5.
* - 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
*/

// XXX: Undocumented private API to set a space's (legacy) compatID
CGError CGSSpaceSetCompatID(CGSConnectionID connection, CGSSpaceID spaceId, NSUInteger compatID);

// XXX: Undocumented private API to move the given windows (CGWindowIDs) to the given space by (legacy) compatID
CGError CGSSetWindowListWorkspace(CGSConnectionID connection,
CGWindowID *windowIds,
NSUInteger windowCount,
NSUInteger compatID);

#pragma mark - Initialising

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

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

NSArray<NSNumber *> *ids = [self identifiersForWindows:windows];
NSUInteger numIds = [ids count];
NSMutableData *contiguousIds = [[NSMutableData alloc] initWithCapacity:numIds * sizeof(CGWindowID)];
for (id item in ids) {
CGWindowID value = [item unsignedIntValue];
[contiguousIds appendBytes:&value length:sizeof(CGWindowID)];
}

CGSSpaceSetCompatID(cid, self.identifier, MoveWindowsCompatId);
CGSSetWindowListWorkspace(cid, (CGWindowID *)[contiguousIds bytes], numIds, MoveWindowsCompatId);
CGSSpaceSetCompatID(cid, self.identifier, 0x0);
}

@end

0 comments on commit d1c46ea

Please sign in to comment.