2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07: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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "nsScreenManagerGtk.h"
|
|
|
|
#include "nsScreenGtk.h"
|
|
|
|
#include "nsIComponentManager.h"
|
|
|
|
#include "nsRect.h"
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
|
2008-08-06 16:24:13 -07:00
|
|
|
#define SCREEN_MANAGER_LIBRARY_LOAD_FAILED ((PRLibrary*)1)
|
2008-08-06 13:48:55 -07:00
|
|
|
|
2008-08-06 13:48:55 -07:00
|
|
|
#ifdef MOZ_X11
|
|
|
|
#include <gdk/gdkx.h>
|
2007-05-02 13:07:32 -07:00
|
|
|
// prototypes from Xinerama.h
|
|
|
|
typedef Bool (*_XnrmIsActive_fn)(Display *dpy);
|
|
|
|
typedef XineramaScreenInfo* (*_XnrmQueryScreens_fn)(Display *dpy, int *number);
|
2008-08-06 13:48:55 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-07-13 20:20:30 -07:00
|
|
|
static GdkFilterReturn
|
|
|
|
root_window_event_filter(GdkXEvent *aGdkXEvent, GdkEvent *aGdkEvent,
|
|
|
|
gpointer aClosure)
|
|
|
|
{
|
2008-08-06 16:24:13 -07:00
|
|
|
nsScreenManagerGtk *manager = static_cast<nsScreenManagerGtk*>(aClosure);
|
2008-08-06 13:48:55 -07:00
|
|
|
#ifdef MOZ_X11
|
|
|
|
XEvent *xevent = static_cast<XEvent*>(aGdkXEvent);
|
2008-07-13 20:20:30 -07: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 13:48:55 -07:00
|
|
|
#endif
|
|
|
|
|
2008-07-13 20:20:30 -07:00
|
|
|
return GDK_FILTER_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsScreenManagerGtk :: nsScreenManagerGtk ( )
|
2012-07-30 07:20:58 -07:00
|
|
|
: mXineramalib(nullptr)
|
|
|
|
, mRootWindow(nullptr)
|
2007-03-22 10:30:00 -07: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-13 20:20:30 -07:00
|
|
|
if (mRootWindow) {
|
|
|
|
gdk_window_remove_filter(mRootWindow, root_window_event_filter, this);
|
|
|
|
g_object_unref(mRootWindow);
|
2012-07-30 07:20:58 -07:00
|
|
|
mRootWindow = nullptr;
|
2008-07-13 20:20:30 -07:00
|
|
|
}
|
|
|
|
|
2008-12-05 01:16:16 -08: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. */
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// addref, release, QI
|
|
|
|
NS_IMPL_ISUPPORTS1(nsScreenManagerGtk, nsIScreenManager)
|
|
|
|
|
|
|
|
|
|
|
|
// this function will make sure that everything has been initialized.
|
|
|
|
nsresult
|
2008-07-13 20:20:30 -07:00
|
|
|
nsScreenManagerGtk :: EnsureInit()
|
|
|
|
{
|
|
|
|
if (mCachedScreenArray.Count() > 0)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
#if GTK_CHECK_VERSION(2,2,0)
|
|
|
|
mRootWindow = gdk_get_default_root_window();
|
|
|
|
#else
|
|
|
|
mRootWindow = GDK_ROOT_PARENT();
|
|
|
|
#endif // GTK_CHECK_VERSION(2,2,0)
|
|
|
|
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 13:48:55 -07:00
|
|
|
#ifdef MOZ_X11
|
2008-07-13 20:20:30 -07:00
|
|
|
mNetWorkareaAtom =
|
|
|
|
XInternAtom(GDK_WINDOW_XDISPLAY(mRootWindow), "_NET_WORKAREA", False);
|
2008-08-06 13:48:55 -07:00
|
|
|
#endif
|
2008-07-13 20:20:30 -07:00
|
|
|
|
|
|
|
return Init();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsScreenManagerGtk :: Init()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-08-06 13:48:55 -07:00
|
|
|
#ifdef MOZ_X11
|
2008-07-13 20:20:30 -07:00
|
|
|
XineramaScreenInfo *screenInfo = NULL;
|
|
|
|
int numScreens;
|
2007-05-02 13:07:32 -07:00
|
|
|
|
2008-07-13 20:20:30 -07:00
|
|
|
if (!mXineramalib) {
|
|
|
|
mXineramalib = PR_LoadLibrary("libXinerama.so.1");
|
2008-07-13 20:20:27 -07:00
|
|
|
if (!mXineramalib) {
|
2008-07-13 20:20:30 -07:00
|
|
|
mXineramalib = SCREEN_MANAGER_LIBRARY_LOAD_FAILED;
|
2008-07-13 20:20:27 -07:00
|
|
|
}
|
2008-07-13 20:20:30 -07: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-13 18:56:59 -07:00
|
|
|
Display *display = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
|
|
|
|
if (_XnrmIsActive && _XnrmQueryScreens && _XnrmIsActive(display)) {
|
|
|
|
screenInfo = _XnrmQueryScreens(display, &numScreens);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2008-07-13 20:20:30 -07:00
|
|
|
}
|
2008-12-05 01:16:16 -08:00
|
|
|
|
2008-07-13 20:20:30 -07:00
|
|
|
// screenInfo == NULL if either Xinerama couldn't be loaded or
|
|
|
|
// isn't running on the current display
|
2008-07-13 20:20:33 -07:00
|
|
|
if (!screenInfo || numScreens == 1) {
|
2008-08-06 16:24:13 -07:00
|
|
|
numScreens = 1;
|
2008-08-06 13:48:55 -07:00
|
|
|
#endif
|
|
|
|
nsRefPtr<nsScreenGtk> screen;
|
2008-07-13 20:20:30 -07:00
|
|
|
|
|
|
|
if (mCachedScreenArray.Count() > 0) {
|
|
|
|
screen = static_cast<nsScreenGtk*>(mCachedScreenArray[0]);
|
|
|
|
} else {
|
|
|
|
screen = new nsScreenGtk();
|
2008-07-13 20:20:24 -07:00
|
|
|
if (!screen || !mCachedScreenArray.AppendObject(screen)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2008-07-13 20:20:24 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2008-07-13 20:20:30 -07:00
|
|
|
|
|
|
|
screen->Init(mRootWindow);
|
2008-08-06 13:48:55 -07:00
|
|
|
#ifdef MOZ_X11
|
2008-07-13 20:20:30 -07: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 {
|
2007-03-22 10:30:00 -07:00
|
|
|
#ifdef DEBUG
|
2008-07-13 20:20:30 -07:00
|
|
|
printf("Xinerama superpowers activated for %d screens!\n", numScreens);
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
2008-07-13 20:20:30 -07: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-13 20:20:24 -07:00
|
|
|
if (!screen || !mCachedScreenArray.AppendObject(screen)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
}
|
2007-04-27 03:19:41 -07:00
|
|
|
|
2008-07-13 20:20:30 -07:00
|
|
|
// initialize this screen object
|
|
|
|
screen->Init(&screenInfo[i]);
|
2007-04-27 03:19:41 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2008-07-13 20:20:30 -07: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 13:48:55 -07:00
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-05-02 13:07:32 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07: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-14 19:27:09 -08:00
|
|
|
// The coordinates are in pixels (not app units) and in screen coordinates.
|
2007-03-22 10:30:00 -07:00
|
|
|
//
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsScreenManagerGtk :: ScreenForRect ( int32_t aX, int32_t aY,
|
|
|
|
int32_t aWidth, int32_t aHeight,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIScreen **aOutScreen )
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
rv = EnsureInit();
|
|
|
|
if (NS_FAILED(rv)) {
|
2010-06-17 13:28:38 -07:00
|
|
|
NS_ERROR("nsScreenManagerGtk::EnsureInit() failed from ScreenForRect");
|
2007-03-22 10:30:00 -07:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
// which screen ( index from zero ) should we return?
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t which = 0;
|
2007-03-22 10:30:00 -07: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-13 20:20:24 -07:00
|
|
|
if (mCachedScreenArray.Count() > 1) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// walk the list of screens and find the one that has the most
|
|
|
|
// surface area.
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t area = 0;
|
2009-01-14 19:27:09 -08:00
|
|
|
nsIntRect windowRect(aX, aY, aWidth, aHeight);
|
2012-08-22 08:56:38 -07:00
|
|
|
for (int32_t i = 0, i_end = mCachedScreenArray.Count(); i < i_end; ++i) {
|
|
|
|
int32_t x, y, width, height;
|
2007-03-22 10:30:00 -07:00
|
|
|
x = y = width = height = 0;
|
2008-07-13 20:20:21 -07:00
|
|
|
mCachedScreenArray[i]->GetRect(&x, &y, &width, &height);
|
2007-03-22 10:30:00 -07:00
|
|
|
// calculate the surface area
|
2009-01-14 19:27:09 -08:00
|
|
|
nsIntRect screenRect(x, y, width, height);
|
2007-03-22 10:30:00 -07:00
|
|
|
screenRect.IntersectRect(screenRect, windowRect);
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t tempArea = screenRect.width * screenRect.height;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (tempArea >= area) {
|
|
|
|
which = i;
|
|
|
|
area = tempArea;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-07-13 20:20:21 -07:00
|
|
|
*aOutScreen = mCachedScreenArray.SafeObjectAt(which);
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IF_ADDREF(*aOutScreen);
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
} // ScreenForRect
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// GetPrimaryScreen
|
|
|
|
//
|
|
|
|
// The screen with the menubar/taskbar. This shouldn't be needed very
|
|
|
|
// often.
|
|
|
|
//
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsScreenManagerGtk :: GetPrimaryScreen(nsIScreen * *aPrimaryScreen)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
rv = EnsureInit();
|
|
|
|
if (NS_FAILED(rv)) {
|
2010-06-17 13:28:38 -07:00
|
|
|
NS_ERROR("nsScreenManagerGtk::EnsureInit() failed from GetPrimaryScreen");
|
2007-03-22 10:30:00 -07:00
|
|
|
return rv;
|
|
|
|
}
|
2008-07-13 20:20:21 -07:00
|
|
|
*aPrimaryScreen = mCachedScreenArray.SafeObjectAt(0);
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IF_ADDREF(*aPrimaryScreen);
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
} // GetPrimaryScreen
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// GetNumberOfScreens
|
|
|
|
//
|
|
|
|
// Returns how many physical screens are available.
|
|
|
|
//
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsScreenManagerGtk :: GetNumberOfScreens(uint32_t *aNumberOfScreens)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
rv = EnsureInit();
|
|
|
|
if (NS_FAILED(rv)) {
|
2010-06-17 13:28:38 -07:00
|
|
|
NS_ERROR("nsScreenManagerGtk::EnsureInit() failed from GetNumberOfScreens");
|
2007-03-22 10:30:00 -07:00
|
|
|
return rv;
|
|
|
|
}
|
2008-07-13 20:20:24 -07:00
|
|
|
*aNumberOfScreens = mCachedScreenArray.Count();
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
} // GetNumberOfScreens
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsScreenManagerGtk :: ScreenForNativeWidget (void *aWidget, nsIScreen **outScreen)
|
|
|
|
{
|
2009-09-01 09:40:49 -07:00
|
|
|
nsresult rv;
|
|
|
|
rv = EnsureInit();
|
|
|
|
if (NS_FAILED(rv)) {
|
2010-06-17 13:28:38 -07:00
|
|
|
NS_ERROR("nsScreenManagerGtk::EnsureInit() failed from ScreenForNativeWidget");
|
2009-09-01 09:40:49 -07: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-13 18:56:59 -07:00
|
|
|
gint x, y, width, height;
|
|
|
|
#if (MOZ_WIDGET_GTK == 2)
|
|
|
|
gint depth;
|
|
|
|
#endif
|
2009-09-01 09:40:49 -07:00
|
|
|
x = y = width = height = 0;
|
|
|
|
|
2012-09-13 18:56:59 -07:00
|
|
|
#if (MOZ_WIDGET_GTK == 2)
|
2009-09-01 09:40:49 -07:00
|
|
|
gdk_window_get_geometry(GDK_WINDOW(aWidget), &x, &y, &width, &height,
|
|
|
|
&depth);
|
2012-09-13 18:56:59 -07:00
|
|
|
#else
|
|
|
|
gdk_window_get_geometry(GDK_WINDOW(aWidget), &x, &y, &width, &height);
|
|
|
|
#endif
|
2009-09-01 09:40:49 -07:00
|
|
|
gdk_window_get_origin(GDK_WINDOW(aWidget), &x, &y);
|
|
|
|
rv = ScreenForRect(x, y, width, height, outScreen);
|
|
|
|
} else {
|
|
|
|
rv = GetPrimaryScreen(outScreen);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|