summaryrefslogtreecommitdiff
path: root/managed.c
blob: 1e735132e5bf6b6bf4a7c657df82a1509a74e965 (plain)
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
/*
 * managed.c
 *
 * Part of gwm, the Gratuitous Window Manager,
 *     by Gary Wong, <gtw@gnu.org>.
 *
 * Copyright (C) 2009  Gary Wong
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of version 3 of the GNU General Public License as
 *  published by the Free Software Foundation.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * $Id$
 */

#include <config.h>

#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <xcb/xcb.h>
#if USE_SHAPE
#include <xcb/shape.h>
#endif

#include "gwm.h"

#include "frame.h"
#include "managed.h"
#include "utf8.h"
#include "window-table.h"

extern void set_managed_state( struct gwm_window *window, int state ) {

    uint32_t values[ 2 ];

    assert( window->type == WINDOW_MANAGED );
    
    /* Place a WM_STATE property on the client window (ICCCM 2.0, section
       4.1.3.1). */
    values[ 0 ] = window->u.managed.state = state;
    values[ 1 ] = XCB_NONE;
    xcb_change_property( c, XCB_PROP_MODE_REPLACE, window->w,
			 atoms[ ATOM_WM_STATE ], atoms[ ATOM_WM_STATE ],
			 32, 2, values );
}

extern void iconic_to_normal( struct gwm_window *window ) {

    assert( window->type == WINDOW_MANAGED );
    
    set_managed_state( window, STATE_NORMAL );
    xcb_map_window( c, window->w );
    xcb_map_window( c, window->u.managed.frame->w );
}

extern void normal_to_iconic( struct gwm_window *window ) {
    
    uint32_t n;
    
    assert( window->type == WINDOW_MANAGED );

    set_managed_state( window, STATE_ICONIC );
    
    /* We need to unmap our child without invoking the normal
       UnmapNotify handler response.  Unfortunately it's very
       difficult to communicate to the handler the distinction between
       the resultant UnmapNotify from an unmap here and an unrelated
       event.  Checking the request sequence is not robust, because
       asynchronous events might not be assigned unique sequence
       numbers.  Instead, we grab the server and temporarily change
       our event mask, which seems a heavyweight approach but does
       guarantee that only this event will be ignored. */    
    xcb_grab_server( c );

    n = XCB_EVENT_MASK_PROPERTY_CHANGE | XCB_EVENT_MASK_COLOR_MAP_CHANGE;
    xcb_change_window_attributes( c, window->w, XCB_CW_EVENT_MASK, &n );
	    
    xcb_unmap_window( c, window->w );
	    
    n = XCB_EVENT_MASK_STRUCTURE_NOTIFY | XCB_EVENT_MASK_PROPERTY_CHANGE |
	XCB_EVENT_MASK_COLOR_MAP_CHANGE;
    xcb_change_window_attributes( c, window->w, XCB_CW_EVENT_MASK, &n );
	    
    xcb_ungrab_server( c );
	    
    xcb_unmap_window( c, window->u.managed.frame->w );    
}

static void managed_destroy_notify( struct gwm_window *window,
				    xcb_destroy_notify_event_t *ev ) {

    assert( window->type == WINDOW_MANAGED );
    
    /* This isn't the preferred notification mechanism, but it can
       happen: if a client has a window in the Iconic state and either
       terminates abnormally or doesn't send a synthetic UnmapNotify
       (as specified in ICCCM 2.0, section 4.1.4), then we might not
       find out about it until we receive a DestroyNotify. */
    unmanage_window( window );
}

static void managed_unmap_notify( struct gwm_window *window,
				  xcb_unmap_notify_event_t *ev ) {

    assert( window->type == WINDOW_MANAGED );

    /* A transition to the Withdrawn state (ICCCM 2.0, section 4.1.4). */
    unmanage_window( window );
}

static void managed_reparent_notify( struct gwm_window *window,
				   xcb_reparent_notify_event_t *ev ) {
    
    assert( window->type == WINDOW_MANAGED );
    
    if( ev->parent != window->u.managed.frame->w )
	/* Handle an obscure case: a client window being reparented away from
	   our frame while iconified.  Much like managed_destroy_notify. */
	unmanage_window( window->u.frame.child );
}

struct managed_get_property {
    xcb_window_t w;
    enum gwm_property_type prop;
};

