summaryrefslogtreecommitdiff
path: root/window-table.c
blob: 6970171083f81c434ffdc3b7516748d216255aca (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
/*
 * 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 <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 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( struct window_table *table, int half,
			     xcb_window_t key ) {

    unsigned long h0, h1;
    unsigned long long l;
    
    if( half ) {
	h0 = table->hash ^ 0x79D9C6D4UL;
	h1 = table->hash * 0x6A391E85UL;
    } else {
	h0 = table->hash;
	h1 = table->hash ^ 0x52C694B6UL;
    }

    l = key * h0;
    l ^= l >> 32;
    l *= h1;
    l ^= l >> 29;

    return l & ( table->n - 1 );
}

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

    int h;

    assert( key );
    
    h = table_hash( table, 0, key );
    if( table->t[ 0 ][ h ].key == key )
	return table->t[ 0 ][ h ].index;
    
    h = table_hash( table, 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 ) {

    table->used++;
    
    for(;;) {
	int i;
    
	for( i = 0; i < table->max; i++ ) {
	    int h = table_hash( table, 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, 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, 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, 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;
    }
}