2 # vim:ts=4:sw=4:expandtab
3 use strict; use warnings;
5 use File::Temp qw(tmpnam tempfile tempdir);
11 use List::Util qw(first);
12 use Time::HiRes qw(sleep);
14 use Scalar::Util qw(blessed);
45 my $tester = Test::Builder->new();
46 my $_cached_socket_path = undef;
47 my $_sync_window = undef;
48 my $tmp_socket_path = undef;
55 return $window_count++;
63 my $test_more_args = @_ ? "qw(@_)" : "";
67 use Test::More $test_more_args;
70 use Time::HiRes qw(sleep);
72 $tester->bail_out("$@") if $@;
73 feature->import(":5.10");
77 $x ||= i3test::X11->new;
79 goto \&Exporter::import;
83 # Waits for the next event and calls the given callback for every event to
84 # determine if this is the event we are waiting for.
86 # Can be used to wait until a window is mapped, until a ClientMessage is
89 # wait_for_event $x, 0.25, sub { $_[0]->{response_type} == MAP_NOTIFY };
92 my ($timeout, $cb) = @_;
98 # unfortunately, there is no constant for this
101 my $guard = AE::io $x->get_file_descriptor, $ae_read, sub {
102 while (defined(my $event = $x->poll_for_event)) {
110 # Trigger timeout after $timeout seconds (can be fractional)
111 my $t = AE::timer $timeout, 0, sub { warn "timeout ($timeout secs)"; $cv->send(0) };
113 my $result = $cv->recv;
119 # thin wrapper around wait_for_event which waits for MAP_NOTIFY
120 # make sure to include 'structure_notify' in the window’s event_mask attribute
123 my $id = (blessed($win) && $win->isa('X11::XCB::Window')) ? $win->id : $win;
124 wait_for_event 2, sub {
125 $_[0]->{response_type} == MAP_NOTIFY and $_[0]->{window} == $id
129 # Wrapper around wait_for_event which waits for UNMAP_NOTIFY. Also calls
130 # sync_with_i3 to make sure i3 also picked up and processed the UnmapNotify
134 # my $id = (blessed($win) && $win->isa('X11::XCB::Window')) ? $win->id : $win;
135 wait_for_event 2, sub {
136 $_[0]->{response_type} == UNMAP_NOTIFY # and $_[0]->{window} == $id
142 # Opens a new window (see X11::XCB::Window), maps it, waits until it got mapped
143 # and synchronizes with i3.
145 # set dont_map to a true value to avoid mapping
148 # class => WINDOW_CLASS_INPUT_OUTPUT
149 # rect => [ 0, 0, 30, 30 ]
150 # background_color => '#c0c0c0'
151 # event_mask => [ 'structure_notify' ]
152 # name => 'Window <n>'
155 my %args = @_ == 1 ? %{$_[0]} : @_;
157 my $dont_map = delete $args{dont_map};
159 $args{class} //= WINDOW_CLASS_INPUT_OUTPUT;
160 $args{rect} //= [ 0, 0, 30, 30 ];
161 $args{background_color} //= '#c0c0c0';
162 $args{event_mask} //= [ 'structure_notify' ];
163 $args{name} //= 'Window ' . counter_window();
165 my $window = $x->root->create_child(%args);
167 return $window if $dont_map;
170 wait_for_map($window);
174 # Thin wrapper around open_window which sets window_type to
175 # _NET_WM_WINDOW_TYPE_UTILITY to make the window floating.
176 sub open_floating_window {
177 my %args = @_ == 1 ? %{$_[0]} : @_;
179 $args{window_type} = $x->atom(name => '_NET_WM_WINDOW_TYPE_UTILITY');
181 return open_window(\%args);
187 my $reply = $i3->command('open')->recv;
191 sub get_workspace_names {
192 my $i3 = i3(get_socket_path());
193 my $tree = $i3->get_tree->recv;
194 my @outputs = @{$tree->{nodes}};
196 for my $output (@outputs) {
197 # get the first CT_CON of each output
198 my $content = first { $_->{type} == 2 } @{$output->{nodes}};
199 @cons = (@cons, @{$content->{nodes}});
201 [ map { $_->{name} } @cons ]
204 sub get_unused_workspace {
205 my @names = get_workspace_names();
207 do { $tmp = tmpnam() } while ($tmp ~~ @names);
211 sub fresh_workspace {
212 my $unused = get_unused_workspace;
213 cmd("workspace $unused");
219 my $i3 = i3(get_socket_path());
220 my $tree = $i3->get_tree->recv;
222 my @outputs = @{$tree->{nodes}};
224 for my $output (@outputs) {
225 # get the first CT_CON of each output
226 my $content = first { $_->{type} == 2 } @{$output->{nodes}};
227 @workspaces = (@workspaces, @{$content->{nodes}});
230 # as there can only be one workspace with this name, we can safely
231 # return the first entry
232 return first { $_->{name} eq $name } @workspaces;
236 # returns the content (== tree, starting from the node of a workspace)
237 # of a workspace. If called in array context, also includes the focus
238 # stack of the workspace
242 my $con = get_ws($name);
243 return wantarray ? ($con->{nodes}, $con->{focus}) : $con->{nodes};
248 my $con = get_ws($ws);
250 my @focused = @{$con->{focus}};
252 while (@focused > 0) {
254 last unless defined($con->{focus});
255 @focused = @{$con->{focus}};
256 my @cons = grep { $_->{id} == $lf } (@{$con->{nodes}}, @{$con->{'floating_nodes'}});
263 sub get_dock_clients {
266 my $tree = i3(get_socket_path())->get_tree->recv;
267 my @outputs = @{$tree->{nodes}};
268 # Children of all dockareas
270 for my $output (@outputs) {
271 if (!defined($which)) {
272 @docked = (@docked, map { @{$_->{nodes}} }
273 grep { $_->{type} == 5 }
274 @{$output->{nodes}});
275 } elsif ($which eq 'top') {
276 my $first = first { $_->{type} == 5 } @{$output->{nodes}};
277 @docked = (@docked, @{$first->{nodes}});
278 } elsif ($which eq 'bottom') {
279 my @matching = grep { $_->{type} == 5 } @{$output->{nodes}};
280 my $last = $matching[-1];
281 @docked = (@docked, @{$last->{nodes}});
288 i3(get_socket_path())->command(@_)->recv
291 sub workspace_exists {
293 ($name ~~ @{get_workspace_names()})
297 my $i3 = i3(get_socket_path());
298 my $tree = $i3->get_tree->recv;
299 my @outputs = @{$tree->{nodes}};
301 for my $output (@outputs) {
302 # get the first CT_CON of each output
303 my $content = first { $_->{type} == 2 } @{$output->{nodes}};
304 my $first = first { $_->{fullscreen_mode} == 1 } @{$content->{nodes}};
305 return $first->{name}
310 # Sends an I3_SYNC ClientMessage with a random value to the root window.
311 # i3 will reply with the same value, but, due to the order of events it
312 # processes, only after all other events are done.
314 # This can be used to ensure the results of a cmd 'focus left' are pushed to
315 # X11 and that $x->input_focus returns the correct value afterwards.
317 # See also docs/testsuite for a long explanation
320 # Since we need a (mapped) window for receiving a ClientMessage, we create
321 # one on the first call of sync_with_i3. It will be re-used in all
323 if (!defined($_sync_window)) {
324 $_sync_window = $x->root->create_child(
325 class => WINDOW_CLASS_INPUT_OUTPUT,
326 rect => X11::XCB::Rect->new(x => -15, y => -15, width => 10, height => 10 ),
327 override_redirect => 1,
328 background_color => '#ff0000',
329 event_mask => [ 'structure_notify' ],
334 wait_for_event 2, sub { $_[0]->{response_type} == MAP_NOTIFY };
337 my $root = $x->get_root_window();
338 # Generate a random number to identify this particular ClientMessage.
339 my $myrnd = int(rand(255)) + 1;
341 # Generate a ClientMessage, see xcb_client_message_t
342 my $msg = pack "CCSLLLLLLL",
343 CLIENT_MESSAGE, # response_type
346 $root, # destination window
347 $x->atom(name => 'I3_SYNC')->id,
349 $_sync_window->id, # data[0]: our own window id
350 $myrnd, # data[1]: a random value to identify the request
355 # Send it to the root window -- since i3 uses the SubstructureRedirect
356 # event mask, it will get the ClientMessage.
357 $x->send_event(0, $root, EVENT_MASK_SUBSTRUCTURE_REDIRECT, $msg);
359 # now wait until the reply is here
360 return wait_for_event 2, sub {
363 return 0 unless $event->{response_type} == 161;
365 my ($win, $rnd) = unpack "LL", $event->{data};
366 return ($rnd == $myrnd);
371 my $tree = i3(get_socket_path())->get_tree->recv;
372 my @nodes = @{$tree->{nodes}};
373 my $ok = (@nodes > 0);
374 $tester->ok($ok, 'i3 still lives');
378 # Tries to exit i3 gracefully (with the 'exit' cmd) or kills the PID if that fails
379 sub exit_gracefully {
380 my ($pid, $socketpath) = @_;
381 $socketpath ||= get_socket_path();
385 say "Exiting i3 cleanly...";
386 i3($socketpath)->command('exit')->recv;
391 kill(9, $pid) or die "could not kill i3";
394 if ($socketpath =~ m,^/tmp/i3-test-socket-,) {
399 # Gets the socket path from the I3_SOCKET_PATH atom stored on the X11 root window
400 sub get_socket_path {
404 if ($cache && defined($_cached_socket_path)) {
405 return $_cached_socket_path;
408 my $atom = $x->atom(name => 'I3_SOCKET_PATH');
409 my $cookie = $x->get_property(0, $x->get_root_window(), $atom->id, GET_PROPERTY_TYPE_ANY, 0, 256);
410 my $reply = $x->get_property_reply($cookie->{sequence});
411 my $socketpath = $reply->{value};
412 $_cached_socket_path = $socketpath;
417 # launches a new i3 process with the given string as configuration file.
418 # useful for tests which test specific config file directives.
420 # be sure to use !NO_I3_INSTANCE! somewhere in the file to signal
421 # complete-run.pl that it should not create an instance of i3
423 sub launch_with_config {
424 my ($config, $dont_add_socket_path) = @_;
426 $dont_add_socket_path //= 0;
428 if (!defined($tmp_socket_path)) {
429 $tmp_socket_path = File::Temp::tempnam('/tmp', 'i3-test-socket-');
432 my ($fh, $tmpfile) = tempfile('/tmp/i3-test-config-XXXXX', UNLINK => 1);
434 say $fh "ipc-socket $tmp_socket_path" unless $dont_add_socket_path;
437 my $cv = AnyEvent->condvar;
438 my $pid = activate_i3(
439 unix_socket_path => "$tmp_socket_path-activation",
440 display => $ENV{DISPLAY},
441 configfile => $tmpfile,
442 outdir => $ENV{OUTDIR},
443 testname => $ENV{TESTNAME},
444 valgrind => $ENV{VALGRIND},
445 strace => $ENV{STRACE},
449 # blockingly wait until i3 is ready
452 # force update of the cached socket path in lib/i3test
459 use parent 'X11::XCB::Connection';
463 i3test::sync_with_i3();
465 return $self->SUPER::input_focus(@_);