summaryrefslogtreecommitdiff
path: root/window-table.c
blob: ccdf80fb64eefa1105e254e70a349de88033b937 (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
/*
 * window-table.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 <limits.h>
#if HAVE_STDINT_H
#include <stdint.h>
#endif
#include <stdlib.h>
#include <xcb/xcb.h>

#include "gwm.h"

#include "window-table.h"

/* Cuckoo hashed window table: see _Cuckoo Hashing_, R. Pagh and F. Rodler,
   Journal of Algorithms 51 (2004), pp. 122-144. */

struct window_table windows, update_windows;

extern INIT void table_init( struct window_table *table ) {

    table->used = 0;
    table->n = 32;
    table->max = 24;
    table->hash = 0xBA97D5B1UL;

    table->t[ 0 ] = xcalloc( table->n << 1, sizeof table->t[ 0 ][ 0 ] );
    table->t[ 1 ] = table->t[ 0 ] + table->n;
    table->values = xmalloc( table->n * sizeof *table->values );
}

#if DEBUG
extern void table_destroy( struct window_table *table ) {

    free( table->t[ 0 ] );
    free( table->values );
}
#endif

static CONST int table_hash( unsigned long hash, int n, int half,
			     xcb_window_t key ) {

    unsigned long h0, h1;
#if defined( UINT64_MAX ) || defined( uint64_t )
    uint64_t l;
#else
    unsigned long l;
#endif
    
    if( half ) {
	h0 = hash ^ 0x79D9C6D4UL;
	h1 = hash * 0x6A391E85UL;
    } else {
	h0 = hash;
	h1 = hash ^ 0x52C694B6UL;
    }

#if defined( UINT64_MAX ) || defined( uint64_t ) || ULONG_MAX > 0xFFFFFFFFUL
    l = key * h0;
    l ^= l >> 32;
    l *= h1;
    l ^= l >> 29;
#else
    l = key * h0;
    l ^= l >> 16;
    l *= h1;
    l ^= l >> 13;
    l *= h0;
    l ^= l >> 15;
#endif
    
    return l & ( n - 1 );
}

static PURE int table_lookup_index( struct window_table *table,
				    xcb_window_t key ) {

    int h;

    assert( key );
    
    h = table_hash( table->hash, table->n, 0, key );
    if( table->t[ 0 ][ h ].key == key )
	return table->t[ 0 ][ h ].index;
    
    h = table_hash( table->hash, table->n, 1, key );
    if( table->t[ 1 ][ h ].key == key )
	return table->t[ 1 ][ h ].index;
    
    return -1;
}

extern PURE struct gwm_window *table_lookup( struct window_table *table,
					     xcb_window_t key ) {

    int index;

    if( !key )
	return NULL;

    return ( index = table_lookup_index( table, key ) ) >= 0 ?
	table->values[ index ] : NULL;
}

static void table_insert_index( struct window_table *table, xcb_window_t key,
				int index );

static void table_rehash( struct window_table *table, unsigned long hash,
			  unsigned long n, unsigned long max ) {

    int old_n = table->n;
    struct half_table *old_t[ 2 ];
    int i;

    old_t[ 0 ] = table->t[ 0 ];
    old_t[ 1 ] = table->t[ 1 ];

    table->hash = hash;
    table->n = n;
    table->max = max;
    table->used = 0;
    
    table->t[ 0 ] = xcalloc( table->n << 1, sizeof table->t[ 0 ][ 0 ] );
    table->t[ 1 ] = table->t[ 0 ] + table->n;
    table->values = xrealloc( table->values, table->n * sizeof *table->values );

    /* We've now gotten everything we want out of the old table.  Although
       table_insert might possibly cause inefficient re-entrancy, that case
       should be both harmless and exceedingly rare. */    
    for( i = 0; i < old_n; i++ ) {
	if( old_t[ 0 ][ i ].key )
	    table_insert_index( table, old_t[ 0 ][ i ].key,
				old_t[ 0 ][ i ].index );
	if( old_t[ 1 ][ i ].key )
	    table_insert_index( table, old_t[ 1 ][ i ].key,
				old_t[ 1 ][ i ].index );
    }
    
    free( old_t[ 0 ] );
}

static void table_insert_index( struct window_table *table, xcb_window_t key,
				int index ) {

    for(;;) {
	int i;
    
	table->used++;

	for( i = 0; i < table->max; i++ ) {
	    int h = table_hash( table->hash, table->n, i & 1, key );
	    xcb_window_t old_key = table->t[ i & 1 ][ h ].key;
	    int old_index = table->t[ i & 1 ][ h ].index;

	    table->t[ i & 1 ][ h ].key = key;
	    table->t[ i & 1 ][ h ].index = index;
		
	    if( !old_key )
		return;

	    key = old_key;
	    index = old_index;
	}
	    
	/* Failure to insert: rehash. */
	table_rehash( table, table->hash + 0x5DB1BA96UL,
		      table->n, table->max );
    }
}

static void table_insert( struct window_table *table, xcb_window_t key,
			  struct gwm_window *value ) {

    if( !key )
	return;

    assert( !table_lookup( table, key ) );
    
    table->values[ table->used ] = value;
    
    if( table->used > ( ( table->n * 3 ) >> 2 ) )
	/* Load becoming high: resize. */
	table_rehash( table, table->hash, table->n << 1, table->max + 4 );

    table_insert_index( table, key, table->used );
}

static void table_delete( struct window_table *table, xcb_window_t key ) {

    int h;
    int i;
    
    if( !key )
	return;

    for( i = 0; i < 2; i++ ) {
	h = table_hash( table->hash, table->n, i, key );
	if( table->t[ i ][ h ].key == key ) {
	    int index;
	    xcb_window_t replacement_key;
	    struct gwm_window *replacement_value;
	    int replacement_hash;
	    
	    table->used--;
	    
	    if( ( index = table->t[ i ][ h ].index ) < table->used ) {
		/* Must replace our old index, to keep the values array
		   compact. */
		table->values[ index ] = replacement_value =
		    table->values[ table->used ];
		replacement_key = replacement_value->w;

		replacement_hash = table_hash( table->hash, table->n, 0,
					       replacement_key );
		if( table->t[ 0 ][ replacement_hash ].key == replacement_key )
		    table->t[ 0 ][ replacement_hash ].index = index;
		else {
		    replacement_hash = table_hash( table->hash, table->n, 1,
						   replacement_key );
		    assert( table->t[ 1 ][ replacement_hash ].key ==
			    replacement_key );
		    table->t[ 1 ][ replacement_hash ].index = index;
		}
	    }
	    
	    table->t[ i ][ h ].key = 0;
	    
	    if( table->used < ( ( table->n * 3 ) >> 4 ) && table->n > 32 )
		/* Load becoming low: resize. */
		table_rehash( table, table->hash, table->n >> 1,
			      table->max - 4 );
	    return;
	}
    }
}

extern void queue_window_update( struct gwm_window *window,
				 int x, int y, int width, int height,
				 int cleared ) {

    /* FIXME Consider computing the region instead of the bounding box. */

    if( !window->update.width || !window->update.height ) {
	/* New update region. */
	window->update.x = x;
	window->update.y = y;
	window->update.width = width;
	window->update.height = height;
	
	if( !cleared )
	    window->cleared = FALSE;
    } else {
	/* Compute the bounding box of the union of the old and new regions.
	   If the bounding box is enlarged, we reset the cleared flag,
	   because it is very likely that it now includes areas which
	   were not in either of the two original regions (and therefore
	   not guaranteed to be cleared). */
	if( x < window->update.x ) {
	    window->update.width += window->update.x - x;
	    window->update.x = x;
	    window->cleared = FALSE;
	}

	if( y < window->update.y ) {
	    window->update.height += window->update.y - y;
	    window->update.y = y;
	    window->cleared = FALSE;
	}

	if( x + width > window->update.x + window->update.width ) {
	    window->update.width = x + width - window->update.x;
	    window->cleared = FALSE;
	}

	if( y + height > window->update.y + window->update.height ) {
	    window->update.height = y + height - window->update.y;
	    window->cleared = FALSE;
	}
    }
    
    if( table_lookup( &update_windows, window->w ) )
	return;

    table_insert( &update_windows, window->w, window );
}

extern void window_update_done( struct gwm_window *window ) {

    window->update.x = 0;
    window->update.y = 0;
    window->update.width = 0;
    window->update.height = 0;
    window->cleared = TRUE;

    table_delete( &update_windows, window->w );
}

extern MALLOC struct gwm_window *add_window( xcb_window_t w ) {

    struct gwm_window *window;
    
    if( table_lookup( &windows, w ) )
	return NULL; /* window already exists */

    window = xmalloc( sizeof *window );
    window->w = w;
    window_update_done( window );
    
    table_insert( &windows, window->w, window );

    return window;
}

extern void forget_window( struct gwm_window *window ) {

    if( pointer_demux == window->w )
	pointer_demux = XCB_NONE;
    
    table_delete( &windows, window->w );
    table_delete( &update_windows, window->w );
    free( window );
}

extern struct gwm_window *lookup_window( xcb_window_t w ) {

    struct gwm_window *window;
    
    for(;;) {
	window = table_lookup( &windows, w );

	if( window && window->type == WINDOW_INCOMPLETE )
	    /* Whoops... all this asynchronicity has gotten us ahead
	       of ourselves.  Catch up on what we deferred, by waiting
	       until the window is complete before we continue. */
	    sync_with_callback( window->u.incomplete.sequence );
	else
	    return window;
    }
}

struct window_stack window_stack;

extern INIT void stack_init( struct window_stack *stack ) {

    stack->used = 0;
    stack->n = 32;
    stack->max = 24;
    stack->hash = 0xBA97D5B1UL;

    stack->t[ 0 ] = xcalloc( stack->n << 1, sizeof stack->t[ 0 ][ 0 ] );
    stack->t[ 1 ] = stack->t[ 0 ] + stack->n;
}

#if DEBUG
extern void stack_destroy( struct window_stack *stack ) {

    free( stack->t[ 0 ] );
}
#endif

extern PURE struct stacking_order *stack_lookup( struct window_stack *stack,
						 xcb_window_t key ) {

    int h;

    if( !key )
	return NULL;

    h = table_hash( stack->hash, stack->n, 0, key );
    if( stack->t[ 0 ][ h ].window == key )
	return &stack->t[ 0 ][ h ];

    h = table_hash( stack->hash, stack->n, 1, key );
    if( stack->t[ 1 ][ h ].window == key )
	return &stack->t[ 1 ][ h ];

    return NULL;
}

static void stack_insert( struct window_stack *stack, xcb_window_t window,
			  xcb_window_t lower_window,
			  xcb_window_t higher_window );

static void stack_rehash( struct window_stack *stack, unsigned long hash,
			  unsigned long n, unsigned long max ) {

    int old_n = stack->n;
    struct stacking_order *old_t[ 2 ];
    int i;

    old_t[ 0 ] = stack->t[ 0 ];
    old_t[ 1 ] = stack->t[ 1 ];

    stack->hash = hash;
    stack->n = n;
    stack->max = max;
    stack->used = 0;

    stack->t[ 0 ] = xcalloc( stack->n << 1, sizeof stack->t[ 0 ][ 0 ] );
    stack->t[ 1 ] = stack->t[ 0 ] + stack->n;

    /* We've now gotten everything we want out of the old stack.  Although
       stack_insert might possibly cause inefficient re-entrancy, that case
       should be both harmless and exceedingly rare. */
    for( i = 0; i < old_n; i++ ) {
	if( old_t[ 0 ][ i ].window )
	    stack_insert( stack, old_t[ 0 ][ i ].window,
			  old_t[ 0 ][ i ].lower_window,
			  old_t[ 0 ][ i ].higher_window );
	if( old_t[ 1 ][ i ].window )
	    stack_insert( stack, old_t[ 1 ][ i ].window,
			  old_t[ 1 ][ i ].lower_window,
			  old_t[ 1 ][ i ].higher_window );
    }

    free( old_t[ 0 ] );
}

static void stack_insert( struct window_stack *stack, xcb_window_t window,
			  xcb_window_t lower_window,
			  xcb_window_t higher_window ) {

    for(;;) {
	int i;

	stack->used++;

	for( i = 0; i < stack->max; i++ ) {
	    int h = table_hash( stack->hash, stack->n, i & 1, window );
	    xcb_window_t old_window = stack->t[ i & 1 ][ h ].window;
	    xcb_window_t old_lower, old_higher;

	    if( old_window ) {
		old_lower = stack->t[ i & 1 ][ h ].lower_window;
		old_higher = stack->t[ i & 1 ][ h ].higher_window;
	    }

	    stack->t[ i & 1 ][ h ].window = window;
	    stack->t[ i & 1 ][ h ].lower_window = lower_window;
	    stack->t[ i & 1 ][ h ].higher_window = higher_window;

	    if( !old_window )
		return;

	    window = old_window;
	    lower_window = old_lower;
	    higher_window = old_higher;
	}

	/* Failure to insert: rehash. */
	stack_rehash( stack, stack->hash + 0x5DB1BA96UL,
		      stack->n, stack->max );
    }
}

static void stack_insert_new( struct window_stack *stack, xcb_window_t window,
			      xcb_window_t lower_window,
			      xcb_window_t higher_window ) {

    if( !window )
	return;

    assert( !stack_lookup( stack, window ) );

    if( stack->used > ( ( stack->n * 3 ) >> 2 ) )
	/* Load becoming high: resize. */
	stack_rehash( stack, stack->hash, stack->n << 1, stack->max + 4 );

    stack_insert( stack, window, lower_window, higher_window );
}

extern void stack_insert_singleton( struct window_stack *stack,
				    xcb_window_t key ) {

    if( !key )
	return;

    stack_insert_new( stack, key, key, key );

    assert( stack_lookup( stack, key ) );
}

extern void stack_insert_above( struct window_stack *stack,
				xcb_window_t key, xcb_window_t lower_window ) {

    struct stacking_order *l, *h;
    xcb_window_t higher_window;

    if( !key )
	return;

    l = stack_lookup( stack, lower_window );
    assert( l );
    higher_window = l->higher_window;

    stack_insert_new( stack, key, lower_window, higher_window );
    /* It's essential to repeat the lookup of lower_window, because inserting
       the new entry might have disturbed the existing ones. */
    l = stack_lookup( stack, lower_window );
    h = stack_lookup( stack, higher_window );
    h->lower_window = key;
    l->higher_window = key;

    assert( stack_lookup( stack, key ) );
    assert( l );
    assert( h );
}

extern void stack_remove( struct window_stack *stack, xcb_window_t window ) {

    struct stacking_order *l, *h, *entry;
    int hash, i;

    if( !window )
	return;

    entry = stack_lookup( stack, window );
    assert( entry );

    l = stack_lookup( stack, entry->lower_window );
    assert( l );

    h = stack_lookup( stack, entry->higher_window );
    assert( h );

    l->higher_window = entry->higher_window;
    h->lower_window = entry->lower_window;

    for( i = 0; i < 2; i++ ) {
	hash = table_hash( stack->hash, stack->n, i, window );
	if( stack->t[ i ][ hash ].window == window ) {
	    stack->t[ i ][ hash ].window = 0;

	    if( stack->used < ( ( stack->n * 3 ) >> 4 ) && stack->n > 32 )
		/* Load becoming low: resize. */
		stack_rehash( stack, stack->hash, stack->n >> 1,
			      stack->max - 4 );

	    assert( !stack_lookup( stack, window ) );

	    return;
	}
    }

    assert( FALSE );
}

extern void stack_move_above( struct window_stack *stack,
			      xcb_window_t window,
			      xcb_window_t lower_window ) {

    struct stacking_order *l, *h, *entry;

    if( !window || window == lower_window )
	return;

    entry = stack_lookup( stack, window );
    assert( entry );

    if( entry->lower_window == lower_window )
	return;

    l = stack_lookup( stack, entry->lower_window );
    assert( l );

    h = stack_lookup( stack, entry->higher_window );
    assert( h );

    l->higher_window = entry->higher_window;
    h->lower_window = entry->lower_window;

    l = stack_lookup( stack, lower_window );
    assert( l );

    h = stack_lookup( stack, l->higher_window );
    assert( h );

    h->lower_window = window;
    l->higher_window = window;
    entry->lower_window = lower_window;
    entry->higher_window = h->window;

    assert( stack_lookup( stack, window ) );
}

extern xcb_window_t stack_real_below( struct window_stack *stack,
				      xcb_window_t window ) {

    xcb_window_t search;

    if( !window )
	return 0;

    for( search = stack_lookup( stack, window )->lower_window;
	 search != window && ( search & STACK_MASK ) != STACK_END;
	 search = stack_lookup( stack, search )->lower_window )
	if( !( search & STACK_MASK ) )
	    return search;

    return 0;
}