OLD | NEW |
---|---|
(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, 0xEE97, 0x4A90, 0xB0, 0x76, 0x33, 0xF5, 0x7B, 0xF4, 0xEA, 0xA7}; | |
22 | |
23 namespace { | |
24 | |
25 struct DeviceInfoListScopedTraits { | |
26 static HDEVINFO InvalidValue() { return INVALID_HANDLE_VALUE; } | |
27 | |
28 static void Free(HDEVINFO h) { SetupDiDestroyDeviceInfoList(h); } | |
29 }; | |
30 | |
31 bool GetSizeFromRegistry(HDEVINFO device_info_list, | |
32 SP_DEVINFO_DATA* device_info, | |
33 int* width_mm, | |
34 int* height_mm) { | |
35 base::win::RegKey reg_key(SetupDiOpenDevRegKey( | |
36 device_info_list, device_info, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_READ)); | |
37 if (!reg_key.Valid()) | |
38 return false; | |
39 | |
40 BYTE data[128]; // EDID block is exactly 128 bytes long. | |
41 ZeroMemory(&data[0], sizeof(data)); | |
42 DWORD data_length = sizeof(data); | |
43 LONG return_value = | |
44 reg_key.ReadValue(L"EDID", &data[0], &data_length, nullptr); | |
45 if (return_value != ERROR_SUCCESS) | |
46 return false; | |
47 | |
48 // Byte 54 is the start of the first descriptor block, which contains the | |
49 // required timing information with the highest preference, and 12 bytes | |
50 // into that block is the size information. | |
51 // 66: width least significant bits | |
52 // 67: height least significant bits | |
53 // 68: 4 bits for each of width and height most significant bits | |
54 if (data[54] == 0) | |
sky
2016/01/14 15:36:58
Might the read amount be < 54?
Bret
2016/01/14 21:46:45
I added a ZeroMemory call above, so if we read les
| |
55 return false; | |
56 const int w = ((data[68] & 0xF0) << 4) + data[66]; | |
57 const int h = ((data[68] & 0x0F) << 8) + data[67]; | |
58 | |
59 if (w <= 0 || h <= 0) | |
60 return false; | |
61 | |
62 *width_mm = w; | |
63 *height_mm = h; | |
64 | |
65 return true; | |
66 } | |
67 | |
68 bool GetInterfaceDetailAndDeviceInfo( | |
69 HDEVINFO device_info_list, | |
70 SP_DEVICE_INTERFACE_DATA* interface_data, | |
71 scoped_ptr<SP_DEVICE_INTERFACE_DETAIL_DATA, base::FreeDeleter>* | |
72 interface_detail, | |
73 SP_DEVINFO_DATA* device_info) { | |
74 DCHECK(device_info->cbSize == sizeof(*device_info)); | |
sky
2016/01/14 15:36:58
nit: DCHECK_EQ
Bret
2016/01/14 21:46:45
Done.
| |
75 DWORD buffer_size; | |
76 // This call also populates device_info. | |
77 SetupDiGetDeviceInterfaceDetail(device_info_list, interface_data, nullptr, 0, | |
sky
2016/01/14 15:36:58
Don't you need to check the return value rather th
robliao
2016/01/14 18:07:06
In this case, SetupDiGetDeviceInterfaceDetail is g
Bret
2016/01/14 21:46:45
I added a comment like scott suggested to clarify
| |
78 &buffer_size, device_info); | |
79 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) | |
80 return false; | |
81 | |
82 interface_detail->reset( | |
83 reinterpret_cast<SP_DEVICE_INTERFACE_DETAIL_DATA*>(malloc(buffer_size))); | |
84 (*interface_detail)->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA); | |
85 return SetupDiGetDeviceInterfaceDetail(device_info_list, interface_data, | |
86 interface_detail->get(), buffer_size, | |
87 nullptr, nullptr) != 0; | |
88 } | |
89 | |
90 } // namespace | |
91 | |
92 namespace gfx { | |
93 | |
94 // The physical size information is only available by looking in the EDID block | |
95 // via setup. However setup has the device path and not the device name that we | |
96 // use to identify displays. Therefore after looking up a device via setup we | |
97 // need to find the display again via EnumDisplayDevices (matching device path | |
98 // to the device ID of the display's interface) so we can return the device name | |
99 // (available from the interface's attached monitor). | |
100 std::vector<PhysicalDisplaySize> GetPhysicalSizeForDisplays() { | |
101 std::vector<PhysicalDisplaySize> out; | |
102 | |
103 base::ScopedGeneric<HDEVINFO, DeviceInfoListScopedTraits> device_info_list( | |
104 SetupDiGetClassDevs(&GUID_DEVICEINTERFACE_MONITOR, nullptr, nullptr, | |
105 DIGCF_PRESENT | DIGCF_DEVICEINTERFACE)); | |
106 | |
107 if (!device_info_list.is_valid()) | |
108 return out; | |
109 | |
110 SP_DEVICE_INTERFACE_DATA interface_data = {}; | |
111 interface_data.cbSize = sizeof(interface_data); | |
112 int interface_index = 0; | |
113 while (SetupDiEnumDeviceInterfaces(device_info_list.get(), nullptr, | |
114 &GUID_DEVICEINTERFACE_MONITOR, | |
115 interface_index++, &interface_data)) { | |
116 scoped_ptr<SP_DEVICE_INTERFACE_DETAIL_DATA, base::FreeDeleter> | |
117 interface_detail; | |
118 SP_DEVINFO_DATA device_info = {}; | |
119 device_info.cbSize = sizeof(device_info); | |
120 bool get_info_succeeded = | |
121 GetInterfaceDetailAndDeviceInfo(device_info_list.get(), &interface_data, | |
122 &interface_detail, &device_info); | |
123 if (!get_info_succeeded) | |
124 continue; | |
125 | |
126 DISPLAY_DEVICE display_device = {}; | |
127 display_device.cb = sizeof(display_device); | |
128 int display_index = 0; | |
129 while (EnumDisplayDevices(nullptr, display_index++, &display_device, | |
130 EDD_GET_DEVICE_INTERFACE_NAME)) { | |
131 DISPLAY_DEVICE attached_device = {}; | |
132 attached_device.cb = sizeof(attached_device); | |
133 int attached_index = 0; | |
134 while (EnumDisplayDevices(display_device.DeviceName, attached_index++, | |
135 &attached_device, | |
136 EDD_GET_DEVICE_INTERFACE_NAME)) { | |
137 wchar_t* attached_device_id = attached_device.DeviceID; | |
138 wchar_t* setup_device_path = interface_detail->DevicePath; | |
139 if (wcsicmp(attached_device_id, setup_device_path) == 0) { | |
140 int width_mm; | |
141 int height_mm; | |
142 bool found = GetSizeFromRegistry(device_info_list.get(), &device_info, | |
143 &width_mm, &height_mm); | |
144 if (found) { | |
145 out.push_back( | |
146 PhysicalDisplaySize(base::WideToUTF8(display_device.DeviceName), | |
147 width_mm, height_mm)); | |
148 } | |
149 break; | |
150 } | |
151 } | |
152 } | |
153 } | |
154 return out; | |
155 } | |
156 | |
157 } // namespace gfx | |
OLD | NEW |