Skip to content

Commit

Permalink
Fix Space#moveWindows on Sonoma 14.5. (#350)
Browse files Browse the repository at this point in the history
* Fix Space#moveWindows on Sonoma 14.5.

* Address MR comments.

* Fix grammar.

* Use array syntax for clarity.

* Address more feedback.

* Address more feedback.

* Rename item to windowId.
  • Loading branch information
metakirby5 authored Jun 9, 2024
1 parent b8f8f27 commit 6955592
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 3 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)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
47 changes: 44 additions & 3 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 PHMoveWindowsCompatId = 0x79616265;

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

Expand All @@ -60,9 +65,19 @@ @implementation PHSpace
// XXX: Undocumented private API to remove the given windows (CGWindowIDs) from the given spaces (CGSSpaceIDs)
void CGSRemoveWindowsFromSpaces(CGSConnectionID connection, CFArrayRef windowIds, CFArrayRef spaceIds);

// XXX: Undocumented private API to move the given windows (CGWindowIDs) to the given space
// 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);

// XXX: Undocumented private API to set a space’s (legacy) compatId
CGError CGSSpaceSetCompatID(CGSConnectionID connection, CGSSpaceID spaceId, CGSMoveWindowCompatID compatId);

// 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 @@ -222,9 +237,35 @@ - (void)removeWindows:(NSArray<PHWindow *> *)windows {
(__bridge CFArrayRef) @[@(self.identifier)]);
}

/**
* - 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 {
NSArray<NSNumber *> *windowIds = [self identifiersForWindows:windows];
NSUInteger windowCount = [windowIds count];
NSMutableData *windowIdSequence = [[NSMutableData alloc] initWithCapacity:windowCount * sizeof(CGWindowID)];
for (NSNumber *windowId in windowIds) {
CGWindowID value = windowId.unsignedIntValue;
[windowIdSequence appendBytes:&value length:sizeof(CGWindowID)];
}

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

- (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;
}

// CGSMoveWindowsToManagedSpace is broken in macOS 14.5+, so use the legacy Compat ID API instead
[self moveWindowsWithCompatId:windows];
}

@end

0 comments on commit 6955592

Please sign in to comment.