summaryrefslogtreecommitdiff
path: root/gdk/quartz/gdkscreen-quartz.c
blob: 6f5762711bdf5ed0297b314f76b8a393cb8eb037 (plain)
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
/* gdkscreen-quartz.c
 *
 * Copyright (C) 2005 Imendio AB
 * Copyright (C) 2009,2010  Kristian Rietveld  <kris@gtk.org>
 *
 * 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 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/>.
 */

#include "config.h"

#include <gdk/gdk.h>

#include "gdkprivate-quartz.h"
 

/* A couple of notes about this file are in order.  In GDK, a
 * GdkScreen can contain multiple monitors.  A GdkScreen has an
 * associated root window, in which the monitors are placed.  The
 * root window "spans" all monitors.  The origin is at the top-left
 * corner of the root window.
 *
 * Cocoa works differently.  The system has a "screen" (NSScreen) for
 * each monitor that is connected (note the conflicting definitions
 * of screen).  The screen containing the menu bar is screen 0 and the
 * bottom-left corner of this screen is the origin of the "monitor
 * coordinate space".  All other screens are positioned according to this
 * origin.  If the menu bar is on a secondary screen (for example on
 * a monitor hooked up to a laptop), then this screen is screen 0 and
 * other monitors will be positioned according to the "secondary screen".
 * The main screen is the monitor that shows the window that is currently
 * active (has focus), the position of the menu bar does not have influence
 * on this!
 *
 * Upon start up and changes in the layout of screens, we calculate the
 * size of the GdkScreen root window that is needed to be able to place
 * all monitors in the root window.  Once that size is known, we iterate
 * over the monitors and translate their Cocoa position to a position
 * in the root window of the GdkScreen.  This happens below in the
 * function gdk_quartz_screen_calculate_layout().
 *
 * A Cocoa coordinate is always relative to the origin of the monitor
 * coordinate space.  Such coordinates are mapped to their respective
 * position in the GdkScreen root window (_gdk_quartz_window_xy_to_gdk_xy)
 * and vice versa (_gdk_quartz_window_gdk_xy_to_xy).  Both functions can
 * be found in gdkwindow-quartz.c.  Note that Cocoa coordinates can have
 * negative values (in case a monitor is located left or below of screen 0),
 * but GDK coordinates can *not*!
 */

static void  gdk_quartz_screen_dispose          (GObject         *object);
static void  gdk_quartz_screen_finalize         (GObject         *object);
static void  gdk_quartz_screen_calculate_layout (GdkQuartzScreen *screen);

static void display_reconfiguration_callback (CGDirectDisplayID            display,
                                              CGDisplayChangeSummaryFlags  flags,
                                              void                        *userInfo);

G_DEFINE_TYPE (GdkQuartzScreen, gdk_quartz_screen, GDK_TYPE_SCREEN);

static void
gdk_quartz_screen_init (GdkQuartzScreen *quartz_screen)
{
  GdkScreen *screen = GDK_SCREEN (quartz_screen);
  NSScreen *nsscreen;

  nsscreen = [[NSScreen screens] objectAtIndex:0];
  _gdk_screen_set_resolution (screen,
                              72.0 * [nsscreen userSpaceScaleFactor]);

  gdk_quartz_screen_calculate_layout (quartz_screen);

  CGDisplayRegisterReconfigurationCallback (display_reconfiguration_callback,
                                            screen);

  quartz_screen->emit_monitors_changed = FALSE;
}

static void
gdk_quartz_screen_dispose (GObject *object)
{
  GdkQuartzScreen *screen = GDK_QUARTZ_SCREEN (object);

  if (screen->screen_changed_id)
    {
      g_source_remove (screen->screen_changed_id);
      screen->screen_changed_id = 0;
    }

  CGDisplayRemoveReconfigurationCallback (display_reconfiguration_callback,
                                          screen);

  G_OBJECT_CLASS (gdk_quartz_screen_parent_class)->dispose (object);
}

static void
gdk_quartz_screen_screen_rects_free (GdkQuartzScreen *screen)
{
  screen->n_screens = 0;
  g_clear_pointer (&screen->screen_rects, g_free);
}

static void
gdk_quartz_screen_finalize (GObject *object)
{
  GdkQuartzScreen *screen = GDK_QUARTZ_SCREEN (object);

  gdk_quartz_screen_screen_rects_free (screen);
}


