tony / oxwm

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