2020-04-07 10:16:38 -07:00
|
|
|
const std = @import("std");
|
|
|
|
const c = @import("c.zig");
|
|
|
|
|
2020-04-08 12:31:07 -07:00
|
|
|
const Log = @import("log.zig").Log;
|
2020-04-13 10:25:37 -07:00
|
|
|
const Seat = @import("seat.zig").Seat;
|
2020-04-13 08:25:39 -07:00
|
|
|
const View = @import("view.zig").View;
|
2020-04-07 10:16:38 -07:00
|
|
|
const ViewStack = @import("view_stack.zig").ViewStack;
|
|
|
|
|
2020-04-07 12:48:56 -07:00
|
|
|
pub const Arg = union {
|
|
|
|
int: i32,
|
|
|
|
uint: u32,
|
|
|
|
float: f64,
|
2020-04-09 03:54:38 -07:00
|
|
|
str: []const u8,
|
2020-04-07 12:48:56 -07:00
|
|
|
none: void,
|
|
|
|
};
|
|
|
|
|
2020-04-13 10:25:37 -07:00
|
|
|
pub const Command = fn (seat: *Seat, arg: Arg) void;
|
2020-04-07 12:48:56 -07:00
|
|
|
|
2020-04-07 10:16:38 -07:00
|
|
|
/// Exit the compositor, terminating the wayland session.
|
2020-04-13 10:25:37 -07:00
|
|
|
pub fn exitCompositor(seat: *Seat, arg: Arg) void {
|
|
|
|
c.wl_display_terminate(seat.input_manager.server.wl_display);
|
2020-04-07 10:16:38 -07:00
|
|
|
}
|
|
|
|
|
2020-04-13 12:00:18 -07:00
|
|
|
/// Focus either the next or the previous visible view, depending on the bool
|
|
|
|
/// passed.
|
|
|
|
fn focusNextPrevView(seat: *Seat, next: bool) void {
|
2020-04-15 08:59:46 -07:00
|
|
|
const output = seat.focused_output;
|
2020-04-13 12:00:18 -07:00
|
|
|
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 = if (next)
|
|
|
|
ViewStack(View).iterator(focused_node, output.current_focused_tags)
|
|
|
|
else
|
|
|
|
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 = if (next)
|
|
|
|
ViewStack(View).iterator(output.views.first, output.current_focused_tags)
|
|
|
|
else
|
|
|
|
ViewStack(View).reverseIterator(output.views.last, output.current_focused_tags);
|
|
|
|
seat.focus(if (it.next()) |node| &node.view else null);
|
|
|
|
}
|
|
|
|
|
2020-04-13 10:25:37 -07:00
|
|
|
/// Focus the next visible view in the stack, wrapping if needed. Does
|
|
|
|
/// nothing if there is only one view in the stack.
|
|
|
|
pub fn focusNextView(seat: *Seat, arg: Arg) void {
|
2020-04-13 12:00:18 -07:00
|
|
|
focusNextPrevView(seat, true);
|
2020-04-07 10:16:38 -07:00
|
|
|
}
|
|
|
|
|
2020-04-13 10:25:37 -07:00
|
|
|
/// Focus the previous view in the stack, wrapping if needed. Does nothing
|
|
|
|
/// if there is only one view in the stack.
|
|
|
|
pub fn focusPrevView(seat: *Seat, arg: Arg) void {
|
2020-04-13 12:00:18 -07:00
|
|
|
focusNextPrevView(seat, false);
|
2020-04-07 10:16:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Modify the number of master views
|
2020-04-13 10:25:37 -07:00
|
|
|
pub fn modifyMasterCount(seat: *Seat, arg: Arg) void {
|
2020-04-07 12:48:56 -07:00
|
|
|
const delta = arg.int;
|
2020-04-15 08:59:46 -07:00
|
|
|
const output = seat.focused_output;
|
2020-04-11 05:24:20 -07:00
|
|
|
output.master_count = @intCast(
|
|
|
|
u32,
|
|
|
|
std.math.max(0, @intCast(i32, output.master_count) + delta),
|
|
|
|
);
|
2020-04-15 08:59:46 -07:00
|
|
|
seat.input_manager.server.root.arrange();
|
2020-04-07 10:16:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Modify the percent of the width of the screen that the master views occupy.
|
2020-04-13 10:25:37 -07:00
|
|
|
pub fn modifyMasterFactor(seat: *Seat, arg: Arg) void {
|
2020-04-07 12:48:56 -07:00
|
|
|
const delta = arg.float;
|
2020-04-15 08:59:46 -07:00
|
|
|
const output = seat.focused_output;
|
2020-04-07 10:16:38 -07:00
|
|
|
const new_master_factor = std.math.min(
|
2020-04-11 05:24:20 -07:00
|
|
|
std.math.max(output.master_factor + delta, 0.05),
|
2020-04-07 10:16:38 -07:00
|
|
|
0.95,
|
|
|
|
);
|
2020-04-11 05:24:20 -07:00
|
|
|
if (new_master_factor != output.master_factor) {
|
|
|
|
output.master_factor = new_master_factor;
|
2020-04-15 08:59:46 -07:00
|
|
|
seat.input_manager.server.root.arrange();
|
2020-04-07 10:16:38 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Bump the focused view to the top of the stack.
|
|
|
|
/// TODO: if the top of the stack is focused, bump the next visible view.
|
2020-04-13 10:25:37 -07:00
|
|
|
pub fn zoom(seat: *Seat, arg: Arg) void {
|
2020-04-13 12:00:18 -07:00
|
|
|
if (seat.focused_view) |current_focus| {
|
2020-04-15 08:59:46 -07:00
|
|
|
const output = seat.focused_output;
|
2020-04-13 12:00:18 -07:00
|
|
|
const node = @fieldParentPtr(ViewStack(View).Node, "view", current_focus);
|
|
|
|
if (node != output.views.first) {
|
|
|
|
output.views.remove(node);
|
|
|
|
output.views.push(node);
|
2020-04-15 08:59:46 -07:00
|
|
|
seat.input_manager.server.root.arrange();
|
2020-04-13 12:00:18 -07:00
|
|
|
}
|
|
|
|
}
|
2020-04-07 10:16:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Switch focus to the passed tags.
|
2020-04-13 10:25:37 -07:00
|
|
|
pub fn focusTags(seat: *Seat, arg: Arg) void {
|
2020-04-07 12:48:56 -07:00
|
|
|
const tags = arg.uint;
|
2020-04-15 08:59:46 -07:00
|
|
|
seat.focused_output.pending_focused_tags = tags;
|
|
|
|
seat.input_manager.server.root.arrange();
|
2020-04-07 10:16:38 -07:00
|
|
|
}
|
|
|
|
|
2020-04-07 15:40:41 -07:00
|
|
|
/// Toggle focus of the passsed tags.
|
2020-04-13 10:25:37 -07:00
|
|
|
pub fn toggleTags(seat: *Seat, arg: Arg) void {
|
2020-04-07 15:40:41 -07:00
|
|
|
const tags = arg.uint;
|
2020-04-15 08:59:46 -07:00
|
|
|
const output = seat.focused_output;
|
2020-04-11 05:24:20 -07:00
|
|
|
const new_focused_tags = output.current_focused_tags ^ tags;
|
2020-04-07 15:40:41 -07:00
|
|
|
if (new_focused_tags != 0) {
|
2020-04-11 05:24:20 -07:00
|
|
|
output.pending_focused_tags = new_focused_tags;
|
2020-04-15 08:59:46 -07:00
|
|
|
seat.input_manager.server.root.arrange();
|
2020-04-07 15:40:41 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-07 10:16:38 -07:00
|
|
|
/// Set the tags of the focused view.
|
2020-04-13 10:25:37 -07:00
|
|
|
pub fn setFocusedViewTags(seat: *Seat, arg: Arg) void {
|
2020-04-13 12:00:18 -07:00
|
|
|
const tags = arg.uint;
|
|
|
|
if (seat.focused_view) |view| {
|
|
|
|
if (view.current_tags != tags) {
|
|
|
|
view.pending_tags = tags;
|
|
|
|
seat.input_manager.server.root.arrange();
|
|
|
|
}
|
|
|
|
}
|
2020-04-07 10:16:38 -07:00
|
|
|
}
|
|
|
|
|
2020-04-08 03:44:41 -07:00
|
|
|
/// Toggle the passed tags of the focused view
|
2020-04-13 10:25:37 -07:00
|
|
|
pub fn toggleFocusedViewTags(seat: *Seat, arg: Arg) void {
|
2020-04-13 12:00:18 -07:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
2020-04-08 03:44:41 -07:00
|
|
|
}
|
|
|
|
|
2020-04-07 10:16:38 -07:00
|
|
|
/// Spawn a program.
|
2020-04-13 10:25:37 -07:00
|
|
|
pub fn spawn(seat: *Seat, arg: Arg) void {
|
2020-04-09 03:54:38 -07:00
|
|
|
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;
|
|
|
|
};
|
2020-04-07 10:16:38 -07:00
|
|
|
}
|
2020-04-08 08:43:00 -07:00
|
|
|
|
|
|
|
/// Close the focused view, if any.
|
2020-04-13 10:25:37 -07:00
|
|
|
pub fn close(seat: *Seat, arg: Arg) void {
|
2020-04-13 12:00:18 -07:00
|
|
|
if (seat.focused_view) |view| {
|
|
|
|
view.close();
|
|
|
|
}
|
2020-04-08 08:43:00 -07:00
|
|
|
}
|