code: use a tagged union to store focus
This simplifies the code and is more robust than two separate pointers.
This commit is contained in:
parent
8afe7c2c87
commit
101f47d78f
11 changed files with 48 additions and 86 deletions
|
@ -139,11 +139,8 @@ fn handleUnmap(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void {
|
||||||
var it = self.output.root.server.input_manager.seats.first;
|
var it = self.output.root.server.input_manager.seats.first;
|
||||||
while (it) |node| : (it = node.next) {
|
while (it) |node| : (it = node.next) {
|
||||||
const seat = &node.data;
|
const seat = &node.data;
|
||||||
if (seat.focused_layer) |current_focus| {
|
if (seat.focused == .layer and seat.focused.layer == self)
|
||||||
if (current_focus == self) {
|
seat.setFocusRaw(.{ .none = {} });
|
||||||
seat.setFocusRaw(.{ .none = {} });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// This gives exclusive focus to a keyboard interactive top or overlay layer
|
// This gives exclusive focus to a keyboard interactive top or overlay layer
|
||||||
|
|
|
@ -393,18 +393,16 @@ pub fn arrangeLayers(self: *Self) void {
|
||||||
const seat = &node.data;
|
const seat = &node.data;
|
||||||
|
|
||||||
// Only grab focus of seats which have the output focused
|
// Only grab focus of seats which have the output focused
|
||||||
if (seat.focused_output != self) {
|
if (seat.focused_output != self) continue;
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (topmost_surface) |to_focus| {
|
if (topmost_surface) |to_focus| {
|
||||||
// If we found a surface that requires focus, grab the focus of all
|
// If we found a surface that requires focus, grab the focus of all
|
||||||
// seats.
|
// seats.
|
||||||
seat.setFocusRaw(.{ .layer = to_focus });
|
seat.setFocusRaw(.{ .layer = to_focus });
|
||||||
} else if (seat.focused_layer) |current_focus| {
|
} else if (seat.focused == .layer) {
|
||||||
// If the seat is currently focusing a layer without keyboard
|
// If the seat is currently focusing a layer without keyboard
|
||||||
// interactivity, clear the focused layer.
|
// interactivity, stop focusing that layer.
|
||||||
if (!current_focus.wlr_layer_surface.current.keyboard_interactive) {
|
if (!seat.focused.layer.wlr_layer_surface.current.keyboard_interactive) {
|
||||||
seat.setFocusRaw(.{ .none = {} });
|
seat.setFocusRaw(.{ .none = {} });
|
||||||
seat.focus(null);
|
seat.focus(null);
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,8 +33,6 @@ const SeatStatus = @import("SeatStatus.zig");
|
||||||
const View = @import("View.zig");
|
const View = @import("View.zig");
|
||||||
const ViewStack = @import("view_stack.zig").ViewStack;
|
const ViewStack = @import("view_stack.zig").ViewStack;
|
||||||
|
|
||||||
// TODO: remove none variant, unify focused_view and focused_layer fields
|
|
||||||
// with type ?FocusTarget
|
|
||||||
const FocusTarget = union(enum) {
|
const FocusTarget = union(enum) {
|
||||||
view: *View,
|
view: *View,
|
||||||
layer: *LayerSurface,
|
layer: *LayerSurface,
|
||||||
|
@ -56,17 +54,13 @@ mode_id: usize,
|
||||||
/// Currently focused output, may be the noop output if no
|
/// Currently focused output, may be the noop output if no
|
||||||
focused_output: *Output,
|
focused_output: *Output,
|
||||||
|
|
||||||
/// Currently focused view if any
|
/// Currently focused view/layer surface if any
|
||||||
focused_view: ?*View,
|
focused: FocusTarget,
|
||||||
|
|
||||||
/// Stack of views in most recently focused order
|
/// Stack of views in most recently focused order
|
||||||
/// If there is a currently focused view, it is on top.
|
/// If there is a currently focused view, it is on top.
|
||||||
focus_stack: ViewStack(*View),
|
focus_stack: ViewStack(*View),
|
||||||
|
|
||||||
/// Currently focused layer, if any. While this is non-null, no views may
|
|
||||||
/// recieve focus.
|
|
||||||
focused_layer: ?*LayerSurface,
|
|
||||||
|
|
||||||
/// List of status tracking objects relaying changes to this seat to clients.
|
/// List of status tracking objects relaying changes to this seat to clients.
|
||||||
status_trackers: std.SinglyLinkedList(SeatStatus),
|
status_trackers: std.SinglyLinkedList(SeatStatus),
|
||||||
|
|
||||||
|
@ -91,12 +85,10 @@ pub fn init(self: *Self, input_manager: *InputManager, name: [*:0]const u8) !voi
|
||||||
|
|
||||||
self.focused_output = &self.input_manager.server.root.noop_output;
|
self.focused_output = &self.input_manager.server.root.noop_output;
|
||||||
|
|
||||||
self.focused_view = null;
|
self.focused = .none;
|
||||||
|
|
||||||
self.focus_stack.init();
|
self.focus_stack.init();
|
||||||
|
|
||||||
self.focused_layer = null;
|
|
||||||
|
|
||||||
self.status_trackers = std.SinglyLinkedList(SeatStatus).init();
|
self.status_trackers = std.SinglyLinkedList(SeatStatus).init();
|
||||||
|
|
||||||
self.pointer_modifier = false;
|
self.pointer_modifier = false;
|
||||||
|
@ -122,10 +114,7 @@ pub fn focus(self: *Self, _view: ?*View) void {
|
||||||
var view = _view;
|
var view = _view;
|
||||||
|
|
||||||
// While a layer surface is focused, views may not recieve focus
|
// While a layer surface is focused, views may not recieve focus
|
||||||
if (self.focused_layer != null) {
|
if (self.focused == .layer) return;
|
||||||
std.debug.assert(self.focused_view == null);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// If the view is not currently visible, behave as if null was passed
|
// If the view is not currently visible, behave as if null was passed
|
||||||
if (view) |v| {
|
if (view) |v| {
|
||||||
|
@ -177,16 +166,12 @@ pub fn focus(self: *Self, _view: ?*View) void {
|
||||||
|
|
||||||
/// Switch focus to the target, handling unfocus and input inhibition
|
/// Switch focus to the target, handling unfocus and input inhibition
|
||||||
/// properly. This should only be called directly if dealing with layers.
|
/// properly. This should only be called directly if dealing with layers.
|
||||||
pub fn setFocusRaw(self: *Self, focus_target: FocusTarget) void {
|
pub fn setFocusRaw(self: *Self, new_focus: FocusTarget) void {
|
||||||
// If the target is already focused, do nothing
|
// If the target is already focused, do nothing
|
||||||
if (switch (focus_target) {
|
if (std.meta.eql(new_focus, self.focused)) return;
|
||||||
.view => |target_view| target_view == self.focused_view,
|
|
||||||
.layer => |target_layer| target_layer == self.focused_layer,
|
|
||||||
.none => false,
|
|
||||||
}) return;
|
|
||||||
|
|
||||||
// Obtain the target wlr_surface
|
// Obtain the target wlr_surface
|
||||||
const target_wlr_surface = switch (focus_target) {
|
const target_wlr_surface = switch (new_focus) {
|
||||||
.view => |target_view| target_view.wlr_surface.?,
|
.view => |target_view| target_view.wlr_surface.?,
|
||||||
.layer => |target_layer| target_layer.wlr_layer_surface.surface.?,
|
.layer => |target_layer| target_layer.wlr_layer_surface.surface.?,
|
||||||
.none => null,
|
.none => null,
|
||||||
|
@ -195,35 +180,21 @@ pub fn setFocusRaw(self: *Self, focus_target: FocusTarget) void {
|
||||||
// If input is not allowed on the target surface (e.g. due to an active
|
// If input is not allowed on the target surface (e.g. due to an active
|
||||||
// input inhibitor) do not set focus. If there is no target surface we
|
// input inhibitor) do not set focus. If there is no target surface we
|
||||||
// still clear the focus.
|
// still clear the focus.
|
||||||
if (if (target_wlr_surface) |wlr_surface|
|
if (if (target_wlr_surface) |wlr_surface| self.input_manager.inputAllowed(wlr_surface) else true) {
|
||||||
self.input_manager.inputAllowed(wlr_surface)
|
|
||||||
else
|
|
||||||
true) {
|
|
||||||
// First clear the current focus
|
// First clear the current focus
|
||||||
if (self.focused_view) |current_focus| {
|
if (self.focused == .view) self.focused.view.setFocused(false);
|
||||||
std.debug.assert(self.focused_layer == null);
|
|
||||||
current_focus.setFocused(false);
|
|
||||||
self.focused_view = null;
|
|
||||||
}
|
|
||||||
if (self.focused_layer) |current_focus| {
|
|
||||||
std.debug.assert(self.focused_view == null);
|
|
||||||
self.focused_layer = null;
|
|
||||||
}
|
|
||||||
c.wlr_seat_keyboard_clear_focus(self.wlr_seat);
|
c.wlr_seat_keyboard_clear_focus(self.wlr_seat);
|
||||||
|
|
||||||
// Set the new focus
|
// Set the new focus
|
||||||
switch (focus_target) {
|
switch (new_focus) {
|
||||||
.view => |target_view| {
|
.view => |target_view| {
|
||||||
std.debug.assert(self.focused_output == target_view.output);
|
std.debug.assert(self.focused_output == target_view.output);
|
||||||
target_view.setFocused(true);
|
target_view.setFocused(true);
|
||||||
self.focused_view = target_view;
|
|
||||||
},
|
|
||||||
.layer => |target_layer| blk: {
|
|
||||||
std.debug.assert(self.focused_output == target_layer.output);
|
|
||||||
self.focused_layer = target_layer;
|
|
||||||
},
|
},
|
||||||
|
.layer => |target_layer| std.debug.assert(self.focused_output == target_layer.output),
|
||||||
.none => {},
|
.none => {},
|
||||||
}
|
}
|
||||||
|
self.focused = new_focus;
|
||||||
|
|
||||||
// Tell wlroots to send the new keyboard focus if we have a target
|
// Tell wlroots to send the new keyboard focus if we have a target
|
||||||
if (target_wlr_surface) |wlr_surface| {
|
if (target_wlr_surface) |wlr_surface| {
|
||||||
|
@ -270,11 +241,7 @@ pub fn handleViewUnmap(self: *Self, view: *View) void {
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the unmapped view is focused, choose a new focus
|
// If the unmapped view is focused, choose a new focus
|
||||||
if (self.focused_view) |current_focus| {
|
if (self.focused == .view and self.focused.view == view) self.focus(null);
|
||||||
if (current_focus == view) {
|
|
||||||
self.focus(null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Handle any user-defined mapping for the passed keysym and modifiers
|
/// Handle any user-defined mapping for the passed keysym and modifiers
|
||||||
|
|
|
@ -75,8 +75,6 @@ pub fn sendOutput(self: Self, state: FocusState) void {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn sendFocusedView(self: Self) void {
|
pub fn sendFocusedView(self: Self) void {
|
||||||
c.zriver_seat_status_v1_send_focused_view(self.wl_resource, if (self.seat.focused_view) |v|
|
const title: [*:0]const u8 = if (self.seat.focused == .view) self.seat.focused.view.getTitle() else "";
|
||||||
v.getTitle()
|
c.zriver_seat_status_v1_send_focused_view(self.wl_resource, title);
|
||||||
else
|
|
||||||
"");
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,5 +29,5 @@ pub fn close(
|
||||||
) Error!void {
|
) Error!void {
|
||||||
// Note: we don't call arrange() here as it will be called
|
// Note: we don't call arrange() here as it will be called
|
||||||
// automatically when the view is unmapped.
|
// automatically when the view is unmapped.
|
||||||
if (seat.focused_view) |view| view.close();
|
if (seat.focused == .view) seat.focused.view.close();
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,12 +37,12 @@ pub fn focusView(
|
||||||
const direction = std.meta.stringToEnum(Direction, args[1]) orelse return Error.InvalidDirection;
|
const direction = std.meta.stringToEnum(Direction, args[1]) orelse return Error.InvalidDirection;
|
||||||
const output = seat.focused_output;
|
const output = seat.focused_output;
|
||||||
|
|
||||||
if (seat.focused_view) |current_focus| {
|
if (seat.focused == .view) {
|
||||||
// If the focused view is fullscreen, do nothing
|
// If the focused view is fullscreen, do nothing
|
||||||
if (current_focus.current.fullscreen) return;
|
if (seat.focused.view.current.fullscreen) return;
|
||||||
|
|
||||||
// If there is a currently focused view, focus the next visible view in the stack.
|
// If there is a currently focused view, focus the next visible view in the stack.
|
||||||
const focused_node = @fieldParentPtr(ViewStack(View).Node, "view", current_focus);
|
const focused_node = @fieldParentPtr(ViewStack(View).Node, "view", seat.focused.view);
|
||||||
var it = switch (direction) {
|
var it = switch (direction) {
|
||||||
.next => ViewStack(View).iterator(focused_node, output.current.tags),
|
.next => ViewStack(View).iterator(focused_node, output.current.tags),
|
||||||
.previous => ViewStack(View).reverseIterator(focused_node, output.current.tags),
|
.previous => ViewStack(View).reverseIterator(focused_node, output.current.tags),
|
||||||
|
|
|
@ -36,22 +36,22 @@ pub fn sendToOutput(
|
||||||
const direction = std.meta.stringToEnum(Direction, args[1]) orelse return Error.InvalidDirection;
|
const direction = std.meta.stringToEnum(Direction, args[1]) orelse return Error.InvalidDirection;
|
||||||
const root = &seat.input_manager.server.root;
|
const root = &seat.input_manager.server.root;
|
||||||
|
|
||||||
if (seat.focused_view) |view| {
|
if (seat.focused == .view) {
|
||||||
// If the noop output is focused, there is nowhere to send the view
|
// If the noop output is focused, there is nowhere to send the view
|
||||||
if (view.output == &root.noop_output) {
|
if (seat.focused_output == &root.noop_output) {
|
||||||
std.debug.assert(root.outputs.len == 0);
|
std.debug.assert(root.outputs.len == 0);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send to the next/prev output in the list if there is one, else wrap
|
// Send to the next/prev output in the list if there is one, else wrap
|
||||||
const current_node = @fieldParentPtr(std.TailQueue(Output).Node, "data", view.output);
|
const current_node = @fieldParentPtr(std.TailQueue(Output).Node, "data", seat.focused_output);
|
||||||
const destination_output = switch (direction) {
|
const destination_output = switch (direction) {
|
||||||
.next => if (current_node.next) |node| &node.data else &root.outputs.first.?.data,
|
.next => if (current_node.next) |node| &node.data else &root.outputs.first.?.data,
|
||||||
.previous => if (current_node.prev) |node| &node.data else &root.outputs.last.?.data,
|
.previous => if (current_node.prev) |node| &node.data else &root.outputs.last.?.data,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Move the view to the target output
|
// Move the view to the target output
|
||||||
view.sendToOutput(destination_output);
|
seat.focused.view.sendToOutput(destination_output);
|
||||||
|
|
||||||
// Handle the change and focus whatever's next in the focus stack
|
// Handle the change and focus whatever's next in the focus stack
|
||||||
root.arrange();
|
root.arrange();
|
||||||
|
|
|
@ -42,9 +42,9 @@ pub fn setViewTags(
|
||||||
out: *?[]const u8,
|
out: *?[]const u8,
|
||||||
) Error!void {
|
) Error!void {
|
||||||
const tags = try parseTags(allocator, args, out);
|
const tags = try parseTags(allocator, args, out);
|
||||||
if (seat.focused_view) |view| {
|
if (seat.focused == .view) {
|
||||||
view.pending.tags = tags;
|
seat.focused.view.pending.tags = tags;
|
||||||
view.output.root.arrange();
|
seat.focused.view.output.root.arrange();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -72,11 +72,11 @@ pub fn toggleViewTags(
|
||||||
out: *?[]const u8,
|
out: *?[]const u8,
|
||||||
) Error!void {
|
) Error!void {
|
||||||
const tags = try parseTags(allocator, args, out);
|
const tags = try parseTags(allocator, args, out);
|
||||||
if (seat.focused_view) |view| {
|
if (seat.focused == .view) {
|
||||||
const new_tags = view.current.tags ^ tags;
|
const new_tags = seat.focused.view.current.tags ^ tags;
|
||||||
if (new_tags != 0) {
|
if (new_tags != 0) {
|
||||||
view.pending.tags = new_tags;
|
seat.focused.view.pending.tags = new_tags;
|
||||||
view.output.root.arrange();
|
seat.focused.view.output.root.arrange();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,7 +30,9 @@ pub fn toggleFloat(
|
||||||
) Error!void {
|
) Error!void {
|
||||||
if (args.len > 1) return Error.TooManyArguments;
|
if (args.len > 1) return Error.TooManyArguments;
|
||||||
|
|
||||||
if (seat.focused_view) |view| {
|
if (seat.focused == .view) {
|
||||||
|
const view = seat.focused.view;
|
||||||
|
|
||||||
// Don't float fullscreen views
|
// Don't float fullscreen views
|
||||||
if (view.pending.fullscreen) return;
|
if (view.pending.fullscreen) return;
|
||||||
|
|
||||||
|
|
|
@ -29,8 +29,8 @@ pub fn toggleFullscreen(
|
||||||
) Error!void {
|
) Error!void {
|
||||||
if (args.len > 1) return Error.TooManyArguments;
|
if (args.len > 1) return Error.TooManyArguments;
|
||||||
|
|
||||||
if (seat.focused_view) |view| {
|
if (seat.focused == .view) {
|
||||||
view.setFullscreen(!view.pending.fullscreen);
|
seat.focused.view.setFullscreen(!seat.focused.view.pending.fullscreen);
|
||||||
view.output.root.arrange();
|
seat.focused.view.output.root.arrange();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,19 +32,19 @@ pub fn zoom(
|
||||||
) Error!void {
|
) Error!void {
|
||||||
if (args.len > 1) return Error.TooManyArguments;
|
if (args.len > 1) return Error.TooManyArguments;
|
||||||
|
|
||||||
if (seat.focused_view) |current_focus| {
|
if (seat.focused == .view) {
|
||||||
const output = seat.focused_output;
|
|
||||||
const focused_node = @fieldParentPtr(ViewStack(View).Node, "view", current_focus);
|
|
||||||
|
|
||||||
// Only zoom views that are part of the layout
|
// Only zoom views that are part of the layout
|
||||||
if (current_focus.pending.float or current_focus.pending.fullscreen) return;
|
if (seat.focused.view.pending.float or seat.focused.view.pending.fullscreen) return;
|
||||||
|
|
||||||
// If the the first view that is part of the layout is focused, zoom
|
// If the first view that is part of the layout is focused, zoom
|
||||||
// the next view in the layout. Otherwise zoom the focused view.
|
// the next view in the layout. Otherwise zoom the focused view.
|
||||||
|
const output = seat.focused_output;
|
||||||
var it = ViewStack(View).iterator(output.views.first, output.current.tags);
|
var it = ViewStack(View).iterator(output.views.first, output.current.tags);
|
||||||
const layout_first = while (it.next()) |node| {
|
const layout_first = while (it.next()) |node| {
|
||||||
if (!node.view.pending.float and !node.view.pending.fullscreen) break node;
|
if (!node.view.pending.float and !node.view.pending.fullscreen) break node;
|
||||||
} else unreachable;
|
} else unreachable;
|
||||||
|
|
||||||
|
const focused_node = @fieldParentPtr(ViewStack(View).Node, "view", seat.focused.view);
|
||||||
const zoom_node = if (focused_node == layout_first) blk: {
|
const zoom_node = if (focused_node == layout_first) blk: {
|
||||||
while (it.next()) |node| {
|
while (it.next()) |node| {
|
||||||
if (!node.view.pending.float and !node.view.pending.fullscreen) break :blk node;
|
if (!node.view.pending.float and !node.view.pending.fullscreen) break :blk node;
|
||||||
|
|
Loading…
Reference in a new issue