Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(722)

Unified Diff: ui/gfx/win/physical_size.cc

Issue 1563183008: Added capability on Windows to get the physical dimensions of your attached monitors. Also added th… (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: . Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« ui/gfx/win/physical_size.h ('K') | « ui/gfx/win/physical_size.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..594c788243f1772896119375e9b71a970ee0aefb
--- /dev/null
+++ b/ui/gfx/win/physical_size.cc
@@ -0,0 +1,155 @@
+// 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 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,
scottmg 2016/01/12 01:03:13 No non-const references. If it can be a const ref,
Bret 2016/01/12 22:55:17 Done.
+ 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];
+ *height_mm = ((data[68] & 0x0F) << 8) + data[67];
+
+ return true;
+}
+
+bool GetInterfaceDetailAndDeviceInfo(
+ HDEVINFO device_info_list,
+ SP_DEVICE_INTERFACE_DATA& interface_data,
scottmg 2016/01/12 01:03:13 Same.
Bret 2016/01/12 22:55:17 Done.
+ scoped_ptr<SP_DEVICE_INTERFACE_DETAIL_DATA, base::FreeDeleter>*
+ interface_detail,
+ SP_DEVINFO_DATA* device_info) {
+ device_info->cbSize = sizeof(SP_DEVINFO_DATA);
+ DWORD buffer_size;
+ // This call also populates device_info.
+ bool success = SetupDiGetDeviceInterfaceDetail(
+ device_info_list, &interface_data, nullptr, 0,
+ &buffer_size, device_info) != 0;
+ if (success || GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
+ // The first call should fail.
+ 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 {
+
+void GetPhysicalSizeForDisplays(std::vector<PhysicalDisplaySize>* out) {
+ DCHECK(out);
+
+ // Get a handle to the list of device interfaces that are monitors via Setup.
+ base::ScopedGeneric<HDEVINFO, DeviceInfoListScopedTraits> device_info_list(
+ SetupDiGetClassDevs(&GUID_DEVICEINTERFACE_MONITOR, nullptr, nullptr,
+ DIGCF_PRESENT | DIGCF_DEVICEINTERFACE));
+
+ if (!device_info_list.is_valid())
+ return;
+
+ // Loop over the device interfaces that are present according to Setup.
+ SP_DEVICE_INTERFACE_DATA interface_data = {0};
+ 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 = {0};
+ // This gives us the device path that we need to associate the device
+ // interface from Setup with the display device from EnumDisplayDevices.
+ bool get_info_succeeded =
+ GetInterfaceDetailAndDeviceInfo(device_info_list.get(), interface_data,
+ &interface_detail, &device_info);
+ if (!get_info_succeeded)
+ continue;
+
+ // Loop over the display interfaces via EnumDisplayDevices. this will give
+ // us the device name and allow us to loop over its attached monitors.
+ DISPLAY_DEVICE display_device = {0};
+ display_device.cb = sizeof(DISPLAY_DEVICE);
+ int display_index = 0;
+ while (EnumDisplayDevices(nullptr, display_index++, &display_device,
+ EDD_GET_DEVICE_INTERFACE_NAME)) {
+ // Loop over the monitors attached to the display interface. This gives us
+ // the device ID that associates the monitor with the setup device path
+ // from the earlier step.
+ DISPLAY_DEVICE attached_device = {0};
+ attached_device.cb = sizeof(DISPLAY_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) {
+ // We've found the monitor that matches the Setup device.
+ int width_mm;
+ int height_mm;
+ bool found = GetSizeFromRegistry(device_info_list.get(), device_info,
+ &width_mm, &height_mm);
+ if (found) {
+ PhysicalDisplaySize size(
+ base::WideToUTF8(display_device.DeviceName), width_mm,
+ height_mm);
scottmg 2016/01/12 01:03:13 nit; I'd probably just put this inline into the pu
Bret 2016/01/12 22:55:17 Done.
+ out->push_back(size);
+ }
+ break;
scottmg 2016/01/12 01:03:13 Should this be inside "if (found)"?
Bret 2016/01/12 22:55:17 No, the Device ID should be unique, so if it match
+ }
+ }
+ }
+ }
+}
+
+} // namespace gfx
« ui/gfx/win/physical_size.h ('K') | « ui/gfx/win/physical_size.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698