obs-hotkey.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481
  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. hotkey_signal("hotkey_bindings_changed", hotkey);
  451. }
  452. static inline void remove_bindings(obs_hotkey_id id);
  453. void obs_hotkey_load_bindings(obs_hotkey_id id,
  454. obs_key_combination_t *combinations, size_t num)
  455. {
  456. if (!lock())
  457. return;
  458. obs_hotkey_t *hotkey;
  459. HASH_FIND_HKEY(obs->hotkeys.hotkeys, id, hotkey);
  460. if (hotkey) {
  461. remove_bindings(id);
  462. for (size_t i = 0; i < num; i++)
  463. create_binding(hotkey, combinations[i]);
  464. hotkey_signal("hotkey_bindings_changed", hotkey);
  465. }
  466. unlock();
  467. }
  468. void obs_hotkey_load(obs_hotkey_id id, obs_data_array_t *data)
  469. {
  470. if (!lock())
  471. return;
  472. obs_hotkey_t *hotkey;
  473. HASH_FIND_HKEY(obs->hotkeys.hotkeys, id, hotkey);
  474. if (hotkey) {
  475. remove_bindings(id);
  476. load_bindings(hotkey, data);
  477. }
  478. unlock();
  479. }
  480. static inline bool enum_load_bindings(void *data, obs_hotkey_t *hotkey)
  481. {
  482. obs_data_array_t *hotkey_data = obs_data_get_array(data, hotkey->name);
  483. if (!hotkey_data)
  484. return true;
  485. load_bindings(hotkey, hotkey_data);
  486. obs_data_array_release(hotkey_data);
  487. return true;
  488. }
  489. void obs_hotkeys_load_encoder(obs_encoder_t *encoder, obs_data_t *hotkeys)
  490. {
  491. if (!encoder || !hotkeys)
  492. return;
  493. if (!lock())
  494. return;
  495. enum_context_hotkeys(&encoder->context, enum_load_bindings, hotkeys);
  496. unlock();
  497. }
  498. void obs_hotkeys_load_output(obs_output_t *output, obs_data_t *hotkeys)
  499. {
  500. if (!output || !hotkeys)
  501. return;
  502. if (!lock())
  503. return;
  504. enum_context_hotkeys(&output->context, enum_load_bindings, hotkeys);
  505. unlock();
  506. }
  507. void obs_hotkeys_load_service(obs_service_t *service, obs_data_t *hotkeys)
  508. {
  509. if (!service || !hotkeys)
  510. return;
  511. if (!lock())
  512. return;
  513. enum_context_hotkeys(&service->context, enum_load_bindings, hotkeys);
  514. unlock();
  515. }
  516. void obs_hotkeys_load_source(obs_source_t *source, obs_data_t *hotkeys)
  517. {
  518. if (!source || !hotkeys)
  519. return;
  520. if (!lock())
  521. return;
  522. enum_context_hotkeys(&source->context, enum_load_bindings, hotkeys);
  523. unlock();
  524. }
  525. void obs_hotkey_pair_load(obs_hotkey_pair_id id, obs_data_array_t *data0,
  526. obs_data_array_t *data1)
  527. {
  528. if ((!data0 && !data1) || !lock())
  529. return;
  530. obs_hotkey_pair_t *pair;
  531. HASH_FIND_HKEY(obs->hotkeys.hotkey_pairs, id, pair);
  532. if (!pair)
  533. goto unlock;
  534. obs_hotkey_t *p1, *p2;
  535. HASH_FIND_HKEY(obs->hotkeys.hotkeys, pair->id[0], p1);
  536. HASH_FIND_HKEY(obs->hotkeys.hotkeys, pair->id[1], p2);
  537. if (p1) {
  538. remove_bindings(pair->id[0]);
  539. load_bindings(p1, data0);
  540. }
  541. if (p2) {
  542. remove_bindings(pair->id[1]);
  543. load_bindings(p2, data1);
  544. }
  545. unlock:
  546. unlock();
  547. }
  548. static inline void save_modifier(uint32_t modifiers, obs_data_t *data,
  549. const char *name, uint32_t flag)
  550. {
  551. if ((modifiers & flag) == flag)
  552. obs_data_set_bool(data, name, true);
  553. }
  554. struct save_bindings_helper_t {
  555. obs_data_array_t *array;
  556. obs_hotkey_t *hotkey;
  557. };
  558. static inline bool save_bindings_helper(void *data, size_t idx,
  559. obs_hotkey_binding_t *binding)
  560. {
  561. UNUSED_PARAMETER(idx);
  562. struct save_bindings_helper_t *h = data;
  563. if (h->hotkey->id != binding->hotkey_id)
  564. return true;
  565. obs_data_t *hotkey = obs_data_create();
  566. uint32_t modifiers = binding->key.modifiers;
  567. save_modifier(modifiers, hotkey, "shift", INTERACT_SHIFT_KEY);
  568. save_modifier(modifiers, hotkey, "control", INTERACT_CONTROL_KEY);
  569. save_modifier(modifiers, hotkey, "alt", INTERACT_ALT_KEY);
  570. save_modifier(modifiers, hotkey, "command", INTERACT_COMMAND_KEY);
  571. obs_data_set_string(hotkey, "key", obs_key_to_name(binding->key.key));
  572. obs_data_array_push_back(h->array, hotkey);
  573. obs_data_release(hotkey);
  574. return true;
  575. }
  576. static inline obs_data_array_t *save_hotkey(obs_hotkey_t *hotkey)
  577. {
  578. obs_data_array_t *data = obs_data_array_create();
  579. struct save_bindings_helper_t arg = {data, hotkey};
  580. enum_bindings(save_bindings_helper, &arg);
  581. return data;
  582. }
  583. obs_data_array_t *obs_hotkey_save(obs_hotkey_id id)
  584. {
  585. obs_data_array_t *result = NULL;
  586. if (!lock())
  587. return result;
  588. obs_hotkey_t *hotkey;
  589. HASH_FIND_HKEY(obs->hotkeys.hotkeys, id, hotkey);
  590. if (hotkey)
  591. result = save_hotkey(hotkey);
  592. unlock();
  593. return result;
  594. }
  595. void obs_hotkey_pair_save(obs_hotkey_pair_id id, obs_data_array_t **p_data0,
  596. obs_data_array_t **p_data1)
  597. {
  598. if ((!p_data0 && !p_data1) || !lock())
  599. return;
  600. obs_hotkey_pair_t *pair;
  601. HASH_FIND_HKEY(obs->hotkeys.hotkey_pairs, id, pair);
  602. if (!pair)
  603. goto unlock;
  604. obs_hotkey_t *hotkey;
  605. if (p_data0) {
  606. HASH_FIND_HKEY(obs->hotkeys.hotkeys, pair->id[0], hotkey);
  607. if (hotkey)
  608. *p_data0 = save_hotkey(hotkey);
  609. }
  610. if (p_data1) {
  611. HASH_FIND_HKEY(obs->hotkeys.hotkeys, pair->id[1], hotkey);
  612. if (hotkey)
  613. *p_data1 = save_hotkey(hotkey);
  614. }
  615. unlock:
  616. unlock();
  617. }
  618. static inline bool enum_save_hotkey(void *data, obs_hotkey_t *hotkey)
  619. {
  620. obs_data_array_t *hotkey_data = save_hotkey(hotkey);
  621. obs_data_set_array(data, hotkey->name, hotkey_data);
  622. obs_data_array_release(hotkey_data);
  623. return true;
  624. }
  625. static inline obs_data_t *save_context_hotkeys(struct obs_context_data *context)
  626. {
  627. if (!context->hotkeys.num)
  628. return NULL;
  629. obs_data_t *result = obs_data_create();
  630. enum_context_hotkeys(context, enum_save_hotkey, result);
  631. return result;
  632. }
  633. obs_data_t *obs_hotkeys_save_encoder(obs_encoder_t *encoder)
  634. {
  635. obs_data_t *result = NULL;
  636. if (!lock())
  637. return result;
  638. result = save_context_hotkeys(&encoder->context);
  639. unlock();
  640. return result;
  641. }
  642. obs_data_t *obs_hotkeys_save_output(obs_output_t *output)
  643. {
  644. obs_data_t *result = NULL;
  645. if (!lock())
  646. return result;
  647. result = save_context_hotkeys(&output->context);
  648. unlock();
  649. return result;
  650. }
  651. obs_data_t *obs_hotkeys_save_service(obs_service_t *service)
  652. {
  653. obs_data_t *result = NULL;
  654. if (!lock())
  655. return result;
  656. result = save_context_hotkeys(&service->context);
  657. unlock();
  658. return result;
  659. }
  660. obs_data_t *obs_hotkeys_save_source(obs_source_t *source)
  661. {
  662. obs_data_t *result = NULL;
  663. if (!lock())
  664. return result;
  665. result = save_context_hotkeys(&source->context);
  666. unlock();
  667. return result;
  668. }
  669. struct binding_find_data {
  670. obs_hotkey_id id;
  671. size_t *idx;
  672. bool found;
  673. };
  674. static inline bool binding_finder(void *data, size_t idx,
  675. obs_hotkey_binding_t *binding)
  676. {
  677. struct binding_find_data *find = data;
  678. if (binding->hotkey_id != find->id)
  679. return true;
  680. *find->idx = idx;
  681. find->found = true;
  682. return false;
  683. }
  684. static inline bool find_binding(obs_hotkey_id id, size_t *idx)
  685. {
  686. struct binding_find_data data = {id, idx, false};
  687. enum_bindings(binding_finder, &data);
  688. return data.found;
  689. }
  690. static inline void release_pressed_binding(obs_hotkey_binding_t *binding);
  691. static inline void remove_bindings(obs_hotkey_id id)
  692. {
  693. size_t idx;
  694. while (find_binding(id, &idx)) {
  695. obs_hotkey_binding_t *binding =
  696. &obs->hotkeys.bindings.array[idx];
  697. if (binding->pressed)
  698. release_pressed_binding(binding);
  699. da_erase(obs->hotkeys.bindings, idx);
  700. }
  701. }
  702. static void release_registerer(obs_hotkey_t *hotkey)
  703. {
  704. switch (hotkey->registerer_type) {
  705. case OBS_HOTKEY_REGISTERER_FRONTEND:
  706. break;
  707. case OBS_HOTKEY_REGISTERER_ENCODER:
  708. obs_weak_encoder_release(hotkey->registerer);
  709. break;
  710. case OBS_HOTKEY_REGISTERER_OUTPUT:
  711. obs_weak_output_release(hotkey->registerer);
  712. break;
  713. case OBS_HOTKEY_REGISTERER_SERVICE:
  714. obs_weak_service_release(hotkey->registerer);
  715. break;
  716. case OBS_HOTKEY_REGISTERER_SOURCE:
  717. obs_weak_source_release(hotkey->registerer);
  718. break;
  719. }
  720. hotkey->registerer = NULL;
  721. }
  722. static inline void unregister_hotkey(obs_hotkey_id id)
  723. {
  724. if (id >= obs->hotkeys.next_id)
  725. return;
  726. obs_hotkey_t *hotkey;
  727. HASH_FIND_HKEY(obs->hotkeys.hotkeys, id, hotkey);
  728. if (!hotkey)
  729. return;
  730. HASH_DEL(obs->hotkeys.hotkeys, hotkey);
  731. hotkey_signal("hotkey_unregister", hotkey);
  732. release_registerer(hotkey);
  733. if (hotkey->registerer_type == OBS_HOTKEY_REGISTERER_SOURCE)
  734. obs_weak_source_release(hotkey->registerer);
  735. bfree(hotkey->name);
  736. bfree(hotkey->description);
  737. bfree(hotkey);
  738. remove_bindings(id);
  739. }
  740. static inline void unregister_hotkey_pair(obs_hotkey_pair_id id)
  741. {
  742. if (id >= obs->hotkeys.next_pair_id)
  743. return;
  744. obs_hotkey_pair_t *pair;
  745. HASH_FIND_HKEY(obs->hotkeys.hotkey_pairs, id, pair);
  746. if (!pair)
  747. return;
  748. unregister_hotkey(pair->id[0]);
  749. unregister_hotkey(pair->id[1]);
  750. HASH_DEL(obs->hotkeys.hotkey_pairs, pair);
  751. bfree(pair);
  752. }
  753. void obs_hotkey_unregister(obs_hotkey_id id)
  754. {
  755. if (!lock())
  756. return;
  757. unregister_hotkey(id);
  758. unlock();
  759. }
  760. void obs_hotkey_pair_unregister(obs_hotkey_pair_id id)
  761. {
  762. if (!lock())
  763. return;
  764. unregister_hotkey_pair(id);
  765. unlock();
  766. }
  767. static void context_release_hotkeys(struct obs_context_data *context)
  768. {
  769. if (!context->hotkeys.num)
  770. goto cleanup;
  771. for (size_t i = 0; i < context->hotkeys.num; i++)
  772. unregister_hotkey(context->hotkeys.array[i]);
  773. cleanup:
  774. da_free(context->hotkeys);
  775. }
  776. static void context_release_hotkey_pairs(struct obs_context_data *context)
  777. {
  778. if (!context->hotkey_pairs.num)
  779. goto cleanup;
  780. for (size_t i = 0; i < context->hotkey_pairs.num; i++)
  781. unregister_hotkey_pair(context->hotkey_pairs.array[i]);
  782. cleanup:
  783. da_free(context->hotkey_pairs);
  784. }
  785. void obs_hotkeys_context_release(struct obs_context_data *context)
  786. {
  787. if (!lock())
  788. return;
  789. context_release_hotkeys(context);
  790. context_release_hotkey_pairs(context);
  791. obs_data_release(context->hotkey_data);
  792. unlock();
  793. }
  794. void obs_hotkeys_free(void)
  795. {
  796. obs_hotkey_t *hotkey, *tmp;
  797. HASH_ITER (hh, obs->hotkeys.hotkeys, hotkey, tmp) {
  798. HASH_DEL(obs->hotkeys.hotkeys, hotkey);
  799. bfree(hotkey->name);
  800. bfree(hotkey->description);
  801. release_registerer(hotkey);
  802. bfree(hotkey);
  803. }
  804. obs_hotkey_pair_t *pair, *tmp2;
  805. HASH_ITER (hh, obs->hotkeys.hotkey_pairs, pair, tmp2) {
  806. HASH_DEL(obs->hotkeys.hotkey_pairs, pair);
  807. bfree(pair);
  808. }
  809. da_free(obs->hotkeys.bindings);
  810. for (size_t i = 0; i < OBS_KEY_LAST_VALUE; i++) {
  811. if (obs->hotkeys.translations[i]) {
  812. bfree(obs->hotkeys.translations[i]);
  813. obs->hotkeys.translations[i] = NULL;
  814. }
  815. }
  816. }
  817. void obs_enum_hotkeys(obs_hotkey_enum_func func, void *data)
  818. {
  819. if (!lock())
  820. return;
  821. obs_hotkey_t *hk, *tmp;
  822. HASH_ITER (hh, obs->hotkeys.hotkeys, hk, tmp) {
  823. if (!func(data, hk->id, hk))
  824. break;
  825. }
  826. unlock();
  827. }
  828. void obs_enum_hotkey_bindings(obs_hotkey_binding_enum_func func, void *data)
  829. {
  830. if (!lock())
  831. return;
  832. enum_bindings(func, data);
  833. unlock();
  834. }
  835. static inline bool modifiers_match(obs_hotkey_binding_t *binding,
  836. uint32_t modifiers_, bool strict_modifiers)
  837. {
  838. uint32_t modifiers = binding->key.modifiers;
  839. if (!strict_modifiers)
  840. return (modifiers & modifiers_) == modifiers;
  841. else
  842. return modifiers == modifiers_;
  843. }
  844. static inline bool is_pressed(obs_key_t key)
  845. {
  846. return obs_hotkeys_platform_is_pressed(obs->hotkeys.platform_context,
  847. key);
  848. }
  849. static inline void press_released_binding(obs_hotkey_binding_t *binding)
  850. {
  851. binding->pressed = true;
  852. obs_hotkey_t *hotkey = binding->hotkey;
  853. if (hotkey->pressed++)
  854. return;
  855. if (!obs->hotkeys.reroute_hotkeys)
  856. hotkey->func(hotkey->data, hotkey->id, hotkey, true);
  857. else if (obs->hotkeys.router_func)
  858. obs->hotkeys.router_func(obs->hotkeys.router_func_data,
  859. hotkey->id, true);
  860. }
  861. static inline void release_pressed_binding(obs_hotkey_binding_t *binding)
  862. {
  863. binding->pressed = false;
  864. obs_hotkey_t *hotkey = binding->hotkey;
  865. if (--hotkey->pressed)
  866. return;
  867. if (!obs->hotkeys.reroute_hotkeys)
  868. hotkey->func(hotkey->data, hotkey->id, hotkey, false);
  869. else if (obs->hotkeys.router_func)
  870. obs->hotkeys.router_func(obs->hotkeys.router_func_data,
  871. hotkey->id, false);
  872. }
  873. static inline void handle_binding(obs_hotkey_binding_t *binding,
  874. uint32_t modifiers, bool no_press,
  875. bool strict_modifiers, bool *pressed)
  876. {
  877. bool modifiers_match_ =
  878. modifiers_match(binding, modifiers, strict_modifiers);
  879. bool modifiers_only = binding->key.key == OBS_KEY_NONE;
  880. if (!strict_modifiers && !binding->key.modifiers)
  881. binding->modifiers_match = true;
  882. if (modifiers_only)
  883. pressed = &modifiers_only;
  884. if (!binding->key.modifiers && modifiers_only)
  885. goto reset;
  886. if ((!binding->modifiers_match && !modifiers_only) || !modifiers_match_)
  887. goto reset;
  888. if ((pressed && !*pressed) ||
  889. (!pressed && !is_pressed(binding->key.key)))
  890. goto reset;
  891. if (binding->pressed || no_press)
  892. return;
  893. press_released_binding(binding);
  894. return;
  895. reset:
  896. binding->modifiers_match = modifiers_match_;
  897. if (!binding->pressed)
  898. return;
  899. release_pressed_binding(binding);
  900. }
  901. struct obs_hotkey_internal_inject {
  902. obs_key_combination_t hotkey;
  903. bool pressed;
  904. bool strict_modifiers;
  905. };
  906. static inline bool inject_hotkey(void *data, size_t idx,
  907. obs_hotkey_binding_t *binding)
  908. {
  909. UNUSED_PARAMETER(idx);
  910. struct obs_hotkey_internal_inject *event = data;
  911. if (modifiers_match(binding, event->hotkey.modifiers,
  912. event->strict_modifiers)) {
  913. bool pressed = binding->key.key == event->hotkey.key &&
  914. event->pressed;
  915. if (binding->key.key == OBS_KEY_NONE)
  916. pressed = true;
  917. if (pressed) {
  918. binding->modifiers_match = true;
  919. if (!binding->pressed)
  920. press_released_binding(binding);
  921. }
  922. }
  923. return true;
  924. }
  925. void obs_hotkey_inject_event(obs_key_combination_t hotkey, bool pressed)
  926. {
  927. if (!lock())
  928. return;
  929. struct obs_hotkey_internal_inject event = {
  930. {hotkey.modifiers, hotkey.key},
  931. pressed,
  932. obs->hotkeys.strict_modifiers,
  933. };
  934. enum_bindings(inject_hotkey, &event);
  935. unlock();
  936. }
  937. void obs_hotkey_enable_background_press(bool enable)
  938. {
  939. if (!lock())
  940. return;
  941. obs->hotkeys.thread_disable_press = !enable;
  942. unlock();
  943. }
  944. void obs_hotkey_enable_strict_modifiers(bool enable)
  945. {
  946. if (!lock())
  947. return;
  948. obs->hotkeys.strict_modifiers = enable;
  949. unlock();
  950. }
  951. struct obs_query_hotkeys_helper {
  952. uint32_t modifiers;
  953. bool no_press;
  954. bool strict_modifiers;
  955. };
  956. static inline bool query_hotkey(void *data, size_t idx,
  957. obs_hotkey_binding_t *binding)
  958. {
  959. UNUSED_PARAMETER(idx);
  960. struct obs_query_hotkeys_helper *param =
  961. (struct obs_query_hotkeys_helper *)data;
  962. handle_binding(binding, param->modifiers, param->no_press,
  963. param->strict_modifiers, NULL);
  964. return true;
  965. }
  966. static inline void query_hotkeys()
  967. {
  968. uint32_t modifiers = 0;
  969. if (is_pressed(OBS_KEY_SHIFT))
  970. modifiers |= INTERACT_SHIFT_KEY;
  971. if (is_pressed(OBS_KEY_CONTROL))
  972. modifiers |= INTERACT_CONTROL_KEY;
  973. if (is_pressed(OBS_KEY_ALT))
  974. modifiers |= INTERACT_ALT_KEY;
  975. if (is_pressed(OBS_KEY_META))
  976. modifiers |= INTERACT_COMMAND_KEY;
  977. struct obs_query_hotkeys_helper param = {
  978. modifiers,
  979. obs->hotkeys.thread_disable_press,
  980. obs->hotkeys.strict_modifiers,
  981. };
  982. enum_bindings(query_hotkey, &param);
  983. }
  984. #define NBSP "\xC2\xA0"
  985. void *obs_hotkey_thread(void *arg)
  986. {
  987. UNUSED_PARAMETER(arg);
  988. os_set_thread_name("libobs: hotkey thread");
  989. const char *hotkey_thread_name =
  990. profile_store_name(obs_get_profiler_name_store(),
  991. "obs_hotkey_thread(%g" NBSP "ms)", 25.);
  992. profile_register_root(hotkey_thread_name, (uint64_t)25000000);
  993. while (os_event_timedwait(obs->hotkeys.stop_event, 25) == ETIMEDOUT) {
  994. if (!lock())
  995. continue;
  996. profile_start(hotkey_thread_name);
  997. query_hotkeys();
  998. profile_end(hotkey_thread_name);
  999. unlock();
  1000. profile_reenable_thread();
  1001. }
  1002. return NULL;
  1003. }
  1004. void obs_hotkey_trigger_routed_callback(obs_hotkey_id id, bool pressed)
  1005. {
  1006. if (!lock())
  1007. return;
  1008. if (!obs->hotkeys.reroute_hotkeys)
  1009. goto unlock;
  1010. obs_hotkey_t *hotkey;
  1011. HASH_FIND_HKEY(obs->hotkeys.hotkeys, id, hotkey);
  1012. if (!hotkey)
  1013. goto unlock;
  1014. hotkey->func(hotkey->data, id, hotkey, pressed);
  1015. unlock:
  1016. unlock();
  1017. }
  1018. void obs_hotkey_set_callback_routing_func(obs_hotkey_callback_router_func func,
  1019. void *data)
  1020. {
  1021. if (!lock())
  1022. return;
  1023. obs->hotkeys.router_func = func;
  1024. obs->hotkeys.router_func_data = data;
  1025. unlock();
  1026. }
  1027. void obs_hotkey_enable_callback_rerouting(bool enable)
  1028. {
  1029. if (!lock())
  1030. return;
  1031. obs->hotkeys.reroute_hotkeys = enable;
  1032. unlock();
  1033. }
  1034. static void obs_set_key_translation(obs_key_t key, const char *translation)
  1035. {
  1036. bfree(obs->hotkeys.translations[key]);
  1037. obs->hotkeys.translations[key] = NULL;
  1038. if (translation)
  1039. obs->hotkeys.translations[key] = bstrdup(translation);
  1040. }
  1041. void obs_hotkeys_set_translations_s(
  1042. struct obs_hotkeys_translations *translations, size_t size)
  1043. {
  1044. #define ADD_TRANSLATION(key_name, var_name) \
  1045. if (t.var_name) \
  1046. obs_set_key_translation(key_name, t.var_name);
  1047. struct obs_hotkeys_translations t = {0};
  1048. struct dstr numpad = {0};
  1049. struct dstr mouse = {0};
  1050. struct dstr button = {0};
  1051. if (!translations) {
  1052. return;
  1053. }
  1054. memcpy(&t, translations, (size < sizeof(t)) ? size : sizeof(t));
  1055. ADD_TRANSLATION(OBS_KEY_INSERT, insert);
  1056. ADD_TRANSLATION(OBS_KEY_DELETE, del);
  1057. ADD_TRANSLATION(OBS_KEY_HOME, home);
  1058. ADD_TRANSLATION(OBS_KEY_END, end);
  1059. ADD_TRANSLATION(OBS_KEY_PAGEUP, page_up);
  1060. ADD_TRANSLATION(OBS_KEY_PAGEDOWN, page_down);
  1061. ADD_TRANSLATION(OBS_KEY_NUMLOCK, num_lock);
  1062. ADD_TRANSLATION(OBS_KEY_SCROLLLOCK, scroll_lock);
  1063. ADD_TRANSLATION(OBS_KEY_CAPSLOCK, caps_lock);
  1064. ADD_TRANSLATION(OBS_KEY_BACKSPACE, backspace);
  1065. ADD_TRANSLATION(OBS_KEY_TAB, tab);
  1066. ADD_TRANSLATION(OBS_KEY_PRINT, print);
  1067. ADD_TRANSLATION(OBS_KEY_PAUSE, pause);
  1068. ADD_TRANSLATION(OBS_KEY_SHIFT, shift);
  1069. ADD_TRANSLATION(OBS_KEY_ALT, alt);
  1070. ADD_TRANSLATION(OBS_KEY_CONTROL, control);
  1071. ADD_TRANSLATION(OBS_KEY_META, meta);
  1072. ADD_TRANSLATION(OBS_KEY_MENU, menu);
  1073. ADD_TRANSLATION(OBS_KEY_SPACE, space);
  1074. ADD_TRANSLATION(OBS_KEY_ESCAPE, escape);
  1075. #ifdef __APPLE__
  1076. const char *numpad_str = t.apple_keypad_num;
  1077. ADD_TRANSLATION(OBS_KEY_NUMSLASH, apple_keypad_divide);
  1078. ADD_TRANSLATION(OBS_KEY_NUMASTERISK, apple_keypad_multiply);
  1079. ADD_TRANSLATION(OBS_KEY_NUMMINUS, apple_keypad_minus);
  1080. ADD_TRANSLATION(OBS_KEY_NUMPLUS, apple_keypad_plus);
  1081. ADD_TRANSLATION(OBS_KEY_NUMPERIOD, apple_keypad_decimal);
  1082. ADD_TRANSLATION(OBS_KEY_NUMEQUAL, apple_keypad_equal);
  1083. #else
  1084. const char *numpad_str = t.numpad_num;
  1085. ADD_TRANSLATION(OBS_KEY_NUMSLASH, numpad_divide);
  1086. ADD_TRANSLATION(OBS_KEY_NUMASTERISK, numpad_multiply);
  1087. ADD_TRANSLATION(OBS_KEY_NUMMINUS, numpad_minus);
  1088. ADD_TRANSLATION(OBS_KEY_NUMPLUS, numpad_plus);
  1089. ADD_TRANSLATION(OBS_KEY_NUMPERIOD, numpad_decimal);
  1090. #endif
  1091. if (numpad_str) {
  1092. dstr_copy(&numpad, numpad_str);
  1093. dstr_depad(&numpad);
  1094. if (dstr_find(&numpad, "%1") == NULL) {
  1095. dstr_cat(&numpad, " %1");
  1096. }
  1097. #define ADD_NUMPAD_NUM(idx) \
  1098. dstr_copy_dstr(&button, &numpad); \
  1099. dstr_replace(&button, "%1", #idx); \
  1100. obs_set_key_translation(OBS_KEY_NUM##idx, button.array)
  1101. ADD_NUMPAD_NUM(0);
  1102. ADD_NUMPAD_NUM(1);
  1103. ADD_NUMPAD_NUM(2);
  1104. ADD_NUMPAD_NUM(3);
  1105. ADD_NUMPAD_NUM(4);
  1106. ADD_NUMPAD_NUM(5);
  1107. ADD_NUMPAD_NUM(6);
  1108. ADD_NUMPAD_NUM(7);
  1109. ADD_NUMPAD_NUM(8);
  1110. ADD_NUMPAD_NUM(9);
  1111. }
  1112. if (t.mouse_num) {
  1113. dstr_copy(&mouse, t.mouse_num);
  1114. dstr_depad(&mouse);
  1115. if (dstr_find(&mouse, "%1") == NULL) {
  1116. dstr_cat(&mouse, " %1");
  1117. }
  1118. #define ADD_MOUSE_NUM(idx) \
  1119. dstr_copy_dstr(&button, &mouse); \
  1120. dstr_replace(&button, "%1", #idx); \
  1121. obs_set_key_translation(OBS_KEY_MOUSE##idx, button.array)
  1122. ADD_MOUSE_NUM(1);
  1123. ADD_MOUSE_NUM(2);
  1124. ADD_MOUSE_NUM(3);
  1125. ADD_MOUSE_NUM(4);
  1126. ADD_MOUSE_NUM(5);
  1127. ADD_MOUSE_NUM(6);
  1128. ADD_MOUSE_NUM(7);
  1129. ADD_MOUSE_NUM(8);
  1130. ADD_MOUSE_NUM(9);
  1131. ADD_MOUSE_NUM(10);
  1132. ADD_MOUSE_NUM(11);
  1133. ADD_MOUSE_NUM(12);
  1134. ADD_MOUSE_NUM(13);
  1135. ADD_MOUSE_NUM(14);
  1136. ADD_MOUSE_NUM(15);
  1137. ADD_MOUSE_NUM(16);
  1138. ADD_MOUSE_NUM(17);
  1139. ADD_MOUSE_NUM(18);
  1140. ADD_MOUSE_NUM(19);
  1141. ADD_MOUSE_NUM(20);
  1142. ADD_MOUSE_NUM(21);
  1143. ADD_MOUSE_NUM(22);
  1144. ADD_MOUSE_NUM(23);
  1145. ADD_MOUSE_NUM(24);
  1146. ADD_MOUSE_NUM(25);
  1147. ADD_MOUSE_NUM(26);
  1148. ADD_MOUSE_NUM(27);
  1149. ADD_MOUSE_NUM(28);
  1150. ADD_MOUSE_NUM(29);
  1151. }
  1152. dstr_free(&numpad);
  1153. dstr_free(&mouse);
  1154. dstr_free(&button);
  1155. }
  1156. const char *obs_get_hotkey_translation(obs_key_t key, const char *def)
  1157. {
  1158. if (key == OBS_KEY_NONE) {
  1159. return NULL;
  1160. }
  1161. return obs->hotkeys.translations[key] ? obs->hotkeys.translations[key]
  1162. : def;
  1163. }
  1164. void obs_hotkey_update_atomic(obs_hotkey_atomic_update_func func, void *data)
  1165. {
  1166. if (!lock())
  1167. return;
  1168. func(data);
  1169. unlock();
  1170. }
  1171. void obs_hotkeys_set_audio_hotkeys_translations(const char *mute,
  1172. const char *unmute,
  1173. const char *push_to_mute,
  1174. const char *push_to_talk)
  1175. {
  1176. #define SET_T(n) \
  1177. bfree(obs->hotkeys.n); \
  1178. obs->hotkeys.n = bstrdup(n)
  1179. SET_T(mute);
  1180. SET_T(unmute);
  1181. SET_T(push_to_mute);
  1182. SET_T(push_to_talk);
  1183. #undef SET_T
  1184. }
  1185. void obs_hotkeys_set_sceneitem_hotkeys_translations(const char *show,
  1186. const char *hide)
  1187. {
  1188. #define SET_T(n) \
  1189. bfree(obs->hotkeys.sceneitem_##n); \
  1190. obs->hotkeys.sceneitem_##n = bstrdup(n)
  1191. SET_T(show);
  1192. SET_T(hide);
  1193. #undef SET_T
  1194. }