obs-hotkey.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519
  1. /******************************************************************************
  2. Copyright (C) 2014-2015 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 <inttypes.h>
  15. #include "obs-internal.h"
  16. static inline bool lock(void)
  17. {
  18. if (!obs)
  19. return false;
  20. pthread_mutex_lock(&obs->hotkeys.mutex);
  21. return true;
  22. }
  23. static inline void unlock(void)
  24. {
  25. pthread_mutex_unlock(&obs->hotkeys.mutex);
  26. }
  27. obs_hotkey_id obs_hotkey_get_id(const obs_hotkey_t *key)
  28. {
  29. return key->id;
  30. }
  31. const char *obs_hotkey_get_name(const obs_hotkey_t *key)
  32. {
  33. return key->name;
  34. }
  35. const char *obs_hotkey_get_description(const obs_hotkey_t *key)
  36. {
  37. return key->description;
  38. }
  39. obs_hotkey_registerer_t obs_hotkey_get_registerer_type(const obs_hotkey_t *key)
  40. {
  41. return key->registerer_type;
  42. }
  43. void *obs_hotkey_get_registerer(const obs_hotkey_t *key)
  44. {
  45. return key->registerer;
  46. }
  47. obs_hotkey_id obs_hotkey_get_pair_partner_id(const obs_hotkey_t *key)
  48. {
  49. return key->pair_partner_id;
  50. }
  51. obs_key_combination_t obs_hotkey_binding_get_key_combination(
  52. obs_hotkey_binding_t *binding)
  53. {
  54. return binding->key;
  55. }
  56. obs_hotkey_id obs_hotkey_binding_get_hotkey_id(obs_hotkey_binding_t *binding)
  57. {
  58. return binding->hotkey_id;
  59. }
  60. obs_hotkey_t *obs_hotkey_binding_get_hotkey(obs_hotkey_binding_t *binding)
  61. {
  62. return binding->hotkey;
  63. }
  64. static void hotkey_signal(const char *signal, obs_hotkey_t *hotkey)
  65. {
  66. calldata_t data;
  67. calldata_init(&data);
  68. calldata_set_ptr(&data, "key", hotkey);
  69. signal_handler_signal(obs->hotkeys.signals, signal, &data);
  70. calldata_free(&data);
  71. }
  72. static inline void fixup_pointers(void);
  73. static inline void load_bindings(obs_hotkey_t *hotkey, obs_data_array_t *data);
  74. static inline void context_add_hotkey(struct obs_context_data *context,
  75. obs_hotkey_id id)
  76. {
  77. da_push_back(context->hotkeys, &id);
  78. }
  79. static inline obs_hotkey_id obs_hotkey_register_internal(
  80. obs_hotkey_registerer_t type, void *registerer,
  81. struct obs_context_data *context,
  82. const char *name, const char *description,
  83. obs_hotkey_func func, void *data)
  84. {
  85. if ((obs->hotkeys.next_id + 1) == OBS_INVALID_HOTKEY_ID)
  86. blog(LOG_WARNING, "obs-hotkey: Available hotkey ids exhausted");
  87. obs_hotkey_t *base_addr = obs->hotkeys.hotkeys.array;
  88. obs_hotkey_id result = obs->hotkeys.next_id++;
  89. obs_hotkey_t *hotkey = da_push_back_new(obs->hotkeys.hotkeys);
  90. hotkey->id = result;
  91. hotkey->name = bstrdup(name);
  92. hotkey->description = bstrdup(description);
  93. hotkey->func = func;
  94. hotkey->data = data;
  95. hotkey->registerer_type = type;
  96. hotkey->registerer = registerer;
  97. hotkey->pair_partner_id = OBS_INVALID_HOTKEY_PAIR_ID;
  98. if (context) {
  99. obs_data_array_t *data =
  100. obs_data_get_array(context->hotkey_data, name);
  101. load_bindings(hotkey, data);
  102. obs_data_array_release(data);
  103. context_add_hotkey(context, result);
  104. }
  105. if (base_addr != obs->hotkeys.hotkeys.array)
  106. fixup_pointers();
  107. hotkey_signal("hotkey_register", hotkey);
  108. return result;
  109. }
  110. obs_hotkey_id obs_hotkey_register_frontend(const char *name,
  111. const char *description, obs_hotkey_func func, void *data)
  112. {
  113. if (!lock())
  114. return OBS_INVALID_HOTKEY_ID;
  115. obs_hotkey_id id = obs_hotkey_register_internal(
  116. OBS_HOTKEY_REGISTERER_FRONTEND, NULL, NULL,
  117. name, description, func, data);
  118. unlock();
  119. return id;
  120. }
  121. obs_hotkey_id obs_hotkey_register_encoder(obs_encoder_t *encoder,
  122. const char *name, const char *description,
  123. obs_hotkey_func func, void *data)
  124. {
  125. if (!encoder || !lock())
  126. return OBS_INVALID_HOTKEY_ID;
  127. obs_hotkey_id id = obs_hotkey_register_internal(
  128. OBS_HOTKEY_REGISTERER_ENCODER,
  129. obs_encoder_get_weak_encoder(encoder),
  130. &encoder->context, name, description,
  131. func, data);
  132. unlock();
  133. return id;
  134. }
  135. obs_hotkey_id obs_hotkey_register_output(obs_output_t *output,
  136. const char *name, const char *description,
  137. obs_hotkey_func func, void *data)
  138. {
  139. if (!output || !lock())
  140. return OBS_INVALID_HOTKEY_ID;
  141. obs_hotkey_id id = obs_hotkey_register_internal(
  142. OBS_HOTKEY_REGISTERER_OUTPUT,
  143. obs_output_get_weak_output(output),
  144. &output->context, name, description,
  145. func, data);
  146. unlock();
  147. return id;
  148. }
  149. obs_hotkey_id obs_hotkey_register_service(obs_service_t *service,
  150. const char *name, const char *description,
  151. obs_hotkey_func func, void *data)
  152. {
  153. if (!service || !lock())
  154. return OBS_INVALID_HOTKEY_ID;
  155. obs_hotkey_id id = obs_hotkey_register_internal(
  156. OBS_HOTKEY_REGISTERER_SERVICE,
  157. obs_service_get_weak_service(service),
  158. &service->context, name, description,
  159. func, data);
  160. unlock();
  161. return id;
  162. }
  163. obs_hotkey_id obs_hotkey_register_source(obs_source_t *source, const char *name,
  164. const char *description, obs_hotkey_func func, void *data)
  165. {
  166. if (!source || !lock())
  167. return OBS_INVALID_HOTKEY_ID;
  168. obs_hotkey_id id = obs_hotkey_register_internal(
  169. OBS_HOTKEY_REGISTERER_SOURCE,
  170. obs_source_get_weak_source(source),
  171. &source->context, name, description,
  172. func, data);
  173. unlock();
  174. return id;
  175. }
  176. static inline void fixup_pair_pointers(void);
  177. static obs_hotkey_pair_t *create_hotkey_pair(struct obs_context_data *context,
  178. obs_hotkey_active_func func0, obs_hotkey_active_func func1,
  179. void *data0, void *data1)
  180. {
  181. if ((obs->hotkeys.next_pair_id + 1) == OBS_INVALID_HOTKEY_PAIR_ID)
  182. blog(LOG_WARNING, "obs-hotkey: Available hotkey pair ids "
  183. "exhausted");
  184. obs_hotkey_pair_t *base_addr = obs->hotkeys.hotkey_pairs.array;
  185. obs_hotkey_pair_t *pair =
  186. da_push_back_new(obs->hotkeys.hotkey_pairs);
  187. pair->pair_id = obs->hotkeys.next_pair_id++;
  188. pair->func[0] = func0;
  189. pair->func[1] = func1;
  190. pair->id[0] = OBS_INVALID_HOTKEY_ID;
  191. pair->id[1] = OBS_INVALID_HOTKEY_ID;
  192. pair->data[0] = data0;
  193. pair->data[1] = data1;
  194. if (context)
  195. da_push_back(context->hotkey_pairs, &pair->pair_id);
  196. if (base_addr != obs->hotkeys.hotkey_pairs.array)
  197. fixup_pair_pointers();
  198. return pair;
  199. }
  200. static void obs_hotkey_pair_first_func(void *data,
  201. obs_hotkey_id id, obs_hotkey_t *hotkey, bool pressed)
  202. {
  203. UNUSED_PARAMETER(id);
  204. obs_hotkey_pair_t *pair = data;
  205. if (pair->pressed1)
  206. return;
  207. if (pair->pressed0 && !pressed)
  208. pair->pressed0 = false;
  209. else if (pair->func[0](pair->data[0], pair->pair_id, hotkey, pressed))
  210. pair->pressed0 = pressed;
  211. }
  212. static void obs_hotkey_pair_second_func(void *data,
  213. obs_hotkey_id id, obs_hotkey_t *hotkey, bool pressed)
  214. {
  215. UNUSED_PARAMETER(id);
  216. obs_hotkey_pair_t *pair = data;
  217. if (pair->pressed0)
  218. return;
  219. if (pair->pressed1 && !pressed)
  220. pair->pressed1 = false;
  221. else if (pair->func[1](pair->data[0], pair->pair_id, hotkey, pressed))
  222. pair->pressed1 = pressed;
  223. }
  224. static inline bool find_id(obs_hotkey_id id, size_t *idx);
  225. static obs_hotkey_pair_id register_hotkey_pair_internal(
  226. obs_hotkey_registerer_t type, void *registerer,
  227. void *(*weak_ref)(void*),
  228. struct obs_context_data *context,
  229. const char *name0, const char *description0,
  230. const char *name1, const char *description1,
  231. obs_hotkey_active_func func0, obs_hotkey_active_func func1,
  232. void *data0, void *data1)
  233. {
  234. if (!lock())
  235. return OBS_INVALID_HOTKEY_PAIR_ID;
  236. obs_hotkey_pair_t *pair = create_hotkey_pair(context,
  237. func0, func1, data0, data1);
  238. pair->id[0] = obs_hotkey_register_internal(
  239. type, weak_ref(registerer), context,
  240. name0, description0,
  241. obs_hotkey_pair_first_func, pair);
  242. pair->id[1] = obs_hotkey_register_internal(
  243. type, weak_ref(registerer), context,
  244. name1, description1,
  245. obs_hotkey_pair_second_func, pair);
  246. size_t idx;
  247. if (find_id(pair->id[0], &idx))
  248. obs->hotkeys.hotkeys.array[idx].pair_partner_id = pair->id[1];
  249. if (find_id(pair->id[1], &idx))
  250. obs->hotkeys.hotkeys.array[idx].pair_partner_id = pair->id[0];
  251. obs_hotkey_pair_id id = pair->pair_id;
  252. unlock();
  253. return id;
  254. }
  255. static inline void *obs_id_(void *id_)
  256. {
  257. return id_;
  258. }
  259. obs_hotkey_pair_id obs_hotkey_pair_register_frontend(
  260. const char *name0, const char *description0,
  261. const char *name1, const char *description1,
  262. obs_hotkey_active_func func0, obs_hotkey_active_func func1,
  263. void *data0, void *data1)
  264. {
  265. return register_hotkey_pair_internal(
  266. OBS_HOTKEY_REGISTERER_FRONTEND, NULL, obs_id_, NULL,
  267. name0, description0, name1, description1,
  268. func0, func1, data0, data1);
  269. }
  270. static inline void *weak_encoder_ref(void *ref)
  271. {
  272. return obs_encoder_get_weak_encoder(ref);
  273. }
  274. obs_hotkey_pair_id obs_hotkey_pair_register_encoder(obs_encoder_t *encoder,
  275. const char *name0, const char *description0,
  276. const char *name1, const char *description1,
  277. obs_hotkey_active_func func0, obs_hotkey_active_func func1,
  278. void *data0, void *data1)
  279. {
  280. if (!encoder) return OBS_INVALID_HOTKEY_PAIR_ID;
  281. return register_hotkey_pair_internal(
  282. OBS_HOTKEY_REGISTERER_ENCODER, encoder,
  283. weak_encoder_ref,
  284. &encoder->context,
  285. name0, description0, name1, description1,
  286. func0, func1, data0, data1);
  287. }
  288. static inline void *weak_output_ref(void *ref)
  289. {
  290. return obs_output_get_weak_output(ref);
  291. }
  292. obs_hotkey_pair_id obs_hotkey_pair_register_output(obs_output_t *output,
  293. const char *name0, const char *description0,
  294. const char *name1, const char *description1,
  295. obs_hotkey_active_func func0, obs_hotkey_active_func func1,
  296. void *data0, void *data1)
  297. {
  298. if (!output) return OBS_INVALID_HOTKEY_PAIR_ID;
  299. return register_hotkey_pair_internal(
  300. OBS_HOTKEY_REGISTERER_OUTPUT, output,
  301. weak_output_ref,
  302. &output->context,
  303. name0, description0, name1, description1,
  304. func0, func1, data0, data1);
  305. }
  306. static inline void *weak_service_ref(void *ref)
  307. {
  308. return obs_service_get_weak_service(ref);
  309. }
  310. obs_hotkey_pair_id obs_hotkey_pair_register_service(obs_service_t *service,
  311. const char *name0, const char *description0,
  312. const char *name1, const char *description1,
  313. obs_hotkey_active_func func0, obs_hotkey_active_func func1,
  314. void *data0, void *data1)
  315. {
  316. if (!service) return OBS_INVALID_HOTKEY_PAIR_ID;
  317. return register_hotkey_pair_internal(
  318. OBS_HOTKEY_REGISTERER_SERVICE, service,
  319. weak_service_ref,
  320. &service->context,
  321. name0, description0, name1, description1,
  322. func0, func1, data0, data1);
  323. }
  324. static inline void *weak_source_ref(void *ref)
  325. {
  326. return obs_source_get_weak_source(ref);
  327. }
  328. obs_hotkey_pair_id obs_hotkey_pair_register_source(obs_source_t *source,
  329. const char *name0, const char *description0,
  330. const char *name1, const char *description1,
  331. obs_hotkey_active_func func0, obs_hotkey_active_func func1,
  332. void *data0, void *data1)
  333. {
  334. if (!source) return OBS_INVALID_HOTKEY_PAIR_ID;
  335. return register_hotkey_pair_internal(
  336. OBS_HOTKEY_REGISTERER_SOURCE, source,
  337. weak_source_ref,
  338. &source->context,
  339. name0, description0, name1, description1,
  340. func0, func1, data0, data1);
  341. }
  342. typedef bool (*obs_hotkey_internal_enum_func)(void *data,
  343. size_t idx, obs_hotkey_t *hotkey);
  344. static inline void enum_hotkeys(obs_hotkey_internal_enum_func func, void *data)
  345. {
  346. const size_t num = obs->hotkeys.hotkeys.num;
  347. obs_hotkey_t *array = obs->hotkeys.hotkeys.array;
  348. for (size_t i = 0; i < num; i++) {
  349. if (!func(data, i, &array[i]))
  350. break;
  351. }
  352. }
  353. typedef bool (*obs_hotkey_pair_internal_enum_func)(size_t idx,
  354. obs_hotkey_pair_t *pair, void *data);
  355. static inline void enum_hotkey_pairs(obs_hotkey_pair_internal_enum_func func,
  356. void *data)
  357. {
  358. const size_t num = obs->hotkeys.hotkey_pairs.num;
  359. obs_hotkey_pair_t *array = obs->hotkeys.hotkey_pairs.array;
  360. for (size_t i = 0; i < num; i++) {
  361. if (!func(i, &array[i], data))
  362. break;
  363. }
  364. }
  365. typedef bool (*obs_hotkey_binding_internal_enum_func)(void *data,
  366. size_t idx, obs_hotkey_binding_t *binding);
  367. static inline void enum_bindings(obs_hotkey_binding_internal_enum_func func,
  368. void *data)
  369. {
  370. const size_t num = obs->hotkeys.bindings.num;
  371. obs_hotkey_binding_t *array = obs->hotkeys.bindings.array;
  372. for (size_t i = 0; i < num; i++) {
  373. if (!func(data, i, &array[i]))
  374. break;
  375. }
  376. }
  377. struct obs_hotkey_internal_find_forward {
  378. obs_hotkey_id id;
  379. bool found;
  380. size_t idx;
  381. };
  382. static inline bool find_id_helper(void *data, size_t idx, obs_hotkey_t *hotkey)
  383. {
  384. struct obs_hotkey_internal_find_forward *find = data;
  385. if (hotkey->id != find->id)
  386. return true;
  387. find->idx = idx;
  388. find->found = true;
  389. return false;
  390. }
  391. static inline bool find_id(obs_hotkey_id id, size_t *idx)
  392. {
  393. struct obs_hotkey_internal_find_forward find = {id, false, 0};
  394. enum_hotkeys(find_id_helper, &find);
  395. *idx = find.idx;
  396. return find.found;
  397. }
  398. static inline bool pointer_fixup_func(void *data,
  399. size_t idx, obs_hotkey_binding_t *binding)
  400. {
  401. UNUSED_PARAMETER(idx);
  402. UNUSED_PARAMETER(data);
  403. size_t idx_;
  404. if (!find_id(binding->hotkey_id, &idx_)) {
  405. bcrash("obs-hotkey: Could not find hotkey id '%" PRIuMAX "' "
  406. "for binding '%s' (modifiers 0x%x)",
  407. (uintmax_t)binding->hotkey_id,
  408. obs_key_to_name(binding->key.key),
  409. binding->key.modifiers);
  410. binding->hotkey = NULL;
  411. return true;
  412. }
  413. binding->hotkey = &obs->hotkeys.hotkeys.array[idx_];
  414. return true;
  415. }
  416. static inline void fixup_pointers(void)
  417. {
  418. enum_bindings(pointer_fixup_func, NULL);
  419. }
  420. struct obs_hotkey_internal_find_pair_forward {
  421. obs_hotkey_pair_id id;
  422. bool found;
  423. size_t idx;
  424. };
  425. static inline bool find_pair_id_helper(size_t idx, obs_hotkey_pair_t *pair,
  426. void *data)
  427. {
  428. struct obs_hotkey_internal_find_pair_forward *find = data;
  429. if (pair->pair_id != find->id)
  430. return true;
  431. find->idx = idx;
  432. find->found = true;
  433. return false;
  434. }
  435. static inline bool find_pair_id(obs_hotkey_pair_id id, size_t *idx)
  436. {
  437. struct obs_hotkey_internal_find_pair_forward find = {id, false, 0};
  438. enum_hotkey_pairs(find_pair_id_helper, &find);
  439. *idx = find.idx;
  440. return find.found;
  441. }
  442. static inline bool pair_pointer_fixup_func(size_t idx,
  443. obs_hotkey_pair_t *pair, void *data)
  444. {
  445. UNUSED_PARAMETER(idx);
  446. UNUSED_PARAMETER(data);
  447. if (find_id(pair->id[0], &idx))
  448. obs->hotkeys.hotkeys.array[idx].data = pair;
  449. if (find_id(pair->id[1], &idx))
  450. obs->hotkeys.hotkeys.array[idx].data = pair;
  451. return true;
  452. }
  453. static inline void fixup_pair_pointers(void)
  454. {
  455. enum_hotkey_pairs(pair_pointer_fixup_func, NULL);
  456. }
  457. static inline void enum_context_hotkeys(struct obs_context_data *context,
  458. obs_hotkey_internal_enum_func func, void *data)
  459. {
  460. const size_t num = context->hotkeys.num;
  461. const obs_hotkey_id *array = context->hotkeys.array;
  462. obs_hotkey_t *hotkey_array = obs->hotkeys.hotkeys.array;
  463. for (size_t i = 0; i < num; i++) {
  464. size_t idx;
  465. if (!find_id(array[i], &idx))
  466. continue;
  467. if (!func(data, idx, &hotkey_array[idx]))
  468. break;
  469. }
  470. }
  471. static inline void load_modifier(uint32_t *modifiers, obs_data_t *data,
  472. const char *name, uint32_t flag)
  473. {
  474. if (obs_data_get_bool(data, name))
  475. *modifiers |= flag;
  476. }
  477. static inline void create_binding(obs_hotkey_t *hotkey,
  478. obs_key_combination_t combo)
  479. {
  480. obs_hotkey_binding_t *binding = da_push_back_new(obs->hotkeys.bindings);
  481. if (!binding)
  482. return;
  483. binding->key = combo;
  484. binding->hotkey_id = hotkey->id;
  485. binding->hotkey = hotkey;
  486. }
  487. static inline void load_binding(obs_hotkey_t *hotkey, obs_data_t *data)
  488. {
  489. if (!hotkey || !data)
  490. return;
  491. obs_key_combination_t combo = {0};
  492. uint32_t *modifiers = &combo.modifiers;
  493. load_modifier(modifiers, data, "shift", INTERACT_SHIFT_KEY);
  494. load_modifier(modifiers, data, "control", INTERACT_CONTROL_KEY);
  495. load_modifier(modifiers, data, "alt", INTERACT_ALT_KEY);
  496. load_modifier(modifiers, data, "command", INTERACT_COMMAND_KEY);
  497. combo.key = obs_key_from_name(obs_data_get_string(data, "key"));
  498. if (!modifiers && (combo.key == OBS_KEY_NONE ||
  499. combo.key >= OBS_KEY_LAST_VALUE))
  500. return;
  501. create_binding(hotkey, combo);
  502. }
  503. static inline void load_bindings(obs_hotkey_t *hotkey, obs_data_array_t *data)
  504. {
  505. const size_t count = obs_data_array_count(data);
  506. for (size_t i = 0; i < count; i++) {
  507. obs_data_t *item = obs_data_array_item(data, i);
  508. load_binding(hotkey, item);
  509. obs_data_release(item);
  510. }
  511. hotkey_signal("hotkey_bindings_changed", hotkey);
  512. }
  513. static inline void remove_bindings(obs_hotkey_id id);
  514. void obs_hotkey_load_bindings(obs_hotkey_id id,
  515. obs_key_combination_t *combinations, size_t num)
  516. {
  517. size_t idx;
  518. if (!lock())
  519. return;
  520. if (find_id(id, &idx)) {
  521. obs_hotkey_t *hotkey = &obs->hotkeys.hotkeys.array[idx];
  522. remove_bindings(id);
  523. for (size_t i = 0; i < num; i++)
  524. create_binding(hotkey, combinations[i]);
  525. hotkey_signal("hotkey_bindings_changed", hotkey);
  526. }
  527. unlock();
  528. }
  529. void obs_hotkey_load(obs_hotkey_id id, obs_data_array_t *data)
  530. {
  531. size_t idx;
  532. if (!lock())
  533. return;
  534. if (find_id(id, &idx)) {
  535. remove_bindings(id);
  536. load_bindings(&obs->hotkeys.hotkeys.array[idx], data);
  537. }
  538. unlock();
  539. }
  540. static inline bool enum_load_bindings(void *data,
  541. size_t idx, obs_hotkey_t *hotkey)
  542. {
  543. UNUSED_PARAMETER(idx);
  544. obs_data_array_t *hotkey_data = obs_data_get_array(data, hotkey->name);
  545. if (!hotkey_data)
  546. return true;
  547. load_bindings(hotkey, hotkey_data);
  548. obs_data_array_release(hotkey_data);
  549. return true;
  550. }
  551. void obs_hotkeys_load_encoder(obs_encoder_t *encoder, obs_data_t *hotkeys)
  552. {
  553. if (!encoder || !hotkeys)
  554. return;
  555. if (!lock())
  556. return;
  557. enum_context_hotkeys(&encoder->context, enum_load_bindings, hotkeys);
  558. unlock();
  559. }
  560. void obs_hotkeys_load_output(obs_output_t *output, obs_data_t *hotkeys)
  561. {
  562. if (!output || !hotkeys)
  563. return;
  564. if (!lock())
  565. return;
  566. enum_context_hotkeys(&output->context, enum_load_bindings, hotkeys);
  567. unlock();
  568. }
  569. void obs_hotkeys_load_service(obs_service_t *service, obs_data_t *hotkeys)
  570. {
  571. if (!service || !hotkeys)
  572. return;
  573. if (!lock())
  574. return;
  575. enum_context_hotkeys(&service->context, enum_load_bindings, hotkeys);
  576. unlock();
  577. }
  578. void obs_hotkeys_load_source(obs_source_t *source, obs_data_t *hotkeys)
  579. {
  580. if (!source || !hotkeys)
  581. return;
  582. if (!lock())
  583. return;
  584. enum_context_hotkeys(&source->context, enum_load_bindings, hotkeys);
  585. unlock();
  586. }
  587. void obs_hotkey_pair_load(obs_hotkey_pair_id id, obs_data_array_t *data0,
  588. obs_data_array_t *data1)
  589. {
  590. if ((!data0 && !data1) || !lock())
  591. return;
  592. size_t idx;
  593. if (!find_pair_id(id, &idx))
  594. goto unlock;
  595. obs_hotkey_pair_t *pair = &obs->hotkeys.hotkey_pairs.array[idx];
  596. if (find_id(pair->id[0], &idx)) {
  597. remove_bindings(pair->id[0]);
  598. load_bindings(&obs->hotkeys.hotkeys.array[idx], data0);
  599. }
  600. if (find_id(pair->id[1], &idx)) {
  601. remove_bindings(pair->id[1]);
  602. load_bindings(&obs->hotkeys.hotkeys.array[idx], data1);
  603. }
  604. unlock:
  605. unlock();
  606. }
  607. static inline void save_modifier(uint32_t modifiers, obs_data_t *data,
  608. const char *name, uint32_t flag)
  609. {
  610. if ((modifiers & flag) == flag)
  611. obs_data_set_bool(data, name, true);
  612. }
  613. struct save_bindings_helper_t {
  614. obs_data_array_t *array;
  615. obs_hotkey_t *hotkey;
  616. };
  617. static inline bool save_bindings_helper(void *data,
  618. size_t idx, obs_hotkey_binding_t *binding)
  619. {
  620. UNUSED_PARAMETER(idx);
  621. struct save_bindings_helper_t *h = data;
  622. if (h->hotkey->id != binding->hotkey_id)
  623. return true;
  624. obs_data_t *hotkey = obs_data_create();
  625. uint32_t modifiers = binding->key.modifiers;
  626. save_modifier(modifiers, hotkey, "shift", INTERACT_SHIFT_KEY);
  627. save_modifier(modifiers, hotkey, "control", INTERACT_CONTROL_KEY);
  628. save_modifier(modifiers, hotkey, "alt", INTERACT_ALT_KEY);
  629. save_modifier(modifiers, hotkey, "command", INTERACT_COMMAND_KEY);
  630. obs_data_set_string(hotkey, "key", obs_key_to_name(binding->key.key));
  631. obs_data_array_push_back(h->array, hotkey);
  632. obs_data_release(hotkey);
  633. return true;
  634. }
  635. static inline obs_data_array_t *save_hotkey(obs_hotkey_t *hotkey)
  636. {
  637. obs_data_array_t *data = obs_data_array_create();
  638. struct save_bindings_helper_t arg = {data, hotkey};
  639. enum_bindings(save_bindings_helper, &arg);
  640. return data;
  641. }
  642. obs_data_array_t *obs_hotkey_save(obs_hotkey_id id)
  643. {
  644. size_t idx;
  645. obs_data_array_t *result = NULL;
  646. if (!lock())
  647. return result;
  648. if (find_id(id, &idx))
  649. result = save_hotkey(&obs->hotkeys.hotkeys.array[idx]);
  650. unlock();
  651. return result;
  652. }
  653. static inline bool enum_save_hotkey(void *data,
  654. size_t idx, obs_hotkey_t *hotkey)
  655. {
  656. UNUSED_PARAMETER(idx);
  657. obs_data_array_t *hotkey_data = save_hotkey(hotkey);
  658. obs_data_set_array(data, hotkey->name, hotkey_data);
  659. obs_data_array_release(hotkey_data);
  660. return true;
  661. }
  662. static inline obs_data_t *save_context_hotkeys(struct obs_context_data *context)
  663. {
  664. if (!context->hotkeys.num)
  665. return NULL;
  666. obs_data_t *result = obs_data_create();
  667. enum_context_hotkeys(context, enum_save_hotkey, result);
  668. return result;
  669. }
  670. obs_data_t *obs_hotkeys_save_encoder(obs_encoder_t *encoder)
  671. {
  672. obs_data_t *result = NULL;
  673. if (!lock())
  674. return result;
  675. result = save_context_hotkeys(&encoder->context);
  676. unlock();
  677. return result;
  678. }
  679. obs_data_t *obs_hotkeys_save_output(obs_output_t *output)
  680. {
  681. obs_data_t *result = NULL;
  682. if (!lock())
  683. return result;
  684. result = save_context_hotkeys(&output->context);
  685. unlock();
  686. return result;
  687. }
  688. obs_data_t *obs_hotkeys_save_service(obs_service_t *service)
  689. {
  690. obs_data_t *result = NULL;
  691. if (!lock())
  692. return result;
  693. result = save_context_hotkeys(&service->context);
  694. unlock();
  695. return result;
  696. }
  697. obs_data_t *obs_hotkeys_save_source(obs_source_t *source)
  698. {
  699. obs_data_t *result = NULL;
  700. if (!lock())
  701. return result;
  702. result = save_context_hotkeys(&source->context);
  703. unlock();
  704. return result;
  705. }
  706. struct binding_find_data {
  707. obs_hotkey_id id;
  708. size_t *idx;
  709. bool found;
  710. };
  711. static inline bool binding_finder(void *data,
  712. size_t idx, obs_hotkey_binding_t *binding)
  713. {
  714. struct binding_find_data *find = data;
  715. if (binding->hotkey_id != find->id)
  716. return true;
  717. *find->idx = idx;
  718. find->found = true;
  719. return false;
  720. }
  721. static inline bool find_binding(obs_hotkey_id id, size_t *idx)
  722. {
  723. struct binding_find_data data = {id, idx, false};
  724. enum_bindings(binding_finder, &data);
  725. return data.found;
  726. }
  727. static inline void release_pressed_binding(obs_hotkey_binding_t *binding);
  728. static inline void remove_bindings(obs_hotkey_id id)
  729. {
  730. size_t idx;
  731. while (find_binding(id, &idx)) {
  732. obs_hotkey_binding_t *binding =
  733. &obs->hotkeys.bindings.array[idx];
  734. if (binding->pressed)
  735. release_pressed_binding(binding);
  736. da_erase(obs->hotkeys.bindings, idx);
  737. }
  738. }
  739. static void release_registerer(obs_hotkey_t *hotkey)
  740. {
  741. switch (hotkey->registerer_type) {
  742. case OBS_HOTKEY_REGISTERER_FRONTEND:
  743. break;
  744. case OBS_HOTKEY_REGISTERER_ENCODER:
  745. obs_weak_encoder_release(hotkey->registerer);
  746. break;
  747. case OBS_HOTKEY_REGISTERER_OUTPUT:
  748. obs_weak_output_release(hotkey->registerer);
  749. break;
  750. case OBS_HOTKEY_REGISTERER_SERVICE:
  751. obs_weak_service_release(hotkey->registerer);
  752. break;
  753. case OBS_HOTKEY_REGISTERER_SOURCE:
  754. obs_weak_source_release(hotkey->registerer);
  755. break;
  756. }
  757. hotkey->registerer = NULL;
  758. }
  759. static inline bool unregister_hotkey(obs_hotkey_id id)
  760. {
  761. if (id >= obs->hotkeys.next_id)
  762. return false;
  763. size_t idx;
  764. if (!find_id(id, &idx))
  765. return false;
  766. obs_hotkey_t *hotkey = &obs->hotkeys.hotkeys.array[idx];
  767. hotkey_signal("hotkey_unregister", hotkey);
  768. release_registerer(hotkey);
  769. bfree(hotkey->name);
  770. bfree(hotkey->description);
  771. if (hotkey->registerer_type == OBS_HOTKEY_REGISTERER_SOURCE)
  772. obs_weak_source_release(hotkey->registerer);
  773. da_erase(obs->hotkeys.hotkeys, idx);
  774. remove_bindings(id);
  775. return obs->hotkeys.hotkeys.num >= idx;
  776. }
  777. static inline bool unregister_hotkey_pair(obs_hotkey_pair_id id)
  778. {
  779. if (id >= obs->hotkeys.next_pair_id)
  780. return false;
  781. size_t idx;
  782. if (!find_pair_id(id, &idx))
  783. return false;
  784. obs_hotkey_pair_t *pair = &obs->hotkeys.hotkey_pairs.array[idx];
  785. bool need_fixup = unregister_hotkey(pair->id[0]);
  786. need_fixup = unregister_hotkey(pair->id[1]) || need_fixup;
  787. if (need_fixup)
  788. fixup_pointers();
  789. da_erase(obs->hotkeys.hotkey_pairs, idx);
  790. return obs->hotkeys.hotkey_pairs.num >= idx;
  791. }
  792. void obs_hotkey_unregister(obs_hotkey_id id)
  793. {
  794. if (!lock())
  795. return;
  796. if (unregister_hotkey(id))
  797. fixup_pointers();
  798. unlock();
  799. }
  800. void obs_hotkey_pair_unregister(obs_hotkey_pair_id id)
  801. {
  802. if (!lock())
  803. return;
  804. if (unregister_hotkey_pair(id))
  805. fixup_pair_pointers();
  806. unlock();
  807. }
  808. static void context_release_hotkeys(struct obs_context_data *context)
  809. {
  810. if (!context->hotkeys.num)
  811. goto cleanup;
  812. bool need_fixup = false;
  813. for (size_t i = 0; i < context->hotkeys.num; i++)
  814. need_fixup = unregister_hotkey(context->hotkeys.array[i]) ||
  815. need_fixup;
  816. if (need_fixup)
  817. fixup_pointers();
  818. cleanup:
  819. da_free(context->hotkeys);
  820. }
  821. static void context_release_hotkey_pairs(struct obs_context_data *context)
  822. {
  823. if (!context->hotkey_pairs.num)
  824. goto cleanup;
  825. bool need_fixup = false;
  826. for (size_t i = 0; i < context->hotkey_pairs.num; i++)
  827. need_fixup =
  828. unregister_hotkey_pair(context->hotkey_pairs.array[i])
  829. || need_fixup;
  830. if (need_fixup)
  831. fixup_pair_pointers();
  832. cleanup:
  833. da_free(context->hotkey_pairs);
  834. }
  835. void obs_hotkeys_context_release(struct obs_context_data *context)
  836. {
  837. if (!lock())
  838. return;
  839. context_release_hotkeys(context);
  840. context_release_hotkey_pairs(context);
  841. obs_data_release(context->hotkey_data);
  842. unlock();
  843. }
  844. void obs_hotkeys_free(void)
  845. {
  846. const size_t num = obs->hotkeys.hotkeys.num;
  847. obs_hotkey_t *hotkeys = obs->hotkeys.hotkeys.array;
  848. for (size_t i = 0; i < num; i++) {
  849. bfree(hotkeys[i].name);
  850. bfree(hotkeys[i].description);
  851. release_registerer(&hotkeys[i]);
  852. }
  853. da_free(obs->hotkeys.bindings);
  854. da_free(obs->hotkeys.hotkeys);
  855. da_free(obs->hotkeys.hotkey_pairs);
  856. for (size_t i = 0; i < OBS_KEY_LAST_VALUE; i++) {
  857. if (obs->hotkeys.translations[i]) {
  858. bfree(obs->hotkeys.translations[i]);
  859. obs->hotkeys.translations[i] = NULL;
  860. }
  861. }
  862. }
  863. struct obs_hotkey_internal_enum_forward {
  864. obs_hotkey_enum_func func;
  865. void *data;
  866. };
  867. static inline bool enum_hotkey(void *data, size_t idx, obs_hotkey_t *hotkey)
  868. {
  869. UNUSED_PARAMETER(idx);
  870. struct obs_hotkey_internal_enum_forward *forward = data;
  871. return forward->func(forward->data, hotkey->id, hotkey);
  872. }
  873. void obs_enum_hotkeys(obs_hotkey_enum_func func, void *data)
  874. {
  875. struct obs_hotkey_internal_enum_forward forwarder = {func, data};
  876. if (!lock())
  877. return;
  878. enum_hotkeys(enum_hotkey, &forwarder);
  879. unlock();
  880. }
  881. void obs_enum_hotkey_bindings(obs_hotkey_binding_enum_func func, void *data)
  882. {
  883. if (!lock())
  884. return;
  885. enum_bindings(func, data);
  886. unlock();
  887. }
  888. static inline bool modifiers_match(obs_hotkey_binding_t *binding,
  889. uint32_t modifiers_, bool strict_modifiers)
  890. {
  891. uint32_t modifiers = binding->key.modifiers;
  892. return !modifiers ||
  893. (!strict_modifiers && (modifiers & modifiers_) == modifiers) ||
  894. (strict_modifiers && modifiers == modifiers_);
  895. }
  896. static inline bool is_pressed(obs_key_t key)
  897. {
  898. return obs_hotkeys_platform_is_pressed(obs->hotkeys.platform_context,
  899. key);
  900. }
  901. static inline void press_released_binding(obs_hotkey_binding_t *binding)
  902. {
  903. binding->pressed = true;
  904. obs_hotkey_t *hotkey = binding->hotkey;
  905. if (hotkey->pressed++)
  906. return;
  907. if (!obs->hotkeys.reroute_hotkeys)
  908. hotkey->func(hotkey->data, hotkey->id, hotkey, true);
  909. else if (obs->hotkeys.router_func)
  910. obs->hotkeys.router_func(obs->hotkeys.router_func_data,
  911. hotkey->id, true);
  912. }
  913. static inline void release_pressed_binding(obs_hotkey_binding_t *binding)
  914. {
  915. binding->pressed = false;
  916. obs_hotkey_t *hotkey = binding->hotkey;
  917. if (--hotkey->pressed)
  918. return;
  919. if (!obs->hotkeys.reroute_hotkeys)
  920. hotkey->func(hotkey->data, hotkey->id, hotkey, false);
  921. else if (obs->hotkeys.router_func)
  922. obs->hotkeys.router_func(obs->hotkeys.router_func_data,
  923. hotkey->id, false);
  924. }
  925. static inline void handle_binding(obs_hotkey_binding_t *binding,
  926. uint32_t modifiers, bool no_press, bool strict_modifiers,
  927. bool *pressed)
  928. {
  929. bool modifiers_match_ = modifiers_match(binding, modifiers,
  930. strict_modifiers);
  931. bool modifiers_only = binding->key.key == OBS_KEY_NONE;
  932. if (!binding->key.modifiers)
  933. binding->modifiers_match = true;
  934. if (modifiers_only)
  935. pressed = &modifiers_only;
  936. if (!binding->key.modifiers && modifiers_only)
  937. goto reset;
  938. if ((!binding->modifiers_match && !modifiers_only) || !modifiers_match_)
  939. goto reset;
  940. if ((pressed && !*pressed) ||
  941. (!pressed && !is_pressed(binding->key.key)))
  942. goto reset;
  943. if (binding->pressed || no_press)
  944. return;
  945. press_released_binding(binding);
  946. return;
  947. reset:
  948. binding->modifiers_match = modifiers_match_;
  949. if (!binding->pressed)
  950. return;
  951. release_pressed_binding(binding);
  952. }
  953. struct obs_hotkey_internal_inject {
  954. obs_key_combination_t hotkey;
  955. bool pressed : 1;
  956. bool strict_modifiers : 1;
  957. };
  958. static inline bool inject_hotkey(void *data,
  959. size_t idx, obs_hotkey_binding_t *binding)
  960. {
  961. UNUSED_PARAMETER(idx);
  962. struct obs_hotkey_internal_inject *event = data;
  963. if (modifiers_match(binding, event->hotkey.modifiers,
  964. event->strict_modifiers)) {
  965. bool pressed = binding->key.key == event->hotkey.key &&
  966. event->pressed;
  967. handle_binding(binding, event->hotkey.modifiers, false,
  968. event->strict_modifiers, &pressed);
  969. }
  970. return true;
  971. }
  972. void obs_hotkey_inject_event(obs_key_combination_t hotkey, bool pressed)
  973. {
  974. if (!lock())
  975. return;
  976. struct obs_hotkey_internal_inject event = {
  977. {hotkey.modifiers, hotkey.key},
  978. pressed, obs->hotkeys.strict_modifiers,
  979. };
  980. enum_bindings(inject_hotkey, &event);
  981. unlock();
  982. }
  983. void obs_hotkey_enable_background_press(bool enable)
  984. {
  985. if (!lock())
  986. return;
  987. obs->hotkeys.thread_disable_press = !enable;
  988. unlock();
  989. }
  990. void obs_hotkey_enable_strict_modifiers(bool enable)
  991. {
  992. if (!lock())
  993. return;
  994. obs->hotkeys.strict_modifiers = enable;
  995. unlock();
  996. }
  997. struct obs_query_hotkeys_helper {
  998. uint32_t modifiers;
  999. bool no_press : 1;
  1000. bool strict_modifiers : 1;
  1001. };
  1002. static inline bool query_hotkey(void *data,
  1003. size_t idx, obs_hotkey_binding_t *binding)
  1004. {
  1005. UNUSED_PARAMETER(idx);
  1006. struct obs_query_hotkeys_helper *param =
  1007. (struct obs_query_hotkeys_helper*)data;
  1008. handle_binding(binding, param->modifiers, param->no_press,
  1009. param->strict_modifiers, NULL);
  1010. return true;
  1011. }
  1012. static inline void query_hotkeys()
  1013. {
  1014. uint32_t modifiers = 0;
  1015. if (is_pressed(OBS_KEY_SHIFT))
  1016. modifiers |= INTERACT_SHIFT_KEY;
  1017. if (is_pressed(OBS_KEY_CONTROL))
  1018. modifiers |= INTERACT_CONTROL_KEY;
  1019. if (is_pressed(OBS_KEY_ALT))
  1020. modifiers |= INTERACT_ALT_KEY;
  1021. if (is_pressed(OBS_KEY_META))
  1022. modifiers |= INTERACT_COMMAND_KEY;
  1023. struct obs_query_hotkeys_helper param = {
  1024. modifiers,
  1025. obs->hotkeys.thread_disable_press,
  1026. obs->hotkeys.strict_modifiers,
  1027. };
  1028. enum_bindings(query_hotkey, &param);
  1029. }
  1030. void *obs_hotkey_thread(void *arg)
  1031. {
  1032. UNUSED_PARAMETER(arg);
  1033. while (os_event_timedwait(obs->hotkeys.stop_event, 25) == ETIMEDOUT) {
  1034. if (!lock())
  1035. continue;
  1036. query_hotkeys();
  1037. unlock();
  1038. }
  1039. return NULL;
  1040. }
  1041. void obs_hotkey_trigger_routed_callback(obs_hotkey_id id, bool pressed)
  1042. {
  1043. if (!lock())
  1044. return;
  1045. if (!obs->hotkeys.reroute_hotkeys)
  1046. goto unlock;
  1047. size_t idx;
  1048. if (!find_id(id, &idx))
  1049. goto unlock;
  1050. obs_hotkey_t *hotkey = &obs->hotkeys.hotkeys.array[idx];
  1051. hotkey->func(hotkey->data, id, hotkey, pressed);
  1052. unlock:
  1053. unlock();
  1054. }
  1055. void obs_hotkey_set_callback_routing_func(obs_hotkey_callback_router_func func,
  1056. void *data)
  1057. {
  1058. if (!lock())
  1059. return;
  1060. obs->hotkeys.router_func = func;
  1061. obs->hotkeys.router_func_data = data;
  1062. unlock();
  1063. }
  1064. void obs_hotkey_enable_callback_rerouting(bool enable)
  1065. {
  1066. if (!lock())
  1067. return;
  1068. obs->hotkeys.reroute_hotkeys = enable;
  1069. unlock();
  1070. }
  1071. static void obs_set_key_translation(obs_key_t key, const char *translation)
  1072. {
  1073. bfree(obs->hotkeys.translations[key]);
  1074. obs->hotkeys.translations[key] = NULL;
  1075. if (translation)
  1076. obs->hotkeys.translations[key] = bstrdup(translation);
  1077. }
  1078. void obs_hotkeys_set_translations_s(
  1079. struct obs_hotkeys_translations *translations, size_t size)
  1080. {
  1081. #define ADD_TRANSLATION(key_name, var_name) \
  1082. if (t.var_name) \
  1083. obs_set_key_translation(key_name, t.var_name);
  1084. struct obs_hotkeys_translations t = {0};
  1085. struct dstr numpad = {0};
  1086. struct dstr mouse = {0};
  1087. struct dstr button = {0};
  1088. if (!translations) {
  1089. return;
  1090. }
  1091. memcpy(&t, translations, (size < sizeof(t)) ? size : sizeof(t));
  1092. ADD_TRANSLATION(OBS_KEY_INSERT, insert);
  1093. ADD_TRANSLATION(OBS_KEY_DELETE, del);
  1094. ADD_TRANSLATION(OBS_KEY_HOME, home);
  1095. ADD_TRANSLATION(OBS_KEY_END, end);
  1096. ADD_TRANSLATION(OBS_KEY_PAGEUP, page_up);
  1097. ADD_TRANSLATION(OBS_KEY_PAGEDOWN, page_down);
  1098. ADD_TRANSLATION(OBS_KEY_NUMLOCK, num_lock);
  1099. ADD_TRANSLATION(OBS_KEY_SCROLLLOCK, scroll_lock);
  1100. ADD_TRANSLATION(OBS_KEY_CAPSLOCK, caps_lock);
  1101. ADD_TRANSLATION(OBS_KEY_BACKSPACE, backspace);
  1102. ADD_TRANSLATION(OBS_KEY_TAB, tab);
  1103. ADD_TRANSLATION(OBS_KEY_PRINT, print);
  1104. ADD_TRANSLATION(OBS_KEY_PAUSE, pause);
  1105. ADD_TRANSLATION(OBS_KEY_SHIFT, shift);
  1106. ADD_TRANSLATION(OBS_KEY_ALT, alt);
  1107. ADD_TRANSLATION(OBS_KEY_CONTROL, control);
  1108. ADD_TRANSLATION(OBS_KEY_META, meta);
  1109. ADD_TRANSLATION(OBS_KEY_MENU, menu);
  1110. ADD_TRANSLATION(OBS_KEY_SPACE, space);
  1111. #ifdef __APPLE__
  1112. const char *numpad_str = t.apple_keypad_num;
  1113. ADD_TRANSLATION(OBS_KEY_NUMSLASH, apple_keypad_divide);
  1114. ADD_TRANSLATION(OBS_KEY_NUMASTERISK, apple_keypad_multiply);
  1115. ADD_TRANSLATION(OBS_KEY_NUMMINUS, apple_keypad_minus);
  1116. ADD_TRANSLATION(OBS_KEY_NUMPLUS, apple_keypad_plus);
  1117. ADD_TRANSLATION(OBS_KEY_NUMPERIOD, apple_keypad_decimal);
  1118. ADD_TRANSLATION(OBS_KEY_NUMEQUAL, apple_keypad_equal);
  1119. #else
  1120. const char *numpad_str = t.numpad_num;
  1121. ADD_TRANSLATION(OBS_KEY_NUMSLASH, numpad_divide);
  1122. ADD_TRANSLATION(OBS_KEY_NUMASTERISK, numpad_multiply);
  1123. ADD_TRANSLATION(OBS_KEY_NUMMINUS, numpad_minus);
  1124. ADD_TRANSLATION(OBS_KEY_NUMPLUS, numpad_plus);
  1125. ADD_TRANSLATION(OBS_KEY_NUMPERIOD, numpad_decimal);
  1126. #endif
  1127. if (numpad_str) {
  1128. dstr_copy(&numpad, numpad_str);
  1129. dstr_depad(&numpad);
  1130. if (dstr_find(&numpad, "%1") == NULL) {
  1131. dstr_cat(&numpad, " %1");
  1132. }
  1133. #define ADD_NUMPAD_NUM(idx) \
  1134. dstr_copy_dstr(&button, &numpad); \
  1135. dstr_replace(&button, "%1", #idx); \
  1136. obs_set_key_translation(OBS_KEY_NUM ## idx, button.array)
  1137. ADD_NUMPAD_NUM(0);
  1138. ADD_NUMPAD_NUM(1);
  1139. ADD_NUMPAD_NUM(2);
  1140. ADD_NUMPAD_NUM(3);
  1141. ADD_NUMPAD_NUM(4);
  1142. ADD_NUMPAD_NUM(5);
  1143. ADD_NUMPAD_NUM(6);
  1144. ADD_NUMPAD_NUM(7);
  1145. ADD_NUMPAD_NUM(8);
  1146. ADD_NUMPAD_NUM(9);
  1147. }
  1148. if (t.mouse_num) {
  1149. dstr_copy(&mouse, t.mouse_num);
  1150. dstr_depad(&mouse);
  1151. if (dstr_find(&mouse, "%1") == NULL) {
  1152. dstr_cat(&mouse, " %1");
  1153. }
  1154. #define ADD_MOUSE_NUM(idx) \
  1155. dstr_copy_dstr(&button, &mouse); \
  1156. dstr_replace(&button, "%1", #idx); \
  1157. obs_set_key_translation(OBS_KEY_MOUSE ## idx, button.array)
  1158. ADD_MOUSE_NUM(1);
  1159. ADD_MOUSE_NUM(2);
  1160. ADD_MOUSE_NUM(3);
  1161. ADD_MOUSE_NUM(4);
  1162. ADD_MOUSE_NUM(5);
  1163. ADD_MOUSE_NUM(6);
  1164. ADD_MOUSE_NUM(7);
  1165. ADD_MOUSE_NUM(8);
  1166. ADD_MOUSE_NUM(9);
  1167. ADD_MOUSE_NUM(10);
  1168. ADD_MOUSE_NUM(11);
  1169. ADD_MOUSE_NUM(12);
  1170. ADD_MOUSE_NUM(13);
  1171. ADD_MOUSE_NUM(14);
  1172. ADD_MOUSE_NUM(15);
  1173. ADD_MOUSE_NUM(16);
  1174. ADD_MOUSE_NUM(17);
  1175. ADD_MOUSE_NUM(18);
  1176. ADD_MOUSE_NUM(19);
  1177. ADD_MOUSE_NUM(20);
  1178. ADD_MOUSE_NUM(21);
  1179. ADD_MOUSE_NUM(22);
  1180. ADD_MOUSE_NUM(23);
  1181. ADD_MOUSE_NUM(24);
  1182. ADD_MOUSE_NUM(25);
  1183. ADD_MOUSE_NUM(26);
  1184. ADD_MOUSE_NUM(27);
  1185. ADD_MOUSE_NUM(28);
  1186. ADD_MOUSE_NUM(29);
  1187. }
  1188. dstr_free(&numpad);
  1189. dstr_free(&mouse);
  1190. dstr_free(&button);
  1191. }
  1192. const char *obs_get_hotkey_translation(obs_key_t key, const char *def)
  1193. {
  1194. if (key == OBS_KEY_NONE) {
  1195. return NULL;
  1196. }
  1197. return obs->hotkeys.translations[key] ?
  1198. obs->hotkeys.translations[key] : def;
  1199. }
  1200. void obs_hotkey_update_atomic(obs_hotkey_atomic_update_func func, void *data)
  1201. {
  1202. if (!lock()) return;
  1203. func(data);
  1204. unlock();
  1205. }
  1206. void obs_hotkeys_set_audio_hotkeys_translations(
  1207. const char *mute, const char *unmute,
  1208. const char *push_to_mute, const char *push_to_talk)
  1209. {
  1210. #define SET_T(n) bfree(obs->hotkeys.n); obs->hotkeys.n = bstrdup(n)
  1211. SET_T(mute);
  1212. SET_T(unmute);
  1213. SET_T(push_to_mute);
  1214. SET_T(push_to_talk);
  1215. #undef SET_T
  1216. }
  1217. void obs_hotkeys_set_sceneitem_hotkeys_translations(
  1218. const char *show, const char *hide)
  1219. {
  1220. #define SET_T(n) bfree(obs->hotkeys.sceneitem_##n); \
  1221. obs->hotkeys.sceneitem_##n = bstrdup(n)
  1222. SET_T(show);
  1223. SET_T(hide);
  1224. #undef SET_T
  1225. }