river/river/PointerMapping.zig

75 lines
2.1 KiB
Zig
Raw Normal View History

// 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 Self = @This();
const std = @import("std");
2022-11-25 04:57:35 -08:00
const assert = std.debug.assert;
const wlr = @import("wlroots");
const util = @import("util.zig");
2022-11-25 04:57:35 -08:00
pub const Action = union(enum) {
move: void,
resize: void,
command: []const [:0]const u8,
};
event_code: u32,
modifiers: wlr.Keyboard.ModifierMask,
action: Action,
2022-11-25 04:57:35 -08:00
/// 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,
2022-11-25 04:57:35 -08:00
action_type: std.meta.Tag(Action),
command_args: []const [:0]const u8,
) !Self {
2022-11-25 04:57:35 -08:00
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) {
2022-11-25 04:57:35 -08:00
.move => .move,
.resize => .resize,
.command => blk: {
const arena_allocator = arena.allocator();
2022-11-25 04:57:35 -08:00
const owned_args = try arena_allocator.alloc([:0]const u8, command_args.len);
for (command_args) |arg, i| {
owned_args[i] = try arena_allocator.dupeZ(u8, arg);
}
2022-11-25 04:57:35 -08:00
break :blk .{ .command = owned_args };
},
};
return Self{
.event_code = event_code,
.modifiers = modifiers,
.action = action,
2022-11-25 04:57:35 -08:00
.arena_state = arena.state,
};
}
pub fn deinit(self: *Self) void {
2022-11-25 04:57:35 -08:00
self.arena_state.promote(util.gpa).deinit();
self.* = undefined;
}