tony / oxwm

Branch: 57531bf
Switch branches/tags
master
tonybanters updates for zig v16 c381491 2026-05-14
475 lines · 17.5 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 .button_release => handleButtonRelease(&event.xbutton, wm),
35 .expose => handleExpose(&event.xexpose, wm),
36 .property_notify => handlePropertyNotify(&event.xproperty, wm),
37 .mapping_notify => handleMappingNotify(&event.xmapping, wm),
38 .reparent_notify => handleReparentNotify(&event.xreparent, wm),
39 .map_notify => handleMapNotify(&event.xmap, wm),
40 .resize_request => handleResizeRequest(&event.xresizerequest, wm),
41 else => {},
42 }
43 }
44
45 fn handleMapRequest(event: *xlib.XMapRequestEvent, wm: *WindowManager) void {
46 std.debug.print("map_request: window=0x{x}\n", .{event.window});
47
48 if (wm.getSystray()) |tray| {
49 if (tray.isIconWindow(event.window)) return;
50 }
51
52 var window_attributes: xlib.XWindowAttributes = undefined;
53 if (xlib.XGetWindowAttributes(wm.display.handle, event.window, &window_attributes) == 0) {
54 return;
55 }
56 if (window_attributes.override_redirect != 0) {
57 return;
58 }
59 if (client_mod.windowToClient(wm.monitors, event.window) != null) {
60 return;
61 }
62
63 core.manage(event.window, &window_attributes, wm);
64 }
65
66 fn handleConfigureRequest(event: *xlib.XConfigureRequestEvent, wm: *WindowManager) void {
67 if (wm.getSystray()) |tray| {
68 if (tray.handleConfigureRequest(event)) {
69 wm.invalidateBars();
70 return;
71 }
72 }
73
74 const client = client_mod.windowToClient(wm.monitors, event.window);
75
76 if (client) |managed_client| {
77 if ((event.value_mask & xlib.c.CWBorderWidth) != 0) {
78 managed_client.border_width = event.border_width;
79 } else if (managed_client.is_floating or (managed_client.monitor != null and managed_client.monitor.?.lt[managed_client.monitor.?.sel_lt] == null)) {
80 const monitor = managed_client.monitor orelse return;
81 if ((event.value_mask & xlib.c.CWX) != 0) {
82 managed_client.old_x = managed_client.x;
83 managed_client.x = monitor.mon_x + event.x;
84 }
85 if ((event.value_mask & xlib.c.CWY) != 0) {
86 managed_client.old_y = managed_client.y;
87 managed_client.y = monitor.mon_y + event.y;
88 }
89 if ((event.value_mask & xlib.c.CWWidth) != 0) {
90 managed_client.old_width = managed_client.width;
91 managed_client.width = event.width;
92 }
93 if ((event.value_mask & xlib.c.CWHeight) != 0) {
94 managed_client.old_height = managed_client.height;
95 managed_client.height = event.height;
96 }
97 const client_full_width = managed_client.width + managed_client.border_width * 2;
98 const client_full_height = managed_client.height + managed_client.border_width * 2;
99 if ((managed_client.x + managed_client.width) > monitor.mon_x + monitor.mon_w and managed_client.is_floating) {
100 managed_client.x = monitor.mon_x + @divTrunc(monitor.mon_w, 2) - @divTrunc(client_full_width, 2);
101 }
102 if ((managed_client.y + managed_client.height) > monitor.mon_y + monitor.mon_h and managed_client.is_floating) {
103 managed_client.y = monitor.mon_y + @divTrunc(monitor.mon_h, 2) - @divTrunc(client_full_height, 2);
104 }
105 if (((event.value_mask & (xlib.c.CWX | xlib.c.CWY)) != 0) and ((event.value_mask & (xlib.c.CWWidth | xlib.c.CWHeight)) == 0)) {
106 tiling.sendConfigure(managed_client);
107 }
108 if (client_mod.isVisible(managed_client)) {
109 _ = xlib.XMoveResizeWindow(wm.display.handle, managed_client.window, managed_client.x, managed_client.y, @intCast(managed_client.width), @intCast(managed_client.height));
110 }
111 } else {
112 tiling.sendConfigure(managed_client);
113 }
114 } else {
115 var changes: xlib.XWindowChanges = undefined;
116 changes.x = event.x;
117 changes.y = event.y;
118 changes.width = event.width;
119 changes.height = event.height;
120 changes.border_width = event.border_width;
121 changes.sibling = event.above;
122 changes.stack_mode = event.detail;
123 _ = xlib.XConfigureWindow(wm.display.handle, event.window, @intCast(event.value_mask), &changes);
124 }
125 _ = xlib.XSync(wm.display.handle, xlib.False);
126 }
127
128 fn handleKeyPress(event: *xlib.XKeyEvent, wm: *WindowManager) void {
129 const keysym = xlib.XKeycodeToKeysym(wm.display.handle, @intCast(event.keycode), 0);
130
131 if (wm.overlay) |overlay| {
132 if (overlay.handleKey(keysym)) return;
133 }
134
135 const clean_state = event.state & ~@as(c_uint, xlib.LockMask | xlib.Mod2Mask);
136
137 if (wm.chord.isTimedOut(wm.io)) {
138 wm.chord.reset(wm.display.handle);
139 }
140
141 _ = wm.chord.push(wm.io, .{ .mod_mask = clean_state, .keysym = keysym });
142
143 for (wm.config.keybinds.items) |keybind| {
144 if (keybind.key_count == 0) continue;
145
146 if (keybind.key_count == wm.chord.index) {
147 var matches = true;
148 for (0..keybind.key_count) |i| {
149 if (wm.chord.keys[i].keysym != keybind.keys[i].keysym or
150 wm.chord.keys[i].mod_mask != keybind.keys[i].mod_mask)
151 {
152 matches = false;
153 break;
154 }
155 }
156 if (matches) {
157 actions.executeAction(keybind.action, keybind.int_arg, keybind.str_arg, wm);
158 wm.chord.reset(wm.display.handle);
159 return;
160 }
161 }
162 }
163
164 var has_partial_match = false;
165 for (wm.config.keybinds.items) |keybind| {
166 if (keybind.key_count > wm.chord.index) {
167 var matches = true;
168 for (0..wm.chord.index) |i| {
169 if (wm.chord.keys[i].keysym != keybind.keys[i].keysym or
170 wm.chord.keys[i].mod_mask != keybind.keys[i].mod_mask)
171 {
172 matches = false;
173 break;
174 }
175 }
176 if (matches) {
177 has_partial_match = true;
178 break;
179 }
180 }
181 }
182
183 if (has_partial_match) {
184 wm.chord.grabKeyboard(wm.display.handle, wm.display.root);
185 } else {
186 wm.chord.reset(wm.display.handle);
187 }
188 }
189
190 fn cleanMask(mask: c_uint, wm: *WindowManager) c_uint {
191 const lock: c_uint = @intCast(xlib.LockMask);
192 const shift: c_uint = @intCast(xlib.ShiftMask);
193 const ctrl: c_uint = @intCast(xlib.ControlMask);
194 const mod1: c_uint = @intCast(xlib.Mod1Mask);
195 const mod2: c_uint = @intCast(xlib.Mod2Mask);
196 const mod3: c_uint = @intCast(xlib.Mod3Mask);
197 const mod4: c_uint = @intCast(xlib.Mod4Mask);
198 const mod5: c_uint = @intCast(xlib.Mod5Mask);
199 return mask & ~(lock | wm.numlock_mask) & (shift | ctrl | mod1 | mod2 | mod3 | mod4 | mod5);
200 }
201
202 fn handleExpose(event: *xlib.XExposeEvent, wm: *WindowManager) void {
203 if (event.count != 0) return;
204 if (bar_mod.windowToBar(wm.bars, event.window)) |bar| {
205 bar.invalidate();
206 bar.draw(wm.display.handle, wm.config);
207 }
208 }
209
210 fn handleConfigureNotify(event: *xlib.XConfigureEvent, wm: *WindowManager) void {
211 if (event.window != wm.display.root) return;
212
213 const dirty = wm.updateGeom();
214 if (dirty) {
215 wm.rebuildBars();
216 var mon = wm.monitors;
217 while (mon) |m| {
218 var c = m.clients;
219 while (c) |client| {
220 if (client.is_fullscreen) {
221 tiling.resizeClient(client, m.mon_x, m.mon_y, m.mon_w, m.mon_h);
222 }
223 c = client.next;
224 }
225 core.arrange(m, wm);
226 mon = m.next;
227 }
228 }
229 }
230
231 fn handleButtonPress(event: *xlib.XButtonEvent, wm: *WindowManager) void {
232 std.debug.print("button_press: window=0x{x} subwindow=0x{x}\n", .{ event.window, event.subwindow });
233
234 if (wm.getSystray()) |tray| {
235 if (tray.handleButtonPress(event)) return;
236 }
237
238 const clicked_monitor = monitor_mod.windowToMonitor(wm, event.window);
239 if (clicked_monitor) |monitor| {
240 if (monitor != wm.selected_monitor) {
241 if (wm.selected_monitor) |selmon| {
242 core.unfocusClient(selmon.sel, true, wm);
243 }
244 wm.selected_monitor = monitor;
245 core.focus(null, wm);
246 }
247 }
248
249 if (bar_mod.windowToBar(wm.bars, event.window)) |bar| {
250 const clicked_tag = bar.handleClick(wm.display.handle, event.x, wm.config);
251 if (clicked_tag) |tag_index| {
252 const tag_mask: u32 = @as(u32, 1) << @intCast(tag_index);
253 core.view(tag_mask, wm);
254 } else if (bar.handleBlockClick(event.x)) |click_action| {
255 wm.next_spawn_floating = click_action.floating;
256 wm.next_spawn_bypass_rules = click_action.bypass_rules;
257 actions.spawnCommand(wm, click_action.command);
258 }
259 return;
260 }
261
262 const click_client = client_mod.windowToClient(wm.monitors, event.window);
263 if (click_client) |found_client| {
264 core.focus(found_client, wm);
265 if (wm.selected_monitor) |selmon| {
266 core.restack(selmon, wm);
267 }
268 _ = xlib.XAllowEvents(wm.display.handle, xlib.ReplayPointer, xlib.CurrentTime);
269 }
270
271 const clean_state = cleanMask(event.state, wm);
272 for (wm.config.buttons.items) |button| {
273 if (button.click != .client_win) continue;
274 const button_clean_mask = cleanMask(button.mod_mask, wm);
275 if (clean_state == button_clean_mask and event.button == button.button) {
276 switch (button.action) {
277 .move_mouse => actions.movemouse(wm),
278 .resize_mouse => actions.resizemouse(wm),
279 .toggle_floating => {
280 if (click_client) |found_client| {
281 found_client.is_floating = !found_client.is_floating;
282 if (wm.selected_monitor) |monitor| {
283 core.arrange(monitor, wm);
284 }
285 }
286 },
287 }
288 return;
289 }
290 }
291 }
292
293 fn handleClientMessage(event: *xlib.XClientMessageEvent, wm: *WindowManager) void {
294 if (wm.getSystray()) |tray| {
295 if (tray.handleClientMessage(event)) {
296 wm.invalidateBars();
297 return;
298 }
299 }
300
301 const client = client_mod.windowToClient(wm.monitors, event.window) orelse return;
302
303 if (event.message_type == wm.atoms.net_wm_state) {
304 const action = event.data.l[0];
305 const first_property: xlib.Atom = @intCast(event.data.l[1]);
306 const second_property: xlib.Atom = @intCast(event.data.l[2]);
307
308 if (first_property == wm.atoms.net_wm_state_fullscreen or second_property == wm.atoms.net_wm_state_fullscreen) {
309 const net_wm_state_remove = 0;
310 const net_wm_state_add = 1;
311 const net_wm_state_toggle = 2;
312
313 if (action == net_wm_state_add) {
314 core.setFullscreen(client, true, wm);
315 } else if (action == net_wm_state_remove) {
316 core.setFullscreen(client, false, wm);
317 } else if (action == net_wm_state_toggle) {
318 core.setFullscreen(client, !client.is_fullscreen, wm);
319 }
320 }
321 } else if (event.message_type == wm.atoms.net_active_window) {
322 const selected = wm.selected_monitor orelse return;
323 if (client != selected.sel and !client.is_urgent) {
324 core.setUrgent(client, true, wm);
325 }
326 }
327 }
328
329 fn handleDestroyNotify(event: *xlib.XDestroyWindowEvent, wm: *WindowManager) void {
330 if (wm.getSystray()) |tray| {
331 if (tray.handleDestroyNotify(event.window)) {
332 wm.invalidateBars();
333 return;
334 }
335 }
336
337 const client = client_mod.windowToClient(wm.monitors, event.window) orelse return;
338 std.debug.print("destroy_notify: window=0x{x}\n", .{event.window});
339 core.unmanage(client, wm);
340 }
341
342 fn handleUnmapNotify(event: *xlib.XUnmapEvent, wm: *WindowManager) void {
343 if (wm.getSystray()) |tray| {
344 if (tray.handleUnmapNotify(event.window)) {
345 wm.invalidateBars();
346 return;
347 }
348 }
349
350 const client = client_mod.windowToClient(wm.monitors, event.window) orelse return;
351 std.debug.print("unmap_notify: window=0x{x}\n", .{event.window});
352 core.unmanage(client, wm);
353 }
354
355 fn handleEnterNotify(event: *xlib.XCrossingEvent, wm: *WindowManager) void {
356 if ((event.mode != xlib.NotifyNormal or event.detail == xlib.NotifyInferior) and event.window != wm.display.root) {
357 return;
358 }
359
360 const client = client_mod.windowToClient(wm.monitors, event.window);
361 const target_mon = if (client) |c| c.monitor else monitor_mod.windowToMonitor(wm, event.window);
362 const selmon = wm.selected_monitor;
363
364 if (target_mon != selmon) {
365 if (selmon) |sel| {
366 core.unfocusClient(sel.sel, true, wm);
367 }
368 wm.selected_monitor = target_mon;
369 } else if (client == null) {
370 return;
371 } else if (selmon) |sel| {
372 if (client.? == sel.sel) {
373 return;
374 }
375 }
376
377 core.focus(client, wm);
378 }
379
380 fn handleFocusIn(event: *xlib.XFocusChangeEvent, wm: *WindowManager) void {
381 const selmon = wm.selected_monitor orelse return;
382 const selected = selmon.sel orelse return;
383 if (event.window != selected.window) {
384 core.setFocus(selected, wm);
385 }
386 }
387
388 fn handleMotionNotify(event: *xlib.XMotionEvent, wm: *WindowManager) void {
389 if (event.window != wm.display.root) return;
390
391 const target_mon = monitor_mod.rectToMonitor(wm, event.x_root, event.y_root, 1, 1);
392 if (target_mon != wm.last_motion_monitor and wm.last_motion_monitor != null) {
393 if (wm.selected_monitor) |selmon| {
394 core.unfocusClient(selmon.sel, true, wm);
395 }
396 wm.selected_monitor = target_mon;
397 core.focus(null, wm);
398 }
399 wm.last_motion_monitor = target_mon;
400 }
401
402 fn handlePropertyNotify(event: *xlib.XPropertyEvent, wm: *WindowManager) void {
403 if (event.state == xlib.PropertyDelete) {
404 return;
405 }
406
407 if (wm.getSystray()) |tray| {
408 if (tray.handlePropertyNotify(event)) {
409 wm.invalidateBars();
410 return;
411 }
412 }
413
414 const client = client_mod.windowToClient(wm.monitors, event.window) orelse return;
415
416 if (event.atom == xlib.XA_WM_TRANSIENT_FOR) {
417 var trans: xlib.Window = 0;
418 if (!client.is_floating and xlib.XGetTransientForHint(wm.display.handle, client.window, &trans) != 0) {
419 client.is_floating = client_mod.windowToClient(wm.monitors, trans) != null;
420 if (client.is_floating) {
421 if (client.monitor) |monitor| {
422 core.arrange(monitor, wm);
423 }
424 }
425 }
426 } else if (event.atom == xlib.XA_WM_NORMAL_HINTS) {
427 client.hints_valid = false;
428 } else if (event.atom == xlib.XA_WM_HINTS) {
429 core.updateWmHints(client, wm);
430 wm.invalidateBars();
431 } else if (event.atom == xlib.XA_WM_NAME or event.atom == wm.atoms.net_wm_name) {
432 core.updateTitle(client, wm);
433 } else if (event.atom == wm.atoms.net_wm_window_type) {
434 core.updateWindowType(client, wm);
435 }
436 }
437
438 fn handleMappingNotify(event: *xlib.XMappingEvent, wm: *WindowManager) void {
439 _ = xlib.XRefreshKeyboardMapping(event);
440
441 if (event.request == xlib.MappingKeyboard or event.request == xlib.MappingModifier) {
442 wm.ungrabKeybinds();
443 wm.grabKeybinds();
444 }
445 }
446
447 fn handleReparentNotify(event: *xlib.XReparentEvent, wm: *WindowManager) void {
448 if (wm.getSystray()) |tray| {
449 if (tray.handleReparentNotify(event.window, event.parent)) {
450 wm.invalidateBars();
451 }
452 }
453 }
454
455 fn handleMapNotify(event: *xlib.c.XMapEvent, wm: *WindowManager) void {
456 if (wm.getSystray()) |tray| {
457 if (tray.handleMapNotify(event.window)) {
458 wm.invalidateBars();
459 }
460 }
461 }
462
463 fn handleResizeRequest(event: *xlib.XResizeRequestEvent, wm: *WindowManager) void {
464 if (wm.getSystray()) |tray| {
465 if (tray.handleResizeRequest(event.window, event.width, event.height)) {
466 wm.invalidateBars();
467 }
468 }
469 }
470
471 fn handleButtonRelease(event: *xlib.XButtonEvent, wm: *WindowManager) void {
472 if (wm.getSystray()) |tray| {
473 _ = tray.handleButtonRelease(event);
474 }
475 }