summaryrefslogtreecommitdiff
path: root/actions.c
blob: 2ea1dbe46bbfc9a9d20315993998313aae67161e (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
/*
 * actions.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 <stdarg.h>
#include <stdlib.h>
#include <string.h>
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <xcb/xcb.h>

#include "gwm.h"

#include "actions.h"
#include "frame.h"
#include "managed.h"
#include "menu.h"
#include "window-table.h"

static void external_command( char *name, ... ) {

    if( !fork() ) {
       va_list val;
       int n;
       char **args;
    
       va_start( val, name );
       for( n = 0; va_arg( val, char * ); n++ )
           ;
       va_end( val );

       args = alloca( ( n + 2 ) * sizeof *args );

       args[ 0 ] = name;
    
       va_start( val, name );
       n = 1;
       while( ( args[ n++ ] = va_arg( val, char * ) ) )
           ;
       va_end( val );

       setpgid( 0, 0 );
       
       execvp( name, args );

       _exit( 1 );
    }
}

extern void action_raise_lowest( struct gwm_window *window,
				 xcb_generic_event_t *ev,
				 union callback_param cp ) {
    
    xcb_circulate_window( c, XCB_CIRCULATE_RAISE_LOWEST,
			  screens[ window->screen ]->root );
}

extern void action_stack_opposite( struct gwm_window *window,
				   xcb_generic_event_t *ev,
				   union callback_param cp ) {
    
    if( window->type == WINDOW_FRAME ) {
	uint32_t n = XCB_STACK_MODE_OPPOSITE;
	
	xcb_configure_window( c, window->w, XCB_CONFIG_WINDOW_STACK_MODE,
			      &n );
    }
}

extern void action_root_menu( struct gwm_window *window,
			      xcb_generic_event_t *ev,
			      union callback_param cp ) {

    /* FIXME this should be configurable, of course */
    const struct menuitem root_menu[] = {
	{ "Map all icons", action_map_all_icons },
	{ "Raise lowest window", action_raise_lowest },
	{ "xterm", action_start_xterm },
	{ NULL },
	{ "Exit", action_exit }
    };
    
    popup_menu( window, ev, sizeof root_menu / sizeof *root_menu, root_menu );
}

extern void action_iconify_window( struct gwm_window *window,
				   xcb_generic_event_t *ev,
				   union callback_param cp ) {

    if( window->type == WINDOW_FRAME )
	window = window->u.frame.child;
    
    if( window->type == WINDOW_MANAGED &&
	window->u.managed.state == STATE_NORMAL )
	normal_to_iconic( window );
}

extern void action_deiconify_window( struct gwm_window *window,
				     xcb_generic_event_t *ev,
				     union callback_param cp ) {

    if( window->type == WINDOW_FRAME )
	window = window->u.frame.child;
    
    if( window->type == WINDOW_MANAGED &&
	window->u.managed.state == STATE_ICONIC )
	iconic_to_normal( window );
}

extern void action_map_raise( struct gwm_window *window,
			      xcb_generic_event_t *ev,
			      union callback_param cp ) {

    if( window->type == WINDOW_FRAME )
	window = window->u.frame.child;
    
    if( window->type == WINDOW_MANAGED ) {
	uint32_t n;

	n = XCB_STACK_MODE_ABOVE;
	xcb_configure_window( c, window->u.managed.frame->w,
			      XCB_CONFIG_WINDOW_STACK_MODE, &n );
	
	if( window->u.managed.state == STATE_ICONIC )
	    iconic_to_normal( window );
    }
}

extern void action_map_all_icons( struct gwm_window *window,
				  xcb_generic_event_t *ev,
				  union callback_param cp ) {
    
    int i;

    for( i = 0; i < windows.used; i++ )
	if( windows.values[ i ]->type == WINDOW_MANAGED &&
	    windows.values[ i ]->u.managed.state == STATE_ICONIC )
	    iconic_to_normal( windows.values[ i ] );
}

extern void action_start_xterm( struct gwm_window *window,
				xcb_generic_event_t *ev,
				union callback_param cp ) {

    external_command( "xterm", NULL );
}

extern void action_window_menu( struct gwm_window *window,
				xcb_generic_event_t *ev,
				union callback_param cp ) {

    /* FIXME this should be configurable, of course */
    const struct menuitem window_menu[] = {
	{ "Iconify", action_iconify_window },
	{ "Raise/lower", action_stack_opposite }
    };
    
    popup_menu( window, ev, sizeof window_menu / sizeof *window_menu,
		window_menu );
}

static xcb_window_t place_holder;
static int window_was_iconic, window_was_dead, dead_x, dead_y;

static void window_list_activate( struct gwm_window *window,
				  xcb_generic_event_t *ev,
				  union callback_param cp ) {

    if( place_holder ) {
	xcb_destroy_window( c, place_holder );
	place_holder = XCB_NONE;
    }
}

