2 * X11DRV display device functions
4 * Copyright 2019 Zhiyi Zhang for CodeWeavers
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.1 of the License, or (at your option) any later version.
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.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(x11drv
);
31 static struct x11drv_display_device_handler host_handler
;
32 struct x11drv_display_device_handler desktop_handler
;
33 static struct x11drv_settings_handler settings_handler
;
35 #define NEXT_DEVMODEW(mode) ((DEVMODEW *)((char *)((mode) + 1) + (mode)->dmDriverExtra))
37 struct x11drv_display_depth
40 x11drv_settings_id display_id
;
44 /* Display device emulated depth list, protected by modes_section */
45 static struct list x11drv_display_depth_list
= LIST_INIT(x11drv_display_depth_list
);
47 /* All Windows drivers seen so far either support 32 bit depths, or 24 bit depths, but never both. So if we have
48 * a 32 bit framebuffer, report 32 bit bpps, otherwise 24 bit ones.
50 static const unsigned int depths_24
[] = {8, 16, 24};
51 static const unsigned int depths_32
[] = {8, 16, 32};
52 const unsigned int *depths
;
54 static pthread_mutex_t settings_mutex
= PTHREAD_MUTEX_INITIALIZER
;
56 void X11DRV_Settings_SetHandler(const struct x11drv_settings_handler
*new_handler
)
58 if (new_handler
->priority
> settings_handler
.priority
)
60 settings_handler
= *new_handler
;
61 TRACE("Display settings are now handled by: %s.\n", settings_handler
.name
);
65 /***********************************************************************
66 * Default handlers if resolution switching is not enabled
69 static BOOL
nores_get_id(const WCHAR
*device_name
, BOOL is_primary
, x11drv_settings_id
*id
)
71 id
->id
= is_primary
? 1 : 0;
75 static BOOL
nores_get_modes(x11drv_settings_id id
, DWORD flags
, DEVMODEW
**new_modes
, UINT
*mode_count
)
77 RECT primary
= get_host_primary_monitor_rect();
80 modes
= calloc(1, sizeof(*modes
));
83 RtlSetLastWin32Error( ERROR_NOT_ENOUGH_MEMORY
);
87 modes
[0].dmSize
= sizeof(*modes
);
88 modes
[0].dmDriverExtra
= 0;
89 modes
[0].dmFields
= DM_DISPLAYORIENTATION
| DM_BITSPERPEL
| DM_PELSWIDTH
| DM_PELSHEIGHT
|
90 DM_DISPLAYFLAGS
| DM_DISPLAYFREQUENCY
;
91 modes
[0].dmDisplayOrientation
= DMDO_DEFAULT
;
92 modes
[0].dmBitsPerPel
= screen_bpp
;
93 modes
[0].dmPelsWidth
= primary
.right
;
94 modes
[0].dmPelsHeight
= primary
.bottom
;
95 modes
[0].dmDisplayFlags
= 0;
96 modes
[0].dmDisplayFrequency
= 60;
103 static void nores_free_modes(DEVMODEW
*modes
)
108 static BOOL
nores_get_current_mode(x11drv_settings_id id
, DEVMODEW
*mode
)
110 RECT primary
= get_host_primary_monitor_rect();
112 mode
->dmFields
= DM_DISPLAYORIENTATION
| DM_BITSPERPEL
| DM_PELSWIDTH
| DM_PELSHEIGHT
|
113 DM_DISPLAYFLAGS
| DM_DISPLAYFREQUENCY
| DM_POSITION
;
114 mode
->dmDisplayOrientation
= DMDO_DEFAULT
;
115 mode
->dmDisplayFlags
= 0;
116 mode
->dmPosition
.x
= 0;
117 mode
->dmPosition
.y
= 0;
121 FIXME("Non-primary adapters are unsupported.\n");
122 mode
->dmBitsPerPel
= 0;
123 mode
->dmPelsWidth
= 0;
124 mode
->dmPelsHeight
= 0;
125 mode
->dmDisplayFrequency
= 0;
129 mode
->dmBitsPerPel
= screen_bpp
;
130 mode
->dmPelsWidth
= primary
.right
;
131 mode
->dmPelsHeight
= primary
.bottom
;
132 mode
->dmDisplayFrequency
= 60;
136 static LONG
nores_set_current_mode(x11drv_settings_id id
, const DEVMODEW
*mode
)
138 WARN("NoRes settings handler, ignoring mode change request.\n");
139 return DISP_CHANGE_SUCCESSFUL
;
142 /* default handler only gets the current X desktop resolution */
143 void X11DRV_Settings_Init(void)
145 struct x11drv_settings_handler nores_handler
;
147 depths
= screen_bpp
== 32 ? depths_32
: depths_24
;
149 nores_handler
.name
= "NoRes";
150 nores_handler
.priority
= 1;
151 nores_handler
.get_id
= nores_get_id
;
152 nores_handler
.get_modes
= nores_get_modes
;
153 nores_handler
.free_modes
= nores_free_modes
;
154 nores_handler
.get_current_mode
= nores_get_current_mode
;
155 nores_handler
.set_current_mode
= nores_set_current_mode
;
156 X11DRV_Settings_SetHandler(&nores_handler
);
159 static void set_display_depth(x11drv_settings_id display_id
, DWORD depth
)
161 struct x11drv_display_depth
*display_depth
;
163 pthread_mutex_lock( &settings_mutex
);
164 LIST_FOR_EACH_ENTRY(display_depth
, &x11drv_display_depth_list
, struct x11drv_display_depth
, entry
)
166 if (display_depth
->display_id
.id
== display_id
.id
)
168 display_depth
->depth
= depth
;
169 pthread_mutex_unlock( &settings_mutex
);
174 display_depth
= malloc(sizeof(*display_depth
));
177 ERR("Failed to allocate memory.\n");
178 pthread_mutex_unlock( &settings_mutex
);
182 display_depth
->display_id
= display_id
;
183 display_depth
->depth
= depth
;
184 list_add_head(&x11drv_display_depth_list
, &display_depth
->entry
);
185 pthread_mutex_unlock( &settings_mutex
);
188 static DWORD
get_display_depth(x11drv_settings_id display_id
)
190 struct x11drv_display_depth
*display_depth
;
193 pthread_mutex_lock( &settings_mutex
);
194 LIST_FOR_EACH_ENTRY(display_depth
, &x11drv_display_depth_list
, struct x11drv_display_depth
, entry
)
196 if (display_depth
->display_id
.id
== display_id
.id
)
198 depth
= display_depth
->depth
;
199 pthread_mutex_unlock( &settings_mutex
);
203 pthread_mutex_unlock( &settings_mutex
);
207 INT
X11DRV_GetDisplayDepth(LPCWSTR name
, BOOL is_primary
)
209 x11drv_settings_id id
;
211 if (settings_handler
.get_id( name
, is_primary
, &id
))
212 return get_display_depth( id
);
217 /***********************************************************************
218 * GetCurrentDisplaySettings (X11DRV.@)
221 BOOL
X11DRV_GetCurrentDisplaySettings( LPCWSTR name
, BOOL is_primary
, LPDEVMODEW devmode
)
224 x11drv_settings_id id
;
226 if (!settings_handler
.get_id( name
, is_primary
, &id
) || !settings_handler
.get_current_mode( id
, &mode
))
228 ERR("Failed to get %s current display settings.\n", wine_dbgstr_w(name
));
232 memcpy( &devmode
->dmFields
, &mode
.dmFields
, devmode
->dmSize
- offsetof(DEVMODEW
, dmFields
) );
233 if (!is_detached_mode( devmode
)) devmode
->dmBitsPerPel
= get_display_depth( id
);
237 BOOL
is_detached_mode(const DEVMODEW
*mode
)
239 return mode
->dmFields
& DM_POSITION
&&
240 mode
->dmFields
& DM_PELSWIDTH
&&
241 mode
->dmFields
& DM_PELSHEIGHT
&&
242 mode
->dmPelsWidth
== 0 &&
243 mode
->dmPelsHeight
== 0;
246 static BOOL
is_same_devmode( const DEVMODEW
*a
, const DEVMODEW
*b
)
248 return a
->dmDisplayOrientation
== b
->dmDisplayOrientation
&&
249 a
->dmBitsPerPel
== b
->dmBitsPerPel
&&
250 a
->dmPelsWidth
== b
->dmPelsWidth
&&
251 a
->dmPelsHeight
== b
->dmPelsHeight
&&
252 a
->dmDisplayFrequency
== b
->dmDisplayFrequency
;
255 /* Get the full display mode with all the necessary fields set.
256 * Return NULL on failure. Caller should call free_full_mode() to free the returned mode. */
257 static DEVMODEW
*get_full_mode(x11drv_settings_id id
, DEVMODEW
*dev_mode
)
259 DEVMODEW
*modes
, *full_mode
, *found_mode
= NULL
;
260 UINT mode_count
, mode_idx
;
262 if (is_detached_mode(dev_mode
))
265 if (!settings_handler
.get_modes(id
, EDS_ROTATEDMODE
, &modes
, &mode_count
))
268 for (mode_idx
= 0; mode_idx
< mode_count
; ++mode_idx
)
270 found_mode
= (DEVMODEW
*)((BYTE
*)modes
+ (sizeof(*modes
) + modes
[0].dmDriverExtra
) * mode_idx
);
271 if (is_same_devmode( found_mode
, dev_mode
)) break;
274 if (!found_mode
|| mode_idx
== mode_count
)
276 settings_handler
.free_modes(modes
);
280 if (!(full_mode
= malloc(sizeof(*found_mode
) + found_mode
->dmDriverExtra
)))
282 settings_handler
.free_modes(modes
);
286 memcpy(full_mode
, found_mode
, sizeof(*found_mode
) + found_mode
->dmDriverExtra
);
287 settings_handler
.free_modes(modes
);
289 full_mode
->dmFields
|= DM_POSITION
;
290 full_mode
->dmPosition
= dev_mode
->dmPosition
;
294 static void free_full_mode(DEVMODEW
*mode
)
296 if (!is_detached_mode(mode
))
300 static LONG
apply_display_settings( DEVMODEW
*displays
, x11drv_settings_id
*ids
, BOOL do_attach
)
307 for (count
= 0, mode
= displays
; mode
->dmSize
; mode
= NEXT_DEVMODEW(mode
), count
++)
309 x11drv_settings_id
*id
= ids
+ count
;
311 attached_mode
= !is_detached_mode(mode
);
312 if ((attached_mode
&& !do_attach
) || (!attached_mode
&& do_attach
))
315 /* FIXME: get a full mode again because X11 driver extra data isn't portable */
316 full_mode
= get_full_mode(*id
, mode
);
318 return DISP_CHANGE_BADMODE
;
320 TRACE("handler:%s changing %s to position:(%d,%d) resolution:%ux%u frequency:%uHz "
321 "depth:%ubits orientation:%#x.\n", settings_handler
.name
,
322 wine_dbgstr_w(mode
->dmDeviceName
),
323 (int)full_mode
->dmPosition
.x
, (int)full_mode
->dmPosition
.y
, (int)full_mode
->dmPelsWidth
,
324 (int)full_mode
->dmPelsHeight
, (int)full_mode
->dmDisplayFrequency
,
325 (int)full_mode
->dmBitsPerPel
, (int)full_mode
->dmDisplayOrientation
);
327 ret
= settings_handler
.set_current_mode(*id
, full_mode
);
328 if (attached_mode
&& ret
== DISP_CHANGE_SUCCESSFUL
)
329 set_display_depth(*id
, full_mode
->dmBitsPerPel
);
330 free_full_mode(full_mode
);
331 if (ret
!= DISP_CHANGE_SUCCESSFUL
)
335 return DISP_CHANGE_SUCCESSFUL
;
338 /***********************************************************************
339 * ChangeDisplaySettings (X11DRV.@)
342 LONG
X11DRV_ChangeDisplaySettings( LPDEVMODEW displays
, LPCWSTR primary_name
, HWND hwnd
, DWORD flags
, LPVOID lpvoid
)
344 INT left_most
= INT_MAX
, top_most
= INT_MAX
;
345 LONG count
, ret
= DISP_CHANGE_BADPARAM
;
346 x11drv_settings_id
*ids
;
349 /* Convert virtual screen coordinates to root coordinates, and find display ids.
350 * We cannot safely get the ids while changing modes, as the backend state may be invalidated.
352 for (count
= 0, mode
= displays
; mode
->dmSize
; mode
= NEXT_DEVMODEW(mode
), count
++)
354 left_most
= min( left_most
, mode
->dmPosition
.x
);
355 top_most
= min( top_most
, mode
->dmPosition
.y
);
358 if (!(ids
= calloc( count
, sizeof(*ids
) ))) return DISP_CHANGE_FAILED
;
359 for (count
= 0, mode
= displays
; mode
->dmSize
; mode
= NEXT_DEVMODEW(mode
), count
++)
361 if (!settings_handler
.get_id( mode
->dmDeviceName
, !wcsicmp( mode
->dmDeviceName
, primary_name
), ids
+ count
)) goto done
;
362 mode
->dmPosition
.x
-= left_most
;
363 mode
->dmPosition
.y
-= top_most
;
366 /* Detach displays first to free up CRTCs */
367 ret
= apply_display_settings( displays
, ids
, FALSE
);
368 if (ret
== DISP_CHANGE_SUCCESSFUL
)
369 ret
= apply_display_settings( displays
, ids
, TRUE
);
376 POINT
virtual_screen_to_root(INT x
, INT y
)
378 RECT
virtual = NtUserGetVirtualScreenRect();
381 pt
.x
= x
- virtual.left
;
382 pt
.y
= y
- virtual.top
;
386 POINT
root_to_virtual_screen(INT x
, INT y
)
388 RECT
virtual = NtUserGetVirtualScreenRect();
391 pt
.x
= x
+ virtual.left
;
392 pt
.y
= y
+ virtual.top
;
396 /* Get the primary monitor rect from the host system */
397 RECT
get_host_primary_monitor_rect(void)
399 INT gpu_count
, adapter_count
, monitor_count
;
400 struct gdi_gpu
*gpus
= NULL
;
401 struct gdi_adapter
*adapters
= NULL
;
402 struct gdi_monitor
*monitors
= NULL
;
405 /* The first monitor is always primary */
406 if (host_handler
.get_gpus(&gpus
, &gpu_count
) && gpu_count
&&
407 host_handler
.get_adapters(gpus
[0].id
, &adapters
, &adapter_count
) && adapter_count
&&
408 host_handler
.get_monitors(adapters
[0].id
, &monitors
, &monitor_count
) && monitor_count
)
409 rect
= monitors
[0].rc_monitor
;
411 if (gpus
) host_handler
.free_gpus(gpus
);
412 if (adapters
) host_handler
.free_adapters(adapters
);
413 if (monitors
) host_handler
.free_monitors(monitors
, monitor_count
);
417 BOOL
get_host_primary_gpu(struct gdi_gpu
*gpu
)
419 struct gdi_gpu
*gpus
;
422 if (host_handler
.get_gpus(&gpus
, &gpu_count
) && gpu_count
)
425 host_handler
.free_gpus(gpus
);
432 RECT
get_work_area(const RECT
*monitor_rect
)
436 unsigned long count
, remaining
, i
;
440 /* Try _GTK_WORKAREAS first as _NET_WORKAREA may be incorrect on multi-monitor systems */
441 if (!XGetWindowProperty(gdi_display
, DefaultRootWindow(gdi_display
),
442 x11drv_atom(_GTK_WORKAREAS_D0
), 0, ~0, False
, XA_CARDINAL
, &type
,
443 &format
, &count
, &remaining
, (unsigned char **)&work_area
))
445 if (type
== XA_CARDINAL
&& format
== 32)
447 for (i
= 0; i
< count
/ 4; ++i
)
449 work_rect
.left
= work_area
[i
* 4];
450 work_rect
.top
= work_area
[i
* 4 + 1];
451 work_rect
.right
= work_rect
.left
+ work_area
[i
* 4 + 2];
452 work_rect
.bottom
= work_rect
.top
+ work_area
[i
* 4 + 3];
454 if (intersect_rect( &work_rect
, &work_rect
, monitor_rect
))
456 TRACE("work_rect:%s.\n", wine_dbgstr_rect(&work_rect
));
465 WARN("_GTK_WORKAREAS is not supported, fallback to _NET_WORKAREA. "
466 "Work areas may be incorrect on multi-monitor systems.\n");
467 if (!XGetWindowProperty(gdi_display
, DefaultRootWindow(gdi_display
), x11drv_atom(_NET_WORKAREA
),
468 0, ~0, False
, XA_CARDINAL
, &type
, &format
, &count
, &remaining
,
469 (unsigned char **)&work_area
))
471 if (type
== XA_CARDINAL
&& format
== 32 && count
>= 4)
473 SetRect(&work_rect
, work_area
[0], work_area
[1], work_area
[0] + work_area
[2],
474 work_area
[1] + work_area
[3]);
476 if (intersect_rect( &work_rect
, &work_rect
, monitor_rect
))
478 TRACE("work_rect:%s.\n", wine_dbgstr_rect(&work_rect
));
486 WARN("_NET_WORKAREA is not supported, Work areas may be incorrect.\n");
487 TRACE("work_rect:%s.\n", wine_dbgstr_rect(monitor_rect
));
488 return *monitor_rect
;
491 void X11DRV_DisplayDevices_SetHandler(const struct x11drv_display_device_handler
*new_handler
)
493 if (new_handler
->priority
> host_handler
.priority
)
495 host_handler
= *new_handler
;
496 TRACE("Display device functions are now handled by: %s\n", host_handler
.name
);
500 void X11DRV_DisplayDevices_RegisterEventHandlers(void)
502 struct x11drv_display_device_handler
*handler
= is_virtual_desktop() ? &desktop_handler
: &host_handler
;
504 if (handler
->register_event_handlers
)
505 handler
->register_event_handlers();
508 /* Report whether a display device handler supports detecting dynamic device changes */
509 BOOL
X11DRV_DisplayDevices_SupportEventHandlers(void)
511 return !!host_handler
.register_event_handlers
;
514 static BOOL force_display_devices_refresh
;
516 static const char *debugstr_devmodew( const DEVMODEW
*devmode
)
518 char position
[32] = {0};
520 if (devmode
->dmFields
& DM_POSITION
)
522 snprintf( position
, sizeof(position
), " at (%d,%d)",
523 (int)devmode
->dmPosition
.x
, (int)devmode
->dmPosition
.y
);
526 return wine_dbg_sprintf( "%ux%u %ubits %uHz rotated %u degrees%s",
527 (unsigned int)devmode
->dmPelsWidth
,
528 (unsigned int)devmode
->dmPelsHeight
,
529 (unsigned int)devmode
->dmBitsPerPel
,
530 (unsigned int)devmode
->dmDisplayFrequency
,
531 (unsigned int)devmode
->dmDisplayOrientation
* 90,
535 BOOL
X11DRV_UpdateDisplayDevices( const struct gdi_device_manager
*device_manager
, BOOL force
, void *param
)
537 struct x11drv_display_device_handler
*handler
;
538 struct gdi_adapter
*adapters
;
539 struct gdi_monitor
*monitors
;
540 struct gdi_gpu
*gpus
;
541 INT gpu_count
, adapter_count
, monitor_count
;
542 INT gpu
, adapter
, monitor
;
543 DEVMODEW
*modes
, *mode
;
545 BOOL virtual_desktop
;
547 if (!force
&& !force_display_devices_refresh
) return TRUE
;
548 force_display_devices_refresh
= FALSE
;
549 virtual_desktop
= is_virtual_desktop();
550 handler
= virtual_desktop
? &desktop_handler
: &host_handler
;
552 TRACE("via %s\n", wine_dbgstr_a(handler
->name
));
554 /* Initialize GPUs */
555 if (!handler
->get_gpus( &gpus
, &gpu_count
)) return FALSE
;
556 TRACE("GPU count: %d\n", gpu_count
);
558 for (gpu
= 0; gpu
< gpu_count
; gpu
++)
560 device_manager
->add_gpu( &gpus
[gpu
], param
);
562 /* Initialize adapters */
563 if (!handler
->get_adapters(gpus
[gpu
].id
, &adapters
, &adapter_count
)) break;
564 TRACE("GPU: %#lx %s, adapter count: %d\n", gpus
[gpu
].id
, wine_dbgstr_w(gpus
[gpu
].name
), adapter_count
);
566 for (adapter
= 0; adapter
< adapter_count
; adapter
++)
568 DEVMODEW current_mode
= {.dmSize
= sizeof(current_mode
)};
571 x11drv_settings_id settings_id
;
572 BOOL is_primary
= adapters
[adapter
].state_flags
& DISPLAY_DEVICE_PRIMARY_DEVICE
;
574 device_manager
->add_adapter( &adapters
[adapter
], param
);
576 if (!handler
->get_monitors(adapters
[adapter
].id
, &monitors
, &monitor_count
)) break;
577 TRACE("adapter: %#lx, monitor count: %d\n", adapters
[adapter
].id
, monitor_count
);
579 /* Initialize monitors */
580 for (monitor
= 0; monitor
< monitor_count
; monitor
++)
581 device_manager
->add_monitor( &monitors
[monitor
], param
);
583 handler
->free_monitors(monitors
, monitor_count
);
585 /* Get the settings handler id for the adapter */
586 snprintf( buffer
, sizeof(buffer
), "\\\\.\\DISPLAY%d", adapter
+ 1 );
587 asciiz_to_unicode( devname
, buffer
);
588 if (!settings_handler
.get_id( devname
, is_primary
, &settings_id
)) break;
590 /* We don't need to set the win32u current mode when we are in
591 * virtual desktop mode, and, additionally, skipping this avoids a
592 * deadlock, since the desktop get_current_mode() implementation
593 * recurses into win32u. */
594 if (!virtual_desktop
) settings_handler
.get_current_mode( settings_id
, ¤t_mode
);
595 if (!settings_handler
.get_modes( settings_id
, EDS_ROTATEDMODE
, &modes
, &mode_count
))
598 for (mode
= modes
; mode_count
; mode_count
--)
600 if (is_same_devmode( mode
, ¤t_mode
))
602 TRACE( "current mode: %s\n", debugstr_devmodew( ¤t_mode
) );
603 device_manager
->add_mode( ¤t_mode
, TRUE
, param
);
607 TRACE( "mode: %s\n", debugstr_devmodew( mode
) );
608 device_manager
->add_mode( mode
, FALSE
, param
);
610 mode
= (DEVMODEW
*)((char *)mode
+ sizeof(*modes
) + modes
[0].dmDriverExtra
);
613 settings_handler
.free_modes( modes
);
616 handler
->free_adapters(adapters
);
619 handler
->free_gpus(gpus
);
623 void X11DRV_DisplayDevices_Init(BOOL force
)
625 UINT32 num_path
, num_mode
;
627 if (force
) force_display_devices_refresh
= TRUE
;
628 /* trigger refresh in win32u */
629 NtUserGetDisplayConfigBufferSizes( QDC_ONLY_ACTIVE_PATHS
, &num_path
, &num_mode
);