If using the default values, also set dwType to REG_SZ as our default
[wine] / dlls / x11drv / 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22 #include <string.h>
23 #include <stdio.h>
24
25 #ifdef HAVE_LIBXRANDR
26
27 #include "ts_xlib.h"
28
29 #include <X11/extensions/Xrandr.h>
30 #include "x11drv.h"
31
32 #include "x11ddraw.h"
33 #include "xrandr.h"
34
35 #include "windef.h"
36 #include "winbase.h"
37 #include "wingdi.h"
38 #include "ddrawi.h"
39 #include "wine/debug.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(xrandr);
42
43 extern int usexrandr;
44
45 static int xrandr_event, xrandr_error, xrandr_major, xrandr_minor;
46
47 static LPDDHALMODEINFO dd_modes;
48 static unsigned int dd_mode_count;
49 static XRRScreenSize *real_xrandr_sizes;
50 static short **real_xrandr_rates;
51 static unsigned int real_xrandr_sizes_count;
52 static int *real_xrandr_rates_count;
53 static unsigned int real_xrandr_modes_count;
54
55 static int XRandRErrorHandler(Display *dpy, XErrorEvent *event, void *arg)
56 {
57     return 1;
58 }
59
60 static Bool in_desktop_mode;
61
62 /* create the mode structures */
63 static void make_modes(void)
64 {
65     int i, j;
66     for (i=0; i<real_xrandr_sizes_count; i++)
67     {
68         if (real_xrandr_rates_count[i])
69         {
70             for (j=0; j < real_xrandr_rates_count[i]; j++)
71             {
72                 X11DRV_Settings_AddOneMode(real_xrandr_sizes[i].width, 
73                                            real_xrandr_sizes[i].height, 
74                                            0, real_xrandr_rates[i][j]);
75             }
76         }
77         else
78         {
79             X11DRV_Settings_AddOneMode(real_xrandr_sizes[i].width, 
80                                        real_xrandr_sizes[i].height, 
81                                        0, 0);
82         }
83     }
84 }
85
86 static int X11DRV_XRandR_GetCurrentMode(void)
87 {
88     SizeID size;
89     Rotation rot;
90     Window root;
91     XRRScreenConfiguration *sc;
92     short rate;
93     int i;
94     int res = -1;
95     
96     wine_tsx11_lock();
97     root = RootWindow (gdi_display, DefaultScreen(gdi_display));
98     sc = XRRGetScreenInfo (gdi_display, root);
99     size = XRRConfigCurrentConfiguration (sc, &rot);
100     rate = XRRConfigCurrentRate (sc);
101     for (i = 0; i < real_xrandr_modes_count; i++)
102     {
103         if ( (dd_modes[i].dwWidth      == real_xrandr_sizes[size].width ) &&
104              (dd_modes[i].dwHeight     == real_xrandr_sizes[size].height) &&
105              (dd_modes[i].wRefreshRate == rate                          ) )
106           {
107               res = i;
108           }
109     }
110     XRRFreeScreenConfigInfo(sc);
111     wine_tsx11_unlock();
112     if (res == -1)
113     {
114         ERR("In unknown mode, returning default\n");
115         res = 0;
116     }
117     return res;
118 }
119
120 static void X11DRV_XRandR_SetCurrentMode(int mode)
121 {
122     SizeID size;
123     Rotation rot;
124     Window root;
125     XRRScreenConfiguration *sc;
126     Status stat = RRSetConfigSuccess;
127     short rate;
128     int i, j;
129     DWORD dwBpp = screen_depth;
130     if (dwBpp == 24) dwBpp = 32;
131     
132     wine_tsx11_lock();
133     root = RootWindow (gdi_display, DefaultScreen(gdi_display));
134     sc = XRRGetScreenInfo (gdi_display, root);
135     size = XRRConfigCurrentConfiguration (sc, &rot);
136     if (dwBpp != dd_modes[mode].dwBPP)
137     {
138         FIXME("Cannot change screen BPP from %ld to %ld\n", dwBpp, dd_modes[mode].dwBPP);
139     }
140     mode = mode%real_xrandr_modes_count;
141     for (i = 0; i < real_xrandr_sizes_count; i++)
142     {
143         if ( (dd_modes[mode].dwWidth  == real_xrandr_sizes[i].width ) && 
144              (dd_modes[mode].dwHeight == real_xrandr_sizes[i].height) )
145         {
146             size = i;
147             if (real_xrandr_rates_count[i])
148             {
149                 for (j=0; j < real_xrandr_rates_count[i]; j++)
150                 {
151                     if (dd_modes[mode].wRefreshRate == real_xrandr_rates[i][j])
152                     {
153                         rate = real_xrandr_rates[i][j];
154                         TRACE("Resizing X display to %ldx%ld @%d Hz\n", 
155                               dd_modes[mode].dwWidth, dd_modes[mode].dwHeight, rate);
156                         stat = XRRSetScreenConfigAndRate (gdi_display, sc, root, 
157                                                           size, rot, rate, CurrentTime);
158                         FIXME("Need to update SYSMETRICS after resizing display (now %ldx%ld)\n",
159                               dd_modes[mode].dwWidth, dd_modes[mode].dwHeight);
160                     }
161                 }
162             }
163             else
164             {
165                 TRACE("Resizing X display to %ldx%ld\n", 
166                       dd_modes[mode].dwWidth, dd_modes[mode].dwHeight);
167                 stat = XRRSetScreenConfig (gdi_display, sc, root, 
168                                            size, rot, CurrentTime);
169                 FIXME("Need to update SYSMETRICS after resizing display (now %ldx%ld)\n",
170                       dd_modes[mode].dwWidth, dd_modes[mode].dwHeight);
171             }
172         }
173     }
174     if (stat != RRSetConfigSuccess)
175     {
176         ERR("Resolution change not successful -- perhaps display has chaned?");
177     }
178     XRRFreeScreenConfigInfo(sc);
179     wine_tsx11_unlock();
180 }
181
182 void X11DRV_XRandR_Init(void)
183 {
184     Bool ok;
185     int nmodes = 0;
186     int i;
187     in_desktop_mode = (root_window != DefaultRootWindow(gdi_display));
188
189     if (xrandr_major) return; /* already initialized? */
190     if (!usexrandr) return; /* disabled in config */
191     if (in_desktop_mode) return; /* not compatible with desktop mode */
192
193     /* see if Xrandr is available */
194     wine_tsx11_lock();
195     ok = XRRQueryExtension(gdi_display, &xrandr_event, &xrandr_error);
196     if (ok)
197     {
198         X11DRV_expect_error(gdi_display, XRandRErrorHandler, NULL);
199         ok = XRRQueryVersion(gdi_display, &xrandr_major, &xrandr_minor);
200         if (X11DRV_check_error()) ok = FALSE;
201     }
202     if (ok)
203     {
204         TRACE("Found XRandR - major: %d, minor: %d\n", xrandr_major, xrandr_minor);
205         /* retrieve modes */
206         real_xrandr_sizes = XRRSizes(gdi_display, DefaultScreen(gdi_display), &real_xrandr_sizes_count);
207         ok = (real_xrandr_sizes_count>0);
208     }
209     if (ok)
210     {
211         real_xrandr_rates = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(short *) * real_xrandr_sizes_count);
212         real_xrandr_rates_count = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(int) * real_xrandr_sizes_count);
213         for (i=0; i < real_xrandr_sizes_count; i++)
214         {
215             real_xrandr_rates[i] = XRRRates (gdi_display, DefaultScreen(gdi_display), i, &(real_xrandr_rates_count[i]));
216             if (real_xrandr_rates_count[i])
217             {
218                 nmodes += real_xrandr_rates_count[i];
219             }
220             else
221             {
222                 nmodes++;
223             }
224         }
225     }
226     wine_tsx11_unlock();
227     if (!ok) return;
228
229     real_xrandr_modes_count = nmodes;
230     TRACE("XRandR modes: count=%d\n", nmodes);
231
232     dd_modes = X11DRV_Settings_SetHandlers("XRandR", 
233                                            X11DRV_XRandR_GetCurrentMode, 
234                                            X11DRV_XRandR_SetCurrentMode, 
235                                            nmodes, 1);
236     make_modes();
237     X11DRV_Settings_AddDepthModes();
238     dd_mode_count = X11DRV_Settings_GetModeCount();
239     X11DRV_Settings_SetDefaultMode(0);
240
241     TRACE("Available DD modes: count=%d\n", dd_mode_count);
242     TRACE("Enabling XRandR\n");
243 }
244
245 void X11DRV_XRandR_Cleanup(void)
246 {
247     if (real_xrandr_rates)
248     {
249         HeapFree(GetProcessHeap(), 0, real_xrandr_rates);
250         real_xrandr_rates = NULL;
251     }
252     if (real_xrandr_rates_count)
253     {
254         HeapFree(GetProcessHeap(), 0, real_xrandr_rates_count);
255         real_xrandr_rates_count = NULL;
256     }
257 }
258
259 #endif /* HAVE_LIBXRANDR */