Chromium Code Reviews| 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(device_info_list, device_info, | |
| 36 DICS_FLAG_GLOBAL, 0, DIREG_DEV, | |
| 37 KEY_READ)); | |
| 38 | |
| 39 if (!reg_key.Valid()) | |
| 40 return false; | |
| 41 | |
| 42 BYTE data[128]; | |
| 43 DWORD data_length = sizeof(data); | |
| 44 LONG return_value = | |
| 45 reg_key.ReadValue(L"EDID", &data[0], &data_length, nullptr); | |
| 46 if (return_value != ERROR_SUCCESS) | |
| 47 return false; | |
| 48 | |
| 49 // Byte 54 is the start of the required descriptor block that contains the | |
| 50 // required timing information with the highest preference, and 12 bytes | |
| 51 // into that block is the size information. | |
| 52 // 66: width least significant bits | |
| 53 // 67: height least significant bits | |
| 54 // 68: 4 bits for each of width and height most significant bits | |
| 55 *width_mm = ((data[68] & 0xF0) << 4) + data[66]; | |
| 56 *height_mm = ((data[68] & 0x0F) << 8) + data[67]; | |
| 57 | |
| 58 return true; | |
| 59 } | |
| 60 | |
| 61 bool GetInterfaceDetailAndDeviceInfo( | |
| 62 HDEVINFO device_info_list, | |
| 63 SP_DEVICE_INTERFACE_DATA* interface_data, | |
| 64 scoped_ptr<SP_DEVICE_INTERFACE_DETAIL_DATA, base::FreeDeleter>* | |
| 65 interface_detail, | |
| 66 SP_DEVINFO_DATA* device_info) { | |
| 67 DCHECK(device_info->cbSize == sizeof(*device_info)); | |
| 68 DWORD buffer_size; | |
| 69 // This call also populates device_info. | |
| 70 SetupDiGetDeviceInterfaceDetail(device_info_list, interface_data, nullptr, | |
| 71 0, &buffer_size, device_info); | |
| 72 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) | |
| 73 return false; | |
| 74 | |
| 75 interface_detail->reset( | |
| 76 reinterpret_cast<SP_DEVICE_INTERFACE_DETAIL_DATA*>(malloc(buffer_size))); | |
| 77 (*interface_detail)->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA); | |
| 78 return SetupDiGetDeviceInterfaceDetail(device_info_list, interface_data, | |
| 79 interface_detail->get(), buffer_size, | |
| 80 nullptr, nullptr) != 0; | |
| 81 } | |
| 82 | |
| 83 } // namespace | |
| 84 | |
| 85 namespace gfx { | |
| 86 | |
| 87 // The physical size information is only available by looking in the EDID block | |
| 88 // via setup. However setup has the device path and not the device name that we | |
| 89 // use to identify displays. Therefore after looking up a device via setup we | |
| 90 // need to find the display again via EnumDisplayDevices (matching device path | |
| 91 // to the device ID of the display's interface) so we can return the device name | |
| 92 // (available from the interface's attached monitor). | |
| 93 std::vector<PhysicalDisplaySize> GetPhysicalSizeForDisplays() { | |
| 94 std::vector<PhysicalDisplaySize> out; | |
| 95 | |
| 96 base::ScopedGeneric<HDEVINFO, DeviceInfoListScopedTraits> device_info_list( | |
| 97 SetupDiGetClassDevs(&GUID_DEVICEINTERFACE_MONITOR, nullptr, nullptr, | |
| 98 DIGCF_PRESENT | DIGCF_DEVICEINTERFACE)); | |
| 99 | |
| 100 if (!device_info_list.is_valid()) | |
| 101 return out; | |
| 102 | |
| 103 SP_DEVICE_INTERFACE_DATA interface_data = {}; | |
| 104 interface_data.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA); | |
|
robliao
2016/01/13 19:10:32
sizeof(interface_data)
Bret
2016/01/14 01:28:23
Done.
| |
| 105 int interface_index = 0; | |
| 106 while (SetupDiEnumDeviceInterfaces(device_info_list.get(), nullptr, | |
| 107 &GUID_DEVICEINTERFACE_MONITOR, | |
| 108 interface_index++, &interface_data)) { | |
| 109 scoped_ptr<SP_DEVICE_INTERFACE_DETAIL_DATA, base::FreeDeleter> | |
| 110 interface_detail; | |
| 111 SP_DEVINFO_DATA device_info = {}; | |
| 112 device_info.cbSize = sizeof(device_info); | |
| 113 bool get_info_succeeded = | |
| 114 GetInterfaceDetailAndDeviceInfo(device_info_list.get(), &interface_data, | |
| 115 &interface_detail, &device_info); | |
| 116 if (!get_info_succeeded) | |
| 117 continue; | |
| 118 | |
| 119 DISPLAY_DEVICE display_device = {}; | |
| 120 display_device.cb = sizeof(display_device); | |
| 121 int display_index = 0; | |
| 122 while (EnumDisplayDevices(nullptr, display_index++, &display_device, | |
| 123 EDD_GET_DEVICE_INTERFACE_NAME)) { | |
| 124 DISPLAY_DEVICE attached_device = {}; | |
| 125 attached_device.cb = sizeof(attached_device); | |
| 126 int attached_index = 0; | |
| 127 while (EnumDisplayDevices(display_device.DeviceName, attached_index++, | |
| 128 &attached_device, | |
| 129 EDD_GET_DEVICE_INTERFACE_NAME)) { | |
| 130 wchar_t* attached_device_id = attached_device.DeviceID; | |
| 131 wchar_t* setup_device_path = interface_detail->DevicePath; | |
| 132 if (wcsicmp(attached_device_id, setup_device_path) == 0) { | |
| 133 int width_mm; | |
| 134 int height_mm; | |
| 135 bool found = GetSizeFromRegistry(device_info_list.get(), &device_info, | |
| 136 &width_mm, &height_mm); | |
| 137 if (found) { | |
| 138 out.push_back( | |
| 139 PhysicalDisplaySize(base::WideToUTF8(display_device.DeviceName), | |
| 140 width_mm, height_mm)); | |
| 141 } | |
| 142 break; | |
| 143 } | |
| 144 } | |
| 145 } | |
| 146 } | |
| 147 return out; | |
| 148 } | |
| 149 | |
| 150 } // namespace gfx | |
| OLD | NEW |