static void handle_managed_get_property( unsigned int sequence, void *reply,
					 xcb_generic_error_t *error,
					 union callback_param cp ) {
    
    struct managed_get_property *p = cp.p;

    if( error ) {
	/* Ignore Window errors, since the window might have been destroyed
	   in the meantime. */
	if( error->error_code != XCB_WINDOW )
	    show_error( error );
	
	free( error );
    }

    if( reply ) {
	struct gwm_window *window = lookup_window( p->w );

	/* Don't assume the window is valid: it might have been destroyed
	   or forgotten asynchronously. */
	if( window && window->type == WINDOW_MANAGED )
	    managed_property_change( window, p->prop, reply );

	free( reply );
    }
    
    free( p );
}

extern void async_get_property( struct gwm_window *window,
				enum gwm_property_type prop ) {
    
    struct managed_get_property *p = xmalloc( sizeof *p );
    union callback_param cp;

    p->w = window->w;
    p->prop = prop;
	    
    cp.p = p;
    handle_async_reply( xcb_get_property( c, FALSE, window->w,
					  prop_atoms[ prop ],
					  prop_types[ prop ], 0,
					  PROP_SIZE ).sequence,
			handle_managed_get_property, cp );
}

struct managed_get_geometry {
    struct gwm_window *window;
    xcb_pixmap_t icon, mask;
};

static void handle_get_geometry( unsigned int sequence, void *reply,
				 xcb_generic_error_t *error,
				 union callback_param cp ) {

    struct managed_get_geometry *p = cp.p;

    if( error ) {
	/* Ignore Drawable errors, since it was specified by the client. */
	if( error->error_code != XCB_DRAWABLE )
	    show_error( error );

	free( error );

	if( !reply )
	    replace_icons( p->window, 0, NULL, NULL, NULL, &p->icon );
    }

    if( reply ) {
	xcb_get_geometry_reply_t *r = reply;
	int width, height;
	xcb_pixmap_t icons[ 2 ];

	width = r->width;
	height = r->height;
	icons[ 0 ] = p->icon;
	icons[ 1 ] = p->mask;
	replace_icons( p->window, 1, &width, &height, NULL, icons );

	free( reply );
    }

    free( p );
}

#define MOTIF_WM_HINTS_DECORATIONS 0x2

#define MOTIF_WM_HINTS_FLAGS_OFF 0
#define MOTIF_WM_HINTS_DECORATIONS_OFF 2
#define MOTIF_WM_HINTS_MIN_SIZE 3 /* ignore hint properties smaller than this */

#define MOTIF_WM_HINTS_DEC_ALL 0x1
#define MOTIF_WM_HINTS_DEC_BORDER 0x2
#define MOTIF_WM_HINTS_DEC_TITLE 0x8

#define WM_HINTS_INPUT 0x01
#define WM_HINTS_STATE 0x02
#define WM_HINTS_ICON 0x04
#define WM_HINTS_ICON_MASK 0x20

#define WM_HINTS_FLAGS_OFF 0
#define WM_HINTS_INPUT_OFF 1
#define WM_HINTS_STATE_OFF 2
#define WM_HINTS_ICON_OFF 3
#define WM_HINTS_MASK_OFF 7

#define WM_HINTS_STATE_NORMAL 1
#define WM_HINTS_STATE_ICONIC 3

#define WM_NORMAL_HINTS_USER_POSITION 0x001
#define WM_NORMAL_HINTS_PROGRAM_POSITION 0x004
#define WM_NORMAL_HINTS_MIN_SIZE 0x010
#define WM_NORMAL_HINTS_MAX_SIZE 0x020
#define WM_NORMAL_HINTS_SIZE_INC 0x040
#define WM_NORMAL_HINTS_ASPECT 0x080
#define WM_NORMAL_HINTS_BASE_SIZE 0x100
#define WM_NORMAL_HINTS_WIN_GRAVITY 0x200

#define WM_NORMAL_HINTS_FLAGS_OFF 0
#define WM_NORMAL_HINTS_MIN_SIZE_OFF 5
#define WM_NORMAL_HINTS_MAX_SIZE_OFF 7
#define WM_NORMAL_HINTS_SIZE_INC_OFF 9
#define WM_NORMAL_HINTS_ASPECT_OFF 11
#define WM_NORMAL_HINTS_BASE_SIZE_OFF 15
#define WM_NORMAL_HINTS_WIN_GRAVITY_OFF 17

