wgl: Cleanup wglMakeCurrent.
[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 HAVE_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 /* some default values just in case */
46 #ifndef SONAME_LIBX11
47 #define SONAME_LIBX11 "libX11.so"
48 #endif
49 #ifndef SONAME_LIBXEXT
50 #define SONAME_LIBXEXT "libXext.so"
51 #endif
52 #ifndef SONAME_LIBXRENDER
53 #define SONAME_LIBXRENDER "libXrender.so"
54 #endif
55 #ifndef SONAME_LIBXRANDR
56 #define SONAME_LIBXRANDR "libXrandr.so"
57 #endif
58
59 #define MAKE_FUNCPTR(f) static typeof(f) * p##f;
60 MAKE_FUNCPTR(XRRConfigCurrentConfiguration)
61 MAKE_FUNCPTR(XRRConfigCurrentRate)
62 MAKE_FUNCPTR(XRRFreeScreenConfigInfo)
63 MAKE_FUNCPTR(XRRGetScreenInfo)
64 MAKE_FUNCPTR(XRRQueryExtension)
65 MAKE_FUNCPTR(XRRQueryVersion)
66 MAKE_FUNCPTR(XRRRates)
67 MAKE_FUNCPTR(XRRSetScreenConfig)
68 MAKE_FUNCPTR(XRRSetScreenConfigAndRate)
69 MAKE_FUNCPTR(XRRSizes)
70 #undef MAKE_FUNCPTR
71
72 extern int usexrandr;
73
74 static int xrandr_event, xrandr_error, xrandr_major, xrandr_minor;
75
76 static LPDDHALMODEINFO dd_modes;
77 static unsigned int dd_mode_count;
78 static XRRScreenSize *real_xrandr_sizes;
79 static short **real_xrandr_rates;
80 static int real_xrandr_sizes_count;
81 static int *real_xrandr_rates_count;
82 static unsigned int real_xrandr_modes_count;
83
84 static int load_xrandr(void)
85 {
86     int r = 0;
87
88     if (wine_dlopen(SONAME_LIBX11, RTLD_NOW|RTLD_GLOBAL, NULL, 0) &&
89         wine_dlopen(SONAME_LIBXEXT, RTLD_NOW|RTLD_GLOBAL, NULL, 0) &&
90         wine_dlopen(SONAME_LIBXRENDER, RTLD_NOW|RTLD_GLOBAL, NULL, 0) &&
91         (xrandr_handle = wine_dlopen(SONAME_LIBXRANDR, RTLD_NOW, NULL, 0)))
92     {
93
94 #define LOAD_FUNCPTR(f) \
95         if((p##f = wine_dlsym(xrandr_handle, #f, NULL, 0)) == NULL) \
96             goto sym_not_found;
97
98         LOAD_FUNCPTR(XRRConfigCurrentConfiguration)
99         LOAD_FUNCPTR(XRRConfigCurrentRate)
100         LOAD_FUNCPTR(XRRFreeScreenConfigInfo)
101         LOAD_FUNCPTR(XRRGetScreenInfo)
102         LOAD_FUNCPTR(XRRQueryExtension)
103         LOAD_FUNCPTR(XRRQueryVersion)
104         LOAD_FUNCPTR(XRRRates)
105         LOAD_FUNCPTR(XRRSetScreenConfig)
106         LOAD_FUNCPTR(XRRSetScreenConfigAndRate)
107         LOAD_FUNCPTR(XRRSizes)
108
109 #undef LOAD_FUNCPTR
110
111         r = 1;   /* success */
112
113 sym_not_found:
114         if (!r)  TRACE("Unable to load function ptrs from XRandR library\n");
115     }
116     return r;
117 }
118
119 static int XRandRErrorHandler(Display *dpy, XErrorEvent *event, void *arg)
120 {
121     return 1;
122 }
123
124
125 /* create the mode structures */
126 static void make_modes(void)
127 {
128     unsigned int i;
129     int 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     unsigned 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     unsigned int i;
193     int j;
194     DWORD dwBpp = screen_depth;
195     if (dwBpp == 24) dwBpp = 32;
196     
197     wine_tsx11_lock();
198     root = RootWindow (gdi_display, DefaultScreen(gdi_display));
199     sc = pXRRGetScreenInfo (gdi_display, root);
200     size = pXRRConfigCurrentConfiguration (sc, &rot);
201     if (dwBpp != dd_modes[mode].dwBPP)
202     {
203         FIXME("Cannot change screen BPP from %d to %d\n", dwBpp, dd_modes[mode].dwBPP);
204     }
205     mode = mode%real_xrandr_modes_count;
206
207     TRACE("Changing Resolution to %dx%d @%d Hz\n", 
208           dd_modes[mode].dwWidth, 
209           dd_modes[mode].dwHeight, 
210           dd_modes[mode].wRefreshRate);
211
212     for (i = 0; i < real_xrandr_sizes_count; i++)
213     {
214         if ( (dd_modes[mode].dwWidth  == real_xrandr_sizes[i].width ) && 
215              (dd_modes[mode].dwHeight == real_xrandr_sizes[i].height) )
216         {
217             size = i;
218             if (real_xrandr_rates_count[i])
219             {
220                 for (j=0; j < real_xrandr_rates_count[i]; j++)
221                 {
222                     if (dd_modes[mode].wRefreshRate == real_xrandr_rates[i][j])
223                     {
224                         rate = real_xrandr_rates[i][j];
225                         TRACE("Resizing X display to %dx%d @%d Hz\n", 
226                               dd_modes[mode].dwWidth, dd_modes[mode].dwHeight, rate);
227                         stat = pXRRSetScreenConfigAndRate (gdi_display, sc, root, 
228                                                           size, rot, rate, CurrentTime);
229                     }
230                 }
231             }
232             else
233             {
234                 TRACE("Resizing X display to %dx%d <default Hz>\n", 
235                       dd_modes[mode].dwWidth, dd_modes[mode].dwHeight);
236                 stat = pXRRSetScreenConfig (gdi_display, sc, root, size, rot, CurrentTime);
237             }
238         }
239     }
240     pXRRFreeScreenConfigInfo(sc);
241     wine_tsx11_unlock();
242     if (stat == RRSetConfigSuccess)
243         X11DRV_handle_desktop_resize( dd_modes[mode].dwWidth, dd_modes[mode].dwHeight );
244     else
245         ERR("Resolution change not successful -- perhaps display has changed?\n");
246 }
247
248 void X11DRV_XRandR_Init(void)
249 {
250     Bool ok;
251     int nmodes = 0;
252     unsigned int i;
253
254     if (xrandr_major) return; /* already initialized? */
255     if (!usexrandr) return; /* disabled in config */
256     if (root_window != DefaultRootWindow( gdi_display )) return;
257     if (!load_xrandr()) return;  /* can't load the Xrandr library */
258
259     /* see if Xrandr is available */
260     wine_tsx11_lock();
261     ok = pXRRQueryExtension(gdi_display, &xrandr_event, &xrandr_error);
262     if (ok)
263     {
264         X11DRV_expect_error(gdi_display, XRandRErrorHandler, NULL);
265         ok = pXRRQueryVersion(gdi_display, &xrandr_major, &xrandr_minor);
266         if (X11DRV_check_error()) ok = FALSE;
267     }
268     if (ok)
269     {
270         TRACE("Found XRandR - major: %d, minor: %d\n", xrandr_major, xrandr_minor);
271         /* retrieve modes */
272         real_xrandr_sizes = pXRRSizes(gdi_display, DefaultScreen(gdi_display), &real_xrandr_sizes_count);
273         ok = (real_xrandr_sizes_count>0);
274     }
275     if (ok)
276     {
277         TRACE("XRandR: found %u resolutions sizes\n", real_xrandr_sizes_count);
278         real_xrandr_rates = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(short *) * real_xrandr_sizes_count);
279         real_xrandr_rates_count = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(int) * real_xrandr_sizes_count);
280         for (i=0; i < real_xrandr_sizes_count; i++)
281         {
282             real_xrandr_rates[i] = pXRRRates (gdi_display, DefaultScreen(gdi_display), i, &(real_xrandr_rates_count[i]));
283             TRACE("- at %u: %dx%d (%d rates):", i, real_xrandr_sizes[i].width, real_xrandr_sizes[i].height, real_xrandr_rates_count[i]);
284             if (real_xrandr_rates_count[i])
285             {
286                 int j;
287                 nmodes += real_xrandr_rates_count[i];
288                 for (j = 0; j < real_xrandr_rates_count[i]; ++j) {
289                   if (j > 0) TRACE(",");
290                   TRACE("  %d", real_xrandr_rates[i][j]);
291                 }
292             }
293             else
294             {
295                 nmodes++;
296                 TRACE(" <default>");
297             }
298             TRACE(" Hz\n");
299         }
300     }
301     wine_tsx11_unlock();
302     if (!ok) return;
303
304     real_xrandr_modes_count = nmodes;
305     TRACE("XRandR modes: count=%d\n", nmodes);
306
307     dd_modes = X11DRV_Settings_SetHandlers("XRandR", 
308                                            X11DRV_XRandR_GetCurrentMode, 
309                                            X11DRV_XRandR_SetCurrentMode, 
310                                            nmodes, 1);
311     make_modes();
312     X11DRV_Settings_AddDepthModes();
313     dd_mode_count = X11DRV_Settings_GetModeCount();
314     X11DRV_Settings_SetDefaultMode(0);
315
316     TRACE("Available DD modes: count=%d\n", dd_mode_count);
317     TRACE("Enabling XRandR\n");
318 }
319
320 void X11DRV_XRandR_Cleanup(void)
321 {
322     HeapFree(GetProcessHeap(), 0, real_xrandr_rates);
323     real_xrandr_rates = NULL;
324     HeapFree(GetProcessHeap(), 0, real_xrandr_rates_count);
325     real_xrandr_rates_count = NULL;
326 }
327
328 #endif /* HAVE_LIBXRANDR */