2 # vim:ts=4:sw=4:expandtab
14 AnyEvent::I3 - communicate with the i3 window manager
18 our $VERSION = '0.01';
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 Note that as soon as you subscribe to some kind of event, you should B<NOT>
31 send any more messages as race conditions might occur. Instead, open another
34 use AnyEvent::I3 qw(:all);
36 my $i3 = i3("/tmp/i3-ipc.sock");
39 say "Connected to i3";
41 my $workspaces = $i3->message(TYPE_GET_WORKSPACES)->recv;
42 say "Currently, you use " . @{$workspaces} . " workspaces";
46 =head2 $i3 = i3([ $path ]);
48 Creates a new C<AnyEvent::I3> object and returns it. C<path> is the path of
49 the UNIX socket to connect to.
51 =head1 SUBROUTINES/METHODS
60 use constant TYPE_COMMAND => 0;
61 use constant TYPE_GET_WORKSPACES => 1;
62 use constant TYPE_SUBSCRIBE => 2;
63 use constant TYPE_GET_OUTPUTS => 3;
65 our %EXPORT_TAGS = ( 'all' => [
66 qw(i3 TYPE_COMMAND TYPE_GET_WORKSPACES TYPE_SUBSCRIBE TYPE_GET_OUTPUTS)
69 our @EXPORT_OK = ( @{ $EXPORT_TAGS{all} } );
73 # TODO: auto-generate this from the header file? (i3/ipc.h)
74 my $event_mask = (1 << 31);
76 workspace => ($event_mask | 0),
77 output => ($event_mask | 1),
84 =head2 $i3 = AnyEvent::I3->new([ $path ])
86 Creates a new C<AnyEvent::I3> object and returns it. C<path> is the path of
87 the UNIX socket to connect to.
91 my ($class, $path) = @_;
93 $path ||= '/tmp/i3-ipc.sock';
95 bless { path => $path } => $class;
100 Establishes the connection to i3. Returns an C<AnyEvent::CondVar> which will
101 be triggered with a boolean (true if the connection was established) as soon as
102 the connection has been established.
104 if ($i3->connect->recv) {
105 say "Connected to i3";
112 my $cv = AnyEvent->condvar;
114 tcp_connect "unix/", $self->{path}, sub {
117 return $cv->send(0) unless $fh;
119 $self->{ipchdl} = AnyEvent::Handle->new(
121 on_read => sub { my ($hdl) = @_; $self->_data_available($hdl) }
130 sub _data_available {
131 my ($self, $hdl) = @_;
134 chunk => length($magic) + 4 + 4,
137 # Unpack message length and read the payload
138 my ($len, $type) = unpack("LL", substr($header, length($magic)));
141 sub { $self->_handle_i3_message($type, $_[1]) }
147 sub _handle_i3_message {
148 my ($self, $type, $payload) = @_;
150 return unless defined($self->{callbacks}->{$type});
152 my $cb = $self->{callbacks}->{$type};
153 $cb->(decode_json $payload);
156 =head2 $i3->subscribe(\%callbacks)
158 Subscribes to the given event types. This function awaits a hashref with the
159 key being the name of the event and the value being a callback.
162 workspace => sub { say "Workspaces changed" }
167 my ($self, $callbacks) = @_;
169 my $payload = encode_json [ keys %{$callbacks} ];
170 my $len = length($payload);
171 my $message = $magic . pack("LL", $len, TYPE_SUBSCRIBE) . $payload;
172 $self->{ipchdl}->push_write($message);
174 # Register callbacks for each message type
175 for my $key (keys %{$callbacks}) {
176 my $type = $events{$key};
177 $self->{callbacks}->{$type} = $callbacks->{$key};
181 =head2 $i3->message($type, $content)
183 Sends a message of the specified C<type> to i3, possibly containing the data
184 structure C<content> (or C<content>, encoded as utf8, if C<content> is a
185 scalar), if specified.
187 my $reply = $i3->message(TYPE_COMMAND, "reload")->recv;
188 if ($reply->{success}) {
189 say "Configuration successfully reloaded";
194 my ($self, $type, $content) = @_;
196 die "No message type specified" unless defined($type);
200 if (not ref($content)) {
201 # Convert from Perl’s internal encoding to UTF8 octets
202 $payload = encode_utf8($content);
204 $payload = encode_json $content;
207 my $message = $magic . pack("LL", length($payload), $type) . $payload;
208 $self->{ipchdl}->push_write($message);
210 my $cv = AnyEvent->condvar;
212 # We don’t preserve the old callback as it makes no sense to
213 # have a callback on message reply types (only on events)
214 $self->{callbacks}->{$type} =
218 undef $self->{callbacks}->{$type};
226 Michael Stapelberg, C<< <michael at stapelberg.de> >>
230 Please report any bugs or feature requests to C<bug-anyevent-i3 at
231 rt.cpan.org>, or through the web interface at
232 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=AnyEvent-I3>. I will be
233 notified, and then you'll automatically be notified of progress on your bug as
238 You can find documentation for this module with the perldoc command.
242 You can also look for information at:
246 =item * RT: CPAN's request tracker
248 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=AnyEvent-I3>
250 =item * The i3 window manager website
252 L<http://i3.zekjur.net/>
257 =head1 ACKNOWLEDGEMENTS
260 =head1 LICENSE AND COPYRIGHT
262 Copyright 2010 Michael Stapelberg.
264 This program is free software; you can redistribute it and/or modify it
265 under the terms of either: the GNU General Public License as published
266 by the Free Software Foundation; or the Artistic License.
268 See http://dev.perl.org/licenses/ for more information.
273 1; # End of AnyEvent::I3