2 * Wine X11drv Xrandr interface
4 * Copyright 2003 Alexander James Pasadyn
5 * Copyright 2012 Henri Verbeet for CodeWeavers
6 * Copyright 2019 Zhiyi Zhang for CodeWeavers
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "wine/port.h"
26 #define NONAMELESSSTRUCT
27 #define NONAMELESSUNION
29 #include "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(xrandr
);
32 #ifdef HAVE_XRRGETPROVIDERRESOURCES
33 WINE_DECLARE_DEBUG_CHANNEL(winediag
);
36 #ifdef SONAME_LIBXRANDR
40 #include <X11/extensions/Xrandr.h>
43 #define VK_NO_PROTOTYPES
46 #include "wine/heap.h"
47 #include "wine/unicode.h"
48 #include "wine/vulkan.h"
49 #include "wine/vulkan_driver.h"
51 static void *xrandr_handle
;
53 #define MAKE_FUNCPTR(f) static typeof(f) * p##f;
54 MAKE_FUNCPTR(XRRConfigCurrentConfiguration
)
55 MAKE_FUNCPTR(XRRConfigCurrentRate
)
56 MAKE_FUNCPTR(XRRFreeScreenConfigInfo
)
57 MAKE_FUNCPTR(XRRGetScreenInfo
)
58 MAKE_FUNCPTR(XRRQueryExtension
)
59 MAKE_FUNCPTR(XRRQueryVersion
)
60 MAKE_FUNCPTR(XRRRates
)
61 MAKE_FUNCPTR(XRRSetScreenConfig
)
62 MAKE_FUNCPTR(XRRSetScreenConfigAndRate
)
63 MAKE_FUNCPTR(XRRSizes
)
65 #ifdef HAVE_XRRGETPROVIDERRESOURCES
66 MAKE_FUNCPTR(XRRFreeCrtcInfo
)
67 MAKE_FUNCPTR(XRRFreeOutputInfo
)
68 MAKE_FUNCPTR(XRRFreeScreenResources
)
69 MAKE_FUNCPTR(XRRGetCrtcInfo
)
70 MAKE_FUNCPTR(XRRGetOutputInfo
)
71 MAKE_FUNCPTR(XRRGetScreenResources
)
72 MAKE_FUNCPTR(XRRGetScreenResourcesCurrent
)
73 MAKE_FUNCPTR(XRRGetScreenSizeRange
)
74 MAKE_FUNCPTR(XRRSetCrtcConfig
)
75 MAKE_FUNCPTR(XRRSetScreenSize
)
76 MAKE_FUNCPTR(XRRSelectInput
)
77 MAKE_FUNCPTR(XRRGetOutputPrimary
)
78 MAKE_FUNCPTR(XRRGetProviderResources
)
79 MAKE_FUNCPTR(XRRFreeProviderResources
)
80 MAKE_FUNCPTR(XRRGetProviderInfo
)
81 MAKE_FUNCPTR(XRRFreeProviderInfo
)
86 static int load_xrandr(void)
90 if (dlopen(SONAME_LIBXRENDER
, RTLD_NOW
|RTLD_GLOBAL
) &&
91 (xrandr_handle
= dlopen(SONAME_LIBXRANDR
, RTLD_NOW
)))
94 #define LOAD_FUNCPTR(f) \
95 if((p##f = dlsym(xrandr_handle, #f)) == NULL) goto sym_not_found
97 LOAD_FUNCPTR(XRRConfigCurrentConfiguration
);
98 LOAD_FUNCPTR(XRRConfigCurrentRate
);
99 LOAD_FUNCPTR(XRRFreeScreenConfigInfo
);
100 LOAD_FUNCPTR(XRRGetScreenInfo
);
101 LOAD_FUNCPTR(XRRQueryExtension
);
102 LOAD_FUNCPTR(XRRQueryVersion
);
103 LOAD_FUNCPTR(XRRRates
);
104 LOAD_FUNCPTR(XRRSetScreenConfig
);
105 LOAD_FUNCPTR(XRRSetScreenConfigAndRate
);
106 LOAD_FUNCPTR(XRRSizes
);
109 #ifdef HAVE_XRRGETPROVIDERRESOURCES
110 LOAD_FUNCPTR(XRRFreeCrtcInfo
);
111 LOAD_FUNCPTR(XRRFreeOutputInfo
);
112 LOAD_FUNCPTR(XRRFreeScreenResources
);
113 LOAD_FUNCPTR(XRRGetCrtcInfo
);
114 LOAD_FUNCPTR(XRRGetOutputInfo
);
115 LOAD_FUNCPTR(XRRGetScreenResources
);
116 LOAD_FUNCPTR(XRRGetScreenResourcesCurrent
);
117 LOAD_FUNCPTR(XRRGetScreenSizeRange
);
118 LOAD_FUNCPTR(XRRSetCrtcConfig
);
119 LOAD_FUNCPTR(XRRSetScreenSize
);
120 LOAD_FUNCPTR(XRRSelectInput
);
121 LOAD_FUNCPTR(XRRGetOutputPrimary
);
122 LOAD_FUNCPTR(XRRGetProviderResources
);
123 LOAD_FUNCPTR(XRRFreeProviderResources
);
124 LOAD_FUNCPTR(XRRGetProviderInfo
);
125 LOAD_FUNCPTR(XRRFreeProviderInfo
);
132 if (!r
) TRACE("Unable to load function ptrs from XRandR library\n");
137 static int XRandRErrorHandler(Display
*dpy
, XErrorEvent
*event
, void *arg
)
142 /* XRandR 1.0 display settings handler */
143 static BOOL
xrandr10_get_id( const WCHAR
*device_name
, ULONG_PTR
*id
)
145 WCHAR primary_adapter
[CCHDEVICENAME
];
147 if (!get_primary_adapter( primary_adapter
))
150 /* RandR 1.0 only supports changing the primary adapter settings.
151 * For non-primary adapters, an id is still provided but getting
152 * and changing non-primary adapters' settings will be ignored. */
153 *id
= !lstrcmpiW( device_name
, primary_adapter
) ? 1 : 0;
157 static void add_xrandr10_mode( DEVMODEW
*mode
, DWORD depth
, DWORD width
, DWORD height
,
158 DWORD frequency
, SizeID size_id
)
160 mode
->dmSize
= sizeof(*mode
);
161 mode
->dmDriverExtra
= sizeof(SizeID
);
162 mode
->dmFields
= DM_DISPLAYORIENTATION
| DM_BITSPERPEL
| DM_PELSWIDTH
|
163 DM_PELSHEIGHT
| DM_DISPLAYFLAGS
;
166 mode
->dmFields
|= DM_DISPLAYFREQUENCY
;
167 mode
->dmDisplayFrequency
= frequency
;
169 mode
->u1
.s2
.dmDisplayOrientation
= DMDO_DEFAULT
;
170 mode
->dmBitsPerPel
= depth
;
171 mode
->dmPelsWidth
= width
;
172 mode
->dmPelsHeight
= height
;
173 mode
->u2
.dmDisplayFlags
= 0;
174 memcpy( (BYTE
*)mode
+ sizeof(*mode
), &size_id
, sizeof(size_id
) );
177 static BOOL
xrandr10_get_modes( ULONG_PTR id
, DWORD flags
, DEVMODEW
**new_modes
, UINT
*new_mode_count
)
179 INT size_idx
, depth_idx
, rate_idx
, mode_idx
= 0;
180 INT size_count
, rate_count
, mode_count
= 0;
181 DEVMODEW
*modes
, *mode
;
182 XRRScreenSize
*sizes
;
185 sizes
= pXRRSizes( gdi_display
, DefaultScreen( gdi_display
), &size_count
);
189 for (size_idx
= 0; size_idx
< size_count
; ++size_idx
)
191 rates
= pXRRRates( gdi_display
, DefaultScreen( gdi_display
), size_idx
, &rate_count
);
193 mode_count
+= rate_count
;
198 /* Allocate space for reported modes in three depths, and put an SizeID at the end of DEVMODEW as
199 * driver private data */
200 modes
= heap_calloc( mode_count
* DEPTH_COUNT
, sizeof(*modes
) + sizeof(SizeID
) );
203 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
207 for (size_idx
= 0; size_idx
< size_count
; ++size_idx
)
209 for (depth_idx
= 0; depth_idx
< DEPTH_COUNT
; ++depth_idx
)
211 rates
= pXRRRates( gdi_display
, DefaultScreen( gdi_display
), size_idx
, &rate_count
);
214 mode
= (DEVMODEW
*)((BYTE
*)modes
+ (sizeof(*mode
) + sizeof(SizeID
)) * mode_idx
++);
215 add_xrandr10_mode( mode
, depths
[depth_idx
], sizes
[size_idx
].width
,
216 sizes
[size_idx
].height
, 0, size_idx
);
220 for (rate_idx
= 0; rate_idx
< rate_count
; ++rate_idx
)
222 mode
= (DEVMODEW
*)((BYTE
*)modes
+ (sizeof(*mode
) + sizeof(SizeID
)) * mode_idx
++);
223 add_xrandr10_mode( mode
, depths
[depth_idx
], sizes
[size_idx
].width
,
224 sizes
[size_idx
].height
, rates
[rate_idx
], size_idx
);
230 *new_mode_count
= mode_idx
;
234 static void xrandr10_free_modes( DEVMODEW
*modes
)
239 static BOOL
xrandr10_get_current_mode( ULONG_PTR id
, DEVMODEW
*mode
)
241 XRRScreenConfiguration
*screen_config
;
242 XRRScreenSize
*sizes
;
248 mode
->dmFields
= DM_DISPLAYORIENTATION
| DM_BITSPERPEL
| DM_PELSWIDTH
| DM_PELSHEIGHT
|
249 DM_DISPLAYFLAGS
| DM_DISPLAYFREQUENCY
| DM_POSITION
;
250 mode
->u1
.s2
.dmDisplayOrientation
= DMDO_DEFAULT
;
251 mode
->u2
.dmDisplayFlags
= 0;
252 mode
->u1
.s2
.dmPosition
.x
= 0;
253 mode
->u1
.s2
.dmPosition
.y
= 0;
257 FIXME("Non-primary adapters are unsupported.\n");
258 mode
->dmBitsPerPel
= 0;
259 mode
->dmPelsWidth
= 0;
260 mode
->dmPelsHeight
= 0;
261 mode
->dmDisplayFrequency
= 0;
265 sizes
= pXRRSizes( gdi_display
, DefaultScreen( gdi_display
), &size_count
);
269 screen_config
= pXRRGetScreenInfo( gdi_display
, DefaultRootWindow( gdi_display
) );
270 size_id
= pXRRConfigCurrentConfiguration( screen_config
, &rotation
);
271 rate
= pXRRConfigCurrentRate( screen_config
);
272 pXRRFreeScreenConfigInfo( screen_config
);
274 mode
->dmBitsPerPel
= screen_bpp
;
275 mode
->dmPelsWidth
= sizes
[size_id
].width
;
276 mode
->dmPelsHeight
= sizes
[size_id
].height
;
277 mode
->dmDisplayFrequency
= rate
;
281 static LONG
xrandr10_set_current_mode( ULONG_PTR id
, DEVMODEW
*mode
)
283 XRRScreenConfiguration
*screen_config
;
291 FIXME("Non-primary adapters are unsupported.\n");
292 return DISP_CHANGE_SUCCESSFUL
;
295 if (is_detached_mode(mode
))
297 FIXME("Detaching adapters is unsupported.\n");
298 return DISP_CHANGE_SUCCESSFUL
;
301 if (mode
->dmFields
& DM_BITSPERPEL
&& mode
->dmBitsPerPel
!= screen_bpp
)
302 WARN("Cannot change screen bit depth from %dbits to %dbits!\n", screen_bpp
, mode
->dmBitsPerPel
);
304 root
= DefaultRootWindow( gdi_display
);
305 screen_config
= pXRRGetScreenInfo( gdi_display
, root
);
306 pXRRConfigCurrentConfiguration( screen_config
, &rotation
);
308 assert( mode
->dmDriverExtra
== sizeof(SizeID
) );
309 memcpy( &size_id
, (BYTE
*)mode
+ sizeof(*mode
), sizeof(size_id
) );
311 if (mode
->dmFields
& DM_DISPLAYFREQUENCY
&& mode
->dmDisplayFrequency
)
312 stat
= pXRRSetScreenConfigAndRate( gdi_display
, screen_config
, root
, size_id
, rotation
,
313 mode
->dmDisplayFrequency
, CurrentTime
);
315 stat
= pXRRSetScreenConfig( gdi_display
, screen_config
, root
, size_id
, rotation
, CurrentTime
);
316 pXRRFreeScreenConfigInfo( screen_config
);
318 if (stat
!= RRSetConfigSuccess
)
319 return DISP_CHANGE_FAILED
;
321 XFlush( gdi_display
);
322 return DISP_CHANGE_SUCCESSFUL
;
325 #ifdef HAVE_XRRGETPROVIDERRESOURCES
327 static XRRScreenResources
*xrandr_get_screen_resources(void)
329 XRRScreenResources
*resources
= pXRRGetScreenResourcesCurrent( gdi_display
, root_window
);
330 if (resources
&& !resources
->ncrtc
)
332 pXRRFreeScreenResources( resources
);
333 resources
= pXRRGetScreenResources( gdi_display
, root_window
);
337 ERR("Failed to get screen resources.\n");
341 /* Some (304.64, possibly earlier) versions of the NVIDIA driver only
342 * report a DFP's native mode through RandR 1.2 / 1.3. Standard DMT modes
343 * are only listed through RandR 1.0 / 1.1. This is completely useless,
344 * but NVIDIA considers this a feature, so it's unlikely to change. The
345 * best we can do is to fall back to RandR 1.0 and encourage users to
346 * consider more cooperative driver vendors when we detect such a
348 static BOOL
is_broken_driver(void)
350 XRRScreenResources
*screen_resources
;
351 XRROutputInfo
*output_info
;
352 XRRModeInfo
*first_mode
;
353 INT major
, event
, error
;
354 INT output_idx
, i
, j
;
357 screen_resources
= xrandr_get_screen_resources();
358 if (!screen_resources
)
361 /* Check if any output only has one native mode */
362 for (output_idx
= 0; output_idx
< screen_resources
->noutput
; ++output_idx
)
364 output_info
= pXRRGetOutputInfo( gdi_display
, screen_resources
,
365 screen_resources
->outputs
[output_idx
] );
369 if (output_info
->connection
!= RR_Connected
)
371 pXRRFreeOutputInfo( output_info
);
376 only_one_mode
= TRUE
;
377 for (i
= 0; i
< output_info
->nmode
; ++i
)
379 for (j
= 0; j
< screen_resources
->nmode
; ++j
)
381 if (output_info
->modes
[i
] != screen_resources
->modes
[j
].id
)
386 first_mode
= &screen_resources
->modes
[j
];
390 if (first_mode
->width
!= screen_resources
->modes
[j
].width
||
391 first_mode
->height
!= screen_resources
->modes
[j
].height
)
392 only_one_mode
= FALSE
;
400 pXRRFreeOutputInfo( output_info
);
405 /* Check if it is NVIDIA proprietary driver */
406 if (XQueryExtension( gdi_display
, "NV-CONTROL", &major
, &event
, &error
))
408 ERR_(winediag
)("Broken NVIDIA RandR detected, falling back to RandR 1.0. "
409 "Please consider using the Nouveau driver instead.\n");
410 pXRRFreeScreenResources( screen_resources
);
414 pXRRFreeScreenResources( screen_resources
);
418 static void get_screen_size( XRRScreenResources
*resources
, unsigned int *width
, unsigned int *height
)
420 int min_width
= 0, min_height
= 0, max_width
, max_height
;
421 XRRCrtcInfo
*crtc_info
;
424 pXRRGetScreenSizeRange( gdi_display
, root_window
, &min_width
, &min_height
, &max_width
, &max_height
);
426 *height
= min_height
;
428 for (i
= 0; i
< resources
->ncrtc
; ++i
)
430 if (!(crtc_info
= pXRRGetCrtcInfo( gdi_display
, resources
, resources
->crtcs
[i
] )))
433 if (crtc_info
->mode
!= None
)
435 *width
= max(*width
, crtc_info
->x
+ crtc_info
->width
);
436 *height
= max(*height
, crtc_info
->y
+ crtc_info
->height
);
439 pXRRFreeCrtcInfo( crtc_info
);
443 static void set_screen_size( int width
, int height
)
445 int screen
= default_visual
.screen
;
446 int mm_width
, mm_height
;
448 mm_width
= width
* DisplayWidthMM( gdi_display
, screen
) / DisplayWidth( gdi_display
, screen
);
449 mm_height
= height
* DisplayHeightMM( gdi_display
, screen
) / DisplayHeight( gdi_display
, screen
);
450 pXRRSetScreenSize( gdi_display
, root_window
, width
, height
, mm_width
, mm_height
);
453 static unsigned int get_frequency( const XRRModeInfo
*mode
)
455 unsigned int dots
= mode
->hTotal
* mode
->vTotal
;
460 if (mode
->modeFlags
& RR_DoubleScan
)
462 if (mode
->modeFlags
& RR_Interlace
)
465 return (mode
->dotClock
+ dots
/ 2) / dots
;
468 static DWORD
get_orientation( Rotation rotation
)
470 if (rotation
& RR_Rotate_270
) return DMDO_270
;
471 if (rotation
& RR_Rotate_180
) return DMDO_180
;
472 if (rotation
& RR_Rotate_90
) return DMDO_90
;
476 static DWORD
get_orientation_count( Rotation rotations
)
480 if (rotations
& RR_Rotate_0
) ++count
;
481 if (rotations
& RR_Rotate_90
) ++count
;
482 if (rotations
& RR_Rotate_180
) ++count
;
483 if (rotations
& RR_Rotate_270
) ++count
;
487 static Rotation
get_rotation( DWORD orientation
)
489 return (Rotation
)(1 << orientation
);
492 static RRCrtc
get_output_free_crtc( XRRScreenResources
*resources
, XRROutputInfo
*output_info
)
494 XRRCrtcInfo
*crtc_info
;
498 for (crtc_idx
= 0; crtc_idx
< output_info
->ncrtc
; ++crtc_idx
)
500 crtc_info
= pXRRGetCrtcInfo( gdi_display
, resources
, output_info
->crtcs
[crtc_idx
] );
504 if (!crtc_info
->noutput
)
506 crtc
= output_info
->crtcs
[crtc_idx
];
507 pXRRFreeCrtcInfo( crtc_info
);
511 pXRRFreeCrtcInfo( crtc_info
);
517 static RECT
get_primary_rect( XRRScreenResources
*resources
)
519 XRROutputInfo
*output_info
= NULL
;
520 XRRCrtcInfo
*crtc_info
= NULL
;
521 RROutput primary_output
;
522 RECT primary_rect
= {0};
523 RECT first_rect
= {0};
526 primary_output
= pXRRGetOutputPrimary( gdi_display
, root_window
);
530 output_info
= pXRRGetOutputInfo( gdi_display
, resources
, primary_output
);
531 if (!output_info
|| output_info
->connection
!= RR_Connected
|| !output_info
->crtc
)
534 crtc_info
= pXRRGetCrtcInfo( gdi_display
, resources
, output_info
->crtc
);
535 if (!crtc_info
|| !crtc_info
->mode
)
538 SetRect( &primary_rect
, crtc_info
->x
, crtc_info
->y
, crtc_info
->x
+ crtc_info
->width
, crtc_info
->y
+ crtc_info
->height
);
539 pXRRFreeCrtcInfo( crtc_info
);
540 pXRRFreeOutputInfo( output_info
);
543 /* Fallback when XRandR primary output is a disconnected output.
544 * Try to find a crtc with (x, y) being (0, 0). If it's found then get the primary rect from that crtc,
545 * otherwise use the first active crtc to get the primary rect */
548 pXRRFreeCrtcInfo( crtc_info
);
550 pXRRFreeOutputInfo( output_info
);
552 WARN("Primary is set to a disconnected XRandR output.\n");
553 for (i
= 0; i
< resources
->ncrtc
; ++i
)
555 crtc_info
= pXRRGetCrtcInfo( gdi_display
, resources
, resources
->crtcs
[i
] );
559 if (!crtc_info
->mode
)
561 pXRRFreeCrtcInfo( crtc_info
);
565 if (!crtc_info
->x
&& !crtc_info
->y
)
567 SetRect( &primary_rect
, 0, 0, crtc_info
->width
, crtc_info
->height
);
568 pXRRFreeCrtcInfo( crtc_info
);
572 if (IsRectEmpty( &first_rect
))
573 SetRect( &first_rect
, crtc_info
->x
, crtc_info
->y
,
574 crtc_info
->x
+ crtc_info
->width
, crtc_info
->y
+ crtc_info
->height
);
576 pXRRFreeCrtcInfo( crtc_info
);
579 return IsRectEmpty( &primary_rect
) ? first_rect
: primary_rect
;
582 static BOOL
is_crtc_primary( RECT primary
, const XRRCrtcInfo
*crtc
)
586 crtc
->x
== primary
.left
&&
587 crtc
->y
== primary
.top
&&
588 crtc
->x
+ crtc
->width
== primary
.right
&&
589 crtc
->y
+ crtc
->height
== primary
.bottom
;
592 VK_DEFINE_NON_DISPATCHABLE_HANDLE(VkDisplayKHR
)
594 static BOOL
get_gpu_properties_from_vulkan( struct x11drv_gpu
*gpu
, const XRRProviderInfo
*provider_info
)
596 static const char *extensions
[] =
598 VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME
,
599 "VK_EXT_acquire_xlib_display",
600 "VK_EXT_direct_mode_display",
602 const struct vulkan_funcs
*vulkan_funcs
= get_vulkan_driver( WINE_VULKAN_DRIVER_VERSION
);
603 VkResult (*pvkGetRandROutputDisplayEXT
)( VkPhysicalDevice
, Display
*, RROutput
, VkDisplayKHR
* );
604 PFN_vkGetPhysicalDeviceProperties2KHR pvkGetPhysicalDeviceProperties2KHR
;
605 PFN_vkEnumeratePhysicalDevices pvkEnumeratePhysicalDevices
;
606 uint32_t device_count
, device_idx
, output_idx
;
607 VkPhysicalDevice
*vk_physical_devices
= NULL
;
608 VkPhysicalDeviceProperties2 properties2
;
609 VkInstanceCreateInfo create_info
;
610 VkPhysicalDeviceIDProperties id
;
611 VkInstance vk_instance
= NULL
;
612 VkDisplayKHR vk_display
;
619 memset( &create_info
, 0, sizeof(create_info
) );
620 create_info
.sType
= VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO
;
621 create_info
.enabledExtensionCount
= ARRAY_SIZE(extensions
);
622 create_info
.ppEnabledExtensionNames
= extensions
;
624 vr
= vulkan_funcs
->p_vkCreateInstance( &create_info
, NULL
, &vk_instance
);
625 if (vr
!= VK_SUCCESS
)
627 WARN("Failed to create a Vulkan instance, vr %d.\n", vr
);
631 #define LOAD_VK_FUNC(f) \
632 if (!(p##f = (void *)vulkan_funcs->p_vkGetInstanceProcAddr( vk_instance, #f ))) \
634 WARN("Failed to load " #f ".\n"); \
638 LOAD_VK_FUNC(vkEnumeratePhysicalDevices
)
639 LOAD_VK_FUNC(vkGetPhysicalDeviceProperties2KHR
)
640 LOAD_VK_FUNC(vkGetRandROutputDisplayEXT
)
643 vr
= pvkEnumeratePhysicalDevices( vk_instance
, &device_count
, NULL
);
644 if (vr
!= VK_SUCCESS
|| !device_count
)
646 WARN("No Vulkan device found, vr %d, device_count %d.\n", vr
, device_count
);
650 if (!(vk_physical_devices
= heap_calloc( device_count
, sizeof(*vk_physical_devices
) )))
653 vr
= pvkEnumeratePhysicalDevices( vk_instance
, &device_count
, vk_physical_devices
);
654 if (vr
!= VK_SUCCESS
)
656 WARN("vkEnumeratePhysicalDevices failed, vr %d.\n", vr
);
660 for (device_idx
= 0; device_idx
< device_count
; ++device_idx
)
662 for (output_idx
= 0; output_idx
< provider_info
->noutputs
; ++output_idx
)
664 X11DRV_expect_error( gdi_display
, XRandRErrorHandler
, NULL
);
665 vr
= pvkGetRandROutputDisplayEXT( vk_physical_devices
[device_idx
], gdi_display
,
666 provider_info
->outputs
[output_idx
], &vk_display
);
667 XSync( gdi_display
, FALSE
);
668 if (X11DRV_check_error() || vr
!= VK_SUCCESS
|| vk_display
== VK_NULL_HANDLE
)
671 memset( &id
, 0, sizeof(id
) );
672 id
.sType
= VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ID_PROPERTIES
;
673 properties2
.sType
= VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2
;
674 properties2
.pNext
= &id
;
676 pvkGetPhysicalDeviceProperties2KHR( vk_physical_devices
[device_idx
], &properties2
);
677 memcpy( &gpu
->vulkan_uuid
, id
.deviceUUID
, sizeof(id
.deviceUUID
) );
678 /* Ignore Khronos vendor IDs */
679 if (properties2
.properties
.vendorID
< 0x10000)
681 gpu
->vendor_id
= properties2
.properties
.vendorID
;
682 gpu
->device_id
= properties2
.properties
.deviceID
;
684 MultiByteToWideChar( CP_UTF8
, 0, properties2
.properties
.deviceName
, -1, gpu
->name
, ARRAY_SIZE(gpu
->name
) );
691 heap_free( vk_physical_devices
);
693 vulkan_funcs
->p_vkDestroyInstance( vk_instance
, NULL
);
697 /* Get a list of GPUs reported by XRandR 1.4. Set get_properties to FALSE if GPU properties are
698 * not needed to avoid unnecessary querying */
699 static BOOL
xrandr14_get_gpus2( struct x11drv_gpu
**new_gpus
, int *count
, BOOL get_properties
)
701 static const WCHAR wine_adapterW
[] = {'W','i','n','e',' ','A','d','a','p','t','e','r',0};
702 struct x11drv_gpu
*gpus
= NULL
;
703 XRRScreenResources
*screen_resources
= NULL
;
704 XRRProviderResources
*provider_resources
= NULL
;
705 XRRProviderInfo
*provider_info
= NULL
;
706 XRRCrtcInfo
*crtc_info
= NULL
;
707 INT primary_provider
= -1;
712 screen_resources
= xrandr_get_screen_resources();
713 if (!screen_resources
)
716 provider_resources
= pXRRGetProviderResources( gdi_display
, root_window
);
717 if (!provider_resources
)
720 gpus
= heap_calloc( provider_resources
->nproviders
? provider_resources
->nproviders
: 1, sizeof(*gpus
) );
724 /* Some XRandR implementations don't support providers.
725 * In this case, report a fake one to try searching adapters in screen resources */
726 if (!provider_resources
->nproviders
)
728 WARN("XRandR implementation doesn't report any providers, faking one.\n");
729 lstrcpyW( gpus
[0].name
, wine_adapterW
);
736 primary_rect
= get_primary_rect( screen_resources
);
737 for (i
= 0; i
< provider_resources
->nproviders
; ++i
)
739 provider_info
= pXRRGetProviderInfo( gdi_display
, screen_resources
, provider_resources
->providers
[i
] );
743 /* Find primary provider */
744 for (j
= 0; primary_provider
== -1 && j
< provider_info
->ncrtcs
; ++j
)
746 crtc_info
= pXRRGetCrtcInfo( gdi_display
, screen_resources
, provider_info
->crtcs
[j
] );
750 if (is_crtc_primary( primary_rect
, crtc_info
))
752 primary_provider
= i
;
753 pXRRFreeCrtcInfo( crtc_info
);
757 pXRRFreeCrtcInfo( crtc_info
);
760 gpus
[i
].id
= provider_resources
->providers
[i
];
763 if (!get_gpu_properties_from_vulkan( &gpus
[i
], provider_info
))
764 MultiByteToWideChar( CP_UTF8
, 0, provider_info
->name
, -1, gpus
[i
].name
, ARRAY_SIZE(gpus
[i
].name
) );
765 /* FIXME: Add an alternate method of getting PCI IDs, for systems that don't support Vulkan */
767 pXRRFreeProviderInfo( provider_info
);
770 /* Make primary GPU the first */
771 if (primary_provider
> 0)
773 struct x11drv_gpu tmp
= gpus
[0];
774 gpus
[0] = gpus
[primary_provider
];
775 gpus
[primary_provider
] = tmp
;
779 *count
= provider_resources
->nproviders
;
782 if (provider_resources
)
783 pXRRFreeProviderResources( provider_resources
);
784 if (screen_resources
)
785 pXRRFreeScreenResources( screen_resources
);
789 ERR("Failed to get gpus\n");
794 static BOOL
xrandr14_get_gpus( struct x11drv_gpu
**new_gpus
, int *count
)
796 return xrandr14_get_gpus2( new_gpus
, count
, TRUE
);
799 static void xrandr14_free_gpus( struct x11drv_gpu
*gpus
)
804 static BOOL
xrandr14_get_adapters( ULONG_PTR gpu_id
, struct x11drv_adapter
**new_adapters
, int *count
)
806 struct x11drv_adapter
*adapters
= NULL
;
807 XRRScreenResources
*screen_resources
= NULL
;
808 XRRProviderInfo
*provider_info
= NULL
;
809 XRRCrtcInfo
*enum_crtc_info
, *crtc_info
= NULL
;
810 XRROutputInfo
*enum_output_info
, *output_info
= NULL
;
812 INT crtc_count
, output_count
;
813 INT primary_adapter
= 0;
814 INT adapter_count
= 0;
815 BOOL mirrored
, detached
;
820 screen_resources
= xrandr_get_screen_resources();
821 if (!screen_resources
)
826 provider_info
= pXRRGetProviderInfo( gdi_display
, screen_resources
, gpu_id
);
830 crtc_count
= provider_info
->ncrtcs
;
831 output_count
= provider_info
->noutputs
;
832 outputs
= provider_info
->outputs
;
834 /* Fake provider id, search adapters in screen resources */
837 crtc_count
= screen_resources
->ncrtc
;
838 output_count
= screen_resources
->noutput
;
839 outputs
= screen_resources
->outputs
;
842 /* Actual adapter count could be less */
843 adapters
= heap_calloc( crtc_count
, sizeof(*adapters
) );
847 primary_rect
= get_primary_rect( screen_resources
);
848 for (i
= 0; i
< output_count
; ++i
)
850 output_info
= pXRRGetOutputInfo( gdi_display
, screen_resources
, outputs
[i
] );
854 /* Only connected output are considered as monitors */
855 if (output_info
->connection
!= RR_Connected
)
857 pXRRFreeOutputInfo( output_info
);
862 /* Connected output doesn't mean the output is attached to a crtc */
864 if (output_info
->crtc
)
866 crtc_info
= pXRRGetCrtcInfo( gdi_display
, screen_resources
, output_info
->crtc
);
871 if (!output_info
->crtc
|| !crtc_info
->mode
)
874 /* Ignore mirroring output replicas because mirrored monitors are under the same adapter */
878 for (j
= 0; j
< screen_resources
->noutput
; ++j
)
880 enum_output_info
= pXRRGetOutputInfo( gdi_display
, screen_resources
, screen_resources
->outputs
[j
] );
881 if (!enum_output_info
)
884 if (enum_output_info
->connection
!= RR_Connected
|| !enum_output_info
->crtc
)
886 pXRRFreeOutputInfo( enum_output_info
);
890 enum_crtc_info
= pXRRGetCrtcInfo( gdi_display
, screen_resources
, enum_output_info
->crtc
);
891 pXRRFreeOutputInfo( enum_output_info
);
895 /* Some outputs may have the same coordinates, aka mirrored. Choose the output with
896 * the lowest value as primary and the rest will then be replicas in a mirroring set */
897 if (crtc_info
->x
== enum_crtc_info
->x
&&
898 crtc_info
->y
== enum_crtc_info
->y
&&
899 crtc_info
->width
== enum_crtc_info
->width
&&
900 crtc_info
->height
== enum_crtc_info
->height
&&
901 outputs
[i
] > screen_resources
->outputs
[j
])
904 pXRRFreeCrtcInfo( enum_crtc_info
);
908 pXRRFreeCrtcInfo( enum_crtc_info
);
912 if (!mirrored
|| detached
)
914 /* Use RROutput as adapter id. The reason of not using RRCrtc is that we need to detect inactive but
915 * attached monitors */
916 adapters
[adapter_count
].id
= outputs
[i
];
918 adapters
[adapter_count
].state_flags
|= DISPLAY_DEVICE_ATTACHED_TO_DESKTOP
;
919 if (is_crtc_primary( primary_rect
, crtc_info
))
921 adapters
[adapter_count
].state_flags
|= DISPLAY_DEVICE_PRIMARY_DEVICE
;
922 primary_adapter
= adapter_count
;
928 pXRRFreeOutputInfo( output_info
);
932 pXRRFreeCrtcInfo( crtc_info
);
937 /* Make primary adapter the first */
940 struct x11drv_adapter tmp
= adapters
[0];
941 adapters
[0] = adapters
[primary_adapter
];
942 adapters
[primary_adapter
] = tmp
;
945 *new_adapters
= adapters
;
946 *count
= adapter_count
;
949 if (screen_resources
)
950 pXRRFreeScreenResources( screen_resources
);
952 pXRRFreeProviderInfo( provider_info
);
954 pXRRFreeOutputInfo( output_info
);
956 pXRRFreeCrtcInfo( crtc_info
);
959 heap_free( adapters
);
960 ERR("Failed to get adapters\n");
965 static void xrandr14_free_adapters( struct x11drv_adapter
*adapters
)
967 heap_free( adapters
);
970 static BOOL
xrandr14_get_monitors( ULONG_PTR adapter_id
, struct x11drv_monitor
**new_monitors
, int *count
)
972 static const WCHAR generic_nonpnp_monitorW
[] = {
973 'G','e','n','e','r','i','c',' ',
974 'N','o','n','-','P','n','P',' ','M','o','n','i','t','o','r',0};
975 struct x11drv_monitor
*realloc_monitors
, *monitors
= NULL
;
976 XRRScreenResources
*screen_resources
= NULL
;
977 XRROutputInfo
*output_info
= NULL
, *enum_output_info
= NULL
;
978 XRRCrtcInfo
*crtc_info
= NULL
, *enum_crtc_info
;
979 INT primary_index
= 0, monitor_count
= 0, capacity
;
984 screen_resources
= xrandr_get_screen_resources();
985 if (!screen_resources
)
988 /* First start with a 2 monitors, should be enough for most cases */
990 monitors
= heap_calloc( capacity
, sizeof(*monitors
) );
994 output_info
= pXRRGetOutputInfo( gdi_display
, screen_resources
, adapter_id
);
998 if (output_info
->crtc
)
1000 crtc_info
= pXRRGetCrtcInfo( gdi_display
, screen_resources
, output_info
->crtc
);
1005 /* Inactive but attached monitor, no need to check for mirrored/replica monitors */
1006 if (!output_info
->crtc
|| !crtc_info
->mode
)
1008 lstrcpyW( monitors
[monitor_count
].name
, generic_nonpnp_monitorW
);
1009 monitors
[monitor_count
].state_flags
= DISPLAY_DEVICE_ATTACHED
;
1012 /* Active monitors, need to find other monitors with the same coordinates as mirrored */
1015 primary_rect
= get_primary_rect( screen_resources
);
1017 for (i
= 0; i
< screen_resources
->noutput
; ++i
)
1019 enum_output_info
= pXRRGetOutputInfo( gdi_display
, screen_resources
, screen_resources
->outputs
[i
] );
1020 if (!enum_output_info
)
1023 /* Detached outputs don't count */
1024 if (enum_output_info
->connection
!= RR_Connected
)
1026 pXRRFreeOutputInfo( enum_output_info
);
1027 enum_output_info
= NULL
;
1031 /* Allocate more space if needed */
1032 if (monitor_count
>= capacity
)
1035 realloc_monitors
= heap_realloc( monitors
, capacity
* sizeof(*monitors
) );
1036 if (!realloc_monitors
)
1038 monitors
= realloc_monitors
;
1041 if (enum_output_info
->crtc
)
1043 enum_crtc_info
= pXRRGetCrtcInfo( gdi_display
, screen_resources
, enum_output_info
->crtc
);
1044 if (!enum_crtc_info
)
1047 if (enum_crtc_info
->x
== crtc_info
->x
&&
1048 enum_crtc_info
->y
== crtc_info
->y
&&
1049 enum_crtc_info
->width
== crtc_info
->width
&&
1050 enum_crtc_info
->height
== crtc_info
->height
)
1052 /* FIXME: Read output EDID property and parse the data to get the correct name */
1053 lstrcpyW( monitors
[monitor_count
].name
, generic_nonpnp_monitorW
);
1055 SetRect( &monitors
[monitor_count
].rc_monitor
, crtc_info
->x
, crtc_info
->y
,
1056 crtc_info
->x
+ crtc_info
->width
, crtc_info
->y
+ crtc_info
->height
);
1057 monitors
[monitor_count
].rc_work
= get_work_area( &monitors
[monitor_count
].rc_monitor
);
1059 monitors
[monitor_count
].state_flags
= DISPLAY_DEVICE_ATTACHED
;
1060 if (!IsRectEmpty( &monitors
[monitor_count
].rc_monitor
))
1061 monitors
[monitor_count
].state_flags
|= DISPLAY_DEVICE_ACTIVE
;
1063 if (is_crtc_primary( primary_rect
, crtc_info
))
1064 primary_index
= monitor_count
;
1068 pXRRFreeCrtcInfo( enum_crtc_info
);
1071 pXRRFreeOutputInfo( enum_output_info
);
1072 enum_output_info
= NULL
;
1075 /* Make sure the first monitor is the primary */
1078 struct x11drv_monitor tmp
= monitors
[0];
1079 monitors
[0] = monitors
[primary_index
];
1080 monitors
[primary_index
] = tmp
;
1083 /* Make sure the primary monitor origin is at (0, 0) */
1084 for (i
= 0; i
< monitor_count
; i
++)
1086 OffsetRect( &monitors
[i
].rc_monitor
, -primary_rect
.left
, -primary_rect
.top
);
1087 OffsetRect( &monitors
[i
].rc_work
, -primary_rect
.left
, -primary_rect
.top
);
1091 *new_monitors
= monitors
;
1092 *count
= monitor_count
;
1095 if (screen_resources
)
1096 pXRRFreeScreenResources( screen_resources
);
1098 pXRRFreeOutputInfo( output_info
);
1100 pXRRFreeCrtcInfo( crtc_info
);
1101 if (enum_output_info
)
1102 pXRRFreeOutputInfo( enum_output_info
);
1105 heap_free( monitors
);
1106 ERR("Failed to get monitors\n");
1111 static void xrandr14_free_monitors( struct x11drv_monitor
*monitors
)
1113 heap_free( monitors
);
1116 static BOOL
xrandr14_device_change_handler( HWND hwnd
, XEvent
*event
)
1118 if (hwnd
== GetDesktopWindow() && GetWindowThreadProcessId( hwnd
, NULL
) == GetCurrentThreadId())
1120 /* Don't send a WM_DISPLAYCHANGE message here because this event may be a result from
1121 * ChangeDisplaySettings(). Otherwise, ChangeDisplaySettings() would send multiple
1122 * WM_DISPLAYCHANGE messages instead of just one */
1123 X11DRV_DisplayDevices_Update( FALSE
);
1125 init_registry_display_settings();
1130 static void xrandr14_register_event_handlers(void)
1132 Display
*display
= thread_init_display();
1133 int event_base
, error_base
;
1135 if (!pXRRQueryExtension( display
, &event_base
, &error_base
))
1138 pXRRSelectInput( display
, root_window
,
1139 RRCrtcChangeNotifyMask
| RROutputChangeNotifyMask
| RRProviderChangeNotifyMask
);
1140 X11DRV_register_event_handler( event_base
+ RRNotify_CrtcChange
, xrandr14_device_change_handler
,
1141 "XRandR CrtcChange" );
1142 X11DRV_register_event_handler( event_base
+ RRNotify_OutputChange
, xrandr14_device_change_handler
,
1143 "XRandR OutputChange" );
1144 X11DRV_register_event_handler( event_base
+ RRNotify_ProviderChange
, xrandr14_device_change_handler
,
1145 "XRandR ProviderChange" );
1148 /* XRandR 1.4 display settings handler */
1149 static BOOL
xrandr14_get_id( const WCHAR
*device_name
, ULONG_PTR
*id
)
1151 INT gpu_count
, adapter_count
, display_count
= 0;
1152 INT gpu_idx
, adapter_idx
, display_idx
;
1153 struct x11drv_adapter
*adapters
;
1154 struct x11drv_gpu
*gpus
;
1157 /* Parse \\.\DISPLAY%d */
1158 display_idx
= strtolW( device_name
+ 11, &end
, 10 ) - 1;
1162 if (!xrandr14_get_gpus2( &gpus
, &gpu_count
, FALSE
))
1165 for (gpu_idx
= 0; gpu_idx
< gpu_count
; ++gpu_idx
)
1167 if (!xrandr14_get_adapters( gpus
[gpu_idx
].id
, &adapters
, &adapter_count
))
1169 xrandr14_free_gpus( gpus
);
1173 adapter_idx
= display_idx
- display_count
;
1174 if (adapter_idx
< adapter_count
)
1176 *id
= adapters
[adapter_idx
].id
;
1177 xrandr14_free_adapters( adapters
);
1178 xrandr14_free_gpus( gpus
);
1182 display_count
+= adapter_count
;
1183 xrandr14_free_adapters( adapters
);
1185 xrandr14_free_gpus( gpus
);
1189 static void add_xrandr14_mode( DEVMODEW
*mode
, XRRModeInfo
*info
, DWORD depth
, DWORD frequency
,
1192 mode
->dmSize
= sizeof(*mode
);
1193 mode
->dmDriverExtra
= sizeof(RRMode
);
1194 mode
->dmFields
= DM_DISPLAYORIENTATION
| DM_BITSPERPEL
| DM_PELSWIDTH
|
1195 DM_PELSHEIGHT
| DM_DISPLAYFLAGS
;
1198 mode
->dmFields
|= DM_DISPLAYFREQUENCY
;
1199 mode
->dmDisplayFrequency
= frequency
;
1201 if (orientation
== DMDO_DEFAULT
|| orientation
== DMDO_180
)
1203 mode
->dmPelsWidth
= info
->width
;
1204 mode
->dmPelsHeight
= info
->height
;
1208 mode
->dmPelsWidth
= info
->height
;
1209 mode
->dmPelsHeight
= info
->width
;
1211 mode
->u1
.s2
.dmDisplayOrientation
= orientation
;
1212 mode
->dmBitsPerPel
= depth
;
1213 mode
->u2
.dmDisplayFlags
= 0;
1214 memcpy( (BYTE
*)mode
+ sizeof(*mode
), &info
->id
, sizeof(info
->id
) );
1217 static BOOL
xrandr14_get_modes( ULONG_PTR id
, DWORD flags
, DEVMODEW
**new_modes
, UINT
*mode_count
)
1219 DWORD frequency
, orientation
, orientation_count
;
1220 XRRScreenResources
*screen_resources
;
1221 XRROutputInfo
*output_info
= NULL
;
1222 RROutput output
= (RROutput
)id
;
1223 XRRCrtcInfo
*crtc_info
= NULL
;
1224 UINT depth_idx
, mode_idx
= 0;
1225 XRRModeInfo
*mode_info
;
1226 DEVMODEW
*mode
, *modes
;
1232 screen_resources
= xrandr_get_screen_resources();
1233 if (!screen_resources
)
1236 output_info
= pXRRGetOutputInfo( gdi_display
, screen_resources
, output
);
1240 if (output_info
->connection
!= RR_Connected
)
1248 crtc
= output_info
->crtc
;
1250 crtc
= get_output_free_crtc( screen_resources
, output_info
);
1252 crtc_info
= pXRRGetCrtcInfo( gdi_display
, screen_resources
, crtc
);
1254 /* If the output is connected to a CRTC, use rotations reported by the CRTC */
1257 if (flags
& EDS_ROTATEDMODE
)
1259 rotations
= crtc_info
->rotations
;
1263 /* According to the RandR spec, RRGetCrtcInfo should set the active rotation to Rotate_0
1264 * when a CRTC is disabled. However, some RandR implementations report 0 in this case */
1265 rotations
= (crtc_info
->rotation
& 0xf) ? crtc_info
->rotation
: RR_Rotate_0
;
1268 /* Not connected to CRTC, assume all rotations are supported */
1271 if (flags
& EDS_ROTATEDMODE
)
1273 rotations
= RR_Rotate_0
| RR_Rotate_90
| RR_Rotate_180
| RR_Rotate_270
;
1277 rotations
= RR_Rotate_0
;
1280 orientation_count
= get_orientation_count( rotations
);
1282 /* Allocate space for display modes in different color depths and orientations.
1283 * Store a RRMode at the end of each DEVMODEW as private driver data */
1284 modes
= heap_calloc( output_info
->nmode
* DEPTH_COUNT
* orientation_count
,
1285 sizeof(*modes
) + sizeof(RRMode
) );
1289 for (i
= 0; i
< output_info
->nmode
; ++i
)
1291 for (j
= 0; j
< screen_resources
->nmode
; ++j
)
1293 if (output_info
->modes
[i
] != screen_resources
->modes
[j
].id
)
1296 mode_info
= &screen_resources
->modes
[j
];
1297 frequency
= get_frequency( mode_info
);
1299 for (depth_idx
= 0; depth_idx
< DEPTH_COUNT
; ++depth_idx
)
1301 for (orientation
= DMDO_DEFAULT
; orientation
<= DMDO_270
; ++orientation
)
1303 if (!((1 << orientation
) & rotations
))
1306 mode
= (DEVMODEW
*)((BYTE
*)modes
+ (sizeof(*modes
) + sizeof(RRMode
)) * mode_idx
);
1307 add_xrandr14_mode( mode
, mode_info
, depths
[depth_idx
], frequency
, orientation
);
1318 *mode_count
= mode_idx
;
1321 pXRRFreeCrtcInfo( crtc_info
);
1323 pXRRFreeOutputInfo( output_info
);
1324 if (screen_resources
)
1325 pXRRFreeScreenResources( screen_resources
);
1329 static void xrandr14_free_modes( DEVMODEW
*modes
)
1334 static BOOL
xrandr14_get_current_mode( ULONG_PTR id
, DEVMODEW
*mode
)
1336 XRRScreenResources
*screen_resources
;
1337 XRROutputInfo
*output_info
= NULL
;
1338 RROutput output
= (RROutput
)id
;
1339 XRRModeInfo
*mode_info
= NULL
;
1340 XRRCrtcInfo
*crtc_info
= NULL
;
1345 screen_resources
= xrandr_get_screen_resources();
1346 if (!screen_resources
)
1349 output_info
= pXRRGetOutputInfo( gdi_display
, screen_resources
, output
);
1353 if (output_info
->crtc
)
1355 crtc_info
= pXRRGetCrtcInfo( gdi_display
, screen_resources
, output_info
->crtc
);
1361 if (output_info
->connection
!= RR_Connected
|| !output_info
->crtc
|| !crtc_info
->mode
)
1363 mode
->dmFields
= DM_DISPLAYORIENTATION
| DM_BITSPERPEL
| DM_PELSWIDTH
| DM_PELSHEIGHT
|
1364 DM_DISPLAYFLAGS
| DM_DISPLAYFREQUENCY
| DM_POSITION
;
1365 mode
->u1
.s2
.dmDisplayOrientation
= DMDO_DEFAULT
;
1366 mode
->dmBitsPerPel
= 0;
1367 mode
->dmPelsWidth
= 0;
1368 mode
->dmPelsHeight
= 0;
1369 mode
->u2
.dmDisplayFlags
= 0;
1370 mode
->dmDisplayFrequency
= 0;
1371 mode
->u1
.s2
.dmPosition
.x
= 0;
1372 mode
->u1
.s2
.dmPosition
.y
= 0;
1378 for (mode_idx
= 0; mode_idx
< screen_resources
->nmode
; ++mode_idx
)
1380 if (crtc_info
->mode
== screen_resources
->modes
[mode_idx
].id
)
1382 mode_info
= &screen_resources
->modes
[mode_idx
];
1390 mode
->dmFields
= DM_DISPLAYORIENTATION
| DM_BITSPERPEL
| DM_PELSWIDTH
| DM_PELSHEIGHT
|
1391 DM_DISPLAYFLAGS
| DM_DISPLAYFREQUENCY
| DM_POSITION
;
1392 mode
->u1
.s2
.dmDisplayOrientation
= get_orientation( crtc_info
->rotation
);
1393 mode
->dmBitsPerPel
= screen_bpp
;
1394 mode
->dmPelsWidth
= crtc_info
->width
;
1395 mode
->dmPelsHeight
= crtc_info
->height
;
1396 mode
->u2
.dmDisplayFlags
= 0;
1397 mode
->dmDisplayFrequency
= get_frequency( mode_info
);
1398 /* Convert RandR coordinates to virtual screen coordinates */
1399 primary
= get_primary_rect( screen_resources
);
1400 mode
->u1
.s2
.dmPosition
.x
= crtc_info
->x
- primary
.left
;
1401 mode
->u1
.s2
.dmPosition
.y
= crtc_info
->y
- primary
.top
;
1405 pXRRFreeCrtcInfo( crtc_info
);
1407 pXRRFreeOutputInfo( output_info
);
1408 if (screen_resources
)
1409 pXRRFreeScreenResources( screen_resources
);
1413 static LONG
xrandr14_set_current_mode( ULONG_PTR id
, DEVMODEW
*mode
)
1415 unsigned int screen_width
, screen_height
;
1416 RROutput output
= (RROutput
)id
, *outputs
;
1417 XRRScreenResources
*screen_resources
;
1418 XRROutputInfo
*output_info
= NULL
;
1419 XRRCrtcInfo
*crtc_info
= NULL
;
1420 LONG ret
= DISP_CHANGE_FAILED
;
1427 if (mode
->dmFields
& DM_BITSPERPEL
&& mode
->dmBitsPerPel
!= screen_bpp
)
1428 WARN("Cannot change screen color depth from %ubits to %ubits!\n", screen_bpp
, mode
->dmBitsPerPel
);
1430 screen_resources
= xrandr_get_screen_resources();
1431 if (!screen_resources
)
1434 XGrabServer( gdi_display
);
1436 output_info
= pXRRGetOutputInfo( gdi_display
, screen_resources
, output
);
1437 if (!output_info
|| output_info
->connection
!= RR_Connected
)
1440 if (is_detached_mode(mode
))
1442 /* Already detached */
1443 if (!output_info
->crtc
)
1445 ret
= DISP_CHANGE_SUCCESSFUL
;
1449 /* Execute detach operation */
1450 status
= pXRRSetCrtcConfig( gdi_display
, screen_resources
, output_info
->crtc
,
1451 CurrentTime
, 0, 0, None
, RR_Rotate_0
, NULL
, 0 );
1452 if (status
== RRSetConfigSuccess
)
1454 get_screen_size( screen_resources
, &screen_width
, &screen_height
);
1455 set_screen_size( screen_width
, screen_height
);
1456 ret
= DISP_CHANGE_SUCCESSFUL
;
1462 if (output_info
->crtc
)
1464 crtc
= output_info
->crtc
;
1466 /* Detached, need to find a free CRTC */
1469 if (!(crtc
= get_output_free_crtc( screen_resources
, output_info
)))
1473 crtc_info
= pXRRGetCrtcInfo( gdi_display
, screen_resources
, crtc
);
1477 assert( mode
->dmDriverExtra
== sizeof(RRMode
) );
1478 memcpy( &rrmode
, (BYTE
*)mode
+ sizeof(*mode
), sizeof(rrmode
) );
1480 if (crtc_info
->noutput
)
1482 outputs
= crtc_info
->outputs
;
1483 output_count
= crtc_info
->noutput
;
1490 rotation
= get_rotation( mode
->u1
.s2
.dmDisplayOrientation
);
1492 /* According to the RandR spec, the entire CRTC must fit inside the screen.
1493 * Since we use the union of all enabled CRTCs to determine the necessary
1494 * screen size, this might involve shrinking the screen, so we must disable
1495 * the CRTC in question first. */
1496 status
= pXRRSetCrtcConfig( gdi_display
, screen_resources
, crtc
, CurrentTime
, 0, 0, None
,
1497 RR_Rotate_0
, NULL
, 0 );
1498 if (status
!= RRSetConfigSuccess
)
1501 get_screen_size( screen_resources
, &screen_width
, &screen_height
);
1502 screen_width
= max( screen_width
, mode
->u1
.s2
.dmPosition
.x
+ mode
->dmPelsWidth
);
1503 screen_height
= max( screen_height
, mode
->u1
.s2
.dmPosition
.y
+ mode
->dmPelsHeight
);
1504 set_screen_size( screen_width
, screen_height
);
1506 status
= pXRRSetCrtcConfig( gdi_display
, screen_resources
, crtc
, CurrentTime
,
1507 mode
->u1
.s2
.dmPosition
.x
, mode
->u1
.s2
.dmPosition
.y
, rrmode
,
1508 rotation
, outputs
, output_count
);
1509 if (status
== RRSetConfigSuccess
)
1510 ret
= DISP_CHANGE_SUCCESSFUL
;
1513 XUngrabServer( gdi_display
);
1514 XFlush( gdi_display
);
1516 pXRRFreeCrtcInfo( crtc_info
);
1518 pXRRFreeOutputInfo( output_info
);
1519 pXRRFreeScreenResources( screen_resources
);
1525 void X11DRV_XRandR_Init(void)
1527 struct x11drv_display_device_handler display_handler
;
1528 struct x11drv_settings_handler settings_handler
;
1529 int event_base
, error_base
, minor
, ret
;
1533 if (major
) return; /* already initialized? */
1534 if (!usexrandr
) return; /* disabled in config */
1535 if (is_virtual_desktop()) return;
1536 if (!(ret
= load_xrandr())) return; /* can't load the Xrandr library */
1538 /* see if Xrandr is available */
1539 if (!pXRRQueryExtension( gdi_display
, &event_base
, &error_base
)) return;
1540 X11DRV_expect_error( gdi_display
, XRandRErrorHandler
, NULL
);
1541 ok
= pXRRQueryVersion( gdi_display
, &major
, &minor
);
1542 if (X11DRV_check_error() || !ok
) return;
1544 TRACE("Found XRandR %d.%d.\n", major
, minor
);
1546 settings_handler
.name
= "XRandR 1.0";
1547 settings_handler
.priority
= 200;
1548 settings_handler
.get_id
= xrandr10_get_id
;
1549 settings_handler
.get_modes
= xrandr10_get_modes
;
1550 settings_handler
.free_modes
= xrandr10_free_modes
;
1551 settings_handler
.get_current_mode
= xrandr10_get_current_mode
;
1552 settings_handler
.set_current_mode
= xrandr10_set_current_mode
;
1553 X11DRV_Settings_SetHandler( &settings_handler
);
1555 #ifdef HAVE_XRRGETPROVIDERRESOURCES
1556 if (ret
>= 4 && (major
> 1 || (major
== 1 && minor
>= 4)))
1558 XRRScreenResources
*screen_resources
;
1559 XRROutputInfo
*output_info
;
1560 BOOL found_output
= FALSE
;
1563 screen_resources
= xrandr_get_screen_resources();
1564 if (!screen_resources
)
1567 for (i
= 0; i
< screen_resources
->noutput
; ++i
)
1569 output_info
= pXRRGetOutputInfo( gdi_display
, screen_resources
, screen_resources
->outputs
[i
] );
1573 if (output_info
->connection
== RR_Connected
)
1575 pXRRFreeOutputInfo( output_info
);
1576 found_output
= TRUE
;
1580 pXRRFreeOutputInfo( output_info
);
1582 pXRRFreeScreenResources( screen_resources
);
1586 WARN("No connected outputs found.\n");
1590 display_handler
.name
= "XRandR 1.4";
1591 display_handler
.priority
= 200;
1592 display_handler
.get_gpus
= xrandr14_get_gpus
;
1593 display_handler
.get_adapters
= xrandr14_get_adapters
;
1594 display_handler
.get_monitors
= xrandr14_get_monitors
;
1595 display_handler
.free_gpus
= xrandr14_free_gpus
;
1596 display_handler
.free_adapters
= xrandr14_free_adapters
;
1597 display_handler
.free_monitors
= xrandr14_free_monitors
;
1598 display_handler
.register_event_handlers
= xrandr14_register_event_handlers
;
1599 X11DRV_DisplayDevices_SetHandler( &display_handler
);
1601 if (is_broken_driver())
1604 settings_handler
.name
= "XRandR 1.4";
1605 settings_handler
.priority
= 300;
1606 settings_handler
.get_id
= xrandr14_get_id
;
1607 settings_handler
.get_modes
= xrandr14_get_modes
;
1608 settings_handler
.free_modes
= xrandr14_free_modes
;
1609 settings_handler
.get_current_mode
= xrandr14_get_current_mode
;
1610 settings_handler
.set_current_mode
= xrandr14_set_current_mode
;
1611 X11DRV_Settings_SetHandler( &settings_handler
);
1616 #else /* SONAME_LIBXRANDR */
1618 void X11DRV_XRandR_Init(void)
1620 TRACE("XRandR support not compiled in.\n");
1623 #endif /* SONAME_LIBXRANDR */