1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
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,
Prev,
};
pub const Arg = union {
int: i32,
uint: u32,
float: f64,
str: []const u8,
direction: Direction,
none: void,
};
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();
}
}
|