tony / oxwm

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