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

Side by Side 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 unified diff | Download patch
« ui/gfx/win/physical_size.h ('K') | « ui/gfx/win/physical_size.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 #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
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,
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.
32 int* width_mm,
33 int* height_mm) {
34 base::win::RegKey reg_key(SetupDiOpenDevRegKey(device_info_list, &device_info,
35 DICS_FLAG_GLOBAL, 0, DIREG_DEV,
36 KEY_READ));
37
38 if (!reg_key.Valid())
39 return false;
40
41 BYTE data[128];
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 required descriptor block that 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 *width_mm = ((data[68] & 0xF0) << 4) + data[66];
55 *height_mm = ((data[68] & 0x0F) << 8) + data[67];
56
57 return true;
58 }
59
60 bool GetInterfaceDetailAndDeviceInfo(
61 HDEVINFO device_info_list,
62 SP_DEVICE_INTERFACE_DATA& interface_data,
scottmg 2016/01/12 01:03:13 Same.
Bret 2016/01/12 22:55:17 Done.
63 scoped_ptr<SP_DEVICE_INTERFACE_DETAIL_DATA, base::FreeDeleter>*
64 interface_detail,
65 SP_DEVINFO_DATA* device_info) {
66 device_info->cbSize = sizeof(SP_DEVINFO_DATA);
67 DWORD buffer_size;
68 // This call also populates device_info.
69 bool success = SetupDiGetDeviceInterfaceDetail(
70 device_info_list, &interface_data, nullptr, 0,
71 &buffer_size, device_info) != 0;
72 if (success || GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
73 // The first call should fail.
74 return false;
75 }
76
77 interface_detail->reset(
78 reinterpret_cast<SP_DEVICE_INTERFACE_DETAIL_DATA*>(malloc(buffer_size)));
79 (*interface_detail)->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
80 return SetupDiGetDeviceInterfaceDetail(device_info_list, &interface_data,
81 interface_detail->get(), buffer_size,
82 nullptr, nullptr) != 0;
83 }
84
85 } // namespace
86
87 namespace gfx {
88
89 void GetPhysicalSizeForDisplays(std::vector<PhysicalDisplaySize>* out) {
90 DCHECK(out);
91
92 // Get a handle to the list of device interfaces that are monitors via Setup.
93 base::ScopedGeneric<HDEVINFO, DeviceInfoListScopedTraits> device_info_list(
94 SetupDiGetClassDevs(&GUID_DEVICEINTERFACE_MONITOR, nullptr, nullptr,
95 DIGCF_PRESENT | DIGCF_DEVICEINTERFACE));
96
97 if (!device_info_list.is_valid())
98 return;
99
100 // Loop over the device interfaces that are present according to Setup.
101 SP_DEVICE_INTERFACE_DATA interface_data = {0};
102 interface_data.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
103 int interface_index = 0;
104 while (SetupDiEnumDeviceInterfaces(device_info_list.get(), nullptr,
105 &GUID_DEVICEINTERFACE_MONITOR,
106 interface_index++, &interface_data)) {
107 scoped_ptr<SP_DEVICE_INTERFACE_DETAIL_DATA, base::FreeDeleter>
108 interface_detail;
109 SP_DEVINFO_DATA device_info = {0};
110 // This gives us the device path that we need to associate the device
111 // interface from Setup with the display device from EnumDisplayDevices.
112 bool get_info_succeeded =
113 GetInterfaceDetailAndDeviceInfo(device_info_list.get(), interface_data,
114 &interface_detail, &device_info);
115 if (!get_info_succeeded)
116 continue;
117
118 // Loop over the display interfaces via EnumDisplayDevices. this will give
119 // us the device name and allow us to loop over its attached monitors.
120 DISPLAY_DEVICE display_device = {0};
121 display_device.cb = sizeof(DISPLAY_DEVICE);
122 int display_index = 0;
123 while (EnumDisplayDevices(nullptr, display_index++, &display_device,
124 EDD_GET_DEVICE_INTERFACE_NAME)) {
125 // Loop over the monitors attached to the display interface. This gives us
126 // the device ID that associates the monitor with the setup device path
127 // from the earlier step.
128 DISPLAY_DEVICE attached_device = {0};
129 attached_device.cb = sizeof(DISPLAY_DEVICE);
130 int attached_index = 0;
131 while (EnumDisplayDevices(display_device.DeviceName, attached_index++,
132 &attached_device,
133 EDD_GET_DEVICE_INTERFACE_NAME)) {
134 wchar_t* attached_device_id = attached_device.DeviceID;
135 wchar_t* setup_device_path = interface_detail->DevicePath;
136 if (wcsicmp(attached_device_id, setup_device_path) == 0) {
137 // We've found the monitor that matches the Setup device.
138 int width_mm;
139 int height_mm;
140 bool found = GetSizeFromRegistry(device_info_list.get(), device_info,
141 &width_mm, &height_mm);
142 if (found) {
143 PhysicalDisplaySize size(
144 base::WideToUTF8(display_device.DeviceName), width_mm,
145 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.
146 out->push_back(size);
147 }
148 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
149 }
150 }
151 }
152 }
153 }
154
155 } // namespace gfx
OLDNEW
« 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