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
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 "ScreenHelperGTK.h"
#ifdef MOZ_X11
# include <gdk/gdkx.h>
#endif /* MOZ_X11 */
#ifdef MOZ_WAYLAND
# include <gdk/gdkwayland.h>
#endif /* MOZ_WAYLAND */
#include <dlfcn.h>
#include <gtk/gtk.h>
#include "gfxPlatformGtk.h"
#include "mozilla/dom/DOMTypes.h"
#include "mozilla/Logging.h"
#include "nsGtkUtils.h"
#include "nsTArray.h"
namespace mozilla {
namespace widget {
static LazyLogModule sScreenLog("WidgetScreen");
static void monitors_changed(GdkScreen* aScreen, gpointer aClosure) {
MOZ_LOG(sScreenLog, LogLevel::Debug, ("Received monitors-changed event"));
ScreenHelperGTK* self = static_cast<ScreenHelperGTK*>(aClosure);
self->RefreshScreens();
}
static void screen_resolution_changed(GdkScreen* aScreen, GParamSpec* aPspec,
ScreenHelperGTK* self) {
self->RefreshScreens();
}
static GdkFilterReturn root_window_event_filter(GdkXEvent* aGdkXEvent,
GdkEvent* aGdkEvent,
gpointer aClosure) {
#ifdef MOZ_X11
ScreenHelperGTK* self = static_cast<ScreenHelperGTK*>(aClosure);
XEvent* xevent = static_cast<XEvent*>(aGdkXEvent);
switch (xevent->type) {
case PropertyNotify: {
XPropertyEvent* propertyEvent = &xevent->xproperty;
if (propertyEvent->atom == self->NetWorkareaAtom()) {
MOZ_LOG(sScreenLog, LogLevel::Debug, ("Work area size changed"));
self->RefreshScreens();
}
} break;
default:
break;
}
#endif
return GDK_FILTER_CONTINUE;
}
ScreenHelperGTK::ScreenHelperGTK()
: mRootWindow(nullptr)
#ifdef MOZ_X11
,
mNetWorkareaAtom(0)
#endif
{
MOZ_LOG(sScreenLog, LogLevel::Debug, ("ScreenHelperGTK created"));
GdkScreen* defaultScreen = gdk_screen_get_default();
if (!defaultScreen) {
// Sometimes we don't initial X (e.g., xpcshell)
MOZ_LOG(sScreenLog, LogLevel::Debug,
("defaultScreen is nullptr, running headless"));
return;
}
mRootWindow = gdk_get_default_root_window();
MOZ_ASSERT(mRootWindow);
g_object_ref(mRootWindow);
// GDK_PROPERTY_CHANGE_MASK ==> PropertyChangeMask, for PropertyNotify
gdk_window_set_events(mRootWindow,
GdkEventMask(gdk_window_get_events(mRootWindow) |
GDK_PROPERTY_CHANGE_MASK));
g_signal_connect(defaultScreen, "monitors-changed",
G_CALLBACK(monitors_changed), this);
// Use _after to ensure this callback is run after gfxPlatformGtk.cpp's
// handler.
g_signal_connect_after(defaultScreen, "notify::resolution",
G_CALLBACK(screen_resolution_changed), this);
#ifdef MOZ_X11
gdk_window_add_filter(mRootWindow, root_window_event_filter, this);
if (GDK_IS_X11_DISPLAY(gdk_display_get_default())) {
mNetWorkareaAtom = XInternAtom(GDK_WINDOW_XDISPLAY(mRootWindow),
"_NET_WORKAREA", X11False);
}
#endif
RefreshScreens();
}
ScreenHelperGTK::~ScreenHelperGTK() {
if (mRootWindow) {
g_signal_handlers_disconnect_by_data(gdk_screen_get_default(), this);
gdk_window_remove_filter(mRootWindow, root_window_event_filter, this);
g_object_unref(mRootWindow);
mRootWindow = nullptr;
}
}
gint ScreenHelperGTK::GetGTKMonitorScaleFactor(gint aMonitorNum) {
// Since GDK 3.10
static auto sGdkScreenGetMonitorScaleFactorPtr =
(gint(*)(GdkScreen*, gint))dlsym(RTLD_DEFAULT,
"gdk_screen_get_monitor_scale_factor");
if (sGdkScreenGetMonitorScaleFactorPtr) {
GdkScreen* screen = gdk_screen_get_default();
return sGdkScreenGetMonitorScaleFactorPtr(screen, aMonitorNum);
}
return 1;
}
static uint32_t GetGTKPixelDepth() {
GdkVisual* visual = gdk_screen_get_system_visual(gdk_screen_get_default());
return gdk_visual_get_depth(visual);
}
static already_AddRefed<Screen> MakeScreen(GdkScreen* aScreen,
gint aMonitorNum) {
GdkRectangle monitor;
GdkRectangle workarea;
gdk_screen_get_monitor_geometry(aScreen, aMonitorNum, &monitor);
gdk_screen_get_monitor_workarea(aScreen, aMonitorNum, &workarea);
gint gdkScaleFactor = ScreenHelperGTK::GetGTKMonitorScaleFactor(aMonitorNum);
// gdk_screen_get_monitor_geometry / workarea returns application pixels
// (desktop pixels), so we need to convert it to device pixels with
// gdkScaleFactor.
LayoutDeviceIntRect rect(
monitor.x * gdkScaleFactor, monitor.y * gdkScaleFactor,
monitor.width * gdkScaleFactor, monitor.height * gdkScaleFactor);
LayoutDeviceIntRect availRect(
workarea.x * gdkScaleFactor, workarea.y * gdkScaleFactor,
workarea.width * gdkScaleFactor, workarea.height * gdkScaleFactor);
uint32_t pixelDepth = GetGTKPixelDepth();
// Use per-monitor scaling factor in gtk/wayland, or 1.0 otherwise.
DesktopToLayoutDeviceScale contentsScale(1.0);
#ifdef MOZ_WAYLAND
GdkDisplay* gdkDisplay = gdk_display_get_default();
if (!GDK_IS_X11_DISPLAY(gdkDisplay)) {
contentsScale.scale = gdkScaleFactor;
}
#endif
CSSToLayoutDeviceScale defaultCssScale(gdkScaleFactor *
gfxPlatformGtk::GetFontScaleFactor());
float dpi = 96.0f;
gint heightMM = gdk_screen_get_monitor_height_mm(aScreen, aMonitorNum);
if (heightMM > 0) {
dpi = rect.height / (heightMM / MM_PER_INCH_FLOAT);
}
MOZ_LOG(sScreenLog, LogLevel::Debug,
("New screen [%d %d %d %d (%d %d %d %d) %d %f %f %f]", rect.x, rect.y,
rect.width, rect.height, availRect.x, availRect.y, availRect.width,
availRect.height, pixelDepth, contentsScale.scale,
defaultCssScale.scale, dpi));
RefPtr<Screen> screen = new Screen(rect, availRect, pixelDepth, pixelDepth,
contentsScale, defaultCssScale, dpi);
return screen.forget();
}
void ScreenHelperGTK::RefreshScreens() {
MOZ_LOG(sScreenLog, LogLevel::Debug, ("Refreshing screens"));
AutoTArray<RefPtr<Screen>, 4> screenList;
GdkScreen* defaultScreen = gdk_screen_get_default();
gint numScreens = gdk_screen_get_n_monitors(defaultScreen);
MOZ_LOG(sScreenLog, LogLevel::Debug, ("GDK reports %d screens", numScreens));
for (gint i = 0; i < numScreens; i++) {
screenList.AppendElement(MakeScreen(defaultScreen, i));
}
ScreenManager& screenManager = ScreenManager::GetSingleton();
screenManager.Refresh(std::move(screenList));
}
} // namespace widget
} // namespace mozilla
|