game-capture.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405
  1. #include <inttypes.h>
  2. #include <obs-module.h>
  3. #include <util/platform.h>
  4. #include <windows.h>
  5. #include <dxgi.h>
  6. #include <ipc-util/pipe.h>
  7. #include "obfuscate.h"
  8. #include "graphics-hook-info.h"
  9. #include "window-helpers.h"
  10. #include "cursor-capture.h"
  11. #define do_log(level, format, ...) \
  12. blog(level, "[game-capture: '%s'] " format, \
  13. obs_source_get_name(gc->source), ##__VA_ARGS__)
  14. #define warn(format, ...) do_log(LOG_WARNING, format, ##__VA_ARGS__)
  15. #define info(format, ...) do_log(LOG_INFO, format, ##__VA_ARGS__)
  16. #define debug(format, ...) do_log(LOG_DEBUG, format, ##__VA_ARGS__)
  17. #define SETTING_ANY_FULLSCREEN "capture_any_fullscreen"
  18. #define SETTING_CAPTURE_WINDOW "window"
  19. #define SETTING_ACTIVE_WINDOW "active_window"
  20. #define SETTING_WINDOW_PRIORITY "priority"
  21. #define SETTING_COMPATIBILITY "sli_compatibility"
  22. #define SETTING_FORCE_SCALING "force_scaling"
  23. #define SETTING_SCALE_RES "scale_res"
  24. #define SETTING_CURSOR "capture_cursor"
  25. #define SETTING_TRANSPARENCY "allow_transparency"
  26. #define SETTING_LIMIT_FRAMERATE "limit_framerate"
  27. #define SETTING_CAPTURE_OVERLAYS "capture_overlays"
  28. #define TEXT_GAME_CAPTURE obs_module_text("GameCapture")
  29. #define TEXT_ANY_FULLSCREEN obs_module_text("GameCapture.AnyFullscreen")
  30. #define TEXT_SLI_COMPATIBILITY obs_module_text("Compatibility")
  31. #define TEXT_ALLOW_TRANSPARENCY obs_module_text("AllowTransparency")
  32. #define TEXT_FORCE_SCALING obs_module_text("GameCapture.ForceScaling")
  33. #define TEXT_SCALE_RES obs_module_text("GameCapture.ScaleRes")
  34. #define TEXT_WINDOW obs_module_text("WindowCapture.Window")
  35. #define TEXT_MATCH_PRIORITY obs_module_text("WindowCapture.Priority")
  36. #define TEXT_MATCH_TITLE obs_module_text("WindowCapture.Priority.Title")
  37. #define TEXT_MATCH_CLASS obs_module_text("WindowCapture.Priority.Class")
  38. #define TEXT_MATCH_EXE obs_module_text("WindowCapture.Priority.Exe")
  39. #define TEXT_CAPTURE_CURSOR obs_module_text("CaptureCursor")
  40. #define TEXT_LIMIT_FRAMERATE obs_module_text("GameCapture.LimitFramerate")
  41. #define TEXT_CAPTURE_OVERLAYS obs_module_text("GameCapture.CaptureOverlays")
  42. #define DEFAULT_RETRY_INTERVAL 2.0f
  43. #define ERROR_RETRY_INTERVAL 4.0f
  44. struct game_capture_config {
  45. char *title;
  46. char *class;
  47. char *executable;
  48. enum window_priority priority;
  49. uint32_t scale_cx;
  50. uint32_t scale_cy;
  51. bool cursor : 1;
  52. bool force_shmem : 1;
  53. bool capture_any_fullscreen : 1;
  54. bool force_scaling : 1;
  55. bool allow_transparency : 1;
  56. bool limit_framerate : 1;
  57. bool capture_overlays : 1;
  58. };
  59. struct game_capture {
  60. obs_source_t *source;
  61. struct cursor_data cursor_data;
  62. HANDLE injector_process;
  63. uint32_t cx;
  64. uint32_t cy;
  65. uint32_t pitch;
  66. DWORD process_id;
  67. DWORD thread_id;
  68. HWND next_window;
  69. HWND window;
  70. float retry_time;
  71. float fps_reset_time;
  72. float retry_interval;
  73. bool wait_for_target_startup : 1;
  74. bool active : 1;
  75. bool capturing : 1;
  76. bool activate_hook : 1;
  77. bool process_is_64bit : 1;
  78. bool error_acquiring : 1;
  79. bool dwm_capture : 1;
  80. bool initial_config : 1;
  81. bool convert_16bit : 1;
  82. struct game_capture_config config;
  83. ipc_pipe_server_t pipe;
  84. gs_texture_t *texture;
  85. struct hook_info *global_hook_info;
  86. HANDLE keep_alive;
  87. HANDLE hook_restart;
  88. HANDLE hook_stop;
  89. HANDLE hook_ready;
  90. HANDLE hook_exit;
  91. HANDLE hook_data_map;
  92. HANDLE global_hook_info_map;
  93. HANDLE target_process;
  94. HANDLE texture_mutexes[2];
  95. union {
  96. struct {
  97. struct shmem_data *shmem_data;
  98. uint8_t *texture_buffers[2];
  99. };
  100. struct shtex_data *shtex_data;
  101. void *data;
  102. };
  103. void (*copy_texture)(struct game_capture*);
  104. };
  105. struct graphics_offsets offsets32 = {0};
  106. struct graphics_offsets offsets64 = {0};
  107. static inline enum gs_color_format convert_format(uint32_t format)
  108. {
  109. switch (format) {
  110. case DXGI_FORMAT_R8G8B8A8_UNORM: return GS_RGBA;
  111. case DXGI_FORMAT_B8G8R8X8_UNORM: return GS_BGRX;
  112. case DXGI_FORMAT_B8G8R8A8_UNORM: return GS_BGRA;
  113. case DXGI_FORMAT_R10G10B10A2_UNORM: return GS_R10G10B10A2;
  114. case DXGI_FORMAT_R16G16B16A16_UNORM: return GS_RGBA16;
  115. case DXGI_FORMAT_R16G16B16A16_FLOAT: return GS_RGBA16F;
  116. case DXGI_FORMAT_R32G32B32A32_FLOAT: return GS_RGBA32F;
  117. }
  118. return GS_UNKNOWN;
  119. }
  120. static void close_handle(HANDLE *p_handle)
  121. {
  122. HANDLE handle = *p_handle;
  123. if (handle) {
  124. if (handle != INVALID_HANDLE_VALUE)
  125. CloseHandle(handle);
  126. *p_handle = NULL;
  127. }
  128. }
  129. static inline HMODULE kernel32(void)
  130. {
  131. static HMODULE kernel32_handle = NULL;
  132. if (!kernel32_handle)
  133. kernel32_handle = GetModuleHandleW(L"kernel32");
  134. return kernel32_handle;
  135. }
  136. static inline HANDLE open_process(DWORD desired_access, bool inherit_handle,
  137. DWORD process_id)
  138. {
  139. static HANDLE (WINAPI *open_process_proc)(DWORD, BOOL, DWORD) = NULL;
  140. if (!open_process_proc)
  141. open_process_proc = get_obfuscated_func(kernel32(),
  142. "NuagUykjcxr", 0x1B694B59451ULL);
  143. return open_process_proc(desired_access, inherit_handle, process_id);
  144. }
  145. static void stop_capture(struct game_capture *gc)
  146. {
  147. ipc_pipe_server_free(&gc->pipe);
  148. if (gc->hook_stop) {
  149. SetEvent(gc->hook_stop);
  150. }
  151. if (gc->global_hook_info) {
  152. UnmapViewOfFile(gc->global_hook_info);
  153. gc->global_hook_info = NULL;
  154. }
  155. if (gc->data) {
  156. UnmapViewOfFile(gc->data);
  157. gc->data = NULL;
  158. }
  159. close_handle(&gc->keep_alive);
  160. close_handle(&gc->hook_restart);
  161. close_handle(&gc->hook_stop);
  162. close_handle(&gc->hook_ready);
  163. close_handle(&gc->hook_exit);
  164. close_handle(&gc->hook_data_map);
  165. close_handle(&gc->global_hook_info_map);
  166. close_handle(&gc->target_process);
  167. close_handle(&gc->texture_mutexes[0]);
  168. close_handle(&gc->texture_mutexes[1]);
  169. if (gc->texture) {
  170. obs_enter_graphics();
  171. gs_texture_destroy(gc->texture);
  172. obs_leave_graphics();
  173. gc->texture = NULL;
  174. }
  175. gc->copy_texture = NULL;
  176. gc->wait_for_target_startup = false;
  177. gc->active = false;
  178. gc->capturing = false;
  179. }
  180. static inline void free_config(struct game_capture_config *config)
  181. {
  182. bfree(config->title);
  183. bfree(config->class);
  184. bfree(config->executable);
  185. memset(config, 0, sizeof(*config));
  186. }
  187. static void game_capture_destroy(void *data)
  188. {
  189. struct game_capture *gc = data;
  190. stop_capture(gc);
  191. obs_enter_graphics();
  192. cursor_data_free(&gc->cursor_data);
  193. obs_leave_graphics();
  194. free_config(&gc->config);
  195. bfree(gc);
  196. }
  197. static inline void get_config(struct game_capture_config *cfg,
  198. obs_data_t *settings, const char *window)
  199. {
  200. int ret;
  201. const char *scale_str;
  202. build_window_strings(window, &cfg->class, &cfg->title,
  203. &cfg->executable);
  204. cfg->capture_any_fullscreen = obs_data_get_bool(settings,
  205. SETTING_ANY_FULLSCREEN);
  206. cfg->priority = (enum window_priority)obs_data_get_int(settings,
  207. SETTING_WINDOW_PRIORITY);
  208. cfg->force_shmem = obs_data_get_bool(settings,
  209. SETTING_COMPATIBILITY);
  210. cfg->cursor = obs_data_get_bool(settings, SETTING_CURSOR);
  211. cfg->allow_transparency = obs_data_get_bool(settings,
  212. SETTING_TRANSPARENCY);
  213. cfg->force_scaling = obs_data_get_bool(settings,
  214. SETTING_FORCE_SCALING);
  215. cfg->limit_framerate = obs_data_get_bool(settings,
  216. SETTING_LIMIT_FRAMERATE);
  217. cfg->capture_overlays = obs_data_get_bool(settings,
  218. SETTING_CAPTURE_OVERLAYS);
  219. scale_str = obs_data_get_string(settings, SETTING_SCALE_RES);
  220. ret = sscanf(scale_str, "%"PRIu32"x%"PRIu32,
  221. &cfg->scale_cx, &cfg->scale_cy);
  222. cfg->scale_cx &= ~2;
  223. cfg->scale_cy &= ~2;
  224. if (cfg->force_scaling) {
  225. if (ret != 2 || cfg->scale_cx == 0 || cfg->scale_cy == 0) {
  226. cfg->scale_cx = 0;
  227. cfg->scale_cy = 0;
  228. }
  229. }
  230. }
  231. static inline int s_cmp(const char *str1, const char *str2)
  232. {
  233. if (!str1 || !str2)
  234. return -1;
  235. return strcmp(str1, str2);
  236. }
  237. static inline bool capture_needs_reset(struct game_capture_config *cfg1,
  238. struct game_capture_config *cfg2)
  239. {
  240. if (cfg1->capture_any_fullscreen != cfg2->capture_any_fullscreen) {
  241. return true;
  242. } else if (!cfg1->capture_any_fullscreen &&
  243. (s_cmp(cfg1->class, cfg2->class) != 0 ||
  244. s_cmp(cfg1->title, cfg2->title) != 0 ||
  245. s_cmp(cfg1->executable, cfg2->executable) != 0 ||
  246. cfg1->priority != cfg2->priority)) {
  247. return true;
  248. } else if (cfg1->force_scaling != cfg2->force_scaling) {
  249. return true;
  250. } else if (cfg1->force_scaling &&
  251. (cfg1->scale_cx != cfg2->scale_cx ||
  252. cfg1->scale_cy != cfg2->scale_cy)) {
  253. return true;
  254. } else if (cfg1->force_shmem != cfg2->force_shmem) {
  255. return true;
  256. } else if (cfg1->limit_framerate != cfg2->limit_framerate) {
  257. return true;
  258. } else if (cfg1->capture_overlays != cfg2->capture_overlays) {
  259. return true;
  260. }
  261. return false;
  262. }
  263. static void game_capture_update(void *data, obs_data_t *settings)
  264. {
  265. struct game_capture *gc = data;
  266. struct game_capture_config cfg;
  267. bool reset_capture = false;
  268. const char *window = obs_data_get_string(settings,
  269. SETTING_CAPTURE_WINDOW);
  270. get_config(&cfg, settings, window);
  271. reset_capture = capture_needs_reset(&cfg, &gc->config);
  272. if (cfg.force_scaling && (cfg.scale_cx == 0 || cfg.scale_cy == 0)) {
  273. gc->error_acquiring = true;
  274. warn("error acquiring, scale is bad");
  275. } else {
  276. gc->error_acquiring = false;
  277. }
  278. free_config(&gc->config);
  279. gc->config = cfg;
  280. gc->activate_hook = !!window && !!*window;
  281. gc->retry_interval = DEFAULT_RETRY_INTERVAL;
  282. if (!gc->initial_config) {
  283. if (reset_capture) {
  284. stop_capture(gc);
  285. }
  286. } else {
  287. gc->initial_config = false;
  288. }
  289. }
  290. static void *game_capture_create(obs_data_t *settings, obs_source_t *source)
  291. {
  292. struct game_capture *gc = bzalloc(sizeof(*gc));
  293. gc->source = source;
  294. gc->initial_config = true;
  295. gc->retry_interval = DEFAULT_RETRY_INTERVAL;
  296. game_capture_update(gc, settings);
  297. return gc;
  298. }
  299. static inline HANDLE create_event_id(bool manual_reset, bool initial_state,
  300. const char *name, DWORD process_id)
  301. {
  302. char new_name[128];
  303. sprintf(new_name, "%s%lu", name, process_id);
  304. return CreateEventA(NULL, manual_reset, initial_state, new_name);
  305. }
  306. static inline HANDLE open_event_id(const char *name, DWORD process_id)
  307. {
  308. char new_name[128];
  309. sprintf(new_name, "%s%lu", name, process_id);
  310. return OpenEventA(EVENT_ALL_ACCESS, false, new_name);
  311. }
  312. #define STOP_BEING_BAD \
  313. " This is most likely due to security software. Please make sure " \
  314. "that the OBS installation folder is excluded/ignored in the " \
  315. "settings of the security software you are using."
  316. static bool check_file_integrity(struct game_capture *gc, const char *file,
  317. const char *name)
  318. {
  319. DWORD error;
  320. HANDLE handle;
  321. if (!file || !*file) {
  322. warn("Game capture %s not found." STOP_BEING_BAD, name);
  323. return false;
  324. }
  325. handle = CreateFileA(file, GENERIC_READ | GENERIC_EXECUTE,
  326. FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
  327. if (handle != INVALID_HANDLE_VALUE) {
  328. CloseHandle(handle);
  329. return true;
  330. }
  331. error = GetLastError();
  332. if (error == ERROR_FILE_NOT_FOUND) {
  333. warn("Game capture file '%s' not found."
  334. STOP_BEING_BAD, file);
  335. } else if (error == ERROR_ACCESS_DENIED) {
  336. warn("Game capture file '%s' could not be loaded."
  337. STOP_BEING_BAD, file);
  338. } else {
  339. warn("Game capture file '%s' could not be loaded: %lu."
  340. STOP_BEING_BAD, file, error);
  341. }
  342. return false;
  343. }
  344. static inline bool is_64bit_windows(void)
  345. {
  346. #ifdef _WIN64
  347. return true;
  348. #else
  349. BOOL x86 = false;
  350. bool success = !!IsWow64Process(GetCurrentProcess(), &x86);
  351. return success && !!x86;
  352. #endif
  353. }
  354. static inline bool is_64bit_process(HANDLE process)
  355. {
  356. BOOL x86 = true;
  357. if (is_64bit_windows()) {
  358. bool success = !!IsWow64Process(process, &x86);
  359. if (!success) {
  360. return false;
  361. }
  362. }
  363. return !x86;
  364. }
  365. static inline bool open_target_process(struct game_capture *gc)
  366. {
  367. gc->target_process = open_process(
  368. PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
  369. false, gc->process_id);
  370. if (!gc->target_process) {
  371. warn("could not open process: %s", gc->config.executable);
  372. return false;
  373. }
  374. gc->process_is_64bit = is_64bit_process(gc->target_process);
  375. return true;
  376. }
  377. static inline bool init_keepalive(struct game_capture *gc)
  378. {
  379. gc->keep_alive = create_event_id(false, false, EVENT_HOOK_KEEPALIVE,
  380. gc->process_id);
  381. if (!gc->keep_alive) {
  382. warn("failed to create keepalive event");
  383. return false;
  384. }
  385. return true;
  386. }
  387. static inline bool init_texture_mutexes(struct game_capture *gc)
  388. {
  389. gc->texture_mutexes[0] = get_mutex_plus_id(MUTEX_TEXTURE1,
  390. gc->process_id);
  391. gc->texture_mutexes[1] = get_mutex_plus_id(MUTEX_TEXTURE2,
  392. gc->process_id);
  393. if (!gc->texture_mutexes[0] || !gc->texture_mutexes[1]) {
  394. warn("failed to create texture mutexes: %lu", GetLastError());
  395. return false;
  396. }
  397. return true;
  398. }
  399. /* if there's already a hook in the process, then signal and start */
  400. static inline bool attempt_existing_hook(struct game_capture *gc)
  401. {
  402. gc->hook_restart = open_event_id(EVENT_CAPTURE_RESTART, gc->process_id);
  403. if (gc->hook_restart) {
  404. debug("existing hook found, signaling process: %s",
  405. gc->config.executable);
  406. SetEvent(gc->hook_restart);
  407. return true;
  408. }
  409. return false;
  410. }
  411. static inline void reset_frame_interval(struct game_capture *gc)
  412. {
  413. struct obs_video_info ovi;
  414. uint64_t interval = 0;
  415. if (gc->config.limit_framerate && obs_get_video_info(&ovi))
  416. interval = ovi.fps_den * 1000000000ULL / ovi.fps_num;
  417. gc->global_hook_info->frame_interval = interval;
  418. }
  419. static inline bool init_hook_info(struct game_capture *gc)
  420. {
  421. gc->global_hook_info_map = get_hook_info(gc->process_id);
  422. if (!gc->global_hook_info_map) {
  423. warn("init_hook_info: get_hook_info failed: %lu",
  424. GetLastError());
  425. return false;
  426. }
  427. gc->global_hook_info = MapViewOfFile(gc->global_hook_info_map,
  428. FILE_MAP_ALL_ACCESS, 0, 0,
  429. sizeof(*gc->global_hook_info));
  430. if (!gc->global_hook_info) {
  431. warn("init_hook_info: failed to map data view: %lu",
  432. GetLastError());
  433. return false;
  434. }
  435. gc->global_hook_info->offsets = gc->process_is_64bit ?
  436. offsets64 : offsets32;
  437. gc->global_hook_info->capture_overlay = gc->config.capture_overlays;
  438. gc->global_hook_info->force_shmem = gc->config.force_shmem;
  439. gc->global_hook_info->use_scale = gc->config.force_scaling;
  440. gc->global_hook_info->cx = gc->config.scale_cx;
  441. gc->global_hook_info->cy = gc->config.scale_cy;
  442. reset_frame_interval(gc);
  443. obs_enter_graphics();
  444. if (!gs_shared_texture_available())
  445. gc->global_hook_info->force_shmem = true;
  446. obs_leave_graphics();
  447. obs_enter_graphics();
  448. if (!gs_shared_texture_available())
  449. gc->global_hook_info->force_shmem = true;
  450. obs_leave_graphics();
  451. return true;
  452. }
  453. static void pipe_log(void *param, uint8_t *data, size_t size)
  454. {
  455. struct game_capture *gc = param;
  456. if (data && size)
  457. info("%s", data);
  458. }
  459. static inline bool init_pipe(struct game_capture *gc)
  460. {
  461. char name[64];
  462. sprintf(name, "%s%lu", PIPE_NAME, gc->process_id);
  463. if (!ipc_pipe_server_start(&gc->pipe, name, pipe_log, gc)) {
  464. warn("init_pipe: failed to start pipe");
  465. return false;
  466. }
  467. return true;
  468. }
  469. static inline bool create_inject_process(struct game_capture *gc,
  470. const char *inject_path, const char *hook_path)
  471. {
  472. wchar_t *command_line_w = malloc(4096 * sizeof(wchar_t));
  473. wchar_t *inject_path_w;
  474. wchar_t *hook_path_w;
  475. PROCESS_INFORMATION pi = {0};
  476. STARTUPINFO si = {0};
  477. bool success = false;
  478. os_utf8_to_wcs_ptr(inject_path, 0, &inject_path_w);
  479. os_utf8_to_wcs_ptr(hook_path, 0, &hook_path_w);
  480. si.cb = sizeof(si);
  481. swprintf(command_line_w, 4096, L"\"%s\" \"%s\" %lu",
  482. inject_path_w, hook_path_w, gc->thread_id);
  483. success = !!CreateProcessW(inject_path_w, command_line_w, NULL, NULL,
  484. false, CREATE_NO_WINDOW, NULL, NULL, &si, &pi);
  485. if (success) {
  486. CloseHandle(pi.hThread);
  487. gc->injector_process = pi.hProcess;
  488. } else {
  489. warn("Failed to create inject helper process: %lu",
  490. GetLastError());
  491. }
  492. free(command_line_w);
  493. bfree(inject_path_w);
  494. bfree(hook_path_w);
  495. return success;
  496. }
  497. static inline bool inject_hook(struct game_capture *gc)
  498. {
  499. bool success = false;
  500. char *inject_path;
  501. char *hook_path;
  502. if (gc->process_is_64bit) {
  503. inject_path = obs_module_file("inject-helper64.exe");
  504. hook_path = obs_module_file("graphics-hook64.dll");
  505. } else {
  506. inject_path = obs_module_file("inject-helper32.exe");
  507. hook_path = obs_module_file("graphics-hook32.dll");
  508. }
  509. if (!check_file_integrity(gc, inject_path, "inject helper")) {
  510. goto cleanup;
  511. }
  512. if (!check_file_integrity(gc, hook_path, "graphics hook")) {
  513. goto cleanup;
  514. }
  515. success = create_inject_process(gc, inject_path, hook_path);
  516. cleanup:
  517. bfree(inject_path);
  518. bfree(hook_path);
  519. return success;
  520. }
  521. static bool init_hook(struct game_capture *gc)
  522. {
  523. if (gc->config.capture_any_fullscreen) {
  524. struct dstr name = {0};
  525. if (get_window_exe(&name, gc->next_window)) {
  526. info("attempting to hook fullscreen process: %s",
  527. name.array);
  528. dstr_free(&name);
  529. }
  530. } else {
  531. info("attempting to hook process: %s", gc->config.executable);
  532. }
  533. if (!open_target_process(gc)) {
  534. return false;
  535. }
  536. if (!init_keepalive(gc)) {
  537. return false;
  538. }
  539. if (!init_texture_mutexes(gc)) {
  540. return false;
  541. }
  542. if (!init_hook_info(gc)) {
  543. return false;
  544. }
  545. if (!init_pipe(gc)) {
  546. return false;
  547. }
  548. if (!attempt_existing_hook(gc)) {
  549. if (!inject_hook(gc)) {
  550. return false;
  551. }
  552. }
  553. gc->window = gc->next_window;
  554. gc->next_window = NULL;
  555. gc->active = true;
  556. return true;
  557. }
  558. static void get_fullscreen_window(struct game_capture *gc)
  559. {
  560. HWND window = GetForegroundWindow();
  561. MONITORINFO mi = {0};
  562. HMONITOR monitor;
  563. DWORD styles;
  564. RECT rect;
  565. gc->next_window = NULL;
  566. if (!window) {
  567. return;
  568. }
  569. if (!GetWindowRect(window, &rect)) {
  570. return;
  571. }
  572. /* ignore regular maximized windows */
  573. styles = (DWORD)GetWindowLongPtr(window, GWL_STYLE);
  574. if ((styles & WS_MAXIMIZE) != 0 && (styles & WS_BORDER) != 0) {
  575. return;
  576. }
  577. monitor = MonitorFromRect(&rect, MONITOR_DEFAULTTONEAREST);
  578. if (!monitor) {
  579. return;
  580. }
  581. mi.cbSize = sizeof(mi);
  582. if (!GetMonitorInfo(monitor, &mi)) {
  583. return;
  584. }
  585. if (rect.left == mi.rcMonitor.left &&
  586. rect.right == mi.rcMonitor.right &&
  587. rect.bottom == mi.rcMonitor.bottom &&
  588. rect.top == mi.rcMonitor.top) {
  589. /* always wait a bit for the target process to start up before
  590. * starting the hook process; sometimes they have important
  591. * modules to load first or other hooks (such as steam) need a
  592. * little bit of time to load. ultimately this helps prevent
  593. * crashes */
  594. if (gc->wait_for_target_startup) {
  595. gc->retry_interval = 3.0f;
  596. gc->wait_for_target_startup = false;
  597. } else {
  598. gc->next_window = window;
  599. }
  600. } else {
  601. gc->wait_for_target_startup = true;
  602. }
  603. }
  604. static void get_selected_window(struct game_capture *gc)
  605. {
  606. if (strcmpi(gc->config.class, "dwm") == 0) {
  607. wchar_t class_w[512];
  608. os_utf8_to_wcs(gc->config.class, 0, class_w, 512);
  609. gc->next_window = FindWindowW(class_w, NULL);
  610. } else {
  611. gc->next_window = find_window(INCLUDE_MINIMIZED,
  612. gc->config.priority,
  613. gc->config.class,
  614. gc->config.title,
  615. gc->config.executable);
  616. }
  617. }
  618. static void try_hook(struct game_capture *gc)
  619. {
  620. if (gc->config.capture_any_fullscreen) {
  621. get_fullscreen_window(gc);
  622. } else {
  623. get_selected_window(gc);
  624. }
  625. if (gc->next_window) {
  626. gc->thread_id = GetWindowThreadProcessId(gc->next_window,
  627. &gc->process_id);
  628. if (!gc->thread_id || !gc->process_id) {
  629. warn("error acquiring, failed to get window "
  630. "thread/process ids: %lu",
  631. GetLastError());
  632. gc->error_acquiring = true;
  633. return;
  634. }
  635. if (!init_hook(gc)) {
  636. stop_capture(gc);
  637. }
  638. } else {
  639. gc->active = false;
  640. }
  641. }
  642. static inline bool init_events(struct game_capture *gc)
  643. {
  644. if (!gc->hook_restart) {
  645. gc->hook_restart = get_event_plus_id(EVENT_CAPTURE_RESTART,
  646. gc->process_id);
  647. if (!gc->hook_restart) {
  648. warn("init_events: failed to get hook_restart "
  649. "event: %lu", GetLastError());
  650. return false;
  651. }
  652. }
  653. if (!gc->hook_stop) {
  654. gc->hook_stop = get_event_plus_id(EVENT_CAPTURE_STOP,
  655. gc->process_id);
  656. if (!gc->hook_stop) {
  657. warn("init_events: failed to get hook_stop event: %lu",
  658. GetLastError());
  659. return false;
  660. }
  661. }
  662. if (!gc->hook_ready) {
  663. gc->hook_ready = get_event_plus_id(EVENT_HOOK_READY,
  664. gc->process_id);
  665. if (!gc->hook_ready) {
  666. warn("init_events: failed to get hook_ready event: %lu",
  667. GetLastError());
  668. return false;
  669. }
  670. }
  671. if (!gc->hook_exit) {
  672. gc->hook_exit = get_event_plus_id(EVENT_HOOK_EXIT,
  673. gc->process_id);
  674. if (!gc->hook_exit) {
  675. warn("init_events: failed to get hook_exit event: %lu",
  676. GetLastError());
  677. return false;
  678. }
  679. }
  680. return true;
  681. }
  682. static inline bool init_capture_data(struct game_capture *gc)
  683. {
  684. char name[64];
  685. sprintf(name, "%s%u", SHMEM_TEXTURE, gc->global_hook_info->map_id);
  686. gc->cx = gc->global_hook_info->cx;
  687. gc->cy = gc->global_hook_info->cy;
  688. gc->pitch = gc->global_hook_info->pitch;
  689. if (gc->data) {
  690. UnmapViewOfFile(gc->data);
  691. gc->data = NULL;
  692. }
  693. CloseHandle(gc->hook_data_map);
  694. gc->hook_data_map = OpenFileMappingA(FILE_MAP_ALL_ACCESS, false, name);
  695. if (!gc->hook_data_map) {
  696. DWORD error = GetLastError();
  697. if (error == 2) {
  698. info("init_capture_data: file mapping not found, "
  699. "retrying. (this is often normal, and may take "
  700. "a few tries)");
  701. } else {
  702. warn("init_capture_data: failed to open file "
  703. "mapping: %lu", error);
  704. }
  705. return false;
  706. }
  707. gc->data = MapViewOfFile(gc->hook_data_map, FILE_MAP_ALL_ACCESS, 0, 0,
  708. gc->global_hook_info->map_size);
  709. if (!gc->data) {
  710. warn("init_capture_data: failed to map data view: %lu",
  711. GetLastError());
  712. return false;
  713. }
  714. return true;
  715. }
  716. #define PIXEL_16BIT_SIZE 2
  717. #define PIXEL_32BIT_SIZE 4
  718. static inline uint32_t convert_5_to_8bit(uint16_t val)
  719. {
  720. return (uint32_t)((double)(val & 0x1F) * (255.0/31.0));
  721. }
  722. static inline uint32_t convert_6_to_8bit(uint16_t val)
  723. {
  724. return (uint32_t)((double)(val & 0x3F) * (255.0/63.0));
  725. }
  726. static void copy_b5g6r5_tex(struct game_capture *gc, int cur_texture,
  727. uint8_t *data, uint32_t pitch)
  728. {
  729. uint8_t *input = gc->texture_buffers[cur_texture];
  730. uint32_t gc_cx = gc->cx;
  731. uint32_t gc_cy = gc->cy;
  732. uint32_t gc_pitch = gc->pitch;
  733. for (uint32_t y = 0; y < gc_cy; y++) {
  734. register uint8_t *in = input + (gc_pitch * y);
  735. register uint8_t *end = in + (gc_cx * PIXEL_16BIT_SIZE);
  736. register uint8_t *out = data + (pitch * y);
  737. while (in < end) {
  738. register uint16_t in_pix = *(uint16_t*)in;
  739. register uint32_t out_pix = 0xFF000000;
  740. out_pix |= convert_5_to_8bit(in_pix);
  741. in_pix >>= 5;
  742. out_pix |= convert_6_to_8bit(in_pix) << 8;
  743. in_pix >>= 6;
  744. out_pix |= convert_5_to_8bit(in_pix) << 16;
  745. *(uint32_t*)out = out_pix;
  746. in += PIXEL_16BIT_SIZE;
  747. out += PIXEL_32BIT_SIZE;
  748. }
  749. }
  750. }
  751. static void copy_b5g5r5a1_tex(struct game_capture *gc, int cur_texture,
  752. uint8_t *data, uint32_t pitch)
  753. {
  754. uint8_t *input = gc->texture_buffers[cur_texture];
  755. uint32_t gc_cx = gc->cx;
  756. uint32_t gc_cy = gc->cy;
  757. uint32_t gc_pitch = gc->pitch;
  758. for (uint32_t y = 0; y < gc_cy; y++) {
  759. register uint8_t *in = input + (gc_pitch * y);
  760. register uint8_t *end = in + (gc_cx * PIXEL_16BIT_SIZE);
  761. register uint8_t *out = data + (pitch * y);
  762. while (in < end) {
  763. register uint16_t in_pix = *(uint16_t*)in;
  764. register uint32_t out_pix = 0;
  765. out_pix |= convert_5_to_8bit(in_pix);
  766. in_pix >>= 5;
  767. out_pix |= convert_5_to_8bit(in_pix) << 8;
  768. in_pix >>= 5;
  769. out_pix |= convert_5_to_8bit(in_pix) << 16;
  770. in_pix >>= 5;
  771. out_pix |= (in_pix * 255) << 24;
  772. *(uint32_t*)out = out_pix;
  773. in += PIXEL_16BIT_SIZE;
  774. out += PIXEL_32BIT_SIZE;
  775. }
  776. }
  777. }
  778. static inline void copy_16bit_tex(struct game_capture *gc, int cur_texture,
  779. uint8_t *data, uint32_t pitch)
  780. {
  781. if (gc->global_hook_info->format == DXGI_FORMAT_B5G5R5A1_UNORM) {
  782. copy_b5g5r5a1_tex(gc, cur_texture, data, pitch);
  783. } else if (gc->global_hook_info->format == DXGI_FORMAT_B5G6R5_UNORM) {
  784. copy_b5g6r5_tex(gc, cur_texture, data, pitch);
  785. }
  786. }
  787. static void copy_shmem_tex(struct game_capture *gc)
  788. {
  789. int cur_texture = gc->shmem_data->last_tex;
  790. HANDLE mutex = NULL;
  791. uint32_t pitch;
  792. int next_texture;
  793. uint8_t *data;
  794. if (cur_texture < 0 || cur_texture > 1)
  795. return;
  796. next_texture = cur_texture == 1 ? 0 : 1;
  797. if (object_signalled(gc->texture_mutexes[cur_texture])) {
  798. mutex = gc->texture_mutexes[cur_texture];
  799. } else if (object_signalled(gc->texture_mutexes[next_texture])) {
  800. mutex = gc->texture_mutexes[next_texture];
  801. cur_texture = next_texture;
  802. } else {
  803. return;
  804. }
  805. if (gs_texture_map(gc->texture, &data, &pitch)) {
  806. if (gc->convert_16bit) {
  807. copy_16bit_tex(gc, cur_texture, data, pitch);
  808. } else if (pitch == gc->pitch) {
  809. memcpy(data, gc->texture_buffers[cur_texture],
  810. pitch * gc->cy);
  811. } else {
  812. uint8_t *input = gc->texture_buffers[cur_texture];
  813. uint32_t best_pitch =
  814. pitch < gc->pitch ? pitch : gc->pitch;
  815. for (uint32_t y = 0; y < gc->cy; y++) {
  816. uint8_t *line_in = input + gc->pitch * y;
  817. uint8_t *line_out = data + pitch * y;
  818. memcpy(line_out, line_in, best_pitch);
  819. }
  820. }
  821. gs_texture_unmap(gc->texture);
  822. }
  823. ReleaseMutex(mutex);
  824. }
  825. static inline bool is_16bit_format(uint32_t format)
  826. {
  827. return format == DXGI_FORMAT_B5G5R5A1_UNORM ||
  828. format == DXGI_FORMAT_B5G6R5_UNORM;
  829. }
  830. static inline bool init_shmem_capture(struct game_capture *gc)
  831. {
  832. enum gs_color_format format;
  833. gc->texture_buffers[0] =
  834. (uint8_t*)gc->data + gc->shmem_data->tex1_offset;
  835. gc->texture_buffers[1] =
  836. (uint8_t*)gc->data + gc->shmem_data->tex2_offset;
  837. gc->convert_16bit = is_16bit_format(gc->global_hook_info->format);
  838. format = gc->convert_16bit ?
  839. GS_BGRA : convert_format(gc->global_hook_info->format);
  840. obs_enter_graphics();
  841. gs_texture_destroy(gc->texture);
  842. gc->texture = gs_texture_create(gc->cx, gc->cy, format, 1, NULL,
  843. GS_DYNAMIC);
  844. obs_leave_graphics();
  845. if (!gc->texture) {
  846. warn("init_shmem_capture: failed to create texture");
  847. return false;
  848. }
  849. gc->copy_texture = copy_shmem_tex;
  850. return true;
  851. }
  852. static inline bool init_shtex_capture(struct game_capture *gc)
  853. {
  854. obs_enter_graphics();
  855. gs_texture_destroy(gc->texture);
  856. gc->texture = gs_texture_open_shared(gc->shtex_data->tex_handle);
  857. obs_leave_graphics();
  858. if (!gc->texture) {
  859. warn("init_shtex_capture: failed to open shared handle");
  860. return false;
  861. }
  862. return true;
  863. }
  864. static bool start_capture(struct game_capture *gc)
  865. {
  866. if (!init_events(gc)) {
  867. return false;
  868. }
  869. if (!init_capture_data(gc)) {
  870. return false;
  871. }
  872. if (gc->global_hook_info->type == CAPTURE_TYPE_MEMORY) {
  873. if (!init_shmem_capture(gc)) {
  874. return false;
  875. }
  876. } else {
  877. if (!init_shtex_capture(gc)) {
  878. return false;
  879. }
  880. }
  881. return true;
  882. }
  883. static inline bool capture_valid(struct game_capture *gc)
  884. {
  885. if (!gc->dwm_capture && !IsWindow(gc->window))
  886. return false;
  887. return !object_signalled(gc->target_process);
  888. }
  889. static void game_capture_tick(void *data, float seconds)
  890. {
  891. struct game_capture *gc = data;
  892. if (gc->hook_stop && object_signalled(gc->hook_stop)) {
  893. stop_capture(gc);
  894. }
  895. if (gc->active && !gc->hook_ready && gc->process_id) {
  896. gc->hook_ready = get_event_plus_id(EVENT_HOOK_READY,
  897. gc->process_id);
  898. }
  899. if (gc->injector_process && object_signalled(gc->injector_process)) {
  900. DWORD exit_code = 0;
  901. GetExitCodeProcess(gc->injector_process, &exit_code);
  902. close_handle(&gc->injector_process);
  903. if (exit_code != 0) {
  904. warn("inject process failed: %lu", exit_code);
  905. gc->error_acquiring = true;
  906. } else if (!gc->capturing) {
  907. gc->retry_interval = ERROR_RETRY_INTERVAL;
  908. stop_capture(gc);
  909. }
  910. }
  911. if (gc->hook_ready && object_signalled(gc->hook_ready)) {
  912. gc->capturing = start_capture(gc);
  913. if (!gc->capturing) {
  914. gc->retry_interval = ERROR_RETRY_INTERVAL;
  915. stop_capture(gc);
  916. }
  917. }
  918. gc->retry_time += seconds;
  919. if (!gc->active) {
  920. if (!gc->error_acquiring &&
  921. gc->retry_time > gc->retry_interval) {
  922. if (gc->config.capture_any_fullscreen ||
  923. gc->activate_hook) {
  924. try_hook(gc);
  925. gc->retry_time = 0.0f;
  926. }
  927. }
  928. } else {
  929. if (!capture_valid(gc)) {
  930. info("capture window no longer exists, "
  931. "terminating capture");
  932. stop_capture(gc);
  933. } else {
  934. if (gc->copy_texture) {
  935. obs_enter_graphics();
  936. gc->copy_texture(gc);
  937. obs_leave_graphics();
  938. }
  939. if (gc->config.cursor) {
  940. obs_enter_graphics();
  941. cursor_capture(&gc->cursor_data);
  942. obs_leave_graphics();
  943. }
  944. gc->fps_reset_time += seconds;
  945. if (gc->fps_reset_time >= gc->retry_interval) {
  946. reset_frame_interval(gc);
  947. gc->fps_reset_time = 0.0f;
  948. }
  949. }
  950. }
  951. }
  952. static inline void game_capture_render_cursor(struct game_capture *gc)
  953. {
  954. POINT p = {0};
  955. if (!gc->global_hook_info->window ||
  956. !gc->global_hook_info->base_cx ||
  957. !gc->global_hook_info->base_cy)
  958. return;
  959. ClientToScreen((HWND)(uintptr_t)gc->global_hook_info->window, &p);
  960. float x_scale = (float)gc->global_hook_info->cx /
  961. (float)gc->global_hook_info->base_cx;
  962. float y_scale = (float)gc->global_hook_info->cy /
  963. (float)gc->global_hook_info->base_cy;
  964. cursor_draw(&gc->cursor_data, -p.x, -p.y, x_scale, y_scale,
  965. gc->global_hook_info->base_cx,
  966. gc->global_hook_info->base_cy);
  967. }
  968. static void game_capture_render(void *data, gs_effect_t *effect)
  969. {
  970. struct game_capture *gc = data;
  971. if (!gc->texture)
  972. return;
  973. effect = gc->config.allow_transparency ?
  974. obs_get_default_effect() : obs_get_opaque_effect();
  975. while (gs_effect_loop(effect, "Draw")) {
  976. obs_source_draw(gc->texture, 0, 0, 0, 0,
  977. gc->global_hook_info->flip);
  978. if (gc->config.allow_transparency && gc->config.cursor) {
  979. game_capture_render_cursor(gc);
  980. }
  981. }
  982. if (!gc->config.allow_transparency && gc->config.cursor) {
  983. effect = obs_get_default_effect();
  984. while (gs_effect_loop(effect, "Draw")) {
  985. game_capture_render_cursor(gc);
  986. }
  987. }
  988. }
  989. static uint32_t game_capture_width(void *data)
  990. {
  991. struct game_capture *gc = data;
  992. return gc->active ? gc->global_hook_info->cx : 0;
  993. }
  994. static uint32_t game_capture_height(void *data)
  995. {
  996. struct game_capture *gc = data;
  997. return gc->active ? gc->global_hook_info->cy : 0;
  998. }
  999. static const char *game_capture_name(void)
  1000. {
  1001. return TEXT_GAME_CAPTURE;
  1002. }
  1003. static void game_capture_defaults(obs_data_t *settings)
  1004. {
  1005. obs_data_set_default_bool(settings, SETTING_ANY_FULLSCREEN, true);
  1006. obs_data_set_default_int(settings, SETTING_WINDOW_PRIORITY,
  1007. (int)WINDOW_PRIORITY_EXE);
  1008. obs_data_set_default_bool(settings, SETTING_COMPATIBILITY, false);
  1009. obs_data_set_default_bool(settings, SETTING_FORCE_SCALING, false);
  1010. obs_data_set_default_bool(settings, SETTING_CURSOR, true);
  1011. obs_data_set_default_bool(settings, SETTING_TRANSPARENCY, false);
  1012. obs_data_set_default_string(settings, SETTING_SCALE_RES, "0x0");
  1013. obs_data_set_default_bool(settings, SETTING_LIMIT_FRAMERATE, false);
  1014. obs_data_set_default_bool(settings, SETTING_CAPTURE_OVERLAYS, false);
  1015. }
  1016. static bool any_fullscreen_callback(obs_properties_t *ppts,
  1017. obs_property_t *p, obs_data_t *settings)
  1018. {
  1019. bool any_fullscreen = obs_data_get_bool(settings,
  1020. SETTING_ANY_FULLSCREEN);
  1021. p = obs_properties_get(ppts, SETTING_CAPTURE_WINDOW);
  1022. obs_property_set_enabled(p, !any_fullscreen);
  1023. p = obs_properties_get(ppts, SETTING_WINDOW_PRIORITY);
  1024. obs_property_set_enabled(p, !any_fullscreen);
  1025. return true;
  1026. }
  1027. static bool use_scaling_callback(obs_properties_t *ppts, obs_property_t *p,
  1028. obs_data_t *settings)
  1029. {
  1030. bool use_scale = obs_data_get_bool(settings, SETTING_FORCE_SCALING);
  1031. p = obs_properties_get(ppts, SETTING_SCALE_RES);
  1032. obs_property_set_enabled(p, use_scale);
  1033. return true;
  1034. }
  1035. static void insert_preserved_val(obs_property_t *p, const char *val)
  1036. {
  1037. char *class = NULL;
  1038. char *title = NULL;
  1039. char *executable = NULL;
  1040. struct dstr desc = {0};
  1041. build_window_strings(val, &class, &title, &executable);
  1042. dstr_printf(&desc, "[%s]: %s", executable, title);
  1043. obs_property_list_insert_string(p, 1, desc.array, val);
  1044. obs_property_list_item_disable(p, 1, true);
  1045. dstr_free(&desc);
  1046. bfree(class);
  1047. bfree(title);
  1048. bfree(executable);
  1049. }
  1050. static bool window_changed_callback(obs_properties_t *ppts, obs_property_t *p,
  1051. obs_data_t *settings)
  1052. {
  1053. const char *cur_val;
  1054. bool match = false;
  1055. size_t i = 0;
  1056. cur_val = obs_data_get_string(settings, SETTING_CAPTURE_WINDOW);
  1057. if (!cur_val) {
  1058. return false;
  1059. }
  1060. for (;;) {
  1061. const char *val = obs_property_list_item_string(p, i++);
  1062. if (!val)
  1063. break;
  1064. if (strcmp(val, cur_val) == 0) {
  1065. match = true;
  1066. break;
  1067. }
  1068. }
  1069. if (cur_val && *cur_val && !match) {
  1070. insert_preserved_val(p, cur_val);
  1071. return true;
  1072. }
  1073. UNUSED_PARAMETER(ppts);
  1074. return false;
  1075. }
  1076. static const double default_scale_vals[] = {
  1077. 1.25,
  1078. 1.5,
  1079. 2.0,
  1080. 2.5,
  1081. 3.0
  1082. };
  1083. #define NUM_DEFAULT_SCALE_VALS \
  1084. (sizeof(default_scale_vals) / sizeof(default_scale_vals[0]))
  1085. static BOOL CALLBACK EnumFirstMonitor(HMONITOR monitor, HDC hdc,
  1086. LPRECT rc, LPARAM data)
  1087. {
  1088. *(HMONITOR*)data = monitor;
  1089. UNUSED_PARAMETER(hdc);
  1090. UNUSED_PARAMETER(rc);
  1091. return false;
  1092. }
  1093. static obs_properties_t *game_capture_properties(void *data)
  1094. {
  1095. HMONITOR monitor;
  1096. uint32_t cx = 1920;
  1097. uint32_t cy = 1080;
  1098. /* scaling is free form, this is mostly just to provide some common
  1099. * values */
  1100. bool success = !!EnumDisplayMonitors(NULL, NULL, EnumFirstMonitor,
  1101. (LPARAM)&monitor);
  1102. if (success) {
  1103. MONITORINFO mi = {0};
  1104. mi.cbSize = sizeof(mi);
  1105. if (!!GetMonitorInfo(monitor, &mi)) {
  1106. cx = (uint32_t)(mi.rcMonitor.right - mi.rcMonitor.left);
  1107. cy = (uint32_t)(mi.rcMonitor.bottom - mi.rcMonitor.top);
  1108. }
  1109. }
  1110. obs_properties_t *ppts = obs_properties_create();
  1111. obs_property_t *p;
  1112. p = obs_properties_add_bool(ppts, SETTING_ANY_FULLSCREEN,
  1113. TEXT_ANY_FULLSCREEN);
  1114. obs_property_set_modified_callback(p, any_fullscreen_callback);
  1115. p = obs_properties_add_list(ppts, SETTING_CAPTURE_WINDOW, TEXT_WINDOW,
  1116. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
  1117. obs_property_list_add_string(p, "", "");
  1118. fill_window_list(p, INCLUDE_MINIMIZED);
  1119. obs_property_set_modified_callback(p, window_changed_callback);
  1120. p = obs_properties_add_list(ppts, SETTING_WINDOW_PRIORITY,
  1121. TEXT_MATCH_PRIORITY, OBS_COMBO_TYPE_LIST,
  1122. OBS_COMBO_FORMAT_INT);
  1123. obs_property_list_add_int(p, TEXT_MATCH_TITLE, WINDOW_PRIORITY_TITLE);
  1124. obs_property_list_add_int(p, TEXT_MATCH_CLASS, WINDOW_PRIORITY_CLASS);
  1125. obs_property_list_add_int(p, TEXT_MATCH_EXE, WINDOW_PRIORITY_EXE);
  1126. obs_properties_add_bool(ppts, SETTING_COMPATIBILITY,
  1127. TEXT_SLI_COMPATIBILITY);
  1128. p = obs_properties_add_bool(ppts, SETTING_FORCE_SCALING,
  1129. TEXT_FORCE_SCALING);
  1130. obs_property_set_modified_callback(p, use_scaling_callback);
  1131. p = obs_properties_add_list(ppts, SETTING_SCALE_RES, TEXT_SCALE_RES,
  1132. OBS_COMBO_TYPE_EDITABLE, OBS_COMBO_FORMAT_STRING);
  1133. for (size_t i = 0; i < NUM_DEFAULT_SCALE_VALS; i++) {
  1134. char scale_str[64];
  1135. uint32_t new_cx =
  1136. (uint32_t)((double)cx / default_scale_vals[i]) & ~2;
  1137. uint32_t new_cy =
  1138. (uint32_t)((double)cy / default_scale_vals[i]) & ~2;
  1139. sprintf(scale_str, "%"PRIu32"x%"PRIu32, new_cx, new_cy);
  1140. obs_property_list_add_string(p, scale_str, scale_str);
  1141. }
  1142. obs_property_set_enabled(p, false);
  1143. obs_properties_add_bool(ppts, SETTING_TRANSPARENCY,
  1144. TEXT_ALLOW_TRANSPARENCY);
  1145. obs_properties_add_bool(ppts, SETTING_LIMIT_FRAMERATE,
  1146. TEXT_LIMIT_FRAMERATE);
  1147. obs_properties_add_bool(ppts, SETTING_CAPTURE_OVERLAYS,
  1148. TEXT_CAPTURE_OVERLAYS);
  1149. obs_properties_add_bool(ppts, SETTING_CURSOR, TEXT_CAPTURE_CURSOR);
  1150. UNUSED_PARAMETER(data);
  1151. return ppts;
  1152. }
  1153. struct obs_source_info game_capture_info = {
  1154. .id = "game_capture",
  1155. .type = OBS_SOURCE_TYPE_INPUT,
  1156. .output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_CUSTOM_DRAW,
  1157. .get_name = game_capture_name,
  1158. .create = game_capture_create,
  1159. .destroy = game_capture_destroy,
  1160. .get_width = game_capture_width,
  1161. .get_height = game_capture_height,
  1162. .get_defaults = game_capture_defaults,
  1163. .get_properties = game_capture_properties,
  1164. .update = game_capture_update,
  1165. .video_tick = game_capture_tick,
  1166. .video_render = game_capture_render
  1167. };