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

commit1238a2bd7293c78d24f037de3fc236d531414bc2
parent85ba4f9f27
authorIsaac Freund <[email protected]>
date2024-06-28 11:44
river: remove modes

 river/Config.zig         | 39 ++++++++-----------------
 river/Cursor.zig         | 18 ++----------
 river/LockManager.zig    |  7 -----
 river/Mapping.zig        | 26 -----------------
 river/Mode.zig           | 38 -------------------------
 river/PointerMapping.zig | 74 ------------------------------------------------
 river/Seat.zig           | 21 ++++----------
 river/SwitchMapping.zig  | 47 ------------------------------
 8 files changed, 20 insertions(+), 250 deletions(-)

diff --git a/river/Config.zig b/river/Config.zig
index 0ac46c0..0e7d015 100644
--- a/river/Config.zig
+++ b/river/Config.zig
@@ -20,6 +20,7 @@ const std = @import("std");
 const fmt = std.fmt;
 const mem = std.mem;
 const globber = @import("globber");
+const wlr = @import("wlroots");
 const xkb = @import("xkbcommon");
 
 const server = &@import("main.zig").server;
@@ -27,7 +28,8 @@ const util = @import("util.zig");
 
 const Server = @import("Server.zig");
 const Output = @import("Output.zig");
