1
0

obs-hotkey.c 38 KB

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