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
35 my $i3 = i3("/tmp/i3-ipc.sock");
38 say "Connected to i3";
40 my $workspaces = $i3->message(1)->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
63 # TODO: auto-generate this from the header file? (i3/ipc.h)
64 my $event_mask = (1 << 31);
66 workspace => ($event_mask | 0),
79 =head2 $i3 = AnyEvent::I3->new([ $path ])
81 Creates a new C<AnyEvent::I3> object and returns it. C<path> is the path of
82 the UNIX socket to connect to.
86 my ($class, $path) = @_;
88 $path ||= '/tmp/i3-ipc.sock';
90 bless { path => $path } => $class;
95 Establishes the connection to i3. Returns an C<AnyEvent::CondVar> which will
96 be triggered as soon as the connection has been established.
102 my $cv = AnyEvent->condvar;
104 tcp_connect "unix/", $self->{path}, sub {
107 $self->{ipchdl} = AnyEvent::Handle->new(
109 on_read => sub { my ($hdl) = @_; $self->data_available($hdl) }
119 my ($self, $hdl) = @_;
122 chunk => length($magic) + 4 + 4,
125 # Unpack message length and read the payload
126 my ($len, $type) = unpack("LL", substr($header, length($magic)));
129 sub { $self->handle_i3_message($type, $_[1]) }
135 sub handle_i3_message {
136 my ($self, $type, $payload) = @_;
138 return unless defined($self->{callbacks}->{$type});
140 my $cb = $self->{callbacks}->{$type};
141 $cb->(decode_json $payload);
144 =head2 $i3->subscribe(\%callbacks)
146 Subscribes to the given event types. This function awaits a hashref with the
147 key being the name of the event and the value being a callback.
150 workspace => sub { say "Workspaces changed" }
155 my ($self, $callbacks) = @_;
157 my $payload = encode_json [ keys %{$callbacks} ];
158 my $message = $magic . pack("LL", bytelength($payload), 2) . $payload;
159 $self->{ipchdl}->push_write($message);
161 # Register callbacks for each message type
162 for my $key (keys %{$callbacks}) {
163 my $type = $events{$key};
164 $self->{callbacks}->{$type} = $callbacks->{$key};
168 =head2 $i3->message($type, $content)
170 Sends a message of the specified C<type> to i3, possibly containing the data
171 structure C<payload>, if specified.
173 my $cv = $i3->message(0, "reload");
174 my $reply = $cv->recv;
175 if ($reply->{success}) {
176 say "Configuration successfully reloaded";
181 my ($self, $type, $content) = @_;
183 die "No message type specified" unless $type;
187 if (ref($content) eq "SCALAR") {
190 $payload = encode_json $content;
193 my $message = $magic . pack("LL", bytelength($payload), $type) . $payload;
194 $self->{ipchdl}->push_write($message);
196 my $cv = AnyEvent->condvar;
198 # We don’t preserve the old callback as it makes no sense to
199 # have a callback on message reply types (only on events)
200 $self->{callbacks}->{$type} =
204 undef $self->{callbacks}->{$type};
212 Michael Stapelberg, C<< <michael at stapelberg.de> >>
216 Please report any bugs or feature requests to C<bug-anyevent-i3 at rt.cpan.org>, or through
217 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=AnyEvent-I3>. I will be notified, and then you'll
218 automatically be notified of progress on your bug as I make changes.
222 You can find documentation for this module with the perldoc command.
226 You can also look for information at:
230 =item * RT: CPAN's request tracker
232 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=AnyEvent-I3>
234 =item * The i3 window manager website
236 L<http://i3.zekjur.net/>
241 =head1 ACKNOWLEDGEMENTS
244 =head1 LICENSE AND COPYRIGHT
246 Copyright 2010 Michael Stapelberg.
248 This program is free software; you can redistribute it and/or modify it
249 under the terms of either: the GNU General Public License as published
250 by the Free Software Foundation; or the Artistic License.
252 See http://dev.perl.org/licenses/ for more information.
257 1; # End of AnyEvent::I3