2017-03-09 14:29:44 +03:00
|
|
|
/* -*- 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>
|
2017-06-16 06:13:59 +03:00
|
|
|
#endif /* MOZ_X11 */
|
|
|
|
#ifdef MOZ_WAYLAND
|
|
|
|
#include <gdk/gdkwayland.h>
|
|
|
|
#endif /* MOZ_WAYLAND */
|
2017-03-09 14:29:44 +03:00
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
|
|
|
#include "gfxPlatformGtk.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 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()
|
2017-06-06 12:57:54 +03:00
|
|
|
: mRootWindow(nullptr)
|
|
|
|
#ifdef MOZ_X11
|
2017-03-09 14:29:44 +03:00
|
|
|
, mNetWorkareaAtom(0)
|
2017-06-06 12:57:54 +03:00
|
|
|
#endif
|
2017-03-09 14:29:44 +03:00
|
|
|
{
|
|
|
|
MOZ_LOG(sScreenLog, LogLevel::Debug, ("ScreenHelperGTK created"));
|
2017-03-29 15:29:58 +03:00
|
|
|
GdkScreen *defaultScreen = gdk_screen_get_default();
|
|
|
|
if (!defaultScreen) {
|
2017-03-09 14:29:44 +03:00
|
|
|
// Sometimes we don't initial X (e.g., xpcshell)
|
2017-06-06 12:57:54 +03:00
|
|
|
MOZ_LOG(sScreenLog, LogLevel::Debug, ("defaultScreen is nullptr, running headless"));
|
2017-03-09 14:29:44 +03:00
|
|
|
return;
|
|
|
|
}
|
2017-03-29 15:29:58 +03:00
|
|
|
mRootWindow = gdk_get_default_root_window();
|
|
|
|
MOZ_ASSERT(mRootWindow);
|
2017-03-09 14:29:44 +03:00
|
|
|
|
|
|
|
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));
|
|
|
|
|
2017-03-29 15:29:58 +03:00
|
|
|
g_signal_connect(defaultScreen, "monitors-changed",
|
2017-03-09 14:29:44 +03:00
|
|
|
G_CALLBACK(monitors_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", False);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
RefreshScreens();
|
|
|
|
}
|
|
|
|
|
|
|
|
ScreenHelperGTK::~ScreenHelperGTK()
|
|
|
|
{
|
|
|
|
if (mRootWindow) {
|
2017-03-29 15:29:58 +03:00
|
|
|
g_signal_handlers_disconnect_by_func(gdk_screen_get_default(),
|
|
|
|
FuncToGpointer(monitors_changed),
|
|
|
|
this);
|
|
|
|
|
2017-03-09 14:29:44 +03:00
|
|
|
gdk_window_remove_filter(mRootWindow, root_window_event_filter, this);
|
|
|
|
g_object_unref(mRootWindow);
|
|
|
|
mRootWindow = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gint
|
2017-06-06 12:57:54 +03:00
|
|
|
ScreenHelperGTK::GetGTKMonitorScaleFactor(gint aMonitorNum)
|
2017-03-09 14:29:44 +03:00
|
|
|
{
|
|
|
|
#if (MOZ_WIDGET_GTK >= 3)
|
|
|
|
// 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();
|
2017-06-06 12:57:54 +03:00
|
|
|
return sGdkScreenGetMonitorScaleFactorPtr(screen, aMonitorNum);
|
2017-03-09 14:29:44 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
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>
|
2017-06-06 12:57:54 +03:00
|
|
|
MakeScreen(GdkScreen* aScreen, gint aMonitorNum)
|
2017-03-09 14:29:44 +03:00
|
|
|
{
|
2017-06-06 12:57:54 +03:00
|
|
|
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);
|
2017-03-09 14:29:44 +03:00
|
|
|
uint32_t pixelDepth = GetGTKPixelDepth();
|
2017-06-16 06:13:59 +03:00
|
|
|
|
|
|
|
// Use per-monitor scaling factor in gtk/wayland, or 1.0 otherwise.
|
2017-03-09 14:29:44 +03:00
|
|
|
DesktopToLayoutDeviceScale contentsScale(1.0);
|
2017-06-16 06:13:59 +03:00
|
|
|
#ifdef MOZ_WAYLAND
|
|
|
|
GdkDisplay* gdkDisplay = gdk_display_get_default();
|
|
|
|
if (GDK_IS_WAYLAND_DISPLAY(gdkDisplay)) {
|
|
|
|
contentsScale.scale = gdkScaleFactor;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-06-06 12:57:54 +03:00
|
|
|
CSSToLayoutDeviceScale defaultCssScale(
|
|
|
|
gdkScaleFactor * gfxPlatformGtk::GetFontScaleFactor());
|
|
|
|
|
2017-06-06 13:09:34 +03:00
|
|
|
float dpi = 96.0f;
|
2017-06-16 05:58:11 +03:00
|
|
|
gint heightMM = gdk_screen_get_monitor_height_mm(aScreen, aMonitorNum);
|
|
|
|
if (heightMM > 0) {
|
|
|
|
dpi = rect.height / (heightMM / MM_PER_INCH_FLOAT);
|
|
|
|
}
|
|
|
|
|
2017-06-06 12:57:54 +03:00
|
|
|
MOZ_LOG(sScreenLog, LogLevel::Debug,
|
2017-06-16 05:58:11 +03:00
|
|
|
("New screen [%d %d %d %d (%d %d %d %d) %d %f %f %f]",
|
2017-06-06 12:57:54 +03:00
|
|
|
rect.x, rect.y, rect.width, rect.height,
|
|
|
|
availRect.x, availRect.y, availRect.width, availRect.height,
|
2017-06-16 05:58:11 +03:00
|
|
|
pixelDepth, contentsScale.scale, defaultCssScale.scale, dpi));
|
2017-06-06 12:57:54 +03:00
|
|
|
RefPtr<Screen> screen = new Screen(rect, availRect,
|
2017-03-09 14:29:44 +03:00
|
|
|
pixelDepth, pixelDepth,
|
2017-06-06 13:09:34 +03:00
|
|
|
contentsScale, defaultCssScale,
|
|
|
|
dpi);
|
2017-03-09 14:29:44 +03:00
|
|
|
return screen.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ScreenHelperGTK::RefreshScreens()
|
|
|
|
{
|
|
|
|
MOZ_LOG(sScreenLog, LogLevel::Debug, ("Refreshing screens"));
|
|
|
|
AutoTArray<RefPtr<Screen>, 4> screenList;
|
|
|
|
|
2017-06-06 12:57:54 +03:00
|
|
|
GdkScreen *defaultScreen = gdk_screen_get_default();
|
|
|
|
gint numScreens = gdk_screen_get_n_monitors(defaultScreen);
|
|
|
|
MOZ_LOG(sScreenLog, LogLevel::Debug,
|
|
|
|
("GDK reports %d screens", numScreens));
|
2017-03-09 14:29:44 +03:00
|
|
|
|
2017-06-06 12:57:54 +03:00
|
|
|
for (gint i = 0; i < numScreens; i++) {
|
|
|
|
screenList.AppendElement(MakeScreen(defaultScreen, i));
|
2017-03-09 14:29:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ScreenManager& screenManager = ScreenManager::GetSingleton();
|
|
|
|
screenManager.Refresh(Move(screenList));
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace widget
|
|
|
|
} // namespace mozilla
|