extern void managed_property_change( struct gwm_window *window, int prop,
				     xcb_get_property_reply_t *p ) {
    
    uint32_t *p32, n32;
    int i;
    struct xcb_screen_t *screen;
    int value_len;
    int old_decoration;
    int num_icons;
    int len;
    
    assert( window->type == WINDOW_MANAGED );
    
    switch( prop ) {
    case PROP__MOTIF_WM_HINTS:
	/* _MOTIF_WM_HINTS property. */
	old_decoration = window->u.managed.frame->u.frame.decoration;
	
	window->u.managed.frame->u.frame.decoration = DEC_DEFAULT;
	
	if( p->format == 32 && p->value_len >= MOTIF_WM_HINTS_MIN_SIZE ) {
	    p32 = xcb_get_property_value( p );

	    if( p32[ MOTIF_WM_HINTS_FLAGS_OFF ] &
		MOTIF_WM_HINTS_DECORATIONS ) {
		if( p32[ MOTIF_WM_HINTS_DECORATIONS_OFF ] &
		    MOTIF_WM_HINTS_DEC_ALL ) {
		    window->u.managed.frame->u.frame.decoration =
			DEC_BORDER | DEC_TITLE;

		    if( p32[ MOTIF_WM_HINTS_DECORATIONS_OFF ] &
			MOTIF_WM_HINTS_DEC_BORDER )
			window->u.managed.frame->u.frame.decoration &=
			    ~DEC_BORDER;
			
		    if( p32[ MOTIF_WM_HINTS_DECORATIONS_OFF ] &
			MOTIF_WM_HINTS_DEC_TITLE )
			window->u.managed.frame->u.frame.decoration &=
			    ~DEC_TITLE;
		} else {
		    window->u.managed.frame->u.frame.decoration = 0;

		    if( p32[ MOTIF_WM_HINTS_DECORATIONS_OFF ] &
			MOTIF_WM_HINTS_DEC_BORDER )
			window->u.managed.frame->u.frame.decoration |=
			    DEC_BORDER;
			
		    if( p32[ MOTIF_WM_HINTS_DECORATIONS_OFF ] &
			MOTIF_WM_HINTS_DEC_TITLE )
			window->u.managed.frame->u.frame.decoration |=
			    DEC_TITLE;
		}
	    }
	}

#if USE_SHAPE
	if( window->u.managed.shaped )
	    /* Never apply borders to shaped windows. */
	    window->u.managed.frame->u.frame.decoration &= ~DEC_BORDER;	    
#endif
	
	if( window->u.managed.state == STATE_NORMAL &&
	    window->u.managed.frame->u.frame.decoration != old_decoration ) {
	    int new_decoration = window->u.managed.frame->u.frame.decoration;
	    int x, y, width, height;
	    uint32_t n[ 5 ];
	    
	    window->u.managed.frame->u.frame.decoration = old_decoration;
	    
	    translate_frame_to_child( window->u.managed.frame, &x, &y,
				      window->u.managed.frame->u.frame.x,
				      window->u.managed.frame->u.frame.y,
				      window->u.managed.border_width,
				      window->u.managed.win_gravity );	    
	    
	    window->u.managed.frame->u.frame.decoration = new_decoration;

	    translate_child_to_frame( window->u.managed.frame, &x, &y,
				      &width, &height, x, y,
				      window->u.managed.frame->u.frame.width -
				      frame_l( window->u.managed.frame,
					       FALSE ) -
				      frame_r( window->u.managed.frame,
					       FALSE ),
				      window->u.managed.frame->u.frame.height -
				      frame_t( window->u.managed.frame,
					       FALSE ) -
				      frame_b( window->u.managed.frame,
					       FALSE ),
				      window->u.managed.border_width,
				      window->u.managed.win_gravity );

	    if( new_decoration & DEC_TITLE )
		xcb_map_window( c,
				window->u.managed.frame->u.frame.button->w );
	    else
		xcb_unmap_window( c,
				  window->u.managed.frame->u.frame.button->w );

	    n[ 0 ] = frame_l( window->u.managed.frame, FALSE );
	    n[ 1 ] = frame_l( window->u.managed.frame, FALSE );
	    xcb_configure_window( c, window->w, XCB_CONFIG_WINDOW_X |
				  XCB_CONFIG_WINDOW_Y, n );
				  
	    n[ 0 ] = x;
	    n[ 1 ] = y;
	    n[ 2 ] = width;
	    n[ 3 ] = height;
	    n[ 4 ] = frame_xb( window->u.managed.frame );
	    /* We'll also notify the client of any changes, in the
	       ConfigureNotify handler for the event we expect to receive in
	       response to this request. */
	    xcb_configure_window( c, window->u.managed.frame->w,
				  XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
				  XCB_CONFIG_WINDOW_WIDTH |
				  XCB_CONFIG_WINDOW_HEIGHT |
				  XCB_CONFIG_WINDOW_BORDER_WIDTH, n );

	    update_frame_extents( window->u.managed.frame );
	}
	
	break;

    case PROP__NET_WM_ICON:
	/* _NET_WM_ICON property (see EWMH "Application Window Properties". */
	p32 = xcb_get_property_value( p );
	len = p->format == 32 ? p->value_len : 0;

	i = num_icons = 0;
	while( len > i + 2 && len >= i + 2 + p32[ i ] * p32[ i + 1 ] ) {
	    i += p32[ i ] * p32[ i + 1 ] + 2;
	    num_icons++;
	}

	if( num_icons ) {
	    int *widths, *heights;
	    uint32_t **icons;

	    widths = alloca( num_icons * sizeof *widths );
	    heights = alloca( num_icons * sizeof *heights );
	    icons = alloca( num_icons * sizeof *icons );

	    i = num_icons = 0;
	    while( len > i + 2 &&
		   len >= i + 2 + ( widths[ num_icons ] = p32[ i ] ) *
		   ( heights[ num_icons ] = p32[ i + 1 ] ) ) {
		icons[ num_icons ] = p32 + i + 2;
		i += widths[ num_icons ] * heights[ num_icons ] + 2;
		num_icons++;
	    }

	    replace_icons( window, num_icons, widths, heights, icons, NULL );
	} else
	    replace_icons( window, 0, NULL, NULL, NULL, NULL );

	break;
	
    case PROP__NET_WM_NAME:
	/* _NET_WM_NAME property (see EWMH "Application Window Properties". */
	if( window->u.managed.name )
	    free( window->u.managed.name );

	if( p->value_len && p->format == 8 ) {
	    window->u.managed.name = (char *) utf8_dup_valid_len(
		xcb_get_property_value( p ), p->value_len );
	    window->u.managed.net_wm_name = TRUE;
	    
	    if( window->u.managed.state == STATE_NORMAL &&
		( window->u.managed.frame->u.frame.decoration & DEC_TITLE ) )
		queue_window_update( window->u.managed.frame, 0, 0,
				     window->u.managed.frame->u.frame.width,
				     frame_t( window->u.managed.frame, FALSE ),
				     FALSE );
	} else {
	    window->u.managed.name = NULL;
	    window->u.managed.net_wm_name = FALSE;
	    /* We've lost the _NET_WM_NAME property.  If the window still
	       has a plain WM_NAME, then fall back to that. */
	    async_get_property( window, PROP_WM_NAME );
	}
	
	break;
	
    case PROP_WM_COLORMAP_WINDOWS:
	/* WM_COLORMAP_WINDOWS property (see ICCCM 2.0, section 4.1.8). */
	window->u.managed.cmap_window = XCB_NONE;

	if( p->format == 32 && p->value_len >= 2 ) {
	    p32 = xcb_get_property_value( p );

	    if( p32[ 0 ] == window->w )
		/* The window itself is given the highest priority, which
		   is what we would do anyway -- ignore the list. */
		break;
	    
	    for( i = 1; i < p->value_len; i++ )
		if( p32[ i ] == window->w ) {
		    /* The window is explicitly given a lower priority
		       than some other one.  Remember whichever is
		       considered more important. */
		    window->u.managed.cmap_window = p32[ i ];
		    
		    break;
		}
	}

	break;
	
    case PROP_WM_HINTS:
	/* WM_HINTS property (see ICCCM 2.0, section 4.1.2.4). */
	window->u.managed.hints &= ~HINT_ICONIC;
	window->u.managed.hints |= HINT_INPUT;

	if( p->format == 32 ) {
	    xcb_pixmap_t icon = XCB_NONE, mask = XCB_NONE;

	    p32 = xcb_get_property_value( p );

	    if( p->value_len > WM_HINTS_INPUT_OFF &&
		( p32[ WM_HINTS_FLAGS_OFF ] & WM_HINTS_INPUT ) &&
		!p32[ WM_HINTS_INPUT_OFF ] )
		window->u.managed.hints &= ~HINT_INPUT;

	    if( p->value_len > WM_HINTS_STATE_OFF &&
		( p32[ WM_HINTS_FLAGS_OFF ] & WM_HINTS_STATE ) &&
		p32[ WM_HINTS_STATE_OFF ] == WM_HINTS_STATE_ICONIC )
		window->u.managed.hints |= HINT_ICONIC;

	    if( p->value_len > WM_HINTS_ICON_OFF &&
		( p32[ WM_HINTS_FLAGS_OFF ] & WM_HINTS_ICON ) )
		icon = p32[ WM_HINTS_ICON_OFF ];

	    if( p->value_len > WM_HINTS_MASK_OFF &&
		( p32[ WM_HINTS_FLAGS_OFF ] & WM_HINTS_ICON_MASK ) )
		mask = p32[ WM_HINTS_MASK_OFF ];

	    if( icon ) {
		struct managed_get_geometry *p = xmalloc( sizeof *p );
		union callback_param cp;

		p->window = window;
		p->icon = icon;
		p->mask = mask;
		cp.p = p;
		handle_async_reply( xcb_get_geometry( c, icon ).sequence,
				    handle_get_geometry, cp );
	    } else
		replace_icons( window, 0, NULL, NULL, NULL, &icon );
	}

	break;
	
    case PROP_WM_NAME:
	/* WM_NAME property (see ICCCM 2.0, section 4.1.2.1). */
	if( window->u.managed.net_wm_name )
	    /* Ignore WM_NAME if _NET_WM_NAME is set. */
	    break;
	
	if( window->u.managed.name )
	    free( window->u.managed.name );

	if( p->value_len && p->format == 8 )
	    window->u.managed.name = to_utf8(
		p->type == atoms[ ATOM_COMPOUND_TEXT ] ? ENCODING_COMPOUND :
		ENCODING_LATIN_1, xcb_get_property_value( p ), p->value_len );
	else
	    window->u.managed.name = NULL;

	if( window->u.managed.state == STATE_NORMAL &&
	    ( window->u.managed.frame->u.frame.decoration & DEC_TITLE ) )
	    queue_window_update( window->u.managed.frame, 0, 0,
				 window->u.managed.frame->u.frame.width,
				 frame_t( window->u.managed.frame, FALSE ),
				 FALSE );
	
	break;

    case PROP_WM_NORMAL_HINTS:
	/* WM_NORMAL_HINTS property (see ICCCM 2.0, section 4.1.2.3). */
	window->u.managed.hints &= ~HINT_POSITION;
	window->u.managed.min_width = frame_t( window->u.managed.frame,
					       FALSE );
	window->u.managed.min_height = frame_t( window->u.managed.frame,
						FALSE ) >> 1;
	window->u.managed.max_width = 0x7FFF;
	window->u.managed.max_height = 0x7FFF;
	window->u.managed.width_inc = 1;
	window->u.managed.height_inc = 1;
	window->u.managed.min_aspect_x = 0;
	window->u.managed.min_aspect_y = 1;
	window->u.managed.max_aspect_x = 1;
	window->u.managed.max_aspect_y = 0;
	window->u.managed.base_width = 0;
	window->u.managed.base_height = 0;
	window->u.managed.win_gravity = XCB_GRAVITY_NORTH_WEST;

	if( p->value_len < 1 || p->format != 32 ) {
	    n32 = 0;
	    p32 = &n32;
	    value_len = 1;
	} else {
	    p32 = xcb_get_property_value( p );
	    value_len = p->value_len;
	}

	if( p32[ WM_NORMAL_HINTS_FLAGS_OFF ] & WM_NORMAL_HINTS_USER_POSITION )
	    window->u.managed.hints |= HINT_USER_POSITION;
	else if( p32[ WM_NORMAL_HINTS_FLAGS_OFF ] &
		 WM_NORMAL_HINTS_PROGRAM_POSITION )
	    window->u.managed.hints |= HINT_PROGRAM_POSITION;

	if( p32[ WM_NORMAL_HINTS_FLAGS_OFF ] & WM_NORMAL_HINTS_MIN_SIZE &&
	    value_len >= WM_NORMAL_HINTS_MIN_SIZE_OFF + 1 ) {
	    window->u.managed.min_width = p32[ WM_NORMAL_HINTS_MIN_SIZE_OFF ];
	    window->u.managed.min_height =
		p32[ WM_NORMAL_HINTS_MIN_SIZE_OFF + 1 ];
	}
	
	if( p32[ WM_NORMAL_HINTS_FLAGS_OFF ] & WM_NORMAL_HINTS_MAX_SIZE &&
	    value_len >= WM_NORMAL_HINTS_MAX_SIZE_OFF + 1 ) {
	    window->u.managed.max_width = p32[ WM_NORMAL_HINTS_MAX_SIZE_OFF ];
	    window->u.managed.max_height =
		p32[ WM_NORMAL_HINTS_MAX_SIZE_OFF + 1 ];
	}
	
	if( p32[ WM_NORMAL_HINTS_FLAGS_OFF ] & WM_NORMAL_HINTS_SIZE_INC &&
	    value_len >= WM_NORMAL_HINTS_SIZE_INC_OFF + 1 ) {
	    window->u.managed.width_inc = p32[ WM_NORMAL_HINTS_SIZE_INC_OFF ];
	    window->u.managed.height_inc =
		p32[ WM_NORMAL_HINTS_SIZE_INC_OFF + 1 ];

	    if( window->u.managed.width_inc < 1 )
		window->u.managed.width_inc = 1;
	    if( window->u.managed.height_inc < 1 )
		window->u.managed.height_inc = 1;
	}
	
	if( p32[ WM_NORMAL_HINTS_FLAGS_OFF ] & WM_NORMAL_HINTS_ASPECT &&
	    value_len >= WM_NORMAL_HINTS_ASPECT_OFF + 3 ) {
	    window->u.managed.min_aspect_x = p32[ WM_NORMAL_HINTS_ASPECT_OFF ];
	    window->u.managed.min_aspect_y =
		p32[ WM_NORMAL_HINTS_ASPECT_OFF + 1 ];
	    window->u.managed.max_aspect_x =
		p32[ WM_NORMAL_HINTS_ASPECT_OFF + 2 ];
	    window->u.managed.max_aspect_y =
		p32[ WM_NORMAL_HINTS_ASPECT_OFF + 3 ];
	}
	
	if( p32[ WM_NORMAL_HINTS_FLAGS_OFF ] & WM_NORMAL_HINTS_BASE_SIZE &&
	    value_len >= WM_NORMAL_HINTS_BASE_SIZE_OFF + 1 ) {
	    window->u.managed.base_width =
		p32[ WM_NORMAL_HINTS_BASE_SIZE_OFF ];
	    window->u.managed.base_height =
		p32[ WM_NORMAL_HINTS_BASE_SIZE_OFF + 1 ];

	    if( !( p32[ WM_NORMAL_HINTS_FLAGS_OFF ] &
		   WM_NORMAL_HINTS_MIN_SIZE ) ) {
		window->u.managed.min_width = window->u.managed.base_width;
		window->u.managed.min_width = window->u.managed.base_width;
	    }
	}
		
	if( p32[ WM_NORMAL_HINTS_FLAGS_OFF ] & WM_NORMAL_HINTS_WIN_GRAVITY &&
	    value_len >= WM_NORMAL_HINTS_WIN_GRAVITY_OFF )
	    window->u.managed.win_gravity =
		p32[ WM_NORMAL_HINTS_WIN_GRAVITY_OFF ];

	/* Sanity check. */
	screen = screens[ window->screen ];
	
	if( window->u.managed.min_width < frame_t( window->u.managed.frame,
						   FALSE ) )
	    window->u.managed.min_width = frame_t( window->u.managed.frame,
						   FALSE );
	if( window->u.managed.min_width > screen->width_in_pixels )
	    window->u.managed.min_width = screen->width_in_pixels;
	
	if( window->u.managed.min_height < frame_t( window->u.managed.frame,
						    FALSE ) >> 1 )
	    window->u.managed.min_height = frame_t( window->u.managed.frame,
						    FALSE ) >> 1;
	if( window->u.managed.min_height > screen->height_in_pixels )
	    window->u.managed.min_height = screen->height_in_pixels;
	    
	if( window->u.managed.max_width < window->u.managed.min_width )
	    window->u.managed.max_width = window->u.managed.min_width;
	if( window->u.managed.max_width > 0x7FFF )
	    window->u.managed.max_width = 0x7FFF;
		
	if( window->u.managed.max_height < window->u.managed.min_height )
	    window->u.managed.max_height = window->u.managed.min_height;
	if( window->u.managed.max_height > 0x7FFF )
	    window->u.managed.max_height = 0x7FFF;

	if( window->u.managed.min_aspect_x < 0 ||
	    window->u.managed.min_aspect_y < 1 ||
	    window->u.managed.max_aspect_x < 1 ||
	    window->u.managed.max_aspect_x < 0 ||
	    window->u.managed.min_aspect_x * window->u.managed.max_aspect_y >
	    window->u.managed.min_aspect_y * window->u.managed.max_aspect_x ) {
	    window->u.managed.min_aspect_x = 0;
	    window->u.managed.min_aspect_y = 1;
	    window->u.managed.max_aspect_x = 1;
	    window->u.managed.max_aspect_y = 0;
	}
	    
	if( window->u.managed.base_width < 0 ||
	    window->u.managed.base_width > window->u.managed.min_width ||
	    window->u.managed.base_height < 0 ||
	    window->u.managed.base_height > window->u.managed.min_height ) {
	    window->u.managed.base_width = 0;
	    window->u.managed.base_height = 0;
	}

	if( window->u.managed.win_gravity < XCB_GRAVITY_NORTH_WEST ||
	    window->u.managed.win_gravity > XCB_GRAVITY_STATIC )
	    window->u.managed.win_gravity = XCB_GRAVITY_NORTH_WEST;

	if( window->u.managed.state == STATE_NORMAL ) {
	    /* Recalculate constraints. */
	    int width = window->u.managed.frame->u.frame.width -
		frame_l( window->u.managed.frame, FALSE ) -
		frame_r( window->u.managed.frame, FALSE );
	    int height = window->u.managed.frame->u.frame.height -
		frame_t( window->u.managed.frame, FALSE ) -
		frame_b( window->u.managed.frame, FALSE );
	    int old_width, old_height;

	    old_width = width;
	    old_height = height;
	    
	    apply_size_constraints( window, &width, &height );

	    if( width != old_width || height != old_height ) {
		xcb_configure_request_event_t ev;
		
		ev.response_type = XCB_CONFIGURE_REQUEST;
		ev.parent = window->u.managed.frame->w;
		ev.window = window->w;
		ev.width = width;
		ev.height = height;
		ev.value_mask = XCB_CONFIG_WINDOW_WIDTH |
		    XCB_CONFIG_WINDOW_HEIGHT;

		frame_handlers[ XCB_CONFIGURE_REQUEST ](
		    window->u.managed.frame, (xcb_generic_event_t *) &ev );
	    }
	}
	
	break;
	
    case PROP_WM_PROTOCOLS:
	/* WM_PROTOCOLS property (see ICCCM 2.0, section 4.1.2.7). */
	/* FIXME Look for _NET_WM_SYNC_REQUEST. */
	window->u.managed.protocols = 0;

	if( p->format == 32 ) {
	    p32 = xcb_get_property_value( p );
	    
	    for( i = 0; i < p->value_len; i++ )
		if( p32[ i ] == atoms[ ATOM_WM_DELETE_WINDOW ] )
		    window->u.managed.protocols |= PROTOCOL_DELETE_WINDOW;
		else if( p32[ i ] == atoms[ ATOM_WM_TAKE_FOCUS ] )
		    window->u.managed.protocols |= PROTOCOL_TAKE_FOCUS;
	}
	
	if( window->u.managed.state == STATE_NORMAL ) {
	    uint32_t n = window->u.managed.protocols & PROTOCOL_DELETE_WINDOW ?
		XCB_NONE : cursors[ CURSOR_DESTROY ];
	    xcb_change_window_attributes( c, window->u.managed.frame->
					  u.frame.button->w, XCB_CW_CURSOR,
					  &n );
	}
	
	break;
    }
}

