diff --git a/dom/base/Makefile.in b/dom/base/Makefile.in index a9fc79f098e..17c56e6d7b7 100644 --- a/dom/base/Makefile.in +++ b/dom/base/Makefile.in @@ -113,6 +113,7 @@ EXPORTS_mozilla/dom = \ DOMError.h \ DOMRequest.h \ StructuredCloneTags.h \ + ScreenOrientation.h \ $(NULL) CPPSRCS = \ diff --git a/dom/base/ScreenOrientation.h b/dom/base/ScreenOrientation.h new file mode 100644 index 00000000000..90ba25c068d --- /dev/null +++ b/dom/base/ScreenOrientation.h @@ -0,0 +1,59 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef mozilla_dom_ScreenOrientation_h +#define mozilla_dom_ScreenOrientation_h + +namespace mozilla { +namespace dom { + +enum ScreenOrientation { + eScreenOrientation_Current = 0, + eScreenOrientation_PortraitPrimary = 1, // 00000001 + eScreenOrientation_PortraitSecondary = 2, // 00000010 + eScreenOrientation_Portrait = 3, // 00000011 + eScreenOrientation_LandscapePrimary = 4, // 00000100 + eScreenOrientation_LandscapeSecondary = 8, // 00001000 + eScreenOrientation_Landscape = 12, // 00001100 + eScreenOrientation_EndGuard +}; + +/** + * ScreenOrientationWrapper is a class wrapping ScreenOrientation so it can be + * used with Observer which is taking a class, not an enum. + * C++11 should make this useless. + */ +class ScreenOrientationWrapper { +public: + ScreenOrientationWrapper() + : orientation(eScreenOrientation_Current) + {} + + ScreenOrientationWrapper(ScreenOrientation aOrientation) + : orientation(aOrientation) + {} + + ScreenOrientation orientation; +}; + +} // namespace dom +} // namespace mozilla + +namespace IPC { + +/** + * Screen orientation serializer. + * Note that technically, 5, 6, 7, 9, 10 and 11 are illegal values but will + * not make the serializer to fail. We might want to write our own serializer. + */ +template <> +struct ParamTraits + : public EnumSerializer +{}; + +} // namespace IPC + +#endif // mozilla_dom_ScreenOrientation_h diff --git a/hal/Hal.cpp b/hal/Hal.cpp index 7596c38050c..76384d3d77d 100644 --- a/hal/Hal.cpp +++ b/hal/Hal.cpp @@ -19,6 +19,7 @@ #include "nsIDocShell.h" #include "mozilla/ClearOnShutdown.h" #include "WindowIdentifier.h" +#include "mozilla/dom/ScreenOrientation.h" using namespace mozilla::services; @@ -294,6 +295,24 @@ protected: static WakeLockObserversManager sWakeLockObservers; +class ScreenOrientationObserversManager : public CachingObserversManager +{ +protected: + void EnableNotifications() { + PROXY_IF_SANDBOXED(EnableScreenOrientationNotifications()); + } + + void DisableNotifications() { + PROXY_IF_SANDBOXED(DisableScreenOrientationNotifications()); + } + + void GetCurrentInformationInternal(dom::ScreenOrientationWrapper* aInfo) { + PROXY_IF_SANDBOXED(GetCurrentScreenOrientation(&(aInfo->orientation))); + } +}; + +static ScreenOrientationObserversManager sScreenOrientationObservers; + void RegisterBatteryObserver(BatteryObserver* aObserver) { @@ -508,5 +527,33 @@ NotifyWakeLockChange(const WakeLockInformation& aInfo) sWakeLockObservers.BroadcastInformation(aInfo); } +void +RegisterScreenOrientationObserver(hal::ScreenOrientationObserver* aObserver) +{ + AssertMainThread(); + sScreenOrientationObservers.AddObserver(aObserver); +} + +void +UnregisterScreenOrientationObserver(hal::ScreenOrientationObserver* aObserver) +{ + AssertMainThread(); + sScreenOrientationObservers.RemoveObserver(aObserver); +} + +void +GetCurrentScreenOrientation(dom::ScreenOrientation* aScreenOrientation) +{ + AssertMainThread(); + *aScreenOrientation = sScreenOrientationObservers.GetCurrentInformation().orientation; +} + +void +NotifyScreenOrientationChange(const dom::ScreenOrientation& aScreenOrientation) +{ + sScreenOrientationObservers.CacheInformation(dom::ScreenOrientationWrapper(aScreenOrientation)); + sScreenOrientationObservers.BroadcastCachedInformation(); +} + } // namespace hal } // namespace mozilla diff --git a/hal/Hal.h b/hal/Hal.h index 15072f464f1..e20eb3f319b 100644 --- a/hal/Hal.h +++ b/hal/Hal.h @@ -16,6 +16,7 @@ #include "mozilla/dom/network/Types.h" #include "mozilla/dom/power/Types.h" #include "mozilla/hal_sandbox/PHal.h" +#include "mozilla/dom/ScreenOrientation.h" /* * Hal.h contains the public Hal API. @@ -36,8 +37,17 @@ class nsIDOMWindow; namespace mozilla { +template +class Observer; + +namespace dom { +class ScreenOrientationWrapper; +} + namespace hal { +typedef Observer ScreenOrientationObserver; + class WindowIdentifier; extern PRLogModuleInfo *sHalLog; @@ -290,6 +300,29 @@ void GetWakeLockInfo(const nsAString &aTopic, hal::WakeLockInformation *aWakeLoc */ void NotifyWakeLockChange(const hal::WakeLockInformation& aWakeLockInfo); +/** + * Inform the backend there is a new screen orientation observer. + * @param aScreenOrientationObserver The observer that should be added. + */ +void RegisterScreenOrientationObserver(hal::ScreenOrientationObserver* aScreenOrientationObserver); + +/** + * Inform the backend a screen orientation observer unregistered. + * @param aScreenOrientationObserver The observer that should be removed. + */ +void UnregisterScreenOrientationObserver(hal::ScreenOrientationObserver* aScreenOrientationObserver); + +/** + * Returns the current screen orientation. + */ +void GetCurrentScreenOrientation(dom::ScreenOrientation* aScreenOrientation); + +/** + * Notify of a change in the screen orientation. + * @param aScreenOrientation The new screen orientation. + */ +void NotifyScreenOrientationChange(const dom::ScreenOrientation& aScreenOrientation); + } // namespace MOZ_HAL_NAMESPACE } // namespace mozilla diff --git a/hal/HalInternal.h b/hal/HalInternal.h index 390e5f42dad..ca7c544f705 100644 --- a/hal/HalInternal.h +++ b/hal/HalInternal.h @@ -77,6 +77,16 @@ void EnableNetworkNotifications(); */ void DisableNetworkNotifications(); +/** + * Enables screen orientation notifications from the backend. + */ +void EnableScreenOrientationNotifications(); + +/** + * Disables screen orientation notifications from the backend. + */ +void DisableScreenOrientationNotifications(); + } // namespace MOZ_HAL_NAMESPACE } // namespace mozilla diff --git a/hal/Makefile.in b/hal/Makefile.in index 8fde9d4d562..37dcb7d3374 100644 --- a/hal/Makefile.in +++ b/hal/Makefile.in @@ -71,6 +71,7 @@ CPPSRCS = \ SandboxHal.cpp \ WindowIdentifier.cpp \ HalWakeLock.cpp \ + ScreenOrientationFallback.cpp \ $(NULL) ifeq (android,$(MOZ_WIDGET_TOOLKIT)) diff --git a/hal/fallback/ScreenOrientationFallback.cpp b/hal/fallback/ScreenOrientationFallback.cpp new file mode 100644 index 00000000000..c1009946401 --- /dev/null +++ b/hal/fallback/ScreenOrientationFallback.cpp @@ -0,0 +1,45 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "Hal.h" +#include "mozilla/dom/ScreenOrientation.h" +#include "nsIScreenManager.h" + +namespace mozilla { +namespace hal_impl { + +void +EnableScreenOrientationNotifications() +{ +} + +void +DisableScreenOrientationNotifications() +{ +} + +void +GetCurrentScreenOrientation(dom::ScreenOrientation* aScreenOrientation) +{ + nsresult result; + nsCOMPtr screenMgr = + do_GetService("@mozilla.org/gfx/screenmanager;1", &result); + if (NS_FAILED(result)) { + NS_ERROR("Can't find nsIScreenManager!"); + return; + } + + PRInt32 screenLeft, screenTop, screenWidth, screenHeight; + nsCOMPtr screen; + + screenMgr->GetPrimaryScreen(getter_AddRefs(screen)); + screen->GetRect(&screenLeft, &screenTop, &screenWidth, &screenHeight); + + *aScreenOrientation = screenWidth >= screenHeight + ? dom::eScreenOrientation_LandscapePrimary + : dom::eScreenOrientation_PortraitPrimary; +} + +} // hal_impl +} // mozilla diff --git a/hal/sandbox/PHal.ipdl b/hal/sandbox/PHal.ipdl index cdfd51397a1..d83b5d4bc6b 100644 --- a/hal/sandbox/PHal.ipdl +++ b/hal/sandbox/PHal.ipdl @@ -42,6 +42,7 @@ include protocol PBrowser; include "nspr/prtime.h"; include "mozilla/HalSensor.h"; include "mozilla/HalTypes.h"; +include "mozilla/dom/ScreenOrientation.h"; using PRTime; using mozilla::hal::FlashMode; @@ -49,6 +50,7 @@ using mozilla::hal::LightType; using mozilla::hal::LightMode; using mozilla::hal::SensorType; using mozilla::hal::WakeLockControl; +using mozilla::dom::ScreenOrientation; namespace mozilla { @@ -73,9 +75,7 @@ namespace hal { PRTime timestamp; float[] values; }; -} -namespace hal { struct NetworkInformation { double bandwidth; bool canBeMetered; @@ -99,6 +99,7 @@ child: NotifyBatteryChange(BatteryInformation aBatteryInfo); NotifyNetworkChange(NetworkInformation aNetworkInfo); NotifyWakeLockChange(WakeLockInformation aWakeLockInfo); + NotifyScreenOrientationChange(ScreenOrientation aScreenOrientation); parent: Vibrate(uint32[] pattern, uint64[] id, PBrowser browser); @@ -137,6 +138,11 @@ parent: sync GetWakeLockInfo(nsString aTopic) returns (WakeLockInformation aWakeLockInfo); + EnableScreenOrientationNotifications(); + DisableScreenOrientationNotifications(); + sync GetCurrentScreenOrientation() + returns (ScreenOrientation aScreenOrientation); + child: NotifySensorChange(SensorData aSensorData); diff --git a/hal/sandbox/SandboxHal.cpp b/hal/sandbox/SandboxHal.cpp index 0f190f5b062..b2855d773e5 100644 --- a/hal/sandbox/SandboxHal.cpp +++ b/hal/sandbox/SandboxHal.cpp @@ -12,6 +12,7 @@ #include "mozilla/dom/TabChild.h" #include "mozilla/dom/battery/Types.h" #include "mozilla/dom/network/Types.h" +#include "mozilla/dom/ScreenOrientation.h" #include "mozilla/Observer.h" #include "mozilla/unused.h" #include "WindowIdentifier.h" @@ -91,6 +92,24 @@ GetCurrentNetworkInformation(NetworkInformation* aNetworkInfo) Hal()->SendGetCurrentNetworkInformation(aNetworkInfo); } +void +EnableScreenOrientationNotifications() +{ + Hal()->SendEnableScreenOrientationNotifications(); +} + +void +DisableScreenOrientationNotifications() +{ + Hal()->SendDisableScreenOrientationNotifications(); +} + +void +GetCurrentScreenOrientation(ScreenOrientation* aScreenOrientation) +{ + Hal()->SendGetCurrentScreenOrientation(aScreenOrientation); +} + bool GetScreenEnabled() { @@ -198,6 +217,7 @@ class HalParent : public PHalParent , public NetworkObserver , public ISensorObserver , public WakeLockObserver + , public ScreenOrientationObserver { public: NS_OVERRIDE virtual bool @@ -283,6 +303,28 @@ public: unused << SendNotifyNetworkChange(aNetworkInfo); } + NS_OVERRIDE virtual bool + RecvEnableScreenOrientationNotifications() { + hal::RegisterScreenOrientationObserver(this); + return true; + } + + NS_OVERRIDE virtual bool + RecvDisableScreenOrientationNotifications() { + hal::UnregisterScreenOrientationObserver(this); + return true; + } + + NS_OVERRIDE virtual bool + RecvGetCurrentScreenOrientation(ScreenOrientation* aScreenOrientation) { + hal::GetCurrentScreenOrientation(aScreenOrientation); + return true; + } + + void Notify(const ScreenOrientationWrapper& aScreenOrientation) { + unused << SendNotifyScreenOrientationChange(aScreenOrientation.orientation); + } + NS_OVERRIDE virtual bool RecvGetScreenEnabled(bool *enabled) { @@ -427,6 +469,12 @@ public: hal::NotifyWakeLockChange(aWakeLockInfo); return true; } + + NS_OVERRIDE virtual bool + RecvNotifyScreenOrientationChange(const ScreenOrientation& aScreenOrientation) { + hal::NotifyScreenOrientationChange(aScreenOrientation); + return true; + } }; bool