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 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 $t = AE::timer $timeout, 0, sub { warn "timeout ($timeout secs)"; $cv->send(0) };
107 my $result = $cv->recv;
112 # thin wrapper around wait_for_event which waits for MAP_NOTIFY
113 # make sure to include 'structure_notify' in the window’s event_mask attribute
116 wait_for_event $x, 2, sub { $_[0]->{response_type} == MAP_NOTIFY };
119 # Wrapper around wait_for_event which waits for UNMAP_NOTIFY. Also calls
120 # sync_with_i3 to make sure i3 also picked up and processed the UnmapNotify
124 wait_for_event $x, 2, sub { $_[0]->{response_type} == UNMAP_NOTIFY };
129 # Opens a new window (see X11::XCB::Window), maps it, waits until it got mapped
130 # and synchronizes with i3.
132 # set dont_map to a true value to avoid mapping
135 # class => WINDOW_CLASS_INPUT_OUTPUT
136 # rect => [ 0, 0, 30, 30 ]
137 # background_color => '#c0c0c0'
138 # event_mask => [ 'structure_notify' ]
139 # name => 'Window <n>'
143 my %args = ($args ? %$args : ());
145 my $dont_map = delete $args{dont_map};
147 $args{class} //= WINDOW_CLASS_INPUT_OUTPUT;
148 $args{rect} //= [ 0, 0, 30, 30 ];
149 $args{background_color} //= '#c0c0c0';
150 $args{event_mask} //= [ 'structure_notify' ];
151 $args{name} //= 'Window ' . counter_window();
153 my $window = $x->root->create_child(%args);
155 return $window if $dont_map;
159 # We sync with i3 here to make sure $x->input_focus is updated.
164 # Thin wrapper around open_window which sets window_type to
165 # _NET_WM_WINDOW_TYPE_UTILITY to make the window floating.
166 sub open_floating_window {
168 my %args = ($args ? %$args : ());
170 $args{window_type} = $x->atom(name => '_NET_WM_WINDOW_TYPE_UTILITY');
172 return open_window($x, \%args);
178 my $reply = $i3->command('open')->recv;
182 sub get_workspace_names {
183 my $i3 = i3(get_socket_path());
184 my $tree = $i3->get_tree->recv;
185 my @outputs = @{$tree->{nodes}};
187 for my $output (@outputs) {
188 # get the first CT_CON of each output
189 my $content = first { $_->{type} == 2 } @{$output->{nodes}};
190 @cons = (@cons, @{$content->{nodes}});
192 [ map { $_->{name} } @cons ]
195 sub get_unused_workspace {
196 my @names = get_workspace_names();
198 do { $tmp = tmpnam() } while ($tmp ~~ @names);
202 sub fresh_workspace {
203 my $unused = get_unused_workspace;
204 cmd("workspace $unused");
210 my $i3 = i3(get_socket_path());
211 my $tree = $i3->get_tree->recv;
213 my @outputs = @{$tree->{nodes}};
215 for my $output (@outputs) {
216 # get the first CT_CON of each output
217 my $content = first { $_->{type} == 2 } @{$output->{nodes}};
218 @workspaces = (@workspaces, @{$content->{nodes}});
221 # as there can only be one workspace with this name, we can safely
222 # return the first entry
223 return first { $_->{name} eq $name } @workspaces;
227 # returns the content (== tree, starting from the node of a workspace)
228 # of a workspace. If called in array context, also includes the focus
229 # stack of the workspace
233 my $con = get_ws($name);
234 return wantarray ? ($con->{nodes}, $con->{focus}) : $con->{nodes};
239 my $con = get_ws($ws);
241 my @focused = @{$con->{focus}};
243 while (@focused > 0) {
245 last unless defined($con->{focus});
246 @focused = @{$con->{focus}};
247 my @cons = grep { $_->{id} == $lf } (@{$con->{nodes}}, @{$con->{'floating_nodes'}});
254 sub get_dock_clients {
257 my $tree = i3(get_socket_path())->get_tree->recv;
258 my @outputs = @{$tree->{nodes}};
259 # Children of all dockareas
261 for my $output (@outputs) {
262 if (!defined($which)) {
263 @docked = (@docked, map { @{$_->{nodes}} }
264 grep { $_->{type} == 5 }
265 @{$output->{nodes}});
266 } elsif ($which eq 'top') {
267 my $first = first { $_->{type} == 5 } @{$output->{nodes}};
268 @docked = (@docked, @{$first->{nodes}});
269 } elsif ($which eq 'bottom') {
270 my @matching = grep { $_->{type} == 5 } @{$output->{nodes}};
271 my $last = $matching[-1];
272 @docked = (@docked, @{$last->{nodes}});
279 i3(get_socket_path())->command(@_)->recv
282 sub workspace_exists {
284 ($name ~~ @{get_workspace_names()})
288 my $i3 = i3(get_socket_path());
289 my $tree = $i3->get_tree->recv;
290 my @outputs = @{$tree->{nodes}};
292 for my $output (@outputs) {
293 # get the first CT_CON of each output
294 my $content = first { $_->{type} == 2 } @{$output->{nodes}};
295 my $first = first { $_->{fullscreen_mode} == 1 } @{$content->{nodes}};
296 return $first->{name}
301 # Sends an I3_SYNC ClientMessage with a random value to the root window.
302 # i3 will reply with the same value, but, due to the order of events it
303 # processes, only after all other events are done.
305 # This can be used to ensure the results of a cmd 'focus left' are pushed to
306 # X11 and that $x->input_focus returns the correct value afterwards.
308 # See also docs/testsuite for a long explanation
313 # Since we need a (mapped) window for receiving a ClientMessage, we create
314 # one on the first call of sync_with_i3. It will be re-used in all
316 if (!defined($_sync_window)) {
317 $_sync_window = $x->root->create_child(
318 class => WINDOW_CLASS_INPUT_OUTPUT,
319 rect => X11::XCB::Rect->new(x => -15, y => -15, width => 10, height => 10 ),
320 override_redirect => 1,
321 background_color => '#ff0000',
322 event_mask => [ 'structure_notify' ],
327 wait_for_event $x, 2, sub { $_[0]->{response_type} == MAP_NOTIFY };
330 my $root = $x->get_root_window();
331 # Generate a random number to identify this particular ClientMessage.
332 my $myrnd = int(rand(255)) + 1;
334 # Generate a ClientMessage, see xcb_client_message_t
335 my $msg = pack "CCSLLLLLLL",
336 CLIENT_MESSAGE, # response_type
339 $root, # destination window
340 $x->atom(name => 'I3_SYNC')->id,
342 $_sync_window->id, # data[0]: our own window id
343 $myrnd, # data[1]: a random value to identify the request
348 # Send it to the root window -- since i3 uses the SubstructureRedirect
349 # event mask, it will get the ClientMessage.
350 $x->send_event(0, $root, EVENT_MASK_SUBSTRUCTURE_REDIRECT, $msg);
352 # now wait until the reply is here
353 return wait_for_event $x, 2, sub {
356 return 0 unless $event->{response_type} == 161;
358 my ($win, $rnd) = unpack "LL", $event->{data};
359 return ($rnd == $myrnd);
364 my $tree = i3(get_socket_path())->get_tree->recv;
365 my @nodes = @{$tree->{nodes}};
366 my $ok = (@nodes > 0);
367 $tester->ok($ok, 'i3 still lives');
371 # Tries to exit i3 gracefully (with the 'exit' cmd) or kills the PID if that fails
372 sub exit_gracefully {
373 my ($pid, $socketpath) = @_;
374 $socketpath ||= get_socket_path();
378 say "Exiting i3 cleanly...";
379 i3($socketpath)->command('exit')->recv;
384 kill(9, $pid) or die "could not kill i3";
387 if ($socketpath =~ m,^/tmp/i3-test-socket-,) {
392 # Gets the socket path from the I3_SOCKET_PATH atom stored on the X11 root window
393 sub get_socket_path {
397 if ($cache && defined($_cached_socket_path)) {
398 return $_cached_socket_path;
401 my $x = X11::XCB::Connection->new;
402 my $atom = $x->atom(name => 'I3_SOCKET_PATH');
403 my $cookie = $x->get_property(0, $x->get_root_window(), $atom->id, GET_PROPERTY_TYPE_ANY, 0, 256);
404 my $reply = $x->get_property_reply($cookie->{sequence});
405 my $socketpath = $reply->{value};
406 $_cached_socket_path = $socketpath;
411 # launches a new i3 process with the given string as configuration file.
412 # useful for tests which test specific config file directives.
414 # be sure to use !NO_I3_INSTANCE! somewhere in the file to signal
415 # complete-run.pl that it should not create an instance of i3
417 sub launch_with_config {
418 my ($config, $dont_add_socket_path) = @_;
420 $dont_add_socket_path //= 0;
422 if (!defined($tmp_socket_path)) {
423 $tmp_socket_path = File::Temp::tempnam('/tmp', 'i3-test-socket-');
426 my ($fh, $tmpfile) = tempfile('i3-test-config-XXXXX', UNLINK => 1);
428 say $fh "ipc-socket $tmp_socket_path" unless $dont_add_socket_path;
431 my $cv = AnyEvent->condvar;
432 my $pid = activate_i3(
433 unix_socket_path => "$tmp_socket_path-activation",
434 display => $ENV{DISPLAY},
435 configfile => $tmpfile,
436 outdir => $ENV{OUTDIR},
437 logpath => $ENV{LOGPATH},
438 valgrind => $ENV{VALGRIND},
442 # blockingly wait until i3 is ready
445 # force update of the cached socket path in lib/i3test