Debian Codegraph @

gdk/quartz/gdkscreen-quartz.c

raw
1 /* gdkscreen-quartz.c
2 *
3 * Copyright (C) 2005 Imendio AB
4 * Copyright (C) 2009,2010 Kristian Rietveld <[email protected]>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "config.h"
21 /* GdkScreen is deprecated, but we need to support it still so silence the warnings.*/
22 #define GDK_DISABLE_DEPRECATION_WARNINGS 1
23 #include <gdk/gdk.h>
24
25 #include "gdkprivate-quartz.h"
26 #include "gdkdisplay-quartz.h"
27 #include "gdkmonitor-quartz.h"
28 #include "gdkinternal-quartz.h"
29
30 /* A couple of notes about this file are in order. In GDK, a
31 * GdkScreen can contain multiple monitors. A GdkScreen has an
32 * associated root window, in which the monitors are placed. The
33 * root window "spans" all monitors. The origin is at the top-left
34 * corner of the root window.
35 *
36 * Cocoa works differently. The system has a "screen" (NSScreen) for
37 * each monitor that is connected (note the conflicting definitions
38 * of screen). The screen containing the menu bar is screen 0 and the
39 * bottom-left corner of this screen is the origin of the "monitor
40 * coordinate space". All other screens are positioned according to this
41 * origin. If the menu bar is on a secondary screen (for example on
42 * a monitor hooked up to a laptop), then this screen is screen 0 and
43 * other monitors will be positioned according to the "secondary screen".
44 * The main screen is the monitor that shows the window that is currently
45 * active (has focus), the position of the menu bar does not have influence
46 * on this!
47 *
48 * Upon start up and changes in the layout of screens, we calculate the
49 * size of the GdkScreen root window that is needed to be able to place
50 * all monitors in the root window. Once that size is known, we iterate
51 * over the monitors and translate their Cocoa position to a position
52 * in the root window of the GdkScreen. This happens below in the
53 * function gdk_quartz_screen_calculate_layout().
54 *
55 * A Cocoa coordinate is always relative to the origin of the monitor
56 * coordinate space. Such coordinates are mapped to their respective
57 * position in the GdkScreen root window (_gdk_quartz_window_xy_to_gdk_xy)
58 * and vice versa (_gdk_quartz_window_gdk_xy_to_xy). Both functions can
59 * be found in gdkwindow-quartz.c. Note that Cocoa coordinates can have
60 * negative values (in case a monitor is located left or below of screen 0),
61 * but GDK coordinates can *not*!
62 */
63
64 static void gdk_quartz_screen_dispose (GObject *object);
65 static void gdk_quartz_screen_finalize (GObject *object);
66 static void gdk_quartz_screen_calculate_layout (GdkQuartzScreen *screen,
67 GdkQuartzDisplay *display);
68 static void gdk_quartz_screen_reconfigure (GdkQuartzDisplay *display,
69 GdkQuartzScreen *screen);
70
71 static const double dpi = 72.0;
72
73 G_DEFINE_TYPE (GdkQuartzScreen, gdk_quartz_screen, GDK_TYPE_SCREEN);
74
75 static void
76 gdk_quartz_screen_init (GdkQuartzScreen *quartz_screen)
77 {
78 GdkScreen *screen = GDK_SCREEN (quartz_screen);
79 /* Screen resolution is used exclusively to pass to Pango for font
80 * scaling. There's a long discussion in
81 * https://bugzilla.gnome.org/show_bug.cgi?id=787867 exploring how
82 * screen resolution and pangocairo-coretext interact. The summary
83 * is that MacOS takes care of scaling fonts for Retina screens and
84 * that while the Apple Documentation goes on about "points" they're
85 * CSS points (96/in), not typeography points (72/in) and
86 * pangocairo-coretext needs to default to that scaling factor.
87 */
88
89 g_signal_connect (_gdk_display, "monitors-changed",
90 G_CALLBACK (gdk_quartz_screen_reconfigure), quartz_screen);
91 /* The first monitors-changed should have fired already. */
92 _gdk_screen_set_resolution (screen, dpi);
93 gdk_quartz_screen_calculate_layout (quartz_screen, NULL);
94 }
95
96 static void
97 gdk_quartz_screen_dispose (GObject *object)
98 {
99 GdkQuartzScreen *screen = GDK_QUARTZ_SCREEN (object);
100
101 if (screen->screen_changed_id)
102 {
103 g_source_remove (screen->screen_changed_id);
104 screen->screen_changed_id = 0;
105 }
106
107 G_OBJECT_CLASS (gdk_quartz_screen_parent_class)->dispose (object);
108 }
109
110 static void
111 gdk_quartz_screen_finalize (GObject *object)
112 {
113 G_OBJECT_CLASS (gdk_quartz_screen_parent_class)->finalize (object);
114 }
115
116 /* Protocol to build cleanly for OSX < 10.7 */
117 @protocol ScaleFactor
118 - (CGFloat) backingScaleFactor;
119 @end
120
121 static void
122 gdk_quartz_screen_calculate_layout (GdkQuartzScreen *screen,
123 GdkQuartzDisplay *display)
124 {
125 if (!display)
126 display = GDK_QUARTZ_DISPLAY (gdk_screen_get_display (GDK_SCREEN (screen)));
127
128 /* Display geometry is the origin and size in AppKit coordinates. AppKit computes */
129 screen->width = (int)trunc (display->geometry.size.width);
130 screen->height = (int)trunc (display->geometry.size.height);
131 screen->orig_x = -(int)trunc (display->geometry.origin.x);
132 screen->orig_y = (int)trunc (display->geometry.origin.y);
133 screen->mm_width = (int)trunc (display->size.width);
134 screen->mm_height = (int)trunc (display->size.height);
135
136 }
137
138 void
139 _gdk_quartz_screen_update_window_sizes (GdkScreen *screen)
140 {
141 GList *windows, *list;
142
143 /* The size of the root window is so that it can contain all
144 * monitors attached to this machine. The monitors are laid out
145 * within this root window. We calculate the size of the root window
146 * and the positions of the different monitors in gdkscreen-quartz.c.
147 *
148 * This data is updated when the monitor configuration is changed.
149 */
150
151 /* FIXME: At some point, fetch the root window from GdkScreen. But
152 * on OS X will we only have a single root window anyway.
153 */
154 _gdk_root->x = 0;
155 _gdk_root->y = 0;
156 _gdk_root->abs_x = 0;
157 _gdk_root->abs_y = 0;
158 _gdk_root->width = gdk_screen_get_width (screen);
159 _gdk_root->height = gdk_screen_get_height (screen);
160
161 windows = gdk_screen_get_toplevel_windows (screen);
162
163 for (list = windows; list; list = list->next)
164 {
165 if (GDK_WINDOW_TYPE(list->data) == GDK_WINDOW_OFFSCREEN)
166 continue;
167 _gdk_quartz_window_update_position (list->data);
168 }
169
170 g_list_free (windows);
171 }
172
173 static void
174 gdk_quartz_screen_reconfigure (GdkQuartzDisplay *display, GdkQuartzScreen *screen)
175 {
176 int width, height;
177
178 width = gdk_screen_get_width (GDK_SCREEN (screen));
179 height = gdk_screen_get_height (GDK_SCREEN (screen));
180
181 gdk_quartz_screen_calculate_layout (screen, display);
182
183 _gdk_quartz_screen_update_window_sizes (GDK_SCREEN (screen));
184
185 g_signal_emit_by_name (screen, "monitors-changed");
186
187 if (width != gdk_screen_get_width (GDK_SCREEN (screen))
188 || height != gdk_screen_get_height (GDK_SCREEN (screen)))
189 g_signal_emit_by_name (screen, "size-changed");
190 }
191
192 static GdkDisplay *
193 gdk_quartz_screen_get_display (GdkScreen *screen)
194 {
195 return _gdk_display;
196 }
197
198 static GdkWindow *
199 gdk_quartz_screen_get_root_window (GdkScreen *screen)
200 {
201 return _gdk_root;
202 }
203
204 static gint
205 gdk_quartz_screen_get_number (GdkScreen *screen)
206 {
207 return 0;
208 }
209
210 static gint
211 gdk_quartz_screen_get_width (GdkScreen *screen)
212 {
213 return GDK_QUARTZ_SCREEN (screen)->width;
214 }
215
216 static gint
217 gdk_quartz_screen_get_height (GdkScreen *screen)
218 {
219 return GDK_QUARTZ_SCREEN (screen)->height;
220 }
221
222 static gchar *
223 gdk_quartz_screen_make_display_name (GdkScreen *screen)
224 {
225 return g_strdup (gdk_display_get_name (_gdk_display));
226 }
227
228 static GdkWindow *
229 gdk_quartz_screen_get_active_window (GdkScreen *screen)
230 {
231 return NULL;
232 }
233
234 static GList *
235 gdk_quartz_screen_get_window_stack (GdkScreen *screen)
236 {
237 return NULL;
238 }
239
240 static gboolean
241 gdk_quartz_screen_is_composited (GdkScreen *screen)
242 {
243 return TRUE;
244 }
245
246 static gint
247 gdk_quartz_screen_get_width_mm (GdkScreen *screen)
248 {
249 return GDK_QUARTZ_SCREEN (screen)->mm_width;
250 }
251
252 static gint
253 gdk_quartz_screen_get_height_mm (GdkScreen *screen)
254 {
255 return GDK_QUARTZ_SCREEN (screen)->mm_height;
256 }
257
258 static void
259 gdk_quartz_screen_class_init (GdkQuartzScreenClass *klass)
260 {
261 GObjectClass *object_class = G_OBJECT_CLASS (klass);
262 GdkScreenClass *screen_class = GDK_SCREEN_CLASS (klass);
263
264 object_class->dispose = gdk_quartz_screen_dispose;
265 object_class->finalize = gdk_quartz_screen_finalize;
266
267 screen_class->get_display = gdk_quartz_screen_get_display;
268 screen_class->get_width = gdk_quartz_screen_get_width;
269 screen_class->get_height = gdk_quartz_screen_get_height;
270 screen_class->get_width_mm = gdk_quartz_screen_get_width_mm;
271 screen_class->get_height_mm = gdk_quartz_screen_get_height_mm;
272 screen_class->get_number = gdk_quartz_screen_get_number;
273 screen_class->get_root_window = gdk_quartz_screen_get_root_window;
274 screen_class->is_composited = gdk_quartz_screen_is_composited;
275 screen_class->make_display_name = gdk_quartz_screen_make_display_name;
276 screen_class->get_active_window = gdk_quartz_screen_get_active_window;
277 screen_class->get_window_stack = gdk_quartz_screen_get_window_stack;
278 screen_class->broadcast_client_message = _gdk_quartz_screen_broadcast_client_message;
279 screen_class->get_setting = _gdk_quartz_screen_get_setting;
280 screen_class->get_rgba_visual = _gdk_quartz_screen_get_rgba_visual;
281 screen_class->get_system_visual = _gdk_quartz_screen_get_system_visual;
282 screen_class->visual_get_best_depth = _gdk_quartz_screen_visual_get_best_depth;
283 screen_class->visual_get_best_type = _gdk_quartz_screen_visual_get_best_type;
284 screen_class->visual_get_best = _gdk_quartz_screen_visual_get_best;
285 screen_class->visual_get_best_with_depth = _gdk_quartz_screen_visual_get_best_with_depth;
286 screen_class->visual_get_best_with_type = _gdk_quartz_screen_visual_get_best_with_type;
287 screen_class->visual_get_best_with_both = _gdk_quartz_screen_visual_get_best_with_both;
288 screen_class->query_depths = _gdk_quartz_screen_query_depths;
289 screen_class->query_visual_types = _gdk_quartz_screen_query_visual_types;
290 screen_class->list_visuals = _gdk_quartz_screen_list_visuals;
291 }