Don't define BEGIN_INTERFACE in unknwn.h.
[wine] / dlls / x11drv / 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 "x11ddraw.h"
30 #include "wine/debug.h"
31
32 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
33
34
35 /* desktop window procedure */
36 static LRESULT WINAPI desktop_winproc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
37 {
38     switch(message)
39     {
40     case WM_NCCREATE:
41         SystemParametersInfoA( SPI_SETDESKPATTERN, -1, NULL, FALSE );
42         SetDeskWallPaper( (LPSTR)-1 );
43         return TRUE;
44
45     case WM_ERASEBKGND:
46         PaintDesktop( (HDC)wParam );
47         ValidateRect( hwnd, NULL );
48         break;
49
50     case WM_SYSCOMMAND:
51         if ((wParam & 0xfff0) == SC_CLOSE) ExitWindows( 0, 0 );
52         break;
53
54     case WM_SETCURSOR:
55         return (LRESULT)SetCursor( LoadCursorA( 0, (LPSTR)IDC_ARROW ) );
56
57     case WM_NCHITTEST:
58         return HTCLIENT;
59     }
60     return 0;
61 }
62
63
64 /* desktop window manager thread */
65 static DWORD CALLBACK desktop_thread( LPVOID driver_data )
66 {
67     Display *display;
68     MSG msg;
69     HWND hwnd;
70     WND *win;
71     Atom atom = x11drv_atom(WM_DELETE_WINDOW);
72
73     NtCurrentTeb()->driver_data = driver_data;
74     display = thread_display();
75     hwnd = GetDesktopWindow();
76
77     /* patch the desktop window queue to point to our queue */
78     win = WIN_GetPtr( hwnd );
79     win->tid = GetCurrentThreadId();
80     X11DRV_register_window( display, hwnd, win->pDriverData );
81     WIN_ReleasePtr( win );
82
83     SetWindowLongW( hwnd, GWL_WNDPROC, (LONG)desktop_winproc );
84     wine_tsx11_lock();
85     XChangeProperty ( display, root_window, x11drv_atom(WM_PROTOCOLS),
86                       XA_ATOM, 32, PropModeReplace, (char *)&atom, 1 );
87     XMapWindow( display, root_window );
88     wine_tsx11_unlock();
89
90     SendMessageW( hwnd, WM_NCCREATE, 0, 0 /* should be CREATESTRUCT */ );
91
92     while (GetMessageW( &msg, hwnd, 0, 0 )) DispatchMessageW( &msg );
93     return 0;
94 }
95
96
97 /***********************************************************************
98  *              X11DRV_create_desktop_thread
99  *
100  * Create the thread that manages the desktop window
101  */
102 void X11DRV_create_desktop_thread(void)
103 {
104     HANDLE handle = CreateThread( NULL, 0, desktop_thread, NtCurrentTeb()->driver_data,
105                                   0, &desktop_tid );
106     if (!handle)
107     {
108         MESSAGE( "Could not create desktop thread\n" );
109         ExitProcess(1);
110     }
111     /* we transferred our driver data to the new thread */
112     NtCurrentTeb()->driver_data = NULL;
113     CloseHandle( handle );
114 }
115
116
117 /* data for resolution changing */
118 static LPDDHALMODEINFO dd_modes;
119 static unsigned int dd_mode_count;
120
121 static unsigned int max_width;
122 static unsigned int max_height;
123
124 static const unsigned int widths[]  = {320, 512, 640, 800, 1024, 1152, 1280, 1600};
125 static const unsigned int heights[] = {200, 384, 480, 600,  768,  864, 1024, 1200};
126 #define NUM_DESKTOP_MODES (8)
127
128 /* create the mode structures */
129 static void make_modes(void)
130 {
131     int i;
132     /* original specified desktop size */
133     X11DRV_Settings_AddOneMode(screen_width, screen_height, 0, 0);
134     for (i=0; i<NUM_DESKTOP_MODES; i++)
135     {
136         if ( (widths[i] <= max_width) && (heights[i] <= max_height) )
137         {
138             if ( ( (widths[i] != max_width) || (heights[i] != max_height) ) &&
139                  ( (widths[i] != screen_width) || (heights[i] != screen_height) ) )
140             {
141                 /* only add them if they are smaller than the root window and unique */
142                 X11DRV_Settings_AddOneMode(widths[i], heights[i], 0, 0);
143             }
144         }
145     }
146     if ((max_width != screen_width) && (max_height != screen_height))
147     {
148         /* root window size (if different from desktop window) */
149         X11DRV_Settings_AddOneMode(max_width, max_height, 0, 0);
150     }
151 }
152
153 /***********************************************************************
154  *              X11DRV_resize_desktop
155  *
156  * Reset the desktop window size and WM hints
157  */
158 int X11DRV_resize_desktop( unsigned int width, unsigned int height )
159 {
160     XSizeHints *size_hints;
161     Display *display = thread_display();
162     Window w = root_window;
163     /* set up */
164     wine_tsx11_lock();
165     size_hints  = XAllocSizeHints();
166     if (!size_hints)
167     {
168         ERR("Not enough memory for window manager hints.\n" );
169         wine_tsx11_unlock();
170         return 0;
171     }
172     size_hints->min_width = size_hints->max_width = width;
173     size_hints->min_height = size_hints->max_height = height;
174     size_hints->flags = PMinSize | PMaxSize | PSize;
175
176     /* do the work */
177     XSetWMNormalHints( display, w, size_hints );
178     XResizeWindow( display, w, width, height );
179
180     /* clean up */
181     XFree( size_hints );
182     XFlush( display );
183     wine_tsx11_unlock();
184     X11DRV_handle_desktop_resize( width, height );
185     return 1;
186 }
187
188 int X11DRV_desktop_GetCurrentMode(void)
189 {
190     unsigned int i;
191     DWORD dwBpp = screen_depth;
192     if (dwBpp == 24) dwBpp = 32;
193     for (i=0; i<dd_mode_count; i++)
194     {
195         if ( (screen_width == dd_modes[i].dwWidth) &&
196              (screen_height == dd_modes[i].dwHeight) && 
197              (dwBpp == dd_modes[i].dwBPP))
198             return i;
199     }
200     ERR("In unknown mode, returning default\n");
201     return 0;
202 }
203
204 void X11DRV_desktop_SetCurrentMode(int mode)
205 {
206     DWORD dwBpp = screen_depth;
207     if (dwBpp == 24) dwBpp = 32;
208     TRACE("Resizing Wine desktop window to %ldx%ld\n", dd_modes[mode].dwWidth, dd_modes[mode].dwHeight);
209     X11DRV_resize_desktop(dd_modes[mode].dwWidth, dd_modes[mode].dwHeight);
210     if (dwBpp != dd_modes[mode].dwBPP)
211     {
212         FIXME("Cannot change screen BPP from %ld to %ld\n", dwBpp, dd_modes[mode].dwBPP);
213     }
214 }
215
216 /***********************************************************************
217  *              X11DRV_create_desktop
218  *
219  * Create the X11 desktop window for the desktop mode.
220  */
221 Window X11DRV_create_desktop( XVisualInfo *desktop_vi, const char *geometry )
222 {
223     int x = 0, y = 0, flags;
224     unsigned int width = 640, height = 480;  /* Default size = 640x480 */
225     char *name = GetCommandLineA();
226     XSizeHints *size_hints;
227     XWMHints   *wm_hints;
228     XClassHint *class_hints;
229     XSetWindowAttributes win_attr;
230     XTextProperty window_name;
231     Window win;
232     Display *display = thread_display();
233
234     wine_tsx11_lock();
235     flags = XParseGeometry( geometry, &x, &y, &width, &height );
236     max_width = screen_width;
237     max_height = screen_height;
238     screen_width  = width;
239     screen_height = height;
240
241     /* Create window */
242     win_attr.background_pixel = BlackPixel(display, 0);
243     win_attr.event_mask = ExposureMask | KeyPressMask | KeyReleaseMask |
244                           PointerMotionMask | ButtonPressMask | ButtonReleaseMask;
245     win_attr.cursor = XCreateFontCursor( display, XC_top_left_arrow );
246
247     if (desktop_vi)
248         win_attr.colormap = XCreateColormap( display, DefaultRootWindow(display),
249                                              visual, AllocNone );
250     else
251         win_attr.colormap = None;
252
253     win = XCreateWindow( display, DefaultRootWindow(display),
254                          x, y, width, height, 0, screen_depth, InputOutput, visual,
255                          CWBackPixel | CWEventMask | CWCursor | CWColormap, &win_attr );
256
257     /* Set window manager properties */
258     size_hints  = XAllocSizeHints();
259     wm_hints    = XAllocWMHints();
260     class_hints = XAllocClassHint();
261     if (!size_hints || !wm_hints || !class_hints)
262     {
263         MESSAGE("Not enough memory for window manager hints.\n" );
264         ExitProcess(1);
265     }
266     size_hints->min_width = size_hints->max_width = width;
267     size_hints->min_height = size_hints->max_height = height;
268     size_hints->flags = PMinSize | PMaxSize;
269     if (flags & (XValue | YValue)) size_hints->flags |= USPosition;
270     if (flags & (WidthValue | HeightValue)) size_hints->flags |= USSize;
271     else size_hints->flags |= PSize;
272
273     wm_hints->flags = InputHint | StateHint;
274     wm_hints->input = True;
275     wm_hints->initial_state = NormalState;
276     class_hints->res_name  = "wine";
277     class_hints->res_class = "Wine";
278
279     XStringListToTextProperty( &name, 1, &window_name );
280     XSetWMProperties( display, win, &window_name, &window_name,
281                       NULL, 0, size_hints, wm_hints, class_hints );
282     XFree( size_hints );
283     XFree( wm_hints );
284     XFree( class_hints );
285     XFlush( display );
286     wine_tsx11_unlock();
287     /* initialize the available resolutions */
288     dd_modes = X11DRV_Settings_SetHandlers("desktop", 
289                                            X11DRV_desktop_GetCurrentMode, 
290                                            X11DRV_desktop_SetCurrentMode, 
291                                            NUM_DESKTOP_MODES+2, 1);
292     make_modes();
293     X11DRV_Settings_AddDepthModes();
294     dd_mode_count = X11DRV_Settings_GetModeCount();
295     X11DRV_Settings_SetDefaultMode(0);
296     return win;
297 }