From 9fef3f70f5aa8997399929f2afdbc6e3e4aae3d6 Mon Sep 17 00:00:00 2001 From: Ben Buhse Date: Sun, 22 Feb 2026 14:57:41 -0600 Subject: [PATCH] Add XkbBinding.deinit() Also fixed a crash if two non-spawn keybinds had the same keys --- src/Config.zig | 8 +------- src/XkbBindings.zig | 41 +++++++++++++++++++++++++++++++++++++++++ src/config/keybind.zig | 8 +------- 3 files changed, 43 insertions(+), 14 deletions(-) diff --git a/src/Config.zig b/src/Config.zig index 2f5ac81..6f1d6ba 100644 --- a/src/Config.zig +++ b/src/Config.zig @@ -127,13 +127,7 @@ pub fn create() !*Config { pub fn destroy(config: *Config) void { for (config.keybinds.values()) |cmd| { - switch (cmd) { - .spawn => |argv| { - for (argv) |arg| utils.gpa.free(arg); - utils.gpa.free(argv); - }, - else => {}, - } + cmd.deinit(); } config.keybinds.deinit(utils.gpa); config.tag_binds.deinit(utils.gpa); diff --git a/src/XkbBindings.zig b/src/XkbBindings.zig index cda2f39..60f889a 100644 --- a/src/XkbBindings.zig +++ b/src/XkbBindings.zig @@ -50,6 +50,47 @@ pub const Command = union(enum) { // When passthrough is enabled, only keybinds set to toggle_passthrough are active toggle_passthrough, + + /// Explicitly list each variant so that, if we add a new one, + /// we'll get a reminder to free it here (instead of it being + /// swallowed by an `else =>`) + pub fn deinit(self: Command) void { + switch (self) { + .spawn => |argv| { + for (argv) |arg| utils.gpa.free(arg); + utils.gpa.free(argv); + }, + .focus_next_window, + .focus_prev_window, + .focus_next_output, + .focus_prev_output, + .send_to_next_output, + .send_to_prev_output, + .zoom, + .toggle_float, + .change_ratio, + .increment_primary_count, + .decrement_primary_count, + .reload_config, + .toggle_fullscreen, + .close_window, + .set_output_tags, + .set_window_tags, + .toggle_output_tags, + .toggle_window_tags, + .move_up, + .move_down, + .move_left, + .move_right, + .resize_width, + .resize_height, + .center_float, + .swap_next, + .swap_prev, + .toggle_passthrough, + => {}, + } + } }; const XkbBinding = struct { diff --git a/src/config/keybind.zig b/src/config/keybind.zig index a9c4206..08bc07b 100644 --- a/src/config/keybind.zig +++ b/src/config/keybind.zig @@ -185,13 +185,7 @@ pub fn load(config: *Config, parser: *kdl.Parser, hostname: ?[]const u8) !void { .keysym = keysym, }); if (gop.found_existing) { - switch (gop.value_ptr.*) { - .spawn => |argv| { - for (argv) |arg| utils.gpa.free(arg); - utils.gpa.free(argv); - }, - else => unreachable, - } + gop.value_ptr.deinit(); } gop.value_ptr.* = command; } else {