mfplat/tests: Mark some tests as broken on Win 8 and 10 v1507.
[wine.git] / dlls / winex11.drv / window.c
blob3ebbee0856e927afea7f5cfaf6b35e337613ff8f
1 /*
2 * Window related functions
4 * Copyright 1993, 1994, 1995, 1996, 2001 Alexandre Julliard
5 * Copyright 1993 David Metcalfe
6 * Copyright 1995, 1996 Alex Korobka
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.
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.
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
23 #if 0
24 #pragma makedep unix
25 #endif
27 #include "config.h"
29 #include <stdarg.h>
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <unistd.h>
34 #include <X11/Xlib.h>
35 #include <X11/Xresource.h>
36 #include <X11/Xutil.h>
37 #ifdef HAVE_LIBXSHAPE
38 #include <X11/extensions/shape.h>
39 #endif /* HAVE_LIBXSHAPE */
40 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
41 #include <X11/extensions/XInput2.h>
42 #endif
44 /* avoid conflict with field names in included win32 headers */
45 #undef Status
47 #include "ntstatus.h"
48 #define WIN32_NO_STATUS
50 #include "x11drv.h"
51 #include "wingdi.h"
52 #include "winuser.h"
54 #include "wine/debug.h"
55 #include "wine/server.h"
56 #include "mwm.h"
58 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
59 WINE_DECLARE_DEBUG_CHANNEL(systray);
61 #define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0
62 #define _NET_WM_MOVERESIZE_SIZE_TOP 1
63 #define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2
64 #define _NET_WM_MOVERESIZE_SIZE_RIGHT 3
65 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4
66 #define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5
67 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6
68 #define _NET_WM_MOVERESIZE_SIZE_LEFT 7
69 #define _NET_WM_MOVERESIZE_MOVE 8
70 #define _NET_WM_MOVERESIZE_SIZE_KEYBOARD 9
71 #define _NET_WM_MOVERESIZE_MOVE_KEYBOARD 10
73 #define _NET_WM_STATE_REMOVE 0
74 #define _NET_WM_STATE_ADD 1
75 #define _NET_WM_STATE_TOGGLE 2
77 #define SYSTEM_TRAY_REQUEST_DOCK 0
78 #define SYSTEM_TRAY_BEGIN_MESSAGE 1
79 #define SYSTEM_TRAY_CANCEL_MESSAGE 2
81 static const unsigned int net_wm_state_atoms[NB_NET_WM_STATES] =
83 XATOM__NET_WM_STATE_FULLSCREEN,
84 XATOM__NET_WM_STATE_ABOVE,
85 XATOM__NET_WM_STATE_MAXIMIZED_VERT,
86 XATOM__NET_WM_STATE_SKIP_PAGER,
87 XATOM__NET_WM_STATE_SKIP_TASKBAR
90 #define SWP_AGG_NOPOSCHANGE (SWP_NOSIZE | SWP_NOMOVE | SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE | SWP_NOZORDER)
92 /* is cursor clipping active? */
93 BOOL clipping_cursor = FALSE;
95 /* X context to associate a hwnd to an X window */
96 XContext winContext = 0;
98 /* X context to associate a struct x11drv_win_data to an hwnd */
99 static XContext win_data_context = 0;
101 /* time of last user event and window where it's stored */
102 static Time last_user_time;
103 static Window user_time_window;
105 static const WCHAR foreign_window_prop[] =
106 {'_','_','w','i','n','e','_','x','1','1','_','f','o','r','e','i','g','n','_','w','i','n','d','o','w',0};
107 static const WCHAR whole_window_prop[] =
108 {'_','_','w','i','n','e','_','x','1','1','_','w','h','o','l','e','_','w','i','n','d','o','w',0};
109 static const WCHAR clip_window_prop[] =
110 {'_','_','w','i','n','e','_','x','1','1','_','c','l','i','p','_','w','i','n','d','o','w',0};
112 static pthread_mutex_t win_data_mutex = PTHREAD_MUTEX_INITIALIZER;
115 /***********************************************************************
116 * http://standards.freedesktop.org/startup-notification-spec
118 static void remove_startup_notification(Display *display, Window window)
120 static LONG startup_notification_removed = 0;
121 char message[1024];
122 const char *id;
123 int i;
124 int pos;
125 XEvent xevent;
126 const char *src;
127 int srclen;
129 if (InterlockedCompareExchange(&startup_notification_removed, 1, 0) != 0)
130 return;
132 if (!(id = getenv( "DESKTOP_STARTUP_ID" )) || !id[0]) return;
134 if ((src = strstr( id, "_TIME" ))) update_user_time( atol( src + 5 ));
136 pos = snprintf(message, sizeof(message), "remove: ID=");
137 message[pos++] = '"';
138 for (i = 0; id[i] && pos < sizeof(message) - 3; i++)
140 if (id[i] == '"' || id[i] == '\\')
141 message[pos++] = '\\';
142 message[pos++] = id[i];
144 message[pos++] = '"';
145 message[pos++] = '\0';
146 unsetenv( "DESKTOP_STARTUP_ID" );
148 xevent.xclient.type = ClientMessage;
149 xevent.xclient.message_type = x11drv_atom(_NET_STARTUP_INFO_BEGIN);
150 xevent.xclient.display = display;
151 xevent.xclient.window = window;
152 xevent.xclient.format = 8;
154 src = message;
155 srclen = strlen(src) + 1;
157 while (srclen > 0)
159 int msglen = srclen;
160 if (msglen > 20)
161 msglen = 20;
162 memset(&xevent.xclient.data.b[0], 0, 20);
163 memcpy(&xevent.xclient.data.b[0], src, msglen);
164 src += msglen;
165 srclen -= msglen;
167 XSendEvent( display, DefaultRootWindow( display ), False, PropertyChangeMask, &xevent );
168 xevent.xclient.message_type = x11drv_atom(_NET_STARTUP_INFO);
173 static BOOL is_managed( HWND hwnd )
175 struct x11drv_win_data *data = get_win_data( hwnd );
176 BOOL ret = data && data->managed;
177 release_win_data( data );
178 return ret;
181 HWND *build_hwnd_list(void)
183 NTSTATUS status;
184 HWND *list;
185 UINT count = 128;
187 for (;;)
189 if (!(list = malloc( count * sizeof(*list) ))) return NULL;
190 status = NtUserBuildHwndList( 0, 0, 0, 0, 0, count, list, &count );
191 if (!status) return list;
192 free( list );
193 if (status != STATUS_BUFFER_TOO_SMALL) return NULL;
197 static BOOL has_owned_popups( HWND hwnd )
199 HWND *list;
200 UINT i;
201 BOOL ret = FALSE;
203 if (!(list = build_hwnd_list())) return FALSE;
205 for (i = 0; list[i] != HWND_BOTTOM; i++)
207 if (list[i] == hwnd) break; /* popups are always above owner */
208 if (NtUserGetWindowRelative( list[i], GW_OWNER ) != hwnd) continue;
209 if ((ret = is_managed( list[i] ))) break;
212 free( list );
213 return ret;
217 /***********************************************************************
218 * alloc_win_data
220 static struct x11drv_win_data *alloc_win_data( Display *display, HWND hwnd )
222 struct x11drv_win_data *data;
224 if ((data = calloc( 1, sizeof(*data) )))
226 data->display = display;
227 data->vis = default_visual;
228 data->hwnd = hwnd;
229 pthread_mutex_lock( &win_data_mutex );
230 XSaveContext( gdi_display, (XID)hwnd, win_data_context, (char *)data );
232 return data;
236 /***********************************************************************
237 * is_window_managed
239 * Check if a given window should be managed
241 static BOOL is_window_managed( HWND hwnd, UINT swp_flags, const RECT *window_rect )
243 DWORD style, ex_style;
245 if (!managed_mode) return FALSE;
247 /* child windows are not managed */
248 style = NtUserGetWindowLongW( hwnd, GWL_STYLE );
249 if ((style & (WS_CHILD|WS_POPUP)) == WS_CHILD) return FALSE;
250 /* activated windows are managed */
251 if (!(swp_flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW))) return TRUE;
252 if (hwnd == get_active_window()) return TRUE;
253 /* windows with caption are managed */
254 if ((style & WS_CAPTION) == WS_CAPTION) return TRUE;
255 /* windows with thick frame are managed */
256 if (style & WS_THICKFRAME) return TRUE;
257 if (style & WS_POPUP)
259 HMONITOR hmon;
260 MONITORINFO mi;
262 /* popup with sysmenu == caption are managed */
263 if (style & WS_SYSMENU) return TRUE;
264 /* full-screen popup windows are managed */
265 hmon = NtUserMonitorFromWindow( hwnd, MONITOR_DEFAULTTOPRIMARY );
266 mi.cbSize = sizeof( mi );
267 NtUserGetMonitorInfo( hmon, &mi );
268 if (window_rect->left <= mi.rcWork.left && window_rect->right >= mi.rcWork.right &&
269 window_rect->top <= mi.rcWork.top && window_rect->bottom >= mi.rcWork.bottom)
270 return TRUE;
272 /* application windows are managed */
273 ex_style = NtUserGetWindowLongW( hwnd, GWL_EXSTYLE );
274 if (ex_style & WS_EX_APPWINDOW) return TRUE;
275 /* windows that own popups are managed */
276 if (has_owned_popups( hwnd )) return TRUE;
277 /* default: not managed */
278 return FALSE;
282 /***********************************************************************
283 * is_window_resizable
285 * Check if window should be made resizable by the window manager
287 static inline BOOL is_window_resizable( struct x11drv_win_data *data, DWORD style )
289 if (style & WS_THICKFRAME) return TRUE;
290 /* Metacity needs the window to be resizable to make it fullscreen */
291 return NtUserIsWindowRectFullScreen( &data->whole_rect );
294 /***********************************************************************
295 * get_mwm_decorations
297 static unsigned long get_mwm_decorations( struct x11drv_win_data *data,
298 DWORD style, DWORD ex_style,
299 const RECT *window_rect,
300 const RECT *client_rect )
302 unsigned long ret = 0;
304 if (!decorated_mode) return 0;
306 if (EqualRect( window_rect, client_rect )) return 0;
307 if (IsRectEmpty( window_rect )) return 0;
308 if (data->shaped) return 0;
310 if (ex_style & WS_EX_TOOLWINDOW) return 0;
311 if (ex_style & WS_EX_LAYERED) return 0;
313 if ((style & WS_CAPTION) == WS_CAPTION)
315 ret |= MWM_DECOR_TITLE | MWM_DECOR_BORDER;
316 if (style & WS_SYSMENU) ret |= MWM_DECOR_MENU;
317 if (style & WS_MINIMIZEBOX) ret |= MWM_DECOR_MINIMIZE;
318 if (style & WS_MAXIMIZEBOX) ret |= MWM_DECOR_MAXIMIZE;
320 if (ex_style & WS_EX_DLGMODALFRAME) ret |= MWM_DECOR_BORDER;
321 else if (style & WS_THICKFRAME) ret |= MWM_DECOR_BORDER | MWM_DECOR_RESIZEH;
322 else if ((style & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME) ret |= MWM_DECOR_BORDER;
323 return ret;
327 /***********************************************************************
328 * get_window_attributes
330 * Fill the window attributes structure for an X window.
332 static int get_window_attributes( struct x11drv_win_data *data, XSetWindowAttributes *attr )
334 attr->override_redirect = !data->managed;
335 attr->colormap = data->whole_colormap ? data->whole_colormap : default_colormap;
336 attr->save_under = ((NtUserGetClassLongW( data->hwnd, GCL_STYLE ) & CS_SAVEBITS) != 0);
337 attr->bit_gravity = NorthWestGravity;
338 attr->backing_store = NotUseful;
339 attr->border_pixel = 0;
340 attr->event_mask = (ExposureMask | PointerMotionMask |
341 ButtonPressMask | ButtonReleaseMask | EnterWindowMask |
342 KeyPressMask | KeyReleaseMask | FocusChangeMask |
343 KeymapStateMask | StructureNotifyMask);
344 if (data->managed) attr->event_mask |= PropertyChangeMask;
346 return (CWOverrideRedirect | CWSaveUnder | CWColormap | CWBorderPixel |
347 CWEventMask | CWBitGravity | CWBackingStore);
351 /***********************************************************************
352 * sync_window_style
354 * Change the X window attributes when the window style has changed.
356 static void sync_window_style( struct x11drv_win_data *data )
358 if (data->whole_window != root_window)
360 XSetWindowAttributes attr;
361 int mask = get_window_attributes( data, &attr );
363 XChangeWindowAttributes( data->display, data->whole_window, mask, &attr );
368 /***********************************************************************
369 * sync_window_region
371 * Update the X11 window region.
373 static void sync_window_region( struct x11drv_win_data *data, HRGN win_region )
375 #ifdef HAVE_LIBXSHAPE
376 HRGN hrgn = win_region;
378 if (!data->whole_window) return;
379 data->shaped = FALSE;
381 if (IsRectEmpty( &data->window_rect )) /* set an empty shape */
383 static XRectangle empty_rect;
384 XShapeCombineRectangles( data->display, data->whole_window, ShapeBounding, 0, 0,
385 &empty_rect, 1, ShapeSet, YXBanded );
386 return;
389 if (hrgn == (HRGN)1) /* hack: win_region == 1 means retrieve region from server */
391 if (!(hrgn = NtGdiCreateRectRgn( 0, 0, 0, 0 ))) return;
392 if (NtUserGetWindowRgnEx( data->hwnd, hrgn, 0 ) == ERROR)
394 NtGdiDeleteObjectApp( hrgn );
395 hrgn = 0;
399 if (!hrgn)
401 XShapeCombineMask( data->display, data->whole_window, ShapeBounding, 0, 0, None, ShapeSet );
403 else
405 RGNDATA *pRegionData;
407 if (NtUserGetWindowLongW( data->hwnd, GWL_EXSTYLE ) & WS_EX_LAYOUTRTL)
408 NtUserMirrorRgn( data->hwnd, hrgn );
409 if ((pRegionData = X11DRV_GetRegionData( hrgn, 0 )))
411 XShapeCombineRectangles( data->display, data->whole_window, ShapeBounding,
412 data->window_rect.left - data->whole_rect.left,
413 data->window_rect.top - data->whole_rect.top,
414 (XRectangle *)pRegionData->Buffer,
415 pRegionData->rdh.nCount, ShapeSet, YXBanded );
416 free( pRegionData );
417 data->shaped = TRUE;
420 if (hrgn && hrgn != win_region) NtGdiDeleteObjectApp( hrgn );
421 #endif /* HAVE_LIBXSHAPE */
425 /***********************************************************************
426 * sync_window_opacity
428 static void sync_window_opacity( Display *display, Window win,
429 COLORREF key, BYTE alpha, DWORD flags )
431 unsigned long opacity = 0xffffffff;
433 if (flags & LWA_ALPHA) opacity = (0xffffffff / 0xff) * alpha;
435 if (opacity == 0xffffffff)
436 XDeleteProperty( display, win, x11drv_atom(_NET_WM_WINDOW_OPACITY) );
437 else
438 XChangeProperty( display, win, x11drv_atom(_NET_WM_WINDOW_OPACITY),
439 XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&opacity, 1 );
443 /***********************************************************************
444 * sync_window_text
446 static void sync_window_text( Display *display, Window win, const WCHAR *text )
448 DWORD count, len;
449 char *buffer, *utf8_buffer;
450 XTextProperty prop;
452 /* allocate new buffer for window text */
453 len = lstrlenW( text );
454 count = len * 3 + 1;
455 if (!(buffer = malloc( count ))) return;
456 ntdll_wcstoumbs( text, len + 1, buffer, count, FALSE );
458 RtlUnicodeToUTF8N( NULL, 0, &count, text, len * sizeof(WCHAR) );
459 if (!(utf8_buffer = malloc( count )))
461 free( buffer );
462 return;
464 RtlUnicodeToUTF8N( utf8_buffer, count, &count, text, len * sizeof(WCHAR) );
466 if (XmbTextListToTextProperty( display, &buffer, 1, XStdICCTextStyle, &prop ) == Success)
468 XSetWMName( display, win, &prop );
469 XSetWMIconName( display, win, &prop );
470 XFree( prop.value );
473 Implements a NET_WM UTF-8 title. It should be without a trailing \0,
474 according to the standard
475 ( http://www.pps.jussieu.fr/~jch/software/UTF8_STRING/UTF8_STRING.text ).
477 XChangeProperty( display, win, x11drv_atom(_NET_WM_NAME), x11drv_atom(UTF8_STRING),
478 8, PropModeReplace, (unsigned char *) utf8_buffer, count);
480 free( utf8_buffer );
481 free( buffer );
485 /***********************************************************************
486 * get_bitmap_argb
488 * Return the bitmap bits in ARGB format. Helper for setting icon hints.
490 static unsigned long *get_bitmap_argb( HDC hdc, HBITMAP color, HBITMAP mask, unsigned int *size )
492 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
493 BITMAPINFO *info = (BITMAPINFO *)buffer;
494 BITMAP bm;
495 unsigned int *ptr, *bits = NULL;
496 unsigned char *mask_bits = NULL;
497 int i, j;
498 BOOL has_alpha = FALSE;
500 if (!NtGdiExtGetObjectW( color, sizeof(bm), &bm )) return NULL;
501 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
502 info->bmiHeader.biWidth = bm.bmWidth;
503 info->bmiHeader.biHeight = -bm.bmHeight;
504 info->bmiHeader.biPlanes = 1;
505 info->bmiHeader.biBitCount = 32;
506 info->bmiHeader.biCompression = BI_RGB;
507 info->bmiHeader.biSizeImage = bm.bmWidth * bm.bmHeight * 4;
508 info->bmiHeader.biXPelsPerMeter = 0;
509 info->bmiHeader.biYPelsPerMeter = 0;
510 info->bmiHeader.biClrUsed = 0;
511 info->bmiHeader.biClrImportant = 0;
512 *size = bm.bmWidth * bm.bmHeight + 2;
513 if (!(bits = malloc( *size * sizeof(long) ))) goto failed;
514 if (!NtGdiGetDIBitsInternal( hdc, color, 0, bm.bmHeight, bits + 2, info, DIB_RGB_COLORS, 0, 0 ))
515 goto failed;
517 bits[0] = bm.bmWidth;
518 bits[1] = bm.bmHeight;
520 for (i = 0; i < bm.bmWidth * bm.bmHeight; i++)
521 if ((has_alpha = (bits[i + 2] & 0xff000000) != 0)) break;
523 if (!has_alpha)
525 unsigned int width_bytes = (bm.bmWidth + 31) / 32 * 4;
526 /* generate alpha channel from the mask */
527 info->bmiHeader.biBitCount = 1;
528 info->bmiHeader.biSizeImage = width_bytes * bm.bmHeight;
529 if (!(mask_bits = malloc( info->bmiHeader.biSizeImage ))) goto failed;
530 if (!NtGdiGetDIBitsInternal( hdc, mask, 0, bm.bmHeight, mask_bits, info, DIB_RGB_COLORS, 0, 0 ))
531 goto failed;
532 ptr = bits + 2;
533 for (i = 0; i < bm.bmHeight; i++)
534 for (j = 0; j < bm.bmWidth; j++, ptr++)
535 if (!((mask_bits[i * width_bytes + j / 8] << (j % 8)) & 0x80)) *ptr |= 0xff000000;
536 free( mask_bits );
539 /* convert to array of longs */
540 if (bits && sizeof(long) > sizeof(int))
541 for (i = *size - 1; i >= 0; i--) ((unsigned long *)bits)[i] = bits[i];
543 return (unsigned long *)bits;
545 failed:
546 free( bits );
547 free( mask_bits );
548 return NULL;
552 /***********************************************************************
553 * create_icon_pixmaps
555 static BOOL create_icon_pixmaps( HDC hdc, const ICONINFO *icon, Pixmap *icon_ret, Pixmap *mask_ret )
557 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
558 BITMAPINFO *info = (BITMAPINFO *)buffer;
559 XVisualInfo vis = default_visual;
560 struct gdi_image_bits bits;
561 Pixmap color_pixmap = 0, mask_pixmap = 0;
562 int lines;
563 unsigned int i;
565 bits.ptr = NULL;
566 bits.free = NULL;
567 bits.is_copy = TRUE;
569 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
570 info->bmiHeader.biBitCount = 0;
571 if (!(lines = NtGdiGetDIBitsInternal( hdc, icon->hbmColor, 0, 0, NULL, info, DIB_RGB_COLORS, 0, 0 )))
572 goto failed;
573 if (!(bits.ptr = malloc( info->bmiHeader.biSizeImage ))) goto failed;
574 if (!NtGdiGetDIBitsInternal( hdc, icon->hbmColor, 0, lines, bits.ptr, info, DIB_RGB_COLORS, 0, 0 ))
575 goto failed;
577 color_pixmap = create_pixmap_from_image( hdc, &vis, info, &bits, DIB_RGB_COLORS );
578 free( bits.ptr );
579 bits.ptr = NULL;
580 if (!color_pixmap) goto failed;
582 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
583 info->bmiHeader.biBitCount = 0;
584 if (!(lines = NtGdiGetDIBitsInternal( hdc, icon->hbmMask, 0, 0, NULL, info, DIB_RGB_COLORS, 0, 0 )))
585 goto failed;
586 if (!(bits.ptr = malloc( info->bmiHeader.biSizeImage ))) goto failed;
587 if (!NtGdiGetDIBitsInternal( hdc, icon->hbmMask, 0, lines, bits.ptr, info, DIB_RGB_COLORS, 0, 0 ))
588 goto failed;
590 /* invert the mask */
591 for (i = 0; i < info->bmiHeader.biSizeImage / sizeof(DWORD); i++) ((DWORD *)bits.ptr)[i] ^= ~0u;
593 vis.depth = 1;
594 mask_pixmap = create_pixmap_from_image( hdc, &vis, info, &bits, DIB_RGB_COLORS );
595 free( bits.ptr );
596 bits.ptr = NULL;
597 if (!mask_pixmap) goto failed;
599 *icon_ret = color_pixmap;
600 *mask_ret = mask_pixmap;
601 return TRUE;
603 failed:
604 if (color_pixmap) XFreePixmap( gdi_display, color_pixmap );
605 free( bits.ptr );
606 return FALSE;
610 static HICON get_icon_info( HICON icon, ICONINFO *ii )
612 return icon && NtUserGetIconInfo( icon, ii, NULL, NULL, NULL, 0 ) ? icon : NULL;
615 /***********************************************************************
616 * fetch_icon_data
618 static void fetch_icon_data( HWND hwnd, HICON icon_big, HICON icon_small )
620 struct x11drv_win_data *data;
621 ICONINFO ii, ii_small;
622 HDC hDC;
623 unsigned int size;
624 unsigned long *bits;
625 Pixmap icon_pixmap, mask_pixmap;
627 if (!icon_big)
629 icon_big = get_icon_info( (HICON)send_message( hwnd, WM_GETICON, ICON_BIG, 0 ), &ii );
630 if (!icon_big)
631 icon_big = get_icon_info( (HICON)NtUserGetClassLongPtrW( hwnd, GCLP_HICON ), &ii );
632 if (!icon_big)
634 icon_big = LoadImageW( 0, (const WCHAR *)IDI_WINLOGO, IMAGE_ICON, 0, 0,
635 LR_SHARED | LR_DEFAULTSIZE );
636 icon_big = get_icon_info( icon_big, &ii );
639 if (!icon_small)
641 icon_small = get_icon_info( (HICON)send_message( hwnd, WM_GETICON, ICON_SMALL, 0 ), &ii_small );
642 if (!icon_small)
643 icon_small = get_icon_info( (HICON)NtUserGetClassLongPtrW( hwnd, GCLP_HICONSM ), &ii_small );
646 if (!icon_big) return;
648 hDC = NtGdiCreateCompatibleDC(0);
649 bits = get_bitmap_argb( hDC, ii.hbmColor, ii.hbmMask, &size );
650 if (bits && icon_small)
652 unsigned int size_small;
653 unsigned long *bits_small, *new;
655 if ((bits_small = get_bitmap_argb( hDC, ii_small.hbmColor, ii_small.hbmMask, &size_small )) &&
656 (bits_small[0] != bits[0] || bits_small[1] != bits[1])) /* size must be different */
658 if ((new = realloc( bits, (size + size_small) * sizeof(unsigned long) )))
660 bits = new;
661 memcpy( bits + size, bits_small, size_small * sizeof(unsigned long) );
662 size += size_small;
665 free( bits_small );
666 NtGdiDeleteObjectApp( ii_small.hbmColor );
667 NtGdiDeleteObjectApp( ii_small.hbmMask );
670 if (!create_icon_pixmaps( hDC, &ii, &icon_pixmap, &mask_pixmap )) icon_pixmap = mask_pixmap = 0;
672 NtGdiDeleteObjectApp( ii.hbmColor );
673 NtGdiDeleteObjectApp( ii.hbmMask );
674 NtGdiDeleteObjectApp( hDC );
676 if ((data = get_win_data( hwnd )))
678 if (data->icon_pixmap) XFreePixmap( gdi_display, data->icon_pixmap );
679 if (data->icon_mask) XFreePixmap( gdi_display, data->icon_mask );
680 free( data->icon_bits );
681 data->icon_pixmap = icon_pixmap;
682 data->icon_mask = mask_pixmap;
683 data->icon_bits = bits;
684 data->icon_size = size;
685 release_win_data( data );
687 else
689 if (icon_pixmap) XFreePixmap( gdi_display, icon_pixmap );
690 if (mask_pixmap) XFreePixmap( gdi_display, mask_pixmap );
691 free( bits );
696 /***********************************************************************
697 * set_size_hints
699 * set the window size hints
701 static void set_size_hints( struct x11drv_win_data *data, DWORD style )
703 XSizeHints* size_hints;
705 if (!(size_hints = XAllocSizeHints())) return;
707 size_hints->win_gravity = StaticGravity;
708 size_hints->flags |= PWinGravity;
710 /* don't update size hints if window is not in normal state */
711 if (!(style & (WS_MINIMIZE | WS_MAXIMIZE)))
713 if (data->hwnd != NtUserGetDesktopWindow()) /* don't force position of desktop */
715 size_hints->x = data->whole_rect.left;
716 size_hints->y = data->whole_rect.top;
717 size_hints->flags |= PPosition;
719 else size_hints->win_gravity = NorthWestGravity;
721 if (!is_window_resizable( data, style ))
723 size_hints->max_width = data->whole_rect.right - data->whole_rect.left;
724 size_hints->max_height = data->whole_rect.bottom - data->whole_rect.top;
725 if (size_hints->max_width <= 0 ||size_hints->max_height <= 0)
726 size_hints->max_width = size_hints->max_height = 1;
727 size_hints->min_width = size_hints->max_width;
728 size_hints->min_height = size_hints->max_height;
729 size_hints->flags |= PMinSize | PMaxSize;
732 XSetWMNormalHints( data->display, data->whole_window, size_hints );
733 XFree( size_hints );
737 /***********************************************************************
738 * set_mwm_hints
740 static void set_mwm_hints( struct x11drv_win_data *data, DWORD style, DWORD ex_style )
742 MwmHints mwm_hints;
744 if (data->hwnd == NtUserGetDesktopWindow())
746 if (is_desktop_fullscreen()) mwm_hints.decorations = 0;
747 else mwm_hints.decorations = MWM_DECOR_TITLE | MWM_DECOR_BORDER | MWM_DECOR_MENU | MWM_DECOR_MINIMIZE;
748 mwm_hints.functions = MWM_FUNC_MOVE | MWM_FUNC_MINIMIZE | MWM_FUNC_CLOSE;
750 else
752 mwm_hints.decorations = get_mwm_decorations( data, style, ex_style, &data->window_rect, &data->client_rect );
753 mwm_hints.functions = MWM_FUNC_MOVE;
754 if (is_window_resizable( data, style )) mwm_hints.functions |= MWM_FUNC_RESIZE;
755 if (!(style & WS_DISABLED))
757 mwm_hints.functions |= MWM_FUNC_CLOSE;
758 if (style & WS_MINIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MINIMIZE;
759 if (style & WS_MAXIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MAXIMIZE;
761 /* The window can be programmatically minimized even without
762 a minimize box button. Allow the WM to restore it. */
763 if (style & WS_MINIMIZE) mwm_hints.functions |= MWM_FUNC_MINIMIZE | MWM_FUNC_MAXIMIZE;
767 TRACE( "%p setting mwm hints to %lx,%lx (style %x exstyle %x)\n",
768 data->hwnd, mwm_hints.decorations, mwm_hints.functions, style, ex_style );
770 mwm_hints.flags = MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
771 mwm_hints.input_mode = 0;
772 mwm_hints.status = 0;
773 XChangeProperty( data->display, data->whole_window, x11drv_atom(_MOTIF_WM_HINTS),
774 x11drv_atom(_MOTIF_WM_HINTS), 32, PropModeReplace,
775 (unsigned char*)&mwm_hints, sizeof(mwm_hints)/sizeof(long) );
779 /***********************************************************************
780 * set_style_hints
782 static void set_style_hints( struct x11drv_win_data *data, DWORD style, DWORD ex_style )
784 Window group_leader = data->whole_window;
785 HWND owner = NtUserGetWindowRelative( data->hwnd, GW_OWNER );
786 Window owner_win = 0;
787 XWMHints *wm_hints;
788 Atom window_type;
790 if (owner)
792 owner = NtUserGetAncestor( owner, GA_ROOT );
793 owner_win = X11DRV_get_whole_window( owner );
796 if (owner_win)
798 XSetTransientForHint( data->display, data->whole_window, owner_win );
799 group_leader = owner_win;
802 /* Only use dialog type for owned popups. Metacity allows making fullscreen
803 * only normal windows, and doesn't handle correctly TRANSIENT_FOR hint for
804 * dialogs owned by fullscreen windows.
806 if (((style & WS_POPUP) || (ex_style & WS_EX_DLGMODALFRAME)) && owner)
807 window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG);
808 else
809 window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
811 XChangeProperty(data->display, data->whole_window, x11drv_atom(_NET_WM_WINDOW_TYPE),
812 XA_ATOM, 32, PropModeReplace, (unsigned char*)&window_type, 1);
814 if ((wm_hints = XAllocWMHints()))
816 wm_hints->flags = InputHint | StateHint | WindowGroupHint;
817 wm_hints->input = !use_take_focus && !(style & WS_DISABLED);
818 wm_hints->initial_state = (style & WS_MINIMIZE) ? IconicState : NormalState;
819 wm_hints->window_group = group_leader;
820 if (data->icon_pixmap)
822 wm_hints->icon_pixmap = data->icon_pixmap;
823 wm_hints->icon_mask = data->icon_mask;
824 wm_hints->flags |= IconPixmapHint | IconMaskHint;
826 XSetWMHints( data->display, data->whole_window, wm_hints );
827 XFree( wm_hints );
830 if (data->icon_bits)
831 XChangeProperty( data->display, data->whole_window, x11drv_atom(_NET_WM_ICON),
832 XA_CARDINAL, 32, PropModeReplace,
833 (unsigned char *)data->icon_bits, data->icon_size );
834 else
835 XDeleteProperty( data->display, data->whole_window, x11drv_atom(_NET_WM_ICON) );
840 /***********************************************************************
841 * set_initial_wm_hints
843 * Set the window manager hints that don't change over the lifetime of a window.
845 static void set_initial_wm_hints( Display *display, Window window )
847 long i;
848 Atom protocols[3];
849 Atom dndVersion = WINE_XDND_VERSION;
850 XClassHint *class_hints;
852 /* wm protocols */
853 i = 0;
854 protocols[i++] = x11drv_atom(WM_DELETE_WINDOW);
855 protocols[i++] = x11drv_atom(_NET_WM_PING);
856 if (use_take_focus) protocols[i++] = x11drv_atom(WM_TAKE_FOCUS);
857 XChangeProperty( display, window, x11drv_atom(WM_PROTOCOLS),
858 XA_ATOM, 32, PropModeReplace, (unsigned char *)protocols, i );
860 /* class hints */
861 if ((class_hints = XAllocClassHint()))
863 class_hints->res_name = process_name;
864 class_hints->res_class = process_name;
865 XSetClassHint( display, window, class_hints );
866 XFree( class_hints );
869 /* set the WM_CLIENT_MACHINE and WM_LOCALE_NAME properties */
870 XSetWMProperties(display, window, NULL, NULL, NULL, 0, NULL, NULL, NULL);
871 /* set the pid. together, these properties are needed so the window manager can kill us if we freeze */
872 i = getpid();
873 XChangeProperty(display, window, x11drv_atom(_NET_WM_PID),
874 XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&i, 1);
876 XChangeProperty( display, window, x11drv_atom(XdndAware),
877 XA_ATOM, 32, PropModeReplace, (unsigned char*)&dndVersion, 1 );
879 update_user_time( 0 ); /* make sure that the user time window exists */
880 if (user_time_window)
881 XChangeProperty( display, window, x11drv_atom(_NET_WM_USER_TIME_WINDOW),
882 XA_WINDOW, 32, PropModeReplace, (unsigned char *)&user_time_window, 1 );
886 /***********************************************************************
887 * make_owner_managed
889 * If the window is managed, make sure its owner window is too.
891 static void make_owner_managed( HWND hwnd )
893 HWND owner;
895 if (!(owner = NtUserGetWindowRelative( hwnd, GW_OWNER ))) return;
896 if (is_managed( owner )) return;
897 if (!is_managed( hwnd )) return;
899 NtUserSetWindowPos( owner, 0, 0, 0, 0, 0,
900 SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOSIZE | SWP_NOMOVE |
901 SWP_NOREDRAW | SWP_DEFERERASE | SWP_NOSENDCHANGING | SWP_STATECHANGED );
905 /***********************************************************************
906 * set_wm_hints
908 * Set all the window manager hints for a window.
910 static void set_wm_hints( struct x11drv_win_data *data )
912 DWORD style, ex_style;
914 if (data->hwnd == NtUserGetDesktopWindow())
916 /* force some styles for the desktop to get the correct decorations */
917 style = WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
918 ex_style = WS_EX_APPWINDOW;
920 else
922 style = NtUserGetWindowLongW( data->hwnd, GWL_STYLE );
923 ex_style = NtUserGetWindowLongW( data->hwnd, GWL_EXSTYLE );
926 set_size_hints( data, style );
927 set_mwm_hints( data, style, ex_style );
928 set_style_hints( data, style, ex_style );
932 /***********************************************************************
933 * init_clip_window
935 Window init_clip_window(void)
937 struct x11drv_thread_data *data = x11drv_init_thread_data();
939 if (!data->clip_window &&
940 (data->clip_window = (Window)NtUserGetProp( NtUserGetDesktopWindow(), clip_window_prop )))
942 XSelectInput( data->display, data->clip_window, StructureNotifyMask );
944 return data->clip_window;
948 /***********************************************************************
949 * update_user_time
951 void update_user_time( Time time )
953 if (!user_time_window)
955 Window win = XCreateWindow( gdi_display, root_window, -1, -1, 1, 1, 0, CopyFromParent,
956 InputOnly, CopyFromParent, 0, NULL );
957 if (InterlockedCompareExchangePointer( (void **)&user_time_window, (void *)win, 0 ))
958 XDestroyWindow( gdi_display, win );
959 TRACE( "user time window %lx\n", user_time_window );
962 if (!time) return;
963 XLockDisplay( gdi_display );
964 if (!last_user_time || (long)(time - last_user_time) > 0)
966 last_user_time = time;
967 XChangeProperty( gdi_display, user_time_window, x11drv_atom(_NET_WM_USER_TIME),
968 XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&time, 1 );
970 XUnlockDisplay( gdi_display );
973 /***********************************************************************
974 * update_net_wm_states
976 void update_net_wm_states( struct x11drv_win_data *data )
978 DWORD i, style, ex_style, new_state = 0;
980 if (!data->managed) return;
981 if (data->whole_window == root_window) return;
983 style = NtUserGetWindowLongW( data->hwnd, GWL_STYLE );
984 if (style & WS_MINIMIZE)
985 new_state |= data->net_wm_state & ((1 << NET_WM_STATE_FULLSCREEN)|(1 << NET_WM_STATE_MAXIMIZED));
986 if (NtUserIsWindowRectFullScreen( &data->whole_rect ))
988 if ((style & WS_MAXIMIZE) && (style & WS_CAPTION) == WS_CAPTION)
989 new_state |= (1 << NET_WM_STATE_MAXIMIZED);
990 else if (!(style & WS_MINIMIZE))
991 new_state |= (1 << NET_WM_STATE_FULLSCREEN);
993 else if (style & WS_MAXIMIZE)
994 new_state |= (1 << NET_WM_STATE_MAXIMIZED);
996 ex_style = NtUserGetWindowLongW( data->hwnd, GWL_EXSTYLE );
997 if (ex_style & WS_EX_TOPMOST)
998 new_state |= (1 << NET_WM_STATE_ABOVE);
999 if (!data->add_taskbar)
1001 if (data->skip_taskbar || (ex_style & (WS_EX_TOOLWINDOW | WS_EX_NOACTIVATE)))
1002 new_state |= (1 << NET_WM_STATE_SKIP_TASKBAR) | (1 << NET_WM_STATE_SKIP_PAGER);
1003 else if (!(ex_style & WS_EX_APPWINDOW) && NtUserGetWindowRelative( data->hwnd, GW_OWNER ))
1004 new_state |= (1 << NET_WM_STATE_SKIP_TASKBAR);
1007 if (!data->mapped) /* set the _NET_WM_STATE atom directly */
1009 Atom atoms[NB_NET_WM_STATES+1];
1010 DWORD count;
1012 for (i = count = 0; i < NB_NET_WM_STATES; i++)
1014 if (!(new_state & (1 << i))) continue;
1015 TRACE( "setting wm state %u for unmapped window %p/%lx\n",
1016 i, data->hwnd, data->whole_window );
1017 atoms[count++] = X11DRV_Atoms[net_wm_state_atoms[i] - FIRST_XATOM];
1018 if (net_wm_state_atoms[i] == XATOM__NET_WM_STATE_MAXIMIZED_VERT)
1019 atoms[count++] = x11drv_atom(_NET_WM_STATE_MAXIMIZED_HORZ);
1021 XChangeProperty( data->display, data->whole_window, x11drv_atom(_NET_WM_STATE), XA_ATOM,
1022 32, PropModeReplace, (unsigned char *)atoms, count );
1024 else /* ask the window manager to do it for us */
1026 XEvent xev;
1028 xev.xclient.type = ClientMessage;
1029 xev.xclient.window = data->whole_window;
1030 xev.xclient.message_type = x11drv_atom(_NET_WM_STATE);
1031 xev.xclient.serial = 0;
1032 xev.xclient.display = data->display;
1033 xev.xclient.send_event = True;
1034 xev.xclient.format = 32;
1035 xev.xclient.data.l[3] = 1;
1036 xev.xclient.data.l[4] = 0;
1038 for (i = 0; i < NB_NET_WM_STATES; i++)
1040 TRACE( "setting wm state %u for window %p/%lx to %u prev %u\n",
1041 i, data->hwnd, data->whole_window,
1042 (new_state & (1 << i)) != 0, (data->net_wm_state & (1 << i)) != 0 );
1044 xev.xclient.data.l[0] = (new_state & (1 << i)) ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
1045 xev.xclient.data.l[1] = X11DRV_Atoms[net_wm_state_atoms[i] - FIRST_XATOM];
1046 xev.xclient.data.l[2] = ((net_wm_state_atoms[i] == XATOM__NET_WM_STATE_MAXIMIZED_VERT) ?
1047 x11drv_atom(_NET_WM_STATE_MAXIMIZED_HORZ) : 0);
1048 XSendEvent( data->display, root_window, False,
1049 SubstructureRedirectMask | SubstructureNotifyMask, &xev );
1052 data->net_wm_state = new_state;
1055 /***********************************************************************
1056 * read_net_wm_states
1058 void read_net_wm_states( Display* display, struct x11drv_win_data *data )
1060 Atom type, *state;
1061 int format;
1062 unsigned long i, j, count, remaining;
1063 DWORD new_state = 0;
1064 BOOL maximized_horz = FALSE;
1066 if (!data->whole_window) return;
1068 if (!XGetWindowProperty( display, data->whole_window, x11drv_atom(_NET_WM_STATE), 0,
1069 65536/sizeof(CARD32), False, XA_ATOM, &type, &format, &count,
1070 &remaining, (unsigned char **)&state ))
1072 if (type == XA_ATOM && format == 32)
1074 for (i = 0; i < count; i++)
1076 if (state[i] == x11drv_atom(_NET_WM_STATE_MAXIMIZED_HORZ))
1077 maximized_horz = TRUE;
1078 for (j=0; j < NB_NET_WM_STATES; j++)
1080 if (state[i] == X11DRV_Atoms[net_wm_state_atoms[j] - FIRST_XATOM])
1082 new_state |= 1 << j;
1087 XFree( state );
1090 if (!maximized_horz)
1091 new_state &= ~(1 << NET_WM_STATE_MAXIMIZED);
1093 data->net_wm_state = new_state;
1097 /***********************************************************************
1098 * set_xembed_flags
1100 static void set_xembed_flags( struct x11drv_win_data *data, unsigned long flags )
1102 unsigned long info[2];
1104 if (!data->whole_window) return;
1106 info[0] = 0; /* protocol version */
1107 info[1] = flags;
1108 XChangeProperty( data->display, data->whole_window, x11drv_atom(_XEMBED_INFO),
1109 x11drv_atom(_XEMBED_INFO), 32, PropModeReplace, (unsigned char*)info, 2 );
1113 /***********************************************************************
1114 * map_window
1116 static void map_window( HWND hwnd, DWORD new_style )
1118 struct x11drv_win_data *data;
1120 make_owner_managed( hwnd );
1121 wait_for_withdrawn_state( hwnd, TRUE );
1123 if (!(data = get_win_data( hwnd ))) return;
1125 if (data->whole_window && !data->mapped)
1127 TRACE( "win %p/%lx\n", data->hwnd, data->whole_window );
1129 remove_startup_notification( data->display, data->whole_window );
1130 set_wm_hints( data );
1132 if (!data->embedded)
1134 update_net_wm_states( data );
1135 sync_window_style( data );
1136 XMapWindow( data->display, data->whole_window );
1137 XFlush( data->display );
1138 if (data->surface && data->vis.visualid != default_visual.visualid)
1139 data->surface->funcs->flush( data->surface );
1141 else set_xembed_flags( data, XEMBED_MAPPED );
1143 data->mapped = TRUE;
1144 data->iconic = (new_style & WS_MINIMIZE) != 0;
1146 release_win_data( data );
1150 /***********************************************************************
1151 * unmap_window
1153 static void unmap_window( HWND hwnd )
1155 struct x11drv_win_data *data;
1157 wait_for_withdrawn_state( hwnd, FALSE );
1159 if (!(data = get_win_data( hwnd ))) return;
1161 if (data->mapped)
1163 TRACE( "win %p/%lx\n", data->hwnd, data->whole_window );
1165 if (data->embedded) set_xembed_flags( data, 0 );
1166 else if (!data->managed) XUnmapWindow( data->display, data->whole_window );
1167 else XWithdrawWindow( data->display, data->whole_window, data->vis.screen );
1169 data->mapped = FALSE;
1170 data->net_wm_state = 0;
1172 release_win_data( data );
1176 /***********************************************************************
1177 * make_window_embedded
1179 void make_window_embedded( struct x11drv_win_data *data )
1181 /* the window cannot be mapped before being embedded */
1182 if (data->mapped)
1184 if (!data->managed) XUnmapWindow( data->display, data->whole_window );
1185 else XWithdrawWindow( data->display, data->whole_window, data->vis.screen );
1186 data->net_wm_state = 0;
1188 data->embedded = TRUE;
1189 data->managed = TRUE;
1190 sync_window_style( data );
1191 set_xembed_flags( data, (data->mapped || data->embedder) ? XEMBED_MAPPED : 0 );
1195 /***********************************************************************
1196 * get_decoration_rect
1198 static void get_decoration_rect( struct x11drv_win_data *data, RECT *rect,
1199 const RECT *window_rect, const RECT *client_rect )
1201 DWORD style, ex_style, style_mask = 0, ex_style_mask = 0;
1202 unsigned long decor;
1204 SetRectEmpty( rect );
1205 if (!data->managed) return;
1207 style = NtUserGetWindowLongW( data->hwnd, GWL_STYLE );
1208 ex_style = NtUserGetWindowLongW( data->hwnd, GWL_EXSTYLE );
1209 decor = get_mwm_decorations( data, style, ex_style, window_rect, client_rect );
1211 if (decor & MWM_DECOR_TITLE) style_mask |= WS_CAPTION;
1212 if (decor & MWM_DECOR_BORDER)
1214 style_mask |= WS_DLGFRAME | WS_THICKFRAME;
1215 ex_style_mask |= WS_EX_DLGMODALFRAME;
1218 AdjustWindowRectEx( rect, style & style_mask, FALSE, ex_style & ex_style_mask );
1222 /***********************************************************************
1223 * X11DRV_window_to_X_rect
1225 * Convert a rect from client to X window coordinates
1227 static void X11DRV_window_to_X_rect( struct x11drv_win_data *data, RECT *rect,
1228 const RECT *window_rect, const RECT *client_rect )
1230 RECT rc;
1232 if (IsRectEmpty( rect )) return;
1234 get_decoration_rect( data, &rc, window_rect, client_rect );
1235 rect->left -= rc.left;
1236 rect->right -= rc.right;
1237 rect->top -= rc.top;
1238 rect->bottom -= rc.bottom;
1239 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
1240 if (rect->left >= rect->right) rect->right = rect->left + 1;
1244 /***********************************************************************
1245 * X11DRV_X_to_window_rect
1247 * Opposite of X11DRV_window_to_X_rect
1249 void X11DRV_X_to_window_rect( struct x11drv_win_data *data, RECT *rect, int x, int y, int cx, int cy )
1251 RECT rc;
1253 get_decoration_rect( data, &rc, &data->window_rect, &data->client_rect );
1255 x += min( data->window_rect.left - data->whole_rect.left, rc.left );
1256 y += min( data->window_rect.top - data->whole_rect.top, rc.top );
1257 cx += max( (data->window_rect.right - data->window_rect.left) -
1258 (data->whole_rect.right - data->whole_rect.left), rc.right - rc.left );
1259 cy += max( (data->window_rect.bottom - data->window_rect.top) -
1260 (data->whole_rect.bottom - data->whole_rect.top), rc.bottom - rc.top );
1261 SetRect( rect, x, y, x + cx, y + cy );
1265 /***********************************************************************
1266 * sync_window_position
1268 * Synchronize the X window position with the Windows one
1270 static void sync_window_position( struct x11drv_win_data *data,
1271 UINT swp_flags, const RECT *old_window_rect,
1272 const RECT *old_whole_rect, const RECT *old_client_rect )
1274 DWORD style = NtUserGetWindowLongW( data->hwnd, GWL_STYLE );
1275 DWORD ex_style = NtUserGetWindowLongW( data->hwnd, GWL_EXSTYLE );
1276 XWindowChanges changes;
1277 unsigned int mask = 0;
1279 if (data->managed && data->iconic) return;
1281 /* resizing a managed maximized window is not allowed */
1282 if (!(style & WS_MAXIMIZE) || !data->managed)
1284 changes.width = data->whole_rect.right - data->whole_rect.left;
1285 changes.height = data->whole_rect.bottom - data->whole_rect.top;
1286 /* if window rect is empty force size to 1x1 */
1287 if (changes.width <= 0 || changes.height <= 0) changes.width = changes.height = 1;
1288 if (changes.width > 65535) changes.width = 65535;
1289 if (changes.height > 65535) changes.height = 65535;
1290 mask |= CWWidth | CWHeight;
1293 /* only the size is allowed to change for the desktop window */
1294 if (data->whole_window != root_window)
1296 POINT pt = virtual_screen_to_root( data->whole_rect.left, data->whole_rect.top );
1297 changes.x = pt.x;
1298 changes.y = pt.y;
1299 mask |= CWX | CWY;
1302 if (!(swp_flags & SWP_NOZORDER) || (swp_flags & SWP_SHOWWINDOW))
1304 /* find window that this one must be after */
1305 HWND prev = NtUserGetWindowRelative( data->hwnd, GW_HWNDPREV );
1306 while (prev && !(NtUserGetWindowLongW( prev, GWL_STYLE ) & WS_VISIBLE))
1307 prev = NtUserGetWindowRelative( prev, GW_HWNDPREV );
1308 if (!prev) /* top child */
1310 changes.stack_mode = Above;
1311 mask |= CWStackMode;
1313 /* should use stack_mode Below but most window managers don't get it right */
1314 /* and Above with a sibling doesn't work so well either, so we ignore it */
1317 set_size_hints( data, style );
1318 set_mwm_hints( data, style, ex_style );
1319 update_net_wm_states( data );
1320 data->configure_serial = NextRequest( data->display );
1321 XReconfigureWMWindow( data->display, data->whole_window, data->vis.screen, mask, &changes );
1322 #ifdef HAVE_LIBXSHAPE
1323 if (IsRectEmpty( old_window_rect ) != IsRectEmpty( &data->window_rect ))
1324 sync_window_region( data, (HRGN)1 );
1325 if (data->shaped)
1327 int old_x_offset = old_window_rect->left - old_whole_rect->left;
1328 int old_y_offset = old_window_rect->top - old_whole_rect->top;
1329 int new_x_offset = data->window_rect.left - data->whole_rect.left;
1330 int new_y_offset = data->window_rect.top - data->whole_rect.top;
1331 if (old_x_offset != new_x_offset || old_y_offset != new_y_offset)
1332 XShapeOffsetShape( data->display, data->whole_window, ShapeBounding,
1333 new_x_offset - old_x_offset, new_y_offset - old_y_offset );
1335 #endif
1337 TRACE( "win %p/%lx pos %d,%d,%dx%d after %lx changes=%x serial=%lu\n",
1338 data->hwnd, data->whole_window, data->whole_rect.left, data->whole_rect.top,
1339 data->whole_rect.right - data->whole_rect.left,
1340 data->whole_rect.bottom - data->whole_rect.top,
1341 changes.sibling, mask, data->configure_serial );
1345 /***********************************************************************
1346 * sync_client_position
1348 * Synchronize the X client window position with the Windows one
1350 static void sync_client_position( struct x11drv_win_data *data,
1351 const RECT *old_client_rect, const RECT *old_whole_rect )
1353 int mask = 0;
1354 XWindowChanges changes;
1356 if (!data->client_window) return;
1358 changes.x = data->client_rect.left - data->whole_rect.left;
1359 changes.y = data->client_rect.top - data->whole_rect.top;
1360 changes.width = min( max( 1, data->client_rect.right - data->client_rect.left ), 65535 );
1361 changes.height = min( max( 1, data->client_rect.bottom - data->client_rect.top ), 65535 );
1363 if (changes.x != old_client_rect->left - old_whole_rect->left) mask |= CWX;
1364 if (changes.y != old_client_rect->top - old_whole_rect->top) mask |= CWY;
1365 if (changes.width != old_client_rect->right - old_client_rect->left) mask |= CWWidth;
1366 if (changes.height != old_client_rect->bottom - old_client_rect->top) mask |= CWHeight;
1368 if (mask)
1370 TRACE( "setting client win %lx pos %d,%d,%dx%d changes=%x\n",
1371 data->client_window, changes.x, changes.y, changes.width, changes.height, mask );
1372 XConfigureWindow( data->display, data->client_window, mask, &changes );
1377 /***********************************************************************
1378 * move_window_bits
1380 * Move the window bits when a window is moved.
1382 static void move_window_bits( HWND hwnd, Window window, const RECT *old_rect, const RECT *new_rect,
1383 const RECT *old_client_rect, const RECT *new_client_rect,
1384 const RECT *new_window_rect )
1386 RECT src_rect = *old_rect;
1387 RECT dst_rect = *new_rect;
1388 HDC hdc_src, hdc_dst;
1389 INT code;
1390 HRGN rgn;
1391 HWND parent = 0;
1393 if (!window)
1395 OffsetRect( &dst_rect, -new_window_rect->left, -new_window_rect->top );
1396 parent = NtUserGetAncestor( hwnd, GA_PARENT );
1397 hdc_src = NtUserGetDCEx( parent, 0, DCX_CACHE );
1398 hdc_dst = NtUserGetDCEx( hwnd, 0, DCX_CACHE | DCX_WINDOW );
1400 else
1402 OffsetRect( &dst_rect, -new_client_rect->left, -new_client_rect->top );
1403 /* make src rect relative to the old position of the window */
1404 OffsetRect( &src_rect, -old_client_rect->left, -old_client_rect->top );
1405 if (dst_rect.left == src_rect.left && dst_rect.top == src_rect.top) return;
1406 hdc_src = hdc_dst = NtUserGetDCEx( hwnd, 0, DCX_CACHE );
1409 rgn = NtGdiCreateRectRgn( dst_rect.left, dst_rect.top, dst_rect.right, dst_rect.bottom );
1410 NtGdiExtSelectClipRgn( hdc_dst, rgn, RGN_COPY );
1411 NtGdiDeleteObjectApp( rgn );
1412 /* WS_CLIPCHILDREN doesn't exclude children from the window update
1413 * region, and ExcludeUpdateRgn call may inappropriately clip valid
1414 * child window contents from the copied parent window bits, but we
1415 * still want to avoid copying invalid window bits when possible.
1417 if (!(NtUserGetWindowLongW( hwnd, GWL_STYLE ) & WS_CLIPCHILDREN ))
1418 NtUserExcludeUpdateRgn( hdc_dst, hwnd );
1420 code = X11DRV_START_EXPOSURES;
1421 NtGdiExtEscape( hdc_dst, NULL, 0, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, 0, NULL );
1423 TRACE( "copying bits for win %p/%lx %s -> %s\n",
1424 hwnd, window, wine_dbgstr_rect(&src_rect), wine_dbgstr_rect(&dst_rect) );
1425 NtGdiBitBlt( hdc_dst, dst_rect.left, dst_rect.top,
1426 dst_rect.right - dst_rect.left, dst_rect.bottom - dst_rect.top,
1427 hdc_src, src_rect.left, src_rect.top, SRCCOPY, 0, 0 );
1429 rgn = 0;
1430 code = X11DRV_END_EXPOSURES;
1431 NtGdiExtEscape( hdc_dst, NULL, 0, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, sizeof(rgn), (LPSTR)&rgn );
1433 NtUserReleaseDC( hwnd, hdc_dst );
1434 if (hdc_src != hdc_dst) NtUserReleaseDC( parent, hdc_src );
1436 if (rgn)
1438 if (!window)
1440 /* map region to client rect since we are using DCX_WINDOW */
1441 NtGdiOffsetRgn( rgn, new_window_rect->left - new_client_rect->left,
1442 new_window_rect->top - new_client_rect->top );
1443 NtUserRedrawWindow( hwnd, NULL, rgn,
1444 RDW_INVALIDATE | RDW_FRAME | RDW_ERASE | RDW_ALLCHILDREN );
1446 else NtUserRedrawWindow( hwnd, NULL, rgn, RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN );
1447 NtGdiDeleteObjectApp( rgn );
1452 /***********************************************************************
1453 * get_dummy_parent
1455 * Create a dummy parent window for child windows that don't have a true X11 parent.
1457 Window get_dummy_parent(void)
1459 static Window dummy_parent;
1461 if (!dummy_parent)
1463 XSetWindowAttributes attrib;
1465 attrib.override_redirect = True;
1466 attrib.border_pixel = 0;
1467 attrib.colormap = default_colormap;
1468 dummy_parent = XCreateWindow( gdi_display, root_window, -1, -1, 1, 1, 0, default_visual.depth,
1469 InputOutput, default_visual.visual,
1470 CWColormap | CWBorderPixel | CWOverrideRedirect, &attrib );
1471 XMapWindow( gdi_display, dummy_parent );
1473 return dummy_parent;
1477 /**********************************************************************
1478 * create_dummy_client_window
1480 Window create_dummy_client_window(void)
1482 XSetWindowAttributes attr;
1484 attr.colormap = default_colormap;
1485 attr.bit_gravity = NorthWestGravity;
1486 attr.win_gravity = NorthWestGravity;
1487 attr.backing_store = NotUseful;
1488 attr.border_pixel = 0;
1490 return XCreateWindow( gdi_display, get_dummy_parent(), 0, 0, 1, 1, 0,
1491 default_visual.depth, InputOutput, default_visual.visual,
1492 CWBitGravity | CWWinGravity | CWBackingStore | CWColormap | CWBorderPixel, &attr );
1495 /**********************************************************************
1496 * create_client_window
1498 Window create_client_window( HWND hwnd, const XVisualInfo *visual )
1500 Window dummy_parent = get_dummy_parent();
1501 struct x11drv_win_data *data = get_win_data( hwnd );
1502 XSetWindowAttributes attr;
1503 Window ret;
1504 int x, y, cx, cy;
1506 if (!data)
1508 /* explicitly create data for HWND_MESSAGE windows since they can be used for OpenGL */
1509 HWND parent = NtUserGetAncestor( hwnd, GA_PARENT );
1510 if (parent == NtUserGetDesktopWindow() || NtUserGetAncestor( parent, GA_PARENT )) return 0;
1511 if (!(data = alloc_win_data( thread_init_display(), hwnd ))) return 0;
1512 NtUserGetClientRect( hwnd, &data->client_rect );
1513 data->window_rect = data->whole_rect = data->client_rect;
1516 if (data->client_window)
1518 XDeleteContext( data->display, data->client_window, winContext );
1519 XReparentWindow( gdi_display, data->client_window, dummy_parent, 0, 0 );
1520 TRACE( "%p reparent xwin %lx/%lx\n", data->hwnd, data->whole_window, data->client_window );
1523 if (data->client_colormap) XFreeColormap( gdi_display, data->client_colormap );
1524 data->client_colormap = XCreateColormap( gdi_display, dummy_parent, visual->visual,
1525 (visual->class == PseudoColor ||
1526 visual->class == GrayScale ||
1527 visual->class == DirectColor) ? AllocAll : AllocNone );
1528 attr.colormap = data->client_colormap;
1529 attr.bit_gravity = NorthWestGravity;
1530 attr.win_gravity = NorthWestGravity;
1531 attr.backing_store = NotUseful;
1532 attr.border_pixel = 0;
1534 x = data->client_rect.left - data->whole_rect.left;
1535 y = data->client_rect.top - data->whole_rect.top;
1536 cx = min( max( 1, data->client_rect.right - data->client_rect.left ), 65535 );
1537 cy = min( max( 1, data->client_rect.bottom - data->client_rect.top ), 65535 );
1539 ret = data->client_window = XCreateWindow( gdi_display,
1540 data->whole_window ? data->whole_window : dummy_parent,
1541 x, y, cx, cy, 0, default_visual.depth, InputOutput,
1542 visual->visual, CWBitGravity | CWWinGravity |
1543 CWBackingStore | CWColormap | CWBorderPixel, &attr );
1544 if (data->client_window)
1546 XSaveContext( data->display, data->client_window, winContext, (char *)data->hwnd );
1547 XMapWindow( gdi_display, data->client_window );
1548 XSync( gdi_display, False );
1549 if (data->whole_window) XSelectInput( data->display, data->client_window, ExposureMask );
1550 TRACE( "%p xwin %lx/%lx\n", data->hwnd, data->whole_window, data->client_window );
1552 release_win_data( data );
1553 return ret;
1557 /**********************************************************************
1558 * create_whole_window
1560 * Create the whole X window for a given window
1562 static void create_whole_window( struct x11drv_win_data *data )
1564 int cx, cy, mask;
1565 XSetWindowAttributes attr;
1566 WCHAR text[1024];
1567 COLORREF key;
1568 BYTE alpha;
1569 DWORD layered_flags;
1570 HRGN win_rgn;
1571 POINT pos;
1573 if (!data->managed && is_window_managed( data->hwnd, SWP_NOACTIVATE, &data->window_rect ))
1575 TRACE( "making win %p/%lx managed\n", data->hwnd, data->whole_window );
1576 data->managed = TRUE;
1579 if ((win_rgn = NtGdiCreateRectRgn( 0, 0, 0, 0 )) &&
1580 NtUserGetWindowRgnEx( data->hwnd, win_rgn, 0 ) == ERROR)
1582 NtGdiDeleteObjectApp( win_rgn );
1583 win_rgn = 0;
1585 data->shaped = (win_rgn != 0);
1587 if (data->vis.visualid != default_visual.visualid)
1588 data->whole_colormap = XCreateColormap( data->display, root_window, data->vis.visual, AllocNone );
1590 mask = get_window_attributes( data, &attr );
1592 if (!(cx = data->whole_rect.right - data->whole_rect.left)) cx = 1;
1593 else if (cx > 65535) cx = 65535;
1594 if (!(cy = data->whole_rect.bottom - data->whole_rect.top)) cy = 1;
1595 else if (cy > 65535) cy = 65535;
1597 pos = virtual_screen_to_root( data->whole_rect.left, data->whole_rect.top );
1598 data->whole_window = XCreateWindow( data->display, root_window, pos.x, pos.y,
1599 cx, cy, 0, data->vis.depth, InputOutput,
1600 data->vis.visual, mask, &attr );
1601 if (!data->whole_window) goto done;
1603 set_initial_wm_hints( data->display, data->whole_window );
1604 set_wm_hints( data );
1606 XSaveContext( data->display, data->whole_window, winContext, (char *)data->hwnd );
1607 NtUserSetProp( data->hwnd, whole_window_prop, (HANDLE)data->whole_window );
1609 /* set the window text */
1610 if (!NtUserInternalGetWindowText( data->hwnd, text, ARRAY_SIZE( text ))) text[0] = 0;
1611 sync_window_text( data->display, data->whole_window, text );
1613 /* set the window region */
1614 if (win_rgn || IsRectEmpty( &data->window_rect )) sync_window_region( data, win_rgn );
1616 /* set the window opacity */
1617 if (!NtUserGetLayeredWindowAttributes( data->hwnd, &key, &alpha, &layered_flags )) layered_flags = 0;
1618 sync_window_opacity( data->display, data->whole_window, key, alpha, layered_flags );
1620 XFlush( data->display ); /* make sure the window exists before we start painting to it */
1622 sync_window_cursor( data->whole_window );
1624 done:
1625 if (win_rgn) NtGdiDeleteObjectApp( win_rgn );
1629 /**********************************************************************
1630 * destroy_whole_window
1632 * Destroy the whole X window for a given window.
1634 static void destroy_whole_window( struct x11drv_win_data *data, BOOL already_destroyed )
1636 TRACE( "win %p xwin %lx/%lx\n", data->hwnd, data->whole_window, data->client_window );
1638 if (data->client_window) XDeleteContext( data->display, data->client_window, winContext );
1640 if (!data->whole_window)
1642 if (data->embedded)
1644 Window xwin = (Window)NtUserGetProp( data->hwnd, foreign_window_prop );
1645 if (xwin)
1647 if (!already_destroyed) XSelectInput( data->display, xwin, 0 );
1648 XDeleteContext( data->display, xwin, winContext );
1649 NtUserRemoveProp( data->hwnd, foreign_window_prop );
1651 return;
1654 else
1656 if (data->client_window && !already_destroyed)
1658 XSelectInput( data->display, data->client_window, 0 );
1659 XReparentWindow( data->display, data->client_window, get_dummy_parent(), 0, 0 );
1660 XSync( data->display, False );
1662 XDeleteContext( data->display, data->whole_window, winContext );
1663 if (!already_destroyed) XDestroyWindow( data->display, data->whole_window );
1665 if (data->whole_colormap) XFreeColormap( data->display, data->whole_colormap );
1666 data->whole_window = data->client_window = 0;
1667 data->whole_colormap = 0;
1668 data->wm_state = WithdrawnState;
1669 data->net_wm_state = 0;
1670 data->mapped = FALSE;
1671 if (data->xic)
1673 XUnsetICFocus( data->xic );
1674 XDestroyIC( data->xic );
1675 data->xic = 0;
1677 /* Outlook stops processing messages after destroying a dialog, so we need an explicit flush */
1678 XFlush( data->display );
1679 if (data->surface) window_surface_release( data->surface );
1680 data->surface = NULL;
1681 NtUserRemoveProp( data->hwnd, whole_window_prop );
1685 /**********************************************************************
1686 * set_window_visual
1688 * Change the visual by destroying and recreating the X window if needed.
1690 void set_window_visual( struct x11drv_win_data *data, const XVisualInfo *vis, BOOL use_alpha )
1692 Window client_window = data->client_window;
1693 Window whole_window = data->whole_window;
1695 if (!data->use_alpha == !use_alpha) return;
1696 if (data->surface) window_surface_release( data->surface );
1697 data->surface = NULL;
1698 data->use_alpha = use_alpha;
1700 if (data->vis.visualid == vis->visualid) return;
1701 data->client_window = 0;
1702 destroy_whole_window( data, client_window != 0 /* don't destroy whole_window until reparented */ );
1703 data->vis = *vis;
1704 create_whole_window( data );
1705 if (!client_window) return;
1706 /* move the client to the new parent */
1707 XReparentWindow( data->display, client_window, data->whole_window,
1708 data->client_rect.left - data->whole_rect.left,
1709 data->client_rect.top - data->whole_rect.top );
1710 data->client_window = client_window;
1711 XDestroyWindow( data->display, whole_window );
1715 /*****************************************************************
1716 * SetWindowText (X11DRV.@)
1718 void X11DRV_SetWindowText( HWND hwnd, LPCWSTR text )
1720 Window win;
1722 if ((win = X11DRV_get_whole_window( hwnd )) && win != DefaultRootWindow(gdi_display))
1724 Display *display = thread_init_display();
1725 sync_window_text( display, win, text );
1730 /***********************************************************************
1731 * SetWindowStyle (X11DRV.@)
1733 * Update the X state of a window to reflect a style change
1735 void X11DRV_SetWindowStyle( HWND hwnd, INT offset, STYLESTRUCT *style )
1737 struct x11drv_win_data *data;
1738 DWORD changed = style->styleNew ^ style->styleOld;
1740 if (hwnd == NtUserGetDesktopWindow()) return;
1741 if (!(data = get_win_data( hwnd ))) return;
1742 if (!data->whole_window) goto done;
1744 if (offset == GWL_STYLE && (changed & WS_DISABLED)) set_wm_hints( data );
1746 if (offset == GWL_EXSTYLE && (changed & WS_EX_LAYERED)) /* changing WS_EX_LAYERED resets attributes */
1748 data->layered = FALSE;
1749 set_window_visual( data, &default_visual, FALSE );
1750 sync_window_opacity( data->display, data->whole_window, 0, 0, 0 );
1751 if (data->surface) set_surface_color_key( data->surface, CLR_INVALID );
1753 done:
1754 release_win_data( data );
1758 /***********************************************************************
1759 * DestroyWindow (X11DRV.@)
1761 void X11DRV_DestroyWindow( HWND hwnd )
1763 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1764 struct x11drv_win_data *data;
1766 if (!(data = get_win_data( hwnd ))) return;
1768 destroy_whole_window( data, FALSE );
1769 if (thread_data->last_focus == hwnd) thread_data->last_focus = 0;
1770 if (thread_data->last_xic_hwnd == hwnd) thread_data->last_xic_hwnd = 0;
1771 if (data->icon_pixmap) XFreePixmap( gdi_display, data->icon_pixmap );
1772 if (data->icon_mask) XFreePixmap( gdi_display, data->icon_mask );
1773 if (data->client_colormap) XFreeColormap( data->display, data->client_colormap );
1774 free( data->icon_bits );
1775 XDeleteContext( gdi_display, (XID)hwnd, win_data_context );
1776 release_win_data( data );
1777 free( data );
1778 destroy_gl_drawable( hwnd );
1779 wine_vk_surface_destroy( hwnd );
1783 /***********************************************************************
1784 * X11DRV_DestroyNotify
1786 BOOL X11DRV_DestroyNotify( HWND hwnd, XEvent *event )
1788 struct x11drv_win_data *data;
1789 BOOL embedded;
1791 if (!(data = get_win_data( hwnd ))) return FALSE;
1792 embedded = data->embedded;
1793 if (!embedded) FIXME( "window %p/%lx destroyed from the outside\n", hwnd, data->whole_window );
1795 destroy_whole_window( data, TRUE );
1796 release_win_data( data );
1797 if (embedded) send_message( hwnd, WM_CLOSE, 0, 0 );
1798 return TRUE;
1802 /* initialize the desktop window id in the desktop manager process */
1803 BOOL create_desktop_win_data( Window win )
1805 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1806 Display *display = thread_data->display;
1807 struct x11drv_win_data *data;
1809 if (!(data = alloc_win_data( display, NtUserGetDesktopWindow() ))) return FALSE;
1810 data->whole_window = win;
1811 data->managed = TRUE;
1812 NtUserSetProp( data->hwnd, whole_window_prop, (HANDLE)win );
1813 set_initial_wm_hints( display, win );
1814 release_win_data( data );
1815 if (thread_data->clip_window) XReparentWindow( display, thread_data->clip_window, win, 0, 0 );
1816 return TRUE;
1819 /**********************************************************************
1820 * CreateDesktopWindow (X11DRV.@)
1822 BOOL X11DRV_CreateDesktopWindow( HWND hwnd )
1824 unsigned int width, height;
1826 /* retrieve the real size of the desktop */
1827 SERVER_START_REQ( get_window_rectangles )
1829 req->handle = wine_server_user_handle( hwnd );
1830 req->relative = COORDS_CLIENT;
1831 wine_server_call( req );
1832 width = reply->window.right;
1833 height = reply->window.bottom;
1835 SERVER_END_REQ;
1837 if (!width && !height) /* not initialized yet */
1839 RECT rect = NtUserGetVirtualScreenRect();
1841 SERVER_START_REQ( set_window_pos )
1843 req->handle = wine_server_user_handle( hwnd );
1844 req->previous = 0;
1845 req->swp_flags = SWP_NOZORDER;
1846 req->window.left = rect.left;
1847 req->window.top = rect.top;
1848 req->window.right = rect.right;
1849 req->window.bottom = rect.bottom;
1850 req->client = req->window;
1851 wine_server_call( req );
1853 SERVER_END_REQ;
1855 else
1857 Window win = (Window)NtUserGetProp( hwnd, whole_window_prop );
1858 if (win && win != root_window) X11DRV_init_desktop( win, width, height );
1860 return TRUE;
1864 #define WM_WINE_NOTIFY_ACTIVITY WM_USER
1865 #define WM_WINE_DELETE_TAB (WM_USER + 1)
1866 #define WM_WINE_ADD_TAB (WM_USER + 2)
1868 /**********************************************************************
1869 * DesktopWindowProc (X11DRV.@)
1871 LRESULT X11DRV_DesktopWindowProc( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp )
1873 switch (msg)
1875 case WM_WINE_NOTIFY_ACTIVITY:
1877 static ULONG last = 0;
1878 ULONG now = NtGetTickCount();
1879 /* calling XResetScreenSaver too often can cause performance
1880 * problems, so throttle it */
1881 if (now > last + 5000)
1883 XResetScreenSaver( gdi_display );
1884 XFlush( gdi_display );
1885 last = now;
1887 break;
1889 case WM_WINE_DELETE_TAB:
1890 send_notify_message( (HWND)wp, WM_X11DRV_DELETE_TAB, 0, 0 );
1891 break;
1892 case WM_WINE_ADD_TAB:
1893 send_notify_message( (HWND)wp, WM_X11DRV_ADD_TAB, 0, 0 );
1894 break;
1896 return NtUserMessageCall( hwnd, msg, wp, lp, 0, NtUserDefWindowProc, FALSE );
1899 /**********************************************************************
1900 * CreateWindow (X11DRV.@)
1902 BOOL X11DRV_CreateWindow( HWND hwnd )
1904 if (hwnd == NtUserGetDesktopWindow())
1906 struct x11drv_thread_data *data = x11drv_init_thread_data();
1907 XSetWindowAttributes attr;
1909 /* create the cursor clipping window */
1910 attr.override_redirect = TRUE;
1911 attr.event_mask = StructureNotifyMask | FocusChangeMask;
1912 data->clip_window = XCreateWindow( data->display, root_window, 0, 0, 1, 1, 0, 0,
1913 InputOnly, default_visual.visual,
1914 CWOverrideRedirect | CWEventMask, &attr );
1915 XFlush( data->display );
1916 NtUserSetProp( hwnd, clip_window_prop, (HANDLE)data->clip_window );
1917 X11DRV_DisplayDevices_RegisterEventHandlers();
1919 return TRUE;
1923 /***********************************************************************
1924 * get_win_data
1926 * Lock and return the X11 data structure associated with a window.
1928 struct x11drv_win_data *get_win_data( HWND hwnd )
1930 char *data;
1932 if (!hwnd) return NULL;
1933 pthread_mutex_lock( &win_data_mutex );
1934 if (!XFindContext( gdi_display, (XID)hwnd, win_data_context, &data ))
1935 return (struct x11drv_win_data *)data;
1936 pthread_mutex_unlock( &win_data_mutex );
1937 return NULL;
1941 /***********************************************************************
1942 * release_win_data
1944 * Release the data returned by get_win_data.
1946 void release_win_data( struct x11drv_win_data *data )
1948 if (data) pthread_mutex_unlock( &win_data_mutex );
1952 /***********************************************************************
1953 * X11DRV_create_win_data
1955 * Create an X11 data window structure for an existing window.
1957 static struct x11drv_win_data *X11DRV_create_win_data( HWND hwnd, const RECT *window_rect,
1958 const RECT *client_rect )
1960 Display *display;
1961 struct x11drv_win_data *data;
1962 HWND parent;
1964 if (!(parent = NtUserGetAncestor( hwnd, GA_PARENT ))) return NULL; /* desktop */
1966 /* don't create win data for HWND_MESSAGE windows */
1967 if (parent != NtUserGetDesktopWindow() && !NtUserGetAncestor( parent, GA_PARENT )) return NULL;
1969 if (NtUserGetWindowThread( hwnd, NULL ) != GetCurrentThreadId()) return NULL;
1971 /* Recreate the parent gl_drawable now that we know there are child windows
1972 * that will need clipping support.
1974 sync_gl_drawable( parent, TRUE );
1976 display = thread_init_display();
1977 init_clip_window(); /* make sure the clip window is initialized in this thread */
1979 if (!(data = alloc_win_data( display, hwnd ))) return NULL;
1981 data->whole_rect = data->window_rect = *window_rect;
1982 data->client_rect = *client_rect;
1983 if (parent == NtUserGetDesktopWindow())
1985 create_whole_window( data );
1986 TRACE( "win %p/%lx window %s whole %s client %s\n",
1987 hwnd, data->whole_window, wine_dbgstr_rect( &data->window_rect ),
1988 wine_dbgstr_rect( &data->whole_rect ), wine_dbgstr_rect( &data->client_rect ));
1990 return data;
1994 /***********************************************************************
1995 * create_foreign_window
1997 * Create a foreign window for the specified X window and its ancestors
1999 HWND create_foreign_window( Display *display, Window xwin )
2001 static const WCHAR classW[] = {'_','_','w','i','n','e','_','x','1','1','_','f','o','r','e','i','g','n','_','w','i','n','d','o','w',0};
2002 static BOOL class_registered;
2003 struct x11drv_win_data *data;
2004 HWND hwnd, parent;
2005 POINT pos;
2006 Window xparent, xroot;
2007 Window *xchildren;
2008 unsigned int nchildren;
2009 XWindowAttributes attr;
2010 DWORD style = WS_CLIPCHILDREN;
2011 UNICODE_STRING class_name;
2013 if (!class_registered)
2015 UNICODE_STRING version = { 0 };
2016 WNDCLASSEXW class;
2018 memset( &class, 0, sizeof(class) );
2019 class.cbSize = sizeof(class);
2020 class.lpfnWndProc = client_foreign_window_proc;
2021 class.lpszClassName = classW;
2022 RtlInitUnicodeString( &class_name, classW );
2023 if (!NtUserRegisterClassExWOW( &class, &class_name, &version, NULL, 0, 0, NULL ) &&
2024 GetLastError() != ERROR_CLASS_ALREADY_EXISTS)
2026 ERR( "Could not register foreign window class\n" );
2027 return FALSE;
2029 class_registered = TRUE;
2032 if (XFindContext( display, xwin, winContext, (char **)&hwnd )) hwnd = 0;
2033 if (hwnd) return hwnd; /* already created */
2035 XSelectInput( display, xwin, StructureNotifyMask );
2036 if (!XGetWindowAttributes( display, xwin, &attr ) ||
2037 !XQueryTree( display, xwin, &xroot, &xparent, &xchildren, &nchildren ))
2039 XSelectInput( display, xwin, 0 );
2040 return 0;
2042 XFree( xchildren );
2044 if (xparent == xroot)
2046 parent = NtUserGetDesktopWindow();
2047 style |= WS_POPUP;
2048 pos = root_to_virtual_screen( attr.x, attr.y );
2050 else
2052 parent = create_foreign_window( display, xparent );
2053 style |= WS_CHILD;
2054 pos.x = attr.x;
2055 pos.y = attr.y;
2058 RtlInitUnicodeString( &class_name, classW );
2059 hwnd = NtUserCreateWindowEx( 0, &class_name, &class_name, NULL, style, pos.x, pos.y,
2060 attr.width, attr.height, parent, 0, NULL, NULL, 0, NULL,
2061 0, FALSE );
2063 if (!(data = alloc_win_data( display, hwnd )))
2065 NtUserDestroyWindow( hwnd );
2066 return 0;
2068 SetRect( &data->window_rect, pos.x, pos.y, pos.x + attr.width, pos.y + attr.height );
2069 data->whole_rect = data->client_rect = data->window_rect;
2070 data->whole_window = data->client_window = 0;
2071 data->embedded = TRUE;
2072 data->mapped = TRUE;
2074 NtUserSetProp( hwnd, foreign_window_prop, (HANDLE)xwin );
2075 XSaveContext( display, xwin, winContext, (char *)data->hwnd );
2077 TRACE( "win %lx parent %p style %08x %s -> hwnd %p\n",
2078 xwin, parent, style, wine_dbgstr_rect(&data->window_rect), hwnd );
2080 release_win_data( data );
2082 NtUserShowWindow( hwnd, SW_SHOW );
2083 return hwnd;
2087 NTSTATUS x11drv_systray_init( void *arg )
2089 Display *display;
2091 if (is_virtual_desktop()) return FALSE;
2093 display = thread_init_display();
2094 if (DefaultScreen( display ) == 0)
2095 systray_atom = x11drv_atom(_NET_SYSTEM_TRAY_S0);
2096 else
2098 char systray_buffer[29]; /* strlen(_NET_SYSTEM_TRAY_S4294967295)+1 */
2099 sprintf( systray_buffer, "_NET_SYSTEM_TRAY_S%u", DefaultScreen( display ) );
2100 systray_atom = XInternAtom( display, systray_buffer, False );
2102 XSelectInput( display, root_window, StructureNotifyMask );
2104 return TRUE;
2108 NTSTATUS x11drv_systray_clear( void *arg )
2110 HWND hwnd = *(HWND*)arg;
2111 Window win = X11DRV_get_whole_window( hwnd );
2112 if (win) XClearArea( gdi_display, win, 0, 0, 0, 0, True );
2113 return 0;
2117 NTSTATUS x11drv_systray_hide( void *arg )
2119 HWND hwnd = *(HWND*)arg;
2120 struct x11drv_win_data *data;
2122 /* make sure we don't try to unmap it, it confuses some systray docks */
2123 if ((data = get_win_data( hwnd )))
2125 if (data->embedded) data->mapped = FALSE;
2126 release_win_data( data );
2129 return 0;
2133 /* find the X11 window owner the system tray selection */
2134 static Window get_systray_selection_owner( Display *display )
2136 return XGetSelectionOwner( display, systray_atom );
2140 static void get_systray_visual_info( Display *display, Window systray_window, XVisualInfo *info )
2142 XVisualInfo *list, template;
2143 VisualID *visual_id;
2144 Atom type;
2145 int format, num;
2146 unsigned long count, remaining;
2148 *info = default_visual;
2149 if (XGetWindowProperty( display, systray_window, x11drv_atom(_NET_SYSTEM_TRAY_VISUAL), 0,
2150 65536/sizeof(CARD32), False, XA_VISUALID, &type, &format, &count,
2151 &remaining, (unsigned char **)&visual_id ))
2152 return;
2154 if (type == XA_VISUALID && format == 32)
2156 template.visualid = visual_id[0];
2157 if ((list = XGetVisualInfo( display, VisualIDMask, &template, &num )))
2159 *info = list[0];
2160 TRACE_(systray)( "systray window %lx got visual %lx\n", systray_window, info->visualid );
2161 XFree( list );
2164 XFree( visual_id );
2168 NTSTATUS x11drv_systray_dock( void *arg )
2170 struct systray_dock_params *params = arg;
2171 Window systray_window, window;
2172 Display *display;
2173 XEvent ev;
2174 XSetWindowAttributes attr;
2175 XVisualInfo visual;
2176 struct x11drv_win_data *data;
2177 UNICODE_STRING class_name;
2178 BOOL layered;
2179 HWND hwnd;
2181 static const WCHAR icon_classname[] =
2182 {'_','_','w','i','n','e','x','1','1','_','t','r','a','y','_','i','c','o','n',0};
2184 if (params->event_handle)
2186 XClientMessageEvent *event = (XClientMessageEvent *)(UINT_PTR)params->event_handle;
2187 display = event->display;
2188 systray_window = event->data.l[2];
2190 else
2192 display = thread_init_display();
2193 if (!(systray_window = get_systray_selection_owner( display ))) return STATUS_UNSUCCESSFUL;
2196 get_systray_visual_info( display, systray_window, &visual );
2198 *params->layered = layered = (visual.depth == 32);
2200 RtlInitUnicodeString( &class_name, icon_classname );
2201 hwnd = NtUserCreateWindowEx( layered ? WS_EX_LAYERED : 0, &class_name, &class_name, NULL,
2202 WS_CLIPSIBLINGS | WS_POPUP, CW_USEDEFAULT, CW_USEDEFAULT,
2203 params->cx, params->cy, NULL, 0, NULL, params->icon, 0,
2204 NULL, 0, FALSE );
2206 if (!(data = get_win_data( hwnd ))) return STATUS_UNSUCCESSFUL;
2207 if (layered) set_window_visual( data, &visual, TRUE );
2208 make_window_embedded( data );
2209 window = data->whole_window;
2210 release_win_data( data );
2212 NtUserShowWindow( hwnd, SW_SHOWNA );
2214 TRACE_(systray)( "icon window %p/%lx\n", hwnd, window );
2216 /* send the docking request message */
2217 ev.xclient.type = ClientMessage;
2218 ev.xclient.window = systray_window;
2219 ev.xclient.message_type = x11drv_atom( _NET_SYSTEM_TRAY_OPCODE );
2220 ev.xclient.format = 32;
2221 ev.xclient.data.l[0] = CurrentTime;
2222 ev.xclient.data.l[1] = SYSTEM_TRAY_REQUEST_DOCK;
2223 ev.xclient.data.l[2] = window;
2224 ev.xclient.data.l[3] = 0;
2225 ev.xclient.data.l[4] = 0;
2226 XSendEvent( display, systray_window, False, NoEventMask, &ev );
2228 if (!layered)
2230 attr.background_pixmap = ParentRelative;
2231 attr.bit_gravity = ForgetGravity;
2232 XChangeWindowAttributes( display, window, CWBackPixmap | CWBitGravity, &attr );
2234 else
2236 /* force repainig */
2237 send_message( hwnd, WM_SIZE, SIZE_RESTORED, MAKELONG( params->cx, params->cy ));
2240 return STATUS_SUCCESS;
2244 /***********************************************************************
2245 * X11DRV_get_whole_window
2247 * Return the X window associated with the full area of a window
2249 Window X11DRV_get_whole_window( HWND hwnd )
2251 struct x11drv_win_data *data = get_win_data( hwnd );
2252 Window ret;
2254 if (!data)
2256 if (hwnd == NtUserGetDesktopWindow()) return root_window;
2257 return (Window)NtUserGetProp( hwnd, whole_window_prop );
2259 ret = data->whole_window;
2260 release_win_data( data );
2261 return ret;
2265 /***********************************************************************
2266 * X11DRV_get_ic
2268 * Return the X input context associated with a window
2270 XIC X11DRV_get_ic( HWND hwnd )
2272 struct x11drv_win_data *data = get_win_data( hwnd );
2273 XIM xim;
2274 XIC ret = 0;
2276 if (data)
2278 x11drv_thread_data()->last_xic_hwnd = hwnd;
2279 ret = data->xic;
2280 if (!ret && (xim = x11drv_thread_data()->xim)) ret = X11DRV_CreateIC( xim, data );
2281 release_win_data( data );
2283 return ret;
2287 /***********************************************************************
2288 * X11DRV_GetDC (X11DRV.@)
2290 void X11DRV_GetDC( HDC hdc, HWND hwnd, HWND top, const RECT *win_rect,
2291 const RECT *top_rect, DWORD flags )
2293 struct x11drv_escape_set_drawable escape;
2294 HWND parent;
2296 escape.code = X11DRV_SET_DRAWABLE;
2297 escape.mode = IncludeInferiors;
2299 escape.dc_rect.left = win_rect->left - top_rect->left;
2300 escape.dc_rect.top = win_rect->top - top_rect->top;
2301 escape.dc_rect.right = win_rect->right - top_rect->left;
2302 escape.dc_rect.bottom = win_rect->bottom - top_rect->top;
2304 if (top == hwnd)
2306 struct x11drv_win_data *data = get_win_data( hwnd );
2308 escape.drawable = data ? data->whole_window : X11DRV_get_whole_window( hwnd );
2310 /* special case: when repainting the root window, clip out top-level windows */
2311 if (data && data->whole_window == root_window) escape.mode = ClipByChildren;
2312 release_win_data( data );
2314 else
2316 /* find the first ancestor that has a drawable */
2317 for (parent = hwnd; parent && parent != top; parent = NtUserGetAncestor( parent, GA_PARENT ))
2318 if ((escape.drawable = X11DRV_get_whole_window( parent ))) break;
2320 if (escape.drawable)
2322 POINT pt = { 0, 0 };
2323 NtUserMapWindowPoints( 0, parent, &pt, 1 );
2324 escape.dc_rect = *win_rect;
2325 OffsetRect( &escape.dc_rect, pt.x, pt.y );
2326 if (flags & DCX_CLIPCHILDREN) escape.mode = ClipByChildren;
2328 else escape.drawable = X11DRV_get_whole_window( top );
2331 NtGdiExtEscape( hdc, NULL, 0, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
2335 /***********************************************************************
2336 * X11DRV_ReleaseDC (X11DRV.@)
2338 void X11DRV_ReleaseDC( HWND hwnd, HDC hdc )
2340 struct x11drv_escape_set_drawable escape;
2342 escape.code = X11DRV_SET_DRAWABLE;
2343 escape.drawable = root_window;
2344 escape.mode = IncludeInferiors;
2345 escape.dc_rect = NtUserGetVirtualScreenRect();
2346 OffsetRect( &escape.dc_rect, -2 * escape.dc_rect.left, -2 * escape.dc_rect.top );
2347 NtGdiExtEscape( hdc, NULL, 0, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
2351 /*************************************************************************
2352 * ScrollDC (X11DRV.@)
2354 BOOL X11DRV_ScrollDC( HDC hdc, INT dx, INT dy, HRGN update )
2356 RECT rect;
2357 BOOL ret;
2358 HRGN expose_rgn = 0;
2360 NtGdiGetAppClipBox( hdc, &rect );
2362 if (update)
2364 INT code = X11DRV_START_EXPOSURES;
2365 NtGdiExtEscape( hdc, NULL, 0, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, 0, NULL );
2367 ret = NtGdiBitBlt( hdc, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top,
2368 hdc, rect.left - dx, rect.top - dy, SRCCOPY, 0, 0 );
2370 code = X11DRV_END_EXPOSURES;
2371 NtGdiExtEscape( hdc, NULL, 0, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code,
2372 sizeof(expose_rgn), (LPSTR)&expose_rgn );
2373 if (expose_rgn)
2375 NtGdiCombineRgn( update, update, expose_rgn, RGN_OR );
2376 NtGdiDeleteObjectApp( expose_rgn );
2379 else ret = NtGdiBitBlt( hdc, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top,
2380 hdc, rect.left - dx, rect.top - dy, SRCCOPY, 0, 0 );
2382 return ret;
2386 /***********************************************************************
2387 * SetCapture (X11DRV.@)
2389 void X11DRV_SetCapture( HWND hwnd, UINT flags )
2391 struct x11drv_thread_data *thread_data = x11drv_thread_data();
2392 struct x11drv_win_data *data;
2394 if (!(flags & (GUI_INMOVESIZE | GUI_INMENUMODE))) return;
2396 if (hwnd)
2398 if (!(data = get_win_data( NtUserGetAncestor( hwnd, GA_ROOT )))) return;
2399 if (data->whole_window)
2401 XFlush( gdi_display );
2402 XGrabPointer( data->display, data->whole_window, False,
2403 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
2404 GrabModeAsync, GrabModeAsync, None, None, CurrentTime );
2405 thread_data->grab_hwnd = data->hwnd;
2407 release_win_data( data );
2409 else /* release capture */
2411 if (!(data = get_win_data( thread_data->grab_hwnd ))) return;
2412 XFlush( gdi_display );
2413 XUngrabPointer( data->display, CurrentTime );
2414 XFlush( data->display );
2415 thread_data->grab_hwnd = NULL;
2416 release_win_data( data );
2421 /*****************************************************************
2422 * SetParent (X11DRV.@)
2424 void X11DRV_SetParent( HWND hwnd, HWND parent, HWND old_parent )
2426 struct x11drv_win_data *data;
2428 if (parent == old_parent) return;
2429 if (!(data = get_win_data( hwnd ))) return;
2430 if (data->embedded) goto done;
2432 if (parent != NtUserGetDesktopWindow()) /* a child window */
2434 if (old_parent == NtUserGetDesktopWindow())
2436 /* destroy the old X windows */
2437 destroy_whole_window( data, FALSE );
2438 data->managed = FALSE;
2441 else /* new top level window */
2443 create_whole_window( data );
2445 done:
2446 release_win_data( data );
2447 set_gl_drawable_parent( hwnd, parent );
2449 /* Recreate the parent gl_drawable now that we know there are child windows
2450 * that will need clipping support.
2452 sync_gl_drawable( parent, TRUE );
2454 fetch_icon_data( hwnd, 0, 0 );
2458 static inline BOOL get_surface_rect( const RECT *visible_rect, RECT *surface_rect )
2460 *surface_rect = NtUserGetVirtualScreenRect();
2462 if (!intersect_rect( surface_rect, surface_rect, visible_rect )) return FALSE;
2463 OffsetRect( surface_rect, -visible_rect->left, -visible_rect->top );
2464 surface_rect->left &= ~31;
2465 surface_rect->top &= ~31;
2466 surface_rect->right = max( surface_rect->left + 32, (surface_rect->right + 31) & ~31 );
2467 surface_rect->bottom = max( surface_rect->top + 32, (surface_rect->bottom + 31) & ~31 );
2468 return TRUE;
2472 /***********************************************************************
2473 * WindowPosChanging (X11DRV.@)
2475 BOOL X11DRV_WindowPosChanging( HWND hwnd, HWND insert_after, UINT swp_flags,
2476 const RECT *window_rect, const RECT *client_rect, RECT *visible_rect,
2477 struct window_surface **surface )
2479 struct x11drv_win_data *data = get_win_data( hwnd );
2480 RECT surface_rect;
2481 DWORD flags;
2482 COLORREF key;
2483 BOOL layered = NtUserGetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_LAYERED;
2485 if (!data && !(data = X11DRV_create_win_data( hwnd, window_rect, client_rect ))) return TRUE;
2487 /* check if we need to switch the window to managed */
2488 if (!data->managed && data->whole_window && is_window_managed( hwnd, swp_flags, window_rect ))
2490 TRACE( "making win %p/%lx managed\n", hwnd, data->whole_window );
2491 release_win_data( data );
2492 unmap_window( hwnd );
2493 if (!(data = get_win_data( hwnd ))) return TRUE;
2494 data->managed = TRUE;
2497 *visible_rect = *window_rect;
2498 X11DRV_window_to_X_rect( data, visible_rect, window_rect, client_rect );
2500 /* create the window surface if necessary */
2502 if (!data->whole_window && !data->embedded) goto done;
2503 if (swp_flags & SWP_HIDEWINDOW) goto done;
2504 if (data->use_alpha) goto done;
2505 if (!get_surface_rect( visible_rect, &surface_rect )) goto done;
2507 if (*surface) window_surface_release( *surface );
2508 *surface = NULL; /* indicate that we want to draw directly to the window */
2510 if (data->embedded) goto done;
2511 if (data->whole_window == root_window) goto done;
2512 if (data->client_window) goto done;
2513 if (!client_side_graphics && !layered) goto done;
2515 if (data->surface)
2517 if (EqualRect( &data->surface->rect, &surface_rect ))
2519 /* existing surface is good enough */
2520 window_surface_add_ref( data->surface );
2521 *surface = data->surface;
2522 goto done;
2525 else if (!(swp_flags & SWP_SHOWWINDOW) && !(NtUserGetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE)) goto done;
2527 if (!layered || !NtUserGetLayeredWindowAttributes( hwnd, &key, NULL, &flags ) || !(flags & LWA_COLORKEY))
2528 key = CLR_INVALID;
2530 *surface = create_surface( data->whole_window, &data->vis, &surface_rect, key, FALSE );
2532 done:
2533 release_win_data( data );
2534 return TRUE;
2538 /***********************************************************************
2539 * WindowPosChanged (X11DRV.@)
2541 void X11DRV_WindowPosChanged( HWND hwnd, HWND insert_after, UINT swp_flags,
2542 const RECT *rectWindow, const RECT *rectClient,
2543 const RECT *visible_rect, const RECT *valid_rects,
2544 struct window_surface *surface )
2546 struct x11drv_thread_data *thread_data;
2547 struct x11drv_win_data *data;
2548 DWORD new_style = NtUserGetWindowLongW( hwnd, GWL_STYLE );
2549 RECT old_window_rect, old_whole_rect, old_client_rect;
2550 int event_type;
2552 if (!(data = get_win_data( hwnd ))) return;
2554 thread_data = x11drv_thread_data();
2556 old_window_rect = data->window_rect;
2557 old_whole_rect = data->whole_rect;
2558 old_client_rect = data->client_rect;
2559 data->window_rect = *rectWindow;
2560 data->whole_rect = *visible_rect;
2561 data->client_rect = *rectClient;
2562 if (data->vis.visualid == default_visual.visualid)
2564 if (surface) window_surface_add_ref( surface );
2565 if (data->surface) window_surface_release( data->surface );
2566 data->surface = surface;
2569 TRACE( "win %p window %s client %s style %08x flags %08x\n",
2570 hwnd, wine_dbgstr_rect(rectWindow), wine_dbgstr_rect(rectClient), new_style, swp_flags );
2572 if (!IsRectEmpty( &valid_rects[0] ))
2574 Window window = data->whole_window;
2575 int x_offset = old_whole_rect.left - data->whole_rect.left;
2576 int y_offset = old_whole_rect.top - data->whole_rect.top;
2578 /* if all that happened is that the whole window moved, copy everything */
2579 if (!(swp_flags & SWP_FRAMECHANGED) &&
2580 old_whole_rect.right - data->whole_rect.right == x_offset &&
2581 old_whole_rect.bottom - data->whole_rect.bottom == y_offset &&
2582 old_client_rect.left - data->client_rect.left == x_offset &&
2583 old_client_rect.right - data->client_rect.right == x_offset &&
2584 old_client_rect.top - data->client_rect.top == y_offset &&
2585 old_client_rect.bottom - data->client_rect.bottom == y_offset &&
2586 EqualRect( &valid_rects[0], &data->client_rect ))
2588 /* if we have an X window the bits will be moved by the X server */
2589 if (!window && (x_offset != 0 || y_offset != 0))
2591 release_win_data( data );
2592 move_window_bits( hwnd, window, &old_whole_rect, visible_rect,
2593 &old_client_rect, rectClient, rectWindow );
2594 if (!(data = get_win_data( hwnd ))) return;
2597 else
2599 release_win_data( data );
2600 move_window_bits( hwnd, window, &valid_rects[1], &valid_rects[0],
2601 &old_client_rect, rectClient, rectWindow );
2602 if (!(data = get_win_data( hwnd ))) return;
2606 XFlush( gdi_display ); /* make sure painting is done before we move the window */
2608 sync_client_position( data, &old_client_rect, &old_whole_rect );
2610 if (!data->whole_window)
2612 BOOL needs_resize = (!data->client_window &&
2613 (data->client_rect.right - data->client_rect.left !=
2614 old_client_rect.right - old_client_rect.left ||
2615 data->client_rect.bottom - data->client_rect.top !=
2616 old_client_rect.bottom - old_client_rect.top));
2617 release_win_data( data );
2618 if (needs_resize) sync_gl_drawable( hwnd, FALSE );
2619 return;
2622 /* check if we are currently processing an event relevant to this window */
2623 event_type = 0;
2624 if (thread_data &&
2625 thread_data->current_event &&
2626 thread_data->current_event->xany.window == data->whole_window)
2628 event_type = thread_data->current_event->type;
2629 if (event_type != ConfigureNotify && event_type != PropertyNotify &&
2630 event_type != GravityNotify && event_type != ReparentNotify)
2631 event_type = 0; /* ignore other events */
2634 if (data->mapped && event_type != ReparentNotify)
2636 if (((swp_flags & SWP_HIDEWINDOW) && !(new_style & WS_VISIBLE)) ||
2637 (!event_type && !(new_style & WS_MINIMIZE) &&
2638 !is_window_rect_mapped( rectWindow ) && is_window_rect_mapped( &old_window_rect )))
2640 release_win_data( data );
2641 unmap_window( hwnd );
2642 if (NtUserIsWindowRectFullScreen( &old_window_rect )) reset_clipping_window();
2643 if (!(data = get_win_data( hwnd ))) return;
2647 /* don't change position if we are about to minimize or maximize a managed window */
2648 if (!event_type &&
2649 !(data->managed && (swp_flags & SWP_STATECHANGED) && (new_style & (WS_MINIMIZE|WS_MAXIMIZE))))
2650 sync_window_position( data, swp_flags, &old_window_rect, &old_whole_rect, &old_client_rect );
2652 if ((new_style & WS_VISIBLE) &&
2653 ((new_style & WS_MINIMIZE) || is_window_rect_mapped( rectWindow )))
2655 if (!data->mapped)
2657 BOOL needs_icon = !data->icon_pixmap;
2658 BOOL needs_map = TRUE;
2660 /* layered windows are mapped only once their attributes are set */
2661 if (NtUserGetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_LAYERED)
2662 needs_map = data->layered || IsRectEmpty( rectWindow );
2663 release_win_data( data );
2664 if (needs_icon) fetch_icon_data( hwnd, 0, 0 );
2665 if (needs_map) map_window( hwnd, new_style );
2666 return;
2668 else if ((swp_flags & SWP_STATECHANGED) && (!data->iconic != !(new_style & WS_MINIMIZE)))
2670 set_wm_hints( data );
2671 data->iconic = (new_style & WS_MINIMIZE) != 0;
2672 TRACE( "changing win %p iconic state to %u\n", data->hwnd, data->iconic );
2673 if (data->iconic)
2674 XIconifyWindow( data->display, data->whole_window, data->vis.screen );
2675 else if (is_window_rect_mapped( rectWindow ))
2676 XMapWindow( data->display, data->whole_window );
2677 update_net_wm_states( data );
2679 else
2681 if (swp_flags & (SWP_FRAMECHANGED|SWP_STATECHANGED)) set_wm_hints( data );
2682 if (!event_type) update_net_wm_states( data );
2686 XFlush( data->display ); /* make sure changes are done before we start painting again */
2687 if (data->surface && data->vis.visualid != default_visual.visualid)
2688 data->surface->funcs->flush( data->surface );
2690 release_win_data( data );
2693 /* check if the window icon should be hidden (i.e. moved off-screen) */
2694 static BOOL hide_icon( struct x11drv_win_data *data )
2696 static const WCHAR trayW[] = {'S','h','e','l','l','_','T','r','a','y','W','n','d'};
2697 UNICODE_STRING str = { sizeof(trayW), sizeof(trayW), (WCHAR *)trayW };
2699 if (data->managed) return TRUE;
2700 /* hide icons in desktop mode when the taskbar is active */
2701 if (!is_virtual_desktop()) return FALSE;
2702 return NtUserIsWindowVisible( NtUserFindWindowEx( 0, 0, &str, NULL, 0 ));
2705 /***********************************************************************
2706 * ShowWindow (X11DRV.@)
2708 UINT X11DRV_ShowWindow( HWND hwnd, INT cmd, RECT *rect, UINT swp )
2710 int x, y;
2711 unsigned int width, height, border, depth;
2712 Window root, top;
2713 POINT pos;
2714 DWORD style = NtUserGetWindowLongW( hwnd, GWL_STYLE );
2715 struct x11drv_thread_data *thread_data = x11drv_thread_data();
2716 struct x11drv_win_data *data = get_win_data( hwnd );
2718 if (!data || !data->whole_window) goto done;
2719 if (style & WS_MINIMIZE)
2721 if (((rect->left != -32000 || rect->top != -32000)) && hide_icon( data ))
2723 OffsetRect( rect, -32000 - rect->left, -32000 - rect->top );
2724 swp &= ~(SWP_NOMOVE | SWP_NOCLIENTMOVE);
2726 goto done;
2728 if (!data->managed || !data->mapped || data->iconic) goto done;
2730 /* only fetch the new rectangle if the ShowWindow was a result of a window manager event */
2732 if (!thread_data->current_event || thread_data->current_event->xany.window != data->whole_window)
2733 goto done;
2735 if (thread_data->current_event->type != ConfigureNotify &&
2736 thread_data->current_event->type != PropertyNotify)
2737 goto done;
2739 TRACE( "win %p/%lx cmd %d at %s flags %08x\n",
2740 hwnd, data->whole_window, cmd, wine_dbgstr_rect(rect), swp );
2742 XGetGeometry( thread_data->display, data->whole_window,
2743 &root, &x, &y, &width, &height, &border, &depth );
2744 XTranslateCoordinates( thread_data->display, data->whole_window, root, 0, 0, &x, &y, &top );
2745 pos = root_to_virtual_screen( x, y );
2746 X11DRV_X_to_window_rect( data, rect, pos.x, pos.y, width, height );
2747 swp &= ~(SWP_NOMOVE | SWP_NOCLIENTMOVE | SWP_NOSIZE | SWP_NOCLIENTSIZE);
2749 done:
2750 release_win_data( data );
2751 return swp;
2755 /**********************************************************************
2756 * SetWindowIcon (X11DRV.@)
2758 * hIcon or hIconSm has changed (or is being initialised for the
2759 * first time). Complete the X11 driver-specific initialisation
2760 * and set the window hints.
2762 void X11DRV_SetWindowIcon( HWND hwnd, UINT type, HICON icon )
2764 struct x11drv_win_data *data;
2766 if (!(data = get_win_data( hwnd ))) return;
2767 if (!data->whole_window) goto done;
2768 release_win_data( data ); /* release the lock, fetching the icon requires sending messages */
2770 if (type == ICON_BIG) fetch_icon_data( hwnd, icon, 0 );
2771 else fetch_icon_data( hwnd, 0, icon );
2773 if (!(data = get_win_data( hwnd ))) return;
2774 set_wm_hints( data );
2775 done:
2776 release_win_data( data );
2780 /***********************************************************************
2781 * SetWindowRgn (X11DRV.@)
2783 * Assign specified region to window (for non-rectangular windows)
2785 void X11DRV_SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL redraw )
2787 struct x11drv_win_data *data;
2789 if ((data = get_win_data( hwnd )))
2791 sync_window_region( data, hrgn );
2792 release_win_data( data );
2794 else if (X11DRV_get_whole_window( hwnd ))
2796 send_message( hwnd, WM_X11DRV_SET_WIN_REGION, 0, 0 );
2801 /***********************************************************************
2802 * SetLayeredWindowAttributes (X11DRV.@)
2804 * Set transparency attributes for a layered window.
2806 void X11DRV_SetLayeredWindowAttributes( HWND hwnd, COLORREF key, BYTE alpha, DWORD flags )
2808 struct x11drv_win_data *data = get_win_data( hwnd );
2810 if (data)
2812 set_window_visual( data, &default_visual, FALSE );
2814 if (data->whole_window)
2815 sync_window_opacity( data->display, data->whole_window, key, alpha, flags );
2816 if (data->surface)
2817 set_surface_color_key( data->surface, (flags & LWA_COLORKEY) ? key : CLR_INVALID );
2819 data->layered = TRUE;
2820 if (!data->mapped) /* mapping is delayed until attributes are set */
2822 DWORD style = NtUserGetWindowLongW( data->hwnd, GWL_STYLE );
2824 if ((style & WS_VISIBLE) &&
2825 ((style & WS_MINIMIZE) || is_window_rect_mapped( &data->window_rect )))
2827 release_win_data( data );
2828 map_window( hwnd, style );
2829 return;
2832 release_win_data( data );
2834 else
2836 Window win = X11DRV_get_whole_window( hwnd );
2837 if (win)
2839 sync_window_opacity( gdi_display, win, key, alpha, flags );
2840 if (flags & LWA_COLORKEY)
2841 FIXME( "LWA_COLORKEY not supported on foreign process window %p\n", hwnd );
2847 /*****************************************************************************
2848 * UpdateLayeredWindow (X11DRV.@)
2850 BOOL X11DRV_UpdateLayeredWindow( HWND hwnd, const UPDATELAYEREDWINDOWINFO *info,
2851 const RECT *window_rect )
2853 struct window_surface *surface;
2854 struct x11drv_win_data *data;
2855 BLENDFUNCTION blend = { AC_SRC_OVER, 0, 255, 0 };
2856 COLORREF color_key = (info->dwFlags & ULW_COLORKEY) ? info->crKey : CLR_INVALID;
2857 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
2858 BITMAPINFO *bmi = (BITMAPINFO *)buffer;
2859 void *src_bits, *dst_bits;
2860 RECT rect, src_rect;
2861 HDC hdc = 0;
2862 HBITMAP dib;
2863 BOOL mapped, ret = FALSE;
2865 if (!(data = get_win_data( hwnd ))) return FALSE;
2867 data->layered = TRUE;
2868 if (!data->embedded && argb_visual.visualid) set_window_visual( data, &argb_visual, TRUE );
2870 rect = *window_rect;
2871 OffsetRect( &rect, -window_rect->left, -window_rect->top );
2873 surface = data->surface;
2874 if (!surface || !EqualRect( &surface->rect, &rect ))
2876 data->surface = create_surface( data->whole_window, &data->vis, &rect,
2877 color_key, data->use_alpha );
2878 if (surface) window_surface_release( surface );
2879 surface = data->surface;
2881 else set_surface_color_key( surface, color_key );
2883 if (surface) window_surface_add_ref( surface );
2884 mapped = data->mapped;
2885 release_win_data( data );
2887 /* layered windows are mapped only once their attributes are set */
2888 if (!mapped)
2890 DWORD style = NtUserGetWindowLongW( hwnd, GWL_STYLE );
2892 if ((style & WS_VISIBLE) && ((style & WS_MINIMIZE) || is_window_rect_mapped( window_rect )))
2893 map_window( hwnd, style );
2896 if (!surface) return FALSE;
2897 if (!info->hdcSrc)
2899 window_surface_release( surface );
2900 return TRUE;
2903 dst_bits = surface->funcs->get_info( surface, bmi );
2905 if (!(dib = NtGdiCreateDIBSection( info->hdcDst, NULL, 0, bmi, DIB_RGB_COLORS, 0, 0, 0, &src_bits )))
2906 goto done;
2907 if (!(hdc = NtGdiCreateCompatibleDC( 0 ))) goto done;
2909 NtGdiSelectBitmap( hdc, dib );
2911 surface->funcs->lock( surface );
2913 if (info->prcDirty)
2915 intersect_rect( &rect, &rect, info->prcDirty );
2916 memcpy( src_bits, dst_bits, bmi->bmiHeader.biSizeImage );
2917 NtGdiPatBlt( hdc, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, BLACKNESS );
2919 src_rect = rect;
2920 if (info->pptSrc) OffsetRect( &src_rect, info->pptSrc->x, info->pptSrc->y );
2921 NtGdiTransformPoints( info->hdcSrc, (POINT *)&src_rect, (POINT *)&src_rect, 2, NtGdiDPtoLP );
2923 ret = NtGdiAlphaBlend( hdc, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top,
2924 info->hdcSrc, src_rect.left, src_rect.top,
2925 src_rect.right - src_rect.left, src_rect.bottom - src_rect.top,
2926 (info->dwFlags & ULW_ALPHA) ? *info->pblend : blend, 0 );
2927 if (ret)
2929 memcpy( dst_bits, src_bits, bmi->bmiHeader.biSizeImage );
2930 add_bounds_rect( surface->funcs->get_bounds( surface ), &rect );
2933 surface->funcs->unlock( surface );
2934 surface->funcs->flush( surface );
2936 done:
2937 window_surface_release( surface );
2938 if (hdc) NtGdiDeleteObjectApp( hdc );
2939 if (dib) NtGdiDeleteObjectApp( dib );
2940 return ret;
2943 /* Add a window to taskbar */
2944 static void taskbar_add_tab( HWND hwnd )
2946 struct x11drv_win_data *data;
2948 TRACE("hwnd %p\n", hwnd);
2950 data = get_win_data( hwnd );
2951 if (!data)
2952 return;
2954 data->add_taskbar = TRUE;
2955 data->skip_taskbar = FALSE;
2956 update_net_wm_states( data );
2957 release_win_data( data );
2960 /* Delete a window from taskbar */
2961 static void taskbar_delete_tab( HWND hwnd )
2963 struct x11drv_win_data *data;
2965 TRACE("hwnd %p\n", hwnd);
2967 data = get_win_data( hwnd );
2968 if (!data)
2969 return;
2971 data->skip_taskbar = TRUE;
2972 data->add_taskbar = FALSE;
2973 update_net_wm_states( data );
2974 release_win_data( data );
2977 /**********************************************************************
2978 * X11DRV_WindowMessage (X11DRV.@)
2980 LRESULT X11DRV_WindowMessage( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp )
2982 struct x11drv_win_data *data;
2984 switch(msg)
2986 case WM_X11DRV_UPDATE_CLIPBOARD:
2987 return update_clipboard( hwnd );
2988 case WM_X11DRV_SET_WIN_REGION:
2989 if ((data = get_win_data( hwnd )))
2991 sync_window_region( data, (HRGN)1 );
2992 release_win_data( data );
2994 return 0;
2995 case WM_X11DRV_RESIZE_DESKTOP:
2996 X11DRV_resize_desktop( (BOOL)lp );
2997 return 0;
2998 case WM_X11DRV_SET_CURSOR:
2999 if ((data = get_win_data( hwnd )))
3001 Window win = data->whole_window;
3002 release_win_data( data );
3003 if (win) set_window_cursor( win, (HCURSOR)lp );
3005 else if (hwnd == x11drv_thread_data()->clip_hwnd)
3006 set_window_cursor( x11drv_thread_data()->clip_window, (HCURSOR)lp );
3007 return 0;
3008 case WM_X11DRV_CLIP_CURSOR_NOTIFY:
3009 return clip_cursor_notify( hwnd, (HWND)wp, (HWND)lp );
3010 case WM_X11DRV_CLIP_CURSOR_REQUEST:
3011 return clip_cursor_request( hwnd, (BOOL)wp, (BOOL)lp );
3012 case WM_X11DRV_DELETE_TAB:
3013 taskbar_delete_tab( hwnd );
3014 return 0;
3015 case WM_X11DRV_ADD_TAB:
3016 taskbar_add_tab( hwnd );
3017 return 0;
3018 default:
3019 FIXME( "got window msg %x hwnd %p wp %lx lp %lx\n", msg, hwnd, wp, lp );
3020 return 0;
3025 /***********************************************************************
3026 * is_netwm_supported
3028 static BOOL is_netwm_supported( Display *display, Atom atom )
3030 static Atom *net_supported;
3031 static int net_supported_count = -1;
3032 int i;
3034 if (net_supported_count == -1)
3036 Atom type;
3037 int format;
3038 unsigned long count, remaining;
3040 if (!XGetWindowProperty( display, DefaultRootWindow(display), x11drv_atom(_NET_SUPPORTED), 0,
3041 ~0UL, False, XA_ATOM, &type, &format, &count,
3042 &remaining, (unsigned char **)&net_supported ))
3043 net_supported_count = get_property_size( format, count ) / sizeof(Atom);
3044 else
3045 net_supported_count = 0;
3048 for (i = 0; i < net_supported_count; i++)
3049 if (net_supported[i] == atom) return TRUE;
3050 return FALSE;
3054 /***********************************************************************
3055 * start_screensaver
3057 static LRESULT start_screensaver(void)
3059 if (!is_virtual_desktop())
3061 const char *argv[3] = { "xdg-screensaver", "activate", NULL };
3062 int pid = __wine_unix_spawnvp( (char **)argv, FALSE );
3063 if (pid > 0)
3065 TRACE( "started process %d\n", pid );
3066 return 0;
3069 return -1;
3073 /***********************************************************************
3074 * SysCommand (X11DRV.@)
3076 * Perform WM_SYSCOMMAND handling.
3078 LRESULT X11DRV_SysCommand( HWND hwnd, WPARAM wparam, LPARAM lparam )
3080 WPARAM hittest = wparam & 0x0f;
3081 int dir;
3082 struct x11drv_win_data *data;
3084 if (!(data = get_win_data( hwnd )))
3086 if (wparam == SC_SCREENSAVE && hwnd == NtUserGetDesktopWindow()) return start_screensaver();
3087 return -1;
3089 if (!data->whole_window || !data->managed || !data->mapped) goto failed;
3091 switch (wparam & 0xfff0)
3093 case SC_MOVE:
3094 if (!hittest) dir = _NET_WM_MOVERESIZE_MOVE_KEYBOARD;
3095 else dir = _NET_WM_MOVERESIZE_MOVE;
3096 break;
3097 case SC_SIZE:
3098 /* windows without WS_THICKFRAME are not resizable through the window manager */
3099 if (!(NtUserGetWindowLongW( hwnd, GWL_STYLE ) & WS_THICKFRAME)) goto failed;
3101 switch (hittest)
3103 case WMSZ_LEFT: dir = _NET_WM_MOVERESIZE_SIZE_LEFT; break;
3104 case WMSZ_RIGHT: dir = _NET_WM_MOVERESIZE_SIZE_RIGHT; break;
3105 case WMSZ_TOP: dir = _NET_WM_MOVERESIZE_SIZE_TOP; break;
3106 case WMSZ_TOPLEFT: dir = _NET_WM_MOVERESIZE_SIZE_TOPLEFT; break;
3107 case WMSZ_TOPRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_TOPRIGHT; break;
3108 case WMSZ_BOTTOM: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOM; break;
3109 case WMSZ_BOTTOMLEFT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT; break;
3110 case WMSZ_BOTTOMRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT; break;
3111 case 9: dir = _NET_WM_MOVERESIZE_MOVE; break;
3112 default: dir = _NET_WM_MOVERESIZE_SIZE_KEYBOARD; break;
3114 break;
3116 case SC_KEYMENU:
3117 /* prevent a simple ALT press+release from activating the system menu,
3118 * as that can get confusing on managed windows */
3119 if ((WCHAR)lparam) goto failed; /* got an explicit char */
3120 if (NtUserGetWindowLongPtrW( hwnd, GWLP_ID )) goto failed; /* window has a real menu */
3121 if (!(NtUserGetWindowLongW( hwnd, GWL_STYLE ) & WS_SYSMENU)) goto failed; /* no system menu */
3122 TRACE( "ignoring SC_KEYMENU wp %lx lp %lx\n", wparam, lparam );
3123 release_win_data( data );
3124 return 0;
3126 default:
3127 goto failed;
3130 if (NtUserGetWindowLongW( hwnd, GWL_STYLE ) & WS_MAXIMIZE) goto failed;
3132 if (!is_netwm_supported( data->display, x11drv_atom(_NET_WM_MOVERESIZE) ))
3134 TRACE( "_NET_WM_MOVERESIZE not supported\n" );
3135 goto failed;
3138 release_win_data( data );
3139 move_resize_window( hwnd, dir );
3140 return 0;
3142 failed:
3143 release_win_data( data );
3144 return -1;
3147 void X11DRV_FlashWindowEx( FLASHWINFO *pfinfo )
3149 struct x11drv_win_data *data = get_win_data( pfinfo->hwnd );
3150 XEvent xev;
3152 if (!data)
3153 return;
3155 if (data->mapped)
3157 xev.type = ClientMessage;
3158 xev.xclient.window = data->whole_window;
3159 xev.xclient.message_type = x11drv_atom( _NET_WM_STATE );
3160 xev.xclient.serial = 0;
3161 xev.xclient.display = data->display;
3162 xev.xclient.send_event = True;
3163 xev.xclient.format = 32;
3164 xev.xclient.data.l[0] = pfinfo->dwFlags ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
3165 xev.xclient.data.l[1] = x11drv_atom( _NET_WM_STATE_DEMANDS_ATTENTION );
3166 xev.xclient.data.l[2] = 0;
3167 xev.xclient.data.l[3] = 1;
3168 xev.xclient.data.l[4] = 0;
3170 XSendEvent( data->display, DefaultRootWindow( data->display ), False,
3171 SubstructureNotifyMask, &xev );
3173 release_win_data( data );
3176 void init_win_context(void)
3178 init_recursive_mutex( &win_data_mutex );
3180 winContext = XUniqueContext();
3181 win_data_context = XUniqueContext();
3182 cursor_context = XUniqueContext();