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