tony / oxwm

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