Don't crash on NULL tab text.
[wine] / dlls / x11drv / xrandr.c
1 /*
2  * Wine X11drv Xrandr interface
3  *
4  * Copyright 2003 Alexander James Pasadyn
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23 #include <string.h>
24 #include <stdio.h>
25
26 #ifdef HAVE_LIBXRANDR
27
28 #include <X11/Xlib.h>
29 #include <X11/extensions/Xrandr.h>
30 #include "x11drv.h"
31
32 #include "x11ddraw.h"
33 #include "xrandr.h"
34
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"
41
42 WINE_DEFAULT_DEBUG_CHANNEL(xrandr);
43
44 static void *xrandr_handle;
45
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
59
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
72
73 extern int usexrandr;
74
75 static int xrandr_event, xrandr_error, xrandr_major, xrandr_minor;
76
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 unsigned int real_xrandr_sizes_count;
82 static int *real_xrandr_rates_count;
83 static unsigned int real_xrandr_modes_count;
84
85 int load_xrandr(void)
86 {
87     int r = 0;
88
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)))
93     {
94
95 #define LOAD_FUNCPTR(f) \
96         if((p##f = wine_dlsym(xrandr_handle, #f, NULL, 0)) == NULL) \
97             goto sym_not_found;
98
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)
109
110 #undef LOAD_FUNCPTR
111
112         r = 1;   /* success */
113
114 sym_not_found:
115         if (!r)  TRACE("Unable to load function ptrs from XRandR library\n");
116     }
117     return r;
118 }
119
120 static int XRandRErrorHandler(Display *dpy, XErrorEvent *event, void *arg)
121 {
122     return 1;
123 }
124
125
126 /* create the mode structures */
127 static void make_modes(void)
128 {
129     int i, j;
130     for (i=0; i<real_xrandr_sizes_count; i++)
131     {
132         if (real_xrandr_rates_count[i])
133         {
134             for (j=0; j < real_xrandr_rates_count[i]; j++)
135             {
136                 X11DRV_Settings_AddOneMode(real_xrandr_sizes[i].width, 
137                                            real_xrandr_sizes[i].height, 
138                                            0, real_xrandr_rates[i][j]);
139             }
140         }
141         else
142         {
143             X11DRV_Settings_AddOneMode(real_xrandr_sizes[i].width, 
144                                        real_xrandr_sizes[i].height, 
145                                        0, 0);
146         }
147     }
148 }
149
150 static int X11DRV_XRandR_GetCurrentMode(void)
151 {
152     SizeID size;
153     Rotation rot;
154     Window root;
155     XRRScreenConfiguration *sc;
156     short rate;
157     int i;
158     int res = -1;
159     
160     wine_tsx11_lock();
161     root = RootWindow (gdi_display, DefaultScreen(gdi_display));
162     sc = pXRRGetScreenInfo (gdi_display, root);
163     size = pXRRConfigCurrentConfiguration (sc, &rot);
164     rate = pXRRConfigCurrentRate (sc);
165     for (i = 0; i < real_xrandr_modes_count; i++)
166     {
167         if ( (dd_modes[i].dwWidth      == real_xrandr_sizes[size].width ) &&
168              (dd_modes[i].dwHeight     == real_xrandr_sizes[size].height) &&
169              (dd_modes[i].wRefreshRate == rate                          ) )
170           {
171               res = i;
172           }
173     }
174     pXRRFreeScreenConfigInfo(sc);
175     wine_tsx11_unlock();
176     if (res == -1)
177     {
178         ERR("In unknown mode, returning default\n");
179         res = 0;
180     }
181     return res;
182 }
183
184 static void X11DRV_XRandR_SetCurrentMode(int mode)
185 {
186     SizeID size;
187     Rotation rot;
188     Window root;
189     XRRScreenConfiguration *sc;
190     Status stat = RRSetConfigSuccess;
191     short rate;
192     int i, j;
193     DWORD dwBpp = screen_depth;
194     if (dwBpp == 24) dwBpp = 32;
195     
196     wine_tsx11_lock();
197     root = RootWindow (gdi_display, DefaultScreen(gdi_display));
198     sc = pXRRGetScreenInfo (gdi_display, root);
199     size = pXRRConfigCurrentConfiguration (sc, &rot);
200     if (dwBpp != dd_modes[mode].dwBPP)
201     {
202         FIXME("Cannot change screen BPP from %ld to %ld\n", dwBpp, dd_modes[mode].dwBPP);
203     }
204     mode = mode%real_xrandr_modes_count;
205     for (i = 0; i < real_xrandr_sizes_count; i++)
206     {
207         if ( (dd_modes[mode].dwWidth  == real_xrandr_sizes[i].width ) && 
208              (dd_modes[mode].dwHeight == real_xrandr_sizes[i].height) )
209         {
210             size = i;
211             if (real_xrandr_rates_count[i])
212             {
213                 for (j=0; j < real_xrandr_rates_count[i]; j++)
214                 {
215                     if (dd_modes[mode].wRefreshRate == real_xrandr_rates[i][j])
216                     {
217                         rate = real_xrandr_rates[i][j];
218                         TRACE("Resizing X display to %ldx%ld @%d Hz\n", 
219                               dd_modes[mode].dwWidth, dd_modes[mode].dwHeight, rate);
220                         stat = pXRRSetScreenConfigAndRate (gdi_display, sc, root, 
221                                                           size, rot, rate, CurrentTime);
222                     }
223                 }
224             }
225             else
226             {
227                 TRACE("Resizing X display to %ldx%ld\n", 
228                       dd_modes[mode].dwWidth, dd_modes[mode].dwHeight);
229                 stat = pXRRSetScreenConfig (gdi_display, sc, root, 
230                                            size, rot, CurrentTime);
231             }
232         }
233     }
234     pXRRFreeScreenConfigInfo(sc);
235     wine_tsx11_unlock();
236     if (stat == RRSetConfigSuccess)
237         X11DRV_handle_desktop_resize( dd_modes[mode].dwWidth, dd_modes[mode].dwHeight );
238     else
239         ERR("Resolution change not successful -- perhaps display has changed?\n");
240 }
241
242 void X11DRV_XRandR_Init(void)
243 {
244     Bool ok;
245     int nmodes = 0;
246     int i;
247
248     if (xrandr_major) return; /* already initialized? */
249     if (!usexrandr) return; /* disabled in config */
250     if (using_wine_desktop) return; /* not compatible with desktop mode */
251     if (!load_xrandr()) return;  /* can't load the Xrandr library */
252
253     /* see if Xrandr is available */
254     wine_tsx11_lock();
255     ok = pXRRQueryExtension(gdi_display, &xrandr_event, &xrandr_error);
256     if (ok)
257     {
258         X11DRV_expect_error(gdi_display, XRandRErrorHandler, NULL);
259         ok = pXRRQueryVersion(gdi_display, &xrandr_major, &xrandr_minor);
260         if (X11DRV_check_error()) ok = FALSE;
261     }
262     if (ok)
263     {
264         TRACE("Found XRandR - major: %d, minor: %d\n", xrandr_major, xrandr_minor);
265         /* retrieve modes */
266         real_xrandr_sizes = pXRRSizes(gdi_display, DefaultScreen(gdi_display), &real_xrandr_sizes_count);
267         ok = (real_xrandr_sizes_count>0);
268     }
269     if (ok)
270     {
271         real_xrandr_rates = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(short *) * real_xrandr_sizes_count);
272         real_xrandr_rates_count = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(int) * real_xrandr_sizes_count);
273         for (i=0; i < real_xrandr_sizes_count; i++)
274         {
275             real_xrandr_rates[i] = pXRRRates (gdi_display, DefaultScreen(gdi_display), i, &(real_xrandr_rates_count[i]));
276             if (real_xrandr_rates_count[i])
277             {
278                 nmodes += real_xrandr_rates_count[i];
279             }
280             else
281             {
282                 nmodes++;
283             }
284         }
285     }
286     wine_tsx11_unlock();
287     if (!ok) return;
288
289     real_xrandr_modes_count = nmodes;
290     TRACE("XRandR modes: count=%d\n", nmodes);
291
292     dd_modes = X11DRV_Settings_SetHandlers("XRandR", 
293                                            X11DRV_XRandR_GetCurrentMode, 
294                                            X11DRV_XRandR_SetCurrentMode, 
295                                            nmodes, 1);
296     make_modes();
297     X11DRV_Settings_AddDepthModes();
298     dd_mode_count = X11DRV_Settings_GetModeCount();
299     X11DRV_Settings_SetDefaultMode(0);
300
301     TRACE("Available DD modes: count=%d\n", dd_mode_count);
302     TRACE("Enabling XRandR\n");
303 }
304
305 void X11DRV_XRandR_Cleanup(void)
306 {
307     if (real_xrandr_rates)
308     {
309         HeapFree(GetProcessHeap(), 0, real_xrandr_rates);
310         real_xrandr_rates = NULL;
311     }
312     if (real_xrandr_rates_count)
313     {
314         HeapFree(GetProcessHeap(), 0, real_xrandr_rates_count);
315         real_xrandr_rates_count = NULL;
316     }
317 }
318
319 #endif /* HAVE_LIBXRANDR */