2 # vim:ts=4:sw=4:expandtab
3 use strict; use warnings;
5 use File::Temp qw(tmpnam tempfile tempdir);
12 use List::Util qw(first);
13 use Time::HiRes qw(sleep);
15 use Scalar::Util qw(blessed);
46 my $tester = Test::Builder->new();
47 my $_cached_socket_path = undef;
48 my $_sync_window = undef;
49 my $tmp_socket_path = undef;
56 return $window_count++;
64 my $test_most_args = @_ ? "qw(@_)" : "";
68 use Test::Most $test_most_args;
71 use Time::HiRes qw(sleep);
72 use Test::Deep qw(eq_deeply cmp_deeply);
74 $tester->bail_out("$@") if $@;
75 feature->import(":5.10");
79 $x ||= i3test::X11->new;
81 goto \&Exporter::import;
85 # Waits for the next event and calls the given callback for every event to
86 # determine if this is the event we are waiting for.
88 # Can be used to wait until a window is mapped, until a ClientMessage is
91 # wait_for_event $x, 0.25, sub { $_[0]->{response_type} == MAP_NOTIFY };
94 my ($timeout, $cb) = @_;
98 my $prep = EV::prepare sub {
102 my $check = EV::check sub {
103 while (defined(my $event = $x->poll_for_event)) {
111 my $watcher = EV::io $x->get_file_descriptor, EV::READ, sub {
112 # do nothing, we only need this watcher so that EV picks up the events
115 # Trigger timeout after $timeout seconds (can be fractional)
116 my $t = AE::timer $timeout, 0, sub { warn "timeout ($timeout secs)"; $cv->send(0) };
118 my $result = $cv->recv;
123 # thin wrapper around wait_for_event which waits for MAP_NOTIFY
124 # make sure to include 'structure_notify' in the window’s event_mask attribute
127 my $id = (blessed($win) && $win->isa('X11::XCB::Window')) ? $win->id : $win;
128 wait_for_event 2, sub {
129 $_[0]->{response_type} == MAP_NOTIFY and $_[0]->{window} == $id
133 # Wrapper around wait_for_event which waits for UNMAP_NOTIFY. Also calls
134 # sync_with_i3 to make sure i3 also picked up and processed the UnmapNotify
138 # my $id = (blessed($win) && $win->isa('X11::XCB::Window')) ? $win->id : $win;
139 wait_for_event 2, sub {
140 $_[0]->{response_type} == UNMAP_NOTIFY # and $_[0]->{window} == $id
146 # Opens a new window (see X11::XCB::Window), maps it, waits until it got mapped
147 # and synchronizes with i3.
149 # set dont_map to a true value to avoid mapping
152 # class => WINDOW_CLASS_INPUT_OUTPUT
153 # rect => [ 0, 0, 30, 30 ]
154 # background_color => '#c0c0c0'
155 # event_mask => [ 'structure_notify' ]
156 # name => 'Window <n>'
160 my %args = ($args ? %$args : ());
162 my $dont_map = delete $args{dont_map};
164 $args{class} //= WINDOW_CLASS_INPUT_OUTPUT;
165 $args{rect} //= [ 0, 0, 30, 30 ];
166 $args{background_color} //= '#c0c0c0';
167 $args{event_mask} //= [ 'structure_notify' ];
168 $args{name} //= 'Window ' . counter_window();
170 my $window = $x->root->create_child(%args);
172 return $window if $dont_map;
175 wait_for_map($window);
179 # Thin wrapper around open_window which sets window_type to
180 # _NET_WM_WINDOW_TYPE_UTILITY to make the window floating.
181 sub open_floating_window {
183 my %args = ($args ? %$args : ());
185 $args{window_type} = $x->atom(name => '_NET_WM_WINDOW_TYPE_UTILITY');
187 return open_window(\%args);
193 my $reply = $i3->command('open')->recv;
197 sub get_workspace_names {
198 my $i3 = i3(get_socket_path());
199 my $tree = $i3->get_tree->recv;
200 my @outputs = @{$tree->{nodes}};
202 for my $output (@outputs) {
203 # get the first CT_CON of each output
204 my $content = first { $_->{type} == 2 } @{$output->{nodes}};
205 @cons = (@cons, @{$content->{nodes}});
207 [ map { $_->{name} } @cons ]
210 sub get_unused_workspace {
211 my @names = get_workspace_names();
213 do { $tmp = tmpnam() } while ($tmp ~~ @names);
217 sub fresh_workspace {
218 my $unused = get_unused_workspace;
219 cmd("workspace $unused");
225 my $i3 = i3(get_socket_path());
226 my $tree = $i3->get_tree->recv;
228 my @outputs = @{$tree->{nodes}};
230 for my $output (@outputs) {
231 # get the first CT_CON of each output
232 my $content = first { $_->{type} == 2 } @{$output->{nodes}};
233 @workspaces = (@workspaces, @{$content->{nodes}});
236 # as there can only be one workspace with this name, we can safely
237 # return the first entry
238 return first { $_->{name} eq $name } @workspaces;
242 # returns the content (== tree, starting from the node of a workspace)
243 # of a workspace. If called in array context, also includes the focus
244 # stack of the workspace
248 my $con = get_ws($name);
249 return wantarray ? ($con->{nodes}, $con->{focus}) : $con->{nodes};
254 my $con = get_ws($ws);
256 my @focused = @{$con->{focus}};
258 while (@focused > 0) {
260 last unless defined($con->{focus});
261 @focused = @{$con->{focus}};
262 my @cons = grep { $_->{id} == $lf } (@{$con->{nodes}}, @{$con->{'floating_nodes'}});
269 sub get_dock_clients {
272 my $tree = i3(get_socket_path())->get_tree->recv;
273 my @outputs = @{$tree->{nodes}};
274 # Children of all dockareas
276 for my $output (@outputs) {
277 if (!defined($which)) {
278 @docked = (@docked, map { @{$_->{nodes}} }
279 grep { $_->{type} == 5 }
280 @{$output->{nodes}});
281 } elsif ($which eq 'top') {
282 my $first = first { $_->{type} == 5 } @{$output->{nodes}};
283 @docked = (@docked, @{$first->{nodes}});
284 } elsif ($which eq 'bottom') {
285 my @matching = grep { $_->{type} == 5 } @{$output->{nodes}};
286 my $last = $matching[-1];
287 @docked = (@docked, @{$last->{nodes}});
294 i3(get_socket_path())->command(@_)->recv
297 sub workspace_exists {
299 ($name ~~ @{get_workspace_names()})
303 my $i3 = i3(get_socket_path());
304 my $tree = $i3->get_tree->recv;
305 my @outputs = @{$tree->{nodes}};
307 for my $output (@outputs) {
308 # get the first CT_CON of each output
309 my $content = first { $_->{type} == 2 } @{$output->{nodes}};
310 my $first = first { $_->{fullscreen_mode} == 1 } @{$content->{nodes}};
311 return $first->{name}
316 # Sends an I3_SYNC ClientMessage with a random value to the root window.
317 # i3 will reply with the same value, but, due to the order of events it
318 # processes, only after all other events are done.
320 # This can be used to ensure the results of a cmd 'focus left' are pushed to
321 # X11 and that $x->input_focus returns the correct value afterwards.
323 # See also docs/testsuite for a long explanation
326 # Since we need a (mapped) window for receiving a ClientMessage, we create
327 # one on the first call of sync_with_i3. It will be re-used in all
329 if (!defined($_sync_window)) {
330 $_sync_window = $x->root->create_child(
331 class => WINDOW_CLASS_INPUT_OUTPUT,
332 rect => X11::XCB::Rect->new(x => -15, y => -15, width => 10, height => 10 ),
333 override_redirect => 1,
334 background_color => '#ff0000',
335 event_mask => [ 'structure_notify' ],
340 wait_for_event 2, sub { $_[0]->{response_type} == MAP_NOTIFY };
343 my $root = $x->get_root_window();
344 # Generate a random number to identify this particular ClientMessage.
345 my $myrnd = int(rand(255)) + 1;
347 # Generate a ClientMessage, see xcb_client_message_t
348 my $msg = pack "CCSLLLLLLL",
349 CLIENT_MESSAGE, # response_type
352 $root, # destination window
353 $x->atom(name => 'I3_SYNC')->id,
355 $_sync_window->id, # data[0]: our own window id
356 $myrnd, # data[1]: a random value to identify the request
361 # Send it to the root window -- since i3 uses the SubstructureRedirect
362 # event mask, it will get the ClientMessage.
363 $x->send_event(0, $root, EVENT_MASK_SUBSTRUCTURE_REDIRECT, $msg);
365 # now wait until the reply is here
366 return wait_for_event 2, sub {
369 return 0 unless $event->{response_type} == 161;
371 my ($win, $rnd) = unpack "LL", $event->{data};
372 return ($rnd == $myrnd);
377 my $tree = i3(get_socket_path())->get_tree->recv;
378 my @nodes = @{$tree->{nodes}};
379 my $ok = (@nodes > 0);
380 $tester->ok($ok, 'i3 still lives');
384 # Tries to exit i3 gracefully (with the 'exit' cmd) or kills the PID if that fails
385 sub exit_gracefully {
386 my ($pid, $socketpath) = @_;
387 $socketpath ||= get_socket_path();
391 say "Exiting i3 cleanly...";
392 i3($socketpath)->command('exit')->recv;
397 kill(9, $pid) or die "could not kill i3";
400 if ($socketpath =~ m,^/tmp/i3-test-socket-,) {
405 # Gets the socket path from the I3_SOCKET_PATH atom stored on the X11 root window
406 sub get_socket_path {
410 if ($cache && defined($_cached_socket_path)) {
411 return $_cached_socket_path;
414 my $atom = $x->atom(name => 'I3_SOCKET_PATH');
415 my $cookie = $x->get_property(0, $x->get_root_window(), $atom->id, GET_PROPERTY_TYPE_ANY, 0, 256);
416 my $reply = $x->get_property_reply($cookie->{sequence});
417 my $socketpath = $reply->{value};
418 $_cached_socket_path = $socketpath;
423 # launches a new i3 process with the given string as configuration file.
424 # useful for tests which test specific config file directives.
426 # be sure to use !NO_I3_INSTANCE! somewhere in the file to signal
427 # complete-run.pl that it should not create an instance of i3
429 sub launch_with_config {
430 my ($config, $dont_add_socket_path) = @_;
432 $dont_add_socket_path //= 0;
434 if (!defined($tmp_socket_path)) {
435 $tmp_socket_path = File::Temp::tempnam('/tmp', 'i3-test-socket-');
438 my ($fh, $tmpfile) = tempfile('/tmp/i3-test-config-XXXXX', UNLINK => 1);
440 say $fh "ipc-socket $tmp_socket_path" unless $dont_add_socket_path;
443 my $cv = AnyEvent->condvar;
444 my $pid = activate_i3(
445 unix_socket_path => "$tmp_socket_path-activation",
446 display => $ENV{DISPLAY},
447 configfile => $tmpfile,
448 outdir => $ENV{OUTDIR},
449 testname => $ENV{TESTNAME},
450 valgrind => $ENV{VALGRIND},
451 strace => $ENV{STRACE},
455 # blockingly wait until i3 is ready
458 # force update of the cached socket path in lib/i3test
465 use parent 'X11::XCB::Connection';
469 i3test::sync_with_i3();
471 return $self->SUPER::input_focus(@_);