Bug 603903 - Part 3: fix screen enumeration when not logged in draft
authorAdam Gashlin <agashlin@mozilla.com>
Wed, 25 Apr 2018 15:01:11 -0700
changeset 795929 e0d7e872980a2fd3366460029ec165a896548e91
parent 795928 26ef37bebe39c42e34ff1346ebb8d00936029f0b
push id110121
push userbmo:agashlin@mozilla.com
push dateWed, 16 May 2018 19:33:58 +0000
bugs603903
milestone62.0a1
Bug 603903 - Part 3: fix screen enumeration when not logged in MozReview-Commit-ID: LboLGStqpp0
widget/windows/ScreenHelperWin.cpp
--- a/widget/windows/ScreenHelperWin.cpp
+++ b/widget/windows/ScreenHelperWin.cpp
@@ -11,48 +11,57 @@
 #include "WinUtils.h"
 
 static mozilla::LazyLogModule sScreenLog("WidgetScreen");
 
 namespace mozilla {
 namespace widget {
 
 BOOL CALLBACK
-CollectMonitors(HMONITOR aMon, HDC hDCScreen, LPRECT, LPARAM ioParam)
+CollectMonitors(HMONITOR aMon, HDC, LPRECT, LPARAM ioParam)
 {
   auto screens = reinterpret_cast<nsTArray<RefPtr<Screen>>*>(ioParam);
   BOOL success = FALSE;
-  MONITORINFO info;
-  info.cbSize = sizeof(MONITORINFO);
+  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);
-  uint32_t pixelDepth = ::GetDeviceCaps(hDCScreen, BITSPIXEL);
+
+  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);
   MOZ_LOG(sScreenLog, LogLevel::Debug,
            ("New screen [%d %d %d %d (%d %d %d %d) %d %f %f %f]",
             rect.X(), rect.Y(), rect.Width(), rect.Height(),
             availRect.X(), availRect.Y(), availRect.Width(), availRect.Height(),
             pixelDepth, contentsScaleFactor.scale, defaultCssScaleFactor.scale,
             dpi));
   auto screen = new Screen(rect, availRect,
@@ -69,22 +78,19 @@ CollectMonitors(HMONITOR aMon, HDC hDCSc
 }
 
 void
 ScreenHelperWin::RefreshScreens()
 {
   MOZ_LOG(sScreenLog, LogLevel::Debug, ("Refreshing screens"));
 
   AutoTArray<RefPtr<Screen>, 4> screens;
-  HDC hdc = ::CreateDC(L"DISPLAY", nullptr, nullptr, nullptr);
-  NS_ASSERTION(hdc,"CreateDC Failure");
-  BOOL result = ::EnumDisplayMonitors(hdc, nullptr,
+  BOOL result = ::EnumDisplayMonitors(nullptr, nullptr,
                                       (MONITORENUMPROC)CollectMonitors,
                                       (LPARAM)&screens);
-  ::DeleteDC(hdc);
   if (!result) {
     NS_WARNING("Unable to EnumDisplayMonitors");
   }
   ScreenManager& screenManager = ScreenManager::GetSingleton();
   screenManager.Refresh(Move(screens));
 }
 
 } // namespace widget