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