tony / oxwm

Branch: a8f88e9
Switch branches/tags
master
emzywastaken treewide: change function names to camelCase b3ae92e 2026-02-23
362 lines · 14.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 .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 }
213 return;
214 }
215
216 const click_client = client_mod.windowToClient(wm.monitors, event.window);
217 if (click_client) |found_client| {
218 core.focus(found_client, wm);
219 if (wm.selected_monitor) |selmon| {
220 core.restack(selmon, wm);
221 }
222 _ = xlib.XAllowEvents(wm.display.handle, xlib.ReplayPointer, xlib.CurrentTime);
223 }
224
225 const clean_state = cleanMask(event.state, wm);
226 for (wm.config.buttons.items) |button| {
227 if (button.click != .client_win) continue;
228 const button_clean_mask = cleanMask(button.mod_mask, wm);
229 if (clean_state == button_clean_mask and event.button == button.button) {
230 switch (button.action) {
231 .move_mouse => actions.movemouse(wm),
232 .resize_mouse => actions.resizemouse(wm),
233 .toggle_floating => {
234 if (click_client) |found_client| {
235 found_client.is_floating = !found_client.is_floating;
236 if (wm.selected_monitor) |monitor| {
237 core.arrange(monitor, wm);
238 }
239 }
240 },
241 }
242 return;
243 }
244 }
245 }
246
247 fn handleClientMessage(event: *xlib.XClientMessageEvent, wm: *WindowManager) void {
248 const client = client_mod.windowToClient(wm.monitors, event.window) orelse return;
249
250 if (event.message_type == wm.atoms.net_wm_state) {
251 const action = event.data.l[0];
252 const first_property: xlib.Atom = @intCast(event.data.l[1]);
253 const second_property: xlib.Atom = @intCast(event.data.l[2]);
254
255 if (first_property == wm.atoms.net_wm_state_fullscreen or second_property == wm.atoms.net_wm_state_fullscreen) {
256 const net_wm_state_remove = 0;
257 const net_wm_state_add = 1;
258 const net_wm_state_toggle = 2;
259
260 if (action == net_wm_state_add) {
261 core.setFullscreen(client, true, wm);
262 } else if (action == net_wm_state_remove) {
263 core.setFullscreen(client, false, wm);
264 } else if (action == net_wm_state_toggle) {
265 core.setFullscreen(client, !client.is_fullscreen, wm);
266 }
267 }
268 } else if (event.message_type == wm.atoms.net_active_window) {
269 const selected = wm.selected_monitor orelse return;
270 if (client != selected.sel and !client.is_urgent) {
271 core.setUrgent(client, true, wm);
272 }
273 }
274 }
275
276 fn handleDestroyNotify(event: *xlib.XDestroyWindowEvent, wm: *WindowManager) void {
277 const client = client_mod.windowToClient(wm.monitors, event.window) orelse return;
278 std.debug.print("destroy_notify: window=0x{x}\n", .{event.window});
279 core.unmanage(client, wm);
280 }
281
282 fn handleUnmapNotify(event: *xlib.XUnmapEvent, wm: *WindowManager) void {
283 const client = client_mod.windowToClient(wm.monitors, event.window) orelse return;
284 std.debug.print("unmap_notify: window=0x{x}\n", .{event.window});
285 core.unmanage(client, wm);
286 }
287
288 fn handleEnterNotify(event: *xlib.XCrossingEvent, wm: *WindowManager) void {
289 if ((event.mode != xlib.NotifyNormal or event.detail == xlib.NotifyInferior) and event.window != wm.display.root) {
290 return;
291 }
292
293 const client = client_mod.windowToClient(wm.monitors, event.window);
294 const target_mon = if (client) |c| c.monitor else monitor_mod.windowToMonitor(wm, event.window);
295 const selmon = wm.selected_monitor;
296
297 if (target_mon != selmon) {
298 if (selmon) |sel| {
299 core.unfocusClient(sel.sel, true, wm);
300 }
301 wm.selected_monitor = target_mon;
302 } else if (client == null) {
303 return;
304 } else if (selmon) |sel| {
305 if (client.? == sel.sel) {
306 return;
307 }
308 }
309
310 core.focus(client, wm);
311 }
312
313 fn handleFocusIn(event: *xlib.XFocusChangeEvent, wm: *WindowManager) void {
314 const selmon = wm.selected_monitor orelse return;
315 const selected = selmon.sel orelse return;
316 if (event.window != selected.window) {
317 core.setFocus(selected, wm);
318 }
319 }
320
321 fn handleMotionNotify(event: *xlib.XMotionEvent, wm: *WindowManager) void {
322 if (event.window != wm.display.root) return;
323
324 const target_mon = monitor_mod.rectToMonitor(wm, event.x_root, event.y_root, 1, 1);
325 if (target_mon != wm.last_motion_monitor and wm.last_motion_monitor != null) {
326 if (wm.selected_monitor) |selmon| {
327 core.unfocusClient(selmon.sel, true, wm);
328 }
329 wm.selected_monitor = target_mon;
330 core.focus(null, wm);
331 }
332 wm.last_motion_monitor = target_mon;
333 }
334
335 fn handlePropertyNotify(event: *xlib.XPropertyEvent, wm: *WindowManager) void {
336 if (event.state == xlib.PropertyDelete) {
337 return;
338 }
339
340 const client = client_mod.windowToClient(wm.monitors, event.window) orelse return;
341
342 if (event.atom == xlib.XA_WM_TRANSIENT_FOR) {
343 var trans: xlib.Window = 0;
344 if (!client.is_floating and xlib.XGetTransientForHint(wm.display.handle, client.window, &trans) != 0) {
345 client.is_floating = client_mod.windowToClient(wm.monitors, trans) != null;
346 if (client.is_floating) {
347 if (client.monitor) |monitor| {
348 core.arrange(monitor, wm);
349 }
350 }
351 }
352 } else if (event.atom == xlib.XA_WM_NORMAL_HINTS) {
353 client.hints_valid = false;
354 } else if (event.atom == xlib.XA_WM_HINTS) {
355 core.updateWmHints(client, wm);
356 wm.invalidateBars();
357 } else if (event.atom == xlib.XA_WM_NAME or event.atom == wm.atoms.net_wm_name) {
358 core.updateTitle(client, wm);
359 } else if (event.atom == wm.atoms.net_wm_window_type) {
360 core.updateWindowType(client, wm);
361 }
362 }