xref: /aosp_15_r20/external/webrtc/modules/desktop_capture/win/window_capture_utils.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #ifndef MODULES_DESKTOP_CAPTURE_WIN_WINDOW_CAPTURE_UTILS_H_
12 #define MODULES_DESKTOP_CAPTURE_WIN_WINDOW_CAPTURE_UTILS_H_
13 
14 #include <shlobj.h>
15 #include <windows.h>
16 #include <wrl/client.h>
17 
18 #include "modules/desktop_capture/desktop_capturer.h"
19 #include "modules/desktop_capture/desktop_geometry.h"
20 
21 namespace webrtc {
22 
23 // Outputs the window rect. The returned DesktopRect is in system coordinates,
24 // i.e. the primary monitor on the system always starts from (0, 0). This
25 // function returns false if native APIs fail.
26 bool GetWindowRect(HWND window, DesktopRect* result);
27 
28 // Outputs the window rect, with the left/right/bottom frame border cropped if
29 // the window is maximized or has a transparent resize border.
30 // `avoid_cropping_border` may be set to true to avoid cropping the visible
31 // border when cropping any resize border.
32 // `cropped_rect` is the cropped rect relative to the
33 // desktop. `original_rect` is the original rect returned from GetWindowRect.
34 // Returns true if all API calls succeeded. The returned DesktopRect is in
35 // system coordinates, i.e. the primary monitor on the system always starts from
36 // (0, 0). `original_rect` can be nullptr.
37 //
38 // TODO(zijiehe): Move this function to CroppingWindowCapturerWin after it has
39 // been removed from MouseCursorMonitorWin.
40 // This function should only be used by CroppingWindowCapturerWin. Instead a
41 // DesktopRect CropWindowRect(const DesktopRect& rect)
42 // should be added as a utility function to help CroppingWindowCapturerWin and
43 // WindowCapturerWinGdi to crop out the borders or shadow according to their
44 // scenarios. But this function is too generic and easy to be misused.
45 bool GetCroppedWindowRect(HWND window,
46                           bool avoid_cropping_border,
47                           DesktopRect* cropped_rect,
48                           DesktopRect* original_rect);
49 
50 // Retrieves the rectangle of the content area of `window`. Usually it contains
51 // title bar and window client area, but borders or shadow are excluded. The
52 // returned DesktopRect is in system coordinates, i.e. the primary monitor on
53 // the system always starts from (0, 0). This function returns false if native
54 // APIs fail.
55 bool GetWindowContentRect(HWND window, DesktopRect* result);
56 
57 // Returns the region type of the `window` and fill `rect` with the region of
58 // `window` if region type is SIMPLEREGION.
59 int GetWindowRegionTypeWithBoundary(HWND window, DesktopRect* result);
60 
61 // Retrieves the size of the `hdc`. This function returns false if native APIs
62 // fail.
63 bool GetDcSize(HDC hdc, DesktopSize* size);
64 
65 // Retrieves whether the `window` is maximized and stores in `result`. This
66 // function returns false if native APIs fail.
67 bool IsWindowMaximized(HWND window, bool* result);
68 
69 // Checks that the HWND is for a valid window, that window's visibility state is
70 // visible, and that it is not minimized.
71 bool IsWindowValidAndVisible(HWND window);
72 
73 // Checks if a window responds to a message within 50ms.
74 bool IsWindowResponding(HWND window);
75 
76 enum GetWindowListFlags {
77   kNone = 0x00,
78   kIgnoreUntitled = 1 << 0,
79   kIgnoreUnresponsive = 1 << 1,
80   kIgnoreCurrentProcessWindows = 1 << 2,
81 };
82 
83 // Retrieves the list of top-level windows on the screen.
84 // Some windows will be ignored:
85 // - Those that are invisible or minimized.
86 // - Program Manager & Start menu.
87 // - [with kIgnoreUntitled] windows with no title.
88 // - [with kIgnoreUnresponsive] windows that are unresponsive.
89 // - [with kIgnoreCurrentProcessWindows] windows owned by the current process.
90 // - Any windows with extended styles that match `ex_style_filters`.
91 // Returns false if native APIs failed.
92 bool GetWindowList(int flags,
93                    DesktopCapturer::SourceList* windows,
94                    LONG ex_style_filters = 0);
95 
96 typedef HRESULT(WINAPI* DwmIsCompositionEnabledFunc)(BOOL* enabled);
97 typedef HRESULT(WINAPI* DwmGetWindowAttributeFunc)(HWND hwnd,
98                                                    DWORD flag,
99                                                    PVOID result_ptr,
100                                                    DWORD result_size);
101 class WindowCaptureHelperWin {
102  public:
103   WindowCaptureHelperWin();
104   ~WindowCaptureHelperWin();
105 
106   WindowCaptureHelperWin(const WindowCaptureHelperWin&) = delete;
107   WindowCaptureHelperWin& operator=(const WindowCaptureHelperWin&) = delete;
108 
109   bool IsAeroEnabled();
110   bool IsWindowChromeNotification(HWND hwnd);
111   bool AreWindowsOverlapping(HWND hwnd,
112                              HWND selected_hwnd,
113                              const DesktopRect& selected_window_rect);
114   bool IsWindowOnCurrentDesktop(HWND hwnd);
115   bool IsWindowVisibleOnCurrentDesktop(HWND hwnd);
116   bool IsWindowCloaked(HWND hwnd);
117 
118   // The optional `ex_style_filters` parameter allows callers to provide
119   // extended window styles (e.g. WS_EX_TOOLWINDOW) and prevent windows that
120   // match from being included in `results`.
121   bool EnumerateCapturableWindows(DesktopCapturer::SourceList* results,
122                                   bool enumerate_current_process_windows,
123                                   LONG ex_style_filters = 0);
124 
125  private:
126   HMODULE dwmapi_library_ = nullptr;
127   DwmIsCompositionEnabledFunc func_ = nullptr;
128   DwmGetWindowAttributeFunc dwm_get_window_attribute_func_ = nullptr;
129 
130   // Only used on Win10+.
131   Microsoft::WRL::ComPtr<IVirtualDesktopManager> virtual_desktop_manager_;
132 };
133 
134 }  // namespace webrtc
135 
136 #endif  // MODULES_DESKTOP_CAPTURE_WIN_WINDOW_CAPTURE_UTILS_H_
137