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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
|
/*
* Copyright (C) 2000 Shawn Betts
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* 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 software; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307 USA */
#include <unistd.h>
#include <sys/wait.h>
#include <X11/keysym.h>
#include <string.h>
#include <time.h>
#include "ratpoison.h"
/* Initialization of the key structure */
rp_action key_actions[] = { {KEY_PREFIX, 0, 0, generate_prefix},
{XK_c, -1, TERM_PROG, spawn},
{XK_e, -1, "emacs", spawn},
{XK_p, -1, 0, prev_window},
{XK_n, -1, 0, next_window},
{XK_space, -1, 0, next_window},
{XK_colon, 0, 0, execute_command},
{KEY_PREFIX, -1, 0, last_window},
{XK_w, -1, 0, toggle_bar},
{XK_K, 0, 0, kill_window},
{XK_k, 0, 0, delete_window},
{XK_quoteright, -1, 0, goto_win_by_name},
{XK_A, -1, 0, rename_current_window},
{XK_a, -1, 0, show_clock},
{XK_g, ControlMask, 0, abort_keypress},
{XK_0, -1, 0, goto_window_0},
{XK_1, -1, 0, goto_window_1},
{XK_2, -1, 0, goto_window_2},
{XK_3, -1, 0, goto_window_3},
{XK_4, -1, 0, goto_window_4},
{XK_5, -1, 0, goto_window_5},
{XK_6, -1, 0, goto_window_6},
{XK_7, -1, 0, goto_window_7},
{XK_8, -1, 0, goto_window_8},
{XK_9, -1, 0, goto_window_9},
{XK_m, -1, 0, maximize},
{ 0, 0, 0, 0 } };
void
prev_window (void *data)
{
rp_window *new_win = (rp_window *)data;
if (new_win == NULL) new_win = rp_current_window;
if (new_win != NULL)
{
new_win = new_win->prev;
if (new_win == NULL)
{
new_win = rp_window_tail;
}
if (new_win->state == STATE_UNMAPPED)
prev_window (new_win);
else
set_active_window (new_win);
}
}
void
next_window (void *data)
{
rp_window *new_win = (rp_window *)data;
if (new_win == NULL) new_win = rp_current_window;
if (new_win != NULL)
{
new_win = new_win->next;
if (new_win == NULL)
{
new_win = rp_window_head;
}
if (new_win->state == STATE_UNMAPPED)
next_window (new_win);
else
set_active_window (new_win);
}
}
void
last_window (void *data)
{
rp_window *oldwin = rp_current_window;
set_active_window (find_last_accessed_window ());
if (rp_current_window == oldwin)
{
display_msg_in_bar (&screens[0], "No other window.");
}
}
void
goto_win_by_name (void *data)
{
char winname[100];
if (rp_current_window == NULL) return;
get_input (rp_current_window->scr, "Window: ", winname, 100);
PRINT_DEBUG ("user entered: %s\n", winname);
goto_window_name (winname);
}
void
rename_current_window (void *data)
{
char winname[100];
if (rp_current_window == NULL) return;
get_input (rp_current_window->scr, "Name: ", winname, 100);
PRINT_DEBUG ("user entered: %s\n", winname);
if (*winname)
{
free (rp_current_window->name);
rp_current_window->name = malloc (sizeof (char) * strlen (winname) + 1);
if (rp_current_window->name == NULL)
{
PRINT_ERROR ("Out of memory\n");
exit (EXIT_FAILURE);
}
strcpy (rp_current_window->name, winname);
rp_current_window->named = 1;
/* Update the program bar. */
update_window_names (rp_current_window->scr);
}
}
void
delete_window (void *data)
{
XEvent ev;
int status;
if (rp_current_window == NULL) return;
ev.xclient.type = ClientMessage;
ev.xclient.window = rp_current_window->w;
ev.xclient.message_type = wm_protocols;
ev.xclient.format = 32;
ev.xclient.data.l[0] = wm_delete;
ev.xclient.data.l[1] = CurrentTime;
status = XSendEvent(dpy, rp_current_window->w, False, 0, &ev);
if (status == 0) fprintf(stderr, "ratpoison: delete window failed\n");
}
void
kill_window (void *data)
{
if (rp_current_window == NULL) return;
XKillClient(dpy, rp_current_window->w);
}
void
execute_command (void *data)
{
char cmd[100];
if (rp_current_window)
{
get_input (rp_current_window->scr, "Command: ", cmd, 100);
}
else
{
/* FIXME: We can always assume there is 1 screen, but which one
is the active one? Need to test on multi-screen x-servers. */
get_input (&screens[0], "Command: ", cmd, 100);
}
PRINT_DEBUG ("user entered: %s\n", cmd);
spawn (cmd);
}
void
spawn(void *data)
{
char *prog = data;
/*
* ugly dance to avoid leaving zombies. Could use SIGCHLD,
* but it's not very portable.
*/
if (fork() == 0) {
if (fork() == 0) {
putenv(DisplayString(dpy));
execlp(prog, prog, 0);
PRINT_ERROR ("exec %s ", prog);
perror(" failed");
exit(EXIT_FAILURE);
}
exit(0);
}
wait((int *) 0);
PRINT_DEBUG ("spawned %s\n", prog);
}
/* Switch to a different Window Manager. Thanks to
"Chr. v. Stuckrad" <stucki@math.fu-berlin.de> for the patch. */
void
switch_to(void *which)
{
char *prog=(char *)which;
PRINT_DEBUG ("Switching to %s\n", prog);
putenv(DisplayString(dpy));
execlp(prog, prog, 0);
PRINT_ERROR ("exec %s ", prog);
perror(" failed");
}
/* Quit ratpoison. Thanks to
"Chr. v. Stuckrad" <stucki@math.fu-berlin.de> for the patch. */
void
bye(void *dummy)
{
PRINT_DEBUG ("Exiting\n");
clean_up ();
exit (EXIT_SUCCESS);
}
/* Show the current time on the bar. Thanks to
Martin Samuelsson <cosis@lysator.liu.se> for the patch. */
void
show_clock (void *data)
{
screen_info *s;
char *msg;
time_t timep;
msg = malloc(9);
if(msg == NULL)
{
PRINT_DEBUG ("Error allocation memory in show_clock()\n");
return;
}
timep = time(NULL);
if(timep == ((time_t)-1))
{
perror("In show_clock() ");
return;
}
strncpy(msg, ctime(&timep) + 11, 8); /* FIXME: a little bit hardcoded looking */
msg[8] = '\0';
if (rp_current_window)
{
s = rp_current_window->scr;
display_msg_in_bar (s, msg);
}
free(msg);
}
void
goto_window_number (int n)
{
rp_window *win;
if ((win = find_window_by_number (n)) == NULL)
{
/* Display window list to indicate failure. */
/* FIXME: We can always assume there is 1 screen, but which one
is the active one? Need to test on multi-screen x-servers. */
show_bar (&screens[0]);
return;
}
set_active_window (win);
}
void
goto_window_0 (void *data)
{
goto_window_number (0);
}
void
goto_window_1 (void *data)
{
goto_window_number (1);
}
void
goto_window_2 (void *data)
{
goto_window_number (2);
}
void
goto_window_3 (void *data)
{
goto_window_number (3);
}
void
goto_window_4 (void *data)
{
goto_window_number (4);
}
void
goto_window_5 (void *data)
{
goto_window_number (5);
}
void
goto_window_6 (void *data)
{
goto_window_number (6);
}
void
goto_window_7 (void *data)
{
goto_window_number (7);
}
void
goto_window_8 (void *data)
{
goto_window_number (8);
}
void
goto_window_9 (void *data)
{
goto_window_number (9);
}
/* Toggle the display of the program bar */
void
toggle_bar (void *data)
{
screen_info *s;
if (rp_current_window)
{
s = rp_current_window->scr;
if (!hide_bar (s)) show_bar (s);
}
}
void
abort_keypress (void *data)
{
}
/* Send the current window the prefix key event */
void
generate_prefix (void *data)
{
XEvent ev;
ev = *rp_current_event;
ev.xkey.window = rp_current_window->w;
ev.xkey.state = MODIFIER_PREFIX;
XSendEvent (dpy, rp_current_window->w, False, KeyPressMask, &ev);
XSync (dpy, False);
}
/* Maximize a transient window. The current window if data = 0, otherwise assume it is a
pointer to a window that should be maximized */
static void
maximize_transient (void *data)
{
int x, y, maxx, maxy;
rp_window *win = (rp_window *)data;
if (!win) win = rp_current_window;
if (!win) return;
/* Honour the window's maximum size */
if (win->hints->flags & PMaxSize)
{
maxx = win->hints->max_width;
maxy = win->hints->max_height;
}
else
{
maxx = win->width;
maxy = win->height;
}
/* Make sure we maximize to the nearest Resize Increment specified
by the window */
if (win->hints->flags & PResizeInc)
{
int amount;
amount = maxx - win->width;
amount -= amount % win->hints->width_inc;
PRINT_DEBUG ("amount x: %d\n", amount);
maxx = amount + win->width;
amount = maxy - win->height;
amount -= amount % win->hints->height_inc;
PRINT_DEBUG ("amount y: %d\n", amount);
maxy = amount + win->height;
}
PRINT_DEBUG ("maxsize: %d %d\n", maxx, maxy);
x = PADDING_LEFT - win->width / 2 + (win->scr->root_attr.width - PADDING_LEFT - PADDING_RIGHT) / 2;
y = PADDING_TOP - win->height / 2 + (win->scr->root_attr.height - PADDING_TOP - PADDING_BOTTOM) / 2;
XMoveResizeWindow (dpy, win->w, x, y, maxx, maxy);
XSync (dpy, False);
}
/* Maximize the current window if data = 0, otherwise assume it is a
pointer to a window that should be maximized */
void
maximize (void *data)
{
int maxx, maxy;
rp_window *win = (rp_window *)data;
if (!win) win = rp_current_window;
if (!win) return;
/* Handle maximizing transient windows differently */
if (win->transient)
{
maximize_transient (data);
return;
}
/* Honour the window's maximum size */
if (win->hints->flags & PMaxSize)
{
maxx = win->hints->max_width;
maxy = win->hints->max_height;
}
else
{
maxx = win->scr->root_attr.width - PADDING_LEFT - PADDING_RIGHT;
maxy = win->scr->root_attr.height - PADDING_TOP - PADDING_BOTTOM;
}
/* Make sure we maximize to the nearest Resize Increment specified
by the window */
if (win->hints->flags & PResizeInc)
{
int amount;
amount = maxx - win->width;
amount -= amount % win->hints->width_inc;
PRINT_DEBUG ("amount x: %d\n", amount);
maxx = amount + win->width;
amount = maxy - win->height;
amount -= amount % win->hints->height_inc;
PRINT_DEBUG ("amount y: %d\n", amount);
maxy = amount + win->height;
}
PRINT_DEBUG ("maxsize: %d %d\n", maxx, maxy);
XMoveResizeWindow (dpy, win->w, PADDING_LEFT, PADDING_TOP, maxx, maxy);
XSync (dpy, False);
}
|