forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnsScreenManagerGtk.cpp
322 lines (279 loc) · 9.09 KB
/
nsScreenManagerGtk.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/. */
#include "nsScreenManagerGtk.h"
#include "nsScreenGtk.h"
#include "nsIComponentManager.h"
#include "nsRect.h"
#include "nsAutoPtr.h"
#define SCREEN_MANAGER_LIBRARY_LOAD_FAILED ((PRLibrary*)1)
#ifdef MOZ_X11
#include <gdk/gdkx.h>
// prototypes from Xinerama.h
typedef Bool (*_XnrmIsActive_fn)(Display *dpy);
typedef XineramaScreenInfo* (*_XnrmQueryScreens_fn)(Display *dpy, int *number);
#endif
#include <gtk/gtk.h>
static GdkFilterReturn
root_window_event_filter(GdkXEvent *aGdkXEvent, GdkEvent *aGdkEvent,
gpointer aClosure)
{
nsScreenManagerGtk *manager = static_cast<nsScreenManagerGtk*>(aClosure);
#ifdef MOZ_X11
XEvent *xevent = static_cast<XEvent*>(aGdkXEvent);
// 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;
}
#endif
return GDK_FILTER_CONTINUE;
}
nsScreenManagerGtk :: nsScreenManagerGtk ( )
: mXineramalib(nullptr)
, mRootWindow(nullptr)
{
// 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()
{
if (mRootWindow) {
gdk_window_remove_filter(mRootWindow, root_window_event_filter, this);
g_object_unref(mRootWindow);
mRootWindow = nullptr;
}
/* 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. */
}
// addref, release, QI
NS_IMPL_ISUPPORTS1(nsScreenManagerGtk, nsIScreenManager)
// this function will make sure that everything has been initialized.
nsresult
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);
#ifdef MOZ_X11
mNetWorkareaAtom =
XInternAtom(GDK_WINDOW_XDISPLAY(mRootWindow), "_NET_WORKAREA", False);
#endif
return Init();
}
nsresult
nsScreenManagerGtk :: Init()
{
#ifdef MOZ_X11
XineramaScreenInfo *screenInfo = nullptr;
int numScreens;
if (!mXineramalib) {
mXineramalib = PR_LoadLibrary("libXinerama.so.1");
if (!mXineramalib) {
mXineramalib = SCREEN_MANAGER_LIBRARY_LOAD_FAILED;
}
}
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
Display *display = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
if (_XnrmIsActive && _XnrmQueryScreens && _XnrmIsActive(display)) {
screenInfo = _XnrmQueryScreens(display, &numScreens);
}
}
// screenInfo == nullptr if either Xinerama couldn't be loaded or
// isn't running on the current display
if (!screenInfo || numScreens == 1) {
numScreens = 1;
#endif
nsRefPtr<nsScreenGtk> screen;
if (mCachedScreenArray.Count() > 0) {
screen = static_cast<nsScreenGtk*>(mCachedScreenArray[0]);
} else {
screen = new nsScreenGtk();
if (!screen || !mCachedScreenArray.AppendObject(screen)) {
return NS_ERROR_OUT_OF_MEMORY;
}
}
screen->Init(mRootWindow);
#ifdef MOZ_X11
}
// 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 {
#ifdef DEBUG
printf("Xinerama superpowers activated for %d screens!\n", numScreens);
#endif
for (int i = 0; i < numScreens; ++i) {
nsRefPtr<nsScreenGtk> screen;
if (mCachedScreenArray.Count() > i) {
screen = static_cast<nsScreenGtk*>(mCachedScreenArray[i]);
} else {
screen = new nsScreenGtk();
if (!screen || !mCachedScreenArray.AppendObject(screen)) {
return NS_ERROR_OUT_OF_MEMORY;
}
}
// initialize this screen object
screen->Init(&screenInfo[i]);
}
}
// Remove any screens that are no longer present.
while (mCachedScreenArray.Count() > numScreens) {
mCachedScreenArray.RemoveObjectAt(mCachedScreenArray.Count() - 1);
}
if (screenInfo) {
XFree(screenInfo);
}
#endif
return NS_OK;
}
//
// ScreenForRect
//
// Returns the screen that contains the rectangle. If the rect overlaps
// multiple screens, it picks the screen with the greatest area of intersection.
//
// The coordinates are in pixels (not app units) and in screen coordinates.
//
NS_IMETHODIMP
nsScreenManagerGtk :: ScreenForRect ( int32_t aX, int32_t aY,
int32_t aWidth, int32_t aHeight,
nsIScreen **aOutScreen )
{
nsresult rv;
rv = EnsureInit();
if (NS_FAILED(rv)) {
NS_ERROR("nsScreenManagerGtk::EnsureInit() failed from ScreenForRect");
return rv;
}
// which screen ( index from zero ) should we return?
uint32_t which = 0;
// 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.
if (mCachedScreenArray.Count() > 1) {
// walk the list of screens and find the one that has the most
// surface area.
uint32_t area = 0;
nsIntRect windowRect(aX, aY, aWidth, aHeight);
for (int32_t i = 0, i_end = mCachedScreenArray.Count(); i < i_end; ++i) {
int32_t x, y, width, height;
x = y = width = height = 0;
mCachedScreenArray[i]->GetRect(&x, &y, &width, &height);
// calculate the surface area
nsIntRect screenRect(x, y, width, height);
screenRect.IntersectRect(screenRect, windowRect);
uint32_t tempArea = screenRect.width * screenRect.height;
if (tempArea >= area) {
which = i;
area = tempArea;
}
}
}
*aOutScreen = mCachedScreenArray.SafeObjectAt(which);
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)) {
NS_ERROR("nsScreenManagerGtk::EnsureInit() failed from GetPrimaryScreen");
return rv;
}
*aPrimaryScreen = mCachedScreenArray.SafeObjectAt(0);
NS_IF_ADDREF(*aPrimaryScreen);
return NS_OK;
} // GetPrimaryScreen
//
// GetNumberOfScreens
//
// Returns how many physical screens are available.
//
NS_IMETHODIMP
nsScreenManagerGtk :: GetNumberOfScreens(uint32_t *aNumberOfScreens)
{
nsresult rv;
rv = EnsureInit();
if (NS_FAILED(rv)) {
NS_ERROR("nsScreenManagerGtk::EnsureInit() failed from GetNumberOfScreens");
return rv;
}
*aNumberOfScreens = mCachedScreenArray.Count();
return NS_OK;
} // GetNumberOfScreens
NS_IMETHODIMP
nsScreenManagerGtk::GetSystemDefaultScale(float *aDefaultScale)
{
*aDefaultScale = 1.0f;
return NS_OK;
}
NS_IMETHODIMP
nsScreenManagerGtk :: ScreenForNativeWidget (void *aWidget, nsIScreen **outScreen)
{
nsresult rv;
rv = EnsureInit();
if (NS_FAILED(rv)) {
NS_ERROR("nsScreenManagerGtk::EnsureInit() failed from ScreenForNativeWidget");
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
gint x, y, width, height;
#if (MOZ_WIDGET_GTK == 2)
gint depth;
#endif
x = y = width = height = 0;
#if (MOZ_WIDGET_GTK == 2)
gdk_window_get_geometry(GDK_WINDOW(aWidget), &x, &y, &width, &height,
&depth);
#else
gdk_window_get_geometry(GDK_WINDOW(aWidget), &x, &y, &width, &height);
#endif
gdk_window_get_origin(GDK_WINDOW(aWidget), &x, &y);
rv = ScreenForRect(x, y, width, height, outScreen);
} else {
rv = GetPrimaryScreen(outScreen);
}
return rv;
}