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
496
497
498
499
500
501
502
503
504
505
506
507
508
|
/*
* Misc function implementation
*
* These are things that either don't fit neatly into another category,
* or fit into a category too small to be worth making individual files
* for.
*/
#include "ctwm.h"
#include <stdlib.h>
#include "animate.h"
#include "ctwm_shutdown.h"
#include "functions.h"
#include "functions_defs.h"
#include "functions_internal.h"
#include "icons.h"
#include "otp.h"
#include "screen.h"
#ifdef SOUNDS
#include "sound.h"
#endif
#include "util.h"
#include "win_iconify.h"
#ifdef WINBOX
#include "windowbox.h"
#endif
#include "workspace_utils.h"
#include "ext/repl_str.h"
/*
* Animation-related
*/
DFHANDLER(startanimation)
{
StartAnimation();
}
DFHANDLER(stopanimation)
{
StopAnimation();
}
DFHANDLER(speedupanimation)
{
ModifyAnimationSpeed(1);
}
DFHANDLER(slowdownanimation)
{
ModifyAnimationSpeed(-1);
}
/*
* Menu-related
*/
DFHANDLER(menu)
{
/*
* n.b.: The f.menu handler is all kinds of magic; it's actually
* completely unrelated to pulling up the menu.
*
* When a button/key binding invokes f.menu to open up a menu, that's
* actually handled in the KeyPress or ButtonPress handlers by
* calling do{_key,}_menu(). When we descend into a submenu, that's
* handled in KeyPress handler for keyboard navigation when we hit
* the Right arrow, or inside the
* event loop recapture in UpdateMenu() for mouse navigation when we
* move it to the right side of the menu entry.
*
* This handler is only used by "invoking" a menu item; releasing the
* mouse button on the left side without moving right to open out the
* submenu, or hitting the Enter key. All it does is immediately
* invoke the default entry, if there is one.
*/
if(action && ! strncmp(action, "WGOTO : ", 8)) {
GotoWorkSpaceByName(/* XXXXX */ Scr->currentvs,
((char *)action) + 8);
}
else {
MenuItem *item;
item = ActiveItem;
while(item && item->sub) {
if(!item->sub->defaultitem) {
break;
}
if(item->sub->defaultitem->func != F_MENU) {
break;
}
item = item->sub->defaultitem;
}
if(item && item->sub && item->sub->defaultitem) {
ExecuteFunction(item->sub->defaultitem->func,
item->sub->defaultitem->action,
w, tmp_win, eventp, context, pulldown);
}
}
}
DFHANDLER(pin)
{
if(! ActiveMenu) {
return;
}
if(ActiveMenu->pinned) {
XUnmapWindow(dpy, ActiveMenu->w);
ActiveMenu->mapped = MRM_UNMAPPED;
}
else {
XWindowAttributes attr;
MenuRoot *menu;
if(ActiveMenu->pmenu == NULL) {
menu = malloc(sizeof(MenuRoot));
*menu = *ActiveMenu;
menu->pinned = true;
menu->mapped = MRM_NEVER;
menu->width -= 10;
if(menu->pull) {
menu->width -= 16 + 10;
}
MakeMenu(menu);
ActiveMenu->pmenu = menu;
}
else {
menu = ActiveMenu->pmenu;
}
if(menu->mapped == MRM_MAPPED) {
return;
}
XGetWindowAttributes(dpy, ActiveMenu->w, &attr);
menu->x = attr.x;
menu->y = attr.y;
XMoveWindow(dpy, menu->w, menu->x, menu->y);
XMapRaised(dpy, menu->w);
menu->mapped = MRM_MAPPED;
}
PopDownMenu();
}
/*
* Alternate keymaps/contexts
*/
DFHANDLER(altkeymap)
{
int alt, stat_;
if(! action) {
return;
}
stat_ = sscanf(action, "%d", &alt);
if(stat_ != 1) {
return;
}
if((alt < 1) || (alt > 5)) {
return;
}
AlternateKeymap = Alt1Mask << (alt - 1);
XGrabPointer(dpy, Scr->Root, True, ButtonPressMask | ButtonReleaseMask,
GrabModeAsync, GrabModeAsync,
Scr->Root, Scr->AlterCursor, CurrentTime);
func_reset_cursor = false; // Leave special cursor alone
XGrabKeyboard(dpy, Scr->Root, True, GrabModeAsync, GrabModeAsync, CurrentTime);
return;
}
DFHANDLER(altcontext)
{
AlternateContext = true;
XGrabPointer(dpy, Scr->Root, False, ButtonPressMask | ButtonReleaseMask,
GrabModeAsync, GrabModeAsync,
Scr->Root, Scr->AlterCursor, CurrentTime);
func_reset_cursor = false; // Leave special cursor alone
XGrabKeyboard(dpy, Scr->Root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
return;
}
/*
* A few trivial ctwm-control-ish meta-functions
*/
DFHANDLER(quit)
{
DoShutdown();
}
DFHANDLER(restart)
{
DoRestart(eventp->xbutton.time);
}
DFHANDLER(beep)
{
XBell(dpy, 0);
}
DFHANDLER(trace)
{
DebugTrace(action);
}
#ifdef WINBOX
/*
* Special windowbox-related
*/
DFHANDLER(fittocontent)
{
if(!tmp_win->iswinbox) {
XBell(dpy, 0);
return;
}
fittocontent(tmp_win);
}
#endif
/*
* A few things that are sorta windows/icons related, but don't really
* fit with the window-targetted things in functions_win.
*/
DFHANDLER(showbackground)
{
ShowBackground(Scr->currentvs, -1);
}
DFHANDLER(raiseicons)
{
for(TwmWindow *t = Scr->FirstWindow; t != NULL; t = t->next) {
if(t->icon && t->icon->w) {
OtpRaise(t, IconWin);
}
}
}
DFHANDLER(rescuewindows)
{
RescueWindows();
}
/*
* Despite the name, this is more like 'gotoworkspace' than the other
* 'warpto*' funcs, as it's just about switching your view, not anything
* going to a window.
*/
static void
WarpToScreen(int n, int inc)
{
Window dumwin;
int x, y, dumint;
unsigned int dummask;
ScreenInfo *newscr = NULL;
while(!newscr) {
/* wrap around */
if(n < 0) {
n = NumScreens - 1;
}
else if(n >= NumScreens) {
n = 0;
}
newscr = ScreenList[n];
if(!newscr) { /* make sure screen is managed */
if(inc) { /* walk around the list */
n += inc;
continue;
}
fprintf(stderr, "%s: unable to warp to unmanaged screen %d\n",
ProgramName, n);
XBell(dpy, 0);
return;
}
}
if(Scr->screen == n) {
return; /* already on that screen */
}
PreviousScreen = Scr->screen;
XQueryPointer(dpy, Scr->Root, &dumwin, &dumwin, &x, &y,
&dumint, &dumint, &dummask);
XWarpPointer(dpy, None, newscr->Root, 0, 0, 0, 0, x, y);
Scr = newscr;
return;
}
DFHANDLER(warptoscreen)
{
if(strcmp(action, WARPSCREEN_NEXT) == 0) {
WarpToScreen(Scr->screen + 1, 1);
}
else if(strcmp(action, WARPSCREEN_PREV) == 0) {
WarpToScreen(Scr->screen - 1, -1);
}
else if(strcmp(action, WARPSCREEN_BACK) == 0) {
WarpToScreen(PreviousScreen, 0);
}
else {
WarpToScreen(atoi(action), 0);
}
}
/*
* Sound-related
*/
#ifdef SOUNDS
DFHANDLER(togglesound)
{
toggle_sound();
}
DFHANDLER(rereadsounds)
{
reread_sounds();
}
#endif
/*
* And executing an external program
*/
static void Execute(const char *_s);
DFHANDLER(exec)
{
PopDownMenu();
if(!Scr->NoGrabServer) {
XUngrabServer(dpy);
XSync(dpy, 0);
}
XUngrabPointer(dpy, CurrentTime);
XSync(dpy, 0);
Execute(action);
}
static void
Execute(const char *_s)
{
char *s;
char *_ds;
char *orig_display;
int restorevar = 0;
char *subs;
/* Seatbelt */
if(!_s) {
return;
}
/* Work on a local copy since we're mutating it */
s = strdup(_s);
if(!s) {
return;
}
/* Stash up current $DISPLAY value for resetting */
orig_display = getenv("DISPLAY");
/*
* Build a display string using the current screen number, so that
* X programs which get fired up from a menu come up on the screen
* that they were invoked from, unless specifically overridden on
* their command line.
*
* Which is to say, given that we're on display "foo.bar:1.2", we
* want to translate that into "foo.bar:1.{Scr->screen}".
*
* We strdup() because DisplayString() is a macro returning into the
* dpy structure, and we're going to mutate the value we get from it.
*/
_ds = DisplayString(dpy);
if(_ds) {
char *ds;
char *colon;
ds = strdup(_ds);
if(!ds) {
goto end_execute;
}
/* If it's not host:dpy, we don't have anything to do here */
colon = strrchr(ds, ':');
if(colon) {
char *dot, *new_display;
/* Find the . in display.screen and chop it off */
dot = strchr(colon, '.');
if(dot) {
*dot = '\0';
}
/* Build a new string with our correct screen info */
asprintf(&new_display, "%s.%d", ds, Scr->screen);
if(!new_display) {
free(ds);
goto end_execute;
}
/* And set */
setenv("DISPLAY", new_display, 1);
free(new_display);
restorevar = 1;
}
free(ds);
}
/*
* We replace a couple placeholders in the string. $currentworkspace
* is documented in the manual; $redirect is not.
*/
subs = strstr(s, "$currentworkspace");
if(subs) {
char *tmp;
char *wsname;
wsname = GetCurrentWorkSpaceName(Scr->currentvs);
if(!wsname) {
wsname = "";
}
tmp = replace_substr(s, "$currentworkspace", wsname);
if(!tmp) {
goto end_execute;
}
free(s);
s = tmp;
}
#ifdef CAPTIVE
subs = strstr(s, "$redirect");
if(subs) {
char *tmp;
char *redir;
if(CLarg.is_captive) {
asprintf(&redir, "-xrm 'ctwm.redirect:%s'", Scr->captivename);
if(!redir) {
goto end_execute;
}
}
else {
redir = malloc(1);
*redir = '\0';
}
tmp = replace_substr(s, "$redirect", redir);
free(s);
s = tmp;
free(redir);
}
#endif
/*
* Call it. Return value doesn't really matter, since whatever
* happened we're done. Maybe someday if we develop a "show user
* message" generalized func, we can tell the user if executing
* failed somehow.
*/
system(s);
/*
* Restore $DISPLAY if we changed it. It's probably only necessary
* in edge cases (it might be used by ctwm restarting itself, for
* instance) and it's not quite clear whether the DisplayString()
* result would even be wrong for that, but what the heck, setenv()
* is cheap.
*/
if(restorevar) {
if(orig_display) {
setenv("DISPLAY", orig_display, 1);
}
else {
unsetenv("DISPLAY");
}
}
/* Clean up */
end_execute:
free(s);
return;
}
|