obs-hotkey.c 34 KB

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