summaryrefslogtreecommitdiff
path: root/root.c
blob: 83176c1240f833d9fe658f6e2b2ad7528da00b53 (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
/*
 * root.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>

#if USE_RANDR
#include <xcb/randr.h>
#endif
#include <xcb/xcb.h>

#include "gwm.h"

#include "actions.h"
#include "frame.h"
#include "keyboard.h"
#include "managed.h"
#include "root.h"
#include "window-table.h"

static void root_key_press( struct gwm_window *window,
			    xcb_key_press_event_t *ev ) {

    int i;

    for( i = 0; i < num_key_actions; i++ )
	if( keyboard_map[ ev->detail ][ 0 ] == key_actions[ i ].keysym &&
	    ( ev->state & 0xFF & key_actions[ i ].modifiers ) ==
	    key_actions[ i ].modifiers ) {
	    union callback_param cp;

	    cp.p = NULL;
	    key_actions[ i ].handler( focus_frame ? focus_frame : window,
				      (xcb_generic_event_t *) ev, cp );

	    break;
	}
}

static const struct button_action {
    int button;
    xcb_mod_mask_t modifiers;
    void ( *handler )( struct gwm_window *window, xcb_generic_event_t *ev,
		       union callback_param cp );
} button_actions[] = {
    /* FIXME This table should be configurable, of course. */
    { 1, 0, action_root_menu },
    { 2, 0, action_window_list_menu },
    { 3, 0, action_start_xterm }
};

#define NUM_BUTTON_ACTIONS ( sizeof button_actions / sizeof *button_actions )

static void root_button_press( struct gwm_window *window,
                              xcb_button_press_event_t *ev ) {

    int i;
    
    if( ev->child )
       return;

    for( i = 0; i < NUM_BUTTON_ACTIONS; i++ )
	if( ev->detail == button_actions[ i ].button &&
	    ( ev->state & 0xFF & button_actions[ i ].modifiers ) ==
	    button_actions[ i ].modifiers ) {
	    union callback_param cp;

	    cp.p = NULL;
	    button_actions[ i ].handler( window, (xcb_generic_event_t *) ev,
					 cp );

	    break;
	}
}

static void root_enter_notify( struct gwm_window *window,
			       xcb_enter_notify_event_t *ev ) {
    
    if( focus_frame && ( ev->detail == XCB_NOTIFY_DETAIL_INFERIOR ||
			 ev->detail == XCB_NOTIFY_DETAIL_NONLINEAR ) ) {
	deactivate_focus_frame();
	
	xcb_set_input_focus( c, XCB_INPUT_FOCUS_NONE,
			     XCB_INPUT_FOCUS_POINTER_ROOT, ev->time );

	focus_frame = NULL;
    }

    install_window_colormap( window->screen, NULL, ev->time );
}

static void root_create_notify( struct gwm_window *window,
				xcb_create_notify_event_t *ev ) {

    /* We only ever update the window stack in response to notify
       events from the server.  The drawback to this approach is that
       our stack can become slightly stale (recently transmitted requests
       might not be reflected in our state).  However, this policy has
       the critical advantage that the window stack is always internally
       consistent: if we updated it eagerly instead, that would introduce
       the possibility of non-causal ordering of events. */
    stack_insert_above( &window_stack, ev->window,
			stack_lookup( &window_stack,
				      window->w | STACK_END )->lower_window );
}

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

    stack_remove( &window_stack, ev->window );
}

static void root_reparent_notify( struct gwm_window *window,
				  xcb_reparent_notify_event_t *ev ) {

    if( ev->parent == window->w ) {
	/* It's possible to reparent a window to its current parent,
	   so we can't assume the window is not already in the stack. */
	if( stack_lookup( &window_stack, ev->window ) )
	    stack_move_above( &window_stack, ev->window,
			      stack_lookup( &window_stack, window->w |
					    STACK_END )->lower_window );
	else
	    stack_insert_above( &window_stack, ev->window,
				stack_lookup( &window_stack, window->w |
					      STACK_END )->lower_window );
    } else
	stack_remove( &window_stack, ev->window );
}

static void root_configure_notify( struct gwm_window *window,
				   xcb_configure_notify_event_t *ev ) {

    if( ev->event == ev->window ) {
	/* The root window itself was configured. */
	if( screens[ window->screen ]->width_in_pixels !=
	    ev->width ||
	    screens[ window->screen ]->height_in_pixels !=
	    ev->height ) {
	    uint32_t values[ 4 ];

	    screens[ window->screen ]->width_in_pixels = ev->width;
	    screens[ window->screen ]->height_in_pixels = ev->height;

	    values[ 0 ] = ev->width;
	    values[ 1 ] = ev->height;
	    xcb_change_property( c, XCB_PROP_MODE_REPLACE, ev->window,
				 atoms[ ATOM__NET_DESKTOP_GEOMETRY ], CARDINAL,
				 32, 2, values );

	    values[ 0 ] = 0;
	    values[ 1 ] = 0;
	    values[ 2 ] = ev->width;
	    values[ 3 ] = ev->height;
	    xcb_change_property( c, XCB_PROP_MODE_REPLACE, ev->window,
				 atoms[ ATOM__NET_WORKAREA ], CARDINAL, 32, 4,
				 values );
	}
    } else
	/* A child of the root was configured. */
	stack_move_above( &window_stack, ev->window, ev->above_sibling ?
			  ev->above_sibling : window->w | STACK_END );
}

