2020-04-03 09:53:36 -07:00
|
|
|
const View = @import("view.zig").View;
|
|
|
|
|
|
|
|
/// A specialized doubly-linked stack that allows for filtered iteration
|
2020-04-13 08:25:39 -07:00
|
|
|
/// over the nodes. T must be View or *View.
|
|
|
|
pub fn ViewStack(comptime T: type) type {
|
|
|
|
if (!(T == View or T == *View)) {
|
|
|
|
@compileError("ViewStack: T must be View or *View");
|
|
|
|
}
|
|
|
|
return struct {
|
|
|
|
const Self = @This();
|
2020-04-03 09:53:36 -07:00
|
|
|
|
2020-04-13 08:25:39 -07:00
|
|
|
pub const Node = struct {
|
|
|
|
/// Previous/next nodes in the stack
|
|
|
|
prev: ?*Node,
|
|
|
|
next: ?*Node,
|
2020-04-03 09:53:36 -07:00
|
|
|
|
2020-04-13 08:25:39 -07:00
|
|
|
/// The view stored in this node
|
|
|
|
view: T,
|
|
|
|
};
|
2020-04-03 09:53:36 -07:00
|
|
|
|
2020-04-13 08:25:39 -07:00
|
|
|
/// Top/bottom nodes in the stack
|
|
|
|
first: ?*Node,
|
|
|
|
last: ?*Node,
|
2020-04-03 09:53:36 -07:00
|
|
|
|
2020-04-13 08:25:39 -07:00
|
|
|
/// Initialize an undefined stack
|
|
|
|
pub fn init(self: *Self) void {
|
|
|
|
self.first = null;
|
|
|
|
self.last = null;
|
2020-04-03 09:53:36 -07:00
|
|
|
}
|
|
|
|
|
2020-04-13 08:25:39 -07:00
|
|
|
/// Add a node to the top of the stack.
|
|
|
|
pub fn push(self: *Self, new_node: *Node) void {
|
|
|
|
// Set the prev/next pointers of the new node
|
|
|
|
new_node.prev = null;
|
|
|
|
new_node.next = self.first;
|
|
|
|
|
|
|
|
if (self.first) |first| {
|
|
|
|
// If the list is not empty, set the prev pointer of the current
|
|
|
|
// first node to the new node.
|
|
|
|
first.prev = new_node;
|
|
|
|
} else {
|
|
|
|
// If the list is empty set the last pointer to the new node.
|
|
|
|
self.last = new_node;
|
|
|
|
}
|
2020-04-03 09:53:36 -07:00
|
|
|
|
2020-04-13 08:25:39 -07:00
|
|
|
// Set the first pointer to the new node
|
|
|
|
self.first = new_node;
|
2020-04-03 09:53:36 -07:00
|
|
|
}
|
|
|
|
|
2020-04-13 08:25:39 -07:00
|
|
|
/// Remove a node from the view stack. This removes it from the stack of
|
|
|
|
/// all views as well as the stack of visible ones.
|
|
|
|
pub fn remove(self: *Self, target_node: *Node) void {
|
|
|
|
// Set the previous node/list head to the next pointer
|
|
|
|
if (target_node.prev) |prev_node| {
|
|
|
|
prev_node.next = target_node.next;
|
|
|
|
} else {
|
|
|
|
self.first = target_node.next;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the next node/list tail to the previous pointer
|
|
|
|
if (target_node.next) |next_node| {
|
|
|
|
next_node.prev = target_node.prev;
|
|
|
|
} else {
|
|
|
|
self.last = target_node.prev;
|
|
|
|
}
|
2020-04-03 09:53:36 -07:00
|
|
|
}
|
|
|
|
|
2020-04-13 08:25:39 -07:00
|
|
|
const Iterator = struct {
|
|
|
|
it: ?*Node,
|
|
|
|
tags: u32,
|
|
|
|
reverse: bool,
|
|
|
|
pending: bool,
|
|
|
|
|
|
|
|
/// Returns the next node in iteration order, or null if done.
|
|
|
|
/// This function is horribly ugly, but it's well tested below.
|
|
|
|
pub fn next(self: *Iterator) ?*Node {
|
|
|
|
while (self.it) |node| : (self.it = if (self.reverse) node.prev else node.next) {
|
2020-04-25 12:40:26 -07:00
|
|
|
if (node.view.wlr_surface != null and if (self.pending)
|
2020-04-13 08:25:39 -07:00
|
|
|
if (node.view.pending_tags) |pending_tags|
|
|
|
|
self.tags & pending_tags != 0
|
|
|
|
else
|
|
|
|
self.tags & node.view.current_tags != 0
|
2020-04-03 09:53:36 -07:00
|
|
|
else
|
2020-04-13 08:25:39 -07:00
|
|
|
self.tags & node.view.current_tags != 0) {
|
|
|
|
self.it = if (self.reverse) node.prev else node.next;
|
|
|
|
return node;
|
|
|
|
}
|
2020-04-03 09:53:36 -07:00
|
|
|
}
|
2020-04-13 08:25:39 -07:00
|
|
|
return null;
|
2020-04-03 09:53:36 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-04-13 08:25:39 -07:00
|
|
|
/// Returns an iterator starting at the passed node and filtered by
|
|
|
|
/// checking the passed tags against the current tags of each view.
|
|
|
|
/// Unmapped views are skipped.
|
|
|
|
pub fn iterator(start: ?*Node, tags: u32) Iterator {
|
|
|
|
return Iterator{
|
|
|
|
.it = start,
|
|
|
|
.tags = tags,
|
|
|
|
.reverse = false,
|
|
|
|
.pending = false,
|
|
|
|
};
|
|
|
|
}
|
2020-04-03 09:53:36 -07:00
|
|
|
|
2020-04-13 08:25:39 -07:00
|
|
|
/// Returns a reverse iterator starting at the passed node and filtered by
|
|
|
|
/// checking the passed tags against the current tags of each view.
|
|
|
|
/// Unmapped views are skipped.
|
|
|
|
pub fn reverseIterator(start: ?*Node, tags: u32) Iterator {
|
|
|
|
return Iterator{
|
|
|
|
.it = start,
|
|
|
|
.tags = tags,
|
|
|
|
.reverse = true,
|
|
|
|
.pending = false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns an iterator starting at the passed node and filtered by
|
|
|
|
/// checking the passed tags against the pending tags of each view.
|
|
|
|
/// If a view has no pending tags, the current tags are used. Unmapped
|
|
|
|
/// views are skipped.
|
|
|
|
pub fn pendingIterator(start: ?*Node, tags: u32) Iterator {
|
|
|
|
return Iterator{
|
|
|
|
.it = start,
|
|
|
|
.tags = tags,
|
|
|
|
.reverse = false,
|
|
|
|
.pending = true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2020-04-03 09:53:36 -07:00
|
|
|
|
|
|
|
const testing = @import("std").testing;
|
|
|
|
|
2020-04-13 08:25:39 -07:00
|
|
|
test "push/remove (*View)" {
|
2020-04-03 09:53:36 -07:00
|
|
|
const allocator = testing.allocator;
|
|
|
|
|
2020-04-13 08:25:39 -07:00
|
|
|
var views: ViewStack(*View) = undefined;
|
2020-04-03 09:53:36 -07:00
|
|
|
views.init();
|
|
|
|
|
2020-04-13 08:25:39 -07:00
|
|
|
const one = try allocator.create(ViewStack(*View).Node);
|
2020-04-03 09:53:36 -07:00
|
|
|
defer allocator.destroy(one);
|
2020-04-13 08:25:39 -07:00
|
|
|
const two = try allocator.create(ViewStack(*View).Node);
|
2020-04-03 09:53:36 -07:00
|
|
|
defer allocator.destroy(two);
|
2020-04-13 08:25:39 -07:00
|
|
|
const three = try allocator.create(ViewStack(*View).Node);
|
2020-04-03 09:53:36 -07:00
|
|
|
defer allocator.destroy(three);
|
2020-04-13 08:25:39 -07:00
|
|
|
const four = try allocator.create(ViewStack(*View).Node);
|
2020-04-03 09:53:36 -07:00
|
|
|
defer allocator.destroy(four);
|
2020-04-13 08:25:39 -07:00
|
|
|
const five = try allocator.create(ViewStack(*View).Node);
|
2020-04-03 09:53:36 -07:00
|
|
|
defer allocator.destroy(five);
|
|
|
|
|
|
|
|
views.push(three); // {3}
|
|
|
|
views.push(one); // {1, 3}
|
|
|
|
views.push(four); // {4, 1, 3}
|
|
|
|
views.push(five); // {5, 4, 1, 3}
|
|
|
|
views.push(two); // {2, 5, 4, 1, 3}
|
|
|
|
|
|
|
|
// Simple insertion
|
|
|
|
{
|
|
|
|
var it = views.first;
|
|
|
|
testing.expect(it == two);
|
|
|
|
it = it.?.next;
|
|
|
|
testing.expect(it == five);
|
|
|
|
it = it.?.next;
|
|
|
|
testing.expect(it == four);
|
|
|
|
it = it.?.next;
|
|
|
|
testing.expect(it == one);
|
|
|
|
it = it.?.next;
|
|
|
|
testing.expect(it == three);
|
|
|
|
it = it.?.next;
|
|
|
|
|
|
|
|
testing.expect(it == null);
|
|
|
|
|
|
|
|
testing.expect(views.first == two);
|
|
|
|
testing.expect(views.last == three);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Removal of first
|
|
|
|
views.remove(two);
|
|
|
|
{
|
|
|
|
var it = views.first;
|
|
|
|
testing.expect(it == five);
|
|
|
|
it = it.?.next;
|
|
|
|
testing.expect(it == four);
|
|
|
|
it = it.?.next;
|
|
|
|
testing.expect(it == one);
|
|
|
|
it = it.?.next;
|
|
|
|
testing.expect(it == three);
|
|
|
|
it = it.?.next;
|
|
|
|
|
|
|
|
testing.expect(it == null);
|
|
|
|
|
|
|
|
testing.expect(views.first == five);
|
|
|
|
testing.expect(views.last == three);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Removal of last
|
|
|
|
views.remove(three);
|
|
|
|
{
|
|
|
|
var it = views.first;
|
|
|
|
testing.expect(it == five);
|
|
|
|
it = it.?.next;
|
|
|
|
testing.expect(it == four);
|
|
|
|
it = it.?.next;
|
|
|
|
testing.expect(it == one);
|
|
|
|
it = it.?.next;
|
|
|
|
|
|
|
|
testing.expect(it == null);
|
|
|
|
|
|
|
|
testing.expect(views.first == five);
|
|
|
|
testing.expect(views.last == one);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove from middle
|
|
|
|
views.remove(four);
|
|
|
|
{
|
|
|
|
var it = views.first;
|
|
|
|
testing.expect(it == five);
|
|
|
|
it = it.?.next;
|
|
|
|
testing.expect(it == one);
|
|
|
|
it = it.?.next;
|
|
|
|
|
|
|
|
testing.expect(it == null);
|
|
|
|
|
|
|
|
testing.expect(views.first == five);
|
|
|
|
testing.expect(views.last == one);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reinsertion
|
|
|
|
views.push(two);
|
|
|
|
views.push(three);
|
|
|
|
views.push(four);
|
|
|
|
{
|
|
|
|
var it = views.first;
|
|
|
|
testing.expect(it == four);
|
|
|
|
it = it.?.next;
|
|
|
|
testing.expect(it == three);
|
|
|
|
it = it.?.next;
|
|
|
|
testing.expect(it == two);
|
|
|
|
it = it.?.next;
|
|
|
|
testing.expect(it == five);
|
|
|
|
it = it.?.next;
|
|
|
|
testing.expect(it == one);
|
|
|
|
it = it.?.next;
|
|
|
|
|
|
|
|
testing.expect(it == null);
|
|
|
|
|
|
|
|
testing.expect(views.first == four);
|
|
|
|
testing.expect(views.last == one);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear
|
|
|
|
views.remove(four);
|
|
|
|
views.remove(two);
|
|
|
|
views.remove(three);
|
|
|
|
views.remove(one);
|
|
|
|
views.remove(five);
|
|
|
|
|
|
|
|
testing.expect(views.first == null);
|
|
|
|
testing.expect(views.last == null);
|
|
|
|
}
|
|
|
|
|
2020-04-13 08:25:39 -07:00
|
|
|
test "iteration (View)" {
|
2020-04-03 09:53:36 -07:00
|
|
|
const allocator = testing.allocator;
|
|
|
|
|
2020-04-13 08:25:39 -07:00
|
|
|
var views: ViewStack(View) = undefined;
|
2020-04-03 09:53:36 -07:00
|
|
|
views.init();
|
|
|
|
|
2020-04-13 08:25:39 -07:00
|
|
|
const one_a_pb = try allocator.create(ViewStack(View).Node);
|
2020-04-03 09:53:36 -07:00
|
|
|
defer allocator.destroy(one_a_pb);
|
|
|
|
one_a_pb.view.mapped = true;
|
|
|
|
one_a_pb.view.current_tags = 1 << 0;
|
|
|
|
one_a_pb.view.pending_tags = 1 << 1;
|
|
|
|
|
2020-04-13 08:25:39 -07:00
|
|
|
const two_a = try allocator.create(ViewStack(View).Node);
|
2020-04-03 09:53:36 -07:00
|
|
|
defer allocator.destroy(two_a);
|
|
|
|
two_a.view.mapped = true;
|
|
|
|
two_a.view.current_tags = 1 << 0;
|
2020-04-21 05:44:26 -07:00
|
|
|
two_a.view.pending_tags = null;
|
2020-04-03 09:53:36 -07:00
|
|
|
|
2020-04-13 08:25:39 -07:00
|
|
|
const three_b_pa = try allocator.create(ViewStack(View).Node);
|
2020-04-03 09:53:36 -07:00
|
|
|
defer allocator.destroy(three_b_pa);
|
|
|
|
three_b_pa.view.mapped = true;
|
|
|
|
three_b_pa.view.current_tags = 1 << 1;
|
|
|
|
three_b_pa.view.pending_tags = 1 << 0;
|
|
|
|
|
2020-04-13 08:25:39 -07:00
|
|
|
const four_b = try allocator.create(ViewStack(View).Node);
|
2020-04-03 09:53:36 -07:00
|
|
|
defer allocator.destroy(four_b);
|
|
|
|
four_b.view.mapped = true;
|
|
|
|
four_b.view.current_tags = 1 << 1;
|
2020-04-21 05:44:26 -07:00
|
|
|
four_b.view.pending_tags = null;
|
2020-04-03 09:53:36 -07:00
|
|
|
|
2020-04-13 08:25:39 -07:00
|
|
|
const five_b = try allocator.create(ViewStack(View).Node);
|
2020-04-03 09:53:36 -07:00
|
|
|
defer allocator.destroy(five_b);
|
|
|
|
five_b.view.mapped = true;
|
|
|
|
five_b.view.current_tags = 1 << 1;
|
2020-04-21 05:44:26 -07:00
|
|
|
five_b.view.pending_tags = null;
|
2020-04-03 09:53:36 -07:00
|
|
|
|
2020-04-13 08:25:39 -07:00
|
|
|
const unmapped_1 = try allocator.create(ViewStack(View).Node);
|
2020-04-04 07:23:51 -07:00
|
|
|
defer allocator.destroy(unmapped_1);
|
|
|
|
unmapped_1.view.mapped = false;
|
|
|
|
|
2020-04-03 09:53:36 -07:00
|
|
|
views.push(three_b_pa); // {3}
|
|
|
|
views.push(one_a_pb); // {1, 3}
|
2020-04-04 07:23:51 -07:00
|
|
|
views.push(unmapped_1); // {u, 1, 3}
|
|
|
|
views.push(four_b); // {4, u, 1, 3}
|
|
|
|
views.push(five_b); // {5, 4, u, 1, 3}
|
|
|
|
views.push(two_a); // {2, 5, 4, u, 1, 3}
|
2020-04-03 09:53:36 -07:00
|
|
|
|
|
|
|
// Iteration over all tags
|
|
|
|
{
|
2020-04-13 08:25:39 -07:00
|
|
|
var it = ViewStack(View).iterator(views.first, 0xFFFFFFFF);
|
|
|
|
testing.expect((if (it.next()) |node| &node.view else null) == &two_a.view);
|
|
|
|
testing.expect((if (it.next()) |node| &node.view else null) == &five_b.view);
|
|
|
|
testing.expect((if (it.next()) |node| &node.view else null) == &four_b.view);
|
|
|
|
testing.expect((if (it.next()) |node| &node.view else null) == &one_a_pb.view);
|
|
|
|
testing.expect((if (it.next()) |node| &node.view else null) == &three_b_pa.view);
|
2020-04-03 09:53:36 -07:00
|
|
|
testing.expect(it.next() == null);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iteration over 'a' tags
|
|
|
|
{
|
2020-04-13 08:25:39 -07:00
|
|
|
var it = ViewStack(View).iterator(views.first, 1 << 0);
|
|
|
|
testing.expect((if (it.next()) |node| &node.view else null) == &two_a.view);
|
|
|
|
testing.expect((if (it.next()) |node| &node.view else null) == &one_a_pb.view);
|
2020-04-03 09:53:36 -07:00
|
|
|
testing.expect(it.next() == null);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iteration over 'b' tags
|
|
|
|
{
|
2020-04-13 08:25:39 -07:00
|
|
|
var it = ViewStack(View).iterator(views.first, 1 << 1);
|
|
|
|
testing.expect((if (it.next()) |node| &node.view else null) == &five_b.view);
|
|
|
|
testing.expect((if (it.next()) |node| &node.view else null) == &four_b.view);
|
|
|
|
testing.expect((if (it.next()) |node| &node.view else null) == &three_b_pa.view);
|
2020-04-03 09:53:36 -07:00
|
|
|
testing.expect(it.next() == null);
|
|
|
|
}
|
|
|
|
|
2020-04-04 06:37:40 -07:00
|
|
|
// Iteration over tags that aren't present
|
|
|
|
{
|
2020-04-13 08:25:39 -07:00
|
|
|
var it = ViewStack(View).iterator(views.first, 1 << 2);
|
2020-04-04 06:37:40 -07:00
|
|
|
testing.expect(it.next() == null);
|
|
|
|
}
|
|
|
|
|
2020-04-03 09:53:36 -07:00
|
|
|
// Reverse iteration over all tags
|
|
|
|
{
|
2020-04-13 08:25:39 -07:00
|
|
|
var it = ViewStack(View).reverseIterator(views.last, 0xFFFFFFFF);
|
|
|
|
testing.expect((if (it.next()) |node| &node.view else null) == &three_b_pa.view);
|
|
|
|
testing.expect((if (it.next()) |node| &node.view else null) == &one_a_pb.view);
|
|
|
|
testing.expect((if (it.next()) |node| &node.view else null) == &four_b.view);
|
|
|
|
testing.expect((if (it.next()) |node| &node.view else null) == &five_b.view);
|
|
|
|
testing.expect((if (it.next()) |node| &node.view else null) == &two_a.view);
|
2020-04-03 09:53:36 -07:00
|
|
|
testing.expect(it.next() == null);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reverse iteration over 'a' tags
|
|
|
|
{
|
2020-04-13 08:25:39 -07:00
|
|
|
var it = ViewStack(View).reverseIterator(views.last, 1 << 0);
|
|
|
|
testing.expect((if (it.next()) |node| &node.view else null) == &one_a_pb.view);
|
|
|
|
testing.expect((if (it.next()) |node| &node.view else null) == &two_a.view);
|
2020-04-03 09:53:36 -07:00
|
|
|
testing.expect(it.next() == null);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reverse iteration over 'b' tags
|
|
|
|
{
|
2020-04-13 08:25:39 -07:00
|
|
|
var it = ViewStack(View).reverseIterator(views.last, 1 << 1);
|
|
|
|
testing.expect((if (it.next()) |node| &node.view else null) == &three_b_pa.view);
|
|
|
|
testing.expect((if (it.next()) |node| &node.view else null) == &four_b.view);
|
|
|
|
testing.expect((if (it.next()) |node| &node.view else null) == &five_b.view);
|
2020-04-03 09:53:36 -07:00
|
|
|
testing.expect(it.next() == null);
|
|
|
|
}
|
|
|
|
|
2020-04-04 06:37:40 -07:00
|
|
|
// Reverse iteration over tags that aren't present
|
|
|
|
{
|
2020-04-13 08:25:39 -07:00
|
|
|
var it = ViewStack(View).reverseIterator(views.first, 1 << 2);
|
2020-04-04 06:37:40 -07:00
|
|
|
testing.expect(it.next() == null);
|
|
|
|
}
|
|
|
|
|
2020-04-03 09:53:36 -07:00
|
|
|
// Iteration over (pending) 'a' tags
|
|
|
|
{
|
2020-04-13 08:25:39 -07:00
|
|
|
var it = ViewStack(View).pendingIterator(views.first, 1 << 0);
|
|
|
|
testing.expect((if (it.next()) |node| &node.view else null) == &two_a.view);
|
|
|
|
testing.expect((if (it.next()) |node| &node.view else null) == &three_b_pa.view);
|
2020-04-03 09:53:36 -07:00
|
|
|
testing.expect(it.next() == null);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iteration over (pending) 'b' tags
|
|
|
|
{
|
2020-04-13 08:25:39 -07:00
|
|
|
var it = ViewStack(View).pendingIterator(views.first, 1 << 1);
|
|
|
|
testing.expect((if (it.next()) |node| &node.view else null) == &five_b.view);
|
|
|
|
testing.expect((if (it.next()) |node| &node.view else null) == &four_b.view);
|
|
|
|
testing.expect((if (it.next()) |node| &node.view else null) == &one_a_pb.view);
|
2020-04-03 09:53:36 -07:00
|
|
|
testing.expect(it.next() == null);
|
|
|
|
}
|
2020-04-04 06:37:40 -07:00
|
|
|
|
|
|
|
// Iteration over (pending) tags that aren't present
|
|
|
|
{
|
2020-04-13 08:25:39 -07:00
|
|
|
var it = ViewStack(View).pendingIterator(views.first, 1 << 2);
|
2020-04-04 06:37:40 -07:00
|
|
|
testing.expect(it.next() == null);
|
|
|
|
}
|
2020-04-03 09:53:36 -07:00
|
|
|
}
|