winex11: Activate the depth-bpp difference.
[wine] / dlls / winex11.drv / desktop.c
1 /*
2  * X11DRV desktop window handling
3  *
4  * Copyright 2001 Alexandre Julliard
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 <X11/cursorfont.h>
23 #include <X11/Xlib.h>
24
25 #include "win.h"
26 #include "ddrawi.h"
27 #include "x11drv.h"
28 #include "wine/debug.h"
29
30 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
31
32 /* data for resolution changing */
33 static LPDDHALMODEINFO dd_modes;
34 static unsigned int dd_mode_count;
35
36 static unsigned int max_width;
37 static unsigned int max_height;
38
39 static const unsigned int widths[]  = {320, 400, 512, 640, 800, 1024, 1152, 1280, 1400, 1600};
40 static const unsigned int heights[] = {200, 300, 384, 480, 600,  768,  864, 1024, 1050, 1200};
41 #define NUM_DESKTOP_MODES (sizeof(widths) / sizeof(widths[0]))
42
43 /* create the mode structures */
44 static void make_modes(void)
45 {
46     int i;
47     /* original specified desktop size */
48     X11DRV_Settings_AddOneMode(screen_width, screen_height, 0, 60);
49     for (i=0; i<NUM_DESKTOP_MODES; i++)
50     {
51         if ( (widths[i] <= max_width) && (heights[i] <= max_height) )
52         {
53             if ( ( (widths[i] != max_width) || (heights[i] != max_height) ) &&
54                  ( (widths[i] != screen_width) || (heights[i] != screen_height) ) )
55             {
56                 /* only add them if they are smaller than the root window and unique */
57                 X11DRV_Settings_AddOneMode(widths[i], heights[i], 0, 60);
58             }
59         }
60     }
61     if ((max_width != screen_width) && (max_height != screen_height))
62     {
63         /* root window size (if different from desktop window) */
64         X11DRV_Settings_AddOneMode(max_width, max_height, 0, 60);
65     }
66 }
67
68 /***********************************************************************
69  *              X11DRV_resize_desktop
70  *
71  * Reset the desktop window size and WM hints
72  */
73 static int X11DRV_resize_desktop( unsigned int width, unsigned int height )
74 {
75     XSizeHints *size_hints;
76     Display *display = thread_display();
77     Window w = root_window;
78     /* set up */
79     wine_tsx11_lock();
80     size_hints  = XAllocSizeHints();
81     if (!size_hints)
82     {
83         ERR("Not enough memory for window manager hints.\n" );
84         wine_tsx11_unlock();
85         return 0;
86     }
87     size_hints->min_width = size_hints->max_width = width;
88     size_hints->min_height = size_hints->max_height = height;
89     size_hints->flags = PMinSize | PMaxSize | PSize;
90
91     /* do the work */
92     XSetWMNormalHints( display, w, size_hints );
93     XResizeWindow( display, w, width, height );
94
95     /* clean up */
96     XFree( size_hints );
97     XFlush( display );
98     wine_tsx11_unlock();
99     X11DRV_handle_desktop_resize( width, height );
100     return 1;
101 }
102
103 static int X11DRV_desktop_GetCurrentMode(void)
104 {
105     unsigned int i;
106     DWORD dwBpp = screen_bpp;
107     for (i=0; i<dd_mode_count; i++)
108     {
109         if ( (screen_width == dd_modes[i].dwWidth) &&
110              (screen_height == dd_modes[i].dwHeight) && 
111              (dwBpp == dd_modes[i].dwBPP))
112             return i;
113     }
114     ERR("In unknown mode, returning default\n");
115     return 0;
116 }
117
118 static LONG X11DRV_desktop_SetCurrentMode(int mode)
119 {
120     DWORD dwBpp = screen_bpp;
121     if (dwBpp != dd_modes[mode].dwBPP)
122     {
123         FIXME("Cannot change screen BPP from %d to %d\n", dwBpp, dd_modes[mode].dwBPP);
124         /* Ignore the depth missmatch
125          *
126          * Some (older) applications require a specific bit depth, this will allow them
127          * to run. X11drv performs a color depth conversion if needed.
128          */
129     }
130     TRACE("Resizing Wine desktop window to %dx%d\n", dd_modes[mode].dwWidth, dd_modes[mode].dwHeight);
131     X11DRV_resize_desktop(dd_modes[mode].dwWidth, dd_modes[mode].dwHeight);
132     return DISP_CHANGE_SUCCESSFUL;
133 }
134
135 /***********************************************************************
136  *              X11DRV_init_desktop
137  *
138  * Setup the desktop when not using the root window.
139  */
140 void X11DRV_init_desktop( Window win, unsigned int width, unsigned int height )
141 {
142     root_window = win;
143     max_width = screen_width;
144     max_height = screen_height;
145     screen_width  = width;
146     screen_height = height;
147     xinerama_init();
148
149     /* initialize the available resolutions */
150     dd_modes = X11DRV_Settings_SetHandlers("desktop", 
151                                            X11DRV_desktop_GetCurrentMode, 
152                                            X11DRV_desktop_SetCurrentMode, 
153                                            NUM_DESKTOP_MODES+2, 1);
154     make_modes();
155     X11DRV_Settings_AddDepthModes();
156     dd_mode_count = X11DRV_Settings_GetModeCount();
157 }
158
159
160 /***********************************************************************
161  *              X11DRV_create_desktop
162  *
163  * Create the X11 desktop window for the desktop mode.
164  */
165 Window X11DRV_create_desktop( UINT width, UINT height )
166 {
167     XSetWindowAttributes win_attr;
168     Window win;
169     Display *display = thread_display();
170
171     wine_tsx11_lock();
172
173     /* Create window */
174     win_attr.event_mask = ExposureMask | KeyPressMask | KeyReleaseMask |
175                           PointerMotionMask | ButtonPressMask | ButtonReleaseMask;
176     win_attr.cursor = XCreateFontCursor( display, XC_top_left_arrow );
177
178     if (visual != DefaultVisual( display, DefaultScreen(display) ))
179         win_attr.colormap = XCreateColormap( display, DefaultRootWindow(display),
180                                              visual, AllocNone );
181     else
182         win_attr.colormap = None;
183
184     win = XCreateWindow( display, DefaultRootWindow(display),
185                          0, 0, width, height, 0, screen_depth, InputOutput, visual,
186                          CWEventMask | CWCursor | CWColormap, &win_attr );
187     XFlush( display );
188     wine_tsx11_unlock();
189     if (win != None) X11DRV_init_desktop( win, width, height );
190     return win;
191 }