obs-hotkey.c 38 KB

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