Chromium Code Reviews| Index: ui/gfx/win/physical_size.cc |
| diff --git a/ui/gfx/win/physical_size.cc b/ui/gfx/win/physical_size.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4285cba23a35a578a02c6879a7f0dd4a3a9ec16f |
| --- /dev/null |
| +++ b/ui/gfx/win/physical_size.cc |
| @@ -0,0 +1,150 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "ui/gfx/win/physical_size.h" |
| + |
| +#include <windows.h> |
| +#include <setupapi.h> |
| + |
| +#include <iostream> |
| + |
| +#include "base/logging.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/scoped_generic.h" |
| +#include "base/strings/utf_string_conversions.h" |
| +#include "base/win/registry.h" |
| + |
| +// This GUID {E6F07B5F-EE97-4A90-B076-33F57BF4EAA7} was taken from |
| +// https://msdn.microsoft.com/en-us/library/windows/hardware/ff545901.aspx |
| +const GUID GUID_DEVICEINTERFACE_MONITOR = { |
| + 0xE6F07B5F, 0xEE97, 0x4A90, 0xB0, 0x76, 0x33, 0xF5, 0x7B, 0xF4, 0xEA, 0xA7}; |
| + |
| +namespace { |
| + |
| +struct DeviceInfoListScopedTraits { |
| + static HDEVINFO InvalidValue() { return INVALID_HANDLE_VALUE; } |
| + |
| + static void Free(HDEVINFO h) { SetupDiDestroyDeviceInfoList(h); } |
| +}; |
| + |
| +bool GetSizeFromRegistry(HDEVINFO device_info_list, |
| + SP_DEVINFO_DATA* device_info, |
| + int* width_mm, |
| + int* height_mm) { |
| + base::win::RegKey reg_key(SetupDiOpenDevRegKey(device_info_list, device_info, |
| + DICS_FLAG_GLOBAL, 0, DIREG_DEV, |
| + KEY_READ)); |
| + |
| + if (!reg_key.Valid()) |
| + return false; |
| + |
| + BYTE data[128]; |
| + DWORD data_length = sizeof(data); |
| + LONG return_value = |
| + reg_key.ReadValue(L"EDID", &data[0], &data_length, nullptr); |
| + if (return_value != ERROR_SUCCESS) |
| + return false; |
| + |
| + // Byte 54 is the start of the required descriptor block that contains the |
| + // required timing information with the highest preference, and 12 bytes |
| + // into that block is the size information. |
| + // 66: width least significant bits |
| + // 67: height least significant bits |
| + // 68: 4 bits for each of width and height most significant bits |
| + *width_mm = ((data[68] & 0xF0) << 4) + data[66]; |
|
sky
2016/01/13 22:15:54
Make sure you deal with error detection. For examp
Bret
2016/01/14 01:28:23
Done.
|
| + *height_mm = ((data[68] & 0x0F) << 8) + data[67]; |
| + |
| + return true; |
| +} |
| + |
| +bool GetInterfaceDetailAndDeviceInfo( |
| + HDEVINFO device_info_list, |
| + SP_DEVICE_INTERFACE_DATA* interface_data, |
| + scoped_ptr<SP_DEVICE_INTERFACE_DETAIL_DATA, base::FreeDeleter>* |
| + interface_detail, |
| + SP_DEVINFO_DATA* device_info) { |
| + DCHECK(device_info->cbSize == sizeof(*device_info)); |
| + DWORD buffer_size; |
| + // This call also populates device_info. |
| + SetupDiGetDeviceInterfaceDetail(device_info_list, interface_data, nullptr, |
| + 0, &buffer_size, device_info); |
| + if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) |
| + return false; |
| + |
| + interface_detail->reset( |
| + reinterpret_cast<SP_DEVICE_INTERFACE_DETAIL_DATA*>(malloc(buffer_size))); |
| + (*interface_detail)->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA); |
| + return SetupDiGetDeviceInterfaceDetail(device_info_list, interface_data, |
| + interface_detail->get(), buffer_size, |
| + nullptr, nullptr) != 0; |
| +} |
| + |
| +} // namespace |
| + |
| +namespace gfx { |
| + |
| +// The physical size information is only available by looking in the EDID block |
| +// via setup. However setup has the device path and not the device name that we |
| +// use to identify displays. Therefore after looking up a device via setup we |
| +// need to find the display again via EnumDisplayDevices (matching device path |
| +// to the device ID of the display's interface) so we can return the device name |
| +// (available from the interface's attached monitor). |
| +std::vector<PhysicalDisplaySize> GetPhysicalSizeForDisplays() { |
| + std::vector<PhysicalDisplaySize> out; |
| + |
| + base::ScopedGeneric<HDEVINFO, DeviceInfoListScopedTraits> device_info_list( |
| + SetupDiGetClassDevs(&GUID_DEVICEINTERFACE_MONITOR, nullptr, nullptr, |
| + DIGCF_PRESENT | DIGCF_DEVICEINTERFACE)); |
| + |
| + if (!device_info_list.is_valid()) |
| + return out; |
| + |
| + SP_DEVICE_INTERFACE_DATA interface_data = {}; |
| + interface_data.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA); |
| + int interface_index = 0; |
| + while (SetupDiEnumDeviceInterfaces(device_info_list.get(), nullptr, |
| + &GUID_DEVICEINTERFACE_MONITOR, |
| + interface_index++, &interface_data)) { |
| + scoped_ptr<SP_DEVICE_INTERFACE_DETAIL_DATA, base::FreeDeleter> |
| + interface_detail; |
| + SP_DEVINFO_DATA device_info = {}; |
| + device_info.cbSize = sizeof(device_info); |
| + bool get_info_succeeded = |
| + GetInterfaceDetailAndDeviceInfo(device_info_list.get(), &interface_data, |
| + &interface_detail, &device_info); |
| + if (!get_info_succeeded) |
| + continue; |
| + |
| + DISPLAY_DEVICE display_device = {}; |
| + display_device.cb = sizeof(display_device); |
| + int display_index = 0; |
| + while (EnumDisplayDevices(nullptr, display_index++, &display_device, |
| + EDD_GET_DEVICE_INTERFACE_NAME)) { |
| + DISPLAY_DEVICE attached_device = {}; |
| + attached_device.cb = sizeof(attached_device); |
| + int attached_index = 0; |
| + while (EnumDisplayDevices(display_device.DeviceName, attached_index++, |
| + &attached_device, |
| + EDD_GET_DEVICE_INTERFACE_NAME)) { |
| + wchar_t* attached_device_id = attached_device.DeviceID; |
| + wchar_t* setup_device_path = interface_detail->DevicePath; |
| + if (wcsicmp(attached_device_id, setup_device_path) == 0) { |
| + int width_mm; |
| + int height_mm; |
| + bool found = GetSizeFromRegistry(device_info_list.get(), &device_info, |
| + &width_mm, &height_mm); |
| + if (found) { |
| + out.push_back( |
| + PhysicalDisplaySize(base::WideToUTF8(display_device.DeviceName), |
| + width_mm, height_mm)); |
| + } |
| + break; |
| + } |
| + } |
| + } |
| + } |
| + return out; |
| +} |
| + |
| +} // namespace gfx |