forked from FelixKratz/SketchyBar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindow.c
309 lines (257 loc) · 9.55 KB
/
window.c
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#include "window.h"
#include "bar_manager.h"
extern struct bar_manager g_bar_manager;
extern int64_t g_disable_capture;
int g_space = 0;
void window_init(struct window* window) {
window->context = NULL;
window->parent = NULL;
window->frame = CGRectNull;
window->id = 0;
window->origin = CGPointZero;
window->surface_id = 0;
window->needs_move = false;
window->needs_resize = false;
window->order_mode = W_ABOVE;
}
static CFTypeRef window_create_region(struct window* window, CGRect frame) {
CFTypeRef frame_region;
CGSNewRegionWithRect(&frame, &frame_region);
return frame_region;
}
void window_create(struct window* window, CGRect frame) {
uint64_t set_tags = kCGSExposeFadeTagBit | kCGSPreventsActivationTagBit;
uint64_t clear_tags = 0;
window->origin = frame.origin;
window->frame.origin = CGPointZero;
window->frame.size = frame.size;
frame.origin = CGPointZero;
CFTypeRef frame_region = window_create_region(window, frame);
uint64_t id;
SLSNewWindow(g_connection,
kCGBackingStoreBuffered,
window->origin.x,
window->origin.y,
frame_region,
&id );
window->id = (uint32_t)id;
CFRelease(frame_region);
SLSSetWindowResolution(g_connection, window->id, 2.0f);
SLSSetWindowTags(g_connection, window->id, &set_tags, 64);
SLSClearWindowTags(g_connection, window->id, &clear_tags, 64);
SLSSetWindowOpacity(g_connection, window->id, 0);
window->context = SLWindowContextCreate(g_connection, window->id, NULL);
CGContextSetInterpolationQuality(window->context, kCGInterpolationNone);
window->needs_move = false;
window->needs_resize = false;
if (g_bar_manager.sticky) {
if (!g_space) {
g_space = SLSSpaceCreate(g_connection, 1, 0);
SLSSpaceSetAbsoluteLevel(g_connection, g_space, 0);
CFArrayRef space_list = cfarray_of_cfnumbers(&g_space,
sizeof(uint32_t),
1,
kCFNumberSInt32Type);
SLSShowSpaces(g_connection, space_list);
CFRelease(space_list);
}
CFArrayRef window_list = cfarray_of_cfnumbers(&window->id,
sizeof(uint32_t),
1,
kCFNumberSInt32Type);
SLSSpaceAddWindowsAndRemoveFromSpaces(g_connection,
g_space,
window_list,
0x7 );
CFRelease(window_list);
}
}
void window_clear(struct window* window) {
window->context = NULL;
window->parent = NULL;
window->id = 0;
window->origin = CGPointZero;
window->frame = CGRectNull;
window->needs_move = false;
window->needs_resize = false;
}
void window_flush(struct window* window) {
SLSFlushWindowContentRegion(g_connection, window->id, NULL);
}
void windows_freeze() {
if (g_transaction) return;
SLSDisableUpdate(g_connection);
g_transaction = SLSTransactionCreate(g_connection);
}
void windows_unfreeze() {
if (g_transaction) {
SLSTransactionCommit(g_transaction, 0);
CFRelease(g_transaction);
g_transaction = NULL;
SLSReenableUpdate(g_connection);
}
}
void window_set_frame(struct window* window, CGRect frame) {
if (window->needs_move
|| !CGPointEqualToPoint(window->origin, frame.origin)) {
window->needs_move = true;
window->origin = frame.origin;
}
if (window->needs_resize
|| !CGSizeEqualToSize(window->frame.size, frame.size)) {
window->needs_resize = true;
window->frame.size = frame.size;
}
}
void window_move(struct window* window, CGPoint point) {
window->origin = point;
if (__builtin_available(macOS 12.0, *)) {
// Monterey and later
windows_freeze();
SLSTransactionMoveWindowWithGroup(g_transaction, window->id, point);
} else {
// Big Sur and previous
SLSMoveWindow(g_connection, window->id, &point);
CFNumberRef number = CFNumberCreate(NULL,
kCFNumberSInt32Type,
&window->id );
const void* values[1] = { number };
CFArrayRef array = CFArrayCreate(NULL, values , 1, &kCFTypeArrayCallBacks);
SLSReassociateWindowsSpacesByGeometry(g_connection, array);
CFRelease(array);
CFRelease(number);
}
}
bool window_apply_frame(struct window* window, bool forced) {
windows_freeze();
if (window->needs_resize || forced) {
CFTypeRef frame_region = window_create_region(window, window->frame);
if (__builtin_available(macOS 13.0, *)) {
// Ventura and later
SLSSetWindowShape(g_connection, window->id,
g_nirvana.x,
g_nirvana.y,
frame_region);
} else {
// Monterey and previous
if (window->parent) {
SLSOrderWindow(g_connection, window->id, 0, window->parent->id);
}
SLSSetWindowShape(g_connection, window->id, 0, 0, frame_region);
if (window->parent) {
CGContextClearRect(window->context, window->frame);
CGContextFlush(window->context);
window_order(window, window->parent, window->order_mode);
}
}
CFRelease(frame_region);
window_move(window, window->origin);
window->needs_move = false;
window->needs_resize = false;
return true;
} else if (window->needs_move) {
window_move(window, window->origin);
window->needs_move = false;
return false;
}
return false;
}
void window_send_to_space(struct window* window, uint64_t dsid) {
CFArrayRef window_list = cfarray_of_cfnumbers(&window->id,
sizeof(uint32_t),
1,
kCFNumberSInt32Type);
SLSMoveWindowsToManagedSpace(g_connection, window_list, dsid);
if (CGPointEqualToPoint(window->origin, g_nirvana)) {
SLSMoveWindow(g_connection, window->id, &g_nirvana);
}
CFRelease(window_list);
}
void window_close(struct window* window) {
if (!window->id) return;
windows_unfreeze();
SLSOrderWindow(g_connection, window->id, 0, 0);
CGContextRelease(window->context);
SLSReleaseWindow(g_connection, window->id);
window_clear(window);
}
void window_set_level(struct window* window, uint32_t level) {
windows_freeze();
if (__builtin_available(macOS 14.0, *)) {
// Sonoma and later
SLSTransactionSetWindowLevel(g_transaction, window->id, level);
} else {
// Ventura and previous
SLSSetWindowLevel(g_connection, window->id, level);
}
}
void window_order(struct window* window, struct window* parent, int mode) {
windows_freeze();
window->parent = parent;
if (mode != W_OUT) window->order_mode = mode;
if (__builtin_available(macOS 14.0, *)) {
// Sonoma and later
SLSTransactionOrderWindow(g_transaction,
window->id,
mode,
parent ? parent->id : 0);
} else {
// Ventura and previous
SLSOrderWindow(g_connection, window->id, mode, parent ? parent->id : 0);
}
}
void window_assign_mouse_tracking_area(struct window* window, CGRect rect) {
SLSRemoveAllTrackingAreas(g_connection, window->id);
SLSAddTrackingRect(g_connection, window->id, rect);
}
void window_set_blur_radius(struct window* window, uint32_t blur_radius) {
SLSSetWindowBackgroundBlurRadius(g_connection, window->id, blur_radius);
}
void context_set_font_smoothing(CGContextRef context, bool smoothing) {
CGContextSetAllowsFontSmoothing(context, smoothing);
}
void window_disable_shadow(struct window* window) {
CFIndex shadow_density = 0;
CFNumberRef shadow_density_cf = CFNumberCreate(kCFAllocatorDefault,
kCFNumberCFIndexType,
&shadow_density );
const void *keys[1] = { CFSTR("com.apple.WindowShadowDensity") };
const void *values[1] = { shadow_density_cf };
CFDictionaryRef shadow_props_cf = CFDictionaryCreate(NULL,
keys,
values,
1,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
SLSWindowSetShadowProperties(window->id, shadow_props_cf);
CFRelease(shadow_density_cf);
CFRelease(shadow_props_cf);
}
CGImageRef window_capture(struct window* window, bool* disabled) {
if (g_disable_capture) {
int64_t time = clock_gettime_nsec_np(CLOCK_MONOTONIC_RAW_APPROX);
if (g_disable_capture < 0) {
*disabled = true;
return NULL;
} else if (time - g_disable_capture > (1ULL << 30)) {
g_disable_capture = 0;
} else {
*disabled = true;
return NULL;
}
}
*disabled = false;
CGImageRef image_ref = NULL;
uint64_t wid = window->id;
SLSCaptureWindowsContentsToRectWithOptions(g_connection,
&wid,
true,
CGRectNull,
1 << 8,
&image_ref );
CGRect bounds;
SLSGetScreenRectForWindow(g_connection, wid, &bounds);
bounds.size.width = (uint32_t) (bounds.size.width + 0.5);
window->frame.size = bounds.size;
return image_ref;
}