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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include <X11/extensions/Xrandr.h>
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(xrandr);
45 static int xrandr_event, xrandr_error, xrandr_major, xrandr_minor;
47 static LPDDHALMODEINFO dd_modes;
48 static unsigned int dd_mode_count;
49 static XRRScreenSize *real_xrandr_sizes;
50 static short **real_xrandr_rates;
51 static unsigned int real_xrandr_sizes_count;
52 static int *real_xrandr_rates_count;
53 static unsigned int real_xrandr_modes_count;
55 static int XRandRErrorHandler(Display *dpy, XErrorEvent *event, void *arg)
60 static Bool in_desktop_mode;
61 static const unsigned int depths[] = {8, 16, 32};
63 /* create the mode structures */
64 static void make_modes(void)
67 for (i=0; i<real_xrandr_sizes_count; i++)
69 if (real_xrandr_rates_count[i])
71 for (j=0; j < real_xrandr_rates_count[i]; j++)
73 X11DRV_Settings_AddOneMode(real_xrandr_sizes[i].width,
74 real_xrandr_sizes[i].height,
75 0, real_xrandr_rates[i][j]);
80 X11DRV_Settings_AddOneMode(real_xrandr_sizes[i].width,
81 real_xrandr_sizes[i].height,
87 static int X11DRV_XRandR_GetCurrentMode(void)
92 XRRScreenConfiguration *sc;
98 root = RootWindow (gdi_display, DefaultScreen(gdi_display));
99 sc = XRRGetScreenInfo (gdi_display, root);
100 size = XRRConfigCurrentConfiguration (sc, &rot);
101 rate = XRRConfigCurrentRate (sc);
102 for (i = 0; i < real_xrandr_modes_count; i++)
104 if ( (dd_modes[i].dwWidth == real_xrandr_sizes[size].width ) &&
105 (dd_modes[i].dwHeight == real_xrandr_sizes[size].height) &&
106 (dd_modes[i].wRefreshRate == rate ) )
111 XRRFreeScreenConfigInfo(sc);
115 ERR("In unknown mode, returning default\n");
121 static void X11DRV_XRandR_SetCurrentMode(int mode)
126 XRRScreenConfiguration *sc;
127 Status stat = RRSetConfigSuccess;
130 DWORD dwBpp = screen_depth;
131 if (dwBpp == 24) dwBpp = 32;
134 root = RootWindow (gdi_display, DefaultScreen(gdi_display));
135 sc = XRRGetScreenInfo (gdi_display, root);
136 size = XRRConfigCurrentConfiguration (sc, &rot);
137 if (dwBpp != dd_modes[mode].dwBPP)
139 FIXME("Cannot change screen BPP from %ld to %ld\n", dwBpp, dd_modes[mode].dwBPP);
141 mode = mode%real_xrandr_modes_count;
142 for (i = 0; i < real_xrandr_sizes_count; i++)
144 if ( (dd_modes[mode].dwWidth == real_xrandr_sizes[i].width ) &&
145 (dd_modes[mode].dwHeight == real_xrandr_sizes[i].height) )
148 if (real_xrandr_rates_count[i])
150 for (j=0; j < real_xrandr_rates_count[i]; j++)
152 if (dd_modes[mode].wRefreshRate == real_xrandr_rates[i][j])
154 rate = real_xrandr_rates[i][j];
155 TRACE("Resizing X display to %ldx%ld @%d Hz\n",
156 dd_modes[mode].dwWidth, dd_modes[mode].dwHeight, rate);
157 stat = XRRSetScreenConfigAndRate (gdi_display, sc, root,
158 size, rot, rate, CurrentTime);
159 FIXME("Need to update SYSMETRICS after resizing display (now %ldx%ld)\n",
160 dd_modes[mode].dwWidth, dd_modes[mode].dwHeight);
166 TRACE("Resizing X display to %ldx%ld\n",
167 dd_modes[mode].dwWidth, dd_modes[mode].dwHeight);
168 stat = XRRSetScreenConfig (gdi_display, sc, root,
169 size, rot, CurrentTime);
170 FIXME("Need to update SYSMETRICS after resizing display (now %ldx%ld)\n",
171 dd_modes[mode].dwWidth, dd_modes[mode].dwHeight);
175 if (stat != RRSetConfigSuccess)
177 ERR("Resolution change not successful -- perhaps display has chaned?");
179 XRRFreeScreenConfigInfo(sc);
183 void X11DRV_XRandR_Init(void)
188 in_desktop_mode = (root_window != DefaultRootWindow(gdi_display));
190 if (xrandr_major) return; /* already initialized? */
191 if (!usexrandr) return; /* disabled in config */
192 if (in_desktop_mode) return; /* not compatible with desktop mode */
194 /* see if Xrandr is available */
196 ok = XRRQueryExtension(gdi_display, &xrandr_event, &xrandr_error);
199 X11DRV_expect_error(gdi_display, XRandRErrorHandler, NULL);
200 ok = XRRQueryVersion(gdi_display, &xrandr_major, &xrandr_minor);
201 if (X11DRV_check_error()) ok = FALSE;
205 TRACE("Found XRandR - major: %d, minor: %d\n", xrandr_major, xrandr_minor);
207 real_xrandr_sizes = XRRSizes(gdi_display, DefaultScreen(gdi_display), &real_xrandr_sizes_count);
208 ok = (real_xrandr_sizes_count>0);
212 real_xrandr_rates = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(short *) * real_xrandr_sizes_count);
213 real_xrandr_rates_count = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(int) * real_xrandr_sizes_count);
214 for (i=0; i < real_xrandr_sizes_count; i++)
216 real_xrandr_rates[i] = XRRRates (gdi_display, DefaultScreen(gdi_display), i, &(real_xrandr_rates_count[i]));
217 if (real_xrandr_rates_count[i])
219 nmodes += real_xrandr_rates_count[i];
230 real_xrandr_modes_count = nmodes;
231 TRACE("XRandR modes: count=%d\n", nmodes);
233 dd_modes = X11DRV_Settings_SetHandlers("XRandR",
234 X11DRV_XRandR_GetCurrentMode,
235 X11DRV_XRandR_SetCurrentMode,
238 X11DRV_Settings_AddDepthModes();
239 dd_mode_count = X11DRV_Settings_GetModeCount();
240 X11DRV_Settings_SetDefaultMode(0);
242 TRACE("Available DD modes: count=%d\n", dd_mode_count);
243 TRACE("Enabling XRandR\n");
246 void X11DRV_XRandR_Cleanup(void)
248 if (real_xrandr_rates)
250 HeapFree(GetProcessHeap(), 0, real_xrandr_rates);
251 real_xrandr_rates = NULL;
253 if (real_xrandr_rates_count)
255 HeapFree(GetProcessHeap(), 0, real_xrandr_rates_count);
256 real_xrandr_rates_count = NULL;
260 #endif /* HAVE_LIBXRANDR */