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