2 * Wine X11drv Xrandr interface
4 * Copyright 2003 Alexander James Pasadyn
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
22 #include "wine/port.h"
26 #ifdef SONAME_LIBXRANDR
29 #include <X11/extensions/Xrandr.h>
38 #include "wine/library.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(xrandr
);
43 static void *xrandr_handle
;
45 #define MAKE_FUNCPTR(f) static typeof(f) * p##f;
46 MAKE_FUNCPTR(XRRConfigCurrentConfiguration
)
47 MAKE_FUNCPTR(XRRConfigCurrentRate
)
48 MAKE_FUNCPTR(XRRFreeScreenConfigInfo
)
49 MAKE_FUNCPTR(XRRGetScreenInfo
)
50 MAKE_FUNCPTR(XRRQueryExtension
)
51 MAKE_FUNCPTR(XRRQueryVersion
)
52 MAKE_FUNCPTR(XRRRates
)
53 MAKE_FUNCPTR(XRRSetScreenConfig
)
54 MAKE_FUNCPTR(XRRSetScreenConfigAndRate
)
55 MAKE_FUNCPTR(XRRSizes
)
60 static int xrandr_event
, xrandr_error
, xrandr_major
, xrandr_minor
;
62 static LPDDHALMODEINFO dd_modes
;
63 static unsigned int dd_mode_count
;
64 static XRRScreenSize
*real_xrandr_sizes
;
65 static short **real_xrandr_rates
;
66 static int real_xrandr_sizes_count
;
67 static int *real_xrandr_rates_count
;
68 static unsigned int real_xrandr_modes_count
;
70 static int load_xrandr(void)
74 if (wine_dlopen(SONAME_LIBX11
, RTLD_NOW
|RTLD_GLOBAL
, NULL
, 0) &&
75 wine_dlopen(SONAME_LIBXEXT
, RTLD_NOW
|RTLD_GLOBAL
, NULL
, 0) &&
76 wine_dlopen(SONAME_LIBXRENDER
, RTLD_NOW
|RTLD_GLOBAL
, NULL
, 0) &&
77 (xrandr_handle
= wine_dlopen(SONAME_LIBXRANDR
, RTLD_NOW
, NULL
, 0)))
80 #define LOAD_FUNCPTR(f) \
81 if((p##f = wine_dlsym(xrandr_handle, #f, NULL, 0)) == NULL) \
84 LOAD_FUNCPTR(XRRConfigCurrentConfiguration
)
85 LOAD_FUNCPTR(XRRConfigCurrentRate
)
86 LOAD_FUNCPTR(XRRFreeScreenConfigInfo
)
87 LOAD_FUNCPTR(XRRGetScreenInfo
)
88 LOAD_FUNCPTR(XRRQueryExtension
)
89 LOAD_FUNCPTR(XRRQueryVersion
)
90 LOAD_FUNCPTR(XRRRates
)
91 LOAD_FUNCPTR(XRRSetScreenConfig
)
92 LOAD_FUNCPTR(XRRSetScreenConfigAndRate
)
93 LOAD_FUNCPTR(XRRSizes
)
100 if (!r
) TRACE("Unable to load function ptrs from XRandR library\n");
105 static int XRandRErrorHandler(Display
*dpy
, XErrorEvent
*event
, void *arg
)
111 /* create the mode structures */
112 static void make_modes(void)
116 for (i
=0; i
<real_xrandr_sizes_count
; i
++)
118 if (real_xrandr_rates_count
[i
])
120 for (j
=0; j
< real_xrandr_rates_count
[i
]; j
++)
122 X11DRV_Settings_AddOneMode(real_xrandr_sizes
[i
].width
,
123 real_xrandr_sizes
[i
].height
,
124 0, real_xrandr_rates
[i
][j
]);
129 X11DRV_Settings_AddOneMode(real_xrandr_sizes
[i
].width
,
130 real_xrandr_sizes
[i
].height
,
136 static int X11DRV_XRandR_GetCurrentMode(void)
141 XRRScreenConfiguration
*sc
;
147 root
= RootWindow (gdi_display
, DefaultScreen(gdi_display
));
148 sc
= pXRRGetScreenInfo (gdi_display
, root
);
149 size
= pXRRConfigCurrentConfiguration (sc
, &rot
);
150 rate
= pXRRConfigCurrentRate (sc
);
151 pXRRFreeScreenConfigInfo(sc
);
153 for (i
= 0; i
< real_xrandr_modes_count
; i
++)
155 if ( (dd_modes
[i
].dwWidth
== real_xrandr_sizes
[size
].width
) &&
156 (dd_modes
[i
].dwHeight
== real_xrandr_sizes
[size
].height
) &&
157 (dd_modes
[i
].wRefreshRate
== rate
) )
165 ERR("In unknown mode, returning default\n");
171 static LONG
X11DRV_XRandR_SetCurrentMode(int mode
)
176 XRRScreenConfiguration
*sc
;
177 Status stat
= RRSetConfigSuccess
;
181 DWORD dwBpp
= screen_depth
;
182 if (dwBpp
== 24) dwBpp
= 32;
185 root
= RootWindow (gdi_display
, DefaultScreen(gdi_display
));
186 sc
= pXRRGetScreenInfo (gdi_display
, root
);
187 size
= pXRRConfigCurrentConfiguration (sc
, &rot
);
188 if (dwBpp
!= dd_modes
[mode
].dwBPP
)
190 FIXME("Cannot change screen BPP from %d to %d\n", dwBpp
, dd_modes
[mode
].dwBPP
);
192 mode
= mode
%real_xrandr_modes_count
;
194 TRACE("Changing Resolution to %dx%d @%d Hz\n",
195 dd_modes
[mode
].dwWidth
,
196 dd_modes
[mode
].dwHeight
,
197 dd_modes
[mode
].wRefreshRate
);
199 for (i
= 0; i
< real_xrandr_sizes_count
; i
++)
201 if ( (dd_modes
[mode
].dwWidth
== real_xrandr_sizes
[i
].width
) &&
202 (dd_modes
[mode
].dwHeight
== real_xrandr_sizes
[i
].height
) )
205 if (real_xrandr_rates_count
[i
])
207 for (j
=0; j
< real_xrandr_rates_count
[i
]; j
++)
209 if (dd_modes
[mode
].wRefreshRate
== real_xrandr_rates
[i
][j
])
211 rate
= real_xrandr_rates
[i
][j
];
212 TRACE("Resizing X display to %dx%d @%d Hz\n",
213 dd_modes
[mode
].dwWidth
, dd_modes
[mode
].dwHeight
, rate
);
214 stat
= pXRRSetScreenConfigAndRate (gdi_display
, sc
, root
,
215 size
, rot
, rate
, CurrentTime
);
222 TRACE("Resizing X display to %dx%d <default Hz>\n",
223 dd_modes
[mode
].dwWidth
, dd_modes
[mode
].dwHeight
);
224 stat
= pXRRSetScreenConfig (gdi_display
, sc
, root
, size
, rot
, CurrentTime
);
229 pXRRFreeScreenConfigInfo(sc
);
231 if (stat
== RRSetConfigSuccess
)
233 X11DRV_handle_desktop_resize( dd_modes
[mode
].dwWidth
, dd_modes
[mode
].dwHeight
);
234 return DISP_CHANGE_SUCCESSFUL
;
237 ERR("Resolution change not successful -- perhaps display has changed?\n");
238 return DISP_CHANGE_FAILED
;
241 void X11DRV_XRandR_Init(void)
247 if (xrandr_major
) return; /* already initialized? */
248 if (!usexrandr
) return; /* disabled in config */
249 if (root_window
!= DefaultRootWindow( gdi_display
)) return;
250 if (!load_xrandr()) return; /* can't load the Xrandr library */
252 /* see if Xrandr is available */
254 ok
= pXRRQueryExtension(gdi_display
, &xrandr_event
, &xrandr_error
);
257 X11DRV_expect_error(gdi_display
, XRandRErrorHandler
, NULL
);
258 ok
= pXRRQueryVersion(gdi_display
, &xrandr_major
, &xrandr_minor
);
259 if (X11DRV_check_error()) ok
= FALSE
;
263 TRACE("Found XRandR - major: %d, minor: %d\n", xrandr_major
, xrandr_minor
);
265 real_xrandr_sizes
= pXRRSizes(gdi_display
, DefaultScreen(gdi_display
), &real_xrandr_sizes_count
);
266 ok
= (real_xrandr_sizes_count
>0);
270 TRACE("XRandR: found %u resolutions sizes\n", real_xrandr_sizes_count
);
271 real_xrandr_rates
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(short *) * real_xrandr_sizes_count
);
272 real_xrandr_rates_count
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(int) * real_xrandr_sizes_count
);
273 for (i
=0; i
< real_xrandr_sizes_count
; i
++)
275 real_xrandr_rates
[i
] = pXRRRates (gdi_display
, DefaultScreen(gdi_display
), i
, &(real_xrandr_rates_count
[i
]));
276 TRACE("- at %u: %dx%d (%d rates):", i
, real_xrandr_sizes
[i
].width
, real_xrandr_sizes
[i
].height
, real_xrandr_rates_count
[i
]);
277 if (real_xrandr_rates_count
[i
])
280 nmodes
+= real_xrandr_rates_count
[i
];
281 for (j
= 0; j
< real_xrandr_rates_count
[i
]; ++j
) {
282 if (j
> 0) TRACE(",");
283 TRACE(" %d", real_xrandr_rates
[i
][j
]);
297 real_xrandr_modes_count
= nmodes
;
298 TRACE("XRandR modes: count=%d\n", nmodes
);
300 dd_modes
= X11DRV_Settings_SetHandlers("XRandR",
301 X11DRV_XRandR_GetCurrentMode
,
302 X11DRV_XRandR_SetCurrentMode
,
305 X11DRV_Settings_AddDepthModes();
306 dd_mode_count
= X11DRV_Settings_GetModeCount();
307 X11DRV_Settings_SetDefaultMode(0);
309 TRACE("Available DD modes: count=%d\n", dd_mode_count
);
310 TRACE("Enabling XRandR\n");
313 void X11DRV_XRandR_Cleanup(void)
315 HeapFree(GetProcessHeap(), 0, real_xrandr_rates
);
316 real_xrandr_rates
= NULL
;
317 HeapFree(GetProcessHeap(), 0, real_xrandr_rates_count
);
318 real_xrandr_rates_count
= NULL
;
321 #endif /* SONAME_LIBXRANDR */