rundll32: Check more heap allocation failure paths for consistency.
[wine] / dlls / winex11.drv / 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23 #include <string.h>
24 #include <stdio.h>
25
26 #ifdef SONAME_LIBXRANDR
27
28 #include <X11/Xlib.h>
29 #include <X11/extensions/Xrandr.h>
30 #include "x11drv.h"
31
32 #include "xrandr.h"
33
34 #include "windef.h"
35 #include "winbase.h"
36 #include "wingdi.h"
37 #include "ddrawi.h"
38 #include "wine/library.h"
39 #include "wine/debug.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(xrandr);
42
43 static void *xrandr_handle;
44
45 #define MAKE_FUNCPTR(f) static typeof(f) * p##f;
46 MAKE_FUNCPTR(XRRConfigCurrentConfiguration)
47 MAKE_FUNCPTR(XRRConfigCurrentRate)
48 MAKE_FUNCPTR(XRRFreeScreenConfigInfo)
49 MAKE_FUNCPTR(XRRGetScreenInfo)
50 MAKE_FUNCPTR(XRRQueryExtension)
51 MAKE_FUNCPTR(XRRQueryVersion)
52 MAKE_FUNCPTR(XRRRates)
53 MAKE_FUNCPTR(XRRSetScreenConfig)
54 MAKE_FUNCPTR(XRRSetScreenConfigAndRate)
55 MAKE_FUNCPTR(XRRSizes)
56 #undef MAKE_FUNCPTR
57
58 extern int usexrandr;
59
60 static int xrandr_event, xrandr_error, xrandr_major, xrandr_minor;
61
62 static LPDDHALMODEINFO dd_modes;
63 static unsigned int dd_mode_count;
64 static XRRScreenSize *real_xrandr_sizes;
65 static short **real_xrandr_rates;
66 static int real_xrandr_sizes_count;
67 static int *real_xrandr_rates_count;
68 static unsigned int real_xrandr_modes_count;
69
70 static int load_xrandr(void)
71 {
72     int r = 0;
73
74     if (wine_dlopen(SONAME_LIBX11, RTLD_NOW|RTLD_GLOBAL, NULL, 0) &&
75         wine_dlopen(SONAME_LIBXEXT, RTLD_NOW|RTLD_GLOBAL, NULL, 0) &&
76         wine_dlopen(SONAME_LIBXRENDER, RTLD_NOW|RTLD_GLOBAL, NULL, 0) &&
77         (xrandr_handle = wine_dlopen(SONAME_LIBXRANDR, RTLD_NOW, NULL, 0)))
78     {
79
80 #define LOAD_FUNCPTR(f) \
81         if((p##f = wine_dlsym(xrandr_handle, #f, NULL, 0)) == NULL) \
82             goto sym_not_found;
83
84         LOAD_FUNCPTR(XRRConfigCurrentConfiguration)
85         LOAD_FUNCPTR(XRRConfigCurrentRate)
86         LOAD_FUNCPTR(XRRFreeScreenConfigInfo)
87         LOAD_FUNCPTR(XRRGetScreenInfo)
88         LOAD_FUNCPTR(XRRQueryExtension)
89         LOAD_FUNCPTR(XRRQueryVersion)
90         LOAD_FUNCPTR(XRRRates)
91         LOAD_FUNCPTR(XRRSetScreenConfig)
92         LOAD_FUNCPTR(XRRSetScreenConfigAndRate)
93         LOAD_FUNCPTR(XRRSizes)
94
95 #undef LOAD_FUNCPTR
96
97         r = 1;   /* success */
98
99 sym_not_found:
100         if (!r)  TRACE("Unable to load function ptrs from XRandR library\n");
101     }
102     return r;
103 }
104
105 static int XRandRErrorHandler(Display *dpy, XErrorEvent *event, void *arg)
106 {
107     return 1;
108 }
109
110
111 /* create the mode structures */
112 static void make_modes(void)
113 {
114     int i, j;
115
116     for (i=0; i<real_xrandr_sizes_count; i++)
117     {
118         if (real_xrandr_rates_count[i])
119         {
120             for (j=0; j < real_xrandr_rates_count[i]; j++)
121             {
122                 X11DRV_Settings_AddOneMode(real_xrandr_sizes[i].width, 
123                                            real_xrandr_sizes[i].height, 
124                                            0, real_xrandr_rates[i][j]);
125             }
126         }
127         else
128         {
129             X11DRV_Settings_AddOneMode(real_xrandr_sizes[i].width, 
130                                        real_xrandr_sizes[i].height, 
131                                        0, 0);
132         }
133     }
134 }
135
136 static int X11DRV_XRandR_GetCurrentMode(void)
137 {
138     SizeID size;
139     Rotation rot;
140     Window root;
141     XRRScreenConfiguration *sc;
142     short rate;
143     unsigned int i;
144     int res = -1;
145     
146     wine_tsx11_lock();
147     root = RootWindow (gdi_display, DefaultScreen(gdi_display));
148     sc = pXRRGetScreenInfo (gdi_display, root);
149     size = pXRRConfigCurrentConfiguration (sc, &rot);
150     rate = pXRRConfigCurrentRate (sc);
151     pXRRFreeScreenConfigInfo(sc);
152     wine_tsx11_unlock();
153     for (i = 0; i < real_xrandr_modes_count; i++)
154     {
155         if ( (dd_modes[i].dwWidth      == real_xrandr_sizes[size].width ) &&
156              (dd_modes[i].dwHeight     == real_xrandr_sizes[size].height) &&
157              (dd_modes[i].wRefreshRate == rate                          ) )
158           {
159               res = i;
160               break;
161           }
162     }
163     if (res == -1)
164     {
165         ERR("In unknown mode, returning default\n");
166         res = 0;
167     }
168     return res;
169 }
170
171 static LONG X11DRV_XRandR_SetCurrentMode(int mode)
172 {
173     SizeID size;
174     Rotation rot;
175     Window root;
176     XRRScreenConfiguration *sc;
177     Status stat = RRSetConfigSuccess;
178     short rate;
179     unsigned int i;
180     int j;
181
182     wine_tsx11_lock();
183     root = RootWindow (gdi_display, DefaultScreen(gdi_display));
184     sc = pXRRGetScreenInfo (gdi_display, root);
185     size = pXRRConfigCurrentConfiguration (sc, &rot);
186     mode = mode%real_xrandr_modes_count;
187
188     TRACE("Changing Resolution to %dx%d @%d Hz\n", 
189           dd_modes[mode].dwWidth, 
190           dd_modes[mode].dwHeight, 
191           dd_modes[mode].wRefreshRate);
192
193     for (i = 0; i < real_xrandr_sizes_count; i++)
194     {
195         if ( (dd_modes[mode].dwWidth  == real_xrandr_sizes[i].width ) && 
196              (dd_modes[mode].dwHeight == real_xrandr_sizes[i].height) )
197         {
198             size = i;
199             if (real_xrandr_rates_count[i])
200             {
201                 for (j=0; j < real_xrandr_rates_count[i]; j++)
202                 {
203                     if (dd_modes[mode].wRefreshRate == real_xrandr_rates[i][j])
204                     {
205                         rate = real_xrandr_rates[i][j];
206                         TRACE("Resizing X display to %dx%d @%d Hz\n", 
207                               dd_modes[mode].dwWidth, dd_modes[mode].dwHeight, rate);
208                         stat = pXRRSetScreenConfigAndRate (gdi_display, sc, root, 
209                                                           size, rot, rate, CurrentTime);
210                         break;
211                     }
212                 }
213             }
214             else
215             {
216                 TRACE("Resizing X display to %dx%d <default Hz>\n", 
217                       dd_modes[mode].dwWidth, dd_modes[mode].dwHeight);
218                 stat = pXRRSetScreenConfig (gdi_display, sc, root, size, rot, CurrentTime);
219             }
220             break;
221         }
222     }
223     pXRRFreeScreenConfigInfo(sc);
224     wine_tsx11_unlock();
225     if (stat == RRSetConfigSuccess)
226     {
227         X11DRV_resize_desktop( dd_modes[mode].dwWidth, dd_modes[mode].dwHeight );
228         return DISP_CHANGE_SUCCESSFUL;
229     }
230
231     ERR("Resolution change not successful -- perhaps display has changed?\n");
232     return DISP_CHANGE_FAILED;
233 }
234
235 void X11DRV_XRandR_Init(void)
236 {
237     Bool ok;
238     int i, nmodes = 0;
239
240     if (xrandr_major) return; /* already initialized? */
241     if (!usexrandr) return; /* disabled in config */
242     if (root_window != DefaultRootWindow( gdi_display )) return;
243     if (!load_xrandr()) return;  /* can't load the Xrandr library */
244
245     /* see if Xrandr is available */
246     wine_tsx11_lock();
247     ok = pXRRQueryExtension(gdi_display, &xrandr_event, &xrandr_error);
248     if (ok)
249     {
250         X11DRV_expect_error(gdi_display, XRandRErrorHandler, NULL);
251         ok = pXRRQueryVersion(gdi_display, &xrandr_major, &xrandr_minor);
252         if (X11DRV_check_error()) ok = FALSE;
253     }
254     if (ok)
255     {
256         TRACE("Found XRandR - major: %d, minor: %d\n", xrandr_major, xrandr_minor);
257         /* retrieve modes */
258         real_xrandr_sizes = pXRRSizes(gdi_display, DefaultScreen(gdi_display), &real_xrandr_sizes_count);
259         ok = (real_xrandr_sizes_count>0);
260     }
261     if (ok)
262     {
263         TRACE("XRandR: found %u resolutions sizes\n", real_xrandr_sizes_count);
264         real_xrandr_rates = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(short *) * real_xrandr_sizes_count);
265         real_xrandr_rates_count = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(int) * real_xrandr_sizes_count);
266         for (i=0; i < real_xrandr_sizes_count; i++)
267         {
268             real_xrandr_rates[i] = pXRRRates (gdi_display, DefaultScreen(gdi_display), i, &(real_xrandr_rates_count[i]));
269             TRACE("- at %d: %dx%d (%d rates):", i, real_xrandr_sizes[i].width, real_xrandr_sizes[i].height, real_xrandr_rates_count[i]);
270             if (real_xrandr_rates_count[i])
271             {
272                 int j;
273                 nmodes += real_xrandr_rates_count[i];
274                 for (j = 0; j < real_xrandr_rates_count[i]; ++j) {
275                   if (j > 0) TRACE(",");
276                   TRACE("  %d", real_xrandr_rates[i][j]);
277                 }
278             }
279             else
280             {
281                 nmodes++;
282                 TRACE(" <default>");
283             }
284             TRACE(" Hz\n");
285         }
286     }
287     wine_tsx11_unlock();
288     if (!ok) return;
289
290     real_xrandr_modes_count = nmodes;
291     TRACE("XRandR modes: count=%d\n", nmodes);
292
293     dd_modes = X11DRV_Settings_SetHandlers("XRandR", 
294                                            X11DRV_XRandR_GetCurrentMode, 
295                                            X11DRV_XRandR_SetCurrentMode, 
296                                            nmodes, 1);
297     make_modes();
298     X11DRV_Settings_AddDepthModes();
299     dd_mode_count = X11DRV_Settings_GetModeCount();
300
301     TRACE("Available DD modes: count=%d\n", dd_mode_count);
302     TRACE("Enabling XRandR\n");
303 }
304
305 #endif /* SONAME_LIBXRANDR */