git.lucas.co / cce-compositor
Wayland compositor (wlroots)
git clone https://git.lucas.co/cce-compositor.git

commit7adc790081202b200591e36090b0a1d53a161048
parent75ba50151f
authorIsaac Freund <[email protected]>
date2020-06-01 00:20
Implement declare_mode

 src/Config.zig               |  4 ++--
 src/Seat.zig                 |  2 +-
 src/command.zig              |  6 ++++--
 src/command/declare_mode.zig | 51 ++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 58 insertions(+), 5 deletions(-)

diff --git a/src/Config.zig b/src/Config.zig
index d948a95..e887efe 100644
--- a/src/Config.zig
+++ b/src/Config.zig
@@ -35,7 +35,7 @@ view_padding: u32,
 outer_padding: u32,
 
 /// Map of mode name to mode id
-mode_to_id: std.StringHashMap(u32),
+mode_to_id: std.StringHashMap(usize),
 
 /// All user-defined keybinding modes, indexed by mode id
 modes: std.ArrayList(std.ArrayList(Keybind)),
@@ -48,7 +48,7 @@ pub fn init(self: *Self, allocator: *std.mem.Allocator) !void {
     self.view_padding = 8;
     self.outer_padding = 8;
 
-    self.mode_to_id = std.StringHashMap(u32).init(allocator);
+    self.mode_to_id = std.StringHashMap(usize).init(allocator);
     try self.mode_to_id.putNoClobber("normal", 0);
     try self.mode_to_id.putNoClobber("passthrough", 1);
 
diff --git a/src/Seat.zig b/src/Seat.zig
index 31be96e..22388d1 100644
--- a/src/Seat.zig
+++ b/src/Seat.zig
@@ -46,7 +46,7 @@ cursor: Cursor,
 keyboards: std.TailQueue(Keyboard),
 
 /// Id of the current keybind mode
-mode_id: u32,
+mode_id: usize,
 
 /// Currently focused output, may be the noop output if no
 focused_output: *Output,
diff --git a/src/command.zig b/src/command.zig
index e2c32b1..bec1b8f 100644
--- a/src/command.zig
+++ b/src/command.zig
@@ -21,6 +21,8 @@ const Seat = @import("Seat.zig");
 
 const impl = struct {
     const close = @import("command/close.zig").close;
+    const declareMode = @import("command/declare_mode.zig").declareMode;
+    const enterMode = @import("command/enter_mode.zig").enterMode;
     const exit = @import("command/exit.zig").exit;
     const focus = @import("command/focus.zig").focus;
     const focusAllTags = @import("command/focus_all_tags.zig").focusAllTags;
@@ -29,7 +31,6 @@ const impl = struct {
     const layout = @import("command/layout.zig").layout;
     const modMasterCount = @import("command/mod_master_count.zig").modMasterCount;
     const modMasterFactor = @import("command/mod_master_factor.zig").modMasterFactor;
-    const enterMode = @import("command/enter_mode.zig").enterMode;
     const sendToOutput = @import("command/send_to_output.zig").sendToOutput;
     const spawn = @import("command/spawn.zig").spawn;
     const tagView = @import("command/tag_view.zig").tagView;
@@ -63,6 +64,8 @@ const Definition = struct {
 // zig fmt: off
 const str_to_impl_fn = [_]Definition{
     .{ .name = "close",             .impl = impl.close },
+    .{ .name = "declare_mode",      .impl = impl.declareMode},
+    .{ .name = "enter_mode",        .impl = impl.enterMode },
     .{ .name = "exit",              .impl = impl.exit },
     .{ .name = "focus",             .impl = impl.focus },
     .{ .name = "focus_all_tags",    .impl = impl.focusAllTags },
@@ -71,7 +74,6 @@ const str_to_impl_fn = [_]Definition{
     .{ .name = "layout",            .impl = impl.layout },
     .{ .name = "mod_master_count",  .impl = impl.modMasterCount },
     .{ .name = "mod_master_factor", .impl = impl.modMasterFactor },
-    .{ .name = "enter_mode",        .impl = impl.enterMode },
     .{ .name = "send_to_output",    .impl = impl.sendToOutput },
     .{ .name = "spawn",             .impl = impl.spawn },
     .{ .name = "tag_view",          .impl = impl.tagView },
diff --git a/src/command/declare_mode.zig b/src/command/declare_mode.zig
new file mode 100644
index 0000000..6cc1489
--- /dev/null
+++ b/src/command/declare_mode.zig
@@ -0,0 +1,51 @@
+// 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 c = @import("../c.zig");
+
+const Error = @import("../command.zig").Error;
+const Keybind = @import("../Keybind.zig");
+const Seat = @import("../Seat.zig");
+
+/// Declare a new keybind mode
+pub fn declareMode(
+    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 new_mode_name = args[1];
+
+    if (config.mode_to_id.get(new_mode_name) != null) {
+        failure_message.* = try std.fmt.allocPrint(
+            allocator,
+            "mode '{}' already exists and cannot be re-declared",
+            .{new_mode_name},
+        );
+        return Error.CommandFailed;
+    }
+
+    try config.mode_to_id.putNoClobber(new_mode_name, config.modes.items.len);
+    errdefer _ = config.mode_to_id.remove(new_mode_name);
+    try config.modes.append(std.ArrayList(Keybind).init(allocator));
+}