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, the ipc-socket gets created
\r
49 in <tt>/tmp/i3-%u/ipc-socket.%p</tt> where <tt>%u</tt> is your UNIX username and <tt>%p</tt> is the
\r
50 PID of i3.</p></div>
\r
51 <div class="paragraph"><p>All i3 utilities, like <tt>i3-msg</tt> and <tt>i3-input</tt> will read the <tt>I3_SOCKET_PATH</tt>
\r
52 X11 property, stored on the X11 root window.</p></div>
\r
56 <h2 id="_establishing_a_connection">1. Establishing a connection</h2>
\r
57 <div class="sectionbody">
\r
58 <div class="paragraph"><p>To establish a connection, simply open the IPC socket. The following code
\r
59 snippet illustrates this in Perl:</p></div>
\r
60 <div class="listingblock">
\r
61 <div class="content">
\r
62 <pre><tt>use IO::Socket::UNIX;
\r
63 my $sock = IO::Socket::UNIX->new(Peer => '/tmp/i3-ipc.sock');</tt></pre>
\r
68 <h2 id="_sending_messages_to_i3">2. Sending messages to i3</h2>
\r
69 <div class="sectionbody">
\r
70 <div class="paragraph"><p>To send a message to i3, you have to format in the binary message format which
\r
71 i3 expects. This format specifies a magic string in the beginning to ensure
\r
72 the integrity of messages (to prevent follow-up errors). Following the magic
\r
73 string comes the length of the payload of the message as 32-bit integer, and
\r
74 the type of the message as 32-bit integer (the integers are not converted, so
\r
75 they are in native byte order).</p></div>
\r
76 <div class="paragraph"><p>The magic string currently is "i3-ipc" and will only be changed when a change
\r
77 in the IPC API is done which breaks compatibility (we hope that we don’t need
\r
78 to do that).</p></div>
\r
79 <div class="paragraph"><p>Currently implemented message types are the following:</p></div>
\r
80 <div class="dlist"><dl>
\r
81 <dt class="hdlist1">
\r
86 The payload of the message is a command for i3 (like the commands you
\r
87 can bind to keys in the configuration file) and will be executed
\r
88 directly after receiving it. There is no reply to this message.
\r
91 <dt class="hdlist1">
\r
96 Gets the current workspaces. The reply will be a JSON-encoded list of
\r
97 workspaces (see the reply section).
\r
100 <dt class="hdlist1">
\r
105 Subscribes your connection to certain events. See <a href="#events">[events]</a> for a
\r
106 description of this message and the concept of events.
\r
109 <dt class="hdlist1">
\r
114 Gets the current outputs. The reply will be a JSON-encoded list of outputs
\r
115 (see the reply section).
\r
118 <dt class="hdlist1">
\r
123 Gets the layout tree. i3 uses a tree as data structure which includes
\r
124 every container. The reply will be the JSON-encoded tree (see the reply
\r
129 <div class="paragraph"><p>So, a typical message could look like this:</p></div>
\r
130 <div class="listingblock">
\r
131 <div class="content">
\r
132 <pre><tt>"i3-ipc" <message length> <message type> <payload></tt></pre>
\r
134 <div class="paragraph"><p>Or, as a hexdump:</p></div>
\r
135 <div class="listingblock">
\r
136 <div class="content">
\r
137 <pre><tt>00000000 69 33 2d 69 70 63 04 00 00 00 00 00 00 00 65 78 |i3-ipc........ex|
\r
138 00000010 69 74 0a |it.|</tt></pre>
\r
140 <div class="paragraph"><p>To generate and send such a message, you could use the following code in Perl:</p></div>
\r
141 <div class="listingblock">
\r
142 <div class="content">
\r
143 <pre><tt>sub format_ipc_command {
\r
146 # Get the real byte count (vs. amount of characters)
\r
147 { use bytes; $len = length($msg); }
\r
148 return "i3-ipc" . pack("LL", $len, 0) . $msg;
\r
151 $sock->write(format_ipc_command("exit"));</tt></pre>
\r
155 <div class="sect1">
\r
156 <h2 id="_receiving_replies_from_i3">3. Receiving replies from i3</h2>
\r
157 <div class="sectionbody">
\r
158 <div class="paragraph"><p>Replies from i3 usually consist of a simple string (the length of the string
\r
159 is the message_length, so you can consider them length-prefixed) which in turn
\r
160 contain the JSON serialization of a data structure. For example, the
\r
161 GET_WORKSPACES message returns an array of workspaces (each workspace is a map
\r
162 with certain attributes).</p></div>
\r
163 <div class="sect2">
\r
164 <h3 id="_reply_format">3.1. Reply format</h3>
\r
165 <div class="paragraph"><p>The reply format is identical to the normal message format. There also is
\r
166 the magic string, then the message length, then the message type and the
\r
168 <div class="paragraph"><p>The following reply types are implemented:</p></div>
\r
169 <div class="dlist"><dl>
\r
170 <dt class="hdlist1">
\r
175 Confirmation/Error code for the COMMAND message.
\r
178 <dt class="hdlist1">
\r
183 Reply to the GET_WORKSPACES message.
\r
186 <dt class="hdlist1">
\r
191 Confirmation/Error code for the SUBSCRIBE message.
\r
194 <dt class="hdlist1">
\r
199 Reply to the GET_OUTPUTS message.
\r
202 <dt class="hdlist1">
\r
207 Reply to the GET_TREE message.
\r
212 <div class="sect2">
\r
213 <h3 id="_command_reply">3.2. COMMAND reply</h3>
\r
214 <div class="paragraph"><p>The reply consists of a single serialized map. At the moment, the only
\r
215 property is <tt>success (bool)</tt>, but this will be expanded in future versions.</p></div>
\r
216 <div class="paragraph"><p><strong>Example:</strong></p></div>
\r
217 <div class="listingblock">
\r
218 <div class="content">
\r
219 <pre><tt>{ "success": true }</tt></pre>
\r
222 <div class="sect2">
\r
223 <h3 id="_get_workspaces_reply">3.3. GET_WORKSPACES reply</h3>
\r
224 <div class="paragraph"><p>The reply consists of a serialized list of workspaces. Each workspace has the
\r
225 following properties:</p></div>
\r
226 <div class="dlist"><dl>
\r
227 <dt class="hdlist1">
\r
232 The logical number of the workspace. Corresponds to the command
\r
233 to switch to this workspace.
\r
236 <dt class="hdlist1">
\r
241 The name of this workspace (by default num+1), as changed by the
\r
242 user. Encoded in UTF-8.
\r
245 <dt class="hdlist1">
\r
250 Whether this workspace is currently visible on an output (multiple
\r
251 workspaces can be visible at the same time).
\r
254 <dt class="hdlist1">
\r
259 Whether this workspace currently has the focus (only one workspace
\r
260 can have the focus at the same time).
\r
263 <dt class="hdlist1">
\r
268 Whether a window on this workspace has the "urgent" flag set.
\r
271 <dt class="hdlist1">
\r
276 The rectangle of this workspace (equals the rect of the output it
\r
277 is on), consists of x, y, width, height.
\r
280 <dt class="hdlist1">
\r
285 The video output this workspace is on (LVDS1, VGA1, …).
\r
289 <div class="paragraph"><p><strong>Example:</strong></p></div>
\r
290 <div class="listingblock">
\r
291 <div class="content">
\r
324 <div class="sect2">
\r
325 <h3 id="_subscribe_reply">3.4. SUBSCRIBE reply</h3>
\r
326 <div class="paragraph"><p>The reply consists of a single serialized map. The only property is
\r
327 <tt>success (bool)</tt>, indicating whether the subscription was successful (the
\r
328 default) or whether a JSON parse error occurred.</p></div>
\r
329 <div class="paragraph"><p><strong>Example:</strong></p></div>
\r
330 <div class="listingblock">
\r
331 <div class="content">
\r
332 <pre><tt>{ "success": true }</tt></pre>
\r
335 <div class="sect2">
\r
336 <h3 id="_get_outputs_reply">3.5. GET_OUTPUTS reply</h3>
\r
337 <div class="paragraph"><p>The reply consists of a serialized list of outputs. Each output has the
\r
338 following properties:</p></div>
\r
339 <div class="dlist"><dl>
\r
340 <dt class="hdlist1">
\r
345 The name of this output (as seen in <tt>xrandr(1)</tt>). Encoded in UTF-8.
\r
348 <dt class="hdlist1">
\r
353 Whether this output is currently active (has a valid mode).
\r
356 <dt class="hdlist1">
\r
357 current_workspace (integer)
\r
361 The current workspace which is visible on this output. <tt>null</tt> if the
\r
362 output is not active.
\r
365 <dt class="hdlist1">
\r
370 The rectangle of this output (equals the rect of the output it
\r
371 is on), consists of x, y, width, height.
\r
375 <div class="paragraph"><p><strong>Example:</strong></p></div>
\r
376 <div class="listingblock">
\r
377 <div class="content">
\r
382 "current_workspace": 4,
\r
393 "current_workspace": 1,
\r
404 <div class="sect2">
\r
405 <h3 id="_get_tree_reply">3.6. GET_TREE reply</h3>
\r
406 <div class="paragraph"><p>The reply consists of a serialized tree. Each node in the tree (representing
\r
407 one container) has at least the properties listed below. While the nodes might
\r
408 have more properties, please do not use any properties which are not documented
\r
409 here. They are not yet finalized and will probably change!</p></div>
\r
410 <div class="dlist"><dl>
\r
411 <dt class="hdlist1">
\r
416 The internal ID (actually a C pointer value) of this container. Do not
\r
417 make any assumptions about it. You can use it to (re-)identify and
\r
418 address containers when talking to i3.
\r
421 <dt class="hdlist1">
\r
426 The internal name of this container. For all containers which are part
\r
427 of the tree structure down to the workspace contents, this is set to a
\r
428 nice human-readable name of the container.
\r
429 For all other containers, the content is not defined (yet).
\r
432 <dt class="hdlist1">
\r
437 Can be either "normal", "none" or "1pixel", dependending on the
\r
438 container’s border style.
\r
441 <dt class="hdlist1">
\r
446 Can be either "default", "stacked", "tabbed", "dockarea" or "output".
\r
447 Other values might be possible in the future, should we add new
\r
451 <dt class="hdlist1">
\r
452 orientation (string)
\r
456 Can be either "none" (for non-split containers), "horizontal" or
\r
460 <dt class="hdlist1">
\r
465 The percentage which this container takes in its parent. A value of
\r
466 <tt>null</tt> means that the percent property does not make sense for this
\r
467 container, for example for the root container.
\r
470 <dt class="hdlist1">
\r
475 The absolute display coordinates for this container. Display
\r
476 coordinates means that when you have two 1600x1200 monitors on a single
\r
477 X11 Display (the standard way), the coordinates of the first window on
\r
478 the second monitor are <tt>{ "x": 1600, "y": 0, "width": 1600, "height":
\r
482 <dt class="hdlist1">
\r
487 The coordinates of the <strong>actual client window</strong> inside its container.
\r
488 These coordinates are relative to the container and do not include the
\r
489 window decoration (which is actually rendered on the parent container).
\r
490 So, when using the <tt>default</tt> layout, you will have a 2 pixel border on
\r
491 each side, making the window_rect <tt>{ "x": 2, "y": 0, "width": 632,
\r
492 "height": 366 }</tt> (for example).
\r
495 <dt class="hdlist1">
\r
500 The original geometry the window specified when i3 mapped it. Used when
\r
501 switching a window to floating mode, for example.
\r
504 <dt class="hdlist1">
\r
509 Whether this container (window or workspace) has the urgency hint set.
\r
512 <dt class="hdlist1">
\r
517 Whether this container is currently focused.
\r
521 <div class="paragraph"><p>Please note that in the following example, I have left out some keys/values
\r
522 which are not relevant for the type of the node. Otherwise, the example would
\r
523 be by far too long (it already is quite long, despite showing only 1 window and
\r
524 one dock window).</p></div>
\r
525 <div class="paragraph"><p>It is useful to have an overview of the structure before taking a look at the
\r
526 JSON dump:</p></div>
\r
527 <div class="ulist"><ul>
\r
532 <div class="ulist"><ul>
\r
537 <div class="ulist"><ul>
\r
547 <div class="ulist"><ul>
\r
552 <div class="ulist"><ul>
\r
566 <div class="ulist"><ul>
\r
584 <div class="paragraph"><p><strong>Example:</strong></p></div>
\r
585 <div class="listingblock">
\r
586 <div class="content">
\r
601 "layout": "output",
\r
613 "layout": "dockarea",
\r
614 "orientation": "vertical",
\r
637 "orientation": "horizontal",
\r
644 "floating_nodes": [],
\r
650 "border": "normal",
\r
668 "name": "bottomdock",
\r
669 "layout": "dockarea",
\r
670 "orientation": "vertical",
\r
701 <div class="sect1">
\r
702 <h2 id="_events">4. Events</h2>
\r
703 <div class="sectionbody">
\r
704 <div class="paragraph" id="events"><p>To get informed when certain things happen in i3, clients can subscribe to
\r
705 events. Events consist of a name (like "workspace") and an event reply type
\r
706 (like I3_IPC_EVENT_WORKSPACE). The events sent by i3 are in the same format
\r
707 as replies to specific commands. However, the highest bit of the message type
\r
708 is set to 1 to indicate that this is an event reply instead of a normal reply.</p></div>
\r
709 <div class="paragraph"><p>Caveat: As soon as you subscribe to an event, it is not guaranteed any longer
\r
710 that the requests to i3 are processed in order. This means, the following
\r
711 situation can happen: You send a GET_WORKSPACES request but you receive a
\r
712 "workspace" event before receiving the reply to GET_WORKSPACES. If your
\r
713 program does not want to cope which such kinds of race conditions (an
\r
714 event based library may not have a problem here), I suggest you create a
\r
715 separate connection to receive events.</p></div>
\r
716 <div class="sect2">
\r
717 <h3 id="_subscribing_to_events">4.1. Subscribing to events</h3>
\r
718 <div class="paragraph"><p>By sending a message of type SUBSCRIBE with a JSON-encoded array as payload
\r
719 you can register to an event.</p></div>
\r
720 <div class="paragraph"><p><strong>Example:</strong></p></div>
\r
721 <div class="listingblock">
\r
722 <div class="content">
\r
723 <pre><tt>type: SUBSCRIBE
\r
724 payload: [ "workspace", "focus" ]</tt></pre>
\r
727 <div class="sect2">
\r
728 <h3 id="_available_events">4.2. Available events</h3>
\r
729 <div class="paragraph"><p>The numbers in parenthesis is the event type (keep in mind that you need to
\r
730 strip the highest bit first).</p></div>
\r
731 <div class="dlist"><dl>
\r
732 <dt class="hdlist1">
\r
737 Sent when the user switches to a different workspace, when a new
\r
738 workspace is initialized or when a workspace is removed (because the
\r
739 last client vanished).
\r
742 <dt class="hdlist1">
\r
747 Sent when RandR issues a change notification (of either screens,
\r
748 outputs, CRTCs or output properties).
\r
752 <div class="paragraph"><p><strong>Example:</strong></p></div>
\r
753 <div class="listingblock">
\r
754 <div class="content">
\r
755 <pre><tt># the appropriate 4 bytes read from the socket are stored in $input
\r
757 # unpack a 32-bit unsigned integer
\r
758 my $message_type = unpack("L", $input);
\r
760 # check if the highest bit is 1
\r
761 my $is_event = (($message_type >> 31) == 1);
\r
763 # use the other bits
\r
764 my $event_type = ($message_type & 0x7F);
\r
767 say "Received event of type $event_type";
\r
771 <div class="sect2">
\r
772 <h3 id="_workspace_event">4.3. workspace event</h3>
\r
773 <div class="paragraph"><p>This event consists of a single serialized map containing a property
\r
774 <tt>change (string)</tt> which indicates the type of the change ("focus", "init",
\r
775 "empty", "urgent").</p></div>
\r
776 <div class="paragraph"><p><strong>Example:</strong></p></div>
\r
777 <div class="listingblock">
\r
778 <div class="content">
\r
779 <pre><tt>{ "change": "focus" }</tt></pre>
\r
782 <div class="sect2">
\r
783 <h3 id="_output_event">4.4. output event</h3>
\r
784 <div class="paragraph"><p>This event consists of a single serialized map containing a property
\r
785 <tt>change (string)</tt> which indicates the type of the change (currently only
\r
786 "unspecified").</p></div>
\r
787 <div class="paragraph"><p><strong>Example:</strong></p></div>
\r
788 <div class="listingblock">
\r
789 <div class="content">
\r
790 <pre><tt>{ "change": "unspecified" }</tt></pre>
\r
795 <div class="sect1">
\r
796 <h2 id="_see_also">5. See also</h2>
\r
797 <div class="sectionbody">
\r
798 <div class="paragraph"><p>For some languages, libraries are available (so you don’t have to implement
\r
799 all this on your own). This list names some (if you wrote one, please let me
\r
801 <div class="dlist"><dl>
\r
802 <dt class="hdlist1">
\r
807 i3 includes a headerfile <tt>i3/ipc.h</tt> which provides you all constants.
\r
808 However, there is no library yet.
\r
811 <dt class="hdlist1">
\r
816 <a href="http://github.com/badboy/i3-ipc">http://github.com/badboy/i3-ipc</a>
\r
819 <dt class="hdlist1">
\r
824 <a href="http://search.cpan.org/search?query=AnyEvent::I3">http://search.cpan.org/search?query=AnyEvent::I3</a>
\r
827 <dt class="hdlist1">
\r
832 <a href="http://github.com/thepub/i3ipc">http://github.com/thepub/i3ipc</a>
\r
839 <div id="footnotes"><hr /></div>
\r
840 <div id="footer" lang="de">
\r
841 © 2009-2011 Michael Stapelberg, <a href="/impress.html">Impressum</a>
\r