2020-05-19 13:59:50 -07:00
|
|
|
// This file is part of river, a dynamic tiling wayland compositor.
|
|
|
|
//
|
|
|
|
// Copyright 2020 Isaac Freund
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// 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 Seat = @import("Seat.zig");
|
|
|
|
|
2020-05-26 13:55:07 -07:00
|
|
|
pub const Direction = enum {
|
2020-07-15 04:02:58 -07:00
|
|
|
next,
|
|
|
|
previous,
|
2020-05-19 13:59:50 -07:00
|
|
|
};
|
|
|
|
|
2020-10-06 18:00:57 -07:00
|
|
|
pub const PhysicalDirection = enum {
|
|
|
|
up,
|
|
|
|
down,
|
|
|
|
left,
|
|
|
|
right,
|
|
|
|
};
|
|
|
|
|
|
|
|
pub const Orientation = enum {
|
|
|
|
horizontal,
|
|
|
|
vertical,
|
|
|
|
};
|
|
|
|
|
2020-05-26 13:55:07 -07:00
|
|
|
// TODO: this could be replaced with a comptime hashmap
|
2020-05-19 13:59:50 -07:00
|
|
|
// zig fmt: off
|
2020-06-02 06:19:08 -07:00
|
|
|
const str_to_impl_fn = [_]struct {
|
|
|
|
name: []const u8,
|
2020-06-26 08:57:03 -07:00
|
|
|
impl: fn (*std.mem.Allocator, *Seat, []const []const u8, *?[]const u8) Error!void,
|
2020-06-02 06:19:08 -07:00
|
|
|
}{
|
2020-08-24 02:50:38 -07:00
|
|
|
.{ .name = "attach-mode", .impl = @import("command/attach_mode.zig").attachMode },
|
2020-07-15 03:54:36 -07:00
|
|
|
.{ .name = "background-color", .impl = @import("command/config.zig").backgroundColor },
|
|
|
|
.{ .name = "border-color-focused", .impl = @import("command/config.zig").borderColorFocused },
|
|
|
|
.{ .name = "border-color-unfocused", .impl = @import("command/config.zig").borderColorUnfocused },
|
|
|
|
.{ .name = "border-width", .impl = @import("command/config.zig").borderWidth },
|
|
|
|
.{ .name = "close", .impl = @import("command/close.zig").close },
|
2020-07-16 10:45:45 -07:00
|
|
|
.{ .name = "csd-filter-add", .impl = @import("command/filter.zig").csdFilterAdd },
|
2020-07-15 03:54:36 -07:00
|
|
|
.{ .name = "declare-mode", .impl = @import("command/declare_mode.zig").declareMode },
|
|
|
|
.{ .name = "enter-mode", .impl = @import("command/enter_mode.zig").enterMode },
|
|
|
|
.{ .name = "exit", .impl = @import("command/exit.zig").exit },
|
2020-07-16 10:45:45 -07:00
|
|
|
.{ .name = "float-filter-add", .impl = @import("command/filter.zig").floatFilterAdd },
|
2020-07-15 03:54:36 -07:00
|
|
|
.{ .name = "focus-output", .impl = @import("command/focus_output.zig").focusOutput },
|
2020-09-14 15:38:50 -07:00
|
|
|
.{ .name = "focus-follows-cursor", .impl = @import("command/focus_follows_cursor.zig").focusFollowsCursor },
|
2020-07-15 03:54:36 -07:00
|
|
|
.{ .name = "focus-view", .impl = @import("command/focus_view.zig").focusView },
|
|
|
|
.{ .name = "layout", .impl = @import("command/layout.zig").layout },
|
|
|
|
.{ .name = "map", .impl = @import("command/map.zig").map },
|
2020-08-24 05:52:47 -07:00
|
|
|
.{ .name = "map-pointer", .impl = @import("command/map.zig").mapPointer },
|
2020-07-15 03:54:36 -07:00
|
|
|
.{ .name = "mod-master-count", .impl = @import("command/mod_master_count.zig").modMasterCount },
|
|
|
|
.{ .name = "mod-master-factor", .impl = @import("command/mod_master_factor.zig").modMasterFactor },
|
2020-10-06 18:00:57 -07:00
|
|
|
.{ .name = "move", .impl = @import("command/move.zig").move },
|
2020-10-03 13:09:15 -07:00
|
|
|
.{ .name = "opacity", .impl = @import("command/opacity.zig").opacity },
|
2020-07-15 03:54:36 -07:00
|
|
|
.{ .name = "outer-padding", .impl = @import("command/config.zig").outerPadding },
|
2020-10-06 18:00:57 -07:00
|
|
|
.{ .name = "resize", .impl = @import("command/move.zig").resize },
|
2020-07-15 03:54:36 -07:00
|
|
|
.{ .name = "send-to-output", .impl = @import("command/send_to_output.zig").sendToOutput },
|
|
|
|
.{ .name = "set-focused-tags", .impl = @import("command/tags.zig").setFocusedTags },
|
|
|
|
.{ .name = "set-view-tags", .impl = @import("command/tags.zig").setViewTags },
|
2020-10-06 18:00:57 -07:00
|
|
|
.{ .name = "snap", .impl = @import("command/move.zig").snap },
|
2020-07-15 03:54:36 -07:00
|
|
|
.{ .name = "spawn", .impl = @import("command/spawn.zig").spawn },
|
2020-10-25 04:41:19 -07:00
|
|
|
.{ .name = "swap", .impl = @import("command/swap.zig").swap},
|
2020-07-15 03:54:36 -07:00
|
|
|
.{ .name = "toggle-float", .impl = @import("command/toggle_float.zig").toggleFloat },
|
|
|
|
.{ .name = "toggle-focused-tags", .impl = @import("command/tags.zig").toggleFocusedTags },
|
|
|
|
.{ .name = "toggle-fullscreen", .impl = @import("command/toggle_fullscreen.zig").toggleFullscreen },
|
|
|
|
.{ .name = "toggle-view-tags", .impl = @import("command/tags.zig").toggleViewTags },
|
2020-10-23 15:59:40 -07:00
|
|
|
.{ .name = "unmap", .impl = @import("command/map.zig").unmap },
|
2020-07-15 03:54:36 -07:00
|
|
|
.{ .name = "view-padding", .impl = @import("command/config.zig").viewPadding },
|
|
|
|
.{ .name = "xcursor-theme", .impl = @import("command/xcursor_theme.zig").xcursorTheme },
|
|
|
|
.{ .name = "zoom", .impl = @import("command/zoom.zig").zoom },
|
2020-05-19 13:59:50 -07:00
|
|
|
};
|
|
|
|
// zig fmt: on
|
|
|
|
|
2020-05-24 06:18:57 -07:00
|
|
|
pub const Error = error{
|
|
|
|
NoCommand,
|
|
|
|
UnknownCommand,
|
|
|
|
NotEnoughArguments,
|
|
|
|
TooManyArguments,
|
|
|
|
Overflow,
|
|
|
|
InvalidCharacter,
|
|
|
|
InvalidDirection,
|
2020-10-06 18:00:57 -07:00
|
|
|
InvalidPhysicalDirection,
|
|
|
|
InvalidOrientation,
|
2020-06-13 04:09:43 -07:00
|
|
|
InvalidRgba,
|
2020-10-03 13:09:15 -07:00
|
|
|
InvalidValue,
|
2020-06-09 08:28:48 -07:00
|
|
|
UnknownOption,
|
2020-05-24 06:18:57 -07:00
|
|
|
OutOfMemory,
|
2020-06-26 08:57:03 -07:00
|
|
|
Other,
|
2020-05-24 06:18:57 -07:00
|
|
|
};
|
|
|
|
|
2020-05-26 13:55:07 -07:00
|
|
|
/// 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.
|
2020-06-26 08:57:03 -07:00
|
|
|
/// The optional slice passed as the out parameter must initially be set to
|
|
|
|
/// null. If the command produces output or Error.Other is returned, the slice
|
|
|
|
/// will be set to the output of the command or a failure message, respectively.
|
|
|
|
/// The caller is then responsible for freeing that slice, which will be
|
|
|
|
/// allocated using the provided allocator.
|
2020-05-26 13:55:07 -07:00
|
|
|
pub fn run(
|
|
|
|
allocator: *std.mem.Allocator,
|
|
|
|
seat: *Seat,
|
|
|
|
args: []const []const u8,
|
2020-06-26 08:57:03 -07:00
|
|
|
out: *?[]const u8,
|
2020-05-26 13:55:07 -07:00
|
|
|
) Error!void {
|
2020-06-26 08:57:03 -07:00
|
|
|
std.debug.assert(out.* == null);
|
2020-05-26 13:55:07 -07:00
|
|
|
if (args.len == 0) return Error.NoCommand;
|
2020-05-19 13:59:50 -07:00
|
|
|
|
2020-05-26 13:55:07 -07:00
|
|
|
const impl_fn = for (str_to_impl_fn) |definition| {
|
2020-06-26 08:57:03 -07:00
|
|
|
if (std.mem.eql(u8, args[0], definition.name)) break definition.impl;
|
2020-05-26 13:55:07 -07:00
|
|
|
} else return Error.UnknownCommand;
|
2020-05-19 13:59:50 -07:00
|
|
|
|
2020-06-26 08:57:03 -07:00
|
|
|
try impl_fn(allocator, seat, args, out);
|
2020-05-19 13:59:50 -07:00
|
|
|
}
|
2020-06-26 09:43:20 -07:00
|
|
|
|
|
|
|
/// Return a short error message for the given error. Passing Error.Other is UB
|
|
|
|
pub fn errToMsg(err: Error) [:0]const u8 {
|
|
|
|
return switch (err) {
|
|
|
|
Error.NoCommand => "no command given",
|
|
|
|
Error.UnknownCommand => "unknown command",
|
|
|
|
Error.UnknownOption => "unknown option",
|
|
|
|
Error.NotEnoughArguments => "not enough arguments",
|
|
|
|
Error.TooManyArguments => "too many arguments",
|
|
|
|
Error.Overflow => "value out of bounds",
|
|
|
|
Error.InvalidCharacter => "invalid character in argument",
|
|
|
|
Error.InvalidDirection => "invalid direction. Must be 'next' or 'previous'",
|
2020-10-06 18:00:57 -07:00
|
|
|
Error.InvalidPhysicalDirection => "invalid direction. Must be 'up', 'down', 'left' or 'right'",
|
|
|
|
Error.InvalidOrientation => "invalid orientation. Must be 'horizontal', or 'vertical'",
|
2020-06-26 09:43:20 -07:00
|
|
|
Error.InvalidRgba => "invalid color format, must be #RRGGBB or #RRGGBBAA",
|
2020-10-03 13:09:15 -07:00
|
|
|
Error.InvalidValue => "invalid value",
|
2020-06-26 09:43:20 -07:00
|
|
|
Error.OutOfMemory => "out of memory",
|
|
|
|
Error.Other => unreachable,
|
|
|
|
};
|
|
|
|
}
|