1
0

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(data);
  486. if (find_id(pair->id[0], &idx))
  487. obs->hotkeys.hotkeys.array[idx].data = pair;
  488. if (find_id(pair->id[1], &idx))
  489. obs->hotkeys.hotkeys.array[idx].data = pair;
  490. return true;
  491. }
  492. static inline void fixup_pair_pointers(void)
  493. {
  494. enum_hotkey_pairs(pair_pointer_fixup_func, NULL);
  495. }
  496. static inline void enum_context_hotkeys(struct obs_context_data *context,
  497. obs_hotkey_internal_enum_func func,
  498. void *data)
  499. {
  500. const size_t num = context->hotkeys.num;
  501. const obs_hotkey_id *array = context->hotkeys.array;
  502. obs_hotkey_t *hotkey_array = obs->hotkeys.hotkeys.array;
  503. for (size_t i = 0; i < num; i++) {
  504. size_t idx;
  505. if (!find_id(array[i], &idx))
  506. continue;
  507. if (!func(data, idx, &hotkey_array[idx]))
  508. break;
  509. }
  510. }
  511. static inline void load_modifier(uint32_t *modifiers, obs_data_t *data,
  512. const char *name, uint32_t flag)
  513. {
  514. if (obs_data_get_bool(data, name))
  515. *modifiers |= flag;
  516. }
  517. static inline void create_binding(obs_hotkey_t *hotkey,
  518. obs_key_combination_t combo)
  519. {
  520. obs_hotkey_binding_t *binding = da_push_back_new(obs->hotkeys.bindings);
  521. if (!binding)
  522. return;
  523. binding->key = combo;
  524. binding->hotkey_id = hotkey->id;
  525. binding->hotkey = hotkey;
  526. }
  527. static inline void load_binding(obs_hotkey_t *hotkey, obs_data_t *data)
  528. {
  529. if (!hotkey || !data)
  530. return;
  531. obs_key_combination_t combo = {0};
  532. uint32_t *modifiers = &combo.modifiers;
  533. load_modifier(modifiers, data, "shift", INTERACT_SHIFT_KEY);
  534. load_modifier(modifiers, data, "control", INTERACT_CONTROL_KEY);
  535. load_modifier(modifiers, data, "alt", INTERACT_ALT_KEY);
  536. load_modifier(modifiers, data, "command", INTERACT_COMMAND_KEY);
  537. combo.key = obs_key_from_name(obs_data_get_string(data, "key"));
  538. if (!modifiers &&
  539. (combo.key == OBS_KEY_NONE || combo.key >= OBS_KEY_LAST_VALUE))
  540. return;
  541. create_binding(hotkey, combo);
  542. }
  543. static inline void load_bindings(obs_hotkey_t *hotkey, obs_data_array_t *data)
  544. {
  545. const size_t count = obs_data_array_count(data);
  546. for (size_t i = 0; i < count; i++) {
  547. obs_data_t *item = obs_data_array_item(data, i);
  548. load_binding(hotkey, item);
  549. obs_data_release(item);
  550. }
  551. hotkey_signal("hotkey_bindings_changed", hotkey);
  552. }
  553. static inline void remove_bindings(obs_hotkey_id id);
  554. void obs_hotkey_load_bindings(obs_hotkey_id id,
  555. obs_key_combination_t *combinations, size_t num)
  556. {
  557. size_t idx;
  558. if (!lock())
  559. return;
  560. if (find_id(id, &idx)) {
  561. obs_hotkey_t *hotkey = &obs->hotkeys.hotkeys.array[idx];
  562. remove_bindings(id);
  563. for (size_t i = 0; i < num; i++)
  564. create_binding(hotkey, combinations[i]);
  565. hotkey_signal("hotkey_bindings_changed", hotkey);
  566. }
  567. unlock();
  568. }
  569. void obs_hotkey_load(obs_hotkey_id id, obs_data_array_t *data)
  570. {
  571. size_t idx;
  572. if (!lock())
  573. return;
  574. if (find_id(id, &idx)) {
  575. remove_bindings(id);
  576. load_bindings(&obs->hotkeys.hotkeys.array[idx], data);
  577. }
  578. unlock();
  579. }
  580. static inline bool enum_load_bindings(void *data, size_t idx,
  581. obs_hotkey_t *hotkey)
  582. {
  583. UNUSED_PARAMETER(idx);
  584. obs_data_array_t *hotkey_data = obs_data_get_array(data, hotkey->name);
  585. if (!hotkey_data)
  586. return true;
  587. load_bindings(hotkey, hotkey_data);
  588. obs_data_array_release(hotkey_data);
  589. return true;
  590. }
  591. void obs_hotkeys_load_encoder(obs_encoder_t *encoder, obs_data_t *hotkeys)
  592. {
  593. if (!encoder || !hotkeys)
  594. return;
  595. if (!lock())
  596. return;
  597. enum_context_hotkeys(&encoder->context, enum_load_bindings, hotkeys);
  598. unlock();
  599. }
  600. void obs_hotkeys_load_output(obs_output_t *output, obs_data_t *hotkeys)
  601. {
  602. if (!output || !hotkeys)
  603. return;
  604. if (!lock())
  605. return;
  606. enum_context_hotkeys(&output->context, enum_load_bindings, hotkeys);
  607. unlock();
  608. }
  609. void obs_hotkeys_load_service(obs_service_t *service, obs_data_t *hotkeys)
  610. {
  611. if (!service || !hotkeys)
  612. return;
  613. if (!lock())
  614. return;
  615. enum_context_hotkeys(&service->context, enum_load_bindings, hotkeys);
  616. unlock();
  617. }
  618. void obs_hotkeys_load_source(obs_source_t *source, obs_data_t *hotkeys)
  619. {
  620. if (!source || !hotkeys)
  621. return;
  622. if (!lock())
  623. return;
  624. enum_context_hotkeys(&source->context, enum_load_bindings, hotkeys);
  625. unlock();
  626. }
  627. void obs_hotkey_pair_load(obs_hotkey_pair_id id, obs_data_array_t *data0,
  628. obs_data_array_t *data1)
  629. {
  630. if ((!data0 && !data1) || !lock())
  631. return;
  632. size_t idx;
  633. if (!find_pair_id(id, &idx))
  634. goto unlock;
  635. obs_hotkey_pair_t *pair = &obs->hotkeys.hotkey_pairs.array[idx];
  636. if (find_id(pair->id[0], &idx)) {
  637. remove_bindings(pair->id[0]);
  638. load_bindings(&obs->hotkeys.hotkeys.array[idx], data0);
  639. }
  640. if (find_id(pair->id[1], &idx)) {
  641. remove_bindings(pair->id[1]);
  642. load_bindings(&obs->hotkeys.hotkeys.array[idx], data1);
  643. }
  644. unlock:
  645. unlock();
  646. }
  647. static inline void save_modifier(uint32_t modifiers, obs_data_t *data,
  648. const char *name, uint32_t flag)
  649. {
  650. if ((modifiers & flag) == flag)
  651. obs_data_set_bool(data, name, true);
  652. }
  653. struct save_bindings_helper_t {
  654. obs_data_array_t *array;
  655. obs_hotkey_t *hotkey;
  656. };
  657. static inline bool save_bindings_helper(void *data, size_t idx,
  658. obs_hotkey_binding_t *binding)
  659. {
  660. UNUSED_PARAMETER(idx);
  661. struct save_bindings_helper_t *h = data;
  662. if (h->hotkey->id != binding->hotkey_id)
  663. return true;
  664. obs_data_t *hotkey = obs_data_create();
  665. uint32_t modifiers = binding->key.modifiers;
  666. save_modifier(modifiers, hotkey, "shift", INTERACT_SHIFT_KEY);
  667. save_modifier(modifiers, hotkey, "control", INTERACT_CONTROL_KEY);
  668. save_modifier(modifiers, hotkey, "alt", INTERACT_ALT_KEY);
  669. save_modifier(modifiers, hotkey, "command", INTERACT_COMMAND_KEY);
  670. obs_data_set_string(hotkey, "key", obs_key_to_name(binding->key.key));
  671. obs_data_array_push_back(h->array, hotkey);
  672. obs_data_release(hotkey);
  673. return true;
  674. }
  675. static inline obs_data_array_t *save_hotkey(obs_hotkey_t *hotkey)
  676. {
  677. obs_data_array_t *data = obs_data_array_create();
  678. struct save_bindings_helper_t arg = {data, hotkey};
  679. enum_bindings(save_bindings_helper, &arg);
  680. return data;
  681. }
  682. obs_data_array_t *obs_hotkey_save(obs_hotkey_id id)
  683. {
  684. size_t idx;
  685. obs_data_array_t *result = NULL;
  686. if (!lock())
  687. return result;
  688. if (find_id(id, &idx))
  689. result = save_hotkey(&obs->hotkeys.hotkeys.array[idx]);
  690. unlock();
  691. return result;
  692. }
  693. void obs_hotkey_pair_save(obs_hotkey_pair_id id, obs_data_array_t **p_data0,
  694. obs_data_array_t **p_data1)
  695. {
  696. if ((!p_data0 && !p_data1) || !lock())
  697. return;
  698. size_t idx;
  699. if (!find_pair_id(id, &idx))
  700. goto unlock;
  701. obs_hotkey_pair_t *pair = &obs->hotkeys.hotkey_pairs.array[idx];
  702. if (p_data0 && find_id(pair->id[0], &idx)) {
  703. *p_data0 = save_hotkey(&obs->hotkeys.hotkeys.array[idx]);
  704. }
  705. if (p_data1 && find_id(pair->id[1], &idx)) {
  706. *p_data1 = save_hotkey(&obs->hotkeys.hotkeys.array[idx]);
  707. }
  708. unlock:
  709. unlock();
  710. }
  711. static inline bool enum_save_hotkey(void *data, size_t idx,
  712. obs_hotkey_t *hotkey)
  713. {
  714. UNUSED_PARAMETER(idx);
  715. obs_data_array_t *hotkey_data = save_hotkey(hotkey);
  716. obs_data_set_array(data, hotkey->name, hotkey_data);
  717. obs_data_array_release(hotkey_data);
  718. return true;
  719. }
  720. static inline obs_data_t *save_context_hotkeys(struct obs_context_data *context)
  721. {
  722. if (!context->hotkeys.num)
  723. return NULL;
  724. obs_data_t *result = obs_data_create();
  725. enum_context_hotkeys(context, enum_save_hotkey, result);
  726. return result;
  727. }
  728. obs_data_t *obs_hotkeys_save_encoder(obs_encoder_t *encoder)
  729. {
  730. obs_data_t *result = NULL;
  731. if (!lock())
  732. return result;
  733. result = save_context_hotkeys(&encoder->context);
  734. unlock();
  735. return result;
  736. }
  737. obs_data_t *obs_hotkeys_save_output(obs_output_t *output)
  738. {
  739. obs_data_t *result = NULL;
  740. if (!lock())
  741. return result;
  742. result = save_context_hotkeys(&output->context);
  743. unlock();
  744. return result;
  745. }
  746. obs_data_t *obs_hotkeys_save_service(obs_service_t *service)
  747. {
  748. obs_data_t *result = NULL;
  749. if (!lock())
  750. return result;
  751. result = save_context_hotkeys(&service->context);
  752. unlock();
  753. return result;
  754. }
  755. obs_data_t *obs_hotkeys_save_source(obs_source_t *source)
  756. {
  757. obs_data_t *result = NULL;
  758. if (!lock())
  759. return result;
  760. result = save_context_hotkeys(&source->context);
  761. unlock();
  762. return result;
  763. }
  764. struct binding_find_data {
  765. obs_hotkey_id id;
  766. size_t *idx;
  767. bool found;
  768. };
  769. static inline bool binding_finder(void *data, size_t idx,
  770. obs_hotkey_binding_t *binding)
  771. {
  772. struct binding_find_data *find = data;
  773. if (binding->hotkey_id != find->id)
  774. return true;
  775. *find->idx = idx;
  776. find->found = true;
  777. return false;
  778. }
  779. static inline bool find_binding(obs_hotkey_id id, size_t *idx)
  780. {
  781. struct binding_find_data data = {id, idx, false};
  782. enum_bindings(binding_finder, &data);
  783. return data.found;
  784. }
  785. static inline void release_pressed_binding(obs_hotkey_binding_t *binding);
  786. static inline void remove_bindings(obs_hotkey_id id)
  787. {
  788. size_t idx;
  789. while (find_binding(id, &idx)) {
  790. obs_hotkey_binding_t *binding =
  791. &obs->hotkeys.bindings.array[idx];
  792. if (binding->pressed)
  793. release_pressed_binding(binding);
  794. da_erase(obs->hotkeys.bindings, idx);
  795. }
  796. }
  797. static void release_registerer(obs_hotkey_t *hotkey)
  798. {
  799. switch (hotkey->registerer_type) {
  800. case OBS_HOTKEY_REGISTERER_FRONTEND:
  801. break;
  802. case OBS_HOTKEY_REGISTERER_ENCODER:
  803. obs_weak_encoder_release(hotkey->registerer);
  804. break;
  805. case OBS_HOTKEY_REGISTERER_OUTPUT:
  806. obs_weak_output_release(hotkey->registerer);
  807. break;
  808. case OBS_HOTKEY_REGISTERER_SERVICE:
  809. obs_weak_service_release(hotkey->registerer);
  810. break;
  811. case OBS_HOTKEY_REGISTERER_SOURCE:
  812. obs_weak_source_release(hotkey->registerer);
  813. break;
  814. }
  815. hotkey->registerer = NULL;
  816. }
  817. static inline bool unregister_hotkey(obs_hotkey_id id)
  818. {
  819. if (id >= obs->hotkeys.next_id)
  820. return false;
  821. size_t idx;
  822. if (!find_id(id, &idx))
  823. return false;
  824. obs_hotkey_t *hotkey = &obs->hotkeys.hotkeys.array[idx];
  825. hotkey_signal("hotkey_unregister", hotkey);
  826. release_registerer(hotkey);
  827. bfree(hotkey->name);
  828. bfree(hotkey->description);
  829. if (hotkey->registerer_type == OBS_HOTKEY_REGISTERER_SOURCE)
  830. obs_weak_source_release(hotkey->registerer);
  831. da_erase(obs->hotkeys.hotkeys, idx);
  832. remove_bindings(id);
  833. return obs->hotkeys.hotkeys.num >= idx;
  834. }
  835. static inline bool unregister_hotkey_pair(obs_hotkey_pair_id id)
  836. {
  837. if (id >= obs->hotkeys.next_pair_id)
  838. return false;
  839. size_t idx;
  840. if (!find_pair_id(id, &idx))
  841. return false;
  842. obs_hotkey_pair_t *pair = &obs->hotkeys.hotkey_pairs.array[idx];
  843. bool need_fixup = unregister_hotkey(pair->id[0]);
  844. need_fixup = unregister_hotkey(pair->id[1]) || need_fixup;
  845. if (need_fixup)
  846. fixup_pointers();
  847. da_erase(obs->hotkeys.hotkey_pairs, idx);
  848. return obs->hotkeys.hotkey_pairs.num >= idx;
  849. }
  850. void obs_hotkey_unregister(obs_hotkey_id id)
  851. {
  852. if (!lock())
  853. return;
  854. if (unregister_hotkey(id))
  855. fixup_pointers();
  856. unlock();
  857. }
  858. void obs_hotkey_pair_unregister(obs_hotkey_pair_id id)
  859. {
  860. if (!lock())
  861. return;
  862. if (unregister_hotkey_pair(id))
  863. fixup_pair_pointers();
  864. unlock();
  865. }
  866. static void context_release_hotkeys(struct obs_context_data *context)
  867. {
  868. if (!context->hotkeys.num)
  869. goto cleanup;
  870. bool need_fixup = false;
  871. for (size_t i = 0; i < context->hotkeys.num; i++)
  872. need_fixup = unregister_hotkey(context->hotkeys.array[i]) ||
  873. need_fixup;
  874. if (need_fixup)
  875. fixup_pointers();
  876. cleanup:
  877. da_free(context->hotkeys);
  878. }
  879. static void context_release_hotkey_pairs(struct obs_context_data *context)
  880. {
  881. if (!context->hotkey_pairs.num)
  882. goto cleanup;
  883. bool need_fixup = false;
  884. for (size_t i = 0; i < context->hotkey_pairs.num; i++)
  885. need_fixup = unregister_hotkey_pair(
  886. context->hotkey_pairs.array[i]) ||
  887. need_fixup;
  888. if (need_fixup)
  889. fixup_pair_pointers();
  890. cleanup:
  891. da_free(context->hotkey_pairs);
  892. }
  893. void obs_hotkeys_context_release(struct obs_context_data *context)
  894. {
  895. if (!lock())
  896. return;
  897. context_release_hotkeys(context);
  898. context_release_hotkey_pairs(context);
  899. obs_data_release(context->hotkey_data);
  900. unlock();
  901. }
  902. void obs_hotkeys_free(void)
  903. {
  904. const size_t num = obs->hotkeys.hotkeys.num;
  905. obs_hotkey_t *hotkeys = obs->hotkeys.hotkeys.array;
  906. for (size_t i = 0; i < num; i++) {
  907. bfree(hotkeys[i].name);
  908. bfree(hotkeys[i].description);
  909. release_registerer(&hotkeys[i]);
  910. }
  911. da_free(obs->hotkeys.bindings);
  912. da_free(obs->hotkeys.hotkeys);
  913. da_free(obs->hotkeys.hotkey_pairs);
  914. for (size_t i = 0; i < OBS_KEY_LAST_VALUE; i++) {
  915. if (obs->hotkeys.translations[i]) {
  916. bfree(obs->hotkeys.translations[i]);
  917. obs->hotkeys.translations[i] = NULL;
  918. }
  919. }
  920. }
  921. struct obs_hotkey_internal_enum_forward {
  922. obs_hotkey_enum_func func;
  923. void *data;
  924. };
  925. static inline bool enum_hotkey(void *data, size_t idx, obs_hotkey_t *hotkey)
  926. {
  927. UNUSED_PARAMETER(idx);
  928. struct obs_hotkey_internal_enum_forward *forward = data;
  929. return forward->func(forward->data, hotkey->id, hotkey);
  930. }
  931. void obs_enum_hotkeys(obs_hotkey_enum_func func, void *data)
  932. {
  933. struct obs_hotkey_internal_enum_forward forwarder = {func, data};
  934. if (!lock())
  935. return;
  936. enum_hotkeys(enum_hotkey, &forwarder);
  937. unlock();
  938. }
  939. void obs_enum_hotkey_bindings(obs_hotkey_binding_enum_func func, void *data)
  940. {
  941. if (!lock())
  942. return;
  943. enum_bindings(func, data);
  944. unlock();
  945. }
  946. static inline bool modifiers_match(obs_hotkey_binding_t *binding,
  947. uint32_t modifiers_, bool strict_modifiers)
  948. {
  949. uint32_t modifiers = binding->key.modifiers;
  950. if (!strict_modifiers)
  951. return (modifiers & modifiers_) == modifiers;
  952. else
  953. return 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 (!strict_modifiers && !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. }