From d9ca9db5a49f0af2f6bef6f198b28a8d29808e6d Mon Sep 17 00:00:00 2001 From: Isaac Freund Date: Tue, 26 May 2020 22:55:07 +0200 Subject: [PATCH] Rework commands to be string based This allows for significantly more flexibility and should make implementing the bind command possible. --- src/Config.zig | 310 ++++++++++++++++-------------- src/Control.zig | 41 ++-- src/Keybind.zig | 27 ++- src/Mode.zig | 2 +- src/Seat.zig | 9 +- src/{Command.zig => command.zig} | 99 +++++----- src/command/close.zig | 11 +- src/command/exit.zig | 12 +- src/command/focus.zig | 19 +- src/command/focus_all_tags.zig | 11 +- src/command/focus_output.zig | 15 +- src/command/focus_tag.zig | 17 +- src/command/layout.zig | 17 +- src/command/mod_master_count.zig | 14 +- src/command/mod_master_factor.zig | 14 +- src/command/mode.zig | 17 +- src/command/send_to_output.zig | 15 +- src/command/spawn.zig | 34 ++-- src/command/tag_view.zig | 17 +- src/command/tag_view_all_tags.zig | 11 +- src/command/toggle_float.zig | 12 +- src/command/toggle_tag_focus.zig | 17 +- src/command/toggle_view_tag.zig | 17 +- src/command/zoom.zig | 13 +- 24 files changed, 501 insertions(+), 270 deletions(-) rename src/{Command.zig => command.zig} (61%) diff --git a/src/Config.zig b/src/Config.zig index 54eb58a..679f248 100644 --- a/src/Config.zig +++ b/src/Config.zig @@ -21,10 +21,10 @@ const std = @import("std"); const c = @import("c.zig"); -const Command = @import("Command.zig"); const Log = @import("log.zig").Log; const Mode = @import("Mode.zig"); const Server = @import("Server.zig"); +const Keybind = @import("Keybind.zig"); /// Width of borders in pixels border_width: u32, @@ -55,190 +55,218 @@ pub fn init(self: *Self, allocator: *std.mem.Allocator) !void { const mod = c.WLR_MODIFIER_LOGO; // Mod+Shift+Return to start an instance of alacritty - try normal.keybinds.append(.{ - .keysym = c.XKB_KEY_Return, - .modifiers = mod | c.WLR_MODIFIER_SHIFT, - .command = try Command.init(&[_][]const u8{ "spawn", "alacritty" }, allocator), - }); + try normal.keybinds.append(try Keybind.init( + allocator, + c.XKB_KEY_Return, + mod | c.WLR_MODIFIER_SHIFT, + &[_][]const u8{ "spawn", "alacritty" }, + )); // Mod+Q to close the focused view - try normal.keybinds.append(.{ - .keysym = c.XKB_KEY_q, - .modifiers = mod, - .command = try Command.init(&[_][]const u8{"close"}, allocator), - }); + try normal.keybinds.append(try Keybind.init( + allocator, + c.XKB_KEY_q, + mod, + &[_][]const u8{"close"}, + )); // Mod+E to exit river - try normal.keybinds.append(.{ - .keysym = c.XKB_KEY_e, - .modifiers = mod, - .command = try Command.init(&[_][]const u8{"exit"}, allocator), - }); + try normal.keybinds.append(try Keybind.init( + allocator, + c.XKB_KEY_e, + mod, + &[_][]const u8{"exit"}, + )); // Mod+J and Mod+K to focus the next/previous view in the layout stack - try normal.keybinds.append(.{ - .keysym = c.XKB_KEY_j, - .modifiers = mod, - .command = try Command.init(&[_][]const u8{ "focus", "next" }, allocator), - }); - try normal.keybinds.append(.{ - .keysym = c.XKB_KEY_k, - .modifiers = mod, - .command = try Command.init(&[_][]const u8{ "focus", "previous" }, allocator), - }); + try normal.keybinds.append(try Keybind.init( + allocator, + c.XKB_KEY_j, + mod, + &[_][]const u8{ "focus", "next" }, + )); + try normal.keybinds.append(try Keybind.init( + allocator, + c.XKB_KEY_k, + mod, + &[_][]const u8{ "focus", "previous" }, + )); // Mod+Return to bump the focused view to the top of the layout stack, // making it the new master - try normal.keybinds.append(.{ - .keysym = c.XKB_KEY_Return, - .modifiers = mod, - .command = try Command.init(&[_][]const u8{"zoom"}, allocator), - }); + try normal.keybinds.append(try Keybind.init( + allocator, + c.XKB_KEY_Return, + mod, + &[_][]const u8{"zoom"}, + )); // Mod+H and Mod+L to increase/decrease the width of the master column - try normal.keybinds.append(.{ - .keysym = c.XKB_KEY_h, - .modifiers = mod, - .command = try Command.init(&[_][]const u8{ "mod_master_factor", "+0.05" }, allocator), - }); - try normal.keybinds.append(.{ - .keysym = c.XKB_KEY_l, - .modifiers = mod, - .command = try Command.init(&[_][]const u8{ "mod_master_factor", "-0.05" }, allocator), - }); + try normal.keybinds.append(try Keybind.init( + allocator, + c.XKB_KEY_h, + mod, + &[_][]const u8{ "mod_master_factor", "+0.05" }, + )); + try normal.keybinds.append(try Keybind.init( + allocator, + c.XKB_KEY_l, + mod, + &[_][]const u8{ "mod_master_factor", "-0.05" }, + )); // Mod+Shift+H and Mod+Shift+L to increment/decrement the number of // master views in the layout - try normal.keybinds.append(.{ - .keysym = c.XKB_KEY_h, - .modifiers = mod | c.WLR_MODIFIER_SHIFT, - .command = try Command.init(&[_][]const u8{ "mod_master_count", "+1" }, allocator), - }); - try normal.keybinds.append(.{ - .keysym = c.XKB_KEY_l, - .modifiers = mod | c.WLR_MODIFIER_SHIFT, - .command = try Command.init(&[_][]const u8{ "mod_master_count", "+1" }, allocator), - }); + try normal.keybinds.append(try Keybind.init( + allocator, + c.XKB_KEY_h, + mod | c.WLR_MODIFIER_SHIFT, + &[_][]const u8{ "mod_master_count", "+1" }, + )); + try normal.keybinds.append(try Keybind.init( + allocator, + c.XKB_KEY_l, + mod | c.WLR_MODIFIER_SHIFT, + &[_][]const u8{ "mod_master_count", "+1" }, + )); comptime var i = 0; inline while (i < 9) : (i += 1) { const str = &[_]u8{i + '0' + 1}; // Mod+[1-9] to focus tag [1-9] - try normal.keybinds.append(.{ - .keysym = c.XKB_KEY_1 + i, - .modifiers = mod, - .command = try Command.init(&[_][]const u8{ "focus_tag", str }, allocator), - }); + try normal.keybinds.append(try Keybind.init( + allocator, + c.XKB_KEY_1 + i, + mod, + &[_][]const u8{ "focus_tag", str }, + )); // Mod+Shift+[1-9] to tag focused view with tag [1-9] - try normal.keybinds.append(.{ - .keysym = c.XKB_KEY_1 + i, - .modifiers = mod | c.WLR_MODIFIER_SHIFT, - .command = try Command.init(&[_][]const u8{ "tag_view", str }, allocator), - }); + try normal.keybinds.append(try Keybind.init( + allocator, + c.XKB_KEY_1 + i, + mod | c.WLR_MODIFIER_SHIFT, + &[_][]const u8{ "tag_view", str }, + )); // Mod+Ctrl+[1-9] to toggle focus of tag [1-9] - try normal.keybinds.append(.{ - .keysym = c.XKB_KEY_1 + i, - .modifiers = mod | c.WLR_MODIFIER_CTRL, - .command = try Command.init(&[_][]const u8{ "toggle_tag_focus", str }, allocator), - }); + try normal.keybinds.append(try Keybind.init( + allocator, + c.XKB_KEY_1 + i, + mod | c.WLR_MODIFIER_CTRL, + &[_][]const u8{ "toggle_tag_focus", str }, + )); // Mod+Shift+Ctrl+[1-9] to toggle tag [1-9] of focused view - try normal.keybinds.append(.{ - .keysym = c.XKB_KEY_1 + i, - .modifiers = mod | c.WLR_MODIFIER_CTRL | c.WLR_MODIFIER_SHIFT, - .command = try Command.init(&[_][]const u8{ "toggle_view_tag", str }, allocator), - }); + try normal.keybinds.append(try Keybind.init( + allocator, + c.XKB_KEY_1 + i, + mod | c.WLR_MODIFIER_CTRL | c.WLR_MODIFIER_SHIFT, + &[_][]const u8{ "toggle_view_tag", str }, + )); } // Mod+0 to focus all tags - try normal.keybinds.append(.{ - .keysym = c.XKB_KEY_0, - .modifiers = mod, - .command = try Command.init(&[_][]const u8{"focus_all_tags"}, allocator), - }); + try normal.keybinds.append(try Keybind.init( + allocator, + c.XKB_KEY_0, + mod, + &[_][]const u8{"focus_all_tags"}, + )); // Mod+Shift+0 to tag focused view with all tags - try normal.keybinds.append(.{ - .keysym = c.XKB_KEY_0, - .modifiers = mod | c.WLR_MODIFIER_SHIFT, - .command = try Command.init(&[_][]const u8{"tag_view_all_tags"}, allocator), - }); + try normal.keybinds.append(try Keybind.init( + allocator, + c.XKB_KEY_0, + mod | c.WLR_MODIFIER_SHIFT, + &[_][]const u8{"tag_view_all_tags"}, + )); // Mod+Period and Mod+Comma to focus the next/previous output - try normal.keybinds.append(.{ - .keysym = c.XKB_KEY_period, - .modifiers = mod, - .command = try Command.init(&[_][]const u8{ "focus_output", "next" }, allocator), - }); - try normal.keybinds.append(.{ - .keysym = c.XKB_KEY_comma, - .modifiers = mod, - .command = try Command.init(&[_][]const u8{ "focus_output", "previous" }, allocator), - }); + try normal.keybinds.append(try Keybind.init( + allocator, + c.XKB_KEY_period, + mod, + &[_][]const u8{ "focus_output", "next" }, + )); + try normal.keybinds.append(try Keybind.init( + allocator, + c.XKB_KEY_comma, + mod, + &[_][]const u8{ "focus_output", "previous" }, + )); // Mod+Shift+Period/Comma to send the focused view to the the // next/previous output - try normal.keybinds.append(.{ - .keysym = c.XKB_KEY_period, - .modifiers = mod | c.WLR_MODIFIER_SHIFT, - .command = try Command.init(&[_][]const u8{ "send_to_output", "next" }, allocator), - }); - try normal.keybinds.append(.{ - .keysym = c.XKB_KEY_comma, - .modifiers = mod | c.WLR_MODIFIER_SHIFT, - .command = try Command.init(&[_][]const u8{ "send_to_output", "previous" }, allocator), - }); + try normal.keybinds.append(try Keybind.init( + allocator, + c.XKB_KEY_period, + mod | c.WLR_MODIFIER_SHIFT, + &[_][]const u8{ "send_to_output", "next" }, + )); + try normal.keybinds.append(try Keybind.init( + allocator, + c.XKB_KEY_comma, + mod | c.WLR_MODIFIER_SHIFT, + &[_][]const u8{ "send_to_output", "previous" }, + )); // Mod+Space to toggle float - try normal.keybinds.append(.{ - .keysym = c.XKB_KEY_space, - .modifiers = mod, - .command = try Command.init(&[_][]const u8{"toggle_float"}, allocator), - }); + try normal.keybinds.append(try Keybind.init( + allocator, + c.XKB_KEY_space, + mod, + &[_][]const u8{"toggle_float"}, + )); // Mod+F11 to enter passthrough mode - try normal.keybinds.append(.{ - .keysym = c.XKB_KEY_F11, - .modifiers = mod, - .command = try Command.init(&[_][]const u8{ "mode", "passthrough" }, allocator), - }); + try normal.keybinds.append(try Keybind.init( + allocator, + c.XKB_KEY_F11, + mod, + &[_][]const u8{ "mode", "passthrough" }, + )); try self.modes.append(try Mode.init("passthrough", allocator)); // Mod+F11 to return to normal mode - try self.modes.items[1].keybinds.append(.{ - .keysym = c.XKB_KEY_F11, - .modifiers = mod, - .command = try Command.init(&[_][]const u8{ "mode", "normal" }, allocator), - }); + try self.modes.items[1].keybinds.append(try Keybind.init( + allocator, + c.XKB_KEY_F11, + mod, + &[_][]const u8{ "mode", "normal" }, + )); // Change master orientation with Mod+{Up,Right,Down,Left} - try normal.keybinds.append(.{ - .keysym = c.XKB_KEY_Up, - .modifiers = mod, - .command = try Command.init(&[_][]const u8{ "layout", "TopMaster" }, allocator), - }); - try normal.keybinds.append(.{ - .keysym = c.XKB_KEY_Right, - .modifiers = mod, - .command = try Command.init(&[_][]const u8{ "layout", "RightMaster" }, allocator), - }); - try normal.keybinds.append(.{ - .keysym = c.XKB_KEY_Down, - .modifiers = mod, - .command = try Command.init(&[_][]const u8{ "layout", "BottomMaster" }, allocator), - }); - try normal.keybinds.append(.{ - .keysym = c.XKB_KEY_Left, - .modifiers = mod, - .command = try Command.init(&[_][]const u8{ "layout", "LeftMaster" }, allocator), - }); + try normal.keybinds.append(try Keybind.init( + allocator, + c.XKB_KEY_Up, + mod, + &[_][]const u8{ "layout", "TopMaster" }, + )); + try normal.keybinds.append(try Keybind.init( + allocator, + c.XKB_KEY_Right, + mod, + &[_][]const u8{ "layout", "RightMaster" }, + )); + try normal.keybinds.append(try Keybind.init( + allocator, + c.XKB_KEY_Down, + mod, + &[_][]const u8{ "layout", "BottomMaster" }, + )); + try normal.keybinds.append(try Keybind.init( + allocator, + c.XKB_KEY_Left, + mod, + &[_][]const u8{ "layout", "LeftMaster" }, + )); // Mod+f to change to Full layout - try normal.keybinds.append(.{ - .keysym = c.XKB_KEY_f, - .modifiers = mod, - .command = try Command.init(&[_][]const u8{ "layout", "Full" }, allocator), - }); + try normal.keybinds.append(try Keybind.init( + allocator, + c.XKB_KEY_f, + mod, + &[_][]const u8{ "layout", "Full" }, + )); // Float views with app_id "float" try self.float_filter.append("float"); diff --git a/src/Control.zig b/src/Control.zig index b870ada..6a94502 100644 --- a/src/Control.zig +++ b/src/Control.zig @@ -20,8 +20,8 @@ const Self = @This(); const std = @import("std"); const c = @import("c.zig"); +const command = @import("command.zig"); -const Command = @import("Command.zig"); const Log = @import("log.zig").Log; const Server = @import("Server.zig"); @@ -82,6 +82,7 @@ fn runCommand( ) callconv(.C) void { const self = @ptrCast(*Self, @alignCast(@alignOf(*Self), c.wl_resource_get_user_data(wl_resource))); const allocator = self.server.allocator; + const seat = self.server.input_manager.default_seat; var args = std.ArrayList([]const u8).init(allocator); @@ -106,22 +107,30 @@ fn runCommand( c.wl_resource_set_implementation(callback_resource, null, null, null); - const command = Command.init(args.items, allocator) catch |err| { - c.zriver_command_callback_v1_send_failure( - callback_resource, - switch (err) { - Command.Error.NoCommand => "no command given", - Command.Error.UnknownCommand => "unknown command", - Command.Error.NotEnoughArguments => "not enough arguments", - Command.Error.TooManyArguments => "too many arguments", - Command.Error.Overflow => "value out of bounds", - Command.Error.InvalidCharacter => "invalid character in argument", - Command.Error.InvalidDirection => "invalid direction. Must be 'next' or 'previous'", - Command.Error.OutOfMemory => unreachable, - }, - ); + var failure_message: []const u8 = undefined; + command.run(allocator, seat, args.items, &failure_message) catch |err| { + if (err == command.Error.CommandFailed) { + const out = std.cstr.addNullByte(allocator, failure_message) catch "out of memory"; + defer allocator.free(out); + allocator.free(failure_message); + c.zriver_command_callback_v1_send_failure(callback_resource, out); + } else { + c.zriver_command_callback_v1_send_failure( + callback_resource, + switch (err) { + command.Error.NoCommand => "no command given", + command.Error.UnknownCommand => "unknown command", + command.Error.NotEnoughArguments => "not enough arguments", + command.Error.TooManyArguments => "too many arguments", + command.Error.Overflow => "value out of bounds", + command.Error.InvalidCharacter => "invalid character in argument", + command.Error.InvalidDirection => "invalid direction. Must be 'next' or 'previous'", + command.Error.OutOfMemory => "out of memory", + command.Error.CommandFailed => unreachable, + }, + ); + } return; }; c.zriver_command_callback_v1_send_success(callback_resource); - command.run(self.server.input_manager.default_seat); } diff --git a/src/Keybind.zig b/src/Keybind.zig index d82be7a..61df624 100644 --- a/src/Keybind.zig +++ b/src/Keybind.zig @@ -15,9 +15,32 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . +const Self = @This(); + +const std = @import("std"); + const c = @import("c.zig"); -const Command = @import("Command.zig"); keysym: c.xkb_keysym_t, modifiers: u32, -command: Command, +command_args: []const []const u8, + +pub fn init( + allocator: *std.mem.Allocator, + keysym: c.xkb_keysym_t, + modifiers: u32, + command_args: []const []const u8, +) !Self { + var owned_args = try allocator.alloc([]u8, command_args.len); + for (command_args) |arg, i| owned_args[i] = try std.mem.dupe(allocator, u8, arg); + return Self{ + .keysym = keysym, + .modifiers = modifiers, + .command_args = owned_args, + }; +} + +pub fn deinit(self: Self, allocator: *std.mem.Allocator) void { + for (self.command_args) |arg| allocator.free(arg); + allocator.free(self.command_args); +} diff --git a/src/Mode.zig b/src/Mode.zig index dfc4f54..b63481e 100644 --- a/src/Mode.zig +++ b/src/Mode.zig @@ -38,6 +38,6 @@ pub fn init(name: []const u8, allocator: *std.mem.Allocator) !Self { pub fn deinit(self: Self) void { const allocator = self.keybinds.allocator; allocator.free(self.name); - for (self.keybinds.items) |keybind| keybind.command.deinit(allocator); + for (self.keybinds.items) |keybind| keybind.deinit(allocator); self.keybinds.deinit(); } diff --git a/src/Seat.zig b/src/Seat.zig index 3ff3667..7e0b06b 100644 --- a/src/Seat.zig +++ b/src/Seat.zig @@ -20,6 +20,7 @@ const Self = @This(); const std = @import("std"); const c = @import("c.zig"); +const command = @import("command.zig"); const Cursor = @import("Cursor.zig"); const InputManager = @import("InputManager.zig"); @@ -252,7 +253,13 @@ pub fn handleKeybinding(self: *Self, keysym: c.xkb_keysym_t, modifiers: u32) boo for (self.mode.keybinds.items) |keybind| { if (modifiers == keybind.modifiers and keysym == keybind.keysym) { // Execute the bound command - keybind.command.run(self); + const allocator = self.input_manager.server.allocator; + var failure_message: []const u8 = undefined; + command.run(allocator, self, keybind.command_args, &failure_message) catch |err| { + // TODO: log the error + if (err == command.Error.CommandFailed) + allocator.free(failure_message); + }; return true; } } diff --git a/src/Command.zig b/src/command.zig similarity index 61% rename from src/Command.zig rename to src/command.zig index 0632e5c..745c86c 100644 --- a/src/Command.zig +++ b/src/command.zig @@ -15,13 +15,11 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -const Self = @This(); - const std = @import("std"); const Seat = @import("Seat.zig"); -const command = struct { +const impl = struct { const close = @import("command/close.zig").close; const exit = @import("command/exit.zig").exit; const focus = @import("command/focus.zig").focus; @@ -42,12 +40,21 @@ const command = struct { const zoom = @import("command/zoom.zig").zoom; }; -const Direction = enum { +pub const Direction = enum { Next, Prev, + + pub fn parse(str: []const u8) error{InvalidDirection}!Direction { + return if (std.mem.eql(u8, str, "next")) + Direction.Next + else if (std.mem.eql(u8, str, "previous")) + Direction.Prev + else + error.InvalidDirection; + } }; -pub const Arg = union(enum) { +const Arg = union(enum) { int: i32, uint: u32, float: f64, @@ -86,34 +93,32 @@ pub const Arg = union(enum) { } }; -const ImplFn = fn (seat: *Seat, arg: Arg) void; - const Definition = struct { name: []const u8, - arg_type: @TagType(Arg), - impl: ImplFn, + impl: fn (*std.mem.Allocator, *Seat, []const []const u8, *[]const u8) Error!void, }; +// TODO: this could be replaced with a comptime hashmap // zig fmt: off -const str_to_read_fn = [_]Definition{ - .{ .name = "close", .arg_type = .none, .impl = command.close }, - .{ .name = "exit", .arg_type = .none, .impl = command.exit }, - .{ .name = "focus", .arg_type = .direction, .impl = command.focus }, - .{ .name = "focus_all_tags", .arg_type = .none, .impl = command.focusAllTags }, - .{ .name = "focus_output", .arg_type = .direction, .impl = command.focusOutput }, - .{ .name = "focus_tag", .arg_type = .uint, .impl = command.focusTag }, - .{ .name = "layout", .arg_type = .str, .impl = command.layout}, - .{ .name = "mod_master_count", .arg_type = .int, .impl = command.modMasterCount }, - .{ .name = "mod_master_factor", .arg_type = .float, .impl = command.modMasterFactor }, - .{ .name = "mode", .arg_type = .str, .impl = command.mode }, - .{ .name = "send_to_output", .arg_type = .direction, .impl = command.sendToOutput }, - .{ .name = "spawn", .arg_type = .str, .impl = command.spawn }, - .{ .name = "tag_view", .arg_type = .uint, .impl = command.tagView }, - .{ .name = "tag_view_all_tags", .arg_type = .none, .impl = command.tagViewAllTags }, - .{ .name = "toggle_float", .arg_type = .none, .impl = command.toggleFloat }, - .{ .name = "toggle_tag_focus", .arg_type = .uint, .impl = command.toggleTagFocus }, - .{ .name = "toggle_view_tag", .arg_type = .uint, .impl = command.toggleViewTag }, - .{ .name = "zoom", .arg_type = .none, .impl = command.zoom }, +const str_to_impl_fn = [_]Definition{ + .{ .name = "close", .impl = impl.close }, + .{ .name = "exit", .impl = impl.exit }, + .{ .name = "focus", .impl = impl.focus }, + .{ .name = "focus_all_tags", .impl = impl.focusAllTags }, + .{ .name = "focus_output", .impl = impl.focusOutput }, + .{ .name = "focus_tag", .impl = impl.focusTag }, + .{ .name = "layout", .impl = impl.layout}, + .{ .name = "mod_master_count", .impl = impl.modMasterCount }, + .{ .name = "mod_master_factor", .impl = impl.modMasterFactor }, + .{ .name = "mode", .impl = impl.mode }, + .{ .name = "send_to_output", .impl = impl.sendToOutput }, + .{ .name = "spawn", .impl = impl.spawn }, + .{ .name = "tag_view", .impl = impl.tagView }, + .{ .name = "tag_view_all_tags", .impl = impl.tagViewAllTags }, + .{ .name = "toggle_float", .impl = impl.toggleFloat }, + .{ .name = "toggle_tag_focus", .impl = impl.toggleTagFocus }, + .{ .name = "toggle_view_tag", .impl = impl.toggleViewTag }, + .{ .name = "zoom", .impl = impl.zoom }, }; // zig fmt: on @@ -126,29 +131,27 @@ pub const Error = error{ InvalidCharacter, InvalidDirection, OutOfMemory, + CommandFailed, }; -impl: ImplFn, -arg: Arg, +/// Run a command for the given Seat. The `args` parameter is similar to the +/// classic argv in that the command to be run is passed as the first argument. +/// If the command fails with Error.CommandFailed, a failure message will be +/// allocated and the slice pointed to by the `failure_message` parameter will +/// be set to point to it. The caller is responsible for freeing this message +/// in the case of failure. +pub fn run( + allocator: *std.mem.Allocator, + seat: *Seat, + args: []const []const u8, + failure_message: *[]const u8, +) Error!void { + if (args.len == 0) return Error.NoCommand; -pub fn init(args: []const []const u8, allocator: *std.mem.Allocator) Error!Self { - if (args.len == 0) return error.NoCommand; const name = args[0]; + const impl_fn = for (str_to_impl_fn) |definition| { + if (std.mem.eql(u8, name, definition.name)) break definition.impl; + } else return Error.UnknownCommand; - const definition = for (str_to_read_fn) |definition| { - if (std.mem.eql(u8, name, definition.name)) break definition; - } else return error.UnknownCommand; - - return Self{ - .impl = definition.impl, - .arg = try Arg.parse(definition.arg_type, args[1..], allocator), - }; -} - -pub fn deinit(self: Self, allocator: *std.mem.Allocator) void { - if (self.arg == .str) allocator.free(self.arg.str); -} - -pub fn run(self: Self, seat: *Seat) void { - self.impl(seat, self.arg); + try impl_fn(allocator, seat, args, failure_message); } diff --git a/src/command/close.zig b/src/command/close.zig index c5be01c..04b2dcd 100644 --- a/src/command/close.zig +++ b/src/command/close.zig @@ -15,13 +15,20 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . +const std = @import("std"); + const c = @import("../c.zig"); -const Arg = @import("../Command.zig").Arg; +const Error = @import("../command.zig").Error; const Seat = @import("../Seat.zig"); /// Close the focused view, if any. -pub fn close(seat: *Seat, arg: Arg) void { +pub fn close( + allocator: *std.mem.Allocator, + seat: *Seat, + args: []const []const u8, + failure_message: *[]const u8, +) Error!void { if (seat.focused_view) |view| { // Note: we don't call arrange() here as it will be called // automatically when the view is unmapped. diff --git a/src/command/exit.zig b/src/command/exit.zig index 799b12c..a21e4f3 100644 --- a/src/command/exit.zig +++ b/src/command/exit.zig @@ -15,12 +15,20 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . +const std = @import("std"); + const c = @import("../c.zig"); -const Arg = @import("../Command.zig").Arg; +const Error = @import("../command.zig").Error; const Seat = @import("../Seat.zig"); /// Exit the compositor, terminating the wayland session. -pub fn exit(seat: *Seat, arg: Arg) void { +pub fn exit( + allocator: *std.mem.Allocator, + seat: *Seat, + args: []const []const u8, + failure_message: *[]const u8, +) Error!void { + if (args.len > 1) return Error.TooManyArguments; c.wl_display_terminate(seat.input_manager.server.wl_display); } diff --git a/src/command/focus.zig b/src/command/focus.zig index 930a36c..0e2c9e0 100644 --- a/src/command/focus.zig +++ b/src/command/focus.zig @@ -15,18 +15,30 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . +const std = @import("std"); + const c = @import("../c.zig"); -const Arg = @import("../Command.zig").Arg; +const Error = @import("../command.zig").Error; +const Direction = @import("../command.zig").Direction; const Seat = @import("../Seat.zig"); const View = @import("../View.zig"); const ViewStack = @import("../view_stack.zig").ViewStack; /// Focus either the next or the previous visible view, depending on the enum /// passed. Does nothing if there are 1 or 0 views in the stack. -pub fn focus(seat: *Seat, arg: Arg) void { - const direction = arg.direction; +pub fn focus( + allocator: *std.mem.Allocator, + seat: *Seat, + args: []const []const u8, + failure_message: *[]const u8, +) Error!void { + if (args.len < 2) return Error.NotEnoughArguments; + if (args.len > 2) return Error.TooManyArguments; + + const direction = try Direction.parse(args[1]); const output = seat.focused_output; + if (seat.focused_view) |current_focus| { // If there is a currently focused view, focus the next visible view in the stack. const focused_node = @fieldParentPtr(ViewStack(View).Node, "view", current_focus); @@ -50,5 +62,6 @@ pub fn focus(seat: *Seat, arg: Arg) void { .Next => ViewStack(View).iterator(output.views.first, output.current_focused_tags), .Prev => ViewStack(View).reverseIterator(output.views.last, output.current_focused_tags), }; + seat.focus(if (it.next()) |node| &node.view else null); } diff --git a/src/command/focus_all_tags.zig b/src/command/focus_all_tags.zig index d2eef88..28e001d 100644 --- a/src/command/focus_all_tags.zig +++ b/src/command/focus_all_tags.zig @@ -15,11 +15,18 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -const Arg = @import("../Command.zig").Arg; +const std = @import("std"); + +const Error = @import("../command.zig").Error; const Seat = @import("../Seat.zig"); /// Set focus to all tags -pub fn focusAllTags(seat: *Seat, arg: Arg) void { +pub fn focusAllTags( + allocator: *std.mem.Allocator, + seat: *Seat, + args: []const []const u8, + failure_message: *[]const u8, +) Error!void { seat.focused_output.pending_focused_tags = 0xFFFFFFFF; seat.input_manager.server.root.arrange(); } diff --git a/src/command/focus_output.zig b/src/command/focus_output.zig index 02b7999..b11664f 100644 --- a/src/command/focus_output.zig +++ b/src/command/focus_output.zig @@ -19,14 +19,23 @@ const std = @import("std"); const c = @import("../c.zig"); -const Arg = @import("../Command.zig").Arg; +const Error = @import("../command.zig").Error; +const Direction = @import("../command.zig").Direction; const Output = @import("../Output.zig"); const Seat = @import("../Seat.zig"); /// Focus either the next or the previous output, depending on the bool passed. /// Does nothing if there is only one output. -pub fn focusOutput(seat: *Seat, arg: Arg) void { - const direction = arg.direction; +pub fn focusOutput( + allocator: *std.mem.Allocator, + seat: *Seat, + args: []const []const u8, + failure_message: *[]const u8, +) Error!void { + if (args.len < 2) return Error.NotEnoughArguments; + if (args.len > 2) return Error.TooManyArguments; + + const direction = try Direction.parse(args[1]); const root = &seat.input_manager.server.root; // If the noop output is focused, there are no other outputs to switch to if (seat.focused_output == &root.noop_output) { diff --git a/src/command/focus_tag.zig b/src/command/focus_tag.zig index edb3292..8f69e6e 100644 --- a/src/command/focus_tag.zig +++ b/src/command/focus_tag.zig @@ -15,12 +15,23 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -const Arg = @import("../Command.zig").Arg; +const std = @import("std"); + +const Error = @import("../command.zig").Error; const Seat = @import("../Seat.zig"); /// Switch focus to the passed tag. -pub fn focusTag(seat: *Seat, arg: Arg) void { - const tags = @as(u32, 1) << @intCast(u5, arg.uint - 1); +pub fn focusTag( + allocator: *std.mem.Allocator, + seat: *Seat, + args: []const []const u8, + failure_message: *[]const u8, +) Error!void { + if (args.len < 2) return Error.NotEnoughArguments; + if (args.len > 2) return Error.TooManyArguments; + + const tag = try std.fmt.parseInt(u32, args[1], 10); + const tags = @as(u32, 1) << @intCast(u5, tag - 1); seat.focused_output.pending_focused_tags = tags; seat.input_manager.server.root.arrange(); } diff --git a/src/command/layout.zig b/src/command/layout.zig index f8e21ad..4a21050 100644 --- a/src/command/layout.zig +++ b/src/command/layout.zig @@ -15,14 +15,23 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . +const std = @import("std"); + const c = @import("../c.zig"); -const Arg = @import("../Command.zig").Arg; +const Error = @import("../command.zig").Error; const Seat = @import("../Seat.zig"); -pub fn layout(seat: *Seat, arg: Arg) void { - const layout_name = arg.str; - seat.focused_output.layout = seat.focused_output.getLayoutByName(layout_name); +pub fn layout( + allocator: *std.mem.Allocator, + seat: *Seat, + args: []const []const u8, + failure_message: *[]const u8, +) Error!void { + if (args.len < 2) return Error.NotEnoughArguments; + if (args.len > 2) return Error.TooManyArguments; + + seat.focused_output.layout = seat.focused_output.getLayoutByName(args[1]); seat.focused_output.arrangeViews(); seat.input_manager.server.root.startTransaction(); } diff --git a/src/command/mod_master_count.zig b/src/command/mod_master_count.zig index bd2fde0..38a379e 100644 --- a/src/command/mod_master_count.zig +++ b/src/command/mod_master_count.zig @@ -19,12 +19,20 @@ const std = @import("std"); const c = @import("../c.zig"); -const Arg = @import("../Command.zig").Arg; +const Error = @import("../command.zig").Error; const Seat = @import("../Seat.zig"); /// Modify the number of master views -pub fn modMasterCount(seat: *Seat, arg: Arg) void { - const delta = arg.int; +pub fn modMasterCount( + allocator: *std.mem.Allocator, + seat: *Seat, + args: []const []const u8, + failure_message: *[]const u8, +) Error!void { + if (args.len < 2) return Error.NotEnoughArguments; + if (args.len > 2) return Error.TooManyArguments; + + const delta = try std.fmt.parseInt(i32, args[1], 10); const output = seat.focused_output; output.master_count = @intCast( u32, diff --git a/src/command/mod_master_factor.zig b/src/command/mod_master_factor.zig index 2e2a73c..ec8065a 100644 --- a/src/command/mod_master_factor.zig +++ b/src/command/mod_master_factor.zig @@ -19,12 +19,20 @@ const std = @import("std"); const c = @import("../c.zig"); -const Arg = @import("../Command.zig").Arg; +const Error = @import("../command.zig").Error; const Seat = @import("../Seat.zig"); /// Modify the percent of the width of the screen that the master views occupy. -pub fn modMasterFactor(seat: *Seat, arg: Arg) void { - const delta = arg.float; +pub fn modMasterFactor( + allocator: *std.mem.Allocator, + seat: *Seat, + args: []const []const u8, + failure_message: *[]const u8, +) Error!void { + if (args.len < 2) return Error.NotEnoughArguments; + if (args.len > 2) return Error.TooManyArguments; + + const delta = try std.fmt.parseFloat(f64, args[1]); const output = seat.focused_output; const new_master_factor = std.math.min( std.math.max(output.master_factor + delta, 0.05), diff --git a/src/command/mode.zig b/src/command/mode.zig index 706533c..fb03bb5 100644 --- a/src/command/mode.zig +++ b/src/command/mode.zig @@ -15,12 +15,21 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -const Arg = @import("../Command.zig").Arg; +const std = @import("std"); + +const Error = @import("../command.zig").Error; const Seat = @import("../Seat.zig"); /// Switch to the given mode -pub fn mode(seat: *Seat, arg: Arg) void { - const mode_name = arg.str; +pub fn mode( + allocator: *std.mem.Allocator, + seat: *Seat, + args: []const []const u8, + failure_message: *[]const u8, +) Error!void { + if (args.len < 2) return Error.NotEnoughArguments; + if (args.len > 2) return Error.TooManyArguments; + const config = seat.input_manager.server.config; - seat.mode = config.getMode(mode_name); + seat.mode = config.getMode(args[1]); } diff --git a/src/command/send_to_output.zig b/src/command/send_to_output.zig index 7e0bbbf..de92f6f 100644 --- a/src/command/send_to_output.zig +++ b/src/command/send_to_output.zig @@ -19,16 +19,23 @@ const std = @import("std"); const c = @import("../c.zig"); -const Arg = @import("../Command.zig").Arg; +const Error = @import("../command.zig").Error; +const Direction = @import("../command.zig").Direction; const Output = @import("../Output.zig"); const Seat = @import("../Seat.zig"); /// Send the focused view to the the next or the previous output, depending on /// the bool passed. Does nothing if there is only one output. -pub fn sendToOutput(seat: *Seat, arg: Arg) void { - @import("../log.zig").Log.Debug.log("send to output", .{}); +pub fn sendToOutput( + allocator: *std.mem.Allocator, + seat: *Seat, + args: []const []const u8, + failure_message: *[]const u8, +) Error!void { + if (args.len < 2) return Error.NotEnoughArguments; + if (args.len > 2) return Error.TooManyArguments; - const direction = arg.direction; + const direction = try Direction.parse(args[1]); const root = &seat.input_manager.server.root; if (seat.focused_view) |view| { diff --git a/src/command/spawn.zig b/src/command/spawn.zig index 61aedab..880483a 100644 --- a/src/command/spawn.zig +++ b/src/command/spawn.zig @@ -17,23 +17,31 @@ const std = @import("std"); -const c = @import("../c.zig"); - -const Arg = @import("../Command.zig").Arg; -const Log = @import("../log.zig").Log; +const Error = @import("../command.zig").Error; const Seat = @import("../Seat.zig"); /// Spawn a program. -pub fn spawn(seat: *Seat, arg: Arg) void { - const cmd = arg.str; +pub fn spawn( + allocator: *std.mem.Allocator, + seat: *Seat, + args: []const []const u8, + failure_message: *[]const u8, +) Error!void { + if (args.len < 2) return Error.NotEnoughArguments; + + const cmd = try std.mem.join(allocator, " ", args[1..]); + defer allocator.free(cmd); + + const child_args = [_][]const u8{ "/bin/sh", "-c", cmd }; + const child = try std.ChildProcess.init(&child_args, allocator); + defer child.deinit(); - const argv = [_][]const u8{ "/bin/sh", "-c", cmd }; - const child = std.ChildProcess.init(&argv, std.heap.c_allocator) catch |err| { - Log.Error.log("Failed to execute {}: {}", .{ cmd, err }); - return; - }; std.ChildProcess.spawn(child) catch |err| { - Log.Error.log("Failed to execute {}: {}", .{ cmd, err }); - return; + failure_message.* = try std.fmt.allocPrint( + allocator, + "failed to spawn {}: {}.", + .{ cmd, err }, + ); + return Error.CommandFailed; }; } diff --git a/src/command/tag_view.zig b/src/command/tag_view.zig index fd74285..735481d 100644 --- a/src/command/tag_view.zig +++ b/src/command/tag_view.zig @@ -15,14 +15,25 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . +const std = @import("std"); + const c = @import("../c.zig"); -const Arg = @import("../Command.zig").Arg; +const Error = @import("../command.zig").Error; const Seat = @import("../Seat.zig"); /// Set the tag of the focused view. -pub fn tagView(seat: *Seat, arg: Arg) void { - const tags = @as(u32, 1) << @intCast(u5, arg.uint - 1); +pub fn tagView( + allocator: *std.mem.Allocator, + seat: *Seat, + args: []const []const u8, + failure_message: *[]const u8, +) Error!void { + if (args.len < 2) return Error.NotEnoughArguments; + if (args.len > 2) return Error.TooManyArguments; + + const tag = try std.fmt.parseInt(u32, args[1], 10); + const tags = @as(u32, 1) << @intCast(u5, tag - 1); if (seat.focused_view) |view| { if (view.current_tags != tags) { view.pending_tags = tags; diff --git a/src/command/tag_view_all_tags.zig b/src/command/tag_view_all_tags.zig index 1b93233..2d187f3 100644 --- a/src/command/tag_view_all_tags.zig +++ b/src/command/tag_view_all_tags.zig @@ -15,13 +15,20 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . +const std = @import("std"); + const c = @import("../c.zig"); -const Arg = @import("../Command.zig").Arg; +const Error = @import("../command.zig").Error; const Seat = @import("../Seat.zig"); /// Tag the focused view with all tags. -pub fn tagViewAllTags(seat: *Seat, arg: Arg) void { +pub fn tagViewAllTags( + allocator: *std.mem.Allocator, + seat: *Seat, + args: []const []const u8, + failure_message: *[]const u8, +) Error!void { if (seat.focused_view) |view| { if (view.current_tags != 0xFFFFFFFF) { view.pending_tags = 0xFFFFFFFF; diff --git a/src/command/toggle_float.zig b/src/command/toggle_float.zig index e890d73..25655e8 100644 --- a/src/command/toggle_float.zig +++ b/src/command/toggle_float.zig @@ -15,14 +15,22 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . +const std = @import("std"); + const c = @import("../c.zig"); -const Arg = @import("../Command.zig").Arg; +const Error = @import("../command.zig").Error; const Seat = @import("../Seat.zig"); /// Make the focused view float or stop floating, depending on its current /// state. -pub fn toggleFloat(seat: *Seat, arg: Arg) void { +pub fn toggleFloat( + allocator: *std.mem.Allocator, + seat: *Seat, + args: []const []const u8, + failure_message: *[]const u8, +) Error!void { + if (args.len > 1) return Error.TooManyArguments; if (seat.focused_view) |view| { view.setFloating(!view.floating); view.output.root.arrange(); diff --git a/src/command/toggle_tag_focus.zig b/src/command/toggle_tag_focus.zig index 8a1a707..09761c7 100644 --- a/src/command/toggle_tag_focus.zig +++ b/src/command/toggle_tag_focus.zig @@ -15,14 +15,25 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . +const std = @import("std"); + const c = @import("../c.zig"); -const Arg = @import("../Command.zig").Arg; +const Error = @import("../command.zig").Error; const Seat = @import("../Seat.zig"); /// Toggle focus of the passsed tags. -pub fn toggleTagFocus(seat: *Seat, arg: Arg) void { - const tags = @as(u32, 1) << @intCast(u5, arg.uint - 1); +pub fn toggleTagFocus( + allocator: *std.mem.Allocator, + seat: *Seat, + args: []const []const u8, + failure_message: *[]const u8, +) Error!void { + if (args.len < 2) return Error.NotEnoughArguments; + if (args.len > 2) return Error.TooManyArguments; + + const tag = try std.fmt.parseInt(u32, args[1], 10); + const tags = @as(u32, 1) << @intCast(u5, tag - 1); const output = seat.focused_output; const new_focused_tags = output.current_focused_tags ^ tags; if (new_focused_tags != 0) { diff --git a/src/command/toggle_view_tag.zig b/src/command/toggle_view_tag.zig index fa869b5..469a750 100644 --- a/src/command/toggle_view_tag.zig +++ b/src/command/toggle_view_tag.zig @@ -15,14 +15,25 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . +const std = @import("std"); + const c = @import("../c.zig"); -const Arg = @import("../Command.zig").Arg; +const Error = @import("../command.zig").Error; const Seat = @import("../Seat.zig"); /// Toggle the passed tag of the focused view -pub fn toggleViewTag(seat: *Seat, arg: Arg) void { - const tags = @as(u32, 1) << @intCast(u5, arg.uint - 1); +pub fn toggleViewTag( + allocator: *std.mem.Allocator, + seat: *Seat, + args: []const []const u8, + failure_message: *[]const u8, +) Error!void { + if (args.len < 2) return Error.NotEnoughArguments; + if (args.len > 2) return Error.TooManyArguments; + + const tag = try std.fmt.parseInt(u32, args[1], 10); + const tags = @as(u32, 1) << @intCast(u5, tag - 1); if (seat.focused_view) |view| { const new_tags = view.current_tags ^ tags; if (new_tags != 0) { diff --git a/src/command/zoom.zig b/src/command/zoom.zig index b603a69..59156ab 100644 --- a/src/command/zoom.zig +++ b/src/command/zoom.zig @@ -15,16 +15,25 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . +const std = @import("std"); + const c = @import("../c.zig"); -const Arg = @import("../Command.zig").Arg; +const Error = @import("../command.zig").Error; const Seat = @import("../Seat.zig"); const View = @import("../View.zig"); const ViewStack = @import("../view_stack.zig").ViewStack; /// Bump the focused view to the top of the stack. If the view on the top of /// the stack is focused, bump the second view to the top. -pub fn zoom(seat: *Seat, arg: Arg) void { +pub fn zoom( + allocator: *std.mem.Allocator, + seat: *Seat, + args: []const []const u8, + failure_message: *[]const u8, +) Error!void { + if (args.len > 1) return Error.TooManyArguments; + if (seat.focused_view) |current_focus| { const output = seat.focused_output; const focused_node = @fieldParentPtr(ViewStack(View).Node, "view", current_focus);