oxwm

https://git.tonybtw.com/oxwm.git git://git.tonybtw.com/oxwm.git

simplify X11 `event_name`

Commit
26f9543b95c2b451fe4e66b5fe943475228c5eef
Parent
4bf2831
Author
emzywastaken <amiamemetoo@gmail.com>
Date
2026-02-15 18:04:14
with tests

Diff

diff --git a/build.zig b/build.zig
index 230d26a..2f728da 100644
--- a/build.zig
+++ b/build.zig
@@ -40,6 +40,16 @@ pub fn build(b: *std.Build) void {
     });
     test_step.dependOn(&b.addRunArtifact(unit_tests).step);
 
+    const src_main_unit_tests = b.addTest(.{
+        .root_module = b.createModule(.{
+            .root_source_file = b.path("src/main.zig"),
+            .target = target,
+            .optimize = optimize,
+        }),
+    });
+    src_main_unit_tests.linkLibC();
+    test_step.dependOn(&b.addRunArtifact(src_main_unit_tests).step);
+
     const lua_config_tests = b.addTest(.{
         .root_module = b.createModule(.{
             .root_source_file = b.path("tests/lua_config_tests.zig"),
diff --git a/src/main.zig b/src/main.zig
index df01d71..d0e54a3 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -562,7 +562,7 @@ fn get_state(display: *Display, window: xlib.Window) c_long {
         return WithdrawnState;
     }
 
-    const state: c_long = @as(*c_long, @alignCast(@ptrCast(prop))).*;
+    const state: c_long = @as(*c_long, @ptrCast(@alignCast(prop))).*;
     _ = xlib.XFree(prop);
     return state;
 }
@@ -2506,3 +2506,7 @@ fn set_urgent(display: *Display, client: *Client, urgent: bool) void {
         _ = xlib.XFree(@ptrCast(hints));
     }
 }
+
+test {
+    _ = @import("x11/events.zig");
+}
diff --git a/src/x11/events.zig b/src/x11/events.zig
index 15a3e86..405e674 100644
--- a/src/x11/events.zig
+++ b/src/x11/events.zig
@@ -44,41 +44,16 @@ pub fn get_event_type(event: *const xlib.XEvent) EventType {
 }
 
 pub fn event_name(event_type: EventType) []const u8 {
-    return switch (event_type) {
-        .key_press => "key_press",
-        .key_release => "key_release",
-        .button_press => "button_press",
-        .button_release => "button_release",
-        .motion_notify => "motion_notify",
-        .enter_notify => "enter_notify",
-        .leave_notify => "leave_notify",
-        .focus_in => "focus_in",
-        .focus_out => "focus_out",
-        .keymap_notify => "keymap_notify",
-        .expose => "expose",
-        .graphics_expose => "graphics_expose",
-        .no_expose => "no_expose",
-        .visibility_notify => "visibility_notify",
-        .create_notify => "create_notify",
-        .destroy_notify => "destroy_notify",
-        .unmap_notify => "unmap_notify",
-        .map_notify => "map_notify",
-        .map_request => "map_request",
-        .reparent_notify => "reparent_notify",
-        .configure_notify => "configure_notify",
-        .configure_request => "configure_request",
-        .gravity_notify => "gravity_notify",
-        .resize_request => "resize_request",
-        .circulate_notify => "circulate_notify",
-        .circulate_request => "circulate_request",
-        .property_notify => "property_notify",
-        .selection_clear => "selection_clear",
-        .selection_request => "selection_request",
-        .selection_notify => "selection_notify",
-        .colormap_notify => "colormap_notify",
-        .client_message => "client_message",
-        .mapping_notify => "mapping_notify",
-        .generic_event => "generic_event",
-        _ => "unknown",
-    };
+    if (@intFromEnum(event_type) > @intFromEnum(EventType.generic_event)) return "unknown";
+
+    return @tagName(event_type);
+}
+
+test event_name {
+    const testing = std.testing;
+
+    const name = event_name(.key_press);
+    try testing.expectEqualStrings("key_press", name);
+
+    try testing.expectEqualStrings("unknown", event_name(@enumFromInt(100)));
 }