static void window_list_enter( struct gwm_window *window,
			       xcb_generic_event_t *ev,
			       union callback_param cp ) {

    struct gwm_window *client;

    if( ( client = lookup_window( cp.l ) ) ) {
	uint32_t values[ 4 ];
	int live_x, live_y;

	/* Introduce another (unmapped and otherwise unused) window into the
	   stack to mark the original position of the client in the stacking
	   order.  This might seem ugly, but an alternative scheme to
	   allow us to track the stacking order synchronously ourselves
	   would require another round trip to the server, which would be
	   worse. */
	place_holder = xcb_generate_id( c );
	xcb_create_window( c, 0, place_holder, screens[ client->screen ]->root,
			   0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_ONLY,
			   screens[ client->screen ]->root_visual, 0, NULL );
	values[ 0 ] = client->u.managed.frame->w;
	values[ 1 ] = XCB_STACK_MODE_BELOW;
	xcb_configure_window( c, place_holder, XCB_CONFIG_WINDOW_SIBLING |
			      XCB_CONFIG_WINDOW_STACK_MODE, values );

	if( ( window_was_iconic = client->u.managed.state == STATE_ICONIC ) )
	    iconic_to_normal( client );

	live_x = dead_x = client->u.managed.frame->u.frame.x;
	live_y = dead_y = client->u.managed.frame->u.frame.y;

	make_area_live( client->screen, &live_x, &live_y,
			client->u.managed.frame->u.frame.width,
			client->u.managed.frame->u.frame.height, 8, 8 );

	if( ( window_was_dead = live_x != dead_x || live_y != dead_y ) ) {
	    values[ 0 ] = live_x;
	    values[ 1 ] = live_y;
	    values[ 2 ] = pointer_demux; /* the menu */
	    values[ 3 ] = XCB_STACK_MODE_BELOW;
	} else {
	    values[ 0 ] = pointer_demux; /* the menu */
	    values[ 1 ] = XCB_STACK_MODE_BELOW;
	}

	xcb_configure_window( c, client->u.managed.frame->w,
			      window_was_dead ? XCB_CONFIG_WINDOW_X |
			      XCB_CONFIG_WINDOW_Y |
			      XCB_CONFIG_WINDOW_SIBLING |
			      XCB_CONFIG_WINDOW_STACK_MODE :
			      XCB_CONFIG_WINDOW_SIBLING |
			      XCB_CONFIG_WINDOW_STACK_MODE, values );

	if( focus_frame != client->u.managed.frame ) {
	    deactivate_focus_frame();

	    focus_frame = client->u.managed.frame;

	    activate_focus_frame( event_time( ev ) );
	}
    }
}

static void window_list_leave( struct gwm_window *window,
			       xcb_generic_event_t *ev,
			       union callback_param cp ) {

    struct gwm_window *client;
    
    if( ( client = lookup_window( cp.l ) ) ) {
	uint32_t values[ 4 ];

	if( window_was_dead ) {
	    values[ 0 ] = dead_x;
	    values[ 1 ] = dead_y;
	    values[ 2 ] = place_holder;
	    values[ 3 ] = XCB_STACK_MODE_ABOVE;
	} else {
	    values[ 0 ] = place_holder;
	    values[ 1 ] = XCB_STACK_MODE_ABOVE;
	}
	xcb_configure_window( c, client->u.managed.frame->w,
			      window_was_dead ?
			      XCB_CONFIG_WINDOW_X |
			      XCB_CONFIG_WINDOW_Y |
			      XCB_CONFIG_WINDOW_SIBLING |
			      XCB_CONFIG_WINDOW_STACK_MODE :
			      XCB_CONFIG_WINDOW_SIBLING |
			      XCB_CONFIG_WINDOW_STACK_MODE, values );

	if( window_was_iconic )
	    normal_to_iconic( client );

	if( focus_frame == client->u.managed.frame ) {
	    deactivate_focus_frame();

	    xcb_set_input_focus( c, XCB_INPUT_FOCUS_NONE,
				 XCB_INPUT_FOCUS_POINTER_ROOT,
				 event_time( ev ) );

	    focus_frame = NULL;
	}
    }

    if( place_holder ) {
	xcb_destroy_window( c, place_holder );
	place_holder = XCB_NONE;
    }
}

extern void action_window_list_menu( struct gwm_window *window,
				     xcb_generic_event_t *ev,
				     union callback_param cp ) {

    int i, num_items;
    
    struct menuitem *items;

    for( i = 0, num_items = 0; i < num_screens; i++ ) {
	xcb_window_t w = screens[ i ]->root | STACK_END;

	if( i )
	    num_items++;

	do {
	    struct gwm_window *window = lookup_window( w );
	    xcb_window_t next =
		stack_lookup( &window_stack, w )->lower_window;

	    if( window && window->type == WINDOW_FRAME )
		num_items++;

	    w = next;
	} while( w != ( screens[ i ]->root | STACK_END ) );
    }
    
    items = alloca( num_items * sizeof *items );

    for( i = 0, num_items = 0; i < num_screens; i++ ) {
	xcb_window_t w = screens[ i ]->root | STACK_END;

	if( i ) {
	    items[ num_items ].label = NULL;

	    num_items++;
	}

	do {
	    struct gwm_window *window = lookup_window( w );
	    xcb_window_t next =
		stack_lookup( &window_stack, w )->lower_window;

	    if( window && window->type == WINDOW_FRAME ) {
		struct gwm_window *managed = window->u.frame.child;
		char *name = managed->u.managed.name ?
		    managed->u.managed.name : "(Untitled)";

		if( managed->u.managed.state == STATE_ICONIC ) {
		    int len = strlen( name );
		    char *new = alloca( len + 3 );

		    new[ 0 ] = '[';
		    strcpy( new + 1, name );
		    new[ len + 1 ] = ']';
		    new[ len + 2 ] = 0;

		    name = new;
		}

		items[ num_items ].label = name;
		items[ num_items ].action = window_list_activate;
		items[ num_items ].enter_action = window_list_enter;
		items[ num_items ].leave_action = window_list_leave;
		items[ num_items ].cp.l = managed->w;
		items[ num_items ].icon = managed->w;

		num_items++;
	    }

	    w = next;
	} while( w != ( screens[ i ]->root | STACK_END ) );
    }

    popup_menu( window, ev, num_items, items );
}

extern void action_exit( struct gwm_window *window, xcb_generic_event_t *ev,
			 union callback_param cp ) {

    /* FIXME prompt for confirmation of exit */
    
    signal_caught = -1;
}

/* FIXME make the current frame bindings (move, resize, close?) actions too */