Rework commands to be string based

This allows for significantly more flexibility and should make
implementing the bind command possible.
This commit is contained in:
Isaac Freund 2020-05-26 22:55:07 +02:00
parent 9cd61f7590
commit d9ca9db5a4
No known key found for this signature in database
GPG Key ID: 86DED400DDFD7A11
24 changed files with 501 additions and 270 deletions

View File

@ -21,10 +21,10 @@ const std = @import("std");
const c = @import("c.zig"); const c = @import("c.zig");
const Command = @import("Command.zig");
const Log = @import("log.zig").Log; const Log = @import("log.zig").Log;
const Mode = @import("Mode.zig"); const Mode = @import("Mode.zig");
const Server = @import("Server.zig"); const Server = @import("Server.zig");
const Keybind = @import("Keybind.zig");
/// Width of borders in pixels /// Width of borders in pixels
border_width: u32, border_width: u32,
@ -55,190 +55,218 @@ pub fn init(self: *Self, allocator: *std.mem.Allocator) !void {
const mod = c.WLR_MODIFIER_LOGO; const mod = c.WLR_MODIFIER_LOGO;
// Mod+Shift+Return to start an instance of alacritty // Mod+Shift+Return to start an instance of alacritty
try normal.keybinds.append(.{ try normal.keybinds.append(try Keybind.init(
.keysym = c.XKB_KEY_Return, allocator,
.modifiers = mod | c.WLR_MODIFIER_SHIFT, c.XKB_KEY_Return,
.command = try Command.init(&[_][]const u8{ "spawn", "alacritty" }, allocator), mod | c.WLR_MODIFIER_SHIFT,
}); &[_][]const u8{ "spawn", "alacritty" },
));
// Mod+Q to close the focused view // Mod+Q to close the focused view
try normal.keybinds.append(.{ try normal.keybinds.append(try Keybind.init(
.keysym = c.XKB_KEY_q, allocator,
.modifiers = mod, c.XKB_KEY_q,
.command = try Command.init(&[_][]const u8{"close"}, allocator), mod,
}); &[_][]const u8{"close"},
));
// Mod+E to exit river // Mod+E to exit river
try normal.keybinds.append(.{ try normal.keybinds.append(try Keybind.init(
.keysym = c.XKB_KEY_e, allocator,
.modifiers = mod, c.XKB_KEY_e,
.command = try Command.init(&[_][]const u8{"exit"}, allocator), mod,
}); &[_][]const u8{"exit"},
));
// Mod+J and Mod+K to focus the next/previous view in the layout stack // Mod+J and Mod+K to focus the next/previous view in the layout stack
try normal.keybinds.append(.{ try normal.keybinds.append(try Keybind.init(
.keysym = c.XKB_KEY_j, allocator,
.modifiers = mod, c.XKB_KEY_j,
.command = try Command.init(&[_][]const u8{ "focus", "next" }, allocator), mod,
}); &[_][]const u8{ "focus", "next" },
try normal.keybinds.append(.{ ));
.keysym = c.XKB_KEY_k, try normal.keybinds.append(try Keybind.init(
.modifiers = mod, allocator,
.command = try Command.init(&[_][]const u8{ "focus", "previous" }, allocator), c.XKB_KEY_k,
}); mod,
&[_][]const u8{ "focus", "previous" },
));
// Mod+Return to bump the focused view to the top of the layout stack, // Mod+Return to bump the focused view to the top of the layout stack,
// making it the new master // making it the new master
try normal.keybinds.append(.{ try normal.keybinds.append(try Keybind.init(
.keysym = c.XKB_KEY_Return, allocator,
.modifiers = mod, c.XKB_KEY_Return,
.command = try Command.init(&[_][]const u8{"zoom"}, allocator), mod,
}); &[_][]const u8{"zoom"},
));
// Mod+H and Mod+L to increase/decrease the width of the master column // Mod+H and Mod+L to increase/decrease the width of the master column
try normal.keybinds.append(.{ try normal.keybinds.append(try Keybind.init(
.keysym = c.XKB_KEY_h, allocator,
.modifiers = mod, c.XKB_KEY_h,
.command = try Command.init(&[_][]const u8{ "mod_master_factor", "+0.05" }, allocator), mod,
}); &[_][]const u8{ "mod_master_factor", "+0.05" },
try normal.keybinds.append(.{ ));
.keysym = c.XKB_KEY_l, try normal.keybinds.append(try Keybind.init(
.modifiers = mod, allocator,
.command = try Command.init(&[_][]const u8{ "mod_master_factor", "-0.05" }, 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 // Mod+Shift+H and Mod+Shift+L to increment/decrement the number of
// master views in the layout // master views in the layout
try normal.keybinds.append(.{ try normal.keybinds.append(try Keybind.init(
.keysym = c.XKB_KEY_h, allocator,
.modifiers = mod | c.WLR_MODIFIER_SHIFT, c.XKB_KEY_h,
.command = try Command.init(&[_][]const u8{ "mod_master_count", "+1" }, allocator), mod | c.WLR_MODIFIER_SHIFT,
}); &[_][]const u8{ "mod_master_count", "+1" },
try normal.keybinds.append(.{ ));
.keysym = c.XKB_KEY_l, try normal.keybinds.append(try Keybind.init(
.modifiers = mod | c.WLR_MODIFIER_SHIFT, allocator,
.command = try Command.init(&[_][]const u8{ "mod_master_count", "+1" }, allocator), c.XKB_KEY_l,
}); mod | c.WLR_MODIFIER_SHIFT,
&[_][]const u8{ "mod_master_count", "+1" },
));
comptime var i = 0; comptime var i = 0;
inline while (i < 9) : (i += 1) { inline while (i < 9) : (i += 1) {
const str = &[_]u8{i + '0' + 1}; const str = &[_]u8{i + '0' + 1};
// Mod+[1-9] to focus tag [1-9] // Mod+[1-9] to focus tag [1-9]
try normal.keybinds.append(.{ try normal.keybinds.append(try Keybind.init(
.keysym = c.XKB_KEY_1 + i, allocator,
.modifiers = mod, c.XKB_KEY_1 + i,
.command = try Command.init(&[_][]const u8{ "focus_tag", str }, allocator), mod,
}); &[_][]const u8{ "focus_tag", str },
));
// Mod+Shift+[1-9] to tag focused view with tag [1-9] // Mod+Shift+[1-9] to tag focused view with tag [1-9]
try normal.keybinds.append(.{ try normal.keybinds.append(try Keybind.init(
.keysym = c.XKB_KEY_1 + i, allocator,
.modifiers = mod | c.WLR_MODIFIER_SHIFT, c.XKB_KEY_1 + i,
.command = try Command.init(&[_][]const u8{ "tag_view", str }, allocator), mod | c.WLR_MODIFIER_SHIFT,
}); &[_][]const u8{ "tag_view", str },
));
// Mod+Ctrl+[1-9] to toggle focus of tag [1-9] // Mod+Ctrl+[1-9] to toggle focus of tag [1-9]
try normal.keybinds.append(.{ try normal.keybinds.append(try Keybind.init(
.keysym = c.XKB_KEY_1 + i, allocator,
.modifiers = mod | c.WLR_MODIFIER_CTRL, c.XKB_KEY_1 + i,
.command = try Command.init(&[_][]const u8{ "toggle_tag_focus", str }, allocator), mod | c.WLR_MODIFIER_CTRL,
}); &[_][]const u8{ "toggle_tag_focus", str },
));
// Mod+Shift+Ctrl+[1-9] to toggle tag [1-9] of focused view // Mod+Shift+Ctrl+[1-9] to toggle tag [1-9] of focused view
try normal.keybinds.append(.{ try normal.keybinds.append(try Keybind.init(
.keysym = c.XKB_KEY_1 + i, allocator,
.modifiers = mod | c.WLR_MODIFIER_CTRL | c.WLR_MODIFIER_SHIFT, c.XKB_KEY_1 + i,
.command = try Command.init(&[_][]const u8{ "toggle_view_tag", str }, allocator), mod | c.WLR_MODIFIER_CTRL | c.WLR_MODIFIER_SHIFT,
}); &[_][]const u8{ "toggle_view_tag", str },
));
} }
// Mod+0 to focus all tags // Mod+0 to focus all tags
try normal.keybinds.append(.{ try normal.keybinds.append(try Keybind.init(
.keysym = c.XKB_KEY_0, allocator,
.modifiers = mod, c.XKB_KEY_0,
.command = try Command.init(&[_][]const u8{"focus_all_tags"}, allocator), mod,
}); &[_][]const u8{"focus_all_tags"},
));
// Mod+Shift+0 to tag focused view with all tags // Mod+Shift+0 to tag focused view with all tags
try normal.keybinds.append(.{ try normal.keybinds.append(try Keybind.init(
.keysym = c.XKB_KEY_0, allocator,
.modifiers = mod | c.WLR_MODIFIER_SHIFT, c.XKB_KEY_0,
.command = try Command.init(&[_][]const u8{"tag_view_all_tags"}, allocator), mod | c.WLR_MODIFIER_SHIFT,
}); &[_][]const u8{"tag_view_all_tags"},
));
// Mod+Period and Mod+Comma to focus the next/previous output // Mod+Period and Mod+Comma to focus the next/previous output
try normal.keybinds.append(.{ try normal.keybinds.append(try Keybind.init(
.keysym = c.XKB_KEY_period, allocator,
.modifiers = mod, c.XKB_KEY_period,
.command = try Command.init(&[_][]const u8{ "focus_output", "next" }, allocator), mod,
}); &[_][]const u8{ "focus_output", "next" },
try normal.keybinds.append(.{ ));
.keysym = c.XKB_KEY_comma, try normal.keybinds.append(try Keybind.init(
.modifiers = mod, allocator,
.command = try Command.init(&[_][]const u8{ "focus_output", "previous" }, allocator), c.XKB_KEY_comma,
}); mod,
&[_][]const u8{ "focus_output", "previous" },
));
// Mod+Shift+Period/Comma to send the focused view to the the // Mod+Shift+Period/Comma to send the focused view to the the
// next/previous output // next/previous output
try normal.keybinds.append(.{ try normal.keybinds.append(try Keybind.init(
.keysym = c.XKB_KEY_period, allocator,
.modifiers = mod | c.WLR_MODIFIER_SHIFT, c.XKB_KEY_period,
.command = try Command.init(&[_][]const u8{ "send_to_output", "next" }, allocator), mod | c.WLR_MODIFIER_SHIFT,
}); &[_][]const u8{ "send_to_output", "next" },
try normal.keybinds.append(.{ ));
.keysym = c.XKB_KEY_comma, try normal.keybinds.append(try Keybind.init(
.modifiers = mod | c.WLR_MODIFIER_SHIFT, allocator,
.command = try Command.init(&[_][]const u8{ "send_to_output", "previous" }, allocator), c.XKB_KEY_comma,
}); mod | c.WLR_MODIFIER_SHIFT,
&[_][]const u8{ "send_to_output", "previous" },
));
// Mod+Space to toggle float // Mod+Space to toggle float
try normal.keybinds.append(.{ try normal.keybinds.append(try Keybind.init(
.keysym = c.XKB_KEY_space, allocator,
.modifiers = mod, c.XKB_KEY_space,
.command = try Command.init(&[_][]const u8{"toggle_float"}, allocator), mod,
}); &[_][]const u8{"toggle_float"},
));
// Mod+F11 to enter passthrough mode // Mod+F11 to enter passthrough mode
try normal.keybinds.append(.{ try normal.keybinds.append(try Keybind.init(
.keysym = c.XKB_KEY_F11, allocator,
.modifiers = mod, c.XKB_KEY_F11,
.command = try Command.init(&[_][]const u8{ "mode", "passthrough" }, allocator), mod,
}); &[_][]const u8{ "mode", "passthrough" },
));
try self.modes.append(try Mode.init("passthrough", allocator)); try self.modes.append(try Mode.init("passthrough", allocator));
// Mod+F11 to return to normal mode // Mod+F11 to return to normal mode
try self.modes.items[1].keybinds.append(.{ try self.modes.items[1].keybinds.append(try Keybind.init(
.keysym = c.XKB_KEY_F11, allocator,
.modifiers = mod, c.XKB_KEY_F11,
.command = try Command.init(&[_][]const u8{ "mode", "normal" }, allocator), mod,
}); &[_][]const u8{ "mode", "normal" },
));
// Change master orientation with Mod+{Up,Right,Down,Left} // Change master orientation with Mod+{Up,Right,Down,Left}
try normal.keybinds.append(.{ try normal.keybinds.append(try Keybind.init(
.keysym = c.XKB_KEY_Up, allocator,
.modifiers = mod, c.XKB_KEY_Up,
.command = try Command.init(&[_][]const u8{ "layout", "TopMaster" }, allocator), mod,
}); &[_][]const u8{ "layout", "TopMaster" },
try normal.keybinds.append(.{ ));
.keysym = c.XKB_KEY_Right, try normal.keybinds.append(try Keybind.init(
.modifiers = mod, allocator,
.command = try Command.init(&[_][]const u8{ "layout", "RightMaster" }, allocator), c.XKB_KEY_Right,
}); mod,
try normal.keybinds.append(.{ &[_][]const u8{ "layout", "RightMaster" },
.keysym = c.XKB_KEY_Down, ));
.modifiers = mod, try normal.keybinds.append(try Keybind.init(
.command = try Command.init(&[_][]const u8{ "layout", "BottomMaster" }, allocator), allocator,
}); c.XKB_KEY_Down,
try normal.keybinds.append(.{ mod,
.keysym = c.XKB_KEY_Left, &[_][]const u8{ "layout", "BottomMaster" },
.modifiers = mod, ));
.command = try Command.init(&[_][]const u8{ "layout", "LeftMaster" }, allocator), try normal.keybinds.append(try Keybind.init(
}); allocator,
c.XKB_KEY_Left,
mod,
&[_][]const u8{ "layout", "LeftMaster" },
));
// Mod+f to change to Full layout // Mod+f to change to Full layout
try normal.keybinds.append(.{ try normal.keybinds.append(try Keybind.init(
.keysym = c.XKB_KEY_f, allocator,
.modifiers = mod, c.XKB_KEY_f,
.command = try Command.init(&[_][]const u8{ "layout", "Full" }, allocator), mod,
}); &[_][]const u8{ "layout", "Full" },
));
// Float views with app_id "float" // Float views with app_id "float"
try self.float_filter.append("float"); try self.float_filter.append("float");

View File

@ -20,8 +20,8 @@ const Self = @This();
const std = @import("std"); const std = @import("std");
const c = @import("c.zig"); const c = @import("c.zig");
const command = @import("command.zig");
const Command = @import("Command.zig");
const Log = @import("log.zig").Log; const Log = @import("log.zig").Log;
const Server = @import("Server.zig"); const Server = @import("Server.zig");
@ -82,6 +82,7 @@ fn runCommand(
) callconv(.C) void { ) callconv(.C) void {
const self = @ptrCast(*Self, @alignCast(@alignOf(*Self), c.wl_resource_get_user_data(wl_resource))); const self = @ptrCast(*Self, @alignCast(@alignOf(*Self), c.wl_resource_get_user_data(wl_resource)));
const allocator = self.server.allocator; const allocator = self.server.allocator;
const seat = self.server.input_manager.default_seat;
var args = std.ArrayList([]const u8).init(allocator); var args = std.ArrayList([]const u8).init(allocator);
@ -106,22 +107,30 @@ fn runCommand(
c.wl_resource_set_implementation(callback_resource, null, null, null); c.wl_resource_set_implementation(callback_resource, null, null, null);
const command = Command.init(args.items, allocator) catch |err| { 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( c.zriver_command_callback_v1_send_failure(
callback_resource, callback_resource,
switch (err) { switch (err) {
Command.Error.NoCommand => "no command given", command.Error.NoCommand => "no command given",
Command.Error.UnknownCommand => "unknown command", command.Error.UnknownCommand => "unknown command",
Command.Error.NotEnoughArguments => "not enough arguments", command.Error.NotEnoughArguments => "not enough arguments",
Command.Error.TooManyArguments => "too many arguments", command.Error.TooManyArguments => "too many arguments",
Command.Error.Overflow => "value out of bounds", command.Error.Overflow => "value out of bounds",
Command.Error.InvalidCharacter => "invalid character in argument", command.Error.InvalidCharacter => "invalid character in argument",
Command.Error.InvalidDirection => "invalid direction. Must be 'next' or 'previous'", command.Error.InvalidDirection => "invalid direction. Must be 'next' or 'previous'",
Command.Error.OutOfMemory => unreachable, command.Error.OutOfMemory => "out of memory",
command.Error.CommandFailed => unreachable,
}, },
); );
}
return; return;
}; };
c.zriver_command_callback_v1_send_success(callback_resource); c.zriver_command_callback_v1_send_success(callback_resource);
command.run(self.server.input_manager.default_seat);
} }

View File

@ -15,9 +15,32 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
const Self = @This();
const std = @import("std");
const c = @import("c.zig"); const c = @import("c.zig");
const Command = @import("Command.zig");
keysym: c.xkb_keysym_t, keysym: c.xkb_keysym_t,
modifiers: u32, 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);
}

View File

@ -38,6 +38,6 @@ pub fn init(name: []const u8, allocator: *std.mem.Allocator) !Self {
pub fn deinit(self: Self) void { pub fn deinit(self: Self) void {
const allocator = self.keybinds.allocator; const allocator = self.keybinds.allocator;
allocator.free(self.name); allocator.free(self.name);
for (self.keybinds.items) |keybind| keybind.command.deinit(allocator); for (self.keybinds.items) |keybind| keybind.deinit(allocator);
self.keybinds.deinit(); self.keybinds.deinit();
} }

View File

@ -20,6 +20,7 @@ const Self = @This();
const std = @import("std"); const std = @import("std");
const c = @import("c.zig"); const c = @import("c.zig");
const command = @import("command.zig");
const Cursor = @import("Cursor.zig"); const Cursor = @import("Cursor.zig");
const InputManager = @import("InputManager.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| { for (self.mode.keybinds.items) |keybind| {
if (modifiers == keybind.modifiers and keysym == keybind.keysym) { if (modifiers == keybind.modifiers and keysym == keybind.keysym) {
// Execute the bound command // 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; return true;
} }
} }

View File

@ -15,13 +15,11 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
const Self = @This();
const std = @import("std"); const std = @import("std");
const Seat = @import("Seat.zig"); const Seat = @import("Seat.zig");
const command = struct { const impl = struct {
const close = @import("command/close.zig").close; const close = @import("command/close.zig").close;
const exit = @import("command/exit.zig").exit; const exit = @import("command/exit.zig").exit;
const focus = @import("command/focus.zig").focus; const focus = @import("command/focus.zig").focus;
@ -42,12 +40,21 @@ const command = struct {
const zoom = @import("command/zoom.zig").zoom; const zoom = @import("command/zoom.zig").zoom;
}; };
const Direction = enum { pub const Direction = enum {
Next, Next,
Prev, 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, int: i32,
uint: u32, uint: u32,
float: f64, float: f64,
@ -86,34 +93,32 @@ pub const Arg = union(enum) {
} }
}; };
const ImplFn = fn (seat: *Seat, arg: Arg) void;
const Definition = struct { const Definition = struct {
name: []const u8, name: []const u8,
arg_type: @TagType(Arg), impl: fn (*std.mem.Allocator, *Seat, []const []const u8, *[]const u8) Error!void,
impl: ImplFn,
}; };
// TODO: this could be replaced with a comptime hashmap
// zig fmt: off // zig fmt: off
const str_to_read_fn = [_]Definition{ const str_to_impl_fn = [_]Definition{
.{ .name = "close", .arg_type = .none, .impl = command.close }, .{ .name = "close", .impl = impl.close },
.{ .name = "exit", .arg_type = .none, .impl = command.exit }, .{ .name = "exit", .impl = impl.exit },
.{ .name = "focus", .arg_type = .direction, .impl = command.focus }, .{ .name = "focus", .impl = impl.focus },
.{ .name = "focus_all_tags", .arg_type = .none, .impl = command.focusAllTags }, .{ .name = "focus_all_tags", .impl = impl.focusAllTags },
.{ .name = "focus_output", .arg_type = .direction, .impl = command.focusOutput }, .{ .name = "focus_output", .impl = impl.focusOutput },
.{ .name = "focus_tag", .arg_type = .uint, .impl = command.focusTag }, .{ .name = "focus_tag", .impl = impl.focusTag },
.{ .name = "layout", .arg_type = .str, .impl = command.layout}, .{ .name = "layout", .impl = impl.layout},
.{ .name = "mod_master_count", .arg_type = .int, .impl = command.modMasterCount }, .{ .name = "mod_master_count", .impl = impl.modMasterCount },
.{ .name = "mod_master_factor", .arg_type = .float, .impl = command.modMasterFactor }, .{ .name = "mod_master_factor", .impl = impl.modMasterFactor },
.{ .name = "mode", .arg_type = .str, .impl = command.mode }, .{ .name = "mode", .impl = impl.mode },
.{ .name = "send_to_output", .arg_type = .direction, .impl = command.sendToOutput }, .{ .name = "send_to_output", .impl = impl.sendToOutput },
.{ .name = "spawn", .arg_type = .str, .impl = command.spawn }, .{ .name = "spawn", .impl = impl.spawn },
.{ .name = "tag_view", .arg_type = .uint, .impl = command.tagView }, .{ .name = "tag_view", .impl = impl.tagView },
.{ .name = "tag_view_all_tags", .arg_type = .none, .impl = command.tagViewAllTags }, .{ .name = "tag_view_all_tags", .impl = impl.tagViewAllTags },
.{ .name = "toggle_float", .arg_type = .none, .impl = command.toggleFloat }, .{ .name = "toggle_float", .impl = impl.toggleFloat },
.{ .name = "toggle_tag_focus", .arg_type = .uint, .impl = command.toggleTagFocus }, .{ .name = "toggle_tag_focus", .impl = impl.toggleTagFocus },
.{ .name = "toggle_view_tag", .arg_type = .uint, .impl = command.toggleViewTag }, .{ .name = "toggle_view_tag", .impl = impl.toggleViewTag },
.{ .name = "zoom", .arg_type = .none, .impl = command.zoom }, .{ .name = "zoom", .impl = impl.zoom },
}; };
// zig fmt: on // zig fmt: on
@ -126,29 +131,27 @@ pub const Error = error{
InvalidCharacter, InvalidCharacter,
InvalidDirection, InvalidDirection,
OutOfMemory, OutOfMemory,
CommandFailed,
}; };
impl: ImplFn, /// Run a command for the given Seat. The `args` parameter is similar to the
arg: Arg, /// 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 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| { try impl_fn(allocator, seat, args, failure_message);
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);
} }

