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);
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 my $test_most_args = @_ ? "qw(@_)" : "";
64 use Test::Most $test_most_args;
67 use Time::HiRes qw(sleep);
68 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);
73 $tester->bail_out("$@") if $@;
76 goto \&Exporter::import;
80 # Waits for the next event and calls the given callback for every event to
81 # determine if this is the event we are waiting for.
83 # Can be used to wait until a window is mapped, until a ClientMessage is
86 # wait_for_event $x, 0.25, sub { $_[0]->{response_type} == MAP_NOTIFY };
89 my ($x, $timeout, $cb) = @_;
93 my $prep = EV::prepare sub {
97 my $check = EV::check sub {
98 while (defined(my $event = $x->poll_for_event)) {
106 my $watcher = EV::io $x->get_file_descriptor, EV::READ, sub {
107 # do nothing, we only need this watcher so that EV picks up the events
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;
118 # thin wrapper around wait_for_event which waits for MAP_NOTIFY
119 # make sure to include 'structure_notify' in the window’s event_mask attribute
122 wait_for_event $x, 2, sub { $_[0]->{response_type} == MAP_NOTIFY };
125 # Wrapper around wait_for_event which waits for UNMAP_NOTIFY. Also calls
126 # sync_with_i3 to make sure i3 also picked up and processed the UnmapNotify
130 wait_for_event $x, 2, sub { $_[0]->{response_type} == UNMAP_NOTIFY };
135 # Opens a new window (see X11::XCB::Window), maps it, waits until it got mapped
136 # and synchronizes with i3.
138 # set dont_map to a true value to avoid mapping
141 # class => WINDOW_CLASS_INPUT_OUTPUT
142 # rect => [ 0, 0, 30, 30 ]
143 # background_color => '#c0c0c0'
144 # event_mask => [ 'structure_notify' ]
145 # name => 'Window <n>'
149 my %args = ($args ? %$args : ());
151 my $dont_map = delete $args{dont_map};
153 $args{class} //= WINDOW_CLASS_INPUT_OUTPUT;
154 $args{rect} //= [ 0, 0, 30, 30 ];
155 $args{background_color} //= '#c0c0c0';
156 $args{event_mask} //= [ 'structure_notify' ];
157 $args{name} //= 'Window ' . counter_window();
159 my $window = $x->root->create_child(%args);
161 return $window if $dont_map;
165 # We sync with i3 here to make sure $x->input_focus is updated.
170 # Thin wrapper around open_window which sets window_type to
171 # _NET_WM_WINDOW_TYPE_UTILITY to make the window floating.
172 sub open_floating_window {
174 my %args = ($args ? %$args : ());
176 $args{window_type} = $x->atom(name => '_NET_WM_WINDOW_TYPE_UTILITY');
178 return open_window($x, \%args);
184 my $reply = $i3->command('open')->recv;
188 sub get_workspace_names {
189 my $i3 = i3(get_socket_path());
190 my $tree = $i3->get_tree->recv;
191 my @outputs = @{$tree->{nodes}};
193 for my $output (@outputs) {
194 # get the first CT_CON of each output
195 my $content = first { $_->{type} == 2 } @{$output->{nodes}};
196 @cons = (@cons, @{$content->{nodes}});
198 [ map { $_->{name} } @cons ]
201 sub get_unused_workspace {
202 my @names = get_workspace_names();
204 do { $tmp = tmpnam() } while ($tmp ~~ @names);
208 sub fresh_workspace {
209 my $unused = get_unused_workspace;
210 cmd("workspace $unused");
216 my $i3 = i3(get_socket_path());
217 my $tree = $i3->get_tree->recv;
219 my @outputs = @{$tree->{nodes}};
221 for my $output (@outputs) {
222 # get the first CT_CON of each output
223 my $content = first { $_->{type} == 2 } @{$output->{nodes}};
224 @workspaces = (@workspaces, @{$content->{nodes}});
227 # as there can only be one workspace with this name, we can safely
228 # return the first entry
229 return first { $_->{name} eq $name } @workspaces;
233 # returns the content (== tree, starting from the node of a workspace)
234 # of a workspace. If called in array context, also includes the focus
235 # stack of the workspace
239 my $con = get_ws($name);
240 return wantarray ? ($con->{nodes}, $con->{focus}) : $con->{nodes};
245 my $con = get_ws($ws);
247 my @focused = @{$con->{focus}};
249 while (@focused > 0) {
251 last unless defined($con->{focus});
252 @focused = @{$con->{focus}};
253 my @cons = grep { $_->{id} == $lf } (@{$con->{nodes}}, @{$con->{'floating_nodes'}});
260 sub get_dock_clients {
263 my $tree = i3(get_socket_path())->get_tree->recv;
264 my @outputs = @{$tree->{nodes}};
265 # Children of all dockareas
267 for my $output (@outputs) {
268 if (!defined($which)) {
269 @docked = (@docked, map { @{$_->{nodes}} }
270 grep { $_->{type} == 5 }
271 @{$output->{nodes}});
272 } elsif ($which eq 'top') {
273 my $first = first { $_->{type} == 5 } @{$output->{nodes}};
274 @docked = (@docked, @{$first->{nodes}});
275 } elsif ($which eq 'bottom') {
276 my @matching = grep { $_->{type} == 5 } @{$output->{nodes}};
277 my $last = $matching[-1];
278 @docked = (@docked, @{$last->{nodes}});
285 i3(get_socket_path())->command(@_)->recv
288 sub workspace_exists {
290 ($name ~~ @{get_workspace_names()})
294 my $i3 = i3(get_socket_path());
295 my $tree = $i3->get_tree->recv;
296 my @outputs = @{$tree->{nodes}};
298 for my $output (@outputs) {
299 # get the first CT_CON of each output
300 my $content = first { $_->{type} == 2 } @{$output->{nodes}};
301 my $first = first { $_->{fullscreen_mode} == 1 } @{$content->{nodes}};
302 return $first->{name}
307 # Sends an I3_SYNC ClientMessage with a random value to the root window.
308 # i3 will reply with the same value, but, due to the order of events it
309 # processes, only after all other events are done.
311 # This can be used to ensure the results of a cmd 'focus left' are pushed to
312 # X11 and that $x->input_focus returns the correct value afterwards.
314 # See also docs/testsuite for a long explanation
319 # Since we need a (mapped) window for receiving a ClientMessage, we create
320 # one on the first call of sync_with_i3. It will be re-used in all
322 if (!defined($_sync_window)) {
323 $_sync_window = $x->root->create_child(
324 class => WINDOW_CLASS_INPUT_OUTPUT,
325 rect => X11::XCB::Rect->new(x => -15, y => -15, width => 10, height => 10 ),
326 override_redirect => 1,
327 background_color => '#ff0000',
328 event_mask => [ 'structure_notify' ],
333 wait_for_event $x, 2, sub { $_[0]->{response_type} == MAP_NOTIFY };
336 my $root = $x->get_root_window();
337 # Generate a random number to identify this particular ClientMessage.
338 my $myrnd = int(rand(255)) + 1;
340 # Generate a ClientMessage, see xcb_client_message_t
341 my $msg = pack "CCSLLLLLLL",
342 CLIENT_MESSAGE, # response_type
345 $root, # destination window
346 $x->atom(name => 'I3_SYNC')->id,
348 $_sync_window->id, # data[0]: our own window id
349 $myrnd, # data[1]: a random value to identify the request
354 # Send it to the root window -- since i3 uses the SubstructureRedirect
355 # event mask, it will get the ClientMessage.
356 $x->send_event(0, $root, EVENT_MASK_SUBSTRUCTURE_REDIRECT, $msg);
358 # now wait until the reply is here
359 return wait_for_event $x, 2, sub {
362 return 0 unless $event->{response_type} == 161;
364 my ($win, $rnd) = unpack "LL", $event->{data};
365 return ($rnd == $myrnd);
370 my $tree = i3(get_socket_path())->get_tree->recv;
371 my @nodes = @{$tree->{nodes}};
372 my $ok = (@nodes > 0);
373 $tester->ok($ok, 'i3 still lives');
377 # Tries to exit i3 gracefully (with the 'exit' cmd) or kills the PID if that fails
378 sub exit_gracefully {
379 my ($pid, $socketpath) = @_;
380 $socketpath ||= get_socket_path();
384 say "Exiting i3 cleanly...";
385 i3($socketpath)->command('exit')->recv;
390 kill(9, $pid) or die "could not kill i3";
393 if ($socketpath =~ m,^/tmp/i3-test-socket-,) {
398 # Gets the socket path from the I3_SOCKET_PATH atom stored on the X11 root window
399 sub get_socket_path {
403 if ($cache && defined($_cached_socket_path)) {
404 return $_cached_socket_path;
407 my $x = X11::XCB::Connection->new;
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($self);
465 return $self->SUPER::input_focus(@_);