static void managed_property_notify( struct gwm_window *window,
				     xcb_property_notify_event_t *ev ) {

    enum gwm_property_type i;

    for( i = 0; i < NUM_PROPS; i++ )
	if( ev->atom == prop_atoms[ i ] ) {
	    async_get_property( window, i );
	    return;
	}
}

static void managed_colormap_notify( struct gwm_window *window,
				     xcb_colormap_notify_event_t *ev ) {
    
    if( ev->_new ) {
	window->u.managed.cmap = ev->colormap;

	if( window->u.managed.frame == focus_frame )
	    /* Gah... the protocol doesn't specify a timestamp on
	       ColormapNotify events.  We'll have to make do with the
	       most recent timestamp we've received instead. */
	    install_window_colormap( window->screen, window, latest_timestamp );
    }
}

#if USE_SHAPE
extern void match_managed_shape( struct gwm_window *window ) {

    if( window->u.managed.shaped ) {
	xcb_rectangle_t rect;
    
	rect.x = rect.y = 0;
	rect.width = window->u.managed.frame->u.frame.width;
	rect.height = frame_t( window->u.managed.frame, FALSE );
	xcb_shape_rectangles( c, XCB_SHAPE_SO_SET, XCB_SHAPE_SK_BOUNDING, 0,
			      window->u.managed.frame->w, 0, 0, 1, &rect );

	xcb_shape_combine( c, XCB_SHAPE_SO_UNION, XCB_SHAPE_SK_BOUNDING,
			   XCB_SHAPE_SK_BOUNDING, window->u.managed.frame->w,
			   frame_l( window->u.managed.frame, FALSE ),
			   frame_t( window->u.managed.frame, FALSE ),
			   window->w );
    } else
	xcb_shape_mask( c, XCB_SHAPE_SO_SET, XCB_SHAPE_SK_BOUNDING,
			window->u.managed.frame->w, 0, 0, XCB_NONE );
}