View File

@ -15,13 +15,20 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
const c = @import("../c.zig"); const c = @import("../c.zig");
const Arg = @import("../Command.zig").Arg; const Error = @import("../command.zig").Error;
const Seat = @import("../Seat.zig"); const Seat = @import("../Seat.zig");
/// Close the focused view, if any. /// 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| { if (seat.focused_view) |view| {
// Note: we don't call arrange() here as it will be called // Note: we don't call arrange() here as it will be called
// automatically when the view is unmapped. // automatically when the view is unmapped.

View File

@ -15,12 +15,20 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
const c = @import("../c.zig"); const c = @import("../c.zig");
const Arg = @import("../Command.zig").Arg; const Error = @import("../command.zig").Error;
const Seat = @import("../Seat.zig"); const Seat = @import("../Seat.zig");
/// Exit the compositor, terminating the wayland session. /// 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); c.wl_display_terminate(seat.input_manager.server.wl_display);
} }

View File

@ -15,18 +15,30 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
const c = @import("../c.zig"); 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 Seat = @import("../Seat.zig");
const View = @import("../View.zig"); const View = @import("../View.zig");
const ViewStack = @import("../view_stack.zig").ViewStack; const ViewStack = @import("../view_stack.zig").ViewStack;
/// Focus either the next or the previous visible view, depending on the enum /// 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. /// passed. Does nothing if there are 1 or 0 views in the stack.
pub fn focus(seat: *Seat, arg: Arg) void { pub fn focus(
const direction = arg.direction; 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; const output = seat.focused_output;
if (seat.focused_view) |current_focus| { if (seat.focused_view) |current_focus| {
// If there is a currently focused view, focus the next visible view in the stack. // 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); 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), .Next => ViewStack(View).iterator(output.views.first, output.current_focused_tags),
.Prev => ViewStack(View).reverseIterator(output.views.last, 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); seat.focus(if (it.next()) |node| &node.view else null);
} }

View File

@ -15,11 +15,18 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
const Arg = @import("../Command.zig").Arg; const std = @import("std");
const Error = @import("../command.zig").Error;
const Seat = @import("../Seat.zig"); const Seat = @import("../Seat.zig");
/// Set focus to all tags /// 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.focused_output.pending_focused_tags = 0xFFFFFFFF;
seat.input_manager.server.root.arrange(); seat.input_manager.server.root.arrange();
} }

