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 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");

View file

@ -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);
}

View file

@ -15,9 +15,32 @@
// You should have received a copy of the GNU General Public License
// 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 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);
}

View file

@ -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();
}

View file

@ -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;
}
}

View file

@ -15,13 +15,11 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
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);
}

View file

@ -15,13 +15,20 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
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.

View file

@ -15,12 +15,20 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
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);
}

View file

@ -15,18 +15,30 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
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);
}

View file

@ -15,11 +15,18 @@
// You should have received a copy of the GNU General Public License
// 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");
/// 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();
}

View file

@ -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) {

View file

@ -15,12 +15,23 @@
// You should have received a copy of the GNU General Public License
// 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");
/// 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();
}

View file

@ -15,14 +15,23 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
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();
}

View file

@ -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,

View file

@ -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),

View file

@ -15,12 +15,21 @@
// You should have received a copy of the GNU General Public License
// 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");
/// 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]);
}

View file

@ -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| {

View file

@ -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;
};
}

View file

@ -15,14 +15,25 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
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;

View file

@ -15,13 +15,20 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
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;

View file

@ -15,14 +15,22 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
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();

View file

@ -15,14 +15,25 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
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) {

View file

@ -15,14 +15,25 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
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) {

View file

@ -15,16 +15,25 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
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);