tony / oxwm

Branch: 7d789a9
Switch branches/tags
master
tonybanters Added xrandr support via updating configurenotify (similar to how dwm handles it) 7d789a9 2026-03-27
394 lines · 15.2 KB Raw Blame History
1 const std = @import("std");
2
3 const actions = @import("actions.zig");
4 const bar_mod = @import("../bar/bar.zig");
5 const client_mod = @import("../client.zig");
6 const core = @import("core.zig");
7 const tiling = @import("../layouts/tiling.zig");
8 const monitor_mod = @import("../monitor.zig");
9 const wm_mod = @import("wm.zig");
10 const events = @import("../x11/events.zig");
11 const xlib = @import("../x11/xlib.zig");
12
13 const WindowManager = wm_mod.WindowManager;
14
15 pub fn handleEvent(event: *xlib.XEvent, wm: *WindowManager) void {
16 const event_type = events.getEventType(event);
17
18 if (event_type == .button_press) {
19 std.debug.print("EVENT: button_press received type={d}\n", .{event.type});
20 }
21
22 switch (event_type) {
23 .map_request => handleMapRequest(&event.xmaprequest, wm),
24 .configure_request => handleConfigureRequest(&event.xconfigurerequest, wm),
25 .configure_notify => handleConfigureNotify(&event.xconfigure, wm),
26 .key_press => handleKeyPress(&event.xkey, wm),
27 .destroy_notify => handleDestroyNotify(&event.xdestroywindow, wm),
28 .unmap_notify => handleUnmapNotify(&event.xunmap, wm),
29 .enter_notify => handleEnterNotify(&event.xcrossing, wm),
30 .focus_in => handleFocusIn(&event.xfocus, wm),
31 .motion_notify => handleMotionNotify(&event.xmotion, wm),
32 .client_message => handleClientMessage(&event.xclient, wm),
33 .button_press => handleButtonPress(&event.xbutton, wm),
34 .expose => handleExpose(&event.xexpose, wm),
35 .property_notify => handlePropertyNotify(&event.xproperty, wm),
36 .mapping_notify => handleMappingNotify(&event.xmapping, wm),
37 else => {},
38 }
39 }
40
41 fn handleMapRequest(event: *xlib.XMapRequestEvent, wm: *WindowManager) void {
42 std.debug.print("map_request: window=0x{x}\n", .{event.window});
43
44 var window_attributes: xlib.XWindowAttributes = undefined;
45 if (xlib.XGetWindowAttributes(wm.display.handle, event.window, &window_attributes) == 0) {
46 return;
47 }
48 if (window_attributes.override_redirect != 0) {
49 return;
50 }
51 if (client_mod.windowToClient(wm.monitors, event.window) != null) {
52 return;
53 }
54
55 core.manage(event.window, &window_attributes, wm);
56 }
57
58 fn handleConfigureRequest(event: *xlib.XConfigureRequestEvent, wm: *WindowManager) void {
59 const client = client_mod.windowToClient(wm.monitors, event.window);
60
61 if (client) |managed_client| {
62 if ((event.value_mask & xlib.c.CWBorderWidth) != 0) {
63 managed_client.border_width = event.border_width;
64 } else if (managed_client.is_floating or (managed_client.monitor != null and managed_client.monitor.?.lt[managed_client.monitor.?.sel_lt] == null)) {
65 const monitor = managed_client.monitor orelse return;
66 if ((event.value_mask & xlib.c.CWX) != 0) {
67 managed_client.old_x = managed_client.x;
68 managed_client.x = monitor.mon_x + event.x;
69 }
70 if ((event.value_mask & xlib.c.CWY) != 0) {
71 managed_client.old_y = managed_client.y;
72 managed_client.y = monitor.mon_y + event.y;
73 }
74 if ((event.value_mask & xlib.c.CWWidth) != 0) {
75 managed_client.old_width = managed_client.width;
76 managed_client.width = event.width;
77 }
78 if ((event.value_mask & xlib.c.CWHeight) != 0) {
79 managed_client.old_height = managed_client.height;
80 managed_client.height = event.height;
81 }
82 const client_full_width = managed_client.width + managed_client.border_width * 2;
83 const client_full_height = managed_client.height + managed_client.border_width * 2;
84 if ((managed_client.x + managed_client.width) > monitor.mon_x + monitor.mon_w and managed_client.is_floating) {
85 managed_client.x = monitor.mon_x + @divTrunc(monitor.mon_w, 2) - @divTrunc(client_full_width, 2);
86 }
87 if ((managed_client.y + managed_client.height) > monitor.mon_y + monitor.mon_h and managed_client.is_floating) {
88 managed_client.y = monitor.mon_y + @divTrunc(monitor.mon_h, 2) - @divTrunc(client_full_height, 2);
89 }
90 if (((event.value_mask & (xlib.c.CWX | xlib.c.CWY)) != 0) and ((event.value_mask & (xlib.c.CWWidth | xlib.c.CWHeight)) == 0)) {
91 tiling.sendConfigure(managed_client);
92 }
93 if (client_mod.isVisible(managed_client)) {
94 _ = xlib.XMoveResizeWindow(wm.display.handle, managed_client.window, managed_client.x, managed_client.y, @intCast(managed_client.width), @intCast(managed_client.height));
95 }
96 } else {
97 tiling.sendConfigure(managed_client);
98 }
99 } else {
100 var changes: xlib.XWindowChanges = undefined;
101 changes.x = event.x;
102 changes.y = event.y;
103 changes.width = event.width;
104 changes.height = event.height;
105 changes.border_width = event.border_width;
106 changes.sibling = event.above;
107 changes.stack_mode = event.detail;
108 _ = xlib.XConfigureWindow(wm.display.handle, event.window, @intCast(event.value_mask), &changes);
109 }
110 _ = xlib.XSync(wm.display.handle, xlib.False);
111 }
112
113 fn handleKeyPress(event: *xlib.XKeyEvent, wm: *WindowManager) void {
114 const keysym = xlib.XKeycodeToKeysym(wm.display.handle, @intCast(event.keycode), 0);
115
116 if (wm.overlay) |overlay| {
117 if (overlay.handleKey(keysym)) return;
118 }
119
120 const clean_state = event.state & ~@as(c_uint, xlib.LockMask | xlib.Mod2Mask);
121
122 if (wm.chord.isTimedOut()) {
123 wm.chord.reset(wm.display.handle);
124 }
125
126 _ = wm.chord.push(.{ .mod_mask = clean_state, .keysym = keysym });
127
128 for (wm.config.keybinds.items) |keybind| {
129 if (keybind.key_count == 0) continue;
130
131 if (keybind.key_count == wm.chord.index) {
132 var matches = true;
133 for (0..keybind.key_count) |i| {
134 if (wm.chord.keys[i].keysym != keybind.keys[i].keysym or
135 wm.chord.keys[i].mod_mask != keybind.keys[i].mod_mask)
136 {
137 matches = false;
138 break;
139 }
140 }
141 if (matches) {
142 actions.executeAction(keybind.action, keybind.int_arg, keybind.str_arg, wm);
143 wm.chord.reset(wm.display.handle);
144 return;
145 }
146 }
147 }
148
149 var has_partial_match = false;
150 for (wm.config.keybinds.items) |keybind| {
151 if (keybind.key_count > wm.chord.index) {
152 var matches = true;
153 for (0..wm.chord.index) |i| {
154 if (wm.chord.keys[i].keysym != keybind.keys[i].keysym or
155 wm.chord.keys[i].mod_mask != keybind.keys[i].mod_mask)
156 {
157 matches = false;
158 break;
159 }
160 }
161 if (matches) {
162 has_partial_match = true;
163 break;
164 }
165 }
166 }
167
168 if (has_partial_match) {
169 wm.chord.grabKeyboard(wm.display.handle, wm.display.root);
170 } else {
171 wm.chord.reset(wm.display.handle);
172 }
173 }
174
175 fn cleanMask(mask: c_uint, wm: *WindowManager) c_uint {
176 const lock: c_uint = @intCast(xlib.LockMask);
177 const shift: c_uint = @intCast(xlib.ShiftMask);
178 const ctrl: c_uint = @intCast(xlib.ControlMask);
179 const mod1: c_uint = @intCast(xlib.Mod1Mask);
180 const mod2: c_uint = @intCast(xlib.Mod2Mask);
181 const mod3: c_uint = @intCast(xlib.Mod3Mask);
182 const mod4: c_uint = @intCast(xlib.Mod4Mask);
183 const mod5: c_uint = @intCast(xlib.Mod5Mask);
184 return mask & ~(lock | wm.numlock_mask) & (shift | ctrl | mod1 | mod2 | mod3 | mod4 | mod5);
185 }
186
187 fn handleExpose(event: *xlib.XExposeEvent, wm: *WindowManager) void {
188 if (event.count != 0) return;
189 if (bar_mod.windowToBar(wm.bars, event.window)) |bar| {
190 bar.invalidate();
191 bar.draw(wm.display.handle, wm.config);
192 }
193 }
194
195 fn handleConfigureNotify(event: *xlib.XConfigureEvent, wm: *WindowManager) void {
196 if (event.window != wm.display.root) return;
197
198 const dirty = wm.updateGeom();
199 if (dirty) {
200 wm.rebuildBars();
201 var mon = wm.monitors;
202 while (mon) |m| {
203 var c = m.clients;
204 while (c) |client| {
205 if (client.is_fullscreen) {
206 tiling.resizeClient(client, m.mon_x, m.mon_y, m.mon_w, m.mon_h);
207 }
208 c = client.next;
209 }
210 core.arrange(m, wm);
211 mon = m.next;
212 }
213 }
214 }
215
216 fn handleButtonPress(event: *xlib.XButtonEvent, wm: *WindowManager) void {
217 std.debug.print("button_press: window=0x{x} subwindow=0x{x}\n", .{ event.window, event.subwindow });
218
219 const clicked_monitor = monitor_mod.windowToMonitor(wm, event.window);
220 if (clicked_monitor) |monitor| {
221 if (monitor != wm.selected_monitor) {
222 if (wm.selected_monitor) |selmon| {
223 core.unfocusClient(selmon.sel, true, wm);
224 }
225 wm.selected_monitor = monitor;
226 core.focus(null, wm);
227 }
228 }
229
230 if (bar_mod.windowToBar(wm.bars, event.window)) |bar| {
231 const clicked_tag = bar.handleClick(wm.display.handle, event.x, wm.config);
232 if (clicked_tag) |tag_index| {
233 const tag_mask: u32 = @as(u32, 1) << @intCast(tag_index);
234 core.view(tag_mask, wm);
235 }
236 return;
237 }
238
239 const click_client = client_mod.windowToClient(wm.monitors, event.window);
240 if (click_client) |found_client| {
241 core.focus(found_client, wm);
242 if (wm.selected_monitor) |selmon| {
243 core.restack(selmon, wm);
244 }
245 _ = xlib.XAllowEvents(wm.display.handle, xlib.ReplayPointer, xlib.CurrentTime);
246 }
247
248 const clean_state = cleanMask(event.state, wm);
249 for (wm.config.buttons.items) |button| {
250 if (button.click != .client_win) continue;
251 const button_clean_mask = cleanMask(button.mod_mask, wm);
252 if (clean_state == button_clean_mask and event.button == button.button) {
253 switch (button.action) {
254 .move_mouse => actions.movemouse(wm),
255 .resize_mouse => actions.resizemouse(wm),
256 .toggle_floating => {
257 if (click_client) |found_client| {
258 found_client.is_floating = !found_client.is_floating;
259 if (wm.selected_monitor) |monitor| {
260 core.arrange(monitor, wm);
261 }
262 }
263 },
264 }
265 return;
266 }
267 }
268 }
269
270 fn handleClientMessage(event: *xlib.XClientMessageEvent, wm: *WindowManager) void {
271 const client = client_mod.windowToClient(wm.monitors, event.window) orelse return;
272
273 if (event.message_type == wm.atoms.net_wm_state) {
274 const action = event.data.l[0];
275 const first_property: xlib.Atom = @intCast(event.data.l[1]);
276 const second_property: xlib.Atom = @intCast(event.data.l[2]);
277
278 if (first_property == wm.atoms.net_wm_state_fullscreen or second_property == wm.atoms.net_wm_state_fullscreen) {
279 const net_wm_state_remove = 0;
280 const net_wm_state_add = 1;
281 const net_wm_state_toggle = 2;
282
283 if (action == net_wm_state_add) {
284 core.setFullscreen(client, true, wm);
285 } else if (action == net_wm_state_remove) {
286 core.setFullscreen(client, false, wm);
287 } else if (action == net_wm_state_toggle) {
288 core.setFullscreen(client, !client.is_fullscreen, wm);
289 }
290 }
291 } else if (event.message_type == wm.atoms.net_active_window) {
292 const selected = wm.selected_monitor orelse return;
293 if (client != selected.sel and !client.is_urgent) {
294 core.setUrgent(client, true, wm);
295 }
296 }
297 }
298
299 fn handleDestroyNotify(event: *xlib.XDestroyWindowEvent, wm: *WindowManager) void {
300 const client = client_mod.windowToClient(wm.monitors, event.window) orelse return;
301 std.debug.print("destroy_notify: window=0x{x}\n", .{event.window});
302 core.unmanage(client, wm);
303 }
304
305 fn handleUnmapNotify(event: *xlib.XUnmapEvent, wm: *WindowManager) void {
306 const client = client_mod.windowToClient(wm.monitors, event.window) orelse return;
307 std.debug.print("unmap_notify: window=0x{x}\n", .{event.window});
308 core.unmanage(client, wm);
309 }
310
311 fn handleEnterNotify(event: *xlib.XCrossingEvent, wm: *WindowManager) void {
312 if ((event.mode != xlib.NotifyNormal or event.detail == xlib.NotifyInferior) and event.window != wm.display.root) {
313 return;
314 }
315
316 const client = client_mod.windowToClient(wm.monitors, event.window);
317 const target_mon = if (client) |c| c.monitor else monitor_mod.windowToMonitor(wm, event.window);
318 const selmon = wm.selected_monitor;
319
320 if (target_mon != selmon) {
321 if (selmon) |sel| {
322 core.unfocusClient(sel.sel, true, wm);
323 }
324 wm.selected_monitor = target_mon;
325 } else if (client == null) {
326 return;
327 } else if (selmon) |sel| {
328 if (client.? == sel.sel) {
329 return;
330 }
331 }
332
333 core.focus(client, wm);
334 }
335
336 fn handleFocusIn(event: *xlib.XFocusChangeEvent, wm: *WindowManager) void {
337 const selmon = wm.selected_monitor orelse return;
338 const selected = selmon.sel orelse return;
339 if (event.window != selected.window) {
340 core.setFocus(selected, wm);
341 }
342 }
343
344 fn handleMotionNotify(event: *xlib.XMotionEvent, wm: *WindowManager) void {
345 if (event.window != wm.display.root) return;
346
347 const target_mon = monitor_mod.rectToMonitor(wm, event.x_root, event.y_root, 1, 1);
348 if (target_mon != wm.last_motion_monitor and wm.last_motion_monitor != null) {
349 if (wm.selected_monitor) |selmon| {
350 core.unfocusClient(selmon.sel, true, wm);
351 }
352 wm.selected_monitor = target_mon;
353 core.focus(null, wm);
354 }
355 wm.last_motion_monitor = target_mon;
356 }
357
358 fn handlePropertyNotify(event: *xlib.XPropertyEvent, wm: *WindowManager) void {
359 if (event.state == xlib.PropertyDelete) {
360 return;
361 }
362
363 const client = client_mod.windowToClient(wm.monitors, event.window) orelse return;
364
365 if (event.atom == xlib.XA_WM_TRANSIENT_FOR) {
366 var trans: xlib.Window = 0;
367 if (!client.is_floating and xlib.XGetTransientForHint(wm.display.handle, client.window, &trans) != 0) {
368 client.is_floating = client_mod.windowToClient(wm.monitors, trans) != null;
369 if (client.is_floating) {
370 if (client.monitor) |monitor| {
371 core.arrange(monitor, wm);
372 }
373 }
374 }
375 } else if (event.atom == xlib.XA_WM_NORMAL_HINTS) {
376 client.hints_valid = false;
377 } else if (event.atom == xlib.XA_WM_HINTS) {
378 core.updateWmHints(client, wm);
379 wm.invalidateBars();
380 } else if (event.atom == xlib.XA_WM_NAME or event.atom == wm.atoms.net_wm_name) {
381 core.updateTitle(client, wm);
382 } else if (event.atom == wm.atoms.net_wm_window_type) {
383 core.updateWindowType(client, wm);
384 }
385 }
386
387 fn handleMappingNotify(event: *xlib.XMappingEvent, wm: *WindowManager) void {
388 _ = xlib.XRefreshKeyboardMapping(event);
389
390 if (event.request == xlib.MappingKeyboard or event.request == xlib.MappingModifier) {
391 wm.ungrabKeybinds();
392 wm.grabKeybinds();
393 }
394 }