summaryrefslogtreecommitdiff
path: root/button.c
blob: 07cb9d5ef5aa2c5823a923a12fc140a5a32bcde9 (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
/*
 * button.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 <xcb/xcb.h>

#include "gwm.h"

#include "button.h"
#include "window-table.h"

#define BUTTON_SIZE 12 /* size of buttons in title bar; excludes X border */
#define BUTTON_X_BORDER 1

extern int button_size( struct gwm_window *window, int include_x_border ) {

    assert( window->type == WINDOW_BUTTON );
    
    return BUTTON_SIZE + ( include_x_border ? BUTTON_X_BORDER << 1 : 0 );
}

extern int button_xb( struct gwm_window *window ) {
    
    assert( window->type == WINDOW_BUTTON );

    return BUTTON_X_BORDER;
}

static int button_active;

static void set_button_active( struct gwm_window *window, int new ) {

    uint32_t n;
    
    if( new == button_active )
	return;

    n = gwm_screens[ window->screen ].pixels[ ( button_active = new ) ?
					      COL_BUTTON_ACTIVE :
					      COL_BUTTON_INACTIVE ];
    xcb_change_window_attributes( c, window->w, XCB_CW_BACK_PIXEL, &n );

    queue_window_update( window, 0, 0, button_size( window, FALSE ),
			 button_size( window, FALSE ), FALSE );
}

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

    if( !pointer_demux ) {
	/* Initiate grab. */
	pointer_demux = window->w;

	set_button_active( window, TRUE );
    }
    
    if( ( window->u.button.frame->u.frame.child->u.managed.protocols &
	  PROTOCOL_DELETE_WINDOW ) && ( ev->detail == 3 ||
					( ev->state & XCB_BUTTON_MASK_3 ) ) )
	/* Button 3 will kill the client, even if it supports WM_DELETE_WINDOW.
	   Change to CURSOR_DESTROY to indicate the distinction. */
	xcb_change_active_pointer_grab( c, cursors[ CURSOR_DESTROY ],
					ev->time, XCB_EVENT_MASK_BUTTON_PRESS |
					XCB_EVENT_MASK_BUTTON_RELEASE |
					XCB_EVENT_MASK_ENTER_WINDOW |
					XCB_EVENT_MASK_LEAVE_WINDOW );
}

static void button_button_release( struct gwm_window *window,
				   xcb_button_release_event_t *ev ) {
    
    if( !pointer_demux )
	return;
    
    if( final_release( ev ) ) {
	/* Final button released; grab terminates. */
	if( button_active ) {
	    if( ev->detail == 3 || !( window->u.button.frame->u.frame.child->
				      u.managed.protocols &
				      PROTOCOL_DELETE_WINDOW ) )
		xcb_kill_client( c, window->u.button.frame->u.frame.child->w );
	    else {
		xcb_client_message_event_t msg;
		struct gwm_window *frame = window->u.button.frame;
	    
		msg.response_type = XCB_CLIENT_MESSAGE;
		msg.format = 32;
		msg.sequence = 0;
		msg.window = frame->u.frame.child->w;
		msg.type = atoms[ ATOM_WM_PROTOCOLS ];
		msg.data.data32[ 0 ] = atoms[ ATOM_WM_DELETE_WINDOW ];
		msg.data.data32[ 1 ] = ev->time;
		msg.data.data32[ 2 ] = 0;
		msg.data.data32[ 3 ] = 0;
		msg.data.data32[ 4 ] = 0;
		
		xcb_send_event( c, FALSE, frame->u.frame.child->w, 0,
				(char *) &msg );
	    }
	    
	    set_button_active( window, FALSE );
	}	

	pointer_demux = XCB_NONE;
    } else if( ev->detail == 3 && ( window->u.button.frame->u.frame.child->
				    u.managed.protocols &
				    PROTOCOL_DELETE_WINDOW ) )
	/* Button 3 released; we'll no longer kill the client.  Remove
	   the temporary CURSOR_DESTROY. */
	xcb_change_active_pointer_grab( c, XCB_NONE, ev->time,
					XCB_EVENT_MASK_BUTTON_PRESS |
					XCB_EVENT_MASK_BUTTON_RELEASE |
					XCB_EVENT_MASK_ENTER_WINDOW |
					XCB_EVENT_MASK_LEAVE_WINDOW );
}

static void button_enter_notify( struct gwm_window *window,
				 xcb_enter_notify_event_t *ev ) {
    
    if( !pointer_demux )
	return;
    
    if( ev->event == window->w )
	set_button_active( window, TRUE );
}

static void button_leave_notify( struct gwm_window *window,
				 xcb_leave_notify_event_t *ev ) {
    
    if( !pointer_demux )
	return;
    
    if( ev->event == window->w )
	set_button_active( window, FALSE );
}

const event_handler button_handlers[ NUM_EXTENDED_EVENTS ] = {
    NULL, /* Error */
    NULL, /* Reply */
    NULL, /* KeyPress */
    NULL, /* KeyRelease */
    (event_handler) button_button_press,
    (event_handler) button_button_release,
    NULL, /* MotionNotify */
    (event_handler) button_enter_notify,
    (event_handler) button_leave_notify,
    NULL, /* FocusIn */
    NULL, /* FocusOut */
    NULL, /* KeymapNotify */
    NULL, /* Expose */
    NULL, /* GraphicsExpose */
    NULL, /* NoExposure */
    NULL, /* VisibilityNotify */
    NULL, /* CreateNotify */
    NULL, /* DestroyNotify */
    NULL, /* UnmapNotify */
    NULL, /* MapNotify */
    NULL, /* MapRequest */
    NULL, /* ReparentNotify */
    NULL, /* ConfigureNotify */
    NULL, /* ConfigureRequest */
    NULL, /* GravityNotify */
    NULL, /* ResizeRequest */
    NULL, /* CirculateNotify */
    NULL, /* CirculateRequest */
    NULL, /* PropertyNotify */
    NULL, /* SelectionClear */
    NULL, /* SelectionRequest */
    NULL, /* SelectionNotify */
    NULL, /* ColormapNotify */
    NULL, /* ClientMessage */
    NULL, /* MappingNotify */
    NULL, /* (synthetic) */
    NULL, /* RRScreenChangeNotify */
    NULL  /* ShapeNotify */
};