forked from wine-mirror/wine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindow.c
3388 lines (2854 loc) · 116 KB
/
window.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Window related functions
*
* Copyright 1993, 1994, 1995, 1996, 2001 Alexandre Julliard
* Copyright 1993 David Metcalfe
* Copyright 1995, 1996 Alex Korobka
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#if 0
#pragma makedep unix
#endif
#include "config.h"
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <X11/Xlib.h>
#include <X11/Xresource.h>
#include <X11/Xutil.h>
#ifdef HAVE_LIBXSHAPE
#include <X11/extensions/shape.h>
#endif /* HAVE_LIBXSHAPE */
#ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
#include <X11/extensions/XInput2.h>
#endif
/* avoid conflict with field names in included win32 headers */
#undef Status
#include "ntstatus.h"
#define WIN32_NO_STATUS
#include "x11drv.h"
#include "wingdi.h"
#include "winuser.h"
#include "wine/debug.h"
#include "wine/server.h"
#include "mwm.h"
WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
WINE_DECLARE_DEBUG_CHANNEL(systray);
#define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0
#define _NET_WM_MOVERESIZE_SIZE_TOP 1
#define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2
#define _NET_WM_MOVERESIZE_SIZE_RIGHT 3
#define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4
#define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5
#define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6
#define _NET_WM_MOVERESIZE_SIZE_LEFT 7
#define _NET_WM_MOVERESIZE_MOVE 8
#define _NET_WM_MOVERESIZE_SIZE_KEYBOARD 9
#define _NET_WM_MOVERESIZE_MOVE_KEYBOARD 10
#define _NET_WM_STATE_REMOVE 0
#define _NET_WM_STATE_ADD 1
#define _NET_WM_STATE_TOGGLE 2
#define SYSTEM_TRAY_REQUEST_DOCK 0
#define SYSTEM_TRAY_BEGIN_MESSAGE 1
#define SYSTEM_TRAY_CANCEL_MESSAGE 2
static const unsigned int net_wm_state_atoms[NB_NET_WM_STATES] =
{
XATOM__KDE_NET_WM_STATE_SKIP_SWITCHER,
XATOM__NET_WM_STATE_FULLSCREEN,
XATOM__NET_WM_STATE_ABOVE,
XATOM__NET_WM_STATE_MAXIMIZED_VERT,
XATOM__NET_WM_STATE_SKIP_PAGER,
XATOM__NET_WM_STATE_SKIP_TASKBAR
};
#define SWP_AGG_NOPOSCHANGE (SWP_NOSIZE | SWP_NOMOVE | SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE | SWP_NOZORDER)
/* is cursor clipping active? */
BOOL clipping_cursor = FALSE;
/* X context to associate a hwnd to an X window */
XContext winContext = 0;
/* X context to associate a struct x11drv_win_data to an hwnd */
static XContext win_data_context = 0;
static XContext host_window_context = 0;
/* time of last user event and window where it's stored */
static Time last_user_time;
static Window user_time_window;
static const WCHAR whole_window_prop[] =
{'_','_','w','i','n','e','_','x','1','1','_','w','h','o','l','e','_','w','i','n','d','o','w',0};
static const WCHAR clip_window_prop[] =
{'_','_','w','i','n','e','_','x','1','1','_','c','l','i','p','_','w','i','n','d','o','w',0};
static pthread_mutex_t win_data_mutex = PTHREAD_MUTEX_INITIALIZER;
static void host_window_add_ref( struct host_window *win )
{
int ref = ++win->refcount;
TRACE( "host window %p/%lx increasing refcount to %d\n", win, win->window, ref );
}
static void host_window_release( struct host_window *win )
{
int ref = --win->refcount;
TRACE( "host window %p/%lx decreasing refcount to %d\n", win, win->window, ref );
if (!ref)
{
struct x11drv_thread_data *data = x11drv_thread_data();
if (!win->destroyed) XSelectInput( data->display, win->window, 0 );
XDeleteContext( data->display, win->window, host_window_context );
if (win->parent) host_window_release( win->parent );
free( win->children );
free( win );
}
}
POINT host_window_map_point( struct host_window *win, int x, int y )
{
POINT pos = {x, y};
while (win)
{
pos.x += win->rect.left;
pos.y += win->rect.top;
win = win->parent;
}
return pos;
}
static unsigned int find_host_window_child( struct host_window *win, Window child )
{
unsigned int i;
for (i = 0; i < win->children_count; i++) if (win->children[i].window == child) break;
return i;
}
static int host_window_error( Display *display, XErrorEvent *event, void *arg )
{
return (event->error_code == BadWindow);
}
struct host_window *get_host_window( Window window, BOOL create )
{
struct x11drv_thread_data *data = x11drv_thread_data();
Window xparent = 0, xroot, *xchildren;
struct host_window *win;
XWindowAttributes attr;
unsigned int nchildren;
if (window == root_window) return NULL;
if (!XFindContext( data->display, window, host_window_context, (XPointer *)&win )) return win;
if (!create || !(win = calloc( 1, sizeof(*win) ))) return NULL;
win->window = window;
X11DRV_expect_error( data->display, host_window_error, NULL );
XSelectInput( data->display, window, StructureNotifyMask );
if (!XGetWindowAttributes( data->display, window, &attr )) memset( &attr, 0, sizeof(attr) );
if (!XQueryTree( data->display, window, &xroot, &xparent, &xchildren, &nchildren )) xparent = root_window;
else XFree( xchildren );
if (X11DRV_check_error()) WARN( "window %lx already destroyed\n", window );
host_window_set_parent( win, xparent );
SetRect( &win->rect, attr.x, attr.y, attr.x + attr.width, attr.y + attr.height );
if (win->parent) host_window_configure_child( win->parent, win->window, win->rect, FALSE );
TRACE( "created host window %p/%lx, parent %lx rect %s\n", win, win->window,
xparent, wine_dbgstr_rect(&win->rect) );
XSaveContext( data->display, window, host_window_context, (char *)win );
return win;
}
static void host_window_reparent( struct host_window **win, Window parent, Window window )
{
struct host_window *old = *win, *new = get_host_window( parent, TRUE );
unsigned int index;
RECT rect = {0};
void *tmp;
if ((*win = new)) host_window_add_ref( new );
if (old && (index = find_host_window_child( old, window )) < old->children_count)
{
rect = old->children[index].rect;
old->children[index] = old->children[old->children_count - 1];
old->children_count--;
}
if (new && (index = find_host_window_child( new, window )) == new->children_count)
{
if (!(tmp = realloc( new->children, (index + 1) * sizeof(*new->children) ))) return;
new->children = tmp;
OffsetRect( &rect, -rect.left, -rect.top );
new->children[index].window = window;
new->children[index].rect = rect;
new->children_count++;
}
if (old) host_window_release( old );
}
RECT host_window_configure_child( struct host_window *win, Window window, RECT rect, BOOL root_coords )
{
unsigned int index;
if (root_coords)
{
POINT offset = host_window_map_point( win, 0, 0 );
OffsetRect( &rect, -offset.x, -offset.y );
}
index = find_host_window_child( win, window );
if (index < win->children_count) win->children[index].rect = rect;
return rect;
}
void host_window_set_parent( struct host_window *win, Window parent )
{
TRACE( "host window %p/%lx, parent %lx\n", win, win->window, parent );
host_window_reparent( &win->parent, parent, win->window );
}
/***********************************************************************
* http://standards.freedesktop.org/startup-notification-spec
*/
static void remove_startup_notification(Display *display, Window window)
{
static LONG startup_notification_removed = 0;
char message[1024];
const char *id;
int i;
int pos;
XEvent xevent;
const char *src;
int srclen;
if (InterlockedCompareExchange(&startup_notification_removed, 1, 0) != 0)
return;
if (!(id = getenv( "DESKTOP_STARTUP_ID" )) || !id[0]) return;
if ((src = strstr( id, "_TIME" ))) update_user_time( atol( src + 5 ));
pos = snprintf(message, sizeof(message), "remove: ID=");
message[pos++] = '"';
for (i = 0; id[i] && pos < sizeof(message) - 3; i++)
{
if (id[i] == '"' || id[i] == '\\')
message[pos++] = '\\';
message[pos++] = id[i];
}
message[pos++] = '"';
message[pos++] = '\0';
unsetenv( "DESKTOP_STARTUP_ID" );
xevent.xclient.type = ClientMessage;
xevent.xclient.message_type = x11drv_atom(_NET_STARTUP_INFO_BEGIN);
xevent.xclient.display = display;
xevent.xclient.window = window;
xevent.xclient.format = 8;
src = message;
srclen = strlen(src) + 1;
while (srclen > 0)
{
int msglen = srclen;
if (msglen > 20)
msglen = 20;
memset(&xevent.xclient.data.b[0], 0, 20);
memcpy(&xevent.xclient.data.b[0], src, msglen);
src += msglen;
srclen -= msglen;
XSendEvent( display, DefaultRootWindow( display ), False, PropertyChangeMask, &xevent );
xevent.xclient.message_type = x11drv_atom(_NET_STARTUP_INFO);
}
}
static BOOL is_managed( HWND hwnd )
{
struct x11drv_win_data *data = get_win_data( hwnd );
BOOL ret = data && data->managed;
release_win_data( data );
return ret;
}
HWND *build_hwnd_list(void)
{
NTSTATUS status;
HWND *list;
ULONG count = 128;
for (;;)
{
if (!(list = malloc( count * sizeof(*list) ))) return NULL;
status = NtUserBuildHwndList( 0, 0, 0, 0, 0, count, list, &count );
if (!status) return list;
free( list );
if (status != STATUS_BUFFER_TOO_SMALL) return NULL;
}
}
static BOOL has_owned_popups( HWND hwnd )
{
HWND *list;
UINT i;
BOOL ret = FALSE;
if (!(list = build_hwnd_list())) return FALSE;
for (i = 0; list[i] != HWND_BOTTOM; i++)
{
if (list[i] == hwnd) break; /* popups are always above owner */
if (NtUserGetWindowRelative( list[i], GW_OWNER ) != hwnd) continue;
if ((ret = is_managed( list[i] ))) break;
}
free( list );
return ret;
}
/***********************************************************************
* alloc_win_data
*/
static struct x11drv_win_data *alloc_win_data( Display *display, HWND hwnd )
{
struct x11drv_win_data *data;
if ((data = calloc( 1, sizeof(*data) )))
{
data->display = display;
data->vis = default_visual;
data->hwnd = hwnd;
pthread_mutex_lock( &win_data_mutex );
XSaveContext( gdi_display, (XID)hwnd, win_data_context, (char *)data );
}
return data;
}
/***********************************************************************
* is_window_managed
*
* Check if a given window should be managed
*/
static BOOL is_window_managed( HWND hwnd, UINT swp_flags, const RECT *window_rect )
{
DWORD style, ex_style;
if (!managed_mode) return FALSE;
/* child windows are not managed */
style = NtUserGetWindowLongW( hwnd, GWL_STYLE );
if ((style & (WS_CHILD|WS_POPUP)) == WS_CHILD) return FALSE;
/* activated windows are managed */
if (!(swp_flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW))) return TRUE;
if (hwnd == get_active_window()) return TRUE;
/* windows with caption are managed */
if ((style & WS_CAPTION) == WS_CAPTION) return TRUE;
/* windows with thick frame are managed */
if (style & WS_THICKFRAME) return TRUE;
if (style & WS_POPUP)
{
HMONITOR hmon;
MONITORINFO mi;
/* popup with sysmenu == caption are managed */
if (style & WS_SYSMENU) return TRUE;
/* full-screen popup windows are managed */
hmon = NtUserMonitorFromWindow( hwnd, MONITOR_DEFAULTTOPRIMARY );
mi.cbSize = sizeof( mi );
NtUserGetMonitorInfo( hmon, &mi );
if (window_rect->left <= mi.rcWork.left && window_rect->right >= mi.rcWork.right &&
window_rect->top <= mi.rcWork.top && window_rect->bottom >= mi.rcWork.bottom)
return TRUE;
}
/* application windows are managed */
ex_style = NtUserGetWindowLongW( hwnd, GWL_EXSTYLE );
if (ex_style & WS_EX_APPWINDOW) return TRUE;
/* windows that own popups are managed */
if (has_owned_popups( hwnd )) return TRUE;
/* default: not managed */
return FALSE;
}
/***********************************************************************
* is_window_resizable
*
* Check if window should be made resizable by the window manager
*/
static inline BOOL is_window_resizable( struct x11drv_win_data *data, DWORD style )
{
if (style & WS_THICKFRAME) return TRUE;
/* Metacity needs the window to be resizable to make it fullscreen */
return data->is_fullscreen;
}
/***********************************************************************
* get_mwm_decorations
*/
static unsigned long get_mwm_decorations_for_style( DWORD style, DWORD ex_style )
{
unsigned long ret = 0;
if (ex_style & WS_EX_TOOLWINDOW) return 0;
if (ex_style & WS_EX_LAYERED) return 0;
if ((style & WS_CAPTION) == WS_CAPTION)
{
ret |= MWM_DECOR_TITLE | MWM_DECOR_BORDER;
if (style & WS_SYSMENU) ret |= MWM_DECOR_MENU;
if (style & WS_MINIMIZEBOX) ret |= MWM_DECOR_MINIMIZE;
if (style & WS_MAXIMIZEBOX) ret |= MWM_DECOR_MAXIMIZE;
}
if (ex_style & WS_EX_DLGMODALFRAME) ret |= MWM_DECOR_BORDER;
else if (style & WS_THICKFRAME) ret |= MWM_DECOR_BORDER | MWM_DECOR_RESIZEH;
else if ((style & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME) ret |= MWM_DECOR_BORDER;
return ret;
}
/***********************************************************************
* get_mwm_decorations
*/
static unsigned long get_mwm_decorations( struct x11drv_win_data *data, DWORD style, DWORD ex_style )
{
if (EqualRect( &data->rects.window, &data->rects.visible )) return 0;
return get_mwm_decorations_for_style( style, ex_style );
}
/***********************************************************************
* get_window_attributes
*
* Fill the window attributes structure for an X window.
*/
static int get_window_attributes( struct x11drv_win_data *data, XSetWindowAttributes *attr )
{
attr->override_redirect = !data->managed;
attr->colormap = data->whole_colormap ? data->whole_colormap : default_colormap;
attr->save_under = ((NtUserGetClassLongW( data->hwnd, GCL_STYLE ) & CS_SAVEBITS) != 0);
attr->bit_gravity = NorthWestGravity;
attr->backing_store = NotUseful;
attr->border_pixel = 0;
attr->event_mask = (ExposureMask | PointerMotionMask |
ButtonPressMask | ButtonReleaseMask | EnterWindowMask |
KeyPressMask | KeyReleaseMask | FocusChangeMask |
KeymapStateMask | StructureNotifyMask | PropertyChangeMask);
return (CWOverrideRedirect | CWSaveUnder | CWColormap | CWBorderPixel |
CWEventMask | CWBitGravity | CWBackingStore);
}
/***********************************************************************
* sync_window_style
*
* Change the X window attributes when the window style has changed.
*/
static void sync_window_style( struct x11drv_win_data *data )
{
if (data->whole_window != root_window && !data->embedded)
{
XSetWindowAttributes attr;
int mask = get_window_attributes( data, &attr );
XChangeWindowAttributes( data->display, data->whole_window, mask, &attr );
x11drv_xinput2_enable( data->display, data->whole_window );
}
}
static void sync_empty_window_shape( struct x11drv_win_data *data, struct window_surface *surface )
{
#ifdef HAVE_LIBXSHAPE
if (IsRectEmpty( &data->rects.window )) /* set an empty shape */
{
static XRectangle empty_rect;
XShapeCombineRectangles( data->display, data->whole_window, ShapeBounding, 0, 0,
&empty_rect, 1, ShapeSet, YXBanded );
}
else
{
XShapeCombineMask( gdi_display, data->whole_window, ShapeBounding, 0, 0, None, ShapeSet );
/* invalidate surface shape to make sure it gets updated again */
if (surface) window_surface_set_shape( surface, 0 );
}
#endif
}
/***********************************************************************
* sync_window_region
*
* Update the X11 window region.
*/
static void sync_window_region( struct x11drv_win_data *data, HRGN win_region )
{
#ifdef HAVE_LIBXSHAPE
HRGN hrgn = win_region;
if (!data->whole_window) return;
if (client_side_graphics) return; /* use surface shape instead */
data->shaped = FALSE;
if (hrgn == (HRGN)1) /* hack: win_region == 1 means retrieve region from server */
{
if (!(hrgn = NtGdiCreateRectRgn( 0, 0, 0, 0 ))) return;
if (NtUserGetWindowRgnEx( data->hwnd, hrgn, 0 ) == ERROR)
{
NtGdiDeleteObjectApp( hrgn );
hrgn = 0;
}
}
if (!hrgn)
{
XShapeCombineMask( data->display, data->whole_window, ShapeBounding, 0, 0, None, ShapeSet );
}
else
{
RGNDATA *pRegionData;
if (NtUserGetWindowLongW( data->hwnd, GWL_EXSTYLE ) & WS_EX_LAYOUTRTL)
NtUserMirrorRgn( data->hwnd, hrgn );
if ((pRegionData = X11DRV_GetRegionData( hrgn, 0 )))
{
XShapeCombineRectangles( data->display, data->whole_window, ShapeBounding,
data->rects.window.left - data->rects.visible.left,
data->rects.window.top - data->rects.visible.top,
(XRectangle *)pRegionData->Buffer,
pRegionData->rdh.nCount, ShapeSet, YXBanded );
free( pRegionData );
data->shaped = TRUE;
}
}
if (hrgn && hrgn != win_region) NtGdiDeleteObjectApp( hrgn );
#endif /* HAVE_LIBXSHAPE */
}
/***********************************************************************
* sync_window_opacity
*/
static void sync_window_opacity( Display *display, Window win, BYTE alpha, DWORD flags )
{
unsigned long opacity = 0xffffffff;
if (flags & LWA_ALPHA) opacity = (0xffffffff / 0xff) * alpha;
if (opacity == 0xffffffff)
XDeleteProperty( display, win, x11drv_atom(_NET_WM_WINDOW_OPACITY) );
else
XChangeProperty( display, win, x11drv_atom(_NET_WM_WINDOW_OPACITY),
XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&opacity, 1 );
}
/***********************************************************************
* sync_window_text
*/
static void sync_window_text( Display *display, Window win, const WCHAR *text )
{
DWORD count, len;
char *buffer, *utf8_buffer;
XTextProperty prop;
/* allocate new buffer for window text */
len = lstrlenW( text );
count = len * 3 + 1;
if (!(buffer = malloc( count ))) return;
ntdll_wcstoumbs( text, len + 1, buffer, count, FALSE );
RtlUnicodeToUTF8N( NULL, 0, &count, text, len * sizeof(WCHAR) );
if (!(utf8_buffer = malloc( count )))
{
free( buffer );
return;
}
RtlUnicodeToUTF8N( utf8_buffer, count, &count, text, len * sizeof(WCHAR) );
if (XmbTextListToTextProperty( display, &buffer, 1, XStdICCTextStyle, &prop ) == Success)
{
XSetWMName( display, win, &prop );
XSetWMIconName( display, win, &prop );
XFree( prop.value );
}
/*
Implements a NET_WM UTF-8 title. It should be without a trailing \0,
according to the standard
( http://www.pps.jussieu.fr/~jch/software/UTF8_STRING/UTF8_STRING.text ).
*/
XChangeProperty( display, win, x11drv_atom(_NET_WM_NAME), x11drv_atom(UTF8_STRING),
8, PropModeReplace, (unsigned char *) utf8_buffer, count);
free( utf8_buffer );
free( buffer );
}
/***********************************************************************
* get_bitmap_argb
*
* Return the bitmap bits in ARGB format. Helper for setting icon hints.
*/
static unsigned long *get_bitmap_argb( HDC hdc, HBITMAP color, HBITMAP mask, unsigned int *size )
{
char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
BITMAPINFO *info = (BITMAPINFO *)buffer;
BITMAP bm;
unsigned long *bits = NULL;
unsigned int *ptr;
unsigned char *mask_bits = NULL;
int i, j;
BOOL has_alpha = FALSE;
if (!NtGdiExtGetObjectW( color, sizeof(bm), &bm )) return NULL;
info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
info->bmiHeader.biWidth = bm.bmWidth;
info->bmiHeader.biHeight = -bm.bmHeight;
info->bmiHeader.biPlanes = 1;
info->bmiHeader.biBitCount = 32;
info->bmiHeader.biCompression = BI_RGB;
info->bmiHeader.biSizeImage = bm.bmWidth * bm.bmHeight * 4;
info->bmiHeader.biXPelsPerMeter = 0;
info->bmiHeader.biYPelsPerMeter = 0;
info->bmiHeader.biClrUsed = 0;
info->bmiHeader.biClrImportant = 0;
*size = bm.bmWidth * bm.bmHeight + 2;
if (!(bits = malloc( *size * sizeof(*bits) ))) goto failed;
ptr = (unsigned int *)bits;
if (!NtGdiGetDIBitsInternal( hdc, color, 0, bm.bmHeight, ptr + 2, info, DIB_RGB_COLORS, 0, 0 ))
goto failed;
ptr[0] = bm.bmWidth;
ptr[1] = bm.bmHeight;
for (i = 0; i < bm.bmWidth * bm.bmHeight; i++)
if ((has_alpha = (ptr[i + 2] & 0xff000000) != 0)) break;
if (!has_alpha)
{
unsigned int width_bytes = (bm.bmWidth + 31) / 32 * 4;
/* generate alpha channel from the mask */
info->bmiHeader.biBitCount = 1;
info->bmiHeader.biSizeImage = width_bytes * bm.bmHeight;
if (!(mask_bits = malloc( info->bmiHeader.biSizeImage ))) goto failed;
if (!NtGdiGetDIBitsInternal( hdc, mask, 0, bm.bmHeight, mask_bits, info, DIB_RGB_COLORS, 0, 0 ))
goto failed;
ptr = (unsigned int *)bits + 2;
for (i = 0; i < bm.bmHeight; i++)
for (j = 0; j < bm.bmWidth; j++, ptr++)
if (!((mask_bits[i * width_bytes + j / 8] << (j % 8)) & 0x80)) *ptr |= 0xff000000;
free( mask_bits );
}
/* convert to array of longs */
if (bits && sizeof(long) > sizeof(int))
{
ptr = (unsigned int *)bits;
for (i = *size - 1; i >= 0; i--) bits[i] = ptr[i];
}
return bits;
failed:
free( bits );
free( mask_bits );
return NULL;
}
/***********************************************************************
* create_icon_pixmaps
*/
static BOOL create_icon_pixmaps( HDC hdc, const ICONINFO *icon, Pixmap *icon_ret, Pixmap *mask_ret )
{
char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
BITMAPINFO *info = (BITMAPINFO *)buffer;
XVisualInfo vis = default_visual;
struct gdi_image_bits bits;
Pixmap color_pixmap = 0, mask_pixmap = 0;
int lines;
unsigned int i;
bits.ptr = NULL;
bits.free = NULL;
bits.is_copy = TRUE;
info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
info->bmiHeader.biBitCount = 0;
if (!(lines = NtGdiGetDIBitsInternal( hdc, icon->hbmColor, 0, 0, NULL, info, DIB_RGB_COLORS, 0, 0 )))
goto failed;
if (!(bits.ptr = malloc( info->bmiHeader.biSizeImage ))) goto failed;
if (!NtGdiGetDIBitsInternal( hdc, icon->hbmColor, 0, lines, bits.ptr, info, DIB_RGB_COLORS, 0, 0 ))
goto failed;
color_pixmap = create_pixmap_from_image( hdc, &vis, info, &bits, DIB_RGB_COLORS );
free( bits.ptr );
bits.ptr = NULL;
if (!color_pixmap) goto failed;
info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
info->bmiHeader.biBitCount = 0;
if (!(lines = NtGdiGetDIBitsInternal( hdc, icon->hbmMask, 0, 0, NULL, info, DIB_RGB_COLORS, 0, 0 )))
goto failed;
if (!(bits.ptr = malloc( info->bmiHeader.biSizeImage ))) goto failed;
if (!NtGdiGetDIBitsInternal( hdc, icon->hbmMask, 0, lines, bits.ptr, info, DIB_RGB_COLORS, 0, 0 ))
goto failed;
/* invert the mask */
for (i = 0; i < info->bmiHeader.biSizeImage / sizeof(DWORD); i++) ((DWORD *)bits.ptr)[i] ^= ~0u;
vis.depth = 1;
mask_pixmap = create_pixmap_from_image( hdc, &vis, info, &bits, DIB_RGB_COLORS );
free( bits.ptr );
bits.ptr = NULL;
if (!mask_pixmap) goto failed;
*icon_ret = color_pixmap;
*mask_ret = mask_pixmap;
return TRUE;
failed:
if (color_pixmap) XFreePixmap( gdi_display, color_pixmap );
free( bits.ptr );
return FALSE;
}
static HICON get_icon_info( HICON icon, ICONINFO *ii )
{
return icon && NtUserGetIconInfo( icon, ii, NULL, NULL, NULL, 0 ) ? icon : NULL;
}
/***********************************************************************
* fetch_icon_data
*/
static void fetch_icon_data( HWND hwnd, HICON icon_big, HICON icon_small )
{
struct x11drv_win_data *data;
ICONINFO ii, ii_small;
HDC hDC;
unsigned int size;
unsigned long *bits;
Pixmap icon_pixmap, mask_pixmap;
icon_big = get_icon_info( icon_big, &ii );
if (!icon_big)
{
icon_big = get_icon_info( (HICON)send_message( hwnd, WM_GETICON, ICON_BIG, 0 ), &ii );
if (!icon_big)
icon_big = get_icon_info( (HICON)NtUserGetClassLongPtrW( hwnd, GCLP_HICON ), &ii );
if (!icon_big)
{
icon_big = LoadImageW( 0, (const WCHAR *)IDI_WINLOGO, IMAGE_ICON, 0, 0,
LR_SHARED | LR_DEFAULTSIZE );
icon_big = get_icon_info( icon_big, &ii );
}
}
icon_small = get_icon_info( icon_small, &ii_small );
if (!icon_small)
{
icon_small = get_icon_info( (HICON)send_message( hwnd, WM_GETICON, ICON_SMALL, 0 ), &ii_small );
if (!icon_small)
icon_small = get_icon_info( (HICON)NtUserGetClassLongPtrW( hwnd, GCLP_HICONSM ), &ii_small );
}
if (!icon_big) return;
hDC = NtGdiCreateCompatibleDC(0);
bits = get_bitmap_argb( hDC, ii.hbmColor, ii.hbmMask, &size );
if (bits && icon_small)
{
unsigned int size_small;
unsigned long *bits_small, *new;
if ((bits_small = get_bitmap_argb( hDC, ii_small.hbmColor, ii_small.hbmMask, &size_small )) &&
(bits_small[0] != bits[0] || bits_small[1] != bits[1])) /* size must be different */
{
if ((new = realloc( bits, (size + size_small) * sizeof(unsigned long) )))
{
bits = new;
memcpy( bits + size, bits_small, size_small * sizeof(unsigned long) );
size += size_small;
}
}
free( bits_small );
NtGdiDeleteObjectApp( ii_small.hbmColor );
NtGdiDeleteObjectApp( ii_small.hbmMask );
}
if (!create_icon_pixmaps( hDC, &ii, &icon_pixmap, &mask_pixmap )) icon_pixmap = mask_pixmap = 0;
NtGdiDeleteObjectApp( ii.hbmColor );
NtGdiDeleteObjectApp( ii.hbmMask );
NtGdiDeleteObjectApp( hDC );
if ((data = get_win_data( hwnd )))
{
if (data->icon_pixmap) XFreePixmap( gdi_display, data->icon_pixmap );
if (data->icon_mask) XFreePixmap( gdi_display, data->icon_mask );
free( data->icon_bits );
data->icon_pixmap = icon_pixmap;
data->icon_mask = mask_pixmap;
data->icon_bits = bits;
data->icon_size = size;
release_win_data( data );
}
else
{
if (icon_pixmap) XFreePixmap( gdi_display, icon_pixmap );
if (mask_pixmap) XFreePixmap( gdi_display, mask_pixmap );
free( bits );
}
}
/***********************************************************************
* set_size_hints
*
* set the window size hints
*/
static void set_size_hints( struct x11drv_win_data *data, DWORD style )
{
XSizeHints* size_hints;
if (!(size_hints = XAllocSizeHints())) return;
size_hints->win_gravity = StaticGravity;
size_hints->flags |= PWinGravity;
/* don't update size hints if window is not in normal state */
if (!(style & (WS_MINIMIZE | WS_MAXIMIZE)))
{
if (data->hwnd != NtUserGetDesktopWindow()) /* don't force position of desktop */
{
POINT pt = virtual_screen_to_root( data->rects.visible.left, data->rects.visible.top );
size_hints->x = pt.x;
size_hints->y = pt.y;
size_hints->flags |= PPosition;
}
else size_hints->win_gravity = NorthWestGravity;
if (!is_window_resizable( data, style ))
{
size_hints->max_width = data->rects.visible.right - data->rects.visible.left;
size_hints->max_height = data->rects.visible.bottom - data->rects.visible.top;
if (size_hints->max_width <= 0 ||size_hints->max_height <= 0)
size_hints->max_width = size_hints->max_height = 1;
size_hints->min_width = size_hints->max_width;
size_hints->min_height = size_hints->max_height;
size_hints->flags |= PMinSize | PMaxSize;
}
}
XSetWMNormalHints( data->display, data->whole_window, size_hints );
XFree( size_hints );
}
/***********************************************************************
* set_mwm_hints
*/
static void set_mwm_hints( struct x11drv_win_data *data, UINT style, UINT ex_style )
{
MwmHints mwm_hints;
if (data->hwnd == NtUserGetDesktopWindow())
{
mwm_hints.functions = MWM_FUNC_MOVE | MWM_FUNC_MINIMIZE | MWM_FUNC_CLOSE;
if (is_desktop_fullscreen())
{
mwm_hints.decorations = 0;
mwm_hints.functions |= MWM_FUNC_RESIZE; /* some WMs need this to make it fullscreen */
}
else mwm_hints.decorations = MWM_DECOR_TITLE | MWM_DECOR_BORDER | MWM_DECOR_MENU | MWM_DECOR_MINIMIZE;
}
else
{
mwm_hints.decorations = get_mwm_decorations( data, style, ex_style );
mwm_hints.functions = MWM_FUNC_MOVE;
if (is_window_resizable( data, style )) mwm_hints.functions |= MWM_FUNC_RESIZE;
if (!(style & WS_DISABLED))
{
mwm_hints.functions |= MWM_FUNC_CLOSE;
if (style & WS_MINIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MINIMIZE;
if (style & WS_MAXIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MAXIMIZE;
/* The window can be programmatically minimized even without
a minimize box button. Allow the WM to restore it. */
if (style & WS_MINIMIZE) mwm_hints.functions |= MWM_FUNC_MINIMIZE | MWM_FUNC_MAXIMIZE;
}
}
TRACE( "%p setting mwm hints to %lx,%lx (style %x exstyle %x)\n",
data->hwnd, mwm_hints.decorations, mwm_hints.functions, style, ex_style );
mwm_hints.flags = MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
mwm_hints.input_mode = 0;
mwm_hints.status = 0;
XChangeProperty( data->display, data->whole_window, x11drv_atom(_MOTIF_WM_HINTS),
x11drv_atom(_MOTIF_WM_HINTS), 32, PropModeReplace,
(unsigned char*)&mwm_hints, sizeof(mwm_hints)/sizeof(long) );
}
/***********************************************************************
* set_style_hints
*/
static void set_style_hints( struct x11drv_win_data *data, DWORD style, DWORD ex_style )
{
Window group_leader = data->whole_window;
HWND owner = NtUserGetWindowRelative( data->hwnd, GW_OWNER );
Window owner_win = 0;
XWMHints *wm_hints;
Atom window_type;
if (owner)
{
owner = NtUserGetAncestor( owner, GA_ROOT );
owner_win = X11DRV_get_whole_window( owner );
}
if (owner_win)
{
XSetTransientForHint( data->display, data->whole_window, owner_win );
group_leader = owner_win;
}
/* Only use dialog type for owned popups. Metacity allows making fullscreen
* only normal windows, and doesn't handle correctly TRANSIENT_FOR hint for
* dialogs owned by fullscreen windows.
*/
if (((style & WS_POPUP) || (ex_style & WS_EX_DLGMODALFRAME)) && owner)
window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG);
else
window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
XChangeProperty(data->display, data->whole_window, x11drv_atom(_NET_WM_WINDOW_TYPE),
XA_ATOM, 32, PropModeReplace, (unsigned char*)&window_type, 1);
if ((wm_hints = XAllocWMHints()))
{
wm_hints->flags = InputHint | StateHint | WindowGroupHint;
wm_hints->input = !use_take_focus && !(style & WS_DISABLED);
wm_hints->initial_state = (style & WS_MINIMIZE) ? IconicState : NormalState;
wm_hints->window_group = group_leader;
if (data->icon_pixmap)
{
wm_hints->icon_pixmap = data->icon_pixmap;
wm_hints->icon_mask = data->icon_mask;
wm_hints->flags |= IconPixmapHint | IconMaskHint;
}
XSetWMHints( data->display, data->whole_window, wm_hints );
XFree( wm_hints );
}
if (data->icon_bits)
XChangeProperty( data->display, data->whole_window, x11drv_atom(_NET_WM_ICON),
XA_CARDINAL, 32, PropModeReplace,
(unsigned char *)data->icon_bits, data->icon_size );
else
XDeleteProperty( data->display, data->whole_window, x11drv_atom(_NET_WM_ICON) );
}
/***********************************************************************
* set_initial_wm_hints
*
* Set the window manager hints that don't change over the lifetime of a window.
*/
static void set_initial_wm_hints( Display *display, Window window )
{