-
-
Notifications
You must be signed in to change notification settings - Fork 648
/
window_manager.c
2677 lines (2153 loc) · 101 KB
/
window_manager.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
extern mach_port_t g_bs_port;
extern uint8_t *g_event_bytes;
extern struct event_loop g_event_loop;
extern void *g_workspace_context;
extern struct process_manager g_process_manager;
extern struct mouse_state g_mouse_state;
extern double g_cv_host_clock_frequency;
static TABLE_HASH_FUNC(hash_wm)
{
return *(uint32_t *) key;
}
static TABLE_COMPARE_FUNC(compare_wm)
{
return *(uint32_t *) key_a == *(uint32_t *) key_b;
}
bool window_manager_is_window_eligible(struct window *window)
{
bool result = window->is_root && (window_is_real(window) || window_check_rule_flag(window, WINDOW_RULE_MANAGED));
return result;
}
void window_manager_query_window_rules(FILE *rsp)
{
TIME_FUNCTION;
fprintf(rsp, "[");
for (int i = 0; i < buf_len(g_window_manager.rules); ++i) {
struct rule *rule = &g_window_manager.rules[i];
rule_serialize(rsp, rule, i);
if (i < buf_len(g_window_manager.rules) - 1) fprintf(rsp, ",");
}
fprintf(rsp, "]\n");
}
void window_manager_query_windows_for_spaces(FILE *rsp, uint64_t *space_list, int space_count, uint64_t flags)
{
TIME_FUNCTION;
int window_count = 0;
uint32_t *window_list = space_window_list_for_connection(space_list, space_count, 0, &window_count, true);
fprintf(rsp, "[");
for (int i = 0; i < window_count; ++i) {
struct window *window = window_manager_find_window(&g_window_manager, window_list[i]);
if (window) window_serialize(rsp, window, flags); else window_nonax_serialize(rsp, window_list[i], flags);
if (i < window_count - 1) fprintf(rsp, ",");
}
fprintf(rsp, "]\n");
}
void window_manager_query_windows_for_display(FILE *rsp, uint32_t did, uint64_t flags)
{
TIME_FUNCTION;
int space_count = 0;
uint64_t *space_list = display_space_list(did, &space_count);
window_manager_query_windows_for_spaces(rsp, space_list, space_count, flags);
}
void window_manager_query_windows_for_displays(FILE *rsp, uint64_t flags)
{
TIME_FUNCTION;
int display_count = 0;
uint32_t *display_list = display_manager_active_display_list(&display_count);
int space_count = 0;
uint64_t *space_list = NULL;
for (int i = 0; i < display_count; ++i) {
int count;
uint64_t *list = display_space_list(display_list[i], &count);
if (!list) continue;
//
// NOTE(koekeishiya): display_space_list(..) uses a linear allocator,
// and so we only need to track the beginning of the first list along
// with the total number of spaces that have been allocated.
//
if (!space_list) space_list = list;
space_count += count;
}
window_manager_query_windows_for_spaces(rsp, space_list, space_count, flags);
}
bool window_manager_rule_matches_window(struct rule *rule, struct window *window, char *window_title, char *window_role, char *window_subrole)
{
int regex_match_app = rule_check_flag(rule, RULE_APP_EXCLUDE) ? REGEX_MATCH_YES : REGEX_MATCH_NO;
if (regex_match(rule_check_flag(rule, RULE_APP_VALID), &rule->app_regex, window->application->name) == regex_match_app) return false;
int regex_match_title = rule_check_flag(rule, RULE_TITLE_EXCLUDE) ? REGEX_MATCH_YES : REGEX_MATCH_NO;
if (regex_match(rule_check_flag(rule, RULE_TITLE_VALID), &rule->title_regex, window_title) == regex_match_title) return false;
int regex_match_role = rule_check_flag(rule, RULE_ROLE_EXCLUDE) ? REGEX_MATCH_YES : REGEX_MATCH_NO;
if (regex_match(rule_check_flag(rule, RULE_ROLE_VALID), &rule->role_regex, window_role) == regex_match_role) return false;
int regex_match_subrole = rule_check_flag(rule, RULE_SUBROLE_EXCLUDE) ? REGEX_MATCH_YES : REGEX_MATCH_NO;
if (regex_match(rule_check_flag(rule, RULE_SUBROLE_VALID), &rule->subrole_regex, window_subrole) == regex_match_subrole) return false;
return true;
}
void window_manager_apply_manage_rule_effects_to_window(struct space_manager *sm, struct window_manager *wm, struct window *window, struct rule_effects *effects)
{
if (effects->manage == RULE_PROP_ON) {
window_set_rule_flag(window, WINDOW_RULE_MANAGED);
window_manager_make_window_floating(sm, wm, window, false, true);
} else if (effects->manage == RULE_PROP_OFF) {
window_clear_rule_flag(window, WINDOW_RULE_MANAGED);
window_manager_make_window_floating(sm, wm, window, true, true);
}
}
void window_manager_apply_rule_effects_to_window(struct space_manager *sm, struct window_manager *wm, struct window *window, struct rule_effects *effects)
{
if (effects->sid || effects->did) {
if (!window_is_fullscreen(window) && !space_is_fullscreen(window_space(window->id))) {
uint64_t sid = effects->sid ? effects->sid : display_space_id(effects->did);
window_manager_send_window_to_space(sm, wm, window, sid, true);
if (rule_effects_check_flag(effects, RULE_FOLLOW_SPACE) || effects->fullscreen == RULE_PROP_ON) {
space_manager_focus_space(sid);
}
}
}
if (effects->sticky == RULE_PROP_ON) {
window_manager_make_window_sticky(sm, wm, window, true);
} else if (effects->sticky == RULE_PROP_OFF) {
window_manager_make_window_sticky(sm, wm, window, false);
}
if (effects->mff == RULE_PROP_ON) {
window_set_rule_flag(window, WINDOW_RULE_MFF);
window_set_rule_flag(window, WINDOW_RULE_MFF_VALUE);
} else if (effects->mff == RULE_PROP_OFF) {
window_set_rule_flag(window, WINDOW_RULE_MFF);
window_clear_rule_flag(window, WINDOW_RULE_MFF_VALUE);
}
if (rule_effects_check_flag(effects, RULE_LAYER)) {
window_manager_set_window_layer(window, effects->layer);
}
if (rule_effects_check_flag(effects, RULE_OPACITY) && in_range_ii(effects->opacity, 0.0f, 1.0f)) {
window->opacity = effects->opacity;
window_manager_set_opacity(wm, window, effects->opacity);
}
if (effects->fullscreen == RULE_PROP_ON) {
AXUIElementSetAttributeValue(window->ref, kAXFullscreenAttribute, kCFBooleanTrue);
window_set_rule_flag(window, WINDOW_RULE_FULLSCREEN);
}
if (effects->scratchpad) {
char *scratchpad = string_copy(effects->scratchpad);
if (!window_manager_set_scratchpad_for_window(wm, window, scratchpad)) {
free(scratchpad);
}
}
if (effects->grid[0] != 0 && effects->grid[1] != 0) {
window_manager_apply_grid(sm, wm, window, effects->grid[0], effects->grid[1], effects->grid[2], effects->grid[3], effects->grid[4], effects->grid[5]);
}
}
void window_manager_apply_manage_rules_to_window(struct space_manager *sm, struct window_manager *wm, struct window *window, char *window_title, char *window_role, char *window_subrole, bool one_shot_rules)
{
bool match = false;
struct rule_effects effects = {0};
for (int i = 0; i < buf_len(wm->rules); ++i) {
if (one_shot_rules || !rule_check_flag(&wm->rules[i], RULE_ONE_SHOT)) {
if (window_manager_rule_matches_window(&wm->rules[i], window, window_title, window_role, window_subrole)) {
if (wm->rules[i].effects.manage == RULE_PROP_ON) {
if (!rule_check_flag(&wm->rules[i], RULE_ROLE_VALID) && !string_equals(window_role , "AXWindow")) continue;
if (!rule_check_flag(&wm->rules[i], RULE_SUBROLE_VALID) && !string_equals(window_subrole, "AXStandardWindow")) continue;
}
match = true;
rule_combine_effects(&wm->rules[i].effects, &effects);
if (rule_check_flag(&wm->rules[i], RULE_ONE_SHOT)) rule_set_flag(&wm->rules[i], RULE_ONE_SHOT_REMOVE);
}
}
}
if (match) window_manager_apply_manage_rule_effects_to_window(sm, wm, window, &effects);
}
void window_manager_apply_rules_to_window(struct space_manager *sm, struct window_manager *wm, struct window *window, char *window_title, char *window_role, char *window_subrole, bool one_shot_rules)
{
bool match = false;
struct rule_effects effects = {0};
for (int i = 0; i < buf_len(wm->rules); ++i) {
if (one_shot_rules || !rule_check_flag(&wm->rules[i], RULE_ONE_SHOT)) {
if (window_manager_rule_matches_window(&wm->rules[i], window, window_title, window_role, window_subrole)) {
if (!window_check_rule_flag(window, WINDOW_RULE_MANAGED)) {
if (!rule_check_flag(&wm->rules[i], RULE_ROLE_VALID) && !string_equals(window_role , "AXWindow")) continue;
if (!rule_check_flag(&wm->rules[i], RULE_SUBROLE_VALID) && !string_equals(window_subrole, "AXStandardWindow")) continue;
}
match = true;
rule_combine_effects(&wm->rules[i].effects, &effects);
if (rule_check_flag(&wm->rules[i], RULE_ONE_SHOT)) rule_set_flag(&wm->rules[i], RULE_ONE_SHOT_REMOVE);
}
}
}
if (match) window_manager_apply_rule_effects_to_window(sm, wm, window, &effects);
}
void window_manager_set_focus_follows_mouse(struct window_manager *wm, enum ffm_mode mode)
{
mouse_handler_end(&g_mouse_state);
if (mode == FFM_DISABLED) {
mouse_handler_begin(&g_mouse_state, MOUSE_EVENT_MASK);
} else {
mouse_handler_begin(&g_mouse_state, MOUSE_EVENT_MASK_FFM);
}
wm->ffm_mode = mode;
}
void window_manager_set_window_opacity_enabled(struct window_manager *wm, bool enabled)
{
wm->enable_window_opacity = enabled;
table_for (struct window *window, wm->window, {
if (window_manager_is_window_eligible(window)) {
window_manager_set_opacity(wm, window, enabled ? window->opacity : 1.0f);
}
})
}
void window_manager_center_mouse(struct window_manager *wm, struct window *window)
{
if (window_check_rule_flag(window, WINDOW_RULE_MFF)) {
if (!window_check_rule_flag(window, WINDOW_RULE_MFF_VALUE)) {
return;
}
} else {
if (!wm->enable_mff) {
return;
}
}
CGPoint cursor;
SLSGetCurrentCursorLocation(g_connection, &cursor);
if (CGRectContainsPoint(window->frame, cursor)) return;
uint32_t did = window_display_id(window->id);
if (!did) return;
CGPoint center = {
window->frame.origin.x + window->frame.size.width / 2,
window->frame.origin.y + window->frame.size.height / 2
};
CGRect bounds = CGDisplayBounds(did);
if (!CGRectContainsPoint(bounds, center)) return;
CGWarpMouseCursorPosition(center);
}
bool window_manager_should_manage_window(struct window *window)
{
if (!window->is_root) return false;
if (window_check_flag(window, WINDOW_FLOAT)) return false;
if (window_is_sticky(window->id)) return false;
if (window_check_flag(window, WINDOW_MINIMIZE)) return false;
if (window->application->is_hidden) return false;
return (window_is_standard(window) && window_level_is_standard(window) && window_can_move(window)) || window_check_rule_flag(window, WINDOW_RULE_MANAGED);
}
struct view *window_manager_find_managed_window(struct window_manager *wm, struct window *window)
{
return table_find(&wm->managed_window, &window->id);
}
void window_manager_remove_managed_window(struct window_manager *wm, uint32_t wid)
{
table_remove(&wm->managed_window, &wid);
}
void window_manager_add_managed_window(struct window_manager *wm, struct window *window, struct view *view)
{
if (view->layout == VIEW_FLOAT) return;
table_add(&wm->managed_window, &window->id, view);
window_manager_purify_window(wm, window);
}
enum window_op_error window_manager_adjust_window_ratio(struct window_manager *wm, struct window *window, int type, float ratio)
{
TIME_FUNCTION;
struct view *view = window_manager_find_managed_window(wm, window);
if (!view) return WINDOW_OP_ERROR_INVALID_SRC_VIEW;
struct window_node *node = view_find_window_node(view, window->id);
if (!node || !node->parent) return WINDOW_OP_ERROR_INVALID_SRC_NODE;
switch (type) {
case TYPE_REL: {
node->parent->ratio = clampf_range(node->parent->ratio + ratio, 0.1f, 0.9f);
} break;
case TYPE_ABS: {
node->parent->ratio = clampf_range(ratio, 0.1f, 0.9f);
} break;
}
window_node_update(view, node->parent);
if (space_is_visible(view->sid)) {
window_node_flush(node->parent);
} else {
view_set_flag(view, VIEW_IS_DIRTY);
}
return WINDOW_OP_ERROR_SUCCESS;
}
enum window_op_error window_manager_move_window_relative(struct window_manager *wm, struct window *window, int type, float dx, float dy)
{
TIME_FUNCTION;
struct view *view = window_manager_find_managed_window(wm, window);
if (view) return WINDOW_OP_ERROR_INVALID_SRC_VIEW;
if (type == TYPE_REL) {
dx += window->frame.origin.x;
dy += window->frame.origin.y;
}
window_manager_animate_window((struct window_capture) { .window = window, .x = dx, .y = dy, .w = window->frame.size.width, .h = window->frame.size.height });
return WINDOW_OP_ERROR_SUCCESS;
}
void window_manager_resize_window_relative_internal(struct window *window, CGRect frame, int direction, float dx, float dy, bool animate)
{
TIME_FUNCTION;
int x_mod = (direction & HANDLE_LEFT) ? -1 : (direction & HANDLE_RIGHT) ? 1 : 0;
int y_mod = (direction & HANDLE_TOP) ? -1 : (direction & HANDLE_BOTTOM) ? 1 : 0;
float fw = max(1, frame.size.width + dx * x_mod);
float fh = max(1, frame.size.height + dy * y_mod);
float fx = (direction & HANDLE_LEFT) ? frame.origin.x + frame.size.width - fw : frame.origin.x;
float fy = (direction & HANDLE_TOP) ? frame.origin.y + frame.size.height - fh : frame.origin.y;
if (animate) {
window_manager_animate_window((struct window_capture) { .window = window, .x = fx, .y = fy, .w = fw, .h = fh });
} else {
AX_ENHANCED_UI_WORKAROUND(window->application->ref, {
window_manager_move_window(window, fx, fy);
window_manager_resize_window(window, fw, fh);
});
}
}
enum window_op_error window_manager_resize_window_relative(struct window_manager *wm, struct window *window, int direction, float dx, float dy, bool animate)
{
TIME_FUNCTION;
struct view *view = window_manager_find_managed_window(wm, window);
if (view) {
if (direction == HANDLE_ABS) return WINDOW_OP_ERROR_INVALID_OPERATION;
struct window_node *node = view_find_window_node(view, window->id);
if (!node) return WINDOW_OP_ERROR_INVALID_SRC_NODE;
struct window_node *x_fence = NULL;
struct window_node *y_fence = NULL;
if (direction & HANDLE_TOP) x_fence = window_node_fence(node, DIR_NORTH);
if (direction & HANDLE_BOTTOM) x_fence = window_node_fence(node, DIR_SOUTH);
if (direction & HANDLE_LEFT) y_fence = window_node_fence(node, DIR_WEST);
if (direction & HANDLE_RIGHT) y_fence = window_node_fence(node, DIR_EAST);
if (!x_fence && !y_fence) return WINDOW_OP_ERROR_INVALID_DST_NODE;
if (y_fence) {
float sr = y_fence->ratio + (float) dx / (float) y_fence->area.w;
y_fence->ratio = clampf_range(sr, 0.1f, 0.9f);
}
if (x_fence) {
float sr = x_fence->ratio + (float) dy / (float) x_fence->area.h;
x_fence->ratio = clampf_range(sr, 0.1f, 0.9f);
}
view_update(view);
view_flush(view);
} else {
if (direction == HANDLE_ABS) {
if (animate) {
window_manager_animate_window((struct window_capture) { .window = window, .x = window->frame.origin.x, .y = window->frame.origin.y, .w = dx, .h = dy });
} else {
AX_ENHANCED_UI_WORKAROUND(window->application->ref, { window_manager_resize_window(window, dx, dy); });
}
} else {
window_manager_resize_window_relative_internal(window, window_ax_frame(window), direction, dx, dy, animate);
}
}
return WINDOW_OP_ERROR_SUCCESS;
}
void window_manager_move_window(struct window *window, float x, float y)
{
CGPoint position = CGPointMake(x, y);
CFTypeRef position_ref = AXValueCreate(kAXValueTypeCGPoint, (void *) &position);
if (!position_ref) return;
AXUIElementSetAttributeValue(window->ref, kAXPositionAttribute, position_ref);
CFRelease(position_ref);
}
void window_manager_resize_window(struct window *window, float width, float height)
{
CGSize size = CGSizeMake(width, height);
CFTypeRef size_ref = AXValueCreate(kAXValueTypeCGSize, (void *) &size);
if (!size_ref) return;
AXUIElementSetAttributeValue(window->ref, kAXSizeAttribute, size_ref);
CFRelease(size_ref);
}
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wmissing-field-initializers"
static inline void window_manager_notify_jankyborders(struct window_animation *animation_list, int animation_count, uint32_t event, bool skip, bool wait)
{
mach_port_t port;
if (g_bs_port && bootstrap_look_up(g_bs_port, "git.felix.jbevent", &port) == KERN_SUCCESS) {
struct {
uint32_t event;
uint32_t count;
uint32_t proxy_wid[512];
uint32_t real_wid[512];
} data = { event, 0 };
for (int i = 0; i < animation_count; ++i) {
if (skip && __atomic_load_n(&animation_list[i].skip, __ATOMIC_RELAXED)) continue;
data.proxy_wid[data.count] = animation_list[i].proxy.id;
data.real_wid[data.count] = animation_list[i].wid;
++data.count;
}
mach_send(port, &data, sizeof(data));
if (wait) usleep(20000);
}
}
#pragma clang diagnostic pop
static void window_manager_create_window_proxy(int animation_connection, float alpha, struct window_proxy *proxy)
{
if (!proxy->image) return;
CFTypeRef frame_region;
CGSNewRegionWithRect(&proxy->frame, &frame_region);
CFTypeRef empty_region = CGRegionCreateEmptyRegion();
uint64_t tags = 1ULL << 46;
SLSNewWindowWithOpaqueShapeAndContext(animation_connection, 2, frame_region, empty_region, 13|(1 << 18), &tags, 0, 0, 64, &proxy->id, NULL);
sls_window_disable_shadow(proxy->id);
SLSSetWindowOpacity(animation_connection, proxy->id, 0);
SLSSetWindowResolution(animation_connection, proxy->id, 2.0f);
SLSSetWindowAlpha(animation_connection, proxy->id, alpha);
SLSSetWindowLevel(animation_connection, proxy->id, proxy->level);
SLSSetWindowSubLevel(animation_connection, proxy->id, proxy->sub_level);
proxy->context = SLWindowContextCreate(animation_connection, proxy->id, 0);
CGRect frame = { {0, 0}, proxy->frame.size };
CGContextClearRect(proxy->context, frame);
CGContextDrawImage(proxy->context, frame, proxy->image);
CGContextFlush(proxy->context);
CFRelease(frame_region);
CFRelease(empty_region);
}
static void window_manager_destroy_window_proxy(int animation_connection, struct window_proxy *proxy)
{
if (proxy->image) {
CFRelease(proxy->image);
proxy->image = NULL;
}
if (proxy->context) {
CGContextRelease(proxy->context);
proxy->context = NULL;
}
if (proxy->id) {
SLSReleaseWindow(animation_connection, proxy->id);
proxy->id = 0;
}
}
static void *window_manager_build_window_proxy_thread_proc(void *data)
{
struct window_animation *animation = data;
float alpha = 1.0f;
SLSGetWindowAlpha(animation->cid, animation->wid, &alpha);
animation->proxy.level = window_level(animation->wid);
animation->proxy.sub_level = window_sub_level(animation->wid);
SLSGetWindowBounds(animation->cid, animation->wid, &animation->proxy.frame);
animation->proxy.tx = animation->proxy.frame.origin.x;
animation->proxy.ty = animation->proxy.frame.origin.y;
animation->proxy.tw = animation->proxy.frame.size.width;
animation->proxy.th = animation->proxy.frame.size.height;
CFArrayRef image_array = SLSHWCaptureWindowList(animation->cid, &animation->wid, 1, (1 << 11) | (1 << 8));
if (image_array) {
animation->proxy.image = alpha == 1.0f
? (CGImageRef) CFRetain(CFArrayGetValueAtIndex(image_array, 0))
: cgimage_restore_alpha((CGImageRef) CFArrayGetValueAtIndex(image_array, 0));
CFRelease(image_array);
} else {
animation->proxy.image = NULL;
}
window_manager_create_window_proxy(animation->cid, alpha, &animation->proxy);
return NULL;
}
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-parameter"
static CVReturn window_manager_animate_window_list_thread_proc(CVDisplayLinkRef link, const CVTimeStamp *now, const CVTimeStamp *output_time, CVOptionFlags flags, CVOptionFlags *flags_out, void *data)
{
struct window_animation_context *context = data;
int animation_count = context->animation_count;
uint64_t current_clock = output_time->hostTime;
if (!context->animation_clock) context->animation_clock = now->hostTime;
double t = (double)(current_clock - context->animation_clock) / (double)(context->animation_duration * g_cv_host_clock_frequency);
if (t <= 0.0) t = 0.0f;
if (t >= 1.0) t = 1.0f;
float mt;
switch (context->animation_easing) {
#define ANIMATION_EASING_TYPE_ENTRY(value) case value##_type: mt = value(t); break;
ANIMATION_EASING_TYPE_LIST
#undef ANIMATION_EASING_TYPE_ENTRY
}
CFTypeRef transaction = SLSTransactionCreate(context->animation_connection);
for (int i = 0; i < animation_count; ++i) {
if (__atomic_load_n(&context->animation_list[i].skip, __ATOMIC_RELAXED)) continue;
context->animation_list[i].proxy.tx = lerp(context->animation_list[i].proxy.frame.origin.x, mt, context->animation_list[i].x);
context->animation_list[i].proxy.ty = lerp(context->animation_list[i].proxy.frame.origin.y, mt, context->animation_list[i].y);
context->animation_list[i].proxy.tw = lerp(context->animation_list[i].proxy.frame.size.width, mt, context->animation_list[i].w);
context->animation_list[i].proxy.th = lerp(context->animation_list[i].proxy.frame.size.height, mt, context->animation_list[i].h);
CGAffineTransform transform = CGAffineTransformMakeTranslation(-context->animation_list[i].proxy.tx, -context->animation_list[i].proxy.ty);
CGAffineTransform scale = CGAffineTransformMakeScale(context->animation_list[i].proxy.frame.size.width / context->animation_list[i].proxy.tw, context->animation_list[i].proxy.frame.size.height / context->animation_list[i].proxy.th);
SLSTransactionSetWindowTransform(transaction, context->animation_list[i].proxy.id, 0, 0, CGAffineTransformConcat(transform, scale));
float alpha = 0.0f;
SLSGetWindowAlpha(context->animation_connection, context->animation_list[i].wid, &alpha);
if (alpha != 0.0f) SLSTransactionSetWindowAlpha(transaction, context->animation_list[i].proxy.id, alpha);
}
SLSTransactionCommit(transaction, 0);
CFRelease(transaction);
if (t != 1.0f) goto out;
pthread_mutex_lock(&g_window_manager.window_animations_lock);
SLSDisableUpdate(context->animation_connection);
window_manager_notify_jankyborders(context->animation_list, context->animation_count, 1326, true, true);
scripting_addition_swap_window_proxy_out(context->animation_list, context->animation_count);
for (int i = 0; i < animation_count; ++i) {
if (__atomic_load_n(&context->animation_list[i].skip, __ATOMIC_RELAXED)) continue;
table_remove(&g_window_manager.window_animations_table, &context->animation_list[i].wid);
window_manager_destroy_window_proxy(context->animation_connection, &context->animation_list[i].proxy);
}
SLSReenableUpdate(context->animation_connection);
pthread_mutex_unlock(&g_window_manager.window_animations_lock);
SLSReleaseConnection(context->animation_connection);
free(context->animation_list);
free(context);
CVDisplayLinkStop(link);
CVDisplayLinkRelease(link);
out:
return kCVReturnSuccess;
}
#pragma clang diagnostic pop
void window_manager_animate_window_list_async(struct window_capture *window_list, int window_count)
{
struct window_animation_context *context = malloc(sizeof(struct window_animation_context));
SLSNewConnection(0, &context->animation_connection);
context->animation_count = window_count;
context->animation_list = malloc(window_count * sizeof(struct window_animation));
context->animation_duration = g_window_manager.window_animation_duration;
context->animation_easing = g_window_manager.window_animation_easing;
context->animation_clock = 0;
int thread_count = 0;
pthread_t *threads = ts_alloc_list(pthread_t, window_count);
TIME_BODY(window_manager_animate_window_list_async___prep_proxies, {
SLSDisableUpdate(context->animation_connection);
pthread_mutex_lock(&g_window_manager.window_animations_lock);
for (int i = 0; i < window_count; ++i) {
context->animation_list[i].window = window_list[i].window;
context->animation_list[i].wid = window_list[i].window->id;
context->animation_list[i].x = window_list[i].x;
context->animation_list[i].y = window_list[i].y;
context->animation_list[i].w = window_list[i].w;
context->animation_list[i].h = window_list[i].h;
context->animation_list[i].cid = context->animation_connection;
context->animation_list[i].skip = false;
memset(&context->animation_list[i].proxy, 0, sizeof(struct window_proxy));
struct window_animation *existing_animation = table_find(&g_window_manager.window_animations_table, &context->animation_list[i].wid);
if (existing_animation) {
__atomic_store_n(&existing_animation->skip, true, __ATOMIC_RELEASE);
context->animation_list[i].proxy.frame.origin.x = (int)(existing_animation->proxy.tx);
context->animation_list[i].proxy.frame.origin.y = (int)(existing_animation->proxy.ty);
context->animation_list[i].proxy.frame.size.width = (int)(existing_animation->proxy.tw);
context->animation_list[i].proxy.frame.size.height = (int)(existing_animation->proxy.th);
context->animation_list[i].proxy.tx = (int)(existing_animation->proxy.tx);
context->animation_list[i].proxy.ty = (int)(existing_animation->proxy.ty);
context->animation_list[i].proxy.tw = (int)(existing_animation->proxy.tw);
context->animation_list[i].proxy.th = (int)(existing_animation->proxy.th);
context->animation_list[i].proxy.level = existing_animation->proxy.level;
context->animation_list[i].proxy.sub_level = existing_animation->proxy.sub_level;
context->animation_list[i].proxy.image = existing_animation->proxy.image
? (CGImageRef) CFRetain(existing_animation->proxy.image)
: NULL;
__asm__ __volatile__ ("" ::: "memory");
float alpha = 1.0f;
SLSGetWindowAlpha(context->animation_connection, context->animation_list[i].wid, &alpha);
window_manager_create_window_proxy(context->animation_connection, alpha, &context->animation_list[i].proxy);
window_manager_notify_jankyborders(&context->animation_list[i], 1, 1325, true, false);
window_manager_notify_jankyborders(existing_animation, 1, 1326, false, false);
CFTypeRef transaction = SLSTransactionCreate(context->animation_connection);
SLSTransactionOrderWindowGroup(transaction, context->animation_list[i].proxy.id, 1, context->animation_list[i].wid);
SLSTransactionSetWindowSystemAlpha(transaction, existing_animation->proxy.id, 0);
SLSTransactionCommit(transaction, 0);
CFRelease(transaction);
table_remove(&g_window_manager.window_animations_table, &context->animation_list[i].wid);
window_manager_destroy_window_proxy(existing_animation->cid, &existing_animation->proxy);
} else {
pthread_t thread;
if (pthread_create(&thread, NULL, &window_manager_build_window_proxy_thread_proc, &context->animation_list[i]) == 0) {
threads[thread_count++] = thread;
} else {
window_manager_build_window_proxy_thread_proc(&context->animation_list[i]);
}
}
table_add(&g_window_manager.window_animations_table, &context->animation_list[i].wid, &context->animation_list[i]);
}
pthread_mutex_unlock(&g_window_manager.window_animations_lock);
});
TIME_BODY(window_manager_animate_window_list_async___wait_for_threads, {
for (int i = 0; i < thread_count; ++i) {
pthread_join(threads[i], NULL);
}
});
TIME_BODY(window_manager_animate_window_list_async___swap_proxy_in, {
scripting_addition_swap_window_proxy_in(context->animation_list, context->animation_count);
});
TIME_BODY(window_manager_animate_window_list_async___notify_jb, {
window_manager_notify_jankyborders(context->animation_list, context->animation_count, 1325, true, false);
});
TIME_BODY(window_manager_animate_window_list_async___set_frame, {
for (int i = 0; i < window_count; ++i) {
window_manager_set_window_frame(context->animation_list[i].window, context->animation_list[i].x, context->animation_list[i].y, context->animation_list[i].w, context->animation_list[i].h);
}
});
CVDisplayLinkRef link;
SLSReenableUpdate(context->animation_connection);
CVDisplayLinkCreateWithActiveCGDisplays(&link);
CVDisplayLinkSetOutputCallback(link, window_manager_animate_window_list_thread_proc, context);
CVDisplayLinkStart(link);
}
void window_manager_animate_window_list(struct window_capture *window_list, int window_count)
{
TIME_FUNCTION;
if (g_window_manager.window_animation_duration) {
window_manager_animate_window_list_async(window_list, window_count);
} else {
for (int i = 0; i < window_count; ++i) {
window_manager_set_window_frame(window_list[i].window, window_list[i].x, window_list[i].y, window_list[i].w, window_list[i].h);
}
}
}
void window_manager_animate_window(struct window_capture capture)
{
TIME_FUNCTION;
if (g_window_manager.window_animation_duration) {
window_manager_animate_window_list_async(&capture, 1);
} else {
window_manager_set_window_frame(capture.window, capture.x, capture.y, capture.w, capture.h);
}
}
void window_manager_set_window_frame(struct window *window, float x, float y, float width, float height)
{
//
// NOTE(koekeishiya): Attempting to check the window frame cache to prevent unnecessary movement and resize calls to the AX API
// is not reliable because it is possible to perform operations that should be applied, at a higher rate than the AX API events
// are received, causing our cache to become out of date and incorrectly guard against some changes that **should** be applied.
// This causes the window layout to **not** be modified the way we expect.
//
// A possible solution is to use the faster CG window notifications, as they are **a lot** more responsive, and can be used to
// track changes to the window frame in real-time without delay.
//
AX_ENHANCED_UI_WORKAROUND(window->application->ref, {
// NOTE(koekeishiya): Due to macOS constraints (visible screen-area), we might need to resize the window *before* moving it.
window_manager_resize_window(window, width, height);
window_manager_move_window(window, x, y);
// NOTE(koekeishiya): Due to macOS constraints (visible screen-area), we might need to resize the window *after* moving it.
window_manager_resize_window(window, width, height);
});
}
void window_manager_set_purify_mode(struct window_manager *wm, enum purify_mode mode)
{
wm->purify_mode = mode;
table_for (struct window *window, wm->window, {
if (window_manager_is_window_eligible(window)) {
window_manager_purify_window(wm, window);
}
})
}
bool window_manager_set_opacity(struct window_manager *wm, struct window *window, float opacity)
{
if (opacity == 0.0f) {
if (wm->enable_window_opacity) {
opacity = window->id == wm->focused_window_id ? wm->active_window_opacity : wm->normal_window_opacity;
} else {
opacity = 1.0f;
}
}
return scripting_addition_set_opacity(window->id, opacity, wm->window_opacity_duration);
}
void window_manager_set_window_opacity(struct window_manager *wm, struct window *window, float opacity)
{
if (!wm->enable_window_opacity) return;
if (!window_manager_is_window_eligible(window)) return;
if (window->opacity != 0.0f) return;
window_manager_set_opacity(wm, window, opacity);
}
void window_manager_set_menubar_opacity(struct window_manager *wm, float opacity)
{
wm->menubar_opacity = opacity;
SLSSetMenuBarInsetAndAlpha(g_connection, 0, 1, opacity);
}
void window_manager_set_active_window_opacity(struct window_manager *wm, float opacity)
{
wm->active_window_opacity = opacity;
struct window *window = window_manager_focused_window(wm);
if (window) window_manager_set_window_opacity(wm, window, wm->active_window_opacity);
}
void window_manager_set_normal_window_opacity(struct window_manager *wm, float opacity)
{
wm->normal_window_opacity = opacity;
table_for (struct window *window, wm->window, {
if (window->id == wm->focused_window_id) continue;
if (window_manager_is_window_eligible(window)) {
window_manager_set_window_opacity(wm, window, wm->normal_window_opacity);
}
})
}
void window_manager_adjust_layer(struct window *window, int layer)
{
if (window->layer != LAYER_AUTO) return;
scripting_addition_set_layer(window->id, layer);
}
bool window_manager_set_window_layer(struct window *window, int layer)
{
int parent_layer = layer;
int child_layer = layer;
if (layer == LAYER_AUTO) {
parent_layer = window_manager_find_managed_window(&g_window_manager, window) ? LAYER_BELOW : LAYER_NORMAL;
child_layer = LAYER_NORMAL;
}
window->layer = layer;
bool result = scripting_addition_set_layer(window->id, parent_layer);
if (!result) return false;
CFArrayRef window_list = SLSCopyAssociatedWindows(g_connection, window->id);
if (!window_list) return result;
int window_count = CFArrayGetCount(window_list);
CFTypeRef query = SLSWindowQueryWindows(g_connection, window_list, window_count);
CFTypeRef iterator = SLSWindowQueryResultCopyWindows(query);
int relation_count = 0;
uint32_t parent_list[window_count];
uint32_t child_list[window_count];
while (SLSWindowIteratorAdvance(iterator)) {
parent_list[relation_count] = SLSWindowIteratorGetParentID(iterator);
child_list[relation_count] = SLSWindowIteratorGetWindowID(iterator);
++relation_count;
}
int check_count = 1;
uint32_t check_list[window_count];
check_list[0] = window->id;
for (int i = 0; i < check_count; ++i) {
for (int j = 0; j < window_count; ++j) {
if (parent_list[j] != check_list[i]) continue;
scripting_addition_set_layer(child_list[j], child_layer);
check_list[check_count++] = child_list[j];
}
}
CFRelease(query);
CFRelease(iterator);
CFRelease(window_list);
return result;
}
void window_manager_purify_window(struct window_manager *wm, struct window *window)
{
int value;
if (wm->purify_mode == PURIFY_DISABLED) {
value = 1;
} else if (wm->purify_mode == PURIFY_MANAGED) {
value = window_manager_find_managed_window(wm, window) ? 0 : 1;
} else /*if (wm->purify_mode == PURIFY_ALWAYS) */ {
value = 0;
}
if (scripting_addition_set_shadow(window->id, value)) {
if (value) {
window_set_flag(window, WINDOW_SHADOW);
} else {
window_clear_flag(window, WINDOW_SHADOW);
}
}
}
int window_manager_find_rank_of_window_in_list(uint32_t wid, uint32_t *window_list, int window_count)
{
for (int i = 0, rank = 0; i < window_count; ++i) {
if (window_list[i] == wid) {
return rank;
} else {
++rank;
}
}
return INT_MAX;
}
struct window *window_manager_find_window_on_space_by_rank_filtering_window(struct window_manager *wm, uint64_t sid, int rank, uint32_t filter_wid)
{
int count;
uint32_t *window_list = space_window_list(sid, &count, false);
if (!window_list) return NULL;
struct window *result = NULL;
for (int i = 0, j = 0; i < count; ++i) {
if (window_list[i] == filter_wid) continue;
struct window *window = window_manager_find_window(wm, window_list[i]);
if (!window) continue;
if (++j == rank) {
result = window;
break;
}
}
return result;
}
static inline bool window_manager_window_connection_is_jankyborders(int window_cid)
{
static char process_name[PROC_PIDPATHINFO_MAXSIZE];
pid_t window_pid = 0;
SLSConnectionGetPID(window_cid, &window_pid);
proc_name(window_pid, process_name, sizeof(process_name));
return strcmp(process_name, "borders") == 0;
}
struct window *window_manager_find_window_at_point_filtering_window(struct window_manager *wm, CGPoint point, uint32_t filter_wid)
{
CGPoint window_point;
uint32_t window_id;
int window_cid;
SLSFindWindowAndOwner(g_connection, filter_wid, -1, 0, &point, &window_point, &window_id, &window_cid);
if (g_connection == window_cid) SLSFindWindowAndOwner(g_connection, window_id, -1, 0, &point, &window_point, &window_id, &window_cid);
if (window_manager_window_connection_is_jankyborders(window_cid)) {
SLSFindWindowAndOwner(g_connection, window_id, -1, 0, &point, &window_point, &window_id, &window_cid);
if (g_connection == window_cid) SLSFindWindowAndOwner(g_connection, window_id, -1, 0, &point, &window_point, &window_id, &window_cid);
}
return window_manager_find_window(wm, window_id);
}
struct window *window_manager_find_window_at_point(struct window_manager *wm, CGPoint point)
{
CGPoint window_point;
uint32_t window_id;
int window_cid;
SLSFindWindowAndOwner(g_connection, 0, 1, 0, &point, &window_point, &window_id, &window_cid);
if (g_connection == window_cid) SLSFindWindowAndOwner(g_connection, window_id, -1, 0, &point, &window_point, &window_id, &window_cid);
if (window_manager_window_connection_is_jankyborders(window_cid)) {
SLSFindWindowAndOwner(g_connection, window_id, -1, 0, &point, &window_point, &window_id, &window_cid);
if (g_connection == window_cid) SLSFindWindowAndOwner(g_connection, window_id, -1, 0, &point, &window_point, &window_id, &window_cid);
}
return window_manager_find_window(wm, window_id);
}
struct window *window_manager_find_window_below_cursor(struct window_manager *wm)
{
CGPoint cursor;
SLSGetCurrentCursorLocation(g_connection, &cursor);
return window_manager_find_window_at_point(wm, cursor);
}
struct window *window_manager_find_closest_managed_window_in_direction(struct window_manager *wm, struct window *window, int direction)
{
struct view *view = window_manager_find_managed_window(wm, window);
if (!view) return NULL;
struct window_node *node = view_find_window_node(view, window->id);
if (!node) return NULL;
struct window_node *closest = view_find_window_node_in_direction(view, node, direction);
if (!closest) return NULL;
return window_manager_find_window(wm, closest->window_order[0]);
}
struct window *window_manager_find_prev_managed_window(struct space_manager *sm, struct window_manager *wm, struct window *window)
{
struct view *view = space_manager_find_view(sm, space_manager_active_space());
if (!view) return NULL;
struct window_node *node = view_find_window_node(view, window->id);
if (!node) return NULL;
struct window_node *prev = window_node_find_prev_leaf(node);
if (!prev) return NULL;
return window_manager_find_window(wm, prev->window_order[0]);
}