tony / oxwm

Branch: fd44600
Switch branches/tags
master
tonybanters Updated systray to support underlines in config.lua fd44600 2026-03-25
449 lines · 16.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 .button_release => handleButtonRelease(&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 handleButtonPress(event: *xlib.XButtonEvent, wm: *WindowManager) void {
210 std.debug.print("button_press: window=0x{x} subwindow=0x{x}\n", .{ event.window, event.subwindow });
211
212 if (wm.getSystray()) |tray| {
213 if (tray.handleButtonPress(event)) return;
214 }
215
216 const clicked_monitor = monitor_mod.windowToMonitor(wm, event.window);
217 if (clicked_monitor) |monitor| {
218 if (monitor != wm.selected_monitor) {
219 if (wm.selected_monitor) |selmon| {
220 core.unfocusClient(selmon.sel, true, wm);
221 }
222 wm.selected_monitor = monitor;
223 core.focus(null, wm);
224 }
225 }
226
227 if (bar_mod.windowToBar(wm.bars, event.window)) |bar| {
228 const clicked_tag = bar.handleClick(wm.display.handle, event.x, wm.config);
229 if (clicked_tag) |tag_index| {
230 const tag_mask: u32 = @as(u32, 1) << @intCast(tag_index);
231 core.view(tag_mask, wm);
232 }
233 return;
234 }
235
236 const click_client = client_mod.windowToClient(wm.monitors, event.window);
237 if (click_client) |found_client| {
238 core.focus(found_client, wm);
239 if (wm.selected_monitor) |selmon| {
240 core.restack(selmon, wm);
241 }
242 _ = xlib.XAllowEvents(wm.display.handle, xlib.ReplayPointer, xlib.CurrentTime);
243 }
244
245 const clean_state = cleanMask(event.state, wm);
246 for (wm.config.buttons.items) |button| {
247 if (button.click != .client_win) continue;
248 const button_clean_mask = cleanMask(button.mod_mask, wm);
249 if (clean_state == button_clean_mask and event.button == button.button) {
250 switch (button.action) {
251 .move_mouse => actions.movemouse(wm),
252 .resize_mouse => actions.resizemouse(wm),
253 .toggle_floating => {
254 if (click_client) |found_client| {
255 found_client.is_floating = !found_client.is_floating;
256 if (wm.selected_monitor) |monitor| {
257 core.arrange(monitor, wm);
258 }
259 }
260 },
261 }
262 return;
263 }
264 }
265 }
266
267 fn handleClientMessage(event: *xlib.XClientMessageEvent, wm: *WindowManager) void {
268 if (wm.getSystray()) |tray| {
269 if (tray.handleClientMessage(event)) {
270 wm.invalidateBars();
271 return;
272 }
273 }
274
275 const client = client_mod.windowToClient(wm.monitors, event.window) orelse return;
276
277 if (event.message_type == wm.atoms.net_wm_state) {
278 const action = event.data.l[0];
279 const first_property: xlib.Atom = @intCast(event.data.l[1]);
280 const second_property: xlib.Atom = @intCast(event.data.l[2]);
281
282 if (first_property == wm.atoms.net_wm_state_fullscreen or second_property == wm.atoms.net_wm_state_fullscreen) {
283 const net_wm_state_remove = 0;
284 const net_wm_state_add = 1;
285 const net_wm_state_toggle = 2;
286
287 if (action == net_wm_state_add) {
288 core.setFullscreen(client, true, wm);
289 } else if (action == net_wm_state_remove) {
290 core.setFullscreen(client, false, wm);
291 } else if (action == net_wm_state_toggle) {
292 core.setFullscreen(client, !client.is_fullscreen, wm);
293 }
294 }
295 } else if (event.message_type == wm.atoms.net_active_window) {
296 const selected = wm.selected_monitor orelse return;
297 if (client != selected.sel and !client.is_urgent) {
298 core.setUrgent(client, true, wm);
299 }
300 }
301 }
302
303 fn handleDestroyNotify(event: *xlib.XDestroyWindowEvent, wm: *WindowManager) void {
304 if (wm.getSystray()) |tray| {
305 if (tray.handleDestroyNotify(event.window)) {
306 wm.invalidateBars();
307 return;
308 }
309 }
310
311 const client = client_mod.windowToClient(wm.monitors, event.window) orelse return;
312 std.debug.print("destroy_notify: window=0x{x}\n", .{event.window});
313 core.unmanage(client, wm);
314 }
315
316 fn handleUnmapNotify(event: *xlib.XUnmapEvent, wm: *WindowManager) void {
317 if (wm.getSystray()) |tray| {
318 if (tray.handleUnmapNotify(event.window)) {
319 wm.invalidateBars();
320 return;
321 }
322 }
323
324 const client = client_mod.windowToClient(wm.monitors, event.window) orelse return;
325 std.debug.print("unmap_notify: window=0x{x}\n", .{event.window});
326 core.unmanage(client, wm);
327 }
328
329 fn handleEnterNotify(event: *xlib.XCrossingEvent, wm: *WindowManager) void {
330 if ((event.mode != xlib.NotifyNormal or event.detail == xlib.NotifyInferior) and event.window != wm.display.root) {
331 return;
332 }
333
334 const client = client_mod.windowToClient(wm.monitors, event.window);
335 const target_mon = if (client) |c| c.monitor else monitor_mod.windowToMonitor(wm, event.window);
336 const selmon = wm.selected_monitor;
337
338 if (target_mon != selmon) {
339 if (selmon) |sel| {
340 core.unfocusClient(sel.sel, true, wm);
341 }
342 wm.selected_monitor = target_mon;
343 } else if (client == null) {
344 return;
345 } else if (selmon) |sel| {
346 if (client.? == sel.sel) {
347 return;
348 }
349 }
350
351 core.focus(client, wm);
352 }
353
354 fn handleFocusIn(event: *xlib.XFocusChangeEvent, wm: *WindowManager) void {
355 const selmon = wm.selected_monitor orelse return;
356 const selected = selmon.sel orelse return;
357 if (event.window != selected.window) {
358 core.setFocus(selected, wm);
359 }
360 }
361
362 fn handleMotionNotify(event: *xlib.XMotionEvent, wm: *WindowManager) void {
363 if (event.window != wm.display.root) return;
364
365 const target_mon = monitor_mod.rectToMonitor(wm, event.x_root, event.y_root, 1, 1);
366 if (target_mon != wm.last_motion_monitor and wm.last_motion_monitor != null) {
367 if (wm.selected_monitor) |selmon| {
368 core.unfocusClient(selmon.sel, true, wm);
369 }
370 wm.selected_monitor = target_mon;
371 core.focus(null, wm);
372 }
373 wm.last_motion_monitor = target_mon;
374 }
375
376 fn handlePropertyNotify(event: *xlib.XPropertyEvent, wm: *WindowManager) void {
377 if (event.state == xlib.PropertyDelete) {
378 return;
379 }
380
381 if (wm.getSystray()) |tray| {
382 if (tray.handlePropertyNotify(event)) {
383 wm.invalidateBars();
384 return;
385 }
386 }
387
388 const client = client_mod.windowToClient(wm.monitors, event.window) orelse return;
389
390 if (event.atom == xlib.XA_WM_TRANSIENT_FOR) {
391 var trans: xlib.Window = 0;
392 if (!client.is_floating and xlib.XGetTransientForHint(wm.display.handle, client.window, &trans) != 0) {
393 client.is_floating = client_mod.windowToClient(wm.monitors, trans) != null;
394 if (client.is_floating) {
395 if (client.monitor) |monitor| {
396 core.arrange(monitor, wm);
397 }
398 }
399 }
400 } else if (event.atom == xlib.XA_WM_NORMAL_HINTS) {
401 client.hints_valid = false;
402 } else if (event.atom == xlib.XA_WM_HINTS) {
403 core.updateWmHints(client, wm);
404 wm.invalidateBars();
405 } else if (event.atom == xlib.XA_WM_NAME or event.atom == wm.atoms.net_wm_name) {
406 core.updateTitle(client, wm);
407 } else if (event.atom == wm.atoms.net_wm_window_type) {
408 core.updateWindowType(client, wm);
409 }
410 }
411
412 fn handleMappingNotify(event: *xlib.XMappingEvent, wm: *WindowManager) void {
413 _ = xlib.XRefreshKeyboardMapping(event);
414
415 if (event.request == xlib.MappingKeyboard or event.request == xlib.MappingModifier) {
416 wm.ungrabKeybinds();
417 wm.grabKeybinds();
418 }
419 }
420
421 fn handleReparentNotify(event: *xlib.XReparentEvent, wm: *WindowManager) void {
422 if (wm.getSystray()) |tray| {
423 if (tray.handleReparentNotify(event.window, event.parent)) {
424 wm.invalidateBars();
425 }
426 }
427 }
428
429 fn handleMapNotify(event: *xlib.c.XMapEvent, wm: *WindowManager) void {
430 if (wm.getSystray()) |tray| {
431 if (tray.handleMapNotify(event.window)) {
432 wm.invalidateBars();
433 }
434 }
435 }
436
437 fn handleResizeRequest(event: *xlib.XResizeRequestEvent, wm: *WindowManager) void {
438 if (wm.getSystray()) |tray| {
439 if (tray.handleResizeRequest(event.window, event.width, event.height)) {
440 wm.invalidateBars();
441 }
442 }
443 }
444
445 fn handleButtonRelease(event: *xlib.XButtonEvent, wm: *WindowManager) void {
446 if (wm.getSystray()) |tray| {
447 _ = tray.handleButtonRelease(event);
448 }
449 }