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
28 #include <X11/extensions/Xrandr.h>
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(xrandr);
44 static int xrandr_event, xrandr_error, xrandr_major, xrandr_minor;
46 static LPDDHALMODEINFO dd_modes;
47 static unsigned int dd_mode_count;
48 static XRRScreenSize *real_xrandr_sizes;
49 static short **real_xrandr_rates;
50 static unsigned int real_xrandr_sizes_count;
51 static int *real_xrandr_rates_count;
52 static unsigned int real_xrandr_modes_count;
54 static int XRandRErrorHandler(Display *dpy, XErrorEvent *event, void *arg)
59 static Bool in_desktop_mode;
61 /* create the mode structures */
62 static void make_modes(void)
65 for (i=0; i<real_xrandr_sizes_count; i++)
67 if (real_xrandr_rates_count[i])
69 for (j=0; j < real_xrandr_rates_count[i]; j++)
71 X11DRV_Settings_AddOneMode(real_xrandr_sizes[i].width,
72 real_xrandr_sizes[i].height,
73 0, real_xrandr_rates[i][j]);
78 X11DRV_Settings_AddOneMode(real_xrandr_sizes[i].width,
79 real_xrandr_sizes[i].height,
85 static int X11DRV_XRandR_GetCurrentMode(void)
90 XRRScreenConfiguration *sc;
96 root = RootWindow (gdi_display, DefaultScreen(gdi_display));
97 sc = XRRGetScreenInfo (gdi_display, root);
98 size = XRRConfigCurrentConfiguration (sc, &rot);
99 rate = XRRConfigCurrentRate (sc);
100 for (i = 0; i < real_xrandr_modes_count; i++)
102 if ( (dd_modes[i].dwWidth == real_xrandr_sizes[size].width ) &&
103 (dd_modes[i].dwHeight == real_xrandr_sizes[size].height) &&
104 (dd_modes[i].wRefreshRate == rate ) )
109 XRRFreeScreenConfigInfo(sc);
113 ERR("In unknown mode, returning default\n");
119 static void X11DRV_XRandR_SetCurrentMode(int mode)
124 XRRScreenConfiguration *sc;
125 Status stat = RRSetConfigSuccess;
128 DWORD dwBpp = screen_depth;
129 if (dwBpp == 24) dwBpp = 32;
132 root = RootWindow (gdi_display, DefaultScreen(gdi_display));
133 sc = XRRGetScreenInfo (gdi_display, root);
134 size = XRRConfigCurrentConfiguration (sc, &rot);
135 if (dwBpp != dd_modes[mode].dwBPP)
137 FIXME("Cannot change screen BPP from %ld to %ld\n", dwBpp, dd_modes[mode].dwBPP);
139 mode = mode%real_xrandr_modes_count;
140 for (i = 0; i < real_xrandr_sizes_count; i++)
142 if ( (dd_modes[mode].dwWidth == real_xrandr_sizes[i].width ) &&
143 (dd_modes[mode].dwHeight == real_xrandr_sizes[i].height) )
146 if (real_xrandr_rates_count[i])
148 for (j=0; j < real_xrandr_rates_count[i]; j++)
150 if (dd_modes[mode].wRefreshRate == real_xrandr_rates[i][j])
152 rate = real_xrandr_rates[i][j];
153 TRACE("Resizing X display to %ldx%ld @%d Hz\n",
154 dd_modes[mode].dwWidth, dd_modes[mode].dwHeight, rate);
155 stat = XRRSetScreenConfigAndRate (gdi_display, sc, root,
156 size, rot, rate, CurrentTime);
157 FIXME("Need to update SYSMETRICS after resizing display (now %ldx%ld)\n",
158 dd_modes[mode].dwWidth, dd_modes[mode].dwHeight);
164 TRACE("Resizing X display to %ldx%ld\n",
165 dd_modes[mode].dwWidth, dd_modes[mode].dwHeight);
166 stat = XRRSetScreenConfig (gdi_display, sc, root,
167 size, rot, CurrentTime);
168 FIXME("Need to update SYSMETRICS after resizing display (now %ldx%ld)\n",
169 dd_modes[mode].dwWidth, dd_modes[mode].dwHeight);
173 if (stat != RRSetConfigSuccess)
175 ERR("Resolution change not successful -- perhaps display has chaned?");
177 XRRFreeScreenConfigInfo(sc);
181 void X11DRV_XRandR_Init(void)
186 in_desktop_mode = (root_window != DefaultRootWindow(gdi_display));
188 if (xrandr_major) return; /* already initialized? */
189 if (!usexrandr) return; /* disabled in config */
190 if (in_desktop_mode) return; /* not compatible with desktop mode */
192 /* see if Xrandr is available */
194 ok = XRRQueryExtension(gdi_display, &xrandr_event, &xrandr_error);
197 X11DRV_expect_error(gdi_display, XRandRErrorHandler, NULL);
198 ok = XRRQueryVersion(gdi_display, &xrandr_major, &xrandr_minor);
199 if (X11DRV_check_error()) ok = FALSE;
203 TRACE("Found XRandR - major: %d, minor: %d\n", xrandr_major, xrandr_minor);
205 real_xrandr_sizes = XRRSizes(gdi_display, DefaultScreen(gdi_display), &real_xrandr_sizes_count);
206 ok = (real_xrandr_sizes_count>0);
210 real_xrandr_rates = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(short *) * real_xrandr_sizes_count);
211 real_xrandr_rates_count = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(int) * real_xrandr_sizes_count);
212 for (i=0; i < real_xrandr_sizes_count; i++)
214 real_xrandr_rates[i] = XRRRates (gdi_display, DefaultScreen(gdi_display), i, &(real_xrandr_rates_count[i]));
215 if (real_xrandr_rates_count[i])
217 nmodes += real_xrandr_rates_count[i];
228 real_xrandr_modes_count = nmodes;
229 TRACE("XRandR modes: count=%d\n", nmodes);
231 dd_modes = X11DRV_Settings_SetHandlers("XRandR",
232 X11DRV_XRandR_GetCurrentMode,
233 X11DRV_XRandR_SetCurrentMode,
236 X11DRV_Settings_AddDepthModes();
237 dd_mode_count = X11DRV_Settings_GetModeCount();
238 X11DRV_Settings_SetDefaultMode(0);
240 TRACE("Available DD modes: count=%d\n", dd_mode_count);
241 TRACE("Enabling XRandR\n");
244 void X11DRV_XRandR_Cleanup(void)
246 if (real_xrandr_rates)
248 HeapFree(GetProcessHeap(), 0, real_xrandr_rates);
249 real_xrandr_rates = NULL;
251 if (real_xrandr_rates_count)
253 HeapFree(GetProcessHeap(), 0, real_xrandr_rates_count);
254 real_xrandr_rates_count = NULL;
258 #endif /* HAVE_LIBXRANDR */