2 # vim:ts=4:sw=4:expandtab
13 AnyEvent::I3 - communicate with the i3 window manager
17 our $VERSION = '0.01';
25 This module connects to the i3 window manager using the UNIX socket based
26 IPC interface it provides (if enabled in the configuration file). You can
27 then subscribe to events or send messages and receive their replies.
29 Note that as soon as you subscribe to some kind of event, you should B<NOT>
30 send any more messages as race conditions might occur. Instead, open another
33 use AnyEvent::I3 qw(:all);
35 my $i3 = i3("/tmp/i3-ipc.sock");
38 say "Connected to i3";
40 my $workspaces = $i3->message(TYPE_GET_WORKSPACES)->recv;
41 say "Currently, you use " . @{$workspaces} . " workspaces";
45 =head2 $i3 = i3([ $path ]);
47 Creates a new C<AnyEvent::I3> object and returns it. C<path> is the path of
48 the UNIX socket to connect to.
50 =head1 SUBROUTINES/METHODS
59 use constant TYPE_COMMAND => 0;
60 use constant TYPE_GET_WORKSPACES => 1;
61 use constant TYPE_SUBSCRIBE => 2;
62 use constant TYPE_GET_OUTPUTS => 3;
64 our %EXPORT_TAGS = ( 'all' => [
65 qw(i3 TYPE_COMMAND TYPE_GET_WORKSPACES TYPE_SUBSCRIBE TYPE_GET_OUTPUTS)
68 our @EXPORT_OK = ( @{ $EXPORT_TAGS{all} } );
72 # TODO: auto-generate this from the header file? (i3/ipc.h)
73 my $event_mask = (1 << 31);
75 workspace => ($event_mask | 0),
76 output => ($event_mask | 1),
89 =head2 $i3 = AnyEvent::I3->new([ $path ])
91 Creates a new C<AnyEvent::I3> object and returns it. C<path> is the path of
92 the UNIX socket to connect to.
96 my ($class, $path) = @_;
98 $path ||= '/tmp/i3-ipc.sock';
100 bless { path => $path } => $class;
105 Establishes the connection to i3. Returns an C<AnyEvent::CondVar> which will
106 be triggered with a boolean (true if the connection was established) as soon as
107 the connection has been established.
109 if ($i3->connect->recv) {
110 say "Connected to i3";
117 my $cv = AnyEvent->condvar;
119 tcp_connect "unix/", $self->{path}, sub {
122 return $cv->send(0) unless $fh;
124 $self->{ipchdl} = AnyEvent::Handle->new(
126 on_read => sub { my ($hdl) = @_; $self->_data_available($hdl) }
135 sub _data_available {
136 my ($self, $hdl) = @_;
139 chunk => length($magic) + 4 + 4,
142 # Unpack message length and read the payload
143 my ($len, $type) = unpack("LL", substr($header, length($magic)));
146 sub { $self->_handle_i3_message($type, $_[1]) }
152 sub _handle_i3_message {
153 my ($self, $type, $payload) = @_;
155 return unless defined($self->{callbacks}->{$type});
157 my $cb = $self->{callbacks}->{$type};
158 $cb->(decode_json $payload);
161 =head2 $i3->subscribe(\%callbacks)
163 Subscribes to the given event types. This function awaits a hashref with the
164 key being the name of the event and the value being a callback.
167 workspace => sub { say "Workspaces changed" }
172 my ($self, $callbacks) = @_;
174 my $payload = encode_json [ keys %{$callbacks} ];
175 my $message = $magic . pack("LL", _bytelength($payload), 2) . $payload;
176 $self->{ipchdl}->push_write($message);
178 # Register callbacks for each message type
179 for my $key (keys %{$callbacks}) {
180 my $type = $events{$key};
181 $self->{callbacks}->{$type} = $callbacks->{$key};
185 =head2 $i3->message($type, $content)
187 Sends a message of the specified C<type> to i3, possibly containing the data
188 structure C<payload>, if specified.
190 my $reply = $i3->message(TYPE_COMMAND, "reload")->recv;
191 if ($reply->{success}) {
192 say "Configuration successfully reloaded";
197 my ($self, $type, $content) = @_;
199 die "No message type specified" unless defined($type);
203 if (ref($content) eq "SCALAR") {
206 $payload = encode_json $content;
209 my $message = $magic . pack("LL", _bytelength($payload), $type) . $payload;
210 $self->{ipchdl}->push_write($message);
212 my $cv = AnyEvent->condvar;
214 # We don’t preserve the old callback as it makes no sense to
215 # have a callback on message reply types (only on events)
216 $self->{callbacks}->{$type} =
220 undef $self->{callbacks}->{$type};
228 Michael Stapelberg, C<< <michael at stapelberg.de> >>
232 Please report any bugs or feature requests to C<bug-anyevent-i3 at rt.cpan.org>, or through
233 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=AnyEvent-I3>. I will be notified, and then you'll
234 automatically be notified of progress on your bug as I make changes.
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