tony / oxwm

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