forked from reddit/rpan-studio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobs-cocoa.m
1806 lines (1602 loc) · 41.9 KB
/
obs-cocoa.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
/******************************************************************************
Copyright (C) 2013 by Ruwen Hahn <palana@stunned.de>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
#include "util/platform.h"
#include "util/dstr.h"
#include "obs.h"
#include "obs-internal.h"
#include <unistd.h>
#include <sys/types.h>
#include <sys/sysctl.h>
#include <objc/objc.h>
#include <Carbon/Carbon.h>
#include <IOKit/hid/IOHIDDevice.h>
#include <IOKit/hid/IOHIDManager.h>
#import <AppKit/AppKit.h>
bool is_in_bundle()
{
NSRunningApplication *app = [NSRunningApplication currentApplication];
return [app bundleIdentifier] != nil;
}
const char *get_module_extension(void)
{
return ".so";
}
static const char *module_bin[] = {
"../obs-plugins",
OBS_INSTALL_PREFIX "obs-plugins",
};
static const char *module_data[] = {
"../data/obs-plugins/%module%",
OBS_INSTALL_DATA_PATH "obs-plugins/%module%",
};
static const int module_patterns_size =
sizeof(module_bin) / sizeof(module_bin[0]);
void add_default_module_paths(void)
{
for (int i = 0; i < module_patterns_size; i++)
obs_add_module_path(module_bin[i], module_data[i]);
if (is_in_bundle()) {
NSRunningApplication *app =
[NSRunningApplication currentApplication];
NSURL *bundleURL = [app bundleURL];
NSURL *pluginsURL = [bundleURL
URLByAppendingPathComponent:@"Contents/PlugIns"];
NSURL *dataURL = [bundleURL
URLByAppendingPathComponent:
@"Contents/Resources/data/obs-plugins/%module%"];
const char *binPath = [[pluginsURL path]
cStringUsingEncoding:NSUTF8StringEncoding];
const char *dataPath = [[dataURL path]
cStringUsingEncoding:NSUTF8StringEncoding];
obs_add_module_path(binPath, dataPath);
}
}
char *find_libobs_data_file(const char *file)
{
struct dstr path;
if (is_in_bundle()) {
NSRunningApplication *app =
[NSRunningApplication currentApplication];
NSURL *bundleURL = [app bundleURL];
NSURL *libobsDataURL =
[bundleURL URLByAppendingPathComponent:
@"Contents/Resources/data/libobs/"];
const char *libobsDataPath = [[libobsDataURL path]
cStringUsingEncoding:NSUTF8StringEncoding];
dstr_init_copy(&path, libobsDataPath);
dstr_cat(&path, "/");
} else {
dstr_init_copy(&path, OBS_INSTALL_DATA_PATH "/libobs/");
}
dstr_cat(&path, file);
return path.array;
}
static void log_processor_name(void)
{
char *name = NULL;
size_t size;
int ret;
ret = sysctlbyname("machdep.cpu.brand_string", NULL, &size, NULL, 0);
if (ret != 0)
return;
name = malloc(size);
ret = sysctlbyname("machdep.cpu.brand_string", name, &size, NULL, 0);
if (ret == 0)
blog(LOG_INFO, "CPU Name: %s", name);
free(name);
}
static void log_processor_speed(void)
{
size_t size;
long long freq;
int ret;
size = sizeof(freq);
ret = sysctlbyname("hw.cpufrequency", &freq, &size, NULL, 0);
if (ret == 0)
blog(LOG_INFO, "CPU Speed: %lldMHz", freq / 1000000);
}
static void log_processor_cores(void)
{
blog(LOG_INFO, "Physical Cores: %d, Logical Cores: %d",
os_get_physical_cores(), os_get_logical_cores());
}
static void log_available_memory(void)
{
size_t size;
long long memory_available;
int ret;
size = sizeof(memory_available);
ret = sysctlbyname("hw.memsize", &memory_available, &size, NULL, 0);
if (ret == 0)
blog(LOG_INFO, "Physical Memory: %lldMB Total",
memory_available / 1024 / 1024);
}
static void log_os_name(id pi, SEL UTF8StringSel)
{
typedef int (*os_func)(id, SEL);
os_func operatingSystem = (os_func)objc_msgSend;
unsigned long os_id = (unsigned long)operatingSystem(
pi, sel_registerName("operatingSystem"));
typedef id (*os_name_func)(id, SEL);
os_name_func operatingSystemName = (os_name_func)objc_msgSend;
id os = operatingSystemName(pi,
sel_registerName("operatingSystemName"));
typedef const char *(*utf8_func)(id, SEL);
utf8_func UTF8String = (utf8_func)objc_msgSend;
const char *name = UTF8String(os, UTF8StringSel);
if (os_id == 5 /*NSMACHOperatingSystem*/) {
blog(LOG_INFO, "OS Name: Mac OS X (%s)", name);
return;
}
blog(LOG_INFO, "OS Name: %s", name ? name : "Unknown");
}
static bool using_10_15_or_above = true;
static void log_os_version(id pi, SEL UTF8StringSel)
{
typedef id (*version_func)(id, SEL);
version_func operatingSystemVersionString = (version_func)objc_msgSend;
id vs = operatingSystemVersionString(
pi, sel_registerName("operatingSystemVersionString"));
typedef const char *(*utf8_func)(id, SEL);
utf8_func UTF8String = (utf8_func)objc_msgSend;
const char *version = UTF8String(vs, UTF8StringSel);
blog(LOG_INFO, "OS Version: %s", version ? version : "Unknown");
if (version) {
int major;
int minor;
int count = sscanf(version, "Version %d.%d", &major, &minor);
if (count == 2 && major == 10) {
using_10_15_or_above = minor >= 15;
}
}
}
static void log_os(void)
{
Class NSProcessInfo = objc_getClass("NSProcessInfo");
typedef id (*func)(id, SEL);
func processInfo = (func)objc_msgSend;
id pi = processInfo((id)NSProcessInfo, sel_registerName("processInfo"));
SEL UTF8String = sel_registerName("UTF8String");
log_os_name(pi, UTF8String);
log_os_version(pi, UTF8String);
}
static void log_kernel_version(void)
{
char kernel_version[1024];
size_t size = sizeof(kernel_version);
int ret;
ret = sysctlbyname("kern.osrelease", kernel_version, &size, NULL, 0);
if (ret == 0)
blog(LOG_INFO, "Kernel Version: %s", kernel_version);
}
void log_system_info(void)
{
log_processor_name();
log_processor_speed();
log_processor_cores();
log_available_memory();
log_os();
log_kernel_version();
}
static bool dstr_from_cfstring(struct dstr *str, CFStringRef ref)
{
CFIndex length = CFStringGetLength(ref);
CFIndex max_size = CFStringGetMaximumSizeForEncoding(
length, kCFStringEncodingUTF8);
dstr_reserve(str, max_size);
if (!CFStringGetCString(ref, str->array, max_size,
kCFStringEncodingUTF8))
return false;
str->len = strlen(str->array);
return true;
}
struct obs_hotkeys_platform {
volatile long refs;
bool secure_input_activated;
TISInputSourceRef tis;
CFDataRef layout_data;
UCKeyboardLayout *layout;
IOHIDManagerRef manager;
DARRAY(IOHIDElementRef) keys[OBS_KEY_LAST_VALUE];
};
static void hotkeys_retain(struct obs_hotkeys_platform *plat)
{
os_atomic_inc_long(&plat->refs);
}
static inline void free_hotkeys_platform(obs_hotkeys_platform_t *plat);
static void hotkeys_release(struct obs_hotkeys_platform *plat)
{
if (os_atomic_dec_long(&plat->refs) == -1)
free_hotkeys_platform(plat);
}
#define INVALID_KEY 0xff
int obs_key_to_virtual_key(obs_key_t code)
{
switch (code) {
case OBS_KEY_A:
return kVK_ANSI_A;
case OBS_KEY_B:
return kVK_ANSI_B;
case OBS_KEY_C:
return kVK_ANSI_C;
case OBS_KEY_D:
return kVK_ANSI_D;
case OBS_KEY_E:
return kVK_ANSI_E;
case OBS_KEY_F:
return kVK_ANSI_F;
case OBS_KEY_G:
return kVK_ANSI_G;
case OBS_KEY_H:
return kVK_ANSI_H;
case OBS_KEY_I:
return kVK_ANSI_I;
case OBS_KEY_J:
return kVK_ANSI_J;
case OBS_KEY_K:
return kVK_ANSI_K;
case OBS_KEY_L:
return kVK_ANSI_L;
case OBS_KEY_M:
return kVK_ANSI_M;
case OBS_KEY_N:
return kVK_ANSI_N;
case OBS_KEY_O:
return kVK_ANSI_O;
case OBS_KEY_P:
return kVK_ANSI_P;
case OBS_KEY_Q:
return kVK_ANSI_Q;
case OBS_KEY_R:
return kVK_ANSI_R;
case OBS_KEY_S:
return kVK_ANSI_S;
case OBS_KEY_T:
return kVK_ANSI_T;
case OBS_KEY_U:
return kVK_ANSI_U;
case OBS_KEY_V:
return kVK_ANSI_V;
case OBS_KEY_W:
return kVK_ANSI_W;
case OBS_KEY_X:
return kVK_ANSI_X;
case OBS_KEY_Y:
return kVK_ANSI_Y;
case OBS_KEY_Z:
return kVK_ANSI_Z;
case OBS_KEY_1:
return kVK_ANSI_1;
case OBS_KEY_2:
return kVK_ANSI_2;
case OBS_KEY_3:
return kVK_ANSI_3;
case OBS_KEY_4:
return kVK_ANSI_4;
case OBS_KEY_5:
return kVK_ANSI_5;
case OBS_KEY_6:
return kVK_ANSI_6;
case OBS_KEY_7:
return kVK_ANSI_7;
case OBS_KEY_8:
return kVK_ANSI_8;
case OBS_KEY_9:
return kVK_ANSI_9;
case OBS_KEY_0:
return kVK_ANSI_0;
case OBS_KEY_RETURN:
return kVK_Return;
case OBS_KEY_ESCAPE:
return kVK_Escape;
case OBS_KEY_BACKSPACE:
return kVK_Delete;
case OBS_KEY_TAB:
return kVK_Tab;
case OBS_KEY_SPACE:
return kVK_Space;
case OBS_KEY_MINUS:
return kVK_ANSI_Minus;
case OBS_KEY_EQUAL:
return kVK_ANSI_Equal;
case OBS_KEY_BRACKETLEFT:
return kVK_ANSI_LeftBracket;
case OBS_KEY_BRACKETRIGHT:
return kVK_ANSI_RightBracket;
case OBS_KEY_BACKSLASH:
return kVK_ANSI_Backslash;
case OBS_KEY_SEMICOLON:
return kVK_ANSI_Semicolon;
case OBS_KEY_QUOTE:
return kVK_ANSI_Quote;
case OBS_KEY_DEAD_GRAVE:
return kVK_ANSI_Grave;
case OBS_KEY_COMMA:
return kVK_ANSI_Comma;
case OBS_KEY_PERIOD:
return kVK_ANSI_Period;
case OBS_KEY_SLASH:
return kVK_ANSI_Slash;
case OBS_KEY_CAPSLOCK:
return kVK_CapsLock;
case OBS_KEY_SECTION:
return kVK_ISO_Section;
case OBS_KEY_F1:
return kVK_F1;
case OBS_KEY_F2:
return kVK_F2;
case OBS_KEY_F3:
return kVK_F3;
case OBS_KEY_F4:
return kVK_F4;
case OBS_KEY_F5:
return kVK_F5;
case OBS_KEY_F6:
return kVK_F6;
case OBS_KEY_F7:
return kVK_F7;
case OBS_KEY_F8:
return kVK_F8;
case OBS_KEY_F9:
return kVK_F9;
case OBS_KEY_F10:
return kVK_F10;
case OBS_KEY_F11:
return kVK_F11;
case OBS_KEY_F12:
return kVK_F12;
case OBS_KEY_HELP:
return kVK_Help;
case OBS_KEY_HOME:
return kVK_Home;
case OBS_KEY_PAGEUP:
return kVK_PageUp;
case OBS_KEY_DELETE:
return kVK_ForwardDelete;
case OBS_KEY_END:
return kVK_End;
case OBS_KEY_PAGEDOWN:
return kVK_PageDown;
case OBS_KEY_RIGHT:
return kVK_RightArrow;
case OBS_KEY_LEFT:
return kVK_LeftArrow;
case OBS_KEY_DOWN:
return kVK_DownArrow;
case OBS_KEY_UP:
return kVK_UpArrow;
case OBS_KEY_CLEAR:
return kVK_ANSI_KeypadClear;
case OBS_KEY_NUMSLASH:
return kVK_ANSI_KeypadDivide;
case OBS_KEY_NUMASTERISK:
return kVK_ANSI_KeypadMultiply;
case OBS_KEY_NUMMINUS:
return kVK_ANSI_KeypadMinus;
case OBS_KEY_NUMPLUS:
return kVK_ANSI_KeypadPlus;
case OBS_KEY_ENTER:
return kVK_ANSI_KeypadEnter;
case OBS_KEY_NUM1:
return kVK_ANSI_Keypad1;
case OBS_KEY_NUM2:
return kVK_ANSI_Keypad2;
case OBS_KEY_NUM3:
return kVK_ANSI_Keypad3;
case OBS_KEY_NUM4:
return kVK_ANSI_Keypad4;
case OBS_KEY_NUM5:
return kVK_ANSI_Keypad5;
case OBS_KEY_NUM6:
return kVK_ANSI_Keypad6;
case OBS_KEY_NUM7:
return kVK_ANSI_Keypad7;
case OBS_KEY_NUM8:
return kVK_ANSI_Keypad8;
case OBS_KEY_NUM9:
return kVK_ANSI_Keypad9;
case OBS_KEY_NUM0:
return kVK_ANSI_Keypad0;
case OBS_KEY_NUMPERIOD:
return kVK_ANSI_KeypadDecimal;
case OBS_KEY_NUMEQUAL:
return kVK_ANSI_KeypadEquals;
case OBS_KEY_F13:
return kVK_F13;
case OBS_KEY_F14:
return kVK_F14;
case OBS_KEY_F15:
return kVK_F15;
case OBS_KEY_F16:
return kVK_F16;
case OBS_KEY_F17:
return kVK_F17;
case OBS_KEY_F18:
return kVK_F18;
case OBS_KEY_F19:
return kVK_F19;
case OBS_KEY_F20:
return kVK_F20;
case OBS_KEY_CONTROL:
return kVK_Control;
case OBS_KEY_SHIFT:
return kVK_Shift;
case OBS_KEY_ALT:
return kVK_Option;
case OBS_KEY_META:
return kVK_Command;
//case OBS_KEY_CONTROL: return kVK_RightControl;
//case OBS_KEY_SHIFT: return kVK_RightShift;
//case OBS_KEY_ALT: return kVK_RightOption;
//case OBS_KEY_META: return 0x36;
case OBS_KEY_NONE:
case OBS_KEY_LAST_VALUE:
default:
break;
}
return INVALID_KEY;
}
static bool localized_key_to_str(obs_key_t key, struct dstr *str)
{
#define MAP_KEY(k, s) \
case k: \
dstr_copy(str, obs_get_hotkey_translation(k, s)); \
return true
#define MAP_BUTTON(i) \
case OBS_KEY_MOUSE##i: \
dstr_copy(str, obs_get_hotkey_translation(key, "Mouse " #i)); \
return true
switch (key) {
MAP_KEY(OBS_KEY_SPACE, "Space");
MAP_KEY(OBS_KEY_NUMEQUAL, "= (Keypad)");
MAP_KEY(OBS_KEY_NUMASTERISK, "* (Keypad)");
MAP_KEY(OBS_KEY_NUMPLUS, "+ (Keypad)");
MAP_KEY(OBS_KEY_NUMMINUS, "- (Keypad)");
MAP_KEY(OBS_KEY_NUMPERIOD, ". (Keypad)");
MAP_KEY(OBS_KEY_NUMSLASH, "/ (Keypad)");
MAP_KEY(OBS_KEY_NUM0, "0 (Keypad)");
MAP_KEY(OBS_KEY_NUM1, "1 (Keypad)");
MAP_KEY(OBS_KEY_NUM2, "2 (Keypad)");
MAP_KEY(OBS_KEY_NUM3, "3 (Keypad)");
MAP_KEY(OBS_KEY_NUM4, "4 (Keypad)");
MAP_KEY(OBS_KEY_NUM5, "5 (Keypad)");
MAP_KEY(OBS_KEY_NUM6, "6 (Keypad)");
MAP_KEY(OBS_KEY_NUM7, "7 (Keypad)");
MAP_KEY(OBS_KEY_NUM8, "8 (Keypad)");
MAP_KEY(OBS_KEY_NUM9, "9 (Keypad)");
MAP_BUTTON(1);
MAP_BUTTON(2);
MAP_BUTTON(3);
MAP_BUTTON(4);
MAP_BUTTON(5);
MAP_BUTTON(6);
MAP_BUTTON(7);
MAP_BUTTON(8);
MAP_BUTTON(9);
MAP_BUTTON(10);
MAP_BUTTON(11);
MAP_BUTTON(12);
MAP_BUTTON(13);
MAP_BUTTON(14);
MAP_BUTTON(15);
MAP_BUTTON(16);
MAP_BUTTON(17);
MAP_BUTTON(18);
MAP_BUTTON(19);
MAP_BUTTON(20);
MAP_BUTTON(21);
MAP_BUTTON(22);
MAP_BUTTON(23);
MAP_BUTTON(24);
MAP_BUTTON(25);
MAP_BUTTON(26);
MAP_BUTTON(27);
MAP_BUTTON(28);
MAP_BUTTON(29);
default:
break;
}
#undef MAP_BUTTON
#undef MAP_KEY
return false;
}
static bool code_to_str(int code, struct dstr *str)
{
#define MAP_GLYPH(c, g) \
case c: \
dstr_from_wcs(str, (wchar_t[]){g, 0}); \
return true
#define MAP_STR(c, s) \
case c: \
dstr_copy(str, s); \
return true
switch (code) {
MAP_GLYPH(kVK_Return, 0x21A9);
MAP_GLYPH(kVK_Escape, 0x238B);
MAP_GLYPH(kVK_Delete, 0x232B);
MAP_GLYPH(kVK_Tab, 0x21e5);
MAP_GLYPH(kVK_CapsLock, 0x21EA);
MAP_GLYPH(kVK_ANSI_KeypadClear, 0x2327);
MAP_GLYPH(kVK_ANSI_KeypadEnter, 0x2305);
MAP_GLYPH(kVK_Help, 0x003F);
MAP_GLYPH(kVK_Home, 0x2196);
MAP_GLYPH(kVK_PageUp, 0x21de);
MAP_GLYPH(kVK_ForwardDelete, 0x2326);
MAP_GLYPH(kVK_End, 0x2198);
MAP_GLYPH(kVK_PageDown, 0x21df);
MAP_GLYPH(kVK_RightArrow, 0x2192);
MAP_GLYPH(kVK_LeftArrow, 0x2190);
MAP_GLYPH(kVK_DownArrow, 0x2193);
MAP_GLYPH(kVK_UpArrow, 0x2191);
MAP_STR(kVK_F1, "F1");
MAP_STR(kVK_F2, "F2");
MAP_STR(kVK_F3, "F3");
MAP_STR(kVK_F4, "F4");
MAP_STR(kVK_F5, "F5");
MAP_STR(kVK_F6, "F6");
MAP_STR(kVK_F7, "F7");
MAP_STR(kVK_F8, "F8");
MAP_STR(kVK_F9, "F9");
MAP_STR(kVK_F10, "F10");
MAP_STR(kVK_F11, "F11");
MAP_STR(kVK_F12, "F12");
MAP_STR(kVK_F13, "F13");
MAP_STR(kVK_F14, "F14");
MAP_STR(kVK_F15, "F15");
MAP_STR(kVK_F16, "F16");
MAP_STR(kVK_F17, "F17");
MAP_STR(kVK_F18, "F18");
MAP_STR(kVK_F19, "F19");
MAP_STR(kVK_F20, "F20");
MAP_GLYPH(kVK_Control, kControlUnicode);
MAP_GLYPH(kVK_Shift, kShiftUnicode);
MAP_GLYPH(kVK_Option, kOptionUnicode);
MAP_GLYPH(kVK_Command, kCommandUnicode);
MAP_GLYPH(kVK_RightControl, kControlUnicode);
MAP_GLYPH(kVK_RightShift, kShiftUnicode);
MAP_GLYPH(kVK_RightOption, kOptionUnicode);
}
#undef MAP_STR
#undef MAP_GLYPH
return false;
}
void obs_key_to_str(obs_key_t key, struct dstr *str)
{
if (localized_key_to_str(key, str))
return;
int code = obs_key_to_virtual_key(key);
if (code_to_str(code, str))
return;
if (code == INVALID_KEY) {
blog(LOG_ERROR,
"hotkey-cocoa: Got invalid key while "
"translating key '%d' (%s)",
key, obs_key_to_name(key));
goto err;
}
struct obs_hotkeys_platform *plat = NULL;
if (obs) {
pthread_mutex_lock(&obs->hotkeys.mutex);
plat = obs->hotkeys.platform_context;
hotkeys_retain(plat);
pthread_mutex_unlock(&obs->hotkeys.mutex);
}
if (!plat) {
blog(LOG_ERROR,
"hotkey-cocoa: Could not get hotkey platform "
"while translating key '%d' (%s)",
key, obs_key_to_name(key));
goto err;
}
const UniCharCount max_length = 16;
UInt32 dead_key_state = 0;
UniChar buffer[max_length];
UniCharCount len = 0;
OSStatus err =
UCKeyTranslate(plat->layout, code, kUCKeyActionDown,
0x104, //caps lock for upper case letters
LMGetKbdType(), kUCKeyTranslateNoDeadKeysBit,
&dead_key_state, max_length, &len, buffer);
if (err == noErr && len <= 0 && dead_key_state) {
err = UCKeyTranslate(plat->layout, kVK_Space, kUCKeyActionDown,
0x104, LMGetKbdType(),
kUCKeyTranslateNoDeadKeysBit,
&dead_key_state, max_length, &len, buffer);
}
hotkeys_release(plat);
if (err != noErr) {
blog(LOG_ERROR,
"hotkey-cocoa: Error while translating key '%d'"
" (0x%x, %s) to string: %d",
key, code, obs_key_to_name(key), err);
goto err;
}
if (len == 0) {
blog(LOG_ERROR,
"hotkey-cocoa: Got 0 length string while "
"translating '%d' (0x%x, %s) to string",
key, code, obs_key_to_name(key));
goto err;
}
CFStringRef string = CFStringCreateWithCharactersNoCopy(
NULL, buffer, len, kCFAllocatorNull);
if (!string) {
blog(LOG_ERROR,
"hotkey-cocoa: Could not create CFStringRef "
"while translating '%d' (0x%x, %s) to string",
key, code, obs_key_to_name(key));
goto err;
}
if (!dstr_from_cfstring(str, string)) {
blog(LOG_ERROR,
"hotkey-cocoa: Could not translate CFStringRef "
"to CString while translating '%d' (0x%x, %s)",
key, code, obs_key_to_name(key));
goto release;
}
CFRelease(string);
return;
release:
CFRelease(string);
err:
dstr_copy(str, obs_key_to_name(key));
}
#define OBS_COCOA_MODIFIER_SIZE 7
static void unichar_to_utf8(const UniChar *c, char *buff)
{
CFStringRef string = CFStringCreateWithCharactersNoCopy(
NULL, c, 2, kCFAllocatorNull);
if (!string) {
blog(LOG_ERROR, "hotkey-cocoa: Could not create CFStringRef "
"while populating modifier strings");
return;
}
if (!CFStringGetCString(string, buff, OBS_COCOA_MODIFIER_SIZE,
kCFStringEncodingUTF8))
blog(LOG_ERROR,
"hotkey-cocoa: Error while populating "
" modifier string with glyph %d (0x%x)",
c[0], c[0]);
CFRelease(string);
}
static char ctrl_str[OBS_COCOA_MODIFIER_SIZE];
static char opt_str[OBS_COCOA_MODIFIER_SIZE];
static char shift_str[OBS_COCOA_MODIFIER_SIZE];
static char cmd_str[OBS_COCOA_MODIFIER_SIZE];
static void init_utf_8_strings(void)
{
const UniChar ctrl_uni[] = {kControlUnicode, 0};
const UniChar opt_uni[] = {kOptionUnicode, 0};
const UniChar shift_uni[] = {kShiftUnicode, 0};
const UniChar cmd_uni[] = {kCommandUnicode, 0};
unichar_to_utf8(ctrl_uni, ctrl_str);
unichar_to_utf8(opt_uni, opt_str);
unichar_to_utf8(shift_uni, shift_str);
unichar_to_utf8(cmd_uni, cmd_str);
}
static pthread_once_t strings_token = PTHREAD_ONCE_INIT;
void obs_key_combination_to_str(obs_key_combination_t key, struct dstr *str)
{
struct dstr key_str = {0};
if (key.key != OBS_KEY_NONE)
obs_key_to_str(key.key, &key_str);
int res = pthread_once(&strings_token, init_utf_8_strings);
if (res) {
blog(LOG_ERROR,
"hotkeys-cocoa: Error while translating "
"modifiers %d (0x%x)",
res, res);
dstr_move(str, &key_str);
return;
}
#define CHECK_MODIFIER(mod, str) ((key.modifiers & mod) ? str : "")
dstr_printf(str, "%s%s%s%s%s",
CHECK_MODIFIER(INTERACT_CONTROL_KEY, ctrl_str),
CHECK_MODIFIER(INTERACT_ALT_KEY, opt_str),
CHECK_MODIFIER(INTERACT_SHIFT_KEY, shift_str),
CHECK_MODIFIER(INTERACT_COMMAND_KEY, cmd_str),
key_str.len ? key_str.array : "");
#undef CHECK_MODIFIER
dstr_free(&key_str);
}
static inline CFDictionaryRef copy_device_mask(UInt32 page, UInt32 usage)
{
CFMutableDictionaryRef dict = CFDictionaryCreateMutable(
kCFAllocatorDefault, 2, &kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
CFNumberRef value;
// Add the page value.
value = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &page);
CFDictionarySetValue(dict, CFSTR(kIOHIDDeviceUsagePageKey), value);
CFRelease(value);
// Add the usage value (which is only valid if page value exists).
value = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &usage);
CFDictionarySetValue(dict, CFSTR(kIOHIDDeviceUsageKey), value);
CFRelease(value);
return dict;
}
static CFSetRef copy_devices(obs_hotkeys_platform_t *plat, UInt32 page,
UInt32 usage)
{
CFDictionaryRef mask = copy_device_mask(page, usage);
IOHIDManagerSetDeviceMatching(plat->manager, mask);
CFRelease(mask);
CFSetRef devices = IOHIDManagerCopyDevices(plat->manager);
if (!devices)
return NULL;
if (CFSetGetCount(devices) < 1) {
CFRelease(devices);
return NULL;
}
return devices;
}
static UInt16 usage_to_carbon(UInt32 usage)
{
switch (usage) {
case kHIDUsage_KeyboardErrorRollOver:
return INVALID_KEY;
case kHIDUsage_KeyboardPOSTFail:
return INVALID_KEY;
case kHIDUsage_KeyboardErrorUndefined:
return INVALID_KEY;
case kHIDUsage_KeyboardA:
return kVK_ANSI_A;
case kHIDUsage_KeyboardB:
return kVK_ANSI_B;
case kHIDUsage_KeyboardC:
return kVK_ANSI_C;
case kHIDUsage_KeyboardD:
return kVK_ANSI_D;
case kHIDUsage_KeyboardE:
return kVK_ANSI_E;
case kHIDUsage_KeyboardF:
return kVK_ANSI_F;
case kHIDUsage_KeyboardG:
return kVK_ANSI_G;
case kHIDUsage_KeyboardH:
return kVK_ANSI_H;
case kHIDUsage_KeyboardI:
return kVK_ANSI_I;
case kHIDUsage_KeyboardJ:
return kVK_ANSI_J;
case kHIDUsage_KeyboardK:
return kVK_ANSI_K;
case kHIDUsage_KeyboardL:
return kVK_ANSI_L;
case kHIDUsage_KeyboardM:
return kVK_ANSI_M;
case kHIDUsage_KeyboardN:
return kVK_ANSI_N;
case kHIDUsage_KeyboardO:
return kVK_ANSI_O;
case kHIDUsage_KeyboardP:
return kVK_ANSI_P;
case kHIDUsage_KeyboardQ:
return kVK_ANSI_Q;
case kHIDUsage_KeyboardR:
return kVK_ANSI_R;
case kHIDUsage_KeyboardS:
return kVK_ANSI_S;
case kHIDUsage_KeyboardT:
return kVK_ANSI_T;
case kHIDUsage_KeyboardU:
return kVK_ANSI_U;
case kHIDUsage_KeyboardV:
return kVK_ANSI_V;
case kHIDUsage_KeyboardW:
return kVK_ANSI_W;
case kHIDUsage_KeyboardX:
return kVK_ANSI_X;
case kHIDUsage_KeyboardY:
return kVK_ANSI_Y;
case kHIDUsage_KeyboardZ:
return kVK_ANSI_Z;
case kHIDUsage_Keyboard1:
return kVK_ANSI_1;
case kHIDUsage_Keyboard2:
return kVK_ANSI_2;
case kHIDUsage_Keyboard3:
return kVK_ANSI_3;
case kHIDUsage_Keyboard4:
return kVK_ANSI_4;
case kHIDUsage_Keyboard5:
return kVK_ANSI_5;
case kHIDUsage_Keyboard6:
return kVK_ANSI_6;
case kHIDUsage_Keyboard7:
return kVK_ANSI_7;
case kHIDUsage_Keyboard8:
return kVK_ANSI_8;
case kHIDUsage_Keyboard9:
return kVK_ANSI_9;
case kHIDUsage_Keyboard0:
return kVK_ANSI_0;
case kHIDUsage_KeyboardReturnOrEnter:
return kVK_Return;
case kHIDUsage_KeyboardEscape:
return kVK_Escape;
case kHIDUsage_KeyboardDeleteOrBackspace:
return kVK_Delete;
case kHIDUsage_KeyboardTab:
return kVK_Tab;
case kHIDUsage_KeyboardSpacebar:
return kVK_Space;
case kHIDUsage_KeyboardHyphen:
return kVK_ANSI_Minus;
case kHIDUsage_KeyboardEqualSign:
return kVK_ANSI_Equal;
case kHIDUsage_KeyboardOpenBracket:
return kVK_ANSI_LeftBracket;
case kHIDUsage_KeyboardCloseBracket:
return kVK_ANSI_RightBracket;
case kHIDUsage_KeyboardBackslash:
return kVK_ANSI_Backslash;
case kHIDUsage_KeyboardNonUSPound:
return INVALID_KEY;
case kHIDUsage_KeyboardSemicolon:
return kVK_ANSI_Semicolon;
case kHIDUsage_KeyboardQuote:
return kVK_ANSI_Quote;
case kHIDUsage_KeyboardGraveAccentAndTilde:
return kVK_ANSI_Grave;
case kHIDUsage_KeyboardComma:
return kVK_ANSI_Comma;
case kHIDUsage_KeyboardPeriod:
return kVK_ANSI_Period;
case kHIDUsage_KeyboardSlash:
return kVK_ANSI_Slash;
case kHIDUsage_KeyboardCapsLock:
return kVK_CapsLock;
case kHIDUsage_KeyboardF1:
return kVK_F1;
case kHIDUsage_KeyboardF2:
return kVK_F2;
case kHIDUsage_KeyboardF3:
return kVK_F3;
case kHIDUsage_KeyboardF4:
return kVK_F4;
case kHIDUsage_KeyboardF5:
return kVK_F5;
case kHIDUsage_KeyboardF6:
return kVK_F6;
case kHIDUsage_KeyboardF7:
return kVK_F7;
case kHIDUsage_KeyboardF8:
return kVK_F8;
case kHIDUsage_KeyboardF9:
return kVK_F9;
case kHIDUsage_KeyboardF10:
return kVK_F10;
case kHIDUsage_KeyboardF11:
return kVK_F11;
case kHIDUsage_KeyboardF12:
return kVK_F12;
case kHIDUsage_KeyboardPrintScreen:
return INVALID_KEY;
case kHIDUsage_KeyboardScrollLock:
return INVALID_KEY;