Fixed header dependencies to be fully compatible with the Windows
[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 "x11drv.h"
29 #include "wine/debug.h"
30
31 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
32
33
34 /* desktop window procedure */
35 static LRESULT WINAPI desktop_winproc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
36 {
37     switch(message)
38     {
39     case WM_NCCREATE:
40         SystemParametersInfoA( SPI_SETDESKPATTERN, -1, NULL, FALSE );
41         SetDeskWallPaper( (LPSTR)-1 );
42         return TRUE;
43
44     case WM_ERASEBKGND:
45         PaintDesktop( (HDC)wParam );
46         ValidateRect( hwnd, NULL );
47         break;
48
49     case WM_SYSCOMMAND:
50         if ((wParam & 0xfff0) == SC_CLOSE) ExitWindows( 0, 0 );
51         break;
52
53     case WM_SETCURSOR:
54         return (LRESULT)SetCursor( LoadCursorA( 0, IDC_ARROWA ) );
55
56     case WM_NCHITTEST:
57         return HTCLIENT;
58     }
59     return 0;
60 }
61
62
63 /* desktop window manager thread */
64 static DWORD CALLBACK desktop_thread( LPVOID driver_data )
65 {
66     Display *display;
67     MSG msg;
68     HWND hwnd;
69     WND *win;
70
71     NtCurrentTeb()->driver_data = driver_data;
72     display = thread_display();
73     hwnd = GetDesktopWindow();
74
75     /* patch the desktop window queue to point to our queue */
76     win = WIN_GetPtr( hwnd );
77     win->tid = GetCurrentThreadId();
78     X11DRV_register_window( display, hwnd, win->pDriverData );
79     WIN_ReleasePtr( win );
80
81     SetWindowLongW( hwnd, GWL_WNDPROC, (LONG)desktop_winproc );
82     wine_tsx11_lock();
83     XSetWMProtocols( display, root_window, &wmDeleteWindow, 1 );
84     XMapWindow( display, root_window );
85     wine_tsx11_unlock();
86
87     SendMessageW( hwnd, WM_NCCREATE, 0, 0 /* should be CREATESTRUCT */ );
88
89     while (GetMessageW( &msg, hwnd, 0, 0 )) DispatchMessageW( &msg );
90     return 0;
91 }
92
93
94 /***********************************************************************
95  *              X11DRV_create_desktop_thread
96  *
97  * Create the thread that manages the desktop window
98  */
99 void X11DRV_create_desktop_thread(void)
100 {
101     HANDLE handle = CreateThread( NULL, 0, desktop_thread, NtCurrentTeb()->driver_data,
102                                   0, &desktop_tid );
103     if (!handle)
104     {
105         MESSAGE( "Could not create desktop thread\n" );
106         ExitProcess(1);
107     }
108     /* we transferred our driver data to the new thread */
109     NtCurrentTeb()->driver_data = NULL;
110     CloseHandle( handle );
111 }
112
113
114 /***********************************************************************
115  *              X11DRV_create_desktop
116  *
117  * Create the X11 desktop window for the desktop mode.
118  */
119 Window X11DRV_create_desktop( XVisualInfo *desktop_vi, const char *geometry )
120 {
121     int x = 0, y = 0, flags;
122     unsigned int width = 640, height = 480;  /* Default size = 640x480 */
123     char *name = GetCommandLineA();
124     XSizeHints *size_hints;
125     XWMHints   *wm_hints;
126     XClassHint *class_hints;
127     XSetWindowAttributes win_attr;
128     XTextProperty window_name;
129     Window win;
130     Display *display = thread_display();
131
132     wine_tsx11_lock();
133     flags = XParseGeometry( geometry, &x, &y, &width, &height );
134     screen_width  = width;
135     screen_height = height;
136
137     /* Create window */
138     win_attr.background_pixel = BlackPixel(display, 0);
139     win_attr.event_mask = ExposureMask | KeyPressMask | KeyReleaseMask |
140                           PointerMotionMask | ButtonPressMask | ButtonReleaseMask;
141     win_attr.cursor = XCreateFontCursor( display, XC_top_left_arrow );
142
143     if (desktop_vi)
144         win_attr.colormap = XCreateColormap( display, DefaultRootWindow(display),
145                                              visual, AllocNone );
146     else
147         win_attr.colormap = None;
148
149     win = XCreateWindow( display, DefaultRootWindow(display),
150                          x, y, width, height, 0, screen_depth, InputOutput, visual,
151                          CWBackPixel | CWEventMask | CWCursor | CWColormap, &win_attr );
152
153     /* Set window manager properties */
154     size_hints  = XAllocSizeHints();
155     wm_hints    = XAllocWMHints();
156     class_hints = XAllocClassHint();
157     if (!size_hints || !wm_hints || !class_hints)
158     {
159         MESSAGE("Not enough memory for window manager hints.\n" );
160         ExitProcess(1);
161     }
162     size_hints->min_width = size_hints->max_width = width;
163     size_hints->min_height = size_hints->max_height = height;
164     size_hints->flags = PMinSize | PMaxSize;
165     if (flags & (XValue | YValue)) size_hints->flags |= USPosition;
166     if (flags & (WidthValue | HeightValue)) size_hints->flags |= USSize;
167     else size_hints->flags |= PSize;
168
169     wm_hints->flags = InputHint | StateHint;
170     wm_hints->input = True;
171     wm_hints->initial_state = NormalState;
172     class_hints->res_name  = "wine";
173     class_hints->res_class = "Wine";
174
175     XStringListToTextProperty( &name, 1, &window_name );
176     XSetWMProperties( display, win, &window_name, &window_name,
177                       NULL, 0, size_hints, wm_hints, class_hints );
178     XFree( size_hints );
179     XFree( wm_hints );
180     XFree( class_hints );
181     XFlush( display );
182     wine_tsx11_unlock();
183     return win;
184 }