From 3fe1edbe3edec7967d0d1c67dc7677da9f6a6f4e Mon Sep 17 00:00:00 2001 From: Isaac Freund Date: Sun, 17 May 2020 00:03:26 +0200 Subject: [PATCH] Implement keybinding modes --- src/Config.zig | 100 +++++++++++++++++++++++++++---------------- src/Keybind.zig | 24 +++++++++++ src/Mode.zig | 42 ++++++++++++++++++ src/Seat.zig | 8 +++- src/Server.zig | 2 +- src/command.zig | 3 +- src/command/mode.zig | 26 +++++++++++ 7 files changed, 164 insertions(+), 41 deletions(-) create mode 100644 src/Keybind.zig create mode 100644 src/Mode.zig create mode 100644 src/command/mode.zig diff --git a/src/Config.zig b/src/Config.zig index 315a045..85c9b32 100644 --- a/src/Config.zig +++ b/src/Config.zig @@ -22,6 +22,8 @@ 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"); /// Width of borders in pixels @@ -33,15 +35,8 @@ view_padding: u32, /// Amount of padding arount the outer edge of the layout in pixels outer_padding: u32, -const Keybind = struct { - keysym: c.xkb_keysym_t, - modifiers: u32, - command: command.Command, - arg: command.Arg, -}; - -/// All user-defined keybindings -keybinds: std.ArrayList(Keybind), +/// All user-defined keybinding modes +modes: std.ArrayList(Mode), /// List of app_ids which will be started floating float_filter: std.ArrayList([*:0]const u8), @@ -51,13 +46,16 @@ pub fn init(self: *Self, allocator: *std.mem.Allocator) !void { self.view_padding = 8; self.outer_padding = 8; - self.keybinds = std.ArrayList(Keybind).init(allocator); + self.modes = std.ArrayList(Mode).init(allocator); + try self.modes.append(try Mode.init("normal", allocator)); + self.float_filter = std.ArrayList([*:0]const u8).init(allocator); + const normal = &self.modes.items[0]; const mod = c.WLR_MODIFIER_LOGO; // Mod+Shift+Return to start an instance of alacritty - try self.keybinds.append(Keybind{ + try normal.keybinds.append(.{ .keysym = c.XKB_KEY_Return, .modifiers = mod | c.WLR_MODIFIER_SHIFT, .command = command.spawn, @@ -65,7 +63,7 @@ pub fn init(self: *Self, allocator: *std.mem.Allocator) !void { }); // Mod+Q to close the focused view - try self.keybinds.append(Keybind{ + try normal.keybinds.append(.{ .keysym = c.XKB_KEY_q, .modifiers = mod, .command = command.close_view, @@ -73,7 +71,7 @@ pub fn init(self: *Self, allocator: *std.mem.Allocator) !void { }); // Mod+E to exit river - try self.keybinds.append(Keybind{ + try normal.keybinds.append(.{ .keysym = c.XKB_KEY_e, .modifiers = mod, .command = command.exitCompositor, @@ -81,15 +79,13 @@ pub fn init(self: *Self, allocator: *std.mem.Allocator) !void { }); // Mod+J and Mod+K to focus the next/previous view in the layout stack - try self.keybinds.append( - Keybind{ - .keysym = c.XKB_KEY_j, - .modifiers = mod, - .command = command.focusView, - .arg = .{ .direction = .Next }, - }, - ); - try self.keybinds.append(Keybind{ + try normal.keybinds.append(.{ + .keysym = c.XKB_KEY_j, + .modifiers = mod, + .command = command.focusView, + .arg = .{ .direction = .Next }, + }); + try normal.keybinds.append(.{ .keysym = c.XKB_KEY_k, .modifiers = mod, .command = command.focusView, @@ -98,7 +94,7 @@ pub fn init(self: *Self, allocator: *std.mem.Allocator) !void { // Mod+Return to bump the focused view to the top of the layout stack, // making it the new master - try self.keybinds.append(Keybind{ + try normal.keybinds.append(.{ .keysym = c.XKB_KEY_Return, .modifiers = mod, .command = command.zoom, @@ -106,13 +102,13 @@ pub fn init(self: *Self, allocator: *std.mem.Allocator) !void { }); // Mod+H and Mod+L to increase/decrease the width of the master column - try self.keybinds.append(Keybind{ + try normal.keybinds.append(.{ .keysym = c.XKB_KEY_h, .modifiers = mod, .command = command.modifyMasterFactor, .arg = .{ .float = 0.05 }, }); - try self.keybinds.append(Keybind{ + try normal.keybinds.append(.{ .keysym = c.XKB_KEY_l, .modifiers = mod, .command = command.modifyMasterFactor, @@ -121,13 +117,13 @@ pub fn init(self: *Self, allocator: *std.mem.Allocator) !void { // Mod+Shift+H and Mod+Shift+L to increment/decrement the number of // master views in the layout - try self.keybinds.append(Keybind{ + try normal.keybinds.append(.{ .keysym = c.XKB_KEY_h, .modifiers = mod | c.WLR_MODIFIER_SHIFT, .command = command.modifyMasterCount, .arg = .{ .int = 1 }, }); - try self.keybinds.append(Keybind{ + try normal.keybinds.append(.{ .keysym = c.XKB_KEY_l, .modifiers = mod | c.WLR_MODIFIER_SHIFT, .command = command.modifyMasterCount, @@ -137,28 +133,28 @@ pub fn init(self: *Self, allocator: *std.mem.Allocator) !void { comptime var i = 0; inline while (i < 9) : (i += 1) { // Mod+[1-9] to focus tag [1-9] - try self.keybinds.append(Keybind{ + try normal.keybinds.append(.{ .keysym = c.XKB_KEY_1 + i, .modifiers = mod, .command = command.focusTags, .arg = .{ .uint = 1 << i }, }); // Mod+Shift+[1-9] to tag focused view with tag [1-9] - try self.keybinds.append(Keybind{ + try normal.keybinds.append(.{ .keysym = c.XKB_KEY_1 + i, .modifiers = mod | c.WLR_MODIFIER_SHIFT, .command = command.setViewTags, .arg = .{ .uint = 1 << i }, }); // Mod+Ctrl+[1-9] to toggle focus of tag [1-9] - try self.keybinds.append(Keybind{ + try normal.keybinds.append(.{ .keysym = c.XKB_KEY_1 + i, .modifiers = mod | c.WLR_MODIFIER_CTRL, .command = command.toggleTags, .arg = .{ .uint = 1 << i }, }); // Mod+Shift+Ctrl+[1-9] to toggle tag [1-9] of focused view - try self.keybinds.append(Keybind{ + try normal.keybinds.append(.{ .keysym = c.XKB_KEY_1 + i, .modifiers = mod | c.WLR_MODIFIER_CTRL | c.WLR_MODIFIER_SHIFT, .command = command.toggleViewTags, @@ -167,7 +163,7 @@ pub fn init(self: *Self, allocator: *std.mem.Allocator) !void { } // Mod+0 to focus all tags - try self.keybinds.append(Keybind{ + try normal.keybinds.append(.{ .keysym = c.XKB_KEY_0, .modifiers = mod, .command = command.focusTags, @@ -175,7 +171,7 @@ pub fn init(self: *Self, allocator: *std.mem.Allocator) !void { }); // Mod+Shift+0 to tag focused view with all tags - try self.keybinds.append(Keybind{ + try normal.keybinds.append(.{ .keysym = c.XKB_KEY_0, .modifiers = mod | c.WLR_MODIFIER_SHIFT, .command = command.setViewTags, @@ -183,13 +179,13 @@ pub fn init(self: *Self, allocator: *std.mem.Allocator) !void { }); // Mod+Period and Mod+Comma to focus the next/previous output - try self.keybinds.append(Keybind{ + try normal.keybinds.append(.{ .keysym = c.XKB_KEY_period, .modifiers = mod, .command = command.focusOutput, .arg = .{ .direction = .Next }, }); - try self.keybinds.append(Keybind{ + try normal.keybinds.append(.{ .keysym = c.XKB_KEY_comma, .modifiers = mod, .command = command.focusOutput, @@ -198,13 +194,13 @@ pub fn init(self: *Self, allocator: *std.mem.Allocator) !void { // Mod+Shift+Period/Comma to send the focused view to the the // next/previous output - try self.keybinds.append(Keybind{ + try normal.keybinds.append(.{ .keysym = c.XKB_KEY_period, .modifiers = mod | c.WLR_MODIFIER_SHIFT, .command = command.sendToOutput, .arg = .{ .direction = .Next }, }); - try self.keybinds.append(Keybind{ + try normal.keybinds.append(.{ .keysym = c.XKB_KEY_comma, .modifiers = mod | c.WLR_MODIFIER_SHIFT, .command = command.sendToOutput, @@ -212,12 +208,40 @@ pub fn init(self: *Self, allocator: *std.mem.Allocator) !void { }); // Mod+Space to toggle float - try self.keybinds.append(Keybind{ + try normal.keybinds.append(.{ .keysym = c.XKB_KEY_space, .modifiers = mod, .command = command.toggleFloat, .arg = .{ .none = {} }, }); + // Mod+F11 to enter passthrough mode + try normal.keybinds.append(.{ + .keysym = c.XKB_KEY_F11, + .modifiers = mod, + .command = command.mode, + .arg = .{ .str = "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 = command.mode, + .arg = .{ .str = "normal" }, + }); + + // Float views with app_id "float" try self.float_filter.append("float"); } + +pub fn getMode(self: Self, name: []const u8) *Mode { + for (self.modes.items) |*mode| + if (std.mem.eql(u8, mode.name, name)) return mode; + Log.Error.log("Mode '{}' does not exist, entering normal mode", .{name}); + for (self.modes.items) |*mode| + if (std.mem.eql(u8, mode.name, "normal")) return mode; + unreachable; +} diff --git a/src/Keybind.zig b/src/Keybind.zig new file mode 100644 index 0000000..9a156ad --- /dev/null +++ b/src/Keybind.zig @@ -0,0 +1,24 @@ +// 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 . + +const c = @import("c.zig"); +const command = @import("command.zig"); + +keysym: c.xkb_keysym_t, +modifiers: u32, +command: command.Command, +arg: command.Arg, diff --git a/src/Mode.zig b/src/Mode.zig new file mode 100644 index 0000000..9210db6 --- /dev/null +++ b/src/Mode.zig @@ -0,0 +1,42 @@ +// 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 . + +const Self = @This(); + +const std = @import("std"); + +const Keybind = @import("Keybind.zig"); + +/// The name of the mode +name: []const u8, + +/// The list of active keybindings for this mode +keybinds: std.ArrayList(Keybind), + +pub fn init(name: []const u8, allocator: *std.mem.Allocator) !Self { + const owned_name = try std.mem.dupe(allocator, u8, name); + return Self{ + .name = owned_name, + .keybinds = std.ArrayList(Keybind).init(allocator), + }; +} + +pub fn deinit(self: Self) void { + const allocator = self.keybinds.allocator; + allocator.free(self.name); + self.keybinds.deinit(); +} diff --git a/src/Seat.zig b/src/Seat.zig index 4335c00..c697010 100644 --- a/src/Seat.zig +++ b/src/Seat.zig @@ -25,6 +25,7 @@ const Cursor = @import("Cursor.zig"); const InputManager = @import("InputManager.zig"); const Keyboard = @import("Keyboard.zig"); const LayerSurface = @import("LayerSurface.zig"); +const Mode = @import("Mode.zig"); const Output = @import("Output.zig"); const View = @import("View.zig"); const ViewStack = @import("view_stack.zig").ViewStack; @@ -44,6 +45,9 @@ cursor: Cursor, /// Mulitple keyboards are handled separately keyboards: std.TailQueue(Keyboard), +/// Current keybind mode +mode: *Mode, + /// Currently focused output, may be the noop output if no focused_output: *Output, @@ -72,6 +76,8 @@ pub fn init(self: *Self, input_manager: *InputManager, name: []const u8) !void { self.keyboards = std.TailQueue(Keyboard).init(); + self.mode = input_manager.server.config.getMode("normal"); + self.focused_output = &self.input_manager.server.root.noop_output; self.focused_view = null; @@ -243,7 +249,7 @@ pub fn handleViewUnmap(self: *Self, view: *View) void { /// Handle any user-defined keybinding for the passed keysym and modifiers /// Returns true if the key was handled pub fn handleKeybinding(self: *Self, keysym: c.xkb_keysym_t, modifiers: u32) bool { - for (self.input_manager.server.config.keybinds.items) |keybind| { + for (self.mode.keybinds.items) |keybind| { if (modifiers == keybind.modifiers and keysym == keybind.keysym) { // Execute the bound command keybind.command(self, keybind.arg); diff --git a/src/Server.zig b/src/Server.zig index b8d1877..69ec78e 100644 --- a/src/Server.zig +++ b/src/Server.zig @@ -115,11 +115,11 @@ pub fn init(self: *Self, allocator: *std.mem.Allocator) !void { c.wl_signal_add(&self.wlr_xwayland.events.new_surface, &self.listen_new_xwayland_surface); } + try self.config.init(self.allocator); try self.decoration_manager.init(self); try self.root.init(self); // Must be called after root is initialized try self.input_manager.init(self); - try self.config.init(self.allocator); // These all free themselves when the wl_display is destroyed _ = c.wlr_data_device_manager_create(self.wl_display) orelse diff --git a/src/command.zig b/src/command.zig index f559315..95baf20 100644 --- a/src/command.zig +++ b/src/command.zig @@ -38,12 +38,13 @@ pub usingnamespace @import("command/exit_compositor.zig"); pub usingnamespace @import("command/focus_output.zig"); pub usingnamespace @import("command/focus_tags.zig"); pub usingnamespace @import("command/focus_view.zig"); +pub usingnamespace @import("command/mode.zig"); pub usingnamespace @import("command/modify_master_count.zig"); pub usingnamespace @import("command/modify_master_factor.zig"); pub usingnamespace @import("command/send_to_output.zig"); pub usingnamespace @import("command/set_view_tags.zig"); pub usingnamespace @import("command/spawn.zig"); +pub usingnamespace @import("command/toggle_float.zig"); pub usingnamespace @import("command/toggle_tags.zig"); pub usingnamespace @import("command/toggle_view_tags.zig"); pub usingnamespace @import("command/zoom.zig"); -pub usingnamespace @import("command/toggle_float.zig"); diff --git a/src/command/mode.zig b/src/command/mode.zig new file mode 100644 index 0000000..cf0d709 --- /dev/null +++ b/src/command/mode.zig @@ -0,0 +1,26 @@ +// 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 . + +const Arg = @import("../command.zig").Arg; +const Seat = @import("../Seat.zig"); + +/// Switch to the given mode +pub fn mode(seat: *Seat, arg: Arg) void { + const mode_name = arg.str; + const config = seat.input_manager.server.config; + seat.mode = config.getMode(mode_name); +}