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 #include "webrtc/modules/desktop_capture/win/screen_capture_utils.h"
12
13 #include <assert.h>
14 #include <windows.h>
15
16 namespace webrtc {
17
GetScreenList(ScreenCapturer::ScreenList * screens)18 bool GetScreenList(ScreenCapturer::ScreenList* screens) {
19 assert(screens->size() == 0);
20
21 BOOL enum_result = TRUE;
22 for (int device_index = 0;; ++device_index) {
23 DISPLAY_DEVICE device;
24 device.cb = sizeof(device);
25 enum_result = EnumDisplayDevices(NULL, device_index, &device, 0);
26
27 // |enum_result| is 0 if we have enumerated all devices.
28 if (!enum_result)
29 break;
30
31 // We only care about active displays.
32 if (!(device.StateFlags & DISPLAY_DEVICE_ACTIVE))
33 continue;
34
35 ScreenCapturer::Screen screen;
36 screen.id = device_index;
37 screens->push_back(screen);
38 }
39 return true;
40 }
41
IsScreenValid(ScreenId screen,std::wstring * device_key)42 bool IsScreenValid(ScreenId screen, std::wstring* device_key) {
43 if (screen == kFullDesktopScreenId) {
44 *device_key = L"";
45 return true;
46 }
47
48 DISPLAY_DEVICE device;
49 device.cb = sizeof(device);
50 BOOL enum_result = EnumDisplayDevices(NULL, screen, &device, 0);
51 if (enum_result)
52 *device_key = device.DeviceKey;
53
54 return !!enum_result;
55 }
56
GetScreenRect(ScreenId screen,const std::wstring & device_key)57 DesktopRect GetScreenRect(ScreenId screen, const std::wstring& device_key) {
58 if (screen == kFullDesktopScreenId) {
59 return DesktopRect::MakeXYWH(GetSystemMetrics(SM_XVIRTUALSCREEN),
60 GetSystemMetrics(SM_YVIRTUALSCREEN),
61 GetSystemMetrics(SM_CXVIRTUALSCREEN),
62 GetSystemMetrics(SM_CYVIRTUALSCREEN));
63 }
64
65 DISPLAY_DEVICE device;
66 device.cb = sizeof(device);
67 BOOL result = EnumDisplayDevices(NULL, screen, &device, 0);
68 if (!result)
69 return DesktopRect();
70
71 // Verifies the device index still maps to the same display device, to make
72 // sure we are capturing the same device when devices are added or removed.
73 // DeviceKey is documented as reserved, but it actually contains the registry
74 // key for the device and is unique for each monitor, while DeviceID is not.
75 if (device_key != device.DeviceKey)
76 return DesktopRect();
77
78 DEVMODE device_mode;
79 device_mode.dmSize = sizeof(device_mode);
80 device_mode.dmDriverExtra = 0;
81 result = EnumDisplaySettingsEx(
82 device.DeviceName, ENUM_CURRENT_SETTINGS, &device_mode, 0);
83 if (!result)
84 return DesktopRect();
85
86 return DesktopRect::MakeXYWH(device_mode.dmPosition.x,
87 device_mode.dmPosition.y,
88 device_mode.dmPelsWidth,
89 device_mode.dmPelsHeight);
90 }
91
92 } // namespace webrtc
93