2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Travis Bogard <travis@netscape.com>
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
2011-12-04 09:07:00 -08:00
|
|
|
#include "mozilla/Hal.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsScreen.h"
|
|
|
|
#include "nsIDocShell.h"
|
|
|
|
#include "nsPresContext.h"
|
|
|
|
#include "nsCOMPtr.h"
|
2011-10-03 12:11:31 -07:00
|
|
|
#include "nsDOMClassInfoID.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIInterfaceRequestorUtils.h"
|
2011-12-04 09:07:00 -08:00
|
|
|
#include "nsIDocShellTreeItem.h"
|
2008-07-26 09:14:49 -07:00
|
|
|
#include "nsLayoutUtils.h"
|
2011-12-04 09:07:00 -08:00
|
|
|
#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");
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
//
|
|
|
|
// Screen class implementation
|
|
|
|
//
|
|
|
|
nsScreen::nsScreen(nsIDocShell* aDocShell)
|
|
|
|
: mDocShell(aDocShell)
|
|
|
|
{
|
2011-12-04 09:07:00 -08:00
|
|
|
if (!sInitialized) {
|
|
|
|
Initialize();
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsScreen::~nsScreen()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-12 05:08:43 -08:00
|
|
|
DOMCI_DATA(Screen, nsScreen)
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// QueryInterface implementation for nsScreen
|
|
|
|
NS_INTERFACE_MAP_BEGIN(nsScreen)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMScreen)
|
|
|
|
NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(Screen)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF(nsScreen)
|
|
|
|
NS_IMPL_RELEASE(nsScreen)
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsScreen::SetDocShell(nsIDocShell* aDocShell)
|
|
|
|
{
|
|
|
|
mDocShell = aDocShell; // Weak Reference
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsScreen::GetTop(PRInt32* aTop)
|
|
|
|
{
|
|
|
|
nsRect rect;
|
|
|
|
nsresult rv = GetRect(rect);
|
|
|
|
|
|
|
|
*aTop = rect.y;
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsScreen::GetLeft(PRInt32* aLeft)
|
|
|
|
{
|
|
|
|
nsRect rect;
|
|
|
|
nsresult rv = GetRect(rect);
|
|
|
|
|
|
|
|
*aLeft = rect.x;
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsScreen::GetWidth(PRInt32* aWidth)
|
|
|
|
{
|
|
|
|
nsRect rect;
|
|
|
|
nsresult rv = GetRect(rect);
|
|
|
|
|
|
|
|
*aWidth = rect.width;
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsScreen::GetHeight(PRInt32* aHeight)
|
|
|
|
{
|
|
|
|
nsRect rect;
|
|
|
|
nsresult rv = GetRect(rect);
|
|
|
|
|
|
|
|
*aHeight = rect.height;
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsScreen::GetPixelDepth(PRInt32* aPixelDepth)
|
|
|
|
{
|
2011-04-16 18:22:44 -07:00
|
|
|
nsDeviceContext* context = GetDeviceContext();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (!context) {
|
|
|
|
*aPixelDepth = -1;
|
|
|
|
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRUint32 depth;
|
|
|
|
context->GetDepth(depth);
|
|
|
|
|
|
|
|
*aPixelDepth = depth;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsScreen::GetColorDepth(PRInt32* aColorDepth)
|
|
|
|
{
|
|
|
|
return GetPixelDepth(aColorDepth);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsScreen::GetAvailWidth(PRInt32* aAvailWidth)
|
|
|
|
{
|
|
|
|
nsRect rect;
|
|
|
|
nsresult rv = GetAvailRect(rect);
|
|
|
|
|
|
|
|
*aAvailWidth = rect.width;
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsScreen::GetAvailHeight(PRInt32* aAvailHeight)
|
|
|
|
{
|
|
|
|
nsRect rect;
|
|
|
|
nsresult rv = GetAvailRect(rect);
|
|
|
|
|
|
|
|
*aAvailHeight = rect.height;
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsScreen::GetAvailLeft(PRInt32* aAvailLeft)
|
|
|
|
{
|
|
|
|
nsRect rect;
|
|
|
|
nsresult rv = GetAvailRect(rect);
|
|
|
|
|
|
|
|
*aAvailLeft = rect.x;
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsScreen::GetAvailTop(PRInt32* aAvailTop)
|
|
|
|
{
|
|
|
|
nsRect rect;
|
|
|
|
nsresult rv = GetAvailRect(rect);
|
|
|
|
|
|
|
|
*aAvailTop = rect.y;
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2011-04-16 18:22:44 -07:00
|
|
|
nsDeviceContext*
|
2007-03-22 10:30:00 -07:00
|
|
|
nsScreen::GetDeviceContext()
|
|
|
|
{
|
2008-07-26 09:14:49 -07:00
|
|
|
return nsLayoutUtils::GetDeviceContextForScreenInfo(mDocShell);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsScreen::GetRect(nsRect& aRect)
|
|
|
|
{
|
2011-04-16 18:22:44 -07:00
|
|
|
nsDeviceContext *context = GetDeviceContext();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (!context) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
context->GetRect(aRect);
|
|
|
|
|
|
|
|
aRect.x = nsPresContext::AppUnitsToIntCSSPixels(aRect.x);
|
|
|
|
aRect.y = nsPresContext::AppUnitsToIntCSSPixels(aRect.y);
|
|
|
|
aRect.height = nsPresContext::AppUnitsToIntCSSPixels(aRect.height);
|
|
|
|
aRect.width = nsPresContext::AppUnitsToIntCSSPixels(aRect.width);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsScreen::GetAvailRect(nsRect& aRect)
|
|
|
|
{
|
2011-04-16 18:22:44 -07:00
|
|
|
nsDeviceContext *context = GetDeviceContext();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (!context) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
context->GetClientRect(aRect);
|
|
|
|
|
|
|
|
aRect.x = nsPresContext::AppUnitsToIntCSSPixels(aRect.x);
|
|
|
|
aRect.y = nsPresContext::AppUnitsToIntCSSPixels(aRect.y);
|
|
|
|
aRect.height = nsPresContext::AppUnitsToIntCSSPixels(aRect.height);
|
|
|
|
aRect.width = nsPresContext::AppUnitsToIntCSSPixels(aRect.width);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-12-04 09:07:00 -08:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|