obs-hotkey.c 36 KB

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