Skip to content

Commit

Permalink
Address MR comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
metakirby5 committed Jun 7, 2024
1 parent d1c46ea commit 2e97091
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Phoenix/NSProcessInfo+PHExtension.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@

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

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

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

@end
49 changes: 27 additions & 22 deletions Phoenix/PHSpace.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
typedef NSUInteger CGSConnectionID;
typedef NSUInteger CGSSpaceID;
typedef NSUInteger CGSWorkspaceID;
typedef NSUInteger CGSMoveWindowCompatID;

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

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

// XXX: Undocumented private API to get the CGSConnectionID for the default connection for this process
CGSConnectionID CGSMainConnectionID(void);
Expand Down Expand Up @@ -68,21 +69,14 @@ @implementation PHSpace
// 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 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 (legacy) 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);
CGSMoveWindowCompatID compatId);

#pragma mark - Initialising

Expand Down Expand Up @@ -244,23 +238,34 @@ - (void)removeWindows:(NSArray<PHWindow *> *)windows {
}

- (void)moveWindows:(NSArray<PHWindow *> *)windows {
CGSConnectionID cid = CGSMainConnectionID();
if ([NSProcessInfo canMoveWindowsToManagedSpace]) {
CGSMoveWindowsToManagedSpace(cid, (__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];
}

/**
* - 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 *> *ids = [self identifiersForWindows:windows];
NSUInteger numIds = [ids count];
NSMutableData *contiguousIds = [[NSMutableData alloc] initWithCapacity:numIds * sizeof(CGWindowID)];
NSUInteger windowCount = [ids count];
NSMutableData *windowIdSequence = [[NSMutableData alloc] initWithCapacity:windowCount * sizeof(CGWindowID)];
for (id item in ids) {
CGWindowID value = [item unsignedIntValue];
[contiguousIds appendBytes:&value length:sizeof(CGWindowID)];
[windowIdSequence appendBytes:&value length:sizeof(CGWindowID)];
}

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

@end

0 comments on commit 2e97091

Please sign in to comment.