From 241c8fabab919b046da1ba1dd5eb6e6f263a1868 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Tue, 21 Dec 2010 14:33:48 -0800 Subject: [PATCH] Bug 616664 - nsIScreen interface for keeping the screen on. r=blassey sr=roc a=blocking-fennec --- widget/public/nsIScreen.idl | 31 +++++++++++++++++++ widget/src/shared/WidgetUtils.cpp | 49 +++++++++++++++++++++++++++++++ widget/src/shared/WidgetUtils.h | 41 ++++++++++++++++++++++++++ 3 files changed, 121 insertions(+) diff --git a/widget/public/nsIScreen.idl b/widget/public/nsIScreen.idl index 668342a4eef..1acca42629c 100644 --- a/widget/public/nsIScreen.idl +++ b/widget/public/nsIScreen.idl @@ -53,3 +53,34 @@ interface nsIScreen : nsISupports %{ C++ %} + +[scriptable, uuid(f7c93d20-c4e4-4628-b343-cb5530b04f15)] +interface nsIScreen_MOZILLA_2_0_BRANCH : nsISupports { + /** + * Levels of brightness for the screen, from off to full brightness. + */ + const unsigned long BRIGHTNESS_DIM = 0; + const unsigned long BRIGHTNESS_FULL = 1; + + /* The number of different brightness levels */ + const unsigned long BRIGHTNESS_LEVELS = 2; + + /** + * Locks the minimum brightness of the screen, forcing it to be at + * least as bright as a certain brightness level. Each call to this + * function must eventually be followed by a corresponding call to + * unlockMinimumBrightness, with the same brightness level. + * + * @param brightness A brightness level, one of the above constants. + */ + void lockMinimumBrightness(in unsigned long brightness); + + /** + * Releases a lock on the screen brightness. This must be called + * (eventually) after a corresponding call to lockMinimumBrightness. + * + * @param brightness A brightness level, one of the above constants. + */ + void unlockMinimumBrightness(in unsigned long brightness); +}; + diff --git a/widget/src/shared/WidgetUtils.cpp b/widget/src/shared/WidgetUtils.cpp index fc5a78fb9a6..133a475cc1e 100644 --- a/widget/src/shared/WidgetUtils.cpp +++ b/widget/src/shared/WidgetUtils.cpp @@ -80,5 +80,54 @@ WidgetUtils::DOMWindowToWidget(nsIDOMWindow *aDOMWindow) return widget.forget(); } +// class BrightnessLockingWidget + +BrightnessLockingWidget::BrightnessLockingWidget() +{ + for (PRUint32 i = 0; i < nsIScreen_MOZILLA_2_0_BRANCH::BRIGHTNESS_LEVELS; i++) + mBrightnessLocks[i] = 0; +} + +NS_IMETHODIMP +BrightnessLockingWidget::LockMinimumBrightness(PRUint32 aBrightness) +{ + NS_ABORT_IF_FALSE( + aBrightness < nsIScreen_MOZILLA_2_0_BRANCH::BRIGHTNESS_LEVELS, + "Invalid brightness level to lock"); + mBrightnessLocks[aBrightness]++; + NS_ABORT_IF_FALSE(mBrightnessLocks[aBrightness] > 0, + "Overflow after locking brightness level"); + + CheckMinimumBrightness(); + + return NS_OK; +} + +NS_IMETHODIMP +BrightnessLockingWidget::UnlockMinimumBrightness(PRUint32 aBrightness) +{ + NS_ABORT_IF_FALSE( + aBrightness < nsIScreen_MOZILLA_2_0_BRANCH::BRIGHTNESS_LEVELS, + "Invalid brightness level to lock"); + NS_ABORT_IF_FALSE(mBrightnessLocks[aBrightness] > 0, + "Unlocking a brightness level with no corresponding lock"); + mBrightnessLocks[aBrightness]--; + + CheckMinimumBrightness(); + + return NS_OK; +} + +void +BrightnessLockingWidget::CheckMinimumBrightness() +{ + PRUint32 brightness = nsIScreen_MOZILLA_2_0_BRANCH::BRIGHTNESS_LEVELS; + for (PRUint32 i = 0; i < nsIScreen_MOZILLA_2_0_BRANCH::BRIGHTNESS_LEVELS; i++) + if (mBrightnessLocks[i] > 0) + brightness = i; + + ApplyMinimumBrightness(brightness); +} + } // namespace widget } // namespace mozilla diff --git a/widget/src/shared/WidgetUtils.h b/widget/src/shared/WidgetUtils.h index 8ab96732d43..32800caa647 100644 --- a/widget/src/shared/WidgetUtils.h +++ b/widget/src/shared/WidgetUtils.h @@ -45,6 +45,7 @@ #include "nsIWidget.h" #include "nsPIDOMWindow.h" #include "nsIDOMWindow.h" +#include "nsIScreen.h" namespace mozilla { namespace widget { @@ -60,6 +61,46 @@ public: static already_AddRefed DOMWindowToWidget(nsIDOMWindow *aDOMWindow); }; +/** + * Simple management of screen brightness locks. This abstract base class + * allows all widget implementations to share brightness locking code. + */ +class BrightnessLockingWidget : public nsIScreen_MOZILLA_2_0_BRANCH +{ +public: + BrightnessLockingWidget(); + + NS_IMETHOD LockMinimumBrightness(PRUint32 aBrightness); + NS_IMETHOD UnlockMinimumBrightness(PRUint32 aBrightness); + +protected: + /** + * Manually set the current level of brightness locking. This is called after + * we determine, based on the current active locks, what the strongest + * lock is. You should normally not call this function - it will be + * called automatically by this class. + * + * Each widget implementation should implement this in a way that + * makes sense there. This is normally the only function that + * contains widget-specific code. + * + * @param aBrightness The current brightness level to set. If this is + * nsIScreen_MOZILLA_2_0_BRANCH::BRIGHTNESS_LEVELS + * (an impossible value for a brightness level to be), + * then that signifies that there is no current + * minimum brightness level, and the screen can shut off. + */ + virtual void ApplyMinimumBrightness(PRUint32 aBrightness) = 0; + + /** + * Checks what the minimum brightness value is, and calls + * ApplyMinimumBrightness. + */ + void CheckMinimumBrightness(); + + PRUint32 mBrightnessLocks[nsIScreen_MOZILLA_2_0_BRANCH::BRIGHTNESS_LEVELS]; +}; + } // namespace widget } // namespace mozilla