obs-hotkey.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509
  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. }