obs-cocoa.m 22 KB

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