mirror of
https://github.com/MezzalunaWM/Mezzaluna.git
synced 2026-03-07 19:49:53 -05:00
keyboard repeat for compositor keybinds
This commit is contained in:
parent
46a359b07e
commit
f639a2f94e
6 changed files with 122 additions and 28 deletions
|
|
@ -81,7 +81,7 @@ pub fn processCursorMotion(self: *Cursor, time_msec: u32) void {
|
||||||
var passthrough = true;
|
var passthrough = true;
|
||||||
|
|
||||||
if (self.mode == .drag) {
|
if (self.mode == .drag) {
|
||||||
const modifiers = server.seat.keyboard_group.keyboard.getModifiers();
|
const modifiers = server.seat.keyboard_group.wlr_group.keyboard.getModifiers();
|
||||||
|
|
||||||
std.debug.assert(self.drag != null);
|
std.debug.assert(self.drag != null);
|
||||||
|
|
||||||
|
|
@ -200,7 +200,7 @@ fn handleButton(
|
||||||
}
|
}
|
||||||
|
|
||||||
var passthrough = true;
|
var passthrough = true;
|
||||||
const modifiers = server.seat.keyboard_group.keyboard.getModifiers();
|
const modifiers = server.seat.keyboard_group.wlr_group.keyboard.getModifiers();
|
||||||
|
|
||||||
// Proceed if mousemap for current mouse and modifier state's exist
|
// Proceed if mousemap for current mouse and modifier state's exist
|
||||||
if (server.mousemaps.get(Mousemap.hash(modifiers, @bitCast(event.button)))) |map| {
|
if (server.mousemaps.get(Mousemap.hash(modifiers, @bitCast(event.button)))) |map| {
|
||||||
|
|
|
||||||
|
|
@ -53,7 +53,7 @@ pub fn init(device: *wlr.InputDevice) *Keyboard {
|
||||||
self.wlr_keyboard.data = self;
|
self.wlr_keyboard.data = self;
|
||||||
|
|
||||||
std.log.err("Adding new keyboard {s}", .{device.name orelse "(unnamed)"});
|
std.log.err("Adding new keyboard {s}", .{device.name orelse "(unnamed)"});
|
||||||
if(!server.seat.keyboard_group.addKeyboard(self.wlr_keyboard)) {
|
if(!server.seat.keyboard_group.wlr_group.addKeyboard(self.wlr_keyboard)) {
|
||||||
std.log.err("Adding new keyboard {s} failed", .{device.name orelse "(unnamed)"});
|
std.log.err("Adding new keyboard {s} failed", .{device.name orelse "(unnamed)"});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -71,32 +71,62 @@ fn handleModifiers(_: *wl.Listener(*wlr.Keyboard), wlr_keyboard: *wlr.Keyboard)
|
||||||
server.seat.wlr_seat.keyboardNotifyModifiers(&wlr_keyboard.modifiers);
|
server.seat.wlr_seat.keyboardNotifyModifiers(&wlr_keyboard.modifiers);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handleKey(_: *wl.Listener(*wlr.Keyboard.event.Key), event: *wlr.Keyboard.event.Key) void {
|
fn handleKey(listener: *wl.Listener(*wlr.Keyboard.event.Key), event: *wlr.Keyboard.event.Key) void {
|
||||||
|
const keyboard: *Keyboard = @fieldParentPtr("key", listener);
|
||||||
// Translate libinput keycode -> xkbcommon
|
// Translate libinput keycode -> xkbcommon
|
||||||
const keycode = event.keycode + 8;
|
const keycode = event.keycode + 8;
|
||||||
|
|
||||||
var handled = false;
|
var handled: bool = false;
|
||||||
const modifiers = server.seat.keyboard_group.keyboard.getModifiers();
|
const modifiers = server.seat.keyboard_group.wlr_group.keyboard.getModifiers();
|
||||||
if (server.seat.keyboard_group.keyboard.xkb_state) |xkb_state| {
|
if (server.seat.keyboard_group.wlr_group.keyboard.xkb_state) |xkb_state| {
|
||||||
for (xkb_state.keyGetSyms(keycode)) |sym| {
|
const keysyms = xkb_state.keyGetSyms(keycode);
|
||||||
if (server.keymaps.get(Keymap.hash(modifiers, sym))) |map| {
|
for (keysyms) |sym| {
|
||||||
if (event.state == .pressed and map.options.lua_press_ref_idx > 0) {
|
handled = keypress(modifiers, sym, event.state);
|
||||||
map.callback(false);
|
|
||||||
handled = true;
|
|
||||||
} else if (event.state == .released and map.options.lua_release_ref_idx > 0) {
|
|
||||||
map.callback(true);
|
|
||||||
handled = true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// give the keyboard group information about what to repeat and update it
|
||||||
|
if (handled and keyboard.wlr_keyboard.repeat_info.delay > 0) {
|
||||||
|
server.seat.keyboard_group.modifiers = modifiers;
|
||||||
|
server.seat.keyboard_group.keysyms = keysyms;
|
||||||
|
server.seat.keyboard_group.repeat_source.?.timerUpdate(
|
||||||
|
keyboard.wlr_keyboard.repeat_info.delay,
|
||||||
|
) catch {
|
||||||
|
std.log.warn("failed to update keyboard repeat timer", .{});
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
server.seat.keyboard_group.modifiers = null;
|
||||||
|
server.seat.keyboard_group.keysyms = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (handled and keyboard.wlr_keyboard.repeat_info.delay > 0) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
if (!handled) {
|
if (!handled) {
|
||||||
server.seat.wlr_seat.setKeyboard(&server.seat.keyboard_group.keyboard);
|
server.seat.wlr_seat.setKeyboard(&server.seat.keyboard_group.wlr_group.keyboard);
|
||||||
server.seat.wlr_seat.keyboardNotifyKey(event.time_msec, event.keycode, event.state);
|
server.seat.wlr_seat.keyboardNotifyKey(event.time_msec, event.keycode, event.state);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn keypress(
|
||||||
|
modifiers: wlr.Keyboard.ModifierMask,
|
||||||
|
sym: xkb.Keysym,
|
||||||
|
state: wl.Keyboard.KeyState
|
||||||
|
) bool {
|
||||||
|
if (server.keymaps.get(Keymap.hash(modifiers, sym))) |map| {
|
||||||
|
if (state == .pressed and map.options.lua_press_ref_idx > 0) {
|
||||||
|
map.callback(false);
|
||||||
|
return true;
|
||||||
|
} else if (state == .released and map.options.lua_release_ref_idx > 0) {
|
||||||
|
map.callback(true);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
fn handleKeyMap(_: *wl.Listener(*wlr.Keyboard), _: *wlr.Keyboard) void {
|
fn handleKeyMap(_: *wl.Listener(*wlr.Keyboard), _: *wlr.Keyboard) void {
|
||||||
std.log.err("Unimplemented handle keyboard keymap", .{});
|
std.log.err("Unimplemented handle keyboard keymap", .{});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
63
src/KeyboardGroup.zig
Normal file
63
src/KeyboardGroup.zig
Normal file
|
|
@ -0,0 +1,63 @@
|
||||||
|
const KeyboardGroup = @This();
|
||||||
|
|
||||||
|
const std = @import("std");
|
||||||
|
const wl = @import("wayland").server.wl;
|
||||||
|
const wlr = @import("wlroots");
|
||||||
|
const xkb = @import("xkbcommon");
|
||||||
|
|
||||||
|
const Keyboard = @import("Keyboard.zig");
|
||||||
|
const Utils = @import("Utils.zig");
|
||||||
|
|
||||||
|
const server = &@import("main.zig").server;
|
||||||
|
const gpa = std.heap.c_allocator;
|
||||||
|
|
||||||
|
wlr_group: *wlr.KeyboardGroup,
|
||||||
|
repeat_source: ?*wl.EventSource,
|
||||||
|
modifiers: ?wlr.Keyboard.ModifierMask,
|
||||||
|
keysyms: ?[]const xkb.Keysym,
|
||||||
|
|
||||||
|
pub fn init() *KeyboardGroup {
|
||||||
|
errdefer Utils.oomPanic();
|
||||||
|
|
||||||
|
const self = try gpa.create(KeyboardGroup);
|
||||||
|
self.* = .{
|
||||||
|
.wlr_group = wlr.KeyboardGroup.create() catch Utils.oomPanic(),
|
||||||
|
.repeat_source = blk: {
|
||||||
|
break :blk server.event_loop.addTimer(?*KeyboardGroup, handleRepeat, self) catch {
|
||||||
|
std.log.err("Failed to create event loop timer, keyboard repeating will not work!", .{});
|
||||||
|
break :blk null;
|
||||||
|
};
|
||||||
|
},
|
||||||
|
.modifiers = null,
|
||||||
|
.keysyms = null,
|
||||||
|
};
|
||||||
|
|
||||||
|
return self;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn deinit(self: *KeyboardGroup) void {
|
||||||
|
self.wlr_group.destroy();
|
||||||
|
gpa.destroy(self);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handleRepeat(data: ?*KeyboardGroup) c_int {
|
||||||
|
// we failed to create the event loop timer, which means we can't repeat keys
|
||||||
|
if (data.?.repeat_source == null) return 0;
|
||||||
|
|
||||||
|
if (data == null or data.?.keysyms == null or data.?.modifiers == null or
|
||||||
|
data.?.wlr_group.keyboard.repeat_info.rate <= 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
data.?.repeat_source.?.timerUpdate(
|
||||||
|
@divTrunc(1000, data.?.wlr_group.keyboard.repeat_info.rate),
|
||||||
|
) catch {
|
||||||
|
// not sure how big of a deal it is if we miss a timer update
|
||||||
|
std.log.warn("failed to update keyboard repeat timer", .{});
|
||||||
|
};
|
||||||
|
for (data.?.keysyms.?) |sym| {
|
||||||
|
_ = Keyboard.keypress(data.?.modifiers.?, sym, .pressed);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
11
src/Seat.zig
11
src/Seat.zig
|
|
@ -5,6 +5,7 @@ const wlr = @import("wlroots");
|
||||||
const wl = @import("wayland").server.wl;
|
const wl = @import("wayland").server.wl;
|
||||||
const xkb = @import("xkbcommon");
|
const xkb = @import("xkbcommon");
|
||||||
|
|
||||||
|
const KeyboardGroup = @import("KeyboardGroup.zig");
|
||||||
const Utils = @import("Utils.zig");
|
const Utils = @import("Utils.zig");
|
||||||
const Popup = @import("Popup.zig");
|
const Popup = @import("Popup.zig");
|
||||||
const View = @import("View.zig");
|
const View = @import("View.zig");
|
||||||
|
|
@ -30,7 +31,7 @@ wlr_seat: *wlr.Seat,
|
||||||
focused_surface: ?FocusData,
|
focused_surface: ?FocusData,
|
||||||
focused_output: ?*Output,
|
focused_output: ?*Output,
|
||||||
|
|
||||||
keyboard_group: *wlr.KeyboardGroup,
|
keyboard_group: *KeyboardGroup,
|
||||||
keymap: *xkb.Keymap,
|
keymap: *xkb.Keymap,
|
||||||
|
|
||||||
request_set_cursor: wl.Listener(*wlr.Seat.event.RequestSetCursor) = .init(handleRequestSetCursor),
|
request_set_cursor: wl.Listener(*wlr.Seat.event.RequestSetCursor) = .init(handleRequestSetCursor),
|
||||||
|
|
@ -57,7 +58,7 @@ pub fn init(self: *Seat) void {
|
||||||
.wlr_seat = try wlr.Seat.create(server.wl_server, "default"),
|
.wlr_seat = try wlr.Seat.create(server.wl_server, "default"),
|
||||||
.focused_surface = null,
|
.focused_surface = null,
|
||||||
.focused_output = null,
|
.focused_output = null,
|
||||||
.keyboard_group = try wlr.KeyboardGroup.create(),
|
.keyboard_group = .init(),
|
||||||
.keymap = keymap.ref(),
|
.keymap = keymap.ref(),
|
||||||
};
|
};
|
||||||
errdefer {
|
errdefer {
|
||||||
|
|
@ -65,8 +66,8 @@ pub fn init(self: *Seat) void {
|
||||||
self.wlr_seat.destroy();
|
self.wlr_seat.destroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
_ = self.keyboard_group.keyboard.setKeymap(self.keymap);
|
_ = self.keyboard_group.wlr_group.keyboard.setKeymap(self.keymap);
|
||||||
self.wlr_seat.setKeyboard(&self.keyboard_group.keyboard);
|
self.wlr_seat.setKeyboard(&self.keyboard_group.wlr_group.keyboard);
|
||||||
|
|
||||||
self.wlr_seat.events.request_set_cursor.add(&self.request_set_cursor);
|
self.wlr_seat.events.request_set_cursor.add(&self.request_set_cursor);
|
||||||
self.wlr_seat.events.request_set_selection.add(&self.request_set_selection);
|
self.wlr_seat.events.request_set_selection.add(&self.request_set_selection);
|
||||||
|
|
@ -78,7 +79,7 @@ pub fn deinit(self: *Seat) void {
|
||||||
self.request_set_selection.link.remove();
|
self.request_set_selection.link.remove();
|
||||||
self.request_set_primary_selection.link.remove();
|
self.request_set_primary_selection.link.remove();
|
||||||
|
|
||||||
self.keyboard_group.destroy();
|
self.keyboard_group.deinit();
|
||||||
self.wlr_seat.destroy();
|
self.wlr_seat.destroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -150,8 +150,8 @@ fn handleMap(listener: *wl.Listener(void)) void {
|
||||||
const xdg_surface = view.xdg_toplevel.base;
|
const xdg_surface = view.xdg_toplevel.base;
|
||||||
server.seat.wlr_seat.keyboardNotifyEnter(
|
server.seat.wlr_seat.keyboardNotifyEnter(
|
||||||
xdg_surface.surface,
|
xdg_surface.surface,
|
||||||
&server.seat.keyboard_group.keyboard.keycodes,
|
&server.seat.keyboard_group.wlr_group.keyboard.keycodes,
|
||||||
&server.seat.keyboard_group.keyboard.modifiers
|
&server.seat.keyboard_group.wlr_group.keyboard.modifiers
|
||||||
);
|
);
|
||||||
|
|
||||||
view.mapped = true;
|
view.mapped = true;
|
||||||
|
|
|
||||||
|
|
@ -148,9 +148,9 @@ pub fn del_mousemap(L: *zlua.Lua) i32 {
|
||||||
pub fn get_repeat_info(L: *zlua.Lua) i32 {
|
pub fn get_repeat_info(L: *zlua.Lua) i32 {
|
||||||
L.newTable();
|
L.newTable();
|
||||||
|
|
||||||
L.pushInteger(server.seat.keyboard_group.keyboard.repeat_info.rate);
|
L.pushInteger(server.seat.keyboard_group.wlr_group.keyboard.repeat_info.rate);
|
||||||
L.setField(-2, "rate");
|
L.setField(-2, "rate");
|
||||||
L.pushInteger(server.seat.keyboard_group.keyboard.repeat_info.delay);
|
L.pushInteger(server.seat.keyboard_group.wlr_group.keyboard.repeat_info.delay);
|
||||||
L.setField(-2, "delay");
|
L.setField(-2, "delay");
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
|
|
@ -167,7 +167,7 @@ pub fn set_repeat_info(L: *zlua.Lua) i32 {
|
||||||
L.raiseErrorStr("The delay must be a valid number", .{});
|
L.raiseErrorStr("The delay must be a valid number", .{});
|
||||||
};
|
};
|
||||||
|
|
||||||
server.seat.keyboard_group.keyboard.setRepeatInfo(rate, delay);
|
server.seat.keyboard_group.wlr_group.keyboard.setRepeatInfo(rate, delay);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue