Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(250)

Side by Side Diff: ui/gfx/win/physical_size.cc

Issue 1563183008: Added capability on Windows to get the physical dimensions of your attached monitors. Also added th… (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix gn build and applied guid brace warning fix Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/gfx/win/physical_size.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "ui/gfx/win/physical_size.h"
6
7 #include <windows.h>
8 #include <setupapi.h>
9
10 #include <iostream>
11
12 #include "base/logging.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/scoped_generic.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "base/win/registry.h"
17
18 // This GUID {E6F07B5F-EE97-4A90-B076-33F57BF4EAA7} was taken from
19 // https://msdn.microsoft.com/en-us/library/windows/hardware/ff545901.aspx
20 const GUID GUID_DEVICEINTERFACE_MONITOR = {
21 0xE6F07B5F,
22 0xEE97,
23 0x4A90,
24 {0xB0, 0x76, 0x33, 0xF5, 0x7B, 0xF4, 0xEA, 0xA7}};
25
26 namespace {
27
28 struct DeviceInfoListScopedTraits {
29 static HDEVINFO InvalidValue() { return INVALID_HANDLE_VALUE; }
30
31 static void Free(HDEVINFO h) { SetupDiDestroyDeviceInfoList(h); }
32 };
33
34 bool GetSizeFromRegistry(HDEVINFO device_info_list,
35 SP_DEVINFO_DATA* device_info,
36 int* width_mm,
37 int* height_mm) {
38 base::win::RegKey reg_key(SetupDiOpenDevRegKey(
39 device_info_list, device_info, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_READ));
40 if (!reg_key.Valid())
41 return false;
42
43 BYTE data[128]; // EDID block is exactly 128 bytes long.
44 ZeroMemory(&data[0], sizeof(data));
45 DWORD data_length = sizeof(data);
46 LONG return_value =
47 reg_key.ReadValue(L"EDID", &data[0], &data_length, nullptr);
48 if (return_value != ERROR_SUCCESS)
49 return false;
50
51 // Byte 54 is the start of the first descriptor block, which contains the
52 // required timing information with the highest preference, and 12 bytes
53 // into that block is the size information.
54 // 66: width least significant bits
55 // 67: height least significant bits
56 // 68: 4 bits for each of width and height most significant bits
57 if (data[54] == 0)
58 return false;
59 const int w = ((data[68] & 0xF0) << 4) + data[66];
60 const int h = ((data[68] & 0x0F) << 8) + data[67];
61
62 if (w <= 0 || h <= 0)
63 return false;
64
65 *width_mm = w;
66 *height_mm = h;
67
68 return true;
69 }
70
71 bool GetInterfaceDetailAndDeviceInfo(
72 HDEVINFO device_info_list,
73 SP_DEVICE_INTERFACE_DATA* interface_data,
74 scoped_ptr<SP_DEVICE_INTERFACE_DETAIL_DATA, base::FreeDeleter>*
75 interface_detail,
76 SP_DEVINFO_DATA* device_info) {
77 DCHECK_EQ(sizeof(*device_info), device_info->cbSize);
78 DWORD buffer_size;
79 // This call populates device_info. It will also fail, but if the error is
80 // "insufficient buffer" then it will set buffer_size and we can call again
81 // with an allocated buffer.
82 SetupDiGetDeviceInterfaceDetail(device_info_list, interface_data, nullptr, 0,
83 &buffer_size, device_info);
84 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
85 return false;
86
87 interface_detail->reset(
88 reinterpret_cast<SP_DEVICE_INTERFACE_DETAIL_DATA*>(malloc(buffer_size)));
89 (*interface_detail)->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
90 return SetupDiGetDeviceInterfaceDetail(device_info_list, interface_data,
91 interface_detail->get(), buffer_size,
92 nullptr, nullptr) != 0;
93 }
94
95 } // namespace
96
97 namespace gfx {
98
99 // The physical size information is only available by looking in the EDID block
100 // via setup. However setup has the device path and not the device name that we
101 // use to identify displays. Therefore after looking up a device via setup we
102 // need to find the display again via EnumDisplayDevices (matching device path
103 // to the device ID of the display's interface) so we can return the device name
104 // (available from the interface's attached monitor).
105 std::vector<PhysicalDisplaySize> GetPhysicalSizeForDisplays() {
106 std::vector<PhysicalDisplaySize> out;
107
108 base::ScopedGeneric<HDEVINFO, DeviceInfoListScopedTraits> device_info_list(
109 SetupDiGetClassDevs(&GUID_DEVICEINTERFACE_MONITOR, nullptr, nullptr,
110 DIGCF_PRESENT | DIGCF_DEVICEINTERFACE));
111
112 if (!device_info_list.is_valid())
113 return out;
114
115 SP_DEVICE_INTERFACE_DATA interface_data = {};
116 interface_data.cbSize = sizeof(interface_data);
117 int interface_index = 0;
118 while (SetupDiEnumDeviceInterfaces(device_info_list.get(), nullptr,
119 &GUID_DEVICEINTERFACE_MONITOR,
120 interface_index++, &interface_data)) {
121 scoped_ptr<SP_DEVICE_INTERFACE_DETAIL_DATA, base::FreeDeleter>
122 interface_detail;
123 SP_DEVINFO_DATA device_info = {};
124 device_info.cbSize = sizeof(device_info);
125 bool get_info_succeeded =
126 GetInterfaceDetailAndDeviceInfo(device_info_list.get(), &interface_data,
127 &interface_detail, &device_info);
128 if (!get_info_succeeded)
129 continue;
130
131 DISPLAY_DEVICE display_device = {};
132 display_device.cb = sizeof(display_device);
133 int display_index = 0;
134 while (EnumDisplayDevices(nullptr, display_index++, &display_device,
135 EDD_GET_DEVICE_INTERFACE_NAME)) {
136 DISPLAY_DEVICE attached_device = {};
137 attached_device.cb = sizeof(attached_device);
138 int attached_index = 0;
139 while (EnumDisplayDevices(display_device.DeviceName, attached_index++,
140 &attached_device,
141 EDD_GET_DEVICE_INTERFACE_NAME)) {
142 wchar_t* attached_device_id = attached_device.DeviceID;
143 wchar_t* setup_device_path = interface_detail->DevicePath;
144 if (wcsicmp(attached_device_id, setup_device_path) == 0) {
145 int width_mm;
146 int height_mm;
147 bool found = GetSizeFromRegistry(device_info_list.get(), &device_info,
148 &width_mm, &height_mm);
149 if (found) {
150 out.push_back(
151 PhysicalDisplaySize(base::WideToUTF8(display_device.DeviceName),
152 width_mm, height_mm));
153 }
154 break;
155 }
156 }
157 }
158 }
159 return out;
160 }
161
162 } // namespace gfx
OLDNEW
« no previous file with comments | « ui/gfx/win/physical_size.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698