static void
gdk_quartz_screen_calculate_layout (GdkQuartzScreen *screen)
{
  NSArray *array;
  int i;
  int max_x, max_y;

  GDK_QUARTZ_ALLOC_POOL;

  gdk_quartz_screen_screen_rects_free (screen);

  array = [NSScreen screens];

  screen->width = 0;
  screen->height = 0;
  screen->min_x = 0;
  screen->min_y = 0;
  max_x = max_y = 0;

  /* We determine the minimum and maximum x and y coordinates
   * covered by the monitors.  From this we can deduce the width
   * and height of the root screen.
   */
  for (i = 0; i < [array count]; i++)
    {
      NSRect rect = [[array objectAtIndex:i] frame];

      screen->min_x = MIN (screen->min_x, rect.origin.x);
      max_x = MAX (max_x, rect.origin.x + rect.size.width);

      screen->min_y = MIN (screen->min_y, rect.origin.y);
      max_y = MAX (max_y, rect.origin.y + rect.size.height);
    }

  screen->width = max_x - screen->min_x;
  screen->height = max_y - screen->min_y;

  screen->n_screens = [array count];
  screen->screen_rects = g_new0 (GdkRectangle, screen->n_screens);

  for (i = 0; i < screen->n_screens; i++)
    {
      NSScreen *nsscreen;
      NSRect rect;

      nsscreen = [array objectAtIndex:i];
      rect = [nsscreen frame];

      screen->screen_rects[i].x = rect.origin.x - screen->min_x;
      screen->screen_rects[i].y
          = screen->height - (rect.origin.y + rect.size.height) + screen->min_y;
      screen->screen_rects[i].width = rect.size.width;
      screen->screen_rects[i].height = rect.size.height;
    }

  GDK_QUARTZ_RELEASE_POOL;
}

void
_gdk_quartz_screen_update_window_sizes (GdkScreen *screen)
{
  GList *windows, *list;

  /* The size of the root window is so that it can contain all
   * monitors attached to this machine.  The monitors are laid out
   * within this root window.  We calculate the size of the root window
   * and the positions of the different monitors in gdkscreen-quartz.c.
   *
   * This data is updated when the monitor configuration is changed.
   */

  /* FIXME: At some point, fetch the root window from GdkScreen.  But
   * on OS X will we only have a single root window anyway.
   */
  _gdk_root->x = 0;
  _gdk_root->y = 0;
  _gdk_root->abs_x = 0;
  _gdk_root->abs_y = 0;
  _gdk_root->width = gdk_screen_get_width (screen);
  _gdk_root->height = gdk_screen_get_height (screen);

  windows = gdk_screen_get_toplevel_windows (screen);

  for (list = windows; list; list = list->next)
    {
      if (GDK_WINDOW_TYPE(list->data) == GDK_WINDOW_OFFSCREEN)
        continue;
      _gdk_quartz_window_update_position (list->data);
    }

  g_list_free (windows);
}

static void
process_display_reconfiguration (GdkQuartzScreen *screen)
{
  int width, height;

  width = gdk_screen_get_width (GDK_SCREEN (screen));
  height = gdk_screen_get_height (GDK_SCREEN (screen));

  gdk_quartz_screen_calculate_layout (GDK_QUARTZ_SCREEN (screen));

  _gdk_quartz_screen_update_window_sizes (GDK_SCREEN (screen));

  if (screen->emit_monitors_changed)
    {
      g_signal_emit_by_name (screen, "monitors-changed");
      screen->emit_monitors_changed = FALSE;
    }

  if (width != gdk_screen_get_width (GDK_SCREEN (screen))
      || height != gdk_screen_get_height (GDK_SCREEN (screen)))
    g_signal_emit_by_name (screen, "size-changed");
}

static gboolean
screen_changed_idle (gpointer data)
{
  GdkQuartzScreen *screen = data;

  process_display_reconfiguration (data);

  screen->screen_changed_id = 0;

  return FALSE;
}

static void
display_reconfiguration_callback (CGDirectDisplayID            display,
                                  CGDisplayChangeSummaryFlags  flags,
                                  void                        *userInfo)
{
  GdkQuartzScreen *screen = userInfo;

  if (flags & kCGDisplayBeginConfigurationFlag)
    {
      /* Ignore the begin configuration signal. */
      return;
    }
  else
    {
      /* We save information about the changes, so we can emit
       * ::monitors-changed when appropriate.  This signal must be
       * emitted when the number, size of position of one of the
       * monitors changes.
       */
      if (flags & kCGDisplayMovedFlag
          || flags & kCGDisplayAddFlag
          || flags & kCGDisplayRemoveFlag
          || flags & kCGDisplayEnabledFlag
          || flags & kCGDisplayDisabledFlag)
        screen->emit_monitors_changed = TRUE;

      /* At this point Cocoa does not know about the new screen data
       * yet, so we delay our refresh into an idle handler.
       */
      if (!screen->screen_changed_id)
        {
          screen->screen_changed_id = gdk_threads_add_idle (screen_changed_idle,
                                                            screen);
          g_source_set_name_by_id (screen->screen_changed_id, "[gtk+] screen_changed_idle");
        }
    }
}

