winex11.drv: Initialize escape.drawable in X11DRV_GetDC().
[wine.git] / dlls / winex11.drv / window.c
blobe1fa665608e0191d1c0d81582f03045d0a9181fb
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_NOACTIVATE)
1002 || (ex_style & WS_EX_TOOLWINDOW && !(ex_style & WS_EX_APPWINDOW)))
1003 new_state |= (1 << NET_WM_STATE_SKIP_TASKBAR) | (1 << NET_WM_STATE_SKIP_PAGER);
1004 else if (!(ex_style & WS_EX_APPWINDOW) && NtUserGetWindowRelative( data->hwnd, GW_OWNER ))
1005 new_state |= (1 << NET_WM_STATE_SKIP_TASKBAR);
1008 if (!data->mapped) /* set the _NET_WM_STATE atom directly */
1010 Atom atoms[NB_NET_WM_STATES+1];
1011 DWORD count;
1013 for (i = count = 0; i < NB_NET_WM_STATES; i++)
1015 if (!(new_state & (1 << i))) continue;
1016 TRACE( "setting wm state %u for unmapped window %p/%lx\n",
1017 i, data->hwnd, data->whole_window );
1018 atoms[count++] = X11DRV_Atoms[net_wm_state_atoms[i] - FIRST_XATOM];
1019 if (net_wm_state_atoms[i] == XATOM__NET_WM_STATE_MAXIMIZED_VERT)
1020 atoms[count++] = x11drv_atom(_NET_WM_STATE_MAXIMIZED_HORZ);
1022 XChangeProperty( data->display, data->whole_window, x11drv_atom(_NET_WM_STATE), XA_ATOM,
1023 32, PropModeReplace, (unsigned char *)atoms, count );
1025 else /* ask the window manager to do it for us */
1027 XEvent xev;
1029 xev.xclient.type = ClientMessage;
1030 xev.xclient.window = data->whole_window;
1031 xev.xclient.message_type = x11drv_atom(_NET_WM_STATE);
1032 xev.xclient.serial = 0;
1033 xev.xclient.display = data->display;
1034 xev.xclient.send_event = True;
1035 xev.xclient.format = 32;
1036 xev.xclient.data.l[3] = 1;
1037 xev.xclient.data.l[4] = 0;
1039 for (i = 0; i < NB_NET_WM_STATES; i++)
1041 TRACE( "setting wm state %u for window %p/%lx to %u prev %u\n",
1042 i, data->hwnd, data->whole_window,
1043 (new_state & (1 << i)) != 0, (data->net_wm_state & (1 << i)) != 0 );
1045 xev.xclient.data.l[0] = (new_state & (1 << i)) ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
1046 xev.xclient.data.l[1] = X11DRV_Atoms[net_wm_state_atoms[i] - FIRST_XATOM];
1047 xev.xclient.data.l[2] = ((net_wm_state_atoms[i] == XATOM__NET_WM_STATE_MAXIMIZED_VERT) ?
1048 x11drv_atom(_NET_WM_STATE_MAXIMIZED_HORZ) : 0);
1049 XSendEvent( data->display, root_window, False,
1050 SubstructureRedirectMask | SubstructureNotifyMask, &xev );
1053 data->net_wm_state = new_state;
1056 /***********************************************************************
1057 * read_net_wm_states
1059 void read_net_wm_states( Display* display, struct x11drv_win_data *data )
1061 Atom type, *state;
1062 int format;
1063 unsigned long i, j, count, remaining;
1064 DWORD new_state = 0;
1065 BOOL maximized_horz = FALSE;
1067 if (!data->whole_window) return;
1069 if (!XGetWindowProperty( display, data->whole_window, x11drv_atom(_NET_WM_STATE), 0,
1070 65536/sizeof(CARD32), False, XA_ATOM, &type, &format, &count,
1071 &remaining, (unsigned char **)&state ))
1073 if (type == XA_ATOM && format == 32)
1075 for (i = 0; i < count; i++)
1077 if (state[i] == x11drv_atom(_NET_WM_STATE_MAXIMIZED_HORZ))
1078 maximized_horz = TRUE;
1079 for (j=0; j < NB_NET_WM_STATES; j++)
1081 if (state[i] == X11DRV_Atoms[net_wm_state_atoms[j] - FIRST_XATOM])
1083 new_state |= 1 << j;
1088 XFree( state );
1091 if (!maximized_horz)
1092 new_state &= ~(1 << NET_WM_STATE_MAXIMIZED);
1094 data->net_wm_state = new_state;
1098 /***********************************************************************
1099 * set_xembed_flags
1101 static void set_xembed_flags( struct x11drv_win_data *data, unsigned long flags )
1103 unsigned long info[2];
1105 if (!data->whole_window) return;
1107 info[0] = 0; /* protocol version */
1108 info[1] = flags;
1109 XChangeProperty( data->display, data->whole_window, x11drv_atom(_XEMBED_INFO),
1110 x11drv_atom(_XEMBED_INFO), 32, PropModeReplace, (unsigned char*)info, 2 );
1114 /***********************************************************************
1115 * map_window
1117 static void map_window( HWND hwnd, DWORD new_style )
1119 struct x11drv_win_data *data;
1121 make_owner_managed( hwnd );
1122 wait_for_withdrawn_state( hwnd, TRUE );
1124 if (!(data = get_win_data( hwnd ))) return;
1126 if (data->whole_window && !data->mapped)
1128 TRACE( "win %p/%lx\n", data->hwnd, data->whole_window );
1130 remove_startup_notification( data->display, data->whole_window );
1131 set_wm_hints( data );
1133 if (!data->embedded)
1135 update_net_wm_states( data );
1136 sync_window_style( data );
1137 XMapWindow( data->display, data->whole_window );
1138 XFlush( data->display );
1139 if (data->surface && data->vis.visualid != default_visual.visualid)
1140 data->surface->funcs->flush( data->surface );
1142 else set_xembed_flags( data, XEMBED_MAPPED );
1144 data->mapped = TRUE;
1145 data->iconic = (new_style & WS_MINIMIZE) != 0;
1147 release_win_data( data );
1151 /***********************************************************************
1152 * unmap_window
1154 static void unmap_window( HWND hwnd )
1156 struct x11drv_win_data *data;
1158 wait_for_withdrawn_state( hwnd, FALSE );
1160 if (!(data = get_win_data( hwnd ))) return;
1162 if (data->mapped)
1164 TRACE( "win %p/%lx\n", data->hwnd, data->whole_window );
1166 if (data->embedded) set_xembed_flags( data, 0 );
1167 else if (!data->managed) XUnmapWindow( data->display, data->whole_window );
1168 else XWithdrawWindow( data->display, data->whole_window, data->vis.screen );
1170 data->mapped = FALSE;
1171 data->net_wm_state = 0;
1173 release_win_data( data );
1177 /***********************************************************************
1178 * make_window_embedded
1180 void make_window_embedded( struct x11drv_win_data *data )
1182 /* the window cannot be mapped before being embedded */
1183 if (data->mapped)
1185 if (!data->managed) XUnmapWindow( data->display, data->whole_window );
1186 else XWithdrawWindow( data->display, data->whole_window, data->vis.screen );
1187 data->net_wm_state = 0;
1189 data->embedded = TRUE;
1190 data->managed = TRUE;
1191 sync_window_style( data );
1192 set_xembed_flags( data, (data->mapped || data->embedder) ? XEMBED_MAPPED : 0 );
1196 /***********************************************************************
1197 * get_decoration_rect
1199 static void get_decoration_rect( struct x11drv_win_data *data, RECT *rect,
1200 const RECT *window_rect, const RECT *client_rect )
1202 DWORD style, ex_style, style_mask = 0, ex_style_mask = 0;
1203 unsigned long decor;
1205 SetRectEmpty( rect );
1206 if (!data->managed) return;
1208 style = NtUserGetWindowLongW( data->hwnd, GWL_STYLE );
1209 ex_style = NtUserGetWindowLongW( data->hwnd, GWL_EXSTYLE );
1210 decor = get_mwm_decorations( data, style, ex_style, window_rect, client_rect );
1212 if (decor & MWM_DECOR_TITLE) style_mask |= WS_CAPTION;
1213 if (decor & MWM_DECOR_BORDER)
1215 style_mask |= WS_DLGFRAME | WS_THICKFRAME;
1216 ex_style_mask |= WS_EX_DLGMODALFRAME;
1219 AdjustWindowRectEx( rect, style & style_mask, FALSE, ex_style & ex_style_mask );
1223 /***********************************************************************
1224 * X11DRV_window_to_X_rect
1226 * Convert a rect from client to X window coordinates
1228 static void X11DRV_window_to_X_rect( struct x11drv_win_data *data, RECT *rect,
1229 const RECT *window_rect, const RECT *client_rect )
1231 RECT rc;
1233 if (IsRectEmpty( rect )) return;
1235 get_decoration_rect( data, &rc, window_rect, client_rect );
1236 rect->left -= rc.left;
1237 rect->right -= rc.right;
1238 rect->top -= rc.top;
1239 rect->bottom -= rc.bottom;
1240 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
1241 if (rect->left >= rect->right) rect->right = rect->left + 1;
1245 /***********************************************************************
1246 * X11DRV_X_to_window_rect
1248 * Opposite of X11DRV_window_to_X_rect
1250 void X11DRV_X_to_window_rect( struct x11drv_win_data *data, RECT *rect, int x, int y, int cx, int cy )
1252 RECT rc;
1254 get_decoration_rect( data, &rc, &data->window_rect, &data->client_rect );
1256 x += min( data->window_rect.left - data->whole_rect.left, rc.left );
1257 y += min( data->window_rect.top - data->whole_rect.top, rc.top );
1258 cx += max( (data->window_rect.right - data->window_rect.left) -
1259 (data->whole_rect.right - data->whole_rect.left), rc.right - rc.left );
1260 cy += max( (data->window_rect.bottom - data->window_rect.top) -
1261 (data->whole_rect.bottom - data->whole_rect.top), rc.bottom - rc.top );
1262 SetRect( rect, x, y, x + cx, y + cy );
1266 /***********************************************************************
1267 * sync_window_position
1269 * Synchronize the X window position with the Windows one
1271 static void sync_window_position( struct x11drv_win_data *data,
1272 UINT swp_flags, const RECT *old_window_rect,
1273 const RECT *old_whole_rect, const RECT *old_client_rect )
1275 DWORD style = NtUserGetWindowLongW( data->hwnd, GWL_STYLE );
1276 DWORD ex_style = NtUserGetWindowLongW( data->hwnd, GWL_EXSTYLE );
1277 XWindowChanges changes;
1278 unsigned int mask = 0;
1280 if (data->managed && data->iconic) return;
1282 /* resizing a managed maximized window is not allowed */
1283 if (!(style & WS_MAXIMIZE) || !data->managed)
1285 changes.width = data->whole_rect.right - data->whole_rect.left;
1286 changes.height = data->whole_rect.bottom - data->whole_rect.top;
1287 /* if window rect is empty force size to 1x1 */
1288 if (changes.width <= 0 || changes.height <= 0) changes.width = changes.height = 1;
1289 if (changes.width > 65535) changes.width = 65535;
1290 if (changes.height > 65535) changes.height = 65535;
1291 mask |= CWWidth | CWHeight;
1294 /* only the size is allowed to change for the desktop window */
1295 if (data->whole_window != root_window)
1297 POINT pt = virtual_screen_to_root( data->whole_rect.left, data->whole_rect.top );
1298 changes.x = pt.x;
1299 changes.y = pt.y;
1300 mask |= CWX | CWY;
1303 if (!(swp_flags & SWP_NOZORDER) || (swp_flags & SWP_SHOWWINDOW))
1305 /* find window that this one must be after */
1306 HWND prev = NtUserGetWindowRelative( data->hwnd, GW_HWNDPREV );
1307 while (prev && !(NtUserGetWindowLongW( prev, GWL_STYLE ) & WS_VISIBLE))
1308 prev = NtUserGetWindowRelative( prev, GW_HWNDPREV );
1309 if (!prev) /* top child */
1311 changes.stack_mode = Above;
1312 mask |= CWStackMode;
1314 /* should use stack_mode Below but most window managers don't get it right */
1315 /* and Above with a sibling doesn't work so well either, so we ignore it */
1318 set_size_hints( data, style );
1319 set_mwm_hints( data, style, ex_style );
1320 update_net_wm_states( data );
1321 data->configure_serial = NextRequest( data->display );
1322 XReconfigureWMWindow( data->display, data->whole_window, data->vis.screen, mask, &changes );
1323 #ifdef HAVE_LIBXSHAPE
1324 if (IsRectEmpty( old_window_rect ) != IsRectEmpty( &data->window_rect ))
1325 sync_window_region( data, (HRGN)1 );
1326 if (data->shaped)
1328 int old_x_offset = old_window_rect->left - old_whole_rect->left;
1329 int old_y_offset = old_window_rect->top - old_whole_rect->top;
1330 int new_x_offset = data->window_rect.left - data->whole_rect.left;
1331 int new_y_offset = data->window_rect.top - data->whole_rect.top;
1332 if (old_x_offset != new_x_offset || old_y_offset != new_y_offset)
1333 XShapeOffsetShape( data->display, data->whole_window, ShapeBounding,
1334 new_x_offset - old_x_offset, new_y_offset - old_y_offset );
1336 #endif
1338 TRACE( "win %p/%lx pos %d,%d,%dx%d after %lx changes=%x serial=%lu\n",
1339 data->hwnd, data->whole_window, data->whole_rect.left, data->whole_rect.top,
1340 data->whole_rect.right - data->whole_rect.left,
1341 data->whole_rect.bottom - data->whole_rect.top,
1342 changes.sibling, mask, data->configure_serial );
1346 /***********************************************************************
1347 * sync_client_position
1349 * Synchronize the X client window position with the Windows one
1351 static void sync_client_position( struct x11drv_win_data *data,
1352 const RECT *old_client_rect, const RECT *old_whole_rect )
1354 int mask = 0;
1355 XWindowChanges changes;
1357 if (!data->client_window) return;
1359 changes.x = data->client_rect.left - data->whole_rect.left;
1360 changes.y = data->client_rect.top - data->whole_rect.top;
1361 changes.width = min( max( 1, data->client_rect.right - data->client_rect.left ), 65535 );
1362 changes.height = min( max( 1, data->client_rect.bottom - data->client_rect.top ), 65535 );
1364 if (changes.x != old_client_rect->left - old_whole_rect->left) mask |= CWX;
1365 if (changes.y != old_client_rect->top - old_whole_rect->top) mask |= CWY;
1366 if (changes.width != old_client_rect->right - old_client_rect->left) mask |= CWWidth;
1367 if (changes.height != old_client_rect->bottom - old_client_rect->top) mask |= CWHeight;
1369 if (mask)
1371 TRACE( "setting client win %lx pos %d,%d,%dx%d changes=%x\n",
1372 data->client_window, changes.x, changes.y, changes.width, changes.height, mask );
1373 XConfigureWindow( data->display, data->client_window, mask, &changes );
1378 /***********************************************************************
1379 * move_window_bits
1381 * Move the window bits when a window is moved.
1383 static void move_window_bits( HWND hwnd, Window window, const RECT *old_rect, const RECT *new_rect,
1384 const RECT *old_client_rect, const RECT *new_client_rect,
1385 const RECT *new_window_rect )
1387 RECT src_rect = *old_rect;
1388 RECT dst_rect = *new_rect;
1389 HDC hdc_src, hdc_dst;
1390 INT code;
1391 HRGN rgn;
1392 HWND parent = 0;
1394 if (!window)
1396 OffsetRect( &dst_rect, -new_window_rect->left, -new_window_rect->top );
1397 parent = NtUserGetAncestor( hwnd, GA_PARENT );
1398 hdc_src = NtUserGetDCEx( parent, 0, DCX_CACHE );
1399 hdc_dst = NtUserGetDCEx( hwnd, 0, DCX_CACHE | DCX_WINDOW );
1401 else
1403 OffsetRect( &dst_rect, -new_client_rect->left, -new_client_rect->top );
1404 /* make src rect relative to the old position of the window */
1405 OffsetRect( &src_rect, -old_client_rect->left, -old_client_rect->top );
1406 if (dst_rect.left == src_rect.left && dst_rect.top == src_rect.top) return;
1407 hdc_src = hdc_dst = NtUserGetDCEx( hwnd, 0, DCX_CACHE );
1410 rgn = NtGdiCreateRectRgn( dst_rect.left, dst_rect.top, dst_rect.right, dst_rect.bottom );
1411 NtGdiExtSelectClipRgn( hdc_dst, rgn, RGN_COPY );
1412 NtGdiDeleteObjectApp( rgn );
1413 /* WS_CLIPCHILDREN doesn't exclude children from the window update
1414 * region, and ExcludeUpdateRgn call may inappropriately clip valid
1415 * child window contents from the copied parent window bits, but we
1416 * still want to avoid copying invalid window bits when possible.
1418 if (!(NtUserGetWindowLongW( hwnd, GWL_STYLE ) & WS_CLIPCHILDREN ))
1419 NtUserExcludeUpdateRgn( hdc_dst, hwnd );
1421 code = X11DRV_START_EXPOSURES;
1422 NtGdiExtEscape( hdc_dst, NULL, 0, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, 0, NULL );
1424 TRACE( "copying bits for win %p/%lx %s -> %s\n",
1425 hwnd, window, wine_dbgstr_rect(&src_rect), wine_dbgstr_rect(&dst_rect) );
1426 NtGdiBitBlt( hdc_dst, dst_rect.left, dst_rect.top,
1427 dst_rect.right - dst_rect.left, dst_rect.bottom - dst_rect.top,
1428 hdc_src, src_rect.left, src_rect.top, SRCCOPY, 0, 0 );
1430 rgn = 0;
1431 code = X11DRV_END_EXPOSURES;
1432 NtGdiExtEscape( hdc_dst, NULL, 0, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, sizeof(rgn), (LPSTR)&rgn );
1434 NtUserReleaseDC( hwnd, hdc_dst );
1435 if (hdc_src != hdc_dst) NtUserReleaseDC( parent, hdc_src );
1437 if (rgn)
1439 if (!window)
1441 /* map region to client rect since we are using DCX_WINDOW */
1442 NtGdiOffsetRgn( rgn, new_window_rect->left - new_client_rect->left,
1443 new_window_rect->top - new_client_rect->top );
1444 NtUserRedrawWindow( hwnd, NULL, rgn,
1445 RDW_INVALIDATE | RDW_FRAME | RDW_ERASE | RDW_ALLCHILDREN );
1447 else NtUserRedrawWindow( hwnd, NULL, rgn, RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN );
1448 NtGdiDeleteObjectApp( rgn );
1453 /***********************************************************************
1454 * get_dummy_parent
1456 * Create a dummy parent window for child windows that don't have a true X11 parent.
1458 Window get_dummy_parent(void)
1460 static Window dummy_parent;
1462 if (!dummy_parent)
1464 XSetWindowAttributes attrib;
1466 attrib.override_redirect = True;
1467 attrib.border_pixel = 0;
1468 attrib.colormap = default_colormap;
1469 dummy_parent = XCreateWindow( gdi_display, root_window, -1, -1, 1, 1, 0, default_visual.depth,
1470 InputOutput, default_visual.visual,
1471 CWColormap | CWBorderPixel | CWOverrideRedirect, &attrib );
1472 XMapWindow( gdi_display, dummy_parent );
1474 return dummy_parent;
1478 /**********************************************************************
1479 * create_dummy_client_window
1481 Window create_dummy_client_window(void)
1483 XSetWindowAttributes attr;
1485 attr.colormap = default_colormap;
1486 attr.bit_gravity = NorthWestGravity;
1487 attr.win_gravity = NorthWestGravity;
1488 attr.backing_store = NotUseful;
1489 attr.border_pixel = 0;
1491 return XCreateWindow( gdi_display, get_dummy_parent(), 0, 0, 1, 1, 0,
1492 default_visual.depth, InputOutput, default_visual.visual,
1493 CWBitGravity | CWWinGravity | CWBackingStore | CWColormap | CWBorderPixel, &attr );
1496 /**********************************************************************
1497 * create_client_window
1499 Window create_client_window( HWND hwnd, const XVisualInfo *visual )
1501 Window dummy_parent = get_dummy_parent();
1502 struct x11drv_win_data *data = get_win_data( hwnd );
1503 XSetWindowAttributes attr;
1504 Window ret;
1505 int x, y, cx, cy;
1507 if (!data)
1509 /* explicitly create data for HWND_MESSAGE windows since they can be used for OpenGL */
1510 HWND parent = NtUserGetAncestor( hwnd, GA_PARENT );
1511 if (parent == NtUserGetDesktopWindow() || NtUserGetAncestor( parent, GA_PARENT )) return 0;
1512 if (!(data = alloc_win_data( thread_init_display(), hwnd ))) return 0;
1513 NtUserGetClientRect( hwnd, &data->client_rect );
1514 data->window_rect = data->whole_rect = data->client_rect;
1517 if (data->client_window)
1519 XDeleteContext( data->display, data->client_window, winContext );
1520 XReparentWindow( gdi_display, data->client_window, dummy_parent, 0, 0 );
1521 TRACE( "%p reparent xwin %lx/%lx\n", data->hwnd, data->whole_window, data->client_window );
1524 if (data->client_colormap) XFreeColormap( gdi_display, data->client_colormap );
1525 data->client_colormap = XCreateColormap( gdi_display, dummy_parent, visual->visual,
1526 (visual->class == PseudoColor ||
1527 visual->class == GrayScale ||
1528 visual->class == DirectColor) ? AllocAll : AllocNone );
1529 attr.colormap = data->client_colormap;
1530 attr.bit_gravity = NorthWestGravity;
1531 attr.win_gravity = NorthWestGravity;
1532 attr.backing_store = NotUseful;
1533 attr.border_pixel = 0;
1535 x = data->client_rect.left - data->whole_rect.left;
1536 y = data->client_rect.top - data->whole_rect.top;
1537 cx = min( max( 1, data->client_rect.right - data->client_rect.left ), 65535 );
1538 cy = min( max( 1, data->client_rect.bottom - data->client_rect.top ), 65535 );
1540 ret = data->client_window = XCreateWindow( gdi_display,
1541 data->whole_window ? data->whole_window : dummy_parent,
1542 x, y, cx, cy, 0, default_visual.depth, InputOutput,
1543 visual->visual, CWBitGravity | CWWinGravity |
1544 CWBackingStore | CWColormap | CWBorderPixel, &attr );
1545 if (data->client_window)
1547 XSaveContext( data->display, data->client_window, winContext, (char *)data->hwnd );
1548 XMapWindow( gdi_display, data->client_window );
1549 XSync( gdi_display, False );
1550 if (data->whole_window) XSelectInput( data->display, data->client_window, ExposureMask );
1551 TRACE( "%p xwin %lx/%lx\n", data->hwnd, data->whole_window, data->client_window );
1553 release_win_data( data );
1554 return ret;
1558 /**********************************************************************
1559 * create_whole_window
1561 * Create the whole X window for a given window
1563 static void create_whole_window( struct x11drv_win_data *data )
1565 int cx, cy, mask;
1566 XSetWindowAttributes attr;
1567 WCHAR text[1024];
1568 COLORREF key;
1569 BYTE alpha;
1570 DWORD layered_flags;
1571 HRGN win_rgn;
1572 POINT pos;
1574 if (!data->managed && is_window_managed( data->hwnd, SWP_NOACTIVATE, &data->window_rect ))
1576 TRACE( "making win %p/%lx managed\n", data->hwnd, data->whole_window );
1577 data->managed = TRUE;
1580 if ((win_rgn = NtGdiCreateRectRgn( 0, 0, 0, 0 )) &&
1581 NtUserGetWindowRgnEx( data->hwnd, win_rgn, 0 ) == ERROR)
1583 NtGdiDeleteObjectApp( win_rgn );
1584 win_rgn = 0;
1586 data->shaped = (win_rgn != 0);
1588 if (data->vis.visualid != default_visual.visualid)
1589 data->whole_colormap = XCreateColormap( data->display, root_window, data->vis.visual, AllocNone );
1591 mask = get_window_attributes( data, &attr );
1593 if (!(cx = data->whole_rect.right - data->whole_rect.left)) cx = 1;
1594 else if (cx > 65535) cx = 65535;
1595 if (!(cy = data->whole_rect.bottom - data->whole_rect.top)) cy = 1;
1596 else if (cy > 65535) cy = 65535;
1598 pos = virtual_screen_to_root( data->whole_rect.left, data->whole_rect.top );
1599 data->whole_window = XCreateWindow( data->display, root_window, pos.x, pos.y,
1600 cx, cy, 0, data->vis.depth, InputOutput,
1601 data->vis.visual, mask, &attr );
1602 if (!data->whole_window) goto done;
1604 set_initial_wm_hints( data->display, data->whole_window );
1605 set_wm_hints( data );
1607 XSaveContext( data->display, data->whole_window, winContext, (char *)data->hwnd );
1608 NtUserSetProp( data->hwnd, whole_window_prop, (HANDLE)data->whole_window );
1610 /* set the window text */
1611 if (!NtUserInternalGetWindowText( data->hwnd, text, ARRAY_SIZE( text ))) text[0] = 0;
1612 sync_window_text( data->display, data->whole_window, text );
1614 /* set the window region */
1615 if (win_rgn || IsRectEmpty( &data->window_rect )) sync_window_region( data, win_rgn );
1617 /* set the window opacity */
1618 if (!NtUserGetLayeredWindowAttributes( data->hwnd, &key, &alpha, &layered_flags )) layered_flags = 0;
1619 sync_window_opacity( data->display, data->whole_window, key, alpha, layered_flags );
1621 XFlush( data->display ); /* make sure the window exists before we start painting to it */
1623 sync_window_cursor( data->whole_window );
1625 done:
1626 if (win_rgn) NtGdiDeleteObjectApp( win_rgn );
1630 /**********************************************************************
1631 * destroy_whole_window
1633 * Destroy the whole X window for a given window.
1635 static void destroy_whole_window( struct x11drv_win_data *data, BOOL already_destroyed )
1637 TRACE( "win %p xwin %lx/%lx\n", data->hwnd, data->whole_window, data->client_window );
1639 if (data->client_window) XDeleteContext( data->display, data->client_window, winContext );
1641 if (!data->whole_window)
1643 if (data->embedded)
1645 Window xwin = (Window)NtUserGetProp( data->hwnd, foreign_window_prop );
1646 if (xwin)
1648 if (!already_destroyed) XSelectInput( data->display, xwin, 0 );
1649 XDeleteContext( data->display, xwin, winContext );
1650 NtUserRemoveProp( data->hwnd, foreign_window_prop );
1652 return;
1655 else
1657 if (data->client_window && !already_destroyed)
1659 XSelectInput( data->display, data->client_window, 0 );
1660 XReparentWindow( data->display, data->client_window, get_dummy_parent(), 0, 0 );
1661 XSync( data->display, False );
1663 XDeleteContext( data->display, data->whole_window, winContext );
1664 if (!already_destroyed) XDestroyWindow( data->display, data->whole_window );
1666 if (data->whole_colormap) XFreeColormap( data->display, data->whole_colormap );
1667 data->whole_window = data->client_window = 0;
1668 data->whole_colormap = 0;
1669 data->wm_state = WithdrawnState;
1670 data->net_wm_state = 0;
1671 data->mapped = FALSE;
1672 if (data->xic)
1674 XUnsetICFocus( data->xic );
1675 XDestroyIC( data->xic );
1676 data->xic = 0;
1678 /* Outlook stops processing messages after destroying a dialog, so we need an explicit flush */
1679 XFlush( data->display );
1680 if (data->surface) window_surface_release( data->surface );
1681 data->surface = NULL;
1682 NtUserRemoveProp( data->hwnd, whole_window_prop );
1686 /**********************************************************************
1687 * set_window_visual
1689 * Change the visual by destroying and recreating the X window if needed.
1691 void set_window_visual( struct x11drv_win_data *data, const XVisualInfo *vis, BOOL use_alpha )
1693 Window client_window = data->client_window;
1694 Window whole_window = data->whole_window;
1696 if (!data->use_alpha == !use_alpha) return;
1697 if (data->surface) window_surface_release( data->surface );
1698 data->surface = NULL;
1699 data->use_alpha = use_alpha;
1701 if (data->vis.visualid == vis->visualid) return;
1702 data->client_window = 0;
1703 destroy_whole_window( data, client_window != 0 /* don't destroy whole_window until reparented */ );
1704 data->vis = *vis;
1705 create_whole_window( data );
1706 if (!client_window) return;
1707 /* move the client to the new parent */
1708 XReparentWindow( data->display, client_window, data->whole_window,
1709 data->client_rect.left - data->whole_rect.left,
1710 data->client_rect.top - data->whole_rect.top );
1711 data->client_window = client_window;
1712 XDestroyWindow( data->display, whole_window );
1716 /*****************************************************************
1717 * SetWindowText (X11DRV.@)
1719 void X11DRV_SetWindowText( HWND hwnd, LPCWSTR text )
1721 Window win;
1723 if ((win = X11DRV_get_whole_window( hwnd )) && win != DefaultRootWindow(gdi_display))
1725 Display *display = thread_init_display();
1726 sync_window_text( display, win, text );
1731 /***********************************************************************
1732 * SetWindowStyle (X11DRV.@)
1734 * Update the X state of a window to reflect a style change
1736 void X11DRV_SetWindowStyle( HWND hwnd, INT offset, STYLESTRUCT *style )
1738 struct x11drv_win_data *data;
1739 DWORD changed = style->styleNew ^ style->styleOld;
1741 if (hwnd == NtUserGetDesktopWindow()) return;
1742 if (!(data = get_win_data( hwnd ))) return;
1743 if (!data->whole_window) goto done;
1745 if (offset == GWL_STYLE && (changed & WS_DISABLED)) set_wm_hints( data );
1747 if (offset == GWL_EXSTYLE && (changed & WS_EX_LAYERED)) /* changing WS_EX_LAYERED resets attributes */
1749 data->layered = FALSE;
1750 set_window_visual( data, &default_visual, FALSE );
1751 sync_window_opacity( data->display, data->whole_window, 0, 0, 0 );
1752 if (data->surface) set_surface_color_key( data->surface, CLR_INVALID );
1754 done:
1755 release_win_data( data );
1759 /***********************************************************************
1760 * DestroyWindow (X11DRV.@)
1762 void X11DRV_DestroyWindow( HWND hwnd )
1764 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1765 struct x11drv_win_data *data;
1767 if (!(data = get_win_data( hwnd ))) return;
1769 destroy_whole_window( data, FALSE );
1770 if (thread_data->last_focus == hwnd) thread_data->last_focus = 0;
1771 if (thread_data->last_xic_hwnd == hwnd) thread_data->last_xic_hwnd = 0;
1772 if (data->icon_pixmap) XFreePixmap( gdi_display, data->icon_pixmap );
1773 if (data->icon_mask) XFreePixmap( gdi_display, data->icon_mask );
1774 if (data->client_colormap) XFreeColormap( data->display, data->client_colormap );
1775 free( data->icon_bits );
1776 XDeleteContext( gdi_display, (XID)hwnd, win_data_context );
1777 release_win_data( data );
1778 free( data );
1779 destroy_gl_drawable( hwnd );
1780 wine_vk_surface_destroy( hwnd );
1784 /***********************************************************************
1785 * X11DRV_DestroyNotify
1787 BOOL X11DRV_DestroyNotify( HWND hwnd, XEvent *event )
1789 struct x11drv_win_data *data;
1790 BOOL embedded;
1792 if (!(data = get_win_data( hwnd ))) return FALSE;
1793 embedded = data->embedded;
1794 if (!embedded) FIXME( "window %p/%lx destroyed from the outside\n", hwnd, data->whole_window );
1796 destroy_whole_window( data, TRUE );
1797 release_win_data( data );
1798 if (embedded) send_message( hwnd, WM_CLOSE, 0, 0 );
1799 return TRUE;
1803 /* initialize the desktop window id in the desktop manager process */
1804 BOOL create_desktop_win_data( Window win )
1806 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1807 Display *display = thread_data->display;
1808 struct x11drv_win_data *data;
1810 if (!(data = alloc_win_data( display, NtUserGetDesktopWindow() ))) return FALSE;
1811 data->whole_window = win;
1812 data->managed = TRUE;
1813 NtUserSetProp( data->hwnd, whole_window_prop, (HANDLE)win );
1814 set_initial_wm_hints( display, win );
1815 release_win_data( data );
1816 if (thread_data->clip_window) XReparentWindow( display, thread_data->clip_window, win, 0, 0 );
1817 return TRUE;
1820 /**********************************************************************
1821 * CreateDesktopWindow (X11DRV.@)
1823 BOOL X11DRV_CreateDesktopWindow( HWND hwnd )
1825 unsigned int width, height;
1827 /* retrieve the real size of the desktop */
1828 SERVER_START_REQ( get_window_rectangles )
1830 req->handle = wine_server_user_handle( hwnd );
1831 req->relative = COORDS_CLIENT;
1832 wine_server_call( req );
1833 width = reply->window.right;
1834 height = reply->window.bottom;
1836 SERVER_END_REQ;
1838 if (!width && !height) /* not initialized yet */
1840 RECT rect = NtUserGetVirtualScreenRect();
1842 SERVER_START_REQ( set_window_pos )
1844 req->handle = wine_server_user_handle( hwnd );
1845 req->previous = 0;
1846 req->swp_flags = SWP_NOZORDER;
1847 req->window.left = rect.left;
1848 req->window.top = rect.top;
1849 req->window.right = rect.right;
1850 req->window.bottom = rect.bottom;
1851 req->client = req->window;
1852 wine_server_call( req );
1854 SERVER_END_REQ;
1856 else
1858 Window win = (Window)NtUserGetProp( hwnd, whole_window_prop );
1859 if (win && win != root_window) X11DRV_init_desktop( win, width, height );
1861 return TRUE;
1865 #define WM_WINE_NOTIFY_ACTIVITY WM_USER
1866 #define WM_WINE_DELETE_TAB (WM_USER + 1)
1867 #define WM_WINE_ADD_TAB (WM_USER + 2)
1869 /**********************************************************************
1870 * DesktopWindowProc (X11DRV.@)
1872 LRESULT X11DRV_DesktopWindowProc( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp )
1874 switch (msg)
1876 case WM_WINE_NOTIFY_ACTIVITY:
1878 static ULONG last = 0;
1879 ULONG now = NtGetTickCount();
1880 /* calling XResetScreenSaver too often can cause performance
1881 * problems, so throttle it */
1882 if (now > last + 5000)
1884 XResetScreenSaver( gdi_display );
1885 XFlush( gdi_display );
1886 last = now;
1888 break;
1890 case WM_WINE_DELETE_TAB:
1891 send_notify_message( (HWND)wp, WM_X11DRV_DELETE_TAB, 0, 0 );
1892 break;
1893 case WM_WINE_ADD_TAB:
1894 send_notify_message( (HWND)wp, WM_X11DRV_ADD_TAB, 0, 0 );
1895 break;
1896 case WM_DISPLAYCHANGE:
1897 X11DRV_resize_desktop();
1898 break;
1900 return NtUserMessageCall( hwnd, msg, wp, lp, 0, NtUserDefWindowProc, FALSE );
1903 /**********************************************************************
1904 * CreateWindow (X11DRV.@)
1906 BOOL X11DRV_CreateWindow( HWND hwnd )
1908 if (hwnd == NtUserGetDesktopWindow())
1910 struct x11drv_thread_data *data = x11drv_init_thread_data();
1911 XSetWindowAttributes attr;
1913 /* create the cursor clipping window */
1914 attr.override_redirect = TRUE;
1915 attr.event_mask = StructureNotifyMask | FocusChangeMask;
1916 data->clip_window = XCreateWindow( data->display, root_window, 0, 0, 1, 1, 0, 0,
1917 InputOnly, default_visual.visual,
1918 CWOverrideRedirect | CWEventMask, &attr );
1919 XFlush( data->display );
1920 NtUserSetProp( hwnd, clip_window_prop, (HANDLE)data->clip_window );
1921 X11DRV_DisplayDevices_RegisterEventHandlers();
1923 return TRUE;
1927 /***********************************************************************
1928 * get_win_data
1930 * Lock and return the X11 data structure associated with a window.
1932 struct x11drv_win_data *get_win_data( HWND hwnd )
1934 char *data;
1936 if (!hwnd) return NULL;
1937 pthread_mutex_lock( &win_data_mutex );
1938 if (!XFindContext( gdi_display, (XID)hwnd, win_data_context, &data ))
1939 return (struct x11drv_win_data *)data;
1940 pthread_mutex_unlock( &win_data_mutex );
1941 return NULL;
1945 /***********************************************************************
1946 * release_win_data
1948 * Release the data returned by get_win_data.
1950 void release_win_data( struct x11drv_win_data *data )
1952 if (data) pthread_mutex_unlock( &win_data_mutex );
1956 /***********************************************************************
1957 * X11DRV_create_win_data
1959 * Create an X11 data window structure for an existing window.
1961 static struct x11drv_win_data *X11DRV_create_win_data( HWND hwnd, const RECT *window_rect,
1962 const RECT *client_rect )
1964 Display *display;
1965 struct x11drv_win_data *data;
1966 HWND parent;
1968 if (!(parent = NtUserGetAncestor( hwnd, GA_PARENT ))) return NULL; /* desktop */
1970 /* don't create win data for HWND_MESSAGE windows */
1971 if (parent != NtUserGetDesktopWindow() && !NtUserGetAncestor( parent, GA_PARENT )) return NULL;
1973 if (NtUserGetWindowThread( hwnd, NULL ) != GetCurrentThreadId()) return NULL;
1975 /* Recreate the parent gl_drawable now that we know there are child windows
1976 * that will need clipping support.
1978 sync_gl_drawable( parent, TRUE );
1980 display = thread_init_display();
1981 init_clip_window(); /* make sure the clip window is initialized in this thread */
1983 if (!(data = alloc_win_data( display, hwnd ))) return NULL;
1985 data->whole_rect = data->window_rect = *window_rect;
1986 data->client_rect = *client_rect;
1987 if (parent == NtUserGetDesktopWindow())
1989 create_whole_window( data );
1990 TRACE( "win %p/%lx window %s whole %s client %s\n",
1991 hwnd, data->whole_window, wine_dbgstr_rect( &data->window_rect ),
1992 wine_dbgstr_rect( &data->whole_rect ), wine_dbgstr_rect( &data->client_rect ));
1994 return data;
1998 /***********************************************************************
1999 * create_foreign_window
2001 * Create a foreign window for the specified X window and its ancestors
2003 HWND create_foreign_window( Display *display, Window xwin )
2005 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};
2006 static BOOL class_registered;
2007 struct x11drv_win_data *data;
2008 HWND hwnd, parent;
2009 POINT pos;
2010 Window xparent, xroot;
2011 Window *xchildren;
2012 unsigned int nchildren;
2013 XWindowAttributes attr;
2014 DWORD style = WS_CLIPCHILDREN;
2015 UNICODE_STRING class_name;
2017 if (!class_registered)
2019 UNICODE_STRING version = { 0 };
2020 WNDCLASSEXW class;
2022 memset( &class, 0, sizeof(class) );
2023 class.cbSize = sizeof(class);
2024 class.lpfnWndProc = client_foreign_window_proc;
2025 class.lpszClassName = classW;
2026 RtlInitUnicodeString( &class_name, classW );
2027 if (!NtUserRegisterClassExWOW( &class, &class_name, &version, NULL, 0, 0, NULL ) &&
2028 GetLastError() != ERROR_CLASS_ALREADY_EXISTS)
2030 ERR( "Could not register foreign window class\n" );
2031 return FALSE;
2033 class_registered = TRUE;
2036 if (XFindContext( display, xwin, winContext, (char **)&hwnd )) hwnd = 0;
2037 if (hwnd) return hwnd; /* already created */
2039 XSelectInput( display, xwin, StructureNotifyMask );
2040 if (!XGetWindowAttributes( display, xwin, &attr ) ||
2041 !XQueryTree( display, xwin, &xroot, &xparent, &xchildren, &nchildren ))
2043 XSelectInput( display, xwin, 0 );
2044 return 0;
2046 XFree( xchildren );
2048 if (xparent == xroot)
2050 parent = NtUserGetDesktopWindow();
2051 style |= WS_POPUP;
2052 pos = root_to_virtual_screen( attr.x, attr.y );
2054 else
2056 parent = create_foreign_window( display, xparent );
2057 style |= WS_CHILD;
2058 pos.x = attr.x;
2059 pos.y = attr.y;
2062 RtlInitUnicodeString( &class_name, classW );
2063 hwnd = NtUserCreateWindowEx( 0, &class_name, &class_name, NULL, style, pos.x, pos.y,
2064 attr.width, attr.height, parent, 0, NULL, NULL, 0, NULL,
2065 0, FALSE );
2067 if (!(data = alloc_win_data( display, hwnd )))
2069 NtUserDestroyWindow( hwnd );
2070 return 0;
2072 SetRect( &data->window_rect, pos.x, pos.y, pos.x + attr.width, pos.y + attr.height );
2073 data->whole_rect = data->client_rect = data->window_rect;
2074 data->whole_window = data->client_window = 0;
2075 data->embedded = TRUE;
2076 data->mapped = TRUE;
2078 NtUserSetProp( hwnd, foreign_window_prop, (HANDLE)xwin );
2079 XSaveContext( display, xwin, winContext, (char *)data->hwnd );
2081 TRACE( "win %lx parent %p style %08x %s -> hwnd %p\n",
2082 xwin, parent, style, wine_dbgstr_rect(&data->window_rect), hwnd );
2084 release_win_data( data );
2086 NtUserShowWindow( hwnd, SW_SHOW );
2087 return hwnd;
2091 NTSTATUS x11drv_systray_init( void *arg )
2093 Display *display;
2095 if (is_virtual_desktop()) return FALSE;
2097 display = thread_init_display();
2098 if (DefaultScreen( display ) == 0)
2099 systray_atom = x11drv_atom(_NET_SYSTEM_TRAY_S0);
2100 else
2102 char systray_buffer[29]; /* strlen(_NET_SYSTEM_TRAY_S4294967295)+1 */
2103 sprintf( systray_buffer, "_NET_SYSTEM_TRAY_S%u", DefaultScreen( display ) );
2104 systray_atom = XInternAtom( display, systray_buffer, False );
2106 XSelectInput( display, root_window, StructureNotifyMask );
2108 return TRUE;
2112 NTSTATUS x11drv_systray_clear( void *arg )
2114 HWND hwnd = *(HWND*)arg;
2115 Window win = X11DRV_get_whole_window( hwnd );
2116 if (win) XClearArea( gdi_display, win, 0, 0, 0, 0, True );
2117 return 0;
2121 NTSTATUS x11drv_systray_hide( void *arg )
2123 HWND hwnd = *(HWND*)arg;
2124 struct x11drv_win_data *data;
2126 /* make sure we don't try to unmap it, it confuses some systray docks */
2127 if ((data = get_win_data( hwnd )))
2129 if (data->embedded) data->mapped = FALSE;
2130 release_win_data( data );
2133 return 0;
2137 /* find the X11 window owner the system tray selection */
2138 static Window get_systray_selection_owner( Display *display )
2140 return XGetSelectionOwner( display, systray_atom );
2144 static void get_systray_visual_info( Display *display, Window systray_window, XVisualInfo *info )
2146 XVisualInfo *list, template;
2147 VisualID *visual_id;
2148 Atom type;
2149 int format, num;
2150 unsigned long count, remaining;
2152 *info = default_visual;
2153 if (XGetWindowProperty( display, systray_window, x11drv_atom(_NET_SYSTEM_TRAY_VISUAL), 0,
2154 65536/sizeof(CARD32), False, XA_VISUALID, &type, &format, &count,
2155 &remaining, (unsigned char **)&visual_id ))
2156 return;
2158 if (type == XA_VISUALID && format == 32)
2160 template.visualid = visual_id[0];
2161 if ((list = XGetVisualInfo( display, VisualIDMask, &template, &num )))
2163 *info = list[0];
2164 TRACE_(systray)( "systray window %lx got visual %lx\n", systray_window, info->visualid );
2165 XFree( list );
2168 XFree( visual_id );
2172 NTSTATUS x11drv_systray_dock( void *arg )
2174 struct systray_dock_params *params = arg;
2175 Window systray_window, window;
2176 Display *display;
2177 XEvent ev;
2178 XSetWindowAttributes attr;
2179 XVisualInfo visual;
2180 struct x11drv_win_data *data;
2181 UNICODE_STRING class_name;
2182 BOOL layered;
2183 HWND hwnd;
2185 static const WCHAR icon_classname[] =
2186 {'_','_','w','i','n','e','x','1','1','_','t','r','a','y','_','i','c','o','n',0};
2188 if (params->event_handle)
2190 XClientMessageEvent *event = (XClientMessageEvent *)(UINT_PTR)params->event_handle;
2191 display = event->display;
2192 systray_window = event->data.l[2];
2194 else
2196 display = thread_init_display();
2197 if (!(systray_window = get_systray_selection_owner( display ))) return STATUS_UNSUCCESSFUL;
2200 get_systray_visual_info( display, systray_window, &visual );
2202 *params->layered = layered = (visual.depth == 32);
2204 RtlInitUnicodeString( &class_name, icon_classname );
2205 hwnd = NtUserCreateWindowEx( layered ? WS_EX_LAYERED : 0, &class_name, &class_name, NULL,
2206 WS_CLIPSIBLINGS | WS_POPUP, CW_USEDEFAULT, CW_USEDEFAULT,
2207 params->cx, params->cy, NULL, 0, NULL, params->icon, 0,
2208 NULL, 0, FALSE );
2210 if (!(data = get_win_data( hwnd ))) return STATUS_UNSUCCESSFUL;
2211 if (layered) set_window_visual( data, &visual, TRUE );
2212 make_window_embedded( data );
2213 window = data->whole_window;
2214 release_win_data( data );
2216 NtUserShowWindow( hwnd, SW_SHOWNA );
2218 TRACE_(systray)( "icon window %p/%lx\n", hwnd, window );
2220 /* send the docking request message */
2221 ev.xclient.type = ClientMessage;
2222 ev.xclient.window = systray_window;
2223 ev.xclient.message_type = x11drv_atom( _NET_SYSTEM_TRAY_OPCODE );
2224 ev.xclient.format = 32;
2225 ev.xclient.data.l[0] = CurrentTime;
2226 ev.xclient.data.l[1] = SYSTEM_TRAY_REQUEST_DOCK;
2227 ev.xclient.data.l[2] = window;
2228 ev.xclient.data.l[3] = 0;
2229 ev.xclient.data.l[4] = 0;
2230 XSendEvent( display, systray_window, False, NoEventMask, &ev );
2232 if (!layered)
2234 attr.background_pixmap = ParentRelative;
2235 attr.bit_gravity = ForgetGravity;
2236 XChangeWindowAttributes( display, window, CWBackPixmap | CWBitGravity, &attr );
2238 else
2240 /* force repainig */
2241 send_message( hwnd, WM_SIZE, SIZE_RESTORED, MAKELONG( params->cx, params->cy ));
2244 return STATUS_SUCCESS;
2248 /***********************************************************************
2249 * X11DRV_get_whole_window
2251 * Return the X window associated with the full area of a window
2253 Window X11DRV_get_whole_window( HWND hwnd )
2255 struct x11drv_win_data *data = get_win_data( hwnd );
2256 Window ret;
2258 if (!data)
2260 if (hwnd == NtUserGetDesktopWindow()) return root_window;
2261 return (Window)NtUserGetProp( hwnd, whole_window_prop );
2263 ret = data->whole_window;
2264 release_win_data( data );
2265 return ret;
2269 /***********************************************************************
2270 * X11DRV_get_ic
2272 * Return the X input context associated with a window
2274 XIC X11DRV_get_ic( HWND hwnd )
2276 struct x11drv_win_data *data = get_win_data( hwnd );
2277 XIM xim;
2278 XIC ret = 0;
2280 if (data)
2282 x11drv_thread_data()->last_xic_hwnd = hwnd;
2283 ret = data->xic;
2284 if (!ret && (xim = x11drv_thread_data()->xim)) ret = X11DRV_CreateIC( xim, data );
2285 release_win_data( data );
2287 return ret;
2291 /***********************************************************************
2292 * X11DRV_GetDC (X11DRV.@)
2294 void X11DRV_GetDC( HDC hdc, HWND hwnd, HWND top, const RECT *win_rect,
2295 const RECT *top_rect, DWORD flags )
2297 struct x11drv_escape_set_drawable escape;
2298 HWND parent;
2300 escape.code = X11DRV_SET_DRAWABLE;
2301 escape.mode = IncludeInferiors;
2302 escape.drawable = 0;
2304 escape.dc_rect.left = win_rect->left - top_rect->left;
2305 escape.dc_rect.top = win_rect->top - top_rect->top;
2306 escape.dc_rect.right = win_rect->right - top_rect->left;
2307 escape.dc_rect.bottom = win_rect->bottom - top_rect->top;
2309 if (top == hwnd)
2311 struct x11drv_win_data *data = get_win_data( hwnd );
2313 escape.drawable = data ? data->whole_window : X11DRV_get_whole_window( hwnd );
2315 /* special case: when repainting the root window, clip out top-level windows */
2316 if (data && data->whole_window == root_window) escape.mode = ClipByChildren;
2317 release_win_data( data );
2319 else
2321 /* find the first ancestor that has a drawable */
2322 for (parent = hwnd; parent && parent != top; parent = NtUserGetAncestor( parent, GA_PARENT ))
2323 if ((escape.drawable = X11DRV_get_whole_window( parent ))) break;
2325 if (escape.drawable)
2327 POINT pt = { 0, 0 };
2328 NtUserMapWindowPoints( 0, parent, &pt, 1 );
2329 escape.dc_rect = *win_rect;
2330 OffsetRect( &escape.dc_rect, pt.x, pt.y );
2331 if (flags & DCX_CLIPCHILDREN) escape.mode = ClipByChildren;
2333 else escape.drawable = X11DRV_get_whole_window( top );
2336 NtGdiExtEscape( hdc, NULL, 0, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
2340 /***********************************************************************
2341 * X11DRV_ReleaseDC (X11DRV.@)
2343 void X11DRV_ReleaseDC( HWND hwnd, HDC hdc )
2345 struct x11drv_escape_set_drawable escape;
2347 escape.code = X11DRV_SET_DRAWABLE;
2348 escape.drawable = root_window;
2349 escape.mode = IncludeInferiors;
2350 escape.dc_rect = NtUserGetVirtualScreenRect();
2351 OffsetRect( &escape.dc_rect, -2 * escape.dc_rect.left, -2 * escape.dc_rect.top );
2352 NtGdiExtEscape( hdc, NULL, 0, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
2356 /*************************************************************************
2357 * ScrollDC (X11DRV.@)
2359 BOOL X11DRV_ScrollDC( HDC hdc, INT dx, INT dy, HRGN update )
2361 RECT rect;
2362 BOOL ret;
2363 HRGN expose_rgn = 0;
2365 NtGdiGetAppClipBox( hdc, &rect );
2367 if (update)
2369 INT code = X11DRV_START_EXPOSURES;
2370 NtGdiExtEscape( hdc, NULL, 0, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, 0, NULL );
2372 ret = NtGdiBitBlt( hdc, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top,
2373 hdc, rect.left - dx, rect.top - dy, SRCCOPY, 0, 0 );
2375 code = X11DRV_END_EXPOSURES;
2376 NtGdiExtEscape( hdc, NULL, 0, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code,
2377 sizeof(expose_rgn), (LPSTR)&expose_rgn );
2378 if (expose_rgn)
2380 NtGdiCombineRgn( update, update, expose_rgn, RGN_OR );
2381 NtGdiDeleteObjectApp( expose_rgn );
2384 else ret = NtGdiBitBlt( hdc, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top,
2385 hdc, rect.left - dx, rect.top - dy, SRCCOPY, 0, 0 );
2387 return ret;
2391 /***********************************************************************
2392 * SetCapture (X11DRV.@)
2394 void X11DRV_SetCapture( HWND hwnd, UINT flags )
2396 struct x11drv_thread_data *thread_data = x11drv_thread_data();
2397 struct x11drv_win_data *data;
2399 if (!(flags & (GUI_INMOVESIZE | GUI_INMENUMODE))) return;
2401 if (hwnd)
2403 if (!(data = get_win_data( NtUserGetAncestor( hwnd, GA_ROOT )))) return;
2404 if (data->whole_window)
2406 XFlush( gdi_display );
2407 XGrabPointer( data->display, data->whole_window, False,
2408 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
2409 GrabModeAsync, GrabModeAsync, None, None, CurrentTime );
2410 thread_data->grab_hwnd = data->hwnd;
2412 release_win_data( data );
2414 else /* release capture */
2416 if (!(data = get_win_data( thread_data->grab_hwnd ))) return;
2417 XFlush( gdi_display );
2418 XUngrabPointer( data->display, CurrentTime );
2419 XFlush( data->display );
2420 thread_data->grab_hwnd = NULL;
2421 release_win_data( data );
2426 /*****************************************************************
2427 * SetParent (X11DRV.@)
2429 void X11DRV_SetParent( HWND hwnd, HWND parent, HWND old_parent )
2431 struct x11drv_win_data *data;
2433 if (parent == old_parent) return;
2434 if (!(data = get_win_data( hwnd ))) return;
2435 if (data->embedded) goto done;
2437 if (parent != NtUserGetDesktopWindow()) /* a child window */
2439 if (old_parent == NtUserGetDesktopWindow())
2441 /* destroy the old X windows */
2442 destroy_whole_window( data, FALSE );
2443 data->managed = FALSE;
2446 else /* new top level window */
2448 create_whole_window( data );
2450 done:
2451 release_win_data( data );
2452 set_gl_drawable_parent( hwnd, parent );
2454 /* Recreate the parent gl_drawable now that we know there are child windows
2455 * that will need clipping support.
2457 sync_gl_drawable( parent, TRUE );
2459 fetch_icon_data( hwnd, 0, 0 );
2463 static inline BOOL get_surface_rect( const RECT *visible_rect, RECT *surface_rect )
2465 *surface_rect = NtUserGetVirtualScreenRect();
2467 if (!intersect_rect( surface_rect, surface_rect, visible_rect )) return FALSE;
2468 OffsetRect( surface_rect, -visible_rect->left, -visible_rect->top );
2469 surface_rect->left &= ~31;
2470 surface_rect->top &= ~31;
2471 surface_rect->right = max( surface_rect->left + 32, (surface_rect->right + 31) & ~31 );
2472 surface_rect->bottom = max( surface_rect->top + 32, (surface_rect->bottom + 31) & ~31 );
2473 return TRUE;
2477 /***********************************************************************
2478 * WindowPosChanging (X11DRV.@)
2480 BOOL X11DRV_WindowPosChanging( HWND hwnd, HWND insert_after, UINT swp_flags,
2481 const RECT *window_rect, const RECT *client_rect, RECT *visible_rect,
2482 struct window_surface **surface )
2484 struct x11drv_win_data *data = get_win_data( hwnd );
2485 RECT surface_rect;
2486 DWORD flags;
2487 COLORREF key;
2488 BOOL layered = NtUserGetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_LAYERED;
2490 if (!data && !(data = X11DRV_create_win_data( hwnd, window_rect, client_rect ))) return TRUE;
2492 /* check if we need to switch the window to managed */
2493 if (!data->managed && data->whole_window && is_window_managed( hwnd, swp_flags, window_rect ))
2495 TRACE( "making win %p/%lx managed\n", hwnd, data->whole_window );
2496 release_win_data( data );
2497 unmap_window( hwnd );
2498 if (!(data = get_win_data( hwnd ))) return TRUE;
2499 data->managed = TRUE;
2502 *visible_rect = *window_rect;
2503 X11DRV_window_to_X_rect( data, visible_rect, window_rect, client_rect );
2505 /* create the window surface if necessary */
2507 if (!data->whole_window && !data->embedded) goto done;
2508 if (swp_flags & SWP_HIDEWINDOW) goto done;
2509 if (data->use_alpha) goto done;
2510 if (!get_surface_rect( visible_rect, &surface_rect )) goto done;
2512 if (*surface) window_surface_release( *surface );
2513 *surface = NULL; /* indicate that we want to draw directly to the window */
2515 if (data->embedded) goto done;
2516 if (data->whole_window == root_window) goto done;
2517 if (data->client_window) goto done;
2518 if (!client_side_graphics && !layered) goto done;
2520 if (data->surface)
2522 if (EqualRect( &data->surface->rect, &surface_rect ))
2524 /* existing surface is good enough */
2525 window_surface_add_ref( data->surface );
2526 *surface = data->surface;
2527 goto done;
2530 else if (!(swp_flags & SWP_SHOWWINDOW) && !(NtUserGetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE)) goto done;
2532 if (!layered || !NtUserGetLayeredWindowAttributes( hwnd, &key, NULL, &flags ) || !(flags & LWA_COLORKEY))
2533 key = CLR_INVALID;
2535 *surface = create_surface( data->whole_window, &data->vis, &surface_rect, key, FALSE );
2537 done:
2538 release_win_data( data );
2539 return TRUE;
2543 /***********************************************************************
2544 * WindowPosChanged (X11DRV.@)
2546 void X11DRV_WindowPosChanged( HWND hwnd, HWND insert_after, UINT swp_flags,
2547 const RECT *rectWindow, const RECT *rectClient,
2548 const RECT *visible_rect, const RECT *valid_rects,
2549 struct window_surface *surface )
2551 struct x11drv_thread_data *thread_data;
2552 struct x11drv_win_data *data;
2553 DWORD new_style = NtUserGetWindowLongW( hwnd, GWL_STYLE );
2554 RECT old_window_rect, old_whole_rect, old_client_rect;
2555 int event_type;
2557 if (!(data = get_win_data( hwnd ))) return;
2559 thread_data = x11drv_thread_data();
2561 old_window_rect = data->window_rect;
2562 old_whole_rect = data->whole_rect;
2563 old_client_rect = data->client_rect;
2564 data->window_rect = *rectWindow;
2565 data->whole_rect = *visible_rect;
2566 data->client_rect = *rectClient;
2567 if (data->vis.visualid == default_visual.visualid)
2569 if (surface) window_surface_add_ref( surface );
2570 if (data->surface) window_surface_release( data->surface );
2571 data->surface = surface;
2574 TRACE( "win %p window %s client %s style %08x flags %08x\n",
2575 hwnd, wine_dbgstr_rect(rectWindow), wine_dbgstr_rect(rectClient), new_style, swp_flags );
2577 if (!IsRectEmpty( &valid_rects[0] ))
2579 Window window = data->whole_window;
2580 int x_offset = old_whole_rect.left - data->whole_rect.left;
2581 int y_offset = old_whole_rect.top - data->whole_rect.top;
2583 /* if all that happened is that the whole window moved, copy everything */
2584 if (!(swp_flags & SWP_FRAMECHANGED) &&
2585 old_whole_rect.right - data->whole_rect.right == x_offset &&
2586 old_whole_rect.bottom - data->whole_rect.bottom == y_offset &&
2587 old_client_rect.left - data->client_rect.left == x_offset &&
2588 old_client_rect.right - data->client_rect.right == x_offset &&
2589 old_client_rect.top - data->client_rect.top == y_offset &&
2590 old_client_rect.bottom - data->client_rect.bottom == y_offset &&
2591 EqualRect( &valid_rects[0], &data->client_rect ))
2593 /* if we have an X window the bits will be moved by the X server */
2594 if (!window && (x_offset != 0 || y_offset != 0))
2596 release_win_data( data );
2597 move_window_bits( hwnd, window, &old_whole_rect, visible_rect,
2598 &old_client_rect, rectClient, rectWindow );
2599 if (!(data = get_win_data( hwnd ))) return;
2602 else
2604 release_win_data( data );
2605 move_window_bits( hwnd, window, &valid_rects[1], &valid_rects[0],
2606 &old_client_rect, rectClient, rectWindow );
2607 if (!(data = get_win_data( hwnd ))) return;
2611 XFlush( gdi_display ); /* make sure painting is done before we move the window */
2613 sync_client_position( data, &old_client_rect, &old_whole_rect );
2615 if (!data->whole_window)
2617 BOOL needs_resize = (!data->client_window &&
2618 (data->client_rect.right - data->client_rect.left !=
2619 old_client_rect.right - old_client_rect.left ||
2620 data->client_rect.bottom - data->client_rect.top !=
2621 old_client_rect.bottom - old_client_rect.top));
2622 release_win_data( data );
2623 if (needs_resize) sync_gl_drawable( hwnd, FALSE );
2624 return;
2627 /* check if we are currently processing an event relevant to this window */
2628 event_type = 0;
2629 if (thread_data &&
2630 thread_data->current_event &&
2631 thread_data->current_event->xany.window == data->whole_window)
2633 event_type = thread_data->current_event->type;
2634 if (event_type != ConfigureNotify && event_type != PropertyNotify &&
2635 event_type != GravityNotify && event_type != ReparentNotify)
2636 event_type = 0; /* ignore other events */
2639 if (data->mapped && event_type != ReparentNotify)
2641 if (((swp_flags & SWP_HIDEWINDOW) && !(new_style & WS_VISIBLE)) ||
2642 (!event_type && !(new_style & WS_MINIMIZE) &&
2643 !is_window_rect_mapped( rectWindow ) && is_window_rect_mapped( &old_window_rect )))
2645 release_win_data( data );
2646 unmap_window( hwnd );
2647 if (NtUserIsWindowRectFullScreen( &old_window_rect )) reset_clipping_window();
2648 if (!(data = get_win_data( hwnd ))) return;
2652 /* don't change position if we are about to minimize or maximize a managed window */
2653 if (!event_type &&
2654 !(data->managed && (swp_flags & SWP_STATECHANGED) && (new_style & (WS_MINIMIZE|WS_MAXIMIZE))))
2655 sync_window_position( data, swp_flags, &old_window_rect, &old_whole_rect, &old_client_rect );
2657 if ((new_style & WS_VISIBLE) &&
2658 ((new_style & WS_MINIMIZE) || is_window_rect_mapped( rectWindow )))
2660 if (!data->mapped)
2662 BOOL needs_icon = !data->icon_pixmap;
2663 BOOL needs_map = TRUE;
2665 /* layered windows are mapped only once their attributes are set */
2666 if (NtUserGetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_LAYERED)
2667 needs_map = data->layered || IsRectEmpty( rectWindow );
2668 release_win_data( data );
2669 if (needs_icon) fetch_icon_data( hwnd, 0, 0 );
2670 if (needs_map) map_window( hwnd, new_style );
2671 return;
2673 else if ((swp_flags & SWP_STATECHANGED) && (!data->iconic != !(new_style & WS_MINIMIZE)))
2675 set_wm_hints( data );
2676 data->iconic = (new_style & WS_MINIMIZE) != 0;
2677 TRACE( "changing win %p iconic state to %u\n", data->hwnd, data->iconic );
2678 if (data->iconic)
2679 XIconifyWindow( data->display, data->whole_window, data->vis.screen );
2680 else if (is_window_rect_mapped( rectWindow ))
2681 XMapWindow( data->display, data->whole_window );
2682 update_net_wm_states( data );
2684 else
2686 if (swp_flags & (SWP_FRAMECHANGED|SWP_STATECHANGED)) set_wm_hints( data );
2687 if (!event_type) update_net_wm_states( data );
2691 XFlush( data->display ); /* make sure changes are done before we start painting again */
2692 if (data->surface && data->vis.visualid != default_visual.visualid)
2693 data->surface->funcs->flush( data->surface );
2695 release_win_data( data );
2698 /* check if the window icon should be hidden (i.e. moved off-screen) */
2699 static BOOL hide_icon( struct x11drv_win_data *data )
2701 static const WCHAR trayW[] = {'S','h','e','l','l','_','T','r','a','y','W','n','d'};
2702 UNICODE_STRING str = { sizeof(trayW), sizeof(trayW), (WCHAR *)trayW };
2704 if (data->managed) return TRUE;
2705 /* hide icons in desktop mode when the taskbar is active */
2706 if (!is_virtual_desktop()) return FALSE;
2707 return NtUserIsWindowVisible( NtUserFindWindowEx( 0, 0, &str, NULL, 0 ));
2710 /***********************************************************************
2711 * ShowWindow (X11DRV.@)
2713 UINT X11DRV_ShowWindow( HWND hwnd, INT cmd, RECT *rect, UINT swp )
2715 int x, y;
2716 unsigned int width, height, border, depth;
2717 Window root, top;
2718 POINT pos;
2719 DWORD style = NtUserGetWindowLongW( hwnd, GWL_STYLE );
2720 struct x11drv_thread_data *thread_data = x11drv_thread_data();
2721 struct x11drv_win_data *data = get_win_data( hwnd );
2723 if (!data || !data->whole_window) goto done;
2724 if (style & WS_MINIMIZE)
2726 if (((rect->left != -32000 || rect->top != -32000)) && hide_icon( data ))
2728 OffsetRect( rect, -32000 - rect->left, -32000 - rect->top );
2729 swp &= ~(SWP_NOMOVE | SWP_NOCLIENTMOVE);
2731 goto done;
2733 if (!data->managed || !data->mapped || data->iconic) goto done;
2735 /* only fetch the new rectangle if the ShowWindow was a result of a window manager event */
2737 if (!thread_data->current_event || thread_data->current_event->xany.window != data->whole_window)
2738 goto done;
2740 if (thread_data->current_event->type != ConfigureNotify &&
2741 thread_data->current_event->type != PropertyNotify)
2742 goto done;
2744 TRACE( "win %p/%lx cmd %d at %s flags %08x\n",
2745 hwnd, data->whole_window, cmd, wine_dbgstr_rect(rect), swp );
2747 XGetGeometry( thread_data->display, data->whole_window,
2748 &root, &x, &y, &width, &height, &border, &depth );
2749 XTranslateCoordinates( thread_data->display, data->whole_window, root, 0, 0, &x, &y, &top );
2750 pos = root_to_virtual_screen( x, y );
2751 X11DRV_X_to_window_rect( data, rect, pos.x, pos.y, width, height );
2752 swp &= ~(SWP_NOMOVE | SWP_NOCLIENTMOVE | SWP_NOSIZE | SWP_NOCLIENTSIZE);
2754 done:
2755 release_win_data( data );
2756 return swp;
2760 /**********************************************************************
2761 * SetWindowIcon (X11DRV.@)
2763 * hIcon or hIconSm has changed (or is being initialised for the
2764 * first time). Complete the X11 driver-specific initialisation
2765 * and set the window hints.
2767 void X11DRV_SetWindowIcon( HWND hwnd, UINT type, HICON icon )
2769 struct x11drv_win_data *data;
2771 if (!(data = get_win_data( hwnd ))) return;
2772 if (!data->whole_window) goto done;
2773 release_win_data( data ); /* release the lock, fetching the icon requires sending messages */
2775 if (type == ICON_BIG) fetch_icon_data( hwnd, icon, 0 );
2776 else fetch_icon_data( hwnd, 0, icon );
2778 if (!(data = get_win_data( hwnd ))) return;
2779 set_wm_hints( data );
2780 done:
2781 release_win_data( data );
2785 /***********************************************************************
2786 * SetWindowRgn (X11DRV.@)
2788 * Assign specified region to window (for non-rectangular windows)
2790 void X11DRV_SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL redraw )
2792 struct x11drv_win_data *data;
2794 if ((data = get_win_data( hwnd )))
2796 sync_window_region( data, hrgn );
2797 release_win_data( data );
2799 else if (X11DRV_get_whole_window( hwnd ))
2801 send_message( hwnd, WM_X11DRV_SET_WIN_REGION, 0, 0 );
2806 /***********************************************************************
2807 * SetLayeredWindowAttributes (X11DRV.@)
2809 * Set transparency attributes for a layered window.
2811 void X11DRV_SetLayeredWindowAttributes( HWND hwnd, COLORREF key, BYTE alpha, DWORD flags )
2813 struct x11drv_win_data *data = get_win_data( hwnd );
2815 if (data)
2817 set_window_visual( data, &default_visual, FALSE );
2819 if (data->whole_window)
2820 sync_window_opacity( data->display, data->whole_window, key, alpha, flags );
2821 if (data->surface)
2822 set_surface_color_key( data->surface, (flags & LWA_COLORKEY) ? key : CLR_INVALID );
2824 data->layered = TRUE;
2825 if (!data->mapped) /* mapping is delayed until attributes are set */
2827 DWORD style = NtUserGetWindowLongW( data->hwnd, GWL_STYLE );
2829 if ((style & WS_VISIBLE) &&
2830 ((style & WS_MINIMIZE) || is_window_rect_mapped( &data->window_rect )))
2832 release_win_data( data );
2833 map_window( hwnd, style );
2834 return;
2837 release_win_data( data );
2839 else
2841 Window win = X11DRV_get_whole_window( hwnd );
2842 if (win)
2844 sync_window_opacity( gdi_display, win, key, alpha, flags );
2845 if (flags & LWA_COLORKEY)
2846 FIXME( "LWA_COLORKEY not supported on foreign process window %p\n", hwnd );
2852 /*****************************************************************************
2853 * UpdateLayeredWindow (X11DRV.@)
2855 BOOL X11DRV_UpdateLayeredWindow( HWND hwnd, const UPDATELAYEREDWINDOWINFO *info,
2856 const RECT *window_rect )
2858 struct window_surface *surface;
2859 struct x11drv_win_data *data;
2860 BLENDFUNCTION blend = { AC_SRC_OVER, 0, 255, 0 };
2861 COLORREF color_key = (info->dwFlags & ULW_COLORKEY) ? info->crKey : CLR_INVALID;
2862 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
2863 BITMAPINFO *bmi = (BITMAPINFO *)buffer;
2864 void *src_bits, *dst_bits;
2865 RECT rect, src_rect;
2866 HDC hdc = 0;
2867 HBITMAP dib;
2868 BOOL mapped, ret = FALSE;
2870 if (!(data = get_win_data( hwnd ))) return FALSE;
2872 data->layered = TRUE;
2873 if (!data->embedded && argb_visual.visualid) set_window_visual( data, &argb_visual, TRUE );
2875 rect = *window_rect;
2876 OffsetRect( &rect, -window_rect->left, -window_rect->top );
2878 surface = data->surface;
2879 if (!surface || !EqualRect( &surface->rect, &rect ))
2881 data->surface = create_surface( data->whole_window, &data->vis, &rect,
2882 color_key, data->use_alpha );
2883 if (surface) window_surface_release( surface );
2884 surface = data->surface;
2886 else set_surface_color_key( surface, color_key );
2888 if (surface) window_surface_add_ref( surface );
2889 mapped = data->mapped;
2890 release_win_data( data );
2892 /* layered windows are mapped only once their attributes are set */
2893 if (!mapped)
2895 DWORD style = NtUserGetWindowLongW( hwnd, GWL_STYLE );
2897 if ((style & WS_VISIBLE) && ((style & WS_MINIMIZE) || is_window_rect_mapped( window_rect )))
2898 map_window( hwnd, style );
2901 if (!surface) return FALSE;
2902 if (!info->hdcSrc)
2904 window_surface_release( surface );
2905 return TRUE;
2908 dst_bits = surface->funcs->get_info( surface, bmi );
2910 if (!(dib = NtGdiCreateDIBSection( info->hdcDst, NULL, 0, bmi, DIB_RGB_COLORS, 0, 0, 0, &src_bits )))
2911 goto done;
2912 if (!(hdc = NtGdiCreateCompatibleDC( 0 ))) goto done;
2914 NtGdiSelectBitmap( hdc, dib );
2916 surface->funcs->lock( surface );
2918 if (info->prcDirty)
2920 intersect_rect( &rect, &rect, info->prcDirty );
2921 memcpy( src_bits, dst_bits, bmi->bmiHeader.biSizeImage );
2922 NtGdiPatBlt( hdc, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, BLACKNESS );
2924 src_rect = rect;
2925 if (info->pptSrc) OffsetRect( &src_rect, info->pptSrc->x, info->pptSrc->y );
2926 NtGdiTransformPoints( info->hdcSrc, (POINT *)&src_rect, (POINT *)&src_rect, 2, NtGdiDPtoLP );
2928 ret = NtGdiAlphaBlend( hdc, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top,
2929 info->hdcSrc, src_rect.left, src_rect.top,
2930 src_rect.right - src_rect.left, src_rect.bottom - src_rect.top,
2931 (info->dwFlags & ULW_ALPHA) ? *info->pblend : blend, 0 );
2932 if (ret)
2934 memcpy( dst_bits, src_bits, bmi->bmiHeader.biSizeImage );
2935 add_bounds_rect( surface->funcs->get_bounds( surface ), &rect );
2938 surface->funcs->unlock( surface );
2939 surface->funcs->flush( surface );
2941 done:
2942 window_surface_release( surface );
2943 if (hdc) NtGdiDeleteObjectApp( hdc );
2944 if (dib) NtGdiDeleteObjectApp( dib );
2945 return ret;
2948 /* Add a window to taskbar */
2949 static void taskbar_add_tab( HWND hwnd )
2951 struct x11drv_win_data *data;
2953 TRACE("hwnd %p\n", hwnd);
2955 data = get_win_data( hwnd );
2956 if (!data)
2957 return;
2959 data->add_taskbar = TRUE;
2960 data->skip_taskbar = FALSE;
2961 update_net_wm_states( data );
2962 release_win_data( data );
2965 /* Delete a window from taskbar */
2966 static void taskbar_delete_tab( HWND hwnd )
2968 struct x11drv_win_data *data;
2970 TRACE("hwnd %p\n", hwnd);
2972 data = get_win_data( hwnd );
2973 if (!data)
2974 return;
2976 data->skip_taskbar = TRUE;
2977 data->add_taskbar = FALSE;
2978 update_net_wm_states( data );
2979 release_win_data( data );
2982 /**********************************************************************
2983 * X11DRV_WindowMessage (X11DRV.@)
2985 LRESULT X11DRV_WindowMessage( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp )
2987 struct x11drv_win_data *data;
2989 switch(msg)
2991 case WM_X11DRV_UPDATE_CLIPBOARD:
2992 return update_clipboard( hwnd );
2993 case WM_X11DRV_SET_WIN_REGION:
2994 if ((data = get_win_data( hwnd )))
2996 sync_window_region( data, (HRGN)1 );
2997 release_win_data( data );
2999 return 0;
3000 case WM_X11DRV_DESKTOP_RESIZED:
3001 if ((data = get_win_data( hwnd )))
3003 /* update the full screen state */
3004 update_net_wm_states( data );
3006 if (data->whole_window)
3008 /* sync window position with the new virtual screen rect */
3009 POINT old_pos = {.x = data->whole_rect.left - wp, .y = data->whole_rect.top - lp};
3010 POINT pos = virtual_screen_to_root( data->whole_rect.left, data->whole_rect.top );
3011 XWindowChanges changes = {.x = pos.x, .y = pos.y};
3012 UINT mask = 0;
3014 if (old_pos.x != pos.x) mask |= CWX;
3015 if (old_pos.y != pos.y) mask |= CWY;
3017 if (mask) XReconfigureWMWindow( data->display, data->whole_window, data->vis.screen, mask, &changes );
3020 release_win_data( data );
3022 return 0;
3023 case WM_X11DRV_SET_CURSOR:
3025 Window win = 0;
3027 if ((data = get_win_data( hwnd )))
3029 win = data->whole_window;
3030 release_win_data( data );
3032 else if (hwnd == x11drv_thread_data()->clip_hwnd)
3033 win = x11drv_thread_data()->clip_window;
3035 if (win)
3037 if (wp == GetCurrentThreadId())
3038 set_window_cursor( win, (HCURSOR)lp );
3039 else
3040 sync_window_cursor( win );
3042 return 0;
3044 case WM_X11DRV_CLIP_CURSOR_NOTIFY:
3045 return clip_cursor_notify( hwnd, (HWND)wp, (HWND)lp );
3046 case WM_X11DRV_CLIP_CURSOR_REQUEST:
3047 return clip_cursor_request( hwnd, (BOOL)wp, (BOOL)lp );
3048 case WM_X11DRV_DELETE_TAB:
3049 taskbar_delete_tab( hwnd );
3050 return 0;
3051 case WM_X11DRV_ADD_TAB:
3052 taskbar_add_tab( hwnd );
3053 return 0;
3054 default:
3055 FIXME( "got window msg %x hwnd %p wp %lx lp %lx\n", msg, hwnd, wp, lp );
3056 return 0;
3061 /***********************************************************************
3062 * is_netwm_supported
3064 static BOOL is_netwm_supported( Display *display, Atom atom )
3066 static Atom *net_supported;
3067 static int net_supported_count = -1;
3068 int i;
3070 if (net_supported_count == -1)
3072 Atom type;
3073 int format;
3074 unsigned long count, remaining;
3076 if (!XGetWindowProperty( display, DefaultRootWindow(display), x11drv_atom(_NET_SUPPORTED), 0,
3077 ~0UL, False, XA_ATOM, &type, &format, &count,
3078 &remaining, (unsigned char **)&net_supported ))
3079 net_supported_count = get_property_size( format, count ) / sizeof(Atom);
3080 else
3081 net_supported_count = 0;
3084 for (i = 0; i < net_supported_count; i++)
3085 if (net_supported[i] == atom) return TRUE;
3086 return FALSE;
3090 /***********************************************************************
3091 * start_screensaver
3093 static LRESULT start_screensaver(void)
3095 if (!is_virtual_desktop())
3097 const char *argv[3] = { "xdg-screensaver", "activate", NULL };
3098 int pid = __wine_unix_spawnvp( (char **)argv, FALSE );
3099 if (pid > 0)
3101 TRACE( "started process %d\n", pid );
3102 return 0;
3105 return -1;
3109 /***********************************************************************
3110 * SysCommand (X11DRV.@)
3112 * Perform WM_SYSCOMMAND handling.
3114 LRESULT X11DRV_SysCommand( HWND hwnd, WPARAM wparam, LPARAM lparam )
3116 WPARAM hittest = wparam & 0x0f;
3117 int dir;
3118 struct x11drv_win_data *data;
3120 if (!(data = get_win_data( hwnd )))
3122 if (wparam == SC_SCREENSAVE && hwnd == NtUserGetDesktopWindow()) return start_screensaver();
3123 return -1;
3125 if (!data->whole_window || !data->managed || !data->mapped) goto failed;
3127 switch (wparam & 0xfff0)
3129 case SC_MOVE:
3130 if (!hittest) dir = _NET_WM_MOVERESIZE_MOVE_KEYBOARD;
3131 else dir = _NET_WM_MOVERESIZE_MOVE;
3132 break;
3133 case SC_SIZE:
3134 /* windows without WS_THICKFRAME are not resizable through the window manager */
3135 if (!(NtUserGetWindowLongW( hwnd, GWL_STYLE ) & WS_THICKFRAME)) goto failed;
3137 switch (hittest)
3139 case WMSZ_LEFT: dir = _NET_WM_MOVERESIZE_SIZE_LEFT; break;
3140 case WMSZ_RIGHT: dir = _NET_WM_MOVERESIZE_SIZE_RIGHT; break;
3141 case WMSZ_TOP: dir = _NET_WM_MOVERESIZE_SIZE_TOP; break;
3142 case WMSZ_TOPLEFT: dir = _NET_WM_MOVERESIZE_SIZE_TOPLEFT; break;
3143 case WMSZ_TOPRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_TOPRIGHT; break;
3144 case WMSZ_BOTTOM: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOM; break;
3145 case WMSZ_BOTTOMLEFT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT; break;
3146 case WMSZ_BOTTOMRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT; break;
3147 case 9: dir = _NET_WM_MOVERESIZE_MOVE; break;
3148 default: dir = _NET_WM_MOVERESIZE_SIZE_KEYBOARD; break;
3150 break;
3152 case SC_KEYMENU:
3153 /* prevent a simple ALT press+release from activating the system menu,
3154 * as that can get confusing on managed windows */
3155 if ((WCHAR)lparam) goto failed; /* got an explicit char */
3156 if (NtUserGetWindowLongPtrW( hwnd, GWLP_ID )) goto failed; /* window has a real menu */
3157 if (!(NtUserGetWindowLongW( hwnd, GWL_STYLE ) & WS_SYSMENU)) goto failed; /* no system menu */
3158 TRACE( "ignoring SC_KEYMENU wp %lx lp %lx\n", wparam, lparam );
3159 release_win_data( data );
3160 return 0;
3162 default:
3163 goto failed;
3166 if (NtUserGetWindowLongW( hwnd, GWL_STYLE ) & WS_MAXIMIZE) goto failed;
3168 if (!is_netwm_supported( data->display, x11drv_atom(_NET_WM_MOVERESIZE) ))
3170 TRACE( "_NET_WM_MOVERESIZE not supported\n" );
3171 goto failed;
3174 release_win_data( data );
3175 move_resize_window( hwnd, dir );
3176 return 0;
3178 failed:
3179 release_win_data( data );
3180 return -1;
3183 void X11DRV_FlashWindowEx( FLASHWINFO *pfinfo )
3185 struct x11drv_win_data *data = get_win_data( pfinfo->hwnd );
3186 XEvent xev;
3188 if (!data)
3189 return;
3191 if (data->mapped)
3193 xev.type = ClientMessage;
3194 xev.xclient.window = data->whole_window;
3195 xev.xclient.message_type = x11drv_atom( _NET_WM_STATE );
3196 xev.xclient.serial = 0;
3197 xev.xclient.display = data->display;
3198 xev.xclient.send_event = True;
3199 xev.xclient.format = 32;
3200 xev.xclient.data.l[0] = pfinfo->dwFlags ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
3201 xev.xclient.data.l[1] = x11drv_atom( _NET_WM_STATE_DEMANDS_ATTENTION );
3202 xev.xclient.data.l[2] = 0;
3203 xev.xclient.data.l[3] = 1;
3204 xev.xclient.data.l[4] = 0;
3206 XSendEvent( data->display, DefaultRootWindow( data->display ), False,
3207 SubstructureNotifyMask, &xev );
3209 release_win_data( data );
3212 void init_win_context(void)
3214 init_recursive_mutex( &win_data_mutex );
3216 winContext = XUniqueContext();
3217 win_data_context = XUniqueContext();
3218 cursor_context = XUniqueContext();