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

commit5e0a0a48a86285245a5a85522e5aa333aa305347
parent155f2b8aba
authorIsaac Freund <[email protected]>
date2022-06-20 16:29
InputDevice: move to separate file

 river/InputConfig.zig  |  2 +-
 river/InputDevice.zig  | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++
 river/InputManager.zig | 53 +----------------------------------
 3 files changed, 77 insertions(+), 53 deletions(-)

diff --git a/river/InputConfig.zig b/river/InputConfig.zig
index 5fdf5f5..92b3974 100644
--- a/river/InputConfig.zig
+++ b/river/InputConfig.zig
@@ -27,7 +27,7 @@ const c = @import("c.zig");
 const server = &@import("main.zig").server;
 const util = @import("util.zig");
 
-const InputDevice = @import("InputManager.zig").InputDevice;
+const InputDevice = @import("InputDevice.zig");
 
 // TODO - keyboards
 //      - mapping to output / region
diff --git a/river/InputDevice.zig b/river/InputDevice.zig
new file mode 100644
index 0000000..5781a14
--- /dev/null
+++ b/river/InputDevice.zig
@@ -0,0 +1,75 @@
+// 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 InputDevice = @This();
+
+const std = @import("std");
+const mem = std.mem;
+const ascii = std.ascii;
+const wlr = @import("wlroots");
+const wl = @import("wayland").server.wl;
+
+const server = &@import("main.zig").server;
+const util = @import("util.zig");
+
+const log = std.log.scoped(.input_manager);
+
+device: *wlr.InputDevice,
+destroy: wl.Listener(*wlr.InputDevice) = wl.Listener(*wlr.InputDevice).init(handleDestroy),
+
+/// Careful: The identifier is not unique! A physical input device may have
+/// multiple logical input devices with the exact same vendor id, product id
+/// and name. However identifiers of InputConfigs are unique.
+identifier: []const u8,
+
+pub fn init(self: *InputDevice, device: *wlr.InputDevice) !void {
+    const identifier = try std.fmt.allocPrint(
+        util.gpa,
+        "{s}-{}-{}-{s}",
+        .{
+            @tagName(device.type),
+            device.vendor,
+            device.product,
+            mem.trim(u8, mem.span(device.name), &ascii.spaces),
+        },
+    );
+    for (identifier) |*char| {
+        if (char.* == ' ' or !ascii.isPrint(char.*)) {
+            char.* = '_';
+        }
+    }
+    self.* = .{
+        .device = device,
+        .identifier = identifier,
+    };
+    log.debug("new input device: {s}", .{self.identifier});
+    device.events.destroy.add(&self.destroy);
+}
+
+pub fn deinit(self: *InputDevice) void {
+    util.gpa.free(self.identifier);
+    self.destroy.link.remove();
+}
+
+fn handleDestroy(listener: *wl.Listener(*wlr.InputDevice), _: *wlr.InputDevice) void {
+    const self = @fieldParentPtr(InputDevice, "destroy", listener);
+    log.debug("removed input device: {s}", .{self.identifier});
+    self.deinit();
+
+    const node = @fieldParentPtr(std.TailQueue(InputDevice).Node, "data", self);
+    server.input_manager.input_devices.remove(node);
+    util.gpa.destroy(node);
+}
diff --git a/river/InputManager.zig b/river/InputManager.zig
index 3bcef06..f3d68a8 100644
--- a/river/InputManager.zig
+++ b/river/InputManager.zig
@@ -19,7 +19,6 @@ const Self = @This();
 const build_options = @import("build_options");
 const std = @import("std");
 const mem = std.mem;
-const ascii = std.ascii;
 const wlr = @import("wlroots");
 const wl = @import("wayland").server.wl;
 
@@ -27,64 +26,14 @@ const server = &@import("main.zig").server;
 const util = @import("util.zig");
 
 const InputConfig = @import("InputConfig.zig");
+const InputDevice = @import("InputDevice.zig");
 const Seat = @import("Seat.zig");
-const Server = @import("Server.zig");
-const View = @import("View.zig");
 const PointerConstraint = @import("PointerConstraint.zig");
 
 const default_seat_name = "default";
 
 const log = std.log.scoped(.input_manager);
 
-pub const InputDevice = struct {
-    device: *wlr.InputDevice,
-    destroy: wl.Listener(*wlr.InputDevice) = wl.Listener(*wlr.InputDevice).init(handleDestroy),
-
-    /// Careful: The identifier is not unique! A physical input device may have
-    /// multiple logical input devices with the exact same vendor id, product id
-    /// and name. However identifiers of InputConfigs are unique.
-    identifier: []const u8,
-
-    pub fn init(self: *InputDevice, device: *wlr.InputDevice) !void {
-        const identifier = try std.fmt.allocPrint(
-            util.gpa,
-            "{s}-{}-{}-{s}",
-            .{
-                @tagName(device.type),
-                device.vendor,
-                device.product,
-                mem.trim(u8, mem.span(device.name), &ascii.spaces),
-            },
-        );
-        for (identifier) |*char| {
-            if (char.* == ' ' or !ascii.isPrint(char.*)) {
-                char.* = '_';
-            }
-        }
-        self.* = .{
-            .device = device,
-            .identifier = identifier,
-        };
-        log.debug("new input device: {s}", .{self.identifier});
-        device.events.destroy.add(&self.destroy);
-    }
-
-    pub fn deinit(self: *InputDevice) void {
-        util.gpa.free(self.identifier);
-        self.destroy.link.remove();
-    }
-
-    fn handleDestroy(listener: *wl.Listener(*wlr.InputDevice), _: *wlr.InputDevice) void {
-        const self = @fieldParentPtr(InputDevice, "destroy", listener);
-        log.debug("removed input device: {s}", .{self.identifier});
-        self.deinit();
-
-        const node = @fieldParentPtr(std.TailQueue(InputDevice).Node, "data", self);
-        server.input_manager.input_devices.remove(node);
-        util.gpa.destroy(node);
-    }
-};
-
 new_input: wl.Listener(*wlr.InputDevice) = wl.Listener(*wlr.InputDevice).init(handleNewInput),
 
 idle: *wlr.Idle,