no bug - Bumping Firefox l10n changesets r=release a=l10n-bump DONTBUILD CLOSED TREE
[gecko.git] / hal / windows / WindowsScreenConfiguration.cpp
blobd7f17d73bfcfd675244b2d9dff7ac4c8de7ddac7
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 file,
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #include "Hal.h"
6 #include "mozilla/widget/ScreenManager.h"
7 #include "nsIWindowsUIUtils.h"
8 #include "WinUtils.h"
10 #include <windows.h>
12 namespace mozilla {
13 namespace hal_impl {
15 static decltype(SetDisplayAutoRotationPreferences)*
16 sSetDisplayAutoRotationPreferences = nullptr;
18 RefPtr<GenericNonExclusivePromise> LockScreenOrientation(
19 const hal::ScreenOrientation& aOrientation) {
20 AR_STATE state;
21 if (!widget::WinUtils::GetAutoRotationState(&state)) {
22 return GenericNonExclusivePromise::CreateAndReject(
23 NS_ERROR_DOM_NOT_SUPPORTED_ERR, __func__);
26 if (state & (AR_DISABLED | AR_REMOTESESSION | AR_MULTIMON | AR_NOSENSOR |
27 AR_NOT_SUPPORTED | AR_LAPTOP | AR_DOCKED)) {
28 return GenericNonExclusivePromise::CreateAndReject(
29 NS_ERROR_DOM_NOT_SUPPORTED_ERR, __func__);
32 if (!sSetDisplayAutoRotationPreferences) {
33 HMODULE user32dll = GetModuleHandleW(L"user32.dll");
34 if (user32dll) {
35 sSetDisplayAutoRotationPreferences =
36 (decltype(SetDisplayAutoRotationPreferences)*)GetProcAddress(
37 user32dll, "SetDisplayAutoRotationPreferences");
39 if (!sSetDisplayAutoRotationPreferences) {
40 return GenericNonExclusivePromise::CreateAndReject(
41 NS_ERROR_DOM_NOT_SUPPORTED_ERR, __func__);
45 ORIENTATION_PREFERENCE orientation = ORIENTATION_PREFERENCE_NONE;
47 if (aOrientation == hal::ScreenOrientation::Default) {
48 // Actually, current screen is single and tablet mode according to
49 // GetAutoRotationState. So get primary screen data for natural orientation.
50 RefPtr<widget::Screen> screen =
51 widget::ScreenManager::GetSingleton().GetPrimaryScreen();
52 hal::ScreenOrientation defaultOrientation =
53 screen->GetDefaultOrientationType();
54 if (defaultOrientation == hal::ScreenOrientation::LandscapePrimary) {
55 orientation = ORIENTATION_PREFERENCE_LANDSCAPE;
56 } else {
57 orientation = ORIENTATION_PREFERENCE_PORTRAIT;
59 } else {
60 if (aOrientation & hal::ScreenOrientation::LandscapePrimary) {
61 orientation |= ORIENTATION_PREFERENCE_LANDSCAPE;
63 if (aOrientation & hal::ScreenOrientation::LandscapeSecondary) {
64 orientation |= ORIENTATION_PREFERENCE_LANDSCAPE_FLIPPED;
66 if (aOrientation & hal::ScreenOrientation::PortraitPrimary) {
67 orientation |= ORIENTATION_PREFERENCE_PORTRAIT;
69 if (aOrientation & hal::ScreenOrientation::PortraitSecondary) {
70 orientation |= ORIENTATION_PREFERENCE_PORTRAIT_FLIPPED;
74 if (!sSetDisplayAutoRotationPreferences(orientation)) {
75 return GenericNonExclusivePromise::CreateAndReject(NS_ERROR_DOM_ABORT_ERR,
76 __func__);
79 return GenericNonExclusivePromise::CreateAndResolve(true, __func__);
82 void UnlockScreenOrientation() {
83 if (!sSetDisplayAutoRotationPreferences) {
84 return;
86 // This does nothing if the device doesn't support orientation lock
87 sSetDisplayAutoRotationPreferences(ORIENTATION_PREFERENCE_NONE);
90 } // namespace hal_impl
91 } // namespace mozilla