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