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 struct x11drv_mode_info *dd_modes;
53 static unsigned int dd_mode_count;
54 static XRRScreenSize *real_xrandr_sizes;
55 static short **real_xrandr_rates;
56 static int real_xrandr_sizes_count;
57 static int *real_xrandr_rates_count;
58 static unsigned int real_xrandr_modes_count;
60 static int load_xrandr(void)
64 if (wine_dlopen(SONAME_LIBXRENDER, RTLD_NOW|RTLD_GLOBAL, NULL, 0) &&
65 (xrandr_handle = wine_dlopen(SONAME_LIBXRANDR, RTLD_NOW, NULL, 0)))
68 #define LOAD_FUNCPTR(f) \
69 if((p##f = wine_dlsym(xrandr_handle, #f, NULL, 0)) == NULL) \
72 LOAD_FUNCPTR(XRRConfigCurrentConfiguration)
73 LOAD_FUNCPTR(XRRConfigCurrentRate)
74 LOAD_FUNCPTR(XRRFreeScreenConfigInfo)
75 LOAD_FUNCPTR(XRRGetScreenInfo)
76 LOAD_FUNCPTR(XRRQueryExtension)
77 LOAD_FUNCPTR(XRRQueryVersion)
78 LOAD_FUNCPTR(XRRRates)
79 LOAD_FUNCPTR(XRRSetScreenConfig)
80 LOAD_FUNCPTR(XRRSetScreenConfigAndRate)
81 LOAD_FUNCPTR(XRRSizes)
88 if (!r) TRACE("Unable to load function ptrs from XRandR library\n");
93 static int XRandRErrorHandler(Display *dpy, XErrorEvent *event, void *arg)
98 static int X11DRV_XRandR_GetCurrentMode(void)
103 XRRScreenConfiguration *sc;
109 root = RootWindow (gdi_display, DefaultScreen(gdi_display));
110 sc = pXRRGetScreenInfo (gdi_display, root);
111 size = pXRRConfigCurrentConfiguration (sc, &rot);
112 rate = pXRRConfigCurrentRate (sc);
113 pXRRFreeScreenConfigInfo(sc);
115 for (i = 0; i < real_xrandr_modes_count; i++)
117 if ( (dd_modes[i].width == real_xrandr_sizes[size].width ) &&
118 (dd_modes[i].height == real_xrandr_sizes[size].height) &&
119 (dd_modes[i].refresh_rate == rate ) )
127 ERR("In unknown mode, returning default\n");
133 static LONG X11DRV_XRandR_SetCurrentMode(int mode)
138 XRRScreenConfiguration *sc;
139 Status stat = RRSetConfigSuccess;
145 root = RootWindow (gdi_display, DefaultScreen(gdi_display));
146 sc = pXRRGetScreenInfo (gdi_display, root);
147 size = pXRRConfigCurrentConfiguration (sc, &rot);
148 mode = mode%real_xrandr_modes_count;
150 TRACE("Changing Resolution to %dx%d @%d Hz\n",
151 dd_modes[mode].width,
152 dd_modes[mode].height,
153 dd_modes[mode].refresh_rate);
155 for (i = 0; i < real_xrandr_sizes_count; i++)
157 if ( (dd_modes[mode].width == real_xrandr_sizes[i].width ) &&
158 (dd_modes[mode].height == real_xrandr_sizes[i].height) )
161 if (real_xrandr_rates_count[i])
163 for (j=0; j < real_xrandr_rates_count[i]; j++)
165 if (dd_modes[mode].refresh_rate == real_xrandr_rates[i][j])
167 rate = real_xrandr_rates[i][j];
168 TRACE("Resizing X display to %dx%d @%d Hz\n",
169 dd_modes[mode].width, dd_modes[mode].height, rate);
170 stat = pXRRSetScreenConfigAndRate (gdi_display, sc, root,
171 size, rot, rate, CurrentTime);
178 TRACE("Resizing X display to %dx%d <default Hz>\n",
179 dd_modes[mode].width, dd_modes[mode].height);
180 stat = pXRRSetScreenConfig (gdi_display, sc, root, size, rot, CurrentTime);
185 pXRRFreeScreenConfigInfo(sc);
187 if (stat == RRSetConfigSuccess)
189 X11DRV_resize_desktop( dd_modes[mode].width, dd_modes[mode].height );
190 return DISP_CHANGE_SUCCESSFUL;
193 ERR("Resolution change not successful -- perhaps display has changed?\n");
194 return DISP_CHANGE_FAILED;
197 static void xrandr_init_modes(void)
199 int i, j, nmodes = 0;
201 real_xrandr_sizes = pXRRSizes( gdi_display, DefaultScreen(gdi_display), &real_xrandr_sizes_count);
202 if (real_xrandr_sizes_count <= 0) return;
204 TRACE("XRandR: found %u sizes.\n", real_xrandr_sizes_count);
205 real_xrandr_rates = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
206 sizeof(short *) * real_xrandr_sizes_count );
207 real_xrandr_rates_count = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
208 sizeof(int) * real_xrandr_sizes_count );
209 for (i = 0; i < real_xrandr_sizes_count; ++i)
211 real_xrandr_rates[i] = pXRRRates( gdi_display, DefaultScreen(gdi_display),
212 i, &real_xrandr_rates_count[i] );
213 TRACE("- at %d: %dx%d (%d rates):", i, real_xrandr_sizes[i].width,
214 real_xrandr_sizes[i].height, real_xrandr_rates_count[i]);
215 if (real_xrandr_rates_count[i])
217 nmodes += real_xrandr_rates_count[i];
218 for (j = 0; j < real_xrandr_rates_count[i]; ++j)
222 TRACE(" %d", real_xrandr_rates[i][j]);
233 real_xrandr_modes_count = nmodes;
234 TRACE("XRandR modes: count=%d\n", nmodes);
236 dd_modes = X11DRV_Settings_SetHandlers( "XRandR",
237 X11DRV_XRandR_GetCurrentMode,
238 X11DRV_XRandR_SetCurrentMode,
241 for (i = 0; i < real_xrandr_sizes_count; ++i)
243 if (real_xrandr_rates_count[i])
245 for (j = 0; j < real_xrandr_rates_count[i]; ++j)
247 X11DRV_Settings_AddOneMode( real_xrandr_sizes[i].width,
248 real_xrandr_sizes[i].height,
249 0, real_xrandr_rates[i][j] );
254 X11DRV_Settings_AddOneMode( real_xrandr_sizes[i].width,
255 real_xrandr_sizes[i].height,
260 X11DRV_Settings_AddDepthModes();
261 dd_mode_count = X11DRV_Settings_GetModeCount();
263 TRACE("Available DD modes: count=%d\n", dd_mode_count);
264 TRACE("Enabling XRandR\n");
267 void X11DRV_XRandR_Init(void)
269 int event_base, error_base, minor;
273 if (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 if (!pXRRQueryExtension( gdi_display, &event_base, &error_base )) goto done;
281 X11DRV_expect_error( gdi_display, XRandRErrorHandler, NULL );
282 ok = pXRRQueryVersion( gdi_display, &major, &minor );
283 if (X11DRV_check_error() || !ok) goto done;
285 TRACE("Found XRandR %d.%d.\n", major, minor);
292 #else /* SONAME_LIBXRANDR */
294 void X11DRV_XRandR_Init(void)
296 TRACE("XRandR support not compiled in.\n");
299 #endif /* SONAME_LIBXRANDR */