Bug 702256 - "[Gonk] Add DOM API for turning screen on/off and adjusting the screen's brightness" r=cjones,mounir

--HG--
extra : rebase_source : 6c1f1b49d911e749da524266695e2f05d52900b2
This commit is contained in:
Justin Lebar 2011-12-05 01:07:00 +08:00
parent 0c1547e22e
commit e33d8ba71a
11 changed files with 310 additions and 40 deletions

View File

@ -36,14 +36,33 @@
*
* ***** END LICENSE BLOCK ***** */
#include "nscore.h"
#include "mozilla/Hal.h"
#include "nsScreen.h"
#include "nsIDocShell.h"
#include "nsPresContext.h"
#include "nsCOMPtr.h"
#include "nsDOMClassInfoID.h"
#include "nsIInterfaceRequestorUtils.h"
#include "nsIDocShellTreeItem.h"
#include "nsLayoutUtils.h"
#include "mozilla/Preferences.h"
using namespace mozilla;
/* static */ bool nsScreen::sInitialized = false;
/* static */ bool nsScreen::sAllowScreenEnabledProperty = false;
/* static */ bool nsScreen::sAllowScreenBrightnessProperty = false;
/* static */ void
nsScreen::Initialize()
{
MOZ_ASSERT(!sInitialized);
sInitialized = true;
Preferences::AddBoolVarCache(&sAllowScreenEnabledProperty,
"dom.screenEnabledProperty.enabled");
Preferences::AddBoolVarCache(&sAllowScreenBrightnessProperty,
"dom.screenBrightnessProperty.enabled");
}
//
// Screen class implementation
@ -51,6 +70,9 @@
nsScreen::nsScreen(nsIDocShell* aDocShell)
: mDocShell(aDocShell)
{
if (!sInitialized) {
Initialize();
}
}
nsScreen::~nsScreen()
@ -237,3 +259,69 @@ nsScreen::GetAvailRect(nsRect& aRect)
return NS_OK;
}
namespace {
bool
IsChromeType(nsIDocShell *aDocShell)
{
nsCOMPtr<nsIDocShellTreeItem> ds = do_QueryInterface(aDocShell);
if (!ds) {
return false;
}
PRInt32 itemType;
ds->GetItemType(&itemType);
return itemType == nsIDocShellTreeItem::typeChrome;
}
} // anonymous namespace
nsresult
nsScreen::GetMozEnabled(bool *aEnabled)
{
if (!sAllowScreenEnabledProperty || !IsChromeType(mDocShell)) {
*aEnabled = true;
return NS_OK;
}
*aEnabled = hal::GetScreenEnabled();
return NS_OK;
}
nsresult
nsScreen::SetMozEnabled(bool aEnabled)
{
if (!sAllowScreenEnabledProperty || !IsChromeType(mDocShell)) {
return NS_OK;
}
// TODO bug 707589: When the screen's state changes, all visible windows
// should fire a visibility change event.
hal::SetScreenEnabled(aEnabled);
return NS_OK;
}
nsresult
nsScreen::GetMozBrightness(double *aBrightness)
{
if (!sAllowScreenBrightnessProperty || !IsChromeType(mDocShell)) {
*aBrightness = 1;
return NS_OK;
}
*aBrightness = hal::GetScreenBrightness();
return NS_OK;
}
nsresult
nsScreen::SetMozBrightness(double aBrightness)
{
if (!sAllowScreenBrightnessProperty || !IsChromeType(mDocShell)) {
return NS_OK;
}
NS_ENSURE_TRUE(0 <= aBrightness && aBrightness <= 1, NS_ERROR_INVALID_ARG);
hal::SetScreenBrightness(aBrightness);
return NS_OK;
}

View File

@ -64,6 +64,13 @@ protected:
nsresult GetAvailRect(nsRect& aRect);
nsIDocShell* mDocShell; // Weak Reference
private:
static bool sInitialized;
static bool sAllowScreenEnabledProperty;
static bool sAllowScreenBrightnessProperty;
static void Initialize();
};
#endif /* nsScreen_h___ */

View File

