-
-
Notifications
You must be signed in to change notification settings - Fork 82
/
workspace.m
226 lines (198 loc) · 7.51 KB
/
workspace.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
#include "workspace.h"
#include "misc/helpers.h"
#include <AppKit/AppKit.h>
@interface workspace_context : NSObject {
}
- (id)init;
- (void)addCustomObserver:(NSString *)name;
@end
float workspace_get_scale() {
float scale = 1.f;
NSArray* screens = [NSScreen screens];
for (int i = 0; i < [screens count]; i++) {
NSScreen* screen = screens[i];
float screen_scale = [screen backingScaleFactor];
if (screen_scale > scale) scale = screen_scale;
}
return scale;
}
void workspace_event_handler_init(void **context) {
workspace_context *ws_context = [workspace_context alloc];
*context = ws_context;
}
void workspace_event_handler_begin(void **context) {
workspace_context *ws_context = *context;
[ws_context init];
}
void workspace_event_handler_end(void *context) {
workspace_context *ws_context = (workspace_context *) context;
[ws_context dealloc];
}
void workspace_create_custom_observer (void **context, char* notification) {
workspace_context *ws_context = *context;
[ws_context addCustomObserver:@(notification)];
}
int workspace_display_notch_height(uint32_t did)
{
if (!CGDisplayIsBuiltin(did)) return 0;
int height = 0;
#if __MAC_OS_X_VERSION_MAX_ALLOWED >= 120000
if (__builtin_available(macos 12.0, *)) {
@autoreleasepool {
for (NSScreen *screen in [NSScreen screens]) {
if ([[[screen deviceDescription] objectForKey:@"NSScreenNumber"] unsignedIntValue] == did) {
height = screen.safeAreaInsets.top;
}
}
}
}
#endif
return height;
}
void forced_front_app_event() {
@autoreleasepool {
NSString* name = [[[NSWorkspace sharedWorkspace] frontmostApplication] localizedName];
if (name) {
const char* front_app = [name cStringUsingEncoding:NSUTF8StringEncoding];
if (front_app) {
struct event event = { string_copy((char*)front_app),
APPLICATION_FRONT_SWITCHED };
event_post(&event);
}
}
}
}
char* workspace_copy_app_name_for_pid(pid_t pid) {
const char* result = NULL;
@autoreleasepool {
NSRunningApplication* app = [NSRunningApplication runningApplicationWithProcessIdentifier:pid];
result = [[app localizedName] UTF8String];
}
return result ? string_copy((char*)result) : NULL;
}
CGImageRef workspace_icon_for_app(char* app) {
@autoreleasepool {
NSString* ns_app = [NSString stringWithUTF8String:app];
NSURL* path = [[NSWorkspace sharedWorkspace] URLForApplicationWithBundleIdentifier:ns_app];
if (!path) {
bool recovered = false;
NSArray* running_apps = [[NSWorkspace sharedWorkspace] runningApplications];
for (NSRunningApplication* app in running_apps) {
if ([[app localizedName] isEqualToString:ns_app]) {
ns_app = [app bundleIdentifier];
if (ns_app) {
path = [[NSWorkspace sharedWorkspace] URLForApplicationWithBundleIdentifier:ns_app];
recovered = true;
}
break;
}
}
if (!recovered) return NULL;
}
NSImage* image = [[NSWorkspace sharedWorkspace] iconForFile:path.path];
if (!image) return NULL;
float scale = workspace_get_scale();
NSRect rect = NSMakeRect( 0, 0, 32 * scale, 32 * scale);
return (CGImageRef)CFRetain([image CGImageForProposedRect: &rect
context: NULL
hints: NULL]);
}
}
@implementation workspace_context
- (id)init {
if ((self = [super init])) {
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self
selector:@selector(activeDisplayDidChange:)
name:@"NSWorkspaceActiveDisplayDidChangeNotification"
object:nil];
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self
selector:@selector(activeSpaceDidChange:)
name:NSWorkspaceActiveSpaceDidChangeNotification
object:nil];
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self
selector:@selector(appSwitched:)
name:NSWorkspaceDidActivateApplicationNotification
object:nil];
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self
selector:@selector(willSleep:)
name:NSWorkspaceWillSleepNotification
object:nil];
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self
selector:@selector(didWake:)
name:NSWorkspaceDidWakeNotification
object:nil];
[[NSDistributedNotificationCenter defaultCenter] addObserver:self
selector:@selector(didChangeMenuBarHiding:)
name:@"AppleInterfaceMenuBarHidingChangedNotification"
object:nil];
[[NSDistributedNotificationCenter defaultCenter] addObserver:self
selector:@selector(didWake:)
name:@"com.apple.screenIsUnlocked"
object:nil];
}
return self;
}
- (void)addCustomObserver:(NSString *)name {
[[NSDistributedNotificationCenter defaultCenter] addObserver:self
selector:@selector(allDistributedNotifications:)
name:name
object:nil];
}
- (void)dealloc {
[[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self];
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[NSDistributedNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
- (void) allDistributedNotifications:(NSNotification *)note {
@autoreleasepool {
struct notification* notification = notification_create();
notification->name = string_copy((char*)[[note name] UTF8String]);
if (note.userInfo && [NSJSONSerialization isValidJSONObject:note.userInfo]) {
NSData* data = [NSJSONSerialization dataWithJSONObject:note.userInfo options:NSJSONWritingPrettyPrinted error:NULL];
if (data && [data length] > 0) {
char* info = malloc([data length] + 1);
memcpy(info, [data bytes], [data length]);
info[[data length]] = '\0';
notification->info = info;
}
}
struct event event = { notification, DISTRIBUTED_NOTIFICATION };
event_post(&event);
}
}
- (void)willSleep:(NSNotification *)notification {
struct event event = { NULL, SYSTEM_WILL_SLEEP };
event_post(&event);
}
- (void)didWake:(NSNotification *)notification {
struct event event = { NULL, SYSTEM_WOKE };
event_post(&event);
}
- (void)appSwitched:(NSNotification *)notification {
@autoreleasepool {
char* name = NULL;
if (notification && notification.userInfo) {
NSRunningApplication* app = [notification.userInfo objectForKey:NSWorkspaceApplicationKey];
if (app) {
char* app_name_tmp = (char*)[[app localizedName] UTF8String];
if (app_name_tmp) name = string_copy(app_name_tmp);
}
}
struct event event = { name, APPLICATION_FRONT_SWITCHED };
event_post(&event);
}
}
- (void)didChangeMenuBarHiding:(NSNotification *)notification {
struct event event = { NULL, MENU_BAR_HIDDEN_CHANGED };
event_post(&event);
}
- (void)activeDisplayDidChange:(NSNotification *)notification {
struct event event = { NULL, DISPLAY_CHANGED };
event_post(&event);
}
- (void)activeSpaceDidChange:(NSNotification *)notification {
struct event event = { NULL, SPACE_CHANGED };
event_post(&event);
}
@end