From afa8be9547248cb559dc473ae7e93833c28f1d5b Mon Sep 17 00:00:00 2001 From: Michael Stapelberg Date: Tue, 1 Jun 2010 20:52:22 +0200 Subject: [PATCH] lib/i3test: Implement get_focused --- testcases/t/lib/i3test.pm | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/testcases/t/lib/i3test.pm b/testcases/t/lib/i3test.pm index 4b76b7f4..da3c1b28 100644 --- a/testcases/t/lib/i3test.pm +++ b/testcases/t/lib/i3test.pm @@ -19,7 +19,7 @@ use AnyEvent::I3; #use Exporter qw(import); use base 'Exporter'; -our @EXPORT = qw(get_workspace_names get_unused_workspace get_ws_content get_ws); +our @EXPORT = qw(get_workspace_names get_unused_workspace get_ws_content get_ws get_focused); BEGIN { my $window_count = 0; @@ -76,6 +76,28 @@ sub get_ws_content { return wantarray ? ($cons[0]->{nodes}, $cons[0]->{focus}) : $cons[0]->{nodes}; } +sub get_focused { + my ($ws) = @_; + my $i3 = i3("/tmp/nestedcons"); + my $tree = $i3->get_workspaces->recv; + + my @ws = map { @{$_->{nodes}} } @{$tree->{nodes}}; + my @cons = grep { $_->{name} eq $ws } @ws; + my $con = $cons[0]; + + my @focused = @{$con->{focus}}; + my $lf; + while (@focused > 0) { + $lf = $focused[0]; + last unless defined($con->{focus}); + @focused = @{$con->{focus}}; + @cons = grep { $_->{id} == $lf } (@{$con->{nodes}}, @{$con->{'floating-nodes'}}); + $con = $cons[0]; + } + + return $lf; +} + sub get_ws { my ($name) = @_; my $i3 = i3("/tmp/nestedcons"); @@ -85,5 +107,4 @@ sub get_ws { return $cons[0]; } - 1 -- 2.39.5