1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
\r
2 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
\r
3 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
\r
5 <link rel="icon" type="image/png" href="/favicon.png">
\r
6 <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8" />
\r
7 <meta name="generator" content="AsciiDoc 8.6.4" />
\r
8 <title>i3: IPC interface (interprocess communication)</title>
\r
9 <link rel="stylesheet" href="/css/style.css" type="text/css" />
\r
10 <link rel="stylesheet" href="/css/xhtml11.css" type="text/css" />
\r
11 <script type="text/javascript">
\r
13 window.onload = function(){asciidoc.footnotes(); asciidoc.toc(2);}
\r
16 <script type="text/javascript" src="/js/asciidoc-xhtml11.js"></script>
\r
18 <body class="article">
\r
21 <a href="/"><h1 id="title">i3 - improved tiling WM</h1></a>
\r
23 <li><a style="border-bottom: 2px solid #fff" href="/docs">Docs</a></li>
\r
24 <li><a href="/screenshots">Screens</a></li>
\r
25 <li><a href="/contact">Contact</a></li>
\r
26 <li><a href="http://bugs.i3wm.org/">Bugs</a></li>
\r
28 <br style="clear: both">
\r
31 <h1>IPC interface (interprocess communication)</h1>
\r
32 <span id="author">Michael Stapelberg</span><br />
\r
33 <span id="email"><tt><<a href="mailto:michael+i3@stapelberg.de">michael+i3@stapelberg.de</a>></tt></span><br />
\r
34 <span id="revdate">March 2010</span>
\r
36 <div id="toctitle">Table of Contents</div>
37 <noscript><p><b>JavaScript must be enabled in your browser to display the table of contents.</b></p></noscript>
41 <div class="sectionbody">
\r
42 <div class="paragraph"><p>This document describes how to interface with i3 from a separate process. This
\r
43 is useful for example to remote-control i3 (to write test cases for example) or
\r
44 to get various information like the current workspaces to implement an external
\r
45 workspace bar.</p></div>
\r
46 <div class="paragraph"><p>The method of choice for IPC in our case is a unix socket because it has very
\r
47 little overhead on both sides and is usually available without headaches in
\r
48 most languages. In the default configuration file, no ipc-socket path is
\r
49 specified and thus no socket is created. The standard path (which <tt>i3-msg</tt> and
\r
50 <tt>i3-input</tt> use) is <tt>~/.i3/ipc.sock</tt>.</p></div>
\r
54 <h2 id="_establishing_a_connection">1. Establishing a connection</h2>
\r
55 <div class="sectionbody">
\r
56 <div class="paragraph"><p>To establish a connection, simply open the IPC socket. The following code
\r
57 snippet illustrates this in Perl:</p></div>
\r
58 <div class="listingblock">
\r
59 <div class="content">
\r
60 <pre><tt>use IO::Socket::UNIX;
\r
61 my $sock = IO::Socket::UNIX->new(Peer => '~/.i3/ipc.sock');</tt></pre>
\r
66 <h2 id="_sending_messages_to_i3">2. Sending messages to i3</h2>
\r
67 <div class="sectionbody">
\r
68 <div class="paragraph"><p>To send a message to i3, you have to format in the binary message format which
\r
69 i3 expects. This format specifies a magic string in the beginning to ensure
\r
70 the integrity of messages (to prevent follow-up errors). Following the magic
\r
71 string comes the length of the payload of the message as 32-bit integer, and
\r
72 the type of the message as 32-bit integer (the integers are not converted, so
\r
73 they are in native byte order).</p></div>
\r
74 <div class="paragraph"><p>The magic string currently is "i3-ipc" and will only be changed when a change
\r
75 in the IPC API is done which breaks compatibility (we hope that we don’t need
\r
76 to do that).</p></div>
\r
77 <div class="paragraph"><p>Currently implemented message types are the following:</p></div>
\r
78 <div class="dlist"><dl>
\r
79 <dt class="hdlist1">
\r
84 The payload of the message is a command for i3 (like the commands you
\r
85 can bind to keys in the configuration file) and will be executed
\r
86 directly after receiving it. There is no reply to this message.
\r
89 <dt class="hdlist1">
\r
94 Gets the current workspaces. The reply will be a JSON-encoded list of
\r
95 workspaces (see the reply section).
\r
98 <dt class="hdlist1">
\r
103 Subscribes your connection to certain events. See <a href="#events">[events]</a> for a
\r
104 description of this message and the concept of events.
\r
107 <dt class="hdlist1">
\r
112 Gets the current outputs. The reply will be a JSON-encoded list of outputs
\r
113 (see the reply section).
\r
117 <div class="paragraph"><p>So, a typical message could look like this:</p></div>
\r
118 <div class="listingblock">
\r
119 <div class="content">
\r
120 <pre><tt>"i3-ipc" <message length> <message type> <payload></tt></pre>
\r
122 <div class="paragraph"><p>Or, as a hexdump:</p></div>
\r
123 <div class="listingblock">
\r
124 <div class="content">
\r
125 <pre><tt>00000000 69 33 2d 69 70 63 04 00 00 00 00 00 00 00 65 78 |i3-ipc........ex|
\r
126 00000010 69 74 0a |it.|</tt></pre>
\r
128 <div class="paragraph"><p>To generate and send such a message, you could use the following code in Perl:</p></div>
\r
129 <div class="listingblock">
\r
130 <div class="content">
\r
131 <pre><tt>sub format_ipc_command {
\r
134 # Get the real byte count (vs. amount of characters)
\r
135 { use bytes; $len = length($msg); }
\r
136 return "i3-ipc" . pack("LL", $len, 0) . $msg;
\r
139 $sock->write(format_ipc_command("exit"));</tt></pre>
\r
143 <div class="sect1">
\r
144 <h2 id="_receiving_replies_from_i3">3. Receiving replies from i3</h2>
\r
145 <div class="sectionbody">
\r
146 <div class="paragraph"><p>Replies from i3 usually consist of a simple string (the length of the string
\r
147 is the message_length, so you can consider them length-prefixed) which in turn
\r
148 contain the JSON serialization of a data structure. For example, the
\r
149 GET_WORKSPACES message returns an array of workspaces (each workspace is a map
\r
150 with certain attributes).</p></div>
\r
151 <div class="sect2">
\r
152 <h3 id="_reply_format">3.1. Reply format</h3>
\r
153 <div class="paragraph"><p>The reply format is identical to the normal message format. There also is
\r
154 the magic string, then the message length, then the message type and the
\r
156 <div class="paragraph"><p>The following reply types are implemented:</p></div>
\r
157 <div class="dlist"><dl>
\r
158 <dt class="hdlist1">
\r
163 Confirmation/Error code for the COMMAND message.
\r
166 <dt class="hdlist1">
\r
171 Reply to the GET_WORKSPACES message.
\r
174 <dt class="hdlist1">
\r
179 Confirmation/Error code for the SUBSCRIBE message.
\r
182 <dt class="hdlist1">
\r
187 Reply to the GET_OUTPUTS message.
\r
192 <div class="sect2">
\r
193 <h3 id="_command_reply">3.2. COMMAND reply</h3>
\r
194 <div class="paragraph"><p>The reply consists of a single serialized map. At the moment, the only
\r
195 property is <tt>success (bool)</tt>, but this will be expanded in future versions.</p></div>
\r
196 <div class="paragraph"><p><strong>Example:</strong></p></div>
\r
197 <div class="listingblock">
\r
198 <div class="content">
\r
199 <pre><tt>{ "success": true }</tt></pre>
\r
202 <div class="sect2">
\r
203 <h3 id="_get_workspaces_reply">3.3. GET_WORKSPACES reply</h3>
\r
204 <div class="paragraph"><p>The reply consists of a serialized list of workspaces. Each workspace has the
\r
205 following properties:</p></div>
\r
206 <div class="dlist"><dl>
\r
207 <dt class="hdlist1">
\r
212 The logical number of the workspace. Corresponds to the command
\r
213 to switch to this workspace.
\r
216 <dt class="hdlist1">
\r
221 The name of this workspace (by default num+1), as changed by the
\r
222 user. Encoded in UTF-8.
\r
225 <dt class="hdlist1">
\r
230 Whether this workspace is currently visible on an output (multiple
\r
231 workspaces can be visible at the same time).
\r
234 <dt class="hdlist1">
\r
239 Whether this workspace currently has the focus (only one workspace
\r
240 can have the focus at the same time).
\r
243 <dt class="hdlist1">
\r
248 Whether a window on this workspace has the "urgent" flag set.
\r
251 <dt class="hdlist1">
\r
256 The rectangle of this workspace (equals the rect of the output it
\r
257 is on), consists of x, y, width, height.
\r
260 <dt class="hdlist1">
\r
265 The video output this workspace is on (LVDS1, VGA1, …).
\r
269 <div class="paragraph"><p><strong>Example:</strong></p></div>
\r
270 <div class="listingblock">
\r
271 <div class="content">
\r
304 <div class="sect2">
\r
305 <h3 id="_subscribe_reply">3.4. SUBSCRIBE reply</h3>
\r
306 <div class="paragraph"><p>The reply consists of a single serialized map. The only property is
\r
307 <tt>success (bool)</tt>, indicating whether the subscription was successful (the
\r
308 default) or whether a JSON parse error occurred.</p></div>
\r
309 <div class="paragraph"><p><strong>Example:</strong></p></div>
\r
310 <div class="listingblock">
\r
311 <div class="content">
\r
312 <pre><tt>{ "success": true }</tt></pre>
\r
315 <div class="sect2">
\r
316 <h3 id="_get_outputs_reply">3.5. GET_OUTPUTS reply</h3>
\r
317 <div class="paragraph"><p>The reply consists of a serialized list of outputs. Each output has the
\r
318 following properties:</p></div>
\r
319 <div class="dlist"><dl>
\r
320 <dt class="hdlist1">
\r
325 The name of this output (as seen in <tt>xrandr(1)</tt>). Encoded in UTF-8.
\r
328 <dt class="hdlist1">
\r
333 Whether this output is currently active (has a valid mode).
\r
336 <dt class="hdlist1">
\r
337 current_workspace (integer)
\r
341 The current workspace which is visible on this output. <tt>null</tt> if the
\r
342 output is not active.
\r
345 <dt class="hdlist1">
\r
350 The rectangle of this output (equals the rect of the output it
\r
351 is on), consists of x, y, width, height.
\r
355 <div class="paragraph"><p><strong>Example:</strong></p></div>
\r
356 <div class="listingblock">
\r
357 <div class="content">
\r
362 "current_workspace": 4,
\r
373 "current_workspace": 1,
\r
386 <div class="sect1">
\r
387 <h2 id="_events">4. Events</h2>
\r
388 <div class="sectionbody">
\r
389 <div class="paragraph" id="events"><p>To get informed when certain things happen in i3, clients can subscribe to
\r
390 events. Events consist of a name (like "workspace") and an event reply type
\r
391 (like I3_IPC_EVENT_WORKSPACE). The events sent by i3 are in the same format
\r
392 as replies to specific commands. However, the highest bit of the message type
\r
393 is set to 1 to indicate that this is an event reply instead of a normal reply.</p></div>
\r
394 <div class="paragraph"><p>Caveat: As soon as you subscribe to an event, it is not guaranteed any longer
\r
395 that the requests to i3 are processed in order. This means, the following
\r
396 situation can happen: You send a GET_WORKSPACES request but you receive a
\r
397 "workspace" event before receiving the reply to GET_WORKSPACES. If your
\r
398 program does not want to cope which such kinds of race conditions (an
\r
399 event based library may not have a problem here), I suggest you create a
\r
400 separate connection to receive events.</p></div>
\r
401 <div class="sect2">
\r
402 <h3 id="_subscribing_to_events">4.1. Subscribing to events</h3>
\r
403 <div class="paragraph"><p>By sending a message of type SUBSCRIBE with a JSON-encoded array as payload
\r
404 you can register to an event.</p></div>
\r
405 <div class="paragraph"><p><strong>Example:</strong></p></div>
\r
406 <div class="listingblock">
\r
407 <div class="content">
\r
408 <pre><tt>type: SUBSCRIBE
\r
409 payload: [ "workspace", "focus" ]</tt></pre>
\r
412 <div class="sect2">
\r
413 <h3 id="_available_events">4.2. Available events</h3>
\r
414 <div class="paragraph"><p>The numbers in parenthesis is the event type (keep in mind that you need to
\r
415 strip the highest bit first).</p></div>
\r
416 <div class="dlist"><dl>
\r
417 <dt class="hdlist1">
\r
422 Sent when the user switches to a different workspace, when a new
\r
423 workspace is initialized or when a workspace is removed (because the
\r
424 last client vanished).
\r
427 <dt class="hdlist1">
\r
432 Sent when RandR issues a change notification (of either screens,
\r
433 outputs, CRTCs or output properties).
\r
437 <div class="paragraph"><p><strong>Example:</strong></p></div>
\r
438 <div class="listingblock">
\r
439 <div class="content">
\r
440 <pre><tt># the appropriate 4 bytes read from the socket are stored in $input
\r
442 # unpack a 32-bit unsigned integer
\r
443 my $message_type = unpack("L", $input);
\r
445 # check if the highest bit is 1
\r
446 my $is_event = (($message_type >> 31) == 1);
\r
448 # use the other bits
\r
449 my $event_type = ($message_type & 0x7F);
\r
452 say "Received event of type $event_type";
\r
456 <div class="sect2">
\r
457 <h3 id="_workspace_event">4.3. workspace event</h3>
\r
458 <div class="paragraph"><p>This event consists of a single serialized map containing a property
\r
459 <tt>change (string)</tt> which indicates the type of the change ("focus", "init",
\r
460 "empty", "urgent").</p></div>
\r
461 <div class="paragraph"><p><strong>Example:</strong></p></div>
\r
462 <div class="listingblock">
\r
463 <div class="content">
\r
464 <pre><tt>{ "change": "focus" }</tt></pre>
\r
467 <div class="sect2">
\r
468 <h3 id="_output_event">4.4. output event</h3>
\r
469 <div class="paragraph"><p>This event consists of a single serialized map containing a property
\r
470 <tt>change (string)</tt> which indicates the type of the change (currently only
\r
471 "unspecified").</p></div>
\r
472 <div class="paragraph"><p><strong>Example:</strong></p></div>
\r
473 <div class="listingblock">
\r
474 <div class="content">
\r
475 <pre><tt>{ "change": "unspecified" }</tt></pre>
\r
480 <div class="sect1">
\r
481 <h2 id="_see_also">5. See also</h2>
\r
482 <div class="sectionbody">
\r
483 <div class="paragraph"><p>For some languages, libraries are available (so you don’t have to implement
\r
484 all this on your own). This list names some (if you wrote one, please let me
\r
486 <div class="dlist"><dl>
\r
487 <dt class="hdlist1">
\r
492 i3 includes a headerfile <tt>i3/ipc.h</tt> which provides you all constants.
\r
493 However, there is no library yet.
\r
496 <dt class="hdlist1">
\r
501 <a href="http://github.com/badboy/i3-ipc">http://github.com/badboy/i3-ipc</a>
\r
504 <dt class="hdlist1">
\r
509 <a href="http://search.cpan.org/search?query=AnyEvent::I3">http://search.cpan.org/search?query=AnyEvent::I3</a>
\r
512 <dt class="hdlist1">
\r
517 <a href="http://github.com/thepub/i3ipc">http://github.com/thepub/i3ipc</a>
\r
524 <div id="footnotes"><hr /></div>
\r
525 <div id="footer" lang="de">
\r
526 © 2009-2011 Michael Stapelberg, <a href="/impress.html">Impressum</a>
\r