river: add keyboard groups

This commit is contained in:
Leon Henrik Plickat
2022-08-30 15:26:35 +02:00
parent c0e64829f0
commit 01f49bbbc1
10 changed files with 314 additions and 31 deletions
+66 -29
View File
@@ -25,13 +25,19 @@ const xkb = @import("xkbcommon");
const server = &@import("main.zig").server;
const util = @import("util.zig");
const KeycodeSet = @import("KeycodeSet.zig");
const Seat = @import("Seat.zig");
const InputDevice = @import("InputDevice.zig");
const KeyboardGroup = @import("KeyboardGroup.zig");
const KeycodeSet = @import("KeycodeSet.zig");
const log = std.log.scoped(.keyboard);
device: InputDevice,
pub const Provider = union(enum) {
device: InputDevice,
group: *KeyboardGroup,
};
provider: Provider,
/// Pressed keys for which a mapping was triggered on press
eaten_keycodes: KeycodeSet = .{},
@@ -40,11 +46,8 @@ key: wl.Listener(*wlr.Keyboard.event.Key) = wl.Listener(*wlr.Keyboard.event.Key)
modifiers: wl.Listener(*wlr.Keyboard) = wl.Listener(*wlr.Keyboard).init(handleModifiers),
pub fn init(self: *Self, seat: *Seat, wlr_device: *wlr.InputDevice) !void {
self.* = .{
.device = undefined,
};
try self.device.init(seat, wlr_device);
errdefer self.device.deinit();
const wlr_keyboard = wlr_device.device.keyboard;
wlr_keyboard.data = @ptrToInt(self);
const context = xkb.Context.new(.no_flags) orelse return error.XkbContextFailed;
defer context.unref();
@@ -54,34 +57,70 @@ pub fn init(self: *Self, seat: *Seat, wlr_device: *wlr.InputDevice) !void {
const keymap = xkb.Keymap.newFromNames(context, null, .no_flags) orelse return error.XkbKeymapFailed;
defer keymap.unref();
const wlr_keyboard = self.device.wlr_device.device.keyboard;
wlr_keyboard.data = @ptrToInt(self);
if (!wlr_keyboard.setKeymap(keymap)) return error.SetKeymapFailed;
wlr_keyboard.setRepeatInfo(server.config.repeat_rate, server.config.repeat_delay);
self.* = .{
.provider = undefined,
};
if (wlr.KeyboardGroup.fromKeyboard(wlr_keyboard)) |grp| {
const group = @intToPtr(*KeyboardGroup, grp.data);
self.provider = .{ .group = group };
} else {
self.provider = .{ .device = undefined };
try self.provider.device.init(seat, wlr_device);
}
wlr_keyboard.events.key.add(&self.key);
wlr_keyboard.events.modifiers.add(&self.modifiers);
wlr_keyboard.setRepeatInfo(server.config.repeat_rate, server.config.repeat_delay);
}
pub fn deinit(self: *Self) void {
self.key.link.remove();
self.modifiers.link.remove();
self.device.deinit();
switch (self.provider) {
.device => {
const wlr_keyboard = self.provider.device.wlr_device.device.keyboard;
if (wlr_keyboard.group) |group| group.removeKeyboard(wlr_keyboard);
self.provider.device.deinit();
},
.group => {},
}
self.* = undefined;
}
/// This event is raised when a key is pressed or released.
fn handleKey(listener: *wl.Listener(*wlr.Keyboard.event.Key), event: *wlr.Keyboard.event.Key) void {
// This event is raised when a key is pressed or released.
const self = @fieldParentPtr(Self, "key", listener);
const wlr_keyboard = self.device.wlr_device.device.keyboard;
switch (self.provider) {
.device => {
if (self.provider.device.wlr_device.device.keyboard.group != null) return;
self.handleKeyImpl(self.provider.device.seat, self.provider.device.wlr_device, event);
},
.group => self.handleKeyImpl(self.provider.group.seat, self.provider.group.group.input_device, event),
}
}
self.device.seat.handleActivity();
/// Simply pass modifiers along to the client
fn handleModifiers(listener: *wl.Listener(*wlr.Keyboard), _: *wlr.Keyboard) void {
const self = @fieldParentPtr(Self, "modifiers", listener);
switch (self.provider) {
.device => {
if (self.provider.device.wlr_device.device.keyboard.group != null) return;
self.handleModifiersImpl(self.provider.device.seat, self.provider.device.wlr_device);
},
.group => self.handleModifiersImpl(self.provider.group.seat, self.provider.group.group.input_device),
}
}
self.device.seat.clearRepeatingMapping();
fn handleKeyImpl(self: *Self, seat: *Seat, wlr_device: *wlr.InputDevice, event: *wlr.Keyboard.event.Key) void {
const wlr_keyboard = wlr_device.device.keyboard;
seat.handleActivity();
seat.clearRepeatingMapping();
// Translate libinput keycode -> xkbcommon
const keycode = event.keycode + 8;
@@ -98,7 +137,7 @@ fn handleKey(listener: *wl.Listener(*wlr.Keyboard.event.Key), event: *wlr.Keyboa
!released and
!isModifier(sym))
{
self.device.seat.cursor.hide();
seat.cursor.hide();
break;
}
}
@@ -109,11 +148,11 @@ fn handleKey(listener: *wl.Listener(*wlr.Keyboard.event.Key), event: *wlr.Keyboa
}
// Handle user-defined mappings
const mapped = self.device.seat.hasMapping(keycode, modifiers, released, xkb_state);
const mapped = seat.hasMapping(keycode, modifiers, released, xkb_state);
if (mapped) {
if (!released) self.eaten_keycodes.add(event.keycode);
const handled = self.device.seat.handleMapping(keycode, modifiers, released, xkb_state);
const handled = seat.handleMapping(keycode, modifiers, released, xkb_state);
assert(handled);
}
@@ -121,8 +160,8 @@ fn handleKey(listener: *wl.Listener(*wlr.Keyboard.event.Key), event: *wlr.Keyboa
if (!eaten) {
// If key was not handled, we pass it along to the client.
const wlr_seat = self.device.seat.wlr_seat;
wlr_seat.setKeyboard(self.device.wlr_device);
const wlr_seat = seat.wlr_seat;
wlr_seat.setKeyboard(wlr_device);
wlr_seat.keyboardNotifyKey(event.time_msec, event.keycode, event.state);
}
}
@@ -131,14 +170,6 @@ fn isModifier(keysym: xkb.Keysym) bool {
return @enumToInt(keysym) >= xkb.Keysym.Shift_L and @enumToInt(keysym) <= xkb.Keysym.Hyper_R;
}
/// Simply pass modifiers along to the client
fn handleModifiers(listener: *wl.Listener(*wlr.Keyboard), _: *wlr.Keyboard) void {
const self = @fieldParentPtr(Self, "modifiers", listener);
self.device.seat.wlr_seat.setKeyboard(self.device.wlr_device);
self.device.seat.wlr_seat.keyboardNotifyModifiers(&self.device.wlr_device.device.keyboard.modifiers);
}
/// Handle any builtin, harcoded compsitor mappings such as VT switching.
/// Returns true if the keysym was handled.
fn handleBuiltinMapping(keysym: xkb.Keysym) bool {
@@ -158,3 +189,9 @@ fn handleBuiltinMapping(keysym: xkb.Keysym) bool {
else => return false,
}
}
/// Simply pass modifiers along to the client
fn handleModifiersImpl(_: *Self, seat: *Seat, wlr_device: *wlr.InputDevice) void {
seat.wlr_seat.setKeyboard(wlr_device);
seat.wlr_seat.keyboardNotifyModifiers(&wlr_device.device.keyboard.modifiers);
}