2 # vim:ts=4:sw=4:expandtab
4 use File::Temp qw(tmpnam);
10 use List::Util qw(first);
11 use List::MoreUtils qw(lastval);
12 use Time::HiRes qw(sleep);
17 our @EXPORT = qw(get_workspace_names get_unused_workspace fresh_workspace get_ws_content get_ws get_focused open_empty_con open_standard_window get_dock_clients cmd does_i3_live exit_gracefully workspace_exists focused_ws get_socket_path);
19 my $tester = Test::Builder->new();
20 my $_cached_socket_path = undef;
25 return $window_count++;
33 use Test::Most" . (@_ > 0 ? " qw(@_)" : "") . ";
36 use Time::HiRes qw(sleep);
37 use Test::Deep qw(eq_deeply cmp_deeply cmp_set cmp_bag cmp_methods useclass noclass set bag subbagof superbagof subsetof supersetof superhashof subhashof bool str arraylength Isa ignore methods regexprefonly regexpmatches num regexponly scalref reftype hashkeysonly blessed array re hash regexpref hash_each shallow array_each code arrayelementsonly arraylengthonly scalarrefonly listmethods any hashkeys isa);
43 goto \&Exporter::import;
46 sub open_standard_window {
51 my $window = $x->root->create_child(
52 class => WINDOW_CLASS_INPUT_OUTPUT,
53 rect => [ 0, 0, 30, 30 ],
54 background_color => $color,
57 $window->name('Window ' . counter_window());
68 my $reply = $i3->command('open')->recv;
72 sub get_workspace_names {
73 my $i3 = i3(get_socket_path());
74 my $tree = $i3->get_tree->recv;
75 my @outputs = @{$tree->{nodes}};
77 for my $output (@outputs) {
78 # get the first CT_CON of each output
79 my $content = first { $_->{type} == 2 } @{$output->{nodes}};
80 @cons = (@cons, @{$content->{nodes}});
82 [ map { $_->{name} } @cons ]
85 sub get_unused_workspace {
86 my @names = get_workspace_names();
88 do { $tmp = tmpnam() } while ($tmp ~~ @names);
93 my $unused = get_unused_workspace;
94 cmd("workspace $unused");
100 my $i3 = i3(get_socket_path());
101 my $tree = $i3->get_tree->recv;
103 my @outputs = @{$tree->{nodes}};
105 for my $output (@outputs) {
106 # get the first CT_CON of each output
107 my $content = first { $_->{type} == 2 } @{$output->{nodes}};
108 @workspaces = (@workspaces, @{$content->{nodes}});
111 # as there can only be one workspace with this name, we can safely
112 # return the first entry
113 return first { $_->{name} eq $name } @workspaces;
117 # returns the content (== tree, starting from the node of a workspace)
118 # of a workspace. If called in array context, also includes the focus
119 # stack of the workspace
123 my $con = get_ws($name);
124 return wantarray ? ($con->{nodes}, $con->{focus}) : $con->{nodes};
129 my $con = get_ws($ws);
131 my @focused = @{$con->{focus}};
133 while (@focused > 0) {
135 last unless defined($con->{focus});
136 @focused = @{$con->{focus}};
137 @cons = grep { $_->{id} == $lf } (@{$con->{nodes}}, @{$con->{'floating_nodes'}});
144 sub get_dock_clients {
147 my $tree = i3(get_socket_path())->get_tree->recv;
148 my @outputs = @{$tree->{nodes}};
149 # Children of all dockareas
151 for my $output (@outputs) {
152 if (!defined($which)) {
153 @docked = (@docked, map { @{$_->{nodes}} }
154 grep { $_->{type} == 5 }
155 @{$output->{nodes}});
156 } elsif ($which eq 'top') {
157 my $first = first { $_->{type} == 5 } @{$output->{nodes}};
158 @docked = (@docked, @{$first->{nodes}});
159 } elsif ($which eq 'bottom') {
160 my $last = lastval { $_->{type} == 5 } @{$output->{nodes}};
161 @docked = (@docked, @{$last->{nodes}});
168 i3(get_socket_path())->command(@_)->recv
171 sub workspace_exists {
173 ($name ~~ @{get_workspace_names()})
177 my $i3 = i3(get_socket_path());
178 my $tree = $i3->get_tree->recv;
179 my @outputs = @{$tree->{nodes}};
181 for my $output (@outputs) {
182 # get the first CT_CON of each output
183 my $content = first { $_->{type} == 2 } @{$output->{nodes}};
184 my $first = first { $_->{fullscreen_mode} == 1 } @{$content->{nodes}};
185 return $first->{name}
190 my $tree = i3(get_socket_path())->get_tree->recv;
191 my @nodes = @{$tree->{nodes}};
192 my $ok = (@nodes > 0);
193 $tester->ok($ok, 'i3 still lives');
197 # Tries to exit i3 gracefully (with the 'exit' cmd) or kills the PID if that fails
198 sub exit_gracefully {
199 my ($pid, $socketpath) = @_;
200 $socketpath ||= get_socket_path();
204 say "Exiting i3 cleanly...";
205 i3($socketpath)->command('exit')->recv;
210 kill(9, $pid) or die "could not kill i3";
214 # Gets the socket path from the I3_SOCKET_PATH atom stored on the X11 root window
215 sub get_socket_path {
219 if ($cache && defined($_cached_socket_path)) {
220 return $_cached_socket_path;
223 my $x = X11::XCB::Connection->new;
224 my $atom = $x->atom(name => 'I3_SOCKET_PATH');
225 my $cookie = $x->get_property(0, $x->get_root_window(), $atom->id, GET_PROPERTY_TYPE_ANY, 0, 256);
226 my $reply = $x->get_property_reply($cookie->{sequence});
227 my $socketpath = $reply->{value};
228 $_cached_socket_path = $socketpath;