1
0

obs-cocoa.m 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  1. /******************************************************************************
  2. Copyright (C) 2013 by Ruwen Hahn <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include "util/dstr.h"
  15. #include "obs.h"
  16. #include "obs-internal.h"
  17. #include <unistd.h>
  18. #include <sys/types.h>
  19. #include <sys/sysctl.h>
  20. #include <Carbon/Carbon.h>
  21. #include <IOKit/hid/IOHIDDevice.h>
  22. #include <IOKit/hid/IOHIDManager.h>
  23. #import <AppKit/AppKit.h>
  24. bool is_in_bundle()
  25. {
  26. NSRunningApplication *app = [NSRunningApplication currentApplication];
  27. return [app bundleIdentifier] != nil;
  28. }
  29. const char *get_module_extension(void)
  30. {
  31. return "";
  32. }
  33. void add_default_module_paths(void)
  34. {
  35. NSURL *pluginURL = [[NSBundle mainBundle] builtInPlugInsURL];
  36. NSString *pluginModulePath = [[pluginURL path] stringByAppendingString:@"/%module%.plugin/Contents/MacOS/"];
  37. NSString *pluginDataPath = [[pluginURL path] stringByAppendingString:@"/%module%.plugin/Contents/Resources/"];
  38. obs_add_module_path(pluginModulePath.UTF8String, pluginDataPath.UTF8String);
  39. }
  40. char *find_libobs_data_file(const char *file)
  41. {
  42. NSBundle *frameworkBundle = [NSBundle bundleWithIdentifier:@"com.obsproject.libobs"];
  43. NSString *libobsDataPath =
  44. [[[frameworkBundle bundleURL] path] stringByAppendingFormat:@"/%@/%s", @"Resources", file];
  45. size_t path_length = strlen(libobsDataPath.UTF8String);
  46. char *path = bmalloc(path_length + 1);
  47. snprintf(path, (path_length + 1), "%s", libobsDataPath.UTF8String);
  48. return path;
  49. }
  50. static void log_processor_name(void)
  51. {
  52. char *name = NULL;
  53. size_t size;
  54. int ret;
  55. ret = sysctlbyname("machdep.cpu.brand_string", NULL, &size, NULL, 0);
  56. if (ret != 0)
  57. return;
  58. name = malloc(size);
  59. ret = sysctlbyname("machdep.cpu.brand_string", name, &size, NULL, 0);
  60. if (ret == 0)
  61. blog(LOG_INFO, "CPU Name: %s", name);
  62. free(name);
  63. }
  64. static void log_processor_speed(void)
  65. {
  66. size_t size;
  67. long long freq;
  68. int ret;
  69. size = sizeof(freq);
  70. ret = sysctlbyname("hw.cpufrequency", &freq, &size, NULL, 0);
  71. if (ret == 0)
  72. blog(LOG_INFO, "CPU Speed: %lldMHz", freq / 1000000);
  73. }
  74. static void log_processor_cores(void)
  75. {
  76. blog(LOG_INFO, "Physical Cores: %d, Logical Cores: %d", os_get_physical_cores(), os_get_logical_cores());
  77. }
  78. static void log_emulation_status(void)
  79. {
  80. blog(LOG_INFO, "Rosetta translation used: %s", os_get_emulation_status() ? "true" : "false");
  81. }
  82. static void log_available_memory(void)
  83. {
  84. size_t size;
  85. long long memory_available;
  86. int ret;
  87. size = sizeof(memory_available);
  88. ret = sysctlbyname("hw.memsize", &memory_available, &size, NULL, 0);
  89. if (ret == 0)
  90. blog(LOG_INFO, "Physical Memory: %lldMB Total", memory_available / 1024 / 1024);
  91. }
  92. static void log_os(void)
  93. {
  94. NSProcessInfo *pi = [NSProcessInfo processInfo];
  95. blog(LOG_INFO, "OS Name: macOS");
  96. blog(LOG_INFO, "OS Version: %s", [[pi operatingSystemVersionString] UTF8String]);
  97. }
  98. static void log_kernel_version(void)
  99. {
  100. char kernel_version[1024];
  101. size_t size = sizeof(kernel_version);
  102. int ret;
  103. ret = sysctlbyname("kern.osrelease", kernel_version, &size, NULL, 0);
  104. if (ret == 0)
  105. blog(LOG_INFO, "Kernel Version: %s", kernel_version);
  106. }
  107. void log_system_info(void)
  108. {
  109. log_processor_name();
  110. log_processor_speed();
  111. log_processor_cores();
  112. log_available_memory();
  113. log_os();
  114. log_emulation_status();
  115. log_kernel_version();
  116. }
  117. static bool dstr_from_cfstring(struct dstr *str, CFStringRef ref)
  118. {
  119. CFIndex length = CFStringGetLength(ref);
  120. CFIndex max_size = CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8);
  121. dstr_reserve(str, max_size);
  122. if (!CFStringGetCString(ref, str->array, max_size, kCFStringEncodingUTF8))
  123. return false;
  124. str->len = strlen(str->array);
  125. return true;
  126. }
  127. struct obs_hotkeys_platform {
  128. volatile long refs;
  129. CFTypeRef monitor;
  130. CFTypeRef local_monitor;
  131. bool is_key_down[OBS_KEY_LAST_VALUE];
  132. TISInputSourceRef tis;
  133. CFDataRef layout_data;
  134. UCKeyboardLayout *layout;
  135. };
  136. static void hotkeys_retain(struct obs_hotkeys_platform *plat)
  137. {
  138. os_atomic_inc_long(&plat->refs);
  139. }
  140. static inline void free_hotkeys_platform(obs_hotkeys_platform_t *plat);
  141. static void hotkeys_release(struct obs_hotkeys_platform *plat)
  142. {
  143. if (os_atomic_dec_long(&plat->refs) == -1)
  144. free_hotkeys_platform(plat);
  145. }
  146. #define INVALID_KEY 0xff
  147. #pragma GCC diagnostic ignored "-Winitializer-overrides"
  148. static const int virtual_keys[] = {
  149. [0 ... OBS_KEY_LAST_VALUE] = INVALID_KEY,
  150. [OBS_KEY_A] = kVK_ANSI_A,
  151. [OBS_KEY_B] = kVK_ANSI_B,
  152. [OBS_KEY_C] = kVK_ANSI_C,
  153. [OBS_KEY_D] = kVK_ANSI_D,
  154. [OBS_KEY_E] = kVK_ANSI_E,
  155. [OBS_KEY_F] = kVK_ANSI_F,
  156. [OBS_KEY_G] = kVK_ANSI_G,
  157. [OBS_KEY_H] = kVK_ANSI_H,
  158. [OBS_KEY_I] = kVK_ANSI_I,
  159. [OBS_KEY_J] = kVK_ANSI_J,
  160. [OBS_KEY_K] = kVK_ANSI_K,
  161. [OBS_KEY_L] = kVK_ANSI_L,
  162. [OBS_KEY_M] = kVK_ANSI_M,
  163. [OBS_KEY_N] = kVK_ANSI_N,
  164. [OBS_KEY_O] = kVK_ANSI_O,
  165. [OBS_KEY_P] = kVK_ANSI_P,
  166. [OBS_KEY_Q] = kVK_ANSI_Q,
  167. [OBS_KEY_R] = kVK_ANSI_R,
  168. [OBS_KEY_S] = kVK_ANSI_S,
  169. [OBS_KEY_T] = kVK_ANSI_T,
  170. [OBS_KEY_U] = kVK_ANSI_U,
  171. [OBS_KEY_V] = kVK_ANSI_V,
  172. [OBS_KEY_W] = kVK_ANSI_W,
  173. [OBS_KEY_X] = kVK_ANSI_X,
  174. [OBS_KEY_Y] = kVK_ANSI_Y,
  175. [OBS_KEY_Z] = kVK_ANSI_Z,
  176. [OBS_KEY_1] = kVK_ANSI_1,
  177. [OBS_KEY_2] = kVK_ANSI_2,
  178. [OBS_KEY_3] = kVK_ANSI_3,
  179. [OBS_KEY_4] = kVK_ANSI_4,
  180. [OBS_KEY_5] = kVK_ANSI_5,
  181. [OBS_KEY_6] = kVK_ANSI_6,
  182. [OBS_KEY_7] = kVK_ANSI_7,
  183. [OBS_KEY_8] = kVK_ANSI_8,
  184. [OBS_KEY_9] = kVK_ANSI_9,
  185. [OBS_KEY_0] = kVK_ANSI_0,
  186. [OBS_KEY_RETURN] = kVK_Return,
  187. [OBS_KEY_ESCAPE] = kVK_Escape,
  188. [OBS_KEY_BACKSPACE] = kVK_Delete,
  189. [OBS_KEY_TAB] = kVK_Tab,
  190. [OBS_KEY_SPACE] = kVK_Space,
  191. [OBS_KEY_MINUS] = kVK_ANSI_Minus,
  192. [OBS_KEY_EQUAL] = kVK_ANSI_Equal,
  193. [OBS_KEY_BRACKETLEFT] = kVK_ANSI_LeftBracket,
  194. [OBS_KEY_BRACKETRIGHT] = kVK_ANSI_RightBracket,
  195. [OBS_KEY_BACKSLASH] = kVK_ANSI_Backslash,
  196. [OBS_KEY_SEMICOLON] = kVK_ANSI_Semicolon,
  197. [OBS_KEY_QUOTE] = kVK_ANSI_Quote,
  198. [OBS_KEY_DEAD_GRAVE] = kVK_ANSI_Grave,
  199. [OBS_KEY_COMMA] = kVK_ANSI_Comma,
  200. [OBS_KEY_PERIOD] = kVK_ANSI_Period,
  201. [OBS_KEY_SLASH] = kVK_ANSI_Slash,
  202. [OBS_KEY_CAPSLOCK] = kVK_CapsLock,
  203. [OBS_KEY_SECTION] = kVK_ISO_Section,
  204. [OBS_KEY_F1] = kVK_F1,
  205. [OBS_KEY_F2] = kVK_F2,
  206. [OBS_KEY_F3] = kVK_F3,
  207. [OBS_KEY_F4] = kVK_F4,
  208. [OBS_KEY_F5] = kVK_F5,
  209. [OBS_KEY_F6] = kVK_F6,
  210. [OBS_KEY_F7] = kVK_F7,
  211. [OBS_KEY_F8] = kVK_F8,
  212. [OBS_KEY_F9] = kVK_F9,
  213. [OBS_KEY_F10] = kVK_F10,
  214. [OBS_KEY_F11] = kVK_F11,
  215. [OBS_KEY_F12] = kVK_F12,
  216. [OBS_KEY_HELP] = kVK_Help,
  217. [OBS_KEY_HOME] = kVK_Home,
  218. [OBS_KEY_PAGEUP] = kVK_PageUp,
  219. [OBS_KEY_DELETE] = kVK_ForwardDelete,
  220. [OBS_KEY_END] = kVK_End,
  221. [OBS_KEY_PAGEDOWN] = kVK_PageDown,
  222. [OBS_KEY_RIGHT] = kVK_RightArrow,
  223. [OBS_KEY_LEFT] = kVK_LeftArrow,
  224. [OBS_KEY_DOWN] = kVK_DownArrow,
  225. [OBS_KEY_UP] = kVK_UpArrow,
  226. [OBS_KEY_CLEAR] = kVK_ANSI_KeypadClear,
  227. [OBS_KEY_NUMSLASH] = kVK_ANSI_KeypadDivide,
  228. [OBS_KEY_NUMASTERISK] = kVK_ANSI_KeypadMultiply,
  229. [OBS_KEY_NUMMINUS] = kVK_ANSI_KeypadMinus,
  230. [OBS_KEY_NUMPLUS] = kVK_ANSI_KeypadPlus,
  231. [OBS_KEY_ENTER] = kVK_ANSI_KeypadEnter,
  232. [OBS_KEY_NUM1] = kVK_ANSI_Keypad1,
  233. [OBS_KEY_NUM2] = kVK_ANSI_Keypad2,
  234. [OBS_KEY_NUM3] = kVK_ANSI_Keypad3,
  235. [OBS_KEY_NUM4] = kVK_ANSI_Keypad4,
  236. [OBS_KEY_NUM5] = kVK_ANSI_Keypad5,
  237. [OBS_KEY_NUM6] = kVK_ANSI_Keypad6,
  238. [OBS_KEY_NUM7] = kVK_ANSI_Keypad7,
  239. [OBS_KEY_NUM8] = kVK_ANSI_Keypad8,
  240. [OBS_KEY_NUM9] = kVK_ANSI_Keypad9,
  241. [OBS_KEY_NUM0] = kVK_ANSI_Keypad0,
  242. [OBS_KEY_NUMPERIOD] = kVK_ANSI_KeypadDecimal,
  243. [OBS_KEY_NUMEQUAL] = kVK_ANSI_KeypadEquals,
  244. [OBS_KEY_F13] = kVK_F13,
  245. [OBS_KEY_F14] = kVK_F14,
  246. [OBS_KEY_F15] = kVK_F15,
  247. [OBS_KEY_F16] = kVK_F16,
  248. [OBS_KEY_F17] = kVK_F17,
  249. [OBS_KEY_F18] = kVK_F18,
  250. [OBS_KEY_F19] = kVK_F19,
  251. [OBS_KEY_F20] = kVK_F20,
  252. [OBS_KEY_CONTROL] = kVK_Control,
  253. [OBS_KEY_SHIFT] = kVK_Shift,
  254. [OBS_KEY_ALT] = kVK_Option,
  255. [OBS_KEY_META] = kVK_Command,
  256. [OBS_KEY_CONTROL] = kVK_RightControl,
  257. };
  258. int obs_key_to_virtual_key(obs_key_t code)
  259. {
  260. return virtual_keys[code];
  261. }
  262. obs_key_t obs_key_from_virtual_key(int code)
  263. {
  264. if (code == kVK_RightShift)
  265. return OBS_KEY_SHIFT;
  266. if (code == kVK_RightOption)
  267. return OBS_KEY_ALT;
  268. if (code == kVK_RightCommand)
  269. return OBS_KEY_META;
  270. if (code == kVK_RightControl)
  271. return OBS_KEY_META;
  272. for (size_t i = 0; i < OBS_KEY_LAST_VALUE; i++) {
  273. if (virtual_keys[i] == code) {
  274. return (obs_key_t) i;
  275. }
  276. }
  277. return OBS_KEY_NONE;
  278. }
  279. static bool localized_key_to_str(obs_key_t key, struct dstr *str)
  280. {
  281. #define MAP_KEY(k, s) \
  282. case k: \
  283. dstr_copy(str, obs_get_hotkey_translation(k, s)); \
  284. return true
  285. #define MAP_BUTTON(i) \
  286. case OBS_KEY_MOUSE##i: \
  287. dstr_copy(str, obs_get_hotkey_translation(key, "Mouse " #i)); \
  288. return true
  289. switch (key) {
  290. MAP_KEY(OBS_KEY_SPACE, "Space");
  291. MAP_KEY(OBS_KEY_NUMEQUAL, "= (Keypad)");
  292. MAP_KEY(OBS_KEY_NUMASTERISK, "* (Keypad)");
  293. MAP_KEY(OBS_KEY_NUMPLUS, "+ (Keypad)");
  294. MAP_KEY(OBS_KEY_NUMMINUS, "- (Keypad)");
  295. MAP_KEY(OBS_KEY_NUMPERIOD, ". (Keypad)");
  296. MAP_KEY(OBS_KEY_NUMSLASH, "/ (Keypad)");
  297. MAP_KEY(OBS_KEY_NUM0, "0 (Keypad)");
  298. MAP_KEY(OBS_KEY_NUM1, "1 (Keypad)");
  299. MAP_KEY(OBS_KEY_NUM2, "2 (Keypad)");
  300. MAP_KEY(OBS_KEY_NUM3, "3 (Keypad)");
  301. MAP_KEY(OBS_KEY_NUM4, "4 (Keypad)");
  302. MAP_KEY(OBS_KEY_NUM5, "5 (Keypad)");
  303. MAP_KEY(OBS_KEY_NUM6, "6 (Keypad)");
  304. MAP_KEY(OBS_KEY_NUM7, "7 (Keypad)");
  305. MAP_KEY(OBS_KEY_NUM8, "8 (Keypad)");
  306. MAP_KEY(OBS_KEY_NUM9, "9 (Keypad)");
  307. MAP_BUTTON(1);
  308. MAP_BUTTON(2);
  309. MAP_BUTTON(3);
  310. MAP_BUTTON(4);
  311. MAP_BUTTON(5);
  312. MAP_BUTTON(6);
  313. MAP_BUTTON(7);
  314. MAP_BUTTON(8);
  315. MAP_BUTTON(9);
  316. MAP_BUTTON(10);
  317. MAP_BUTTON(11);
  318. MAP_BUTTON(12);
  319. MAP_BUTTON(13);
  320. MAP_BUTTON(14);
  321. MAP_BUTTON(15);
  322. MAP_BUTTON(16);
  323. MAP_BUTTON(17);
  324. MAP_BUTTON(18);
  325. MAP_BUTTON(19);
  326. MAP_BUTTON(20);
  327. MAP_BUTTON(21);
  328. MAP_BUTTON(22);
  329. MAP_BUTTON(23);
  330. MAP_BUTTON(24);
  331. MAP_BUTTON(25);
  332. MAP_BUTTON(26);
  333. MAP_BUTTON(27);
  334. MAP_BUTTON(28);
  335. MAP_BUTTON(29);
  336. default:
  337. break;
  338. }
  339. #undef MAP_BUTTON
  340. #undef MAP_KEY
  341. return false;
  342. }
  343. static bool code_to_str(int code, struct dstr *str)
  344. {
  345. #define MAP_GLYPH(c, g) \
  346. case c: \
  347. dstr_from_wcs(str, (wchar_t[]) {g, 0}); \
  348. return true
  349. #define MAP_STR(c, s) \
  350. case c: \
  351. dstr_copy(str, s); \
  352. return true
  353. switch (code) {
  354. MAP_GLYPH(kVK_Return, 0x21A9);
  355. MAP_GLYPH(kVK_Escape, 0x238B);
  356. MAP_GLYPH(kVK_Delete, 0x232B);
  357. MAP_GLYPH(kVK_Tab, 0x21e5);
  358. MAP_GLYPH(kVK_CapsLock, 0x21EA);
  359. MAP_GLYPH(kVK_ANSI_KeypadClear, 0x2327);
  360. MAP_GLYPH(kVK_ANSI_KeypadEnter, 0x2305);
  361. MAP_GLYPH(kVK_Help, 0x003F);
  362. MAP_GLYPH(kVK_Home, 0x2196);
  363. MAP_GLYPH(kVK_PageUp, 0x21de);
  364. MAP_GLYPH(kVK_ForwardDelete, 0x2326);
  365. MAP_GLYPH(kVK_End, 0x2198);
  366. MAP_GLYPH(kVK_PageDown, 0x21df);
  367. MAP_GLYPH(kVK_RightArrow, 0x2192);
  368. MAP_GLYPH(kVK_LeftArrow, 0x2190);
  369. MAP_GLYPH(kVK_DownArrow, 0x2193);
  370. MAP_GLYPH(kVK_UpArrow, 0x2191);
  371. MAP_STR(kVK_F1, "F1");
  372. MAP_STR(kVK_F2, "F2");
  373. MAP_STR(kVK_F3, "F3");
  374. MAP_STR(kVK_F4, "F4");
  375. MAP_STR(kVK_F5, "F5");
  376. MAP_STR(kVK_F6, "F6");
  377. MAP_STR(kVK_F7, "F7");
  378. MAP_STR(kVK_F8, "F8");
  379. MAP_STR(kVK_F9, "F9");
  380. MAP_STR(kVK_F10, "F10");
  381. MAP_STR(kVK_F11, "F11");
  382. MAP_STR(kVK_F12, "F12");
  383. MAP_STR(kVK_F13, "F13");
  384. MAP_STR(kVK_F14, "F14");
  385. MAP_STR(kVK_F15, "F15");
  386. MAP_STR(kVK_F16, "F16");
  387. MAP_STR(kVK_F17, "F17");
  388. MAP_STR(kVK_F18, "F18");
  389. MAP_STR(kVK_F19, "F19");
  390. MAP_STR(kVK_F20, "F20");
  391. MAP_GLYPH(kVK_Control, kControlUnicode);
  392. MAP_GLYPH(kVK_Shift, kShiftUnicode);
  393. MAP_GLYPH(kVK_Option, kOptionUnicode);
  394. MAP_GLYPH(kVK_Command, kCommandUnicode);
  395. MAP_GLYPH(kVK_RightControl, kControlUnicode);
  396. MAP_GLYPH(kVK_RightShift, kShiftUnicode);
  397. MAP_GLYPH(kVK_RightOption, kOptionUnicode);
  398. }
  399. #undef MAP_STR
  400. #undef MAP_GLYPH
  401. return false;
  402. }
  403. void obs_key_to_str(obs_key_t key, struct dstr *str)
  404. {
  405. const UniCharCount max_length = 16;
  406. UniChar buffer[16];
  407. if (localized_key_to_str(key, str))
  408. return;
  409. int code = obs_key_to_virtual_key(key);
  410. if (code_to_str(code, str))
  411. return;
  412. if (code == INVALID_KEY) {
  413. blog(LOG_ERROR,
  414. "hotkey-cocoa: Got invalid key while "
  415. "translating key '%d' (%s)",
  416. key, obs_key_to_name(key));
  417. goto err;
  418. }
  419. struct obs_hotkeys_platform *plat = NULL;
  420. if (obs) {
  421. pthread_mutex_lock(&obs->hotkeys.mutex);
  422. plat = obs->hotkeys.platform_context;
  423. hotkeys_retain(plat);
  424. pthread_mutex_unlock(&obs->hotkeys.mutex);
  425. }
  426. if (!plat) {
  427. blog(LOG_ERROR,
  428. "hotkey-cocoa: Could not get hotkey platform "
  429. "while translating key '%d' (%s)",
  430. key, obs_key_to_name(key));
  431. goto err;
  432. }
  433. UInt32 dead_key_state = 0;
  434. UniCharCount len = 0;
  435. OSStatus err = UCKeyTranslate(plat->layout, code, kUCKeyActionDown,
  436. 0x104, //caps lock for upper case letters
  437. LMGetKbdType(), kUCKeyTranslateNoDeadKeysBit, &dead_key_state, max_length, &len,
  438. buffer);
  439. if (err == noErr && len <= 0 && dead_key_state) {
  440. err = UCKeyTranslate(plat->layout, kVK_Space, kUCKeyActionDown, 0x104, LMGetKbdType(),
  441. kUCKeyTranslateNoDeadKeysBit, &dead_key_state, max_length, &len, buffer);
  442. }
  443. hotkeys_release(plat);
  444. if (err != noErr) {
  445. blog(LOG_ERROR,
  446. "hotkey-cocoa: Error while translating key '%d'"
  447. " (0x%x, %s) to string: %d",
  448. key, code, obs_key_to_name(key), err);
  449. goto err;
  450. }
  451. if (len == 0) {
  452. blog(LOG_ERROR,
  453. "hotkey-cocoa: Got 0 length string while "
  454. "translating '%d' (0x%x, %s) to string",
  455. key, code, obs_key_to_name(key));
  456. goto err;
  457. }
  458. CFStringRef string = CFStringCreateWithCharactersNoCopy(NULL, buffer, len, kCFAllocatorNull);
  459. if (!string) {
  460. blog(LOG_ERROR,
  461. "hotkey-cocoa: Could not create CFStringRef "
  462. "while translating '%d' (0x%x, %s) to string",
  463. key, code, obs_key_to_name(key));
  464. goto err;
  465. }
  466. if (!dstr_from_cfstring(str, string)) {
  467. blog(LOG_ERROR,
  468. "hotkey-cocoa: Could not translate CFStringRef "
  469. "to CString while translating '%d' (0x%x, %s)",
  470. key, code, obs_key_to_name(key));
  471. goto release;
  472. }
  473. CFRelease(string);
  474. return;
  475. release:
  476. CFRelease(string);
  477. err:
  478. dstr_copy(str, obs_key_to_name(key));
  479. }
  480. #define OBS_COCOA_MODIFIER_SIZE 7
  481. static void unichar_to_utf8(const UniChar *c, char *buff)
  482. {
  483. CFStringRef string = CFStringCreateWithCharactersNoCopy(NULL, c, 2, kCFAllocatorNull);
  484. if (!string) {
  485. blog(LOG_ERROR, "hotkey-cocoa: Could not create CFStringRef "
  486. "while populating modifier strings");
  487. return;
  488. }
  489. if (!CFStringGetCString(string, buff, OBS_COCOA_MODIFIER_SIZE, kCFStringEncodingUTF8))
  490. blog(LOG_ERROR,
  491. "hotkey-cocoa: Error while populating "
  492. " modifier string with glyph %d (0x%x)",
  493. c[0], c[0]);
  494. CFRelease(string);
  495. }
  496. static char ctrl_str[OBS_COCOA_MODIFIER_SIZE];
  497. static char opt_str[OBS_COCOA_MODIFIER_SIZE];
  498. static char shift_str[OBS_COCOA_MODIFIER_SIZE];
  499. static char cmd_str[OBS_COCOA_MODIFIER_SIZE];
  500. static void init_utf_8_strings(void)
  501. {
  502. const UniChar ctrl_uni[] = {kControlUnicode, 0};
  503. const UniChar opt_uni[] = {kOptionUnicode, 0};
  504. const UniChar shift_uni[] = {kShiftUnicode, 0};
  505. const UniChar cmd_uni[] = {kCommandUnicode, 0};
  506. unichar_to_utf8(ctrl_uni, ctrl_str);
  507. unichar_to_utf8(opt_uni, opt_str);
  508. unichar_to_utf8(shift_uni, shift_str);
  509. unichar_to_utf8(cmd_uni, cmd_str);
  510. }
  511. static pthread_once_t strings_token = PTHREAD_ONCE_INIT;
  512. void obs_key_combination_to_str(obs_key_combination_t key, struct dstr *str)
  513. {
  514. struct dstr key_str = {0};
  515. if (key.key != OBS_KEY_NONE)
  516. obs_key_to_str(key.key, &key_str);
  517. int res = pthread_once(&strings_token, init_utf_8_strings);
  518. if (res) {
  519. blog(LOG_ERROR,
  520. "hotkeys-cocoa: Error while translating "
  521. "modifiers %d (0x%x)",
  522. res, res);
  523. dstr_move(str, &key_str);
  524. return;
  525. }
  526. #define CHECK_MODIFIER(mod, str) ((key.modifiers & mod) ? str : "")
  527. dstr_printf(str, "%s%s%s%s%s", CHECK_MODIFIER(INTERACT_CONTROL_KEY, ctrl_str),
  528. CHECK_MODIFIER(INTERACT_ALT_KEY, opt_str), CHECK_MODIFIER(INTERACT_SHIFT_KEY, shift_str),
  529. CHECK_MODIFIER(INTERACT_COMMAND_KEY, cmd_str), key_str.len ? key_str.array : "");
  530. #undef CHECK_MODIFIER
  531. dstr_free(&key_str);
  532. }
  533. static bool log_layout_name(TISInputSourceRef tis)
  534. {
  535. struct dstr layout_name = {0};
  536. CFStringRef sid = (CFStringRef) TISGetInputSourceProperty(tis, kTISPropertyInputSourceID);
  537. if (!sid) {
  538. blog(LOG_ERROR, "hotkeys-cocoa: Failed getting InputSourceID");
  539. return false;
  540. }
  541. if (!dstr_from_cfstring(&layout_name, sid)) {
  542. blog(LOG_ERROR, "hotkeys-cocoa: Could not convert InputSourceID"
  543. " to CString");
  544. goto fail;
  545. }
  546. blog(LOG_INFO, "hotkeys-cocoa: Using layout '%s'", layout_name.array);
  547. dstr_free(&layout_name);
  548. return true;
  549. fail:
  550. dstr_free(&layout_name);
  551. return false;
  552. }
  553. static void handle_monitor_event(obs_hotkeys_platform_t *plat, NSEvent *event)
  554. {
  555. if (event.type == NSEventTypeFlagsChanged) {
  556. NSEventModifierFlags flags = event.modifierFlags;
  557. plat->is_key_down[OBS_KEY_CAPSLOCK] = !!(flags & NSEventModifierFlagCapsLock);
  558. plat->is_key_down[OBS_KEY_SHIFT] = !!(flags & NSEventModifierFlagShift);
  559. plat->is_key_down[OBS_KEY_ALT] = !!(flags & NSEventModifierFlagOption);
  560. plat->is_key_down[OBS_KEY_META] = !!(flags & NSEventModifierFlagCommand);
  561. plat->is_key_down[OBS_KEY_CONTROL] = !!(flags & NSEventModifierFlagControl);
  562. } else if (event.type == NSEventTypeKeyDown || event.type == NSEventTypeKeyUp) {
  563. plat->is_key_down[obs_key_from_virtual_key(event.keyCode)] = (event.type == NSEventTypeKeyDown);
  564. }
  565. }
  566. static bool init_hotkeys_platform(obs_hotkeys_platform_t **plat_)
  567. {
  568. if (!plat_)
  569. return false;
  570. *plat_ = bzalloc(sizeof(obs_hotkeys_platform_t));
  571. obs_hotkeys_platform_t *plat = *plat_;
  572. if (!plat) {
  573. *plat_ = NULL;
  574. return false;
  575. }
  576. void (^handler)(NSEvent *) = ^(NSEvent *event) {
  577. handle_monitor_event(plat, event);
  578. };
  579. plat->monitor = (__bridge CFTypeRef)
  580. [NSEvent addGlobalMonitorForEventsMatchingMask:NSEventMaskKeyDown | NSEventMaskKeyUp | NSEventMaskFlagsChanged
  581. handler:handler];
  582. NSEvent *_Nullable (^local_handler)(NSEvent *event) = ^NSEvent *_Nullable(NSEvent *event)
  583. {
  584. handle_monitor_event(plat, event);
  585. return event;
  586. };
  587. plat->local_monitor = (__bridge CFTypeRef)
  588. [NSEvent addLocalMonitorForEventsMatchingMask:NSEventMaskKeyDown | NSEventMaskKeyUp | NSEventMaskFlagsChanged
  589. handler:local_handler];
  590. plat->tis = TISCopyCurrentKeyboardLayoutInputSource();
  591. plat->layout_data = (CFDataRef) TISGetInputSourceProperty(plat->tis, kTISPropertyUnicodeKeyLayoutData);
  592. if (!plat->layout_data) {
  593. blog(LOG_ERROR, "hotkeys-cocoa: Failed getting LayoutData");
  594. goto fail;
  595. }
  596. CFRetain(plat->layout_data);
  597. plat->layout = (UCKeyboardLayout *) CFDataGetBytePtr(plat->layout_data);
  598. return true;
  599. fail:
  600. hotkeys_release(plat);
  601. *plat_ = NULL;
  602. return false;
  603. }
  604. static inline void free_hotkeys_platform(obs_hotkeys_platform_t *plat)
  605. {
  606. if (!plat)
  607. return;
  608. if (plat->monitor) {
  609. [NSEvent removeMonitor:(__bridge id _Nonnull)(plat->monitor)];
  610. plat->monitor = NULL;
  611. }
  612. if (plat->local_monitor) {
  613. [NSEvent removeMonitor:(__bridge id _Nonnull)(plat->local_monitor)];
  614. plat->local_monitor = NULL;
  615. }
  616. if (plat->tis) {
  617. CFRelease(plat->tis);
  618. plat->tis = NULL;
  619. }
  620. if (plat->layout_data) {
  621. CFRelease(plat->layout_data);
  622. plat->layout_data = NULL;
  623. }
  624. bfree(plat);
  625. }
  626. static void input_method_changed(CFNotificationCenterRef nc, void *observer, CFStringRef name, const void *object,
  627. CFDictionaryRef user_info)
  628. {
  629. UNUSED_PARAMETER(nc);
  630. UNUSED_PARAMETER(name);
  631. UNUSED_PARAMETER(object);
  632. UNUSED_PARAMETER(user_info);
  633. struct obs_core_hotkeys *hotkeys = observer;
  634. obs_hotkeys_platform_t *new_plat;
  635. if (init_hotkeys_platform(&new_plat)) {
  636. obs_hotkeys_platform_t *plat;
  637. pthread_mutex_lock(&hotkeys->mutex);
  638. plat = hotkeys->platform_context;
  639. if (new_plat && plat && new_plat->layout_data == plat->layout_data) {
  640. pthread_mutex_unlock(&hotkeys->mutex);
  641. hotkeys_release(new_plat);
  642. return;
  643. }
  644. hotkeys->platform_context = new_plat;
  645. if (new_plat)
  646. log_layout_name(new_plat->tis);
  647. pthread_mutex_unlock(&hotkeys->mutex);
  648. calldata_t params = {0};
  649. signal_handler_signal(hotkeys->signals, "hotkey_layout_change", &params);
  650. if (plat)
  651. hotkeys_release(plat);
  652. }
  653. }
  654. bool obs_hotkeys_platform_init(struct obs_core_hotkeys *hotkeys)
  655. {
  656. CFNotificationCenterAddObserver(CFNotificationCenterGetDistributedCenter(), hotkeys, input_method_changed,
  657. kTISNotifySelectedKeyboardInputSourceChanged, NULL,
  658. CFNotificationSuspensionBehaviorDeliverImmediately);
  659. input_method_changed(NULL, hotkeys, NULL, NULL, NULL);
  660. return hotkeys->platform_context != NULL;
  661. }
  662. void obs_hotkeys_platform_free(struct obs_core_hotkeys *hotkeys)
  663. {
  664. CFNotificationCenterRemoveEveryObserver(CFNotificationCenterGetDistributedCenter(), hotkeys);
  665. hotkeys_release(hotkeys->platform_context);
  666. }
  667. typedef unsigned long NSUInteger;
  668. static bool mouse_button_pressed(obs_key_t key, bool *pressed)
  669. {
  670. int button = 0;
  671. switch (key) {
  672. #define MAP_BUTTON(n) \
  673. case OBS_KEY_MOUSE##n: \
  674. button = n - 1; \
  675. break
  676. MAP_BUTTON(1);
  677. MAP_BUTTON(2);
  678. MAP_BUTTON(3);
  679. MAP_BUTTON(4);
  680. MAP_BUTTON(5);
  681. MAP_BUTTON(6);
  682. MAP_BUTTON(7);
  683. MAP_BUTTON(8);
  684. MAP_BUTTON(9);
  685. MAP_BUTTON(10);
  686. MAP_BUTTON(11);
  687. MAP_BUTTON(12);
  688. MAP_BUTTON(13);
  689. MAP_BUTTON(14);
  690. MAP_BUTTON(15);
  691. MAP_BUTTON(16);
  692. MAP_BUTTON(17);
  693. MAP_BUTTON(18);
  694. MAP_BUTTON(19);
  695. MAP_BUTTON(20);
  696. MAP_BUTTON(21);
  697. MAP_BUTTON(22);
  698. MAP_BUTTON(23);
  699. MAP_BUTTON(24);
  700. MAP_BUTTON(25);
  701. MAP_BUTTON(26);
  702. MAP_BUTTON(27);
  703. MAP_BUTTON(28);
  704. MAP_BUTTON(29);
  705. break;
  706. #undef MAP_BUTTON
  707. default:
  708. return false;
  709. }
  710. NSUInteger buttons = [NSEvent pressedMouseButtons];
  711. *pressed = (buttons & (1 << button)) != 0;
  712. return true;
  713. }
  714. bool obs_hotkeys_platform_is_pressed(obs_hotkeys_platform_t *plat, obs_key_t key)
  715. {
  716. bool mouse_pressed = false;
  717. if (mouse_button_pressed(key, &mouse_pressed))
  718. return mouse_pressed;
  719. if (!plat)
  720. return false;
  721. if (key >= OBS_KEY_LAST_VALUE)
  722. return false;
  723. return plat->is_key_down[key];
  724. }
  725. void *obs_graphics_thread_autorelease(void *param)
  726. {
  727. @autoreleasepool {
  728. return obs_graphics_thread(param);
  729. }
  730. }
  731. bool obs_graphics_thread_loop_autorelease(struct obs_graphics_context *context)
  732. {
  733. @autoreleasepool {
  734. return obs_graphics_thread_loop(context);
  735. }
  736. }