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;
63 our %EXPORT_TAGS = ( 'all' => [
64 qw(TYPE_COMMAND TYPE_GET_WORKSPACES TYPE_SUBSCRIBE)
67 our @EXPORT_OK = ( @{ $EXPORT_TAGS{all} } );
71 # TODO: auto-generate this from the header file? (i3/ipc.h)
72 my $event_mask = (1 << 31);
74 workspace => ($event_mask | 0),
87 =head2 $i3 = AnyEvent::I3->new([ $path ])
89 Creates a new C<AnyEvent::I3> object and returns it. C<path> is the path of
90 the UNIX socket to connect to.
94 my ($class, $path) = @_;
96 $path ||= '/tmp/i3-ipc.sock';
98 bless { path => $path } => $class;
103 Establishes the connection to i3. Returns an C<AnyEvent::CondVar> which will
104 be triggered with a boolean (true if the connection was established) as soon as
105 the connection has been established.
107 if ($i3->connect->recv) {
108 say "Connected to i3";
115 my $cv = AnyEvent->condvar;
117 tcp_connect "unix/", $self->{path}, sub {
120 return $cv->send(0) unless $fh;
122 $self->{ipchdl} = AnyEvent::Handle->new(
124 on_read => sub { my ($hdl) = @_; $self->_data_available($hdl) }
133 sub _data_available {
134 my ($self, $hdl) = @_;
137 chunk => length($magic) + 4 + 4,
140 # Unpack message length and read the payload
141 my ($len, $type) = unpack("LL", substr($header, length($magic)));
144 sub { $self->_handle_i3_message($type, $_[1]) }
150 sub _handle_i3_message {
151 my ($self, $type, $payload) = @_;
153 return unless defined($self->{callbacks}->{$type});
155 my $cb = $self->{callbacks}->{$type};
156 $cb->(decode_json $payload);
159 =head2 $i3->subscribe(\%callbacks)
161 Subscribes to the given event types. This function awaits a hashref with the
162 key being the name of the event and the value being a callback.
165 workspace => sub { say "Workspaces changed" }
170 my ($self, $callbacks) = @_;
172 my $payload = encode_json [ keys %{$callbacks} ];
173 my $message = $magic . pack("LL", _bytelength($payload), 2) . $payload;
174 $self->{ipchdl}->push_write($message);
176 # Register callbacks for each message type
177 for my $key (keys %{$callbacks}) {
178 my $type = $events{$key};
179 $self->{callbacks}->{$type} = $callbacks->{$key};
183 =head2 $i3->message($type, $content)
185 Sends a message of the specified C<type> to i3, possibly containing the data
186 structure C<payload>, if specified.
188 my $reply = $i3->message(TYPE_COMMAND, "reload")->recv;
189 if ($reply->{success}) {
190 say "Configuration successfully reloaded";
195 my ($self, $type, $content) = @_;
197 die "No message type specified" unless $type;
201 if (ref($content) eq "SCALAR") {
204 $payload = encode_json $content;
207 my $message = $magic . pack("LL", _bytelength($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 rt.cpan.org>, or through
231 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=AnyEvent-I3>. I will be notified, and then you'll
232 automatically be notified of progress on your bug as I make changes.
236 You can find documentation for this module with the perldoc command.
240 You can also look for information at:
244 =item * RT: CPAN's request tracker
246 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=AnyEvent-I3>
248 =item * The i3 window manager website
250 L<http://i3.zekjur.net/>
255 =head1 ACKNOWLEDGEMENTS
258 =head1 LICENSE AND COPYRIGHT
260 Copyright 2010 Michael Stapelberg.
262 This program is free software; you can redistribute it and/or modify it
263 under the terms of either: the GNU General Public License as published
264 by the Free Software Foundation; or the Artistic License.
266 See http://dev.perl.org/licenses/ for more information.
271 1; # End of AnyEvent::I3