-const Mode = @import("Mode.zig");
+const Mapping = @import("Mapping.zig");
+const Switch = @import("Switch.zig");
 const View = @import("View.zig");
 
 pub const Position = struct {
@@ -49,12 +51,15 @@ border_width: u31 = 2,
 /// Color of border in RGBA with premultiplied alpha
 border_color: [4]f32 = [_]f32{ 0.34509804, 0.43137255, 0.45882353, 1.0 }, // Solarized base01
 
-/// Map of keymap mode name to mode id
-/// Does not own the string keys. They are owned by the corresponding Mode struct.
-mode_to_id: std.StringHashMap(u32),
-
-/// All user-defined keymap modes, indexed by mode id
-modes: std.ArrayListUnmanaged(Mode),
+mappings: std.ArrayListUnmanaged(Mapping) = .{},
+pointer_mappings: std.ArrayListUnmanaged(struct {
+    event_code: u32,
+    modifiers: wlr.Keyboard.ModifierMask,
+}) = .{},
+switch_mappings: std.ArrayListUnmanaged(struct {
+    switch_type: Switch.Type,
+    switch_state: Switch.State,
+}) = .{},
 
 /// Keyboard repeat rate in characters per second
 repeat_rate: u31 = 25,
@@ -76,35 +81,15 @@ pub fn init() !Config {
     defer keymap.unref();
 
     var config = Config{
-        .mode_to_id = std.StringHashMap(u32).init(util.gpa),
-        .modes = try std.ArrayListUnmanaged(Mode).initCapacity(util.gpa, 2),
         .xkb_context = xkb_context.ref(),
         .keymap = keymap.ref(),
     };
     errdefer config.deinit();
 
-    // Start with two empty modes, "normal" and "locked"
-    {
-        // Normal mode, id 0
-        const owned_slice = try util.gpa.dupeZ(u8, "normal");
-        try config.mode_to_id.putNoClobber(owned_slice, 0);
-        config.modes.appendAssumeCapacity(.{ .name = owned_slice });
-    }
-    {
-        // Locked mode, id 1
-        const owned_slice = try util.gpa.dupeZ(u8, "locked");
-        try config.mode_to_id.putNoClobber(owned_slice, 1);
-        config.modes.appendAssumeCapacity(.{ .name = owned_slice });
-    }
-
     return config;
 }
 
 pub fn deinit(config: *Config) void {
-    config.mode_to_id.deinit();
-    for (config.modes.items) |*mode| mode.deinit();
-    config.modes.deinit(util.gpa);
-
     config.keymap.unref();
     config.xkb_context.unref();
 }
diff --git a/river/Cursor.zig b/river/Cursor.zig
index 95b6acb..84d357a 100644
--- a/river/Cursor.zig
+++ b/river/Cursor.zig
@@ -640,25 +640,13 @@ fn handleTabletToolButton(
 
 /// Handle the mapping for the passed button if any. Returns true if there
 /// was a mapping and the button was handled.
-fn handlePointerMapping(cursor: *Cursor, event: *wlr.Pointer.event.Button, view: *View) bool {
+fn handlePointerMapping(cursor: *Cursor, event: *wlr.Pointer.event.Button, _: *View) bool {
     const wlr_keyboard = cursor.seat.wlr_seat.getKeyboard() orelse return false;
     const modifiers = wlr_keyboard.getModifiers();
 
-    const fullscreen = view.current.fullscreen or view.pending.fullscreen;
-
-    return for (server.config.modes.items[cursor.seat.mode_id].pointer_mappings.items) |mapping| {
+    return for (server.config.pointer_mappings.items) |mapping| {
         if (event.button == mapping.event_code and std.meta.eql(modifiers, mapping.modifiers)) {
-            switch (mapping.action) {
-                .move => if (!fullscreen) cursor.startMove(view),
-                .resize => if (!fullscreen) cursor.startResize(view, null),
-                .command => |_| {
-                    cursor.seat.focus(view);
-                    // This is mildly inefficient as running the command may have already
-                    // started a transaction. However we need to start one after the Seat.focus()
-                    // call in the case where it didn't.
-                    server.root.applyPending();
-                },
-            }
+            // trigger action
             break true;
         }
     } else false;
diff --git a/river/LockManager.zig b/river/LockManager.zig
index efd1efb..85332e1 100644
--- a/river/LockManager.zig
+++ b/river/LockManager.zig
@@ -114,10 +114,6 @@ fn handleLock(listener: *wl.Listener(*wlr.SessionLockV1), lock: *wlr.SessionLock
             while (it) |node| : (it = node.next) {
                 const seat = &node.data;
                 seat.setFocusRaw(.none);
-
-                // Enter locked mode
-                seat.prev_mode_id = seat.mode_id;
-                seat.enterMode(1);
             }
         }
     } else {
@@ -217,9 +213,6 @@ fn handleUnlock(listener: *wl.Listener(void)) void {
         while (it) |node| : (it = node.next) {
             const seat = &node.data;
             seat.setFocusRaw(.none);
-
-            // Exit locked mode
-            seat.enterMode(seat.prev_mode_id);
         }
     }
 
diff --git a/river/Mapping.zig b/river/Mapping.zig
index 48b62cd..f38e2d4 100644
--- a/river/Mapping.zig
+++ b/river/Mapping.zig
@@ -24,7 +24,6 @@ const util = @import("util.zig");
 
 keysym: xkb.Keysym,
 modifiers: wlr.Keyboard.ModifierMask,
-command_args: []const [:0]const u8,
 options: Options,
 
 pub const Options = struct {
@@ -37,31 +36,6 @@ pub const Options = struct {
     layout_index: ?u32,
 };
 
-pub fn init(
-    keysym: xkb.Keysym,
-    modifiers: wlr.Keyboard.ModifierMask,
-    command_args: []const []const u8,
-    options: Options,
-) !Mapping {
-    const owned_args = try util.gpa.alloc([:0]u8, command_args.len);
-    errdefer util.gpa.free(owned_args);
-    for (command_args, 0..) |arg, i| {
-        errdefer for (owned_args[0..i]) |a| util.gpa.free(a);
-        owned_args[i] = try util.gpa.dupeZ(u8, arg);
-    }
-    return Mapping{
-        .keysym = keysym,
-        .modifiers = modifiers,
-        .command_args = owned_args,
-        .options = options,
-    };
-}
-
-pub fn deinit(mapping: Mapping) void {
-    for (mapping.command_args) |arg| util.gpa.free(arg);
-    util.gpa.free(mapping.command_args);
-}
-
 /// Compare mapping with given keycode, modifiers and keyboard state
 pub fn match(
     mapping: Mapping,
diff --git a/river/Mode.zig b/river/Mode.zig
deleted file mode 100644
index d0f70d2..0000000
--- a/river/Mode.zig
+++ /dev/null
@@ -1,38 +0,0 @@
-// This file is part of river, a dynamic tiling wayland compositor.
-//
-// Copyright 2020 The River Developers
-//
-// 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, version 3.
-//
-// 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 Mode = @This();
-
-const std = @import("std");
-const util = @import("util.zig");
-
-const Mapping = @import("Mapping.zig");
-const PointerMapping = @import("PointerMapping.zig");
-const SwitchMapping = @import("SwitchMapping.zig");
-
-name: [:0]const u8,
-mappings: std.ArrayListUnmanaged(Mapping) = .{},
-pointer_mappings: std.ArrayListUnmanaged(PointerMapping) = .{},
-switch_mappings: std.ArrayListUnmanaged(SwitchMapping) = .{},
-
-pub fn deinit(mode: *Mode) void {
-    util.gpa.free(mode.name);
-    for (mode.mappings.items) |m| m.deinit();
-    mode.mappings.deinit(util.gpa);
-    for (mode.pointer_mappings.items) |*m| m.deinit();
-    mode.pointer_mappings.deinit(util.gpa);
-    mode.switch_mappings.deinit(util.gpa);
-}
diff --git a/river/PointerMapping.zig b/river/PointerMapping.zig
deleted file mode 100644
index 441800f..0000000
--- a/river/PointerMapping.zig
+++ /dev/null
@@ -1,74 +0,0 @@
-// This file is part of river, a dynamic tiling wayland compositor.
-//
-// Copyright 2020 The River Developers
-//
-// 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, version 3.
-//
-// 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 PointerMapping = @This();
-
-const std = @import("std");
-const assert = std.debug.assert;
-const wlr = @import("wlroots");
-
-const util = @import("util.zig");
-
-pub const Action = union(enum) {
-    move: void,
-    resize: void,
-    command: []const [:0]const u8,
-};
-
-event_code: u32,
-modifiers: wlr.Keyboard.ModifierMask,
-action: Action,
-/// Owns the memory backing the arguments if action is a command.
-arena_state: std.heap.ArenaAllocator.State,
-
-pub fn init(
-    event_code: u32,
-    modifiers: wlr.Keyboard.ModifierMask,
-    action_type: std.meta.Tag(Action),
-    command_args: []const [:0]const u8,
-) !PointerMapping {
-    assert(action_type == .command or command_args.len == 1);
-
-    var arena = std.heap.ArenaAllocator.init(util.gpa);
-    errdefer arena.deinit();
-
-    const action: Action = switch (action_type) {
-        .move => .move,
-        .resize => .resize,
-        .command => blk: {
-            const arena_allocator = arena.allocator();
-
-            const owned_args = try arena_allocator.alloc([:0]const u8, command_args.len);
-            for (command_args, 0..) |arg, i| {
-                owned_args[i] = try arena_allocator.dupeZ(u8, arg);
-            }
-
-            break :blk .{ .command = owned_args };
-        },
-    };
-
-    return PointerMapping{
-        .event_code = event_code,
-        .modifiers = modifiers,
-        .action = action,
-        .arena_state = arena.state,
-    };
-}
-
-pub fn deinit(pointer_mapping: *PointerMapping) void {
-    pointer_mapping.arena_state.promote(util.gpa).deinit();
-    pointer_mapping.* = undefined;
-}
diff --git a/river/Seat.zig b/river/Seat.zig
index b5c160f..a730bd2 100644
--- a/river/Seat.zig
+++ b/river/Seat.zig
@@ -70,12 +70,6 @@ cursor: Cursor,
 /// Input Method handling
 relay: InputRelay,
 
-/// ID of the current keymap mode
-mode_id: u32 = 0,
-
-/// ID of previous keymap mode, used when returning from "locked" mode
-prev_mode_id: u32 = 0,
-
 /// Timer for repeating keyboard mappings
 mapping_repeat_timer: *wl.EventSource,
 
@@ -327,10 +321,6 @@ pub fn handleActivity(seat: Seat) void {
     server.input_manager.idle_notifier.notifyActivity(seat.wlr_seat);
 }
 
-pub fn enterMode(seat: *Seat, mode_id: u32) void {
-    seat.mode_id = mode_id;
-}
-
 /// Handle any user-defined mapping for passed keycode, modifiers and keyboard state
 /// Returns true if a mapping was run
 pub fn handleMapping(
@@ -340,7 +330,7 @@ pub fn handleMapping(
     released: bool,
     xkb_state: *xkb.State,
 ) bool {
-    const modes = &server.config.modes;
+    const mappings = &server.config.mappings;
 
     // It is possible for more than one mapping to be matched due to the
     // existence of layout-independent mappings. It is also possible due to
@@ -355,7 +345,7 @@ pub fn handleMapping(
     // That is, if the physical keys Mod+Shift+1 are pressed on a US layout don't
     // translate the keysym 1 to an exclamation mark. This behavior is generally
     // what is desired.
-    for (modes.items[seat.mode_id].mappings.items) |*mapping| {
+    for (mappings.items) |*mapping| {
         if (mapping.match(keycode, modifiers, released, xkb_state, .no_translate)) {
             if (found == null) {
                 found = mapping;
@@ -369,7 +359,7 @@ pub fn handleMapping(
     // with xkbcommon for intuitive behavior. For example, layouts may require
     // translation with the numlock modifier to obtain keypad number keysyms
     // (e.g. KP_1).
-    for (modes.items[seat.mode_id].mappings.items) |*mapping| {
+    for (mappings.items) |*mapping| {
         if (mapping.match(keycode, modifiers, released, xkb_state, .translate)) {
             if (found == null) {
                 found = mapping;
@@ -396,12 +386,11 @@ pub fn handleMapping(
 
 /// Handle any user-defined mapping for switches
 pub fn handleSwitchMapping(
-    seat: *Seat,
+    _: *Seat,
     switch_type: Switch.Type,
     switch_state: Switch.State,
 ) void {
-    const modes = &server.config.modes;
-    for (modes.items[seat.mode_id].switch_mappings.items) |mapping| {
+    for (server.config.switch_mappings.items) |mapping| {
         if (std.meta.eql(mapping.switch_type, switch_type) and std.meta.eql(mapping.switch_state, switch_state)) {
             // send trigger
         }
diff --git a/river/SwitchMapping.zig b/river/SwitchMapping.zig
deleted file mode 100644
index 36fcee9..0000000
--- a/river/SwitchMapping.zig
+++ /dev/null
@@ -1,47 +0,0 @@
-// This file is part of river, a dynamic tiling wayland compositor.
-//
-// Copyright 2022 The River Developers
-//
-// 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, version 3.
-//
-// 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 SwitchMapping = @This();
-
-const Switch = @import("Switch.zig");
-const util = @import("util.zig");
-
-switch_type: Switch.Type,
-switch_state: Switch.State,
-command_args: []const [:0]const u8,
-
-pub fn init(
-    switch_type: Switch.Type,
-    switch_state: Switch.State,
-    command_args: []const []const u8,
-) !SwitchMapping {
-    const owned_args = try util.gpa.alloc([:0]u8, command_args.len);
-    errdefer util.gpa.free(owned_args);
-    for (command_args, 0..) |arg, i| {
-        errdefer for (owned_args[0..i]) |a| util.gpa.free(a);
-        owned_args[i] = try util.gpa.dupeZ(u8, arg);
-    }
-    return SwitchMapping{
-        .switch_type = switch_type,
-        .switch_state = switch_state,
-        .command_args = owned_args,
-    };
-}
-
-pub fn deinit(mapping: SwitchMapping) void {
-    for (mapping.command_args) |arg| util.gpa.free(arg);
-    util.gpa.free(mapping.command_args);
-}