1
0

obs-hotkey.c 35 KB

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