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