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
|
/*
* Copyright © 2020 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#include "config.h"
#include <gdk/gdk.h>
#include <math.h>
#include "gdkmacosdisplay-private.h"
#include "gdkmacosmonitor-private.h"
#include "gdkmacosutils-private.h"
struct _GdkMacosMonitor
{
GdkMonitor parent_instance;
CGDirectDisplayID screen_id;
NSRect workarea;
guint has_opengl : 1;
};
struct _GdkMacosMonitorClass
{
GdkMonitorClass parent_class;
};
G_DEFINE_TYPE (GdkMacosMonitor, gdk_macos_monitor, GDK_TYPE_MONITOR)
/**
* gdk_macos_monitor_get_workarea:
* @monitor: a #GdkMonitor
* @workarea: (out): a #GdkRectangle to be filled with
* the monitor workarea
*
* Retrieves the size and position of the “work area” on a monitor
* within the display coordinate space. The returned geometry is in
* ”application pixels”, not in ”device pixels” (see
* gdk_monitor_get_scale_factor()).
*/
void
gdk_macos_monitor_get_workarea (GdkMonitor *monitor,
GdkRectangle *geometry)
{
GDK_BEGIN_MACOS_ALLOC_POOL;
GdkMacosMonitor *self = (GdkMacosMonitor *)monitor;
int x, y;
g_assert (GDK_IS_MACOS_MONITOR (self));
g_assert (geometry != NULL);
x = self->workarea.origin.x;
y = self->workarea.origin.y + self->workarea.size.height;
_gdk_macos_display_from_display_coords (GDK_MACOS_DISPLAY (monitor->display),
x, y,
&x, &y);
geometry->x = x;
geometry->y = y;
geometry->width = self->workarea.size.width;
geometry->height = self->workarea.size.height;
GDK_END_MACOS_ALLOC_POOL;
}
static void
gdk_macos_monitor_class_init (GdkMacosMonitorClass *klass)
{
}
static void
gdk_macos_monitor_init (GdkMacosMonitor *self)
{
}
static GdkSubpixelLayout
GetSubpixelLayout (CGDirectDisplayID screen_id)
{
#if 0
GDK_BEGIN_MACOS_ALLOC_POOL;
GdkSubpixelLayout subpixel_layout = GDK_SUBPIXEL_LAYOUT_UNKNOWN;
io_service_t iosvc;
NSDictionary *dict;
guint layout;
gboolean rotation;
rotation = CGDisplayRotation (screen_id);
iosvc = CGDisplayIOServicePort (screen_id);
dict = (NSDictionary *)IODisplayCreateInfoDictionary (iosvc, kIODisplayOnlyPreferredName);
layout = [[dict objectForKey:@kDisplaySubPixelLayout] unsignedIntValue];
switch (layout)
{
case kDisplaySubPixelLayoutRGB:
if (rotation == 0.0)
subpixel_layout = GDK_SUBPIXEL_LAYOUT_HORIZONTAL_RGB;
else if (rotation == 90.0)
subpixel_layout = GDK_SUBPIXEL_LAYOUT_VERTICAL_RGB;
else if (rotation == 180.0 || rotation == -180.0)
subpixel_layout = GDK_SUBPIXEL_LAYOUT_HORIZONTAL_BGR;
else if (rotation == -90.0)
subpixel_layout = GDK_SUBPIXEL_LAYOUT_VERTICAL_BGR;
break;
case kDisplaySubPixelLayoutBGR:
if (rotation == 0.0)
subpixel_layout = GDK_SUBPIXEL_LAYOUT_HORIZONTAL_BGR;
else if (rotation == 90.0)
subpixel_layout = GDK_SUBPIXEL_LAYOUT_VERTICAL_BGR;
else if (rotation == 180.0 || rotation == -180.0)
subpixel_layout = GDK_SUBPIXEL_LAYOUT_HORIZONTAL_RGB;
else if (rotation == -90.0 || rotation == 270.0)
subpixel_layout = GDK_SUBPIXEL_LAYOUT_VERTICAL_RGB;
break;
default:
break;
}
GDK_END_MACOS_ALLOC_POOL;
return subpixel_layout;
#endif
return GDK_SUBPIXEL_LAYOUT_UNKNOWN;
}
static char *
GetLocalizedName (NSScreen *screen)
{
#ifdef AVAILABLE_MAC_OS_X_VERSION_10_15_AND_LATER
GDK_BEGIN_MACOS_ALLOC_POOL;
NSString *str;
char *name;
g_assert (screen);
str = [screen localizedName];
name = g_strdup ([str UTF8String]);
GDK_END_MACOS_ALLOC_POOL;
return g_steal_pointer (&name);
#else
return NULL;
#endif
}
static char *
GetConnectorName (CGDirectDisplayID screen_id)
{
guint unit = CGDisplayUnitNumber (screen_id);
return g_strdup_printf ("unit-%u", unit);
}
static NSScreen *
find_screen (CGDirectDisplayID screen_id)
{
GDK_BEGIN_MACOS_ALLOC_POOL;
NSScreen *screen = NULL;
for (id obj in [NSScreen screens])
{
if (screen_id == [[[obj deviceDescription] objectForKey:@"NSScreenNumber"] unsignedIntValue])
{
screen = (NSScreen *)obj;
break;
}
}
GDK_END_MACOS_ALLOC_POOL;
return screen;
}
gboolean
_gdk_macos_monitor_reconfigure (GdkMacosMonitor *self)
{
GdkSubpixelLayout subpixel_layout;
CGDisplayModeRef mode;
GdkMacosDisplay *display;
NSScreen *screen;
GdkRectangle geom;
gboolean has_opengl;
CGSize size;
CGRect bounds;
size_t width;
size_t pixel_width;
char *connector;
char *name;
int refresh_rate;
int scale_factor = 1;
int width_mm;
int height_mm;
g_return_val_if_fail (GDK_IS_MACOS_MONITOR (self), FALSE);
display = GDK_MACOS_DISPLAY (GDK_MONITOR (self)->display);
if (!(screen = find_screen (self->screen_id)) ||
!(mode = CGDisplayCopyDisplayMode (self->screen_id)))
return FALSE;
size = CGDisplayScreenSize (self->screen_id);
bounds = [screen frame];
width = CGDisplayModeGetWidth (mode);
pixel_width = CGDisplayModeGetPixelWidth (mode);
has_opengl = CGDisplayUsesOpenGLAcceleration (self->screen_id);
subpixel_layout = GetSubpixelLayout (self->screen_id);
name = GetLocalizedName (screen);
connector = GetConnectorName (self->screen_id);
if (width != 0 && pixel_width != 0)
scale_factor = MAX (1, pixel_width / width);
width_mm = size.width;
height_mm = size.height;
geom.x = bounds.origin.x - display->min_x;
geom.y = display->height - bounds.origin.y - bounds.size.height + display->min_y;
geom.width = bounds.size.width;
geom.height = bounds.size.height;
/* We will often get 0 back from CGDisplayModeGetRefreshRate(). We
* can fallback by getting it from CoreVideo based on a CVDisplayLink
* setting (which is also used by the frame clock).
*/
if (!(refresh_rate = CGDisplayModeGetRefreshRate (mode)))
refresh_rate = _gdk_macos_display_get_nominal_refresh_rate (display);
gdk_monitor_set_connector (GDK_MONITOR (self), connector);
gdk_monitor_set_model (GDK_MONITOR (self), name);
gdk_monitor_set_geometry (GDK_MONITOR (self), &geom);
gdk_monitor_set_physical_size (GDK_MONITOR (self), width_mm, height_mm);
gdk_monitor_set_scale_factor (GDK_MONITOR (self), scale_factor);
gdk_monitor_set_refresh_rate (GDK_MONITOR (self), refresh_rate);
gdk_monitor_set_subpixel_layout (GDK_MONITOR (self), GDK_SUBPIXEL_LAYOUT_UNKNOWN);
self->workarea = [screen visibleFrame];
/* We might be able to use this at some point to change which GSK renderer
* we use for surfaces on this monitor. For example, it might be better
* to use cairo if we cannot use OpenGL (or it would be software) anyway.
* Presumably that is more common in cases where macOS is running under
* an emulator such as QEMU.
*/
self->has_opengl = !!has_opengl;
CGDisplayModeRelease (mode);
g_free (name);
g_free (connector);
return TRUE;
}
GdkMacosMonitor *
_gdk_macos_monitor_new (GdkMacosDisplay *display,
CGDirectDisplayID screen_id)
{
GdkMacosMonitor *self;
g_return_val_if_fail (GDK_IS_MACOS_DISPLAY (display), NULL);
self = g_object_new (GDK_TYPE_MACOS_MONITOR,
"display", display,
NULL);
self->screen_id = screen_id;
_gdk_macos_monitor_reconfigure (self);
return g_steal_pointer (&self);
}
CGDirectDisplayID
_gdk_macos_monitor_get_screen_id (GdkMacosMonitor *self)
{
g_return_val_if_fail (GDK_IS_MACOS_MONITOR (self), 0);
return self->screen_id;
}
|