Skip to content

Commit

Permalink
Bug 911242 - [LED] De-couple the control of screen backlight and keyb…
Browse files Browse the repository at this point in the history
…oard backlight. r=dhylands, sr=sicking
  • Loading branch information
seanyhlin committed May 30, 2014
1 parent c9d1aee commit 08364d3
Show file tree
Hide file tree
Showing 11 changed files with 237 additions and 46 deletions.
12 changes: 12 additions & 0 deletions dom/power/PowerManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,18 @@ PowerManager::SetScreenEnabled(bool aEnabled)
hal::SetScreenEnabled(aEnabled);
}

bool
PowerManager::KeyLightEnabled()
{
return hal::GetKeyLightEnabled();
}

void
PowerManager::SetKeyLightEnabled(bool aEnabled)
{
hal::SetKeyLightEnabled(aEnabled);
}

double
PowerManager::ScreenBrightness()
{
Expand Down
2 changes: 2 additions & 0 deletions dom/power/PowerManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ class PowerManager MOZ_FINAL : public nsIDOMMozWakeLockListener
ErrorResult& aRv);
bool ScreenEnabled();
void SetScreenEnabled(bool aEnabled);
bool KeyLightEnabled();
void SetKeyLightEnabled(bool aEnabled);
double ScreenBrightness();
void SetScreenBrightness(double aBrightness, ErrorResult& aRv);
bool CpuSleepAllowed();
Expand Down
2 changes: 2 additions & 0 deletions dom/power/test/mochitest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ skip-if = toolkit != "gonk"
skip-if = toolkit != "gonk"
[test_power_set_screen_enabled.html]
skip-if = toolkit != "gonk"
[test_power_set_key_light_enabled.html]
skip-if = toolkit != "gonk"
64 changes: 64 additions & 0 deletions dom/power/test/test_power_set_key_light_enabled.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Test Enabling/Disabling Screen with Power Management API</title>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
</head>
<body>
<p id="display"></p>
<div id="content" style="display: none"></div>
<pre id="test">
<script type="application/javascript">

"use strict";

function testEnableKeyLight() {
try {
navigator.mozPower.keyLightEnabled = true;
ok(navigator.mozPower.keyLightEnabled === true, "Enabled key backlight.");
} catch (e) {
ok(false, "Unexpected exception trying to enable key backlight.");
}
}

function testDisableKeyLight() {
try {
navigator.mozPower.keyLightEnabled = false;
ok(navigator.mozPower.keyLightEnabled === false, "Disabled key backlight.");
} catch (e) {
ok(false, "Unexpected exception trying to disable key backlight.");
}
}

function startTests() {

// Make sure we don't suspend
navigator.mozPower.cpuSleepAllowed = false;

testDisableKeyLight();
testEnableKeyLight();

SimpleTest.finish();
}

SimpleTest.waitForExplicitFinish();
if (SpecialPowers.hasPermission("power", document)) {
// Currently only applicable on FxOS
if (navigator.userAgent.indexOf("Mobile") != -1 &&
navigator.appVersion.indexOf("Android") == -1) {
startTests();
} else {
ok(true, "mozPower on Firefox OS only.");
SimpleTest.finish();
}
} else {
// Add the permission and reload so it's propogated
SpecialPowers.addPermission("power", true, document);
window.location.reload();
}
</script>
</pre>
</body>
</html>
7 changes: 7 additions & 0 deletions dom/webidl/MozPowerManager.webidl
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ interface MozPowerManager
*/
attribute boolean screenEnabled;

/**
* Is the device's keypad/button backlight enabled? Setting it to false will
* turn off the device's keypad/button backlight. And the brightness level
* is the same as |screenBrightness|.
*/
attribute boolean keyLightEnabled;

