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