-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindows.c
560 lines (508 loc) · 15.9 KB
/
windows.c
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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
// WINDOWS
// "How should this window appear and behave?"
#include "atlas.h"
#include "config.h"
#include "util.h"
#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <stdlib.h>
#include <string.h>
static const char broken[] = "BORKED";
/* HACK: Need to implement TOML config for these */
static const Rule rules[] = {
/* xprop(1):
* WM_CLASS(STRING) = instance, class
* WM_NAME(STRING) = title
*/
/* class instance title tags mask isfloating monitor */
{"Gimp", NULL, NULL, 0, 1, -1},
{"Firefox", NULL, NULL, 1 << 8, 0, -1},
};
/* HACK: End of hack*/
void manage(Window w, XWindowAttributes *wa) {
Client *c, *t = NULL;
Window trans = None;
XWindowChanges wc;
c = ecalloc(1, sizeof(Client));
c->win = w;
/* geometry */
c->x = c->oldx = wa->x;
c->y = c->oldy = wa->y;
c->w = c->oldw = wa->width;
c->h = c->oldh = wa->height;
c->oldBorderWidth = wa->border_width;
c->horizontalRatio = 0.5;
c->verticalRatio = 0.5;
if (XGetTransientForHint(display, w, &trans) &&
(t = findClientFromWindow(trans))) {
c->monitor = t->monitor;
c->workspaces = t->workspaces;
} else {
c->monitor = selectedMonitor;
applyWindowRules(c);
}
if (c->x + WIDTH(c) > c->monitor->wx + c->monitor->ww)
c->x = c->monitor->wx + c->monitor->ww - WIDTH(c);
if (c->y + HEIGHT(c) > c->monitor->wy + c->monitor->wh)
c->y = c->monitor->wy + c->monitor->wh - HEIGHT(c);
c->x = MAX(c->x, c->monitor->wx);
c->y = MAX(c->y, c->monitor->wy);
c->borderWidth = cfg.borderWidth;
wc.border_width = c->borderWidth;
XConfigureWindow(display, w, CWBorderWidth, &wc);
Clr borderColor;
drw_clr_create(drawContext, &borderColor, cfg.borderInactiveColor);
XSetWindowBorder(display, w, borderColor.pixel);
configure(c); /* propagates border_width, if size doesn't change */
updateWindowTypeProps(c);
updateWindowSizeHints(c);
updateWindowManagerHints(c);
XSelectInput(display, w,
EnterWindowMask | FocusChangeMask | PropertyChangeMask |
StructureNotifyMask);
registerMouseButtons(c, 0);
if (!c->isFloating)
c->isFloating = c->previousState = trans != None || c->isFixedSize;
if (c->isFloating)
XRaiseWindow(display, c->win);
attach(c);
attachWindowToStack(c);
XChangeProperty(display, root, netAtoms[NET_CLIENT_LIST], XA_WINDOW, 32,
PropModeAppend, (unsigned char *)&(c->win), 1);
XMoveResizeWindow(display, c->win, c->x + 2 * screenWidth, c->y, c->w,
c->h); /* some windows require this */
setclientstate(c, NormalState);
if (c->monitor == selectedMonitor)
unfocus(selectedMonitor->active, 0);
c->monitor->active = c;
arrange(c->monitor);
XMapWindow(display, c->win);
if (cfg.focusNewWindows) {
focus(c);
moveCursorToClientCenter(c);
} else {
focus(NULL);
}
}
void unmanage(Client *c, int destroyed) {
Monitor *m = c->monitor;
XWindowChanges wc;
Client *prev = NULL;
Client *curr = m->clients;
// Find the previous client
while (curr && curr != c) {
prev = curr;
curr = curr->next;
}
detach(c);
detachWindowFromStack(c);
if (!destroyed) {
wc.border_width = c->oldBorderWidth;
XGrabServer(display); /* avoid race conditions */
XSetErrorHandler(handleXErrorDummy);
XSelectInput(display, c->win, NoEventMask);
XConfigureWindow(display, c->win, CWBorderWidth, &wc); /* restore border */
XUngrabButton(display, AnyButton, AnyModifier, c->win);
setclientstate(c, WithdrawnState);
XSync(display, False);
XSetErrorHandler(handleXError);
XUngrabServer(display);
}
free(c);
focus(prev);
updateClientList();
arrange(m);
}
void updateWindowTitle(Client *c) {
if (!gettextprop(c->win, netAtoms[NET_WM_NAME], c->name, sizeof c->name))
gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
if (c->name[0] == '\0') /* hack to mark broken clients */
strcpy(c->name, broken);
}
void updateWindowTypeProps(Client *c) {
Atom state = getatomprop(c, netAtoms[NET_WM_STATE]);
Atom wtype = getatomprop(c, netAtoms[NET_WM_WINDOW_TYPE]);
if (state == netAtoms[NET_WM_FULLSCREEN])
setWindowFullscreen(c, 1);
if (wtype == netAtoms[NET_WM_WINDOW_TYPE_DIALOG])
c->isFloating = 1;
}
void updateWindowManagerHints(Client *c) {
XWMHints *wmh;
if ((wmh = XGetWMHints(display, c->win))) {
if (c == selectedMonitor->active && wmh->flags & XUrgencyHint) {
wmh->flags &= ~XUrgencyHint;
XSetWMHints(display, c->win, wmh);
} else
c->isUrgent = (wmh->flags & XUrgencyHint) ? 1 : 0;
if (wmh->flags & InputHint)
c->neverFocus = !wmh->input;
else
c->neverFocus = 0;
XFree(wmh);
}
}
void updateWindowSizeHints(Client *c) {
long msize;
XSizeHints size;
if (!XGetWMNormalHints(display, c->win, &size, &msize))
/* size is uninitialized, ensure that size.flags aren't used */
size.flags = PSize;
if (size.flags & PBaseSize) {
c->basew = size.base_width;
c->baseh = size.base_height;
} else if (size.flags & PMinSize) {
c->basew = size.min_width;
c->baseh = size.min_height;
} else
c->basew = c->baseh = 0;
if (size.flags & PResizeInc) {
c->incw = size.width_inc;
c->inch = size.height_inc;
} else
c->incw = c->inch = 0;
if (size.flags & PMaxSize) {
c->maxw = size.max_width;
c->maxh = size.max_height;
} else
c->maxw = c->maxh = 0;
if (size.flags & PMinSize) {
c->minw = size.min_width;
c->minh = size.min_height;
} else if (size.flags & PBaseSize) {
c->minw = size.base_width;
c->minh = size.base_height;
} else
c->minw = c->minh = 0;
if (size.flags & PAspect) {
c->minAspectRatio = (float)size.min_aspect.y / size.min_aspect.x;
c->maxAspectRatio = (float)size.max_aspect.x / size.max_aspect.y;
} else
c->maxAspectRatio = c->minAspectRatio = 0.0;
c->isFixedSize =
(c->maxw && c->maxh && c->maxw == c->minw && c->maxh == c->minh);
c->hintsvalid = 1;
}
void configure(Client *c) {
XConfigureEvent ce;
ce.type = ConfigureNotify;
ce.display = display;
ce.event = c->win;
ce.window = c->win;
ce.x = c->x;
ce.y = c->y;
ce.width = c->w;
ce.height = c->h;
ce.border_width = c->borderWidth;
ce.above = None;
ce.override_redirect = False;
XSendEvent(display, c->win, False, StructureNotifyMask, (XEvent *)&ce);
}
void applyWindowRules(Client *c) {
const char *class, *instance;
unsigned int i;
const Rule *r;
Monitor *m;
XClassHint ch = {NULL, NULL};
/* rule matching */
c->isFloating = 0;
c->workspaces = 0;
XGetClassHint(display, c->win, &ch);
class = ch.res_class ? ch.res_class : broken;
instance = ch.res_name ? ch.res_name : broken;
for (i = 0; i < LENGTH(rules); i++) {
r = &rules[i];
if ((!r->title || strstr(c->name, r->title)) &&
(!r->class || strstr(class, r->class)) &&
(!r->instance || strstr(instance, r->instance))) {
c->isFloating = r->isfloating;
c->workspaces |= r->tags;
for (m = monitors; m && m->num != r->monitor; m = m->next)
;
if (m)
c->monitor = m;
}
}
if (ch.res_class)
XFree(ch.res_class);
if (ch.res_name)
XFree(ch.res_name);
c->workspaces =
c->workspaces & WORKSPACEMASK
? c->workspaces & WORKSPACEMASK
: c->monitor->workspaceset[c->monitor->selectedWorkspaces];
}
int applyWindowSizeConstraints(Client *c, int *x, int *y, int *w, int *h,
int interact) {
int baseismin;
Monitor *m = c->monitor;
/* set minimum possible */
*w = MAX(1, *w);
*h = MAX(1, *h);
if (interact) {
if (*x > screenWidth)
*x = screenWidth - WIDTH(c);
if (*y > screenHeight)
*y = screenHeight - HEIGHT(c);
if (*x + *w + 2 * c->borderWidth < 0)
*x = 0;
if (*y + *h + 2 * c->borderWidth < 0)
*y = 0;
} else {
if (*x >= m->wx + m->ww)
*x = m->wx + m->ww - WIDTH(c);
if (*y >= m->wy + m->wh)
*y = m->wy + m->wh - HEIGHT(c);
if (*x + *w + 2 * c->borderWidth <= m->wx)
*x = m->wx;
if (*y + *h + 2 * c->borderWidth <= m->wy)
*y = m->wy;
}
if (c->isFloating ||
!c->monitor->layouts[c->monitor->selectedLayout]->arrange) {
if (!c->hintsvalid)
updateWindowSizeHints(c);
/* see last two sentences in ICCCM 4.1.2.3 */
baseismin = c->basew == c->minw && c->baseh == c->minh;
if (!baseismin) { /* temporarily remove base dimensions */
*w -= c->basew;
*h -= c->baseh;
}
/* adjust for aspect limits */
if (c->minAspectRatio > 0 && c->maxAspectRatio > 0) {
if (c->maxAspectRatio < (float)*w / *h)
*w = *h * c->maxAspectRatio + 0.5;
else if (c->minAspectRatio < (float)*h / *w)
*h = *w * c->minAspectRatio + 0.5;
}
if (baseismin) { /* increment calculation requires this */
*w -= c->basew;
*h -= c->baseh;
}
/* adjust for increment value */
if (c->incw)
*w -= *w % c->incw;
if (c->inch)
*h -= *h % c->inch;
/* restore base dimensions */
*w = MAX(*w + c->basew, c->minw);
*h = MAX(*h + c->baseh, c->minh);
if (c->maxw)
*w = MIN(*w, c->maxw);
if (c->maxh)
*h = MIN(*h, c->maxh);
}
return *x != c->x || *y != c->y || *w != c->w || *h != c->h;
}
void setWindowFullscreen(Client *c, int fullscreen) {
if (fullscreen && !c->isFullscreen) {
XChangeProperty(display, c->win, netAtoms[NET_WM_STATE], XA_ATOM, 32,
PropModeReplace,
(unsigned char *)&netAtoms[NET_WM_FULLSCREEN], 1);
c->isFullscreen = 1;
c->previousState = c->isFloating;
c->oldBorderWidth = c->borderWidth;
c->borderWidth = 0;
c->isFloating = 1;
resizeclient(c, c->monitor->mx, c->monitor->my, c->monitor->mw,
c->monitor->mh);
XRaiseWindow(display, c->win);
} else if (!fullscreen && c->isFullscreen) {
XChangeProperty(display, c->win, netAtoms[NET_WM_STATE], XA_ATOM, 32,
PropModeReplace, (unsigned char *)0, 0);
c->isFullscreen = 0;
c->isFloating = c->previousState;
c->borderWidth = c->oldBorderWidth;
c->x = c->oldx;
c->y = c->oldy;
c->w = c->oldw;
c->h = c->oldh;
resizeclient(c, c->x, c->y, c->w, c->h);
arrange(c->monitor);
}
}
void setWindowUrgent(Client *c, int urg) {
XWMHints *wmh;
c->isUrgent = urg;
if (!(wmh = XGetWMHints(display, c->win)))
return;
wmh->flags = urg ? (wmh->flags | XUrgencyHint) : (wmh->flags & ~XUrgencyHint);
XSetWMHints(display, c->win, wmh);
XFree(wmh);
}
void toggleWindowFloating(const Arg *arg) {
if (!selectedMonitor->active)
return;
if (selectedMonitor->active
->isFullscreen) /* no support for fullscreen windows */
return;
selectedMonitor->active->isFloating = !selectedMonitor->active->isFloating ||
selectedMonitor->active->isFixedSize;
if (selectedMonitor->active->isFloating)
resize(selectedMonitor->active, selectedMonitor->active->x,
selectedMonitor->active->y, selectedMonitor->active->w,
selectedMonitor->active->h, 0);
arrange(selectedMonitor);
}
void toggleWindowVisibility(Client *c) {
if (!c)
return;
if (ISVISIBLE(c)) {
/* show clients top down */
XMoveWindow(display, c->win, c->x, c->y);
if ((!c->monitor->layouts[c->monitor->selectedLayout]->arrange ||
c->isFloating) &&
!c->isFullscreen)
resize(c, c->x, c->y, c->w, c->h, 0);
toggleWindowVisibility(c->nextInStack);
} else {
/* hide clients bottom up */
toggleWindowVisibility(c->nextInStack);
XMoveWindow(display, c->win, WIDTH(c) * -2, c->y);
}
}
void resize(Client *c, int x, int y, int w, int h, int interact) {
if (applyWindowSizeConstraints(c, &x, &y, &w, &h, interact))
resizeclient(c, x, y, w, h);
}
void resizeclient(Client *c, int x, int y, int w, int h) {
XWindowChanges wc;
c->oldx = c->x;
c->x = wc.x = x;
c->oldy = c->y;
c->y = wc.y = y;
c->oldw = c->w;
c->w = wc.width = w;
c->oldh = c->h;
c->h = wc.height = h;
wc.border_width = c->borderWidth;
XConfigureWindow(display, c->win,
CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
configure(c);
XSync(display, False);
}
void setclientstate(Client *c, long state) {
long data[] = {state, None};
XChangeProperty(display, c->win, wmAtoms[WM_STATE], wmAtoms[WM_STATE], 32,
PropModeReplace, (unsigned char *)data, 2);
}
int sendevent(Client *c, Atom proto) {
int n;
Atom *protocols;
int exists = 0;
XEvent ev;
if (XGetWMProtocols(display, c->win, &protocols, &n)) {
while (!exists && n--)
exists = protocols[n] == proto;
XFree(protocols);
}
if (exists) {
ev.type = ClientMessage;
ev.xclient.window = c->win;
ev.xclient.message_type = wmAtoms[WM_PROTOCOLS];
ev.xclient.format = 32;
ev.xclient.data.l[0] = proto;
ev.xclient.data.l[1] = CurrentTime;
XSendEvent(display, c->win, False, NoEventMask, &ev);
}
return exists;
}
int shouldscale(Client *c) {
return (c && !c->isFixedSize && !c->isFloating && !c->isFullscreen);
}
void scaleclient(Client *c, int x, int y, int w, int h, float scale) {
if (!shouldscale(c))
return;
int new_w = w * scale;
int new_h = h * scale;
int new_x = x + (w - new_w) / 2;
int new_y = y + (h - new_h) / 2;
resize(c, new_x, new_y, new_w - 2 * c->borderWidth,
new_h - 2 * c->borderWidth, 0);
}
Atom getatomprop(Client *c, Atom prop) {
int di;
unsigned long dl;
unsigned char *p = NULL;
Atom da, atom = None;
if (XGetWindowProperty(display, c->win, prop, 0L, sizeof atom, False, XA_ATOM,
&da, &di, &dl, &dl, &p) == Success &&
p) {
atom = *(Atom *)p;
XFree(p);
}
return atom;
}
long getstate(Window w) {
int format;
long result = -1;
unsigned char *p = NULL;
unsigned long n, extra;
Atom real;
if (XGetWindowProperty(display, w, wmAtoms[WM_STATE], 0L, 2L, False,
wmAtoms[WM_STATE], &real, &format, &n, &extra,
(unsigned char **)&p) != Success)
return -1;
if (n != 0)
result = *p;
XFree(p);
return result;
}
int gettextprop(Window w, Atom atom, char *text, unsigned int size) {
char **list = NULL;
int n;
XTextProperty name;
if (!text || size == 0)
return 0;
text[0] = '\0';
if (!XGetTextProperty(display, w, &name, atom) || !name.nitems)
return 0;
if (name.encoding == XA_STRING) {
strncpy(text, (char *)name.value, size - 1);
} else if (XmbTextPropertyToTextList(display, &name, &list, &n) >= Success &&
n > 0 && *list) {
strncpy(text, *list, size - 1);
XFreeStringList(list);
}
text[size - 1] = '\0';
XFree(name.value);
return 1;
}
void setCurrentDesktop(void) {
long data[] = {0};
XChangeProperty(display, root, netAtoms[NET_CURRENT_DESKTOP], XA_CARDINAL, 32,
PropModeReplace, (unsigned char *)data, 1);
}
void setDesktopNames(void) {
XTextProperty text;
// Create list of workspace names
char **list = ecalloc(cfg.workspaceCount, sizeof(char *));
for (size_t i = 0; i < cfg.workspaceCount; i++) {
list[i] = strdup(cfg.workspaces[i].name);
}
// Convert list to text property
Xutf8TextListToTextProperty(display, list, cfg.workspaceCount,
XUTF8StringStyle, &text);
XSetTextProperty(display, root, &text, netAtoms[NET_DESKTOP_NAMES]);
}
void setNumDesktops(void) {
long data[] = {cfg.workspaceCount};
XChangeProperty(display, root, netAtoms[NET_NUMBER_OF_DESKTOPS], XA_CARDINAL,
32, PropModeReplace, (unsigned char *)data, 1);
}
void setViewport(void) {
long data[] = {0, 0};
XChangeProperty(display, root, netAtoms[NET_DESKTOP_VIEWPORT], XA_CARDINAL,
32, PropModeReplace, (unsigned char *)data, 2);
}
void updateCurrentDesktop(void) {
long rawdata[] = {
selectedMonitor->workspaceset[selectedMonitor->selectedWorkspaces]};
int i = 0;
while (*rawdata >> (i + 1)) {
i++;
}
long data[] = {i};
XChangeProperty(display, root, netAtoms[NET_CURRENT_DESKTOP], XA_CARDINAL, 32,
PropModeReplace, (unsigned char *)data, 1);
}