2 # vim:ts=4:sw=4:expandtab
4 use File::Temp qw(tmpnam tempfile tempdir);
11 use List::Util qw(first);
12 use List::MoreUtils qw(lastval);
13 use Time::HiRes qw(sleep);
44 my $tester = Test::Builder->new();
45 my $_cached_socket_path = undef;
46 my $_sync_window = undef;
47 my $tmp_socket_path = undef;
52 return $window_count++;
60 use Test::Most" . (@_ > 0 ? " qw(@_)" : "") . ";
63 use Time::HiRes qw(sleep);
64 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);
70 goto \&Exporter::import;
74 # Waits for the next event and calls the given callback for every event to
75 # determine if this is the event we are waiting for.
77 # Can be used to wait until a window is mapped, until a ClientMessage is
80 # wait_for_event $x, 0.25, sub { $_[0]->{response_type} == MAP_NOTIFY };
83 my ($x, $timeout, $cb) = @_;
87 my $prep = EV::prepare sub {
91 my $check = EV::check sub {
92 while (defined(my $event = $x->poll_for_event)) {
100 my $watcher = EV::io $x->get_file_descriptor, EV::READ, sub {
101 # do nothing, we only need this watcher so that EV picks up the events
104 # Trigger timeout after $timeout seconds (can be fractional)
105 my $timeout = AE::timer $timeout, 0, sub { warn "timeout"; $cv->send(0) };
107 my $result = $cv->recv;
111 # thin wrapper around wait_for_event which waits for MAP_NOTIFY
112 # make sure to include 'structure_notify' in the window’s event_mask attribute
115 wait_for_event $x, 1, sub { $_[0]->{response_type} == MAP_NOTIFY };
118 # Wrapper around wait_for_event which waits for UNMAP_NOTIFY. Also calls
119 # sync_with_i3 to make sure i3 also picked up and processed the UnmapNotify
123 wait_for_event $x, 1, sub { $_[0]->{response_type} == UNMAP_NOTIFY };
127 sub open_standard_window {
128 my ($x, $color, $floating) = @_;
130 $color ||= '#c0c0c0';
132 # We cannot use a hashref here because create_child expands the arguments into an array
134 class => WINDOW_CLASS_INPUT_OUTPUT,
135 rect => X11::XCB::Rect->new(x => 0, y => 0, width => 30, height => 30 ),
136 background_color => $color,
137 event_mask => [ 'structure_notify' ],
140 if (defined($floating) && $floating) {
141 @args = (@args, window_type => $x->atom(name => '_NET_WM_WINDOW_TYPE_UTILITY'));
144 my $window = $x->root->create_child(@args);
146 $window->name('Window ' . counter_window());
149 wait_for_event $x, 0.5, sub { $_[0]->{response_type} == MAP_NOTIFY };
157 my $reply = $i3->command('open')->recv;
161 sub get_workspace_names {
162 my $i3 = i3(get_socket_path());
163 my $tree = $i3->get_tree->recv;
164 my @outputs = @{$tree->{nodes}};
166 for my $output (@outputs) {
167 # get the first CT_CON of each output
168 my $content = first { $_->{type} == 2 } @{$output->{nodes}};
169 @cons = (@cons, @{$content->{nodes}});
171 [ map { $_->{name} } @cons ]
174 sub get_unused_workspace {
175 my @names = get_workspace_names();
177 do { $tmp = tmpnam() } while ($tmp ~~ @names);
181 sub fresh_workspace {
182 my $unused = get_unused_workspace;
183 cmd("workspace $unused");
189 my $i3 = i3(get_socket_path());
190 my $tree = $i3->get_tree->recv;
192 my @outputs = @{$tree->{nodes}};
194 for my $output (@outputs) {
195 # get the first CT_CON of each output
196 my $content = first { $_->{type} == 2 } @{$output->{nodes}};
197 @workspaces = (@workspaces, @{$content->{nodes}});
200 # as there can only be one workspace with this name, we can safely
201 # return the first entry
202 return first { $_->{name} eq $name } @workspaces;
206 # returns the content (== tree, starting from the node of a workspace)
207 # of a workspace. If called in array context, also includes the focus
208 # stack of the workspace
212 my $con = get_ws($name);
213 return wantarray ? ($con->{nodes}, $con->{focus}) : $con->{nodes};
218 my $con = get_ws($ws);
220 my @focused = @{$con->{focus}};
222 while (@focused > 0) {
224 last unless defined($con->{focus});
225 @focused = @{$con->{focus}};
226 @cons = grep { $_->{id} == $lf } (@{$con->{nodes}}, @{$con->{'floating_nodes'}});
233 sub get_dock_clients {
236 my $tree = i3(get_socket_path())->get_tree->recv;
237 my @outputs = @{$tree->{nodes}};
238 # Children of all dockareas
240 for my $output (@outputs) {
241 if (!defined($which)) {
242 @docked = (@docked, map { @{$_->{nodes}} }
243 grep { $_->{type} == 5 }
244 @{$output->{nodes}});
245 } elsif ($which eq 'top') {
246 my $first = first { $_->{type} == 5 } @{$output->{nodes}};
247 @docked = (@docked, @{$first->{nodes}});
248 } elsif ($which eq 'bottom') {
249 my $last = lastval { $_->{type} == 5 } @{$output->{nodes}};
250 @docked = (@docked, @{$last->{nodes}});
257 i3(get_socket_path())->command(@_)->recv
260 sub workspace_exists {
262 ($name ~~ @{get_workspace_names()})
266 my $i3 = i3(get_socket_path());
267 my $tree = $i3->get_tree->recv;
268 my @outputs = @{$tree->{nodes}};
270 for my $output (@outputs) {
271 # get the first CT_CON of each output
272 my $content = first { $_->{type} == 2 } @{$output->{nodes}};
273 my $first = first { $_->{fullscreen_mode} == 1 } @{$content->{nodes}};
274 return $first->{name}
279 # Sends an I3_SYNC ClientMessage with a random value to the root window.
280 # i3 will reply with the same value, but, due to the order of events it
281 # processes, only after all other events are done.
283 # This can be used to ensure the results of a cmd 'focus left' are pushed to
284 # X11 and that $x->input_focus returns the correct value afterwards.
286 # See also docs/testsuite for a long explanation
291 # Since we need a (mapped) window for receiving a ClientMessage, we create
292 # one on the first call of sync_with_i3. It will be re-used in all
294 if (!defined($_sync_window)) {
295 $_sync_window = $x->root->create_child(
296 class => WINDOW_CLASS_INPUT_OUTPUT,
297 rect => X11::XCB::Rect->new(x => -15, y => -15, width => 10, height => 10 ),
298 override_redirect => 1,
299 background_color => '#ff0000',
300 event_mask => [ 'structure_notify' ],
305 wait_for_event $x, 0.5, sub { $_[0]->{response_type} == MAP_NOTIFY };
308 my $root = $x->get_root_window();
309 # Generate a random number to identify this particular ClientMessage.
310 my $myrnd = int(rand(255)) + 1;
312 # Generate a ClientMessage, see xcb_client_message_t
313 my $msg = pack "CCSLLLLLLL",
314 CLIENT_MESSAGE, # response_type
317 $root, # destination window
318 $x->atom(name => 'I3_SYNC')->id,
320 $_sync_window->id, # data[0]: our own window id
321 $myrnd, # data[1]: a random value to identify the request
326 # Send it to the root window -- since i3 uses the SubstructureRedirect
327 # event mask, it will get the ClientMessage.
328 $x->send_event(0, $root, EVENT_MASK_SUBSTRUCTURE_REDIRECT, $msg);
330 # now wait until the reply is here
331 return wait_for_event $x, 1, sub {
334 return 0 unless $event->{response_type} == 161;
336 my ($win, $rnd) = unpack "LL", $event->{data};
337 return ($rnd == $myrnd);
342 my $tree = i3(get_socket_path())->get_tree->recv;
343 my @nodes = @{$tree->{nodes}};
344 my $ok = (@nodes > 0);
345 $tester->ok($ok, 'i3 still lives');
349 # Tries to exit i3 gracefully (with the 'exit' cmd) or kills the PID if that fails
350 sub exit_gracefully {
351 my ($pid, $socketpath) = @_;
352 $socketpath ||= get_socket_path();
356 say "Exiting i3 cleanly...";
357 i3($socketpath)->command('exit')->recv;
362 kill(9, $pid) or die "could not kill i3";
366 # Gets the socket path from the I3_SOCKET_PATH atom stored on the X11 root window
367 sub get_socket_path {
371 if ($cache && defined($_cached_socket_path)) {
372 return $_cached_socket_path;
375 my $x = X11::XCB::Connection->new;
376 my $atom = $x->atom(name => 'I3_SOCKET_PATH');
377 my $cookie = $x->get_property(0, $x->get_root_window(), $atom->id, GET_PROPERTY_TYPE_ANY, 0, 256);
378 my $reply = $x->get_property_reply($cookie->{sequence});
379 my $socketpath = $reply->{value};
380 $_cached_socket_path = $socketpath;
385 # launches a new i3 process with the given string as configuration file.
386 # useful for tests which test specific config file directives.
388 # be sure to use !NO_I3_INSTANCE! somewhere in the file to signal
389 # complete-run.pl that it should not create an instance of i3
391 sub launch_with_config {
394 if (!defined($tmp_socket_path)) {
395 $tmp_socket_path = File::Temp::tempnam('/tmp', 'i3-test-socket-');
398 my ($fh, $tmpfile) = tempfile('i3-test-config-XXXXX', UNLINK => 1);
400 say $fh "ipc-socket $tmp_socket_path";
403 # Use $ENV{LOGPATH}, gets set in complete-run.pl. We append instead of
404 # overwrite because there might be multiple instances of i3 running during
406 my $i3cmd = "exec " . abs_path("../i3") . " -V -d all --disable-signalhandler -c $tmpfile >>$ENV{LOGPATH} 2>&1";
407 my $process = Proc::Background->new($i3cmd);
410 # force update of the cached socket path in lib/i3test