Implement the GET_VERSION request (with a fallback to i3 --version)

next
Michael Stapelberg 2012-08-05 17:31:22 +02:00
parent d8ad62c36f
commit 64fddbe41b
1 changed files with 88 additions and 33 deletions

View File

@ -94,10 +94,11 @@ use constant TYPE_GET_OUTPUTS => 3;
use constant TYPE_GET_TREE => 4;
use constant TYPE_GET_MARKS => 5;
use constant TYPE_GET_BAR_CONFIG => 6;
use constant TYPE_GET_VERSION => 7;
our %EXPORT_TAGS = ( 'all' => [
qw(i3 TYPE_COMMAND TYPE_GET_WORKSPACES TYPE_SUBSCRIBE TYPE_GET_OUTPUTS
TYPE_GET_TREE TYPE_GET_MARKS TYPE_GET_BAR_CONFIG)
TYPE_GET_TREE TYPE_GET_MARKS TYPE_GET_BAR_CONFIG TYPE_GET_VERSION)
] );
our @EXPORT_OK = ( @{ $EXPORT_TAGS{all} } );
@ -116,20 +117,10 @@ sub i3 {
AnyEvent::I3->new(@_)
}
=head2 $i3 = AnyEvent::I3->new([ $path ])
# Calls i3, even when running in taint mode.
sub _call_i3 {
my ($args) = @_;
Creates a new C<AnyEvent::I3> object and returns it.
C<path> is an optional path of the UNIX socket to connect to. It is strongly
advised to NOT specify this unless you're absolutely sure you need it.
C<AnyEvent::I3> will automatically figure it out by querying the running i3
instance on the current DISPLAY which is almost always what you want.
=cut
sub new {
my ($class, $path) = @_;
if (!$path) {
my $path_tainted = tainted($ENV{PATH});
# This effectively circumvents taint mode checking for $ENV{PATH}. We
# do this because users might specify PATH explicitly to call i3 in a
@ -152,16 +143,32 @@ sub new {
}
# Otherwise the qx() operator wont work:
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};
chomp($path = qx(i3 --get-socketpath));
chomp(my $result = qx(i3 $args));
# Circumventing taint mode again: the socket can be anywhere on the
# system and thats okay.
if ($path =~ /^([^\0]+)$/) {
$path = $1;
} else {
warn "Asking i3 for the socket path failed. Is DISPLAY set and is i3 in your PATH?";
if ($result =~ /^([^\0]+)$/) {
return $1;
}
warn "Calling i3 $args failed. Is DISPLAY set and is i3 in your PATH?";
return undef;
}
=head2 $i3 = AnyEvent::I3->new([ $path ])
Creates a new C<AnyEvent::I3> object and returns it.
C<path> is an optional path of the UNIX socket to connect to. It is strongly
advised to NOT specify this unless you're absolutely sure you need it.
C<AnyEvent::I3> will automatically figure it out by querying the running i3
instance on the current DISPLAY which is almost always what you want.
=cut
sub new {
my ($class, $path) = @_;
$path = _call_i3('--get-socketpath') unless $path;
# This is the old default path (v3.*). This fallback line can be removed in
# a year from now. -- Michael, 2012-07-09
$path ||= '~/.i3/ipc.sock';
@ -441,6 +448,54 @@ sub get_bar_config {
$self->message(TYPE_GET_BAR_CONFIG, $id)
}
=head2 get_version
Gets the i3 version via IPC, with a fall-back that parses the output of i3
--version (for i3 < v4.3).
my $version = i3->get_version()->recv;
say "major: " . $version->{major} . ", minor = " . $version->{minor};
=cut
sub get_version {
my ($self) = @_;
$self->_ensure_connection;
my $cv = AnyEvent->condvar;
my $version_cv = $self->message(TYPE_GET_VERSION);
my $timeout;
$timeout = AnyEvent->timer(
after => 1,
cb => sub {
warn "Falling back to i3 --version since the running i3 doesnt support GET_VERSION yet.";
my $version = _call_i3('--version');
$version =~ s/^i3 version //;
my $patch = 0;
my ($major, $minor) = ($version =~ /^([0-9]+)\.([0-9]+)/);
if ($version =~ /^[0-9]+\.[0-9]+\.([0-9]+)/) {
$patch = $1;
}
# Strip everything from the © sign on.
$version =~ s/ ©.*$//g;
$cv->send({
major => int($major),
minor => int($minor),
patch => int($patch),
human_readable => $version,
});
undef $timeout;
},
);
$version_cv->cb(sub {
undef $timeout;
$cv->send($version_cv->recv);
});
return $cv;
}
=head2 command($content)
Makes i3 execute the given command