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}; | |
Nico
2016/01/15 02:16:50
clang/win says:
..\..\ui\gfx\win\physical_size.cc
| |
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) | |
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_EQ(sizeof(*device_info), device_info->cbSize); | |
75 DWORD buffer_size; | |
76 // This call populates device_info. It will also fail, but if the error is | |
77 // "insufficient buffer" then it will set buffer_size and we can call again | |
78 // with an allocated buffer. | |
79 SetupDiGetDeviceInterfaceDetail(device_info_list, interface_data, nullptr, 0, | |
80 &buffer_size, device_info); | |
81 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) | |
82 return false; | |
83 | |
84 interface_detail->reset( | |
85 reinterpret_cast<SP_DEVICE_INTERFACE_DETAIL_DATA*>(malloc(buffer_size))); | |
huangs
2016/01/15 15:34:57
This mixes malloc() and free, which is undefined b
huangs
2016/01/15 15:41:05
I mean malloc() and delete.
scottmg
2016/01/15 17:39:35
interface_detail has a Deleter of base::FreeDelete
huangs
2016/01/15 18:35:53
Ah. So SP_DEVICE_INTERFACE_DETAIL_DATA needs to t
| |
86 (*interface_detail)->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA); | |
87 return SetupDiGetDeviceInterfaceDetail(device_info_list, interface_data, | |
88 interface_detail->get(), buffer_size, | |
89 nullptr, nullptr) != 0; | |
90 } | |
91 | |
92 } // namespace | |
93 | |
94 namespace gfx { | |
95 | |
96 // The physical size information is only available by looking in the EDID block | |
97 // via setup. However setup has the device path and not the device name that we | |
98 // use to identify displays. Therefore after looking up a device via setup we | |
99 // need to find the display again via EnumDisplayDevices (matching device path | |
100 // to the device ID of the display's interface) so we can return the device name | |
101 // (available from the interface's attached monitor). | |
102 std::vector<PhysicalDisplaySize> GetPhysicalSizeForDisplays() { | |
103 std::vector<PhysicalDisplaySize> out; | |
104 | |
105 base::ScopedGeneric<HDEVINFO, DeviceInfoListScopedTraits> device_info_list( | |
106 SetupDiGetClassDevs(&GUID_DEVICEINTERFACE_MONITOR, nullptr, nullptr, | |
107 DIGCF_PRESENT | DIGCF_DEVICEINTERFACE)); | |
108 | |
109 if (!device_info_list.is_valid()) | |
110 return out; | |
111 | |
112 SP_DEVICE_INTERFACE_DATA interface_data = {}; | |
113 interface_data.cbSize = sizeof(interface_data); | |
114 int interface_index = 0; | |
115 while (SetupDiEnumDeviceInterfaces(device_info_list.get(), nullptr, | |
116 &GUID_DEVICEINTERFACE_MONITOR, | |
117 interface_index++, &interface_data)) { | |
118 scoped_ptr<SP_DEVICE_INTERFACE_DETAIL_DATA, base::FreeDeleter> | |
119 interface_detail; | |
120 SP_DEVINFO_DATA device_info = {}; | |
121 device_info.cbSize = sizeof(device_info); | |
122 bool get_info_succeeded = | |
123 GetInterfaceDetailAndDeviceInfo(device_info_list.get(), &interface_data, | |
124 &interface_detail, &device_info); | |
125 if (!get_info_succeeded) | |
126 continue; | |
127 | |
128 DISPLAY_DEVICE display_device = {}; | |
129 display_device.cb = sizeof(display_device); | |
130 int display_index = 0; | |
131 while (EnumDisplayDevices(nullptr, display_index++, &display_device, | |
132 EDD_GET_DEVICE_INTERFACE_NAME)) { | |
133 DISPLAY_DEVICE attached_device = {}; | |
134 attached_device.cb = sizeof(attached_device); | |
135 int attached_index = 0; | |
136 while (EnumDisplayDevices(display_device.DeviceName, attached_index++, | |
137 &attached_device, | |
138 EDD_GET_DEVICE_INTERFACE_NAME)) { | |
139 wchar_t* attached_device_id = attached_device.DeviceID; | |
140 wchar_t* setup_device_path = interface_detail->DevicePath; | |
141 if (wcsicmp(attached_device_id, setup_device_path) == 0) { | |
142 int width_mm; | |
143 int height_mm; | |
144 bool found = GetSizeFromRegistry(device_info_list.get(), &device_info, | |
145 &width_mm, &height_mm); | |
146 if (found) { | |
147 out.push_back( | |
148 PhysicalDisplaySize(base::WideToUTF8(display_device.DeviceName), | |
149 width_mm, height_mm)); | |
150 } | |
151 break; | |
152 } | |
153 } | |
154 } | |
155 } | |
156 return out; | |
157 } | |
158 | |
159 } // namespace gfx | |
OLD | NEW |