WindowsScreenConfiguration.cpp (3287B)
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/. */ 4 5 #include "Hal.h" 6 #include "mozilla/widget/ScreenManager.h" 7 #include "nsIWindowsUIUtils.h" 8 #include "WinUtils.h" 9 10 #include <windows.h> 11 12 namespace mozilla { 13 namespace hal_impl { 14 15 static decltype(SetDisplayAutoRotationPreferences)* 16 sSetDisplayAutoRotationPreferences = nullptr; 17 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__); 24 } 25 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__); 30 } 31 32 if (!sSetDisplayAutoRotationPreferences) { 33 HMODULE user32dll = GetModuleHandleW(L"user32.dll"); 34 if (user32dll) { 35 sSetDisplayAutoRotationPreferences = 36 (decltype(SetDisplayAutoRotationPreferences)*)GetProcAddress( 37 user32dll, "SetDisplayAutoRotationPreferences"); 38 } 39 if (!sSetDisplayAutoRotationPreferences) { 40 return GenericNonExclusivePromise::CreateAndReject( 41 NS_ERROR_DOM_NOT_SUPPORTED_ERR, __func__); 42 } 43 } 44 45 ORIENTATION_PREFERENCE orientation = ORIENTATION_PREFERENCE_NONE; 46 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; 58 } 59 } else { 60 if (aOrientation & hal::ScreenOrientation::LandscapePrimary) { 61 orientation |= ORIENTATION_PREFERENCE_LANDSCAPE; 62 } 63 if (aOrientation & hal::ScreenOrientation::LandscapeSecondary) { 64 orientation |= ORIENTATION_PREFERENCE_LANDSCAPE_FLIPPED; 65 } 66 if (aOrientation & hal::ScreenOrientation::PortraitPrimary) { 67 orientation |= ORIENTATION_PREFERENCE_PORTRAIT; 68 } 69 if (aOrientation & hal::ScreenOrientation::PortraitSecondary) { 70 orientation |= ORIENTATION_PREFERENCE_PORTRAIT_FLIPPED; 71 } 72 } 73 74 if (!sSetDisplayAutoRotationPreferences(orientation)) { 75 return GenericNonExclusivePromise::CreateAndReject(NS_ERROR_DOM_ABORT_ERR, 76 __func__); 77 } 78 79 return GenericNonExclusivePromise::CreateAndResolve(true, __func__); 80 } 81 82 void UnlockScreenOrientation() { 83 if (!sSetDisplayAutoRotationPreferences) { 84 return; 85 } 86 // This does nothing if the device doesn't support orientation lock 87 sSetDisplayAutoRotationPreferences(ORIENTATION_PREFERENCE_NONE); 88 } 89 90 } // namespace hal_impl 91 } // namespace mozilla