river: Implement input_method and text_input

This commit is contained in:
praschke
2023-07-11 13:22:05 +01:00
committed by Isaac Freund
parent 3aba3abbcd
commit 2abab1e9c7
6 changed files with 456 additions and 3 deletions

View File

@ -28,10 +28,12 @@ const util = @import("util.zig");
const InputConfig = @import("InputConfig.zig");
const InputDevice = @import("InputDevice.zig");
const InputRelay = @import("InputRelay.zig");
const Keyboard = @import("Keyboard.zig");
const PointerConstraint = @import("PointerConstraint.zig");
const Seat = @import("Seat.zig");
const Switch = @import("Switch.zig");
const TextInput = @import("TextInput.zig");
const default_seat_name = "default";
@ -44,6 +46,8 @@ relative_pointer_manager: *wlr.RelativePointerManagerV1,
virtual_pointer_manager: *wlr.VirtualPointerManagerV1,
virtual_keyboard_manager: *wlr.VirtualKeyboardManagerV1,
pointer_constraints: *wlr.PointerConstraintsV1,
input_method_manager: *wlr.InputMethodManagerV2,
text_input_manager: *wlr.TextInputManagerV3,
configs: std.ArrayList(InputConfig),
devices: wl.list.Head(InputDevice, .link),
@ -57,6 +61,10 @@ new_virtual_keyboard: wl.Listener(*wlr.VirtualKeyboardV1) =
wl.Listener(*wlr.VirtualKeyboardV1).init(handleNewVirtualKeyboard),
new_constraint: wl.Listener(*wlr.PointerConstraintV1) =
wl.Listener(*wlr.PointerConstraintV1).init(handleNewConstraint),
new_input_method: wl.Listener(*wlr.InputMethodV2) =
wl.Listener(*wlr.InputMethodV2).init(handleNewInputMethod),
new_text_input: wl.Listener(*wlr.TextInputV3) =
wl.Listener(*wlr.TextInputV3).init(handleNewTextInput),
pub fn init(self: *Self) !void {
const seat_node = try util.gpa.create(std.TailQueue(Seat).Node);
@ -69,6 +77,8 @@ pub fn init(self: *Self) !void {
.virtual_pointer_manager = try wlr.VirtualPointerManagerV1.create(server.wl_server),
.virtual_keyboard_manager = try wlr.VirtualKeyboardManagerV1.create(server.wl_server),
.pointer_constraints = try wlr.PointerConstraintsV1.create(server.wl_server),
.input_method_manager = try wlr.InputMethodManagerV2.create(server.wl_server),
.text_input_manager = try wlr.TextInputManagerV3.create(server.wl_server),
.configs = std.ArrayList(InputConfig).init(util.gpa),
.devices = undefined,
@ -84,6 +94,8 @@ pub fn init(self: *Self) !void {
self.virtual_pointer_manager.events.new_virtual_pointer.add(&self.new_virtual_pointer);
self.virtual_keyboard_manager.events.new_virtual_keyboard.add(&self.new_virtual_keyboard);
self.pointer_constraints.events.new_constraint.add(&self.new_constraint);
self.input_method_manager.events.input_method.add(&self.new_input_method);
self.text_input_manager.events.text_input.add(&self.new_text_input);
}
pub fn deinit(self: *Self) void {
@ -93,6 +105,8 @@ pub fn deinit(self: *Self) void {
self.new_virtual_pointer.link.remove();
self.new_virtual_keyboard.link.remove();
self.new_constraint.link.remove();
self.new_input_method.link.remove();
self.new_text_input.link.remove();
while (self.seats.pop()) |seat_node| {
seat_node.data.deinit();
@ -158,3 +172,51 @@ fn handleNewConstraint(
wlr_constraint.resource.postNoMemory();
};
}
fn handleNewInputMethod(
_: *wl.Listener(*wlr.InputMethodV2),
input_method: *wlr.InputMethodV2,
) void {
//const self = @fieldParentPtr(Self, "new_input_method", listener);
const seat = @as(*Seat, @ptrFromInt(input_method.seat.data));
const relay = &seat.relay;
// Only one input_method can be bound to a seat.
if (relay.input_method != null) {
log.debug("attempted to connect second input method to a seat", .{});
input_method.sendUnavailable();
return;
}
relay.input_method = input_method;
input_method.events.commit.add(&relay.input_method_commit);
input_method.events.grab_keyboard.add(&relay.grab_keyboard);
input_method.events.destroy.add(&relay.input_method_destroy);
log.debug("new input method on seat {s}", .{relay.seat.wlr_seat.name});
const text_input = relay.getFocusableTextInput() orelse return;
if (text_input.pending_focused_surface) |surface| {
text_input.wlr_text_input.sendEnter(surface);
text_input.setPendingFocusedSurface(null);
}
}
fn handleNewTextInput(
_: *wl.Listener(*wlr.TextInputV3),
wlr_text_input: *wlr.TextInputV3,
) void {
//const self = @fieldParentPtr(Self, "new_text_input", listener);
const seat = @as(*Seat, @ptrFromInt(wlr_text_input.seat.data));
const relay = &seat.relay;
const text_input_node = util.gpa.create(std.TailQueue(TextInput).Node) catch {
wlr_text_input.resource.postNoMemory();
return;
};
text_input_node.data.init(relay, wlr_text_input);
relay.text_inputs.append(text_input_node);
log.debug("new text input on seat {s}", .{relay.seat.wlr_seat.name});
}