river/river/KeycodeSet.zig
tiosgz 1e3b8ed161 Seat: avoid leaking eaten keys to client on focus
Until now, Seat.setFocusRaw sent all pressed keys to the client,
including ones that should be eaten. (Try e.g. changing focus to a
nested wlroots compositor with a terminal open to easily see it.)

However, only filtering out the eaten keys is not enough; they were
eaten only once all mappings had been executed. Therefore, the original
function had to be split into one looking up mappings and another
executing them.
2022-05-29 23:12:21 +02:00

58 lines
1.7 KiB
Zig

// 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 Self = @This();
const std = @import("std");
const assert = std.debug.assert;
const log = std.log.scoped(.keyboard);
const wlr = @import("wlroots");
items: [32]u32 = undefined,
len: usize = 0,
pub fn add(self: *Self, new: u32) void {
for (self.items[0..self.len]) |item| if (new == item) return;
comptime assert(@typeInfo(std.meta.fieldInfo(Self, .items).field_type).Array.len ==
@typeInfo(std.meta.fieldInfo(wlr.Keyboard, .keycodes).field_type).Array.len);
if (self.len == self.items.len) {
log.err("KeycodeSet limit reached, code {d} omitted", .{new});
return;
}
self.items[self.len] = new;
self.len += 1;
}
pub fn remove(self: *Self, old: u32) bool {
for (self.items[0..self.len]) |item, idx| if (old == item) {
self.len -= 1;
if (self.len > 0) self.items[idx] = self.items[self.len];
return true;
};
return false;
}
/// Removes other's contents from self (if present)
pub fn subtract(self: *Self, other: Self) void {
for (other.items[0..other.len]) |item| _ = self.remove(item);
}