diff --git a/dom/base/Makefile.in b/dom/base/Makefile.in index 74867bd35a45..5ff5a8b85bdf 100644 --- a/dom/base/Makefile.in +++ b/dom/base/Makefile.in @@ -106,6 +106,7 @@ EXPORTS_NAMESPACES = mozilla/dom EXPORTS_mozilla/dom = \ DOMError.h \ StructuredCloneTags.h \ + ScreenOrientation.h \ $(NULL) CPPSRCS = \ diff --git a/dom/base/ScreenOrientation.h b/dom/base/ScreenOrientation.h new file mode 100644 index 000000000000..90ba25c068df --- /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 de985bc9d094..6166e7fae8b2 100644 --- a/hal/Hal.cpp +++ b/hal/Hal.cpp @@ -25,6 +25,7 @@ #include "nsIDocShell.h" #include "mozilla/ClearOnShutdown.h" #include "WindowIdentifier.h" +#include "mozilla/dom/ScreenOrientation.h" using namespace mozilla::services; @@ -264,6 +265,24 @@ protected: static NetworkObserversManager sNetworkObservers; +class ScreenOrientationObserversManager : public ObserversManager +{ +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) { @@ -426,5 +445,33 @@ void PowerOff() PROXY_IF_SANDBOXED(PowerOff()); } +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 15e859f0999b..11149af3ef08 100644 --- a/hal/Hal.h +++ b/hal/Hal.h @@ -21,6 +21,7 @@ #include "mozilla/dom/battery/Types.h" #include "mozilla/dom/network/Types.h" #include "mozilla/hal_sandbox/PHal.h" +#include "mozilla/dom/ScreenOrientation.h" /* * Hal.h contains the public Hal API. @@ -41,8 +42,17 @@ class nsIDOMWindow; namespace mozilla { +template +class Observer; + +namespace dom { +class ScreenOrientationWrapper; +} + namespace hal { +typedef Observer ScreenOrientationObserver; + class WindowIdentifier; extern PRLogModuleInfo *sHalLog; @@ -233,6 +243,29 @@ void Reboot(); */ void PowerOff(); +/** + * 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 390e5f42dad7..ca7c544f7059 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 11194a24b527..71fbf65eda1b 100644 --- a/hal/Makefile.in +++ b/hal/Makefile.in @@ -69,6 +69,7 @@ CPPSRCS = \ Hal.cpp \ SandboxHal.cpp \ WindowIdentifier.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 000000000000..c10099464017 --- /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 5cabfab94817..090b27c76a9b 100644 --- a/hal/sandbox/PHal.ipdl +++ b/hal/sandbox/PHal.ipdl @@ -42,12 +42,14 @@ 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; using mozilla::hal::LightType; using mozilla::hal::LightMode; using mozilla::hal::SensorType; +using mozilla::dom::ScreenOrientation; namespace mozilla { @@ -72,9 +74,7 @@ namespace hal { PRTime timestamp; float[] values; }; -} -namespace hal { struct NetworkInformation { double bandwidth; bool canBeMetered; @@ -89,6 +89,7 @@ sync protocol PHal { child: NotifyBatteryChange(BatteryInformation aBatteryInfo); NotifyNetworkChange(NetworkInformation aNetworkInfo); + NotifyScreenOrientationChange(ScreenOrientation aScreenOrientation); parent: Vibrate(uint32[] pattern, uint64[] id, PBrowser browser); @@ -118,6 +119,11 @@ parent: Reboot(); PowerOff(); + EnableScreenOrientationNotifications(); + DisableScreenOrientationNotifications(); + sync GetCurrentScreenOrientation() + returns (ScreenOrientation aScreenOrientation); + child: NotifySensorChange(SensorData aSensorData); diff --git a/hal/sandbox/SandboxHal.cpp b/hal/sandbox/SandboxHal.cpp index 9e121332b5eb..d59b6be1485e 100644 --- a/hal/sandbox/SandboxHal.cpp +++ b/hal/sandbox/SandboxHal.cpp @@ -18,6 +18,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" @@ -97,6 +98,24 @@ GetCurrentNetworkInformation(NetworkInformation* aNetworkInfo) Hal()->SendGetCurrentNetworkInformation(aNetworkInfo); } +void +EnableScreenOrientationNotifications() +{ + Hal()->SendEnableScreenOrientationNotifications(); +} + +void +DisableScreenOrientationNotifications() +{ + Hal()->SendDisableScreenOrientationNotifications(); +} + +void +GetCurrentScreenOrientation(ScreenOrientation* aScreenOrientation) +{ + Hal()->SendGetCurrentScreenOrientation(aScreenOrientation); +} + bool GetScreenEnabled() { @@ -167,6 +186,7 @@ class HalParent : public PHalParent , public BatteryObserver , public NetworkObserver , public ISensorObserver + , public ScreenOrientationObserver { public: NS_OVERRIDE virtual bool @@ -252,6 +272,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) { @@ -341,6 +383,12 @@ public: hal::NotifyNetworkChange(aNetworkInfo); return true; } + + NS_OVERRIDE virtual bool + RecvNotifyScreenOrientationChange(const ScreenOrientation& aScreenOrientation) { + hal::NotifyScreenOrientationChange(aScreenOrientation); + return true; + } }; bool