static GdkDisplay *
gdk_quartz_screen_get_display (GdkScreen *screen)
{
  return _gdk_display;
}

static GdkWindow *
gdk_quartz_screen_get_root_window (GdkScreen *screen)
{
  return _gdk_root;
}

static gint
gdk_quartz_screen_get_number (GdkScreen *screen)
{
  return 0;
}

static gint
gdk_quartz_screen_get_width (GdkScreen *screen)
{
  return GDK_QUARTZ_SCREEN (screen)->width;
}

static gint
gdk_quartz_screen_get_height (GdkScreen *screen)
{
  return GDK_QUARTZ_SCREEN (screen)->height;
}

static gint
get_mm_from_pixels (NSScreen *screen, int pixels)
{
  /* userSpaceScaleFactor is in "pixels per point", 
   * 72 is the number of points per inch, 
   * and 25.4 is the number of millimeters per inch.
   */
#if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_3
  float dpi = [screen userSpaceScaleFactor] * 72.0;
#else
  float dpi = 96.0 / 72.0;
#endif

  return (pixels / dpi) * 25.4;
}

static NSScreen *
get_nsscreen_for_monitor (gint monitor_num)
{
  NSArray *array;
  NSScreen *screen;

  GDK_QUARTZ_ALLOC_POOL;

  array = [NSScreen screens];
  screen = [array objectAtIndex:monitor_num];

  GDK_QUARTZ_RELEASE_POOL;

  return screen;
}

static gint
gdk_quartz_screen_get_width_mm (GdkScreen *screen)
{
  return get_mm_from_pixels (get_nsscreen_for_monitor (0),
                             GDK_QUARTZ_SCREEN (screen)->width);
}

static gint
gdk_quartz_screen_get_height_mm (GdkScreen *screen)
{
  return get_mm_from_pixels (get_nsscreen_for_monitor (0),
                             GDK_QUARTZ_SCREEN (screen)->height);
}

static gint
gdk_quartz_screen_get_n_monitors (GdkScreen *screen)
{
  return GDK_QUARTZ_SCREEN (screen)->n_screens;
}

static gint
gdk_quartz_screen_get_primary_monitor (GdkScreen *screen)
{
  return 0;
}

static gint
gdk_quartz_screen_get_monitor_width_mm (GdkScreen *screen,
                                        gint       monitor_num)
{
  return get_mm_from_pixels (get_nsscreen_for_monitor (monitor_num),
                             GDK_QUARTZ_SCREEN (screen)->screen_rects[monitor_num].width);
}

static gint
gdk_quartz_screen_get_monitor_height_mm (GdkScreen *screen,
                                         gint       monitor_num)
{
  return get_mm_from_pixels (get_nsscreen_for_monitor (monitor_num),
                             GDK_QUARTZ_SCREEN (screen)->screen_rects[monitor_num].height);
}

static gchar *
gdk_quartz_screen_get_monitor_plug_name (GdkScreen *screen,
                                         gint       monitor_num)
{
  /* FIXME: Is there some useful name we could use here? */
  return NULL;
}

static void
gdk_quartz_screen_get_monitor_geometry (GdkScreen    *screen,
                                        gint          monitor_num,
                                        GdkRectangle *dest)
{
  *dest = GDK_QUARTZ_SCREEN (screen)->screen_rects[monitor_num];
}

static void
gdk_quartz_screen_get_monitor_workarea (GdkScreen    *screen,
                                        gint          monitor_num,
                                        GdkRectangle *dest)
{
  GdkQuartzScreen *quartz_screen = GDK_QUARTZ_SCREEN (screen);
  NSArray *array;
  NSScreen *nsscreen;
  NSRect rect;

  GDK_QUARTZ_ALLOC_POOL;

  array = [NSScreen screens];
  nsscreen = [array objectAtIndex:monitor_num];
  rect = [nsscreen visibleFrame];

  dest->x = rect.origin.x - quartz_screen->min_x;
  dest->y = quartz_screen->height - (rect.origin.y + rect.size.height) + quartz_screen->min_y;
  dest->width = rect.size.width;
  dest->height = rect.size.height;

  GDK_QUARTZ_RELEASE_POOL;
}

