Implement declare_mode

This commit is contained in:
Isaac Freund 2020-06-01 00:20:49 +02:00
parent 134eff2593
commit d59b0791a9
No known key found for this signature in database
GPG key ID: 86DED400DDFD7A11
4 changed files with 58 additions and 5 deletions

View file

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

View file

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

View file

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

View file

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