obs-windows.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. /******************************************************************************
  2. Copyright (C) 2013 by Hugh Bailey <[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 "util/windows/win-registry.h"
  15. #include "util/windows/win-version.h"
  16. #include "util/platform.h"
  17. #include "util/dstr.h"
  18. #include "obs.h"
  19. #include "obs-internal.h"
  20. #include <windows.h>
  21. #include <wscapi.h>
  22. #include <iwscapi.h>
  23. static uint32_t win_ver = 0;
  24. const char *get_module_extension(void)
  25. {
  26. return ".dll";
  27. }
  28. #ifdef _WIN64
  29. #define BIT_STRING "64bit"
  30. #else
  31. #define BIT_STRING "32bit"
  32. #endif
  33. static const char *module_bin[] = {
  34. "obs-plugins/" BIT_STRING,
  35. "../../obs-plugins/" BIT_STRING,
  36. };
  37. static const char *module_data[] = {
  38. "data/%module%",
  39. "../../data/obs-plugins/%module%"
  40. };
  41. static const int module_patterns_size =
  42. sizeof(module_bin)/sizeof(module_bin[0]);
  43. void add_default_module_paths(void)
  44. {
  45. for (int i = 0; i < module_patterns_size; i++)
  46. obs_add_module_path(module_bin[i], module_data[i]);
  47. }
  48. /* on windows, points to [base directory]/data/libobs */
  49. char *find_libobs_data_file(const char *file)
  50. {
  51. struct dstr path;
  52. dstr_init(&path);
  53. if (check_path(file, "data/libobs/", &path))
  54. return path.array;
  55. if (check_path(file, "../../data/libobs/", &path))
  56. return path.array;
  57. dstr_free(&path);
  58. return NULL;
  59. }
  60. static void log_processor_info(void)
  61. {
  62. HKEY key;
  63. wchar_t data[1024];
  64. char *str = NULL;
  65. DWORD size, speed;
  66. LSTATUS status;
  67. memset(data, 0, 1024);
  68. status = RegOpenKeyW(HKEY_LOCAL_MACHINE,
  69. L"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0",
  70. &key);
  71. if (status != ERROR_SUCCESS)
  72. return;
  73. size = 1024;
  74. status = RegQueryValueExW(key, L"ProcessorNameString", NULL, NULL,
  75. (LPBYTE)data, &size);
  76. if (status == ERROR_SUCCESS) {
  77. os_wcs_to_utf8_ptr(data, 0, &str);
  78. blog(LOG_INFO, "CPU Name: %s", str);
  79. bfree(str);
  80. }
  81. size = sizeof(speed);
  82. status = RegQueryValueExW(key, L"~MHz", NULL, NULL, (LPBYTE)&speed,
  83. &size);
  84. if (status == ERROR_SUCCESS)
  85. blog(LOG_INFO, "CPU Speed: %ldMHz", speed);
  86. RegCloseKey(key);
  87. }
  88. static void log_processor_cores(void)
  89. {
  90. blog(LOG_INFO, "Physical Cores: %d, Logical Cores: %d",
  91. os_get_physical_cores(), os_get_logical_cores());
  92. }
  93. static void log_available_memory(void)
  94. {
  95. MEMORYSTATUSEX ms;
  96. ms.dwLength = sizeof(ms);
  97. GlobalMemoryStatusEx(&ms);
  98. #ifdef _WIN64
  99. const char *note = "";
  100. #else
  101. const char *note = " (NOTE: 32bit programs cannot use more than 3gb)";
  102. #endif
  103. blog(LOG_INFO, "Physical Memory: %luMB Total, %luMB Free%s",
  104. (DWORD)(ms.ullTotalPhys / 1048576),
  105. (DWORD)(ms.ullAvailPhys / 1048576),
  106. note);
  107. }
  108. static void log_windows_version(void)
  109. {
  110. struct win_version_info ver;
  111. get_win_ver(&ver);
  112. bool b64 = is_64_bit_windows();
  113. const char *windows_bitness = b64 ? "64" : "32";
  114. blog(LOG_INFO, "Windows Version: %d.%d Build %d (revision: %d; %s-bit)",
  115. ver.major, ver.minor, ver.build, ver.revis,
  116. windows_bitness);
  117. }
  118. static void log_admin_status(void)
  119. {
  120. SID_IDENTIFIER_AUTHORITY auth = SECURITY_NT_AUTHORITY;
  121. PSID admin_group;
  122. BOOL success;
  123. success = AllocateAndInitializeSid(&auth, 2,
  124. SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS,
  125. 0, 0, 0, 0, 0, 0, &admin_group);
  126. if (success) {
  127. if (!CheckTokenMembership(NULL, admin_group, &success))
  128. success = false;
  129. FreeSid(admin_group);
  130. }
  131. blog(LOG_INFO, "Running as administrator: %s",
  132. success ? "true" : "false");
  133. }
  134. typedef HRESULT (WINAPI *dwm_is_composition_enabled_t)(BOOL*);
  135. static void log_aero(void)
  136. {
  137. dwm_is_composition_enabled_t composition_enabled = NULL;
  138. const char *aeroMessage = win_ver >= 0x602 ?
  139. " (Aero is always on for windows 8 and above)" : "";
  140. HMODULE dwm = LoadLibraryW(L"dwmapi");
  141. BOOL bComposition = true;
  142. if (!dwm) {
  143. return;
  144. }
  145. composition_enabled = (dwm_is_composition_enabled_t)GetProcAddress(dwm,
  146. "DwmIsCompositionEnabled");
  147. if (!composition_enabled) {
  148. return;
  149. }
  150. composition_enabled(&bComposition);
  151. blog(LOG_INFO, "Aero is %s%s", bComposition ? "Enabled" : "Disabled",
  152. aeroMessage);
  153. }
  154. #define WIN10_GAME_BAR_REG_KEY \
  155. L"Software\\Microsoft\\Windows\\CurrentVersion\\GameDVR"
  156. #define WIN10_GAME_DVR_POLICY_REG_KEY \
  157. L"SOFTWARE\\Policies\\Microsoft\\Windows\\GameDVR"
  158. #define WIN10_GAME_DVR_REG_KEY L"System\\GameConfigStore"
  159. #define WIN10_GAME_MODE_REG_KEY L"Software\\Microsoft\\GameBar"
  160. static void log_gaming_features(void)
  161. {
  162. if (win_ver < 0xA00)
  163. return;
  164. struct reg_dword game_bar_enabled;
  165. struct reg_dword game_dvr_allowed;
  166. struct reg_dword game_dvr_enabled;
  167. struct reg_dword game_dvr_bg_recording;
  168. struct reg_dword game_mode_enabled;
  169. get_reg_dword(HKEY_CURRENT_USER, WIN10_GAME_BAR_REG_KEY,
  170. L"AppCaptureEnabled", &game_bar_enabled);
  171. get_reg_dword(HKEY_CURRENT_USER, WIN10_GAME_DVR_POLICY_REG_KEY,
  172. L"AllowGameDVR", &game_dvr_allowed);
  173. get_reg_dword(HKEY_CURRENT_USER, WIN10_GAME_DVR_REG_KEY,
  174. L"GameDVR_Enabled", &game_dvr_enabled);
  175. get_reg_dword(HKEY_CURRENT_USER, WIN10_GAME_BAR_REG_KEY,
  176. L"HistoricalCaptureEnabled", &game_dvr_bg_recording);
  177. get_reg_dword(HKEY_CURRENT_USER, WIN10_GAME_MODE_REG_KEY,
  178. L"AllowAutoGameMode", &game_mode_enabled);
  179. blog(LOG_INFO, "Windows 10 Gaming Features:");
  180. if (game_bar_enabled.status == ERROR_SUCCESS) {
  181. blog(LOG_INFO, "\tGame Bar: %s",
  182. (bool)game_bar_enabled.return_value ? "On" : "Off");
  183. }
  184. if (game_dvr_allowed.status == ERROR_SUCCESS) {
  185. blog(LOG_INFO, "\tGame DVR Allowed: %s",
  186. (bool)game_dvr_allowed.return_value ? "Yes" : "No");
  187. }
  188. if (game_dvr_enabled.status == ERROR_SUCCESS) {
  189. blog(LOG_INFO, "\tGame DVR: %s",
  190. (bool)game_dvr_enabled.return_value ? "On" : "Off");
  191. }
  192. if (game_dvr_bg_recording.status == ERROR_SUCCESS) {
  193. blog(LOG_INFO, "\tGame DVR Background Recording: %s",
  194. (bool)game_dvr_bg_recording.return_value ? "On" :
  195. "Off");
  196. }
  197. if (game_mode_enabled.status == ERROR_SUCCESS) {
  198. blog(LOG_INFO, "\tGame Mode: %s",
  199. (bool)game_mode_enabled.return_value ? "On" : "Off");
  200. }
  201. }
  202. static const char *get_str_for_state(int state)
  203. {
  204. switch (state) {
  205. case WSC_SECURITY_PRODUCT_STATE_ON:
  206. return "enabled";
  207. case WSC_SECURITY_PRODUCT_STATE_OFF:
  208. return "disabled";
  209. case WSC_SECURITY_PRODUCT_STATE_SNOOZED:
  210. return "temporarily disabled";
  211. case WSC_SECURITY_PRODUCT_STATE_EXPIRED:
  212. return "expired";
  213. default:
  214. return "unknown";
  215. }
  216. }
  217. static const char *get_str_for_type(int type)
  218. {
  219. switch (type) {
  220. case WSC_SECURITY_PROVIDER_ANTIVIRUS:
  221. return "AV";
  222. case WSC_SECURITY_PROVIDER_FIREWALL:
  223. return "FW";
  224. case WSC_SECURITY_PROVIDER_ANTISPYWARE:
  225. return "ASW";
  226. default:
  227. return "unknown";
  228. }
  229. }
  230. static void log_security_products_by_type(IWSCProductList *prod_list, int type)
  231. {
  232. HRESULT hr;
  233. LONG count = 0;
  234. IWscProduct *prod;
  235. BSTR name;
  236. WSC_SECURITY_PRODUCT_STATE prod_state;
  237. hr = prod_list->lpVtbl->Initialize(prod_list, type);
  238. if (FAILED(hr))
  239. return;
  240. hr = prod_list->lpVtbl->get_Count(prod_list, &count);
  241. if (FAILED(hr)) {
  242. prod_list->lpVtbl->Release(prod_list);
  243. return;
  244. }
  245. for (int i = 0; i < count; i++) {
  246. hr = prod_list->lpVtbl->get_Item(prod_list, i, &prod);
  247. if (FAILED(hr))
  248. continue;
  249. hr = prod->lpVtbl->get_ProductName(prod, &name);
  250. if (FAILED(hr))
  251. continue;
  252. hr = prod->lpVtbl->get_ProductState(prod, &prod_state);
  253. if (FAILED(hr)) {
  254. SysFreeString(name);
  255. continue;
  256. }
  257. blog(LOG_INFO, "\t%S: %s (%s)", name,
  258. get_str_for_state(prod_state),
  259. get_str_for_type(type));
  260. SysFreeString(name);
  261. prod->lpVtbl->Release(prod);
  262. }
  263. prod_list->lpVtbl->Release(prod_list);
  264. }
  265. static void log_security_products(void)
  266. {
  267. IWSCProductList *prod_list = NULL;
  268. HMODULE h_wsc;
  269. HRESULT hr;
  270. /* We load the DLL rather than import wcsapi.lib because the clsid /
  271. * iid only exists on Windows 8 or higher. */
  272. h_wsc = LoadLibraryW(L"wscapi.dll");
  273. if (!h_wsc)
  274. return;
  275. const CLSID *prod_list_clsid =
  276. (const CLSID *)GetProcAddress(h_wsc, "CLSID_WSCProductList");
  277. const IID *prod_list_iid =
  278. (const IID *)GetProcAddress(h_wsc, "IID_IWSCProductList");
  279. if (prod_list_clsid && prod_list_iid) {
  280. blog(LOG_INFO, "Sec. Software Status:");
  281. hr = CoCreateInstance(prod_list_clsid, NULL,
  282. CLSCTX_INPROC_SERVER, prod_list_iid,
  283. &prod_list);
  284. if (!FAILED(hr)) {
  285. log_security_products_by_type(prod_list,
  286. WSC_SECURITY_PROVIDER_ANTIVIRUS);
  287. }
  288. hr = CoCreateInstance(prod_list_clsid, NULL,
  289. CLSCTX_INPROC_SERVER, prod_list_iid,
  290. &prod_list);
  291. if (!FAILED(hr)) {
  292. log_security_products_by_type(prod_list,
  293. WSC_SECURITY_PROVIDER_FIREWALL);
  294. }
  295. hr = CoCreateInstance(prod_list_clsid, NULL,
  296. CLSCTX_INPROC_SERVER, prod_list_iid,
  297. &prod_list);
  298. if (!FAILED(hr)) {
  299. log_security_products_by_type(prod_list,
  300. WSC_SECURITY_PROVIDER_ANTISPYWARE);
  301. }
  302. }
  303. FreeLibrary(h_wsc);
  304. }
  305. void log_system_info(void)
  306. {
  307. struct win_version_info ver;
  308. get_win_ver(&ver);
  309. win_ver = (ver.major << 8) | ver.minor;
  310. log_processor_info();
  311. log_processor_cores();
  312. log_available_memory();
  313. log_windows_version();
  314. log_admin_status();
  315. log_aero();
  316. log_gaming_features();
  317. log_security_products();
  318. }
  319. struct obs_hotkeys_platform {
  320. int vk_codes[OBS_KEY_LAST_VALUE];
  321. };
  322. static int get_virtual_key(obs_key_t key)
  323. {
  324. switch (key) {
  325. case OBS_KEY_RETURN: return VK_RETURN;
  326. case OBS_KEY_ESCAPE: return VK_ESCAPE;
  327. case OBS_KEY_TAB: return VK_TAB;
  328. case OBS_KEY_BACKTAB: return VK_OEM_BACKTAB;
  329. case OBS_KEY_BACKSPACE: return VK_BACK;
  330. case OBS_KEY_INSERT: return VK_INSERT;
  331. case OBS_KEY_DELETE: return VK_DELETE;
  332. case OBS_KEY_PAUSE: return VK_PAUSE;
  333. case OBS_KEY_PRINT: return VK_SNAPSHOT;
  334. case OBS_KEY_CLEAR: return VK_CLEAR;
  335. case OBS_KEY_HOME: return VK_HOME;
  336. case OBS_KEY_END: return VK_END;
  337. case OBS_KEY_LEFT: return VK_LEFT;
  338. case OBS_KEY_UP: return VK_UP;
  339. case OBS_KEY_RIGHT: return VK_RIGHT;
  340. case OBS_KEY_DOWN: return VK_DOWN;
  341. case OBS_KEY_PAGEUP: return VK_PRIOR;
  342. case OBS_KEY_PAGEDOWN: return VK_NEXT;
  343. case OBS_KEY_SHIFT: return VK_SHIFT;
  344. case OBS_KEY_CONTROL: return VK_CONTROL;
  345. case OBS_KEY_ALT: return VK_MENU;
  346. case OBS_KEY_CAPSLOCK: return VK_CAPITAL;
  347. case OBS_KEY_NUMLOCK: return VK_NUMLOCK;
  348. case OBS_KEY_SCROLLLOCK: return VK_SCROLL;
  349. case OBS_KEY_F1: return VK_F1;
  350. case OBS_KEY_F2: return VK_F2;
  351. case OBS_KEY_F3: return VK_F3;
  352. case OBS_KEY_F4: return VK_F4;
  353. case OBS_KEY_F5: return VK_F5;
  354. case OBS_KEY_F6: return VK_F6;
  355. case OBS_KEY_F7: return VK_F7;
  356. case OBS_KEY_F8: return VK_F8;
  357. case OBS_KEY_F9: return VK_F9;
  358. case OBS_KEY_F10: return VK_F10;
  359. case OBS_KEY_F11: return VK_F11;
  360. case OBS_KEY_F12: return VK_F12;
  361. case OBS_KEY_F13: return VK_F13;
  362. case OBS_KEY_F14: return VK_F14;
  363. case OBS_KEY_F15: return VK_F15;
  364. case OBS_KEY_F16: return VK_F16;
  365. case OBS_KEY_F17: return VK_F17;
  366. case OBS_KEY_F18: return VK_F18;
  367. case OBS_KEY_F19: return VK_F19;
  368. case OBS_KEY_F20: return VK_F20;
  369. case OBS_KEY_F21: return VK_F21;
  370. case OBS_KEY_F22: return VK_F22;
  371. case OBS_KEY_F23: return VK_F23;
  372. case OBS_KEY_F24: return VK_F24;
  373. case OBS_KEY_SPACE: return VK_SPACE;
  374. case OBS_KEY_APOSTROPHE: return VK_OEM_7;
  375. case OBS_KEY_PLUS: return VK_OEM_PLUS;
  376. case OBS_KEY_COMMA: return VK_OEM_COMMA;
  377. case OBS_KEY_MINUS: return VK_OEM_MINUS;
  378. case OBS_KEY_PERIOD: return VK_OEM_PERIOD;
  379. case OBS_KEY_SLASH: return VK_OEM_2;
  380. case OBS_KEY_0: return '0';
  381. case OBS_KEY_1: return '1';
  382. case OBS_KEY_2: return '2';
  383. case OBS_KEY_3: return '3';
  384. case OBS_KEY_4: return '4';
  385. case OBS_KEY_5: return '5';
  386. case OBS_KEY_6: return '6';
  387. case OBS_KEY_7: return '7';
  388. case OBS_KEY_8: return '8';
  389. case OBS_KEY_9: return '9';
  390. case OBS_KEY_NUMASTERISK: return VK_MULTIPLY;
  391. case OBS_KEY_NUMPLUS: return VK_ADD;
  392. case OBS_KEY_NUMMINUS: return VK_SUBTRACT;
  393. case OBS_KEY_NUMPERIOD: return VK_DECIMAL;
  394. case OBS_KEY_NUMSLASH: return VK_DIVIDE;
  395. case OBS_KEY_NUM0: return VK_NUMPAD0;
  396. case OBS_KEY_NUM1: return VK_NUMPAD1;
  397. case OBS_KEY_NUM2: return VK_NUMPAD2;
  398. case OBS_KEY_NUM3: return VK_NUMPAD3;
  399. case OBS_KEY_NUM4: return VK_NUMPAD4;
  400. case OBS_KEY_NUM5: return VK_NUMPAD5;
  401. case OBS_KEY_NUM6: return VK_NUMPAD6;
  402. case OBS_KEY_NUM7: return VK_NUMPAD7;
  403. case OBS_KEY_NUM8: return VK_NUMPAD8;
  404. case OBS_KEY_NUM9: return VK_NUMPAD9;
  405. case OBS_KEY_SEMICOLON: return VK_OEM_1;
  406. case OBS_KEY_A: return 'A';
  407. case OBS_KEY_B: return 'B';
  408. case OBS_KEY_C: return 'C';
  409. case OBS_KEY_D: return 'D';
  410. case OBS_KEY_E: return 'E';
  411. case OBS_KEY_F: return 'F';
  412. case OBS_KEY_G: return 'G';
  413. case OBS_KEY_H: return 'H';
  414. case OBS_KEY_I: return 'I';
  415. case OBS_KEY_J: return 'J';
  416. case OBS_KEY_K: return 'K';
  417. case OBS_KEY_L: return 'L';
  418. case OBS_KEY_M: return 'M';
  419. case OBS_KEY_N: return 'N';
  420. case OBS_KEY_O: return 'O';
  421. case OBS_KEY_P: return 'P';
  422. case OBS_KEY_Q: return 'Q';
  423. case OBS_KEY_R: return 'R';
  424. case OBS_KEY_S: return 'S';
  425. case OBS_KEY_T: return 'T';
  426. case OBS_KEY_U: return 'U';
  427. case OBS_KEY_V: return 'V';
  428. case OBS_KEY_W: return 'W';
  429. case OBS_KEY_X: return 'X';
  430. case OBS_KEY_Y: return 'Y';
  431. case OBS_KEY_Z: return 'Z';
  432. case OBS_KEY_BRACKETLEFT: return VK_OEM_4;
  433. case OBS_KEY_BACKSLASH: return VK_OEM_5;
  434. case OBS_KEY_BRACKETRIGHT: return VK_OEM_6;
  435. case OBS_KEY_ASCIITILDE: return VK_OEM_3;
  436. case OBS_KEY_HENKAN: return VK_CONVERT;
  437. case OBS_KEY_MUHENKAN: return VK_NONCONVERT;
  438. case OBS_KEY_KANJI: return VK_KANJI;
  439. case OBS_KEY_TOUROKU: return VK_OEM_FJ_TOUROKU;
  440. case OBS_KEY_MASSYO: return VK_OEM_FJ_MASSHOU;
  441. case OBS_KEY_HANGUL: return VK_HANGUL;
  442. case OBS_KEY_BACKSLASH_RT102: return VK_OEM_102;
  443. case OBS_KEY_MOUSE1: return VK_LBUTTON;
  444. case OBS_KEY_MOUSE2: return VK_RBUTTON;
  445. case OBS_KEY_MOUSE3: return VK_MBUTTON;
  446. case OBS_KEY_MOUSE4: return VK_XBUTTON1;
  447. case OBS_KEY_MOUSE5: return VK_XBUTTON2;
  448. /* TODO: Implement keys for non-US keyboards */
  449. default:;
  450. }
  451. return 0;
  452. }
  453. bool obs_hotkeys_platform_init(struct obs_core_hotkeys *hotkeys)
  454. {
  455. hotkeys->platform_context = bzalloc(sizeof(obs_hotkeys_platform_t));
  456. for (size_t i = 0; i < OBS_KEY_LAST_VALUE; i++)
  457. hotkeys->platform_context->vk_codes[i] = get_virtual_key(i);
  458. return true;
  459. }
  460. void obs_hotkeys_platform_free(struct obs_core_hotkeys *hotkeys)
  461. {
  462. bfree(hotkeys->platform_context);
  463. hotkeys->platform_context = NULL;
  464. }
  465. static bool vk_down(DWORD vk)
  466. {
  467. short state = GetAsyncKeyState(vk);
  468. bool down = (state & 0x8000) != 0;
  469. return down;
  470. }
  471. bool obs_hotkeys_platform_is_pressed(obs_hotkeys_platform_t *context,
  472. obs_key_t key)
  473. {
  474. if (key == OBS_KEY_META) {
  475. return vk_down(VK_LWIN) || vk_down(VK_RWIN);
  476. }
  477. UNUSED_PARAMETER(context);
  478. return vk_down(obs_key_to_virtual_key(key));
  479. }
  480. void obs_key_to_str(obs_key_t key, struct dstr *str)
  481. {
  482. wchar_t name[128] = L"";
  483. UINT scan_code;
  484. int vk;
  485. if (key == OBS_KEY_NONE) {
  486. return;
  487. } else if (key >= OBS_KEY_MOUSE1 && key <= OBS_KEY_MOUSE29) {
  488. if (obs->hotkeys.translations[key]) {
  489. dstr_copy(str, obs->hotkeys.translations[key]);
  490. } else {
  491. dstr_printf(str, "Mouse %d",
  492. (int)(key - OBS_KEY_MOUSE1 + 1));
  493. }
  494. return;
  495. } if (key == OBS_KEY_PAUSE) {
  496. dstr_copy(str, obs_get_hotkey_translation(key, "Pause"));
  497. return;
  498. } else if (key == OBS_KEY_META) {
  499. dstr_copy(str, obs_get_hotkey_translation(key, "Windows"));
  500. return;
  501. }
  502. vk = obs_key_to_virtual_key(key);
  503. scan_code = MapVirtualKey(vk, 0) << 16;
  504. switch (vk) {
  505. case VK_HOME:
  506. case VK_END:
  507. case VK_LEFT:
  508. case VK_UP:
  509. case VK_RIGHT:
  510. case VK_DOWN:
  511. case VK_PRIOR:
  512. case VK_NEXT:
  513. case VK_INSERT:
  514. case VK_DELETE:
  515. case VK_NUMLOCK:
  516. scan_code |= 0x01000000;
  517. }
  518. if (scan_code != 0 && GetKeyNameTextW(scan_code, name, 128) != 0) {
  519. dstr_from_wcs(str, name);
  520. } else if (key != OBS_KEY_NONE) {
  521. dstr_copy(str, obs_key_to_name(key));
  522. }
  523. }
  524. obs_key_t obs_key_from_virtual_key(int code)
  525. {
  526. obs_hotkeys_platform_t *platform = obs->hotkeys.platform_context;
  527. for (size_t i = 0; i < OBS_KEY_LAST_VALUE; i++) {
  528. if (platform->vk_codes[i] == code) {
  529. return (obs_key_t)i;
  530. }
  531. }
  532. return OBS_KEY_NONE;
  533. }
  534. int obs_key_to_virtual_key(obs_key_t key)
  535. {
  536. if (key == OBS_KEY_META)
  537. return VK_LWIN;
  538. return obs->hotkeys.platform_context->vk_codes[(int)key];
  539. }
  540. static inline void add_combo_key(obs_key_t key, struct dstr *str)
  541. {
  542. struct dstr key_str = {0};
  543. obs_key_to_str(key, &key_str);
  544. if (!dstr_is_empty(&key_str)) {
  545. if (!dstr_is_empty(str)) {
  546. dstr_cat(str, " + ");
  547. }
  548. dstr_cat_dstr(str, &key_str);
  549. }
  550. dstr_free(&key_str);
  551. }
  552. void obs_key_combination_to_str(obs_key_combination_t combination,
  553. struct dstr *str)
  554. {
  555. if ((combination.modifiers & INTERACT_CONTROL_KEY) != 0) {
  556. add_combo_key(OBS_KEY_CONTROL, str);
  557. }
  558. if ((combination.modifiers & INTERACT_COMMAND_KEY) != 0) {
  559. add_combo_key(OBS_KEY_META, str);
  560. }
  561. if ((combination.modifiers & INTERACT_ALT_KEY) != 0) {
  562. add_combo_key(OBS_KEY_ALT, str);
  563. }
  564. if ((combination.modifiers & INTERACT_SHIFT_KEY) != 0) {
  565. add_combo_key(OBS_KEY_SHIFT, str);
  566. }
  567. if (combination.key != OBS_KEY_NONE) {
  568. add_combo_key(combination.key, str);
  569. }
  570. }
  571. bool sym_initialize_called = false;
  572. void reset_win32_symbol_paths(void)
  573. {
  574. static BOOL (WINAPI *sym_initialize_w)(HANDLE, const wchar_t*, BOOL);
  575. static BOOL (WINAPI *sym_set_search_path_w)(HANDLE, const wchar_t*);
  576. static bool funcs_initialized = false;
  577. static bool initialize_success = false;
  578. struct obs_module *module = obs->first_module;
  579. struct dstr path_str = {0};
  580. DARRAY(char*) paths;
  581. wchar_t *path_str_w = NULL;
  582. char *abspath;
  583. da_init(paths);
  584. if (!funcs_initialized) {
  585. HMODULE mod;
  586. funcs_initialized = true;
  587. mod = LoadLibraryW(L"DbgHelp");
  588. if (!mod)
  589. return;
  590. sym_initialize_w = (void*)GetProcAddress(mod, "SymInitializeW");
  591. sym_set_search_path_w = (void*)GetProcAddress(mod,
  592. "SymSetSearchPathW");
  593. if (!sym_initialize_w || !sym_set_search_path_w)
  594. return;
  595. initialize_success = true;
  596. }
  597. if (!initialize_success)
  598. return;
  599. abspath = os_get_abs_path_ptr(".");
  600. if (abspath)
  601. da_push_back(paths, &abspath);
  602. while (module) {
  603. bool found = false;
  604. struct dstr path = {0};
  605. char *path_end;
  606. dstr_copy(&path, module->bin_path);
  607. dstr_replace(&path, "/", "\\");
  608. path_end = strrchr(path.array, '\\');
  609. if (!path_end) {
  610. module = module->next;
  611. dstr_free(&path);
  612. continue;
  613. }
  614. *path_end = 0;
  615. for (size_t i = 0; i < paths.num; i++) {
  616. const char *existing_path = paths.array[i];
  617. if (astrcmpi(path.array, existing_path) == 0) {
  618. found = true;
  619. break;
  620. }
  621. }
  622. if (!found) {
  623. abspath = os_get_abs_path_ptr(path.array);
  624. if (abspath)
  625. da_push_back(paths, &abspath);
  626. }
  627. dstr_free(&path);
  628. module = module->next;
  629. }
  630. for (size_t i = 0; i < paths.num; i++) {
  631. const char *path = paths.array[i];
  632. if (path && *path) {
  633. if (i != 0)
  634. dstr_cat(&path_str, ";");
  635. dstr_cat(&path_str, paths.array[i]);
  636. }
  637. }
  638. if (path_str.array) {
  639. os_utf8_to_wcs_ptr(path_str.array, path_str.len, &path_str_w);
  640. if (path_str_w) {
  641. if (!sym_initialize_called) {
  642. sym_initialize_w(GetCurrentProcess(),
  643. path_str_w, false);
  644. sym_initialize_called = true;
  645. } else {
  646. sym_set_search_path_w(GetCurrentProcess(),
  647. path_str_w);
  648. }
  649. bfree(path_str_w);
  650. }
  651. }
  652. for (size_t i = 0; i < paths.num; i++)
  653. bfree(paths.array[i]);
  654. dstr_free(&path_str);
  655. da_free(paths);
  656. }
  657. void initialize_com(void)
  658. {
  659. CoInitializeEx(0, COINIT_MULTITHREADED);
  660. }
  661. void uninitialize_com(void)
  662. {
  663. CoUninitialize();
  664. }