diff --git a/src/command.zig b/src/command.zig index d82d4c3..8abf288 100644 --- a/src/command.zig +++ b/src/command.zig @@ -1,11 +1,4 @@ -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; pub const Direction = enum { Next, @@ -23,161 +16,15 @@ pub const Arg = union { pub const Command = fn (seat: *Seat, arg: Arg) void; -/// Exit the compositor, terminating the wayland session. -pub fn exitCompositor(seat: *Seat, arg: Arg) void { - c.wl_display_terminate(seat.input_manager.server.wl_display); -} - -/// Focus either the next or the previous visible view, depending on the enum -/// passed. Does nothing if there are 1 or 0 views in the stack. -pub fn focusView(seat: *Seat, arg: Arg) void { - const direction = arg.direction; - const output = seat.focused_output; - if (seat.focused_view) |current_focus| { - // 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); - var it = switch (direction) { - .Next => ViewStack(View).iterator(focused_node, output.current_focused_tags), - .Prev => ViewStack(View).reverseIterator(focused_node, output.current_focused_tags), - }; - - // Skip past the focused node - _ = it.next(); - // Focus the next visible node if there is one - if (it.next()) |node| { - seat.focus(&node.view); - return; - } - } - - // There is either no currently focused view or the last visible view in the - // stack is focused and we need to wrap. - var it = switch (direction) { - .Next => ViewStack(View).iterator(output.views.first, output.current_focused_tags), - .Prev => ViewStack(View).reverseIterator(output.views.last, output.current_focused_tags), - }; - seat.focus(if (it.next()) |node| &node.view else null); -} - -/// Focus either the next or the previous output, depending on the bool passed. -/// Does nothing if there is only one output. -pub fn focusOutput(seat: *Seat, arg: Arg) void { - const direction = arg.direction; - 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; - } - - // Focus the next/prev output in the list if there is one, else wrap - const focused_node = @fieldParentPtr(std.TailQueue(Output).Node, "data", seat.focused_output); - seat.focused_output = switch (direction) { - .Next => if (focused_node.next) |node| &node.data else &root.outputs.first.?.data, - .Prev => if (focused_node.prev) |node| &node.data else &root.outputs.last.?.data, - }; - - seat.focus(null); -} - -/// Modify the number of master views -pub fn modifyMasterCount(seat: *Seat, arg: Arg) void { - const delta = arg.int; - const output = seat.focused_output; - output.master_count = @intCast( - u32, - std.math.max(0, @intCast(i32, output.master_count) + delta), - ); - seat.input_manager.server.root.arrange(); -} - -/// Modify the percent of the width of the screen that the master views occupy. -pub fn modifyMasterFactor(seat: *Seat, arg: Arg) void { - const delta = arg.float; - const output = seat.focused_output; - const new_master_factor = std.math.min( - std.math.max(output.master_factor + delta, 0.05), - 0.95, - ); - if (new_master_factor != output.master_factor) { - output.master_factor = new_master_factor; - seat.input_manager.server.root.arrange(); - } -} - -/// Bump the focused view to the top of the stack. -/// TODO: if the top of the stack is focused, bump the next visible view. -pub fn zoom(seat: *Seat, arg: Arg) void { - if (seat.focused_view) |current_focus| { - const output = seat.focused_output; - const node = @fieldParentPtr(ViewStack(View).Node, "view", current_focus); - if (node != output.views.first) { - output.views.remove(node); - output.views.push(node); - seat.input_manager.server.root.arrange(); - } - } -} - -/// Switch focus to the passed tags. -pub fn focusTags(seat: *Seat, arg: Arg) void { - const tags = arg.uint; - seat.focused_output.pending_focused_tags = tags; - seat.input_manager.server.root.arrange(); -} - -/// Toggle focus of the passsed tags. -pub fn toggleTags(seat: *Seat, arg: Arg) void { - const tags = arg.uint; - const output = seat.focused_output; - const new_focused_tags = output.current_focused_tags ^ tags; - if (new_focused_tags != 0) { - output.pending_focused_tags = new_focused_tags; - seat.input_manager.server.root.arrange(); - } -} - -/// Set the tags of the focused view. -pub fn setFocusedViewTags(seat: *Seat, arg: Arg) void { - const tags = arg.uint; - if (seat.focused_view) |view| { - if (view.current_tags != tags) { - view.pending_tags = tags; - seat.input_manager.server.root.arrange(); - } - } -} - -/// Toggle the passed tags of the focused view -pub fn toggleFocusedViewTags(seat: *Seat, arg: Arg) void { - const tags = arg.uint; - if (seat.focused_view) |view| { - const new_tags = view.current_tags ^ tags; - if (new_tags != 0) { - view.pending_tags = new_tags; - seat.input_manager.server.root.arrange(); - } - } -} - -/// Spawn a program. -pub fn spawn(seat: *Seat, arg: Arg) void { - const cmd = arg.str; - - const argv = [_][]const u8{ "/bin/sh", "-c", cmd }; - const child = std.ChildProcess.init(&argv, std.heap.c_allocator) catch |err| { - Log.Error.log("Failed to execute {}: {}", .{ cmd, err }); - return; - }; - std.ChildProcess.spawn(child) catch |err| { - Log.Error.log("Failed to execute {}: {}", .{ cmd, err }); - return; - }; -} - -/// Close the focused view, if any. -pub fn close(seat: *Seat, arg: Arg) void { - if (seat.focused_view) |view| { - view.close(); - } -} +pub usingnamespace @import("command/close_view.zig"); +pub usingnamespace @import("command/exit_compositor.zig"); +pub usingnamespace @import("command/focus_output.zig"); +pub usingnamespace @import("command/focus_tags.zig"); +pub usingnamespace @import("command/focus_view.zig"); +pub usingnamespace @import("command/modify_master_count.zig"); +pub usingnamespace @import("command/modify_master_factor.zig"); +pub usingnamespace @import("command/set_view_tags.zig"); +pub usingnamespace @import("command/spawn.zig"); +pub usingnamespace @import("command/toggle_tags.zig"); +pub usingnamespace @import("command/toggle_view_tags.zig"); +pub usingnamespace @import("command/zoom.zig"); diff --git a/src/command/close_view.zig b/src/command/close_view.zig new file mode 100644 index 0000000..b0f03b9 --- /dev/null +++ b/src/command/close_view.zig @@ -0,0 +1,11 @@ +const c = @import("../c.zig"); + +const Arg = @import("../command.zig").Arg; +const Seat = @import("../seat.zig").Seat; + +/// Close the focused view, if any. +pub fn close_view(seat: *Seat, arg: Arg) void { + if (seat.focused_view) |view| { + view.close(); + } +} diff --git a/src/command/exit_compositor.zig b/src/command/exit_compositor.zig new file mode 100644 index 0000000..ff620ee --- /dev/null +++ b/src/command/exit_compositor.zig @@ -0,0 +1,9 @@ +const c = @import("../c.zig"); + +const Arg = @import("../command.zig").Arg; +const Seat = @import("../seat.zig").Seat; + +/// Exit the compositor, terminating the wayland session. +pub fn exitCompositor(seat: *Seat, arg: Arg) void { + c.wl_display_terminate(seat.input_manager.server.wl_display); +} diff --git a/src/command/focus_output.zig b/src/command/focus_output.zig new file mode 100644 index 0000000..368bb96 --- /dev/null +++ b/src/command/focus_output.zig @@ -0,0 +1,27 @@ +const c = @import("../c.zig"); +const std = @import("std"); + +const Arg = @import("../command.zig").Arg; +const Output = @import("../output.zig").Output; +const Seat = @import("../seat.zig").Seat; + +/// Focus either the next or the previous output, depending on the bool passed. +/// Does nothing if there is only one output. +pub fn focusOutput(seat: *Seat, arg: Arg) void { + const direction = arg.direction; + 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; + } + + // Focus the next/prev output in the list if there is one, else wrap + const focused_node = @fieldParentPtr(std.TailQueue(Output).Node, "data", seat.focused_output); + seat.focused_output = switch (direction) { + .Next => if (focused_node.next) |node| &node.data else &root.outputs.first.?.data, + .Prev => if (focused_node.prev) |node| &node.data else &root.outputs.last.?.data, + }; + + seat.focus(null); +} diff --git a/src/command/focus_tags.zig b/src/command/focus_tags.zig new file mode 100644 index 0000000..941a129 --- /dev/null +++ b/src/command/focus_tags.zig @@ -0,0 +1,11 @@ +const c = @import("../c.zig"); + +const Arg = @import("../command.zig").Arg; +const Seat = @import("../seat.zig").Seat; + +/// Switch focus to the passed tags. +pub fn focusTags(seat: *Seat, arg: Arg) void { + const tags = arg.uint; + seat.focused_output.pending_focused_tags = tags; + seat.input_manager.server.root.arrange(); +} diff --git a/src/command/focus_view.zig b/src/command/focus_view.zig new file mode 100644 index 0000000..ec51b83 --- /dev/null +++ b/src/command/focus_view.zig @@ -0,0 +1,37 @@ +const c = @import("../c.zig"); + +const Arg = @import("../command.zig").Arg; +const Seat = @import("../seat.zig").Seat; +const View = @import("../view.zig").View; +const ViewStack = @import("../view_stack.zig").ViewStack; + +/// Focus either the next or the previous visible view, depending on the enum +/// passed. Does nothing if there are 1 or 0 views in the stack. +pub fn focusView(seat: *Seat, arg: Arg) void { + const direction = arg.direction; + const output = seat.focused_output; + if (seat.focused_view) |current_focus| { + // 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); + var it = switch (direction) { + .Next => ViewStack(View).iterator(focused_node, output.current_focused_tags), + .Prev => ViewStack(View).reverseIterator(focused_node, output.current_focused_tags), + }; + + // Skip past the focused node + _ = it.next(); + // Focus the next visible node if there is one + if (it.next()) |node| { + seat.focus(&node.view); + return; + } + } + + // There is either no currently focused view or the last visible view in the + // stack is focused and we need to wrap. + var it = switch (direction) { + .Next => ViewStack(View).iterator(output.views.first, output.current_focused_tags), + .Prev => ViewStack(View).reverseIterator(output.views.last, output.current_focused_tags), + }; + seat.focus(if (it.next()) |node| &node.view else null); +} diff --git a/src/command/modify_master_count.zig b/src/command/modify_master_count.zig new file mode 100644 index 0000000..6c64091 --- /dev/null +++ b/src/command/modify_master_count.zig @@ -0,0 +1,16 @@ +const c = @import("../c.zig"); +const std = @import("std"); + +const Arg = @import("../command.zig").Arg; +const Seat = @import("../seat.zig").Seat; + +/// Modify the number of master views +pub fn modifyMasterCount(seat: *Seat, arg: Arg) void { + const delta = arg.int; + const output = seat.focused_output; + output.master_count = @intCast( + u32, + std.math.max(0, @intCast(i32, output.master_count) + delta), + ); + seat.input_manager.server.root.arrange(); +} diff --git a/src/command/modify_master_factor.zig b/src/command/modify_master_factor.zig new file mode 100644 index 0000000..c522485 --- /dev/null +++ b/src/command/modify_master_factor.zig @@ -0,0 +1,19 @@ +const c = @import("../c.zig"); +const std = @import("std"); + +const Arg = @import("../command.zig").Arg; +const Seat = @import("../seat.zig").Seat; + +/// Modify the percent of the width of the screen that the master views occupy. +pub fn modifyMasterFactor(seat: *Seat, arg: Arg) void { + const delta = arg.float; + const output = seat.focused_output; + const new_master_factor = std.math.min( + std.math.max(output.master_factor + delta, 0.05), + 0.95, + ); + if (new_master_factor != output.master_factor) { + output.master_factor = new_master_factor; + seat.input_manager.server.root.arrange(); + } +} diff --git a/src/command/set_view_tags.zig b/src/command/set_view_tags.zig new file mode 100644 index 0000000..7bc4d87 --- /dev/null +++ b/src/command/set_view_tags.zig @@ -0,0 +1,15 @@ +const c = @import("../c.zig"); + +const Arg = @import("../command.zig").Arg; +const Seat = @import("../seat.zig").Seat; + +/// Set the tags of the focused view. +pub fn setViewTags(seat: *Seat, arg: Arg) void { + const tags = arg.uint; + if (seat.focused_view) |view| { + if (view.current_tags != tags) { + view.pending_tags = tags; + seat.input_manager.server.root.arrange(); + } + } +} diff --git a/src/command/spawn.zig b/src/command/spawn.zig new file mode 100644 index 0000000..495cb94 --- /dev/null +++ b/src/command/spawn.zig @@ -0,0 +1,21 @@ +const c = @import("../c.zig"); +const std = @import("std"); + +const Arg = @import("../command.zig").Arg; +const Log = @import("../log.zig").Log; +const Seat = @import("../seat.zig").Seat; + +/// Spawn a program. +pub fn spawn(seat: *Seat, arg: Arg) void { + const cmd = arg.str; + + const argv = [_][]const u8{ "/bin/sh", "-c", cmd }; + const child = std.ChildProcess.init(&argv, std.heap.c_allocator) catch |err| { + Log.Error.log("Failed to execute {}: {}", .{ cmd, err }); + return; + }; + std.ChildProcess.spawn(child) catch |err| { + Log.Error.log("Failed to execute {}: {}", .{ cmd, err }); + return; + }; +} diff --git a/src/command/toggle_tags.zig b/src/command/toggle_tags.zig new file mode 100644 index 0000000..16bf4e5 --- /dev/null +++ b/src/command/toggle_tags.zig @@ -0,0 +1,15 @@ +const c = @import("../c.zig"); + +const Arg = @import("../command.zig").Arg; +const Seat = @import("../seat.zig").Seat; + +/// Toggle focus of the passsed tags. +pub fn toggleTags(seat: *Seat, arg: Arg) void { + const tags = arg.uint; + const output = seat.focused_output; + const new_focused_tags = output.current_focused_tags ^ tags; + if (new_focused_tags != 0) { + output.pending_focused_tags = new_focused_tags; + seat.input_manager.server.root.arrange(); + } +} diff --git a/src/command/toggle_view_tags.zig b/src/command/toggle_view_tags.zig new file mode 100644 index 0000000..a4845f1 --- /dev/null +++ b/src/command/toggle_view_tags.zig @@ -0,0 +1,16 @@ +const c = @import("../c.zig"); + +const Arg = @import("../command.zig").Arg; +const Seat = @import("../seat.zig").Seat; + +/// Toggle the passed tags of the focused view +pub fn toggleViewTags(seat: *Seat, arg: Arg) void { + const tags = arg.uint; + if (seat.focused_view) |view| { + const new_tags = view.current_tags ^ tags; + if (new_tags != 0) { + view.pending_tags = new_tags; + seat.input_manager.server.root.arrange(); + } + } +} diff --git a/src/command/zoom.zig b/src/command/zoom.zig new file mode 100644 index 0000000..507620d --- /dev/null +++ b/src/command/zoom.zig @@ -0,0 +1,20 @@ +const c = @import("../c.zig"); + +const Arg = @import("../command.zig").Arg; +const Seat = @import("../seat.zig").Seat; +const View = @import("../view.zig").View; +const ViewStack = @import("../view_stack.zig").ViewStack; + +/// Bump the focused view to the top of the stack. +/// TODO: if the top of the stack is focused, bump the next visible view. +pub fn zoom(seat: *Seat, arg: Arg) void { + if (seat.focused_view) |current_focus| { + const output = seat.focused_output; + const node = @fieldParentPtr(ViewStack(View).Node, "view", current_focus); + if (node != output.views.first) { + output.views.remove(node); + output.views.push(node); + seat.input_manager.server.root.arrange(); + } + } +} diff --git a/src/config.zig b/src/config.zig index f927aca..01d217d 100644 --- a/src/config.zig +++ b/src/config.zig @@ -36,13 +36,28 @@ pub const Config = struct { const mod = c.WLR_MODIFIER_LOGO; // Mod+Shift+Return to start an instance of alacritty - try self.keybinds.append(Keybind{ .keysym = c.XKB_KEY_Return, .modifiers = mod | c.WLR_MODIFIER_SHIFT, .command = command.spawn, .arg = .{ .str = "alacritty" } }); + try self.keybinds.append(Keybind{ + .keysym = c.XKB_KEY_Return, + .modifiers = mod | c.WLR_MODIFIER_SHIFT, + .command = command.spawn, + .arg = .{ .str = "alacritty" }, + }); // Mod+Q to close the focused view - try self.keybinds.append(Keybind{ .keysym = c.XKB_KEY_q, .modifiers = mod, .command = command.close, .arg = .{ .none = {} } }); + try self.keybinds.append(Keybind{ + .keysym = c.XKB_KEY_q, + .modifiers = mod, + .command = command.close_view, + .arg = .{ .none = {} }, + }); // Mod+E to exit river - try self.keybinds.append(Keybind{ .keysym = c.XKB_KEY_e, .modifiers = mod, .command = command.exitCompositor, .arg = .{ .none = {} } }); + try self.keybinds.append(Keybind{ + .keysym = c.XKB_KEY_e, + .modifiers = mod, + .command = command.exitCompositor, + .arg = .{ .none = {} }, + }); // Mod+J and Mod+K to focus the next/previous view in the layout stack try self.keybinds.append( @@ -60,16 +75,43 @@ pub const Config = struct { .arg = .{ .direction = .Prev }, }); - // Mod+Return to bump the focused view to the top of the layout stack, making it the new master - try self.keybinds.append(Keybind{ .keysym = c.XKB_KEY_Return, .modifiers = mod, .command = command.zoom, .arg = .{ .none = {} } }); + // Mod+Return to bump the focused view to the top of the layout stack, + // making it the new master + try self.keybinds.append(Keybind{ + .keysym = c.XKB_KEY_Return, + .modifiers = mod, + .command = command.zoom, + .arg = .{ .none = {} }, + }); // Mod+H and Mod+L to increase/decrease the width of the master column - try self.keybinds.append(Keybind{ .keysym = c.XKB_KEY_h, .modifiers = mod, .command = command.modifyMasterFactor, .arg = .{ .float = 0.05 } }); - try self.keybinds.append(Keybind{ .keysym = c.XKB_KEY_l, .modifiers = mod, .command = command.modifyMasterFactor, .arg = .{ .float = -0.05 } }); + try self.keybinds.append(Keybind{ + .keysym = c.XKB_KEY_h, + .modifiers = mod, + .command = command.modifyMasterFactor, + .arg = .{ .float = 0.05 }, + }); + try self.keybinds.append(Keybind{ + .keysym = c.XKB_KEY_l, + .modifiers = mod, + .command = command.modifyMasterFactor, + .arg = .{ .float = -0.05 }, + }); - // Mod+Shift+H and Mod+Shift+L to increment/decrement the number of master views in the layout - try self.keybinds.append(Keybind{ .keysym = c.XKB_KEY_h, .modifiers = mod | c.WLR_MODIFIER_SHIFT, .command = command.modifyMasterCount, .arg = .{ .int = 1 } }); - try self.keybinds.append(Keybind{ .keysym = c.XKB_KEY_l, .modifiers = mod | c.WLR_MODIFIER_SHIFT, .command = command.modifyMasterCount, .arg = .{ .int = -1 } }); + // Mod+Shift+H and Mod+Shift+L to increment/decrement the number of + // master views in the layout + try self.keybinds.append(Keybind{ + .keysym = c.XKB_KEY_h, + .modifiers = mod | c.WLR_MODIFIER_SHIFT, + .command = command.modifyMasterCount, + .arg = .{ .int = 1 }, + }); + try self.keybinds.append(Keybind{ + .keysym = c.XKB_KEY_l, + .modifiers = mod | c.WLR_MODIFIER_SHIFT, + .command = command.modifyMasterCount, + .arg = .{ .int = -1 }, + }); comptime var i = 0; inline while (i < 9) : (i += 1) { @@ -84,7 +126,7 @@ pub const Config = struct { try self.keybinds.append(Keybind{ .keysym = c.XKB_KEY_1 + i, .modifiers = mod | c.WLR_MODIFIER_SHIFT, - .command = command.setFocusedViewTags, + .command = command.setViewTags, .arg = .{ .uint = 1 << i }, }); // Mod+Ctrl+[1-9] to toggle focus of tag [1-9] @@ -98,16 +140,26 @@ pub const Config = struct { try self.keybinds.append(Keybind{ .keysym = c.XKB_KEY_1 + i, .modifiers = mod | c.WLR_MODIFIER_CTRL | c.WLR_MODIFIER_SHIFT, - .command = command.toggleFocusedViewTags, + .command = command.toggleViewTags, .arg = .{ .uint = 1 << i }, }); } // Mod+0 to focus all tags - try self.keybinds.append(Keybind{ .keysym = c.XKB_KEY_0, .modifiers = mod, .command = command.focusTags, .arg = .{ .uint = 0xFFFFFFFF } }); + try self.keybinds.append(Keybind{ + .keysym = c.XKB_KEY_0, + .modifiers = mod, + .command = command.focusTags, + .arg = .{ .uint = 0xFFFFFFFF }, + }); // 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 } }); + try self.keybinds.append(Keybind{ + .keysym = c.XKB_KEY_0, + .modifiers = mod | c.WLR_MODIFIER_SHIFT, + .command = command.setViewTags, + .arg = .{ .uint = 0xFFFFFFFF }, + }); // Mod+Period and Mod+Comma to focus the next/previous output try self.keybinds.append(Keybind{