tony / oxwm

Branch: 2d0f539
Switch branches/tags
master
tonybanters Updated with master. 2d0f539 2026-03-30
398 lines · 15.4 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 } else if (bar.handleBlockClick(event.x)) |click_action| {
236 wm.next_spawn_floating = click_action.floating;
237 wm.next_spawn_bypass_rules = click_action.bypass_rules;
238 actions.spawnCommand(wm, click_action.command);
239 }
240 return;
241 }
242
243 const click_client = client_mod.windowToClient(wm.monitors, event.window);
244 if (click_client) |found_client| {
245 core.focus(found_client, wm);
246 if (wm.selected_monitor) |selmon| {
247 core.restack(selmon, wm);
248 }
249 _ = xlib.XAllowEvents(wm.display.handle, xlib.ReplayPointer, xlib.CurrentTime);
250 }
251
252 const clean_state = cleanMask(event.state, wm);
253 for (wm.config.buttons.items) |button| {
254 if (button.click != .client_win) continue;
255 const button_clean_mask = cleanMask(button.mod_mask, wm);
256 if (clean_state == button_clean_mask and event.button == button.button) {
257 switch (button.action) {
258 .move_mouse => actions.movemouse(wm),
259 .resize_mouse => actions.resizemouse(wm),
260 .toggle_floating => {
261 if (click_client) |found_client| {
262 found_client.is_floating = !found_client.is_floating;
263 if (wm.selected_monitor) |monitor| {
264 core.arrange(monitor, wm);
265 }
266 }
267 },
268 }
269 return;
270 }
271 }
272 }
273
274 fn handleClientMessage(event: *xlib.XClientMessageEvent, wm: *WindowManager) void {
275 const client = client_mod.windowToClient(wm.monitors, event.window) orelse return;
276
277 if (event.message_type == wm.atoms.net_wm_state) {
278 const action = event.data.l[0];
279 const first_property: xlib.Atom = @intCast(event.data.l[1]);
280 const second_property: xlib.Atom = @intCast(event.data.l[2]);
281
282 if (first_property == wm.atoms.net_wm_state_fullscreen or second_property == wm.atoms.net_wm_state_fullscreen) {
283 const net_wm_state_remove = 0;
284 const net_wm_state_add = 1;
285 const net_wm_state_toggle = 2;
286
287 if (action == net_wm_state_add) {
288 core.setFullscreen(client, true, wm);
289 } else if (action == net_wm_state_remove) {
290 core.setFullscreen(client, false, wm);
291 } else if (action == net_wm_state_toggle) {
292 core.setFullscreen(client, !client.is_fullscreen, wm);
293 }
294 }
295 } else if (event.message_type == wm.atoms.net_active_window) {
296 const selected = wm.selected_monitor orelse return;
297 if (client != selected.sel and !client.is_urgent) {
298 core.setUrgent(client, true, wm);
299 }
300 }
301 }
302
303 fn handleDestroyNotify(event: *xlib.XDestroyWindowEvent, wm: *WindowManager) void {
304 const client = client_mod.windowToClient(wm.monitors, event.window) orelse return;
305 std.debug.print("destroy_notify: window=0x{x}\n", .{event.window});
306 core.unmanage(client, wm);
307 }
308
309 fn handleUnmapNotify(event: *xlib.XUnmapEvent, wm: *WindowManager) void {
310 const client = client_mod.windowToClient(wm.monitors, event.window) orelse return;
311 std.debug.print("unmap_notify: window=0x{x}\n", .{event.window});
312 core.unmanage(client, wm);
313 }
314
315 fn handleEnterNotify(event: *xlib.XCrossingEvent, wm: *WindowManager) void {
316 if ((event.mode != xlib.NotifyNormal or event.detail == xlib.NotifyInferior) and event.window != wm.display.root) {
317 return;
318 }
319
320 const client = client_mod.windowToClient(wm.monitors, event.window);
321 const target_mon = if (client) |c| c.monitor else monitor_mod.windowToMonitor(wm, event.window);
322 const selmon = wm.selected_monitor;
323
324 if (target_mon != selmon) {
325 if (selmon) |sel| {
326 core.unfocusClient(sel.sel, true, wm);
327 }
328 wm.selected_monitor = target_mon;
329 } else if (client == null) {
330 return;
331 } else if (selmon) |sel| {
332 if (client.? == sel.sel) {
333 return;
334 }
335 }
336
337 core.focus(client, wm);
338 }
339
340 fn handleFocusIn(event: *xlib.XFocusChangeEvent, wm: *WindowManager) void {
341 const selmon = wm.selected_monitor orelse return;
342 const selected = selmon.sel orelse return;
343 if (event.window != selected.window) {
344 core.setFocus(selected, wm);
345 }
346 }
347
348 fn handleMotionNotify(event: *xlib.XMotionEvent, wm: *WindowManager) void {
349 if (event.window != wm.display.root) return;
350
351 const target_mon = monitor_mod.rectToMonitor(wm, event.x_root, event.y_root, 1, 1);
352 if (target_mon != wm.last_motion_monitor and wm.last_motion_monitor != null) {
353 if (wm.selected_monitor) |selmon| {
354 core.unfocusClient(selmon.sel, true, wm);
355 }
356 wm.selected_monitor = target_mon;
357 core.focus(null, wm);
358 }
359 wm.last_motion_monitor = target_mon;
360 }
361
362 fn handlePropertyNotify(event: *xlib.XPropertyEvent, wm: *WindowManager) void {
363 if (event.state == xlib.PropertyDelete) {
364 return;
365 }
366
367 const client = client_mod.windowToClient(wm.monitors, event.window) orelse return;
368
369 if (event.atom == xlib.XA_WM_TRANSIENT_FOR) {
370 var trans: xlib.Window = 0;
371 if (!client.is_floating and xlib.XGetTransientForHint(wm.display.handle, client.window, &trans) != 0) {
372 client.is_floating = client_mod.windowToClient(wm.monitors, trans) != null;
373 if (client.is_floating) {
374 if (client.monitor) |monitor| {
375 core.arrange(monitor, wm);
376 }
377 }
378 }
379 } else if (event.atom == xlib.XA_WM_NORMAL_HINTS) {
380 client.hints_valid = false;
381 } else if (event.atom == xlib.XA_WM_HINTS) {
382 core.updateWmHints(client, wm);
383 wm.invalidateBars();
384 } else if (event.atom == xlib.XA_WM_NAME or event.atom == wm.atoms.net_wm_name) {
385 core.updateTitle(client, wm);
386 } else if (event.atom == wm.atoms.net_wm_window_type) {
387 core.updateWindowType(client, wm);
388 }
389 }
390
391 fn handleMappingNotify(event: *xlib.XMappingEvent, wm: *WindowManager) void {
392 _ = xlib.XRefreshKeyboardMapping(event);
393
394 if (event.request == xlib.MappingKeyboard or event.request == xlib.MappingModifier) {
395 wm.ungrabKeybinds();
396 wm.grabKeybinds();
397 }
398 }