-
Notifications
You must be signed in to change notification settings - Fork 127
/
Copy pathPHSpace.m
271 lines (203 loc) · 9.77 KB
/
PHSpace.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
* Phoenix is released under the MIT License. Refer to https://github.com/kasper/phoenix/blob/master/LICENSE.md
*/
@import Cocoa;
#import "PHSpace.h"
#import "NSArray+PHExtension.h"
#import "NSProcessInfo+PHExtension.h"
#import "NSScreen+PHExtension.h"
#import "PHWindow.h"
/* XXX: Undocumented private typedefs for CGSSpace */
typedef NSUInteger CGSConnectionID;
typedef NSUInteger CGSSpaceID;
typedef NSUInteger CGSWorkspaceID;
typedef NSUInteger CGSMoveWindowCompatID;
typedef enum {
kCGSSpaceIncludesCurrent = 1 << 0,
kCGSSpaceIncludesOthers = 1 << 1,
kCGSSpaceIncludesUser = 1 << 2,
kCGSAllSpacesMask = kCGSSpaceIncludesCurrent | kCGSSpaceIncludesOthers | kCGSSpaceIncludesUser
} CGSSpaceMask;
typedef enum { kCGSSpaceUser, kCGSSpaceFullScreen = 4 } CGSSpaceType;
@interface PHSpace ()
@property CGSSpaceID identifier;
@end
@implementation PHSpace
static NSString *const CGSScreenIDKey = @"Display Identifier";
static NSString *const CGSSpaceIDKey = @"ManagedSpaceID";
static NSString *const CGSSpacesKey = @"Spaces";
// An arbitrary ID we can use with CGSSpaceSetCompatID
static const CGSMoveWindowCompatID PHMoveWindowsCompatId = 4000;
static const CGSMoveWindowCompatID PHMoveWindowsResetCompatId = 0;
// XXX: Undocumented private API to get the CGSConnectionID for the default connection for this process
CGSConnectionID CGSMainConnectionID(void);
// XXX: Undocumented private API to get the CGSSpaceID for the active space
CGSSpaceID CGSGetActiveSpace(CGSConnectionID connection);
// XXX: Undocumented private API to get the CGSSpaceID for the current space for a given screen (UUID)
CGSSpaceID CGSManagedDisplayGetCurrentSpace(CGSConnectionID connection, CFStringRef screenId);
// XXX: Undocumented private API to get the CGSSpaceIDs for all spaces in order
CFArrayRef CGSCopyManagedDisplaySpaces(CGSConnectionID connection);
// XXX: Undocumented private API to get the CGSSpaceIDs for the given windows (CGWindowIDs)
CFArrayRef CGSCopySpacesForWindows(CGSConnectionID connection, CGSSpaceMask mask, CFArrayRef windowIds);
// XXX: Undocumented private API to get the CGSSpaceType for a given space
CGSSpaceType CGSSpaceGetType(CGSConnectionID connection, CGSSpaceID space);
// XXX: Undocumented private API to add the given windows (CGWindowIDs) to the given spaces (CGSSpaceIDs)
void CGSAddWindowsToSpaces(CGSConnectionID connection, CFArrayRef windowIds, CFArrayRef spaceIds);
// 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,
// 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 {
if (self = [super init]) {
self.identifier = identifier;
}
return self;
}
#pragma mark - Spaces
+ (instancetype)active {
return [(PHSpace *)[self alloc] initWithIdentifier:CGSGetActiveSpace(CGSMainConnectionID())];
}
+ (NSArray<PHSpace *> *)all {
NSMutableArray *spaces = [NSMutableArray array];
NSArray *displaySpacesInfo = CFBridgingRelease(CGSCopyManagedDisplaySpaces(CGSMainConnectionID()));
for (NSDictionary<NSString *, id> *spacesInfo in displaySpacesInfo) {
NSArray<NSNumber *> *identifiers = [spacesInfo[CGSSpacesKey] valueForKey:CGSSpaceIDKey];
for (NSNumber *identifier in identifiers) {
[spaces addObject:[(PHSpace *)[self alloc] initWithIdentifier:identifier.unsignedLongValue]];
}
}
return spaces;
}
+ (instancetype)currentSpaceForScreen:(NSScreen *)screen {
NSUInteger identifier =
CGSManagedDisplayGetCurrentSpace(CGSMainConnectionID(), (__bridge CFStringRef)[screen identifier]);
return [(PHSpace *)[self alloc] initWithIdentifier:identifier];
}
+ (NSArray<PHSpace *> *)spacesForWindow:(PHWindow *)window {
NSMutableArray *spaces = [NSMutableArray array];
NSArray<NSNumber *> *identifiers = CFBridgingRelease(CGSCopySpacesForWindows(
CGSMainConnectionID(), kCGSAllSpacesMask, (__bridge CFArrayRef) @[@([window identifier])]));
for (PHSpace *space in [self all]) {
NSNumber *identifier = @([space hash]);
if ([identifiers containsObject:identifier]) {
[spaces addObject:[(PHSpace *)[self alloc] initWithIdentifier:identifier.unsignedLongValue]];
}
}
return spaces;
}
#pragma mark - Identifying
- (NSUInteger)hash {
return self.identifier;
}
- (BOOL)isEqual:(id)object {
return [object isKindOfClass:[PHSpace class]] && [self hash] == [object hash];
}
#pragma mark - PHIterableJSExport
- (instancetype)next {
return [[PHSpace all] nextFrom:self];
}
- (instancetype)previous {
return [[PHSpace all] previousFrom:self];
}
#pragma mark - Properties
- (BOOL)isNormal {
return CGSSpaceGetType(CGSMainConnectionID(), self.identifier) == kCGSSpaceUser;
}
- (BOOL)isFullScreen {
return CGSSpaceGetType(CGSMainConnectionID(), self.identifier) == kCGSSpaceFullScreen;
}
- (NSArray<NSScreen *> *)screens {
if (![NSScreen screensHaveSeparateSpaces]) {
return [NSScreen screens];
}
NSArray *displaySpacesInfo = CFBridgingRelease(CGSCopyManagedDisplaySpaces(CGSMainConnectionID()));
for (NSDictionary<NSString *, id> *spacesInfo in displaySpacesInfo) {
NSString *screenIdentifier = spacesInfo[CGSScreenIDKey];
NSArray<NSNumber *> *identifiers = [spacesInfo[CGSSpacesKey] valueForKey:CGSSpaceIDKey];
if ([identifiers containsObject:@(self.identifier)]) {
NSScreen *screen = [NSScreen screenForIdentifier:screenIdentifier];
return screen ? @[screen] : @[];
}
}
return @[];
}
#pragma mark - Windows
- (NSArray<PHWindow *> *)windows {
return [PHWindow
filteredWindowsUsingPredicateBlock:^BOOL(PHWindow *window, __unused NSDictionary<NSString *, id> *bindings) {
return [[window spaces] containsObject:self];
}];
}
- (NSArray<PHWindow *> *)windows:(NSDictionary<NSString *, id> *)optionals {
NSNumber *visibilityOption = optionals[PHWindowVisibilityOptionKey];
// Filter based on visibility
if (visibilityOption) {
return [[self windows] filteredArrayUsingPredicate:[PHWindow isVisible:visibilityOption.boolValue]];
}
return [self windows];
}
- (NSArray<NSNumber *> *)identifiersForWindows:(NSArray<PHWindow *> *)windows {
NSMutableArray<NSNumber *> *identifiers = [NSMutableArray array];
for (PHWindow *window in windows) {
if ([window respondsToSelector:@selector(identifier)]) {
[identifiers addObject:@([window identifier])];
}
}
return identifiers;
}
- (void)addWindows:(NSArray<PHWindow *> *)windows {
if ([NSProcessInfo isOperatingSystemAtLeastMonterey]) {
NSLog(@"Deprecated: Function Space#addWindows(...) is deprecated and will be removed in later versions, use "
@"Space#moveWindows(...) instead.");
return;
}
CGSAddWindowsToSpaces(CGSMainConnectionID(),
(__bridge CFArrayRef)[self identifiersForWindows:windows],
(__bridge CFArrayRef) @[@(self.identifier)]);
}
- (void)removeWindows:(NSArray<PHWindow *> *)windows {
if ([NSProcessInfo isOperatingSystemAtLeastMonterey]) {
NSLog(@"Deprecated: Function Space#removeWindows(...) is deprecated and will be removed in later versions, use "
@"Space#moveWindows(...) instead.");
return;
}
CGSRemoveWindowsFromSpaces(CGSMainConnectionID(),
(__bridge CFArrayRef)[self identifiersForWindows:windows],
(__bridge CFArrayRef) @[@(self.identifier)]);
}
/* Fixes https://github.com/kasper/phoenix/issues/348, referencing:
* - 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, PHMoveWindowsResetCompatId);
}
- (void)moveWindows:(NSArray<PHWindow *> *)windows {
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