Files
river/river/Keyboard.zig
T

175 lines
6.0 KiB
Zig
Raw Normal View History

2020-05-02 19:21:10 +02:00
// This file is part of river, a dynamic tiling wayland compositor.
//
// Copyright 2020 The River Developers
2020-05-02 19:21:10 +02: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 19:33:22 +01:00
// the Free Software Foundation, version 3.
2020-05-02 19:21:10 +02: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/>.
2020-05-02 16:48:09 +02:00
const Self = @This();
2020-03-22 22:42:55 +01:00
const std = @import("std");
const assert = std.debug.assert;
const wlr = @import("wlroots");
const wl = @import("wayland").server.wl;
const xkb = @import("xkbcommon");
2020-05-02 16:48:09 +02:00
2021-05-13 14:53:08 +02:00
const server = &@import("main.zig").server;
2020-06-16 20:54:05 +02:00
const util = @import("util.zig");
2020-03-22 22:42:55 +01:00
const KeycodeSet = @import("KeycodeSet.zig");
2020-05-02 23:11:56 +02:00
const Seat = @import("Seat.zig");
2020-03-23 16:50:20 +01:00
2021-02-05 09:46:18 +01:00
const log = std.log.scoped(.keyboard);
2020-05-02 16:48:09 +02:00
seat: *Seat,
input_device: *wlr.InputDevice,
2020-03-24 20:48:38 +01:00
/// Pressed keys for which a mapping was triggered on press
eaten_keycodes: KeycodeSet = .{},
2020-12-31 15:35:35 +01:00
key: wl.Listener(*wlr.Keyboard.event.Key) = wl.Listener(*wlr.Keyboard.event.Key).init(handleKey),
modifiers: wl.Listener(*wlr.Keyboard) = wl.Listener(*wlr.Keyboard).init(handleModifiers),
destroy: wl.Listener(*wlr.Keyboard) = wl.Listener(*wlr.Keyboard).init(handleDestroy),
2020-03-22 22:42:55 +01:00
pub fn init(self: *Self, seat: *Seat, input_device: *wlr.InputDevice) !void {
2020-08-21 19:57:10 +02:00
self.* = .{
.seat = seat,
.input_device = input_device,
2020-08-21 19:57:10 +02:00
};
2020-03-22 22:42:55 +01:00
2020-05-02 16:48:09 +02:00
// We need to prepare an XKB keymap and assign it to the keyboard. This
// assumes the defaults (e.g. layout = "us").
const rules = xkb.RuleNames{
2020-05-02 16:48:09 +02:00
.rules = null,
.model = null,
.layout = null,
.variant = null,
.options = null,
};
const context = xkb.Context.new(.no_flags) orelse return error.XkbContextFailed;
defer context.unref();
2020-03-22 22:42:55 +01:00
const keymap = xkb.Keymap.newFromNames(context, &rules, .no_flags) orelse return error.XkbKeymapFailed;
defer keymap.unref();
2020-03-22 22:42:55 +01:00
const wlr_keyboard = self.input_device.device.keyboard;
wlr_keyboard.data = @ptrToInt(self);
2020-03-22 22:42:55 +01:00
if (!wlr_keyboard.setKeymap(keymap)) return error.SetKeymapFailed;
2021-05-13 14:53:08 +02:00
wlr_keyboard.setRepeatInfo(server.config.repeat_rate, server.config.repeat_delay);
2020-03-22 22:42:55 +01:00
wlr_keyboard.events.key.add(&self.key);
wlr_keyboard.events.modifiers.add(&self.modifiers);
wlr_keyboard.events.destroy.add(&self.destroy);
2020-11-29 21:05:27 +01:00
}
pub fn deinit(self: *Self) void {
self.key.link.remove();
self.modifiers.link.remove();
self.destroy.link.remove();
2020-05-02 16:48:09 +02:00
}
2020-03-22 22:42:55 +01:00
fn handleKey(listener: *wl.Listener(*wlr.Keyboard.event.Key), event: *wlr.Keyboard.event.Key) void {
2020-05-02 16:48:09 +02:00
// This event is raised when a key is pressed or released.
const self = @fieldParentPtr(Self, "key", listener);
const wlr_keyboard = self.input_device.device.keyboard;
2020-03-22 22:42:55 +01:00
2020-08-13 12:22:32 +02:00
self.seat.handleActivity();
self.seat.clearRepeatingMapping();
2020-05-02 16:48:09 +02:00
// Translate libinput keycode -> xkbcommon
const keycode = event.keycode + 8;
2020-03-22 22:42:55 +01:00
const modifiers = wlr_keyboard.getModifiers();
const released = event.state == .released;
2020-04-02 13:44:24 +02:00
2022-04-20 15:38:18 +02:00
const xkb_state = wlr_keyboard.xkb_state orelse return;
const keysyms = xkb_state.keyGetSyms(keycode);
2020-09-15 13:46:08 +02:00
2022-04-20 15:38:18 +02:00
// Hide cursor when typing
for (keysyms) |sym| {
if (server.config.cursor_hide_when_typing == .enabled and
!released and
!isModifier(sym))
{
self.seat.cursor.hide();
2020-09-15 13:46:08 +02:00
break;
2020-03-22 22:42:55 +01:00
}
2020-09-15 13:46:08 +02:00
}
2022-04-20 15:38:18 +02:00
// Handle builtin mapping, only when keys are pressed
for (keysyms) |sym| {
if (!released and handleBuiltinMapping(sym)) return;
2020-10-21 15:24:11 +02:00
}
2020-05-02 16:48:09 +02:00
// Handle user-defined mappings
const mapped = self.seat.hasMapping(keycode, modifiers, released, xkb_state);
if (mapped) {
if (!released) self.eaten_keycodes.add(event.keycode);
const handled = self.seat.handleMapping(keycode, modifiers, released, xkb_state);
assert(handled);
}
const eaten = if (released) self.eaten_keycodes.remove(event.keycode) else mapped;
if (!eaten) {
2022-04-20 15:38:18 +02:00
// If key was not handled, we pass it along to the client.
2020-05-02 16:48:09 +02:00
const wlr_seat = self.seat.wlr_seat;
wlr_seat.setKeyboard(self.input_device);
wlr_seat.keyboardNotifyKey(event.time_msec, event.keycode, event.state);
2020-05-02 16:48:09 +02:00
}
2022-02-28 00:39:10 +01:00
}
fn isModifier(keysym: xkb.Keysym) bool {
return @enumToInt(keysym) >= xkb.Keysym.Shift_L and @enumToInt(keysym) <= xkb.Keysym.Hyper_R;
2020-05-02 16:48:09 +02:00
}
/// Simply pass modifiers along to the client
2021-10-11 12:44:46 +02:00
fn handleModifiers(listener: *wl.Listener(*wlr.Keyboard), _: *wlr.Keyboard) void {
const self = @fieldParentPtr(Self, "modifiers", listener);
2020-05-02 16:48:09 +02:00
self.seat.wlr_seat.setKeyboard(self.input_device);
self.seat.wlr_seat.keyboardNotifyModifiers(&self.input_device.device.keyboard.modifiers);
2020-05-02 16:48:09 +02:00
}
2021-10-11 12:44:46 +02:00
fn handleDestroy(listener: *wl.Listener(*wlr.Keyboard), _: *wlr.Keyboard) void {
const self = @fieldParentPtr(Self, "destroy", listener);
2020-11-29 21:05:27 +01:00
const node = @fieldParentPtr(std.TailQueue(Self).Node, "data", self);
2020-11-29 21:05:27 +01:00
self.seat.keyboards.remove(node);
self.deinit();
2020-11-29 21:05:27 +01:00
util.gpa.destroy(node);
}
2020-05-02 16:48:09 +02:00
2020-06-01 15:16:18 +02:00
/// Handle any builtin, harcoded compsitor mappings such as VT switching.
2020-05-02 16:48:09 +02:00
/// Returns true if the keysym was handled.
2021-10-11 12:44:46 +02:00
fn handleBuiltinMapping(keysym: xkb.Keysym) bool {
switch (@enumToInt(keysym)) {
2021-10-11 12:44:46 +02:00
xkb.Keysym.XF86Switch_VT_1...xkb.Keysym.XF86Switch_VT_12 => {
2021-02-05 09:46:18 +01:00
log.debug("switch VT keysym received", .{});
2021-05-13 14:53:08 +02:00
if (server.backend.isMulti()) {
if (server.backend.getSession()) |session| {
2021-10-11 12:44:46 +02:00
const vt = @enumToInt(keysym) - xkb.Keysym.XF86Switch_VT_1 + 1;
2021-02-05 09:46:18 +01:00
const log_server = std.log.scoped(.server);
2021-10-11 12:44:46 +02:00
log_server.info("switching to VT {}", .{vt});
2021-02-05 09:46:18 +01:00
session.changeVt(vt) catch log_server.err("changing VT failed", .{});
}
2020-05-02 16:48:09 +02:00
}
return true;
},
else => return false,
2020-05-02 16:48:09 +02:00
}
}