tony / oxwm

simplify X11 `event_name` Browse files
with tests
emzywastaken committed 2026-02-15 parent 4bf2831 commit 26f9543b95c2b451fe4e66b5fe943475228c5eef
Showing 3 changed files with 27 additions and 38 deletions. UnifiedSplit
+10 −0 build.zig
@@ -40,6 +40,16 @@ pub fn build(b: *std.Build) void {
40 }); 40 });
41 test_step.dependOn(&b.addRunArtifact(unit_tests).step); 41 test_step.dependOn(&b.addRunArtifact(unit_tests).step);
42 42
43 + const src_main_unit_tests = b.addTest(.{
44 + .root_module = b.createModule(.{
45 + .root_source_file = b.path("src/main.zig"),
46 + .target = target,
47 + .optimize = optimize,
48 + }),
49 + });
50 + src_main_unit_tests.linkLibC();
51 + test_step.dependOn(&b.addRunArtifact(src_main_unit_tests).step);
52 +
43 const lua_config_tests = b.addTest(.{ 53 const lua_config_tests = b.addTest(.{
44 .root_module = b.createModule(.{ 54 .root_module = b.createModule(.{
45 .root_source_file = b.path("tests/lua_config_tests.zig"), 55 .root_source_file = b.path("tests/lua_config_tests.zig"),
+5 −1 src/main.zig
@@ -562,7 +562,7 @@ fn get_state(display: *Display, window: xlib.Window) c_long {
562 return WithdrawnState; 562 return WithdrawnState;
563 } 563 }
564 564
565 - const state: c_long = @as(*c_long, @alignCast(@ptrCast(prop))).*; 565 + const state: c_long = @as(*c_long, @ptrCast(@alignCast(prop))).*;
566 _ = xlib.XFree(prop); 566 _ = xlib.XFree(prop);
567 return state; 567 return state;
568 } 568 }
@@ -2506,3 +2506,7 @@ fn set_urgent(display: *Display, client: *Client, urgent: bool) void {
2506 _ = xlib.XFree(@ptrCast(hints)); 2506 _ = xlib.XFree(@ptrCast(hints));
2507 } 2507 }
2508 } 2508 }
2509 +
2510 +test {
2511 + _ = @import("x11/events.zig");
2512 +}
+12 −37 src/x11/events.zig
@@ -44,41 +44,16 @@ pub fn get_event_type(event: *const xlib.XEvent) EventType {
44 } 44 }
45 45
46 pub fn event_name(event_type: EventType) []const u8 { 46 pub fn event_name(event_type: EventType) []const u8 {
47 - return switch (event_type) { 47 + if (@intFromEnum(event_type) > @intFromEnum(EventType.generic_event)) return "unknown";
48 - .key_press => "key_press", 48 +
49 - .key_release => "key_release", 49 + return @tagName(event_type);
50 - .button_press => "button_press", 50 +}
51 - .button_release => "button_release", 51 +
52 - .motion_notify => "motion_notify", 52 +test event_name {
53 - .enter_notify => "enter_notify", 53 + const testing = std.testing;
54 - .leave_notify => "leave_notify", 54 +
55 - .focus_in => "focus_in", 55 + const name = event_name(.key_press);
56 - .focus_out => "focus_out", 56 + try testing.expectEqualStrings("key_press", name);
57 - .keymap_notify => "keymap_notify", 57 +
58 - .expose => "expose", 58 + try testing.expectEqualStrings("unknown", event_name(@enumFromInt(100)));
59 - .graphics_expose => "graphics_expose",
60 - .no_expose => "no_expose",
61 - .visibility_notify => "visibility_notify",
62 - .create_notify => "create_notify",
63 - .destroy_notify => "destroy_notify",
64 - .unmap_notify => "unmap_notify",
65 - .map_notify => "map_notify",
66 - .map_request => "map_request",
67 - .reparent_notify => "reparent_notify",
68 - .configure_notify => "configure_notify",
69 - .configure_request => "configure_request",
70 - .gravity_notify => "gravity_notify",
71 - .resize_request => "resize_request",
72 - .circulate_notify => "circulate_notify",
73 - .circulate_request => "circulate_request",
74 - .property_notify => "property_notify",
75 - .selection_clear => "selection_clear",
76 - .selection_request => "selection_request",
77 - .selection_notify => "selection_notify",
78 - .colormap_notify => "colormap_notify",
79 - .client_message => "client_message",
80 - .mapping_notify => "mapping_notify",
81 - .generic_event => "generic_event",
82 - _ => "unknown",
83 - };
84 } 59 }