/**
* 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
Expand Down
24 changes: 18 additions & 6 deletions hal/Hal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -374,10 +374,22 @@ bool GetScreenEnabled()
RETURN_PROXY_IF_SANDBOXED(GetScreenEnabled(), false);
}

void SetScreenEnabled(bool enabled)
void SetScreenEnabled(bool aEnabled)
{
AssertMainThread();
PROXY_IF_SANDBOXED(SetScreenEnabled(enabled));
PROXY_IF_SANDBOXED(SetScreenEnabled(aEnabled));
}

bool GetKeyLightEnabled()
{
AssertMainThread();
RETURN_PROXY_IF_SANDBOXED(GetKeyLightEnabled(), false);
}

void SetKeyLightEnabled(bool aEnabled)
{
AssertMainThread();
PROXY_IF_SANDBOXED(SetKeyLightEnabled(aEnabled));
}

bool GetCpuSleepAllowed()
Expand All @@ -390,10 +402,10 @@ bool GetCpuSleepAllowed()
RETURN_PROXY_IF_SANDBOXED(GetCpuSleepAllowed(), true);
}

void SetCpuSleepAllowed(bool allowed)
void SetCpuSleepAllowed(bool aAllowed)
{
AssertMainThread();
PROXY_IF_SANDBOXED(SetCpuSleepAllowed(allowed));
PROXY_IF_SANDBOXED(SetCpuSleepAllowed(aAllowed));
}

double GetScreenBrightness()
Expand All @@ -402,10 +414,10 @@ double GetScreenBrightness()
RETURN_PROXY_IF_SANDBOXED(GetScreenBrightness(), 0);
}

void SetScreenBrightness(double brightness)
void SetScreenBrightness(double aBrightness)
{
AssertMainThread();
PROXY_IF_SANDBOXED(SetScreenBrightness(clamped(brightness, 0.0, 1.0)));
PROXY_IF_SANDBOXED(SetScreenBrightness(clamped(aBrightness, 0.0, 1.0)));
}

bool SetLight(LightType light, const LightConfiguration& aConfig)
Expand Down
16 changes: 13 additions & 3 deletions hal/Hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,17 @@ bool GetScreenEnabled();
*
* Note that it may take a few seconds for the screen to turn on or off.
*/
void SetScreenEnabled(bool enabled);
void SetScreenEnabled(bool aEnabled);

/**
* Determine whether the device's keypad/button backlight is currently enabled.
*/
bool GetKeyLightEnabled();

/**
* Enable or disable the device's keypad/button backlight.
*/
void SetKeyLightEnabled(bool aEnabled);

/**
* Get the brightness of the device's screen's backlight, on a scale from 0
Expand All @@ -145,7 +155,7 @@ double GetScreenBrightness();
* followed by GetScreenBrightness(), the value returned by
* GetScreenBrightness() may not be exactly x.
*/
void SetScreenBrightness(double brightness);
void SetScreenBrightness(double aBrightness);

/**
* Determine whether the device is allowed to sleep.
Expand All @@ -156,7 +166,7 @@ bool GetCpuSleepAllowed();
* Set whether the device is allowed to suspend automatically after
* the screen is disabled.
*/
void SetCpuSleepAllowed(bool allowed);
void SetCpuSleepAllowed(bool aAllowed);

