record_desktop_mag.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #pragma once
  2. // documents
  3. // https://learn.microsoft.com/en-us/previous-versions/windows/desktop/magapi/magapi-intro
  4. // https://learn.microsoft.com/en-us/previous-versions/windows/desktop/magapi/magapi-whats-new
  5. // https://github.com/microsoft/Windows-classic-samples/blob/main/Samples/Magnification/cpp/Windowed/MagnifierSample.cpp
  6. #include <magnification.h>
  7. #include "record_desktop.h"
  8. namespace am {
  9. class record_desktop_mag : public record_desktop
  10. {
  11. typedef BOOL(WINAPI *MagImageScalingCallback)(HWND hwnd,
  12. void *srcdata,
  13. MAGIMAGEHEADER srcheader,
  14. void *destdata,
  15. MAGIMAGEHEADER destheader,
  16. RECT unclipped,
  17. RECT clipped,
  18. HRGN dirty);
  19. typedef BOOL(WINAPI *MagInitializeFunc)(void);
  20. typedef BOOL(WINAPI *MagUninitializeFunc)(void);
  21. typedef BOOL(WINAPI *MagSetWindowSourceFunc)(HWND hwnd, RECT rect);
  22. typedef BOOL(WINAPI *MagSetWindowFilterListFunc)(HWND hwnd,
  23. DWORD dwFilterMode,
  24. int count,
  25. HWND *pHWND);
  26. typedef BOOL(WINAPI *MagSetImageScalingCallbackFunc)(HWND hwnd,
  27. MagImageScalingCallback callback);
  28. static BOOL WINAPI on_mag_scaling_callback(HWND hwnd,
  29. void *srcdata,
  30. MAGIMAGEHEADER srcheader,
  31. void *destdata,
  32. MAGIMAGEHEADER destheader,
  33. RECT unclipped,
  34. RECT clipped,
  35. HRGN dirty);
  36. public:
  37. record_desktop_mag();
  38. ~record_desktop_mag() override;
  39. int init(const RECORD_DESKTOP_RECT &rect, const int fps) override;
  40. int start() override;
  41. int pause() override;
  42. int resume() override;
  43. int stop() override;
  44. void set_exclude(HWND excluded_window);
  45. protected:
  46. void clean_up() override;
  47. private:
  48. void record_func();
  49. void do_sleep(int64_t dur, int64_t pre, int64_t now);
  50. bool do_mag_initialize();
  51. int do_mag_record();
  52. void on_mag_data(void *data, const MAGIMAGEHEADER &header);
  53. bool seh_mag_set_window_source(HWND hwnd, RECT rect, DWORD &exception);
  54. private:
  55. uint32_t _width = 0;
  56. uint32_t _height = 0;
  57. int64_t _current_pts = -1;
  58. // Used to exclude window with specified window id.
  59. HWND _excluded_window = NULL;
  60. // Used for getting the screen dpi.
  61. HDC _desktop_dc = NULL;
  62. // Module handler
  63. HMODULE _mag_lib_handle = NULL;
  64. // Mag functions
  65. MagInitializeFunc _mag_initialize_func = nullptr;
  66. MagUninitializeFunc _mag_uninitialize_func = nullptr;
  67. MagSetWindowSourceFunc _mag_set_window_source_func = nullptr;
  68. MagSetWindowFilterListFunc _mag_set_window_filter_list_func = nullptr;
  69. MagSetImageScalingCallbackFunc _mag_set_image_scaling_callback_func = nullptr;
  70. // The hidden window hosting the magnifier control.
  71. HWND _host_window = NULL;
  72. // The magnifier control that captures the screen.
  73. HWND _magnifier_window = NULL;
  74. // True if the magnifier control has been successfully initialized.
  75. bool _magnifier_initialized = false;
  76. // True if the last OnMagImageScalingCallback was called and handled
  77. // successfully. Reset at the beginning of each CaptureImage call.
  78. bool _magnifier_capture_succeeded = true;
  79. };
  80. } // namespace am