game-capture.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071
  1. #include <inttypes.h>
  2. #include <obs-module.h>
  3. #include <obs-hotkey.h>
  4. #include <util/platform.h>
  5. #include <util/threading.h>
  6. #include <windows.h>
  7. #include <dxgi.h>
  8. #include <util/sse-intrin.h>
  9. #include <util/util_uint64.h>
  10. #include <ipc-util/pipe.h>
  11. #include "obfuscate.h"
  12. #include "inject-library.h"
  13. #include "graphics-hook-info.h"
  14. #include "graphics-hook-ver.h"
  15. #include "window-helpers.h"
  16. #include "cursor-capture.h"
  17. #include "app-helpers.h"
  18. #include "nt-stuff.h"
  19. #define do_log(level, format, ...) \
  20. blog(level, "[game-capture: '%s'] " format, \
  21. obs_source_get_name(gc->source), ##__VA_ARGS__)
  22. #define warn(format, ...) do_log(LOG_WARNING, format, ##__VA_ARGS__)
  23. #define info(format, ...) do_log(LOG_INFO, format, ##__VA_ARGS__)
  24. #define debug(format, ...) do_log(LOG_DEBUG, format, ##__VA_ARGS__)
  25. /* clang-format off */
  26. #define SETTING_MODE "capture_mode"
  27. #define SETTING_CAPTURE_WINDOW "window"
  28. #define SETTING_ACTIVE_WINDOW "active_window"
  29. #define SETTING_WINDOW_PRIORITY "priority"
  30. #define SETTING_COMPATIBILITY "sli_compatibility"
  31. #define SETTING_CURSOR "capture_cursor"
  32. #define SETTING_TRANSPARENCY "allow_transparency"
  33. #define SETTING_LIMIT_FRAMERATE "limit_framerate"
  34. #define SETTING_CAPTURE_OVERLAYS "capture_overlays"
  35. #define SETTING_ANTI_CHEAT_HOOK "anti_cheat_hook"
  36. #define SETTING_HOOK_RATE "hook_rate"
  37. /* deprecated */
  38. #define SETTING_ANY_FULLSCREEN "capture_any_fullscreen"
  39. #define SETTING_MODE_ANY "any_fullscreen"
  40. #define SETTING_MODE_WINDOW "window"
  41. #define SETTING_MODE_HOTKEY "hotkey"
  42. #define HOTKEY_START "hotkey_start"
  43. #define HOTKEY_STOP "hotkey_stop"
  44. #define TEXT_MODE obs_module_text("Mode")
  45. #define TEXT_GAME_CAPTURE obs_module_text("GameCapture")
  46. #define TEXT_ANY_FULLSCREEN obs_module_text("GameCapture.AnyFullscreen")
  47. #define TEXT_SLI_COMPATIBILITY obs_module_text("SLIFix")
  48. #define TEXT_ALLOW_TRANSPARENCY obs_module_text("AllowTransparency")
  49. #define TEXT_WINDOW obs_module_text("WindowCapture.Window")
  50. #define TEXT_MATCH_PRIORITY obs_module_text("WindowCapture.Priority")
  51. #define TEXT_MATCH_TITLE obs_module_text("WindowCapture.Priority.Title")
  52. #define TEXT_MATCH_CLASS obs_module_text("WindowCapture.Priority.Class")
  53. #define TEXT_MATCH_EXE obs_module_text("WindowCapture.Priority.Exe")
  54. #define TEXT_CAPTURE_CURSOR obs_module_text("CaptureCursor")
  55. #define TEXT_LIMIT_FRAMERATE obs_module_text("GameCapture.LimitFramerate")
  56. #define TEXT_CAPTURE_OVERLAYS obs_module_text("GameCapture.CaptureOverlays")
  57. #define TEXT_ANTI_CHEAT_HOOK obs_module_text("GameCapture.AntiCheatHook")
  58. #define TEXT_HOOK_RATE obs_module_text("GameCapture.HookRate")
  59. #define TEXT_HOOK_RATE_SLOW obs_module_text("GameCapture.HookRate.Slow")
  60. #define TEXT_HOOK_RATE_NORMAL obs_module_text("GameCapture.HookRate.Normal")
  61. #define TEXT_HOOK_RATE_FAST obs_module_text("GameCapture.HookRate.Fast")
  62. #define TEXT_HOOK_RATE_FASTEST obs_module_text("GameCapture.HookRate.Fastest")
  63. #define TEXT_MODE_ANY TEXT_ANY_FULLSCREEN
  64. #define TEXT_MODE_WINDOW obs_module_text("GameCapture.CaptureWindow")
  65. #define TEXT_MODE_HOTKEY obs_module_text("GameCapture.UseHotkey")
  66. #define TEXT_HOTKEY_START obs_module_text("GameCapture.HotkeyStart")
  67. #define TEXT_HOTKEY_STOP obs_module_text("GameCapture.HotkeyStop")
  68. /* clang-format on */
  69. #define DEFAULT_RETRY_INTERVAL 2.0f
  70. #define ERROR_RETRY_INTERVAL 4.0f
  71. enum capture_mode {
  72. CAPTURE_MODE_ANY,
  73. CAPTURE_MODE_WINDOW,
  74. CAPTURE_MODE_HOTKEY
  75. };
  76. enum hook_rate {
  77. HOOK_RATE_SLOW,
  78. HOOK_RATE_NORMAL,
  79. HOOK_RATE_FAST,
  80. HOOK_RATE_FASTEST
  81. };
  82. struct game_capture_config {
  83. char *title;
  84. char *class;
  85. char *executable;
  86. enum window_priority priority;
  87. enum capture_mode mode;
  88. bool cursor;
  89. bool force_shmem;
  90. bool allow_transparency;
  91. bool limit_framerate;
  92. bool capture_overlays;
  93. bool anticheat_hook;
  94. enum hook_rate hook_rate;
  95. };
  96. struct game_capture {
  97. obs_source_t *source;
  98. struct cursor_data cursor_data;
  99. HANDLE injector_process;
  100. uint32_t cx;
  101. uint32_t cy;
  102. uint32_t pitch;
  103. DWORD process_id;
  104. DWORD thread_id;
  105. HWND next_window;
  106. HWND window;
  107. float retry_time;
  108. float fps_reset_time;
  109. float retry_interval;
  110. struct dstr title;
  111. struct dstr class;
  112. struct dstr executable;
  113. enum window_priority priority;
  114. obs_hotkey_pair_id hotkey_pair;
  115. volatile long hotkey_window;
  116. volatile bool deactivate_hook;
  117. volatile bool activate_hook_now;
  118. bool wait_for_target_startup;
  119. bool showing;
  120. bool active;
  121. bool capturing;
  122. bool activate_hook;
  123. bool process_is_64bit;
  124. bool error_acquiring;
  125. bool dwm_capture;
  126. bool initial_config;
  127. bool convert_16bit;
  128. bool is_app;
  129. bool cursor_hidden;
  130. struct game_capture_config config;
  131. ipc_pipe_server_t pipe;
  132. gs_texture_t *texture;
  133. struct hook_info *global_hook_info;
  134. HANDLE keepalive_mutex;
  135. HANDLE hook_init;
  136. HANDLE hook_restart;
  137. HANDLE hook_stop;
  138. HANDLE hook_ready;
  139. HANDLE hook_exit;
  140. HANDLE hook_data_map;
  141. HANDLE global_hook_info_map;
  142. HANDLE target_process;
  143. HANDLE texture_mutexes[2];
  144. wchar_t *app_sid;
  145. int retrying;
  146. float cursor_check_time;
  147. union {
  148. struct {
  149. struct shmem_data *shmem_data;
  150. uint8_t *texture_buffers[2];
  151. };
  152. struct shtex_data *shtex_data;
  153. void *data;
  154. };
  155. void (*copy_texture)(struct game_capture *);
  156. };
  157. struct graphics_offsets offsets32 = {0};
  158. struct graphics_offsets offsets64 = {0};
  159. static inline bool use_anticheat(struct game_capture *gc)
  160. {
  161. return gc->config.anticheat_hook && !gc->is_app;
  162. }
  163. static inline HANDLE open_mutex_plus_id(struct game_capture *gc,
  164. const wchar_t *name, DWORD id)
  165. {
  166. wchar_t new_name[64];
  167. _snwprintf(new_name, 64, L"%s%lu", name, id);
  168. return gc->is_app ? open_app_mutex(gc->app_sid, new_name)
  169. : open_mutex(new_name);
  170. }
  171. static inline HANDLE open_mutex_gc(struct game_capture *gc, const wchar_t *name)
  172. {
  173. return open_mutex_plus_id(gc, name, gc->process_id);
  174. }
  175. static inline HANDLE open_event_plus_id(struct game_capture *gc,
  176. const wchar_t *name, DWORD id)
  177. {
  178. wchar_t new_name[64];
  179. _snwprintf(new_name, 64, L"%s%lu", name, id);
  180. return gc->is_app ? open_app_event(gc->app_sid, new_name)
  181. : open_event(new_name);
  182. }
  183. static inline HANDLE open_event_gc(struct game_capture *gc, const wchar_t *name)
  184. {
  185. return open_event_plus_id(gc, name, gc->process_id);
  186. }
  187. static inline HANDLE open_map_plus_id(struct game_capture *gc,
  188. const wchar_t *name, DWORD id)
  189. {
  190. wchar_t new_name[64];
  191. swprintf(new_name, 64, L"%s%lu", name, id);
  192. debug("map id: %S", new_name);
  193. return gc->is_app ? open_app_map(gc->app_sid, new_name)
  194. : OpenFileMappingW(GC_MAPPING_FLAGS, false, new_name);
  195. }
  196. static inline HANDLE open_hook_info(struct game_capture *gc)
  197. {
  198. return open_map_plus_id(gc, SHMEM_HOOK_INFO, gc->process_id);
  199. }
  200. static inline enum gs_color_format convert_format(uint32_t format)
  201. {
  202. switch (format) {
  203. case DXGI_FORMAT_R8G8B8A8_UNORM:
  204. return GS_RGBA;
  205. case DXGI_FORMAT_B8G8R8X8_UNORM:
  206. return GS_BGRX;
  207. case DXGI_FORMAT_B8G8R8A8_UNORM:
  208. return GS_BGRA;
  209. case DXGI_FORMAT_R10G10B10A2_UNORM:
  210. return GS_R10G10B10A2;
  211. case DXGI_FORMAT_R16G16B16A16_UNORM:
  212. return GS_RGBA16;
  213. case DXGI_FORMAT_R16G16B16A16_FLOAT:
  214. return GS_RGBA16F;
  215. case DXGI_FORMAT_R32G32B32A32_FLOAT:
  216. return GS_RGBA32F;
  217. }
  218. return GS_UNKNOWN;
  219. }
  220. static void close_handle(HANDLE *p_handle)
  221. {
  222. HANDLE handle = *p_handle;
  223. if (handle) {
  224. if (handle != INVALID_HANDLE_VALUE)
  225. CloseHandle(handle);
  226. *p_handle = NULL;
  227. }
  228. }
  229. static inline HMODULE kernel32(void)
  230. {
  231. static HMODULE kernel32_handle = NULL;
  232. if (!kernel32_handle)
  233. kernel32_handle = GetModuleHandleW(L"kernel32");
  234. return kernel32_handle;
  235. }
  236. static inline HANDLE open_process(DWORD desired_access, bool inherit_handle,
  237. DWORD process_id)
  238. {
  239. typedef HANDLE(WINAPI * PFN_OpenProcess)(DWORD, BOOL, DWORD);
  240. PFN_OpenProcess open_process_proc = NULL;
  241. if (!open_process_proc)
  242. open_process_proc = (PFN_OpenProcess)get_obfuscated_func(
  243. kernel32(), "NuagUykjcxr", 0x1B694B59451ULL);
  244. return open_process_proc(desired_access, inherit_handle, process_id);
  245. }
  246. static inline float hook_rate_to_float(enum hook_rate rate)
  247. {
  248. switch (rate) {
  249. case HOOK_RATE_SLOW:
  250. return 2.0f;
  251. case HOOK_RATE_FAST:
  252. return 0.5f;
  253. case HOOK_RATE_FASTEST:
  254. return 0.1f;
  255. case HOOK_RATE_NORMAL:
  256. /* FALLTHROUGH */
  257. default:
  258. return 1.0f;
  259. }
  260. }
  261. static void stop_capture(struct game_capture *gc)
  262. {
  263. ipc_pipe_server_free(&gc->pipe);
  264. if (gc->hook_stop) {
  265. SetEvent(gc->hook_stop);
  266. }
  267. if (gc->global_hook_info) {
  268. UnmapViewOfFile(gc->global_hook_info);
  269. gc->global_hook_info = NULL;
  270. }
  271. if (gc->data) {
  272. UnmapViewOfFile(gc->data);
  273. gc->data = NULL;
  274. }
  275. if (gc->app_sid) {
  276. LocalFree(gc->app_sid);
  277. gc->app_sid = NULL;
  278. }
  279. close_handle(&gc->hook_restart);
  280. close_handle(&gc->hook_stop);
  281. close_handle(&gc->hook_ready);
  282. close_handle(&gc->hook_exit);
  283. close_handle(&gc->hook_init);
  284. close_handle(&gc->hook_data_map);
  285. close_handle(&gc->keepalive_mutex);
  286. close_handle(&gc->global_hook_info_map);
  287. close_handle(&gc->target_process);
  288. close_handle(&gc->texture_mutexes[0]);
  289. close_handle(&gc->texture_mutexes[1]);
  290. if (gc->texture) {
  291. obs_enter_graphics();
  292. gs_texture_destroy(gc->texture);
  293. obs_leave_graphics();
  294. gc->texture = NULL;
  295. }
  296. if (gc->active)
  297. info("capture stopped");
  298. gc->copy_texture = NULL;
  299. gc->wait_for_target_startup = false;
  300. gc->active = false;
  301. gc->capturing = false;
  302. if (gc->retrying)
  303. gc->retrying--;
  304. }
  305. static inline void free_config(struct game_capture_config *config)
  306. {
  307. bfree(config->title);
  308. bfree(config->class);
  309. bfree(config->executable);
  310. memset(config, 0, sizeof(*config));
  311. }
  312. static void game_capture_destroy(void *data)
  313. {
  314. struct game_capture *gc = data;
  315. stop_capture(gc);
  316. if (gc->hotkey_pair)
  317. obs_hotkey_pair_unregister(gc->hotkey_pair);
  318. obs_enter_graphics();
  319. cursor_data_free(&gc->cursor_data);
  320. obs_leave_graphics();
  321. dstr_free(&gc->title);
  322. dstr_free(&gc->class);
  323. dstr_free(&gc->executable);
  324. free_config(&gc->config);
  325. bfree(gc);
  326. }
  327. static inline bool using_older_non_mode_format(obs_data_t *settings)
  328. {
  329. return obs_data_has_user_value(settings, SETTING_ANY_FULLSCREEN) &&
  330. !obs_data_has_user_value(settings, SETTING_MODE);
  331. }
  332. static inline void get_config(struct game_capture_config *cfg,
  333. obs_data_t *settings, const char *window)
  334. {
  335. const char *mode_str = NULL;
  336. build_window_strings(window, &cfg->class, &cfg->title,
  337. &cfg->executable);
  338. if (using_older_non_mode_format(settings)) {
  339. bool any = obs_data_get_bool(settings, SETTING_ANY_FULLSCREEN);
  340. mode_str = any ? SETTING_MODE_ANY : SETTING_MODE_WINDOW;
  341. } else {
  342. mode_str = obs_data_get_string(settings, SETTING_MODE);
  343. }
  344. if (mode_str && strcmp(mode_str, SETTING_MODE_WINDOW) == 0)
  345. cfg->mode = CAPTURE_MODE_WINDOW;
  346. else if (mode_str && strcmp(mode_str, SETTING_MODE_HOTKEY) == 0)
  347. cfg->mode = CAPTURE_MODE_HOTKEY;
  348. else
  349. cfg->mode = CAPTURE_MODE_ANY;
  350. cfg->priority = (enum window_priority)obs_data_get_int(
  351. settings, SETTING_WINDOW_PRIORITY);
  352. cfg->force_shmem = obs_data_get_bool(settings, SETTING_COMPATIBILITY);
  353. cfg->cursor = obs_data_get_bool(settings, SETTING_CURSOR);
  354. cfg->allow_transparency =
  355. obs_data_get_bool(settings, SETTING_TRANSPARENCY);
  356. cfg->limit_framerate =
  357. obs_data_get_bool(settings, SETTING_LIMIT_FRAMERATE);
  358. cfg->capture_overlays =
  359. obs_data_get_bool(settings, SETTING_CAPTURE_OVERLAYS);
  360. cfg->anticheat_hook =
  361. obs_data_get_bool(settings, SETTING_ANTI_CHEAT_HOOK);
  362. cfg->hook_rate =
  363. (enum hook_rate)obs_data_get_int(settings, SETTING_HOOK_RATE);
  364. }
  365. static inline int s_cmp(const char *str1, const char *str2)
  366. {
  367. if (!str1 || !str2)
  368. return -1;
  369. return strcmp(str1, str2);
  370. }
  371. static inline bool capture_needs_reset(struct game_capture_config *cfg1,
  372. struct game_capture_config *cfg2)
  373. {
  374. if (cfg1->mode != cfg2->mode) {
  375. return true;
  376. } else if (cfg1->mode == CAPTURE_MODE_WINDOW &&
  377. (s_cmp(cfg1->class, cfg2->class) != 0 ||
  378. s_cmp(cfg1->title, cfg2->title) != 0 ||
  379. s_cmp(cfg1->executable, cfg2->executable) != 0 ||
  380. cfg1->priority != cfg2->priority)) {
  381. return true;
  382. } else if (cfg1->force_shmem != cfg2->force_shmem) {
  383. return true;
  384. } else if (cfg1->limit_framerate != cfg2->limit_framerate) {
  385. return true;
  386. } else if (cfg1->capture_overlays != cfg2->capture_overlays) {
  387. return true;
  388. }
  389. return false;
  390. }
  391. static bool hotkey_start(void *data, obs_hotkey_pair_id id,
  392. obs_hotkey_t *hotkey, bool pressed)
  393. {
  394. UNUSED_PARAMETER(id);
  395. UNUSED_PARAMETER(hotkey);
  396. struct game_capture *gc = data;
  397. if (pressed && gc->config.mode == CAPTURE_MODE_HOTKEY) {
  398. info("Activate hotkey pressed");
  399. os_atomic_set_long(&gc->hotkey_window,
  400. (long)(uintptr_t)GetForegroundWindow());
  401. os_atomic_set_bool(&gc->deactivate_hook, true);
  402. os_atomic_set_bool(&gc->activate_hook_now, true);
  403. }
  404. return true;
  405. }
  406. static bool hotkey_stop(void *data, obs_hotkey_pair_id id, obs_hotkey_t *hotkey,
  407. bool pressed)
  408. {
  409. UNUSED_PARAMETER(id);
  410. UNUSED_PARAMETER(hotkey);
  411. struct game_capture *gc = data;
  412. if (pressed && gc->config.mode == CAPTURE_MODE_HOTKEY) {
  413. info("Deactivate hotkey pressed");
  414. os_atomic_set_bool(&gc->deactivate_hook, true);
  415. }
  416. return true;
  417. }
  418. static void game_capture_update(void *data, obs_data_t *settings)
  419. {
  420. struct game_capture *gc = data;
  421. struct game_capture_config cfg;
  422. bool reset_capture = false;
  423. const char *window =
  424. obs_data_get_string(settings, SETTING_CAPTURE_WINDOW);
  425. get_config(&cfg, settings, window);
  426. reset_capture = capture_needs_reset(&cfg, &gc->config);
  427. gc->error_acquiring = false;
  428. if (cfg.mode == CAPTURE_MODE_HOTKEY &&
  429. gc->config.mode != CAPTURE_MODE_HOTKEY) {
  430. gc->activate_hook = false;
  431. } else {
  432. gc->activate_hook = !!window && !!*window;
  433. }
  434. free_config(&gc->config);
  435. gc->config = cfg;
  436. gc->retry_interval = DEFAULT_RETRY_INTERVAL *
  437. hook_rate_to_float(gc->config.hook_rate);
  438. gc->wait_for_target_startup = false;
  439. dstr_free(&gc->title);
  440. dstr_free(&gc->class);
  441. dstr_free(&gc->executable);
  442. if (cfg.mode == CAPTURE_MODE_WINDOW) {
  443. dstr_copy(&gc->title, gc->config.title);
  444. dstr_copy(&gc->class, gc->config.class);
  445. dstr_copy(&gc->executable, gc->config.executable);
  446. gc->priority = gc->config.priority;
  447. }
  448. if (!gc->initial_config) {
  449. if (reset_capture) {
  450. stop_capture(gc);
  451. }
  452. } else {
  453. gc->initial_config = false;
  454. }
  455. }
  456. extern void wait_for_hook_initialization(void);
  457. static void *game_capture_create(obs_data_t *settings, obs_source_t *source)
  458. {
  459. struct game_capture *gc = bzalloc(sizeof(*gc));
  460. wait_for_hook_initialization();
  461. gc->source = source;
  462. gc->initial_config = true;
  463. gc->retry_interval = DEFAULT_RETRY_INTERVAL *
  464. hook_rate_to_float(gc->config.hook_rate);
  465. gc->hotkey_pair = obs_hotkey_pair_register_source(
  466. gc->source, HOTKEY_START, TEXT_HOTKEY_START, HOTKEY_STOP,
  467. TEXT_HOTKEY_STOP, hotkey_start, hotkey_stop, gc, gc);
  468. game_capture_update(gc, settings);
  469. return gc;
  470. }
  471. #define STOP_BEING_BAD \
  472. " This is most likely due to security software. Please make sure " \
  473. "that the OBS installation folder is excluded/ignored in the " \
  474. "settings of the security software you are using."
  475. static bool check_file_integrity(struct game_capture *gc, const char *file,
  476. const char *name)
  477. {
  478. DWORD error;
  479. HANDLE handle;
  480. wchar_t *w_file = NULL;
  481. if (!file || !*file) {
  482. warn("Game capture %s not found." STOP_BEING_BAD, name);
  483. return false;
  484. }
  485. if (!os_utf8_to_wcs_ptr(file, 0, &w_file)) {
  486. warn("Could not convert file name to wide string");
  487. return false;
  488. }
  489. handle = CreateFileW(w_file, GENERIC_READ | GENERIC_EXECUTE,
  490. FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
  491. bfree(w_file);
  492. if (handle != INVALID_HANDLE_VALUE) {
  493. CloseHandle(handle);
  494. return true;
  495. }
  496. error = GetLastError();
  497. if (error == ERROR_FILE_NOT_FOUND) {
  498. warn("Game capture file '%s' not found." STOP_BEING_BAD, file);
  499. } else if (error == ERROR_ACCESS_DENIED) {
  500. warn("Game capture file '%s' could not be loaded." STOP_BEING_BAD,
  501. file);
  502. } else {
  503. warn("Game capture file '%s' could not be loaded: %lu." STOP_BEING_BAD,
  504. file, error);
  505. }
  506. return false;
  507. }
  508. static inline bool is_64bit_windows(void)
  509. {
  510. #ifdef _WIN64
  511. return true;
  512. #else
  513. BOOL x86 = false;
  514. bool success = !!IsWow64Process(GetCurrentProcess(), &x86);
  515. return success && !!x86;
  516. #endif
  517. }
  518. static inline bool is_64bit_process(HANDLE process)
  519. {
  520. BOOL x86 = true;
  521. if (is_64bit_windows()) {
  522. bool success = !!IsWow64Process(process, &x86);
  523. if (!success) {
  524. return false;
  525. }
  526. }
  527. return !x86;
  528. }
  529. static inline bool open_target_process(struct game_capture *gc)
  530. {
  531. gc->target_process = open_process(
  532. PROCESS_QUERY_INFORMATION | SYNCHRONIZE, false, gc->process_id);
  533. if (!gc->target_process) {
  534. warn("could not open process: %s", gc->config.executable);
  535. return false;
  536. }
  537. gc->process_is_64bit = is_64bit_process(gc->target_process);
  538. gc->is_app = is_app(gc->target_process);
  539. if (gc->is_app) {
  540. gc->app_sid = get_app_sid(gc->target_process);
  541. }
  542. return true;
  543. }
  544. static inline bool init_keepalive(struct game_capture *gc)
  545. {
  546. wchar_t new_name[64];
  547. swprintf(new_name, 64, WINDOW_HOOK_KEEPALIVE L"%lu", gc->process_id);
  548. gc->keepalive_mutex = gc->is_app
  549. ? create_app_mutex(gc->app_sid, new_name)
  550. : CreateMutexW(NULL, false, new_name);
  551. if (!gc->keepalive_mutex) {
  552. warn("Failed to create keepalive mutex: %lu", GetLastError());
  553. return false;
  554. }
  555. return true;
  556. }
  557. static inline bool init_texture_mutexes(struct game_capture *gc)
  558. {
  559. gc->texture_mutexes[0] = open_mutex_gc(gc, MUTEX_TEXTURE1);
  560. gc->texture_mutexes[1] = open_mutex_gc(gc, MUTEX_TEXTURE2);
  561. if (!gc->texture_mutexes[0] || !gc->texture_mutexes[1]) {
  562. DWORD error = GetLastError();
  563. if (error == 2) {
  564. if (!gc->retrying) {
  565. gc->retrying = 2;
  566. info("hook not loaded yet, retrying..");
  567. }
  568. } else {
  569. warn("failed to open texture mutexes: %lu",
  570. GetLastError());
  571. }
  572. return false;
  573. }
  574. return true;
  575. }
  576. /* if there's already a hook in the process, then signal and start */
  577. static inline bool attempt_existing_hook(struct game_capture *gc)
  578. {
  579. gc->hook_restart = open_event_gc(gc, EVENT_CAPTURE_RESTART);
  580. if (gc->hook_restart) {
  581. debug("existing hook found, signaling process: %s",
  582. gc->config.executable);
  583. SetEvent(gc->hook_restart);
  584. return true;
  585. }
  586. return false;
  587. }
  588. static inline void reset_frame_interval(struct game_capture *gc)
  589. {
  590. struct obs_video_info ovi;
  591. uint64_t interval = 0;
  592. if (obs_get_video_info(&ovi)) {
  593. interval =
  594. util_mul_div64(ovi.fps_den, 1000000000ULL, ovi.fps_num);
  595. /* Always limit capture framerate to some extent. If a game
  596. * running at 900 FPS is being captured without some sort of
  597. * limited capture interval, it will dramatically reduce
  598. * performance. */
  599. if (!gc->config.limit_framerate)
  600. interval /= 2;
  601. }
  602. gc->global_hook_info->frame_interval = interval;
  603. }
  604. static inline bool init_hook_info(struct game_capture *gc)
  605. {
  606. gc->global_hook_info_map = open_hook_info(gc);
  607. if (!gc->global_hook_info_map) {
  608. warn("init_hook_info: get_hook_info failed: %lu",
  609. GetLastError());
  610. return false;
  611. }
  612. gc->global_hook_info = MapViewOfFile(gc->global_hook_info_map,
  613. FILE_MAP_ALL_ACCESS, 0, 0,
  614. sizeof(*gc->global_hook_info));
  615. if (!gc->global_hook_info) {
  616. warn("init_hook_info: failed to map data view: %lu",
  617. GetLastError());
  618. return false;
  619. }
  620. if (gc->config.force_shmem) {
  621. warn("init_hook_info: user is forcing shared memory "
  622. "(multi-adapter compatibility mode)");
  623. }
  624. gc->global_hook_info->offsets = gc->process_is_64bit ? offsets64
  625. : offsets32;
  626. gc->global_hook_info->capture_overlay = gc->config.capture_overlays;
  627. gc->global_hook_info->force_shmem = gc->config.force_shmem;
  628. gc->global_hook_info->UNUSED_use_scale = false;
  629. gc->global_hook_info->allow_srgb_alias = true;
  630. reset_frame_interval(gc);
  631. obs_enter_graphics();
  632. if (!gs_shared_texture_available()) {
  633. warn("init_hook_info: shared texture capture unavailable");
  634. gc->global_hook_info->force_shmem = true;
  635. }
  636. obs_leave_graphics();
  637. return true;
  638. }
  639. static void pipe_log(void *param, uint8_t *data, size_t size)
  640. {
  641. struct game_capture *gc = param;
  642. if (data && size)
  643. info("%s", data);
  644. }
  645. static inline bool init_pipe(struct game_capture *gc)
  646. {
  647. char name[64];
  648. sprintf(name, "%s%lu", PIPE_NAME, gc->process_id);
  649. if (!ipc_pipe_server_start(&gc->pipe, name, pipe_log, gc)) {
  650. warn("init_pipe: failed to start pipe");
  651. return false;
  652. }
  653. return true;
  654. }
  655. static inline int inject_library(HANDLE process, const wchar_t *dll)
  656. {
  657. return inject_library_obf(process, dll, "D|hkqkW`kl{k\\osofj",
  658. 0xa178ef3655e5ade7, "[uawaRzbhh{tIdkj~~",
  659. 0x561478dbd824387c, "[fr}pboIe`dlN}",
  660. 0x395bfbc9833590fd, "\\`zs}gmOzhhBq",
  661. 0x12897dd89168789a, "GbfkDaezbp~X",
  662. 0x76aff7238788f7db);
  663. }
  664. static inline bool hook_direct(struct game_capture *gc,
  665. const char *hook_path_rel)
  666. {
  667. wchar_t hook_path_abs_w[MAX_PATH];
  668. wchar_t *hook_path_rel_w;
  669. wchar_t *path_ret;
  670. HANDLE process;
  671. int ret;
  672. os_utf8_to_wcs_ptr(hook_path_rel, 0, &hook_path_rel_w);
  673. if (!hook_path_rel_w) {
  674. warn("hook_direct: could not convert string");
  675. return false;
  676. }
  677. path_ret = _wfullpath(hook_path_abs_w, hook_path_rel_w, MAX_PATH);
  678. bfree(hook_path_rel_w);
  679. if (path_ret == NULL) {
  680. warn("hook_direct: could not make absolute path");
  681. return false;
  682. }
  683. process = open_process(PROCESS_ALL_ACCESS, false, gc->process_id);
  684. if (!process) {
  685. warn("hook_direct: could not open process: %s (%lu)",
  686. gc->config.executable, GetLastError());
  687. return false;
  688. }
  689. ret = inject_library(process, hook_path_abs_w);
  690. CloseHandle(process);
  691. if (ret != 0) {
  692. warn("hook_direct: inject failed: %d", ret);
  693. return false;
  694. }
  695. return true;
  696. }
  697. static inline bool create_inject_process(struct game_capture *gc,
  698. const char *inject_path,
  699. const char *hook_dll)
  700. {
  701. wchar_t *command_line_w = malloc(4096 * sizeof(wchar_t));
  702. wchar_t *inject_path_w;
  703. wchar_t *hook_dll_w;
  704. bool anti_cheat = use_anticheat(gc);
  705. PROCESS_INFORMATION pi = {0};
  706. STARTUPINFO si = {0};
  707. bool success = false;
  708. os_utf8_to_wcs_ptr(inject_path, 0, &inject_path_w);
  709. os_utf8_to_wcs_ptr(hook_dll, 0, &hook_dll_w);
  710. si.cb = sizeof(si);
  711. swprintf(command_line_w, 4096, L"\"%s\" \"%s\" %lu %lu", inject_path_w,
  712. hook_dll_w, (unsigned long)anti_cheat,
  713. anti_cheat ? gc->thread_id : gc->process_id);
  714. success = !!CreateProcessW(inject_path_w, command_line_w, NULL, NULL,
  715. false, CREATE_NO_WINDOW, NULL, NULL, &si,
  716. &pi);
  717. if (success) {
  718. CloseHandle(pi.hThread);
  719. gc->injector_process = pi.hProcess;
  720. } else {
  721. warn("Failed to create inject helper process: %lu",
  722. GetLastError());
  723. }
  724. free(command_line_w);
  725. bfree(inject_path_w);
  726. bfree(hook_dll_w);
  727. return success;
  728. }
  729. extern char *get_hook_path(bool b64);
  730. static inline bool inject_hook(struct game_capture *gc)
  731. {
  732. bool matching_architecture;
  733. bool success = false;
  734. char *inject_path;
  735. char *hook_path;
  736. if (gc->process_is_64bit) {
  737. inject_path = obs_module_file("inject-helper64.exe");
  738. } else {
  739. inject_path = obs_module_file("inject-helper32.exe");
  740. }
  741. hook_path = get_hook_path(gc->process_is_64bit);
  742. if (!check_file_integrity(gc, inject_path, "inject helper")) {
  743. goto cleanup;
  744. }
  745. if (!check_file_integrity(gc, hook_path, "graphics hook")) {
  746. goto cleanup;
  747. }
  748. #ifdef _WIN64
  749. matching_architecture = gc->process_is_64bit;
  750. #else
  751. matching_architecture = !gc->process_is_64bit;
  752. #endif
  753. if (matching_architecture && !use_anticheat(gc)) {
  754. info("using direct hook");
  755. success = hook_direct(gc, hook_path);
  756. } else {
  757. info("using helper (%s hook)",
  758. use_anticheat(gc) ? "compatibility" : "direct");
  759. success = create_inject_process(gc, inject_path, hook_path);
  760. }
  761. cleanup:
  762. bfree(inject_path);
  763. bfree(hook_path);
  764. return success;
  765. }
  766. static const char *blacklisted_exes[] = {
  767. "explorer",
  768. "steam",
  769. "battle.net",
  770. "galaxyclient",
  771. "skype",
  772. "uplay",
  773. "origin",
  774. "devenv",
  775. "taskmgr",
  776. "chrome",
  777. "discord",
  778. "firefox",
  779. "systemsettings",
  780. "applicationframehost",
  781. "cmd",
  782. "shellexperiencehost",
  783. "winstore.app",
  784. "searchui",
  785. "lockapp",
  786. "windowsinternal.composableshell.experiences.textinput.inputapp",
  787. NULL,
  788. };
  789. static bool is_blacklisted_exe(const char *exe)
  790. {
  791. char cur_exe[MAX_PATH];
  792. if (!exe)
  793. return false;
  794. for (const char **vals = blacklisted_exes; *vals; vals++) {
  795. strcpy(cur_exe, *vals);
  796. strcat(cur_exe, ".exe");
  797. if (strcmpi(cur_exe, exe) == 0)
  798. return true;
  799. }
  800. return false;
  801. }
  802. static bool target_suspended(struct game_capture *gc)
  803. {
  804. return thread_is_suspended(gc->process_id, gc->thread_id);
  805. }
  806. static bool init_events(struct game_capture *gc);
  807. static bool init_hook(struct game_capture *gc)
  808. {
  809. struct dstr exe = {0};
  810. bool blacklisted_process = false;
  811. if (gc->config.mode == CAPTURE_MODE_ANY) {
  812. if (get_window_exe(&exe, gc->next_window)) {
  813. info("attempting to hook fullscreen process: %s",
  814. exe.array);
  815. }
  816. } else {
  817. if (get_window_exe(&exe, gc->next_window)) {
  818. info("attempting to hook process: %s", exe.array);
  819. }
  820. }
  821. blacklisted_process = is_blacklisted_exe(exe.array);
  822. if (blacklisted_process)
  823. info("cannot capture %s due to being blacklisted", exe.array);
  824. dstr_free(&exe);
  825. if (blacklisted_process) {
  826. return false;
  827. }
  828. if (target_suspended(gc)) {
  829. return false;
  830. }
  831. if (!open_target_process(gc)) {
  832. return false;
  833. }
  834. if (!init_keepalive(gc)) {
  835. return false;
  836. }
  837. if (!init_pipe(gc)) {
  838. return false;
  839. }
  840. if (!attempt_existing_hook(gc)) {
  841. if (!inject_hook(gc)) {
  842. return false;
  843. }
  844. }
  845. if (!init_texture_mutexes(gc)) {
  846. return false;
  847. }
  848. if (!init_hook_info(gc)) {
  849. return false;
  850. }
  851. if (!init_events(gc)) {
  852. return false;
  853. }
  854. SetEvent(gc->hook_init);
  855. gc->window = gc->next_window;
  856. gc->next_window = NULL;
  857. gc->active = true;
  858. gc->retrying = 0;
  859. return true;
  860. }
  861. static void setup_window(struct game_capture *gc, HWND window)
  862. {
  863. HANDLE hook_restart;
  864. HANDLE process;
  865. GetWindowThreadProcessId(window, &gc->process_id);
  866. if (gc->process_id) {
  867. process = open_process(PROCESS_QUERY_INFORMATION, false,
  868. gc->process_id);
  869. if (process) {
  870. gc->is_app = is_app(process);
  871. if (gc->is_app) {
  872. gc->app_sid = get_app_sid(process);
  873. }
  874. CloseHandle(process);
  875. }
  876. }
  877. /* do not wait if we're re-hooking a process */
  878. hook_restart = open_event_gc(gc, EVENT_CAPTURE_RESTART);
  879. if (hook_restart) {
  880. gc->wait_for_target_startup = false;
  881. CloseHandle(hook_restart);
  882. }
  883. /* otherwise if it's an unhooked process, always wait a bit for the
  884. * target process to start up before starting the hook process;
  885. * sometimes they have important modules to load first or other hooks
  886. * (such as steam) need a little bit of time to load. ultimately this
  887. * helps prevent crashes */
  888. if (gc->wait_for_target_startup) {
  889. gc->retry_interval =
  890. 3.0f * hook_rate_to_float(gc->config.hook_rate);
  891. gc->wait_for_target_startup = false;
  892. } else {
  893. gc->next_window = window;
  894. }
  895. }
  896. static void get_fullscreen_window(struct game_capture *gc)
  897. {
  898. HWND window = GetForegroundWindow();
  899. MONITORINFO mi = {0};
  900. HMONITOR monitor;
  901. DWORD styles;
  902. RECT rect;
  903. gc->next_window = NULL;
  904. if (!window) {
  905. return;
  906. }
  907. if (!GetWindowRect(window, &rect)) {
  908. return;
  909. }
  910. /* ignore regular maximized windows */
  911. styles = (DWORD)GetWindowLongPtr(window, GWL_STYLE);
  912. if ((styles & WS_MAXIMIZE) != 0 && (styles & WS_BORDER) != 0) {
  913. return;
  914. }
  915. monitor = MonitorFromRect(&rect, MONITOR_DEFAULTTONEAREST);
  916. if (!monitor) {
  917. return;
  918. }
  919. mi.cbSize = sizeof(mi);
  920. if (!GetMonitorInfo(monitor, &mi)) {
  921. return;
  922. }
  923. if (rect.left == mi.rcMonitor.left &&
  924. rect.right == mi.rcMonitor.right &&
  925. rect.bottom == mi.rcMonitor.bottom &&
  926. rect.top == mi.rcMonitor.top) {
  927. setup_window(gc, window);
  928. } else {
  929. gc->wait_for_target_startup = true;
  930. }
  931. }
  932. static void get_selected_window(struct game_capture *gc)
  933. {
  934. HWND window;
  935. if (dstr_cmpi(&gc->class, "dwm") == 0) {
  936. wchar_t class_w[512];
  937. os_utf8_to_wcs(gc->class.array, 0, class_w, 512);
  938. window = FindWindowW(class_w, NULL);
  939. } else {
  940. window = find_window(INCLUDE_MINIMIZED, gc->priority,
  941. gc->class.array, gc->title.array,
  942. gc->executable.array);
  943. }
  944. if (window) {
  945. setup_window(gc, window);
  946. } else {
  947. gc->wait_for_target_startup = true;
  948. }
  949. }
  950. static void try_hook(struct game_capture *gc)
  951. {
  952. if (gc->config.mode == CAPTURE_MODE_ANY) {
  953. get_fullscreen_window(gc);
  954. } else {
  955. get_selected_window(gc);
  956. }
  957. if (gc->next_window) {
  958. gc->thread_id = GetWindowThreadProcessId(gc->next_window,
  959. &gc->process_id);
  960. // Make sure we never try to hook ourselves (projector)
  961. if (gc->process_id == GetCurrentProcessId())
  962. return;
  963. if (!gc->thread_id && gc->process_id)
  964. return;
  965. if (!gc->process_id) {
  966. warn("error acquiring, failed to get window "
  967. "thread/process ids: %lu",
  968. GetLastError());
  969. gc->error_acquiring = true;
  970. return;
  971. }
  972. if (!init_hook(gc)) {
  973. stop_capture(gc);
  974. }
  975. } else {
  976. gc->active = false;
  977. }
  978. }
  979. static inline bool init_events(struct game_capture *gc)
  980. {
  981. if (!gc->hook_restart) {
  982. gc->hook_restart = open_event_gc(gc, EVENT_CAPTURE_RESTART);
  983. if (!gc->hook_restart) {
  984. warn("init_events: failed to get hook_restart "
  985. "event: %lu",
  986. GetLastError());
  987. return false;
  988. }
  989. }
  990. if (!gc->hook_stop) {
  991. gc->hook_stop = open_event_gc(gc, EVENT_CAPTURE_STOP);
  992. if (!gc->hook_stop) {
  993. warn("init_events: failed to get hook_stop event: %lu",
  994. GetLastError());
  995. return false;
  996. }
  997. }
  998. if (!gc->hook_init) {
  999. gc->hook_init = open_event_gc(gc, EVENT_HOOK_INIT);
  1000. if (!gc->hook_init) {
  1001. warn("init_events: failed to get hook_init event: %lu",
  1002. GetLastError());
  1003. return false;
  1004. }
  1005. }
  1006. if (!gc->hook_ready) {
  1007. gc->hook_ready = open_event_gc(gc, EVENT_HOOK_READY);
  1008. if (!gc->hook_ready) {
  1009. warn("init_events: failed to get hook_ready event: %lu",
  1010. GetLastError());
  1011. return false;
  1012. }
  1013. }
  1014. if (!gc->hook_exit) {
  1015. gc->hook_exit = open_event_gc(gc, EVENT_HOOK_EXIT);
  1016. if (!gc->hook_exit) {
  1017. warn("init_events: failed to get hook_exit event: %lu",
  1018. GetLastError());
  1019. return false;
  1020. }
  1021. }
  1022. return true;
  1023. }
  1024. enum capture_result { CAPTURE_FAIL, CAPTURE_RETRY, CAPTURE_SUCCESS };
  1025. static inline bool init_data_map(struct game_capture *gc, HWND window)
  1026. {
  1027. wchar_t name[64];
  1028. swprintf(name, 64, SHMEM_TEXTURE "_%" PRIu64 "_",
  1029. (uint64_t)(uintptr_t)window);
  1030. gc->hook_data_map =
  1031. open_map_plus_id(gc, name, gc->global_hook_info->map_id);
  1032. return !!gc->hook_data_map;
  1033. }
  1034. static inline enum capture_result init_capture_data(struct game_capture *gc)
  1035. {
  1036. gc->cx = gc->global_hook_info->cx;
  1037. gc->cy = gc->global_hook_info->cy;
  1038. gc->pitch = gc->global_hook_info->pitch;
  1039. if (gc->data) {
  1040. UnmapViewOfFile(gc->data);
  1041. gc->data = NULL;
  1042. }
  1043. CloseHandle(gc->hook_data_map);
  1044. DWORD error = 0;
  1045. if (!init_data_map(gc, gc->window)) {
  1046. HWND retry_hwnd = (HWND)(uintptr_t)gc->global_hook_info->window;
  1047. error = GetLastError();
  1048. /* if there's an error, just override. some windows don't play
  1049. * nice. */
  1050. if (init_data_map(gc, retry_hwnd)) {
  1051. error = 0;
  1052. }
  1053. }
  1054. if (!gc->hook_data_map) {
  1055. if (error == 2) {
  1056. return CAPTURE_RETRY;
  1057. } else {
  1058. warn("init_capture_data: failed to open file "
  1059. "mapping: %lu",
  1060. error);
  1061. }
  1062. return CAPTURE_FAIL;
  1063. }
  1064. gc->data = MapViewOfFile(gc->hook_data_map, FILE_MAP_ALL_ACCESS, 0, 0,
  1065. gc->global_hook_info->map_size);
  1066. if (!gc->data) {
  1067. warn("init_capture_data: failed to map data view: %lu",
  1068. GetLastError());
  1069. return CAPTURE_FAIL;
  1070. }
  1071. return CAPTURE_SUCCESS;
  1072. }
  1073. #define PIXEL_16BIT_SIZE 2
  1074. #define PIXEL_32BIT_SIZE 4
  1075. static inline uint32_t convert_5_to_8bit(uint16_t val)
  1076. {
  1077. return (uint32_t)((double)(val & 0x1F) * (255.0 / 31.0));
  1078. }
  1079. static inline uint32_t convert_6_to_8bit(uint16_t val)
  1080. {
  1081. return (uint32_t)((double)(val & 0x3F) * (255.0 / 63.0));
  1082. }
  1083. static void copy_b5g6r5_tex(struct game_capture *gc, int cur_texture,
  1084. uint8_t *data, uint32_t pitch)
  1085. {
  1086. uint8_t *input = gc->texture_buffers[cur_texture];
  1087. uint32_t gc_cx = gc->cx;
  1088. uint32_t gc_cy = gc->cy;
  1089. uint32_t gc_pitch = gc->pitch;
  1090. for (size_t y = 0; y < gc_cy; y++) {
  1091. uint8_t *row = input + (gc_pitch * y);
  1092. uint8_t *out = data + (pitch * y);
  1093. for (size_t x = 0; x < gc_cx; x += 8) {
  1094. __m128i pixels_blue, pixels_green, pixels_red;
  1095. __m128i pixels_result;
  1096. __m128i *pixels_dest;
  1097. __m128i *pixels_src =
  1098. (__m128i *)(row + x * sizeof(uint16_t));
  1099. __m128i pixels = _mm_load_si128(pixels_src);
  1100. __m128i zero = _mm_setzero_si128();
  1101. __m128i pixels_low = _mm_unpacklo_epi16(pixels, zero);
  1102. __m128i pixels_high = _mm_unpackhi_epi16(pixels, zero);
  1103. __m128i blue_channel_mask = _mm_set1_epi32(0x0000001F);
  1104. __m128i blue_offset = _mm_set1_epi32(0x00000003);
  1105. __m128i green_channel_mask = _mm_set1_epi32(0x000007E0);
  1106. __m128i green_offset = _mm_set1_epi32(0x00000008);
  1107. __m128i red_channel_mask = _mm_set1_epi32(0x0000F800);
  1108. __m128i red_offset = _mm_set1_epi32(0x00000300);
  1109. pixels_blue =
  1110. _mm_and_si128(pixels_low, blue_channel_mask);
  1111. pixels_blue = _mm_slli_epi32(pixels_blue, 3);
  1112. pixels_blue = _mm_add_epi32(pixels_blue, blue_offset);
  1113. pixels_green =
  1114. _mm_and_si128(pixels_low, green_channel_mask);
  1115. pixels_green =
  1116. _mm_add_epi32(pixels_green, green_offset);
  1117. pixels_green = _mm_slli_epi32(pixels_green, 5);
  1118. pixels_red =
  1119. _mm_and_si128(pixels_low, red_channel_mask);
  1120. pixels_red = _mm_add_epi32(pixels_red, red_offset);
  1121. pixels_red = _mm_slli_epi32(pixels_red, 8);
  1122. pixels_result = _mm_set1_epi32(0xFF000000);
  1123. pixels_result =
  1124. _mm_or_si128(pixels_result, pixels_blue);
  1125. pixels_result =
  1126. _mm_or_si128(pixels_result, pixels_green);
  1127. pixels_result = _mm_or_si128(pixels_result, pixels_red);
  1128. pixels_dest = (__m128i *)(out + x * sizeof(uint32_t));
  1129. _mm_store_si128(pixels_dest, pixels_result);
  1130. pixels_blue =
  1131. _mm_and_si128(pixels_high, blue_channel_mask);
  1132. pixels_blue = _mm_slli_epi32(pixels_blue, 3);
  1133. pixels_blue = _mm_add_epi32(pixels_blue, blue_offset);
  1134. pixels_green =
  1135. _mm_and_si128(pixels_high, green_channel_mask);
  1136. pixels_green =
  1137. _mm_add_epi32(pixels_green, green_offset);
  1138. pixels_green = _mm_slli_epi32(pixels_green, 5);
  1139. pixels_red =
  1140. _mm_and_si128(pixels_high, red_channel_mask);
  1141. pixels_red = _mm_add_epi32(pixels_red, red_offset);
  1142. pixels_red = _mm_slli_epi32(pixels_red, 8);
  1143. pixels_result = _mm_set1_epi32(0xFF000000);
  1144. pixels_result =
  1145. _mm_or_si128(pixels_result, pixels_blue);
  1146. pixels_result =
  1147. _mm_or_si128(pixels_result, pixels_green);
  1148. pixels_result = _mm_or_si128(pixels_result, pixels_red);
  1149. pixels_dest =
  1150. (__m128i *)(out + (x + 4) * sizeof(uint32_t));
  1151. _mm_store_si128(pixels_dest, pixels_result);
  1152. }
  1153. }
  1154. }
  1155. static void copy_b5g5r5a1_tex(struct game_capture *gc, int cur_texture,
  1156. uint8_t *data, uint32_t pitch)
  1157. {
  1158. uint8_t *input = gc->texture_buffers[cur_texture];
  1159. uint32_t gc_cx = gc->cx;
  1160. uint32_t gc_cy = gc->cy;
  1161. uint32_t gc_pitch = gc->pitch;
  1162. for (size_t y = 0; y < gc_cy; y++) {
  1163. uint8_t *row = input + (gc_pitch * y);
  1164. uint8_t *out = data + (pitch * y);
  1165. for (size_t x = 0; x < gc_cx; x += 8) {
  1166. __m128i pixels_blue, pixels_green, pixels_red,
  1167. pixels_alpha;
  1168. __m128i pixels_result;
  1169. __m128i *pixels_dest;
  1170. __m128i *pixels_src =
  1171. (__m128i *)(row + x * sizeof(uint16_t));
  1172. __m128i pixels = _mm_load_si128(pixels_src);
  1173. __m128i zero = _mm_setzero_si128();
  1174. __m128i pixels_low = _mm_unpacklo_epi16(pixels, zero);
  1175. __m128i pixels_high = _mm_unpackhi_epi16(pixels, zero);
  1176. __m128i blue_channel_mask = _mm_set1_epi32(0x0000001F);
  1177. __m128i blue_offset = _mm_set1_epi32(0x00000003);
  1178. __m128i green_channel_mask = _mm_set1_epi32(0x000003E0);
  1179. __m128i green_offset = _mm_set1_epi32(0x000000C);
  1180. __m128i red_channel_mask = _mm_set1_epi32(0x00007C00);
  1181. __m128i red_offset = _mm_set1_epi32(0x00000180);
  1182. __m128i alpha_channel_mask = _mm_set1_epi32(0x00008000);
  1183. __m128i alpha_offset = _mm_set1_epi32(0x00000001);
  1184. __m128i alpha_mask32 = _mm_set1_epi32(0xFF000000);
  1185. pixels_blue =
  1186. _mm_and_si128(pixels_low, blue_channel_mask);
  1187. pixels_blue = _mm_slli_epi32(pixels_blue, 3);
  1188. pixels_blue = _mm_add_epi32(pixels_blue, blue_offset);
  1189. pixels_green =
  1190. _mm_and_si128(pixels_low, green_channel_mask);
  1191. pixels_green =
  1192. _mm_add_epi32(pixels_green, green_offset);
  1193. pixels_green = _mm_slli_epi32(pixels_green, 6);
  1194. pixels_red =
  1195. _mm_and_si128(pixels_low, red_channel_mask);
  1196. pixels_red = _mm_add_epi32(pixels_red, red_offset);
  1197. pixels_red = _mm_slli_epi32(pixels_red, 9);
  1198. pixels_alpha =
  1199. _mm_and_si128(pixels_low, alpha_channel_mask);
  1200. pixels_alpha = _mm_srli_epi32(pixels_alpha, 15);
  1201. pixels_alpha =
  1202. _mm_sub_epi32(pixels_alpha, alpha_offset);
  1203. pixels_alpha =
  1204. _mm_andnot_si128(pixels_alpha, alpha_mask32);
  1205. pixels_result = pixels_red;
  1206. pixels_result =
  1207. _mm_or_si128(pixels_result, pixels_alpha);
  1208. pixels_result =
  1209. _mm_or_si128(pixels_result, pixels_blue);
  1210. pixels_result =
  1211. _mm_or_si128(pixels_result, pixels_green);
  1212. pixels_dest = (__m128i *)(out + x * sizeof(uint32_t));
  1213. _mm_store_si128(pixels_dest, pixels_result);
  1214. pixels_blue =
  1215. _mm_and_si128(pixels_high, blue_channel_mask);
  1216. pixels_blue = _mm_slli_epi32(pixels_blue, 3);
  1217. pixels_blue = _mm_add_epi32(pixels_blue, blue_offset);
  1218. pixels_green =
  1219. _mm_and_si128(pixels_high, green_channel_mask);
  1220. pixels_green =
  1221. _mm_add_epi32(pixels_green, green_offset);
  1222. pixels_green = _mm_slli_epi32(pixels_green, 6);
  1223. pixels_red =
  1224. _mm_and_si128(pixels_high, red_channel_mask);
  1225. pixels_red = _mm_add_epi32(pixels_red, red_offset);
  1226. pixels_red = _mm_slli_epi32(pixels_red, 9);
  1227. pixels_alpha =
  1228. _mm_and_si128(pixels_high, alpha_channel_mask);
  1229. pixels_alpha = _mm_srli_epi32(pixels_alpha, 15);
  1230. pixels_alpha =
  1231. _mm_sub_epi32(pixels_alpha, alpha_offset);
  1232. pixels_alpha =
  1233. _mm_andnot_si128(pixels_alpha, alpha_mask32);
  1234. pixels_result = pixels_red;
  1235. pixels_result =
  1236. _mm_or_si128(pixels_result, pixels_alpha);
  1237. pixels_result =
  1238. _mm_or_si128(pixels_result, pixels_blue);
  1239. pixels_result =
  1240. _mm_or_si128(pixels_result, pixels_green);
  1241. pixels_dest =
  1242. (__m128i *)(out + (x + 4) * sizeof(uint32_t));
  1243. _mm_store_si128(pixels_dest, pixels_result);
  1244. }
  1245. }
  1246. }
  1247. static inline void copy_16bit_tex(struct game_capture *gc, int cur_texture,
  1248. uint8_t *data, uint32_t pitch)
  1249. {
  1250. if (gc->global_hook_info->format == DXGI_FORMAT_B5G5R5A1_UNORM) {
  1251. copy_b5g5r5a1_tex(gc, cur_texture, data, pitch);
  1252. } else if (gc->global_hook_info->format == DXGI_FORMAT_B5G6R5_UNORM) {
  1253. copy_b5g6r5_tex(gc, cur_texture, data, pitch);
  1254. }
  1255. }
  1256. static void copy_shmem_tex(struct game_capture *gc)
  1257. {
  1258. int cur_texture;
  1259. HANDLE mutex = NULL;
  1260. uint32_t pitch;
  1261. int next_texture;
  1262. uint8_t *data;
  1263. if (!gc->shmem_data)
  1264. return;
  1265. cur_texture = gc->shmem_data->last_tex;
  1266. if (cur_texture < 0 || cur_texture > 1)
  1267. return;
  1268. next_texture = cur_texture == 1 ? 0 : 1;
  1269. if (object_signalled(gc->texture_mutexes[cur_texture])) {
  1270. mutex = gc->texture_mutexes[cur_texture];
  1271. } else if (object_signalled(gc->texture_mutexes[next_texture])) {
  1272. mutex = gc->texture_mutexes[next_texture];
  1273. cur_texture = next_texture;
  1274. } else {
  1275. return;
  1276. }
  1277. if (gs_texture_map(gc->texture, &data, &pitch)) {
  1278. if (gc->convert_16bit) {
  1279. copy_16bit_tex(gc, cur_texture, data, pitch);
  1280. } else if (pitch == gc->pitch) {
  1281. memcpy(data, gc->texture_buffers[cur_texture],
  1282. (size_t)pitch * (size_t)gc->cy);
  1283. } else {
  1284. uint8_t *input = gc->texture_buffers[cur_texture];
  1285. uint32_t best_pitch = pitch < gc->pitch ? pitch
  1286. : gc->pitch;
  1287. for (size_t y = 0; y < gc->cy; y++) {
  1288. uint8_t *line_in = input + gc->pitch * y;
  1289. uint8_t *line_out = data + pitch * y;
  1290. memcpy(line_out, line_in, best_pitch);
  1291. }
  1292. }
  1293. gs_texture_unmap(gc->texture);
  1294. }
  1295. ReleaseMutex(mutex);
  1296. }
  1297. static inline bool is_16bit_format(uint32_t format)
  1298. {
  1299. return format == DXGI_FORMAT_B5G5R5A1_UNORM ||
  1300. format == DXGI_FORMAT_B5G6R5_UNORM;
  1301. }
  1302. static inline bool init_shmem_capture(struct game_capture *gc)
  1303. {
  1304. enum gs_color_format format;
  1305. gc->texture_buffers[0] =
  1306. (uint8_t *)gc->data + gc->shmem_data->tex1_offset;
  1307. gc->texture_buffers[1] =
  1308. (uint8_t *)gc->data + gc->shmem_data->tex2_offset;
  1309. gc->convert_16bit = is_16bit_format(gc->global_hook_info->format);
  1310. format = gc->convert_16bit
  1311. ? GS_BGRA
  1312. : convert_format(gc->global_hook_info->format);
  1313. obs_enter_graphics();
  1314. gs_texture_destroy(gc->texture);
  1315. gc->texture =
  1316. gs_texture_create(gc->cx, gc->cy, format, 1, NULL, GS_DYNAMIC);
  1317. obs_leave_graphics();
  1318. if (!gc->texture) {
  1319. warn("init_shmem_capture: failed to create texture");
  1320. return false;
  1321. }
  1322. gc->copy_texture = copy_shmem_tex;
  1323. return true;
  1324. }
  1325. static inline bool init_shtex_capture(struct game_capture *gc)
  1326. {
  1327. obs_enter_graphics();
  1328. gs_texture_destroy(gc->texture);
  1329. gc->texture = gs_texture_open_shared(gc->shtex_data->tex_handle);
  1330. obs_leave_graphics();
  1331. if (!gc->texture) {
  1332. warn("init_shtex_capture: failed to open shared handle");
  1333. return false;
  1334. }
  1335. return true;
  1336. }
  1337. static bool start_capture(struct game_capture *gc)
  1338. {
  1339. debug("Starting capture");
  1340. /* prevent from using a DLL version that's higher than current */
  1341. if (gc->global_hook_info->hook_ver_major > HOOK_VER_MAJOR) {
  1342. warn("cannot initialize hook, DLL hook version is "
  1343. "%" PRIu32 ".%" PRIu32
  1344. ", current plugin hook major version is %d.%d",
  1345. gc->global_hook_info->hook_ver_major,
  1346. gc->global_hook_info->hook_ver_minor, HOOK_VER_MAJOR,
  1347. HOOK_VER_MINOR);
  1348. return false;
  1349. }
  1350. if (gc->global_hook_info->type == CAPTURE_TYPE_MEMORY) {
  1351. if (!init_shmem_capture(gc)) {
  1352. return false;
  1353. }
  1354. info("memory capture successful");
  1355. } else {
  1356. if (!init_shtex_capture(gc)) {
  1357. return false;
  1358. }
  1359. info("shared texture capture successful");
  1360. }
  1361. return true;
  1362. }
  1363. static inline bool capture_valid(struct game_capture *gc)
  1364. {
  1365. if (!gc->dwm_capture && !IsWindow(gc->window))
  1366. return false;
  1367. return !object_signalled(gc->target_process);
  1368. }
  1369. static void check_foreground_window(struct game_capture *gc, float seconds)
  1370. {
  1371. // Hides the cursor if the user isn't actively in the game
  1372. gc->cursor_check_time += seconds;
  1373. if (gc->cursor_check_time >= 0.1f) {
  1374. DWORD foreground_process_id;
  1375. GetWindowThreadProcessId(GetForegroundWindow(),
  1376. &foreground_process_id);
  1377. if (gc->process_id != foreground_process_id)
  1378. gc->cursor_hidden = true;
  1379. else
  1380. gc->cursor_hidden = false;
  1381. gc->cursor_check_time = 0.0f;
  1382. }
  1383. }
  1384. static void game_capture_tick(void *data, float seconds)
  1385. {
  1386. struct game_capture *gc = data;
  1387. bool deactivate = os_atomic_set_bool(&gc->deactivate_hook, false);
  1388. bool activate_now = os_atomic_set_bool(&gc->activate_hook_now, false);
  1389. if (activate_now) {
  1390. HWND hwnd = (HWND)(uintptr_t)os_atomic_load_long(
  1391. &gc->hotkey_window);
  1392. if (is_uwp_window(hwnd))
  1393. hwnd = get_uwp_actual_window(hwnd);
  1394. if (get_window_exe(&gc->executable, hwnd)) {
  1395. get_window_title(&gc->title, hwnd);
  1396. get_window_class(&gc->class, hwnd);
  1397. gc->priority = WINDOW_PRIORITY_CLASS;
  1398. gc->retry_time = 10.0f * hook_rate_to_float(
  1399. gc->config.hook_rate);
  1400. gc->activate_hook = true;
  1401. } else {
  1402. deactivate = false;
  1403. activate_now = false;
  1404. }
  1405. } else if (deactivate) {
  1406. gc->activate_hook = false;
  1407. }
  1408. if (!obs_source_showing(gc->source)) {
  1409. if (gc->showing) {
  1410. if (gc->active)
  1411. stop_capture(gc);
  1412. gc->showing = false;
  1413. }
  1414. return;
  1415. } else if (!gc->showing) {
  1416. gc->retry_time =
  1417. 10.0f * hook_rate_to_float(gc->config.hook_rate);
  1418. }
  1419. if (gc->hook_stop && object_signalled(gc->hook_stop)) {
  1420. debug("hook stop signal received");
  1421. stop_capture(gc);
  1422. }
  1423. if (gc->active && deactivate) {
  1424. stop_capture(gc);
  1425. }
  1426. if (gc->active && !gc->hook_ready && gc->process_id) {
  1427. gc->hook_ready = open_event_gc(gc, EVENT_HOOK_READY);
  1428. }
  1429. if (gc->injector_process && object_signalled(gc->injector_process)) {
  1430. DWORD exit_code = 0;
  1431. GetExitCodeProcess(gc->injector_process, &exit_code);
  1432. close_handle(&gc->injector_process);
  1433. if (exit_code != 0) {
  1434. warn("inject process failed: %ld", (long)exit_code);
  1435. gc->error_acquiring = true;
  1436. } else if (!gc->capturing) {
  1437. gc->retry_interval =
  1438. ERROR_RETRY_INTERVAL *
  1439. hook_rate_to_float(gc->config.hook_rate);
  1440. stop_capture(gc);
  1441. }
  1442. }
  1443. if (gc->hook_ready && object_signalled(gc->hook_ready)) {
  1444. debug("capture initializing!");
  1445. enum capture_result result = init_capture_data(gc);
  1446. if (result == CAPTURE_SUCCESS)
  1447. gc->capturing = start_capture(gc);
  1448. else
  1449. debug("init_capture_data failed");
  1450. if (result != CAPTURE_RETRY && !gc->capturing) {
  1451. gc->retry_interval =
  1452. ERROR_RETRY_INTERVAL *
  1453. hook_rate_to_float(gc->config.hook_rate);
  1454. stop_capture(gc);
  1455. }
  1456. }
  1457. gc->retry_time += seconds;
  1458. if (!gc->active) {
  1459. if (!gc->error_acquiring &&
  1460. gc->retry_time > gc->retry_interval) {
  1461. if (gc->config.mode == CAPTURE_MODE_ANY ||
  1462. gc->activate_hook) {
  1463. try_hook(gc);
  1464. gc->retry_time = 0.0f;
  1465. }
  1466. }
  1467. } else {
  1468. if (!capture_valid(gc)) {
  1469. info("capture window no longer exists, "
  1470. "terminating capture");
  1471. stop_capture(gc);
  1472. } else {
  1473. if (gc->copy_texture) {
  1474. obs_enter_graphics();
  1475. gc->copy_texture(gc);
  1476. obs_leave_graphics();
  1477. }
  1478. if (gc->config.cursor) {
  1479. check_foreground_window(gc, seconds);
  1480. obs_enter_graphics();
  1481. cursor_capture(&gc->cursor_data);
  1482. obs_leave_graphics();
  1483. }
  1484. gc->fps_reset_time += seconds;
  1485. if (gc->fps_reset_time >= gc->retry_interval) {
  1486. reset_frame_interval(gc);
  1487. gc->fps_reset_time = 0.0f;
  1488. }
  1489. }
  1490. }
  1491. if (!gc->showing)
  1492. gc->showing = true;
  1493. }
  1494. static inline void game_capture_render_cursor(struct game_capture *gc)
  1495. {
  1496. POINT p = {0};
  1497. HWND window;
  1498. if (!gc->global_hook_info->cx || !gc->global_hook_info->cy)
  1499. return;
  1500. window = !!gc->global_hook_info->window
  1501. ? (HWND)(uintptr_t)gc->global_hook_info->window
  1502. : gc->window;
  1503. ClientToScreen(window, &p);
  1504. cursor_draw(&gc->cursor_data, -p.x, -p.y, gc->global_hook_info->cx,
  1505. gc->global_hook_info->cy);
  1506. }
  1507. static void game_capture_render(void *data, gs_effect_t *effect)
  1508. {
  1509. struct game_capture *gc = data;
  1510. if (!gc->texture || !gc->active)
  1511. return;
  1512. effect = obs_get_base_effect(gc->config.allow_transparency
  1513. ? OBS_EFFECT_DEFAULT
  1514. : OBS_EFFECT_OPAQUE);
  1515. while (gs_effect_loop(effect, "Draw")) {
  1516. obs_source_draw(gc->texture, 0, 0, 0, 0,
  1517. gc->global_hook_info->flip);
  1518. if (gc->config.allow_transparency && gc->config.cursor &&
  1519. !gc->cursor_hidden) {
  1520. game_capture_render_cursor(gc);
  1521. }
  1522. }
  1523. if (!gc->config.allow_transparency && gc->config.cursor &&
  1524. !gc->cursor_hidden) {
  1525. effect = obs_get_base_effect(OBS_EFFECT_DEFAULT);
  1526. while (gs_effect_loop(effect, "Draw")) {
  1527. game_capture_render_cursor(gc);
  1528. }
  1529. }
  1530. }
  1531. static uint32_t game_capture_width(void *data)
  1532. {
  1533. struct game_capture *gc = data;
  1534. return gc->active ? gc->cx : 0;
  1535. }
  1536. static uint32_t game_capture_height(void *data)
  1537. {
  1538. struct game_capture *gc = data;
  1539. return gc->active ? gc->cy : 0;
  1540. }
  1541. static const char *game_capture_name(void *unused)
  1542. {
  1543. UNUSED_PARAMETER(unused);
  1544. return TEXT_GAME_CAPTURE;
  1545. }
  1546. static void game_capture_defaults(obs_data_t *settings)
  1547. {
  1548. obs_data_set_default_string(settings, SETTING_MODE, SETTING_MODE_ANY);
  1549. obs_data_set_default_int(settings, SETTING_WINDOW_PRIORITY,
  1550. (int)WINDOW_PRIORITY_EXE);
  1551. obs_data_set_default_bool(settings, SETTING_COMPATIBILITY, false);
  1552. obs_data_set_default_bool(settings, SETTING_CURSOR, true);
  1553. obs_data_set_default_bool(settings, SETTING_TRANSPARENCY, false);
  1554. obs_data_set_default_bool(settings, SETTING_LIMIT_FRAMERATE, false);
  1555. obs_data_set_default_bool(settings, SETTING_CAPTURE_OVERLAYS, false);
  1556. obs_data_set_default_bool(settings, SETTING_ANTI_CHEAT_HOOK, true);
  1557. obs_data_set_default_int(settings, SETTING_HOOK_RATE,
  1558. (int)HOOK_RATE_NORMAL);
  1559. }
  1560. static bool mode_callback(obs_properties_t *ppts, obs_property_t *p,
  1561. obs_data_t *settings)
  1562. {
  1563. bool capture_window;
  1564. if (using_older_non_mode_format(settings)) {
  1565. capture_window =
  1566. !obs_data_get_bool(settings, SETTING_ANY_FULLSCREEN);
  1567. } else {
  1568. const char *mode = obs_data_get_string(settings, SETTING_MODE);
  1569. capture_window = strcmp(mode, SETTING_MODE_WINDOW) == 0;
  1570. }
  1571. p = obs_properties_get(ppts, SETTING_CAPTURE_WINDOW);
  1572. obs_property_set_visible(p, capture_window);
  1573. p = obs_properties_get(ppts, SETTING_WINDOW_PRIORITY);
  1574. obs_property_set_visible(p, capture_window);
  1575. return true;
  1576. }
  1577. static void insert_preserved_val(obs_property_t *p, const char *val, size_t idx)
  1578. {
  1579. char *class = NULL;
  1580. char *title = NULL;
  1581. char *executable = NULL;
  1582. struct dstr desc = {0};
  1583. build_window_strings(val, &class, &title, &executable);
  1584. dstr_printf(&desc, "[%s]: %s", executable, title);
  1585. obs_property_list_insert_string(p, idx, desc.array, val);
  1586. obs_property_list_item_disable(p, idx, true);
  1587. dstr_free(&desc);
  1588. bfree(class);
  1589. bfree(title);
  1590. bfree(executable);
  1591. }
  1592. bool check_window_property_setting(obs_properties_t *ppts, obs_property_t *p,
  1593. obs_data_t *settings, const char *val,
  1594. size_t idx)
  1595. {
  1596. const char *cur_val;
  1597. bool match = false;
  1598. size_t i = 0;
  1599. cur_val = obs_data_get_string(settings, val);
  1600. if (!cur_val) {
  1601. return false;
  1602. }
  1603. for (;;) {
  1604. const char *val = obs_property_list_item_string(p, i++);
  1605. if (!val)
  1606. break;
  1607. if (strcmp(val, cur_val) == 0) {
  1608. match = true;
  1609. break;
  1610. }
  1611. }
  1612. if (cur_val && *cur_val && !match) {
  1613. insert_preserved_val(p, cur_val, idx);
  1614. return true;
  1615. }
  1616. UNUSED_PARAMETER(ppts);
  1617. return false;
  1618. }
  1619. static bool window_changed_callback(obs_properties_t *ppts, obs_property_t *p,
  1620. obs_data_t *settings)
  1621. {
  1622. return check_window_property_setting(ppts, p, settings,
  1623. SETTING_CAPTURE_WINDOW, 1);
  1624. }
  1625. static BOOL CALLBACK EnumFirstMonitor(HMONITOR monitor, HDC hdc, LPRECT rc,
  1626. LPARAM data)
  1627. {
  1628. *(HMONITOR *)data = monitor;
  1629. UNUSED_PARAMETER(hdc);
  1630. UNUSED_PARAMETER(rc);
  1631. return false;
  1632. }
  1633. static bool window_not_blacklisted(const char *title, const char *class,
  1634. const char *exe)
  1635. {
  1636. UNUSED_PARAMETER(title);
  1637. UNUSED_PARAMETER(class);
  1638. return !is_blacklisted_exe(exe);
  1639. }
  1640. static obs_properties_t *game_capture_properties(void *data)
  1641. {
  1642. HMONITOR monitor;
  1643. uint32_t cx = 1920;
  1644. uint32_t cy = 1080;
  1645. /* scaling is free form, this is mostly just to provide some common
  1646. * values */
  1647. bool success = !!EnumDisplayMonitors(NULL, NULL, EnumFirstMonitor,
  1648. (LPARAM)&monitor);
  1649. if (success) {
  1650. MONITORINFO mi = {0};
  1651. mi.cbSize = sizeof(mi);
  1652. if (!!GetMonitorInfo(monitor, &mi)) {
  1653. cx = (uint32_t)(mi.rcMonitor.right - mi.rcMonitor.left);
  1654. cy = (uint32_t)(mi.rcMonitor.bottom - mi.rcMonitor.top);
  1655. }
  1656. }
  1657. /* update from deprecated settings */
  1658. if (data) {
  1659. struct game_capture *gc = data;
  1660. obs_data_t *settings = obs_source_get_settings(gc->source);
  1661. if (using_older_non_mode_format(settings)) {
  1662. bool any = obs_data_get_bool(settings,
  1663. SETTING_ANY_FULLSCREEN);
  1664. const char *mode = any ? SETTING_MODE_ANY
  1665. : SETTING_MODE_WINDOW;
  1666. obs_data_set_string(settings, SETTING_MODE, mode);
  1667. }
  1668. obs_data_release(settings);
  1669. }
  1670. obs_properties_t *ppts = obs_properties_create();
  1671. obs_property_t *p;
  1672. p = obs_properties_add_list(ppts, SETTING_MODE, TEXT_MODE,
  1673. OBS_COMBO_TYPE_LIST,
  1674. OBS_COMBO_FORMAT_STRING);
  1675. obs_property_list_add_string(p, TEXT_MODE_ANY, SETTING_MODE_ANY);
  1676. obs_property_list_add_string(p, TEXT_MODE_WINDOW, SETTING_MODE_WINDOW);
  1677. obs_property_list_add_string(p, TEXT_MODE_HOTKEY, SETTING_MODE_HOTKEY);
  1678. obs_property_set_modified_callback(p, mode_callback);
  1679. p = obs_properties_add_list(ppts, SETTING_CAPTURE_WINDOW, TEXT_WINDOW,
  1680. OBS_COMBO_TYPE_LIST,
  1681. OBS_COMBO_FORMAT_STRING);
  1682. obs_property_list_add_string(p, "", "");
  1683. fill_window_list(p, INCLUDE_MINIMIZED, window_not_blacklisted);
  1684. obs_property_set_modified_callback(p, window_changed_callback);
  1685. p = obs_properties_add_list(ppts, SETTING_WINDOW_PRIORITY,
  1686. TEXT_MATCH_PRIORITY, OBS_COMBO_TYPE_LIST,
  1687. OBS_COMBO_FORMAT_INT);
  1688. obs_property_list_add_int(p, TEXT_MATCH_TITLE, WINDOW_PRIORITY_TITLE);
  1689. obs_property_list_add_int(p, TEXT_MATCH_CLASS, WINDOW_PRIORITY_CLASS);
  1690. obs_property_list_add_int(p, TEXT_MATCH_EXE, WINDOW_PRIORITY_EXE);
  1691. obs_properties_add_bool(ppts, SETTING_COMPATIBILITY,
  1692. TEXT_SLI_COMPATIBILITY);
  1693. obs_properties_add_bool(ppts, SETTING_TRANSPARENCY,
  1694. TEXT_ALLOW_TRANSPARENCY);
  1695. obs_properties_add_bool(ppts, SETTING_LIMIT_FRAMERATE,
  1696. TEXT_LIMIT_FRAMERATE);
  1697. obs_properties_add_bool(ppts, SETTING_CURSOR, TEXT_CAPTURE_CURSOR);
  1698. obs_properties_add_bool(ppts, SETTING_ANTI_CHEAT_HOOK,
  1699. TEXT_ANTI_CHEAT_HOOK);
  1700. obs_properties_add_bool(ppts, SETTING_CAPTURE_OVERLAYS,
  1701. TEXT_CAPTURE_OVERLAYS);
  1702. p = obs_properties_add_list(ppts, SETTING_HOOK_RATE, TEXT_HOOK_RATE,
  1703. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  1704. obs_property_list_add_int(p, TEXT_HOOK_RATE_SLOW, HOOK_RATE_SLOW);
  1705. obs_property_list_add_int(p, TEXT_HOOK_RATE_NORMAL, HOOK_RATE_NORMAL);
  1706. obs_property_list_add_int(p, TEXT_HOOK_RATE_FAST, HOOK_RATE_FAST);
  1707. obs_property_list_add_int(p, TEXT_HOOK_RATE_FASTEST, HOOK_RATE_FASTEST);
  1708. UNUSED_PARAMETER(data);
  1709. return ppts;
  1710. }
  1711. struct obs_source_info game_capture_info = {
  1712. .id = "game_capture",
  1713. .type = OBS_SOURCE_TYPE_INPUT,
  1714. .output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_CUSTOM_DRAW |
  1715. OBS_SOURCE_DO_NOT_DUPLICATE,
  1716. .get_name = game_capture_name,
  1717. .create = game_capture_create,
  1718. .destroy = game_capture_destroy,
  1719. .get_width = game_capture_width,
  1720. .get_height = game_capture_height,
  1721. .get_defaults = game_capture_defaults,
  1722. .get_properties = game_capture_properties,
  1723. .update = game_capture_update,
  1724. .video_tick = game_capture_tick,
  1725. .video_render = game_capture_render,
  1726. .icon_type = OBS_ICON_TYPE_GAME_CAPTURE,
  1727. };