| 1 |
/* gdkdisplay-quartz.c |
| 2 |
* |
| 3 |
* Copyright (C) 2005 Imendio AB |
| 4 |
* |
| 5 |
* This library is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU Lesser General Public |
| 7 |
* License as published by the Free Software Foundation; either |
| 8 |
* version 2 of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This library is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* Lesser General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU Lesser General Public |
| 16 |
* License along with this library. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
*/ |
| 18 |
|
| 19 |
#include "config.h" |
| 20 |
|
| 21 |
#include <gdk/gdk.h> |
| 22 |
#include <gdk/gdkdisplayprivate.h> |
| 23 |
#include <gdk/gdkmonitorprivate.h> |
| 24 |
#include <gdk/gdkframeclockprivate.h> |
| 25 |
|
| 26 |
#include "gdkprivate-quartz.h" |
| 27 |
#include "gdkquartzscreen.h" |
| 28 |
#include "gdkquartzwindow.h" |
| 29 |
#include "gdkquartzdisplay.h" |
| 30 |
#include "gdkquartzdevicemanager-core.h" |
| 31 |
#include "gdkscreen.h" |
| 32 |
#include "gdkmonitorprivate.h" |
| 33 |
#include "gdkdisplaylinksource.h" |
| 34 |
#include "gdkdisplay-quartz.h" |
| 35 |
#include "gdkmonitor-quartz.h" |
| 36 |
#include "gdkglcontext-quartz.h" |
| 37 |
#include "gdkinternal-quartz.h" |
| 38 |
#include "gdkwindow.h" |
| 39 |
|
| 40 |
/* Note about coordinates: There are three coordinate systems at play: |
| 41 |
* |
| 42 |
* 1. Core Graphics starts at the origin at the upper right of the |
| 43 |
* main window (the one with the menu bar when you look at arrangement |
| 44 |
* in System Preferences>Displays) and increases down and to the |
| 45 |
* right; up and to the left are negative values of y and x |
| 46 |
* respectively. |
| 47 |
* |
| 48 |
* 2. AppKit (functions beginning with "NS" for NextStep) coordinates |
| 49 |
* also have their origin at the main window, but it's the *lower* |
| 50 |
* left corner and coordinates increase up and to the |
| 51 |
* right. Coordinates below or left of the origin are negative. |
| 52 |
* |
| 53 |
* 3. Gdk coordinates origin is at the upper left corner of the |
| 54 |
* imaginary rectangle enclosing all monitors and like Core Graphics |
| 55 |
* increase down and to the right. There are no negative coordinates. |
| 56 |
* |
| 57 |
* We need to deal with all three because AppKit's NSScreen array is |
| 58 |
* recomputed with new pointers whenever the monitor arrangement |
| 59 |
* changes so we can't cache the references it provides. CoreGraphics |
| 60 |
* screen IDs are constant between reboots so those are what we use to |
| 61 |
* map GdkMonitors and screens, but the sizes and origins must be |
| 62 |
* converted to Gdk coordinates to make sense to Gdk and we must |
| 63 |
* frequently convert between Gdk and AppKit coordinates when |
| 64 |
* determining the drawable area of a monitor and placing windows and |
| 65 |
* views (the latter containing our cairo surfaces for drawing on). |
| 66 |
*/ |
| 67 |
|
| 68 |
static gint MONITORS_CHANGED = 0; |
| 69 |
|
| 70 |
static void display_reconfiguration_callback (CGDirectDisplayID display, |
| 71 |
CGDisplayChangeSummaryFlags flags, |
| 72 |
void *data); |
| 73 |
|
| 74 |
static GdkWindow * |
| 75 |
gdk_quartz_display_get_default_group (GdkDisplay *display) |
| 76 |
{ |
| 77 |
/* X11-only. */ |
| 78 |
return NULL; |
| 79 |
} |
| 80 |
|
| 81 |
GdkDeviceManager * |
| 82 |
_gdk_device_manager_new (GdkDisplay *display) |
| 83 |
{ |
| 84 |
return g_object_new (GDK_TYPE_QUARTZ_DEVICE_MANAGER_CORE, |
| 85 |
"display", display, |
| 86 |
NULL); |
| 87 |
} |
| 88 |
|
| 89 |
void |
| 90 |
_gdk_quartz_display_add_frame_callback (GdkDisplay *display, |
| 91 |
GdkWindow *window) |
| 92 |
{ |
| 93 |
GdkQuartzDisplay *display_quartz; |
| 94 |
GdkWindowImplQuartz *impl = GDK_WINDOW_IMPL_QUARTZ (window->impl); |
| 95 |
|
| 96 |
display_quartz = GDK_QUARTZ_DISPLAY (display); |
| 97 |
|
| 98 |
impl->frame_link.data = window; |
| 99 |
impl->frame_link.next = display_quartz->windows_awaiting_frame; |
| 100 |
|
| 101 |
display_quartz->windows_awaiting_frame = &impl->frame_link; |
| 102 |
|
| 103 |
if (impl->frame_link.next == NULL) |
| 104 |
gdk_display_link_source_unpause ((GdkDisplayLinkSource *)display_quartz->frame_source); |
| 105 |
} |
| 106 |
|
| 107 |
void |
| 108 |
_gdk_quartz_display_remove_frame_callback (GdkDisplay *display, |
| 109 |
GdkWindow *window) |
| 110 |
{ |
| 111 |
GdkQuartzDisplay *display_quartz = GDK_QUARTZ_DISPLAY (display); |
| 112 |
GSList *link; |
| 113 |
|
| 114 |
link = g_slist_find (display_quartz->windows_awaiting_frame, window); |
| 115 |
|
| 116 |
if (link != NULL) |
| 117 |
{ |
| 118 |
display_quartz->windows_awaiting_frame = |
| 119 |
g_slist_remove_link (display_quartz->windows_awaiting_frame, link); |
| 120 |
} |
| 121 |
|
| 122 |
if (display_quartz->windows_awaiting_frame == NULL) |
| 123 |
gdk_display_link_source_pause ((GdkDisplayLinkSource *)display_quartz->frame_source); |
| 124 |
} |
| 125 |
|
| 126 |
static gboolean |
| 127 |
gdk_quartz_display_frame_cb (gpointer data) |
| 128 |
{ |
| 129 |
GdkDisplayLinkSource *source; |
| 130 |
GdkQuartzDisplay *display_quartz = data; |
| 131 |
GSList *iter, **last_next = NULL; |
| 132 |
gint64 presentation_time; |
| 133 |
|
| 134 |
source = (GdkDisplayLinkSource *)display_quartz->frame_source; |
| 135 |
|
| 136 |
iter = display_quartz->windows_awaiting_frame; |
| 137 |
display_quartz->windows_awaiting_frame = NULL; |
| 138 |
|
| 139 |
if (iter == NULL) |
| 140 |
{ |
| 141 |
gdk_display_link_source_pause (source); |
| 142 |
return G_SOURCE_CONTINUE; |
| 143 |
} |
| 144 |
|
| 145 |
presentation_time = source->presentation_time; |
| 146 |
|
| 147 |
for (; iter != NULL; iter = iter->next) |
| 148 |
{ |
| 149 |
GdkWindow *window = iter->data; |
| 150 |
GdkWindowImplQuartz *impl = GDK_WINDOW_IMPL_QUARTZ (window->impl); |
| 151 |
GdkFrameClock *frame_clock = gdk_window_get_frame_clock (window); |
| 152 |
GdkFrameTimings *timings; |
| 153 |
|
| 154 |
/* Clear the frame_link */ |
| 155 |
iter->data = NULL; |
| 156 |
if (last_next && *last_next) |
| 157 |
*last_next = NULL; |
| 158 |
last_next = &iter->next; |
| 159 |
|
| 160 |
if (frame_clock == NULL) |
| 161 |
continue; |
| 162 |
|
| 163 |
_gdk_frame_clock_thaw (frame_clock); |
| 164 |
|
| 165 |
if (impl->pending_frame_counter) |
| 166 |
{ |
| 167 |
timings = gdk_frame_clock_get_timings (frame_clock, impl->pending_frame_counter); |
| 168 |
if (timings != NULL) |
| 169 |
timings->presentation_time = presentation_time - source->refresh_interval; |
| 170 |
impl->pending_frame_counter = 0; |
| 171 |
} |
| 172 |
|
| 173 |
timings = gdk_frame_clock_get_current_timings (frame_clock); |
| 174 |
|
| 175 |
if (timings != NULL) |
| 176 |
{ |
| 177 |
timings->refresh_interval = source->refresh_interval; |
| 178 |
timings->predicted_presentation_time = source->presentation_time; |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
return G_SOURCE_CONTINUE; |
| 183 |
} |
| 184 |
|
| 185 |
static void |
| 186 |
gdk_quartz_display_init_display_link (GdkDisplay *display) |
| 187 |
{ |
| 188 |
GdkQuartzDisplay *display_quartz = GDK_QUARTZ_DISPLAY (display); |
| 189 |
|
| 190 |
display_quartz->frame_source = gdk_display_link_source_new (); |
| 191 |
g_source_set_callback (display_quartz->frame_source, |
| 192 |
gdk_quartz_display_frame_cb, |
| 193 |
display, |
| 194 |
NULL); |
| 195 |
g_source_attach (display_quartz->frame_source, NULL); |
| 196 |
} |
| 197 |
|
| 198 |
GdkDisplay * |
| 199 |
_gdk_quartz_display_open (const gchar *display_name) |
| 200 |
{ |
| 201 |
if (_gdk_display != NULL) |
| 202 |
return NULL; |
| 203 |
|
| 204 |
_gdk_display = g_object_new (gdk_quartz_display_get_type (), NULL); |
| 205 |
_gdk_display->device_manager = _gdk_device_manager_new (_gdk_display); |
| 206 |
|
| 207 |
_gdk_screen = g_object_new (gdk_quartz_screen_get_type (), NULL); |
| 208 |
_gdk_quartz_screen_init_visuals (_gdk_screen); |
| 209 |
|
| 210 |
_gdk_quartz_window_init_windowing (_gdk_display, _gdk_screen); |
| 211 |
|
| 212 |
_gdk_quartz_events_init (); |
| 213 |
|
| 214 |
/* Initialize application */ |
| 215 |
[NSApplication sharedApplication]; |
| 216 |
gdk_quartz_display_init_display_link (_gdk_display); |
| 217 |
|
| 218 |
#if 0 |
| 219 |
/* FIXME: Remove the #if 0 when we have these functions */ |
| 220 |
_gdk_quartz_dnd_init (); |
| 221 |
#endif |
| 222 |
|
| 223 |
g_signal_emit_by_name (_gdk_display, "opened"); |
| 224 |
|
| 225 |
return _gdk_display; |
| 226 |
} |
| 227 |
|
| 228 |
static const gchar * |
| 229 |
gdk_quartz_display_get_name (GdkDisplay *display) |
| 230 |
{ |
| 231 |
static gchar *display_name = NULL; |
| 232 |
|
| 233 |
if (!display_name) |
| 234 |
{ |
| 235 |
GDK_QUARTZ_ALLOC_POOL; |
| 236 |
display_name = g_strdup ([[[NSHost currentHost] name] UTF8String]); |
| 237 |
GDK_QUARTZ_RELEASE_POOL; |
| 238 |
} |
| 239 |
|
| 240 |
return display_name; |
| 241 |
} |
| 242 |
|
| 243 |
static GdkScreen * |
| 244 |
gdk_quartz_display_get_default_screen (GdkDisplay *display) |
| 245 |
{ |
| 246 |
return _gdk_screen; |
| 247 |
} |
| 248 |
|
| 249 |
static void |
| 250 |
gdk_quartz_display_beep (GdkDisplay *display) |
| 251 |
{ |
| 252 |
g_return_if_fail (GDK_IS_DISPLAY (display)); |
| 253 |
|
| 254 |
NSBeep(); |
| 255 |
} |
| 256 |
|
| 257 |
static void |
| 258 |
gdk_quartz_display_sync (GdkDisplay *display) |
| 259 |
{ |
| 260 |
/* Not needed. */ |
| 261 |
} |
| 262 |
|
| 263 |
static void |
| 264 |
gdk_quartz_display_flush (GdkDisplay *display) |
| 265 |
{ |
| 266 |
/* Not needed. */ |
| 267 |
} |
| 268 |
|
| 269 |
static gboolean |
| 270 |
gdk_quartz_display_supports_selection_notification (GdkDisplay *display) |
| 271 |
{ |
| 272 |
g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE); |
| 273 |
/* X11-only. */ |
| 274 |
return FALSE; |
| 275 |
} |
| 276 |
|
| 277 |
static gboolean |
| 278 |
gdk_quartz_display_request_selection_notification (GdkDisplay *display, |
| 279 |
GdkAtom selection) |
| 280 |
{ |
| 281 |
/* X11-only. */ |
| 282 |
return FALSE; |
| 283 |
} |
| 284 |
|
| 285 |
static gboolean |
| 286 |
gdk_quartz_display_supports_clipboard_persistence (GdkDisplay *display) |
| 287 |
{ |
| 288 |
/* X11-only */ |
| 289 |
return FALSE; |
| 290 |
} |
| 291 |
|
| 292 |
static gboolean |
| 293 |
gdk_quartz_display_supports_shapes (GdkDisplay *display) |
| 294 |
{ |
| 295 |
/* Not needed, nothing ever calls this.*/ |
| 296 |
return FALSE; |
| 297 |
} |
| 298 |
|
| 299 |
static gboolean |
| 300 |
gdk_quartz_display_supports_input_shapes (GdkDisplay *display) |
| 301 |
{ |
| 302 |
/* Not needed, nothign ever calls this. */ |
| 303 |
return FALSE; |
| 304 |
} |
| 305 |
|
| 306 |
static void |
| 307 |
gdk_quartz_display_store_clipboard (GdkDisplay *display, |
| 308 |
GdkWindow *clipboard_window, |
| 309 |
guint32 time_, |
| 310 |
const GdkAtom *targets, |
| 311 |
gint n_targets) |
| 312 |
{ |
| 313 |
/* MacOS persists pasteboard items automatically, no application |
| 314 |
* action is required. |
| 315 |
*/ |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
static gboolean |
| 320 |
gdk_quartz_display_supports_composite (GdkDisplay *display) |
| 321 |
{ |
| 322 |
/* X11-only. */ |
| 323 |
return FALSE; |
| 324 |
} |
| 325 |
|
| 326 |
static gulong |
| 327 |
gdk_quartz_display_get_next_serial (GdkDisplay *display) |
| 328 |
{ |
| 329 |
/* X11-only. */ |
| 330 |
return 0; |
| 331 |
} |
| 332 |
|
| 333 |
static void |
| 334 |
gdk_quartz_display_notify_startup_complete (GdkDisplay *display, |
| 335 |
const gchar *startup_id) |
| 336 |
{ |
| 337 |
/* This should call finishLaunching, but doing so causes Quartz to throw |
| 338 |
* "_createMenuRef called with existing principal MenuRef already" |
| 339 |
* " associated with menu". |
| 340 |
[NSApp finishLaunching]; |
| 341 |
*/ |
| 342 |
} |
| 343 |
|
| 344 |
static void |
| 345 |
gdk_quartz_display_push_error_trap (GdkDisplay *display) |
| 346 |
{ |
| 347 |
/* X11-only. */ |
| 348 |
} |
| 349 |
|
| 350 |
static gint |
| 351 |
gdk_quartz_display_pop_error_trap (GdkDisplay *display, gboolean ignore) |
| 352 |
{ |
| 353 |
/* X11 only. */ |
| 354 |
return 0; |
| 355 |
} |
| 356 |
|
| 357 |
/* The display monitor list comprises all of the CGDisplays connected |
| 358 |
to the system, some of which may not be drawable either because |
| 359 |
they're asleep or are mirroring another monitor. The NSScreens |
| 360 |
array contains only the monitors that are currently drawable and we |
| 361 |
use the index of the screens array placing GdkNSViews, so we'll use |
| 362 |
the same for determining the number of monitors and indexing them. |
| 363 |
*/ |
| 364 |
|
| 365 |
int |
| 366 |
get_active_displays (CGDirectDisplayID **displays) |
| 367 |
{ |
| 368 |
unsigned int n_displays = 0; |
| 369 |
|
| 370 |
CGGetActiveDisplayList (0, NULL, &n_displays); |
| 371 |
if (displays) |
| 372 |
{ |
| 373 |
*displays = g_new0 (CGDirectDisplayID, n_displays); |
| 374 |
CGGetActiveDisplayList (n_displays, *displays, &n_displays); |
| 375 |
} |
| 376 |
|
| 377 |
return n_displays; |
| 378 |
} |
| 379 |
|
| 380 |
static inline GdkRectangle |
| 381 |
cgrect_to_gdkrect (CGRect cgrect) |
| 382 |
{ |
| 383 |
GdkRectangle gdkrect = {(int)trunc (cgrect.origin.x), |
| 384 |
(int)trunc (cgrect.origin.y), |
| 385 |
(int)trunc (cgrect.size.width), |
| 386 |
(int)trunc (cgrect.size.height)}; |
| 387 |
return gdkrect; |
| 388 |
} |
| 389 |
|
| 390 |
static void |
| 391 |
configure_monitor (GdkMonitor *monitor, |
| 392 |
GdkQuartzDisplay *display) |
| 393 |
{ |
| 394 |
GdkQuartzMonitor *quartz_monitor = GDK_QUARTZ_MONITOR (monitor); |
| 395 |
CGSize disp_size = CGDisplayScreenSize (quartz_monitor->id); |
| 396 |
gint width = (int)trunc (disp_size.width); |
| 397 |
gint height = (int)trunc (disp_size.height); |
| 398 |
CGRect disp_bounds = CGDisplayBounds (quartz_monitor->id); |
| 399 |
CGRect main_bounds = CGDisplayBounds (CGMainDisplayID()); |
| 400 |
/* Change origin to Gdk coordinates. */ |
| 401 |
disp_bounds.origin.x = disp_bounds.origin.x + display->geometry.origin.x; |
| 402 |
disp_bounds.origin.y = |
| 403 |
display->geometry.origin.y - main_bounds.size.height + disp_bounds.origin.y; |
| 404 |
GdkRectangle disp_geometry = cgrect_to_gdkrect (disp_bounds); |
| 405 |
CGDisplayModeRef mode = CGDisplayCopyDisplayMode (quartz_monitor->id); |
| 406 |
gint refresh_rate = (int)trunc (CGDisplayModeGetRefreshRate (mode)); |
| 407 |
|
| 408 |
monitor->width_mm = width; |
| 409 |
monitor->height_mm = height; |
| 410 |
monitor->geometry = disp_geometry; |
| 411 |
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1080 |
| 412 |
if (mode && gdk_quartz_osx_version () >= GDK_OSX_MOUNTAIN_LION) |
| 413 |
{ |
| 414 |
monitor->scale_factor = CGDisplayModeGetPixelWidth (mode) / CGDisplayModeGetWidth (mode); |
| 415 |
CGDisplayModeRelease (mode); |
| 416 |
} |
| 417 |
else |
| 418 |
#endif |
| 419 |
monitor->scale_factor = 1; |
| 420 |
monitor->refresh_rate = refresh_rate; |
| 421 |
monitor->subpixel_layout = GDK_SUBPIXEL_LAYOUT_UNKNOWN; |
| 422 |
} |
| 423 |
|
| 424 |
static void |
| 425 |
display_rect (GdkQuartzDisplay *display) |
| 426 |
{ |
| 427 |
uint32_t disp, n_displays = 0; |
| 428 |
float min_x = 0.0, max_x = 0.0, min_y = 0.0, max_y = 0.0; |
| 429 |
float min_x_mm = 0.0, max_x_mm = 0.0, min_y_mm = 0.0, max_y_mm = 0.0; |
| 430 |
float main_height; |
| 431 |
CGDirectDisplayID *displays; |
| 432 |
|
| 433 |
n_displays = get_active_displays (&displays); |
| 434 |
for (disp = 0; disp < n_displays; ++disp) |
| 435 |
{ |
| 436 |
CGRect bounds = CGDisplayBounds (displays[disp]); |
| 437 |
CGSize disp_size = CGDisplayScreenSize (displays[disp]); |
| 438 |
float x_scale = disp_size.width / bounds.size.width; |
| 439 |
float y_scale = disp_size.height / bounds.size.height; |
| 440 |
if (disp == 0) |
| 441 |
main_height = bounds.size.height; |
| 442 |
min_x = MIN (min_x, bounds.origin.x); |
| 443 |
min_y = MIN (min_y, bounds.origin.y); |
| 444 |
|
| 445 |
max_x = MAX (max_x, bounds.origin.x + bounds.size.width); |
| 446 |
max_y = MAX (max_y, bounds.origin.y + bounds.size.height); |
| 447 |
min_x_mm = MIN (min_x_mm, bounds.origin.x / x_scale); |
| 448 |
min_y_mm = MIN (min_y_mm, main_height - (bounds.size.height + bounds.origin.y) / y_scale); |
| 449 |
max_x_mm = MAX (max_x_mm, (bounds.origin.x + bounds.size.width) / x_scale); |
| 450 |
max_y_mm = MAX (max_y_mm, (bounds.origin.y + bounds.size.height) / y_scale); |
| 451 |
|
| 452 |
} |
| 453 |
g_free (displays); |
| 454 |
/* Adjusts the origin to AppKit coordinates. */ |
| 455 |
display->geometry = NSMakeRect (-min_x, main_height - min_y, |
| 456 |
max_x - min_x, max_y - min_y); |
| 457 |
display->size = NSMakeSize (max_x_mm - min_x_mm, max_y_mm - min_y_mm); |
| 458 |
} |
| 459 |
|
| 460 |
static gboolean |
| 461 |
same_monitor (gconstpointer a, gconstpointer b) |
| 462 |
{ |
| 463 |
GdkQuartzMonitor *mon_a = GDK_QUARTZ_MONITOR (a); |
| 464 |
CGDirectDisplayID disp_id = (CGDirectDisplayID)GPOINTER_TO_INT (b); |
| 465 |
if (!mon_a) |
| 466 |
return FALSE; |
| 467 |
return mon_a->id == disp_id; |
| 468 |
} |
| 469 |
|
| 470 |
static void |
| 471 |
display_reconfiguration_callback (CGDirectDisplayID cg_display, |
| 472 |
CGDisplayChangeSummaryFlags flags, |
| 473 |
void *data) |
| 474 |
{ |
| 475 |
GdkQuartzDisplay *display = data; |
| 476 |
|
| 477 |
/* Ignore the begin configuration signal. */ |
| 478 |
if (flags & kCGDisplayBeginConfigurationFlag) |
| 479 |
return; |
| 480 |
|
| 481 |
if (flags & (kCGDisplayMovedFlag | kCGDisplayAddFlag | kCGDisplayEnabledFlag | |
| 482 |
kCGDisplaySetMainFlag | kCGDisplayMirrorFlag | |
| 483 |
kCGDisplayUnMirrorFlag)) |
| 484 |
{ |
| 485 |
GdkQuartzMonitor *monitor = NULL; |
| 486 |
guint index; |
| 487 |
|
| 488 |
if (!g_ptr_array_find_with_equal_func (display->monitors, |
| 489 |
GINT_TO_POINTER (cg_display), |
| 490 |
same_monitor, |
| 491 |
&index)) |
| 492 |
{ |
| 493 |
monitor = g_object_new (GDK_TYPE_QUARTZ_MONITOR, |
| 494 |
"display", display, NULL); |
| 495 |
monitor->id = cg_display; |
| 496 |
g_ptr_array_add (display->monitors, monitor); |
| 497 |
display_rect (display); |
| 498 |
configure_monitor (GDK_MONITOR (monitor), display); |
| 499 |
gdk_display_monitor_added (GDK_DISPLAY (display), |
| 500 |
GDK_MONITOR (monitor)); |
| 501 |
} |
| 502 |
else |
| 503 |
{ |
| 504 |
monitor = g_ptr_array_index (display->monitors, index); |
| 505 |
display_rect (display); |
| 506 |
configure_monitor (GDK_MONITOR (monitor), display); |
| 507 |
} |
| 508 |
} |
| 509 |
else if (flags & (kCGDisplayRemoveFlag | kCGDisplayDisabledFlag)) |
| 510 |
{ |
| 511 |
guint index; |
| 512 |
|
| 513 |
if (g_ptr_array_find_with_equal_func (display->monitors, |
| 514 |
GINT_TO_POINTER (cg_display), |
| 515 |
same_monitor, |
| 516 |
&index)) |
| 517 |
{ |
| 518 |
GdkQuartzMonitor *monitor = g_ptr_array_index (display->monitors, |
| 519 |
index); |
| 520 |
gdk_display_monitor_removed (GDK_DISPLAY (display), |
| 521 |
GDK_MONITOR (monitor)); |
| 522 |
g_ptr_array_remove_fast (display->monitors, monitor); |
| 523 |
} |
| 524 |
} |
| 525 |
|
| 526 |
g_signal_emit (display, MONITORS_CHANGED, 0); |
| 527 |
} |
| 528 |
|
| 529 |
|
| 530 |
static int |
| 531 |
gdk_quartz_display_get_n_monitors (GdkDisplay *display) |
| 532 |
{ |
| 533 |
GdkQuartzDisplay *quartz_display = GDK_QUARTZ_DISPLAY (display); |
| 534 |
return quartz_display->monitors->len; |
| 535 |
} |
| 536 |
|
| 537 |
static GdkMonitor * |
| 538 |
gdk_quartz_display_get_monitor (GdkDisplay *display, |
| 539 |
int monitor_num) |
| 540 |
{ |
| 541 |
GdkQuartzDisplay *quartz_display = GDK_QUARTZ_DISPLAY (display); |
| 542 |
int n_displays = gdk_quartz_display_get_n_monitors (display); |
| 543 |
|
| 544 |
if (monitor_num >= 0 && monitor_num < n_displays) |
| 545 |
return g_ptr_array_index (quartz_display->monitors, monitor_num); |
| 546 |
|
| 547 |
return NULL; |
| 548 |
} |
| 549 |
|
| 550 |
static GdkMonitor * |
| 551 |
gdk_quartz_display_get_primary_monitor (GdkDisplay *display) |
| 552 |
{ |
| 553 |
GdkQuartzDisplay *quartz_display = GDK_QUARTZ_DISPLAY (display); |
| 554 |
CGDirectDisplayID primary_id = CGMainDisplayID (); |
| 555 |
GdkMonitor *monitor = NULL; |
| 556 |
guint index; |
| 557 |
|
| 558 |
if (g_ptr_array_find_with_equal_func (quartz_display->monitors, |
| 559 |
GINT_TO_POINTER (primary_id), |
| 560 |
same_monitor, &index)) |
| 561 |
monitor = g_ptr_array_index (quartz_display->monitors, index); |
| 562 |
|
| 563 |
return monitor; |
| 564 |
} |
| 565 |
|
| 566 |
static GdkMonitor * |
| 567 |
gdk_quartz_display_get_monitor_at_window (GdkDisplay *display, |
| 568 |
GdkWindow *window) |
| 569 |
{ |
| 570 |
GdkWindowImplQuartz *impl = NULL; |
| 571 |
NSWindow *nswindow = NULL; |
| 572 |
NSScreen *screen = NULL; |
| 573 |
GdkMonitor *monitor = NULL; |
| 574 |
GdkWindow *onscreen_window = window; |
| 575 |
|
| 576 |
/* |
| 577 |
* This stops crashes when there is no NSWindow available on |
| 578 |
* an offscreen window which occurs for children of children |
| 579 |
* of an onscreen window (children of an onscreen window do |
| 580 |
* have NSWindow set) |
| 581 |
* https://gitlab.gnome.org/GNOME/gimp/-/issues/7608 |
| 582 |
*/ |
| 583 |
while (onscreen_window && onscreen_window->window_type == GDK_WINDOW_OFFSCREEN) |
| 584 |
onscreen_window = onscreen_window->parent; |
| 585 |
|
| 586 |
if (!onscreen_window) |
| 587 |
return NULL; |
| 588 |
|
| 589 |
impl = GDK_WINDOW_IMPL_QUARTZ (onscreen_window->impl); |
| 590 |
nswindow = impl->toplevel; |
| 591 |
screen = [nswindow screen]; |
| 592 |
|
| 593 |
if (screen) |
| 594 |
{ |
| 595 |
GdkQuartzDisplay *quartz_display = GDK_QUARTZ_DISPLAY (display); |
| 596 |
guint index; |
| 597 |
CGDirectDisplayID disp_id = |
| 598 |
[[[screen deviceDescription] |
| 599 |
objectForKey: @"NSScreenNumber"] unsignedIntValue]; |
| 600 |
if (g_ptr_array_find_with_equal_func (quartz_display->monitors, |
| 601 |
GINT_TO_POINTER (disp_id), |
| 602 |
same_monitor, &index)) |
| 603 |
monitor = g_ptr_array_index (quartz_display->monitors, index); |
| 604 |
} |
| 605 |
if (!monitor) |
| 606 |
{ |
| 607 |
GdkRectangle rect = cgrect_to_gdkrect (NSRectToCGRect ([nswindow frame])); |
| 608 |
monitor = gdk_display_get_monitor_at_point (display, |
| 609 |
rect.x + rect.width/2, |
| 610 |
rect.y + rect.height /2); |
| 611 |
} |
| 612 |
return monitor; |
| 613 |
} |
| 614 |
|
| 615 |
G_DEFINE_TYPE (GdkQuartzDisplay, gdk_quartz_display, GDK_TYPE_DISPLAY) |
| 616 |
|
| 617 |
static void |
| 618 |
gdk_quartz_display_init (GdkQuartzDisplay *display) |
| 619 |
{ |
| 620 |
uint32_t n_displays = 0, disp; |
| 621 |
CGDirectDisplayID *displays; |
| 622 |
|
| 623 |
display_rect(display); /* Initialize the overall display coordinates. */ |
| 624 |
n_displays = get_active_displays (&displays); |
| 625 |
display->monitors = g_ptr_array_new_full (n_displays, g_object_unref); |
| 626 |
for (disp = 0; disp < n_displays; ++disp) |
| 627 |
{ |
| 628 |
GdkQuartzMonitor *monitor = g_object_new (GDK_TYPE_QUARTZ_MONITOR, |
| 629 |
"display", display, NULL); |
| 630 |
monitor->id = displays[disp]; |
| 631 |
g_ptr_array_add (display->monitors, monitor); |
| 632 |
configure_monitor (GDK_MONITOR (monitor), display); |
| 633 |
} |
| 634 |
g_free (displays); |
| 635 |
CGDisplayRegisterReconfigurationCallback (display_reconfiguration_callback, |
| 636 |
display); |
| 637 |
/* So that monitors changed will keep display->geometry syncronized. */ |
| 638 |
g_signal_emit (display, MONITORS_CHANGED, 0); |
| 639 |
} |
| 640 |
|
| 641 |
static void |
| 642 |
gdk_quartz_display_dispose (GObject *object) |
| 643 |
{ |
| 644 |
GdkQuartzDisplay *quartz_display = GDK_QUARTZ_DISPLAY (object); |
| 645 |
|
| 646 |
g_ptr_array_free (quartz_display->monitors, TRUE); |
| 647 |
CGDisplayRemoveReconfigurationCallback (display_reconfiguration_callback, |
| 648 |
quartz_display); |
| 649 |
|
| 650 |
G_OBJECT_CLASS (gdk_quartz_display_parent_class)->dispose (object); |
| 651 |
} |
| 652 |
|
| 653 |
static void |
| 654 |
gdk_quartz_display_finalize (GObject *object) |
| 655 |
{ |
| 656 |
G_OBJECT_CLASS (gdk_quartz_display_parent_class)->finalize (object); |
| 657 |
} |
| 658 |
|
| 659 |
static void |
| 660 |
gdk_quartz_display_class_init (GdkQuartzDisplayClass *class) |
| 661 |
{ |
| 662 |
GObjectClass *object_class = G_OBJECT_CLASS (class); |
| 663 |
GdkDisplayClass *display_class = GDK_DISPLAY_CLASS (class); |
| 664 |
|
| 665 |
object_class->finalize = gdk_quartz_display_finalize; |
| 666 |
object_class->dispose = gdk_quartz_display_dispose; |
| 667 |
|
| 668 |
display_class->window_type = GDK_TYPE_QUARTZ_WINDOW; |
| 669 |
|
| 670 |
display_class->get_name = gdk_quartz_display_get_name; |
| 671 |
display_class->get_default_screen = gdk_quartz_display_get_default_screen; |
| 672 |
display_class->beep = gdk_quartz_display_beep; |
| 673 |
display_class->sync = gdk_quartz_display_sync; |
| 674 |
display_class->flush = gdk_quartz_display_flush; |
| 675 |
display_class->has_pending = _gdk_quartz_display_has_pending; |
| 676 |
display_class->queue_events = _gdk_quartz_display_queue_events; |
| 677 |
display_class->get_default_group = gdk_quartz_display_get_default_group; |
| 678 |
display_class->supports_selection_notification = gdk_quartz_display_supports_selection_notification; |
| 679 |
display_class->request_selection_notification = gdk_quartz_display_request_selection_notification; |
| 680 |
|
| 681 |
display_class->supports_shapes = gdk_quartz_display_supports_shapes; |
| 682 |
display_class->supports_input_shapes = gdk_quartz_display_supports_input_shapes; |
| 683 |
display_class->supports_composite = gdk_quartz_display_supports_composite; |
| 684 |
display_class->supports_cursor_alpha = _gdk_quartz_display_supports_cursor_alpha; |
| 685 |
display_class->supports_cursor_color = _gdk_quartz_display_supports_cursor_color; |
| 686 |
|
| 687 |
display_class->supports_clipboard_persistence = gdk_quartz_display_supports_clipboard_persistence; |
| 688 |
display_class->store_clipboard = gdk_quartz_display_store_clipboard; |
| 689 |
|
| 690 |
display_class->get_default_cursor_size = _gdk_quartz_display_get_default_cursor_size; |
| 691 |
display_class->get_maximal_cursor_size = _gdk_quartz_display_get_maximal_cursor_size; |
| 692 |
display_class->get_cursor_for_type = _gdk_quartz_display_get_cursor_for_type; |
| 693 |
display_class->get_cursor_for_name = _gdk_quartz_display_get_cursor_for_name; |
| 694 |
display_class->get_cursor_for_surface = _gdk_quartz_display_get_cursor_for_surface; |
| 695 |
|
| 696 |
/* display_class->get_app_launch_context = NULL; Has default. */ |
| 697 |
display_class->before_process_all_updates = _gdk_quartz_display_before_process_all_updates; |
| 698 |
display_class->after_process_all_updates = _gdk_quartz_display_after_process_all_updates; |
| 699 |
display_class->get_next_serial = gdk_quartz_display_get_next_serial; |
| 700 |
display_class->notify_startup_complete = gdk_quartz_display_notify_startup_complete; |
| 701 |
display_class->event_data_copy = _gdk_quartz_display_event_data_copy; |
| 702 |
display_class->event_data_free = _gdk_quartz_display_event_data_free; |
| 703 |
display_class->create_window_impl = _gdk_quartz_display_create_window_impl; |
| 704 |
display_class->get_keymap = _gdk_quartz_display_get_keymap; |
| 705 |
display_class->push_error_trap = gdk_quartz_display_push_error_trap; |
| 706 |
display_class->pop_error_trap = gdk_quartz_display_pop_error_trap; |
| 707 |
|
| 708 |
display_class->get_selection_owner = _gdk_quartz_display_get_selection_owner; |
| 709 |
display_class->set_selection_owner = _gdk_quartz_display_set_selection_owner; |
| 710 |
display_class->send_selection_notify = NULL; /* Ignore. X11 stuff removed in master. */ |
| 711 |
display_class->get_selection_property = _gdk_quartz_display_get_selection_property; |
| 712 |
display_class->convert_selection = _gdk_quartz_display_convert_selection; |
| 713 |
display_class->text_property_to_utf8_list = _gdk_quartz_display_text_property_to_utf8_list; |
| 714 |
display_class->utf8_to_string_target = _gdk_quartz_display_utf8_to_string_target; |
| 715 |
|
| 716 |
/* display_class->get_default_seat; The parent class default works fine. */ |
| 717 |
|
| 718 |
display_class->get_n_monitors = gdk_quartz_display_get_n_monitors; |
| 719 |
display_class->get_monitor = gdk_quartz_display_get_monitor; |
| 720 |
display_class->get_primary_monitor = gdk_quartz_display_get_primary_monitor; |
| 721 |
display_class->get_monitor_at_window = gdk_quartz_display_get_monitor_at_window; |
| 722 |
display_class->is_gl_context_current = gdk_quartz_display_is_gl_context_current; |
| 723 |
display_class->make_gl_context_current = gdk_quartz_display_make_gl_context_current; |
| 724 |
|
| 725 |
/** |
| 726 |
* GdkQuartzDisplay::monitors-changed: |
| 727 |
* @display: The object on which the signal is emitted |
| 728 |
* |
| 729 |
* The ::monitors-changed signal is emitted whenever the arrangement |
| 730 |
* of the monitors changes, either because of the addition or |
| 731 |
* removal of a monitor or because of some other configuration |
| 732 |
* change in System Preferences>Displays including a resolution |
| 733 |
* change or a position change. Note that enabling or disabling |
| 734 |
* mirroring will result in the addition or removal of the mirror |
| 735 |
* monitor(s). |
| 736 |
*/ |
| 737 |
MONITORS_CHANGED = |
| 738 |
g_signal_new (g_intern_static_string ("monitors-changed"), |
| 739 |
G_OBJECT_CLASS_TYPE (object_class), |
| 740 |
G_SIGNAL_RUN_LAST | G_SIGNAL_NO_RECURSE | G_SIGNAL_NO_HOOKS, |
| 741 |
0, NULL, NULL, NULL, |
| 742 |
G_TYPE_NONE, 0, NULL); |
| 743 |
|
| 744 |
ProcessSerialNumber psn = { 0, kCurrentProcess }; |
| 745 |
|
| 746 |
/* Make the current process a foreground application, i.e. an app |
| 747 |
* with a user interface, in case we're not running from a .app bundle |
| 748 |
*/ |
| 749 |
TransformProcessType (&psn, kProcessTransformToForegroundApplication); |
| 750 |
} |