-
-
Notifications
You must be signed in to change notification settings - Fork 648
/
payload.m
1108 lines (926 loc) · 36.2 KB
/
payload.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
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
#include <Foundation/Foundation.h>
#include <mach-o/getsect.h>
#include <mach-o/dyld.h>
#include <mach/mach.h>
#include <mach/mach_vm.h>
#include <mach/vm_map.h>
#include <mach/vm_page_size.h>
#include <objc/message.h>
#include <objc/runtime.h>
#include <CoreGraphics/CoreGraphics.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/un.h>
#include <unistd.h>
#include <netdb.h>
#include <dlfcn.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "common.h"
#ifdef __x86_64__
#include "x64_payload.m"
#elif __arm64__
#include "arm64_payload.m"
#include <ptrauth.h>
#endif
#define HASHTABLE_IMPLEMENTATION
#include "../misc/hashtable.h"
#undef HASHTABLE_IMPLEMENTATION
#define SOCKET_PATH_FMT "/tmp/yabai-sa_%s.socket"
#define page_align(addr) (vm_address_t)((uintptr_t)(addr) & (~(vm_page_size - 1)))
#define unpack(v) memcpy(&v, message, sizeof(v)); message += sizeof(v)
#define lerp(a, t, b) (((1.0-t)*a) + (t*b))
extern int SLSMainConnectionID(void);
extern CGError SLSGetConnectionPSN(int cid, ProcessSerialNumber *psn);
extern CGError SLSGetWindowAlpha(int cid, uint32_t wid, float *alpha);
extern CGError SLSSetWindowAlpha(int cid, uint32_t wid, float alpha);
extern OSStatus SLSMoveWindowWithGroup(int cid, uint32_t wid, CGPoint *point);
extern CGError SLSReassociateWindowsSpacesByGeometry(int cid, CFArrayRef window_list);
extern CGError SLSGetWindowOwner(int cid, uint32_t wid, int *window_cid);
extern CGError SLSSetWindowTags(int cid, uint32_t wid, uint64_t *tags, size_t tag_size);
extern CGError SLSClearWindowTags(int cid, uint32_t wid, uint64_t *tags, size_t tag_size);
extern CGError SLSGetWindowBounds(int cid, uint32_t wid, CGRect *frame);
extern CGError SLSGetWindowTransform(int cid, uint32_t wid, CGAffineTransform *t);
extern CGError SLSSetWindowTransform(int cid, uint32_t wid, CGAffineTransform t);
extern CGError SLSOrderWindow(int cid, uint32_t wid, int order, uint32_t rel_wid);
extern void SLSManagedDisplaySetCurrentSpace(int cid, CFStringRef display_ref, uint64_t sid);
extern uint64_t SLSManagedDisplayGetCurrentSpace(int cid, CFStringRef display_ref);
extern CFStringRef SLSCopyManagedDisplayForSpace(int cid, uint64_t sid);
extern void SLSMoveWindowsToManagedSpace(int cid, CFArrayRef window_list, uint64_t sid);
extern void SLSShowSpaces(int cid, CFArrayRef space_list);
extern void SLSHideSpaces(int cid, CFArrayRef space_list);
extern CFTypeRef SLSTransactionCreate(int cid);
extern CGError SLSTransactionCommit(CFTypeRef transaction, int synchronous);
extern CGError SLSTransactionOrderWindowGroup(CFTypeRef transaction, uint32_t wid, int order, uint32_t rel_wid);
extern CGError SLSTransactionSetWindowSystemAlpha(CFTypeRef transaction, uint32_t wid, float alpha);
extern CGError SLSSetWindowSubLevel(int cid, uint32_t wid, int level);
struct window_fade_context
{
pthread_t thread;
uint32_t wid;
volatile float alpha;
volatile float duration;
volatile bool skip;
};
pthread_mutex_t window_fade_lock;
struct table window_fade_table;
static id dock_spaces;
static id dp_desktop_picture_manager;
static uint64_t add_space_fp;
static uint64_t remove_space_fp;
static uint64_t move_space_fp;
static uint64_t set_front_window_fp;
static uint64_t animation_time_addr;
static bool macOSSequoia;
static pthread_t daemon_thread;
static int daemon_sockfd;
static void dump_class_info(Class c)
{
const char *name = class_getName(c);
unsigned int count = 0;
Ivar *ivar_list = class_copyIvarList(c, &count);
for (int i = 0; i < count; i++) {
Ivar ivar = ivar_list[i];
const char *ivar_name = ivar_getName(ivar);
NSLog(@"%s ivar: %s", name, ivar_name);
}
if (ivar_list) free(ivar_list);
objc_property_t *property_list = class_copyPropertyList(c, &count);
for (int i = 0; i < count; i++) {
objc_property_t property = property_list[i];
const char *prop_name = property_getName(property);
NSLog(@"%s property: %s", name, prop_name);
}
if (property_list) free(property_list);
Method *method_list = class_copyMethodList(c, &count);
for (int i = 0; i < count; i++) {
Method method = method_list[i];
const char *method_name = sel_getName(method_getName(method));
NSLog(@"%s method: %s", name, method_name);
}
if (method_list) free(method_list);
}
static Class dump_class_info_by_name(const char *name)
{
Class c = objc_getClass(name);
if (c != nil) {
dump_class_info(c);
}
return c;
}
static uint64_t static_base_address(void)
{
const struct segment_command_64 *command = getsegbyname("__TEXT");
uint64_t addr = command->vmaddr;
return addr;
}
static uint64_t image_slide(void)
{
char path[1024];
uint32_t size = sizeof(path);
if (_NSGetExecutablePath(path, &size) != 0) {
return -1;
}
for (uint32_t i = 0; i < _dyld_image_count(); i++) {
if (strcmp(_dyld_get_image_name(i), path) == 0) {
return _dyld_get_image_vmaddr_slide(i);
}
}
return 0;
}
static uint64_t hex_find_seq(uint64_t baddr, const char *c_pattern)
{
if (!baddr || !c_pattern) return 0;
uint64_t addr = baddr;
uint64_t pattern_length = (strlen(c_pattern) + 1) / 3;
char buffer_a[pattern_length];
char buffer_b[pattern_length];
memset(buffer_a, 0, sizeof(buffer_a));
memset(buffer_b, 0, sizeof(buffer_b));
char *pattern = (char *) c_pattern + 1;
for (int i = 0; i < pattern_length; ++i) {
char c = pattern[-1];
if (c == '?') {
buffer_b[i] = 1;
} else {
int temp = c <= '9' ? 0 : 9;
temp = (temp + c) << 0x4;
c = pattern[0];
int temp2 = c <= '9' ? 0xd0 : 0xc9;
buffer_a[i] = temp2 + c + temp;
}
pattern += 3;
}
loop:
for (int counter = 0; counter < pattern_length; ++counter) {
if ((buffer_b[counter] == 0) && (((char *)addr)[counter] != buffer_a[counter])) {
addr = (uint64_t)((char *)addr + 1);
if (addr - baddr < 0x1286a0) {
goto loop;
} else {
return 0;
}
}
}
return addr;
}
#if __arm64__
uint64_t decode_adrp_add(uint64_t addr, uint64_t offset)
{
uint32_t adrp_instr = *(uint32_t *) addr;
uint32_t immlo = (0x60000000 & adrp_instr) >> 29;
uint32_t immhi = (0xffffe0 & adrp_instr) >> 3;
int32_t value = (immhi | immlo) << 12;
int64_t value_64 = value;
uint32_t add_instr = *(uint32_t *) (addr + 4);
uint64_t imm12 = (add_instr & 0x3ffc00) >> 10;
if (add_instr & 0xc00000) {
imm12 <<= 12;
}
return (offset & 0xfffffffffffff000) + value_64 + imm12;
}
#endif
static bool verify_os_version(NSOperatingSystemVersion os_version)
{
NSLog(@"[yabai-sa] checking for macOS %ld.%ld.%ld compatibility!", os_version.majorVersion, os_version.minorVersion, os_version.patchVersion);
#ifdef __x86_64__
if (os_version.majorVersion == 11) {
return true; // Big Sur 11.0
} else if (os_version.majorVersion == 12) {
return true; // Monterey 12.0
} else if (os_version.majorVersion == 13) {
return true; // Ventura 13.0
} else if (os_version.majorVersion == 14) {
return true; // Sonoma 14.0
} else if (os_version.majorVersion == 15) {
macOSSequoia = true;
return true; // Sequoia 15.0
}
NSLog(@"[yabai-sa] spaces functionality is only supported on macOS Big Sur 11.0.0+, Monterey 12.0.0+, Ventura 13.0.0+, Sonoma 14.0.0+, and Sequoia 15.0");
#elif __arm64__
if (os_version.majorVersion == 12) {
return true; // Monterey 12.0
} else if (os_version.majorVersion == 13) {
return true; // Ventura 13.0
} else if (os_version.majorVersion == 14) {
return true; // Sonoma 14.0
} else if (os_version.majorVersion == 15) {
macOSSequoia = true;
return true; // Sequoia 15.0
}
NSLog(@"[yabai-sa] spaces functionality is only supported on macOS Monterey 12.0.0+, and Ventura 13.0.0+, Sonoma 14.0.0+, and Sequoia 15.0");
#endif
return false;
}
static void init_instances()
{
NSOperatingSystemVersion os_version = [[NSProcessInfo processInfo] operatingSystemVersion];
if (!verify_os_version(os_version)) return;
uint64_t baseaddr = static_base_address() + image_slide();
uint64_t dock_spaces_addr = hex_find_seq(baseaddr + get_dock_spaces_offset(os_version), get_dock_spaces_pattern(os_version));
if (dock_spaces_addr == 0) {
dock_spaces = nil;
NSLog(@"[yabai-sa] could not locate pointer to dock.spaces! spaces functionality will not work!");
} else {
#ifdef __x86_64__
uint32_t dock_spaces_offset = *(int32_t *)dock_spaces_addr;
NSLog(@"[yabai-sa] (0x%llx) dock.spaces found at address 0x%llX (0x%llx)", baseaddr, dock_spaces_addr, dock_spaces_addr - baseaddr);
dock_spaces = [(*(id *)(dock_spaces_addr + dock_spaces_offset + 0x4)) retain];
#elif __arm64__
uint64_t dock_spaces_offset = decode_adrp_add(dock_spaces_addr, dock_spaces_addr - baseaddr);
NSLog(@"[yabai-sa] (0x%llx) dock.spaces found at address 0x%llX (0x%llx)", baseaddr, dock_spaces_offset, dock_spaces_offset - baseaddr);
dock_spaces = [(*(id *)(baseaddr + dock_spaces_offset)) retain];
#endif
}
uint64_t dppm_addr = hex_find_seq(baseaddr + get_dppm_offset(os_version), get_dppm_pattern(os_version));
if (dppm_addr == 0) {
dp_desktop_picture_manager = nil;
NSLog(@"[yabai-sa] could not locate pointer to dppm! moving spaces will not work!");
} else {
#ifdef __x86_64__
uint32_t dppm_offset = *(int32_t *)dppm_addr;
NSLog(@"[yabai-sa] (0x%llx) dppm found at address 0x%llX (0x%llx)", baseaddr, dppm_addr, dppm_addr - baseaddr);
dp_desktop_picture_manager = [(*(id *)(dppm_addr + dppm_offset + 0x4)) retain];
#elif __arm64__
uint64_t dppm_offset = decode_adrp_add(dppm_addr, dppm_addr - baseaddr);
NSLog(@"[yabai-sa] (0x%llx) dppm found at address 0x%llX (0x%llx)", baseaddr, dppm_offset, dppm_offset - baseaddr);
dp_desktop_picture_manager = [(*(id *)(baseaddr + dppm_offset)) retain];
#endif
//
// @hack
//
// NOTE(koekeishiya): For whatever reason, in Sonoma, DPDesktopPictureManager is initialized and swapped
// to an alternate storage location instead of where it used to be stored in previous macOS versions..
//
// This alternate storage location resides 8-bytes before the usual location, so we simply do
// the subtract to arrive at the correct location in cases where the usual location is null.
//
#ifdef __x86_64__
if (dp_desktop_picture_manager == nil) {
dp_desktop_picture_manager = [(*(id *)(dppm_addr + dppm_offset + 0x4 - 0x8)) retain];
}
#elif __arm64__
if (dp_desktop_picture_manager == nil) {
dp_desktop_picture_manager = [(*(id *)(baseaddr + dppm_offset - 0x8)) retain];
}
#endif
}
uint64_t add_space_addr = hex_find_seq(baseaddr + get_add_space_offset(os_version), get_add_space_pattern(os_version));
if (add_space_addr == 0x0) {
NSLog(@"[yabai-sa] failed to get pointer to addSpace function..");
add_space_fp = 0;
} else {
NSLog(@"[yabai-sa] (0x%llx) addSpace found at address 0x%llX (0x%llx)", baseaddr, add_space_addr, add_space_addr - baseaddr);
#ifdef __x86_64__
add_space_fp = add_space_addr;
#elif __arm64__
add_space_fp = (uint64_t) ptrauth_sign_unauthenticated((void *) add_space_addr, ptrauth_key_asia, 0);
#endif
}
uint64_t remove_space_addr = hex_find_seq(baseaddr + get_remove_space_offset(os_version), get_remove_space_pattern(os_version));
if (remove_space_addr == 0x0) {
NSLog(@"[yabai-sa] failed to get pointer to removeSpace function..");
remove_space_fp = 0;
} else {
NSLog(@"[yabai-sa] (0x%llx) removeSpace found at address 0x%llX (0x%llx)", baseaddr, remove_space_addr, remove_space_addr - baseaddr);
#ifdef __x86_64__
remove_space_fp = remove_space_addr;
#elif __arm64__
remove_space_fp = (uint64_t) ptrauth_sign_unauthenticated((void *) remove_space_addr, ptrauth_key_asia, 0);
#endif
}
uint64_t move_space_addr = hex_find_seq(baseaddr + get_move_space_offset(os_version), get_move_space_pattern(os_version));
if (move_space_addr == 0x0) {
NSLog(@"[yabai-sa] failed to get pointer to moveSpace function..");
move_space_fp = 0;
} else {
NSLog(@"[yabai-sa] (0x%llx) moveSpace found at address 0x%llX (0x%llx)", baseaddr, move_space_addr, move_space_addr - baseaddr);
#ifdef __x86_64__
move_space_fp = move_space_addr;
#elif __arm64__
move_space_fp = (uint64_t) ptrauth_sign_unauthenticated((void *) move_space_addr, ptrauth_key_asia, 0);
#endif
}
uint64_t set_front_window_addr = hex_find_seq(baseaddr + get_set_front_window_offset(os_version), get_set_front_window_pattern(os_version));
if (set_front_window_addr == 0x0) {
NSLog(@"[yabai-sa] failed to get pointer to setFrontWindow function..");
set_front_window_fp = 0;
} else {
NSLog(@"[yabai-sa] (0x%llx) setFrontWindow found at address 0x%llX (0x%llx)", baseaddr, set_front_window_addr, set_front_window_addr - baseaddr);
#ifdef __x86_64__
set_front_window_fp = set_front_window_addr;
#elif __arm64__
set_front_window_fp = (uint64_t) ptrauth_sign_unauthenticated((void *) set_front_window_addr, ptrauth_key_asia, 0);
#endif
}
animation_time_addr = hex_find_seq(baseaddr + get_fix_animation_offset(os_version), get_fix_animation_pattern(os_version));
if (animation_time_addr == 0x0) {
NSLog(@"[yabai-sa] failed to get pointer to animation-time..");
} else {
NSLog(@"[yabai-sa] (0x%llx) animation_time_addr found at address 0x%llX (0x%llx)", baseaddr, animation_time_addr, animation_time_addr - baseaddr);
if (vm_protect(mach_task_self(), page_align(animation_time_addr), vm_page_size, 0, VM_PROT_READ | VM_PROT_WRITE | VM_PROT_COPY) == KERN_SUCCESS) {
#ifdef __x86_64__
*(uint64_t *) animation_time_addr = 0x660fefc0660fefc0;
#elif __arm64__
*(uint32_t *) animation_time_addr = 0x2f00e400;
#endif
vm_protect(mach_task_self(), page_align(animation_time_addr), vm_page_size, 0, VM_PROT_READ | VM_PROT_EXECUTE);
} else {
NSLog(@"[yabai-sa] animation_time_addr vm_protect failed; unable to patch instruction!");
}
}
}
static inline id get_ivar_value(id instance, const char *name)
{
id result = nil;
object_getInstanceVariable(instance, name, (void **) &result);
return result;
}
static inline void set_ivar_value(id instance, const char *name, id value)
{
object_setInstanceVariable(instance, name, value);
}
static inline uint64_t get_space_id(id space)
{
return ((uint64_t (*)(id, SEL)) objc_msgSend)(space, @selector(spid));
}
static inline id space_for_display_with_id(CFStringRef display_uuid, uint64_t space_id)
{
NSArray *spaces_for_display = ((NSArray *(*)(id, SEL, CFStringRef)) objc_msgSend)(dock_spaces, @selector(spacesForDisplay:), display_uuid);
for (id space in spaces_for_display) {
if (space_id == get_space_id(space)) {
return space;
}
}
return nil;
}
static inline id display_space_for_display_uuid(CFStringRef display_uuid)
{
id result = nil;
NSArray *display_spaces = get_ivar_value(dock_spaces, "_displaySpaces");
if (display_spaces != nil) {
for (id display_space in display_spaces) {
id display_source_space = get_ivar_value(display_space, "_currentSpace");
uint64_t sid = get_space_id(display_source_space);
CFStringRef uuid = SLSCopyManagedDisplayForSpace(SLSMainConnectionID(), sid);
bool match = CFEqual(uuid, display_uuid);
CFRelease(uuid);
if (match) {
result = display_space;
break;
}
}
}
return result;
}
static inline id display_space_for_space_with_id(uint64_t space_id)
{
NSArray *display_spaces = get_ivar_value(dock_spaces, "_displaySpaces");
if (display_spaces != nil) {
for (id display_space in display_spaces) {
id display_source_space = get_ivar_value(display_space, "_currentSpace");
if (get_space_id(display_source_space) == space_id) {
return display_space;
}
}
}
return nil;
}
static void do_space_move(char *message)
{
if (dock_spaces == nil || dp_desktop_picture_manager == nil || move_space_fp == 0) return;
uint64_t source_space_id, dest_space_id, source_prev_space_id;
unpack(source_space_id);
unpack(dest_space_id);
unpack(source_prev_space_id);
bool focus_dest_space;
unpack(focus_dest_space);
CFStringRef source_display_uuid = SLSCopyManagedDisplayForSpace(SLSMainConnectionID(), source_space_id);
id source_space = space_for_display_with_id(source_display_uuid, source_space_id);
id source_display_space = display_space_for_display_uuid(source_display_uuid);
CFStringRef dest_display_uuid = SLSCopyManagedDisplayForSpace(SLSMainConnectionID(), dest_space_id);
id dest_space = space_for_display_with_id(dest_display_uuid, dest_space_id);
unsigned dest_display_id = ((unsigned (*)(id, SEL, id)) objc_msgSend)(dock_spaces, @selector(displayIDForSpace:), dest_space);
id dest_display_space = display_space_for_display_uuid(dest_display_uuid);
if (source_prev_space_id) {
NSArray *ns_source_space = @[ @(source_space_id) ];
NSArray *ns_dest_space = @[ @(source_prev_space_id) ];
id new_source_space = space_for_display_with_id(source_display_uuid, source_prev_space_id);
SLSShowSpaces(SLSMainConnectionID(), (__bridge CFArrayRef) ns_dest_space);
SLSHideSpaces(SLSMainConnectionID(), (__bridge CFArrayRef) ns_source_space);
SLSManagedDisplaySetCurrentSpace(SLSMainConnectionID(), source_display_uuid, source_prev_space_id);
set_ivar_value(source_display_space, "_currentSpace", [new_source_space retain]);
[ns_dest_space release];
[ns_source_space release];
}
asm__call_move_space(source_space, dest_space, dest_display_uuid, dock_spaces, move_space_fp);
dispatch_sync(dispatch_get_main_queue(), ^{
((void (*)(id, SEL, id, unsigned, CFStringRef)) objc_msgSend)(dp_desktop_picture_manager, @selector(moveSpace:toDisplay:displayUUID:), source_space, dest_display_id, dest_display_uuid);
});
if (focus_dest_space) {
uint64_t new_source_space_id = SLSManagedDisplayGetCurrentSpace(SLSMainConnectionID(), source_display_uuid);
id new_source_space = space_for_display_with_id(source_display_uuid, new_source_space_id);
set_ivar_value(source_display_space, "_currentSpace", [new_source_space retain]);
NSArray *ns_dest_monitor_space = @[ @(dest_space_id) ];
SLSHideSpaces(SLSMainConnectionID(), (__bridge CFArrayRef) ns_dest_monitor_space);
SLSManagedDisplaySetCurrentSpace(SLSMainConnectionID(), dest_display_uuid, source_space_id);
set_ivar_value(dest_display_space, "_currentSpace", [source_space retain]);
[ns_dest_monitor_space release];
}
CFRelease(source_display_uuid);
CFRelease(dest_display_uuid);
}
typedef void (*remove_space_call)(id space, id display_space, id dock_spaces, uint64_t space_id1, uint64_t space_id2);
static void do_space_destroy(char *message)
{
if (dock_spaces == nil || remove_space_fp == 0) return;
uint64_t space_id;
unpack(space_id);
CFStringRef display_uuid = SLSCopyManagedDisplayForSpace(SLSMainConnectionID(), space_id);
uint64_t active_space_id = SLSManagedDisplayGetCurrentSpace(SLSMainConnectionID(), display_uuid);
id space = space_for_display_with_id(display_uuid, space_id);
id display_space = display_space_for_display_uuid(display_uuid);
dispatch_sync(dispatch_get_main_queue(), ^{
((remove_space_call) remove_space_fp)(space, display_space, dock_spaces, space_id, space_id);
});
if (active_space_id == space_id) {
uint64_t dest_space_id = SLSManagedDisplayGetCurrentSpace(SLSMainConnectionID(), display_uuid);
id dest_space = space_for_display_with_id(display_uuid, dest_space_id);
set_ivar_value(display_space, "_currentSpace", [dest_space retain]);
}
CFRelease(display_uuid);
}
static void do_space_create(char *message)
{
if (dock_spaces == nil || add_space_fp == 0) return;
uint64_t space_id;
unpack(space_id);
CFStringRef __block display_uuid = SLSCopyManagedDisplayForSpace(SLSMainConnectionID(), space_id);
dispatch_sync(dispatch_get_main_queue(), ^{
id new_space = macOSSequoia
? [[objc_getClass("ManagedSpace") alloc] init]
: [[objc_getClass("Dock.ManagedSpace") alloc] init];
id display_space = display_space_for_display_uuid(display_uuid);
asm__call_add_space(new_space, display_space, add_space_fp);
CFRelease(display_uuid);
});
}
static void do_space_focus(char *message)
{
if (dock_spaces == nil) return;
uint64_t dest_space_id;
unpack(dest_space_id);
if (dest_space_id) {
CFStringRef dest_display = SLSCopyManagedDisplayForSpace(SLSMainConnectionID(), dest_space_id);
id source_space = macOSSequoia
? ((id (*)(id, SEL, CFStringRef)) objc_msgSend)(dock_spaces, @selector(currentSpaceForDisplayUUID:), dest_display)
: ((id (*)(id, SEL, CFStringRef)) objc_msgSend)(dock_spaces, @selector(currentSpaceforDisplayUUID:), dest_display);
uint64_t source_space_id = get_space_id(source_space);
if (source_space_id != dest_space_id) {
id dest_space = space_for_display_with_id(dest_display, dest_space_id);
if (dest_space != nil) {
id display_space = display_space_for_space_with_id(source_space_id);
if (display_space != nil) {
NSArray *ns_source_space = @[ @(source_space_id) ];
NSArray *ns_dest_space = @[ @(dest_space_id) ];
SLSShowSpaces(SLSMainConnectionID(), (__bridge CFArrayRef) ns_dest_space);
SLSHideSpaces(SLSMainConnectionID(), (__bridge CFArrayRef) ns_source_space);
SLSManagedDisplaySetCurrentSpace(SLSMainConnectionID(), dest_display, dest_space_id);
set_ivar_value(display_space, "_currentSpace", [dest_space retain]);
[ns_dest_space release];
[ns_source_space release];
}
}
}
CFRelease(dest_display);
}
}
static void do_window_scale(char *message)
{
uint32_t wid;
unpack(wid);
if (!wid) return;
CGRect frame = {};
SLSGetWindowBounds(SLSMainConnectionID(), wid, &frame);
CGAffineTransform original_transform = CGAffineTransformMakeTranslation(-frame.origin.x, -frame.origin.y);
CGAffineTransform current_transform;
SLSGetWindowTransform(SLSMainConnectionID(), wid, ¤t_transform);
if (CGAffineTransformEqualToTransform(current_transform, original_transform)) {
float dx, dy, dw, dh;
unpack(dx);
unpack(dy);
unpack(dw);
unpack(dh);
int target_width = dw / 4;
int target_height = target_width / (frame.size.width/frame.size.height);
float x_scale = frame.size.width/target_width;
float y_scale = frame.size.height/target_height;
CGFloat transformed_x = -(dx+dw) + (frame.size.width * (1/x_scale));
CGFloat transformed_y = -dy;
CGAffineTransform scale = CGAffineTransformConcat(CGAffineTransformIdentity, CGAffineTransformMakeScale(x_scale, y_scale));
CGAffineTransform transform = CGAffineTransformTranslate(scale, transformed_x, transformed_y);
SLSSetWindowTransform(SLSMainConnectionID(), wid, transform);
} else {
SLSSetWindowTransform(SLSMainConnectionID(), wid, original_transform);
}
}
static void do_window_move(char *message)
{
uint32_t wid;
unpack(wid);
if (!wid) return;
int x, y;
unpack(x);
unpack(y);
CGPoint point = CGPointMake(x, y);
SLSMoveWindowWithGroup(SLSMainConnectionID(), wid, &point);
NSArray *window_list = @[ @(wid) ];
SLSReassociateWindowsSpacesByGeometry(SLSMainConnectionID(), (__bridge CFArrayRef) window_list);
[window_list release];
}
static void do_window_opacity(char *message)
{
uint32_t wid;
unpack(wid);
if (!wid) return;
float alpha;
unpack(alpha);
pthread_mutex_lock(&window_fade_lock);
struct window_fade_context *context = table_find(&window_fade_table, &wid);
if (context) {
context->alpha = alpha;
context->duration = 0.0f;
__asm__ __volatile__ ("" ::: "memory");
context->skip = true;
pthread_mutex_unlock(&window_fade_lock);
} else {
SLSSetWindowAlpha(SLSMainConnectionID(), wid, alpha);
pthread_mutex_unlock(&window_fade_lock);
}
}
static void *window_fade_thread_proc(void *data)
{
entry:;
struct window_fade_context *context = (struct window_fade_context *) data;
context->skip = false;
float start_alpha;
float end_alpha = context->alpha;
SLSGetWindowAlpha(SLSMainConnectionID(), context->wid, &start_alpha);
int frame_duration = 8;
int total_duration = (int)(context->duration * 1000.0f);
int frame_count = (int)(((float) total_duration / (float) frame_duration) + 1.0f);
for (int frame_index = 1; frame_index <= frame_count; ++frame_index) {
if (context->skip) goto entry;
float t = (float) frame_index / (float) frame_count;
if (t < 0.0f) t = 0.0f;
if (t > 1.0f) t = 1.0f;
float alpha = lerp(start_alpha, t, end_alpha);
SLSSetWindowAlpha(SLSMainConnectionID(), context->wid, alpha);
usleep(frame_duration*1000);
}
pthread_mutex_lock(&window_fade_lock);
if (!context->skip) {
table_remove(&window_fade_table, &context->wid);
pthread_mutex_unlock(&window_fade_lock);
free(context);
return NULL;
}
pthread_mutex_unlock(&window_fade_lock);
goto entry;
}
static void do_window_opacity_fade(char *message)
{
uint32_t wid;
unpack(wid);
if (!wid) return;
float alpha, duration;
unpack(alpha);
unpack(duration);
pthread_mutex_lock(&window_fade_lock);
struct window_fade_context *context = table_find(&window_fade_table, &wid);
if (context) {
context->alpha = alpha;
context->duration = duration;
__asm__ __volatile__ ("" ::: "memory");
context->skip = true;
pthread_mutex_unlock(&window_fade_lock);
} else {
context = malloc(sizeof(struct window_fade_context));
context->wid = wid;
context->alpha = alpha;
context->duration = duration;
context->skip = false;
__asm__ __volatile__ ("" ::: "memory");
table_add(&window_fade_table, &wid, context);
pthread_mutex_unlock(&window_fade_lock);
pthread_create(&context->thread, NULL, &window_fade_thread_proc, context);
pthread_detach(context->thread);
}
}
static void do_window_layer(char *message)
{
uint32_t wid;
unpack(wid);
if (!wid) return;
int layer;
unpack(layer);
SLSSetWindowSubLevel(SLSMainConnectionID(), wid, CGWindowLevelForKey(layer));
}
static void do_window_sticky(char *message)
{
uint32_t wid;
unpack(wid);
if (!wid) return;
bool value;
unpack(value);
uint64_t tags = (1 << 11);
if (value == 1) {
SLSSetWindowTags(SLSMainConnectionID(), wid, &tags, 64);
} else {
SLSClearWindowTags(SLSMainConnectionID(), wid, &tags, 64);
}
}
typedef void (*focus_window_call)(ProcessSerialNumber psn, uint32_t wid);
static void do_window_focus(char *message)
{
if (set_front_window_fp == 0) return;
int window_connection;
ProcessSerialNumber window_psn;
uint32_t wid;
unpack(wid);
SLSGetWindowOwner(SLSMainConnectionID(), wid, &window_connection);
SLSGetConnectionPSN(SLSMainConnectionID(), &window_psn);
((focus_window_call) set_front_window_fp)(window_psn, wid);
}
static void do_window_shadow(char *message)
{
uint32_t wid;
unpack(wid);
if (!wid) return;
bool value;
unpack(value);
uint64_t tags = (1 << 3);
if (value == 1) {
SLSClearWindowTags(SLSMainConnectionID(), wid, &tags, 64);
} else {
SLSSetWindowTags(SLSMainConnectionID(), wid, &tags, 64);
}
}
static void do_window_swap_proxy_in(char *message)
{
int count = 0;
unpack(count);
if (!count) return;
CFTypeRef transaction = SLSTransactionCreate(SLSMainConnectionID());
for (int i = 0; i < count; ++i) {
uint32_t wid;
unpack(wid);
if (!wid) continue;
uint32_t proxy_wid;
unpack(proxy_wid);
SLSTransactionOrderWindowGroup(transaction, proxy_wid, 1, wid);
SLSTransactionSetWindowSystemAlpha(transaction, wid, 0);
}
SLSTransactionCommit(transaction, 0);
CFRelease(transaction);
}
static void do_window_swap_proxy_out(char *message)
{
int count = 0;
unpack(count);
if (!count) return;
CFTypeRef transaction = SLSTransactionCreate(SLSMainConnectionID());
for (int i = 0; i < count; ++i) {
uint32_t wid;
unpack(wid);
if (!wid) continue;
uint32_t proxy_wid;
unpack(proxy_wid);
SLSTransactionSetWindowSystemAlpha(transaction, wid, 1.0f);
SLSTransactionOrderWindowGroup(transaction, proxy_wid, 0, wid);
}
SLSTransactionCommit(transaction, 0);
CFRelease(transaction);
}
static void do_window_order(char *message)
{
uint32_t a_wid;
unpack(a_wid);
if (!a_wid) return;
int order;
unpack(order);
uint32_t b_wid;
unpack(b_wid);
SLSOrderWindow(SLSMainConnectionID(), a_wid, order, b_wid);
}
static void do_window_order_in(char *message)
{
int count = 0;
unpack(count);
if (!count) return;
CFTypeRef transaction = SLSTransactionCreate(SLSMainConnectionID());
for (int i = 0; i < count; ++i) {
uint32_t wid;
unpack(wid);
if (!wid) continue;
SLSTransactionOrderWindowGroup(transaction, wid, 1, 0);
}
SLSTransactionCommit(transaction, 0);
CFRelease(transaction);
}
static inline CFArrayRef cfarray_of_cfnumbers(void *values, size_t size, int count, CFNumberType type)
{
CFNumberRef temp[count];
for (int i = 0; i < count; ++i) {
temp[i] = CFNumberCreate(NULL, type, ((char *)values) + (size * i));
}
CFArrayRef result = CFArrayCreate(NULL, (const void **)temp, count, &kCFTypeArrayCallBacks);
for (int i = 0; i < count; ++i) {
CFRelease(temp[i]);
}
return result;
}
static void do_window_list_move_to_space(char *message)
{
uint64_t sid;
unpack(sid);
int count = 0;
unpack(count);
CFArrayRef window_list_ref = cfarray_of_cfnumbers((uint32_t*)message, sizeof(uint32_t), count, kCFNumberSInt32Type);
SLSMoveWindowsToManagedSpace(SLSMainConnectionID(), window_list_ref, sid);
CFRelease(window_list_ref);
}
static void do_window_move_to_space(char *message)
{
uint64_t sid;
unpack(sid);
uint32_t wid;
unpack(wid);
CFArrayRef window_list_ref = cfarray_of_cfnumbers(&wid, sizeof(uint32_t), 1, kCFNumberSInt32Type);
SLSMoveWindowsToManagedSpace(SLSMainConnectionID(), window_list_ref, sid);
CFRelease(window_list_ref);
}
static void do_handshake(int sockfd)
{
uint32_t attrib = 0;
if (dock_spaces != nil) attrib |= OSAX_ATTRIB_DOCK_SPACES;
if (dp_desktop_picture_manager != nil) attrib |= OSAX_ATTRIB_DPPM;
if (add_space_fp) attrib |= OSAX_ATTRIB_ADD_SPACE;
if (remove_space_fp) attrib |= OSAX_ATTRIB_REM_SPACE;
if (move_space_fp) attrib |= OSAX_ATTRIB_MOV_SPACE;
if (set_front_window_fp) attrib |= OSAX_ATTRIB_SET_WINDOW;
if (animation_time_addr) attrib |= OSAX_ATTRIB_ANIM_TIME;
char bytes[BUFSIZ] = {};
int version_length = strlen(OSAX_VERSION);
int attrib_length = sizeof(uint32_t);
int bytes_length = version_length + 1 + attrib_length;
memcpy(bytes, OSAX_VERSION, version_length);
memcpy(bytes + version_length + 1, &attrib, attrib_length);
bytes[version_length] = '\0';
bytes[bytes_length] = '\n';
send(sockfd, bytes, bytes_length+1, 0);
}
static void handle_message(int sockfd, char *message)
{
enum sa_opcode op = *message++;
switch (op) {
case SA_OPCODE_HANDSHAKE: {
do_handshake(sockfd);
} break;
case SA_OPCODE_SPACE_FOCUS: {
do_space_focus(message);
} break;
case SA_OPCODE_SPACE_CREATE: {
do_space_create(message);
} break;
case SA_OPCODE_SPACE_DESTROY: {
do_space_destroy(message);
} break;
case SA_OPCODE_SPACE_MOVE: {
do_space_move(message);
} break;
case SA_OPCODE_WINDOW_MOVE: {
do_window_move(message);
} break;
case SA_OPCODE_WINDOW_OPACITY: {
do_window_opacity(message);
} break;
case SA_OPCODE_WINDOW_OPACITY_FADE: {
do_window_opacity_fade(message);
} break;
case SA_OPCODE_WINDOW_LAYER: {
do_window_layer(message);
} break;
case SA_OPCODE_WINDOW_STICKY: {
do_window_sticky(message);
} break;
case SA_OPCODE_WINDOW_SHADOW: {
do_window_shadow(message);
} break;
case SA_OPCODE_WINDOW_FOCUS: {
do_window_focus(message);
} break;
case SA_OPCODE_WINDOW_SCALE: {
do_window_scale(message);
} break;
case SA_OPCODE_WINDOW_SWAP_PROXY_IN: {
do_window_swap_proxy_in(message);
} break;
case SA_OPCODE_WINDOW_SWAP_PROXY_OUT: {
do_window_swap_proxy_out(message);
} break;
case SA_OPCODE_WINDOW_ORDER: {
do_window_order(message);
} break;
case SA_OPCODE_WINDOW_ORDER_IN: {