summaryrefslogtreecommitdiffstats
path: root/ui/gfx/win/physical_size.cc
blob: 2aa0c3fd89256cc517238d933072619ffa2c0826 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// 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/free_deleter.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];  // EDID block is exactly 128 bytes long.
  ZeroMemory(&data[0], sizeof(data));
  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 first descriptor block, which 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
  if (data[54] == 0)
    return false;
  const int w = ((data[68] & 0xF0) << 4) + data[66];
  const int h = ((data[68] & 0x0F) << 8) + data[67];

  if (w <= 0 || h <= 0)
    return false;

  *width_mm = w;
  *height_mm = h;

  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_EQ(sizeof(*device_info), device_info->cbSize);
  DWORD buffer_size;
  // This call populates device_info. It will also fail, but if the error is
  // "insufficient buffer" then it will set buffer_size and we can call again
  // with an allocated buffer.
  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(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