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/. */
6 #include "mozilla/widget/ScreenManager.h"
7 #include "nsIWindowsUIUtils.h"
15 static decltype(SetDisplayAutoRotationPreferences
)*
16 sSetDisplayAutoRotationPreferences
= nullptr;
18 RefPtr
<GenericNonExclusivePromise
> LockScreenOrientation(
19 const hal::ScreenOrientation
& aOrientation
) {
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");
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
;
57 orientation
= ORIENTATION_PREFERENCE_PORTRAIT
;
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
,
79 return GenericNonExclusivePromise::CreateAndResolve(true, __func__
);
82 void UnlockScreenOrientation() {
83 if (!sSetDisplayAutoRotationPreferences
) {
86 // This does nothing if the device doesn't support orientation lock
87 sSetDisplayAutoRotationPreferences(ORIENTATION_PREFERENCE_NONE
);
90 } // namespace hal_impl
91 } // namespace mozilla