tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

WindowsDpiInitialization.cpp (2483B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 #include "mozilla/WindowsDpiInitialization.h"
      6 
      7 #include "mozilla/DynamicallyLinkedFunctionPtr.h"
      8 #include "mozilla/WindowsProcessMitigations.h"
      9 #include "mozilla/WindowsVersion.h"
     10 
     11 #include <shellscalingapi.h>
     12 #include <windows.h>
     13 
     14 namespace mozilla {
     15 
     16 typedef HRESULT(WINAPI* SetProcessDpiAwarenessType)(PROCESS_DPI_AWARENESS);
     17 typedef BOOL(WINAPI* SetProcessDpiAwarenessContextType)(DPI_AWARENESS_CONTEXT);
     18 
     19 WindowsDpiInitializationResult WindowsDpiInitialization() {
     20  // DPI Awareness can't be used in a Win32k Lockdown process, so there's
     21  // nothing to do
     22  if (IsWin32kLockedDown()) {
     23    return WindowsDpiInitializationResult::Success;
     24  }
     25 
     26  // From MSDN:
     27  //  SetProcessDpiAwarenessContext() was added in the Win10 Anniversary Update
     28  //  SetProcessDpiAwareness() was added in Windows 8.1
     29  //  SetProcessDpiAware() was added in Windows Vista
     30  //
     31  //  DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 wasn't added later until
     32  //  the Creators Update, so if it fails we just fall back to
     33  //  DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE
     34  if (IsWin10AnniversaryUpdateOrLater()) {
     35    DynamicallyLinkedFunctionPtr<SetProcessDpiAwarenessContextType>
     36        setProcessDpiAwarenessContext(L"user32.dll",
     37                                      "SetProcessDpiAwarenessContext");
     38    if (!setProcessDpiAwarenessContext) {
     39      return WindowsDpiInitializationResult::
     40          FindSetProcessDpiAwarenessContextFailed;
     41    }
     42 
     43    if (!setProcessDpiAwarenessContext(
     44            DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2) &&
     45        !setProcessDpiAwarenessContext(
     46            DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE)) {
     47      return WindowsDpiInitializationResult::
     48          SetProcessDpiAwarenessContextFailed;
     49    }
     50 
     51    return WindowsDpiInitializationResult::Success;
     52  } else {
     53    DynamicallyLinkedFunctionPtr<SetProcessDpiAwarenessType>
     54        setProcessDpiAwareness(L"Shcore.dll", "SetProcessDpiAwareness");
     55    if (!setProcessDpiAwareness) {
     56      return WindowsDpiInitializationResult::FindSetProcessDpiAwarenessFailed;
     57    }
     58 
     59    if (FAILED(setProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE))) {
     60      return WindowsDpiInitializationResult::SetProcessDpiAwarenessFailed;
     61    }
     62 
     63    return WindowsDpiInitializationResult::Success;
     64  }
     65 }
     66 
     67 }  // namespace mozilla