static void managed_shape_notify( struct gwm_window *window,
				  xcb_shape_notify_event_t *ev ) {

    if( ev->shape_kind == XCB_SHAPE_SK_BOUNDING ) {
	if( window->u.managed.shaped != ev->shaped &&
	    !( window->u.managed.shaped = ev->shaped ) )
	    /* The client is no longer shaped.  Retrieve its original
	       _MOTIF_WM_HINTS, since we might now want to apply a border. */
	    async_get_property( window, PROP__MOTIF_WM_HINTS );
	
	match_managed_shape( window );
    }
}
#endif

const event_handler managed_handlers[ NUM_EXTENDED_EVENTS ] = {
    NULL, /* Error */
    NULL, /* Reply */
    NULL, /* KeyPress */
    NULL, /* KeyRelease */
    NULL, /* ButtonPress */
    NULL, /* ButtonRelease */
    NULL, /* MotionNotify */
    NULL, /* EnterNotify */
    NULL, /* LeaveNotify */
    NULL, /* FocusIn */
    NULL, /* FocusOut */
    NULL, /* KeymapNotify */
    NULL, /* Expose */
    NULL, /* GraphicsExpose */
    NULL, /* NoExposure */
    NULL, /* VisibilityNotify */
    NULL, /* CreateNotify */
    (event_handler) managed_destroy_notify,
    (event_handler) managed_unmap_notify,
    NULL, /* MapNotify */
    NULL, /* MapRequest */
    (event_handler) managed_reparent_notify,
    NULL, /* ConfigureNotify */
    NULL, /* ConfigureRequest */
    NULL, /* GravityNotify */
    NULL, /* ResizeRequest */
    NULL, /* CirculateNotify */
    NULL, /* CirculateRequest */
    (event_handler) managed_property_notify,
    NULL, /* SelectionClear */
    NULL, /* SelectionRequest */
    NULL, /* SelectionNotify */
    (event_handler) managed_colormap_notify,
    NULL, /* ClientMessage */
    NULL, /* MappingNotify */
    NULL, /* (synthetic) */
    NULL, /* RRNotify */
#if USE_SHAPE
    (event_handler) managed_shape_notify
#else
    NULL /* ShapeNotify */
#endif
};