/* Protocol to build cleanly for OSX < 10.7 */
@protocol ScaleFactor
- (CGFloat) backingScaleFactor;
@end

gint
_gdk_quartz_screen_get_monitor_scale_factor (GdkScreen *screen,
                                             gint       monitor_num)
{
  GdkQuartzScreen *quartz_screen;
  NSArray *array;
  NSScreen *nsscreen;
  gint scale_factor = 1;

  quartz_screen = GDK_QUARTZ_SCREEN (screen);

  GDK_QUARTZ_ALLOC_POOL;

  array = [NSScreen screens];
  nsscreen = [array objectAtIndex:monitor_num];

  if (gdk_quartz_osx_version() >= GDK_OSX_LION)
    scale_factor = [(id <ScaleFactor>) nsscreen backingScaleFactor];

  GDK_QUARTZ_RELEASE_POOL;

  return scale_factor;
}

static gchar *
gdk_quartz_screen_make_display_name (GdkScreen *screen)
{
  return g_strdup (gdk_display_get_name (_gdk_display));
}

static GdkWindow *
gdk_quartz_screen_get_active_window (GdkScreen *screen)
{
  return NULL;
}

static GList *
gdk_quartz_screen_get_window_stack (GdkScreen *screen)
{
  return NULL;
}

static gboolean
gdk_quartz_screen_is_composited (GdkScreen *screen)
{
  return TRUE;
}

static void
gdk_quartz_screen_class_init (GdkQuartzScreenClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  GdkScreenClass *screen_class = GDK_SCREEN_CLASS (klass);

  object_class->dispose = gdk_quartz_screen_dispose;
  object_class->finalize = gdk_quartz_screen_finalize;

  screen_class->get_display = gdk_quartz_screen_get_display;
  screen_class->get_width = gdk_quartz_screen_get_width;
  screen_class->get_height = gdk_quartz_screen_get_height;
  screen_class->get_width_mm = gdk_quartz_screen_get_width_mm;
  screen_class->get_height_mm = gdk_quartz_screen_get_height_mm;
  screen_class->get_number = gdk_quartz_screen_get_number;
  screen_class->get_root_window = gdk_quartz_screen_get_root_window;
  screen_class->get_n_monitors = gdk_quartz_screen_get_n_monitors;
  screen_class->get_primary_monitor = gdk_quartz_screen_get_primary_monitor;
  screen_class->get_monitor_width_mm = gdk_quartz_screen_get_monitor_width_mm;
  screen_class->get_monitor_height_mm = gdk_quartz_screen_get_monitor_height_mm;
  screen_class->get_monitor_plug_name = gdk_quartz_screen_get_monitor_plug_name;
  screen_class->get_monitor_geometry = gdk_quartz_screen_get_monitor_geometry;
  screen_class->get_monitor_workarea = gdk_quartz_screen_get_monitor_workarea;
  screen_class->is_composited = gdk_quartz_screen_is_composited;
  screen_class->make_display_name = gdk_quartz_screen_make_display_name;
  screen_class->get_active_window = gdk_quartz_screen_get_active_window;
  screen_class->get_window_stack = gdk_quartz_screen_get_window_stack;
  screen_class->broadcast_client_message = _gdk_quartz_screen_broadcast_client_message;
  screen_class->get_setting = _gdk_quartz_screen_get_setting;
  screen_class->get_rgba_visual = _gdk_quartz_screen_get_rgba_visual;
  screen_class->get_system_visual = _gdk_quartz_screen_get_system_visual;
  screen_class->visual_get_best_depth = _gdk_quartz_screen_visual_get_best_depth;
  screen_class->visual_get_best_type = _gdk_quartz_screen_visual_get_best_type;
  screen_class->visual_get_best = _gdk_quartz_screen_visual_get_best;
  screen_class->visual_get_best_with_depth = _gdk_quartz_screen_visual_get_best_with_depth;
  screen_class->visual_get_best_with_type = _gdk_quartz_screen_visual_get_best_with_type;
  screen_class->visual_get_best_with_both = _gdk_quartz_screen_visual_get_best_with_both;
  screen_class->query_depths = _gdk_quartz_screen_query_depths;
  screen_class->query_visual_types = _gdk_quartz_screen_query_visual_types;
  screen_class->list_visuals = _gdk_quartz_screen_list_visuals;
  screen_class->get_monitor_scale_factor = _gdk_quartz_screen_get_monitor_scale_factor;
}