Bug 616664 - nsIScreen interface for keeping the screen on. r=blassey sr=roc a=blocking-fennec

This commit is contained in:
Alon Zakai 2010-12-21 14:33:48 -08:00
parent 8a02f568a0
commit d7e10068a5
3 changed files with 121 additions and 0 deletions

View File

@ -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);
};

View File

@ -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

View File

@ -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<nsIWidget> 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