]> git.sur5r.net Git - i3/i3/blob - lib/AnyEvent/I3.pm
Initial commit
[i3/i3] / lib / AnyEvent / I3.pm
1 package AnyEvent::I3;
2 # vim:ts=4:sw=4:expandtab
3
4 use strict;
5 use warnings;
6 use JSON::XS;
7 use AnyEvent::Handle;
8 use AnyEvent::Socket;
9 use AnyEvent;
10
11 =head1 NAME
12
13 AnyEvent::I3 - communicate with the i3 window manager
14
15 =cut
16
17 our $VERSION = '0.01';
18
19 =head1 VERSION
20
21 Version 0.01
22
23 =head1 SYNOPSIS
24
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.
28
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
31 connection for that.
32
33     use AnyEvent::I3;
34
35     my $i3 = i3("/tmp/i3-ipc.sock");
36
37     $i3->connect->recv;
38     say "Connected to i3";
39
40     my $workspaces = $i3->message(1)->recv;
41     say "Currently, you use " . @{$workspaces} . " workspaces";
42
43 =head1 EXPORT
44
45 =head2 $i3 = i3([ $path ]);
46
47 Creates a new C<AnyEvent::I3> object and returns it. C<path> is the path of
48 the UNIX socket to connect to.
49
50 =head1 SUBROUTINES/METHODS
51
52 =cut
53
54
55 use Exporter;
56 use base 'Exporter';
57
58 our @EXPORT = qw(i3);
59
60
61 my $magic = "i3-ipc";
62
63 # TODO: auto-generate this from the header file? (i3/ipc.h)
64 my $event_mask = (1 << 31);
65 my %events = (
66     workspace => ($event_mask | 0),
67 );
68
69 sub bytelength {
70     my ($scalar) = @_;
71     use bytes;
72     length($scalar)
73 }
74
75 sub i3 {
76     AnyEvent::I3->new(@_)
77 }
78
79 =head2 $i3 = AnyEvent::I3->new([ $path ])
80
81 Creates a new C<AnyEvent::I3> object and returns it. C<path> is the path of
82 the UNIX socket to connect to.
83
84 =cut
85 sub new {
86     my ($class, $path) = @_;
87
88     $path ||= '/tmp/i3-ipc.sock';
89
90     bless { path => $path } => $class;
91 }
92
93 =head2 $i3->connect
94
95 Establishes the connection to i3. Returns an C<AnyEvent::CondVar> which will
96 be triggered as soon as the connection has been established.
97
98 =cut
99 sub connect {
100     my ($self) = @_;
101     my $hdl;
102     my $cv = AnyEvent->condvar;
103
104     tcp_connect "unix/", $self->{path}, sub {
105         my ($fh) = @_;
106
107         $self->{ipchdl} = AnyEvent::Handle->new(
108             fh => $fh,
109             on_read => sub { my ($hdl) = @_; $self->data_available($hdl) }
110         );
111
112         $cv->send
113     };
114
115     $cv
116 }
117
118 sub data_available {
119     my ($self, $hdl) = @_;
120
121     $hdl->unshift_read(
122         chunk => length($magic) + 4 + 4,
123         sub {
124             my $header = $_[1];
125             # Unpack message length and read the payload
126             my ($len, $type) = unpack("LL", substr($header, length($magic)));
127             $hdl->unshift_read(
128                 chunk => $len,
129                 sub { $self->handle_i3_message($type, $_[1]) }
130             );
131         }
132     );
133 }
134
135 sub handle_i3_message {
136     my ($self, $type, $payload) = @_;
137
138     return unless defined($self->{callbacks}->{$type});
139
140     my $cb = $self->{callbacks}->{$type};
141     $cb->(decode_json $payload);
142 }
143
144 =head2 $i3->subscribe(\%callbacks)
145
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.
148
149     $i3->subscribe({
150         workspace => sub { say "Workspaces changed" }
151     });
152
153 =cut
154 sub subscribe {
155     my ($self, $callbacks) = @_;
156
157     my $payload = encode_json [ keys %{$callbacks} ];
158     my $message = $magic . pack("LL", bytelength($payload), 2) . $payload;
159     $self->{ipchdl}->push_write($message);
160
161     # Register callbacks for each message type
162     for my $key (keys %{$callbacks}) {
163         my $type = $events{$key};
164         $self->{callbacks}->{$type} = $callbacks->{$key};
165     }
166 }
167
168 =head2 $i3->message($type, $content)
169
170 Sends a message of the specified C<type> to i3, possibly containing the data
171 structure C<payload>, if specified.
172
173     my $cv = $i3->message(0, "reload");
174     my $reply = $cv->recv;
175     if ($reply->{success}) {
176         say "Configuration successfully reloaded";
177     }
178
179 =cut
180 sub message {
181     my ($self, $type, $content) = @_;
182
183     die "No message type specified" unless $type;
184
185     my $payload = "";
186     if ($content) {
187         if (ref($content) eq "SCALAR") {
188             $payload = $content;
189         } else {
190             $payload = encode_json $content;
191         }
192     }
193     my $message = $magic . pack("LL", bytelength($payload), $type) . $payload;
194     $self->{ipchdl}->push_write($message);
195
196     my $cv = AnyEvent->condvar;
197
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} =
201         sub {
202             my ($reply) = @_;
203             $cv->send($reply);
204             undef $self->{callbacks}->{$type};
205         };
206
207     $cv
208 }
209
210 =head1 AUTHOR
211
212 Michael Stapelberg, C<< <michael at stapelberg.de> >>
213
214 =head1 BUGS
215
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.
219
220 =head1 SUPPORT
221
222 You can find documentation for this module with the perldoc command.
223
224     perldoc AnyEvent::I3
225
226 You can also look for information at:
227
228 =over 2
229
230 =item * RT: CPAN's request tracker
231
232 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=AnyEvent-I3>
233
234 =item * The i3 window manager website
235
236 L<http://i3.zekjur.net/>
237
238 =back
239
240
241 =head1 ACKNOWLEDGEMENTS
242
243
244 =head1 LICENSE AND COPYRIGHT
245
246 Copyright 2010 Michael Stapelberg.
247
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.
251
252 See http://dev.perl.org/licenses/ for more information.
253
254
255 =cut
256
257 1; # End of AnyEvent::I3