1
0

obs-windows.c 20 KB

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