Seat: eliminate "self" naming convention

This commit is contained in:
Isaac Freund 2024-03-14 12:05:36 +01:00
parent 8a9b07ae4b
commit 267aa9a9b5
No known key found for this signature in database
GPG Key ID: 86DED400DDFD7A11

View File

@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const Self = @This();
const Seat = @This();
const build_options = @import("build_options");
const std = @import("std");
@ -110,64 +110,64 @@ drag_destroy: wl.Listener(*wlr.Drag) = wl.Listener(*wlr.Drag).init(handleDragDes
request_set_primary_selection: wl.Listener(*wlr.Seat.event.RequestSetPrimarySelection) =
wl.Listener(*wlr.Seat.event.RequestSetPrimarySelection).init(handleRequestSetPrimarySelection),
pub fn init(self: *Self, name: [*:0]const u8) !void {
pub fn init(seat: *Seat, name: [*:0]const u8) !void {
const event_loop = server.wl_server.getEventLoop();
const mapping_repeat_timer = try event_loop.addTimer(*Self, handleMappingRepeatTimeout, self);
const mapping_repeat_timer = try event_loop.addTimer(*Seat, handleMappingRepeatTimeout, seat);
errdefer mapping_repeat_timer.remove();
self.* = .{
seat.* = .{
// This will be automatically destroyed when the display is destroyed
.wlr_seat = try wlr.Seat.create(server.wl_server, name),
.cursor = undefined,
.relay = undefined,
.mapping_repeat_timer = mapping_repeat_timer,
};
self.wlr_seat.data = @intFromPtr(self);
seat.wlr_seat.data = @intFromPtr(seat);
try self.cursor.init(self);
self.relay.init();
try seat.cursor.init(seat);
seat.relay.init();
self.wlr_seat.events.request_set_selection.add(&self.request_set_selection);
self.wlr_seat.events.request_start_drag.add(&self.request_start_drag);
self.wlr_seat.events.start_drag.add(&self.start_drag);
self.wlr_seat.events.request_set_primary_selection.add(&self.request_set_primary_selection);
seat.wlr_seat.events.request_set_selection.add(&seat.request_set_selection);
seat.wlr_seat.events.request_start_drag.add(&seat.request_start_drag);
seat.wlr_seat.events.start_drag.add(&seat.start_drag);
seat.wlr_seat.events.request_set_primary_selection.add(&seat.request_set_primary_selection);
}
pub fn deinit(self: *Self) void {
pub fn deinit(seat: *Seat) void {
{
var it = server.input_manager.devices.iterator(.forward);
while (it.next()) |device| assert(device.seat != self);
while (it.next()) |device| assert(device.seat != seat);
}
self.cursor.deinit();
self.mapping_repeat_timer.remove();
seat.cursor.deinit();
seat.mapping_repeat_timer.remove();
while (self.keyboard_groups.first) |node| {
while (seat.keyboard_groups.first) |node| {
node.data.destroy();
}
self.request_set_selection.link.remove();
self.request_start_drag.link.remove();
self.start_drag.link.remove();
if (self.drag != .none) self.drag_destroy.link.remove();
self.request_set_primary_selection.link.remove();
seat.request_set_selection.link.remove();
seat.request_start_drag.link.remove();
seat.start_drag.link.remove();
if (seat.drag != .none) seat.drag_destroy.link.remove();
seat.request_set_primary_selection.link.remove();
}
/// Set the current focus. If a visible view is passed it will be focused.
/// If null is passed, the top view in the stack of the focused output will be focused.
/// Requires a call to Root.applyPending()
pub fn focus(self: *Self, _target: ?*View) void {
pub fn focus(seat: *Seat, _target: ?*View) void {
var target = _target;
// Don't change focus if there are no outputs.
if (self.focused_output == null) return;
if (seat.focused_output == null) return;
// Views may not receive focus while locked.
if (server.lock_manager.state != .unlocked) return;
// While a layer surface is exclusively focused, views may not receive focus
if (self.focused == .layer) {
const wlr_layer_surface = self.focused.layer.wlr_layer_surface;
if (seat.focused == .layer) {
const wlr_layer_surface = seat.focused.layer.wlr_layer_surface;
assert(wlr_layer_surface.surface.mapped);
if (wlr_layer_surface.current.keyboard_interactive == .exclusive and
(wlr_layer_surface.current.layer == .top or wlr_layer_surface.current.layer == .overlay))
@ -182,17 +182,17 @@ pub fn focus(self: *Self, _target: ?*View) void {
{
// If the view is not currently visible, behave as if null was passed
target = null;
} else if (view.pending.output.? != self.focused_output.?) {
} else if (view.pending.output.? != seat.focused_output.?) {
// If the view is not on the currently focused output, focus it
self.focusOutput(view.pending.output.?);
seat.focusOutput(view.pending.output.?);
}
}
{
var it = self.focused_output.?.pending.focus_stack.iterator(.forward);
var it = seat.focused_output.?.pending.focus_stack.iterator(.forward);
while (it.next()) |view| {
if (view.pending.fullscreen and
view.pending.tags & self.focused_output.?.pending.tags != 0)
view.pending.tags & seat.focused_output.?.pending.tags != 0)
{
target = view;
break;
@ -202,9 +202,9 @@ pub fn focus(self: *Self, _target: ?*View) void {
// If null, set the target to the first currently visible view in the focus stack if any
if (target == null) {
var it = self.focused_output.?.pending.focus_stack.iterator(.forward);
var it = seat.focused_output.?.pending.focus_stack.iterator(.forward);
target = while (it.next()) |view| {
if (view.pending.tags & self.focused_output.?.pending.tags != 0) {
if (view.pending.tags & seat.focused_output.?.pending.tags != 0) {
break view;
}
} else null;
@ -213,24 +213,24 @@ pub fn focus(self: *Self, _target: ?*View) void {
// Focus the target view or clear the focus if target is null
if (target) |view| {
view.pending_focus_stack_link.remove();
self.focused_output.?.pending.focus_stack.prepend(view);
self.setFocusRaw(.{ .view = view });
seat.focused_output.?.pending.focus_stack.prepend(view);
seat.setFocusRaw(.{ .view = view });
} else {
self.setFocusRaw(.{ .none = {} });
seat.setFocusRaw(.{ .none = {} });
}
}
/// Switch focus to the target, handling unfocus and input inhibition
/// properly. This should only be called directly if dealing with layers or
/// override redirect xwayland views.
pub fn setFocusRaw(self: *Self, new_focus: FocusTarget) void {
pub fn setFocusRaw(seat: *Seat, new_focus: FocusTarget) void {
// If the target is already focused, do nothing
if (std.meta.eql(new_focus, self.focused)) return;
if (std.meta.eql(new_focus, seat.focused)) return;
const target_surface = new_focus.surface();
// First clear the current focus
switch (self.focused) {
switch (seat.focused) {
.view => |view| {
view.pending.focus -= 1;
view.destroyPopups();
@ -245,66 +245,66 @@ pub fn setFocusRaw(self: *Self, new_focus: FocusTarget) void {
switch (new_focus) {
.view => |target_view| {
assert(server.lock_manager.state != .locked);
assert(self.focused_output == target_view.pending.output);
assert(seat.focused_output == target_view.pending.output);
target_view.pending.focus += 1;
target_view.pending.urgent = false;
},
.layer => |target_layer| {
assert(server.lock_manager.state != .locked);
assert(self.focused_output == target_layer.output);
assert(seat.focused_output == target_layer.output);
},
.lock_surface => assert(server.lock_manager.state != .unlocked),
.xwayland_override_redirect, .none => {},
}
self.focused = new_focus;
seat.focused = new_focus;
if (self.cursor.constraint) |constraint| {
if (seat.cursor.constraint) |constraint| {
if (constraint.wlr_constraint.surface != target_surface) {
if (constraint.state == .active) {
log.info("deactivating pointer constraint for surface, keyboard focus lost", .{});
constraint.deactivate();
}
self.cursor.constraint = null;
seat.cursor.constraint = null;
}
}
self.keyboardEnterOrLeave(target_surface);
self.relay.focus(target_surface);
seat.keyboardEnterOrLeave(target_surface);
seat.relay.focus(target_surface);
if (target_surface) |surface| {
const pointer_constraints = server.input_manager.pointer_constraints;
if (pointer_constraints.constraintForSurface(surface, self.wlr_seat)) |wlr_constraint| {
if (self.cursor.constraint) |constraint| {
if (pointer_constraints.constraintForSurface(surface, seat.wlr_seat)) |wlr_constraint| {
if (seat.cursor.constraint) |constraint| {
assert(constraint.wlr_constraint == wlr_constraint);
} else {
self.cursor.constraint = @ptrFromInt(wlr_constraint.data);
assert(self.cursor.constraint != null);
seat.cursor.constraint = @ptrFromInt(wlr_constraint.data);
assert(seat.cursor.constraint != null);
}
}
}
// Depending on configuration and cursor position, changing keyboard focus
// may cause the cursor to be warped.
self.cursor.may_need_warp = true;
seat.cursor.may_need_warp = true;
// Inform any clients tracking status of the change
var it = self.status_trackers.first;
var it = seat.status_trackers.first;
while (it) |node| : (it = node.next) node.data.sendFocusedView();
}
/// Send keyboard enter/leave events and handle pointer constraints
/// This should never normally be called from outside of setFocusRaw(), but we make an exception for
/// XwaylandOverrideRedirect surfaces as they don't conform to the Wayland focus model.
pub fn keyboardEnterOrLeave(self: *Self, target_surface: ?*wlr.Surface) void {
pub fn keyboardEnterOrLeave(seat: *Seat, target_surface: ?*wlr.Surface) void {
if (target_surface) |wlr_surface| {
self.keyboardNotifyEnter(wlr_surface);
seat.keyboardNotifyEnter(wlr_surface);
} else {
self.wlr_seat.keyboardNotifyClearFocus();
seat.wlr_seat.keyboardNotifyClearFocus();
}
}
fn keyboardNotifyEnter(self: *Self, wlr_surface: *wlr.Surface) void {
if (self.wlr_seat.getKeyboard()) |wlr_keyboard| {
fn keyboardNotifyEnter(seat: *Seat, wlr_surface: *wlr.Surface) void {
if (seat.wlr_seat.getKeyboard()) |wlr_keyboard| {
const keyboard: *Keyboard = @ptrFromInt(wlr_keyboard.data);
var keycodes: std.BoundedArray(u32, Keyboard.Pressed.capacity) = .{};
@ -312,45 +312,45 @@ fn keyboardNotifyEnter(self: *Self, wlr_surface: *wlr.Surface) void {
if (item.consumer == .focus) keycodes.appendAssumeCapacity(item.code);
}
self.wlr_seat.keyboardNotifyEnter(
seat.wlr_seat.keyboardNotifyEnter(
wlr_surface,
keycodes.constSlice(),
&wlr_keyboard.modifiers,
);
} else {
self.wlr_seat.keyboardNotifyEnter(wlr_surface, &.{}, null);
seat.wlr_seat.keyboardNotifyEnter(wlr_surface, &.{}, null);
}
}
/// Focus the given output, notifying any listening clients of the change.
pub fn focusOutput(self: *Self, output: ?*Output) void {
if (self.focused_output == output) return;
pub fn focusOutput(seat: *Seat, output: ?*Output) void {
if (seat.focused_output == output) return;
if (self.focused_output) |old| {
var it = self.status_trackers.first;
if (seat.focused_output) |old| {
var it = seat.status_trackers.first;
while (it) |node| : (it = node.next) node.data.sendOutput(old, .unfocused);
}
self.focused_output = output;
seat.focused_output = output;
if (self.focused_output) |new| {
var it = self.status_trackers.first;
if (seat.focused_output) |new| {
var it = seat.status_trackers.first;
while (it) |node| : (it = node.next) node.data.sendOutput(new, .focused);
}
// Depending on configuration and cursor position, changing output focus
// may cause the cursor to be warped.
self.cursor.may_need_warp = true;
seat.cursor.may_need_warp = true;
}
pub fn handleActivity(self: Self) void {
server.input_manager.idle_notifier.notifyActivity(self.wlr_seat);
pub fn handleActivity(seat: Seat) void {
server.input_manager.idle_notifier.notifyActivity(seat.wlr_seat);
}
pub fn enterMode(self: *Self, mode_id: u32) void {
self.mode_id = mode_id;
pub fn enterMode(seat: *Seat, mode_id: u32) void {
seat.mode_id = mode_id;
var it = self.status_trackers.first;
var it = seat.status_trackers.first;
while (it) |node| : (it = node.next) {
node.data.sendMode(server.config.modes.items[mode_id].name);
}
@ -359,7 +359,7 @@ pub fn enterMode(self: *Self, mode_id: u32) void {
/// Handle any user-defined mapping for passed keycode, modifiers and keyboard state
/// Returns true if a mapping was run
pub fn handleMapping(
self: *Self,
seat: *Seat,
keycode: xkb.Keycode,
modifiers: wlr.Keyboard.ModifierMask,
released: bool,
@ -380,7 +380,7 @@ pub fn handleMapping(
// That is, if the physical keys Mod+Shift+1 are pressed on a US layout don't
// translate the keysym 1 to an exclamation mark. This behavior is generally
// what is desired.
for (modes.items[self.mode_id].mappings.items) |*mapping| {
for (modes.items[seat.mode_id].mappings.items) |*mapping| {
if (mapping.match(keycode, modifiers, released, xkb_state, .no_translate)) {
if (found == null) {
found = mapping;
@ -394,7 +394,7 @@ pub fn handleMapping(
// with xkbcommon for intuitive behavior. For example, layouts may require
// translation with the numlock modifier to obtain keypad number keysyms
// (e.g. KP_1).
for (modes.items[self.mode_id].mappings.items) |*mapping| {
for (modes.items[seat.mode_id].mappings.items) |*mapping| {
if (mapping.match(keycode, modifiers, released, xkb_state, .translate)) {
if (found == null) {
found = mapping;
@ -408,12 +408,12 @@ pub fn handleMapping(
// the list of mappings we are iterating through, possibly causing it to be re-allocated.
if (found) |mapping| {
if (mapping.options.repeat) {
self.repeating_mapping = mapping;
self.mapping_repeat_timer.timerUpdate(server.config.repeat_delay) catch {
seat.repeating_mapping = mapping;
seat.mapping_repeat_timer.timerUpdate(server.config.repeat_delay) catch {
log.err("failed to update mapping repeat timer", .{});
};
}
self.runCommand(mapping.command_args);
seat.runCommand(mapping.command_args);
return true;
}
@ -422,22 +422,22 @@ pub fn handleMapping(
/// Handle any user-defined mapping for switches
pub fn handleSwitchMapping(
self: *Self,
seat: *Seat,
switch_type: Switch.Type,
switch_state: Switch.State,
) void {
const modes = &server.config.modes;
for (modes.items[self.mode_id].switch_mappings.items) |mapping| {
for (modes.items[seat.mode_id].switch_mappings.items) |mapping| {
if (std.meta.eql(mapping.switch_type, switch_type) and std.meta.eql(mapping.switch_state, switch_state)) {
self.runCommand(mapping.command_args);
seat.runCommand(mapping.command_args);
}
}
}
pub fn runCommand(self: *Self, args: []const [:0]const u8) void {
pub fn runCommand(seat: *Seat, args: []const [:0]const u8) void {
var out: ?[]const u8 = null;
defer if (out) |s| util.gpa.free(s);
command.run(self, args, &out) catch |err| {
command.run(seat, args, &out) catch |err| {
const failure_message = switch (err) {
command.Error.Other => out.?,
else => command.errToMsg(err),
@ -453,63 +453,63 @@ pub fn runCommand(self: *Self, args: []const [:0]const u8) void {
}
}
pub fn clearRepeatingMapping(self: *Self) void {
self.mapping_repeat_timer.timerUpdate(0) catch {
pub fn clearRepeatingMapping(seat: *Seat) void {
seat.mapping_repeat_timer.timerUpdate(0) catch {
log.err("failed to clear mapping repeat timer", .{});
};
self.repeating_mapping = null;
seat.repeating_mapping = null;
}
/// Repeat key mapping
fn handleMappingRepeatTimeout(self: *Self) c_int {
if (self.repeating_mapping) |mapping| {
fn handleMappingRepeatTimeout(seat: *Seat) c_int {
if (seat.repeating_mapping) |mapping| {
const rate = server.config.repeat_rate;
const ms_delay = if (rate > 0) 1000 / rate else 0;
self.mapping_repeat_timer.timerUpdate(ms_delay) catch {
seat.mapping_repeat_timer.timerUpdate(ms_delay) catch {
log.err("failed to update mapping repeat timer", .{});
};
self.runCommand(mapping.command_args);
seat.runCommand(mapping.command_args);
}
return 0;
}
pub fn addDevice(self: *Self, wlr_device: *wlr.InputDevice) void {
self.tryAddDevice(wlr_device) catch |err| switch (err) {
pub fn addDevice(seat: *Seat, wlr_device: *wlr.InputDevice) void {
seat.tryAddDevice(wlr_device) catch |err| switch (err) {
error.OutOfMemory => log.err("out of memory", .{}),
};
}
fn tryAddDevice(self: *Self, wlr_device: *wlr.InputDevice) !void {
fn tryAddDevice(seat: *Seat, wlr_device: *wlr.InputDevice) !void {
switch (wlr_device.type) {
.keyboard => {
const keyboard = try util.gpa.create(Keyboard);
errdefer util.gpa.destroy(keyboard);
try keyboard.init(self, wlr_device);
try keyboard.init(seat, wlr_device);
self.wlr_seat.setKeyboard(keyboard.device.wlr_device.toKeyboard());
if (self.wlr_seat.keyboard_state.focused_surface) |wlr_surface| {
self.wlr_seat.keyboardNotifyClearFocus();
self.keyboardNotifyEnter(wlr_surface);
seat.wlr_seat.setKeyboard(keyboard.device.wlr_device.toKeyboard());
if (seat.wlr_seat.keyboard_state.focused_surface) |wlr_surface| {
seat.wlr_seat.keyboardNotifyClearFocus();
seat.keyboardNotifyEnter(wlr_surface);
}
},
.pointer, .touch => {
const device = try util.gpa.create(InputDevice);
errdefer util.gpa.destroy(device);
try device.init(self, wlr_device);
try device.init(seat, wlr_device);
self.cursor.wlr_cursor.attachInputDevice(wlr_device);
seat.cursor.wlr_cursor.attachInputDevice(wlr_device);
},
.tablet_tool => {
try Tablet.create(self, wlr_device);
self.cursor.wlr_cursor.attachInputDevice(wlr_device);
try Tablet.create(seat, wlr_device);
seat.cursor.wlr_cursor.attachInputDevice(wlr_device);
},
.switch_device => {
const switch_device = try util.gpa.create(Switch);
errdefer util.gpa.destroy(switch_device);
try switch_device.init(self, wlr_device);
try switch_device.init(seat, wlr_device);
},
// TODO Support these types of input devices.
@ -517,14 +517,14 @@ fn tryAddDevice(self: *Self, wlr_device: *wlr.InputDevice) !void {
}
}
pub fn updateCapabilities(self: *Self) void {
pub fn updateCapabilities(seat: *Seat) void {
// Currently a cursor is always drawn even if there are no pointer input devices.
// TODO Don't draw a cursor if there are no input devices.
var capabilities: wl.Seat.Capability = .{ .pointer = true };
var it = server.input_manager.devices.iterator(.forward);
while (it.next()) |device| {
if (device.seat == self) {
if (device.seat == seat) {
switch (device.wlr_device.type) {
.keyboard => capabilities.keyboard = true,
.touch => capabilities.touch = true,
@ -534,36 +534,36 @@ pub fn updateCapabilities(self: *Self) void {
}
}
self.wlr_seat.setCapabilities(capabilities);
seat.wlr_seat.setCapabilities(capabilities);
}
fn handleRequestSetSelection(
listener: *wl.Listener(*wlr.Seat.event.RequestSetSelection),
event: *wlr.Seat.event.RequestSetSelection,
) void {
const self = @fieldParentPtr(Self, "request_set_selection", listener);
self.wlr_seat.setSelection(event.source, event.serial);
const seat = @fieldParentPtr(Seat, "request_set_selection", listener);
seat.wlr_seat.setSelection(event.source, event.serial);
}
fn handleRequestStartDrag(
listener: *wl.Listener(*wlr.Seat.event.RequestStartDrag),
event: *wlr.Seat.event.RequestStartDrag,
) void {
const self = @fieldParentPtr(Self, "request_start_drag", listener);
const seat = @fieldParentPtr(Seat, "request_start_drag", listener);
// The start_drag request is ignored by wlroots if a drag is currently in progress.
assert(self.drag == .none);
assert(seat.drag == .none);
if (self.wlr_seat.validatePointerGrabSerial(event.origin, event.serial)) {
if (seat.wlr_seat.validatePointerGrabSerial(event.origin, event.serial)) {
log.debug("starting pointer drag", .{});
self.wlr_seat.startPointerDrag(event.drag, event.serial);
seat.wlr_seat.startPointerDrag(event.drag, event.serial);
return;
}
var point: *wlr.TouchPoint = undefined;
if (self.wlr_seat.validateTouchGrabSerial(event.origin, event.serial, &point)) {
if (seat.wlr_seat.validateTouchGrabSerial(event.origin, event.serial, &point)) {
log.debug("starting touch drag", .{});
self.wlr_seat.startTouchDrag(event.drag, event.serial, point);
seat.wlr_seat.startTouchDrag(event.drag, event.serial, point);
return;
}
@ -573,21 +573,21 @@ fn handleRequestStartDrag(
}
fn handleStartDrag(listener: *wl.Listener(*wlr.Drag), wlr_drag: *wlr.Drag) void {
const self = @fieldParentPtr(Self, "start_drag", listener);
const seat = @fieldParentPtr(Seat, "start_drag", listener);
assert(self.drag == .none);
assert(seat.drag == .none);
switch (wlr_drag.grab_type) {
.keyboard_pointer => {
self.drag = .pointer;
self.cursor.mode = .passthrough;
seat.drag = .pointer;
seat.cursor.mode = .passthrough;
},
.keyboard_touch => self.drag = .touch,
.keyboard_touch => seat.drag = .touch,
.keyboard => unreachable,
}
wlr_drag.events.destroy.add(&self.drag_destroy);
wlr_drag.events.destroy.add(&seat.drag_destroy);
if (wlr_drag.icon) |wlr_drag_icon| {
DragIcon.create(wlr_drag_icon, &self.cursor) catch {
DragIcon.create(wlr_drag_icon, &seat.cursor) catch {
log.err("out of memory", .{});
wlr_drag.seat_client.client.postNoMemory();
return;
@ -596,24 +596,24 @@ fn handleStartDrag(listener: *wl.Listener(*wlr.Drag), wlr_drag: *wlr.Drag) void
}
fn handleDragDestroy(listener: *wl.Listener(*wlr.Drag), _: *wlr.Drag) void {
const self = @fieldParentPtr(Self, "drag_destroy", listener);
self.drag_destroy.link.remove();
const seat = @fieldParentPtr(Seat, "drag_destroy", listener);
seat.drag_destroy.link.remove();
switch (self.drag) {
switch (seat.drag) {
.none => unreachable,
.pointer => {
self.cursor.checkFocusFollowsCursor();
self.cursor.updateState();
seat.cursor.checkFocusFollowsCursor();
seat.cursor.updateState();
},
.touch => {},
}
self.drag = .none;
seat.drag = .none;
}
fn handleRequestSetPrimarySelection(
listener: *wl.Listener(*wlr.Seat.event.RequestSetPrimarySelection),
event: *wlr.Seat.event.RequestSetPrimarySelection,
) void {
const self = @fieldParentPtr(Self, "request_set_primary_selection", listener);
self.wlr_seat.setPrimarySelection(event.source, event.serial);
const seat = @fieldParentPtr(Seat, "request_set_primary_selection", listener);
seat.wlr_seat.setPrimarySelection(event.source, event.serial);
}