1
0

obs-windows.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  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, sizeof(data));
  68. status = RegOpenKeyW(HKEY_LOCAL_MACHINE,
  69. L"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0",
  70. &key);
  71. if (status != ERROR_SUCCESS)
  72. return;
  73. size = sizeof(data);
  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. if (game_mode_enabled.status != ERROR_SUCCESS) {
  180. get_reg_dword(HKEY_CURRENT_USER, WIN10_GAME_MODE_REG_KEY,
  181. L"AutoGameModeEnabled", &game_mode_enabled);
  182. }
  183. blog(LOG_INFO, "Windows 10 Gaming Features:");
  184. if (game_bar_enabled.status == ERROR_SUCCESS) {
  185. blog(LOG_INFO, "\tGame Bar: %s",
  186. (bool)game_bar_enabled.return_value ? "On" : "Off");
  187. }
  188. if (game_dvr_allowed.status == ERROR_SUCCESS) {
  189. blog(LOG_INFO, "\tGame DVR Allowed: %s",
  190. (bool)game_dvr_allowed.return_value ? "Yes" : "No");
  191. }
  192. if (game_dvr_enabled.status == ERROR_SUCCESS) {
  193. blog(LOG_INFO, "\tGame DVR: %s",
  194. (bool)game_dvr_enabled.return_value ? "On" : "Off");
  195. }
  196. if (game_dvr_bg_recording.status == ERROR_SUCCESS) {
  197. blog(LOG_INFO, "\tGame DVR Background Recording: %s",
  198. (bool)game_dvr_bg_recording.return_value ? "On" :
  199. "Off");
  200. }
  201. if (game_mode_enabled.status == ERROR_SUCCESS) {
  202. blog(LOG_INFO, "\tGame Mode: %s",
  203. (bool)game_mode_enabled.return_value ? "On" : "Off");
  204. }
  205. }
  206. static const char *get_str_for_state(int state)
  207. {
  208. switch (state) {
  209. case WSC_SECURITY_PRODUCT_STATE_ON:
  210. return "enabled";
  211. case WSC_SECURITY_PRODUCT_STATE_OFF:
  212. return "disabled";
  213. case WSC_SECURITY_PRODUCT_STATE_SNOOZED:
  214. return "temporarily disabled";
  215. case WSC_SECURITY_PRODUCT_STATE_EXPIRED:
  216. return "expired";
  217. default:
  218. return "unknown";
  219. }
  220. }
  221. static const char *get_str_for_type(int type)
  222. {
  223. switch (type) {
  224. case WSC_SECURITY_PROVIDER_ANTIVIRUS:
  225. return "AV";
  226. case WSC_SECURITY_PROVIDER_FIREWALL:
  227. return "FW";
  228. case WSC_SECURITY_PROVIDER_ANTISPYWARE:
  229. return "ASW";
  230. default:
  231. return "unknown";
  232. }
  233. }
  234. static void log_security_products_by_type(IWSCProductList *prod_list, int type)
  235. {
  236. HRESULT hr;
  237. LONG count = 0;
  238. IWscProduct *prod;
  239. BSTR name;
  240. WSC_SECURITY_PRODUCT_STATE prod_state;
  241. hr = prod_list->lpVtbl->Initialize(prod_list, type);
  242. if (FAILED(hr))
  243. return;
  244. hr = prod_list->lpVtbl->get_Count(prod_list, &count);
  245. if (FAILED(hr)) {
  246. prod_list->lpVtbl->Release(prod_list);
  247. return;
  248. }
  249. for (int i = 0; i < count; i++) {
  250. hr = prod_list->lpVtbl->get_Item(prod_list, i, &prod);
  251. if (FAILED(hr))
  252. continue;
  253. hr = prod->lpVtbl->get_ProductName(prod, &name);
  254. if (FAILED(hr))
  255. continue;
  256. hr = prod->lpVtbl->get_ProductState(prod, &prod_state);
  257. if (FAILED(hr)) {
  258. SysFreeString(name);
  259. continue;
  260. }
  261. blog(LOG_INFO, "\t%S: %s (%s)", name,
  262. get_str_for_state(prod_state),
  263. get_str_for_type(type));
  264. SysFreeString(name);
  265. prod->lpVtbl->Release(prod);
  266. }
  267. prod_list->lpVtbl->Release(prod_list);
  268. }
  269. static void log_security_products(void)
  270. {
  271. IWSCProductList *prod_list = NULL;
  272. HMODULE h_wsc;
  273. HRESULT hr;
  274. /* We load the DLL rather than import wcsapi.lib because the clsid /
  275. * iid only exists on Windows 8 or higher. */
  276. h_wsc = LoadLibraryW(L"wscapi.dll");
  277. if (!h_wsc)
  278. return;
  279. const CLSID *prod_list_clsid =
  280. (const CLSID *)GetProcAddress(h_wsc, "CLSID_WSCProductList");
  281. const IID *prod_list_iid =
  282. (const IID *)GetProcAddress(h_wsc, "IID_IWSCProductList");
  283. if (prod_list_clsid && prod_list_iid) {
  284. blog(LOG_INFO, "Sec. Software Status:");
  285. hr = CoCreateInstance(prod_list_clsid, NULL,
  286. CLSCTX_INPROC_SERVER, prod_list_iid,
  287. &prod_list);
  288. if (!FAILED(hr)) {
  289. log_security_products_by_type(prod_list,
  290. WSC_SECURITY_PROVIDER_ANTIVIRUS);
  291. }
  292. hr = CoCreateInstance(prod_list_clsid, NULL,
  293. CLSCTX_INPROC_SERVER, prod_list_iid,
  294. &prod_list);
  295. if (!FAILED(hr)) {
  296. log_security_products_by_type(prod_list,
  297. WSC_SECURITY_PROVIDER_FIREWALL);
  298. }
  299. hr = CoCreateInstance(prod_list_clsid, NULL,
  300. CLSCTX_INPROC_SERVER, prod_list_iid,
  301. &prod_list);
  302. if (!FAILED(hr)) {
  303. log_security_products_by_type(prod_list,
  304. WSC_SECURITY_PROVIDER_ANTISPYWARE);
  305. }
  306. }
  307. FreeLibrary(h_wsc);
  308. }
  309. void log_system_info(void)
  310. {
  311. struct win_version_info ver;
  312. get_win_ver(&ver);
  313. win_ver = (ver.major << 8) | ver.minor;
  314. log_processor_info();
  315. log_processor_cores();
  316. log_available_memory();
  317. log_windows_version();
  318. log_admin_status();
  319. log_aero();
  320. log_gaming_features();
  321. log_security_products();
  322. }
  323. struct obs_hotkeys_platform {
  324. int vk_codes[OBS_KEY_LAST_VALUE];
  325. };
  326. static int get_virtual_key(obs_key_t key)
  327. {
  328. switch (key) {
  329. case OBS_KEY_RETURN: return VK_RETURN;
  330. case OBS_KEY_ESCAPE: return VK_ESCAPE;
  331. case OBS_KEY_TAB: return VK_TAB;
  332. case OBS_KEY_BACKTAB: return VK_OEM_BACKTAB;
  333. case OBS_KEY_BACKSPACE: return VK_BACK;
  334. case OBS_KEY_INSERT: return VK_INSERT;
  335. case OBS_KEY_DELETE: return VK_DELETE;
  336. case OBS_KEY_PAUSE: return VK_PAUSE;
  337. case OBS_KEY_PRINT: return VK_SNAPSHOT;
  338. case OBS_KEY_CLEAR: return VK_CLEAR;
  339. case OBS_KEY_HOME: return VK_HOME;
  340. case OBS_KEY_END: return VK_END;
  341. case OBS_KEY_LEFT: return VK_LEFT;
  342. case OBS_KEY_UP: return VK_UP;
  343. case OBS_KEY_RIGHT: return VK_RIGHT;
  344. case OBS_KEY_DOWN: return VK_DOWN;
  345. case OBS_KEY_PAGEUP: return VK_PRIOR;
  346. case OBS_KEY_PAGEDOWN: return VK_NEXT;
  347. case OBS_KEY_SHIFT: return VK_SHIFT;
  348. case OBS_KEY_CONTROL: return VK_CONTROL;
  349. case OBS_KEY_ALT: return VK_MENU;
  350. case OBS_KEY_CAPSLOCK: return VK_CAPITAL;
  351. case OBS_KEY_NUMLOCK: return VK_NUMLOCK;
  352. case OBS_KEY_SCROLLLOCK: return VK_SCROLL;
  353. case OBS_KEY_F1: return VK_F1;
  354. case OBS_KEY_F2: return VK_F2;
  355. case OBS_KEY_F3: return VK_F3;
  356. case OBS_KEY_F4: return VK_F4;
  357. case OBS_KEY_F5: return VK_F5;
  358. case OBS_KEY_F6: return VK_F6;
  359. case OBS_KEY_F7: return VK_F7;
  360. case OBS_KEY_F8: return VK_F8;
  361. case OBS_KEY_F9: return VK_F9;
  362. case OBS_KEY_F10: return VK_F10;
  363. case OBS_KEY_F11: return VK_F11;
  364. case OBS_KEY_F12: return VK_F12;
  365. case OBS_KEY_F13: return VK_F13;
  366. case OBS_KEY_F14: return VK_F14;
  367. case OBS_KEY_F15: return VK_F15;
  368. case OBS_KEY_F16: return VK_F16;
  369. case OBS_KEY_F17: return VK_F17;
  370. case OBS_KEY_F18: return VK_F18;
  371. case OBS_KEY_F19: return VK_F19;
  372. case OBS_KEY_F20: return VK_F20;
  373. case OBS_KEY_F21: return VK_F21;
  374. case OBS_KEY_F22: return VK_F22;
  375. case OBS_KEY_F23: return VK_F23;
  376. case OBS_KEY_F24: return VK_F24;
  377. case OBS_KEY_SPACE: return VK_SPACE;
  378. case OBS_KEY_APOSTROPHE: return VK_OEM_7;
  379. case OBS_KEY_PLUS: return VK_OEM_PLUS;
  380. case OBS_KEY_COMMA: return VK_OEM_COMMA;
  381. case OBS_KEY_MINUS: return VK_OEM_MINUS;
  382. case OBS_KEY_PERIOD: return VK_OEM_PERIOD;
  383. case OBS_KEY_SLASH: return VK_OEM_2;
  384. case OBS_KEY_0: return '0';
  385. case OBS_KEY_1: return '1';
  386. case OBS_KEY_2: return '2';
  387. case OBS_KEY_3: return '3';
  388. case OBS_KEY_4: return '4';
  389. case OBS_KEY_5: return '5';
  390. case OBS_KEY_6: return '6';
  391. case OBS_KEY_7: return '7';
  392. case OBS_KEY_8: return '8';
  393. case OBS_KEY_9: return '9';
  394. case OBS_KEY_NUMASTERISK: return VK_MULTIPLY;
  395. case OBS_KEY_NUMPLUS: return VK_ADD;
  396. case OBS_KEY_NUMMINUS: return VK_SUBTRACT;
  397. case OBS_KEY_NUMPERIOD: return VK_DECIMAL;
  398. case OBS_KEY_NUMSLASH: return VK_DIVIDE;
  399. case OBS_KEY_NUM0: return VK_NUMPAD0;
  400. case OBS_KEY_NUM1: return VK_NUMPAD1;
  401. case OBS_KEY_NUM2: return VK_NUMPAD2;
  402. case OBS_KEY_NUM3: return VK_NUMPAD3;
  403. case OBS_KEY_NUM4: return VK_NUMPAD4;
  404. case OBS_KEY_NUM5: return VK_NUMPAD5;
  405. case OBS_KEY_NUM6: return VK_NUMPAD6;
  406. case OBS_KEY_NUM7: return VK_NUMPAD7;
  407. case OBS_KEY_NUM8: return VK_NUMPAD8;
  408. case OBS_KEY_NUM9: return VK_NUMPAD9;
  409. case OBS_KEY_SEMICOLON: return VK_OEM_1;
  410. case OBS_KEY_A: return 'A';
  411. case OBS_KEY_B: return 'B';
  412. case OBS_KEY_C: return 'C';
  413. case OBS_KEY_D: return 'D';
  414. case OBS_KEY_E: return 'E';
  415. case OBS_KEY_F: return 'F';
  416. case OBS_KEY_G: return 'G';
  417. case OBS_KEY_H: return 'H';
  418. case OBS_KEY_I: return 'I';
  419. case OBS_KEY_J: return 'J';
  420. case OBS_KEY_K: return 'K';
  421. case OBS_KEY_L: return 'L';
  422. case OBS_KEY_M: return 'M';
  423. case OBS_KEY_N: return 'N';
  424. case OBS_KEY_O: return 'O';
  425. case OBS_KEY_P: return 'P';
  426. case OBS_KEY_Q: return 'Q';
  427. case OBS_KEY_R: return 'R';
  428. case OBS_KEY_S: return 'S';
  429. case OBS_KEY_T: return 'T';
  430. case OBS_KEY_U: return 'U';
  431. case OBS_KEY_V: return 'V';
  432. case OBS_KEY_W: return 'W';
  433. case OBS_KEY_X: return 'X';
  434. case OBS_KEY_Y: return 'Y';
  435. case OBS_KEY_Z: return 'Z';
  436. case OBS_KEY_BRACKETLEFT: return VK_OEM_4;
  437. case OBS_KEY_BACKSLASH: return VK_OEM_5;
  438. case OBS_KEY_BRACKETRIGHT: return VK_OEM_6;
  439. case OBS_KEY_ASCIITILDE: return VK_OEM_3;
  440. case OBS_KEY_HENKAN: return VK_CONVERT;
  441. case OBS_KEY_MUHENKAN: return VK_NONCONVERT;
  442. case OBS_KEY_KANJI: return VK_KANJI;
  443. case OBS_KEY_TOUROKU: return VK_OEM_FJ_TOUROKU;
  444. case OBS_KEY_MASSYO: return VK_OEM_FJ_MASSHOU;
  445. case OBS_KEY_HANGUL: return VK_HANGUL;
  446. case OBS_KEY_BACKSLASH_RT102: return VK_OEM_102;
  447. case OBS_KEY_MOUSE1: return VK_LBUTTON;
  448. case OBS_KEY_MOUSE2: return VK_RBUTTON;
  449. case OBS_KEY_MOUSE3: return VK_MBUTTON;
  450. case OBS_KEY_MOUSE4: return VK_XBUTTON1;
  451. case OBS_KEY_MOUSE5: return VK_XBUTTON2;
  452. /* TODO: Implement keys for non-US keyboards */
  453. default:;
  454. }
  455. return 0;
  456. }
  457. bool obs_hotkeys_platform_init(struct obs_core_hotkeys *hotkeys)
  458. {
  459. hotkeys->platform_context = bzalloc(sizeof(obs_hotkeys_platform_t));
  460. for (size_t i = 0; i < OBS_KEY_LAST_VALUE; i++)
  461. hotkeys->platform_context->vk_codes[i] = get_virtual_key(i);
  462. return true;
  463. }
  464. void obs_hotkeys_platform_free(struct obs_core_hotkeys *hotkeys)
  465. {
  466. bfree(hotkeys->platform_context);
  467. hotkeys->platform_context = NULL;
  468. }
  469. static bool vk_down(DWORD vk)
  470. {
  471. short state = GetAsyncKeyState(vk);
  472. bool down = (state & 0x8000) != 0;
  473. return down;
  474. }
  475. bool obs_hotkeys_platform_is_pressed(obs_hotkeys_platform_t *context,
  476. obs_key_t key)
  477. {
  478. if (key == OBS_KEY_META) {
  479. return vk_down(VK_LWIN) || vk_down(VK_RWIN);
  480. }
  481. UNUSED_PARAMETER(context);
  482. return vk_down(obs_key_to_virtual_key(key));
  483. }
  484. void obs_key_to_str(obs_key_t key, struct dstr *str)
  485. {
  486. wchar_t name[128] = L"";
  487. UINT scan_code;
  488. int vk;
  489. if (key == OBS_KEY_NONE) {
  490. return;
  491. } else if (key >= OBS_KEY_MOUSE1 && key <= OBS_KEY_MOUSE29) {
  492. if (obs->hotkeys.translations[key]) {
  493. dstr_copy(str, obs->hotkeys.translations[key]);
  494. } else {
  495. dstr_printf(str, "Mouse %d",
  496. (int)(key - OBS_KEY_MOUSE1 + 1));
  497. }
  498. return;
  499. } if (key == OBS_KEY_PAUSE) {
  500. dstr_copy(str, obs_get_hotkey_translation(key, "Pause"));
  501. return;
  502. } else if (key == OBS_KEY_META) {
  503. dstr_copy(str, obs_get_hotkey_translation(key, "Windows"));
  504. return;
  505. }
  506. vk = obs_key_to_virtual_key(key);
  507. scan_code = MapVirtualKey(vk, 0) << 16;
  508. switch (vk) {
  509. case VK_HOME:
  510. case VK_END:
  511. case VK_LEFT:
  512. case VK_UP:
  513. case VK_RIGHT:
  514. case VK_DOWN:
  515. case VK_PRIOR:
  516. case VK_NEXT:
  517. case VK_INSERT:
  518. case VK_DELETE:
  519. case VK_NUMLOCK:
  520. scan_code |= 0x01000000;
  521. }
  522. if (scan_code != 0 && GetKeyNameTextW(scan_code, name, 128) != 0) {
  523. dstr_from_wcs(str, name);
  524. } else if (key != OBS_KEY_NONE) {
  525. dstr_copy(str, obs_key_to_name(key));
  526. }
  527. }
  528. obs_key_t obs_key_from_virtual_key(int code)
  529. {
  530. obs_hotkeys_platform_t *platform = obs->hotkeys.platform_context;
  531. for (size_t i = 0; i < OBS_KEY_LAST_VALUE; i++) {
  532. if (platform->vk_codes[i] == code) {
  533. return (obs_key_t)i;
  534. }
  535. }
  536. return OBS_KEY_NONE;
  537. }
  538. int obs_key_to_virtual_key(obs_key_t key)
  539. {
  540. if (key == OBS_KEY_META)
  541. return VK_LWIN;
  542. return obs->hotkeys.platform_context->vk_codes[(int)key];
  543. }
  544. static inline void add_combo_key(obs_key_t key, struct dstr *str)
  545. {
  546. struct dstr key_str = {0};
  547. obs_key_to_str(key, &key_str);
  548. if (!dstr_is_empty(&key_str)) {
  549. if (!dstr_is_empty(str)) {
  550. dstr_cat(str, " + ");
  551. }
  552. dstr_cat_dstr(str, &key_str);
  553. }
  554. dstr_free(&key_str);
  555. }
  556. void obs_key_combination_to_str(obs_key_combination_t combination,
  557. struct dstr *str)
  558. {
  559. if ((combination.modifiers & INTERACT_CONTROL_KEY) != 0) {
  560. add_combo_key(OBS_KEY_CONTROL, str);
  561. }
  562. if ((combination.modifiers & INTERACT_COMMAND_KEY) != 0) {
  563. add_combo_key(OBS_KEY_META, str);
  564. }
  565. if ((combination.modifiers & INTERACT_ALT_KEY) != 0) {
  566. add_combo_key(OBS_KEY_ALT, str);
  567. }
  568. if ((combination.modifiers & INTERACT_SHIFT_KEY) != 0) {
  569. add_combo_key(OBS_KEY_SHIFT, str);
  570. }
  571. if (combination.key != OBS_KEY_NONE) {
  572. add_combo_key(combination.key, str);
  573. }
  574. }
  575. bool sym_initialize_called = false;
  576. void reset_win32_symbol_paths(void)
  577. {
  578. static BOOL (WINAPI *sym_initialize_w)(HANDLE, const wchar_t*, BOOL);
  579. static BOOL (WINAPI *sym_set_search_path_w)(HANDLE, const wchar_t*);
  580. static bool funcs_initialized = false;
  581. static bool initialize_success = false;
  582. struct obs_module *module = obs->first_module;
  583. struct dstr path_str = {0};
  584. DARRAY(char*) paths;
  585. wchar_t *path_str_w = NULL;
  586. char *abspath;
  587. da_init(paths);
  588. if (!funcs_initialized) {
  589. HMODULE mod;
  590. funcs_initialized = true;
  591. mod = LoadLibraryW(L"DbgHelp");
  592. if (!mod)
  593. return;
  594. sym_initialize_w = (void*)GetProcAddress(mod, "SymInitializeW");
  595. sym_set_search_path_w = (void*)GetProcAddress(mod,
  596. "SymSetSearchPathW");
  597. if (!sym_initialize_w || !sym_set_search_path_w)
  598. return;
  599. initialize_success = true;
  600. }
  601. if (!initialize_success)
  602. return;
  603. abspath = os_get_abs_path_ptr(".");
  604. if (abspath)
  605. da_push_back(paths, &abspath);
  606. while (module) {
  607. bool found = false;
  608. struct dstr path = {0};
  609. char *path_end;
  610. dstr_copy(&path, module->bin_path);
  611. dstr_replace(&path, "/", "\\");
  612. path_end = strrchr(path.array, '\\');
  613. if (!path_end) {
  614. module = module->next;
  615. dstr_free(&path);
  616. continue;
  617. }
  618. *path_end = 0;
  619. for (size_t i = 0; i < paths.num; i++) {
  620. const char *existing_path = paths.array[i];
  621. if (astrcmpi(path.array, existing_path) == 0) {
  622. found = true;
  623. break;
  624. }
  625. }
  626. if (!found) {
  627. abspath = os_get_abs_path_ptr(path.array);
  628. if (abspath)
  629. da_push_back(paths, &abspath);
  630. }
  631. dstr_free(&path);
  632. module = module->next;
  633. }
  634. for (size_t i = 0; i < paths.num; i++) {
  635. const char *path = paths.array[i];
  636. if (path && *path) {
  637. if (i != 0)
  638. dstr_cat(&path_str, ";");
  639. dstr_cat(&path_str, paths.array[i]);
  640. }
  641. }
  642. if (path_str.array) {
  643. os_utf8_to_wcs_ptr(path_str.array, path_str.len, &path_str_w);
  644. if (path_str_w) {
  645. if (!sym_initialize_called) {
  646. sym_initialize_w(GetCurrentProcess(),
  647. path_str_w, false);
  648. sym_initialize_called = true;
  649. } else {
  650. sym_set_search_path_w(GetCurrentProcess(),
  651. path_str_w);
  652. }
  653. bfree(path_str_w);
  654. }
  655. }
  656. for (size_t i = 0; i < paths.num; i++)
  657. bfree(paths.array[i]);
  658. dstr_free(&path_str);
  659. da_free(paths);
  660. }
  661. extern void initialize_crash_handler(void);
  662. void obs_init_win32_crash_handler(void)
  663. {
  664. initialize_crash_handler();
  665. }
  666. void initialize_com(void)
  667. {
  668. CoInitializeEx(0, COINIT_MULTITHREADED);
  669. }
  670. void uninitialize_com(void)
  671. {
  672. CoUninitialize();
  673. }