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-03-23 08:50:20 -07:00
|
|
|
const Cursor = @import("cursor.zig").Cursor;
|
|
|
|
const Keyboard = @import("keyboard.zig").Keyboard;
|
|
|
|
const Server = @import("server.zig").Server;
|
|
|
|
|
2020-03-22 14:42:55 -07:00
|
|
|
// TODO: InputManager and multi-seat support
|
|
|
|
pub const Seat = struct {
|
2020-03-24 12:48:38 -07:00
|
|
|
const Self = @This();
|
|
|
|
|
2020-03-22 14:42:55 -07:00
|
|
|
server: *Server,
|
|
|
|
wlr_seat: *c.wlr_seat,
|
|
|
|
listen_new_input: c.wl_listener,
|
|
|
|
|
|
|
|
// Multiple mice are handled by the same Cursor
|
|
|
|
cursor: Cursor,
|
|
|
|
// Mulitple keyboards are handled separately
|
2020-03-23 13:51:46 -07:00
|
|
|
keyboards: std.TailQueue(Keyboard),
|
2020-03-22 14:42:55 -07:00
|
|
|
|
2020-03-25 08:24:21 -07:00
|
|
|
pub fn init(self: *Self, server: *Server) !void {
|
|
|
|
self.server = server;
|
2020-03-22 14:42:55 -07:00
|
|
|
|
2020-03-23 13:51:46 -07:00
|
|
|
// This seems to be the default seat name used by compositors
|
2020-03-25 08:24:21 -07:00
|
|
|
// This will be automatically destroyed when the display is destroyed
|
|
|
|
self.wlr_seat = c.wlr_seat_create(server.wl_display, "seat0") orelse
|
2020-03-23 13:51:46 -07:00
|
|
|
return error.CantCreateWlrSeat;
|
|
|
|
|
2020-03-25 08:24:21 -07:00
|
|
|
try self.cursor.init(self);
|
|
|
|
errdefer self.cursor.destroy();
|
2020-03-23 13:51:46 -07:00
|
|
|
|
2020-03-25 08:24:21 -07:00
|
|
|
self.keyboards = std.TailQueue(Keyboard).init();
|
2020-03-22 14:42:55 -07:00
|
|
|
|
|
|
|
// Set up handler for all new input devices made available. This
|
|
|
|
// includes keyboards, pointers, touch, etc.
|
2020-03-25 08:24:21 -07:00
|
|
|
self.listen_new_input.notify = handleNewInput;
|
2020-03-23 13:51:46 -07:00
|
|
|
c.wl_signal_add(&self.server.wlr_backend.events.new_input, &self.listen_new_input);
|
2020-03-22 14:42:55 -07:00
|
|
|
}
|
|
|
|
|
2020-03-29 06:58:04 -07:00
|
|
|
pub fn destroy(self: Self) void {
|
2020-03-25 08:24:21 -07:00
|
|
|
self.cursor.destroy();
|
|
|
|
}
|
|
|
|
|
2020-03-24 13:13:56 -07:00
|
|
|
fn addKeyboard(self: *Self, device: *c.wlr_input_device) !void {
|
2020-03-23 08:50:20 -07:00
|
|
|
c.wlr_seat_set_keyboard(self.wlr_seat, device);
|
2020-03-23 13:51:46 -07:00
|
|
|
|
2020-03-24 12:35:45 -07:00
|
|
|
const node = try self.keyboards.allocateNode(self.server.allocator);
|
2020-03-23 13:51:46 -07:00
|
|
|
try node.data.init(self, device);
|
|
|
|
self.keyboards.append(node);
|
2020-03-22 14:42:55 -07:00
|
|
|
}
|
|
|
|
|
2020-03-29 06:58:04 -07:00
|
|
|
fn addPointer(self: Self, device: *c.struct_wlr_input_device) void {
|
2020-03-22 14:42:55 -07:00
|
|
|
// We don't do anything special with pointers. All of our pointer handling
|
|
|
|
// is proxied through wlr_cursor. On another compositor, you might take this
|
|
|
|
// opportunity to do libinput configuration on the device to set
|
|
|
|
// acceleration, etc.
|
|
|
|
c.wlr_cursor_attach_input_device(self.cursor.wlr_cursor, device);
|
|
|
|
}
|
|
|
|
|
2020-03-24 13:13:56 -07:00
|
|
|
fn handleNewInput(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void {
|
2020-03-22 14:42:55 -07:00
|
|
|
// This event is raised by the backend when a new input device becomes available.
|
2020-03-24 12:35:45 -07:00
|
|
|
const seat = @fieldParentPtr(Seat, "listen_new_input", listener.?);
|
|
|
|
const device = @ptrCast(*c.wlr_input_device, @alignCast(@alignOf(*c.wlr_input_device), data));
|
2020-03-22 14:42:55 -07:00
|
|
|
|
2020-03-24 12:03:48 -07:00
|
|
|
switch (device.type) {
|
2020-03-24 13:13:56 -07:00
|
|
|
.WLR_INPUT_DEVICE_KEYBOARD => seat.addKeyboard(device) catch unreachable,
|
|
|
|
.WLR_INPUT_DEVICE_POINTER => seat.addPointer(device),
|
2020-03-22 14:42:55 -07:00
|
|
|
else => {},
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need to let the wlr_seat know what our capabilities are, which is
|
|
|
|
// communiciated to the client. In TinyWL we always have a cursor, even if
|
|
|
|
// there are no pointer devices, so we always include that capability.
|
|
|
|
var caps: u32 = @intCast(u32, c.WL_SEAT_CAPABILITY_POINTER);
|
|
|
|
// if list not empty
|
2020-03-23 13:51:46 -07:00
|
|
|
if (seat.keyboards.len > 0) {
|
2020-03-22 14:42:55 -07:00
|
|
|
caps |= @intCast(u32, c.WL_SEAT_CAPABILITY_KEYBOARD);
|
|
|
|
}
|
2020-03-23 08:50:20 -07:00
|
|
|
c.wlr_seat_set_capabilities(seat.wlr_seat, caps);
|
2020-03-22 14:42:55 -07:00
|
|
|
}
|
|
|
|
};
|