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