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" | |
robliao
2016/01/13 00:22:10
https://google.github.io/styleguide/cppguide.html#
Bret
2016/01/13 18:02:48
Done. The only problem seems to be missing a line
| |
6 | |
7 #include <windows.h> | |
8 #include <setupapi.h> | |
9 #include <iostream> | |
10 | |
11 #include "base/logging.h" | |
12 #include "base/memory/scoped_ptr.h" | |
13 #include "base/scoped_generic.h" | |
14 #include "base/strings/utf_string_conversions.h" | |
15 #include "base/win/registry.h" | |
16 | |
17 // This GUID was taken from | |
18 // https://msdn.microsoft.com/en-us/library/windows/hardware/ff545901.aspx | |
robliao
2016/01/13 00:22:11
Nit: Add comment:
// {E6F07B5F-EE97-4A90-B076-33F5
Bret
2016/01/13 18:02:48
Done.
| |
19 const GUID GUID_DEVICEINTERFACE_MONITOR = { | |
20 0xE6F07B5F, 0xEE97, 0x4A90, 0xB0, 0x76, 0x33, 0xF5, 0x7B, 0xF4, 0xEA, 0xA7}; | |
21 | |
22 namespace { | |
23 | |
24 struct DeviceInfoListScopedTraits { | |
25 static HDEVINFO InvalidValue() { return INVALID_HANDLE_VALUE; } | |
26 | |
27 static void Free(HDEVINFO h) { SetupDiDestroyDeviceInfoList(h); } | |
28 }; | |
29 | |
30 bool GetSizeFromRegistry(HDEVINFO device_info_list, | |
31 SP_DEVINFO_DATA* device_info, | |
32 int* width_mm, | |
33 int* height_mm) { | |
34 base::win::RegKey reg_key(SetupDiOpenDevRegKey( | |
35 device_info_list, device_info, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_READ)); | |
robliao
2016/01/13 00:22:10
The preference in ui/gfx/* seems to be to line up
Bret
2016/01/13 18:02:48
I just ran git cl format for this... Should I be d
scottmg
2016/01/13 18:08:36
Sticking to the output of clang-format/git cl form
robliao
2016/01/13 18:15:07
I'd defer to the owners on this one. I've gotten p
| |
36 | |
37 if (!reg_key.Valid()) | |
38 return false; | |
39 | |
40 BYTE data[128]; | |
41 DWORD data_length = sizeof(data); | |
42 LONG return_value = | |
43 reg_key.ReadValue(L"EDID", &data[0], &data_length, nullptr); | |
44 if (return_value != ERROR_SUCCESS) | |
45 return false; | |
46 | |
47 // Byte 54 is the start of the required descriptor block that contains the | |
48 // required timing information with the highest preference, and 12 bytes | |
49 // into that block is the size information. | |
50 // 66: width least significant bits | |
51 // 67: height least significant bits | |
52 // 68: 4 bits for each of width and height most significant bits | |
53 *width_mm = ((data[68] & 0xF0) << 4) + data[66]; | |
54 *height_mm = ((data[68] & 0x0F) << 8) + data[67]; | |
55 | |
56 return true; | |
57 } | |
58 | |
59 bool GetInterfaceDetailAndDeviceInfo( | |
60 HDEVINFO device_info_list, | |
61 SP_DEVICE_INTERFACE_DATA* interface_data, | |
62 scoped_ptr<SP_DEVICE_INTERFACE_DETAIL_DATA, base::FreeDeleter>* | |
63 interface_detail, | |
64 SP_DEVINFO_DATA* device_info) { | |
65 device_info->cbSize = sizeof(SP_DEVINFO_DATA); | |
robliao
2016/01/13 00:22:10
Similar to Windows, the caller should be initializ
Bret
2016/01/13 18:02:48
Done.
| |
66 DWORD buffer_size; | |
67 // This call also populates device_info. | |
68 bool success = | |
69 SetupDiGetDeviceInterfaceDetail(device_info_list, interface_data, nullptr, | |
70 0, &buffer_size, device_info) != 0; | |
71 if (success || GetLastError() != ERROR_INSUFFICIENT_BUFFER) { | |
robliao
2016/01/13 00:22:10
As set up, this call is guaranteed to fail, so a c
Bret
2016/01/13 18:02:49
Done.
| |
72 // The first call should fail. | |
73 return false; | |
74 } | |
75 | |
76 interface_detail->reset( | |
77 reinterpret_cast<SP_DEVICE_INTERFACE_DETAIL_DATA*>(malloc(buffer_size))); | |
78 (*interface_detail)->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA); | |
79 return SetupDiGetDeviceInterfaceDetail(device_info_list, interface_data, | |
80 interface_detail->get(), buffer_size, | |
81 nullptr, nullptr) != 0; | |
82 } | |
83 | |
84 } // namespace | |
85 | |
86 namespace gfx { | |
87 | |
88 std::vector<PhysicalDisplaySize> GetPhysicalSizeForDisplays() { | |
89 std::vector<PhysicalDisplaySize> out; | |
90 | |
91 // Get a handle to the list of device interfaces that are monitors via Setup. | |
robliao
2016/01/13 00:22:11
Remove this comment.
See https://google.github.io/
Bret
2016/01/13 18:02:48
I moved all the important information into a big h
| |
92 base::ScopedGeneric<HDEVINFO, DeviceInfoListScopedTraits> device_info_list( | |
93 SetupDiGetClassDevs(&GUID_DEVICEINTERFACE_MONITOR, nullptr, nullptr, | |
94 DIGCF_PRESENT | DIGCF_DEVICEINTERFACE)); | |
95 | |
96 if (!device_info_list.is_valid()) | |
97 return out; | |
98 | |
99 // Loop over the device interfaces that are present according to Setup. | |
100 SP_DEVICE_INTERFACE_DATA interface_data = {0}; | |
101 interface_data.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA); | |
102 int interface_index = 0; | |
103 while (SetupDiEnumDeviceInterfaces(device_info_list.get(), nullptr, | |
104 &GUID_DEVICEINTERFACE_MONITOR, | |
105 interface_index++, &interface_data)) { | |
106 scoped_ptr<SP_DEVICE_INTERFACE_DETAIL_DATA, base::FreeDeleter> | |
107 interface_detail; | |
108 SP_DEVINFO_DATA device_info = {0}; | |
robliao
2016/01/13 00:22:10
cbSize should be set up here as part of initializa
Bret
2016/01/13 18:02:48
Done.
| |
109 // This gives us the device path that we need to associate the device | |
110 // interface from Setup with the display device from EnumDisplayDevices. | |
111 bool get_info_succeeded = | |
robliao
2016/01/13 00:22:10
Optional: You could plausibly inline this into the
Bret
2016/01/13 18:02:48
Keeping the temp variables reads a bit better imo
| |
112 GetInterfaceDetailAndDeviceInfo(device_info_list.get(), &interface_data, | |
113 &interface_detail, &device_info); | |
114 if (!get_info_succeeded) | |
115 continue; | |
116 | |
117 // Loop over the display interfaces via EnumDisplayDevices. this will give | |
118 // us the device name and allow us to loop over its attached monitors. | |
119 DISPLAY_DEVICE display_device = {0}; | |
robliao
2016/01/13 00:22:11
Nit: {}
Bret
2016/01/13 18:02:49
Done.
| |
120 display_device.cb = sizeof(DISPLAY_DEVICE); | |
robliao
2016/01/13 00:22:10
Nit: sizeof(display_device)
Bret
2016/01/13 18:02:48
Done.
| |
121 int display_index = 0; | |
122 while (EnumDisplayDevices(nullptr, display_index++, &display_device, | |
123 EDD_GET_DEVICE_INTERFACE_NAME)) { | |
124 // Loop over the monitors attached to the display interface. This gives us | |
125 // the device ID that associates the monitor with the setup device path | |
126 // from the earlier step. | |
127 DISPLAY_DEVICE attached_device = {0}; | |
robliao
2016/01/13 00:22:10
Nit: {}
Bret
2016/01/13 18:02:48
Done.
| |
128 attached_device.cb = sizeof(DISPLAY_DEVICE); | |
robliao
2016/01/13 00:22:11
Nit: sizeof(attached_device)
Bret
2016/01/13 18:02:48
Done.
| |
129 int attached_index = 0; | |
130 while (EnumDisplayDevices(display_device.DeviceName, attached_index++, | |
131 &attached_device, | |
132 EDD_GET_DEVICE_INTERFACE_NAME)) { | |
133 wchar_t* attached_device_id = attached_device.DeviceID; | |
134 wchar_t* setup_device_path = interface_detail->DevicePath; | |
135 if (wcsicmp(attached_device_id, setup_device_path) == 0) { | |
136 // We've found the monitor that matches the Setup device. | |
robliao
2016/01/13 00:22:10
Nit: Setup -> setup.
Bret
2016/01/13 18:02:48
Done.
| |
137 int width_mm; | |
138 int height_mm; | |
139 bool found = GetSizeFromRegistry(device_info_list.get(), &device_info, | |
140 &width_mm, &height_mm); | |
141 if (found) { | |
142 out.push_back( | |
143 PhysicalDisplaySize(base::WideToUTF8(display_device.DeviceName), | |
144 width_mm, height_mm)); | |
145 } | |
146 break; | |
147 } | |
148 } | |
149 } | |
150 } | |
151 return out; | |
152 } | |
153 | |
154 } // namespace gfx | |
OLD | NEW |