Improved word wrapping and tab key handling.
[wine/multimedia.git] / dlls / x11drv / xrandr.c
blob96b5a7ac92b3e04e82fb683ffacb749ea6ae40c8
1 /*
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
21 #include "config.h"
22 #include "wine/port.h"
23 #include <string.h>
24 #include <stdio.h>
26 #ifdef HAVE_LIBXRANDR
28 #include <X11/Xlib.h>
29 #include <X11/extensions/Xrandr.h>
30 #include "x11drv.h"
32 #include "x11ddraw.h"
33 #include "xrandr.h"
35 #include "windef.h"
36 #include "winbase.h"
37 #include "wingdi.h"
38 #include "ddrawi.h"
39 #include "wine/library.h"
40 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(xrandr);
44 static void *xrandr_handle;
46 /* some default values just in case */
47 #ifndef SONAME_LIBX11
48 #define SONAME_LIBX11 "libX11.so"
49 #endif
50 #ifndef SONAME_LIBXEXT
51 #define SONAME_LIBXEXT "libXext.so"
52 #endif
53 #ifndef SONAME_LIBXRENDER
54 #define SONAME_LIBXRENDER "libXrender.so"
55 #endif
56 #ifndef SONAME_LIBXRANDR
57 #define SONAME_LIBXRANDR "libXrandr.so"
58 #endif
60 #define MAKE_FUNCPTR(f) static typeof(f) * p##f;
61 MAKE_FUNCPTR(XRRConfigCurrentConfiguration)
62 MAKE_FUNCPTR(XRRConfigCurrentRate)
63 MAKE_FUNCPTR(XRRFreeScreenConfigInfo)
64 MAKE_FUNCPTR(XRRGetScreenInfo)
65 MAKE_FUNCPTR(XRRQueryExtension)
66 MAKE_FUNCPTR(XRRQueryVersion)
67 MAKE_FUNCPTR(XRRRates)
68 MAKE_FUNCPTR(XRRSetScreenConfig)
69 MAKE_FUNCPTR(XRRSetScreenConfigAndRate)
70 MAKE_FUNCPTR(XRRSizes)
71 #undef MAKE_FUNCPTR
73 extern int usexrandr;
75 static int xrandr_event, xrandr_error, xrandr_major, xrandr_minor;
77 static LPDDHALMODEINFO dd_modes;
78 static unsigned int dd_mode_count;
79 static XRRScreenSize *real_xrandr_sizes;
80 static short **real_xrandr_rates;
81 static int real_xrandr_sizes_count;
82 static int *real_xrandr_rates_count;
83 static unsigned int real_xrandr_modes_count;
85 static int load_xrandr(void)
87 int r = 0;
89 if (wine_dlopen(SONAME_LIBX11, RTLD_NOW|RTLD_GLOBAL, NULL, 0) &&
90 wine_dlopen(SONAME_LIBXEXT, RTLD_NOW|RTLD_GLOBAL, NULL, 0) &&
91 wine_dlopen(SONAME_LIBXRENDER, RTLD_NOW|RTLD_GLOBAL, NULL, 0) &&
92 (xrandr_handle = wine_dlopen(SONAME_LIBXRANDR, RTLD_NOW, NULL, 0)))
95 #define LOAD_FUNCPTR(f) \
96 if((p##f = wine_dlsym(xrandr_handle, #f, NULL, 0)) == NULL) \
97 goto sym_not_found;
99 LOAD_FUNCPTR(XRRConfigCurrentConfiguration)
100 LOAD_FUNCPTR(XRRConfigCurrentRate)
101 LOAD_FUNCPTR(XRRFreeScreenConfigInfo)
102 LOAD_FUNCPTR(XRRGetScreenInfo)
103 LOAD_FUNCPTR(XRRQueryExtension)
104 LOAD_FUNCPTR(XRRQueryVersion)
105 LOAD_FUNCPTR(XRRRates)
106 LOAD_FUNCPTR(XRRSetScreenConfig)
107 LOAD_FUNCPTR(XRRSetScreenConfigAndRate)
108 LOAD_FUNCPTR(XRRSizes)
110 #undef LOAD_FUNCPTR
112 r = 1; /* success */
114 sym_not_found:
115 if (!r) TRACE("Unable to load function ptrs from XRandR library\n");
117 return r;
120 static int XRandRErrorHandler(Display *dpy, XErrorEvent *event, void *arg)
122 return 1;
126 /* create the mode structures */
127 static void make_modes(void)
129 unsigned int i;
130 int j;
131 for (i=0; i<real_xrandr_sizes_count; i++)
133 if (real_xrandr_rates_count[i])
135 for (j=0; j < real_xrandr_rates_count[i]; j++)
137 X11DRV_Settings_AddOneMode(real_xrandr_sizes[i].width,
138 real_xrandr_sizes[i].height,
139 0, real_xrandr_rates[i][j]);
142 else
144 X11DRV_Settings_AddOneMode(real_xrandr_sizes[i].width,
145 real_xrandr_sizes[i].height,
146 0, 0);
151 static int X11DRV_XRandR_GetCurrentMode(void)
153 SizeID size;
154 Rotation rot;
155 Window root;
156 XRRScreenConfiguration *sc;
157 short rate;
158 unsigned int i;
159 int res = -1;
161 wine_tsx11_lock();
162 root = RootWindow (gdi_display, DefaultScreen(gdi_display));
163 sc = pXRRGetScreenInfo (gdi_display, root);
164 size = pXRRConfigCurrentConfiguration (sc, &rot);
165 rate = pXRRConfigCurrentRate (sc);
166 for (i = 0; i < real_xrandr_modes_count; i++)
168 if ( (dd_modes[i].dwWidth == real_xrandr_sizes[size].width ) &&
169 (dd_modes[i].dwHeight == real_xrandr_sizes[size].height) &&
170 (dd_modes[i].wRefreshRate == rate ) )
172 res = i;
175 pXRRFreeScreenConfigInfo(sc);
176 wine_tsx11_unlock();
177 if (res == -1)
179 ERR("In unknown mode, returning default\n");
180 res = 0;
182 return res;
185 static void X11DRV_XRandR_SetCurrentMode(int mode)
187 SizeID size;
188 Rotation rot;
189 Window root;
190 XRRScreenConfiguration *sc;
191 Status stat = RRSetConfigSuccess;
192 short rate;
193 unsigned int i;
194 int j;
195 DWORD dwBpp = screen_depth;
196 if (dwBpp == 24) dwBpp = 32;
198 wine_tsx11_lock();
199 root = RootWindow (gdi_display, DefaultScreen(gdi_display));
200 sc = pXRRGetScreenInfo (gdi_display, root);
201 size = pXRRConfigCurrentConfiguration (sc, &rot);
202 if (dwBpp != dd_modes[mode].dwBPP)
204 FIXME("Cannot change screen BPP from %ld to %ld\n", dwBpp, dd_modes[mode].dwBPP);
206 mode = mode%real_xrandr_modes_count;
208 TRACE("Changing Resolution to %ldx%ld @%d Hz\n",
209 dd_modes[mode].dwWidth,
210 dd_modes[mode].dwHeight,
211 dd_modes[mode].wRefreshRate);
213 for (i = 0; i < real_xrandr_sizes_count; i++)
215 if ( (dd_modes[mode].dwWidth == real_xrandr_sizes[i].width ) &&
216 (dd_modes[mode].dwHeight == real_xrandr_sizes[i].height) )
218 size = i;
219 if (real_xrandr_rates_count[i])
221 for (j=0; j < real_xrandr_rates_count[i]; j++)
223 if (dd_modes[mode].wRefreshRate == real_xrandr_rates[i][j])
225 rate = real_xrandr_rates[i][j];
226 TRACE("Resizing X display to %ldx%ld @%d Hz\n",
227 dd_modes[mode].dwWidth, dd_modes[mode].dwHeight, rate);
228 stat = pXRRSetScreenConfigAndRate (gdi_display, sc, root,
229 size, rot, rate, CurrentTime);
233 else
235 TRACE("Resizing X display to %ldx%ld <default Hz>\n",
236 dd_modes[mode].dwWidth, dd_modes[mode].dwHeight);
237 stat = pXRRSetScreenConfig (gdi_display, sc, root, size, rot, CurrentTime);
241 pXRRFreeScreenConfigInfo(sc);
242 wine_tsx11_unlock();
243 if (stat == RRSetConfigSuccess)
244 X11DRV_handle_desktop_resize( dd_modes[mode].dwWidth, dd_modes[mode].dwHeight );
245 else
246 ERR("Resolution change not successful -- perhaps display has changed?\n");
249 void X11DRV_XRandR_Init(void)
251 Bool ok;
252 int nmodes = 0;
253 unsigned int i;
255 if (xrandr_major) return; /* already initialized? */
256 if (!usexrandr) return; /* disabled in config */
257 if (using_wine_desktop) return; /* not compatible with desktop mode */
258 if (!load_xrandr()) return; /* can't load the Xrandr library */
260 /* see if Xrandr is available */
261 wine_tsx11_lock();
262 ok = pXRRQueryExtension(gdi_display, &xrandr_event, &xrandr_error);
263 if (ok)
265 X11DRV_expect_error(gdi_display, XRandRErrorHandler, NULL);
266 ok = pXRRQueryVersion(gdi_display, &xrandr_major, &xrandr_minor);
267 if (X11DRV_check_error()) ok = FALSE;
269 if (ok)
271 TRACE("Found XRandR - major: %d, minor: %d\n", xrandr_major, xrandr_minor);
272 /* retrieve modes */
273 real_xrandr_sizes = pXRRSizes(gdi_display, DefaultScreen(gdi_display), &real_xrandr_sizes_count);
274 ok = (real_xrandr_sizes_count>0);
276 if (ok)
278 TRACE("XRandR: found %u resolutions sizes\n", real_xrandr_sizes_count);
279 real_xrandr_rates = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(short *) * real_xrandr_sizes_count);
280 real_xrandr_rates_count = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(int) * real_xrandr_sizes_count);
281 for (i=0; i < real_xrandr_sizes_count; i++)
283 real_xrandr_rates[i] = pXRRRates (gdi_display, DefaultScreen(gdi_display), i, &(real_xrandr_rates_count[i]));
284 TRACE("- at %u: %dx%d (%d rates):", i, real_xrandr_sizes[i].width, real_xrandr_sizes[i].height, real_xrandr_rates_count[i]);
285 if (real_xrandr_rates_count[i])
287 int j;
288 nmodes += real_xrandr_rates_count[i];
289 for (j = 0; j < real_xrandr_rates_count[i]; ++j) {
290 if (j > 0) TRACE(",");
291 TRACE(" %d", real_xrandr_rates[i][j]);
294 else
296 nmodes++;
297 TRACE(" <default>");
299 TRACE(" Hz\n");
302 wine_tsx11_unlock();
303 if (!ok) return;
305 real_xrandr_modes_count = nmodes;
306 TRACE("XRandR modes: count=%d\n", nmodes);
308 dd_modes = X11DRV_Settings_SetHandlers("XRandR",
309 X11DRV_XRandR_GetCurrentMode,
310 X11DRV_XRandR_SetCurrentMode,
311 nmodes, 1);
312 make_modes();
313 X11DRV_Settings_AddDepthModes();
314 dd_mode_count = X11DRV_Settings_GetModeCount();
315 X11DRV_Settings_SetDefaultMode(0);
317 TRACE("Available DD modes: count=%d\n", dd_mode_count);
318 TRACE("Enabling XRandR\n");
321 void X11DRV_XRandR_Cleanup(void)
323 HeapFree(GetProcessHeap(), 0, real_xrandr_rates);
324 real_xrandr_rates = NULL;
325 HeapFree(GetProcessHeap(), 0, real_xrandr_rates_count);
326 real_xrandr_rates_count = NULL;
329 #endif /* HAVE_LIBXRANDR */