Move commands to a separate file

This commit is contained in:
Isaac Freund 2020-04-07 19:16:38 +02:00
parent 626b2c691b
commit 15f97314a9
No known key found for this signature in database
GPG key ID: 86DED400DDFD7A11
2 changed files with 100 additions and 105 deletions

78
src/command.zig Normal file
View file

@ -0,0 +1,78 @@
const std = @import("std");
const c = @import("c.zig");
const Server = @import("server.zig").Server;
const ViewStack = @import("view_stack.zig").ViewStack;
/// Exit the compositor, terminating the wayland session.
pub fn exitCompositor(server: *Server) void {
c.wl_display_terminate(server.wl_display);
}
/// Shift focus to the next visible view, wrapping if needed.
pub fn focusNextView(server: *Server) void {
server.root.focusNextView();
}
/// Shift focus to the previous visible view, wrapping if needed.
pub fn focusPrevView(server: *Server) void {
server.root.focusPrevView();
}
/// Modify the number of master views
pub fn modifyMasterCount(server: *Server, delta: i32) void {
server.root.master_count = @intCast(u32, std.math.max(
0,
@intCast(i32, server.root.master_count) + delta,
));
server.root.arrange();
}
/// Modify the percent of the width of the screen that the master views occupy.
pub fn modifyMasterFactor(server: *Server, delta: f64) void {
const new_master_factor = std.math.min(
std.math.max(server.root.master_factor + delta, 0.05),
0.95,
);
if (new_master_factor != server.root.master_factor) {
server.root.master_factor = new_master_factor;
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(server: *Server) void {
if (server.root.focused_view) |current_focus| {
const node = @fieldParentPtr(ViewStack.Node, "view", current_focus);
if (node != server.root.views.first) {
server.root.views.remove(node);
server.root.views.push(node);
server.root.arrange();
}
}
}
/// Switch focus to the passed tags.
pub fn focusTags(server: *Server, tags: u32) void {
server.root.pending_focused_tags = tags;
server.root.arrange();
}
/// Set the tags of the focused view.
pub fn setFocusedViewTags(server: *Server, tags: u32) void {
if (server.root.focused_view) |view| {
if (view.current_tags != tags) {
view.pending_tags = tags;
server.root.arrange();
}
}
}
/// Spawn a program.
/// TODO: make this take a program as a paramter and spawn that
pub fn spawn(server: *Server) void {
const argv = [_][]const u8{ "/bin/sh", "-c", "alacritty" };
const child = std.ChildProcess.init(&argv, std.heap.c_allocator) catch unreachable;
std.ChildProcess.spawn(child) catch unreachable;
}

View file

@ -1,6 +1,6 @@
const std = @import("std");
const c = @import("c.zig");
const util = @import("util.zig");
const command = @import("command.zig");
const DecorationManager = @import("decoration_manager.zig").DecorationManager;
const Output = @import("output.zig").Output;
@ -120,114 +120,31 @@ pub const Server = struct {
}
if (modifiers & @intCast(u32, c.WLR_MODIFIER_SHIFT) != 0) {
switch (sym) {
c.XKB_KEY_H => {
//if (self.root.master_count < self.root.views.len) {
self.root.master_count += 1;
self.root.arrange();
//}
},
c.XKB_KEY_L => {
//if (self.root.master_count > 0) {
self.root.master_count -= 1;
self.root.arrange();
//}
},
c.XKB_KEY_Return => {
// Spawn an instance of alacritty
// const argv = [_][]const u8{ "/bin/sh", "-c", "WAYLAND_DEBUG=1 alacritty" };
const argv = [_][]const u8{ "/bin/sh", "-c", "alacritty" };
const child = std.ChildProcess.init(&argv, std.heap.c_allocator) catch unreachable;
std.ChildProcess.spawn(child) catch unreachable;
},
c.XKB_KEY_1 => {
if (self.root.focused_view) |view| {
view.pending_tags = 1 << 0;
self.root.arrange();
}
},
c.XKB_KEY_2 => {
if (self.root.focused_view) |view| {
view.pending_tags = 1 << 1;
self.root.arrange();
}
},
c.XKB_KEY_3 => {
if (self.root.focused_view) |view| {
view.pending_tags = 1 << 2;
self.root.arrange();
}
},
c.XKB_KEY_4 => {
if (self.root.focused_view) |view| {
view.pending_tags = 1 << 3;
self.root.arrange();
}
},
c.XKB_KEY_5 => {
if (self.root.focused_view) |view| {
view.pending_tags = 1 << 4;
self.root.arrange();
}
},
c.XKB_KEY_6 => {
if (self.root.focused_view) |view| {
view.pending_tags = 1 << 5;
self.root.arrange();
}
},
c.XKB_KEY_H => command.modifyMasterCount(self, 1),
c.XKB_KEY_L => command.modifyMasterCount(self, -1),
c.XKB_KEY_Return => command.spawn(self),
c.XKB_KEY_1 => command.setFocusedViewTags(self, 1 << 0),
c.XKB_KEY_2 => command.setFocusedViewTags(self, 1 << 1),
c.XKB_KEY_3 => command.setFocusedViewTags(self, 1 << 2),
c.XKB_KEY_4 => command.setFocusedViewTags(self, 1 << 3),
c.XKB_KEY_5 => command.setFocusedViewTags(self, 1 << 4),
c.XKB_KEY_6 => command.setFocusedViewTags(self, 1 << 5),
else => return false,
}
} else {
switch (sym) {
c.XKB_KEY_e => c.wl_display_terminate(self.wl_display),
c.XKB_KEY_j => self.root.focusNextView(),
c.XKB_KEY_k => self.root.focusPrevView(),
c.XKB_KEY_h => {
if (self.root.master_factor > 0.05) {
self.root.master_factor = util.max(f64, self.root.master_factor - 0.05, 0.05);
self.root.arrange();
}
},
c.XKB_KEY_l => {
if (self.root.master_factor < 0.95) {
self.root.master_factor = util.min(f64, self.root.master_factor + 0.05, 0.95);
self.root.arrange();
}
},
c.XKB_KEY_Return => {
if (self.root.focused_view) |current_focus| {
const node = @fieldParentPtr(ViewStack.Node, "view", current_focus);
if (node != self.root.views.first) {
self.root.views.remove(node);
self.root.views.push(node);
self.root.arrange();
}
}
},
c.XKB_KEY_1 => {
self.root.pending_focused_tags = 1 << 0;
self.root.arrange();
},
c.XKB_KEY_2 => {
self.root.pending_focused_tags = 1 << 1;
self.root.arrange();
},
c.XKB_KEY_3 => {
self.root.pending_focused_tags = 1 << 2;
self.root.arrange();
},
c.XKB_KEY_4 => {
self.root.pending_focused_tags = 1 << 3;
self.root.arrange();
},
c.XKB_KEY_5 => {
self.root.pending_focused_tags = 1 << 4;
self.root.arrange();
},
c.XKB_KEY_6 => {
self.root.pending_focused_tags = 1 << 5;
self.root.arrange();
},
c.XKB_KEY_e => command.exitCompositor(self),
c.XKB_KEY_j => command.focusNextView(self),
c.XKB_KEY_k => command.focusPrevView(self),
c.XKB_KEY_h => command.modifyMasterFactor(self, 0.05),
c.XKB_KEY_l => command.modifyMasterFactor(self, -0.05),
c.XKB_KEY_Return => command.zoom(self),
c.XKB_KEY_1 => command.focusTags(self, 1 << 0),
c.XKB_KEY_2 => command.focusTags(self, 1 << 1),
c.XKB_KEY_3 => command.focusTags(self, 1 << 2),
c.XKB_KEY_4 => command.focusTags(self, 1 << 3),
c.XKB_KEY_5 => command.focusTags(self, 1 << 4),
c.XKB_KEY_6 => command.focusTags(self, 1 << 5),
else => return false,
}
}