-
Notifications
You must be signed in to change notification settings - Fork 0
/
events.c
297 lines (262 loc) · 8.14 KB
/
events.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
// EVENTS
// "How should this xserver event be handled?"
#include "atlas.h"
#include "configurer.h"
#include "ipc.h"
#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <stdlib.h>
#include <string.h>
/* HACK: Need to implement TOML config for these */
#define MODKEY Mod4Mask
static const Button buttons[] = {
/* click event mask button function argument */
{ClkClientWin, MODKEY, Button1, moveWindow, {0}},
{ClkClientWin, MODKEY, Button2, toggleWindowFloating, {0}},
{ClkClientWin, MODKEY, Button3, resizeWindow, {0}},
};
void handleMouseButtonPress(XEvent *e) {
unsigned int i, click;
Arg arg = {0};
Client *c;
Monitor *m;
XButtonPressedEvent *ev = &e->xbutton;
click = ClkRootWin;
/* focus monitor if necessary */
if ((m = findMonitorFromWindow(ev->window)) && m != selectedMonitor) {
unfocus(selectedMonitor->active, 1);
selectedMonitor = m;
focus(NULL);
}
if ((c = findClientFromWindow(ev->window))) {
focus(c);
restack(selectedMonitor);
XAllowEvents(dpy, ReplayPointer, CurrentTime);
click = ClkClientWin;
}
for (i = 0; i < LENGTH(buttons); i++)
if (click == buttons[i].click && buttons[i].func &&
buttons[i].button == ev->button &&
CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
buttons[i].func(buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
}
void handleClientMessage(XEvent *e) {
XClientMessageEvent *cme = &e->xclient;
Client *c = findClientFromWindow(cme->window);
if (!c)
return;
if (cme->message_type == netatom[NetWMState]) {
if (cme->data.l[1] == netatom[NetWMFullscreen] ||
cme->data.l[2] == netatom[NetWMFullscreen])
setWindowFullscreen(c,
(cme->data.l[0] == 1 /* _NET_WM_STATE_ADD */
|| (cme->data.l[0] == 2 /* _NET_WM_STATE_TOGGLE */ &&
!c->isFullscreen)));
} else if (cme->message_type == netatom[NetActiveWindow]) {
if (c != selectedMonitor->active && !c->isUrgent)
setWindowUrgent(c, 1);
}
}
void handleConfigureRequest(XEvent *e) {
Client *c;
Monitor *m;
XConfigureRequestEvent *ev = &e->xconfigurerequest;
XWindowChanges wc;
if ((c = findClientFromWindow(ev->window))) {
if (ev->value_mask & CWBorderWidth)
c->borderWidth = ev->border_width;
else if (c->isFloating ||
!selectedMonitor->layouts[selectedMonitor->selectedLayout]
->arrange) {
m = c->monitor;
if (ev->value_mask & CWX) {
c->oldx = c->x;
c->x = m->mx + ev->x;
}
if (ev->value_mask & CWY) {
c->oldy = c->y;
c->y = m->my + ev->y;
}
if (ev->value_mask & CWWidth) {
c->oldw = c->w;
c->w = ev->width;
}
if (ev->value_mask & CWHeight) {
c->oldh = c->h;
c->h = ev->height;
}
if ((c->x + c->w) > m->mx + m->mw && c->isFloating)
c->x = m->mx + (m->mw / 2 - WIDTH(c) / 2); /* center in x direction */
if ((c->y + c->h) > m->my + m->mh && c->isFloating)
c->y = m->my + (m->mh / 2 - HEIGHT(c) / 2); /* center in y direction */
if ((ev->value_mask & (CWX | CWY)) &&
!(ev->value_mask & (CWWidth | CWHeight)))
configure(c);
if (ISVISIBLE(c))
XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
} else
configure(c);
} else {
wc.x = ev->x;
wc.y = ev->y;
wc.width = ev->width;
wc.height = ev->height;
wc.border_width = ev->border_width;
wc.sibling = ev->above;
wc.stack_mode = ev->detail;
XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
}
XSync(dpy, False);
}
void handleWindowDestroy(XEvent *e) {
Client *c;
XDestroyWindowEvent *ev = &e->xdestroywindow;
if ((c = findClientFromWindow(ev->window)))
unmanage(c, 1);
}
void handleMouseEnter(XEvent *e) {
Client *c;
Monitor *m;
XCrossingEvent *ev = &e->xcrossing;
if ((ev->mode != NotifyNormal || ev->detail == NotifyInferior) &&
ev->window != root)
return;
c = findClientFromWindow(ev->window);
m = c ? c->monitor : findMonitorFromWindow(ev->window);
if (m != selectedMonitor) {
unfocus(selectedMonitor->active, 1);
selectedMonitor = m;
} else if (!c || c == selectedMonitor->active)
return;
focus(c);
}
/* there are some broken focus acquiring clients needing extra handling */
void handleFocusIn(XEvent *e) {
XFocusChangeEvent *ev = &e->xfocus;
if (selectedMonitor->active && ev->window != selectedMonitor->active->win)
setfocus(selectedMonitor->active);
}
void handleMouseMotion(XEvent *e) {
static Monitor *mon = NULL;
Monitor *m;
XMotionEvent *ev = &e->xmotion;
if (ev->window != root)
return;
if ((m = getMonitorForArea(ev->x_root, ev->y_root, 1, 1)) != mon && mon) {
unfocus(selectedMonitor->active, 1);
selectedMonitor = m;
focus(NULL);
}
mon = m;
}
void handlePropertyChange(XEvent *e) {
Client *c;
Window trans;
XPropertyEvent *ev = &e->xproperty;
if ((ev->window == root) && (ev->atom == command_atom)) {
Atom type;
int format;
unsigned long nitems, bytes_after;
unsigned char *data = NULL;
if (XGetWindowProperty(dpy, root, command_atom, 0, 1, True, XA_CARDINAL,
&type, &format, &nitems, &bytes_after,
&data) == Success) {
if (data) {
CommandType cmd = *(CommandType *)data;
handle_command(cmd);
XFree(data);
}
}
} else if (ev->state == PropertyDelete)
return; /* ignore */
else if ((c = findClientFromWindow(ev->window))) {
switch (ev->atom) {
default:
break;
case XA_WM_TRANSIENT_FOR:
if (!c->isFloating && (XGetTransientForHint(dpy, c->win, &trans)) &&
(c->isFloating = (findClientFromWindow(trans)) != NULL))
arrange(c->monitor);
break;
case XA_WM_NORMAL_HINTS:
c->hintsvalid = 0;
break;
case XA_WM_HINTS:
updateWindowManagerHints(c);
break;
}
if (ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
updateWindowTitle(c);
}
if (ev->atom == netatom[NetWMWindowType])
updateWindowTypeProps(c);
}
}
void handleWindowUnmap(XEvent *e) {
Client *c;
XUnmapEvent *ev = &e->xunmap;
if ((c = findClientFromWindow(ev->window))) {
if (ev->send_event)
setclientstate(c, WithdrawnState);
else
unmanage(c, 0);
}
}
void handleKeymappingChange(XEvent *e) {
XMappingEvent *ev = &e->xmapping;
XRefreshKeyboardMapping(ev);
if (ev->request == MappingKeyboard)
registerKeyboardShortcuts();
}
void handleWindowMappingRequest(XEvent *e) {
static XWindowAttributes wa;
XMapRequestEvent *ev = &e->xmaprequest;
if (!XGetWindowAttributes(dpy, ev->window, &wa) || wa.override_redirect)
return;
if (!findClientFromWindow(ev->window))
manage(ev->window, &wa);
}
void handleKeypress(XEvent *e) {
XKeyEvent *ev = &e->xkey;
KeySym keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
unsigned int cleanMask = CLEANMASK(ev->state);
for (int i = 0; i < cfg.keybindingCount; i++) {
if (cfg.keybindings[i].keysym == keysym &&
CLEANMASK(cfg.keybindings[i].modifier) == cleanMask) {
executeKeybinding(&cfg.keybindings[i]);
return;
}
}
}
void handleWindowConfigChange(XEvent *e) {
XConfigureEvent *ev = &e->xconfigure;
// Only handle root window configuration changes
if (ev->window != root) {
return;
}
// Check if screen dimensions have changed
int dimensionsChanged =
(screenWidth != ev->width || screenHeight != ev->height);
if (!dimensionsChanged) {
return;
}
// Update screen dimensions
screenWidth = ev->width;
screenHeight = ev->height;
// Update monitor geometry and check if any monitors were affected
if (!updateMonitorGeometry() && !dimensionsChanged) {
return; // No changes needed
}
// Update all monitors and their clients
for (Monitor *m = monitors; m; m = m->next) {
// Resize fullscreen windows to match their monitor
for (Client *c = m->clients; c; c = c->next) {
if (c->isFullscreen) {
resizeclient(c, m->mx, m->my, m->mw, m->mh);
}
}
}
// Reset focus and rearrange all windows
focus(NULL);
arrange(NULL);
}