/**
* Set the value of a light to a particular color, with a specific flash pattern.
Expand Down
14 changes: 12 additions & 2 deletions hal/fallback/FallbackScreenPower.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,17 @@ GetScreenEnabled()
}

void
SetScreenEnabled(bool enabled)
SetScreenEnabled(bool aEnabled)
{}

bool
GetKeyLightEnabled()
{
return true;
}

void
SetKeyLightEnabled(bool aEnabled)
{}

double
Expand All @@ -24,7 +34,7 @@ GetScreenBrightness()
}

void
SetScreenBrightness(double brightness)
SetScreenBrightness(double aBrightness)
{}

} // hal_impl
Expand Down
63 changes: 49 additions & 14 deletions hal/gonk/GonkHal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -540,21 +540,53 @@ GetScreenEnabled()
}

void
SetScreenEnabled(bool enabled)
SetScreenEnabled(bool aEnabled)
{
GetGonkDisplay()->SetEnabled(enabled);
sScreenEnabled = enabled;
GetGonkDisplay()->SetEnabled(aEnabled);
sScreenEnabled = aEnabled;
}

bool
GetKeyLightEnabled()
{
hal::LightConfiguration config;
hal_impl::GetLight(hal::eHalLightID_Buttons, &config);
return (config.color() != 0x00000000);
}

void
SetKeyLightEnabled(bool aEnabled)
{
hal::LightConfiguration config;
config.mode() = hal::eHalLightMode_User;
config.flash() = hal::eHalLightFlash_None;
config.flashOnMS() = config.flashOffMS() = 0;
config.color() = 0x00000000;

if (aEnabled) {
// Convert the value in [0, 1] to an int between 0 and 255 and then convert
// it to a color. Note that the high byte is FF, corresponding to the alpha
// channel.
double brightness = GetScreenBrightness();
uint32_t val = static_cast<int>(round(brightness * 255.0));
uint32_t color = (0xff<<24) + (val<<16) + (val<<8) + val;

config.color() = color;
}

hal_impl::SetLight(hal::eHalLightID_Buttons, config);
hal_impl::SetLight(hal::eHalLightID_Keyboard, config);
}

double
GetScreenBrightness()
{
hal::LightConfiguration aConfig;
hal::LightConfiguration config;
hal::LightType light = hal::eHalLightID_Backlight;

hal::GetLight(light, &aConfig);
hal_impl::GetLight(light, &config);
// backlight is brightness only, so using one of the RGB elements as value.
int brightness = aConfig.color() & 0xFF;
int brightness = config.color() & 0xFF;
return brightness / 255.0;
}

Expand All @@ -571,16 +603,19 @@ SetScreenBrightness(double brightness)

// Convert the value in [0, 1] to an int between 0 and 255 and convert to a color
// note that the high byte is FF, corresponding to the alpha channel.
int val = static_cast<int>(round(brightness * 255));
uint32_t val = static_cast<int>(round(brightness * 255.0));
uint32_t color = (0xff<<24) + (val<<16) + (val<<8) + val;

hal::LightConfiguration aConfig;
aConfig.mode() = hal::eHalLightMode_User;
aConfig.flash() = hal::eHalLightFlash_None;
aConfig.flashOnMS() = aConfig.flashOffMS() = 0;
aConfig.color() = color;
hal::SetLight(hal::eHalLightID_Backlight, aConfig);
hal::SetLight(hal::eHalLightID_Buttons, aConfig);
hal::LightConfiguration config;
config.mode() = hal::eHalLightMode_User;
config.flash() = hal::eHalLightFlash_None;
config.flashOnMS() = config.flashOffMS() = 0;
config.color() = color;
hal_impl::SetLight(hal::eHalLightID_Backlight, config);
if (GetKeyLightEnabled()) {
hal_impl::SetLight(hal::eHalLightID_Buttons, config);
hal_impl::SetLight(hal::eHalLightID_Keyboard, config);
}
}

static Monitor* sInternalLockCpuMonitor = nullptr;
Expand Down
9 changes: 6 additions & 3 deletions hal/sandbox/PHal.ipdl
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,16 @@ parent:
returns (NetworkInformation aNetworkInfo);

sync GetScreenEnabled() returns (bool enabled);
SetScreenEnabled(bool enabled);
SetScreenEnabled(bool aEnabled);

sync GetKeyLightEnabled() returns (bool enabled);
SetKeyLightEnabled(bool aEnabled);

sync GetCpuSleepAllowed() returns (bool allowed);
SetCpuSleepAllowed(bool allowed);
SetCpuSleepAllowed(bool aAllowed);

sync GetScreenBrightness() returns (double brightness);
SetScreenBrightness(double brightness);
SetScreenBrightness(double aBrightness);

AdjustSystemClock(int64_t aDeltaMilliseconds);
SetTimezone(nsCString aTimezoneSpec);
Expand Down
Loading

0 comments on commit 08364d3

Please sign in to comment.