View File

@ -19,14 +19,23 @@ const std = @import("std");
const c = @import("../c.zig"); 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 Output = @import("../Output.zig");
const Seat = @import("../Seat.zig"); const Seat = @import("../Seat.zig");
/// Focus either the next or the previous output, depending on the bool passed. /// Focus either the next or the previous output, depending on the bool passed.
/// Does nothing if there is only one output. /// Does nothing if there is only one output.
pub fn focusOutput(seat: *Seat, arg: Arg) void { pub fn focusOutput(
const direction = arg.direction; 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; const root = &seat.input_manager.server.root;
// If the noop output is focused, there are no other outputs to switch to // If the noop output is focused, there are no other outputs to switch to
if (seat.focused_output == &root.noop_output) { if (seat.focused_output == &root.noop_output) {

View File

@ -15,12 +15,23 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
const Arg = @import("../Command.zig").Arg; const std = @import("std");
const Error = @import("../command.zig").Error;
const Seat = @import("../Seat.zig"); const Seat = @import("../Seat.zig");
/// Switch focus to the passed tag. /// Switch focus to the passed tag.
pub fn focusTag(seat: *Seat, arg: Arg) void { pub fn focusTag(
const tags = @as(u32, 1) << @intCast(u5, arg.uint - 1); 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.focused_output.pending_focused_tags = tags;
seat.input_manager.server.root.arrange(); seat.input_manager.server.root.arrange();
} }

View File

@ -15,14 +15,23 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
const c = @import("../c.zig"); const c = @import("../c.zig");
const Arg = @import("../Command.zig").Arg; const Error = @import("../command.zig").Error;
const Seat = @import("../Seat.zig"); const Seat = @import("../Seat.zig");
pub fn layout(seat: *Seat, arg: Arg) void { pub fn layout(
const layout_name = arg.str; allocator: *std.mem.Allocator,
seat.focused_output.layout = seat.focused_output.getLayoutByName(layout_name); 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.focused_output.arrangeViews();
seat.input_manager.server.root.startTransaction(); seat.input_manager.server.root.startTransaction();
} }

View File

@ -19,12 +19,20 @@ const std = @import("std");
const c = @import("../c.zig"); const c = @import("../c.zig");
const Arg = @import("../Command.zig").Arg; const Error = @import("../command.zig").Error;
const Seat = @import("../Seat.zig"); const Seat = @import("../Seat.zig");
/// Modify the number of master views /// Modify the number of master views
pub fn modMasterCount(seat: *Seat, arg: Arg) void { pub fn modMasterCount(
const delta = arg.int; 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; const output = seat.focused_output;
output.master_count = @intCast( output.master_count = @intCast(
u32, u32,

View File

@ -19,12 +19,20 @@ const std = @import("std");
const c = @import("../c.zig"); const c = @import("../c.zig");
const Arg = @import("../Command.zig").Arg; const Error = @import("../command.zig").Error;
const Seat = @import("../Seat.zig"); const Seat = @import("../Seat.zig");
/// Modify the percent of the width of the screen that the master views occupy. /// Modify the percent of the width of the screen that the master views occupy.
pub fn modMasterFactor(seat: *Seat, arg: Arg) void { pub fn modMasterFactor(
const delta = arg.float; 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 output = seat.focused_output;
const new_master_factor = std.math.min( const new_master_factor = std.math.min(
std.math.max(output.master_factor + delta, 0.05), std.math.max(output.master_factor + delta, 0.05),

View File

@ -15,12 +15,21 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
const Arg = @import("../Command.zig").Arg; const std = @import("std");
const Error = @import("../command.zig").Error;
const Seat = @import("../Seat.zig"); const Seat = @import("../Seat.zig");
/// Switch to the given mode /// Switch to the given mode
pub fn mode(seat: *Seat, arg: Arg) void { pub fn mode(
const mode_name = arg.str; 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; const config = seat.input_manager.server.config;
seat.mode = config.getMode(mode_name); seat.mode = config.getMode(args[1]);
} }

View File

@ -19,16 +19,23 @@ const std = @import("std");
const c = @import("../c.zig"); 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 Output = @import("../Output.zig");
const Seat = @import("../Seat.zig"); const Seat = @import("../Seat.zig");
/// Send the focused view to the the next or the previous output, depending on /// 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. /// the bool passed. Does nothing if there is only one output.
pub fn sendToOutput(seat: *Seat, arg: Arg) void { pub fn sendToOutput(
@import("../log.zig").Log.Debug.log("send to output", .{}); 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; const root = &seat.input_manager.server.root;
if (seat.focused_view) |view| { if (seat.focused_view) |view| {

View File

@ -17,23 +17,31 @@
const std = @import("std"); const std = @import("std");
const c = @import("../c.zig"); const Error = @import("../command.zig").Error;
const Arg = @import("../Command.zig").Arg;
const Log = @import("../log.zig").Log;
const Seat = @import("../Seat.zig"); const Seat = @import("../Seat.zig");
/// Spawn a program. /// Spawn a program.
pub fn spawn(seat: *Seat, arg: Arg) void { pub fn spawn(
const cmd = arg.str; 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| { std.ChildProcess.spawn(child) catch |err| {
Log.Error.log("Failed to execute {}: {}", .{ cmd, err }); failure_message.* = try std.fmt.allocPrint(
return; allocator,
"failed to spawn {}: {}.",
.{ cmd, err },
);
return Error.CommandFailed;
}; };
} }

View File

@ -15,14 +15,25 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
const c = @import("../c.zig"); const c = @import("../c.zig");
const Arg = @import("../Command.zig").Arg; const Error = @import("../command.zig").Error;
const Seat = @import("../Seat.zig"); const Seat = @import("../Seat.zig");
/// Set the tag of the focused view. /// Set the tag of the focused view.
pub fn tagView(seat: *Seat, arg: Arg) void { pub fn tagView(
const tags = @as(u32, 1) << @intCast(u5, arg.uint - 1); 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 (seat.focused_view) |view| {
if (view.current_tags != tags) { if (view.current_tags != tags) {
view.pending_tags = tags; view.pending_tags = tags;

View File

@ -15,13 +15,20 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
const c = @import("../c.zig"); const c = @import("../c.zig");
const Arg = @import("../Command.zig").Arg; const Error = @import("../command.zig").Error;
const Seat = @import("../Seat.zig"); const Seat = @import("../Seat.zig");
/// Tag the focused view with all tags. /// 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 (seat.focused_view) |view| {
if (view.current_tags != 0xFFFFFFFF) { if (view.current_tags != 0xFFFFFFFF) {
view.pending_tags = 0xFFFFFFFF; view.pending_tags = 0xFFFFFFFF;

View File

@ -15,14 +15,22 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
const c = @import("../c.zig"); const c = @import("../c.zig");
const Arg = @import("../Command.zig").Arg; const Error = @import("../command.zig").Error;
const Seat = @import("../Seat.zig"); const Seat = @import("../Seat.zig");
/// Make the focused view float or stop floating, depending on its current /// Make the focused view float or stop floating, depending on its current
/// state. /// 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| { if (seat.focused_view) |view| {
view.setFloating(!view.floating); view.setFloating(!view.floating);
view.output.root.arrange(); view.output.root.arrange();

View File

@ -15,14 +15,25 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
const c = @import("../c.zig"); const c = @import("../c.zig");
const Arg = @import("../Command.zig").Arg; const Error = @import("../command.zig").Error;
const Seat = @import("../Seat.zig"); const Seat = @import("../Seat.zig");
/// Toggle focus of the passsed tags. /// Toggle focus of the passsed tags.
pub fn toggleTagFocus(seat: *Seat, arg: Arg) void { pub fn toggleTagFocus(
const tags = @as(u32, 1) << @intCast(u5, arg.uint - 1); 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 output = seat.focused_output;
const new_focused_tags = output.current_focused_tags ^ tags; const new_focused_tags = output.current_focused_tags ^ tags;
if (new_focused_tags != 0) { if (new_focused_tags != 0) {

View File

@ -15,14 +15,25 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
const c = @import("../c.zig"); const c = @import("../c.zig");
const Arg = @import("../Command.zig").Arg; const Error = @import("../command.zig").Error;
const Seat = @import("../Seat.zig"); const Seat = @import("../Seat.zig");
/// Toggle the passed tag of the focused view /// Toggle the passed tag of the focused view
pub fn toggleViewTag(seat: *Seat, arg: Arg) void { pub fn toggleViewTag(
const tags = @as(u32, 1) << @intCast(u5, arg.uint - 1); 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 (seat.focused_view) |view| {
const new_tags = view.current_tags ^ tags; const new_tags = view.current_tags ^ tags;
if (new_tags != 0) { if (new_tags != 0) {

View File

@ -15,16 +15,25 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
const c = @import("../c.zig"); const c = @import("../c.zig");
const Arg = @import("../Command.zig").Arg; const Error = @import("../command.zig").Error;
const Seat = @import("../Seat.zig"); const Seat = @import("../Seat.zig");
const View = @import("../View.zig"); const View = @import("../View.zig");
const ViewStack = @import("../view_stack.zig").ViewStack; const ViewStack = @import("../view_stack.zig").ViewStack;
/// Bump the focused view to the top of the stack. If the view on the top of /// 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. /// 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| { if (seat.focused_view) |current_focus| {
const output = seat.focused_output; const output = seat.focused_output;
const focused_node = @fieldParentPtr(ViewStack(View).Node, "view", current_focus); const focused_node = @fieldParentPtr(ViewStack(View).Node, "view", current_focus);