Debian Codegraph @

gdk/win32/gdkdisplay-win32.c

raw
1 /* GDK - The GIMP Drawing Kit
2 * Copyright (C) 2002,2005 Hans Breuer
3 * Copyright (C) 2003 Tor Lillqvist
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 #define _WIN32_WINNT 0x0600
22
23 #include "gdk.h"
24 #include "gdkprivate-win32.h"
25 #include "gdkdisplay-win32.h"
26 #include "gdkdevicemanager-win32.h"
27 #include "gdkglcontext-win32.h"
28 #include "gdkwin32display.h"
29 #include "gdkwin32screen.h"
30 #include "gdkwin32window.h"
31 #include "gdkmonitor-win32.h"
32 #include "gdkwin32.h"
33
34 #include "gdkwin32langnotification.h"
35
36 #ifndef IMAGE_FILE_MACHINE_ARM64
37 # define IMAGE_FILE_MACHINE_ARM64 0xAA64
38 #endif
39
40 static int debug_indent = 0;
41
42 static GdkMonitor *
43 _gdk_win32_display_find_matching_monitor (GdkWin32Display *win32_display,
44 GdkMonitor *needle)
45 {
46 int i;
47
48 for (i = 0; i < win32_display->monitors->len; i++)
49 {
50 GdkWin32Monitor *m;
51
52 m = GDK_WIN32_MONITOR (g_ptr_array_index (win32_display->monitors, i));
53
54 if (_gdk_win32_monitor_compare (m, GDK_WIN32_MONITOR (needle)) == 0)
55 return GDK_MONITOR (m);
56 }
57
58 return NULL;
59 }
60
61 gboolean
62 _gdk_win32_display_init_monitors (GdkWin32Display *win32_display)
63 {
64 GdkDisplay *display = GDK_DISPLAY (win32_display);
65 GPtrArray *new_monitors;
66 gint i;
67 gboolean changed = FALSE;
68 GdkWin32Monitor *primary_to_move = NULL;
69
70 for (i = 0; i < win32_display->monitors->len; i++)
71 GDK_WIN32_MONITOR (g_ptr_array_index (win32_display->monitors, i))->remove = TRUE;
72
73 new_monitors = _gdk_win32_display_get_monitor_list (win32_display);
74
75 for (i = 0; i < new_monitors->len; i++)
76 {
77 GdkWin32Monitor *w32_m;
78 GdkMonitor *m;
79 GdkWin32Monitor *w32_ex_monitor;
80 GdkMonitor *ex_monitor;
81 GdkRectangle geometry, ex_geometry;
82 GdkRectangle workarea, ex_workarea;
83
84 w32_m = GDK_WIN32_MONITOR (g_ptr_array_index (new_monitors, i));
85 m = GDK_MONITOR (w32_m);
86 ex_monitor = _gdk_win32_display_find_matching_monitor (win32_display, m);
87 w32_ex_monitor = GDK_WIN32_MONITOR (ex_monitor);
88
89 if (ex_monitor == NULL)
90 {
91 w32_m->add = TRUE;
92 changed = TRUE;
93 continue;
94 }
95
96 w32_ex_monitor->remove = FALSE;
97
98 if (i == 0)
99 primary_to_move = w32_ex_monitor;
100
101 gdk_monitor_get_geometry (m, &geometry);
102 gdk_monitor_get_geometry (ex_monitor, &ex_geometry);
103 gdk_monitor_get_workarea (m, &workarea);
104 gdk_monitor_get_workarea (ex_monitor, &ex_workarea);
105
106 if (memcmp (&workarea, &ex_workarea, sizeof (GdkRectangle)) != 0)
107 {
108 w32_ex_monitor->work_rect = workarea;
109 changed = TRUE;
110 }
111
112 if (memcmp (&geometry, &ex_geometry, sizeof (GdkRectangle)) != 0)
113 {
114 gdk_monitor_set_size (ex_monitor, geometry.width, geometry.height);
115 gdk_monitor_set_position (ex_monitor, geometry.x, geometry.y);
116 changed = TRUE;
117 }
118
119 if (gdk_monitor_get_width_mm (m) != gdk_monitor_get_width_mm (ex_monitor) ||
120 gdk_monitor_get_height_mm (m) != gdk_monitor_get_height_mm (ex_monitor))
121 {
122 gdk_monitor_set_physical_size (ex_monitor,
123 gdk_monitor_get_width_mm (m),
124 gdk_monitor_get_height_mm (m));
125 changed = TRUE;
126 }
127
128 if (g_strcmp0 (gdk_monitor_get_model (m), gdk_monitor_get_model (ex_monitor)) != 0)
129 {
130 gdk_monitor_set_model (ex_monitor,
131 gdk_monitor_get_model (m));
132 changed = TRUE;
133 }
134
135 if (g_strcmp0 (gdk_monitor_get_manufacturer (m), gdk_monitor_get_manufacturer (ex_monitor)) != 0)
136 {
137 gdk_monitor_set_manufacturer (ex_monitor,
138 gdk_monitor_get_manufacturer (m));
139 changed = TRUE;
140 }
141
142 if (gdk_monitor_get_refresh_rate (m) != gdk_monitor_get_refresh_rate (ex_monitor))
143 {
144 gdk_monitor_set_refresh_rate (ex_monitor, gdk_monitor_get_refresh_rate (m));
145 changed = TRUE;
146 }
147
148 if (gdk_monitor_get_scale_factor (m) != gdk_monitor_get_scale_factor (ex_monitor))
149 {
150 gdk_monitor_set_scale_factor (ex_monitor, gdk_monitor_get_scale_factor (m));
151 changed = TRUE;
152 }
153
154 if (gdk_monitor_get_subpixel_layout (m) != gdk_monitor_get_subpixel_layout (ex_monitor))
155 {
156 gdk_monitor_set_subpixel_layout (ex_monitor, gdk_monitor_get_subpixel_layout (m));
157 changed = TRUE;
158 }
159 }
160
161 for (i = win32_display->monitors->len - 1; i >= 0; i--)
162 {
163 GdkWin32Monitor *w32_ex_monitor;
164 GdkMonitor *ex_monitor;
165
166 w32_ex_monitor = GDK_WIN32_MONITOR (g_ptr_array_index (win32_display->monitors, i));
167 ex_monitor = GDK_MONITOR (w32_ex_monitor);
168
169 if (!w32_ex_monitor->remove)
170 continue;
171
172 changed = TRUE;
173 gdk_display_monitor_removed (display, ex_monitor);
174 g_ptr_array_remove_index (win32_display->monitors, i);
175 }
176
177 for (i = 0; i < new_monitors->len; i++)
178 {
179 GdkWin32Monitor *w32_m;
180 GdkMonitor *m;
181
182 w32_m = GDK_WIN32_MONITOR (g_ptr_array_index (new_monitors, i));
183 m = GDK_MONITOR (w32_m);
184
185 if (!w32_m->add)
186 continue;
187
188 gdk_display_monitor_added (display, m);
189
190 if (i == 0)
191 g_ptr_array_insert (win32_display->monitors, 0, g_object_ref (w32_m));
192 else
193 g_ptr_array_add (win32_display->monitors, g_object_ref (w32_m));
194 }
195
196 g_ptr_array_free (new_monitors, TRUE);
197
198 if (primary_to_move)
199 {
200 g_ptr_array_remove (win32_display->monitors, g_object_ref (primary_to_move));
201 g_ptr_array_insert (win32_display->monitors, 0, primary_to_move);
202 changed = TRUE;
203 }
204 return changed;
205 }
206
207
208 /**
209 * gdk_win32_display_set_cursor_theme:
210 * @display: (type GdkWin32Display): a #GdkDisplay
211 * @name: (allow-none): the name of the cursor theme to use, or %NULL to unset
212 * a previously set value
213 * @size: the cursor size to use, or 0 to keep the previous size
214 *
215 * Sets the cursor theme from which the images for cursor
216 * should be taken.
217 *
218 * If the windowing system supports it, existing cursors created
219 * with gdk_cursor_new(), gdk_cursor_new_for_display() and
220 * gdk_cursor_new_from_name() are updated to reflect the theme
221 * change. Custom cursors constructed with
222 * gdk_cursor_new_from_pixbuf() will have to be handled
223 * by the application (GTK+ applications can learn about
224 * cursor theme changes by listening for change notification
225 * for the corresponding #GtkSetting).
226 *
227 * Since: 3.18
228 */
229 void
230 gdk_win32_display_set_cursor_theme (GdkDisplay *display,
231 const gchar *name,
232 gint size)
233 {
234 gint cursor_size;
235 gint w, h;
236 Win32CursorTheme *theme;
237 GdkWin32Display *win32_display = GDK_WIN32_DISPLAY (display);
238
239 g_assert (win32_display);
240
241 if (name == NULL)
242 name = "system";
243
244 w = GetSystemMetrics (SM_CXCURSOR);
245 h = GetSystemMetrics (SM_CYCURSOR);
246
247 /* We can load cursors of any size, but SetCursor() will scale them back
248 * to this value. It's possible to break that restrictions with SetSystemCursor(),
249 * but that will override cursors for the whole desktop session.
250 */
251 cursor_size = (w == h) ? w : size;
252
253 if (win32_display->cursor_theme_name != NULL &&
254 g_strcmp0 (name, win32_display->cursor_theme_name) == 0 &&
255 win32_display->cursor_theme_size == cursor_size)
256 return;
257
258 theme = win32_cursor_theme_load (name, cursor_size);
259 if (theme == NULL)
260 {
261 g_warning ("Failed to load cursor theme %s", name);
262 return;
263 }
264
265 if (win32_display->cursor_theme)
266 {
267 win32_cursor_theme_destroy (win32_display->cursor_theme);
268 win32_display->cursor_theme = NULL;
269 }
270
271 win32_display->cursor_theme = theme;
272 g_free (win32_display->cursor_theme_name);
273 win32_display->cursor_theme_name = g_strdup (name);
274 win32_display->cursor_theme_size = cursor_size;
275
276 _gdk_win32_display_update_cursors (win32_display);
277 }
278
279 Win32CursorTheme *
280 _gdk_win32_display_get_cursor_theme (GdkWin32Display *win32_display)
281 {
282 Win32CursorTheme *theme;
283
284 g_assert (win32_display->cursor_theme_name);
285
286 theme = win32_display->cursor_theme;
287 if (!theme)
288 {
289 theme = win32_cursor_theme_load (win32_display->cursor_theme_name,
290 win32_display->cursor_theme_size);
291 if (theme == NULL)
292 {
293 g_warning ("Failed to load cursor theme %s",
294 win32_display->cursor_theme_name);
295 return NULL;
296 }
297 win32_display->cursor_theme = theme;
298 }
299
300 return theme;
301 }
302
303 static gulong
304 gdk_win32_display_get_next_serial (GdkDisplay *display)
305 {
306 return 0;
307 }
308
309 static LRESULT CALLBACK
310 inner_display_change_window_procedure (HWND hwnd,
311 UINT message,
312 WPARAM wparam,
313 LPARAM lparam)
314 {
315 switch (message)
316 {
317 case WM_DESTROY:
318 {
319 PostQuitMessage (0);
320 return 0;
321 }
322 case WM_DISPLAYCHANGE:
323 {
324 GdkWin32Display *win32_display = GDK_WIN32_DISPLAY (_gdk_display);
325
326 _gdk_win32_screen_on_displaychange_event (GDK_WIN32_SCREEN (win32_display->screen));
327 return 0;
328 }
329 default:
330 /* Otherwise call DefWindowProcW(). */
331 GDK_NOTE (EVENTS, g_print (" DefWindowProcW"));
332 return DefWindowProc (hwnd, message, wparam, lparam);
333 }
334 }
335
336 static LRESULT CALLBACK
337 display_change_window_procedure (HWND hwnd,
338 UINT message,
339 WPARAM wparam,
340 LPARAM lparam)
341 {
342 LRESULT retval;
343
344 GDK_NOTE (EVENTS, g_print ("%s%*s%s %p",
345 (debug_indent > 0 ? "\n" : ""),
346 debug_indent, "",
347 _gdk_win32_message_to_string (message), hwnd));
348 debug_indent += 2;
349 retval = inner_display_change_window_procedure (hwnd, message, wparam, lparam);
350 debug_indent -= 2;
351
352 GDK_NOTE (EVENTS, g_print (" => %" G_GINT64_FORMAT "%s", (gint64) retval, (debug_indent == 0 ? "\n" : "")));
353
354 return retval;
355 }
356
357 /* Use a hidden window to be notified about display changes */
358 static void
359 register_display_change_notification (GdkDisplay *display)
360 {
361 GdkWin32Display *display_win32 = GDK_WIN32_DISPLAY (display);
362 WNDCLASS wclass = { 0, };
363 ATOM klass;
364
365 wclass.lpszClassName = "GdkDisplayChange";
366 wclass.lpfnWndProc = display_change_window_procedure;
367 wclass.hInstance = _gdk_app_hmodule;
368
369 klass = RegisterClass (&wclass);
370 if (klass)
371 {
372 display_win32->hwnd = CreateWindow (MAKEINTRESOURCE (klass),
373 NULL, WS_POPUP,
374 0, 0, 0, 0, NULL, NULL,
375 _gdk_app_hmodule, NULL);
376 if (!display_win32->hwnd)
377 {
378 UnregisterClass (MAKEINTRESOURCE (klass), _gdk_app_hmodule);
379 }
380 }
381 }
382
383 GdkDisplay *
384 _gdk_win32_display_open (const gchar *display_name)
385 {
386 GdkWin32Display *win32_display;
387
388 GDK_NOTE (MISC, g_print ("gdk_display_open: %s\n", (display_name ? display_name : "NULL")));
389
390 if (display_name == NULL ||
391 g_ascii_strcasecmp (display_name,
392 gdk_display_get_name (_gdk_display)) == 0)
393 {
394 if (_gdk_display != NULL)
395 {
396 GDK_NOTE (MISC, g_print ("... return _gdk_display\n"));
397 return _gdk_display;
398 }
399 }
400 else
401 {
402 GDK_NOTE (MISC, g_print ("... return NULL\n"));
403 return NULL;
404 }
405
406 _gdk_display = g_object_new (GDK_TYPE_WIN32_DISPLAY, NULL);
407 win32_display = GDK_WIN32_DISPLAY (_gdk_display);
408
409 win32_display->screen = g_object_new (GDK_TYPE_WIN32_SCREEN, NULL);
410
411 _gdk_events_init (_gdk_display);
412
413 _gdk_input_ignore_core = 0;
414
415 _gdk_display->device_manager = g_object_new (GDK_TYPE_DEVICE_MANAGER_WIN32,
416 "display", _gdk_display,
417 NULL);
418
419 _gdk_win32_lang_notification_init ();
420 _gdk_dnd_init ();
421
422 /* Precalculate display name */
423 (void) gdk_display_get_name (_gdk_display);
424
425 register_display_change_notification (_gdk_display);
426
427 g_signal_emit_by_name (_gdk_display, "opened");
428
429 GDK_NOTE (MISC, g_print ("... _gdk_display now set up\n"));
430
431 return _gdk_display;
432 }
433
434 G_DEFINE_TYPE (GdkWin32Display, gdk_win32_display, GDK_TYPE_DISPLAY)
435
436 static const gchar *
437 gdk_win32_display_get_name (GdkDisplay *display)
438 {
439 HDESK hdesk = GetThreadDesktop (GetCurrentThreadId ());
440 char dummy;
441 char *desktop_name;
442 HWINSTA hwinsta = GetProcessWindowStation ();
443 char *window_station_name;
444 DWORD n;
445 DWORD session_id;
446 char *display_name;
447 static const char *display_name_cache = NULL;
448 typedef BOOL (WINAPI *PFN_ProcessIdToSessionId) (DWORD, DWORD *);
449 PFN_ProcessIdToSessionId processIdToSessionId;
450
451 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
452
453 if (display_name_cache != NULL)
454 return display_name_cache;
455
456 n = 0;
457 GetUserObjectInformation (hdesk, UOI_NAME, &dummy, 0, &n);
458 if (n == 0)
459 desktop_name = "Default";
460 else
461 {
462 n++;
463 desktop_name = g_alloca (n + 1);
464 memset (desktop_name, 0, n + 1);
465
466 if (!GetUserObjectInformation (hdesk, UOI_NAME, desktop_name, n, &n))
467 desktop_name = "Default";
468 }
469
470 n = 0;
471 GetUserObjectInformation (hwinsta, UOI_NAME, &dummy, 0, &n);
472 if (n == 0)
473 window_station_name = "WinSta0";
474 else
475 {
476 n++;
477 window_station_name = g_alloca (n + 1);
478 memset (window_station_name, 0, n + 1);
479
480 if (!GetUserObjectInformation (hwinsta, UOI_NAME, window_station_name, n, &n))
481 window_station_name = "WinSta0";
482 }
483
484 processIdToSessionId = (PFN_ProcessIdToSessionId) GetProcAddress (GetModuleHandle ("kernel32.dll"), "ProcessIdToSessionId");
485 if (!processIdToSessionId || !processIdToSessionId (GetCurrentProcessId (), &session_id))
486 session_id = 0;
487
488 display_name = g_strdup_printf ("%ld\\%s\\%s",
489 session_id,
490 window_station_name,
491 desktop_name);
492
493 GDK_NOTE (MISC, g_print ("gdk_win32_display_get_name: %s\n", display_name));
494
495 display_name_cache = display_name;
496
497 return display_name_cache;
498 }
499
500 static GdkScreen *
501 gdk_win32_display_get_default_screen (GdkDisplay *display)
502 {
503 g_return_val_if_fail (GDK_IS_WIN32_DISPLAY (display), NULL);
504
505 return GDK_WIN32_DISPLAY (display)->screen;
506 }
507
508 static GdkWindow *
509 gdk_win32_display_get_default_group (GdkDisplay *display)
510 {
511 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
512
513 g_warning ("gdk_display_get_default_group not yet implemented");
514
515 return NULL;
516 }
517
518 static gboolean
519 gdk_win32_display_supports_selection_notification (GdkDisplay *display)
520 {
521 g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
522
523 return TRUE;
524 }
525
526 /*
527 * maybe this should be integrated with the default message loop - or maybe not ;-)
528 */
529 static LRESULT CALLBACK
530 inner_clipboard_window_procedure (HWND hwnd,
531 UINT message,
532 WPARAM wparam,
533 LPARAM lparam)
534 {
535 switch (message)
536 {
537 case WM_DESTROY: /* remove us from chain */
538 {
539 RemoveClipboardFormatListener (hwnd);
540 PostQuitMessage (0);
541 return 0;
542 }
543 case WM_CLIPBOARDUPDATE:
544 {
545 HWND hwnd_owner;
546 HWND stored_hwnd_owner;
547 HWND hwnd_opener;
548 GdkEvent *event;
549 GdkWindow *owner;
550 GdkWindow *stored_owner;
551 GdkWin32Selection *win32_sel = _gdk_win32_selection_get ();
552
553 SetLastError (0);
554 hwnd_owner = GetClipboardOwner ();
555
556 if (hwnd_owner == NULL && GetLastError () != 0)
557 WIN32_API_FAILED ("GetClipboardOwner");
558
559 hwnd_opener = GetOpenClipboardWindow ();
560
561 GDK_NOTE (DND, g_print (" drawclipboard owner: %p; opener %p ", hwnd_owner, hwnd_opener));
562
563 #ifdef G_ENABLE_DEBUG
564 if (_gdk_debug_flags & GDK_DEBUG_DND)
565 {
566 if (win32_sel->clipboard_opened_for != INVALID_HANDLE_VALUE ||
567 OpenClipboard (hwnd))
568 {
569 UINT nFormat = 0;
570
571 while ((nFormat = EnumClipboardFormats (nFormat)) != 0)
572 g_print ("%s ", _gdk_win32_cf_to_string (nFormat));
573
574 if (win32_sel->clipboard_opened_for == INVALID_HANDLE_VALUE)
575 CloseClipboard ();
576 }
577 else
578 {
579 WIN32_API_FAILED ("OpenClipboard");
580 }
581 }
582 #endif
583
584 GDK_NOTE (DND, g_print (" \n"));
585
586 owner = gdk_win32_window_lookup_for_display (_gdk_display, hwnd_owner);
587 if (owner == NULL)
588 owner = gdk_win32_window_foreign_new_for_display (_gdk_display, hwnd_owner);
589
590 stored_owner = _gdk_win32_display_get_selection_owner (gdk_display_get_default (),
591 GDK_SELECTION_CLIPBOARD);
592
593 if (stored_owner)
594 stored_hwnd_owner = GDK_WINDOW_HWND (stored_owner);
595 else
596 stored_hwnd_owner = NULL;
597
598 if (stored_hwnd_owner != hwnd_owner)
599 {
600 if (win32_sel->clipboard_opened_for != INVALID_HANDLE_VALUE)
601 {
602 CloseClipboard ();
603 GDK_NOTE (DND, g_print ("Closed clipboard @ %s:%d\n", __FILE__, __LINE__));
604 }
605
606 win32_sel->clipboard_opened_for = INVALID_HANDLE_VALUE;
607
608 _gdk_win32_clear_clipboard_queue ();
609 }
610
611 event = gdk_event_new (GDK_OWNER_CHANGE);
612 event->owner_change.window = gdk_get_default_root_window ();
613 event->owner_change.owner = owner;
614 event->owner_change.reason = GDK_OWNER_CHANGE_NEW_OWNER;
615 event->owner_change.selection = GDK_SELECTION_CLIPBOARD;
616 event->owner_change.time = _gdk_win32_get_next_tick (0);
617 event->owner_change.selection_time = GDK_CURRENT_TIME;
618 _gdk_win32_append_event (event);
619
620 /* clear error to avoid confusing SetClipboardViewer() return */
621 SetLastError (0);
622 return 0;
623 }
624 default:
625 /* Otherwise call DefWindowProcW(). */
626 GDK_NOTE (EVENTS, g_print (" DefWindowProcW"));
627 return DefWindowProc (hwnd, message, wparam, lparam);
628 }
629 }
630
631 static LRESULT CALLBACK
632 _clipboard_window_procedure (HWND hwnd,
633 UINT message,
634 WPARAM wparam,
635 LPARAM lparam)
636 {
637 LRESULT retval;
638
639 GDK_NOTE (EVENTS, g_print ("%s%*s%s %p",
640 (debug_indent > 0 ? "\n" : ""),
641 debug_indent, "",
642 _gdk_win32_message_to_string (message), hwnd));
643 debug_indent += 2;
644 retval = inner_clipboard_window_procedure (hwnd, message, wparam, lparam);
645 debug_indent -= 2;
646
647 GDK_NOTE (EVENTS, g_print (" => %" G_GINT64_FORMAT "%s", (gint64) retval, (debug_indent == 0 ? "\n" : "")));
648
649 return retval;
650 }
651
652 /*
653 * Creates a hidden window and adds it to the clipboard chain
654 */
655 static gboolean
656 register_clipboard_notification (GdkDisplay *display)
657 {
658 GdkWin32Display *display_win32 = GDK_WIN32_DISPLAY (display);
659 WNDCLASS wclass = { 0, };
660 ATOM klass;
661
662 wclass.lpszClassName = "GdkClipboardNotification";
663 wclass.lpfnWndProc = _clipboard_window_procedure;
664 wclass.hInstance = _gdk_app_hmodule;
665
666 klass = RegisterClass (&wclass);
667 if (!klass)
668 return FALSE;
669
670 display_win32->clipboard_hwnd = CreateWindow (MAKEINTRESOURCE (klass),
671 NULL, WS_POPUP,
672 0, 0, 0, 0, NULL, NULL,
673 _gdk_app_hmodule, NULL);
674
675 if (display_win32->clipboard_hwnd == NULL)
676 goto failed;
677
678 SetLastError (0);
679
680 if (AddClipboardFormatListener (display_win32->clipboard_hwnd) == FALSE)
681 goto failed;
682
683 return TRUE;
684
685 failed:
686 g_critical ("Failed to install clipboard viewer");
687 UnregisterClass (MAKEINTRESOURCE (klass), _gdk_app_hmodule);
688 return FALSE;
689 }
690
691 static gboolean
692 gdk_win32_display_request_selection_notification (GdkDisplay *display,
693 GdkAtom selection)
694
695 {
696 GdkWin32Display *display_win32 = GDK_WIN32_DISPLAY (display);
697 gboolean ret = FALSE;
698 gchar *selection_name = gdk_atom_name (selection);
699
700 GDK_NOTE (DND,
701 g_print ("gdk_display_request_selection_notification (..., %s)",
702 selection_name));
703
704 if (selection == GDK_SELECTION_CLIPBOARD ||
705 selection == GDK_SELECTION_PRIMARY)
706 {
707 if (display_win32->clipboard_hwnd == NULL)
708 {
709 if (register_clipboard_notification (display))
710 GDK_NOTE (DND, g_print (" registered"));
711 else
712 GDK_NOTE (DND, g_print (" failed to register"));
713 }
714 ret = (display_win32->clipboard_hwnd != NULL);
715 }
716 else
717 {
718 GDK_NOTE (DND, g_print (" unsupported"));
719 ret = FALSE;
720 }
721
722 g_free (selection_name);
723
724 GDK_NOTE (DND, g_print (" -> %s\n", ret ? "TRUE" : "FALSE"));
725 return ret;
726 }
727
728 static gboolean
729 gdk_win32_display_supports_clipboard_persistence (GdkDisplay *display)
730 {
731 return TRUE;
732 }
733
734 static void
735 gdk_win32_display_store_clipboard (GdkDisplay *display,
736 GdkWindow *clipboard_window,
737 guint32 time_,
738 const GdkAtom *targets,
739 gint n_targets)
740 {
741 GdkEvent tmp_event;
742 SendMessage (GDK_WINDOW_HWND (clipboard_window), WM_RENDERALLFORMATS, 0, 0);
743
744 memset (&tmp_event, 0, sizeof (tmp_event));
745 tmp_event.selection.type = GDK_SELECTION_NOTIFY;
746 tmp_event.selection.window = clipboard_window;
747 tmp_event.selection.send_event = FALSE;
748 tmp_event.selection.selection = _gdk_win32_selection_atom (GDK_WIN32_ATOM_INDEX_CLIPBOARD_MANAGER);
749 tmp_event.selection.target = 0;
750 tmp_event.selection.property = GDK_NONE;
751 tmp_event.selection.requestor = 0;
752 tmp_event.selection.time = GDK_CURRENT_TIME;
753
754 gdk_event_put (&tmp_event);
755 }
756
757 static gboolean
758 gdk_win32_display_supports_shapes (GdkDisplay *display)
759 {
760 g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
761
762 return TRUE;
763 }
764
765 static gboolean
766 gdk_win32_display_supports_input_shapes (GdkDisplay *display)
767 {
768 g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
769
770 /* Partially supported, see WM_NCHITTEST handler. */
771 return TRUE;
772 }
773
774 static gboolean
775 gdk_win32_display_supports_composite (GdkDisplay *display)
776 {
777 return FALSE;
778 }
779
780 static void
781 gdk_win32_display_beep (GdkDisplay *display)
782 {
783 g_return_if_fail (display == gdk_display_get_default());
784 if (!MessageBeep (-1))
785 Beep(1000, 50);
786 }
787
788 static void
789 gdk_win32_display_flush (GdkDisplay * display)
790 {
791 g_return_if_fail (display == _gdk_display);
792
793 GdiFlush ();
794 }
795
796 static void
797 gdk_win32_display_sync (GdkDisplay * display)
798 {
799 g_return_if_fail (display == _gdk_display);
800
801 GdiFlush ();
802 }
803
804 static void
805 gdk_win32_display_dispose (GObject *object)
806 {
807 GdkWin32Display *display_win32 = GDK_WIN32_DISPLAY (object);
808
809 _gdk_screen_close (display_win32->screen);
810
811 #ifdef GDK_WIN32_ENABLE_EGL
812 if (display_win32->egl_disp != EGL_NO_DISPLAY)
813 {
814 eglTerminate (display_win32->egl_disp);
815 display_win32->egl_disp = EGL_NO_DISPLAY;
816 }
817 #endif
818
819 if (display_win32->hwnd != NULL)
820 {
821 DestroyWindow (display_win32->hwnd);
822 display_win32->hwnd = NULL;
823 }
824
825 if (display_win32->clipboard_hwnd != NULL)
826 {
827 DestroyWindow (display_win32->clipboard_hwnd);
828 display_win32->clipboard_hwnd = NULL;
829 }
830
831 if (display_win32->have_at_least_win81)
832 {
833 if (display_win32->shcore_funcs.hshcore != NULL)
834 {
835 FreeLibrary (display_win32->shcore_funcs.hshcore);
836 display_win32->shcore_funcs.hshcore = NULL;
837 }
838 }
839
840 G_OBJECT_CLASS (gdk_win32_display_parent_class)->dispose (object);
841 }
842
843 static void
844 gdk_win32_display_finalize (GObject *object)
845 {
846 GdkWin32Display *display_win32 = GDK_WIN32_DISPLAY (object);
847
848 _gdk_win32_display_finalize_cursors (display_win32);
849 _gdk_win32_dnd_exit ();
850 _gdk_win32_lang_notification_exit ();
851
852 g_ptr_array_free (display_win32->monitors, TRUE);
853
854 G_OBJECT_CLASS (gdk_win32_display_parent_class)->finalize (object);
855 }
856
857 static void
858 _gdk_win32_enable_hidpi (GdkWin32Display *display)
859 {
860 gboolean check_for_dpi_awareness = FALSE;
861 gboolean have_hpi_disable_envvar = FALSE;
862
863 enum dpi_aware_status {
864 DPI_STATUS_PENDING,
865 DPI_STATUS_SUCCESS,
866 DPI_STATUS_DISABLED,
867 DPI_STATUS_FAILED
868 } status = DPI_STATUS_PENDING;
869
870 if (g_win32_check_windows_version (6, 3, 0, G_WIN32_OS_ANY))
871 {
872 /* If we are on Windows 8.1 or later, cache up functions from shcore.dll, by all means */
873 display->have_at_least_win81 = TRUE;
874 display->shcore_funcs.hshcore = LoadLibraryW (L"shcore.dll");
875
876 if (display->shcore_funcs.hshcore != NULL)
877 {
878 display->shcore_funcs.setDpiAwareFunc =
879 (funcSetProcessDpiAwareness) GetProcAddress (display->shcore_funcs.hshcore,
880 "SetProcessDpiAwareness");
881 display->shcore_funcs.getDpiAwareFunc =
882 (funcGetProcessDpiAwareness) GetProcAddress (display->shcore_funcs.hshcore,
883 "GetProcessDpiAwareness");
884
885 display->shcore_funcs.getDpiForMonitorFunc =
886 (funcGetDpiForMonitor) GetProcAddress (display->shcore_funcs.hshcore,
887 "GetDpiForMonitor");
888 }
889 }
890 else
891 {
892 /* Windows Vista through 8: use functions from user32.dll directly */
893 HMODULE user32;
894
895 display->have_at_least_win81 = FALSE;
896 user32 = GetModuleHandleW (L"user32.dll");
897
898 if (user32 != NULL)
899 {
900 display->user32_dpi_funcs.setDpiAwareFunc =
901 (funcSetProcessDPIAware) GetProcAddress (user32, "SetProcessDPIAware");
902 display->user32_dpi_funcs.isDpiAwareFunc =
903 (funcIsProcessDPIAware) GetProcAddress (user32, "IsProcessDPIAware");
904 }
905 }
906
907 if (g_getenv ("GDK_WIN32_DISABLE_HIDPI") == NULL)
908 {
909 /* For Windows 8.1 and later, use SetProcessDPIAwareness() */
910 if (display->have_at_least_win81)
911 {
912 /* then make the GDK-using app DPI-aware */
913 if (display->shcore_funcs.setDpiAwareFunc != NULL)
914 {
915 switch (display->shcore_funcs.setDpiAwareFunc (PROCESS_SYSTEM_DPI_AWARE))
916 {
917 case S_OK:
918 display->dpi_aware_type = PROCESS_SYSTEM_DPI_AWARE;
919 status = DPI_STATUS_SUCCESS;
920 break;
921 case E_ACCESSDENIED:
922 /* This means the app used a manifest to set DPI awareness, or a
923 DPI compatibility setting is used.
924 The manifest is the trump card in this game of bridge here. The
925 same applies if one uses the control panel or program properties to
926 force system DPI awareness */
927 check_for_dpi_awareness = TRUE;
928 break;
929 default:
930 display->dpi_aware_type = PROCESS_DPI_UNAWARE;
931 status = DPI_STATUS_FAILED;
932 break;
933 }
934 }
935 else
936 {
937 check_for_dpi_awareness = TRUE;
938 }
939 }
940 else
941 {
942 /* For Windows Vista through 8, use SetProcessDPIAware() */
943 display->have_at_least_win81 = FALSE;
944 if (display->user32_dpi_funcs.setDpiAwareFunc != NULL)
945 {
946 if (display->user32_dpi_funcs.setDpiAwareFunc () != 0)
947 {
948 display->dpi_aware_type = PROCESS_SYSTEM_DPI_AWARE;
949 status = DPI_STATUS_SUCCESS;
950 }
951 else
952 {
953 check_for_dpi_awareness = TRUE;
954 }
955 }
956 else
957 {
958 display->dpi_aware_type = PROCESS_DPI_UNAWARE;
959 status = DPI_STATUS_FAILED;
960 }
961 }
962 }
963 else
964 {
965 /* if GDK_WIN32_DISABLE_HIDPI is set, check for any DPI
966 * awareness settings done via manifests or user settings
967 */
968 check_for_dpi_awareness = TRUE;
969 have_hpi_disable_envvar = TRUE;
970 }
971
972 if (check_for_dpi_awareness)
973 {
974 if (display->have_at_least_win81)
975 {
976 if (display->shcore_funcs.getDpiAwareFunc != NULL)
977 {
978 display->shcore_funcs.getDpiAwareFunc (NULL, &display->dpi_aware_type);
979
980 if (display->dpi_aware_type != PROCESS_DPI_UNAWARE)
981 status = DPI_STATUS_SUCCESS;
982 else
983 /* This means the DPI awareness setting was forcefully disabled */
984 status = DPI_STATUS_DISABLED;
985 }
986 else
987 {
988 display->dpi_aware_type = PROCESS_DPI_UNAWARE;
989 status = DPI_STATUS_FAILED;
990 }
991 }
992 else
993 {
994 if (display->user32_dpi_funcs.isDpiAwareFunc != NULL)
995 {
996 /* This most probably means DPI awareness is set through
997 the manifest, or a DPI compatibility setting is used. */
998 display->dpi_aware_type = display->user32_dpi_funcs.isDpiAwareFunc () ?
999 PROCESS_SYSTEM_DPI_AWARE :
1000 PROCESS_DPI_UNAWARE;
1001
1002 if (display->dpi_aware_type == PROCESS_SYSTEM_DPI_AWARE)
1003 status = DPI_STATUS_SUCCESS;
1004 else
1005 status = DPI_STATUS_DISABLED;
1006 }
1007 else
1008 {
1009 display->dpi_aware_type = PROCESS_DPI_UNAWARE;
1010 status = DPI_STATUS_FAILED;
1011 }
1012 }
1013 if (have_hpi_disable_envvar &&
1014 status == DPI_STATUS_SUCCESS)
1015 {
1016 /* The user setting or application manifest trumps over GDK_WIN32_DISABLE_HIDPI */
1017 g_print ("Note: GDK_WIN32_DISABLE_HIDPI is ignored due to preset\n"
1018 " DPI awareness settings in user settings or application\n"
1019 " manifest, DPI awareness is still enabled.");
1020 }
1021 }
1022
1023 switch (status)
1024 {
1025 case DPI_STATUS_SUCCESS:
1026 GDK_NOTE (MISC, g_message ("HiDPI support enabled, type: %s",
1027 display->dpi_aware_type == PROCESS_PER_MONITOR_DPI_AWARE ? "per-monitor" : "system"));
1028 break;
1029 case DPI_STATUS_DISABLED:
1030 GDK_NOTE (MISC, g_message ("HiDPI support disabled via manifest"));
1031 break;
1032 case DPI_STATUS_FAILED:
1033 g_warning ("Failed to enable HiDPI support.");
1034 }
1035 }
1036
1037 gboolean
1038 _gdk_win32_check_processor (GdkWin32ProcessorCheckType check_type)
1039 {
1040 static gsize checked = 0;
1041 static gboolean is_arm64 = FALSE;
1042 static gboolean is_wow64 = FALSE;
1043
1044 if (g_once_init_enter (&checked))
1045 {
1046 gboolean fallback_wow64_check = FALSE;
1047 HMODULE kernel32 = LoadLibraryW (L"kernel32.dll");
1048
1049 if (kernel32 != NULL)
1050 {
1051 typedef BOOL (WINAPI *funcIsWow64Process2) (HANDLE, USHORT *, USHORT *);
1052
1053 funcIsWow64Process2 isWow64Process2 =
1054 (funcIsWow64Process2) GetProcAddress (kernel32, "IsWow64Process2");
1055
1056 if (isWow64Process2 != NULL)
1057 {
1058 USHORT proc_cpu = 0;
1059 USHORT native_cpu = 0;
1060
1061 isWow64Process2 (GetCurrentProcess (), &proc_cpu, &native_cpu);
1062
1063 if (native_cpu == IMAGE_FILE_MACHINE_ARM64)
1064 is_arm64 = TRUE;
1065
1066 if (proc_cpu != IMAGE_FILE_MACHINE_UNKNOWN)
1067 is_wow64 = TRUE;
1068 }
1069 else
1070 {
1071 fallback_wow64_check = TRUE;
1072 }
1073
1074 FreeLibrary (kernel32);
1075 }
1076 else
1077 {
1078 fallback_wow64_check = TRUE;
1079 }
1080
1081 if (fallback_wow64_check)
1082 IsWow64Process (GetCurrentProcess (), &is_wow64);
1083
1084 g_once_init_leave (&checked, 1);
1085 }
1086
1087 switch (check_type)
1088 {
1089 case GDK_WIN32_ARM64:
1090 return is_arm64;
1091 break;
1092
1093 case GDK_WIN32_WOW64:
1094 return is_wow64;
1095 break;
1096
1097 default:
1098 g_warning ("unknown CPU check type");
1099 return FALSE;
1100 break;
1101 }
1102 }
1103
1104 static void
1105 gdk_win32_display_init (GdkWin32Display *display)
1106 {
1107 const gchar *scale_str = g_getenv ("GDK_SCALE");
1108
1109 display->monitors = g_ptr_array_new_with_free_func (g_object_unref);
1110
1111 _gdk_win32_enable_hidpi (display);
1112
1113 /* if we have DPI awareness, set up fixed scale if set */
1114 if (display->dpi_aware_type != PROCESS_DPI_UNAWARE &&
1115 scale_str != NULL)
1116 {
1117 display->window_scale = atol (scale_str);
1118
1119 if (display->window_scale == 0)
1120 display->window_scale = 1;
1121
1122 display->has_fixed_scale = TRUE;
1123 }
1124 else
1125 display->window_scale = 1;
1126
1127 _gdk_win32_display_init_cursors (display);
1128 }
1129
1130 static void
1131 gdk_win32_display_before_process_all_updates (GdkDisplay *display)
1132 {
1133 /* nothing */
1134 }
1135 static void
1136 gdk_win32_display_after_process_all_updates (GdkDisplay *display)
1137 {
1138 /* nothing */
1139 }
1140
1141 static void
1142 gdk_win32_display_notify_startup_complete (GdkDisplay *display,
1143 const gchar *startup_id)
1144 {
1145 /* nothing */
1146 }
1147
1148 static void
1149 gdk_win32_display_push_error_trap (GdkDisplay *display)
1150 {
1151 /* nothing */
1152 }
1153
1154 static gint
1155 gdk_win32_display_pop_error_trap (GdkDisplay *display,
1156 gboolean ignored)
1157 {
1158 return 0;
1159 }
1160
1161 static int
1162 gdk_win32_display_get_n_monitors (GdkDisplay *display)
1163 {
1164 GdkWin32Display *win32_display = GDK_WIN32_DISPLAY (display);
1165
1166 return win32_display->monitors->len;
1167 }
1168
1169
1170 static GdkMonitor *
1171 gdk_win32_display_get_monitor (GdkDisplay *display,
1172 int monitor_num)
1173 {
1174 GdkWin32Display *win32_display = GDK_WIN32_DISPLAY (display);
1175
1176 if (monitor_num < 0 || monitor_num >= win32_display->monitors->len)
1177 return NULL;
1178
1179 return (GdkMonitor *) g_ptr_array_index (win32_display->monitors, monitor_num);
1180 }
1181
1182 static GdkMonitor *
1183 gdk_win32_display_get_primary_monitor (GdkDisplay *display)
1184 {
1185 GdkWin32Display *win32_display = GDK_WIN32_DISPLAY (display);
1186
1187 /* We arrange for the first monitor in the array to also be the primiary monitor */
1188 if (win32_display->monitors->len > 0)
1189 return (GdkMonitor *) g_ptr_array_index (win32_display->monitors, 0);
1190
1191 return NULL;
1192 }
1193
1194 guint
1195 _gdk_win32_display_get_monitor_scale_factor (GdkWin32Display *win32_display,
1196 HMONITOR hmonitor,
1197 HWND hwnd,
1198 gint *dpi)
1199 {
1200 gboolean is_scale_acquired = FALSE;
1201 gboolean use_dpi_for_monitor = FALSE;
1202 guint dpix, dpiy;
1203
1204 if (win32_display->have_at_least_win81)
1205 {
1206 if (hmonitor != NULL)
1207 use_dpi_for_monitor = TRUE;
1208
1209 else
1210 {
1211 if (hwnd != NULL)
1212 {
1213 hmonitor = MonitorFromWindow (hwnd, MONITOR_DEFAULTTONEAREST);
1214 use_dpi_for_monitor = TRUE;
1215 }
1216 }
1217 }
1218
1219 if (use_dpi_for_monitor)
1220 {
1221 /* Use GetDpiForMonitor() for Windows 8.1+, when we have a HMONITOR */
1222 if (win32_display->shcore_funcs.hshcore != NULL &&
1223 win32_display->shcore_funcs.getDpiForMonitorFunc != NULL)
1224 {
1225 if (win32_display->shcore_funcs.getDpiForMonitorFunc (hmonitor,
1226 MDT_EFFECTIVE_DPI,
1227 &dpix,
1228 &dpiy) == S_OK)
1229 {
1230 is_scale_acquired = TRUE;
1231 }
1232 }
1233 }
1234 else
1235 {
1236 /* Go back to GetDeviceCaps() for Windows 8 and earler, or when we don't
1237 * have a HMONITOR nor a HWND
1238 */
1239 HDC hdc = GetDC (hwnd);
1240
1241 /* in case we can't get the DC for the window, return 1 for the scale */
1242 if (hdc == NULL)
1243 {
1244 if (dpi != NULL)
1245 *dpi = USER_DEFAULT_SCREEN_DPI;
1246
1247 return 1;
1248 }
1249
1250 dpix = GetDeviceCaps (hdc, LOGPIXELSX);
1251 dpiy = GetDeviceCaps (hdc, LOGPIXELSY);
1252 ReleaseDC (hwnd, hdc);
1253
1254 is_scale_acquired = TRUE;
1255 }
1256
1257 if (is_scale_acquired)
1258 /* USER_DEFAULT_SCREEN_DPI = 96, in winuser.h */
1259 {
1260 if (dpi != NULL)
1261 *dpi = dpix;
1262
1263 if (win32_display->has_fixed_scale)
1264 return win32_display->window_scale;
1265 else
1266 return dpix / USER_DEFAULT_SCREEN_DPI > 1 ? dpix / USER_DEFAULT_SCREEN_DPI : 1;
1267 }
1268 else
1269 {
1270 if (dpi != NULL)
1271 *dpi = USER_DEFAULT_SCREEN_DPI;
1272
1273 return 1;
1274 }
1275 }
1276
1277 static void
1278 gdk_win32_display_class_init (GdkWin32DisplayClass *klass)
1279 {
1280 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1281 GdkDisplayClass *display_class = GDK_DISPLAY_CLASS (klass);
1282
1283 object_class->dispose = gdk_win32_display_dispose;
1284 object_class->finalize = gdk_win32_display_finalize;
1285
1286 display_class->window_type = GDK_TYPE_WIN32_WINDOW;
1287
1288 display_class->get_name = gdk_win32_display_get_name;
1289 display_class->get_default_screen = gdk_win32_display_get_default_screen;
1290 display_class->beep = gdk_win32_display_beep;
1291 display_class->sync = gdk_win32_display_sync;
1292 display_class->flush = gdk_win32_display_flush;
1293 display_class->has_pending = _gdk_win32_display_has_pending;
1294 display_class->queue_events = _gdk_win32_display_queue_events;
1295 display_class->get_default_group = gdk_win32_display_get_default_group;
1296
1297 display_class->supports_selection_notification = gdk_win32_display_supports_selection_notification;
1298 display_class->request_selection_notification = gdk_win32_display_request_selection_notification;
1299 display_class->supports_clipboard_persistence = gdk_win32_display_supports_clipboard_persistence;
1300 display_class->store_clipboard = gdk_win32_display_store_clipboard;
1301 display_class->supports_shapes = gdk_win32_display_supports_shapes;
1302 display_class->supports_input_shapes = gdk_win32_display_supports_input_shapes;
1303 display_class->supports_composite = gdk_win32_display_supports_composite;
1304
1305 //? display_class->get_app_launch_context = _gdk_win32_display_get_app_launch_context;
1306 display_class->get_cursor_for_type = _gdk_win32_display_get_cursor_for_type;
1307 display_class->get_cursor_for_name = _gdk_win32_display_get_cursor_for_name;
1308 display_class->get_cursor_for_surface = _gdk_win32_display_get_cursor_for_surface;
1309 display_class->get_default_cursor_size = _gdk_win32_display_get_default_cursor_size;
1310 display_class->get_maximal_cursor_size = _gdk_win32_display_get_maximal_cursor_size;
1311 display_class->supports_cursor_alpha = _gdk_win32_display_supports_cursor_alpha;
1312 display_class->supports_cursor_color = _gdk_win32_display_supports_cursor_color;
1313
1314 display_class->before_process_all_updates = gdk_win32_display_before_process_all_updates;
1315 display_class->after_process_all_updates = gdk_win32_display_after_process_all_updates;
1316 display_class->get_next_serial = gdk_win32_display_get_next_serial;
1317 display_class->notify_startup_complete = gdk_win32_display_notify_startup_complete;
1318 display_class->create_window_impl = _gdk_win32_display_create_window_impl;
1319
1320 display_class->get_keymap = _gdk_win32_display_get_keymap;
1321 display_class->push_error_trap = gdk_win32_display_push_error_trap;
1322 display_class->pop_error_trap = gdk_win32_display_pop_error_trap;
1323 display_class->get_selection_owner = _gdk_win32_display_get_selection_owner;
1324 display_class->set_selection_owner = _gdk_win32_display_set_selection_owner;
1325 display_class->send_selection_notify = _gdk_win32_display_send_selection_notify;
1326 display_class->get_selection_property = _gdk_win32_display_get_selection_property;
1327 display_class->convert_selection = _gdk_win32_display_convert_selection;
1328 display_class->text_property_to_utf8_list = _gdk_win32_display_text_property_to_utf8_list;
1329 display_class->utf8_to_string_target = _gdk_win32_display_utf8_to_string_target;
1330 display_class->is_gl_context_current = gdk_win32_display_is_gl_context_current;
1331 display_class->make_gl_context_current = gdk_win32_display_make_gl_context_current;
1332
1333 display_class->get_n_monitors = gdk_win32_display_get_n_monitors;
1334 display_class->get_monitor = gdk_win32_display_get_monitor;
1335 display_class->get_primary_monitor = gdk_win32_display_get_primary_monitor;
1336
1337 _gdk_win32_windowing_init ();
1338 }