@ -39,7 +39,7 @@
#include "domstubs.idl"
[scriptable, uuid(77947960-b4af-11d2-bd93-00805f8ae3f4)]
[scriptable, uuid(4507e43f-097c-452a-bfc4-dbb99748f6fd)]
interface nsIDOMScreen : nsISupports
{
readonly attribute long top;
@ -52,4 +52,27 @@ interface nsIDOMScreen : nsISupports
readonly attribute long availHeight;
readonly attribute long availLeft;
readonly attribute long availTop;
/**
* Is the device's screen currently enabled? This attribute controls the
* device's screen, so setting it to false will turn off the screen.
*/
attribute boolean mozEnabled;
/**
* How bright is the screen's backlight, on a scale from 0 (very dim) to 1
* (full brightness)? Setting this attribute modifies the screen's
* brightness.
*
* You can read and write this attribute even when the screen is disabled,
* but the backlight is off while the screen is disabled.
*
* If you write a value of X into this attribute, the attribute may not have
* the same value X when you later read it. Most screens don't support as
* many different brightness levels as there are doubles between 0 and 1, so
* we may reduce the value's precision before storing it.
*
* @throw NS_ERROR_INVALID_ARG if brightness is not in the range [0, 1].
*/
attribute double mozBrightness;
};

View File

@ -66,6 +66,15 @@ using namespace mozilla::services;
} \
} while (0)
#define RETURN_PROXY_IF_SANDBOXED(_call) \
do { \
if (InSandbox()) { \
return hal_sandbox::_call; \
} else { \
return hal_impl::_call; \
} \
} while (0)
namespace mozilla {
namespace hal {
@ -277,7 +286,7 @@ CancelVibrate(const WindowIdentifier &id)
hal_impl::CancelVibrate(WindowIdentifier());
}
}
class BatteryObserversManager
{
public:
@ -372,5 +381,29 @@ void NotifyBatteryChange(const BatteryInformation& aBatteryInfo)
sBatteryObservers.Broadcast(aBatteryInfo);
}
bool GetScreenEnabled()
{
AssertMainThread();
RETURN_PROXY_IF_SANDBOXED(GetScreenEnabled());
}
void SetScreenEnabled(bool enabled)
{
AssertMainThread();
PROXY_IF_SANDBOXED(SetScreenEnabled(enabled));
}
double GetScreenBrightness()
{
AssertMainThread();
RETURN_PROXY_IF_SANDBOXED(GetScreenBrightness());
}
void SetScreenBrightness(double brightness)
{
AssertMainThread();
PROXY_IF_SANDBOXED(SetScreenBrightness(clamped(brightness, 0.0, 1.0)));
}
} // namespace hal
} // namespace mozilla

View File

@ -257,6 +257,39 @@ void GetCurrentBatteryInformation(hal::BatteryInformation* aBatteryInfo);
*/
void NotifyBatteryChange(const hal::BatteryInformation& aBatteryInfo);
/**
* Determine whether the device's screen is currently enabled.
*/
bool GetScreenEnabled();
/**
* Enable or disable the device's screen.
*
* Note that it may take a few seconds for the screen to turn on or off.
*/
void SetScreenEnabled(bool enabled);
/**
* Get the brightness of the device's screen's backlight, on a scale from 0
* (very dim) to 1 (full blast).
*
* If the display is currently disabled, this returns the brightness the
* backlight will have when the display is re-enabled.
*/
double GetScreenBrightness();
/**
* Set the brightness of the device's screen's backlight, on a scale from 0
* (very dimm) to 1 (full blast). Values larger than 1 are treated like 1, and
* values smaller than 0 are treated like 0.
*
* Note that we may reduce the resolution of the given brightness value before
* sending it to the screen. Therefore if you call SetScreenBrightness(x)
* followed by GetScreenBrightness(), the value returned by
* GetScreenBrightness() may not be exactly x.
*/
void SetScreenBrightness(double brightness);
} // namespace MOZ_HAL_NAMESPACE
} // namespace mozilla

View File

@ -105,6 +105,26 @@ GetCurrentBatteryInformation(hal::BatteryInformation* aBatteryInfo)
bridge->GetCurrentBatteryInformation(aBatteryInfo);
}
bool
GetScreenEnabled()
{
return true;
}
void
SetScreenEnabled(bool enabled)
{}
double
GetScreenBrightness()
{
return 1;
}
void
SetScreenBrightness(double brightness)
{}
} // hal_impl
} // mozilla

View File

@ -69,5 +69,25 @@ GetCurrentBatteryInformation(hal::BatteryInformation* aBatteryInfo)
aBatteryInfo->remainingTime() = dom::battery::kDefaultRemainingTime;
}
bool
GetScreenEnabled()
{
return true;
}
void
SetScreenEnabled(bool enabled)
{}
double
GetScreenBrightness()
{
return 1;
}
void
SetScreenBrightness(double brightness)
{}
} // hal_impl
} // namespace mozilla