extern void root_circulate_request( struct gwm_window *window,
				    xcb_circulate_request_event_t *ev ) {

    uint32_t value = ev->place == XCB_PLACE_ON_TOP ? XCB_STACK_MODE_ABOVE :
	XCB_STACK_MODE_BELOW;

    /* Circulating children of the root -- honour the request as is. */
    xcb_configure_window( c, ev->window, XCB_CONFIG_WINDOW_STACK_MODE,
			  &value );
}

static void root_synthetic( struct gwm_window *root,
			    xcb_generic_event_t *ev ) {

    xcb_unmap_notify_event_t *unmap = (xcb_unmap_notify_event_t *) ev;
    xcb_configure_request_event_t *config =
	(xcb_configure_request_event_t *) ev;
    struct gwm_window *window;
	
    switch( ev->response_type & ~SEND_EVENT_MASK ) {
    case XCB_UNMAP_NOTIFY:
	/* Handle a synthetic UnmapNotify request to move a window to
	   the Withdrawn state (see ICCCM 2.0, section 4.1.4). */
	if( ( window = lookup_window( unmap->window ) ) &&
	    window->type == WINDOW_MANAGED )
	    unmanage_window( window );

	break;
	
    case XCB_CONFIGURE_REQUEST:
	/* Handle a synthetic ConfigureRequest, which should be used
	   only to restack managed windows relative to windows which
	   were originally siblings (see ICCCM 4.1.5). */
	if( ( config->value_mask & XCB_CONFIG_WINDOW_STACK_MODE ) &&
	    ( window = lookup_window( config->window ) ) &&
	    window->type == WINDOW_MANAGED ) {
	    struct gwm_window *sibling;
	    uint32_t values[ 2 ];

	    if( ( sibling = lookup_window( config->sibling ) ) &&
		sibling->type == WINDOW_MANAGED )
		sibling = sibling->u.managed.frame;

	    values[ 0 ] = sibling ? sibling->w : config->sibling;
	    values[ 1 ] = config->stack_mode;
	    
	    handle_error_reply( xcb_configure_window_checked(
				    c, window->u.managed.frame->w,
				    XCB_CONFIG_WINDOW_SIBLING |
				    XCB_CONFIG_WINDOW_STACK_MODE,
				    values ),
				ERR_MASK_VALUE | ERR_MASK_WINDOW |
				ERR_MASK_MATCH );
	}
	
	break;
    }
}

#if USE_RANDR
static void root_rr_crtc_change_notify( struct gwm_window *window,
					xcb_randr_notify_event_t *ev ) {

    int rotated = ev->u.cc.rotation & ( XCB_RANDR_ROTATION_ROTATE_90 |
					XCB_RANDR_ROTATION_ROTATE_270 );

    update_crtc( window->screen, ev->u.cc.crtc,
		 rotated ? ev->u.cc.height : ev->u.cc.width,
		 rotated ? ev->u.cc.width : ev->u.cc.height,
		 ev->u.cc.x, ev->u.cc.y );
}
#endif

const event_handler root_handlers[ NUM_EXTENDED_EVENTS ] = {
    NULL, /* Error */
    NULL, /* Reply */
    (event_handler) root_key_press,
    NULL, /* KeyRelease */
    (event_handler) root_button_press,
    NULL, /* ButtonRelease */
    NULL, /* MotionNotify */
    (event_handler) root_enter_notify,
    NULL, /* LeaveNotify */
    NULL, /* FocusIn */
    NULL, /* FocusOut */
    NULL, /* KeymapNotify */
    NULL, /* Expose */
    NULL, /* GraphicsExpose */
    NULL, /* NoExposure */
    NULL, /* VisibilityNotify */
    (event_handler) root_create_notify,
    (event_handler) root_destroy_notify,
    NULL, /* UnmapNotify */
    NULL, /* MapNotify */
    (event_handler) withdrawn_map_request,
    (event_handler) root_reparent_notify,
    (event_handler) root_configure_notify,
    (event_handler) withdrawn_configure_request,
    NULL, /* GravityNotify */
    NULL, /* ResizeRequest */
    NULL, /* CirculateNotify */
    (event_handler) root_circulate_request,
    NULL, /* PropertyNotify */
    NULL, /* SelectionClear */
    NULL, /* SelectionRequest */
    NULL, /* SelectionNotify */
    NULL, /* ColormapNotify */
    NULL, /* ClientMessage */
    NULL, /* MappingNotify */
    (event_handler) root_synthetic,
#if USE_RANDR
    (event_handler) root_rr_crtc_change_notify, /* RRCrtcChangeNotify */
#else
    NULL, /* RRCrtcChangeNotify */
#endif
    NULL  /* ShapeNotify */
};