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 static struct x11drv_settings_handler settings_handler
;
34 #define NEXT_DEVMODEW(mode) ((DEVMODEW *)((char *)((mode) + 1) + (mode)->dmDriverExtra))
36 struct x11drv_display_depth
39 x11drv_settings_id display_id
;
43 /* Display device emulated depth list, protected by modes_section */
44 static struct list x11drv_display_depth_list
= LIST_INIT(x11drv_display_depth_list
);
46 /* All Windows drivers seen so far either support 32 bit depths, or 24 bit depths, but never both. So if we have
47 * a 32 bit framebuffer, report 32 bit bpps, otherwise 24 bit ones.
49 static const unsigned int depths_24
[] = {8, 16, 24};
50 static const unsigned int depths_32
[] = {8, 16, 32};
51 const unsigned int *depths
;
53 static pthread_mutex_t settings_mutex
= PTHREAD_MUTEX_INITIALIZER
;
55 void X11DRV_Settings_SetHandler(const struct x11drv_settings_handler
*new_handler
)
57 if (new_handler
->priority
> settings_handler
.priority
)
59 settings_handler
= *new_handler
;
60 TRACE("Display settings are now handled by: %s.\n", settings_handler
.name
);
64 /***********************************************************************
65 * Default handlers if resolution switching is not enabled
68 static BOOL
nores_get_id(const WCHAR
*device_name
, BOOL is_primary
, x11drv_settings_id
*id
)
70 id
->id
= is_primary
? 1 : 0;
74 static BOOL
nores_get_modes(x11drv_settings_id id
, DWORD flags
, DEVMODEW
**new_modes
, UINT
*mode_count
)
76 RECT primary
= get_host_primary_monitor_rect();
79 modes
= calloc(1, sizeof(*modes
));
82 RtlSetLastWin32Error( ERROR_NOT_ENOUGH_MEMORY
);
86 modes
[0].dmSize
= sizeof(*modes
);
87 modes
[0].dmDriverExtra
= 0;
88 modes
[0].dmFields
= DM_DISPLAYORIENTATION
| DM_BITSPERPEL
| DM_PELSWIDTH
| DM_PELSHEIGHT
|
89 DM_DISPLAYFLAGS
| DM_DISPLAYFREQUENCY
;
90 modes
[0].dmDisplayOrientation
= DMDO_DEFAULT
;
91 modes
[0].dmBitsPerPel
= screen_bpp
;
92 modes
[0].dmPelsWidth
= primary
.right
;
93 modes
[0].dmPelsHeight
= primary
.bottom
;
94 modes
[0].dmDisplayFlags
= 0;
95 modes
[0].dmDisplayFrequency
= 60;
102 static void nores_free_modes(DEVMODEW
*modes
)
107 static BOOL
nores_get_current_mode(x11drv_settings_id id
, DEVMODEW
*mode
)
109 RECT primary
= get_host_primary_monitor_rect();
111 mode
->dmFields
= DM_DISPLAYORIENTATION
| DM_BITSPERPEL
| DM_PELSWIDTH
| DM_PELSHEIGHT
|
112 DM_DISPLAYFLAGS
| DM_DISPLAYFREQUENCY
| DM_POSITION
;
113 mode
->dmDisplayOrientation
= DMDO_DEFAULT
;
114 mode
->dmDisplayFlags
= 0;
115 mode
->dmPosition
.x
= 0;
116 mode
->dmPosition
.y
= 0;
120 FIXME("Non-primary adapters are unsupported.\n");
121 mode
->dmBitsPerPel
= 0;
122 mode
->dmPelsWidth
= 0;
123 mode
->dmPelsHeight
= 0;
124 mode
->dmDisplayFrequency
= 0;
128 mode
->dmBitsPerPel
= screen_bpp
;
129 mode
->dmPelsWidth
= primary
.right
;
130 mode
->dmPelsHeight
= primary
.bottom
;
131 mode
->dmDisplayFrequency
= 60;
135 static LONG
nores_set_current_mode(x11drv_settings_id id
, const DEVMODEW
*mode
)
137 WARN("NoRes settings handler, ignoring mode change request.\n");
138 return DISP_CHANGE_SUCCESSFUL
;
141 /* default handler only gets the current X desktop resolution */
142 void X11DRV_Settings_Init(void)
144 struct x11drv_settings_handler nores_handler
;
146 depths
= screen_bpp
== 32 ? depths_32
: depths_24
;
148 nores_handler
.name
= "NoRes";
149 nores_handler
.priority
= 1;
150 nores_handler
.get_id
= nores_get_id
;
151 nores_handler
.get_modes
= nores_get_modes
;
152 nores_handler
.free_modes
= nores_free_modes
;
153 nores_handler
.get_current_mode
= nores_get_current_mode
;
154 nores_handler
.set_current_mode
= nores_set_current_mode
;
155 X11DRV_Settings_SetHandler(&nores_handler
);
158 static void set_display_depth(x11drv_settings_id display_id
, DWORD depth
)
160 struct x11drv_display_depth
*display_depth
;
162 pthread_mutex_lock( &settings_mutex
);
163 LIST_FOR_EACH_ENTRY(display_depth
, &x11drv_display_depth_list
, struct x11drv_display_depth
, entry
)
165 if (display_depth
->display_id
.id
== display_id
.id
)
167 display_depth
->depth
= depth
;
168 pthread_mutex_unlock( &settings_mutex
);
173 display_depth
= malloc(sizeof(*display_depth
));
176 ERR("Failed to allocate memory.\n");
177 pthread_mutex_unlock( &settings_mutex
);
181 display_depth
->display_id
= display_id
;
182 display_depth
->depth
= depth
;
183 list_add_head(&x11drv_display_depth_list
, &display_depth
->entry
);
184 pthread_mutex_unlock( &settings_mutex
);
187 static DWORD
get_display_depth(x11drv_settings_id display_id
)
189 struct x11drv_display_depth
*display_depth
;
192 pthread_mutex_lock( &settings_mutex
);
193 LIST_FOR_EACH_ENTRY(display_depth
, &x11drv_display_depth_list
, struct x11drv_display_depth
, entry
)
195 if (display_depth
->display_id
.id
== display_id
.id
)
197 depth
= display_depth
->depth
;
198 pthread_mutex_unlock( &settings_mutex
);
202 pthread_mutex_unlock( &settings_mutex
);
206 INT
X11DRV_GetDisplayDepth(LPCWSTR name
, BOOL is_primary
)
208 x11drv_settings_id id
;
210 if (settings_handler
.get_id( name
, is_primary
, &id
))
211 return get_display_depth( id
);
216 /***********************************************************************
217 * GetCurrentDisplaySettings (X11DRV.@)
220 BOOL
X11DRV_GetCurrentDisplaySettings( LPCWSTR name
, BOOL is_primary
, LPDEVMODEW devmode
)
223 x11drv_settings_id id
;
225 if (!settings_handler
.get_id( name
, is_primary
, &id
) || !settings_handler
.get_current_mode( id
, &mode
))
227 ERR("Failed to get %s current display settings.\n", wine_dbgstr_w(name
));
231 memcpy( &devmode
->dmFields
, &mode
.dmFields
, devmode
->dmSize
- offsetof(DEVMODEW
, dmFields
) );
232 if (!is_detached_mode( devmode
)) devmode
->dmBitsPerPel
= get_display_depth( id
);
236 BOOL
is_detached_mode(const DEVMODEW
*mode
)
238 return mode
->dmFields
& DM_POSITION
&&
239 mode
->dmFields
& DM_PELSWIDTH
&&
240 mode
->dmFields
& DM_PELSHEIGHT
&&
241 mode
->dmPelsWidth
== 0 &&
242 mode
->dmPelsHeight
== 0;
245 static BOOL
is_same_devmode( const DEVMODEW
*a
, const DEVMODEW
*b
)
247 return a
->dmDisplayOrientation
== b
->dmDisplayOrientation
&&
248 a
->dmBitsPerPel
== b
->dmBitsPerPel
&&
249 a
->dmPelsWidth
== b
->dmPelsWidth
&&
250 a
->dmPelsHeight
== b
->dmPelsHeight
&&
251 a
->dmDisplayFrequency
== b
->dmDisplayFrequency
;
254 /* Get the full display mode with all the necessary fields set.
255 * Return NULL on failure. Caller should call free_full_mode() to free the returned mode. */
256 static DEVMODEW
*get_full_mode(x11drv_settings_id id
, DEVMODEW
*dev_mode
)
258 DEVMODEW
*modes
, *full_mode
, *found_mode
= NULL
;
259 UINT mode_count
, mode_idx
;
261 if (is_detached_mode(dev_mode
))
264 if (!settings_handler
.get_modes(id
, EDS_ROTATEDMODE
, &modes
, &mode_count
))
267 for (mode_idx
= 0; mode_idx
< mode_count
; ++mode_idx
)
269 found_mode
= (DEVMODEW
*)((BYTE
*)modes
+ (sizeof(*modes
) + modes
[0].dmDriverExtra
) * mode_idx
);
270 if (is_same_devmode( found_mode
, dev_mode
)) break;
273 if (!found_mode
|| mode_idx
== mode_count
)
275 settings_handler
.free_modes(modes
);
279 if (!(full_mode
= malloc(sizeof(*found_mode
) + found_mode
->dmDriverExtra
)))
281 settings_handler
.free_modes(modes
);
285 memcpy(full_mode
, found_mode
, sizeof(*found_mode
) + found_mode
->dmDriverExtra
);
286 settings_handler
.free_modes(modes
);
288 full_mode
->dmFields
|= DM_POSITION
;
289 full_mode
->dmPosition
= dev_mode
->dmPosition
;
293 static void free_full_mode(DEVMODEW
*mode
)
295 if (!is_detached_mode(mode
))
299 static LONG
apply_display_settings( DEVMODEW
*displays
, x11drv_settings_id
*ids
, BOOL do_attach
)
306 for (count
= 0, mode
= displays
; mode
->dmSize
; mode
= NEXT_DEVMODEW(mode
), count
++)
308 x11drv_settings_id
*id
= ids
+ count
;
310 attached_mode
= !is_detached_mode(mode
);
311 if ((attached_mode
&& !do_attach
) || (!attached_mode
&& do_attach
))
314 /* FIXME: get a full mode again because X11 driver extra data isn't portable */
315 full_mode
= get_full_mode(*id
, mode
);
317 return DISP_CHANGE_BADMODE
;
319 TRACE("handler:%s changing %s to position:(%d,%d) resolution:%ux%u frequency:%uHz "
320 "depth:%ubits orientation:%#x.\n", settings_handler
.name
,
321 wine_dbgstr_w(mode
->dmDeviceName
),
322 (int)full_mode
->dmPosition
.x
, (int)full_mode
->dmPosition
.y
, (int)full_mode
->dmPelsWidth
,
323 (int)full_mode
->dmPelsHeight
, (int)full_mode
->dmDisplayFrequency
,
324 (int)full_mode
->dmBitsPerPel
, (int)full_mode
->dmDisplayOrientation
);
326 ret
= settings_handler
.set_current_mode(*id
, full_mode
);
327 if (attached_mode
&& ret
== DISP_CHANGE_SUCCESSFUL
)
328 set_display_depth(*id
, full_mode
->dmBitsPerPel
);
329 free_full_mode(full_mode
);
330 if (ret
!= DISP_CHANGE_SUCCESSFUL
)
334 return DISP_CHANGE_SUCCESSFUL
;
337 /***********************************************************************
338 * ChangeDisplaySettings (X11DRV.@)
341 LONG
X11DRV_ChangeDisplaySettings( LPDEVMODEW displays
, LPCWSTR primary_name
, HWND hwnd
, DWORD flags
, LPVOID lpvoid
)
343 INT left_most
= INT_MAX
, top_most
= INT_MAX
;
344 LONG count
, ret
= DISP_CHANGE_BADPARAM
;
345 x11drv_settings_id
*ids
;
348 /* Convert virtual screen coordinates to root coordinates, and find display ids.
349 * We cannot safely get the ids while changing modes, as the backend state may be invalidated.
351 for (count
= 0, mode
= displays
; mode
->dmSize
; mode
= NEXT_DEVMODEW(mode
), count
++)
353 left_most
= min( left_most
, mode
->dmPosition
.x
);
354 top_most
= min( top_most
, mode
->dmPosition
.y
);
357 if (!(ids
= calloc( count
, sizeof(*ids
) ))) return DISP_CHANGE_FAILED
;
358 for (count
= 0, mode
= displays
; mode
->dmSize
; mode
= NEXT_DEVMODEW(mode
), count
++)
360 if (!settings_handler
.get_id( mode
->dmDeviceName
, !wcsicmp( mode
->dmDeviceName
, primary_name
), ids
+ count
)) goto done
;
361 mode
->dmPosition
.x
-= left_most
;
362 mode
->dmPosition
.y
-= top_most
;
365 /* Detach displays first to free up CRTCs */
366 ret
= apply_display_settings( displays
, ids
, FALSE
);
367 if (ret
== DISP_CHANGE_SUCCESSFUL
)
368 ret
= apply_display_settings( displays
, ids
, TRUE
);
375 POINT
virtual_screen_to_root(INT x
, INT y
)
377 RECT
virtual = NtUserGetVirtualScreenRect();
380 pt
.x
= x
- virtual.left
;
381 pt
.y
= y
- virtual.top
;
385 POINT
root_to_virtual_screen(INT x
, INT y
)
387 RECT
virtual = NtUserGetVirtualScreenRect();
390 pt
.x
= x
+ virtual.left
;
391 pt
.y
= y
+ virtual.top
;
395 /* Get the primary monitor rect from the host system */
396 RECT
get_host_primary_monitor_rect(void)
398 INT gpu_count
, adapter_count
, monitor_count
;
399 struct gdi_gpu
*gpus
= NULL
;
400 struct gdi_adapter
*adapters
= NULL
;
401 struct gdi_monitor
*monitors
= NULL
;
404 /* The first monitor is always primary */
405 if (host_handler
.get_gpus(&gpus
, &gpu_count
, FALSE
) && gpu_count
&&
406 host_handler
.get_adapters(gpus
[0].id
, &adapters
, &adapter_count
) && adapter_count
&&
407 host_handler
.get_monitors(adapters
[0].id
, &monitors
, &monitor_count
) && monitor_count
)
408 rect
= monitors
[0].rc_monitor
;
410 if (gpus
) host_handler
.free_gpus(gpus
);
411 if (adapters
) host_handler
.free_adapters(adapters
);
412 if (monitors
) host_handler
.free_monitors(monitors
, monitor_count
);
416 RECT
get_work_area(const RECT
*monitor_rect
)
420 unsigned long count
, remaining
, i
;
424 /* Try _GTK_WORKAREAS first as _NET_WORKAREA may be incorrect on multi-monitor systems */
425 if (!XGetWindowProperty(gdi_display
, DefaultRootWindow(gdi_display
),
426 x11drv_atom(_GTK_WORKAREAS_D0
), 0, ~0, False
, XA_CARDINAL
, &type
,
427 &format
, &count
, &remaining
, (unsigned char **)&work_area
))
429 if (type
== XA_CARDINAL
&& format
== 32)
431 for (i
= 0; i
< count
/ 4; ++i
)
433 work_rect
.left
= work_area
[i
* 4];
434 work_rect
.top
= work_area
[i
* 4 + 1];
435 work_rect
.right
= work_rect
.left
+ work_area
[i
* 4 + 2];
436 work_rect
.bottom
= work_rect
.top
+ work_area
[i
* 4 + 3];
438 if (intersect_rect( &work_rect
, &work_rect
, monitor_rect
))
440 TRACE("work_rect:%s.\n", wine_dbgstr_rect(&work_rect
));
449 WARN("_GTK_WORKAREAS is not supported, fallback to _NET_WORKAREA. "
450 "Work areas may be incorrect on multi-monitor systems.\n");
451 if (!XGetWindowProperty(gdi_display
, DefaultRootWindow(gdi_display
), x11drv_atom(_NET_WORKAREA
),
452 0, ~0, False
, XA_CARDINAL
, &type
, &format
, &count
, &remaining
,
453 (unsigned char **)&work_area
))
455 if (type
== XA_CARDINAL
&& format
== 32 && count
>= 4)
457 SetRect(&work_rect
, work_area
[0], work_area
[1], work_area
[0] + work_area
[2],
458 work_area
[1] + work_area
[3]);
460 if (intersect_rect( &work_rect
, &work_rect
, monitor_rect
))
462 TRACE("work_rect:%s.\n", wine_dbgstr_rect(&work_rect
));
470 WARN("_NET_WORKAREA is not supported, Work areas may be incorrect.\n");
471 TRACE("work_rect:%s.\n", wine_dbgstr_rect(monitor_rect
));
472 return *monitor_rect
;
475 void X11DRV_DisplayDevices_SetHandler(const struct x11drv_display_device_handler
*new_handler
)
477 if (new_handler
->priority
> host_handler
.priority
)
479 host_handler
= *new_handler
;
480 TRACE("Display device functions are now handled by: %s\n", host_handler
.name
);
484 void X11DRV_DisplayDevices_RegisterEventHandlers(void)
486 if (host_handler
.register_event_handlers
) host_handler
.register_event_handlers();
489 /* Report whether a display device handler supports detecting dynamic device changes */
490 BOOL
X11DRV_DisplayDevices_SupportEventHandlers(void)
492 return !!host_handler
.register_event_handlers
;
495 static BOOL force_display_devices_refresh
;
497 static const char *debugstr_devmodew( const DEVMODEW
*devmode
)
499 char position
[32] = {0};
501 if (devmode
->dmFields
& DM_POSITION
)
503 snprintf( position
, sizeof(position
), " at (%d,%d)",
504 (int)devmode
->dmPosition
.x
, (int)devmode
->dmPosition
.y
);
507 return wine_dbg_sprintf( "%ux%u %ubits %uHz rotated %u degrees%s",
508 (unsigned int)devmode
->dmPelsWidth
,
509 (unsigned int)devmode
->dmPelsHeight
,
510 (unsigned int)devmode
->dmBitsPerPel
,
511 (unsigned int)devmode
->dmDisplayFrequency
,
512 (unsigned int)devmode
->dmDisplayOrientation
* 90,
516 BOOL
X11DRV_UpdateDisplayDevices( const struct gdi_device_manager
*device_manager
, BOOL force
, void *param
)
518 struct gdi_adapter
*adapters
;
519 struct gdi_monitor
*monitors
;
520 struct gdi_gpu
*gpus
;
521 INT gpu_count
, adapter_count
, monitor_count
;
522 INT gpu
, adapter
, monitor
;
523 DEVMODEW
*modes
, *mode
;
526 if (!force
&& !force_display_devices_refresh
) return TRUE
;
527 force_display_devices_refresh
= FALSE
;
529 TRACE( "via %s\n", debugstr_a(host_handler
.name
) );
531 /* Initialize GPUs */
532 if (!host_handler
.get_gpus( &gpus
, &gpu_count
, TRUE
)) return FALSE
;
533 TRACE("GPU count: %d\n", gpu_count
);
535 for (gpu
= 0; gpu
< gpu_count
; gpu
++)
537 device_manager
->add_gpu( &gpus
[gpu
], param
);
539 /* Initialize adapters */
540 if (!host_handler
.get_adapters( gpus
[gpu
].id
, &adapters
, &adapter_count
)) break;
541 TRACE("GPU: %#lx %s, adapter count: %d\n", gpus
[gpu
].id
, wine_dbgstr_w(gpus
[gpu
].name
), adapter_count
);
543 for (adapter
= 0; adapter
< adapter_count
; adapter
++)
545 DEVMODEW current_mode
= {.dmSize
= sizeof(current_mode
)};
548 x11drv_settings_id settings_id
;
549 BOOL is_primary
= adapters
[adapter
].state_flags
& DISPLAY_DEVICE_PRIMARY_DEVICE
;
551 device_manager
->add_adapter( &adapters
[adapter
], param
);
553 if (!host_handler
.get_monitors( adapters
[adapter
].id
, &monitors
, &monitor_count
)) break;
554 TRACE("adapter: %#lx, monitor count: %d\n", adapters
[adapter
].id
, monitor_count
);
556 /* Initialize monitors */
557 for (monitor
= 0; monitor
< monitor_count
; monitor
++)
558 device_manager
->add_monitor( &monitors
[monitor
], param
);
560 host_handler
.free_monitors( monitors
, monitor_count
);
562 /* Get the settings handler id for the adapter */
563 snprintf( buffer
, sizeof(buffer
), "\\\\.\\DISPLAY%d", adapter
+ 1 );
564 asciiz_to_unicode( devname
, buffer
);
565 if (!settings_handler
.get_id( devname
, is_primary
, &settings_id
)) break;
567 settings_handler
.get_current_mode( settings_id
, ¤t_mode
);
568 if (!settings_handler
.get_modes( settings_id
, EDS_ROTATEDMODE
, &modes
, &mode_count
))
571 for (mode
= modes
; mode_count
; mode_count
--)
573 if (is_same_devmode( mode
, ¤t_mode
))
575 TRACE( "current mode: %s\n", debugstr_devmodew( ¤t_mode
) );
576 device_manager
->add_mode( ¤t_mode
, TRUE
, param
);
580 TRACE( "mode: %s\n", debugstr_devmodew( mode
) );
581 device_manager
->add_mode( mode
, FALSE
, param
);
583 mode
= (DEVMODEW
*)((char *)mode
+ sizeof(*modes
) + modes
[0].dmDriverExtra
);
586 settings_handler
.free_modes( modes
);
589 host_handler
.free_adapters( adapters
);
592 host_handler
.free_gpus( gpus
);
596 void X11DRV_DisplayDevices_Init(BOOL force
)
598 UINT32 num_path
, num_mode
;
600 if (force
) force_display_devices_refresh
= TRUE
;
601 /* trigger refresh in win32u */
602 NtUserGetDisplayConfigBufferSizes( QDC_ONLY_ACTIVE_PATHS
, &num_path
, &num_mode
);