]> git.sur5r.net Git - i3/i3/blobdiff - lib/AnyEvent/I3.pm
tag 0.17
[i3/i3] / lib / AnyEvent / I3.pm
index 6b8f905ba77fd375429d21244c0755cc721f16f1..875f379018dd3dffad7c2d985d6517971f8cce63 100644 (file)
@@ -8,6 +8,7 @@ use AnyEvent::Handle;
 use AnyEvent::Socket;
 use AnyEvent;
 use Encode;
+use Scalar::Util qw(tainted);
 
 =head1 NAME
 
@@ -15,11 +16,11 @@ AnyEvent::I3 - communicate with the i3 window manager
 
 =cut
 
-our $VERSION = '0.09';
+our $VERSION = '0.17';
 
 =head1 VERSION
 
-Version 0.09
+Version 0.17
 
 =head1 SYNOPSIS
 
@@ -29,7 +30,7 @@ then subscribe to events or send messages and receive their replies.
 
     use AnyEvent::I3 qw(:all);
 
-    my $i3 = i3("~/.i3/ipc.sock");
+    my $i3 = i3();
 
     $i3->connect->recv or die "Error connecting";
     say "Connected to i3";
@@ -44,12 +45,38 @@ then subscribe to events or send messages and receive their replies.
     my $workspaces = i3->get_workspaces->recv;
     say "Currently, you use " . @{$workspaces} . " workspaces";
 
+A somewhat more involved example which dumps the i3 layout tree whenever there
+is a workspace event:
+
+    use Data::Dumper;
+    use AnyEvent;
+    use AnyEvent::I3;
+
+    my $i3 = i3();
+
+    $i3->connect->recv or die "Error connecting to i3";
+
+    $i3->subscribe({
+        workspace => sub {
+            $i3->get_tree->cb(sub {
+                my ($tree) = @_;
+                say "tree: " . Dumper($tree);
+            });
+        }
+    })->recv->{success} or die "Error subscribing to events";
+
+    AE::cv->recv
+
 =head1 EXPORT
 
 =head2 $i3 = i3([ $path ]);
 
-Creates a new C<AnyEvent::I3> object and returns it. C<path> is the path of
-the UNIX socket to connect to.
+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.
 
 =head1 SUBROUTINES/METHODS
 
@@ -67,11 +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_LOG_MARKERS => 7;
+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_LOG_MARKERS)
+       TYPE_GET_TREE TYPE_GET_MARKS TYPE_GET_BAR_CONFIG TYPE_GET_VERSION)
 ] );
 
 our @EXPORT_OK = ( @{ $EXPORT_TAGS{all} } );
@@ -83,6 +110,11 @@ my $event_mask = (1 << 31);
 my %events = (
     workspace => ($event_mask | 0),
     output => ($event_mask | 1),
+    mode => ($event_mask | 2),
+    window => ($event_mask | 3),
+    barconfig_update => ($event_mask | 4),
+    binding => ($event_mask | 5),
+    shutdown => ($event_mask | 6),
     _error => 0xFFFFFFFF,
 );
 
@@ -90,15 +122,60 @@ sub i3 {
     AnyEvent::I3->new(@_)
 }
 
+# Calls i3, even when running in taint mode.
+sub _call_i3 {
+    my ($args) = @_;
+
+    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
+    # custom location (think ~/.bin/).
+    (local $ENV{PATH}) = ($ENV{PATH} =~ /(.*)/);
+
+    # In taint mode, we also need to remove all relative directories from
+    # PATH (like . or ../bin). We only do this in taint mode and warn the
+    # user, since this might break a real-world use case for some people.
+    if ($path_tainted) {
+        my @dirs = split /:/, $ENV{PATH};
+        my @filtered = grep !/^\./, @dirs;
+        if (scalar @dirs != scalar @filtered) {
+            $ENV{PATH} = join ':', @filtered;
+            warn qq|Removed relative directories from PATH because you | .
+                 qq|are running Perl with taint mode enabled. Remove -T | .
+                 qq|to be able to use relative directories in PATH. | .
+                 qq|New PATH is "$ENV{PATH}"|;
+        }
+    }
+    # Otherwise the qx() operator wont work:
+    delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};
+    chomp(my $result = qx(i3 $args));
+    # Circumventing taint mode again: the socket can be anywhere on the
+    # system and that’s okay.
+    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 the path of
-the UNIX socket to connect to.
+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';
 
     # Check if we need to resolve ~
@@ -147,6 +224,7 @@ sub connect {
                 for my $type (keys %{$cb}) {
                     next if ($type & $event_mask) == $event_mask;
                     $cb->{$type}->();
+                    delete $cb->{$type};
                 }
 
                 # Trigger _error callback, if set
@@ -203,7 +281,7 @@ key being the name of the event and the value being a callback.
         workspace => sub { say "Workspaces changed" }
     );
 
-    if ($i3->subscribe(\%callbacks)->recv->{success})
+    if ($i3->subscribe(\%callbacks)->recv->{success}) {
         say "Successfully subscribed";
     }
 
@@ -292,7 +370,7 @@ sub _ensure_connection {
 
     return if defined($self->{ipchdl});
 
-    $self->connect->recv or die "Unable to connect to i3"
+    $self->connect->recv or die "Unable to connect to i3 (socket path " . $self->{path} . ")";
 }
 
 =head2 get_workspaces
@@ -375,22 +453,53 @@ sub get_bar_config {
     $self->message(TYPE_GET_BAR_CONFIG, $id)
 }
 
-=head2 get_log_markers
+=head2 get_version
 
-Gets the bar configuration for the specific bar id from i3 (>= v4.1).
+Gets the i3 version via IPC, with a fall-back that parses the output of i3
+--version (for i3 < v4.3).
 
-    my $markers = i3->get_log_markers()->recv;
-    say Dumper($markers);
+    my $version = i3->get_version()->recv;
+    say "major: " . $version->{major} . ", minor = " . $version->{minor};
 
 =cut
-sub get_log_markers {
+sub get_version {
     my ($self) = @_;
 
     $self->_ensure_connection;
 
-    $self->message(TYPE_GET_LOG_MARKERS)
-}
+    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 doesn’t 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)
 
@@ -410,7 +519,7 @@ sub command {
 
 =head1 AUTHOR
 
-Michael Stapelberg, C<< <michael at stapelberg.de> >>
+Michael Stapelberg, C<< <michael at i3wm.org> >>
 
 =head1 BUGS
 
@@ -436,7 +545,7 @@ L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=AnyEvent-I3>
 
 =item * The i3 window manager website
 
-L<http://i3.zekjur.net/>
+L<http://i3wm.org>
 
 =back
 
@@ -446,7 +555,7 @@ L<http://i3.zekjur.net/>
 
 =head1 LICENSE AND COPYRIGHT
 
-Copyright 2010 Michael Stapelberg.
+Copyright 2010-2012 Michael Stapelberg.
 
 This program is free software; you can redistribute it and/or modify it
 under the terms of either: the GNU General Public License as published