From 8a326541f6728749dd24b786960dadc2148f0f43 Mon Sep 17 00:00:00 2001 From: Isaac Freund Date: Wed, 15 Apr 2020 19:16:55 +0200 Subject: [PATCH] Add keybinds to cycle between outputs --- src/command.zig | 31 +++++++++++++++++++++++++++++++ src/config.zig | 4 ++++ 2 files changed, 35 insertions(+) diff --git a/src/command.zig b/src/command.zig index 1062d4a..709bf5b 100644 --- a/src/command.zig +++ b/src/command.zig @@ -2,6 +2,7 @@ const std = @import("std"); const c = @import("c.zig"); const Log = @import("log.zig").Log; +const Output = @import("output.zig").Output; const Seat = @import("seat.zig").Seat; const View = @import("view.zig").View; const ViewStack = @import("view_stack.zig").ViewStack; @@ -63,6 +64,36 @@ pub fn focusPrevView(seat: *Seat, arg: Arg) void { focusNextPrevView(seat, false); } +/// Focus either the next or the previous output, depending on the bool passed. +fn focusNextPrevOutput(seat: *Seat, next: bool) void { + const root = &seat.input_manager.server.root; + // If the noop output is focused, there are no other outputs to switch to + if (seat.focused_output == &root.noop_output) { + std.debug.assert(root.outputs.len == 0); + return; + } + + const focused_node = @fieldParentPtr(std.TailQueue(Output).Node, "data", seat.focused_output); + seat.focused_output = if (if (next) focused_node.next else focused_node.prev) |output_node| + // Focus the next/prev output in the list if there is one + &output_node.data + else if (next) &root.outputs.first.?.data else &root.outputs.last.?.data; + + seat.focus(null); +} + +/// Focus the next output, wrapping if needed. Does nothing if there is +/// only one output. +pub fn focusNextOutput(seat: *Seat, arg: Arg) void { + focusNextPrevOutput(seat, true); +} + +/// Focus the previous output, wrapping if needed. Does nothing if there is +/// only one output. +pub fn focusPrevOutput(seat: *Seat, arg: Arg) void { + focusNextPrevOutput(seat, false); +} + /// Modify the number of master views pub fn modifyMasterCount(seat: *Seat, arg: Arg) void { const delta = arg.int; diff --git a/src/config.zig b/src/config.zig index cbbdcea..b7be734 100644 --- a/src/config.zig +++ b/src/config.zig @@ -96,5 +96,9 @@ pub const Config = struct { // Mod+Shift+0 to tag focused view with all tags try self.keybinds.append(Keybind{ .keysym = c.XKB_KEY_0, .modifiers = mod | c.WLR_MODIFIER_SHIFT, .command = command.setFocusedViewTags, .arg = .{ .uint = 0xFFFFFFFF } }); + + // Mod+Period and Mod+Comma to focus the next/previous output + try self.keybinds.append(Keybind{ .keysym = c.XKB_KEY_period, .modifiers = mod, .command = command.focusNextOutput, .arg = .{ .none = {} } }); + try self.keybinds.append(Keybind{ .keysym = c.XKB_KEY_comma, .modifiers = mod, .command = command.focusPrevOutput, .arg = .{ .none = {} } }); } };