Bug 616664 - nsIScreen interface for keeping the screen on. r=blassey sr=roc a=blocking-fennec
authorAlon Zakai <azakai@mozilla.com>
Tue, 21 Dec 2010 14:33:48 -0800
changeset 59570 d488f09f6f8d7652107ec264d51f7b04816afffa
parent 59569 e5ed12d16160b695c7388e4087c6596c8a007791
child 59571 77956e4dd8f889d732934e23e7b08a431a8e93f9
push idunknown
push userunknown
push dateunknown
reviewersblassey, roc, blocking-fennec
bugs616664
milestone2.0b9pre
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 616664 - nsIScreen interface for keeping the screen on. r=blassey sr=roc a=blocking-fennec
widget/public/nsIScreen.idl
widget/src/shared/WidgetUtils.cpp
widget/src/shared/WidgetUtils.h
--- a/widget/public/nsIScreen.idl
+++ b/widget/public/nsIScreen.idl
@@ -48,8 +48,39 @@ interface nsIScreen : nsISupports
   readonly attribute long pixelDepth;
   readonly attribute long colorDepth;  
 };
 
 
 %{ 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);
+};
+
--- a/widget/src/shared/WidgetUtils.cpp
+++ b/widget/src/shared/WidgetUtils.cpp
@@ -75,10 +75,59 @@ WidgetUtils::DOMWindowToWidget(nsIDOMWin
         baseWin = do_QueryInterface(window->GetDocShell());
       }
     }
   }
 
   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
--- a/widget/src/shared/WidgetUtils.h
+++ b/widget/src/shared/WidgetUtils.h
@@ -40,27 +40,68 @@
 
 #ifndef __mozilla_widget_WidgetUtils_h__
 #define __mozilla_widget_WidgetUtils_h__
 
 #include "nsCOMPtr.h"
 #include "nsIWidget.h"
 #include "nsPIDOMWindow.h"
 #include "nsIDOMWindow.h"
+#include "nsIScreen.h"
 
 namespace mozilla {
 namespace widget {
 
 class WidgetUtils
 {
 public:
 
   /**
    * Starting at the docshell item for the passed in DOM window this looks up
    * the docshell tree until it finds a docshell item that has a widget.
    */
   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
 
 #endif