-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfling.cc
507 lines (456 loc) · 14.9 KB
/
fling.cc
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
#include "wmhack.h"
#include <poll.h>
#include <X11/Xatom.h>
#include <X11/keysymdef.h>
#include <sys/time.h>
#include <string>
#include <string.h>
#include <map>
#include <set>
static int intarg() { return atoi(optarg); } // XXX: use strtol and invoke usage()
static bool nodo = false;
static unsigned int border = 2;
static bool glide = true;
constexpr int MAXIDLE = 3000;
extern char readme_txt[];
static void
usage(std::ostream &stream)
{
stream << readme_txt;
exit(1);
}
static void
adjustForStruts(const X11Env &x11, Geometry *g, long targetDesktop)
{
Atom actualType;
int actualFormat;
unsigned long itemCount;
unsigned long afterBytes;
unsigned char *winlist;
// get a list of all clients, so we can adjust monitor sizes for extents.
int rc = XGetWindowProperty(x11, x11.root, x11.NetClientList,
0, std::numeric_limits<long>::max(), False, x11.AWindow,
&actualType, &actualFormat, &itemCount, &afterBytes, &winlist);
if (rc != 0 || actualFormat != 32 || itemCount <= 0 ) {
std::cerr << "can't list clients to do strut processing" << std::endl;
return;
}
Window *w = (Window *)winlist;
for (size_t i = itemCount; i-- > 0;) {
auto clipDesktop = x11.desktopForWindow(w[i]);
unsigned char *prop;
if (clipDesktop == targetDesktop || clipDesktop == -1 || targetDesktop == -1) {
rc = XGetWindowProperty(x11, w[i], x11.NetWmStrutPartial,
0, std::numeric_limits<long>::max(), False, x11.Cardinal,
&actualType, &actualFormat, &itemCount, &afterBytes, &prop);
if (rc == 0) {
if (itemCount == 12 && actualFormat == 32) {
PartialStrut *strut = (PartialStrut *)prop;
strut->box(x11, *g);
}
XFree(prop);
} else {
unsigned char *prop;
rc = XGetWindowProperty(x11, w[i], x11.NetWmStrut,
0, std::numeric_limits<long>::max(), False, x11.Cardinal,
&actualType, &actualFormat, &itemCount, &afterBytes, &prop);
if (rc == 0) {
std::clog << "TODO: deal with legacy strut\n";
XFree(prop);
}
}
}
}
XFree(winlist);
}
static void
setOpacityRaw(const X11Env &x11, Window w, unsigned long opacity)
{
XChangeProperty(x11, w, x11.NetWmOpacity, XA_CARDINAL, 32, PropModeReplace,
(unsigned char *)&opacity, 1);
XFlush(x11);
}
static void
setOpacity(const X11Env &x11, Window w, double opacity)
{
setOpacityRaw(x11, w, opacity * std::numeric_limits<uint32_t>::max());
}
static double
getOpacity(const X11Env &x11, Window w)
{
Atom actualType;
int actualFormat;
unsigned char *property;
unsigned long items, size;
int rc = XGetWindowProperty(x11, w, x11.NetWmOpacity, 0, 1, False, XA_CARDINAL,
&actualType, &actualFormat, &items, &size, &property);
unsigned long propertyValue = rc == 0 && items == 1 ? *(unsigned long *)property : std::numeric_limits<uint32_t>::max();
propertyValue &= 0xffffffff; // XXX: Seems to get sign extended.
return double(propertyValue) / std::numeric_limits<uint32_t>::max();
}
static void
setOpacityDelta(const X11Env &x11, Window w, double opacity)
{
setOpacity(x11, w, std::max(0.0, std::min(1.0, getOpacity(x11, w) + opacity)));
}
static void
setWorkdir(const X11Env &x11, Window w, const char *value)
{
XChangeProperty(x11, w, x11.WorkDir, XA_STRING, 8, PropModeReplace,
(const unsigned char *)value, strlen(value));
XFlush(x11);
}
void
resizeWindow(X11Env &x11,
long desktop, Geometry &geom,
Window win,
unsigned *border,
const long *frame,
const char *location)
{
char curChar;
for (const char *path = location; (curChar = *path) != 0; ++path) {
double num = 1, denom = 2;
if (isdigit(curChar) || curChar == '.') {
char *newpath;
num = 1;
denom = strtod(path, &newpath);
path = newpath;
curChar = *path;
if (curChar == '/') {
num = denom;
denom = strtod(path + 1, &newpath);
path = newpath;
curChar = *path;
}
}
switch (curChar) {
case 'b':
*border = denom;
break;
case 'r':
// move to right
geom.x += geom.size.width - geom.size.width * (num / denom);
// and then...
case 'l':
// cut out right hand side.
geom.size.width = geom.size.width * num / denom;
break;
case 'd':
geom.y += geom.size.height - geom.size.height * (num / denom);
// and then...
case 'u':
geom.size.height = geom.size.height * (num / denom);
break;
case 'h': {
// reduce horizontal size and centre
int newsize = geom.size.width * (num / denom);
geom.x += (geom.size.width - newsize) / 2;
geom.size.width = newsize;
break;
}
case 'v': {
// reduce vertical size and centre
int newsize = geom.size.height * (num / denom);
geom.y += (geom.size.height - newsize) / 2;
geom.size.height = newsize;
break;
}
default:
usage(std::cerr);
}
}
adjustForStruts(x11, &geom, desktop);
// Adjust the geometry downwards to account for the frame around the window, and our border.
geom.size.width -= frame[0] + frame[1] + *border * 2;
geom.size.height -= frame[2] + frame[3] + *border * 2;
geom.x += frame[0] + *border;
geom.y += frame[2] + *border;
Geometry oldgeom = x11.getGeometry(win);
int duration = 150000;
int sleeptime = 500000 / 60;
int iters = duration / sleeptime;
if (glide) {
#define update(f) next.f = (oldgeom.f * (iters - i) + geom.f * i) / iters
for (auto i = 1;; ++i) {
Geometry next;
update(size.width);
update(size.height);
update(x);
update(y);
x11.setGeometry(win, next);
if (i == iters)
break;
usleep(sleeptime);
}
#undef update
} else {
x11.setGeometry(win, geom);
}
}
long
msecDiff(const timeval &l, const timeval &r)
{
suseconds_t usec = l.tv_usec - r.tv_usec;
time_t sec = l.tv_sec - r.tv_sec;
if (l.tv_usec < r.tv_usec) {
usec += 1000000;
sec -= 1;
}
return usec / 1000 + sec * 1000;
}
int
catchmain(int argc, char *argv[])
{
int screen = -1, c;
int verbose = 0;
bool doPick = false;
bool interactive = false;
double opacity = -1;
double opacityDelta = 0;
bool windowRelative = false;
Window win = 0;
const char *workdir = 0;
std::set<Atom> toggles;
Display *display = XOpenDisplay(0);
if (display == 0) {
std::clog << "failed to open display: set DISPLAY environment variable" << std::endl;
return 1;
}
X11Env x11(display);
if (argc == 1)
usage(std::cerr);
X11Env::StateUpdateAction action = X11Env::TOGGLE;
while ((c = getopt(argc, argv, "o:s:w:W:abfghimnpuvx_O:YNA")) != -1) {
switch (c) {
case 'N':
action = X11Env::REMOVE;
break;
case 'Y':
action = X11Env::ADD;
break;
case 'p':
doPick = true;
break;
case 's':
screen = intarg();
break;
case 'n':
nodo = true;
break;
case 'b':
toggles.insert(x11.NetWmStateBelow);
break;
case 'f':
toggles.insert(x11.NetWmStateFullscreen);
break;
case 'm':
toggles.insert(x11.NetWmStateMaximizedHoriz);
break;
case 'h':
usage(std::cout);
break;
case '_':
toggles.insert(x11.NetWmStateShaded);
break;
case 'a':
toggles.insert(x11.NetWmStateAbove);
break;
case 'u':
toggles.insert(x11.NetWmStateBelow);
break;
case 'o':
opacity = strtod(optarg, 0);
if (opacity < 0.0 || opacity > 1)
usage(std::cerr);
break;
case 'O':
opacityDelta = strtod(optarg, 0);
break;
case 'w':
win = intarg();
break;
case 'x':
windowRelative = true;
border = 0; // assume the window already has adequate space around it.
break;
case 'W':
workdir = optarg;
break;
case 'v':
verbose++;
break;
case 'i':
interactive = true;
break;
case 'g':
glide = !glide;
break;
default:
usage(std::cerr);
break;
}
}
// Which window are we modifying?
if (win == 0)
win = doPick ? x11.pick() : x11.active();
if (win == 0) {
std::cerr << "no window selected\n";
return 0;
}
std::clog << "updating " << win << "\n";
// If we're doing state toggles/misc changes to window, do it now.
if (opacity >= 0.0)
setOpacity(x11, win, opacity);
if (opacityDelta != 0.0)
setOpacityDelta(x11, win, opacityDelta);
if (workdir != 0)
setWorkdir(x11, win, workdir);
for (auto atom : toggles)
x11.updateState(win, atom, action);
// If nothing else to do, just exit.
if (argc == optind && !interactive)
return 0;
if (screen == -1)
screen = x11.monitorForWindow(win);
/*
* get the extent of the frame around the window: we assume the new frame
* will have the same extents when we resize it, and use that to adjust the
* position of the client window so its frame abuts the edge of the screen.
*/
Atom actualType;
int actualFormat;
unsigned long itemCount;
unsigned long afterBytes;
unsigned char *prop;
long desktop;
int rc;
const long *frame;
frame = 0;
rc = XGetWindowProperty(x11, win, x11.NetFrameExtents,
0, std::numeric_limits<long>::max(), False, x11.Cardinal,
&actualType, &actualFormat, &itemCount, &afterBytes, &prop);
bool haveFrame = rc == 0 && actualFormat == 32 && itemCount == 4;
if (!haveFrame) {
std::cerr << "can't find frame sizes: rc = " << rc << ", actualFormat="
<< actualFormat << ", itemCount=" << itemCount << std::endl;
static long defaultFrame[] = { 0, 0, 0, 0 };
if (frame)
XFree((void *)frame);
frame = defaultFrame;
} else {
frame = (long *)prop;
}
// Work out starting geometry - either existing size, or entire window
Geometry window;
if (windowRelative) {
window = x11.getGeometry(win);
if (rc == 0)
XFree(prop);
} else {
window = x11.monitors[screen];
}
/*
* Find desktop of the window in question - we ignore windows on other
* desktops for struts avoidance, etc.
*/
desktop = x11.desktopForWindow(win);
// Remove any toggles that make the window size moot.
x11.updateState(win, x11.NetWmStateShaded, X11Env::REMOVE);
x11.updateState(win, x11.NetWmStateMaximizedHoriz, X11Env::REMOVE);
x11.updateState(win, x11.NetWmStateFullscreen, X11Env::REMOVE);
if (interactive) {
auto keyWin = XCreateSimpleWindow(x11, x11.root, 0, 0, 1, 1, 0, 0, 0);
if (keyWin == 0)
abort();
XMapWindow(x11, keyWin);
XFlush(x11);
XSelectInput(x11, keyWin, ExposureMask | KeyPressMask);
int symsPerKey, minCodes, maxCodes;
auto codes = XDisplayKeycodes(x11, &minCodes, &maxCodes);
if (!codes)
abort();
auto keySyms = XGetKeyboardMapping(x11, minCodes, maxCodes - minCodes, &symsPerKey);
static std::map<int, const char *> keyToOperation = {
{ XK_Up, "u" },
{ XK_Down, "d" },
{ XK_Left, "l" },
{ XK_Right, "r" },
{ XK_KP_8, "u" },
{ XK_KP_2, "d" },
{ XK_KP_4, "l" },
{ XK_KP_6, "r" },
{ XK_KP_7, "ul" },
{ XK_KP_9, "ur" },
{ XK_KP_1, "dl" },
{ XK_KP_3, "dr" },
{ XK_KP_Up, "u" },
{ XK_KP_Down, "d" },
{ XK_KP_Left, "l" },
{ XK_KP_Right, "r" },
{ XK_KP_Home, "ul" },
{ XK_KP_Page_Up, "ur" },
{ XK_KP_End, "dl" },
{ XK_KP_Page_Down, "dr" }
};
int fd = ConnectionNumber(x11.display);
struct pollfd pfd;
pfd.events = POLLIN;
pfd.revents = 0;
pfd.fd = fd;
struct timeval lastKey;
gettimeofday(&lastKey, 0);
for (bool done = false; !done;) {
struct timeval now;
gettimeofday(&now, 0);
long wait = MAXIDLE - msecDiff(now, lastKey);
poll(&pfd, 1, wait);
gettimeofday(&now, 0);
if (msecDiff(now, lastKey) > MAXIDLE)
exit(0);
XEvent event;
XNextEvent(x11, &event);
switch (event.type) {
case KeyPress:
gettimeofday(&lastKey, 0);
auto i = (event.xkey.keycode - minCodes) * symsPerKey;
auto todo = keyToOperation.find(keySyms[i]);
if (todo == keyToOperation.end())
exit(0);
resizeWindow(x11, desktop, window, win, &border, frame, todo->second);
break;
}
}
} else {
static std::map<std::string, const char *> aliases = {
{ "top", "u" },
{ "bottom", "d" },
{ "left", "l" },
{ "right", "r" },
{ "topleft", "ul" },
{ "topright", "ur" },
{ "bottomleft", "dl" },
{ "bottomright", "dr" },
};
const char *location = argv[optind];
auto alias = aliases.find(location);
if (alias != aliases.end())
location = alias->second;
resizeWindow(x11, desktop, window, win, &border, frame, location);
}
if (haveFrame)
XFree((unsigned char *)frame);
XCloseDisplay(display);
return 0;
}
int
main(int argc, char *argv[])
{
try {
return catchmain(argc, argv);
}
catch (const char *msg) {
std::clog << "internal error: " << msg << "\n";
return 1;
}
}