extern void withdrawn_map_request( struct gwm_window *window,
				   xcb_map_request_event_t *ev ) {

    manage_window( ev->window, TRUE );
}

extern void withdrawn_configure_request( struct gwm_window *window,
					 xcb_configure_request_event_t *ev ) {
    
    int i = 0;
    uint32_t values[ 7 ];

    /* Configuring an unmanaged window -- just honour the request as is. */
    if( ev->value_mask & XCB_CONFIG_WINDOW_X )
	values[ i++ ] = ev->x;
    if( ev->value_mask & XCB_CONFIG_WINDOW_Y )
	values[ i++ ] = ev->y;
    if( ev->value_mask & XCB_CONFIG_WINDOW_WIDTH )
	values[ i++ ] = ev->width;
    if( ev->value_mask & XCB_CONFIG_WINDOW_HEIGHT )
	values[ i++ ] = ev->height;
    if( ev->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH )
	values[ i++ ] = ev->border_width;
    if( ev->value_mask & XCB_CONFIG_WINDOW_SIBLING )
	values[ i++ ] = ev->sibling;
    if( ev->value_mask & XCB_CONFIG_WINDOW_STACK_MODE )
	values[ i++ ] = ev->stack_mode;
	
    xcb_configure_window( c, ev->window, ev->value_mask, values );
}