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"
23 #include "wine/debug.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(xrandr);
27 #ifdef SONAME_LIBXRANDR
30 #include <X11/extensions/Xrandr.h>
33 #include "wine/library.h"
35 static void *xrandr_handle;
37 #define MAKE_FUNCPTR(f) static typeof(f) * p##f;
38 MAKE_FUNCPTR(XRRConfigCurrentConfiguration)
39 MAKE_FUNCPTR(XRRConfigCurrentRate)
40 MAKE_FUNCPTR(XRRFreeScreenConfigInfo)
41 MAKE_FUNCPTR(XRRGetScreenInfo)
42 MAKE_FUNCPTR(XRRQueryExtension)
43 MAKE_FUNCPTR(XRRQueryVersion)
44 MAKE_FUNCPTR(XRRRates)
45 MAKE_FUNCPTR(XRRSetScreenConfig)
46 MAKE_FUNCPTR(XRRSetScreenConfigAndRate)
47 MAKE_FUNCPTR(XRRSizes)
52 static int xrandr_event, xrandr_error, xrandr_major, xrandr_minor;
54 static struct x11drv_mode_info *dd_modes;
55 static unsigned int dd_mode_count;
56 static XRRScreenSize *real_xrandr_sizes;
57 static short **real_xrandr_rates;
58 static int real_xrandr_sizes_count;
59 static int *real_xrandr_rates_count;
60 static unsigned int real_xrandr_modes_count;
62 static int load_xrandr(void)
66 if (wine_dlopen(SONAME_LIBXRENDER, RTLD_NOW|RTLD_GLOBAL, NULL, 0) &&
67 (xrandr_handle = wine_dlopen(SONAME_LIBXRANDR, RTLD_NOW, NULL, 0)))
70 #define LOAD_FUNCPTR(f) \
71 if((p##f = wine_dlsym(xrandr_handle, #f, NULL, 0)) == NULL) \
74 LOAD_FUNCPTR(XRRConfigCurrentConfiguration)
75 LOAD_FUNCPTR(XRRConfigCurrentRate)
76 LOAD_FUNCPTR(XRRFreeScreenConfigInfo)
77 LOAD_FUNCPTR(XRRGetScreenInfo)
78 LOAD_FUNCPTR(XRRQueryExtension)
79 LOAD_FUNCPTR(XRRQueryVersion)
80 LOAD_FUNCPTR(XRRRates)
81 LOAD_FUNCPTR(XRRSetScreenConfig)
82 LOAD_FUNCPTR(XRRSetScreenConfigAndRate)
83 LOAD_FUNCPTR(XRRSizes)
90 if (!r) TRACE("Unable to load function ptrs from XRandR library\n");
95 static int XRandRErrorHandler(Display *dpy, XErrorEvent *event, void *arg)
100 static int X11DRV_XRandR_GetCurrentMode(void)
105 XRRScreenConfiguration *sc;
111 root = RootWindow (gdi_display, DefaultScreen(gdi_display));
112 sc = pXRRGetScreenInfo (gdi_display, root);
113 size = pXRRConfigCurrentConfiguration (sc, &rot);
114 rate = pXRRConfigCurrentRate (sc);
115 pXRRFreeScreenConfigInfo(sc);
117 for (i = 0; i < real_xrandr_modes_count; i++)
119 if ( (dd_modes[i].width == real_xrandr_sizes[size].width ) &&
120 (dd_modes[i].height == real_xrandr_sizes[size].height) &&
121 (dd_modes[i].refresh_rate == rate ) )
129 ERR("In unknown mode, returning default\n");
135 static LONG X11DRV_XRandR_SetCurrentMode(int mode)
140 XRRScreenConfiguration *sc;
141 Status stat = RRSetConfigSuccess;
147 root = RootWindow (gdi_display, DefaultScreen(gdi_display));
148 sc = pXRRGetScreenInfo (gdi_display, root);
149 size = pXRRConfigCurrentConfiguration (sc, &rot);
150 mode = mode%real_xrandr_modes_count;
152 TRACE("Changing Resolution to %dx%d @%d Hz\n",
153 dd_modes[mode].width,
154 dd_modes[mode].height,
155 dd_modes[mode].refresh_rate);
157 for (i = 0; i < real_xrandr_sizes_count; i++)
159 if ( (dd_modes[mode].width == real_xrandr_sizes[i].width ) &&
160 (dd_modes[mode].height == real_xrandr_sizes[i].height) )
163 if (real_xrandr_rates_count[i])
165 for (j=0; j < real_xrandr_rates_count[i]; j++)
167 if (dd_modes[mode].refresh_rate == real_xrandr_rates[i][j])
169 rate = real_xrandr_rates[i][j];
170 TRACE("Resizing X display to %dx%d @%d Hz\n",
171 dd_modes[mode].width, dd_modes[mode].height, rate);
172 stat = pXRRSetScreenConfigAndRate (gdi_display, sc, root,
173 size, rot, rate, CurrentTime);
180 TRACE("Resizing X display to %dx%d <default Hz>\n",
181 dd_modes[mode].width, dd_modes[mode].height);
182 stat = pXRRSetScreenConfig (gdi_display, sc, root, size, rot, CurrentTime);
187 pXRRFreeScreenConfigInfo(sc);
189 if (stat == RRSetConfigSuccess)
191 X11DRV_resize_desktop( dd_modes[mode].width, dd_modes[mode].height );
192 return DISP_CHANGE_SUCCESSFUL;
195 ERR("Resolution change not successful -- perhaps display has changed?\n");
196 return DISP_CHANGE_FAILED;
199 static void xrandr_init_modes(void)
201 int i, j, nmodes = 0;
203 real_xrandr_sizes = pXRRSizes( gdi_display, DefaultScreen(gdi_display), &real_xrandr_sizes_count);
204 if (real_xrandr_sizes_count <= 0) return;
206 TRACE("XRandR: found %u sizes.\n", real_xrandr_sizes_count);
207 real_xrandr_rates = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
208 sizeof(short *) * real_xrandr_sizes_count );
209 real_xrandr_rates_count = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
210 sizeof(int) * real_xrandr_sizes_count );
211 for (i = 0; i < real_xrandr_sizes_count; ++i)
213 real_xrandr_rates[i] = pXRRRates( gdi_display, DefaultScreen(gdi_display),
214 i, &real_xrandr_rates_count[i] );
215 TRACE("- at %d: %dx%d (%d rates):", i, real_xrandr_sizes[i].width,
216 real_xrandr_sizes[i].height, real_xrandr_rates_count[i]);
217 if (real_xrandr_rates_count[i])
219 nmodes += real_xrandr_rates_count[i];
220 for (j = 0; j < real_xrandr_rates_count[i]; ++j)
224 TRACE(" %d", real_xrandr_rates[i][j]);
235 real_xrandr_modes_count = nmodes;
236 TRACE("XRandR modes: count=%d\n", nmodes);
238 dd_modes = X11DRV_Settings_SetHandlers( "XRandR",
239 X11DRV_XRandR_GetCurrentMode,
240 X11DRV_XRandR_SetCurrentMode,
243 for (i = 0; i < real_xrandr_sizes_count; ++i)
245 if (real_xrandr_rates_count[i])
247 for (j = 0; j < real_xrandr_rates_count[i]; ++j)
249 X11DRV_Settings_AddOneMode( real_xrandr_sizes[i].width,
250 real_xrandr_sizes[i].height,
251 0, real_xrandr_rates[i][j] );
256 X11DRV_Settings_AddOneMode( real_xrandr_sizes[i].width,
257 real_xrandr_sizes[i].height,
262 X11DRV_Settings_AddDepthModes();
263 dd_mode_count = X11DRV_Settings_GetModeCount();
265 TRACE("Available DD modes: count=%d\n", dd_mode_count);
266 TRACE("Enabling XRandR\n");
269 void X11DRV_XRandR_Init(void)
273 if (xrandr_major) return; /* already initialized? */
274 if (!usexrandr) return; /* disabled in config */
275 if (root_window != DefaultRootWindow( gdi_display )) return;
276 if (!load_xrandr()) return; /* can't load the Xrandr library */
278 /* see if Xrandr is available */
280 ok = pXRRQueryExtension(gdi_display, &xrandr_event, &xrandr_error);
283 X11DRV_expect_error(gdi_display, XRandRErrorHandler, NULL);
284 ok = pXRRQueryVersion(gdi_display, &xrandr_major, &xrandr_minor);
285 if (X11DRV_check_error()) ok = FALSE;
289 TRACE("Found XRandR - major: %d, minor: %d\n", xrandr_major, xrandr_minor);
295 #else /* SONAME_LIBXRANDR */
297 void X11DRV_XRandR_Init(void)
299 TRACE("XRandR support not compiled in.\n");
302 #endif /* SONAME_LIBXRANDR */