winex11: Don't try to create the owner window if it belongs to a different thread.
[wine] / dlls / winex11.drv / window.c
1 /*
2  * Window related functions
3  *
4  * Copyright 1993, 1994, 1995, 1996, 2001 Alexandre Julliard
5  * Copyright 1993 David Metcalfe
6  * Copyright 1995, 1996 Alex Korobka
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #include "config.h"
24
25 #include <stdarg.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #ifdef HAVE_UNISTD_H
29 # include <unistd.h>
30 #endif
31
32 #include <X11/Xlib.h>
33 #include <X11/Xresource.h>
34 #include <X11/Xutil.h>
35 #ifdef HAVE_LIBXSHAPE
36 #include <X11/extensions/shape.h>
37 #endif /* HAVE_LIBXSHAPE */
38
39 #include "windef.h"
40 #include "winbase.h"
41 #include "wingdi.h"
42 #include "winuser.h"
43 #include "wine/unicode.h"
44
45 #include "x11drv.h"
46 #include "xcomposite.h"
47 #include "wine/debug.h"
48 #include "wine/server.h"
49 #include "mwm.h"
50
51 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
52
53 #define _NET_WM_MOVERESIZE_SIZE_TOPLEFT      0
54 #define _NET_WM_MOVERESIZE_SIZE_TOP          1
55 #define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT     2
56 #define _NET_WM_MOVERESIZE_SIZE_RIGHT        3
57 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT  4
58 #define _NET_WM_MOVERESIZE_SIZE_BOTTOM       5
59 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT   6
60 #define _NET_WM_MOVERESIZE_SIZE_LEFT         7
61 #define _NET_WM_MOVERESIZE_MOVE              8
62 #define _NET_WM_MOVERESIZE_SIZE_KEYBOARD     9
63 #define _NET_WM_MOVERESIZE_MOVE_KEYBOARD    10
64
65 #define _NET_WM_STATE_REMOVE  0
66 #define _NET_WM_STATE_ADD     1
67 #define _NET_WM_STATE_TOGGLE  2
68
69 #define SWP_AGG_NOPOSCHANGE (SWP_NOSIZE | SWP_NOMOVE | SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE | SWP_NOZORDER)
70
71 /* X context to associate a hwnd to an X window */
72 XContext winContext = 0;
73
74 /* X context to associate a struct x11drv_win_data to an hwnd */
75 static XContext win_data_context;
76
77 static const char whole_window_prop[] = "__wine_x11_whole_window";
78 static const char client_window_prop[]= "__wine_x11_client_window";
79 static const char icon_window_prop[]  = "__wine_x11_icon_window";
80 static const char fbconfig_id_prop[]  = "__wine_x11_fbconfig_id";
81 static const char gl_drawable_prop[]  = "__wine_x11_gl_drawable";
82 static const char pixmap_prop[]       = "__wine_x11_pixmap";
83 static const char managed_prop[]      = "__wine_x11_managed";
84
85
86 /***********************************************************************
87  * http://standards.freedesktop.org/startup-notification-spec
88  */
89 static void remove_startup_notification(Display *display, Window window)
90 {
91     static LONG startup_notification_removed = 0;
92     char id[1024];
93     char message[1024];
94     int i;
95     int pos;
96     XEvent xevent;
97     const char *src;
98     int srclen;
99
100     if (InterlockedCompareExchange(&startup_notification_removed, 1, 0) != 0)
101         return;
102
103     if (GetEnvironmentVariableA("DESKTOP_STARTUP_ID", id, sizeof(id)) == 0)
104         return;
105     SetEnvironmentVariableA("DESKTOP_STARTUP_ID", NULL);
106
107     pos = snprintf(message, sizeof(message), "remove: ID=");
108     message[pos++] = '"';
109     for (i = 0; id[i] && pos < sizeof(message) - 2; i++)
110     {
111         if (id[i] == '"' || id[i] == '\\')
112             message[pos++] = '\\';
113         message[pos++] = id[i];
114     }
115     message[pos++] = '"';
116     message[pos++] = '\0';
117
118     xevent.xclient.type = ClientMessage;
119     xevent.xclient.message_type = x11drv_atom(_NET_STARTUP_INFO_BEGIN);
120     xevent.xclient.display = display;
121     xevent.xclient.window = window;
122     xevent.xclient.format = 8;
123
124     src = message;
125     srclen = strlen(src) + 1;
126
127     wine_tsx11_lock();
128     while (srclen > 0)
129     {
130         int msglen = srclen;
131         if (msglen > 20)
132             msglen = 20;
133         memset(&xevent.xclient.data.b[0], 0, 20);
134         memcpy(&xevent.xclient.data.b[0], src, msglen);
135         src += msglen;
136         srclen -= msglen;
137
138         XSendEvent( display, DefaultRootWindow( display ), False, PropertyChangeMask, &xevent );
139         xevent.xclient.message_type = x11drv_atom(_NET_STARTUP_INFO);
140     }
141     wine_tsx11_unlock();
142 }
143
144
145 struct has_popup_result
146 {
147     HWND hwnd;
148     BOOL found;
149 };
150
151 static BOOL CALLBACK has_popup( HWND hwnd, LPARAM lparam )
152 {
153     struct has_popup_result *result = (struct has_popup_result *)lparam;
154
155     if (hwnd == result->hwnd) return FALSE;  /* popups are always above owner */
156     result->found = (GetWindow( hwnd, GW_OWNER ) == result->hwnd);
157     return !result->found;
158 }
159
160 static BOOL has_owned_popups( HWND hwnd )
161 {
162     struct has_popup_result result;
163
164     result.hwnd = hwnd;
165     result.found = FALSE;
166     EnumWindows( has_popup, (LPARAM)&result );
167     return result.found;
168 }
169
170 /***********************************************************************
171  *              is_window_managed
172  *
173  * Check if a given window should be managed
174  */
175 static BOOL is_window_managed( HWND hwnd, UINT swp_flags, const RECT *window_rect )
176 {
177     DWORD style, ex_style;
178
179     if (!managed_mode) return FALSE;
180
181     /* child windows are not managed */
182     style = GetWindowLongW( hwnd, GWL_STYLE );
183     if ((style & (WS_CHILD|WS_POPUP)) == WS_CHILD) return FALSE;
184     /* activated windows are managed */
185     if (!(swp_flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW))) return TRUE;
186     if (hwnd == GetActiveWindow()) return TRUE;
187     /* windows with caption are managed */
188     if ((style & WS_CAPTION) == WS_CAPTION) return TRUE;
189     /* windows with thick frame are managed */
190     if (style & WS_THICKFRAME) return TRUE;
191     if (style & WS_POPUP)
192     {
193         HMONITOR hmon;
194         MONITORINFO mi;
195
196         /* popup with sysmenu == caption are managed */
197         if (style & WS_SYSMENU) return TRUE;
198         /* full-screen popup windows are managed */
199         hmon = MonitorFromWindow( hwnd, MONITOR_DEFAULTTOPRIMARY );
200         mi.cbSize = sizeof( mi );
201         GetMonitorInfoW( hmon, &mi );
202         if (window_rect->left <= mi.rcWork.left && window_rect->right >= mi.rcWork.right &&
203             window_rect->top <= mi.rcWork.top && window_rect->bottom >= mi.rcWork.bottom)
204             return TRUE;
205     }
206     /* application windows are managed */
207     ex_style = GetWindowLongW( hwnd, GWL_EXSTYLE );
208     if (ex_style & WS_EX_APPWINDOW) return TRUE;
209     /* windows that own popups are managed */
210     if (has_owned_popups( hwnd )) return TRUE;
211     /* default: not managed */
212     return FALSE;
213 }
214
215
216 /***********************************************************************
217  *              X11DRV_is_window_rect_mapped
218  *
219  * Check if the X whole window should be mapped based on its rectangle
220  */
221 static BOOL is_window_rect_mapped( const RECT *rect )
222 {
223     /* don't map if rect is empty */
224     if (IsRectEmpty( rect )) return FALSE;
225
226     /* don't map if rect is off-screen */
227     if (rect->left >= virtual_screen_rect.right ||
228         rect->top >= virtual_screen_rect.bottom ||
229         rect->right <= virtual_screen_rect.left ||
230         rect->bottom <= virtual_screen_rect.top)
231         return FALSE;
232
233     return TRUE;
234 }
235
236
237 /***********************************************************************
238  *              is_window_resizable
239  *
240  * Check if window should be made resizable by the window manager
241  */
242 static inline BOOL is_window_resizable( struct x11drv_win_data *data, DWORD style )
243 {
244     if (style & WS_THICKFRAME) return TRUE;
245     /* Metacity needs the window to be resizable to make it fullscreen */
246     return (data->whole_rect.left <= 0 && data->whole_rect.right >= screen_width &&
247             data->whole_rect.top <= 0 && data->whole_rect.bottom >= screen_height);
248 }
249
250
251 /***********************************************************************
252  *              get_window_owner
253  */
254 static HWND get_window_owner( HWND hwnd )
255 {
256     RECT rect;
257     HWND owner = GetWindow( hwnd, GW_OWNER );
258     /* ignore the zero-size owners used by Delphi apps */
259     if (owner && GetWindowRect( owner, &rect ) && IsRectEmpty( &rect )) owner = 0;
260     return owner;
261 }
262
263
264 /***********************************************************************
265  *              get_mwm_decorations
266  */
267 static unsigned long get_mwm_decorations( struct x11drv_win_data *data,
268                                           DWORD style, DWORD ex_style )
269 {
270     unsigned long ret = 0;
271
272     if (!decorated_mode) return 0;
273
274     if (IsRectEmpty( &data->window_rect )) return 0;
275     if (data->shaped) return 0;
276
277     if (ex_style & WS_EX_TOOLWINDOW) return 0;
278
279     if ((style & WS_CAPTION) == WS_CAPTION)
280     {
281         ret |= MWM_DECOR_TITLE | MWM_DECOR_BORDER;
282         if (style & WS_SYSMENU) ret |= MWM_DECOR_MENU;
283         if (style & WS_MINIMIZEBOX) ret |= MWM_DECOR_MINIMIZE;
284         if (style & WS_MAXIMIZEBOX) ret |= MWM_DECOR_MAXIMIZE;
285     }
286     if (ex_style & WS_EX_DLGMODALFRAME) ret |= MWM_DECOR_BORDER;
287     else if (style & WS_THICKFRAME) ret |= MWM_DECOR_BORDER | MWM_DECOR_RESIZEH;
288     else if ((style & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME) ret |= MWM_DECOR_BORDER;
289     return ret;
290 }
291
292
293 /***********************************************************************
294  *              get_x11_rect_offset
295  *
296  * Helper for X11DRV_window_to_X_rect and X11DRV_X_to_window_rect.
297  */
298 static void get_x11_rect_offset( struct x11drv_win_data *data, RECT *rect )
299 {
300     DWORD style, ex_style, style_mask = 0, ex_style_mask = 0;
301     unsigned long decor;
302
303     rect->top = rect->bottom = rect->left = rect->right = 0;
304
305     style = GetWindowLongW( data->hwnd, GWL_STYLE );
306     ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
307     decor = get_mwm_decorations( data, style, ex_style );
308
309     if (decor & MWM_DECOR_TITLE) style_mask |= WS_CAPTION;
310     if (decor & MWM_DECOR_BORDER)
311     {
312         style_mask |= WS_DLGFRAME | WS_THICKFRAME;
313         ex_style_mask |= WS_EX_DLGMODALFRAME;
314     }
315
316     AdjustWindowRectEx( rect, style & style_mask, FALSE, ex_style & ex_style_mask );
317 }
318
319
320 /***********************************************************************
321  *              get_window_attributes
322  *
323  * Fill the window attributes structure for an X window.
324  */
325 static int get_window_attributes( Display *display, struct x11drv_win_data *data,
326                                   XSetWindowAttributes *attr )
327 {
328     attr->override_redirect = !data->managed;
329     attr->colormap          = X11DRV_PALETTE_PaletteXColormap;
330     attr->save_under        = ((GetClassLongW( data->hwnd, GCL_STYLE ) & CS_SAVEBITS) != 0);
331     attr->cursor            = x11drv_thread_data()->cursor;
332     attr->bit_gravity       = NorthWestGravity;
333     attr->win_gravity       = StaticGravity;
334     attr->backing_store     = NotUseful;
335     attr->event_mask        = (ExposureMask | PointerMotionMask |
336                                ButtonPressMask | ButtonReleaseMask | EnterWindowMask |
337                                KeyPressMask | KeyReleaseMask | FocusChangeMask |
338                                KeymapStateMask | StructureNotifyMask);
339     if (data->managed) attr->event_mask |= PropertyChangeMask;
340
341     return (CWOverrideRedirect | CWSaveUnder | CWColormap | CWCursor |
342             CWEventMask | CWBitGravity | CWBackingStore);
343 }
344
345
346 /***********************************************************************
347  *              create_client_window
348  */
349 static Window create_client_window( Display *display, struct x11drv_win_data *data, XVisualInfo *vis )
350 {
351     int cx, cy, mask;
352     XSetWindowAttributes attr;
353     Window client;
354     Visual *client_visual = vis ? vis->visual : visual;
355
356     attr.bit_gravity = NorthWestGravity;
357     attr.win_gravity = NorthWestGravity;
358     attr.backing_store = NotUseful;
359     attr.event_mask = (ExposureMask | PointerMotionMask |
360                        ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
361     mask = CWEventMask | CWBitGravity | CWWinGravity | CWBackingStore;
362
363     if ((cx = data->client_rect.right - data->client_rect.left) <= 0) cx = 1;
364     else if (cx > 65535) cx = 65535;
365     if ((cy = data->client_rect.bottom - data->client_rect.top) <= 0) cy = 1;
366     else if (cy > 65535) cy = 65535;
367
368     wine_tsx11_lock();
369
370     if (vis)
371     {
372         attr.colormap = XCreateColormap( display, root_window, vis->visual,
373                                          (vis->class == PseudoColor || vis->class == GrayScale ||
374                                           vis->class == DirectColor) ? AllocAll : AllocNone );
375         mask |= CWColormap;
376     }
377
378     client = XCreateWindow( display, data->whole_window,
379                             data->client_rect.left - data->whole_rect.left,
380                             data->client_rect.top - data->whole_rect.top,
381                             cx, cy, 0, screen_depth, InputOutput,
382                             client_visual, mask, &attr );
383     if (!client)
384     {
385         wine_tsx11_unlock();
386         return 0;
387     }
388
389     if (data->client_window)
390     {
391         struct x11drv_thread_data *thread_data = x11drv_thread_data();
392         if (thread_data->cursor_window == data->client_window) thread_data->cursor_window = None;
393         XDeleteContext( display, data->client_window, winContext );
394         XDestroyWindow( display, data->client_window );
395     }
396     data->client_window = client;
397     data->visualid = XVisualIDFromVisual( client_visual );
398
399     if (data->colormap) XFreeColormap( display, data->colormap );
400     data->colormap = vis ? attr.colormap : 0;
401
402     XMapWindow( display, data->client_window );
403     XSaveContext( display, data->client_window, winContext, (char *)data->hwnd );
404     wine_tsx11_unlock();
405
406     SetPropA( data->hwnd, client_window_prop, (HANDLE)data->client_window );
407     return data->client_window;
408 }
409
410
411 /***********************************************************************
412  *              sync_window_style
413  *
414  * Change the X window attributes when the window style has changed.
415  */
416 static void sync_window_style( Display *display, struct x11drv_win_data *data )
417 {
418     if (data->whole_window != root_window)
419     {
420         XSetWindowAttributes attr;
421         int mask = get_window_attributes( display, data, &attr );
422
423         wine_tsx11_lock();
424         XChangeWindowAttributes( display, data->whole_window, mask, &attr );
425         wine_tsx11_unlock();
426     }
427 }
428
429
430 /***********************************************************************
431  *              sync_window_region
432  *
433  * Update the X11 window region.
434  */
435 static void sync_window_region( Display *display, struct x11drv_win_data *data, HRGN win_region )
436 {
437 #ifdef HAVE_LIBXSHAPE
438     HRGN hrgn = win_region;
439
440     if (!data->whole_window) return;
441     data->shaped = FALSE;
442
443     if (hrgn == (HRGN)1)  /* hack: win_region == 1 means retrieve region from server */
444     {
445         if (!(hrgn = CreateRectRgn( 0, 0, 0, 0 ))) return;
446         if (GetWindowRgn( data->hwnd, hrgn ) == ERROR)
447         {
448             DeleteObject( hrgn );
449             hrgn = 0;
450         }
451     }
452
453     if (!hrgn)
454     {
455         wine_tsx11_lock();
456         XShapeCombineMask( display, data->whole_window, ShapeBounding, 0, 0, None, ShapeSet );
457         wine_tsx11_unlock();
458     }
459     else
460     {
461         RGNDATA *pRegionData = X11DRV_GetRegionData( hrgn, 0 );
462         if (pRegionData)
463         {
464             wine_tsx11_lock();
465             XShapeCombineRectangles( display, data->whole_window, ShapeBounding,
466                                      data->window_rect.left - data->whole_rect.left,
467                                      data->window_rect.top - data->whole_rect.top,
468                                      (XRectangle *)pRegionData->Buffer,
469                                      pRegionData->rdh.nCount, ShapeSet, YXBanded );
470             wine_tsx11_unlock();
471             HeapFree(GetProcessHeap(), 0, pRegionData);
472             data->shaped = TRUE;
473         }
474     }
475     if (hrgn && hrgn != win_region) DeleteObject( hrgn );
476 #endif  /* HAVE_LIBXSHAPE */
477 }
478
479
480 /***********************************************************************
481  *              sync_window_opacity
482  */
483 static void sync_window_opacity( Display *display, Window win,
484                                  COLORREF key, BYTE alpha, DWORD flags )
485 {
486     unsigned long opacity = 0xffffffff;
487
488     if (flags & LWA_ALPHA) opacity = (0xffffffff / 0xff) * alpha;
489
490     if (flags & LWA_COLORKEY) FIXME("LWA_COLORKEY not supported\n");
491
492     wine_tsx11_lock();
493     if (opacity == 0xffffffff)
494         XDeleteProperty( display, win, x11drv_atom(_NET_WM_WINDOW_OPACITY) );
495     else
496         XChangeProperty( display, win, x11drv_atom(_NET_WM_WINDOW_OPACITY),
497                          XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&opacity, 1 );
498     wine_tsx11_unlock();
499 }
500
501
502 /***********************************************************************
503  *              sync_window_text
504  */
505 static void sync_window_text( Display *display, Window win, const WCHAR *text )
506 {
507     UINT count;
508     char *buffer, *utf8_buffer;
509     XTextProperty prop;
510
511     /* allocate new buffer for window text */
512     count = WideCharToMultiByte(CP_UNIXCP, 0, text, -1, NULL, 0, NULL, NULL);
513     if (!(buffer = HeapAlloc( GetProcessHeap(), 0, count ))) return;
514     WideCharToMultiByte(CP_UNIXCP, 0, text, -1, buffer, count, NULL, NULL);
515
516     count = WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), NULL, 0, NULL, NULL);
517     if (!(utf8_buffer = HeapAlloc( GetProcessHeap(), 0, count )))
518     {
519         HeapFree( GetProcessHeap(), 0, buffer );
520         return;
521     }
522     WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), utf8_buffer, count, NULL, NULL);
523
524     wine_tsx11_lock();
525     if (XmbTextListToTextProperty( display, &buffer, 1, XStdICCTextStyle, &prop ) == Success)
526     {
527         XSetWMName( display, win, &prop );
528         XSetWMIconName( display, win, &prop );
529         XFree( prop.value );
530     }
531     /*
532       Implements a NET_WM UTF-8 title. It should be without a trailing \0,
533       according to the standard
534       ( http://www.pps.jussieu.fr/~jch/software/UTF8_STRING/UTF8_STRING.text ).
535     */
536     XChangeProperty( display, win, x11drv_atom(_NET_WM_NAME), x11drv_atom(UTF8_STRING),
537                      8, PropModeReplace, (unsigned char *) utf8_buffer, count);
538     wine_tsx11_unlock();
539
540     HeapFree( GetProcessHeap(), 0, utf8_buffer );
541     HeapFree( GetProcessHeap(), 0, buffer );
542 }
543
544
545 /***********************************************************************
546  *              set_win_format
547  */
548 static BOOL set_win_format( HWND hwnd, XID fbconfig_id )
549 {
550     struct x11drv_win_data *data;
551     XVisualInfo *vis;
552     int w, h;
553
554     if (!(data = X11DRV_get_win_data(hwnd)) &&
555         !(data = X11DRV_create_win_data(hwnd))) return FALSE;
556
557     if (!(vis = visual_from_fbconfig_id(fbconfig_id))) return FALSE;
558
559     if (data->whole_window)
560     {
561         Display *display = thread_display();
562         Window client = data->client_window;
563
564         if (vis->visualid != data->visualid)
565         {
566             client = create_client_window( display, data, vis );
567             TRACE( "re-created client window %lx for %p fbconfig %lx\n", client, data->hwnd, fbconfig_id );
568         }
569         wine_tsx11_lock();
570         XFree(vis);
571         XFlush( display );
572         wine_tsx11_unlock();
573         if (client) goto done;
574         return FALSE;
575     }
576
577     w = data->client_rect.right - data->client_rect.left;
578     h = data->client_rect.bottom - data->client_rect.top;
579
580     if(w <= 0) w = 1;
581     if(h <= 0) h = 1;
582
583 #ifdef SONAME_LIBXCOMPOSITE
584     if(usexcomposite)
585     {
586         XSetWindowAttributes attrib;
587         static Window dummy_parent;
588
589         wine_tsx11_lock();
590         attrib.override_redirect = True;
591         if (!dummy_parent)
592         {
593             dummy_parent = XCreateWindow( gdi_display, root_window, -1, -1, 1, 1, 0, screen_depth,
594                                          InputOutput, visual, CWOverrideRedirect, &attrib );
595             XMapWindow( gdi_display, dummy_parent );
596         }
597         data->colormap = XCreateColormap(gdi_display, dummy_parent, vis->visual,
598                                          (vis->class == PseudoColor ||
599                                           vis->class == GrayScale ||
600                                           vis->class == DirectColor) ?
601                                          AllocAll : AllocNone);
602         attrib.colormap = data->colormap;
603         XInstallColormap(gdi_display, attrib.colormap);
604
605         if(data->gl_drawable) XDestroyWindow(gdi_display, data->gl_drawable);
606         data->gl_drawable = XCreateWindow(gdi_display, dummy_parent, -w, 0, w, h, 0,
607                                           vis->depth, InputOutput, vis->visual,
608                                           CWColormap | CWOverrideRedirect,
609                                           &attrib);
610         if(data->gl_drawable)
611         {
612             pXCompositeRedirectWindow(gdi_display, data->gl_drawable,
613                                       CompositeRedirectManual);
614             XMapWindow(gdi_display, data->gl_drawable);
615         }
616         XFree(vis);
617         XFlush( gdi_display );
618         wine_tsx11_unlock();
619     }
620     else
621 #endif
622     {
623         WARN("XComposite is not available, using GLXPixmap hack\n");
624
625         wine_tsx11_lock();
626
627         if(data->pixmap) XFreePixmap(gdi_display, data->pixmap);
628         data->pixmap = XCreatePixmap(gdi_display, root_window, w, h, vis->depth);
629         if(!data->pixmap)
630         {
631             XFree(vis);
632             wine_tsx11_unlock();
633             return FALSE;
634         }
635
636         if(data->gl_drawable) destroy_glxpixmap(gdi_display, data->gl_drawable);
637         data->gl_drawable = create_glxpixmap(gdi_display, vis, data->pixmap);
638         if(!data->gl_drawable)
639         {
640             XFreePixmap(gdi_display, data->pixmap);
641             data->pixmap = 0;
642         }
643         XFree(vis);
644         XFlush( gdi_display );
645         wine_tsx11_unlock();
646         if (data->pixmap) SetPropA(hwnd, pixmap_prop, (HANDLE)data->pixmap);
647     }
648
649     if (!data->gl_drawable) return FALSE;
650
651     TRACE("Created GL drawable 0x%lx, using FBConfigID 0x%lx\n",
652           data->gl_drawable, fbconfig_id);
653     SetPropA(hwnd, gl_drawable_prop, (HANDLE)data->gl_drawable);
654
655 done:
656     data->fbconfig_id = fbconfig_id;
657     SetPropA(hwnd, fbconfig_id_prop, (HANDLE)data->fbconfig_id);
658     /* force DCE invalidation */
659     SetWindowPos( hwnd, 0, 0, 0, 0, 0,
660                   SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOSIZE | SWP_NOMOVE |
661                   SWP_NOREDRAW | SWP_DEFERERASE | SWP_NOSENDCHANGING | SWP_STATECHANGED);
662     return TRUE;
663 }
664
665 /***********************************************************************
666  *              sync_gl_drawable
667  */
668 static void sync_gl_drawable(struct x11drv_win_data *data)
669 {
670     int w = data->client_rect.right - data->client_rect.left;
671     int h = data->client_rect.bottom - data->client_rect.top;
672     XVisualInfo *vis;
673     Drawable glxp;
674     Pixmap pix;
675
676     if (w <= 0) w = 1;
677     if (h <= 0) h = 1;
678
679     TRACE("Resizing GL drawable 0x%lx to %dx%d\n", data->gl_drawable, w, h);
680 #ifdef SONAME_LIBXCOMPOSITE
681     if(usexcomposite)
682     {
683         wine_tsx11_lock();
684         XMoveResizeWindow(gdi_display, data->gl_drawable, -w, 0, w, h);
685         wine_tsx11_unlock();
686         return;
687     }
688 #endif
689
690     if (!(vis = visual_from_fbconfig_id(data->fbconfig_id))) return;
691
692     wine_tsx11_lock();
693     pix = XCreatePixmap(gdi_display, root_window, w, h, vis->depth);
694     if(!pix)
695     {
696         ERR("Failed to create pixmap for offscreen rendering\n");
697         XFree(vis);
698         wine_tsx11_unlock();
699         return;
700     }
701
702     glxp = create_glxpixmap(gdi_display, vis, pix);
703     if(!glxp)
704     {
705         ERR("Failed to create drawable for offscreen rendering\n");
706         XFreePixmap(gdi_display, pix);
707         XFree(vis);
708         wine_tsx11_unlock();
709         return;
710     }
711
712     XFree(vis);
713
714     mark_drawable_dirty(data->gl_drawable, glxp);
715
716     XFreePixmap(gdi_display, data->pixmap);
717     destroy_glxpixmap(gdi_display, data->gl_drawable);
718     TRACE( "Recreated GL drawable %lx to replace %lx\n", glxp, data->gl_drawable );
719
720     data->pixmap = pix;
721     data->gl_drawable = glxp;
722
723     XFlush( gdi_display );
724     wine_tsx11_unlock();
725
726     SetPropA(data->hwnd, gl_drawable_prop, (HANDLE)data->gl_drawable);
727     SetPropA(data->hwnd, pixmap_prop, (HANDLE)data->pixmap);
728 }
729
730
731 /***********************************************************************
732  *              get_window_changes
733  *
734  * fill the window changes structure
735  */
736 static int get_window_changes( XWindowChanges *changes, const RECT *old, const RECT *new )
737 {
738     int mask = 0;
739
740     if (old->right - old->left != new->right - new->left )
741     {
742         if ((changes->width = new->right - new->left) <= 0) changes->width = 1;
743         else if (changes->width > 65535) changes->width = 65535;
744         mask |= CWWidth;
745     }
746     if (old->bottom - old->top != new->bottom - new->top)
747     {
748         if ((changes->height = new->bottom - new->top) <= 0) changes->height = 1;
749         else if (changes->height > 65535) changes->height = 65535;
750         mask |= CWHeight;
751     }
752     if (old->left != new->left)
753     {
754         changes->x = new->left;
755         mask |= CWX;
756     }
757     if (old->top != new->top)
758     {
759         changes->y = new->top;
760         mask |= CWY;
761     }
762     return mask;
763 }
764
765
766 /***********************************************************************
767  *              create_icon_window
768  */
769 static Window create_icon_window( Display *display, struct x11drv_win_data *data )
770 {
771     XSetWindowAttributes attr;
772
773     attr.event_mask = (ExposureMask | KeyPressMask | KeyReleaseMask | PointerMotionMask |
774                        ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
775     attr.bit_gravity = NorthWestGravity;
776     attr.backing_store = NotUseful/*WhenMapped*/;
777     attr.colormap      = X11DRV_PALETTE_PaletteXColormap; /* Needed due to our visual */
778
779     wine_tsx11_lock();
780     data->icon_window = XCreateWindow( display, root_window, 0, 0,
781                                        GetSystemMetrics( SM_CXICON ),
782                                        GetSystemMetrics( SM_CYICON ),
783                                        0, screen_depth,
784                                        InputOutput, visual,
785                                        CWEventMask | CWBitGravity | CWBackingStore | CWColormap, &attr );
786     XSaveContext( display, data->icon_window, winContext, (char *)data->hwnd );
787     XFlush( display );  /* make sure the window exists before we start painting to it */
788     wine_tsx11_unlock();
789
790     TRACE( "created %lx\n", data->icon_window );
791     SetPropA( data->hwnd, icon_window_prop, (HANDLE)data->icon_window );
792     return data->icon_window;
793 }
794
795
796
797 /***********************************************************************
798  *              destroy_icon_window
799  */
800 static void destroy_icon_window( Display *display, struct x11drv_win_data *data )
801 {
802     if (!data->icon_window) return;
803     if (x11drv_thread_data()->cursor_window == data->icon_window)
804         x11drv_thread_data()->cursor_window = None;
805     wine_tsx11_lock();
806     XDeleteContext( display, data->icon_window, winContext );
807     XDestroyWindow( display, data->icon_window );
808     data->icon_window = 0;
809     wine_tsx11_unlock();
810     RemovePropA( data->hwnd, icon_window_prop );
811 }
812
813
814 /***********************************************************************
815  *              set_icon_hints
816  *
817  * Set the icon wm hints
818  */
819 static void set_icon_hints( Display *display, struct x11drv_win_data *data, HICON hIcon )
820 {
821     XWMHints *hints = data->wm_hints;
822
823     if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
824     if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
825     data->hWMIconBitmap = 0;
826     data->hWMIconMask = 0;
827
828     if (!hIcon)
829     {
830         if (!data->icon_window) create_icon_window( display, data );
831         hints->icon_window = data->icon_window;
832         hints->flags = (hints->flags & ~(IconPixmapHint | IconMaskHint)) | IconWindowHint;
833     }
834     else
835     {
836         HBITMAP hbmOrig;
837         RECT rcMask;
838         BITMAP bmMask;
839         ICONINFO ii;
840         HDC hDC;
841
842         GetIconInfo(hIcon, &ii);
843
844         GetObjectA(ii.hbmMask, sizeof(bmMask), &bmMask);
845         rcMask.top    = 0;
846         rcMask.left   = 0;
847         rcMask.right  = bmMask.bmWidth;
848         rcMask.bottom = bmMask.bmHeight;
849
850         hDC = CreateCompatibleDC(0);
851         hbmOrig = SelectObject(hDC, ii.hbmMask);
852         InvertRect(hDC, &rcMask);
853         SelectObject(hDC, ii.hbmColor);  /* force the color bitmap to x11drv mode too */
854         SelectObject(hDC, hbmOrig);
855         DeleteDC(hDC);
856
857         data->hWMIconBitmap = ii.hbmColor;
858         data->hWMIconMask = ii.hbmMask;
859
860         hints->icon_pixmap = X11DRV_get_pixmap(data->hWMIconBitmap);
861         hints->icon_mask = X11DRV_get_pixmap(data->hWMIconMask);
862         destroy_icon_window( display, data );
863         hints->flags = (hints->flags & ~IconWindowHint) | IconPixmapHint | IconMaskHint;
864     }
865 }
866
867
868 /***********************************************************************
869  *              set_size_hints
870  *
871  * set the window size hints
872  */
873 static void set_size_hints( Display *display, struct x11drv_win_data *data, DWORD style )
874 {
875     XSizeHints* size_hints;
876
877     if (!(size_hints = XAllocSizeHints())) return;
878
879     size_hints->win_gravity = StaticGravity;
880     size_hints->flags |= PWinGravity;
881
882     /* don't update size hints if window is not in normal state */
883     if (!(style & (WS_MINIMIZE | WS_MAXIMIZE)))
884     {
885         if (data->hwnd != GetDesktopWindow())  /* don't force position of desktop */
886         {
887             size_hints->x = data->whole_rect.left;
888             size_hints->y = data->whole_rect.top;
889             size_hints->flags |= PPosition;
890         }
891
892         if (!is_window_resizable( data, style ))
893         {
894             size_hints->max_width = data->whole_rect.right - data->whole_rect.left;
895             size_hints->max_height = data->whole_rect.bottom - data->whole_rect.top;
896             size_hints->min_width = size_hints->max_width;
897             size_hints->min_height = size_hints->max_height;
898             size_hints->flags |= PMinSize | PMaxSize;
899         }
900     }
901     XSetWMNormalHints( display, data->whole_window, size_hints );
902     XFree( size_hints );
903 }
904
905
906 /***********************************************************************
907  *              get_process_name
908  *
909  * get the name of the current process for setting class hints
910  */
911 static char *get_process_name(void)
912 {
913     static char *name;
914
915     if (!name)
916     {
917         WCHAR module[MAX_PATH];
918         DWORD len = GetModuleFileNameW( 0, module, MAX_PATH );
919         if (len && len < MAX_PATH)
920         {
921             char *ptr;
922             WCHAR *p, *appname = module;
923
924             if ((p = strrchrW( appname, '/' ))) appname = p + 1;
925             if ((p = strrchrW( appname, '\\' ))) appname = p + 1;
926             len = WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, NULL, 0, NULL, NULL );
927             if ((ptr = HeapAlloc( GetProcessHeap(), 0, len )))
928             {
929                 WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, ptr, len, NULL, NULL );
930                 name = ptr;
931             }
932         }
933     }
934     return name;
935 }
936
937
938 /***********************************************************************
939  *              set_initial_wm_hints
940  *
941  * Set the window manager hints that don't change over the lifetime of a window.
942  */
943 static void set_initial_wm_hints( Display *display, struct x11drv_win_data *data )
944 {
945     long i;
946     Atom protocols[3];
947     Atom dndVersion = WINE_XDND_VERSION;
948     XClassHint *class_hints;
949     char *process_name = get_process_name();
950
951     wine_tsx11_lock();
952
953     /* wm protocols */
954     i = 0;
955     protocols[i++] = x11drv_atom(WM_DELETE_WINDOW);
956     protocols[i++] = x11drv_atom(_NET_WM_PING);
957     if (use_take_focus) protocols[i++] = x11drv_atom(WM_TAKE_FOCUS);
958     XChangeProperty( display, data->whole_window, x11drv_atom(WM_PROTOCOLS),
959                      XA_ATOM, 32, PropModeReplace, (unsigned char *)protocols, i );
960
961     /* class hints */
962     if ((class_hints = XAllocClassHint()))
963     {
964         static char wine[] = "Wine";
965
966         class_hints->res_name = process_name;
967         class_hints->res_class = wine;
968         XSetClassHint( display, data->whole_window, class_hints );
969         XFree( class_hints );
970     }
971
972     /* set the WM_CLIENT_MACHINE and WM_LOCALE_NAME properties */
973     XSetWMProperties(display, data->whole_window, NULL, NULL, NULL, 0, NULL, NULL, NULL);
974     /* set the pid. together, these properties are needed so the window manager can kill us if we freeze */
975     i = getpid();
976     XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_PID),
977                     XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&i, 1);
978
979     XChangeProperty( display, data->whole_window, x11drv_atom(XdndAware),
980                      XA_ATOM, 32, PropModeReplace, (unsigned char*)&dndVersion, 1 );
981
982     data->wm_hints = XAllocWMHints();
983     wine_tsx11_unlock();
984
985     if (data->wm_hints)
986     {
987         HICON icon = (HICON)SendMessageW( data->hwnd, WM_GETICON, ICON_BIG, 0 );
988         if (!icon) icon = (HICON)GetClassLongPtrW( data->hwnd, GCLP_HICON );
989         data->wm_hints->flags = 0;
990         set_icon_hints( display, data, icon );
991     }
992 }
993
994
995 /***********************************************************************
996  *              get_owner_whole_window
997  *
998  * Retrieve an owner's window, creating it if necessary.
999  */
1000 static Window get_owner_whole_window( HWND owner )
1001 {
1002     struct x11drv_win_data *data;
1003
1004     if (!owner) return 0;
1005
1006     if (!(data = X11DRV_get_win_data( owner )))
1007     {
1008         if (GetWindowThreadProcessId( owner, NULL ) != GetCurrentThreadId() ||
1009             !(data = X11DRV_create_win_data( owner )))
1010             return (Window)GetPropA( owner, whole_window_prop );
1011     }
1012     else if (!data->managed)  /* make it managed */
1013     {
1014         SetWindowPos( owner, 0, 0, 0, 0, 0,
1015                       SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOSIZE | SWP_NOMOVE |
1016                       SWP_NOREDRAW | SWP_DEFERERASE | SWP_NOSENDCHANGING | SWP_STATECHANGED );
1017     }
1018     return data->whole_window;
1019 }
1020
1021
1022 /***********************************************************************
1023  *              set_wm_hints
1024  *
1025  * Set the window manager hints for a newly-created window
1026  */
1027 static void set_wm_hints( Display *display, struct x11drv_win_data *data )
1028 {
1029     Window group_leader = data->whole_window;
1030     Window owner_win = 0;
1031     Atom window_type;
1032     MwmHints mwm_hints;
1033     DWORD style, ex_style;
1034     HWND owner;
1035
1036     if (data->hwnd == GetDesktopWindow())
1037     {
1038         /* force some styles for the desktop to get the correct decorations */
1039         style = WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
1040         ex_style = WS_EX_APPWINDOW;
1041         owner = 0;
1042     }
1043     else
1044     {
1045         style = GetWindowLongW( data->hwnd, GWL_STYLE );
1046         ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
1047         owner = get_window_owner( data->hwnd );
1048         if ((owner_win = get_owner_whole_window( owner ))) group_leader = owner_win;
1049     }
1050
1051     wine_tsx11_lock();
1052
1053     if (owner_win) XSetTransientForHint( display, data->whole_window, owner_win );
1054
1055     /* size hints */
1056     set_size_hints( display, data, style );
1057
1058     /* set the WM_WINDOW_TYPE */
1059     if (style & WS_THICKFRAME) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
1060     else if (ex_style & WS_EX_APPWINDOW) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
1061     else if (style & WS_DLGFRAME) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG);
1062     else if (ex_style & WS_EX_DLGMODALFRAME) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG);
1063     else if ((style & WS_POPUP) && owner) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG);
1064 #if 0  /* many window managers don't handle utility windows very well */
1065     else if (ex_style & WS_EX_TOOLWINDOW) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_UTILITY);
1066 #endif
1067     else window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
1068
1069     XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_WINDOW_TYPE),
1070                     XA_ATOM, 32, PropModeReplace, (unsigned char*)&window_type, 1);
1071
1072     mwm_hints.flags = MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
1073     mwm_hints.decorations = get_mwm_decorations( data, style, ex_style );
1074     mwm_hints.functions = MWM_FUNC_MOVE;
1075     if (is_window_resizable( data, style )) mwm_hints.functions |= MWM_FUNC_RESIZE;
1076     if (style & WS_MINIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MINIMIZE;
1077     if (style & WS_MAXIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MAXIMIZE;
1078     if (style & WS_SYSMENU)     mwm_hints.functions |= MWM_FUNC_CLOSE;
1079
1080     XChangeProperty( display, data->whole_window, x11drv_atom(_MOTIF_WM_HINTS),
1081                      x11drv_atom(_MOTIF_WM_HINTS), 32, PropModeReplace,
1082                      (unsigned char*)&mwm_hints, sizeof(mwm_hints)/sizeof(long) );
1083
1084     /* wm hints */
1085     if (data->wm_hints)
1086     {
1087         data->wm_hints->flags |= InputHint | StateHint | WindowGroupHint;
1088         data->wm_hints->input = !use_take_focus && !(style & WS_DISABLED);
1089         data->wm_hints->initial_state = (style & WS_MINIMIZE) ? IconicState : NormalState;
1090         data->wm_hints->window_group = group_leader;
1091         XSetWMHints( display, data->whole_window, data->wm_hints );
1092     }
1093
1094     wine_tsx11_unlock();
1095 }
1096
1097
1098 /***********************************************************************
1099  *     update_net_wm_states
1100  */
1101 void update_net_wm_states( Display *display, struct x11drv_win_data *data )
1102 {
1103     static const unsigned int state_atoms[NB_NET_WM_STATES] =
1104     {
1105         XATOM__NET_WM_STATE_FULLSCREEN,
1106         XATOM__NET_WM_STATE_ABOVE,
1107         XATOM__NET_WM_STATE_MAXIMIZED_VERT,
1108         XATOM__NET_WM_STATE_SKIP_PAGER,
1109         XATOM__NET_WM_STATE_SKIP_TASKBAR
1110     };
1111
1112     DWORD i, style, ex_style, new_state = 0;
1113
1114     if (!data->managed) return;
1115     if (data->whole_window == root_window) return;
1116
1117     style = GetWindowLongW( data->hwnd, GWL_STYLE );
1118     if (data->whole_rect.left <= 0 && data->whole_rect.right >= screen_width &&
1119         data->whole_rect.top <= 0 && data->whole_rect.bottom >= screen_height)
1120     {
1121         if ((style & WS_MAXIMIZE) && (style & WS_CAPTION) == WS_CAPTION)
1122             new_state |= (1 << NET_WM_STATE_MAXIMIZED);
1123         else if (!(style & WS_MINIMIZE))
1124             new_state |= (1 << NET_WM_STATE_FULLSCREEN);
1125     }
1126     else if (style & WS_MAXIMIZE)
1127         new_state |= (1 << NET_WM_STATE_MAXIMIZED);
1128
1129     ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
1130     if (ex_style & WS_EX_TOPMOST)
1131         new_state |= (1 << NET_WM_STATE_ABOVE);
1132     if (ex_style & WS_EX_TOOLWINDOW)
1133         new_state |= (1 << NET_WM_STATE_SKIP_TASKBAR) | (1 << NET_WM_STATE_SKIP_PAGER);
1134     if (!(ex_style & WS_EX_APPWINDOW) && get_window_owner( data->hwnd ))
1135         new_state |= (1 << NET_WM_STATE_SKIP_TASKBAR);
1136
1137     if (!data->mapped)  /* set the _NET_WM_STATE atom directly */
1138     {
1139         Atom atoms[NB_NET_WM_STATES+1];
1140         DWORD count;
1141
1142         for (i = count = 0; i < NB_NET_WM_STATES; i++)
1143         {
1144             if (!(new_state & (1 << i))) continue;
1145             TRACE( "setting wm state %u for unmapped window %p/%lx\n",
1146                    i, data->hwnd, data->whole_window );
1147             atoms[count++] = X11DRV_Atoms[state_atoms[i] - FIRST_XATOM];
1148             if (state_atoms[i] == XATOM__NET_WM_STATE_MAXIMIZED_VERT)
1149                 atoms[count++] = x11drv_atom(_NET_WM_STATE_MAXIMIZED_HORZ);
1150         }
1151         wine_tsx11_lock();
1152         XChangeProperty( display, data->whole_window, x11drv_atom(_NET_WM_STATE), XA_ATOM,
1153                          32, PropModeReplace, (unsigned char *)atoms, count );
1154         wine_tsx11_unlock();
1155     }
1156     else  /* ask the window manager to do it for us */
1157     {
1158         XEvent xev;
1159
1160         xev.xclient.type = ClientMessage;
1161         xev.xclient.window = data->whole_window;
1162         xev.xclient.message_type = x11drv_atom(_NET_WM_STATE);
1163         xev.xclient.serial = 0;
1164         xev.xclient.display = display;
1165         xev.xclient.send_event = True;
1166         xev.xclient.format = 32;
1167         xev.xclient.data.l[3] = 1;
1168
1169         for (i = 0; i < NB_NET_WM_STATES; i++)
1170         {
1171             if (!((data->net_wm_state ^ new_state) & (1 << i))) continue;  /* unchanged */
1172
1173             TRACE( "setting wm state %u for window %p/%lx to %u prev %u\n",
1174                    i, data->hwnd, data->whole_window,
1175                    (new_state & (1 << i)) != 0, (data->net_wm_state & (1 << i)) != 0 );
1176
1177             xev.xclient.data.l[0] = (new_state & (1 << i)) ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
1178             xev.xclient.data.l[1] = X11DRV_Atoms[state_atoms[i] - FIRST_XATOM];
1179             xev.xclient.data.l[2] = ((state_atoms[i] == XATOM__NET_WM_STATE_MAXIMIZED_VERT) ?
1180                                      x11drv_atom(_NET_WM_STATE_MAXIMIZED_HORZ) : 0);
1181             wine_tsx11_lock();
1182             XSendEvent( display, root_window, False,
1183                         SubstructureRedirectMask | SubstructureNotifyMask, &xev );
1184             wine_tsx11_unlock();
1185         }
1186     }
1187     data->net_wm_state = new_state;
1188 }
1189
1190
1191 /***********************************************************************
1192  *     set_xembed_flags
1193  */
1194 static void set_xembed_flags( Display *display, struct x11drv_win_data *data, unsigned long flags )
1195 {
1196     unsigned long info[2];
1197
1198     info[0] = 0; /* protocol version */
1199     info[1] = flags;
1200     wine_tsx11_lock();
1201     XChangeProperty( display, data->whole_window, x11drv_atom(_XEMBED_INFO),
1202                      x11drv_atom(_XEMBED_INFO), 32, PropModeReplace, (unsigned char*)info, 2 );
1203     wine_tsx11_unlock();
1204 }
1205
1206
1207 /***********************************************************************
1208  *     map_window
1209  */
1210 static void map_window( Display *display, struct x11drv_win_data *data, DWORD new_style )
1211 {
1212     TRACE( "win %p/%lx\n", data->hwnd, data->whole_window );
1213
1214     remove_startup_notification( display, data->whole_window );
1215
1216     wait_for_withdrawn_state( display, data, TRUE );
1217
1218     if (!data->embedded)
1219     {
1220         update_net_wm_states( display, data );
1221         sync_window_style( display, data );
1222         wine_tsx11_lock();
1223         XMapWindow( display, data->whole_window );
1224         wine_tsx11_unlock();
1225     }
1226     else set_xembed_flags( display, data, XEMBED_MAPPED );
1227
1228     data->mapped = TRUE;
1229     data->iconic = (new_style & WS_MINIMIZE) != 0;
1230 }
1231
1232
1233 /***********************************************************************
1234  *     unmap_window
1235  */
1236 static void unmap_window( Display *display, struct x11drv_win_data *data )
1237 {
1238     TRACE( "win %p/%lx\n", data->hwnd, data->whole_window );
1239
1240     if (!data->embedded)
1241     {
1242         wait_for_withdrawn_state( display, data, FALSE );
1243         wine_tsx11_lock();
1244         if (data->managed) XWithdrawWindow( display, data->whole_window, DefaultScreen(display) );
1245         else XUnmapWindow( display, data->whole_window );
1246         wine_tsx11_unlock();
1247     }
1248     else set_xembed_flags( display, data, 0 );
1249
1250     data->mapped = FALSE;
1251     data->net_wm_state = 0;
1252 }
1253
1254
1255 /***********************************************************************
1256  *     make_window_embedded
1257  */
1258 void make_window_embedded( Display *display, struct x11drv_win_data *data )
1259 {
1260     if (data->mapped)
1261     {
1262         /* the window cannot be mapped before being embedded */
1263         unmap_window( display, data );
1264         data->embedded = TRUE;
1265         map_window( display, data, 0 );
1266     }
1267     else
1268     {
1269         data->embedded = TRUE;
1270         set_xembed_flags( display, data, 0 );
1271     }
1272 }
1273
1274
1275 /***********************************************************************
1276  *              X11DRV_window_to_X_rect
1277  *
1278  * Convert a rect from client to X window coordinates
1279  */
1280 static void X11DRV_window_to_X_rect( struct x11drv_win_data *data, RECT *rect )
1281 {
1282     RECT rc;
1283
1284     if (!data->managed) return;
1285     if (IsRectEmpty( rect )) return;
1286
1287     get_x11_rect_offset( data, &rc );
1288
1289     rect->left   -= rc.left;
1290     rect->right  -= rc.right;
1291     rect->top    -= rc.top;
1292     rect->bottom -= rc.bottom;
1293     if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
1294     if (rect->left >= rect->right) rect->right = rect->left + 1;
1295 }
1296
1297
1298 /***********************************************************************
1299  *              X11DRV_X_to_window_rect
1300  *
1301  * Opposite of X11DRV_window_to_X_rect
1302  */
1303 void X11DRV_X_to_window_rect( struct x11drv_win_data *data, RECT *rect )
1304 {
1305     RECT rc;
1306
1307     if (!data->managed) return;
1308     if (IsRectEmpty( rect )) return;
1309
1310     get_x11_rect_offset( data, &rc );
1311
1312     rect->left   += rc.left;
1313     rect->right  += rc.right;
1314     rect->top    += rc.top;
1315     rect->bottom += rc.bottom;
1316     if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
1317     if (rect->left >= rect->right) rect->right = rect->left + 1;
1318 }
1319
1320
1321 /***********************************************************************
1322  *              sync_window_position
1323  *
1324  * Synchronize the X window position with the Windows one
1325  */
1326 static void sync_window_position( Display *display, struct x11drv_win_data *data,
1327                                   UINT swp_flags, const RECT *old_window_rect,
1328                                   const RECT *old_whole_rect, const RECT *old_client_rect )
1329 {
1330     DWORD style = GetWindowLongW( data->hwnd, GWL_STYLE );
1331     XWindowChanges changes;
1332     unsigned int mask = 0;
1333
1334     if (data->managed && data->iconic) return;
1335
1336     /* resizing a managed maximized window is not allowed */
1337     if (!(style & WS_MAXIMIZE) || !data->managed)
1338     {
1339         changes.width = data->whole_rect.right - data->whole_rect.left;
1340         changes.height = data->whole_rect.bottom - data->whole_rect.top;
1341         /* if window rect is empty force size to 1x1 */
1342         if (changes.width <= 0 || changes.height <= 0) changes.width = changes.height = 1;
1343         if (changes.width > 65535) changes.width = 65535;
1344         if (changes.height > 65535) changes.height = 65535;
1345         mask |= CWWidth | CWHeight;
1346     }
1347
1348     /* only the size is allowed to change for the desktop window */
1349     if (data->whole_window != root_window)
1350     {
1351         changes.x = data->whole_rect.left - virtual_screen_rect.left;
1352         changes.y = data->whole_rect.top - virtual_screen_rect.top;
1353         mask |= CWX | CWY;
1354     }
1355
1356     if (!(swp_flags & SWP_NOZORDER) || (swp_flags & SWP_SHOWWINDOW))
1357     {
1358         /* find window that this one must be after */
1359         HWND prev = GetWindow( data->hwnd, GW_HWNDPREV );
1360         while (prev && !(GetWindowLongW( prev, GWL_STYLE ) & WS_VISIBLE))
1361             prev = GetWindow( prev, GW_HWNDPREV );
1362         if (!prev)  /* top child */
1363         {
1364             changes.stack_mode = Above;
1365             mask |= CWStackMode;
1366         }
1367         /* should use stack_mode Below but most window managers don't get it right */
1368         /* and Above with a sibling doesn't work so well either, so we ignore it */
1369     }
1370
1371     wine_tsx11_lock();
1372     set_size_hints( display, data, style );
1373     data->configure_serial = NextRequest( display );
1374     XReconfigureWMWindow( display, data->whole_window,
1375                           DefaultScreen(display), mask, &changes );
1376 #ifdef HAVE_LIBXSHAPE
1377     if (data->shaped)
1378     {
1379         int old_x_offset = old_window_rect->left - old_whole_rect->left;
1380         int old_y_offset = old_window_rect->top - old_whole_rect->top;
1381         int new_x_offset = data->window_rect.left - data->whole_rect.left;
1382         int new_y_offset = data->window_rect.top - data->whole_rect.top;
1383         if (old_x_offset != new_x_offset || old_y_offset != new_y_offset)
1384             XShapeOffsetShape( display, data->whole_window, ShapeBounding,
1385                                new_x_offset - old_x_offset, new_y_offset - old_y_offset );
1386     }
1387 #endif
1388     wine_tsx11_unlock();
1389
1390     TRACE( "win %p/%lx pos %d,%d,%dx%d after %lx changes=%x serial=%lu\n",
1391            data->hwnd, data->whole_window, data->whole_rect.left, data->whole_rect.top,
1392            data->whole_rect.right - data->whole_rect.left,
1393            data->whole_rect.bottom - data->whole_rect.top,
1394            changes.sibling, mask, data->configure_serial );
1395 }
1396
1397
1398 /***********************************************************************
1399  *              sync_client_position
1400  *
1401  * Synchronize the X client window position with the Windows one
1402  */
1403 static void sync_client_position( Display *display, struct x11drv_win_data *data,
1404                                   UINT swp_flags, const RECT *old_client_rect,
1405                                   const RECT *old_whole_rect )
1406 {
1407     int mask;
1408     XWindowChanges changes;
1409     RECT old = *old_client_rect;
1410     RECT new = data->client_rect;
1411
1412     OffsetRect( &old, -old_whole_rect->left, -old_whole_rect->top );
1413     OffsetRect( &new, -data->whole_rect.left, -data->whole_rect.top );
1414     if (!(mask = get_window_changes( &changes, &old, &new ))) return;
1415
1416     if (data->client_window)
1417     {
1418         TRACE( "setting client win %lx pos %d,%d,%dx%d changes=%x\n",
1419                data->client_window, new.left, new.top,
1420                new.right - new.left, new.bottom - new.top, mask );
1421         wine_tsx11_lock();
1422         XConfigureWindow( display, data->client_window, mask, &changes );
1423         wine_tsx11_unlock();
1424     }
1425
1426     if (data->gl_drawable && (mask & (CWWidth|CWHeight))) sync_gl_drawable( data );
1427 }
1428
1429
1430 /***********************************************************************
1431  *              move_window_bits
1432  *
1433  * Move the window bits when a window is moved.
1434  */
1435 static void move_window_bits( struct x11drv_win_data *data, const RECT *old_rect, const RECT *new_rect,
1436                               const RECT *old_client_rect )
1437 {
1438     RECT src_rect = *old_rect;
1439     RECT dst_rect = *new_rect;
1440     HDC hdc_src, hdc_dst;
1441     INT code;
1442     HRGN rgn = 0;
1443     HWND parent = 0;
1444
1445     if (!data->whole_window)
1446     {
1447         OffsetRect( &dst_rect, -data->window_rect.left, -data->window_rect.top );
1448         parent = GetAncestor( data->hwnd, GA_PARENT );
1449         hdc_src = GetDCEx( parent, 0, DCX_CACHE );
1450         hdc_dst = GetDCEx( data->hwnd, 0, DCX_CACHE | DCX_WINDOW );
1451     }
1452     else
1453     {
1454         OffsetRect( &dst_rect, -data->client_rect.left, -data->client_rect.top );
1455         /* make src rect relative to the old position of the window */
1456         OffsetRect( &src_rect, -old_client_rect->left, -old_client_rect->top );
1457         if (dst_rect.left == src_rect.left && dst_rect.top == src_rect.top) return;
1458         hdc_src = hdc_dst = GetDCEx( data->hwnd, 0, DCX_CACHE );
1459     }
1460
1461     code = X11DRV_START_EXPOSURES;
1462     ExtEscape( hdc_dst, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, 0, NULL );
1463
1464     TRACE( "copying bits for win %p/%lx/%lx %s -> %s\n",
1465            data->hwnd, data->whole_window, data->client_window,
1466            wine_dbgstr_rect(&src_rect), wine_dbgstr_rect(&dst_rect) );
1467     BitBlt( hdc_dst, dst_rect.left, dst_rect.top,
1468             dst_rect.right - dst_rect.left, dst_rect.bottom - dst_rect.top,
1469             hdc_src, src_rect.left, src_rect.top, SRCCOPY );
1470
1471     code = X11DRV_END_EXPOSURES;
1472     ExtEscape( hdc_dst, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, sizeof(rgn), (LPSTR)&rgn );
1473
1474     ReleaseDC( data->hwnd, hdc_dst );
1475     if (hdc_src != hdc_dst) ReleaseDC( parent, hdc_src );
1476
1477     if (rgn)
1478     {
1479         if (!data->whole_window)
1480         {
1481             /* map region to client rect since we are using DCX_WINDOW */
1482             OffsetRgn( rgn, data->window_rect.left - data->client_rect.left,
1483                        data->window_rect.top - data->client_rect.top );
1484             RedrawWindow( data->hwnd, NULL, rgn,
1485                           RDW_INVALIDATE | RDW_FRAME | RDW_ERASE | RDW_ALLCHILDREN );
1486         }
1487         else RedrawWindow( data->hwnd, NULL, rgn, RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN );
1488         DeleteObject( rgn );
1489     }
1490 }
1491
1492
1493 /**********************************************************************
1494  *              create_whole_window
1495  *
1496  * Create the whole X window for a given window
1497  */
1498 static Window create_whole_window( Display *display, struct x11drv_win_data *data )
1499 {
1500     int cx, cy, mask;
1501     XSetWindowAttributes attr;
1502     WCHAR text[1024];
1503     COLORREF key;
1504     BYTE alpha;
1505     DWORD layered_flags;
1506     HRGN win_rgn;
1507
1508     if (!data->managed && is_window_managed( data->hwnd, SWP_NOACTIVATE, &data->window_rect ))
1509     {
1510         TRACE( "making win %p/%lx managed\n", data->hwnd, data->whole_window );
1511         data->managed = TRUE;
1512         SetPropA( data->hwnd, managed_prop, (HANDLE)1 );
1513     }
1514
1515     if ((win_rgn = CreateRectRgn( 0, 0, 0, 0 )) &&
1516         GetWindowRgn( data->hwnd, win_rgn ) == ERROR)
1517     {
1518         DeleteObject( win_rgn );
1519         win_rgn = 0;
1520     }
1521     data->shaped = (win_rgn != 0);
1522
1523     mask = get_window_attributes( display, data, &attr );
1524
1525     data->whole_rect = data->window_rect;
1526     X11DRV_window_to_X_rect( data, &data->whole_rect );
1527     if (!(cx = data->whole_rect.right - data->whole_rect.left)) cx = 1;
1528     else if (cx > 65535) cx = 65535;
1529     if (!(cy = data->whole_rect.bottom - data->whole_rect.top)) cy = 1;
1530     else if (cy > 65535) cy = 65535;
1531
1532     wine_tsx11_lock();
1533     data->whole_window = XCreateWindow( display, root_window,
1534                                         data->whole_rect.left - virtual_screen_rect.left,
1535                                         data->whole_rect.top - virtual_screen_rect.top,
1536                                         cx, cy, 0, screen_depth, InputOutput,
1537                                         visual, mask, &attr );
1538
1539     if (data->whole_window) XSaveContext( display, data->whole_window, winContext, (char *)data->hwnd );
1540     wine_tsx11_unlock();
1541
1542     if (!data->whole_window) goto done;
1543
1544     if (!create_client_window( display, data, NULL ))
1545     {
1546         wine_tsx11_lock();
1547         XDeleteContext( display, data->whole_window, winContext );
1548         XDestroyWindow( display, data->whole_window );
1549         data->whole_window = 0;
1550         wine_tsx11_unlock();
1551         goto done;
1552     }
1553
1554     set_initial_wm_hints( display, data );
1555     set_wm_hints( display, data );
1556
1557     SetPropA( data->hwnd, whole_window_prop, (HANDLE)data->whole_window );
1558
1559     /* set the window text */
1560     if (!InternalGetWindowText( data->hwnd, text, sizeof(text)/sizeof(WCHAR) )) text[0] = 0;
1561     sync_window_text( display, data->whole_window, text );
1562
1563     /* set the window region */
1564     if (win_rgn) sync_window_region( display, data, win_rgn );
1565
1566     /* set the window opacity */
1567     if (!GetLayeredWindowAttributes( data->hwnd, &key, &alpha, &layered_flags )) layered_flags = 0;
1568     sync_window_opacity( display, data->whole_window, key, alpha, layered_flags );
1569
1570     wine_tsx11_lock();
1571     XFlush( display );  /* make sure the window exists before we start painting to it */
1572     wine_tsx11_unlock();
1573 done:
1574     if (win_rgn) DeleteObject( win_rgn );
1575     return data->whole_window;
1576 }
1577
1578
1579 /**********************************************************************
1580  *              destroy_whole_window
1581  *
1582  * Destroy the whole X window for a given window.
1583  */
1584 static void destroy_whole_window( Display *display, struct x11drv_win_data *data, BOOL already_destroyed )
1585 {
1586     struct x11drv_thread_data *thread_data = x11drv_thread_data();
1587
1588     if (!data->whole_window) return;
1589
1590     TRACE( "win %p xwin %lx/%lx\n", data->hwnd, data->whole_window, data->client_window );
1591     if (thread_data->cursor_window == data->whole_window ||
1592         thread_data->cursor_window == data->client_window)
1593         thread_data->cursor_window = None;
1594     wine_tsx11_lock();
1595     XDeleteContext( display, data->whole_window, winContext );
1596     XDeleteContext( display, data->client_window, winContext );
1597     if (!already_destroyed) XDestroyWindow( display, data->whole_window );
1598     data->whole_window = data->client_window = 0;
1599     data->wm_state = WithdrawnState;
1600     data->net_wm_state = 0;
1601     data->mapped = FALSE;
1602     if (data->xic)
1603     {
1604         XUnsetICFocus( data->xic );
1605         XDestroyIC( data->xic );
1606         data->xic = 0;
1607     }
1608     /* Outlook stops processing messages after destroying a dialog, so we need an explicit flush */
1609     XFlush( display );
1610     XFree( data->wm_hints );
1611     data->wm_hints = NULL;
1612     wine_tsx11_unlock();
1613     RemovePropA( data->hwnd, whole_window_prop );
1614     RemovePropA( data->hwnd, client_window_prop );
1615 }
1616
1617
1618 /*****************************************************************
1619  *              SetWindowText   (X11DRV.@)
1620  */
1621 void CDECL X11DRV_SetWindowText( HWND hwnd, LPCWSTR text )
1622 {
1623     Window win;
1624
1625     if ((win = X11DRV_get_whole_window( hwnd )) && win != DefaultRootWindow(gdi_display))
1626     {
1627         Display *display = thread_init_display();
1628         sync_window_text( display, win, text );
1629     }
1630 }
1631
1632
1633 /***********************************************************************
1634  *              SetWindowStyle   (X11DRV.@)
1635  *
1636  * Update the X state of a window to reflect a style change
1637  */
1638 void CDECL X11DRV_SetWindowStyle( HWND hwnd, INT offset, STYLESTRUCT *style )
1639 {
1640     struct x11drv_win_data *data;
1641     DWORD changed;
1642
1643     if (hwnd == GetDesktopWindow()) return;
1644     changed = style->styleNew ^ style->styleOld;
1645
1646     if (offset == GWL_STYLE && (changed & WS_VISIBLE) && (style->styleNew & WS_VISIBLE))
1647     {
1648         /* we don't unmap windows, that causes trouble with the window manager */
1649         if (!(data = X11DRV_get_win_data( hwnd )) &&
1650             !(data = X11DRV_create_win_data( hwnd ))) return;
1651
1652         if (data->whole_window && is_window_rect_mapped( &data->window_rect ))
1653         {
1654             Display *display = thread_display();
1655             set_wm_hints( display, data );
1656             if (!data->mapped) map_window( display, data, style->styleNew );
1657         }
1658     }
1659
1660     if (offset == GWL_STYLE && (changed & WS_DISABLED))
1661     {
1662         data = X11DRV_get_win_data( hwnd );
1663         if (data && data->wm_hints)
1664         {
1665             wine_tsx11_lock();
1666             data->wm_hints->input = !(style->styleNew & WS_DISABLED);
1667             XSetWMHints( thread_display(), data->whole_window, data->wm_hints );
1668             wine_tsx11_unlock();
1669         }
1670     }
1671
1672     if (offset == GWL_EXSTYLE && (changed & WS_EX_LAYERED))
1673     {
1674         /* changing WS_EX_LAYERED resets attributes */
1675         if ((data = X11DRV_get_win_data( hwnd )) && data->whole_window)
1676             sync_window_opacity( thread_display(), data->whole_window, 0, 0, 0 );
1677     }
1678 }
1679
1680
1681 /***********************************************************************
1682  *              DestroyWindow   (X11DRV.@)
1683  */
1684 void CDECL X11DRV_DestroyWindow( HWND hwnd )
1685 {
1686     struct x11drv_thread_data *thread_data = x11drv_thread_data();
1687     struct x11drv_win_data *data;
1688
1689     if (!(data = X11DRV_get_win_data( hwnd ))) return;
1690
1691     if (data->pixmap)
1692     {
1693         wine_tsx11_lock();
1694         destroy_glxpixmap(gdi_display, data->gl_drawable);
1695         XFreePixmap(gdi_display, data->pixmap);
1696         wine_tsx11_unlock();
1697     }
1698     else if (data->gl_drawable)
1699     {
1700         wine_tsx11_lock();
1701         XDestroyWindow(gdi_display, data->gl_drawable);
1702         wine_tsx11_unlock();
1703     }
1704
1705     destroy_whole_window( thread_data->display, data, FALSE );
1706     destroy_icon_window( thread_data->display, data );
1707
1708     if (data->colormap)
1709     {
1710         wine_tsx11_lock();
1711         XFreeColormap( thread_data->display, data->colormap );
1712         wine_tsx11_unlock();
1713     }
1714
1715     if (thread_data->last_focus == hwnd) thread_data->last_focus = 0;
1716     if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
1717     if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
1718     wine_tsx11_lock();
1719     XDeleteContext( thread_data->display, (XID)hwnd, win_data_context );
1720     wine_tsx11_unlock();
1721     HeapFree( GetProcessHeap(), 0, data );
1722 }
1723
1724
1725 /***********************************************************************
1726  *              X11DRV_DestroyNotify
1727  */
1728 void X11DRV_DestroyNotify( HWND hwnd, XEvent *event )
1729 {
1730     Display *display = event->xdestroywindow.display;
1731     struct x11drv_win_data *data;
1732
1733     if (!(data = X11DRV_get_win_data( hwnd ))) return;
1734
1735     FIXME( "window %p/%lx destroyed from the outside\n", hwnd, data->whole_window );
1736     destroy_whole_window( display, data, TRUE );
1737 }
1738
1739
1740 static struct x11drv_win_data *alloc_win_data( Display *display, HWND hwnd )
1741 {
1742     struct x11drv_win_data *data;
1743
1744     if ((data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data))))
1745     {
1746         data->hwnd = hwnd;
1747         wine_tsx11_lock();
1748         if (!winContext) winContext = XUniqueContext();
1749         if (!win_data_context) win_data_context = XUniqueContext();
1750         XSaveContext( display, (XID)hwnd, win_data_context, (char *)data );
1751         wine_tsx11_unlock();
1752     }
1753     return data;
1754 }
1755
1756
1757 /* initialize the desktop window id in the desktop manager process */
1758 static struct x11drv_win_data *create_desktop_win_data( Display *display, HWND hwnd )
1759 {
1760     struct x11drv_win_data *data;
1761
1762     if (!(data = alloc_win_data( display, hwnd ))) return NULL;
1763     data->whole_window = data->client_window = root_window;
1764     data->managed = TRUE;
1765     SetPropA( data->hwnd, managed_prop, (HANDLE)1 );
1766     SetPropA( data->hwnd, whole_window_prop, (HANDLE)root_window );
1767     SetPropA( data->hwnd, client_window_prop, (HANDLE)root_window );
1768     set_initial_wm_hints( display, data );
1769     return data;
1770 }
1771
1772 /**********************************************************************
1773  *              CreateDesktopWindow   (X11DRV.@)
1774  */
1775 BOOL CDECL X11DRV_CreateDesktopWindow( HWND hwnd )
1776 {
1777     unsigned int width, height;
1778
1779     /* retrieve the real size of the desktop */
1780     SERVER_START_REQ( get_window_rectangles )
1781     {
1782         req->handle = wine_server_user_handle( hwnd );
1783         wine_server_call( req );
1784         width  = reply->window.right - reply->window.left;
1785         height = reply->window.bottom - reply->window.top;
1786     }
1787     SERVER_END_REQ;
1788
1789     if (!width && !height)  /* not initialized yet */
1790     {
1791         SERVER_START_REQ( set_window_pos )
1792         {
1793             req->handle        = wine_server_user_handle( hwnd );
1794             req->previous      = 0;
1795             req->flags         = SWP_NOZORDER;
1796             req->window.left   = virtual_screen_rect.left;
1797             req->window.top    = virtual_screen_rect.top;
1798             req->window.right  = virtual_screen_rect.right;
1799             req->window.bottom = virtual_screen_rect.bottom;
1800             req->client        = req->window;
1801             wine_server_call( req );
1802         }
1803         SERVER_END_REQ;
1804     }
1805     else
1806     {
1807         Window win = (Window)GetPropA( hwnd, whole_window_prop );
1808         if (win && win != root_window) X11DRV_init_desktop( win, width, height );
1809     }
1810     return TRUE;
1811 }
1812
1813
1814 /**********************************************************************
1815  *              CreateWindow   (X11DRV.@)
1816  */
1817 BOOL CDECL X11DRV_CreateWindow( HWND hwnd )
1818 {
1819     if (hwnd == GetDesktopWindow() && root_window != DefaultRootWindow( gdi_display ))
1820     {
1821         Display *display = thread_init_display();
1822
1823         /* the desktop win data can't be created lazily */
1824         if (!create_desktop_win_data( display, hwnd )) return FALSE;
1825     }
1826     return TRUE;
1827 }
1828
1829
1830 /***********************************************************************
1831  *              X11DRV_get_win_data
1832  *
1833  * Return the X11 data structure associated with a window.
1834  */
1835 struct x11drv_win_data *X11DRV_get_win_data( HWND hwnd )
1836 {
1837     struct x11drv_thread_data *thread_data = x11drv_thread_data();
1838     char *data;
1839
1840     if (!thread_data) return NULL;
1841     if (!hwnd) return NULL;
1842     if (XFindContext( thread_data->display, (XID)hwnd, win_data_context, &data )) data = NULL;
1843     return (struct x11drv_win_data *)data;
1844 }
1845
1846
1847 /***********************************************************************
1848  *              X11DRV_create_win_data
1849  *
1850  * Create an X11 data window structure for an existing window.
1851  */
1852 struct x11drv_win_data *X11DRV_create_win_data( HWND hwnd )
1853 {
1854     Display *display;
1855     struct x11drv_win_data *data;
1856     HWND parent;
1857
1858     if (!(parent = GetAncestor( hwnd, GA_PARENT ))) return NULL;  /* desktop */
1859
1860     /* don't create win data for HWND_MESSAGE windows */
1861     if (parent != GetDesktopWindow() && !GetAncestor( parent, GA_PARENT )) return NULL;
1862
1863     display = thread_init_display();
1864     if (!(data = alloc_win_data( display, hwnd ))) return NULL;
1865
1866     GetWindowRect( hwnd, &data->window_rect );
1867     MapWindowPoints( 0, parent, (POINT *)&data->window_rect, 2 );
1868     data->whole_rect = data->window_rect;
1869     GetClientRect( hwnd, &data->client_rect );
1870     MapWindowPoints( hwnd, parent, (POINT *)&data->client_rect, 2 );
1871
1872     if (parent == GetDesktopWindow())
1873     {
1874         if (!create_whole_window( display, data ))
1875         {
1876             HeapFree( GetProcessHeap(), 0, data );
1877             return NULL;
1878         }
1879         TRACE( "win %p/%lx/%lx window %s whole %s client %s\n",
1880                hwnd, data->whole_window, data->client_window, wine_dbgstr_rect( &data->window_rect ),
1881                wine_dbgstr_rect( &data->whole_rect ), wine_dbgstr_rect( &data->client_rect ));
1882     }
1883     return data;
1884 }
1885
1886
1887 /***********************************************************************
1888  *              X11DRV_get_whole_window
1889  *
1890  * Return the X window associated with the full area of a window
1891  */
1892 Window X11DRV_get_whole_window( HWND hwnd )
1893 {
1894     struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1895
1896     if (!data)
1897     {
1898         if (hwnd == GetDesktopWindow()) return root_window;
1899         return (Window)GetPropA( hwnd, whole_window_prop );
1900     }
1901     return data->whole_window;
1902 }
1903
1904
1905 /***********************************************************************
1906  *              X11DRV_get_client_window
1907  *
1908  * Return the X window associated with the client area of a window
1909  */
1910 static Window X11DRV_get_client_window( HWND hwnd )
1911 {
1912     struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1913
1914     if (!data)
1915     {
1916         if (hwnd == GetDesktopWindow()) return root_window;
1917         return (Window)GetPropA( hwnd, client_window_prop );
1918     }
1919     return data->client_window;
1920 }
1921
1922
1923 /***********************************************************************
1924  *              X11DRV_get_ic
1925  *
1926  * Return the X input context associated with a window
1927  */
1928 XIC X11DRV_get_ic( HWND hwnd )
1929 {
1930     struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1931     XIM xim;
1932
1933     if (!data) return 0;
1934     if (data->xic) return data->xic;
1935     if (!(xim = x11drv_thread_data()->xim)) return 0;
1936     return X11DRV_CreateIC( xim, data );
1937 }
1938
1939
1940 /***********************************************************************
1941  *              X11DRV_GetDC   (X11DRV.@)
1942  */
1943 void CDECL X11DRV_GetDC( HDC hdc, HWND hwnd, HWND top, const RECT *win_rect,
1944                          const RECT *top_rect, DWORD flags )
1945 {
1946     struct x11drv_escape_set_drawable escape;
1947     struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1948
1949     escape.code        = X11DRV_SET_DRAWABLE;
1950     escape.mode        = IncludeInferiors;
1951     escape.fbconfig_id = 0;
1952     escape.gl_drawable = 0;
1953     escape.pixmap      = 0;
1954     escape.gl_copy     = FALSE;
1955
1956     if (top == hwnd && data && IsIconic( hwnd ) && data->icon_window)
1957     {
1958         escape.drawable = data->icon_window;
1959     }
1960     else if (top == hwnd)
1961     {
1962         escape.fbconfig_id = data ? data->fbconfig_id : (XID)GetPropA( hwnd, fbconfig_id_prop );
1963         /* GL draws to the client area even for window DCs */
1964         escape.gl_drawable = data ? data->client_window : X11DRV_get_client_window( hwnd );
1965         if (flags & DCX_WINDOW)
1966             escape.drawable = data ? data->whole_window : X11DRV_get_whole_window( hwnd );
1967         else
1968             escape.drawable = escape.gl_drawable;
1969     }
1970     else
1971     {
1972         escape.drawable    = X11DRV_get_client_window( top );
1973         escape.fbconfig_id = data ? data->fbconfig_id : (XID)GetPropA( hwnd, fbconfig_id_prop );
1974         escape.gl_drawable = data ? data->gl_drawable : (Drawable)GetPropA( hwnd, gl_drawable_prop );
1975         escape.pixmap      = data ? data->pixmap : (Pixmap)GetPropA( hwnd, pixmap_prop );
1976         escape.gl_copy     = (escape.gl_drawable != 0);
1977         if (flags & DCX_CLIPCHILDREN) escape.mode = ClipByChildren;
1978     }
1979
1980     escape.dc_rect.left         = win_rect->left - top_rect->left;
1981     escape.dc_rect.top          = win_rect->top - top_rect->top;
1982     escape.dc_rect.right        = win_rect->right - top_rect->left;
1983     escape.dc_rect.bottom       = win_rect->bottom - top_rect->top;
1984     escape.drawable_rect.left   = top_rect->left;
1985     escape.drawable_rect.top    = top_rect->top;
1986     escape.drawable_rect.right  = top_rect->right;
1987     escape.drawable_rect.bottom = top_rect->bottom;
1988
1989     ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
1990 }
1991
1992
1993 /***********************************************************************
1994  *              X11DRV_ReleaseDC  (X11DRV.@)
1995  */
1996 void CDECL X11DRV_ReleaseDC( HWND hwnd, HDC hdc )
1997 {
1998     struct x11drv_escape_set_drawable escape;
1999
2000     escape.code = X11DRV_SET_DRAWABLE;
2001     escape.drawable = root_window;
2002     escape.mode = IncludeInferiors;
2003     escape.drawable_rect = virtual_screen_rect;
2004     SetRect( &escape.dc_rect, 0, 0, virtual_screen_rect.right - virtual_screen_rect.left,
2005              virtual_screen_rect.bottom - virtual_screen_rect.top );
2006     OffsetRect( &escape.dc_rect, -escape.drawable_rect.left, -escape.drawable_rect.top );
2007     escape.fbconfig_id = 0;
2008     escape.gl_drawable = 0;
2009     escape.pixmap = 0;
2010     ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
2011 }
2012
2013
2014 /***********************************************************************
2015  *              SetCapture  (X11DRV.@)
2016  */
2017 void CDECL X11DRV_SetCapture( HWND hwnd, UINT flags )
2018 {
2019     struct x11drv_thread_data *thread_data = x11drv_thread_data();
2020
2021     if (!thread_data) return;
2022     if (!(flags & (GUI_INMOVESIZE | GUI_INMENUMODE))) return;
2023
2024     if (hwnd)
2025     {
2026         Window grab_win = X11DRV_get_client_window( GetAncestor( hwnd, GA_ROOT ) );
2027
2028         if (!grab_win) return;
2029         wine_tsx11_lock();
2030         XFlush( gdi_display );
2031         XGrabPointer( thread_data->display, grab_win, False,
2032                       PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
2033                       GrabModeAsync, GrabModeAsync, None, None, CurrentTime );
2034         wine_tsx11_unlock();
2035         thread_data->grab_window = grab_win;
2036     }
2037     else  /* release capture */
2038     {
2039         wine_tsx11_lock();
2040         XFlush( gdi_display );
2041         XUngrabPointer( thread_data->display, CurrentTime );
2042         XFlush( thread_data->display );
2043         wine_tsx11_unlock();
2044         thread_data->grab_window = None;
2045     }
2046 }
2047
2048
2049 /*****************************************************************
2050  *              SetParent   (X11DRV.@)
2051  */
2052 void CDECL X11DRV_SetParent( HWND hwnd, HWND parent, HWND old_parent )
2053 {
2054     Display *display = thread_display();
2055     struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
2056
2057     if (!data) return;
2058     if (parent == old_parent) return;
2059
2060     if (parent != GetDesktopWindow()) /* a child window */
2061     {
2062         if (old_parent == GetDesktopWindow())
2063         {
2064             /* destroy the old X windows */
2065             destroy_whole_window( display, data, FALSE );
2066             destroy_icon_window( display, data );
2067             if (data->managed)
2068             {
2069                 data->managed = FALSE;
2070                 RemovePropA( data->hwnd, managed_prop );
2071             }
2072         }
2073     }
2074     else  /* new top level window */
2075     {
2076         /* FIXME: we ignore errors since we can't really recover anyway */
2077         create_whole_window( display, data );
2078     }
2079 }
2080
2081
2082 /*****************************************************************
2083  *              SetFocus   (X11DRV.@)
2084  *
2085  * Set the X focus.
2086  */
2087 void CDECL X11DRV_SetFocus( HWND hwnd )
2088 {
2089     Display *display = thread_display();
2090     struct x11drv_win_data *data;
2091     XWindowChanges changes;
2092
2093     if (!(hwnd = GetAncestor( hwnd, GA_ROOT ))) return;
2094     if (!(data = X11DRV_get_win_data( hwnd ))) return;
2095     if (data->managed || !data->whole_window) return;
2096
2097     /* Set X focus and install colormap */
2098     wine_tsx11_lock();
2099     changes.stack_mode = Above;
2100     XConfigureWindow( display, data->whole_window, CWStackMode, &changes );
2101     /* we must not use CurrentTime (ICCCM), so try to use last message time instead */
2102     /* FIXME: this is not entirely correct */
2103     XSetInputFocus( display, data->whole_window, RevertToParent,
2104                     /* CurrentTime */
2105                     GetMessageTime() - EVENT_x11_time_to_win32_time(0));
2106     wine_tsx11_unlock();
2107 }
2108
2109
2110 /***********************************************************************
2111  *              WindowPosChanging   (X11DRV.@)
2112  */
2113 void CDECL X11DRV_WindowPosChanging( HWND hwnd, HWND insert_after, UINT swp_flags,
2114                                      const RECT *window_rect, const RECT *client_rect, RECT *visible_rect )
2115 {
2116     struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
2117     DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
2118
2119     if (!data)
2120     {
2121         /* create the win data if the window is being made visible */
2122         if (!(style & WS_VISIBLE) && !(swp_flags & SWP_SHOWWINDOW)) return;
2123         if (!(data = X11DRV_create_win_data( hwnd ))) return;
2124     }
2125
2126     /* check if we need to switch the window to managed */
2127     if (!data->managed && data->whole_window && is_window_managed( hwnd, swp_flags, window_rect ))
2128     {
2129         TRACE( "making win %p/%lx managed\n", hwnd, data->whole_window );
2130         if (data->mapped) unmap_window( thread_display(), data );
2131         data->managed = TRUE;
2132         SetPropA( hwnd, managed_prop, (HANDLE)1 );
2133     }
2134
2135     *visible_rect = *window_rect;
2136     X11DRV_window_to_X_rect( data, visible_rect );
2137 }
2138
2139
2140 /***********************************************************************
2141  *              WindowPosChanged   (X11DRV.@)
2142  */
2143 void CDECL X11DRV_WindowPosChanged( HWND hwnd, HWND insert_after, UINT swp_flags,
2144                                     const RECT *rectWindow, const RECT *rectClient,
2145                                     const RECT *visible_rect, const RECT *valid_rects )
2146 {
2147     struct x11drv_thread_data *thread_data;
2148     Display *display;
2149     struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
2150     DWORD new_style = GetWindowLongW( hwnd, GWL_STYLE );
2151     RECT old_window_rect, old_whole_rect, old_client_rect;
2152     int event_type;
2153
2154     if (!data) return;
2155
2156     thread_data = x11drv_thread_data();
2157     display = thread_data->display;
2158
2159     old_window_rect = data->window_rect;
2160     old_whole_rect  = data->whole_rect;
2161     old_client_rect = data->client_rect;
2162     data->window_rect = *rectWindow;
2163     data->whole_rect  = *visible_rect;
2164     data->client_rect = *rectClient;
2165
2166     TRACE( "win %p window %s client %s style %08x flags %08x\n",
2167            hwnd, wine_dbgstr_rect(rectWindow), wine_dbgstr_rect(rectClient), new_style, swp_flags );
2168
2169     if (!IsRectEmpty( &valid_rects[0] ))
2170     {
2171         int x_offset = old_whole_rect.left - data->whole_rect.left;
2172         int y_offset = old_whole_rect.top - data->whole_rect.top;
2173
2174         /* if all that happened is that the whole window moved, copy everything */
2175         if (!(swp_flags & SWP_FRAMECHANGED) &&
2176             old_whole_rect.right   - data->whole_rect.right   == x_offset &&
2177             old_whole_rect.bottom  - data->whole_rect.bottom  == y_offset &&
2178             old_client_rect.left   - data->client_rect.left   == x_offset &&
2179             old_client_rect.right  - data->client_rect.right  == x_offset &&
2180             old_client_rect.top    - data->client_rect.top    == y_offset &&
2181             old_client_rect.bottom - data->client_rect.bottom == y_offset &&
2182             !memcmp( &valid_rects[0], &data->client_rect, sizeof(RECT) ))
2183         {
2184             /* if we have an X window the bits will be moved by the X server */
2185             if (!data->whole_window)
2186                 move_window_bits( data, &old_whole_rect, &data->whole_rect, &old_client_rect );
2187         }
2188         else
2189             move_window_bits( data, &valid_rects[1], &valid_rects[0], &old_client_rect );
2190     }
2191
2192     wine_tsx11_lock();
2193     XFlush( gdi_display );  /* make sure painting is done before we move the window */
2194     wine_tsx11_unlock();
2195
2196     sync_client_position( display, data, swp_flags, &old_client_rect, &old_whole_rect );
2197
2198     if (!data->whole_window) return;
2199
2200     /* check if we are currently processing an event relevant to this window */
2201     event_type = 0;
2202     if (thread_data->current_event && thread_data->current_event->xany.window == data->whole_window)
2203         event_type = thread_data->current_event->type;
2204
2205     if (event_type != ConfigureNotify && event_type != PropertyNotify)
2206         event_type = 0;  /* ignore other events */
2207
2208     if (data->mapped)
2209     {
2210         if (((swp_flags & SWP_HIDEWINDOW) && !(new_style & WS_VISIBLE)) ||
2211             (!event_type && !is_window_rect_mapped( rectWindow )))
2212             unmap_window( display, data );
2213     }
2214
2215     /* don't change position if we are about to minimize or maximize a managed window */
2216     if (!event_type &&
2217         !(data->managed && (swp_flags & SWP_STATECHANGED) && (new_style & (WS_MINIMIZE|WS_MAXIMIZE))))
2218         sync_window_position( display, data, swp_flags,
2219                               &old_window_rect, &old_whole_rect, &old_client_rect );
2220
2221     if ((new_style & WS_VISIBLE) &&
2222         ((new_style & WS_MINIMIZE) || is_window_rect_mapped( rectWindow )))
2223     {
2224         if (!data->mapped || (swp_flags & (SWP_FRAMECHANGED|SWP_STATECHANGED)))
2225             set_wm_hints( display, data );
2226
2227         if (!data->mapped)
2228         {
2229             map_window( display, data, new_style );
2230         }
2231         else if ((swp_flags & SWP_STATECHANGED) && (!data->iconic != !(new_style & WS_MINIMIZE)))
2232         {
2233             data->iconic = (new_style & WS_MINIMIZE) != 0;
2234             TRACE( "changing win %p iconic state to %u\n", data->hwnd, data->iconic );
2235             wine_tsx11_lock();
2236             if (data->iconic)
2237                 XIconifyWindow( display, data->whole_window, DefaultScreen(display) );
2238             else if (is_window_rect_mapped( rectWindow ))
2239                 XMapWindow( display, data->whole_window );
2240             wine_tsx11_unlock();
2241             update_net_wm_states( display, data );
2242         }
2243         else if (!event_type)
2244         {
2245             update_net_wm_states( display, data );
2246         }
2247     }
2248
2249     wine_tsx11_lock();
2250     XFlush( display );  /* make sure changes are done before we start painting again */
2251     wine_tsx11_unlock();
2252 }
2253
2254
2255 /***********************************************************************
2256  *           ShowWindow   (X11DRV.@)
2257  */
2258 UINT CDECL X11DRV_ShowWindow( HWND hwnd, INT cmd, RECT *rect, UINT swp )
2259 {
2260     int x, y;
2261     unsigned int width, height, border, depth;
2262     Window root, top;
2263     DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
2264     struct x11drv_thread_data *thread_data = x11drv_thread_data();
2265     struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
2266
2267     if (!data || !data->whole_window || !data->managed || !data->mapped || data->iconic) return swp;
2268     if (style & WS_MINIMIZE) return swp;
2269     if (IsRectEmpty( rect )) return swp;
2270
2271     /* only fetch the new rectangle if the ShowWindow was a result of a window manager event */
2272
2273     if (!thread_data->current_event || thread_data->current_event->xany.window != data->whole_window)
2274         return swp;
2275
2276     if (thread_data->current_event->type != ConfigureNotify &&
2277         thread_data->current_event->type != PropertyNotify)
2278         return swp;
2279
2280     TRACE( "win %p/%lx cmd %d at %s flags %08x\n",
2281            hwnd, data->whole_window, cmd, wine_dbgstr_rect(rect), swp );
2282
2283     wine_tsx11_lock();
2284     XGetGeometry( thread_data->display, data->whole_window,
2285                   &root, &x, &y, &width, &height, &border, &depth );
2286     XTranslateCoordinates( thread_data->display, data->whole_window, root, 0, 0, &x, &y, &top );
2287     wine_tsx11_unlock();
2288     rect->left   = x;
2289     rect->top    = y;
2290     rect->right  = x + width;
2291     rect->bottom = y + height;
2292     OffsetRect( rect, virtual_screen_rect.left, virtual_screen_rect.top );
2293     X11DRV_X_to_window_rect( data, rect );
2294     return swp & ~(SWP_NOMOVE | SWP_NOCLIENTMOVE | SWP_NOSIZE | SWP_NOCLIENTSIZE);
2295 }
2296
2297
2298 /**********************************************************************
2299  *              SetWindowIcon (X11DRV.@)
2300  *
2301  * hIcon or hIconSm has changed (or is being initialised for the
2302  * first time). Complete the X11 driver-specific initialisation
2303  * and set the window hints.
2304  *
2305  * This is not entirely correct, may need to create
2306  * an icon window and set the pixmap as a background
2307  */
2308 void CDECL X11DRV_SetWindowIcon( HWND hwnd, UINT type, HICON icon )
2309 {
2310     Display *display = thread_display();
2311     struct x11drv_win_data *data;
2312
2313     if (type != ICON_BIG) return;  /* nothing to do here */
2314
2315     if (!(data = X11DRV_get_win_data( hwnd ))) return;
2316     if (!data->whole_window) return;
2317     if (!data->managed) return;
2318
2319     if (data->wm_hints)
2320     {
2321         set_icon_hints( display, data, icon );
2322         wine_tsx11_lock();
2323         XSetWMHints( display, data->whole_window, data->wm_hints );
2324         wine_tsx11_unlock();
2325     }
2326 }
2327
2328
2329 /***********************************************************************
2330  *              SetWindowRgn  (X11DRV.@)
2331  *
2332  * Assign specified region to window (for non-rectangular windows)
2333  */
2334 int CDECL X11DRV_SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL redraw )
2335 {
2336     struct x11drv_win_data *data;
2337
2338     if ((data = X11DRV_get_win_data( hwnd )))
2339     {
2340         sync_window_region( thread_display(), data, hrgn );
2341     }
2342     else if (X11DRV_get_whole_window( hwnd ))
2343     {
2344         SendMessageW( hwnd, WM_X11DRV_SET_WIN_REGION, 0, 0 );
2345     }
2346     return TRUE;
2347 }
2348
2349
2350 /***********************************************************************
2351  *              SetLayeredWindowAttributes  (X11DRV.@)
2352  *
2353  * Set transparency attributes for a layered window.
2354  */
2355 void CDECL X11DRV_SetLayeredWindowAttributes( HWND hwnd, COLORREF key, BYTE alpha, DWORD flags )
2356 {
2357     struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
2358
2359     if (data)
2360     {
2361         if (data->whole_window)
2362             sync_window_opacity( thread_display(), data->whole_window, key, alpha, flags );
2363     }
2364     else
2365     {
2366         Window win = X11DRV_get_whole_window( hwnd );
2367         if (win) sync_window_opacity( gdi_display, win, key, alpha, flags );
2368     }
2369 }
2370
2371
2372 /**********************************************************************
2373  *           X11DRV_WindowMessage   (X11DRV.@)
2374  */
2375 LRESULT CDECL X11DRV_WindowMessage( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp )
2376 {
2377     struct x11drv_win_data *data;
2378
2379     switch(msg)
2380     {
2381     case WM_X11DRV_ACQUIRE_SELECTION:
2382         return X11DRV_AcquireClipboard( hwnd );
2383     case WM_X11DRV_SET_WIN_FORMAT:
2384         return set_win_format( hwnd, (XID)wp );
2385     case WM_X11DRV_SET_WIN_REGION:
2386         if ((data = X11DRV_get_win_data( hwnd ))) sync_window_region( thread_display(), data, (HRGN)1 );
2387         return 0;
2388     case WM_X11DRV_RESIZE_DESKTOP:
2389         X11DRV_resize_desktop( LOWORD(lp), HIWORD(lp) );
2390         return 0;
2391     default:
2392         FIXME( "got window msg %x hwnd %p wp %lx lp %lx\n", msg, hwnd, wp, lp );
2393         return 0;
2394     }
2395 }
2396
2397
2398 /***********************************************************************
2399  *              is_netwm_supported
2400  */
2401 static BOOL is_netwm_supported( Display *display, Atom atom )
2402 {
2403     static Atom *net_supported;
2404     static int net_supported_count = -1;
2405     int i;
2406
2407     wine_tsx11_lock();
2408     if (net_supported_count == -1)
2409     {
2410         Atom type;
2411         int format;
2412         unsigned long count, remaining;
2413
2414         if (!XGetWindowProperty( display, DefaultRootWindow(display), x11drv_atom(_NET_SUPPORTED), 0,
2415                                  ~0UL, False, XA_ATOM, &type, &format, &count,
2416                                  &remaining, (unsigned char **)&net_supported ))
2417             net_supported_count = get_property_size( format, count ) / sizeof(Atom);
2418         else
2419             net_supported_count = 0;
2420     }
2421     wine_tsx11_unlock();
2422
2423     for (i = 0; i < net_supported_count; i++)
2424         if (net_supported[i] == atom) return TRUE;
2425     return FALSE;
2426 }
2427
2428
2429 /***********************************************************************
2430  *           SysCommand   (X11DRV.@)
2431  *
2432  * Perform WM_SYSCOMMAND handling.
2433  */
2434 LRESULT CDECL X11DRV_SysCommand( HWND hwnd, WPARAM wparam, LPARAM lparam )
2435 {
2436     WPARAM hittest = wparam & 0x0f;
2437     DWORD dwPoint;
2438     int x, y, dir;
2439     XEvent xev;
2440     Display *display = thread_display();
2441     struct x11drv_win_data *data;
2442
2443     if (!(data = X11DRV_get_win_data( hwnd ))) return -1;
2444     if (!data->whole_window || !data->managed || !data->mapped) return -1;
2445
2446     switch (wparam & 0xfff0)
2447     {
2448     case SC_MOVE:
2449         if (!hittest) dir = _NET_WM_MOVERESIZE_MOVE_KEYBOARD;
2450         else dir = _NET_WM_MOVERESIZE_MOVE;
2451         break;
2452     case SC_SIZE:
2453         /* windows without WS_THICKFRAME are not resizable through the window manager */
2454         if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_THICKFRAME)) return -1;
2455
2456         switch (hittest)
2457         {
2458         case WMSZ_LEFT:        dir = _NET_WM_MOVERESIZE_SIZE_LEFT; break;
2459         case WMSZ_RIGHT:       dir = _NET_WM_MOVERESIZE_SIZE_RIGHT; break;
2460         case WMSZ_TOP:         dir = _NET_WM_MOVERESIZE_SIZE_TOP; break;
2461         case WMSZ_TOPLEFT:     dir = _NET_WM_MOVERESIZE_SIZE_TOPLEFT; break;
2462         case WMSZ_TOPRIGHT:    dir = _NET_WM_MOVERESIZE_SIZE_TOPRIGHT; break;
2463         case WMSZ_BOTTOM:      dir = _NET_WM_MOVERESIZE_SIZE_BOTTOM; break;
2464         case WMSZ_BOTTOMLEFT:  dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT; break;
2465         case WMSZ_BOTTOMRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT; break;
2466         default:               dir = _NET_WM_MOVERESIZE_SIZE_KEYBOARD; break;
2467         }
2468         break;
2469
2470     case SC_KEYMENU:
2471         /* prevent a simple ALT press+release from activating the system menu,
2472          * as that can get confusing on managed windows */
2473         if ((WCHAR)lparam) return -1;  /* got an explicit char */
2474         if (GetMenu( hwnd )) return -1;  /* window has a real menu */
2475         if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_SYSMENU)) return -1;  /* no system menu */
2476         TRACE( "ignoring SC_KEYMENU wp %lx lp %lx\n", wparam, lparam );
2477         return 0;
2478
2479     default:
2480         return -1;
2481     }
2482
2483     if (IsZoomed(hwnd)) return -1;
2484
2485     if (!is_netwm_supported( display, x11drv_atom(_NET_WM_MOVERESIZE) ))
2486     {
2487         TRACE( "_NET_WM_MOVERESIZE not supported\n" );
2488         return -1;
2489     }
2490
2491     dwPoint = GetMessagePos();
2492     x = (short)LOWORD(dwPoint);
2493     y = (short)HIWORD(dwPoint);
2494
2495     TRACE("hwnd %p, x %d, y %d, dir %d\n", hwnd, x, y, dir);
2496
2497     xev.xclient.type = ClientMessage;
2498     xev.xclient.window = X11DRV_get_whole_window(hwnd);
2499     xev.xclient.message_type = x11drv_atom(_NET_WM_MOVERESIZE);
2500     xev.xclient.serial = 0;
2501     xev.xclient.display = display;
2502     xev.xclient.send_event = True;
2503     xev.xclient.format = 32;
2504     xev.xclient.data.l[0] = x - virtual_screen_rect.left; /* x coord */
2505     xev.xclient.data.l[1] = y - virtual_screen_rect.top;  /* y coord */
2506     xev.xclient.data.l[2] = dir; /* direction */
2507     xev.xclient.data.l[3] = 1; /* button */
2508     xev.xclient.data.l[4] = 0; /* unused */
2509
2510     /* need to ungrab the pointer that may have been automatically grabbed
2511      * with a ButtonPress event */
2512     wine_tsx11_lock();
2513     XUngrabPointer( display, CurrentTime );
2514     XSendEvent(display, root_window, False, SubstructureNotifyMask, &xev);
2515     wine_tsx11_unlock();
2516     return 0;
2517 }