plugin-main.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #include <windows.h>
  2. #include <obs-module.h>
  3. #include <util/windows/win-version.h>
  4. #include <util/platform.h>
  5. OBS_DECLARE_MODULE()
  6. OBS_MODULE_USE_DEFAULT_LOCALE("win-capture", "en-US")
  7. MODULE_EXPORT const char *obs_module_description(void)
  8. {
  9. return "Windows game/screen/window capture";
  10. }
  11. extern struct obs_source_info duplicator_capture_info;
  12. extern struct obs_source_info monitor_capture_info;
  13. extern struct obs_source_info window_capture_info;
  14. extern struct obs_source_info game_capture_info;
  15. static HANDLE init_hooks_thread = NULL;
  16. extern bool cached_versions_match(void);
  17. extern bool load_cached_graphics_offsets(bool is32bit, const char *config_path);
  18. extern bool load_graphics_offsets(bool is32bit, const char *config_path);
  19. /* temporary, will eventually be erased once we figure out how to create both
  20. * 32bit and 64bit versions of the helpers/hook */
  21. #ifdef _WIN64
  22. #define IS32BIT false
  23. #else
  24. #define IS32BIT true
  25. #endif
  26. /* note, need to enable cache writing in load-graphics-offsets.c if you turn
  27. * this back on*/
  28. #define USE_HOOK_ADDRESS_CACHE false
  29. static DWORD WINAPI init_hooks(LPVOID param)
  30. {
  31. char *config_path = param;
  32. if (USE_HOOK_ADDRESS_CACHE && cached_versions_match() &&
  33. load_cached_graphics_offsets(IS32BIT, config_path)) {
  34. load_cached_graphics_offsets(!IS32BIT, config_path);
  35. obs_register_source(&game_capture_info);
  36. } else if (load_graphics_offsets(IS32BIT, config_path)) {
  37. load_graphics_offsets(!IS32BIT, config_path);
  38. }
  39. bfree(config_path);
  40. return 0;
  41. }
  42. void wait_for_hook_initialization(void)
  43. {
  44. static bool initialized = false;
  45. if (!initialized) {
  46. if (init_hooks_thread) {
  47. WaitForSingleObject(init_hooks_thread, INFINITE);
  48. CloseHandle(init_hooks_thread);
  49. init_hooks_thread = NULL;
  50. }
  51. initialized = true;
  52. }
  53. }
  54. void init_hook_files(void);
  55. bool obs_module_load(void)
  56. {
  57. struct win_version_info ver;
  58. bool win8_or_above = false;
  59. char *config_dir;
  60. config_dir = obs_module_config_path(NULL);
  61. if (config_dir) {
  62. os_mkdirs(config_dir);
  63. bfree(config_dir);
  64. }
  65. get_win_ver(&ver);
  66. win8_or_above = ver.major > 6 || (ver.major == 6 && ver.minor >= 2);
  67. obs_enter_graphics();
  68. if (win8_or_above && gs_get_device_type() == GS_DEVICE_DIRECT3D_11)
  69. obs_register_source(&duplicator_capture_info);
  70. else
  71. obs_register_source(&monitor_capture_info);
  72. obs_leave_graphics();
  73. obs_register_source(&window_capture_info);
  74. char *config_path = obs_module_config_path(NULL);
  75. init_hook_files();
  76. init_hooks_thread =
  77. CreateThread(NULL, 0, init_hooks, config_path, 0, NULL);
  78. obs_register_source(&game_capture_info);
  79. return true;
  80. }
  81. void obs_module_unload(void)
  82. {
  83. wait_for_hook_initialization();
  84. }