2006-01-11 21:28:25 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 11:12:37 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2006-01-11 21:27:48 +00:00
|
|
|
|
|
|
|
#include "nsScreenManagerGtk.h"
|
|
|
|
#include "nsScreenGtk.h"
|
2006-01-11 21:28:10 +00:00
|
|
|
#include "nsIComponentManager.h"
|
|
|
|
#include "nsRect.h"
|
2006-01-11 21:28:41 +00:00
|
|
|
#include "nsAutoPtr.h"
|
2006-01-11 21:27:48 +00:00
|
|
|
|
2008-08-06 23:24:13 +00:00
|
|
|
#define SCREEN_MANAGER_LIBRARY_LOAD_FAILED ((PRLibrary*)1)
|
2008-08-06 20:48:55 +00:00
|
|
|
|
2008-08-06 20:48:55 +00:00
|
|
|
#ifdef MOZ_X11
|
|
|
|
#include <gdk/gdkx.h>
|
2007-05-02 20:07:32 +00:00
|
|
|
// prototypes from Xinerama.h
|
|
|
|
typedef Bool (*_XnrmIsActive_fn)(Display *dpy);
|
|
|
|
typedef XineramaScreenInfo* (*_XnrmQueryScreens_fn)(Display *dpy, int *number);
|
2008-08-06 20:48:55 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
2006-01-11 21:27:48 +00:00
|
|
|
|
2008-07-14 03:20:30 +00:00
|
|
|
static GdkFilterReturn
|
|
|
|
root_window_event_filter(GdkXEvent *aGdkXEvent, GdkEvent *aGdkEvent,
|
|
|
|
gpointer aClosure)
|
|
|
|
{
|
2008-08-06 23:24:13 +00:00
|
|
|
nsScreenManagerGtk *manager = static_cast<nsScreenManagerGtk*>(aClosure);
|
2008-08-06 20:48:55 +00:00
|
|
|
#ifdef MOZ_X11
|
|
|
|
XEvent *xevent = static_cast<XEvent*>(aGdkXEvent);
|
2008-07-14 03:20:30 +00:00
|
|
|
|
|
|
|
// See comments in nsScreenGtk::Init below.
|
|
|
|
switch (xevent->type) {
|
|
|
|
case ConfigureNotify:
|
|
|
|
manager->Init();
|
|
|
|
break;
|
|
|
|
case PropertyNotify:
|
|
|
|
{
|
|
|
|
XPropertyEvent *propertyEvent = &xevent->xproperty;
|
|
|
|
if (propertyEvent->atom == manager->NetWorkareaAtom()) {
|
|
|
|
manager->Init();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2008-08-06 20:48:55 +00:00
|
|
|
#endif
|
|
|
|
|
2008-07-14 03:20:30 +00:00
|
|
|
return GDK_FILTER_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2006-01-11 21:27:48 +00:00
|
|
|
nsScreenManagerGtk :: nsScreenManagerGtk ( )
|
2012-07-30 14:20:58 +00:00
|
|
|
: mXineramalib(nullptr)
|
|
|
|
, mRootWindow(nullptr)
|
2006-01-11 21:27:48 +00:00
|
|
|
{
|
|
|
|
// nothing else to do. I guess we could cache a bunch of information
|
|
|
|
// here, but we want to ask the device at runtime in case anything
|
|
|
|
// has changed.
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsScreenManagerGtk :: ~nsScreenManagerGtk()
|
|
|
|
{
|
2008-07-14 03:20:30 +00:00
|
|
|
if (mRootWindow) {
|
|
|
|
gdk_window_remove_filter(mRootWindow, root_window_event_filter, this);
|
|
|
|
g_object_unref(mRootWindow);
|
2012-07-30 14:20:58 +00:00
|
|
|
mRootWindow = nullptr;
|
2008-07-14 03:20:30 +00:00
|
|
|
}
|
|
|
|
|
2008-12-05 09:16:16 +00:00
|
|
|
/* XineramaIsActive() registers a callback function close_display()
|
|
|
|
* in X, which is to be called in XCloseDisplay(). This is the case
|
|
|
|
* if Xinerama is active, even if only with one screen.
|
|
|
|
*
|
|
|
|
* We can't unload libXinerama.so.1 here because this will make
|
|
|
|
* the address of close_display() registered in X to be invalid and
|
|
|
|
* it will crash when XCloseDisplay() is called later. */
|
2006-01-11 21:27:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// addref, release, QI
|
2014-04-27 07:06:00 +00:00
|
|
|
NS_IMPL_ISUPPORTS(nsScreenManagerGtk, nsIScreenManager)
|
2006-01-11 21:27:48 +00:00
|
|
|
|
|
|
|
|
2006-01-11 21:28:10 +00:00
|
|
|
// this function will make sure that everything has been initialized.
|
|
|
|
nsresult
|
2008-07-14 03:20:30 +00:00
|
|
|
nsScreenManagerGtk :: EnsureInit()
|
|
|
|
{
|
|
|
|
if (mCachedScreenArray.Count() > 0)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
mRootWindow = gdk_get_default_root_window();
|
|
|
|
g_object_ref(mRootWindow);
|
|
|
|
|
|
|
|
// GDK_STRUCTURE_MASK ==> StructureNotifyMask, for ConfigureNotify
|
|
|
|
// GDK_PROPERTY_CHANGE_MASK ==> PropertyChangeMask, for PropertyNotify
|
|
|
|
gdk_window_set_events(mRootWindow,
|
|
|
|
GdkEventMask(gdk_window_get_events(mRootWindow) |
|
|
|
|
GDK_STRUCTURE_MASK |
|
|
|
|
GDK_PROPERTY_CHANGE_MASK));
|
|
|
|
gdk_window_add_filter(mRootWindow, root_window_event_filter, this);
|
2008-08-06 20:48:55 +00:00
|
|
|
#ifdef MOZ_X11
|
2008-07-14 03:20:30 +00:00
|
|
|
mNetWorkareaAtom =
|
|
|
|
XInternAtom(GDK_WINDOW_XDISPLAY(mRootWindow), "_NET_WORKAREA", False);
|
2008-08-06 20:48:55 +00:00
|
|
|
#endif
|
2008-07-14 03:20:30 +00:00
|
|
|
|
|
|
|
return Init();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsScreenManagerGtk :: Init()
|
2006-01-11 21:27:48 +00:00
|
|
|
{
|
2008-08-06 20:48:55 +00:00
|
|
|
#ifdef MOZ_X11
|
2013-10-08 18:47:37 +00:00
|
|
|
XineramaScreenInfo *screenInfo = nullptr;
|
2008-07-14 03:20:30 +00:00
|
|
|
int numScreens;
|
2007-05-02 20:07:32 +00:00
|
|
|
|
2008-07-14 03:20:30 +00:00
|
|
|
if (!mXineramalib) {
|
|
|
|
mXineramalib = PR_LoadLibrary("libXinerama.so.1");
|
2008-07-14 03:20:27 +00:00
|
|
|
if (!mXineramalib) {
|
2008-07-14 03:20:30 +00:00
|
|
|
mXineramalib = SCREEN_MANAGER_LIBRARY_LOAD_FAILED;
|
2008-07-14 03:20:27 +00:00
|
|
|
}
|
2008-07-14 03:20:30 +00:00
|
|
|
}
|
|
|
|
if (mXineramalib && mXineramalib != SCREEN_MANAGER_LIBRARY_LOAD_FAILED) {
|
|
|
|
_XnrmIsActive_fn _XnrmIsActive = (_XnrmIsActive_fn)
|
|
|
|
PR_FindFunctionSymbol(mXineramalib, "XineramaIsActive");
|
|
|
|
|
|
|
|
_XnrmQueryScreens_fn _XnrmQueryScreens = (_XnrmQueryScreens_fn)
|
|
|
|
PR_FindFunctionSymbol(mXineramalib, "XineramaQueryScreens");
|
|
|
|
|
|
|
|
// get the number of screens via xinerama
|
2012-09-14 01:56:59 +00:00
|
|
|
Display *display = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
|
|
|
|
if (_XnrmIsActive && _XnrmQueryScreens && _XnrmIsActive(display)) {
|
|
|
|
screenInfo = _XnrmQueryScreens(display, &numScreens);
|
2006-01-11 21:28:28 +00:00
|
|
|
}
|
2008-07-14 03:20:30 +00:00
|
|
|
}
|
2008-12-05 09:16:16 +00:00
|
|
|
|
2013-10-08 18:47:37 +00:00
|
|
|
// screenInfo == nullptr if either Xinerama couldn't be loaded or
|
2008-07-14 03:20:30 +00:00
|
|
|
// isn't running on the current display
|
2008-07-14 03:20:33 +00:00
|
|
|
if (!screenInfo || numScreens == 1) {
|
2008-08-06 23:24:13 +00:00
|
|
|
numScreens = 1;
|
2008-08-06 20:48:55 +00:00
|
|
|
#endif
|
|
|
|
nsRefPtr<nsScreenGtk> screen;
|
2008-07-14 03:20:30 +00:00
|
|
|
|
|
|
|
if (mCachedScreenArray.Count() > 0) {
|
|
|
|
screen = static_cast<nsScreenGtk*>(mCachedScreenArray[0]);
|
|
|
|
} else {
|
|
|
|
screen = new nsScreenGtk();
|
2008-07-14 03:20:24 +00:00
|
|
|
if (!screen || !mCachedScreenArray.AppendObject(screen)) {
|
2006-01-11 21:28:10 +00:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2008-07-14 03:20:24 +00:00
|
|
|
}
|
2006-01-11 21:28:10 +00:00
|
|
|
}
|
2008-07-14 03:20:30 +00:00
|
|
|
|
|
|
|
screen->Init(mRootWindow);
|
2008-08-06 20:48:55 +00:00
|
|
|
#ifdef MOZ_X11
|
2008-07-14 03:20:30 +00:00
|
|
|
}
|
|
|
|
// If Xinerama is enabled and there's more than one screen, fill
|
|
|
|
// in the info for all of the screens. If that's not the case
|
|
|
|
// then nsScreenGTK() defaults to the screen width + height
|
|
|
|
else {
|
2006-01-11 21:28:10 +00:00
|
|
|
#ifdef DEBUG
|
2008-07-14 03:20:30 +00:00
|
|
|
printf("Xinerama superpowers activated for %d screens!\n", numScreens);
|
2006-01-11 21:28:10 +00:00
|
|
|
#endif
|
2008-07-14 03:20:30 +00:00
|
|
|
for (int i = 0; i < numScreens; ++i) {
|
|
|
|
nsRefPtr<nsScreenGtk> screen;
|
|
|
|
if (mCachedScreenArray.Count() > i) {
|
|
|
|
screen = static_cast<nsScreenGtk*>(mCachedScreenArray[i]);
|
|
|
|
} else {
|
|
|
|
screen = new nsScreenGtk();
|
2008-07-14 03:20:24 +00:00
|
|
|
if (!screen || !mCachedScreenArray.AppendObject(screen)) {
|
2006-01-11 21:28:10 +00:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
}
|
2007-04-27 10:19:41 +00:00
|
|
|
|
2008-07-14 03:20:30 +00:00
|
|
|
// initialize this screen object
|
|
|
|
screen->Init(&screenInfo[i]);
|
2007-04-27 10:19:41 +00:00
|
|
|
}
|
2006-01-11 21:28:10 +00:00
|
|
|
}
|
2008-07-14 03:20:30 +00:00
|
|
|
// Remove any screens that are no longer present.
|
|
|
|
while (mCachedScreenArray.Count() > numScreens) {
|
|
|
|
mCachedScreenArray.RemoveObjectAt(mCachedScreenArray.Count() - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (screenInfo) {
|
|
|
|
XFree(screenInfo);
|
|
|
|
}
|
2008-08-06 20:48:55 +00:00
|
|
|
#endif
|
2006-01-11 21:28:41 +00:00
|
|
|
|
2007-05-02 20:07:32 +00:00
|
|
|
return NS_OK;
|
2006-01-11 21:27:48 +00:00
|
|
|
}
|
|
|
|
|
2014-07-14 17:22:26 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsScreenManagerGtk :: ScreenForId ( uint32_t aId, nsIScreen **outScreen )
|
|
|
|
{
|
|
|
|
*outScreen = nullptr;
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
rv = EnsureInit();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_ERROR("nsScreenManagerGtk::EnsureInit() failed from ScreenForId");
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int32_t i = 0, i_end = mCachedScreenArray.Count(); i < i_end; ++i) {
|
|
|
|
uint32_t id;
|
|
|
|
rv = mCachedScreenArray[i]->GetId(&id);
|
|
|
|
if (NS_SUCCEEDED(rv) && id == aId) {
|
|
|
|
NS_IF_ADDREF(*outScreen = mCachedScreenArray[i]);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2006-01-11 21:27:48 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// ScreenForRect
|
|
|
|
//
|
|
|
|
// Returns the screen that contains the rectangle. If the rect overlaps
|
|
|
|
// multiple screens, it picks the screen with the greatest area of intersection.
|
|
|
|
//
|
2009-01-15 03:27:09 +00:00
|
|
|
// The coordinates are in pixels (not app units) and in screen coordinates.
|
2006-01-11 21:27:48 +00:00
|
|
|
//
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsScreenManagerGtk :: ScreenForRect ( int32_t aX, int32_t aY,
|
|
|
|
int32_t aWidth, int32_t aHeight,
|
2006-01-11 21:28:10 +00:00
|
|
|
nsIScreen **aOutScreen )
|
2006-01-11 21:27:48 +00:00
|
|
|
{
|
2006-01-11 21:28:10 +00:00
|
|
|
nsresult rv;
|
|
|
|
rv = EnsureInit();
|
|
|
|
if (NS_FAILED(rv)) {
|
2010-06-17 20:28:38 +00:00
|
|
|
NS_ERROR("nsScreenManagerGtk::EnsureInit() failed from ScreenForRect");
|
2006-01-11 21:28:10 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
// which screen ( index from zero ) should we return?
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t which = 0;
|
2006-01-11 21:28:10 +00:00
|
|
|
// Optimize for the common case. If the number of screens is only
|
|
|
|
// one then this will fall through with which == 0 and will get the
|
|
|
|
// primary screen.
|
2008-07-14 03:20:24 +00:00
|
|
|
if (mCachedScreenArray.Count() > 1) {
|
2006-01-11 21:28:10 +00:00
|
|
|
// walk the list of screens and find the one that has the most
|
|
|
|
// surface area.
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t area = 0;
|
2009-01-15 03:27:09 +00:00
|
|
|
nsIntRect windowRect(aX, aY, aWidth, aHeight);
|
2012-08-22 15:56:38 +00:00
|
|
|
for (int32_t i = 0, i_end = mCachedScreenArray.Count(); i < i_end; ++i) {
|
|
|
|
int32_t x, y, width, height;
|
2006-01-11 21:28:10 +00:00
|
|
|
x = y = width = height = 0;
|
2008-07-14 03:20:21 +00:00
|
|
|
mCachedScreenArray[i]->GetRect(&x, &y, &width, &height);
|
2006-01-11 21:28:10 +00:00
|
|
|
// calculate the surface area
|
2009-01-15 03:27:09 +00:00
|
|
|
nsIntRect screenRect(x, y, width, height);
|
2006-01-11 21:28:10 +00:00
|
|
|
screenRect.IntersectRect(screenRect, windowRect);
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t tempArea = screenRect.width * screenRect.height;
|
2006-01-11 21:28:10 +00:00
|
|
|
if (tempArea >= area) {
|
|
|
|
which = i;
|
|
|
|
area = tempArea;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-07-14 03:20:21 +00:00
|
|
|
*aOutScreen = mCachedScreenArray.SafeObjectAt(which);
|
2006-01-11 21:28:10 +00:00
|
|
|
NS_IF_ADDREF(*aOutScreen);
|
2006-01-11 21:27:48 +00:00
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
} // ScreenForRect
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// GetPrimaryScreen
|
|
|
|
//
|
|
|
|
// The screen with the menubar/taskbar. This shouldn't be needed very
|
|
|
|
// often.
|
|
|
|
//
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsScreenManagerGtk :: GetPrimaryScreen(nsIScreen * *aPrimaryScreen)
|
|
|
|
{
|
2006-01-11 21:28:10 +00:00
|
|
|
nsresult rv;
|
|
|
|
rv = EnsureInit();
|
|
|
|
if (NS_FAILED(rv)) {
|
2010-06-17 20:28:38 +00:00
|
|
|
NS_ERROR("nsScreenManagerGtk::EnsureInit() failed from GetPrimaryScreen");
|
2006-01-11 21:28:10 +00:00
|
|
|
return rv;
|
|
|
|
}
|
2008-07-14 03:20:21 +00:00
|
|
|
*aPrimaryScreen = mCachedScreenArray.SafeObjectAt(0);
|
2006-01-11 21:28:10 +00:00
|
|
|
NS_IF_ADDREF(*aPrimaryScreen);
|
2006-01-11 21:27:48 +00:00
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
} // GetPrimaryScreen
|
|
|
|
|
2006-01-11 21:27:52 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// GetNumberOfScreens
|
|
|
|
//
|
|
|
|
// Returns how many physical screens are available.
|
|
|
|
//
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsScreenManagerGtk :: GetNumberOfScreens(uint32_t *aNumberOfScreens)
|
2006-01-11 21:27:52 +00:00
|
|
|
{
|
2006-01-11 21:28:10 +00:00
|
|
|
nsresult rv;
|
|
|
|
rv = EnsureInit();
|
|
|
|
if (NS_FAILED(rv)) {
|
2010-06-17 20:28:38 +00:00
|
|
|
NS_ERROR("nsScreenManagerGtk::EnsureInit() failed from GetNumberOfScreens");
|
2006-01-11 21:28:10 +00:00
|
|
|
return rv;
|
|
|
|
}
|
2008-07-14 03:20:24 +00:00
|
|
|
*aNumberOfScreens = mCachedScreenArray.Count();
|
2006-01-11 21:27:52 +00:00
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
} // GetNumberOfScreens
|
|
|
|
|
2013-04-09 21:07:02 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsScreenManagerGtk::GetSystemDefaultScale(float *aDefaultScale)
|
|
|
|
{
|
|
|
|
*aDefaultScale = 1.0f;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2006-09-20 21:08:24 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsScreenManagerGtk :: ScreenForNativeWidget (void *aWidget, nsIScreen **outScreen)
|
|
|
|
{
|
2009-09-01 16:40:49 +00:00
|
|
|
nsresult rv;
|
|
|
|
rv = EnsureInit();
|
|
|
|
if (NS_FAILED(rv)) {
|
2010-06-17 20:28:38 +00:00
|
|
|
NS_ERROR("nsScreenManagerGtk::EnsureInit() failed from ScreenForNativeWidget");
|
2009-09-01 16:40:49 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mCachedScreenArray.Count() > 1) {
|
|
|
|
// I don't know how to go from GtkWindow to nsIScreen, especially
|
|
|
|
// given xinerama and stuff, so let's just do this
|
2012-09-14 01:56:59 +00:00
|
|
|
gint x, y, width, height;
|
|
|
|
#if (MOZ_WIDGET_GTK == 2)
|
|
|
|
gint depth;
|
|
|
|
#endif
|
2009-09-01 16:40:49 +00:00
|
|
|
x = y = width = height = 0;
|
|
|
|
|
2012-09-14 01:56:59 +00:00
|
|
|
#if (MOZ_WIDGET_GTK == 2)
|
2009-09-01 16:40:49 +00:00
|
|
|
gdk_window_get_geometry(GDK_WINDOW(aWidget), &x, &y, &width, &height,
|
|
|
|
&depth);
|
2012-09-14 01:56:59 +00:00
|
|
|
#else
|
|
|
|
gdk_window_get_geometry(GDK_WINDOW(aWidget), &x, &y, &width, &height);
|
|
|
|
#endif
|
2009-09-01 16:40:49 +00:00
|
|
|
gdk_window_get_origin(GDK_WINDOW(aWidget), &x, &y);
|
|
|
|
rv = ScreenForRect(x, y, width, height, outScreen);
|
|
|
|
} else {
|
|
|
|
rv = GetPrimaryScreen(outScreen);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
2006-09-20 21:08:24 +00:00
|
|
|
}
|