obs-hotkey.c 38 KB

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