summaryrefslogtreecommitdiffstats
path: root/widget/windows/ScreenHelperWin.cpp
blob: b3a48bae23a88d2263c04c64a4ff98d7df55652a (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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "ScreenHelperWin.h"

#include "mozilla/Logging.h"
#include "nsTArray.h"
#include "WinUtils.h"

static mozilla::LazyLogModule sScreenLog("WidgetScreen");

namespace mozilla {
namespace widget {

static void GetDisplayInfo(const char16ptr_t aName,
                           hal::ScreenOrientation& aOrientation,
                           uint16_t& aAngle, bool& aIsPseudoDisplay,
                           uint32_t& aRefreshRate) {
  DISPLAY_DEVICEW displayDevice = {.cb = sizeof(DISPLAY_DEVICEW)};

  // XXX Is the pseudodisplay status really useful?
  aIsPseudoDisplay =
      EnumDisplayDevicesW(aName, 0, &displayDevice, 0) &&
      (displayDevice.StateFlags & DISPLAY_DEVICE_MIRRORING_DRIVER);

  DEVMODEW mode = {.dmSize = sizeof(DEVMODEW)};
  if (!EnumDisplaySettingsW(aName, ENUM_CURRENT_SETTINGS, &mode)) {
    return;
  }
  MOZ_ASSERT(mode.dmFields & DM_DISPLAYORIENTATION);

  aRefreshRate = mode.dmDisplayFrequency;

  // conver to default/natural size
  if (mode.dmDisplayOrientation == DMDO_90 ||
      mode.dmDisplayOrientation == DMDO_270) {
    DWORD temp = mode.dmPelsHeight;
    mode.dmPelsHeight = mode.dmPelsWidth;
    mode.dmPelsWidth = temp;
  }

  bool defaultIsLandscape = mode.dmPelsWidth >= mode.dmPelsHeight;
  switch (mode.dmDisplayOrientation) {
    case DMDO_DEFAULT:
      aOrientation = defaultIsLandscape
                         ? hal::ScreenOrientation::LandscapePrimary
                         : hal::ScreenOrientation::PortraitPrimary;
      aAngle = 0;
      break;
    case DMDO_90:
      aOrientation = defaultIsLandscape
                         ? hal::ScreenOrientation::PortraitPrimary
                         : hal::ScreenOrientation::LandscapeSecondary;
      aAngle = 270;
      break;
    case DMDO_180:
      aOrientation = defaultIsLandscape
                         ? hal::ScreenOrientation::LandscapeSecondary
                         : hal::ScreenOrientation::PortraitSecondary;
      aAngle = 180;
      break;
    case DMDO_270:
      aOrientation = defaultIsLandscape
                         ? hal::ScreenOrientation::PortraitSecondary
                         : hal::ScreenOrientation::LandscapePrimary;
      aAngle = 90;
      break;
    default:
      MOZ_ASSERT_UNREACHABLE("Unexpected angle");
      break;
  }
}

BOOL CALLBACK CollectMonitors(HMONITOR aMon, HDC, LPRECT, LPARAM ioParam) {
  auto screens = reinterpret_cast<nsTArray<RefPtr<Screen>>*>(ioParam);
  BOOL success = FALSE;
  MONITORINFOEX info;
  info.cbSize = sizeof(MONITORINFOEX);
  success = ::GetMonitorInfoW(aMon, &info);
  if (!success) {
    MOZ_LOG(sScreenLog, LogLevel::Error, ("GetMonitorInfoW failed"));
    return TRUE;  // continue the enumeration
  }

  double scale = WinUtils::LogToPhysFactor(aMon);
  DesktopToLayoutDeviceScale contentsScaleFactor;
  if (WinUtils::IsPerMonitorDPIAware()) {
    contentsScaleFactor.scale = 1.0;
  } else {
    contentsScaleFactor.scale = scale;
  }
  CSSToLayoutDeviceScale defaultCssScaleFactor(scale);
  LayoutDeviceIntRect rect(info.rcMonitor.left, info.rcMonitor.top,
                           info.rcMonitor.right - info.rcMonitor.left,
                           info.rcMonitor.bottom - info.rcMonitor.top);
  LayoutDeviceIntRect availRect(info.rcWork.left, info.rcWork.top,
                                info.rcWork.right - info.rcWork.left,
                                info.rcWork.bottom - info.rcWork.top);

  HDC hDC = CreateDC(nullptr, info.szDevice, nullptr, nullptr);
  if (!hDC) {
    MOZ_LOG(sScreenLog, LogLevel::Error, ("CollectMonitors CreateDC failed"));
    return TRUE;
  }
  uint32_t pixelDepth = ::GetDeviceCaps(hDC, BITSPIXEL);
  DeleteDC(hDC);
  if (pixelDepth == 32) {
    // If a device uses 32 bits per pixel, it's still only using 8 bits
    // per color component, which is what our callers want to know.
    // (Some devices report 32 and some devices report 24.)
    pixelDepth = 24;
  }

  float dpi = WinUtils::MonitorDPI(aMon);

  auto orientation = hal::ScreenOrientation::None;
  uint16_t angle = 0;
  bool isPseudoDisplay = false;
  uint32_t refreshRate = 0;
  GetDisplayInfo(info.szDevice, orientation, angle, isPseudoDisplay,
                 refreshRate);

  MOZ_LOG(sScreenLog, LogLevel::Debug,
          ("New screen [%s (%s) %d %u %f %f %f %d %d %d]",
           ToString(rect).c_str(), ToString(availRect).c_str(), pixelDepth,
           refreshRate, contentsScaleFactor.scale, defaultCssScaleFactor.scale,
           dpi, isPseudoDisplay, orientation, angle));
  auto screen = MakeRefPtr<Screen>(
      rect, availRect, pixelDepth, pixelDepth, refreshRate, contentsScaleFactor,
      defaultCssScaleFactor, dpi, Screen::IsPseudoDisplay(isPseudoDisplay),
      orientation, angle);
  if (info.dwFlags & MONITORINFOF_PRIMARY) {
    // The primary monitor must be the first element of the screen list.
    screens->InsertElementAt(0, std::move(screen));
  } else {
    screens->AppendElement(std::move(screen));
  }
  return TRUE;
}

void ScreenHelperWin::RefreshScreens() {
  MOZ_LOG(sScreenLog, LogLevel::Debug, ("Refreshing screens"));

  AutoTArray<RefPtr<Screen>, 4> screens;
  BOOL result = ::EnumDisplayMonitors(
      nullptr, nullptr, (MONITORENUMPROC)CollectMonitors, (LPARAM)&screens);
  if (!result) {
    NS_WARNING("Unable to EnumDisplayMonitors");
  }
  ScreenManager::Refresh(std::move(screens));
}

}  // namespace widget
}  // namespace mozilla