game-capture.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410
  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. enum capture_result {
  683. CAPTURE_FAIL,
  684. CAPTURE_RETRY,
  685. CAPTURE_SUCCESS
  686. };
  687. static inline enum capture_result init_capture_data(struct game_capture *gc)
  688. {
  689. char name[64];
  690. sprintf(name, "%s%u", SHMEM_TEXTURE, gc->global_hook_info->map_id);
  691. gc->cx = gc->global_hook_info->cx;
  692. gc->cy = gc->global_hook_info->cy;
  693. gc->pitch = gc->global_hook_info->pitch;
  694. if (gc->data) {
  695. UnmapViewOfFile(gc->data);
  696. gc->data = NULL;
  697. }
  698. CloseHandle(gc->hook_data_map);
  699. gc->hook_data_map = OpenFileMappingA(FILE_MAP_ALL_ACCESS, false, name);
  700. if (!gc->hook_data_map) {
  701. DWORD error = GetLastError();
  702. if (error == 2) {
  703. return CAPTURE_RETRY;
  704. } else {
  705. warn("init_capture_data: failed to open file "
  706. "mapping: %lu", error);
  707. }
  708. return CAPTURE_FAIL;
  709. }
  710. gc->data = MapViewOfFile(gc->hook_data_map, FILE_MAP_ALL_ACCESS, 0, 0,
  711. gc->global_hook_info->map_size);
  712. if (!gc->data) {
  713. warn("init_capture_data: failed to map data view: %lu",
  714. GetLastError());
  715. return CAPTURE_FAIL;
  716. }
  717. return CAPTURE_SUCCESS;
  718. }
  719. #define PIXEL_16BIT_SIZE 2
  720. #define PIXEL_32BIT_SIZE 4
  721. static inline uint32_t convert_5_to_8bit(uint16_t val)
  722. {
  723. return (uint32_t)((double)(val & 0x1F) * (255.0/31.0));
  724. }
  725. static inline uint32_t convert_6_to_8bit(uint16_t val)
  726. {
  727. return (uint32_t)((double)(val & 0x3F) * (255.0/63.0));
  728. }
  729. static void copy_b5g6r5_tex(struct game_capture *gc, int cur_texture,
  730. uint8_t *data, uint32_t pitch)
  731. {
  732. uint8_t *input = gc->texture_buffers[cur_texture];
  733. uint32_t gc_cx = gc->cx;
  734. uint32_t gc_cy = gc->cy;
  735. uint32_t gc_pitch = gc->pitch;
  736. for (uint32_t y = 0; y < gc_cy; y++) {
  737. register uint8_t *in = input + (gc_pitch * y);
  738. register uint8_t *end = in + (gc_cx * PIXEL_16BIT_SIZE);
  739. register uint8_t *out = data + (pitch * y);
  740. while (in < end) {
  741. register uint16_t in_pix = *(uint16_t*)in;
  742. register uint32_t out_pix = 0xFF000000;
  743. out_pix |= convert_5_to_8bit(in_pix);
  744. in_pix >>= 5;
  745. out_pix |= convert_6_to_8bit(in_pix) << 8;
  746. in_pix >>= 6;
  747. out_pix |= convert_5_to_8bit(in_pix) << 16;
  748. *(uint32_t*)out = out_pix;
  749. in += PIXEL_16BIT_SIZE;
  750. out += PIXEL_32BIT_SIZE;
  751. }
  752. }
  753. }
  754. static void copy_b5g5r5a1_tex(struct game_capture *gc, int cur_texture,
  755. uint8_t *data, uint32_t pitch)
  756. {
  757. uint8_t *input = gc->texture_buffers[cur_texture];
  758. uint32_t gc_cx = gc->cx;
  759. uint32_t gc_cy = gc->cy;
  760. uint32_t gc_pitch = gc->pitch;
  761. for (uint32_t y = 0; y < gc_cy; y++) {
  762. register uint8_t *in = input + (gc_pitch * y);
  763. register uint8_t *end = in + (gc_cx * PIXEL_16BIT_SIZE);
  764. register uint8_t *out = data + (pitch * y);
  765. while (in < end) {
  766. register uint16_t in_pix = *(uint16_t*)in;
  767. register uint32_t out_pix = 0;
  768. out_pix |= convert_5_to_8bit(in_pix);
  769. in_pix >>= 5;
  770. out_pix |= convert_5_to_8bit(in_pix) << 8;
  771. in_pix >>= 5;
  772. out_pix |= convert_5_to_8bit(in_pix) << 16;
  773. in_pix >>= 5;
  774. out_pix |= (in_pix * 255) << 24;
  775. *(uint32_t*)out = out_pix;
  776. in += PIXEL_16BIT_SIZE;
  777. out += PIXEL_32BIT_SIZE;
  778. }
  779. }
  780. }
  781. static inline void copy_16bit_tex(struct game_capture *gc, int cur_texture,
  782. uint8_t *data, uint32_t pitch)
  783. {
  784. if (gc->global_hook_info->format == DXGI_FORMAT_B5G5R5A1_UNORM) {
  785. copy_b5g5r5a1_tex(gc, cur_texture, data, pitch);
  786. } else if (gc->global_hook_info->format == DXGI_FORMAT_B5G6R5_UNORM) {
  787. copy_b5g6r5_tex(gc, cur_texture, data, pitch);
  788. }
  789. }
  790. static void copy_shmem_tex(struct game_capture *gc)
  791. {
  792. int cur_texture = gc->shmem_data->last_tex;
  793. HANDLE mutex = NULL;
  794. uint32_t pitch;
  795. int next_texture;
  796. uint8_t *data;
  797. if (cur_texture < 0 || cur_texture > 1)
  798. return;
  799. next_texture = cur_texture == 1 ? 0 : 1;
  800. if (object_signalled(gc->texture_mutexes[cur_texture])) {
  801. mutex = gc->texture_mutexes[cur_texture];
  802. } else if (object_signalled(gc->texture_mutexes[next_texture])) {
  803. mutex = gc->texture_mutexes[next_texture];
  804. cur_texture = next_texture;
  805. } else {
  806. return;
  807. }
  808. if (gs_texture_map(gc->texture, &data, &pitch)) {
  809. if (gc->convert_16bit) {
  810. copy_16bit_tex(gc, cur_texture, data, pitch);
  811. } else if (pitch == gc->pitch) {
  812. memcpy(data, gc->texture_buffers[cur_texture],
  813. pitch * gc->cy);
  814. } else {
  815. uint8_t *input = gc->texture_buffers[cur_texture];
  816. uint32_t best_pitch =
  817. pitch < gc->pitch ? pitch : gc->pitch;
  818. for (uint32_t y = 0; y < gc->cy; y++) {
  819. uint8_t *line_in = input + gc->pitch * y;
  820. uint8_t *line_out = data + pitch * y;
  821. memcpy(line_out, line_in, best_pitch);
  822. }
  823. }
  824. gs_texture_unmap(gc->texture);
  825. }
  826. ReleaseMutex(mutex);
  827. }
  828. static inline bool is_16bit_format(uint32_t format)
  829. {
  830. return format == DXGI_FORMAT_B5G5R5A1_UNORM ||
  831. format == DXGI_FORMAT_B5G6R5_UNORM;
  832. }
  833. static inline bool init_shmem_capture(struct game_capture *gc)
  834. {
  835. enum gs_color_format format;
  836. gc->texture_buffers[0] =
  837. (uint8_t*)gc->data + gc->shmem_data->tex1_offset;
  838. gc->texture_buffers[1] =
  839. (uint8_t*)gc->data + gc->shmem_data->tex2_offset;
  840. gc->convert_16bit = is_16bit_format(gc->global_hook_info->format);
  841. format = gc->convert_16bit ?
  842. GS_BGRA : convert_format(gc->global_hook_info->format);
  843. obs_enter_graphics();
  844. gs_texture_destroy(gc->texture);
  845. gc->texture = gs_texture_create(gc->cx, gc->cy, format, 1, NULL,
  846. GS_DYNAMIC);
  847. obs_leave_graphics();
  848. if (!gc->texture) {
  849. warn("init_shmem_capture: failed to create texture");
  850. return false;
  851. }
  852. gc->copy_texture = copy_shmem_tex;
  853. return true;
  854. }
  855. static inline bool init_shtex_capture(struct game_capture *gc)
  856. {
  857. obs_enter_graphics();
  858. gs_texture_destroy(gc->texture);
  859. gc->texture = gs_texture_open_shared(gc->shtex_data->tex_handle);
  860. obs_leave_graphics();
  861. if (!gc->texture) {
  862. warn("init_shtex_capture: failed to open shared handle");
  863. return false;
  864. }
  865. return true;
  866. }
  867. static bool start_capture(struct game_capture *gc)
  868. {
  869. if (!init_events(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. enum capture_result result = init_capture_data(gc);
  913. if (result == CAPTURE_SUCCESS)
  914. gc->capturing = start_capture(gc);
  915. if (result != CAPTURE_RETRY && !gc->capturing) {
  916. gc->retry_interval = ERROR_RETRY_INTERVAL;
  917. stop_capture(gc);
  918. }
  919. }
  920. gc->retry_time += seconds;
  921. if (!gc->active) {
  922. if (!gc->error_acquiring &&
  923. gc->retry_time > gc->retry_interval) {
  924. if (gc->config.capture_any_fullscreen ||
  925. gc->activate_hook) {
  926. try_hook(gc);
  927. gc->retry_time = 0.0f;
  928. }
  929. }
  930. } else {
  931. if (!capture_valid(gc)) {
  932. info("capture window no longer exists, "
  933. "terminating capture");
  934. stop_capture(gc);
  935. } else {
  936. if (gc->copy_texture) {
  937. obs_enter_graphics();
  938. gc->copy_texture(gc);
  939. obs_leave_graphics();
  940. }
  941. if (gc->config.cursor) {
  942. obs_enter_graphics();
  943. cursor_capture(&gc->cursor_data);
  944. obs_leave_graphics();
  945. }
  946. gc->fps_reset_time += seconds;
  947. if (gc->fps_reset_time >= gc->retry_interval) {
  948. reset_frame_interval(gc);
  949. gc->fps_reset_time = 0.0f;
  950. }
  951. }
  952. }
  953. }
  954. static inline void game_capture_render_cursor(struct game_capture *gc)
  955. {
  956. POINT p = {0};
  957. if (!gc->global_hook_info->window ||
  958. !gc->global_hook_info->base_cx ||
  959. !gc->global_hook_info->base_cy)
  960. return;
  961. ClientToScreen((HWND)(uintptr_t)gc->global_hook_info->window, &p);
  962. float x_scale = (float)gc->global_hook_info->cx /
  963. (float)gc->global_hook_info->base_cx;
  964. float y_scale = (float)gc->global_hook_info->cy /
  965. (float)gc->global_hook_info->base_cy;
  966. cursor_draw(&gc->cursor_data, -p.x, -p.y, x_scale, y_scale,
  967. gc->global_hook_info->base_cx,
  968. gc->global_hook_info->base_cy);
  969. }
  970. static void game_capture_render(void *data, gs_effect_t *effect)
  971. {
  972. struct game_capture *gc = data;
  973. if (!gc->texture)
  974. return;
  975. effect = gc->config.allow_transparency ?
  976. obs_get_default_effect() : obs_get_opaque_effect();
  977. while (gs_effect_loop(effect, "Draw")) {
  978. obs_source_draw(gc->texture, 0, 0, 0, 0,
  979. gc->global_hook_info->flip);
  980. if (gc->config.allow_transparency && gc->config.cursor) {
  981. game_capture_render_cursor(gc);
  982. }
  983. }
  984. if (!gc->config.allow_transparency && gc->config.cursor) {
  985. effect = obs_get_default_effect();
  986. while (gs_effect_loop(effect, "Draw")) {
  987. game_capture_render_cursor(gc);
  988. }
  989. }
  990. }
  991. static uint32_t game_capture_width(void *data)
  992. {
  993. struct game_capture *gc = data;
  994. return gc->active ? gc->global_hook_info->cx : 0;
  995. }
  996. static uint32_t game_capture_height(void *data)
  997. {
  998. struct game_capture *gc = data;
  999. return gc->active ? gc->global_hook_info->cy : 0;
  1000. }
  1001. static const char *game_capture_name(void)
  1002. {
  1003. return TEXT_GAME_CAPTURE;
  1004. }
  1005. static void game_capture_defaults(obs_data_t *settings)
  1006. {
  1007. obs_data_set_default_bool(settings, SETTING_ANY_FULLSCREEN, true);
  1008. obs_data_set_default_int(settings, SETTING_WINDOW_PRIORITY,
  1009. (int)WINDOW_PRIORITY_EXE);
  1010. obs_data_set_default_bool(settings, SETTING_COMPATIBILITY, false);
  1011. obs_data_set_default_bool(settings, SETTING_FORCE_SCALING, false);
  1012. obs_data_set_default_bool(settings, SETTING_CURSOR, true);
  1013. obs_data_set_default_bool(settings, SETTING_TRANSPARENCY, false);
  1014. obs_data_set_default_string(settings, SETTING_SCALE_RES, "0x0");
  1015. obs_data_set_default_bool(settings, SETTING_LIMIT_FRAMERATE, false);
  1016. obs_data_set_default_bool(settings, SETTING_CAPTURE_OVERLAYS, false);
  1017. }
  1018. static bool any_fullscreen_callback(obs_properties_t *ppts,
  1019. obs_property_t *p, obs_data_t *settings)
  1020. {
  1021. bool any_fullscreen = obs_data_get_bool(settings,
  1022. SETTING_ANY_FULLSCREEN);
  1023. p = obs_properties_get(ppts, SETTING_CAPTURE_WINDOW);
  1024. obs_property_set_enabled(p, !any_fullscreen);
  1025. p = obs_properties_get(ppts, SETTING_WINDOW_PRIORITY);
  1026. obs_property_set_enabled(p, !any_fullscreen);
  1027. return true;
  1028. }
  1029. static bool use_scaling_callback(obs_properties_t *ppts, obs_property_t *p,
  1030. obs_data_t *settings)
  1031. {
  1032. bool use_scale = obs_data_get_bool(settings, SETTING_FORCE_SCALING);
  1033. p = obs_properties_get(ppts, SETTING_SCALE_RES);
  1034. obs_property_set_enabled(p, use_scale);
  1035. return true;
  1036. }
  1037. static void insert_preserved_val(obs_property_t *p, const char *val)
  1038. {
  1039. char *class = NULL;
  1040. char *title = NULL;
  1041. char *executable = NULL;
  1042. struct dstr desc = {0};
  1043. build_window_strings(val, &class, &title, &executable);
  1044. dstr_printf(&desc, "[%s]: %s", executable, title);
  1045. obs_property_list_insert_string(p, 1, desc.array, val);
  1046. obs_property_list_item_disable(p, 1, true);
  1047. dstr_free(&desc);
  1048. bfree(class);
  1049. bfree(title);
  1050. bfree(executable);
  1051. }
  1052. static bool window_changed_callback(obs_properties_t *ppts, obs_property_t *p,
  1053. obs_data_t *settings)
  1054. {
  1055. const char *cur_val;
  1056. bool match = false;
  1057. size_t i = 0;
  1058. cur_val = obs_data_get_string(settings, SETTING_CAPTURE_WINDOW);
  1059. if (!cur_val) {
  1060. return false;
  1061. }
  1062. for (;;) {
  1063. const char *val = obs_property_list_item_string(p, i++);
  1064. if (!val)
  1065. break;
  1066. if (strcmp(val, cur_val) == 0) {
  1067. match = true;
  1068. break;
  1069. }
  1070. }
  1071. if (cur_val && *cur_val && !match) {
  1072. insert_preserved_val(p, cur_val);
  1073. return true;
  1074. }
  1075. UNUSED_PARAMETER(ppts);
  1076. return false;
  1077. }
  1078. static const double default_scale_vals[] = {
  1079. 1.25,
  1080. 1.5,
  1081. 2.0,
  1082. 2.5,
  1083. 3.0
  1084. };
  1085. #define NUM_DEFAULT_SCALE_VALS \
  1086. (sizeof(default_scale_vals) / sizeof(default_scale_vals[0]))
  1087. static BOOL CALLBACK EnumFirstMonitor(HMONITOR monitor, HDC hdc,
  1088. LPRECT rc, LPARAM data)
  1089. {
  1090. *(HMONITOR*)data = monitor;
  1091. UNUSED_PARAMETER(hdc);
  1092. UNUSED_PARAMETER(rc);
  1093. return false;
  1094. }
  1095. static obs_properties_t *game_capture_properties(void *data)
  1096. {
  1097. HMONITOR monitor;
  1098. uint32_t cx = 1920;
  1099. uint32_t cy = 1080;
  1100. /* scaling is free form, this is mostly just to provide some common
  1101. * values */
  1102. bool success = !!EnumDisplayMonitors(NULL, NULL, EnumFirstMonitor,
  1103. (LPARAM)&monitor);
  1104. if (success) {
  1105. MONITORINFO mi = {0};
  1106. mi.cbSize = sizeof(mi);
  1107. if (!!GetMonitorInfo(monitor, &mi)) {
  1108. cx = (uint32_t)(mi.rcMonitor.right - mi.rcMonitor.left);
  1109. cy = (uint32_t)(mi.rcMonitor.bottom - mi.rcMonitor.top);
  1110. }
  1111. }
  1112. obs_properties_t *ppts = obs_properties_create();
  1113. obs_property_t *p;
  1114. p = obs_properties_add_bool(ppts, SETTING_ANY_FULLSCREEN,
  1115. TEXT_ANY_FULLSCREEN);
  1116. obs_property_set_modified_callback(p, any_fullscreen_callback);
  1117. p = obs_properties_add_list(ppts, SETTING_CAPTURE_WINDOW, TEXT_WINDOW,
  1118. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
  1119. obs_property_list_add_string(p, "", "");
  1120. fill_window_list(p, INCLUDE_MINIMIZED);
  1121. obs_property_set_modified_callback(p, window_changed_callback);
  1122. p = obs_properties_add_list(ppts, SETTING_WINDOW_PRIORITY,
  1123. TEXT_MATCH_PRIORITY, OBS_COMBO_TYPE_LIST,
  1124. OBS_COMBO_FORMAT_INT);
  1125. obs_property_list_add_int(p, TEXT_MATCH_TITLE, WINDOW_PRIORITY_TITLE);
  1126. obs_property_list_add_int(p, TEXT_MATCH_CLASS, WINDOW_PRIORITY_CLASS);
  1127. obs_property_list_add_int(p, TEXT_MATCH_EXE, WINDOW_PRIORITY_EXE);
  1128. obs_properties_add_bool(ppts, SETTING_COMPATIBILITY,
  1129. TEXT_SLI_COMPATIBILITY);
  1130. p = obs_properties_add_bool(ppts, SETTING_FORCE_SCALING,
  1131. TEXT_FORCE_SCALING);
  1132. obs_property_set_modified_callback(p, use_scaling_callback);
  1133. p = obs_properties_add_list(ppts, SETTING_SCALE_RES, TEXT_SCALE_RES,
  1134. OBS_COMBO_TYPE_EDITABLE, OBS_COMBO_FORMAT_STRING);
  1135. for (size_t i = 0; i < NUM_DEFAULT_SCALE_VALS; i++) {
  1136. char scale_str[64];
  1137. uint32_t new_cx =
  1138. (uint32_t)((double)cx / default_scale_vals[i]) & ~2;
  1139. uint32_t new_cy =
  1140. (uint32_t)((double)cy / default_scale_vals[i]) & ~2;
  1141. sprintf(scale_str, "%"PRIu32"x%"PRIu32, new_cx, new_cy);
  1142. obs_property_list_add_string(p, scale_str, scale_str);
  1143. }
  1144. obs_property_set_enabled(p, false);
  1145. obs_properties_add_bool(ppts, SETTING_TRANSPARENCY,
  1146. TEXT_ALLOW_TRANSPARENCY);
  1147. obs_properties_add_bool(ppts, SETTING_LIMIT_FRAMERATE,
  1148. TEXT_LIMIT_FRAMERATE);
  1149. obs_properties_add_bool(ppts, SETTING_CAPTURE_OVERLAYS,
  1150. TEXT_CAPTURE_OVERLAYS);
  1151. obs_properties_add_bool(ppts, SETTING_CURSOR, TEXT_CAPTURE_CURSOR);
  1152. UNUSED_PARAMETER(data);
  1153. return ppts;
  1154. }
  1155. struct obs_source_info game_capture_info = {
  1156. .id = "game_capture",
  1157. .type = OBS_SOURCE_TYPE_INPUT,
  1158. .output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_CUSTOM_DRAW,
  1159. .get_name = game_capture_name,
  1160. .create = game_capture_create,
  1161. .destroy = game_capture_destroy,
  1162. .get_width = game_capture_width,
  1163. .get_height = game_capture_height,
  1164. .get_defaults = game_capture_defaults,
  1165. .get_properties = game_capture_properties,
  1166. .update = game_capture_update,
  1167. .video_tick = game_capture_tick,
  1168. .video_render = game_capture_render
  1169. };