obs-windows.c 20 KB

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