2 # vim:ts=4:sw=4:expandtab
14 AnyEvent::I3 - communicate with the i3 window manager
18 our $VERSION = '0.07';
26 This module connects to the i3 window manager using the UNIX socket based
27 IPC interface it provides (if enabled in the configuration file). You can
28 then subscribe to events or send messages and receive their replies.
30 use AnyEvent::I3 qw(:all);
32 my $i3 = i3("~/.i3/ipc.sock");
34 $i3->connect->recv or die "Error connecting";
35 say "Connected to i3";
37 my $workspaces = $i3->message(TYPE_GET_WORKSPACES)->recv;
38 say "Currently, you use " . @{$workspaces} . " workspaces";
40 ...or, using the sugar methods:
44 my $workspaces = i3->get_workspaces->recv;
45 say "Currently, you use " . @{$workspaces} . " workspaces";
49 =head2 $i3 = i3([ $path ]);
51 Creates a new C<AnyEvent::I3> object and returns it. C<path> is the path of
52 the UNIX socket to connect to.
54 =head1 SUBROUTINES/METHODS
58 use Exporter qw(import);
63 use constant TYPE_COMMAND => 0;
64 use constant TYPE_GET_WORKSPACES => 1;
65 use constant TYPE_SUBSCRIBE => 2;
66 use constant TYPE_GET_OUTPUTS => 3;
67 use constant TYPE_GET_TREE => 4;
68 use constant TYPE_GET_MARKS => 5;
70 our %EXPORT_TAGS = ( 'all' => [
71 qw(i3 TYPE_COMMAND TYPE_GET_WORKSPACES TYPE_SUBSCRIBE TYPE_GET_OUTPUTS TYPE_GET_TREE TYPE_GET_MARKS)
74 our @EXPORT_OK = ( @{ $EXPORT_TAGS{all} } );
78 # TODO: auto-generate this from the header file? (i3/ipc.h)
79 my $event_mask = (1 << 31);
81 workspace => ($event_mask | 0),
82 output => ($event_mask | 1),
90 =head2 $i3 = AnyEvent::I3->new([ $path ])
92 Creates a new C<AnyEvent::I3> object and returns it. C<path> is the path of
93 the UNIX socket to connect to.
97 my ($class, $path) = @_;
99 $path ||= '~/.i3/ipc.sock';
101 # Check if we need to resolve ~
103 # We use getpwuid() instead of $ENV{HOME} because the latter is tainted
104 # and thus produces warnings when running tests with perl -T
105 my $home = (getpwuid($<))[7];
106 die "Could not get home directory" unless $home and -d $home;
107 $path =~ s/~/$home/g;
110 bless { path => $path } => $class;
115 Establishes the connection to i3. Returns an C<AnyEvent::CondVar> which will
116 be triggered with a boolean (true if the connection was established) as soon as
117 the connection has been established.
119 if ($i3->connect->recv) {
120 say "Connected to i3";
126 my $cv = AnyEvent->condvar;
128 tcp_connect "unix/", $self->{path}, sub {
131 return $cv->send(0) unless $fh;
133 $self->{ipchdl} = AnyEvent::Handle->new(
135 on_read => sub { my ($hdl) = @_; $self->_data_available($hdl) },
137 my ($hdl, $fatal, $msg) = @_;
138 delete $self->{ipchdl};
141 my $cb = $self->{callbacks};
143 # Trigger all one-time callbacks with undef
144 for my $type (keys %{$cb}) {
145 next if ($type & $event_mask) == $event_mask;
149 # Trigger _error callback, if set
150 my $type = $events{_error};
151 return unless defined($cb->{$type});
152 $cb->{$type}->($msg);
162 sub _data_available {
163 my ($self, $hdl) = @_;
166 chunk => length($magic) + 4 + 4,
169 # Unpack message length and read the payload
170 my ($len, $type) = unpack("LL", substr($header, length($magic)));
173 sub { $self->_handle_i3_message($type, $_[1]) }
179 sub _handle_i3_message {
180 my ($self, $type, $payload) = @_;
182 return unless defined($self->{callbacks}->{$type});
184 my $cb = $self->{callbacks}->{$type};
185 $cb->(decode_json $payload);
187 return if ($type & $event_mask) == $event_mask;
189 # If this was a one-time callback, we delete it
190 # (when connection is lost, all one-time callbacks get triggered)
191 delete $self->{callbacks}->{$type};
194 =head2 $i3->subscribe(\%callbacks)
196 Subscribes to the given event types. This function awaits a hashref with the
197 key being the name of the event and the value being a callback.
200 workspace => sub { say "Workspaces changed" }
203 if ($i3->subscribe(\%callbacks)->recv->{success})
204 say "Successfully subscribed";
207 The special callback with name C<_error> is called when the connection to i3
208 is killed (because of a crash, exit or restart of i3 most likely). You can
209 use it to print an appropriate message and exit cleanly or to try to reconnect.
214 say "I am sorry. I am so sorry: $msg";
219 $i3->subscribe(\%callbacks)->recv;
223 my ($self, $callbacks) = @_;
225 # Register callbacks for each message type
226 for my $key (keys %{$callbacks}) {
227 my $type = $events{$key};
228 $self->{callbacks}->{$type} = $callbacks->{$key};
231 $self->message(TYPE_SUBSCRIBE, [ keys %{$callbacks} ])
234 =head2 $i3->message($type, $content)
236 Sends a message of the specified C<type> to i3, possibly containing the data
237 structure C<content> (or C<content>, encoded as utf8, if C<content> is a
238 scalar), if specified.
240 my $reply = $i3->message(TYPE_COMMAND, "reload")->recv;
241 if ($reply->{success}) {
242 say "Configuration successfully reloaded";
247 my ($self, $type, $content) = @_;
249 die "No message type specified" unless defined($type);
251 die "No connection to i3" unless defined($self->{ipchdl});
255 if (not ref($content)) {
256 # Convert from Perl’s internal encoding to UTF8 octets
257 $payload = encode_utf8($content);
259 $payload = encode_json $content;
262 my $message = $magic . pack("LL", length($payload), $type) . $payload;
263 $self->{ipchdl}->push_write($message);
265 my $cv = AnyEvent->condvar;
267 # We don’t preserve the old callback as it makes no sense to
268 # have a callback on message reply types (only on events)
269 $self->{callbacks}->{$type} =
273 undef $self->{callbacks}->{$type};
281 These methods intend to make your scripts as beautiful as possible. All of
282 them automatically establish a connection to i3 blockingly (if it does not
287 sub _ensure_connection {
290 return if defined($self->{ipchdl});
292 $self->connect->recv or die "Unable to connect to i3"
295 =head2 get_workspaces
297 Gets the current workspaces from i3.
299 my $ws = i3->get_workspaces->recv;
306 $self->_ensure_connection;
308 $self->message(TYPE_GET_WORKSPACES)
313 Gets the current outputs from i3.
315 my $outs = i3->get_outputs->recv;
322 $self->_ensure_connection;
324 $self->message(TYPE_GET_OUTPUTS)
329 Gets the layout tree from i3 (>= v4.0).
331 my $tree = i3->get_tree->recv;
338 $self->_ensure_connection;
340 $self->message(TYPE_GET_TREE)
345 Gets all the window identifier marks from i3 (>= v4.1).
347 my $marks = i3->get_marks->recv;
354 $self->_ensure_connection;
356 $self->message(TYPE_GET_MARKS)
359 =head2 command($content)
361 Makes i3 execute the given command
363 my $reply = i3->command("reload")->recv;
364 die "command failed" unless $reply->{success};
368 my ($self, $content) = @_;
370 $self->_ensure_connection;
372 $self->message(TYPE_COMMAND, $content)
377 Michael Stapelberg, C<< <michael at stapelberg.de> >>
381 Please report any bugs or feature requests to C<bug-anyevent-i3 at
382 rt.cpan.org>, or through the web interface at
383 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=AnyEvent-I3>. I will be
384 notified, and then you'll automatically be notified of progress on your bug as
389 You can find documentation for this module with the perldoc command.
393 You can also look for information at:
397 =item * RT: CPAN's request tracker
399 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=AnyEvent-I3>
401 =item * The i3 window manager website
403 L<http://i3.zekjur.net/>
408 =head1 ACKNOWLEDGEMENTS
411 =head1 LICENSE AND COPYRIGHT
413 Copyright 2010 Michael Stapelberg.
415 This program is free software; you can redistribute it and/or modify it
416 under the terms of either: the GNU General Public License as published
417 by the Free Software Foundation; or the Artistic License.
419 See http://dev.perl.org/licenses/ for more information.
424 1; # End of AnyEvent::I3