2020-08-24 05:52:47 -07:00
|
|
|
// This file is part of river, a dynamic tiling wayland compositor.
|
|
|
|
//
|
2020-11-11 11:30:21 -08:00
|
|
|
// Copyright 2020 The River Developers
|
2020-08-24 05:52:47 -07:00
|
|
|
//
|
|
|
|
// 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
|
2022-01-31 10:33:22 -08:00
|
|
|
// the Free Software Foundation, version 3.
|
2020-08-24 05:52:47 -07:00
|
|
|
//
|
|
|
|
// 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/>.
|
|
|
|
|
2022-09-12 19:05:21 -07:00
|
|
|
const Self = @This();
|
|
|
|
|
|
|
|
const std = @import("std");
|
2022-11-25 04:57:35 -08:00
|
|
|
const assert = std.debug.assert;
|
2020-11-03 15:23:21 -08:00
|
|
|
const wlr = @import("wlroots");
|
|
|
|
|
2022-09-12 19:05:21 -07:00
|
|
|
const util = @import("util.zig");
|
|
|
|
|
2022-11-25 04:57:35 -08:00
|
|
|
pub const Action = union(enum) {
|
2022-09-12 19:05:21 -07:00
|
|
|
move: void,
|
|
|
|
resize: void,
|
|
|
|
command: []const [:0]const u8,
|
2020-08-24 05:52:47 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
event_code: u32,
|
2020-11-03 15:23:21 -08:00
|
|
|
modifiers: wlr.Keyboard.ModifierMask,
|
2020-08-24 05:52:47 -07:00
|
|
|
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,
|
2022-09-12 19:05:21 -07:00
|
|
|
|
|
|
|
pub fn init(
|
|
|
|
event_code: u32,
|
|
|
|
modifiers: wlr.Keyboard.ModifierMask,
|
2022-11-25 04:57:35 -08:00
|
|
|
action_type: std.meta.Tag(Action),
|
2022-09-12 19:05:21 -07:00
|
|
|
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);
|
2022-09-12 19:05:21 -07:00
|
|
|
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-09-12 19:05:21 -07:00
|
|
|
|
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-09-12 19:05:21 -07:00
|
|
|
}
|
|
|
|
|
2022-11-25 04:57:35 -08:00
|
|
|
break :blk .{ .command = owned_args };
|
2022-09-12 19:05:21 -07:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
return Self{
|
|
|
|
.event_code = event_code,
|
|
|
|
.modifiers = modifiers,
|
|
|
|
.action = action,
|
2022-11-25 04:57:35 -08:00
|
|
|
.arena_state = arena.state,
|
2022-09-12 19:05:21 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn deinit(self: *Self) void {
|
2022-11-25 04:57:35 -08:00
|
|
|
self.arena_state.promote(util.gpa).deinit();
|
|
|
|
self.* = undefined;
|
2022-09-12 19:05:21 -07:00
|
|
|
}
|