View File

@ -70,6 +70,26 @@ GetCurrentBatteryInformation(hal::BatteryInformation* aBatteryInfo)
}
#endif // !MOZ_ENABLE_DBUS
bool
GetScreenEnabled()
{
return true;
}
void
SetScreenEnabled(bool enabled)
{}
double
GetScreenBrightness()
{
return 1;
}
void
SetScreenBrightness(double brightness)
{}
} // hal_impl
} // mozilla

View File

@ -67,6 +67,12 @@ parent:
sync GetCurrentBatteryInformation()
returns (BatteryInformation aBatteryInfo);
sync GetScreenEnabled() returns (bool enabled);
SetScreenEnabled(bool enabled);
sync GetScreenBrightness() returns (double brightness);
SetScreenBrightness(double brightness);
__delete__();
};

View File

@ -104,6 +104,34 @@ GetCurrentBatteryInformation(BatteryInformation* aBatteryInfo)
Hal()->SendGetCurrentBatteryInformation(aBatteryInfo);
}
bool
GetScreenEnabled()
{
bool enabled = false;
Hal()->SendGetScreenEnabled(&enabled);
return enabled;
}
void
SetScreenEnabled(bool enabled)
{
Hal()->SendSetScreenEnabled(enabled);
}
double
GetScreenBrightness()
{
double brightness = 0;
Hal()->SendGetScreenBrightness(&brightness);
return brightness;
}
void
SetScreenBrightness(double brightness)
{
Hal()->SendSetScreenBrightness(brightness);
}
class HalParent : public PHalParent
, public BatteryObserver {
public:
@ -167,6 +195,34 @@ public:
void Notify(const BatteryInformation& aBatteryInfo) {
unused << SendNotifyBatteryChange(aBatteryInfo);
}
NS_OVERRIDE virtual bool
RecvGetScreenEnabled(bool *enabled)
{
*enabled = hal::GetScreenEnabled();
return true;
}
NS_OVERRIDE virtual bool
RecvSetScreenEnabled(const bool &enabled)
{
hal::SetScreenEnabled(enabled);
return true;
}
NS_OVERRIDE virtual bool
RecvGetScreenBrightness(double *brightness)
{
*brightness = hal::GetScreenBrightness();
return true;
}
NS_OVERRIDE virtual bool
RecvSetScreenBrightness(const double &brightness)
{
hal::SetScreenBrightness(brightness);
return true;
}
};
class HalChild : public PHalChild {

View File

@ -244,30 +244,6 @@ sendSpecialKeyEvent(nsIAtom *command, const struct timeval &time)
nsWindow::DispatchInputEvent(event);
}
static int screenFd = -1;
static bool sScreenOn = true;
static void
handlePowerKeyPressed()
{
if (screenFd == -1) {
screenFd = open("/sys/power/state", O_RDWR);
if (screenFd < 0) {
LOG("Unable to open /sys/power/state.");
return;
}
}
// No idea why the screen-off string is "mem" rather than "off".
const char *state = sScreenOn ? "mem" : "on";
if (write(screenFd, state, strlen(state)) < 0) {
LOG("Unable to write to /sys/power/state.");
return;
}
sScreenOn = !sScreenOn;
}
static void
keyHandler(int fd, FdHandler *data)
{
@ -299,13 +275,6 @@ keyHandler(int fd, FdHandler *data)
continue;
}
if (!sScreenOn && e.code != KEY_POWER) {
LOG("Ignoring key event, because the screen is off. "
"type 0x%04x code 0x%04x value %d",
e.type, e.code, e.value);
continue;
}
bool pressed = e.value == 1;
const char* upOrDown = pressed ? "pressed" : "released";
switch (e.code) {
@ -324,12 +293,7 @@ keyHandler(int fd, FdHandler *data)
sendKeyEvent(NS_VK_HOME, pressed, e.time);
break;
case KEY_POWER:
LOG("Power key %s", upOrDown);
// Ideally, we'd send a NS_VK_SLEEP event here and let the front-end
// sort out what to do. But for now, let's just turn off the screen
// ourselves.
if (pressed)
handlePowerKeyPressed();
sendKeyEvent(NS_VK_SLEEP, pressed, e.time);
break;
case KEY_VOLUMEUP:
if (pressed)