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

#include <xcb/xcb.h>

#include "gwm.h"

#include "actions.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 ) {
	    key_actions[ i ].handler( window, (xcb_generic_event_t *) ev );

	    break;
	}
}

static const struct button_action {
    int button;
    xcb_mod_mask_t modifiers;
    void ( *handler )( struct gwm_window *window, xcb_generic_event_t *ev );
} button_actions[] = {
    /* FIXME This table should be configurable, of course. */
    { 1, 0, action_map_all_icons },
    { 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 ) {
	    button_actions[ i ].handler( window, (xcb_generic_event_t *) ev );

	    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_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(
				    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;
    }
}

event_handler root_handlers[] = {
    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 */
    NULL, /* CreateNotify */
    NULL, /* DestroyNotify */
    NULL, /* UnmapNotify */
    NULL, /* MapNotify */
    (event_handler) withdrawn_map_request,
    NULL, /* ReparentNotify */
    NULL, /* ConfigureNotify */
    (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,
    NULL /* ShapeNotify */
};