tony / oxwm

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