2020-03-22 14:42:55 -07:00
|
|
|
const std = @import("std");
|
2020-03-29 10:36:15 -07:00
|
|
|
const c = @import("c.zig");
|
2020-03-22 14:42:55 -07:00
|
|
|
|
2020-04-06 13:23:30 -07:00
|
|
|
const Log = @import("log.zig").Log;
|
2020-03-23 08:50:20 -07:00
|
|
|
const Seat = @import("seat.zig").Seat;
|
|
|
|
|
|
|
|
pub const Keyboard = struct {
|
2020-03-24 12:48:38 -07:00
|
|
|
const Self = @This();
|
|
|
|
|
2020-03-22 14:42:55 -07:00
|
|
|
seat: *Seat,
|
|
|
|
device: *c.wlr_input_device,
|
2020-03-24 12:03:48 -07:00
|
|
|
wlr_keyboard: *c.wlr_keyboard,
|
2020-03-22 14:42:55 -07:00
|
|
|
|
|
|
|
listen_key: c.wl_listener,
|
2020-04-18 03:21:43 -07:00
|
|
|
listen_modifiers: c.wl_listener,
|
2020-03-22 14:42:55 -07:00
|
|
|
|
2020-03-24 12:48:38 -07:00
|
|
|
pub fn init(self: *Self, seat: *Seat, device: *c.wlr_input_device) !void {
|
2020-03-23 13:51:46 -07:00
|
|
|
self.seat = seat;
|
|
|
|
self.device = device;
|
2020-04-09 03:54:38 -07:00
|
|
|
self.wlr_keyboard = device.unnamed_133.keyboard;
|
2020-03-22 14:42:55 -07:00
|
|
|
|
|
|
|
// We need to prepare an XKB keymap and assign it to the keyboard. This
|
|
|
|
// assumes the defaults (e.g. layout = "us").
|
|
|
|
const rules = c.xkb_rule_names{
|
|
|
|
.rules = null,
|
|
|
|
.model = null,
|
|
|
|
.layout = null,
|
|
|
|
.variant = null,
|
|
|
|
.options = null,
|
|
|
|
};
|
2020-03-23 13:51:46 -07:00
|
|
|
const context = c.xkb_context_new(c.enum_xkb_context_flags.XKB_CONTEXT_NO_FLAGS) orelse
|
|
|
|
return error.CantCreateXkbContext;
|
2020-03-22 14:42:55 -07:00
|
|
|
defer c.xkb_context_unref(context);
|
|
|
|
|
2020-03-23 08:50:20 -07:00
|
|
|
const keymap = c.xkb_keymap_new_from_names(
|
2020-03-22 14:42:55 -07:00
|
|
|
context,
|
|
|
|
&rules,
|
|
|
|
c.enum_xkb_keymap_compile_flags.XKB_KEYMAP_COMPILE_NO_FLAGS,
|
2020-03-23 13:51:46 -07:00
|
|
|
) orelse
|
|
|
|
return error.CantCreateXkbKeymap;
|
2020-03-22 14:42:55 -07:00
|
|
|
defer c.xkb_keymap_unref(keymap);
|
|
|
|
|
2020-03-23 13:51:46 -07:00
|
|
|
// TODO: handle failure after https://github.com/swaywm/wlroots/pull/2081
|
2020-03-24 12:03:48 -07:00
|
|
|
c.wlr_keyboard_set_keymap(self.wlr_keyboard, keymap);
|
|
|
|
c.wlr_keyboard_set_repeat_info(self.wlr_keyboard, 25, 600);
|
2020-03-22 14:42:55 -07:00
|
|
|
|
|
|
|
// Setup listeners for keyboard events
|
2020-03-24 13:13:56 -07:00
|
|
|
self.listen_key.notify = handleKey;
|
2020-03-24 12:03:48 -07:00
|
|
|
c.wl_signal_add(&self.wlr_keyboard.events.key, &self.listen_key);
|
2020-03-22 14:42:55 -07:00
|
|
|
|
2020-04-18 03:21:43 -07:00
|
|
|
self.listen_modifiers.notify = handleModifiers;
|
|
|
|
c.wl_signal_add(&self.wlr_keyboard.events.modifiers, &self.listen_modifiers);
|
2020-03-22 14:42:55 -07:00
|
|
|
}
|
|
|
|
|
2020-03-24 13:13:56 -07:00
|
|
|
fn handleKey(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void {
|
2020-03-22 14:42:55 -07:00
|
|
|
// This event is raised when a key is pressed or released.
|
2020-04-21 07:29:17 -07:00
|
|
|
const self = @fieldParentPtr(Self, "listen_key", listener.?);
|
2020-03-22 14:42:55 -07:00
|
|
|
const event = @ptrCast(
|
|
|
|
*c.wlr_event_keyboard_key,
|
|
|
|
@alignCast(@alignOf(*c.wlr_event_keyboard_key), data),
|
|
|
|
);
|
|
|
|
|
2020-04-21 07:29:17 -07:00
|
|
|
const wlr_keyboard: *c.wlr_keyboard = self.device.unnamed_133.keyboard;
|
2020-03-22 14:42:55 -07:00
|
|
|
|
|
|
|
// Translate libinput keycode -> xkbcommon
|
2020-03-23 08:50:20 -07:00
|
|
|
const keycode = event.keycode + 8;
|
2020-04-02 04:44:24 -07:00
|
|
|
|
|
|
|
// Get a list of keysyms as xkb reports them
|
|
|
|
var translated_keysyms: ?[*]c.xkb_keysym_t = undefined;
|
|
|
|
const translated_keysyms_len = c.xkb_state_key_get_syms(
|
|
|
|
wlr_keyboard.xkb_state,
|
|
|
|
keycode,
|
|
|
|
&translated_keysyms,
|
|
|
|
);
|
|
|
|
|
|
|
|
// Get a list of keysyms ignoring modifiers (e.g. 1 instead of !)
|
|
|
|
// Important for bindings like Mod+Shift+1
|
|
|
|
var raw_keysyms: ?[*]c.xkb_keysym_t = undefined;
|
|
|
|
const layout_index = c.xkb_state_key_get_layout(wlr_keyboard.xkb_state, keycode);
|
|
|
|
const raw_keysyms_len = c.xkb_keymap_key_get_syms_by_level(
|
|
|
|
wlr_keyboard.keymap,
|
|
|
|
keycode,
|
|
|
|
layout_index,
|
|
|
|
0,
|
|
|
|
&raw_keysyms,
|
|
|
|
);
|
2020-03-22 14:42:55 -07:00
|
|
|
|
|
|
|
var handled = false;
|
2020-04-02 04:44:24 -07:00
|
|
|
// TODO: These modifiers aren't properly handled, see sway's code
|
2020-03-24 12:03:48 -07:00
|
|
|
const modifiers = c.wlr_keyboard_get_modifiers(wlr_keyboard);
|
2020-04-06 13:23:30 -07:00
|
|
|
if (event.state == c.enum_wlr_key_state.WLR_KEY_PRESSED) {
|
2020-03-22 14:42:55 -07:00
|
|
|
var i: usize = 0;
|
2020-04-02 04:44:24 -07:00
|
|
|
while (i < translated_keysyms_len) : (i += 1) {
|
2020-04-21 07:29:17 -07:00
|
|
|
if (self.handleBuiltinKeybind(translated_keysyms.?[i])) {
|
2020-04-06 13:23:30 -07:00
|
|
|
handled = true;
|
|
|
|
break;
|
2020-04-21 07:29:17 -07:00
|
|
|
} else if (self.seat.handleKeybinding(translated_keysyms.?[i], modifiers)) {
|
2020-04-02 04:44:24 -07:00
|
|
|
handled = true;
|
2020-03-22 14:42:55 -07:00
|
|
|
break;
|
|
|
|
}
|
2020-04-02 04:44:24 -07:00
|
|
|
}
|
|
|
|
if (!handled) {
|
|
|
|
i = 0;
|
|
|
|
while (i < raw_keysyms_len) : (i += 1) {
|
2020-04-21 07:29:17 -07:00
|
|
|
if (self.handleBuiltinKeybind(raw_keysyms.?[i])) {
|
2020-04-06 13:23:30 -07:00
|
|
|
handled = true;
|
|
|
|
break;
|
2020-04-21 07:29:17 -07:00
|
|
|
} else if (self.seat.handleKeybinding(raw_keysyms.?[i], modifiers)) {
|
2020-04-02 04:44:24 -07:00
|
|
|
handled = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-03-22 14:42:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!handled) {
|
|
|
|
// Otherwise, we pass it along to the client.
|
2020-04-21 07:29:17 -07:00
|
|
|
const wlr_seat = self.seat.wlr_seat;
|
|
|
|
c.wlr_seat_set_keyboard(wlr_seat, self.device);
|
2020-03-22 14:42:55 -07:00
|
|
|
c.wlr_seat_keyboard_notify_key(
|
2020-03-23 08:50:20 -07:00
|
|
|
wlr_seat,
|
|
|
|
event.time_msec,
|
|
|
|
event.keycode,
|
|
|
|
@intCast(u32, @enumToInt(event.state)),
|
2020-03-22 14:42:55 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2020-04-06 13:23:30 -07:00
|
|
|
|
2020-04-18 03:21:43 -07:00
|
|
|
fn handleModifiers(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void {
|
|
|
|
// This event is raised when a modifier key, such as shift or alt, is
|
|
|
|
// pressed. We simply communicate this to the client. */
|
2020-04-21 07:29:17 -07:00
|
|
|
const self = @fieldParentPtr(Self, "listen_modifiers", listener.?);
|
2020-04-18 03:21:43 -07:00
|
|
|
|
|
|
|
// A seat can only have one keyboard, but this is a limitation of the
|
|
|
|
// Wayland protocol - not wlroots. We assign all connected keyboards to the
|
|
|
|
// same seat. You can swap out the underlying wlr_keyboard like this and
|
|
|
|
// wlr_seat handles this transparently.
|
2020-04-21 07:29:17 -07:00
|
|
|
c.wlr_seat_set_keyboard(self.seat.wlr_seat, self.device);
|
2020-04-18 03:21:43 -07:00
|
|
|
|
|
|
|
// Send modifiers to the client.
|
|
|
|
c.wlr_seat_keyboard_notify_modifiers(
|
2020-04-21 07:29:17 -07:00
|
|
|
self.seat.wlr_seat,
|
|
|
|
&self.wlr_keyboard.modifiers,
|
2020-04-18 03:21:43 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-04-06 13:23:30 -07:00
|
|
|
/// Handle any builtin, harcoded compsitor bindings such as VT switching.
|
|
|
|
/// Returns true if the keysym was handled.
|
|
|
|
fn handleBuiltinKeybind(self: Self, keysym: c.xkb_keysym_t) bool {
|
|
|
|
if (keysym >= c.XKB_KEY_XF86Switch_VT_1 and keysym <= c.XKB_KEY_XF86Switch_VT_12) {
|
|
|
|
Log.Debug.log("Switch VT keysym received", .{});
|
2020-04-12 13:19:48 -07:00
|
|
|
const wlr_backend = self.seat.input_manager.server.wlr_backend;
|
2020-04-06 13:23:30 -07:00
|
|
|
if (c.river_wlr_backend_is_multi(wlr_backend)) {
|
|
|
|
if (c.river_wlr_backend_get_session(wlr_backend)) |session| {
|
|
|
|
const vt = keysym - c.XKB_KEY_XF86Switch_VT_1 + 1;
|
|
|
|
Log.Debug.log("Switching to VT {}", .{vt});
|
|
|
|
_ = c.wlr_session_change_vt(session, vt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2020-03-22 14:42:55 -07:00
|
|
|
};
|