1
0

obs-cocoa.m 23 KB

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