Debian Codegraph @

gdk/win32/gdkmonitor-win32.c

raw symbols SCIP
1 /*
2 * Copyright © 2016 Red Hat, Inc
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "config.h"
19
20 #include "gdkprivate-win32.h"
21 #include "gdkdisplay-win32.h"
22 #include "gdkmonitor-win32.h"
23
24 #include <glib.h>
25 #include <gio/gio.h>
26
27 #include <cfgmgr32.h>
28 #include <devpropdef.h>
29 #include <setupapi.h>
30
31 #include "gdkprivate-win32.h"
32
33 G_DEFINE_TYPE (GdkWin32Monitor, gdk_win32_monitor, GDK_TYPE_MONITOR)
34
35 /* MinGW-w64 carelessly put DISPLAYCONFIG_OUTPUT_TECHNOLOGY_OTHER = -1 into this
36 * enum, as documented by MSDN. However, with
37 * DISPLAYCONFIG_OUTPUT_TECHNOLOGY_INTERNAL = 0x80000000 and
38 * DISPLAYCONFIG_OUTPUT_TECHNOLOGY_FORCE_UINT32 = 0xFFFFFFFF
39 * this had the effect of increasing enum size from 4 to 8 bytes,
40 * when compiled by GCC (MSVC doesn't have this problem), breaking ABI.
41 * At the moment of writing MinGW-w64 headers are still broken.
42 * When they are fixed, replace 9999 with actual version numbers.
43 * The fix below is not necessarily correct, but it works.
44 */
45 #if SIZEOF_DISPLAYCONFIG_VIDEO_OUTPUT_TECHNOLOGY == 4
46 # define fixedDISPLAYCONFIG_PATH_INFO DISPLAYCONFIG_PATH_INFO
47 # define fixedDISPLAYCONFIG_TARGET_DEVICE_NAME DISPLAYCONFIG_TARGET_DEVICE_NAME
48 #else
49 typedef enum {
50 fixedDISPLAYCONFIG_OUTPUT_TECHNOLOGY_OTHER = (int) -1,
51 fixedDISPLAYCONFIG_OUTPUT_TECHNOLOGY_HD15 = (int) 0,
52 fixedDISPLAYCONFIG_OUTPUT_TECHNOLOGY_SVIDEO = (int) 1,
53 fixedDISPLAYCONFIG_OUTPUT_TECHNOLOGY_COMPOSITE_VIDEO = (int) 2,
54 fixedDISPLAYCONFIG_OUTPUT_TECHNOLOGY_COMPONENT_VIDEO = (int) 3,
55 fixedDISPLAYCONFIG_OUTPUT_TECHNOLOGY_DVI = (int) 4,
56 fixedDISPLAYCONFIG_OUTPUT_TECHNOLOGY_HDMI = (int) 5,
57 fixedDISPLAYCONFIG_OUTPUT_TECHNOLOGY_LVDS = (int) 6,
58 fixedDISPLAYCONFIG_OUTPUT_TECHNOLOGY_D_JPN = (int) 8,
59 fixedDISPLAYCONFIG_OUTPUT_TECHNOLOGY_SDI = (int) 9,
60 fixedDISPLAYCONFIG_OUTPUT_TECHNOLOGY_DISPLAYPORT_EXTERNAL = (int) 10,
61 fixedDISPLAYCONFIG_OUTPUT_TECHNOLOGY_DISPLAYPORT_EMBEDDED = (int) 11,
62 fixedDISPLAYCONFIG_OUTPUT_TECHNOLOGY_UDI_EXTERNAL = (int) 12,
63 fixedDISPLAYCONFIG_OUTPUT_TECHNOLOGY_UDI_EMBEDDED = (int) 13,
64 fixedDISPLAYCONFIG_OUTPUT_TECHNOLOGY_SDTVDONGLE = (int) 14,
65 fixedDISPLAYCONFIG_OUTPUT_TECHNOLOGY_INTERNAL = (int) 0x80000000,
66 fixedDISPLAYCONFIG_OUTPUT_TECHNOLOGY_FORCE_UINT32 = (int) 0xFFFFFFFF
67 } fixedDISPLAYCONFIG_VIDEO_OUTPUT_TECHNOLOGY;
68
69 typedef struct fixedDISPLAYCONFIG_PATH_TARGET_INFO {
70 LUID adapterId;
71 UINT32 id;
72 UINT32 modeInfoIdx;
73 fixedDISPLAYCONFIG_VIDEO_OUTPUT_TECHNOLOGY outputTechnology;
74 DISPLAYCONFIG_ROTATION rotation;
75 DISPLAYCONFIG_SCALING scaling;
76 DISPLAYCONFIG_RATIONAL refreshRate;
77 DISPLAYCONFIG_SCANLINE_ORDERING scanLineOrdering;
78 BOOL targetAvailable;
79 UINT32 statusFlags;
80 } fixedDISPLAYCONFIG_PATH_TARGET_INFO;
81
82 typedef struct fixedDISPLAYCONFIG_PATH_INFO
83 {
84 DISPLAYCONFIG_PATH_SOURCE_INFO sourceInfo;
85 fixedDISPLAYCONFIG_PATH_TARGET_INFO targetInfo;
86 UINT32 flags;
87 } fixedDISPLAYCONFIG_PATH_INFO;
88
89 typedef struct fixedDISPLAYCONFIG_TARGET_DEVICE_NAME
90 {
91 DISPLAYCONFIG_DEVICE_INFO_HEADER header;
92 DISPLAYCONFIG_TARGET_DEVICE_NAME_FLAGS flags;
93 fixedDISPLAYCONFIG_VIDEO_OUTPUT_TECHNOLOGY outputTechnology;
94 UINT16 edidManufactureId;
95 UINT16 edidProductCodeId;
96 UINT32 connectorInstance;
97 WCHAR monitorFriendlyDeviceName[64];
98 WCHAR monitorDevicePath[128];
99 } fixedDISPLAYCONFIG_TARGET_DEVICE_NAME;
100
101 #endif
102
103
104 /* MinGW-w64 does not have these functions in its import libraries
105 * at the moment of writing.
106 * Also, Windows Vista doesn't have these functions at all
107 * (according to MSDN it does, but that is a lie), so we'd have
108 * to load them manually anyway (otherwise GTK apps won't even start
109 * on Vista).
110 */
111 typedef LONG
112 (WINAPI *funcGetDisplayConfigBufferSizes) (UINT32 flags,
113 UINT32* numPathArrayElements,
114 UINT32* numModeInfoArrayElements);
115
116 typedef LONG
117 (WINAPI *funcQueryDisplayConfig) (UINT32 flags,
118 UINT32* numPathArrayElements,
119 fixedDISPLAYCONFIG_PATH_INFO* pathArray,
120 UINT32* numModeInfoArrayElements,
121 DISPLAYCONFIG_MODE_INFO* modeInfoArray,
122 DISPLAYCONFIG_TOPOLOGY_ID* currentTopologyId);
123
124 typedef LONG
125 (WINAPI *funcDisplayConfigGetDeviceInfo) (DISPLAYCONFIG_DEVICE_INFO_HEADER* requestPacket);
126
127 #define G_GUID_FORMAT "%08lX-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X"
128 #define g_format_guid(guid) (guid)->Data1, \
129 (guid)->Data2, \
130 (guid)->Data3, \
131 (guid)->Data4[0], \
132 (guid)->Data4[1], \
133 (guid)->Data4[2], \
134 (guid)->Data4[3], \
135 (guid)->Data4[4], \
136 (guid)->Data4[5], \
137 (guid)->Data4[6], \
138 (guid)->Data4[7]
139
140 static gboolean
141 get_device_property (HDEVINFO device_infoset,
142 SP_DEVINFO_DATA *device_info_data,
143 DEVPROPKEY *property_key,
144 gpointer *r_buffer,
145 gsize *r_buffer_size,
146 DEVPROPTYPE *r_property_type,
147 GError **error)
148 {
149 DEVPROPTYPE property_type;
150 gpointer property;
151 DWORD property_size;
152
153 property = NULL;
154 property_size = 0;
155
156 if (!SetupDiGetDevicePropertyW (device_infoset,
157 device_info_data,
158 property_key,
159 &property_type,
160 property,
161 property_size,
162 &property_size,
163 0))
164 {
165 DWORD error_code = GetLastError ();
166
167 if (error_code != ERROR_INSUFFICIENT_BUFFER)
168 {
169 char *emsg = g_win32_error_message (error_code);
170 g_warning ("Failed to get device node property {" G_GUID_FORMAT "},%lu size: %s",
171 g_format_guid (&property_key->fmtid),
172 property_key->pid,
173 emsg);
174 g_free (emsg);
175
176 return FALSE;
177 }
178 }
179
180 if (r_buffer)
181 {
182 property = g_malloc (property_size);
183
184 if (!SetupDiGetDevicePropertyW (device_infoset,
185 device_info_data,
186 property_key,
187 &property_type,
188 property,
189 property_size,
190 &property_size,
191 0))
192 {
193 DWORD error_code = GetLastError ();
194
195 char *emsg = g_win32_error_message (error_code);
196 g_warning ("Failed to get device node property {" G_GUID_FORMAT "},%lu: %s",
197 g_format_guid (&property_key->fmtid),
198 property_key->pid,
199 emsg);
200 g_free (emsg);
201
202 return FALSE;
203 }
204
205 *r_buffer = property;
206 }
207
208 if (r_buffer_size)
209 *r_buffer_size = property_size;
210
211 if (r_property_type)
212 *r_property_type = property_type;
213
214 return TRUE;
215 }
216
217 static GPtrArray *
218 get_monitor_devices (GdkWin32Display *win32_display)
219 {
220 GPtrArray *monitor_array;
221 HDEVINFO device_infoset;
222 SP_DEVINFO_DATA device_info_data;
223 DWORD device_index;
224 GUID device_interface_monitor = {0xe6f07b5f, 0xee97, 0x4a90, {0xb0, 0x76, 0x33, 0xf5, 0x7b, 0xf4, 0xea, 0xa7}};
225 DEVPROPKEY pkey_device_instance_id = {{0x78C34FC8, 0x104A, 0x4ACA, {0x9E, 0xA4, 0x52, 0x4D, 0x52, 0x99, 0x6E, 0x57}}, 256};
226 DEVPROPKEY pkey_manufacturer = {{0xA45C254E, 0xDF1C, 0x4EFD, {0x80, 0x20, 0x67, 0xD1, 0x46, 0xA8, 0x50, 0xE0}}, 13};
227 DEVPROPKEY pkey_display_name = {{0xB725F130, 0x47EF, 0x101A, {0xA5, 0xF1, 0x02, 0x60, 0x8C, 0x9E, 0xEB, 0xAC}}, 10};
228
229 monitor_array = g_ptr_array_new_with_free_func (g_object_unref);
230
231 device_infoset = SetupDiGetClassDevs (&device_interface_monitor, 0, 0, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT);
232
233 if (device_infoset == INVALID_HANDLE_VALUE)
234 return monitor_array;
235
236 for (device_index = 0; TRUE; device_index++)
237 {
238 gunichar2 *p;
239 char *instance_path;
240 gunichar2 *prop;
241 DWORD proptype;
242 HKEY device_registry_key;
243 GdkWin32Monitor *w32mon;
244 GdkMonitor *mon;
245 unsigned char *edid;
246 DWORD edid_size;
247 DWORD edid_type;
248
249 memset (&device_info_data, 0, sizeof (device_info_data));
250 device_info_data.cbSize = sizeof (device_info_data);
251
252 if (!SetupDiEnumDeviceInfo (device_infoset, device_index, &device_info_data))
253 {
254 DWORD error_code = GetLastError ();
255
256 if (error_code == ERROR_NO_MORE_ITEMS)
257 break;
258
259 g_warning ("SetupDiEnumDeviceInfo() failed: %lu\n", error_code);
260 break;
261 }
262
263 if (!get_device_property (device_infoset,
264 &device_info_data,
265 &pkey_device_instance_id,
266 (gpointer *) &prop,
267 NULL,
268 &proptype,
269 NULL))
270 continue;
271
272 if (proptype != DEVPROP_TYPE_STRING)
273 {
274 g_free (prop);
275 continue;
276 }
277
278 w32mon = g_object_new (GDK_TYPE_WIN32_MONITOR, "display", win32_display, NULL);
279 mon = GDK_MONITOR (w32mon);
280
281 g_ptr_array_add (monitor_array, w32mon);
282
283 /* Half-initialized monitors are candidates for removal */
284 w32mon->remove = TRUE;
285
286 /* device instance ID looks like: DISPLAY\FOO\X&XXXXXXX&X&UIDXXX */
287 for (p = prop; p[0]; p++)
288 if (p[0] == L'\\')
289 p[0] = L'#';
290 /* now device instance ID looks like: DISPLAY#FOO#X&XXXXXXX&X&UIDXXX */
291
292 /* instance path looks like: \\?\DISPLAY#FOO#X&XXXXXXX&X&UIDXXX#{e6f07b5f-ee97-4a90-b076-33f57bf4eaa7} */
293 instance_path = g_strdup_printf ("\\\\?\\%ls#{" G_GUID_FORMAT "}",
294 prop,
295 g_format_guid (&device_interface_monitor));
296 w32mon->instance_path = g_utf8_strdown (instance_path, -1);
297 g_free (instance_path);
298 g_free (prop);
299
300 if (get_device_property (device_infoset,
301 &device_info_data,
302 &pkey_manufacturer,
303 (gpointer *) &prop,
304 NULL, &proptype, NULL))
305 {
306 if (proptype == DEVPROP_TYPE_STRING)
307 {
308 char *manufacturer = g_utf16_to_utf8 (prop, -1, NULL, NULL, NULL);
309 gdk_monitor_set_manufacturer (mon, manufacturer);
310 g_free (manufacturer);
311 }
312
313 g_free (prop);
314 }
315
316 if (get_device_property (device_infoset,
317 &device_info_data,
318 &pkey_display_name,
319 (gpointer *) &prop,
320 NULL, &proptype, NULL))
321 {
322 if (proptype == DEVPROP_TYPE_STRING)
323 {
324 char *name = g_utf16_to_utf8 (prop, -1, NULL, NULL, NULL);
325 gdk_monitor_set_model (mon, name);
326 g_free (name);
327 }
328
329 g_free (prop);
330 }
331
332 device_registry_key = SetupDiOpenDevRegKey (device_infoset, &device_info_data, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_READ);
333
334 if (device_registry_key == NULL || device_registry_key == INVALID_HANDLE_VALUE)
335 continue;
336
337 edid = NULL;
338 edid_size = 0;
339
340
341 if (RegQueryValueExW (device_registry_key, L"EDID",
342 NULL, &edid_type,
343 edid, &edid_size) == ERROR_SUCCESS)
344 {
345 edid = g_malloc (edid_size);
346
347 if (RegQueryValueExW (device_registry_key, L"EDID",
348 NULL, &edid_type,
349 edid, &edid_size) == ERROR_SUCCESS)
350 {
351 gdk_monitor_set_physical_size (mon,
352 ((edid[68] & 0x00F0) << 4) + edid[66],
353 ((edid[68] & 0x000F) << 8) + edid[67]);
354 }
355
356 g_free (edid);
357 }
358
359 RegCloseKey (device_registry_key);
360 }
361
362 SetupDiDestroyDeviceInfoList (device_infoset);
363
364 return monitor_array;
365 }
366
367 static void
368 populate_monitor_devices_from_display_config (GPtrArray *monitors)
369 {
370 HMODULE user32;
371 LONG return_code;
372 funcGetDisplayConfigBufferSizes getDisplayConfigBufferSizes;
373 funcQueryDisplayConfig queryDisplayConfig;
374 funcDisplayConfigGetDeviceInfo displayConfigGetDeviceInfo;
375 UINT32 dispconf_mode_count;
376 UINT32 dispconf_path_count;
377 fixedDISPLAYCONFIG_PATH_INFO *dispconf_paths;
378 DISPLAYCONFIG_MODE_INFO *dispconf_modes;
379 int path_index;
380
381 user32 = LoadLibraryA ("user32.dll");
382
383 if (user32 == NULL)
384 return;
385
386 getDisplayConfigBufferSizes = (funcGetDisplayConfigBufferSizes) GetProcAddress (user32,
387 "GetDisplayConfigBufferSizes");
388 queryDisplayConfig = (funcQueryDisplayConfig) GetProcAddress (user32,
389 "QueryDisplayConfig");
390 displayConfigGetDeviceInfo = (funcDisplayConfigGetDeviceInfo) GetProcAddress (user32,
391 "DisplayConfigGetDeviceInfo");
392 if (getDisplayConfigBufferSizes == NULL ||
393 queryDisplayConfig == NULL ||
394 displayConfigGetDeviceInfo == NULL)
395 {
396 /* This does happen on Windows Vista, so don't warn about this */
397 FreeLibrary (user32);
398
399 return;
400 }
401
402 return_code = getDisplayConfigBufferSizes (QDC_ONLY_ACTIVE_PATHS,
403 &dispconf_path_count,
404 &dispconf_mode_count);
405
406 if (return_code != ERROR_SUCCESS)
407 {
408 g_warning ("Can't get displayconfig buffer size: 0x%lx\n", return_code);
409 FreeLibrary (user32);
410
411 return;
412 }
413
414 dispconf_paths = g_new (fixedDISPLAYCONFIG_PATH_INFO, dispconf_path_count);
415 dispconf_modes = g_new (DISPLAYCONFIG_MODE_INFO, dispconf_mode_count);
416
417 return_code = queryDisplayConfig (QDC_ONLY_ACTIVE_PATHS,
418 &dispconf_path_count,
419 dispconf_paths,
420 &dispconf_mode_count,
421 dispconf_modes,
422 NULL);
423
424 if (return_code != ERROR_SUCCESS)
425 {
426 g_free (dispconf_paths);
427 g_free (dispconf_modes);
428 FreeLibrary (user32);
429
430 return;
431 }
432
433 for (path_index = 0; path_index < dispconf_path_count; path_index++)
434 {
435 fixedDISPLAYCONFIG_TARGET_DEVICE_NAME tdn;
436 int i;
437 GdkWin32Monitor *w32mon;
438 GdkMonitor *mon;
439 char *path, *path_lower;
440 DISPLAYCONFIG_RATIONAL *refresh;
441
442 tdn.header.type = DISPLAYCONFIG_DEVICE_INFO_GET_TARGET_NAME;
443 tdn.header.size = sizeof (tdn);
444 tdn.header.adapterId = dispconf_paths[path_index].targetInfo.adapterId;
445 tdn.header.id = dispconf_paths[path_index].targetInfo.id;
446
447 return_code = displayConfigGetDeviceInfo (&tdn.header);
448
449 if (return_code != ERROR_SUCCESS)
450 continue;
451
452 path = g_utf16_to_utf8 (tdn.monitorDevicePath, -1, NULL, NULL, NULL);
453
454 if (path == NULL)
455 continue;
456
457 path_lower = g_utf8_strdown (path, -1);
458
459 g_free (path);
460
461 for (i = 0, w32mon = NULL; i < monitors->len; i++)
462 {
463 GdkWin32Monitor *m = g_ptr_array_index (monitors, i);
464
465 if (g_strcmp0 (m->instance_path, path_lower) != 0)
466 continue;
467
468 w32mon = m;
469 break;
470 }
471
472 g_free (path_lower);
473
474 if (w32mon == NULL)
475 continue;
476
477 if ((dispconf_paths[path_index].flags & DISPLAYCONFIG_PATH_ACTIVE) == 0)
478 {
479 w32mon->remove = TRUE;
480 continue;
481 }
482
483 mon = GDK_MONITOR (w32mon);
484
485 if (!tdn.flags.friendlyNameForced)
486 {
487 /* monitorFriendlyDeviceName is usually nicer */
488 char *name = g_utf16_to_utf8 (tdn.monitorFriendlyDeviceName, -1, NULL, NULL, NULL);
489 gdk_monitor_set_model (mon, name);
490 g_free (name);
491 }
492
493 refresh = &dispconf_paths[path_index].targetInfo.refreshRate;
494 gdk_monitor_set_refresh_rate (mon,
495 refresh->Numerator * (UINT64) 1000 / refresh->Denominator);
496 }
497
498 g_free (dispconf_paths);
499 g_free (dispconf_modes);
500
501 FreeLibrary (user32);
502 }
503
504 typedef struct {
505 GPtrArray *monitors;
506 gboolean have_monitor_devices;
507 GdkWin32Display *display;
508 } EnumMonitorData;
509
510 static BOOL CALLBACK
511 enum_monitor (HMONITOR hmonitor,
512 HDC hdc,
513 LPRECT unused,
514 LPARAM param)
515 {
516 EnumMonitorData *data = (EnumMonitorData *) param;
517 MONITORINFOEXW monitor_info;
518 DWORD i_adapter;
519
520 /* Grab monitor_info for this logical monitor */
521 monitor_info.cbSize = sizeof (MONITORINFOEXW);
522 GetMonitorInfoW (hmonitor, (MONITORINFO *) &monitor_info);
523
524 /* Sidestep to enumerate display adapters */
525 for (i_adapter = 0; TRUE; i_adapter++)
526 {
527 DISPLAY_DEVICEW dd;
528 DEVMODEW dm;
529 DWORD i_monitor;
530 DWORD frequency;
531
532 memset (&dd, 0, sizeof (dd));
533 dd.cb = sizeof (dd);
534
535 /* Get i_adapter'th adapter */
536 if (!EnumDisplayDevicesW (NULL, i_adapter, &dd, EDD_GET_DEVICE_INTERFACE_NAME))
537 break;
538
539 if ((dd.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP) == 0)
540 continue;
541
542 /* Match this display adapter to one for which we've got monitor_info
543 * (logical monitor == adapter)
544 */
545 if (wcscmp (dd.DeviceName, monitor_info.szDevice) != 0)
546 continue;
547
548 dm.dmSize = sizeof (dm);
549
550 /* Grab refresh rate for this adapter while we're at it */
551 if (EnumDisplaySettingsW (dd.DeviceName, ENUM_CURRENT_SETTINGS, &dm))
552 frequency = dm.dmDisplayFrequency;
553 else
554 frequency = 0;
555
556 /* Enumerate monitors connected to this display adapter */
557 for (i_monitor = 0; TRUE; i_monitor++)
558 {
559 DISPLAY_DEVICEW dd_monitor;
560 char *device_id_lower, *tmp;
561 DWORD i;
562 GdkWin32Monitor *w32mon;
563 GdkMonitor *mon;
564 GdkRectangle rect;
565 HMONITOR scale_hmonitor;
566 POINT pt;
567 int scale;
568
569 memset (&dd_monitor, 0, sizeof (dd_monitor));
570 dd_monitor.cb = sizeof (dd_monitor);
571
572 if (data->have_monitor_devices)
573 {
574 /* Get i_monitor'th monitor */
575 if (!EnumDisplayDevicesW (dd.DeviceName, i_monitor, &dd_monitor, EDD_GET_DEVICE_INTERFACE_NAME))
576 break;
577
578 tmp = g_utf16_to_utf8 (dd_monitor.DeviceID, -1, NULL, NULL, NULL);
579
580 if (tmp == NULL)
581 continue;
582
583 device_id_lower = g_utf8_strdown (tmp, -1);
584 g_free (tmp);
585
586 /* Match this monitor to one of the monitor devices we found earlier */
587 for (i = 0, w32mon = NULL; i < data->monitors->len; i++)
588 {
589 GdkWin32Monitor *m = g_ptr_array_index (data->monitors, i);
590
591 if (g_strcmp0 (device_id_lower, m->instance_path) != 0)
592 continue;
593
594 w32mon = m;
595 break;
596 }
597
598 g_free (device_id_lower);
599
600 if (w32mon == NULL)
601 continue;
602
603 w32mon->hmonitor = hmonitor;
604 }
605 else
606 {
607 /* Headless PC or a virtual machine, it has no monitor devices.
608 * Make one up.
609 */
610 w32mon = g_object_new (GDK_TYPE_WIN32_MONITOR, "display", data->display, NULL);
611 g_ptr_array_add (data->monitors, w32mon);
612 i = data->monitors->len - 1;
613 w32mon->madeup = TRUE;
614 }
615
616 mon = GDK_MONITOR (w32mon);
617
618 if (gdk_monitor_get_model (mon) == NULL)
619 {
620
621 char *name = NULL;
622
623 /* Only use dd.DeviceName as a last resort, as it is just
624 * \\.\DISPLAYX\MonitorY (for some values of X and Y).
625 */
626 if (dd_monitor.DeviceName[0] != L'\0')
627 name = g_utf16_to_utf8 (dd_monitor.DeviceName, -1, NULL, NULL, NULL);
628 else if (dd.DeviceName[0] != L'\0')
629 name = g_utf16_to_utf8 (dd.DeviceName, -1, NULL, NULL, NULL);
630
631 if (name != NULL)
632 gdk_monitor_set_model (mon, name);
633
634 g_free (name);
635 }
636
637 /* GetDeviceCaps seems to provide a wild guess, prefer more precise EDID info */
638 if (gdk_monitor_get_width_mm (mon) == 0 &&
639 gdk_monitor_get_height_mm (mon) == 0)
640 {
641 HDC hDC = CreateDCW (L"DISPLAY", monitor_info.szDevice, NULL, NULL);
642
643 gdk_monitor_set_physical_size (mon,
644 GetDeviceCaps (hDC, HORZSIZE),
645 GetDeviceCaps (hDC, VERTSIZE));
646 DeleteDC (hDC);
647 }
648
649 /* frequency is in Hz and is unsigned long,
650 * prefer more precise refresh_rate found earlier,
651 * which comes as a Numerator & Denominator pair and is more precise.
652 */
653 if (gdk_monitor_get_refresh_rate (mon) == 0)
654 gdk_monitor_set_refresh_rate (mon, frequency * 1000);
655
656 /* This is the reason this function exists. This data is not available
657 * via other functions.
658 */
659 rect.x = monitor_info.rcWork.left;
660 rect.y = monitor_info.rcWork.top;
661 rect.width = (monitor_info.rcWork.right - monitor_info.rcWork.left);
662 rect.height = (monitor_info.rcWork.bottom - monitor_info.rcWork.top);
663 /* This is temporary, scale will be applied below */
664 w32mon->work_rect = rect;
665
666 /* First acquire the scale using the current screen */
667 scale = gdk_win32_display_get_monitor_scale_factor (data->display, NULL, NULL);
668
669 /* acquire the scale using the monitor which the window is nearest */
670 pt.x = w32mon->work_rect.x + w32mon->work_rect.width / 2;
671 pt.y = w32mon->work_rect.y + w32mon->work_rect.height / 2;
672 scale_hmonitor = MonitorFromPoint (pt, MONITOR_DEFAULTTONEAREST);
673 scale = gdk_win32_display_get_monitor_scale_factor (data->display, NULL, scale_hmonitor);
674
675 gdk_monitor_set_scale_factor (mon, scale);
676 /* Now apply the scale to the work rectangle */
677 w32mon->work_rect.x /= scale;
678 w32mon->work_rect.y /= scale;
679 w32mon->work_rect.width /= scale;
680 w32mon->work_rect.height /= scale;
681
682 rect.x = monitor_info.rcMonitor.left / scale;
683 rect.y = monitor_info.rcMonitor.top / scale;
684 rect.width = (monitor_info.rcMonitor.right - monitor_info.rcMonitor.left) / scale;
685 rect.height = (monitor_info.rcMonitor.bottom - monitor_info.rcMonitor.top) / scale;
686 gdk_monitor_set_geometry (mon, &rect);
687
688 if (monitor_info.dwFlags & MONITORINFOF_PRIMARY && i != 0)
689 {
690 /* Put primary monitor at index 0, just in case somebody needs
691 * to know which one is the primary.
692 */
693 GdkWin32Monitor *temp = g_ptr_array_index (data->monitors, 0);
694 g_ptr_array_index (data->monitors, 0) = w32mon;
695 g_ptr_array_index (data->monitors, i) = temp;
696 }
697
698 /* Work area is the most important component, actively used by GTK,
699 * but our initial list of monitor devices did not have it.
700 * Any monitor devices not matched in this functions will have
701 * 0-filled work area and will therefore be useless, so let them
702 * keep remove == TRUE and be removed further up the stack.
703 */
704 w32mon->remove = FALSE;
705
706 /* One virtual monitor per display adapter */
707 if (w32mon->madeup)
708 break;
709 }
710 }
711
712 return TRUE;
713 }
714
715 static void
716 prune_monitors (EnumMonitorData *data)
717 {
718 int i;
719
720 for (i = 0; i < data->monitors->len; i++)
721 {
722 GdkWin32Monitor *m;
723
724 m = g_ptr_array_index (data->monitors, i);
725
726 if (m->remove)
727 g_ptr_array_remove_index (data->monitors, i--);
728 }
729 }
730
731 GPtrArray *
732 _gdk_win32_display_get_monitor_list (GdkWin32Display *win32_display)
733 {
734 EnumMonitorData data;
735
736 data.display = win32_display;
737 data.monitors = get_monitor_devices (win32_display);
738
739 if (data.monitors->len != 0)
740 {
741 populate_monitor_devices_from_display_config (data.monitors);
742 data.have_monitor_devices = TRUE;
743 }
744 else
745 {
746 data.have_monitor_devices = FALSE;
747 }
748
749 EnumDisplayMonitors (NULL, NULL, enum_monitor, (LPARAM) &data);
750
751 prune_monitors (&data);
752
753 if (data.monitors->len == 0 && data.have_monitor_devices)
754 {
755 /* We thought we had monitors, but enumeration eventually failed, and
756 * we have none. Try again, this time making stuff up as we go.
757 */
758 data.have_monitor_devices = FALSE;
759 EnumDisplayMonitors (NULL, NULL, enum_monitor, (LPARAM) &data);
760 prune_monitors (&data);
761 }
762
763 return data.monitors;
764 }
765
766 static void
767 gdk_win32_monitor_finalize (GObject *object)
768 {
769 GdkWin32Monitor *win32_monitor = GDK_WIN32_MONITOR (object);
770
771 g_free (win32_monitor->instance_path);
772
773 G_OBJECT_CLASS (gdk_win32_monitor_parent_class)->finalize (object);
774 }
775
776 int
777 _gdk_win32_monitor_compare (GdkWin32Monitor *a,
778 GdkWin32Monitor *b)
779 {
780 if (a->instance_path != NULL &&
781 b->instance_path != NULL)
782 return g_strcmp0 (a->instance_path, b->instance_path);
783
784 return a == b ? 0 : a < b ? -1 : 1;
785 }
786
787 /**
788 * gdk_win32_monitor_get_workarea:
789 * @monitor: a `GdkMonitor`
790 * @workarea: (out): a `GdkRectangle` to be filled with the monitor workarea
791 *
792 * Retrieves the size and position of the “work area” on a monitor
793 * within the display coordinate space.
794 *
795 * The returned geometry is in ”application pixels”, not in ”device pixels”
796 * (see [[email protected]_scale_factor]).
797 */
798 void
799 gdk_win32_monitor_get_workarea (GdkMonitor *monitor,
800 GdkRectangle *dest)
801 {
802 GdkWin32Monitor *win32_monitor = GDK_WIN32_MONITOR (monitor);
803
804 *dest = win32_monitor->work_rect;
805 }
806
807 static void
808 gdk_win32_monitor_init (GdkWin32Monitor *monitor)
809 {
810 }
811
812 static void
813 gdk_win32_monitor_class_init (GdkWin32MonitorClass *class)
814 {
815 G_OBJECT_CLASS (class)->finalize = gdk_win32_monitor_finalize;
816 }