=cut
-our $VERSION = '0.01';
+our $VERSION = '0.05';
=head1 VERSION
-Version 0.01
+Version 0.05
=head1 SYNOPSIS
IPC interface it provides (if enabled in the configuration file). You can
then subscribe to events or send messages and receive their replies.
-Note that as soon as you subscribe to some kind of event, you should B<NOT>
-send any more messages as race conditions might occur. Instead, open another
-connection for that.
-
use AnyEvent::I3 qw(:all);
- my $i3 = i3("/tmp/i3-ipc.sock");
+ my $i3 = i3("~/.i3/ipc.sock");
- $i3->connect->recv;
+ $i3->connect->recv or die "Error connecting";
say "Connected to i3";
my $workspaces = $i3->message(TYPE_GET_WORKSPACES)->recv;
say "Currently, you use " . @{$workspaces} . " workspaces";
+...or, using the sugar methods:
+
+ use AnyEvent::I3;
+
+ my $workspaces = i3->workspaces->recv;
+ say "Currently, you use " . @{$workspaces} . " workspaces";
+
=head1 EXPORT
=head2 $i3 = i3([ $path ]);
=cut
-use Exporter;
+use Exporter qw(import);
use base 'Exporter';
our @EXPORT = qw(i3);
my %events = (
workspace => ($event_mask | 0),
output => ($event_mask | 1),
+ _error => 0xFFFFFFFF,
);
sub i3 {
sub new {
my ($class, $path) = @_;
- $path ||= '/tmp/i3-ipc.sock';
+ $path ||= '~/.i3/ipc.sock';
+
+ # Check if we need to resolve ~
+ if ($path =~ /~/) {
+ # We use getpwuid() instead of $ENV{HOME} because the latter is tainted
+ # and thus produces warnings when running tests with perl -T
+ my $home = (getpwuid($<))[7];
+ die "Could not get home directory" unless $home and -d $home;
+ $path =~ s/~/$home/g;
+ }
bless { path => $path } => $class;
}
=cut
sub connect {
my ($self) = @_;
- my $hdl;
my $cv = AnyEvent->condvar;
tcp_connect "unix/", $self->{path}, sub {
$self->{ipchdl} = AnyEvent::Handle->new(
fh => $fh,
- on_read => sub { my ($hdl) = @_; $self->_data_available($hdl) }
+ on_read => sub { my ($hdl) = @_; $self->_data_available($hdl) },
+ on_error => sub {
+ my ($hdl, $fatal, $msg) = @_;
+ delete $self->{ipchdl};
+ $hdl->destroy;
+
+ my $cb = $self->{callbacks};
+
+ # Trigger all one-time callbacks with undef
+ for my $type (keys %{$cb}) {
+ next if ($type & $event_mask) == $event_mask;
+ $cb->{$type}->();
+ }
+
+ # Trigger _error callback, if set
+ my $type = $events{_error};
+ return unless defined($cb->{$type});
+ $cb->{$type}->($msg);
+ }
);
$cv->send(1)
my $cb = $self->{callbacks}->{$type};
$cb->(decode_json $payload);
+
+ return if ($type & $event_mask) == $event_mask;
+
+ # If this was a one-time callback, we delete it
+ # (when connection is lost, all one-time callbacks get triggered)
+ delete $self->{callbacks}->{$type};
}
=head2 $i3->subscribe(\%callbacks)
Subscribes to the given event types. This function awaits a hashref with the
key being the name of the event and the value being a callback.
- $i3->subscribe({
+ my %callbacks = (
workspace => sub { say "Workspaces changed" }
- });
+ );
+
+ if ($i3->subscribe(\%callbacks)->recv->{success})
+ say "Successfully subscribed";
+ }
+
+The special callback with name C<_error> is called when the connection to i3
+is killed (because of a crash, exit or restart of i3 most likely). You can
+use it to print an appropriate message and exit cleanly or to try to reconnect.
+
+ my %callbacks = (
+ _error => sub {
+ my ($msg) = @_;
+ say "I am sorry. I am so sorry: $msg";
+ exit 1;
+ }
+ );
+
+ $i3->subscribe(\%callbacks)->recv;
=cut
sub subscribe {
my ($self, $callbacks) = @_;
- my $payload = encode_json [ keys %{$callbacks} ];
- my $len = length($payload);
- my $message = $magic . pack("LL", $len, TYPE_SUBSCRIBE) . $payload;
- $self->{ipchdl}->push_write($message);
-
# Register callbacks for each message type
for my $key (keys %{$callbacks}) {
my $type = $events{$key};
$self->{callbacks}->{$type} = $callbacks->{$key};
}
+
+ $self->message(TYPE_SUBSCRIBE, [ keys %{$callbacks} ])
}
=head2 $i3->message($type, $content)
die "No message type specified" unless defined($type);
+ die "No connection to i3" unless defined($self->{ipchdl});
+
my $payload = "";
if ($content) {
if (not ref($content)) {
$cv
}
+=head1 SUGAR METHODS
+
+These methods intend to make your scripts as beautiful as possible. All of
+them automatically establish a connection to i3 blockingly (if it does not
+already exist).
+
+=cut
+
+sub _ensure_connection {
+ my ($self) = @_;
+
+ return if defined($self->{ipchdl});
+
+ $self->connect->recv or die "Unable to connect to i3"
+}
+
+=head2 get_workspaces
+
+Gets the current workspaces from i3.
+
+ my $ws = i3->get_workspaces->recv;
+ say Dumper($ws);
+
+=cut
+sub get_workspaces {
+ my ($self) = @_;
+
+ $self->_ensure_connection;
+
+ $self->message(TYPE_GET_WORKSPACES)
+}
+
+=head2 get_outputs
+
+Gets the current outputs from i3.
+
+ my $outs = i3->get_outputs->recv;
+ say Dumper($outs);
+
+=cut
+sub get_outputs {
+ my ($self) = @_;
+
+ $self->_ensure_connection;
+
+ $self->message(TYPE_GET_OUTPUTS)
+}
+
+=head2 command($content)
+
+Makes i3 execute the given command
+
+ my $reply = i3->command("reload")->recv;
+ die "command failed" unless $reply->{success};
+
+=cut
+sub command {
+ my ($self, $content) = @_;
+
+ $self->_ensure_connection;
+
+ $self->message(TYPE_COMMAND, $content)
+}
+
=head1 AUTHOR
Michael Stapelberg, C<< <michael at stapelberg.de> >>
=head1 BUGS
-Please report any bugs or feature requests to C<bug-anyevent-i3 at rt.cpan.org>, or through
-the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=AnyEvent-I3>. I will be notified, and then you'll
-automatically be notified of progress on your bug as I make changes.
+Please report any bugs or feature requests to C<bug-anyevent-i3 at
+rt.cpan.org>, or through the web interface at
+L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=AnyEvent-I3>. I will be
+notified, and then you'll automatically be notified of progress on your bug as
+I make changes.
=head1 SUPPORT