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
31 #ifdef HAVE_X11_EXTENSIONS_XRANDR_H
32 #include <X11/extensions/Xrandr.h>
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(xrandr
);
39 #ifdef HAVE_XRRGETPROVIDERRESOURCES
40 WINE_DECLARE_DEBUG_CHANNEL(winediag
);
43 #ifdef SONAME_LIBXRANDR
45 #define VK_NO_PROTOTYPES
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(XRRGetOutputProperty
)
72 MAKE_FUNCPTR(XRRGetScreenResources
)
73 MAKE_FUNCPTR(XRRGetScreenResourcesCurrent
)
74 MAKE_FUNCPTR(XRRGetScreenSizeRange
)
75 MAKE_FUNCPTR(XRRSetCrtcConfig
)
76 MAKE_FUNCPTR(XRRSetScreenSize
)
77 MAKE_FUNCPTR(XRRSelectInput
)
78 MAKE_FUNCPTR(XRRGetOutputPrimary
)
79 MAKE_FUNCPTR(XRRGetProviderResources
)
80 MAKE_FUNCPTR(XRRFreeProviderResources
)
81 MAKE_FUNCPTR(XRRGetProviderInfo
)
82 MAKE_FUNCPTR(XRRFreeProviderInfo
)
87 static int load_xrandr(void)
91 if (dlopen(SONAME_LIBXRENDER
, RTLD_NOW
|RTLD_GLOBAL
) &&
92 (xrandr_handle
= dlopen(SONAME_LIBXRANDR
, RTLD_NOW
)))
95 #define LOAD_FUNCPTR(f) \
96 if((p##f = dlsym(xrandr_handle, #f)) == NULL) goto sym_not_found
98 LOAD_FUNCPTR(XRRConfigCurrentConfiguration
);
99 LOAD_FUNCPTR(XRRConfigCurrentRate
);
100 LOAD_FUNCPTR(XRRFreeScreenConfigInfo
);
101 LOAD_FUNCPTR(XRRGetScreenInfo
);
102 LOAD_FUNCPTR(XRRQueryExtension
);
103 LOAD_FUNCPTR(XRRQueryVersion
);
104 LOAD_FUNCPTR(XRRRates
);
105 LOAD_FUNCPTR(XRRSetScreenConfig
);
106 LOAD_FUNCPTR(XRRSetScreenConfigAndRate
);
107 LOAD_FUNCPTR(XRRSizes
);
110 #ifdef HAVE_XRRGETPROVIDERRESOURCES
111 LOAD_FUNCPTR(XRRFreeCrtcInfo
);
112 LOAD_FUNCPTR(XRRFreeOutputInfo
);
113 LOAD_FUNCPTR(XRRFreeScreenResources
);
114 LOAD_FUNCPTR(XRRGetCrtcInfo
);
115 LOAD_FUNCPTR(XRRGetOutputInfo
);
116 LOAD_FUNCPTR(XRRGetOutputProperty
);
117 LOAD_FUNCPTR(XRRGetScreenResources
);
118 LOAD_FUNCPTR(XRRGetScreenResourcesCurrent
);
119 LOAD_FUNCPTR(XRRGetScreenSizeRange
);
120 LOAD_FUNCPTR(XRRSetCrtcConfig
);
121 LOAD_FUNCPTR(XRRSetScreenSize
);
122 LOAD_FUNCPTR(XRRSelectInput
);
123 LOAD_FUNCPTR(XRRGetOutputPrimary
);
124 LOAD_FUNCPTR(XRRGetProviderResources
);
125 LOAD_FUNCPTR(XRRFreeProviderResources
);
126 LOAD_FUNCPTR(XRRGetProviderInfo
);
127 LOAD_FUNCPTR(XRRFreeProviderInfo
);
134 if (!r
) TRACE("Unable to load function ptrs from XRandR library\n");
139 static int XRandRErrorHandler(Display
*dpy
, XErrorEvent
*event
, void *arg
)
144 /* XRandR 1.0 display settings handler */
145 static BOOL
xrandr10_get_id( const WCHAR
*device_name
, BOOL is_primary
, x11drv_settings_id
*id
)
147 /* RandR 1.0 only supports changing the primary adapter settings.
148 * For non-primary adapters, an id is still provided but getting
149 * and changing non-primary adapters' settings will be ignored. */
150 id
->id
= is_primary
? 1 : 0;
154 static void add_xrandr10_mode( DEVMODEW
*mode
, DWORD depth
, DWORD width
, DWORD height
,
155 DWORD frequency
, SizeID size_id
)
157 mode
->dmSize
= sizeof(*mode
);
158 mode
->dmDriverExtra
= sizeof(SizeID
);
159 mode
->dmFields
= DM_DISPLAYORIENTATION
| DM_BITSPERPEL
| DM_PELSWIDTH
|
160 DM_PELSHEIGHT
| DM_DISPLAYFLAGS
;
163 mode
->dmFields
|= DM_DISPLAYFREQUENCY
;
164 mode
->dmDisplayFrequency
= frequency
;
166 mode
->dmDisplayOrientation
= DMDO_DEFAULT
;
167 mode
->dmBitsPerPel
= depth
;
168 mode
->dmPelsWidth
= width
;
169 mode
->dmPelsHeight
= height
;
170 mode
->dmDisplayFlags
= 0;
171 memcpy( (BYTE
*)mode
+ sizeof(*mode
), &size_id
, sizeof(size_id
) );
174 static BOOL
xrandr10_get_modes( x11drv_settings_id id
, DWORD flags
, DEVMODEW
**new_modes
, UINT
*new_mode_count
)
176 INT size_idx
, depth_idx
, rate_idx
, mode_idx
= 0;
177 INT size_count
, rate_count
, mode_count
= 0;
178 DEVMODEW
*modes
, *mode
;
179 XRRScreenSize
*sizes
;
182 sizes
= pXRRSizes( gdi_display
, DefaultScreen( gdi_display
), &size_count
);
186 for (size_idx
= 0; size_idx
< size_count
; ++size_idx
)
188 rates
= pXRRRates( gdi_display
, DefaultScreen( gdi_display
), size_idx
, &rate_count
);
190 mode_count
+= rate_count
;
195 /* Allocate space for reported modes in three depths, and put an SizeID at the end of DEVMODEW as
196 * driver private data */
197 modes
= calloc( mode_count
* DEPTH_COUNT
, sizeof(*modes
) + sizeof(SizeID
) );
200 RtlSetLastWin32Error( ERROR_NOT_ENOUGH_MEMORY
);
204 for (size_idx
= 0; size_idx
< size_count
; ++size_idx
)
206 for (depth_idx
= 0; depth_idx
< DEPTH_COUNT
; ++depth_idx
)
208 rates
= pXRRRates( gdi_display
, DefaultScreen( gdi_display
), size_idx
, &rate_count
);
211 mode
= (DEVMODEW
*)((BYTE
*)modes
+ (sizeof(*mode
) + sizeof(SizeID
)) * mode_idx
++);
212 add_xrandr10_mode( mode
, depths
[depth_idx
], sizes
[size_idx
].width
,
213 sizes
[size_idx
].height
, 0, size_idx
);
217 for (rate_idx
= 0; rate_idx
< rate_count
; ++rate_idx
)
219 mode
= (DEVMODEW
*)((BYTE
*)modes
+ (sizeof(*mode
) + sizeof(SizeID
)) * mode_idx
++);
220 add_xrandr10_mode( mode
, depths
[depth_idx
], sizes
[size_idx
].width
,
221 sizes
[size_idx
].height
, rates
[rate_idx
], size_idx
);
227 *new_mode_count
= mode_idx
;
231 static void xrandr10_free_modes( DEVMODEW
*modes
)
236 static BOOL
xrandr10_get_current_mode( x11drv_settings_id id
, DEVMODEW
*mode
)
238 XRRScreenConfiguration
*screen_config
;
239 XRRScreenSize
*sizes
;
245 mode
->dmFields
= DM_DISPLAYORIENTATION
| DM_BITSPERPEL
| DM_PELSWIDTH
| DM_PELSHEIGHT
|
246 DM_DISPLAYFLAGS
| DM_DISPLAYFREQUENCY
| DM_POSITION
;
247 mode
->dmDisplayOrientation
= DMDO_DEFAULT
;
248 mode
->dmDisplayFlags
= 0;
249 mode
->dmPosition
.x
= 0;
250 mode
->dmPosition
.y
= 0;
254 FIXME("Non-primary adapters are unsupported.\n");
255 mode
->dmBitsPerPel
= 0;
256 mode
->dmPelsWidth
= 0;
257 mode
->dmPelsHeight
= 0;
258 mode
->dmDisplayFrequency
= 0;
262 sizes
= pXRRSizes( gdi_display
, DefaultScreen( gdi_display
), &size_count
);
266 screen_config
= pXRRGetScreenInfo( gdi_display
, DefaultRootWindow( gdi_display
) );
267 size_id
= pXRRConfigCurrentConfiguration( screen_config
, &rotation
);
268 rate
= pXRRConfigCurrentRate( screen_config
);
269 pXRRFreeScreenConfigInfo( screen_config
);
271 mode
->dmBitsPerPel
= screen_bpp
;
272 mode
->dmPelsWidth
= sizes
[size_id
].width
;
273 mode
->dmPelsHeight
= sizes
[size_id
].height
;
274 mode
->dmDisplayFrequency
= rate
;
278 static LONG
xrandr10_set_current_mode( x11drv_settings_id id
, const DEVMODEW
*mode
)
280 XRRScreenConfiguration
*screen_config
;
288 FIXME("Non-primary adapters are unsupported.\n");
289 return DISP_CHANGE_SUCCESSFUL
;
292 if (is_detached_mode(mode
))
294 FIXME("Detaching adapters is unsupported.\n");
295 return DISP_CHANGE_SUCCESSFUL
;
298 if (mode
->dmFields
& DM_BITSPERPEL
&& mode
->dmBitsPerPel
!= screen_bpp
)
299 WARN("Cannot change screen bit depth from %dbits to %dbits!\n",
300 screen_bpp
, (int)mode
->dmBitsPerPel
);
302 root
= DefaultRootWindow( gdi_display
);
303 screen_config
= pXRRGetScreenInfo( gdi_display
, root
);
304 pXRRConfigCurrentConfiguration( screen_config
, &rotation
);
306 assert( mode
->dmDriverExtra
== sizeof(SizeID
) );
307 memcpy( &size_id
, (BYTE
*)mode
+ sizeof(*mode
), sizeof(size_id
) );
309 if (mode
->dmFields
& DM_DISPLAYFREQUENCY
&& mode
->dmDisplayFrequency
)
310 stat
= pXRRSetScreenConfigAndRate( gdi_display
, screen_config
, root
, size_id
, rotation
,
311 mode
->dmDisplayFrequency
, CurrentTime
);
313 stat
= pXRRSetScreenConfig( gdi_display
, screen_config
, root
, size_id
, rotation
, CurrentTime
);
314 pXRRFreeScreenConfigInfo( screen_config
);
316 if (stat
!= RRSetConfigSuccess
)
317 return DISP_CHANGE_FAILED
;
319 XFlush( gdi_display
);
320 return DISP_CHANGE_SUCCESSFUL
;
323 #ifdef HAVE_XRRGETPROVIDERRESOURCES
325 static struct current_mode
331 static int current_mode_count
;
333 static pthread_mutex_t xrandr_mutex
= PTHREAD_MUTEX_INITIALIZER
;
335 static void xrandr14_invalidate_current_mode_cache(void)
337 pthread_mutex_lock( &xrandr_mutex
);
338 free( current_modes
);
339 current_modes
= NULL
;
340 current_mode_count
= 0;
341 pthread_mutex_unlock( &xrandr_mutex
);
344 static XRRScreenResources
*xrandr_get_screen_resources(void)
346 XRRScreenResources
*resources
= pXRRGetScreenResourcesCurrent( gdi_display
, root_window
);
347 if (resources
&& !resources
->ncrtc
)
349 pXRRFreeScreenResources( resources
);
350 resources
= pXRRGetScreenResources( gdi_display
, root_window
);
354 ERR("Failed to get screen resources.\n");
358 /* Some (304.64, possibly earlier) versions of the NVIDIA driver only
359 * report a DFP's native mode through RandR 1.2 / 1.3. Standard DMT modes
360 * are only listed through RandR 1.0 / 1.1. This is completely useless,
361 * but NVIDIA considers this a feature, so it's unlikely to change. The
362 * best we can do is to fall back to RandR 1.0 and encourage users to
363 * consider more cooperative driver vendors when we detect such a
365 static BOOL
is_broken_driver(void)
367 XRRScreenResources
*screen_resources
;
368 XRROutputInfo
*output_info
;
369 XRRModeInfo
*first_mode
;
370 INT major
, event
, error
;
371 INT output_idx
, i
, j
;
374 screen_resources
= xrandr_get_screen_resources();
375 if (!screen_resources
)
378 /* Check if any output only has one native mode */
379 for (output_idx
= 0; output_idx
< screen_resources
->noutput
; ++output_idx
)
381 output_info
= pXRRGetOutputInfo( gdi_display
, screen_resources
,
382 screen_resources
->outputs
[output_idx
] );
386 if (output_info
->connection
!= RR_Connected
)
388 pXRRFreeOutputInfo( output_info
);
393 only_one_mode
= TRUE
;
394 for (i
= 0; i
< output_info
->nmode
; ++i
)
396 for (j
= 0; j
< screen_resources
->nmode
; ++j
)
398 if (output_info
->modes
[i
] != screen_resources
->modes
[j
].id
)
403 first_mode
= &screen_resources
->modes
[j
];
407 if (first_mode
->width
!= screen_resources
->modes
[j
].width
||
408 first_mode
->height
!= screen_resources
->modes
[j
].height
)
409 only_one_mode
= FALSE
;
417 pXRRFreeOutputInfo( output_info
);
422 /* Check if it is NVIDIA proprietary driver */
423 if (XQueryExtension( gdi_display
, "NV-CONTROL", &major
, &event
, &error
))
425 ERR_(winediag
)("Broken NVIDIA RandR detected, falling back to RandR 1.0. "
426 "Please consider using the Nouveau driver instead.\n");
427 pXRRFreeScreenResources( screen_resources
);
431 pXRRFreeScreenResources( screen_resources
);
435 static void get_screen_size( XRRScreenResources
*resources
, unsigned int *width
, unsigned int *height
)
437 int min_width
= 0, min_height
= 0, max_width
, max_height
;
438 XRRCrtcInfo
*crtc_info
;
441 pXRRGetScreenSizeRange( gdi_display
, root_window
, &min_width
, &min_height
, &max_width
, &max_height
);
443 *height
= min_height
;
445 for (i
= 0; i
< resources
->ncrtc
; ++i
)
447 if (!(crtc_info
= pXRRGetCrtcInfo( gdi_display
, resources
, resources
->crtcs
[i
] )))
450 if (crtc_info
->mode
!= None
)
452 *width
= max(*width
, crtc_info
->x
+ crtc_info
->width
);
453 *height
= max(*height
, crtc_info
->y
+ crtc_info
->height
);
456 pXRRFreeCrtcInfo( crtc_info
);
460 static unsigned int get_edid( RROutput output
, unsigned char **prop
)
462 int result
, actual_format
;
463 unsigned long bytes_after
, len
;
466 result
= pXRRGetOutputProperty( gdi_display
, output
, x11drv_atom(EDID
), 0, 128, FALSE
, FALSE
,
467 AnyPropertyType
, &actual_type
, &actual_format
, &len
,
468 &bytes_after
, prop
);
470 if (result
!= Success
)
472 WARN("Could not retrieve EDID property for output %#lx.\n", output
);
479 static void set_screen_size( int width
, int height
)
481 int screen
= default_visual
.screen
;
482 int mm_width
, mm_height
;
484 mm_width
= width
* DisplayWidthMM( gdi_display
, screen
) / DisplayWidth( gdi_display
, screen
);
485 mm_height
= height
* DisplayHeightMM( gdi_display
, screen
) / DisplayHeight( gdi_display
, screen
);
486 pXRRSetScreenSize( gdi_display
, root_window
, width
, height
, mm_width
, mm_height
);
489 static unsigned int get_frequency( const XRRModeInfo
*mode
)
491 unsigned int dots
= mode
->hTotal
* mode
->vTotal
;
496 if (mode
->modeFlags
& RR_DoubleScan
)
498 if (mode
->modeFlags
& RR_Interlace
)
501 return (mode
->dotClock
+ dots
/ 2) / dots
;
504 static DWORD
get_orientation( Rotation rotation
)
506 if (rotation
& RR_Rotate_270
) return DMDO_270
;
507 if (rotation
& RR_Rotate_180
) return DMDO_180
;
508 if (rotation
& RR_Rotate_90
) return DMDO_90
;
512 static DWORD
get_orientation_count( Rotation rotations
)
516 if (rotations
& RR_Rotate_0
) ++count
;
517 if (rotations
& RR_Rotate_90
) ++count
;
518 if (rotations
& RR_Rotate_180
) ++count
;
519 if (rotations
& RR_Rotate_270
) ++count
;
523 static Rotation
get_rotation( DWORD orientation
)
525 return (Rotation
)(1 << orientation
);
528 static RRCrtc
get_output_free_crtc( XRRScreenResources
*resources
, XRROutputInfo
*output_info
)
530 XRRCrtcInfo
*crtc_info
;
534 for (crtc_idx
= 0; crtc_idx
< output_info
->ncrtc
; ++crtc_idx
)
536 crtc_info
= pXRRGetCrtcInfo( gdi_display
, resources
, output_info
->crtcs
[crtc_idx
] );
540 if (!crtc_info
->noutput
)
542 crtc
= output_info
->crtcs
[crtc_idx
];
543 pXRRFreeCrtcInfo( crtc_info
);
547 pXRRFreeCrtcInfo( crtc_info
);
553 static RECT
get_primary_rect( XRRScreenResources
*resources
)
555 XRROutputInfo
*output_info
= NULL
;
556 XRRCrtcInfo
*crtc_info
= NULL
;
557 RROutput primary_output
;
558 RECT primary_rect
= {0};
559 RECT first_rect
= {0};
562 primary_output
= pXRRGetOutputPrimary( gdi_display
, root_window
);
566 output_info
= pXRRGetOutputInfo( gdi_display
, resources
, primary_output
);
567 if (!output_info
|| output_info
->connection
!= RR_Connected
|| !output_info
->crtc
)
570 crtc_info
= pXRRGetCrtcInfo( gdi_display
, resources
, output_info
->crtc
);
571 if (!crtc_info
|| !crtc_info
->mode
)
574 SetRect( &primary_rect
, crtc_info
->x
, crtc_info
->y
, crtc_info
->x
+ crtc_info
->width
, crtc_info
->y
+ crtc_info
->height
);
575 pXRRFreeCrtcInfo( crtc_info
);
576 pXRRFreeOutputInfo( output_info
);
579 /* Fallback when XRandR primary output is a disconnected output.
580 * Try to find a crtc with (x, y) being (0, 0). If it's found then get the primary rect from that crtc,
581 * otherwise use the first active crtc to get the primary rect */
584 pXRRFreeCrtcInfo( crtc_info
);
586 pXRRFreeOutputInfo( output_info
);
588 WARN("Primary is set to a disconnected XRandR output.\n");
589 for (i
= 0; i
< resources
->ncrtc
; ++i
)
591 crtc_info
= pXRRGetCrtcInfo( gdi_display
, resources
, resources
->crtcs
[i
] );
595 if (!crtc_info
->mode
)
597 pXRRFreeCrtcInfo( crtc_info
);
601 if (!crtc_info
->x
&& !crtc_info
->y
)
603 SetRect( &primary_rect
, 0, 0, crtc_info
->width
, crtc_info
->height
);
604 pXRRFreeCrtcInfo( crtc_info
);
608 if (IsRectEmpty( &first_rect
))
609 SetRect( &first_rect
, crtc_info
->x
, crtc_info
->y
,
610 crtc_info
->x
+ crtc_info
->width
, crtc_info
->y
+ crtc_info
->height
);
612 pXRRFreeCrtcInfo( crtc_info
);
615 return IsRectEmpty( &primary_rect
) ? first_rect
: primary_rect
;
618 static BOOL
is_crtc_primary( RECT primary
, const XRRCrtcInfo
*crtc
)
622 crtc
->x
== primary
.left
&&
623 crtc
->y
== primary
.top
&&
624 crtc
->x
+ crtc
->width
== primary
.right
&&
625 crtc
->y
+ crtc
->height
== primary
.bottom
;
628 VK_DEFINE_NON_DISPATCHABLE_HANDLE(VkDisplayKHR
)
630 static BOOL
get_gpu_properties_from_vulkan( struct gdi_gpu
*gpu
, const XRRProviderInfo
*provider_info
,
631 struct gdi_gpu
*prev_gpus
, int prev_gpu_count
)
633 static const char *extensions
[] =
635 VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME
,
636 VK_KHR_EXTERNAL_MEMORY_CAPABILITIES_EXTENSION_NAME
,
637 "VK_EXT_acquire_xlib_display",
638 "VK_EXT_direct_mode_display",
640 VK_KHR_SURFACE_EXTENSION_NAME
,
642 const struct vulkan_funcs
*vulkan_funcs
= get_vulkan_driver( WINE_VULKAN_DRIVER_VERSION
);
643 VkResult (*pvkGetRandROutputDisplayEXT
)( VkPhysicalDevice
, Display
*, RROutput
, VkDisplayKHR
* );
644 PFN_vkGetPhysicalDeviceProperties2KHR pvkGetPhysicalDeviceProperties2KHR
;
645 PFN_vkEnumeratePhysicalDevices pvkEnumeratePhysicalDevices
;
646 uint32_t device_count
, device_idx
, output_idx
, i
;
647 VkPhysicalDevice
*vk_physical_devices
= NULL
;
648 VkPhysicalDeviceProperties2 properties2
;
649 VkInstanceCreateInfo create_info
;
650 VkPhysicalDeviceIDProperties id
;
651 VkInstance vk_instance
= NULL
;
652 VkDisplayKHR vk_display
;
660 memset( &create_info
, 0, sizeof(create_info
) );
661 create_info
.sType
= VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO
;
662 create_info
.enabledExtensionCount
= ARRAY_SIZE(extensions
);
663 create_info
.ppEnabledExtensionNames
= extensions
;
665 vr
= vulkan_funcs
->p_vkCreateInstance( &create_info
, NULL
, &vk_instance
);
666 if (vr
!= VK_SUCCESS
)
668 WARN("Failed to create a Vulkan instance, vr %d.\n", vr
);
672 #define LOAD_VK_FUNC(f) \
673 if (!(p##f = (void *)vulkan_funcs->p_vkGetInstanceProcAddr( vk_instance, #f ))) \
675 WARN("Failed to load " #f ".\n"); \
679 LOAD_VK_FUNC(vkEnumeratePhysicalDevices
)
680 LOAD_VK_FUNC(vkGetPhysicalDeviceProperties2KHR
)
681 LOAD_VK_FUNC(vkGetRandROutputDisplayEXT
)
684 vr
= pvkEnumeratePhysicalDevices( vk_instance
, &device_count
, NULL
);
685 if (vr
!= VK_SUCCESS
|| !device_count
)
687 WARN("No Vulkan device found, vr %d, device_count %d.\n", vr
, device_count
);
691 if (!(vk_physical_devices
= calloc( device_count
, sizeof(*vk_physical_devices
) )))
694 vr
= pvkEnumeratePhysicalDevices( vk_instance
, &device_count
, vk_physical_devices
);
695 if (vr
!= VK_SUCCESS
)
697 WARN("vkEnumeratePhysicalDevices failed, vr %d.\n", vr
);
701 TRACE("provider name %s.\n", debugstr_a(provider_info
->name
));
703 for (device_idx
= 0; device_idx
< device_count
; ++device_idx
)
705 for (output_idx
= 0; output_idx
< provider_info
->noutputs
; ++output_idx
)
707 X11DRV_expect_error( gdi_display
, XRandRErrorHandler
, NULL
);
708 vr
= pvkGetRandROutputDisplayEXT( vk_physical_devices
[device_idx
], gdi_display
,
709 provider_info
->outputs
[output_idx
], &vk_display
);
710 XSync( gdi_display
, FALSE
);
711 if (X11DRV_check_error() || vr
!= VK_SUCCESS
|| vk_display
== VK_NULL_HANDLE
)
714 memset( &id
, 0, sizeof(id
) );
715 id
.sType
= VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ID_PROPERTIES
;
716 properties2
.sType
= VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2
;
717 properties2
.pNext
= &id
;
719 pvkGetPhysicalDeviceProperties2KHR( vk_physical_devices
[device_idx
], &properties2
);
720 for (i
= 0; i
< prev_gpu_count
; ++i
)
722 if (!memcmp( &prev_gpus
[i
].vulkan_uuid
, &id
.deviceUUID
, sizeof(id
.deviceUUID
) ))
724 WARN( "device UUID %#x:%#x already assigned to GPU %u.\n", *((uint32_t *)id
.deviceUUID
+ 1),
725 *(uint32_t *)id
.deviceUUID
, i
);
729 if (i
< prev_gpu_count
) continue;
731 memcpy( &gpu
->vulkan_uuid
, id
.deviceUUID
, sizeof(id
.deviceUUID
) );
733 /* Ignore Khronos vendor IDs */
734 if (properties2
.properties
.vendorID
< 0x10000)
736 gpu
->vendor_id
= properties2
.properties
.vendorID
;
737 gpu
->device_id
= properties2
.properties
.deviceID
;
739 RtlUTF8ToUnicodeN( gpu
->name
, sizeof(gpu
->name
), &len
, properties2
.properties
.deviceName
,
740 strlen( properties2
.properties
.deviceName
) + 1 );
747 free( vk_physical_devices
);
749 vulkan_funcs
->p_vkDestroyInstance( vk_instance
, NULL
);
753 /* Get a list of GPUs reported by XRandR 1.4. Set get_properties to FALSE if GPU properties are
754 * not needed to avoid unnecessary querying */
755 static BOOL
xrandr14_get_gpus( struct gdi_gpu
**new_gpus
, int *count
, BOOL get_properties
)
757 static const WCHAR wine_adapterW
[] = {'W','i','n','e',' ','A','d','a','p','t','e','r',0};
758 struct gdi_gpu
*gpus
= NULL
;
759 XRRScreenResources
*screen_resources
= NULL
;
760 XRRProviderResources
*provider_resources
= NULL
;
761 XRRProviderInfo
*provider_info
= NULL
;
762 XRRCrtcInfo
*crtc_info
= NULL
;
763 INT primary_provider
= -1;
769 screen_resources
= xrandr_get_screen_resources();
770 if (!screen_resources
)
773 provider_resources
= pXRRGetProviderResources( gdi_display
, root_window
);
774 if (!provider_resources
)
777 gpus
= calloc( provider_resources
->nproviders
? provider_resources
->nproviders
: 1, sizeof(*gpus
) );
781 /* Some XRandR implementations don't support providers.
782 * In this case, report a fake one to try searching adapters in screen resources */
783 if (!provider_resources
->nproviders
)
785 WARN("XRandR implementation doesn't report any providers, faking one.\n");
786 lstrcpyW( gpus
[0].name
, wine_adapterW
);
793 primary_rect
= get_primary_rect( screen_resources
);
794 for (i
= 0; i
< provider_resources
->nproviders
; ++i
)
796 provider_info
= pXRRGetProviderInfo( gdi_display
, screen_resources
, provider_resources
->providers
[i
] );
800 /* Find primary provider */
801 for (j
= 0; primary_provider
== -1 && j
< provider_info
->ncrtcs
; ++j
)
803 crtc_info
= pXRRGetCrtcInfo( gdi_display
, screen_resources
, provider_info
->crtcs
[j
] );
807 if (is_crtc_primary( primary_rect
, crtc_info
))
809 primary_provider
= i
;
810 pXRRFreeCrtcInfo( crtc_info
);
814 pXRRFreeCrtcInfo( crtc_info
);
817 gpus
[i
].id
= provider_resources
->providers
[i
];
820 if (!get_gpu_properties_from_vulkan( &gpus
[i
], provider_info
, gpus
, i
))
821 RtlUTF8ToUnicodeN( gpus
[i
].name
, sizeof(gpus
[i
].name
), &len
, provider_info
->name
,
822 strlen( provider_info
->name
) + 1 );
823 /* FIXME: Add an alternate method of getting PCI IDs, for systems that don't support Vulkan */
825 pXRRFreeProviderInfo( provider_info
);
828 /* Make primary GPU the first */
829 if (primary_provider
> 0)
831 struct gdi_gpu tmp
= gpus
[0];
832 gpus
[0] = gpus
[primary_provider
];
833 gpus
[primary_provider
] = tmp
;
837 *count
= provider_resources
->nproviders
;
840 if (provider_resources
)
841 pXRRFreeProviderResources( provider_resources
);
842 if (screen_resources
)
843 pXRRFreeScreenResources( screen_resources
);
847 ERR("Failed to get gpus\n");
852 static void xrandr14_free_gpus( struct gdi_gpu
*gpus
)
857 static BOOL
xrandr14_get_adapters( ULONG_PTR gpu_id
, struct gdi_adapter
**new_adapters
, int *count
)
859 struct gdi_adapter
*adapters
= NULL
;
860 XRRScreenResources
*screen_resources
= NULL
;
861 XRRProviderInfo
*provider_info
= NULL
;
862 XRRCrtcInfo
*enum_crtc_info
, *crtc_info
= NULL
;
863 XRROutputInfo
*enum_output_info
, *output_info
= NULL
;
865 INT crtc_count
, output_count
;
866 INT primary_adapter
= 0;
867 INT adapter_count
= 0;
868 BOOL mirrored
, detached
;
873 screen_resources
= xrandr_get_screen_resources();
874 if (!screen_resources
)
879 provider_info
= pXRRGetProviderInfo( gdi_display
, screen_resources
, gpu_id
);
883 crtc_count
= provider_info
->ncrtcs
;
884 output_count
= provider_info
->noutputs
;
885 outputs
= provider_info
->outputs
;
887 /* Fake provider id, search adapters in screen resources */
890 crtc_count
= screen_resources
->ncrtc
;
891 output_count
= screen_resources
->noutput
;
892 outputs
= screen_resources
->outputs
;
895 /* Actual adapter count could be less */
896 adapters
= calloc( crtc_count
, sizeof(*adapters
) );
900 primary_rect
= get_primary_rect( screen_resources
);
901 for (i
= 0; i
< output_count
; ++i
)
903 output_info
= pXRRGetOutputInfo( gdi_display
, screen_resources
, outputs
[i
] );
907 /* Only connected output are considered as monitors */
908 if (output_info
->connection
!= RR_Connected
)
910 pXRRFreeOutputInfo( output_info
);
915 /* Connected output doesn't mean the output is attached to a crtc */
917 if (output_info
->crtc
)
919 crtc_info
= pXRRGetCrtcInfo( gdi_display
, screen_resources
, output_info
->crtc
);
924 if (!output_info
->crtc
|| !crtc_info
->mode
)
927 /* Ignore mirroring output replicas because mirrored monitors are under the same adapter */
931 for (j
= 0; j
< screen_resources
->noutput
; ++j
)
933 enum_output_info
= pXRRGetOutputInfo( gdi_display
, screen_resources
, screen_resources
->outputs
[j
] );
934 if (!enum_output_info
)
937 if (enum_output_info
->connection
!= RR_Connected
|| !enum_output_info
->crtc
)
939 pXRRFreeOutputInfo( enum_output_info
);
943 enum_crtc_info
= pXRRGetCrtcInfo( gdi_display
, screen_resources
, enum_output_info
->crtc
);
944 pXRRFreeOutputInfo( enum_output_info
);
948 /* Some outputs may have the same coordinates, aka mirrored. Choose the output with
949 * the lowest value as primary and the rest will then be replicas in a mirroring set */
950 if (crtc_info
->x
== enum_crtc_info
->x
&&
951 crtc_info
->y
== enum_crtc_info
->y
&&
952 crtc_info
->width
== enum_crtc_info
->width
&&
953 crtc_info
->height
== enum_crtc_info
->height
&&
954 outputs
[i
] > screen_resources
->outputs
[j
])
957 pXRRFreeCrtcInfo( enum_crtc_info
);
961 pXRRFreeCrtcInfo( enum_crtc_info
);
965 if (!mirrored
|| detached
)
967 /* Use RROutput as adapter id. The reason of not using RRCrtc is that we need to detect inactive but
968 * attached monitors */
969 adapters
[adapter_count
].id
= outputs
[i
];
971 adapters
[adapter_count
].state_flags
|= DISPLAY_DEVICE_ATTACHED_TO_DESKTOP
;
972 if (is_crtc_primary( primary_rect
, crtc_info
))
974 adapters
[adapter_count
].state_flags
|= DISPLAY_DEVICE_PRIMARY_DEVICE
;
975 primary_adapter
= adapter_count
;
981 pXRRFreeOutputInfo( output_info
);
985 pXRRFreeCrtcInfo( crtc_info
);
990 /* Make primary adapter the first */
993 struct gdi_adapter tmp
= adapters
[0];
994 adapters
[0] = adapters
[primary_adapter
];
995 adapters
[primary_adapter
] = tmp
;
998 *new_adapters
= adapters
;
999 *count
= adapter_count
;
1002 if (screen_resources
)
1003 pXRRFreeScreenResources( screen_resources
);
1005 pXRRFreeProviderInfo( provider_info
);
1007 pXRRFreeOutputInfo( output_info
);
1009 pXRRFreeCrtcInfo( crtc_info
);
1013 ERR("Failed to get adapters\n");
1018 static void xrandr14_free_adapters( struct gdi_adapter
*adapters
)
1023 static BOOL
xrandr14_get_monitors( ULONG_PTR adapter_id
, struct gdi_monitor
**new_monitors
, int *count
)
1025 struct gdi_monitor
*realloc_monitors
, *monitors
= NULL
;
1026 XRRScreenResources
*screen_resources
= NULL
;
1027 XRROutputInfo
*output_info
= NULL
, *enum_output_info
= NULL
;
1028 XRRCrtcInfo
*crtc_info
= NULL
, *enum_crtc_info
;
1029 INT primary_index
= 0, monitor_count
= 0, capacity
;
1034 screen_resources
= xrandr_get_screen_resources();
1035 if (!screen_resources
)
1038 /* First start with a 2 monitors, should be enough for most cases */
1040 monitors
= calloc( capacity
, sizeof(*monitors
) );
1044 output_info
= pXRRGetOutputInfo( gdi_display
, screen_resources
, adapter_id
);
1048 if (output_info
->crtc
)
1050 crtc_info
= pXRRGetCrtcInfo( gdi_display
, screen_resources
, output_info
->crtc
);
1055 /* Inactive but attached monitor, no need to check for mirrored/replica monitors */
1056 if (!output_info
->crtc
|| !crtc_info
->mode
)
1058 monitors
[monitor_count
].state_flags
= DISPLAY_DEVICE_ATTACHED
;
1059 monitors
[monitor_count
].edid_len
= get_edid( adapter_id
, &monitors
[monitor_count
].edid
);
1062 /* Active monitors, need to find other monitors with the same coordinates as mirrored */
1065 primary_rect
= get_primary_rect( screen_resources
);
1067 for (i
= 0; i
< screen_resources
->noutput
; ++i
)
1069 enum_output_info
= pXRRGetOutputInfo( gdi_display
, screen_resources
, screen_resources
->outputs
[i
] );
1070 if (!enum_output_info
)
1073 /* Detached outputs don't count */
1074 if (enum_output_info
->connection
!= RR_Connected
)
1076 pXRRFreeOutputInfo( enum_output_info
);
1077 enum_output_info
= NULL
;
1081 /* Allocate more space if needed */
1082 if (monitor_count
>= capacity
)
1085 realloc_monitors
= realloc( monitors
, capacity
* sizeof(*monitors
) );
1086 if (!realloc_monitors
)
1088 monitors
= realloc_monitors
;
1091 if (enum_output_info
->crtc
)
1093 enum_crtc_info
= pXRRGetCrtcInfo( gdi_display
, screen_resources
, enum_output_info
->crtc
);
1094 if (!enum_crtc_info
)
1097 if (enum_crtc_info
->x
== crtc_info
->x
&&
1098 enum_crtc_info
->y
== crtc_info
->y
&&
1099 enum_crtc_info
->width
== crtc_info
->width
&&
1100 enum_crtc_info
->height
== crtc_info
->height
)
1102 SetRect( &monitors
[monitor_count
].rc_monitor
, crtc_info
->x
, crtc_info
->y
,
1103 crtc_info
->x
+ crtc_info
->width
, crtc_info
->y
+ crtc_info
->height
);
1104 monitors
[monitor_count
].rc_work
= get_work_area( &monitors
[monitor_count
].rc_monitor
);
1106 monitors
[monitor_count
].state_flags
= DISPLAY_DEVICE_ATTACHED
;
1107 if (!IsRectEmpty( &monitors
[monitor_count
].rc_monitor
))
1108 monitors
[monitor_count
].state_flags
|= DISPLAY_DEVICE_ACTIVE
;
1110 if (is_crtc_primary( primary_rect
, crtc_info
))
1111 primary_index
= monitor_count
;
1113 monitors
[monitor_count
].edid_len
= get_edid( screen_resources
->outputs
[i
],
1114 &monitors
[monitor_count
].edid
);
1118 pXRRFreeCrtcInfo( enum_crtc_info
);
1121 pXRRFreeOutputInfo( enum_output_info
);
1122 enum_output_info
= NULL
;
1125 /* Make sure the first monitor is the primary */
1128 struct gdi_monitor tmp
= monitors
[0];
1129 monitors
[0] = monitors
[primary_index
];
1130 monitors
[primary_index
] = tmp
;
1133 /* Make sure the primary monitor origin is at (0, 0) */
1134 for (i
= 0; i
< monitor_count
; i
++)
1136 OffsetRect( &monitors
[i
].rc_monitor
, -primary_rect
.left
, -primary_rect
.top
);
1137 OffsetRect( &monitors
[i
].rc_work
, -primary_rect
.left
, -primary_rect
.top
);
1141 *new_monitors
= monitors
;
1142 *count
= monitor_count
;
1145 if (screen_resources
)
1146 pXRRFreeScreenResources( screen_resources
);
1148 pXRRFreeOutputInfo( output_info
);
1150 pXRRFreeCrtcInfo( crtc_info
);
1151 if (enum_output_info
)
1152 pXRRFreeOutputInfo( enum_output_info
);
1155 for (i
= 0; i
< monitor_count
; i
++)
1157 if (monitors
[i
].edid
)
1158 XFree( monitors
[i
].edid
);
1161 ERR("Failed to get monitors\n");
1166 static void xrandr14_free_monitors( struct gdi_monitor
*monitors
, int count
)
1170 for (i
= 0; i
< count
; i
++)
1172 if (monitors
[i
].edid
)
1173 XFree( monitors
[i
].edid
);
1178 static BOOL
xrandr14_device_change_handler( HWND hwnd
, XEvent
*event
)
1182 xrandr14_invalidate_current_mode_cache();
1183 if (hwnd
== NtUserGetDesktopWindow() && NtUserGetWindowThread( hwnd
, NULL
) == GetCurrentThreadId())
1185 X11DRV_DisplayDevices_Init( TRUE
);
1186 X11DRV_resize_desktop();
1188 /* Update xinerama monitors for xinerama_get_fullscreen_monitors() */
1189 rect
= get_host_primary_monitor_rect();
1190 xinerama_init( rect
.right
- rect
.left
, rect
.bottom
- rect
.top
);
1194 static void xrandr14_register_event_handlers(void)
1196 Display
*display
= thread_init_display();
1197 int event_base
, error_base
;
1199 if (!pXRRQueryExtension( display
, &event_base
, &error_base
))
1202 pXRRSelectInput( display
, root_window
,
1203 RRCrtcChangeNotifyMask
| RROutputChangeNotifyMask
| RRProviderChangeNotifyMask
);
1204 X11DRV_register_event_handler( event_base
+ RRNotify_CrtcChange
, xrandr14_device_change_handler
,
1205 "XRandR CrtcChange" );
1206 X11DRV_register_event_handler( event_base
+ RRNotify_OutputChange
, xrandr14_device_change_handler
,
1207 "XRandR OutputChange" );
1208 X11DRV_register_event_handler( event_base
+ RRNotify_ProviderChange
, xrandr14_device_change_handler
,
1209 "XRandR ProviderChange" );
1212 /* XRandR 1.4 display settings handler */
1213 static BOOL
xrandr14_get_id( const WCHAR
*device_name
, BOOL is_primary
, x11drv_settings_id
*id
)
1215 struct current_mode
*tmp_modes
, *new_current_modes
= NULL
;
1216 INT gpu_count
, adapter_count
, new_current_mode_count
= 0;
1217 INT gpu_idx
, adapter_idx
, display_idx
;
1218 struct gdi_adapter
*adapters
;
1219 struct gdi_gpu
*gpus
;
1222 /* Parse \\.\DISPLAY%d */
1223 display_idx
= wcstol( device_name
+ 11, &end
, 10 ) - 1;
1228 pthread_mutex_lock( &xrandr_mutex
);
1231 if (!xrandr14_get_gpus( &gpus
, &gpu_count
, FALSE
))
1233 pthread_mutex_unlock( &xrandr_mutex
);
1237 for (gpu_idx
= 0; gpu_idx
< gpu_count
; ++gpu_idx
)
1239 if (!xrandr14_get_adapters( gpus
[gpu_idx
].id
, &adapters
, &adapter_count
))
1242 tmp_modes
= realloc( new_current_modes
, (new_current_mode_count
+ adapter_count
) * sizeof(*tmp_modes
) );
1245 xrandr14_free_adapters( adapters
);
1248 new_current_modes
= tmp_modes
;
1250 for (adapter_idx
= 0; adapter_idx
< adapter_count
; ++adapter_idx
)
1252 new_current_modes
[new_current_mode_count
+ adapter_idx
].id
= adapters
[adapter_idx
].id
;
1253 new_current_modes
[new_current_mode_count
+ adapter_idx
].loaded
= FALSE
;
1255 new_current_mode_count
+= adapter_count
;
1256 xrandr14_free_adapters( adapters
);
1258 xrandr14_free_gpus( gpus
);
1260 if (new_current_modes
)
1262 free( current_modes
);
1263 current_modes
= new_current_modes
;
1264 current_mode_count
= new_current_mode_count
;
1268 if (display_idx
>= current_mode_count
)
1270 pthread_mutex_unlock( &xrandr_mutex
);
1274 id
->id
= current_modes
[display_idx
].id
;
1275 pthread_mutex_unlock( &xrandr_mutex
);
1279 static void add_xrandr14_mode( DEVMODEW
*mode
, XRRModeInfo
*info
, DWORD depth
, DWORD frequency
,
1282 mode
->dmSize
= sizeof(*mode
);
1283 mode
->dmDriverExtra
= sizeof(RRMode
);
1284 mode
->dmFields
= DM_DISPLAYORIENTATION
| DM_BITSPERPEL
| DM_PELSWIDTH
|
1285 DM_PELSHEIGHT
| DM_DISPLAYFLAGS
;
1288 mode
->dmFields
|= DM_DISPLAYFREQUENCY
;
1289 mode
->dmDisplayFrequency
= frequency
;
1291 if (orientation
== DMDO_DEFAULT
|| orientation
== DMDO_180
)
1293 mode
->dmPelsWidth
= info
->width
;
1294 mode
->dmPelsHeight
= info
->height
;
1298 mode
->dmPelsWidth
= info
->height
;
1299 mode
->dmPelsHeight
= info
->width
;
1301 mode
->dmDisplayOrientation
= orientation
;
1302 mode
->dmBitsPerPel
= depth
;
1303 mode
->dmDisplayFlags
= 0;
1304 memcpy( (BYTE
*)mode
+ sizeof(*mode
), &info
->id
, sizeof(info
->id
) );
1307 static BOOL
xrandr14_get_modes( x11drv_settings_id id
, DWORD flags
, DEVMODEW
**new_modes
, UINT
*mode_count
)
1309 DWORD frequency
, orientation
, orientation_count
;
1310 XRRScreenResources
*screen_resources
;
1311 XRROutputInfo
*output_info
= NULL
;
1312 RROutput output
= (RROutput
)id
.id
;
1313 XRRCrtcInfo
*crtc_info
= NULL
;
1314 UINT depth_idx
, mode_idx
= 0;
1315 XRRModeInfo
*mode_info
;
1316 DEVMODEW
*mode
, *modes
;
1322 screen_resources
= xrandr_get_screen_resources();
1323 if (!screen_resources
)
1326 output_info
= pXRRGetOutputInfo( gdi_display
, screen_resources
, output
);
1330 if (output_info
->connection
!= RR_Connected
)
1338 crtc
= output_info
->crtc
;
1340 crtc
= get_output_free_crtc( screen_resources
, output_info
);
1342 crtc_info
= pXRRGetCrtcInfo( gdi_display
, screen_resources
, crtc
);
1344 /* If the output is connected to a CRTC, use rotations reported by the CRTC */
1347 if (flags
& EDS_ROTATEDMODE
)
1349 rotations
= crtc_info
->rotations
;
1353 /* According to the RandR spec, RRGetCrtcInfo should set the active rotation to Rotate_0
1354 * when a CRTC is disabled. However, some RandR implementations report 0 in this case */
1355 rotations
= (crtc_info
->rotation
& 0xf) ? crtc_info
->rotation
: RR_Rotate_0
;
1358 /* Not connected to CRTC, assume all rotations are supported */
1361 if (flags
& EDS_ROTATEDMODE
)
1363 rotations
= RR_Rotate_0
| RR_Rotate_90
| RR_Rotate_180
| RR_Rotate_270
;
1367 rotations
= RR_Rotate_0
;
1370 orientation_count
= get_orientation_count( rotations
);
1372 /* Allocate space for display modes in different color depths and orientations.
1373 * Store a RRMode at the end of each DEVMODEW as private driver data */
1374 modes
= calloc( output_info
->nmode
* DEPTH_COUNT
* orientation_count
,
1375 sizeof(*modes
) + sizeof(RRMode
) );
1379 for (i
= 0; i
< output_info
->nmode
; ++i
)
1381 for (j
= 0; j
< screen_resources
->nmode
; ++j
)
1383 if (output_info
->modes
[i
] != screen_resources
->modes
[j
].id
)
1386 mode_info
= &screen_resources
->modes
[j
];
1387 frequency
= get_frequency( mode_info
);
1389 for (depth_idx
= 0; depth_idx
< DEPTH_COUNT
; ++depth_idx
)
1391 for (orientation
= DMDO_DEFAULT
; orientation
<= DMDO_270
; ++orientation
)
1393 if (!((1 << orientation
) & rotations
))
1396 mode
= (DEVMODEW
*)((BYTE
*)modes
+ (sizeof(*modes
) + sizeof(RRMode
)) * mode_idx
);
1397 add_xrandr14_mode( mode
, mode_info
, depths
[depth_idx
], frequency
, orientation
);
1408 *mode_count
= mode_idx
;
1411 pXRRFreeCrtcInfo( crtc_info
);
1413 pXRRFreeOutputInfo( output_info
);
1414 if (screen_resources
)
1415 pXRRFreeScreenResources( screen_resources
);
1419 static void xrandr14_free_modes( DEVMODEW
*modes
)
1424 static BOOL
xrandr14_get_current_mode( x11drv_settings_id id
, DEVMODEW
*mode
)
1426 struct current_mode
*mode_ptr
= NULL
;
1427 XRRScreenResources
*screen_resources
;
1428 XRROutputInfo
*output_info
= NULL
;
1429 RROutput output
= (RROutput
)id
.id
;
1430 XRRModeInfo
*mode_info
= NULL
;
1431 XRRCrtcInfo
*crtc_info
= NULL
;
1436 pthread_mutex_lock( &xrandr_mutex
);
1437 for (mode_idx
= 0; mode_idx
< current_mode_count
; ++mode_idx
)
1439 if (current_modes
[mode_idx
].id
!= id
.id
)
1442 if (!current_modes
[mode_idx
].loaded
)
1444 mode_ptr
= ¤t_modes
[mode_idx
];
1448 memcpy( mode
, ¤t_modes
[mode_idx
].mode
, sizeof(*mode
) );
1449 pthread_mutex_unlock( &xrandr_mutex
);
1453 screen_resources
= xrandr_get_screen_resources();
1454 if (!screen_resources
)
1457 output_info
= pXRRGetOutputInfo( gdi_display
, screen_resources
, output
);
1461 if (output_info
->crtc
)
1463 crtc_info
= pXRRGetCrtcInfo( gdi_display
, screen_resources
, output_info
->crtc
);
1469 if (output_info
->connection
!= RR_Connected
|| !output_info
->crtc
|| !crtc_info
->mode
)
1471 mode
->dmFields
= DM_DISPLAYORIENTATION
| DM_BITSPERPEL
| DM_PELSWIDTH
| DM_PELSHEIGHT
|
1472 DM_DISPLAYFLAGS
| DM_DISPLAYFREQUENCY
| DM_POSITION
;
1473 mode
->dmDisplayOrientation
= DMDO_DEFAULT
;
1474 mode
->dmBitsPerPel
= 0;
1475 mode
->dmPelsWidth
= 0;
1476 mode
->dmPelsHeight
= 0;
1477 mode
->dmDisplayFlags
= 0;
1478 mode
->dmDisplayFrequency
= 0;
1479 mode
->dmPosition
.x
= 0;
1480 mode
->dmPosition
.y
= 0;
1486 for (mode_idx
= 0; mode_idx
< screen_resources
->nmode
; ++mode_idx
)
1488 if (crtc_info
->mode
== screen_resources
->modes
[mode_idx
].id
)
1490 mode_info
= &screen_resources
->modes
[mode_idx
];
1498 mode
->dmFields
= DM_DISPLAYORIENTATION
| DM_BITSPERPEL
| DM_PELSWIDTH
| DM_PELSHEIGHT
|
1499 DM_DISPLAYFLAGS
| DM_DISPLAYFREQUENCY
| DM_POSITION
;
1500 mode
->dmDisplayOrientation
= get_orientation( crtc_info
->rotation
);
1501 mode
->dmBitsPerPel
= screen_bpp
;
1502 mode
->dmPelsWidth
= crtc_info
->width
;
1503 mode
->dmPelsHeight
= crtc_info
->height
;
1504 mode
->dmDisplayFlags
= 0;
1505 mode
->dmDisplayFrequency
= get_frequency( mode_info
);
1506 /* Convert RandR coordinates to virtual screen coordinates */
1507 primary
= get_primary_rect( screen_resources
);
1508 mode
->dmPosition
.x
= crtc_info
->x
- primary
.left
;
1509 mode
->dmPosition
.y
= crtc_info
->y
- primary
.top
;
1513 if (ret
&& mode_ptr
)
1515 memcpy( &mode_ptr
->mode
, mode
, sizeof(*mode
) );
1516 mode_ptr
->mode
.dmSize
= sizeof(*mode
);
1517 mode_ptr
->mode
.dmDriverExtra
= 0;
1518 mode_ptr
->loaded
= TRUE
;
1520 pthread_mutex_unlock( &xrandr_mutex
);
1522 pXRRFreeCrtcInfo( crtc_info
);
1524 pXRRFreeOutputInfo( output_info
);
1525 if (screen_resources
)
1526 pXRRFreeScreenResources( screen_resources
);
1530 static LONG
xrandr14_set_current_mode( x11drv_settings_id id
, const DEVMODEW
*mode
)
1532 unsigned int screen_width
, screen_height
;
1533 RROutput output
= (RROutput
)id
.id
, *outputs
;
1534 XRRScreenResources
*screen_resources
;
1535 XRROutputInfo
*output_info
= NULL
;
1536 XRRCrtcInfo
*crtc_info
= NULL
;
1537 LONG ret
= DISP_CHANGE_FAILED
;
1544 if (mode
->dmFields
& DM_BITSPERPEL
&& mode
->dmBitsPerPel
!= screen_bpp
)
1545 WARN("Cannot change screen color depth from %ubits to %ubits!\n",
1546 screen_bpp
, (int)mode
->dmBitsPerPel
);
1548 screen_resources
= xrandr_get_screen_resources();
1549 if (!screen_resources
)
1552 XGrabServer( gdi_display
);
1554 output_info
= pXRRGetOutputInfo( gdi_display
, screen_resources
, output
);
1555 if (!output_info
|| output_info
->connection
!= RR_Connected
)
1558 if (is_detached_mode(mode
))
1560 /* Already detached */
1561 if (!output_info
->crtc
)
1563 ret
= DISP_CHANGE_SUCCESSFUL
;
1567 /* Execute detach operation */
1568 status
= pXRRSetCrtcConfig( gdi_display
, screen_resources
, output_info
->crtc
,
1569 CurrentTime
, 0, 0, None
, RR_Rotate_0
, NULL
, 0 );
1570 if (status
== RRSetConfigSuccess
)
1572 get_screen_size( screen_resources
, &screen_width
, &screen_height
);
1573 set_screen_size( screen_width
, screen_height
);
1574 ret
= DISP_CHANGE_SUCCESSFUL
;
1580 if (output_info
->crtc
)
1582 crtc
= output_info
->crtc
;
1584 /* Detached, need to find a free CRTC */
1587 if (!(crtc
= get_output_free_crtc( screen_resources
, output_info
)))
1591 crtc_info
= pXRRGetCrtcInfo( gdi_display
, screen_resources
, crtc
);
1595 assert( mode
->dmDriverExtra
== sizeof(RRMode
) );
1596 memcpy( &rrmode
, (BYTE
*)mode
+ sizeof(*mode
), sizeof(rrmode
) );
1598 if (crtc_info
->noutput
)
1600 outputs
= crtc_info
->outputs
;
1601 output_count
= crtc_info
->noutput
;
1608 rotation
= get_rotation( mode
->dmDisplayOrientation
);
1610 /* According to the RandR spec, the entire CRTC must fit inside the screen.
1611 * Since we use the union of all enabled CRTCs to determine the necessary
1612 * screen size, this might involve shrinking the screen, so we must disable
1613 * the CRTC in question first. */
1614 status
= pXRRSetCrtcConfig( gdi_display
, screen_resources
, crtc
, CurrentTime
, 0, 0, None
,
1615 RR_Rotate_0
, NULL
, 0 );
1616 if (status
!= RRSetConfigSuccess
)
1619 get_screen_size( screen_resources
, &screen_width
, &screen_height
);
1620 screen_width
= max( screen_width
, mode
->dmPosition
.x
+ mode
->dmPelsWidth
);
1621 screen_height
= max( screen_height
, mode
->dmPosition
.y
+ mode
->dmPelsHeight
);
1622 set_screen_size( screen_width
, screen_height
);
1624 status
= pXRRSetCrtcConfig( gdi_display
, screen_resources
, crtc
, CurrentTime
,
1625 mode
->dmPosition
.x
, mode
->dmPosition
.y
, rrmode
,
1626 rotation
, outputs
, output_count
);
1627 if (status
== RRSetConfigSuccess
)
1628 ret
= DISP_CHANGE_SUCCESSFUL
;
1631 XUngrabServer( gdi_display
);
1632 XFlush( gdi_display
);
1634 pXRRFreeCrtcInfo( crtc_info
);
1636 pXRRFreeOutputInfo( output_info
);
1637 pXRRFreeScreenResources( screen_resources
);
1638 xrandr14_invalidate_current_mode_cache();
1644 void X11DRV_XRandR_Init(void)
1646 struct x11drv_display_device_handler display_handler
;
1647 struct x11drv_settings_handler settings_handler
;
1648 int event_base
, error_base
, minor
, ret
;
1652 if (major
) return; /* already initialized? */
1653 if (!usexrandr
) return; /* disabled in config */
1654 if (is_virtual_desktop()) return;
1655 if (!(ret
= load_xrandr())) return; /* can't load the Xrandr library */
1657 /* see if Xrandr is available */
1658 if (!pXRRQueryExtension( gdi_display
, &event_base
, &error_base
)) return;
1659 X11DRV_expect_error( gdi_display
, XRandRErrorHandler
, NULL
);
1660 ok
= pXRRQueryVersion( gdi_display
, &major
, &minor
);
1661 if (X11DRV_check_error() || !ok
) return;
1663 TRACE("Found XRandR %d.%d.\n", major
, minor
);
1665 settings_handler
.name
= "XRandR 1.0";
1666 settings_handler
.priority
= 200;
1667 settings_handler
.get_id
= xrandr10_get_id
;
1668 settings_handler
.get_modes
= xrandr10_get_modes
;
1669 settings_handler
.free_modes
= xrandr10_free_modes
;
1670 settings_handler
.get_current_mode
= xrandr10_get_current_mode
;
1671 settings_handler
.set_current_mode
= xrandr10_set_current_mode
;
1672 X11DRV_Settings_SetHandler( &settings_handler
);
1674 #ifdef HAVE_XRRGETPROVIDERRESOURCES
1675 if (ret
>= 4 && (major
> 1 || (major
== 1 && minor
>= 4)))
1677 XRRScreenResources
*screen_resources
;
1678 XRROutputInfo
*output_info
;
1679 BOOL found_output
= FALSE
;
1682 screen_resources
= xrandr_get_screen_resources();
1683 if (!screen_resources
)
1686 for (i
= 0; i
< screen_resources
->noutput
; ++i
)
1688 output_info
= pXRRGetOutputInfo( gdi_display
, screen_resources
, screen_resources
->outputs
[i
] );
1692 if (output_info
->connection
== RR_Connected
)
1694 pXRRFreeOutputInfo( output_info
);
1695 found_output
= TRUE
;
1699 pXRRFreeOutputInfo( output_info
);
1701 pXRRFreeScreenResources( screen_resources
);
1705 WARN("No connected outputs found.\n");
1709 display_handler
.name
= "XRandR 1.4";
1710 display_handler
.priority
= 200;
1711 display_handler
.get_gpus
= xrandr14_get_gpus
;
1712 display_handler
.get_adapters
= xrandr14_get_adapters
;
1713 display_handler
.get_monitors
= xrandr14_get_monitors
;
1714 display_handler
.free_gpus
= xrandr14_free_gpus
;
1715 display_handler
.free_adapters
= xrandr14_free_adapters
;
1716 display_handler
.free_monitors
= xrandr14_free_monitors
;
1717 display_handler
.register_event_handlers
= xrandr14_register_event_handlers
;
1718 X11DRV_DisplayDevices_SetHandler( &display_handler
);
1720 if (is_broken_driver())
1723 settings_handler
.name
= "XRandR 1.4";
1724 settings_handler
.priority
= 300;
1725 settings_handler
.get_id
= xrandr14_get_id
;
1726 settings_handler
.get_modes
= xrandr14_get_modes
;
1727 settings_handler
.free_modes
= xrandr14_free_modes
;
1728 settings_handler
.get_current_mode
= xrandr14_get_current_mode
;
1729 settings_handler
.set_current_mode
= xrandr14_set_current_mode
;
1730 X11DRV_Settings_SetHandler( &settings_handler
);
1735 #else /* SONAME_LIBXRANDR */
1737 void X11DRV_XRandR_Init(void)
1739 TRACE("XRandR support not compiled in.\n");
1742 #endif /* SONAME_LIBXRANDR */