]> git.sur5r.net Git - i3/i3.github.io/blob - docs/ipc.html
add docs (with custom stylesheet)
[i3/i3.github.io] / docs / ipc.html
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
4 <head>\r
5 <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8" />\r
6 <meta name="generator" content="AsciiDoc 8.6.4" />\r
7 <title>i3: IPC interface (interprocess communication)</title>\r
8 <link rel="stylesheet" href="/css/style.css" type="text/css" />\r
9 <link rel="stylesheet" href="/css/xhtml11.css" type="text/css" />\r
10 <script type="text/javascript">\r
11 /*<![CDATA[*/\r
12 window.onload = function(){asciidoc.footnotes(); asciidoc.toc(2);}\r
13 /*]]>*/\r
14 </script>\r
15 <script type="text/javascript" src="/js/asciidoc-xhtml11.js"></script>\r
16 </head>\r
17 <body class="article">\r
18 \r
19         <div id="main">\r
20             <h1 id="title">i3 - improved tiling WM</h1>\r
21                         <ul id="nav">\r
22                                 <li><a style="border-bottom: 2px solid #fff" href="/docs">Docs</a></li>\r
23                                 <li><a href="/screenshots">Screens</a></li>\r
24                                 <li><a href="/contact">Contact</a></li>\r
25                                 <li><a href="http://bugs.i3wm.org/">Bugs</a></li>\r
26                         </ul>\r
27         <br style="clear: both">\r
28 <div id="content">\r
29 <div id="header">\r
30 <h1>IPC interface (interprocess communication)</h1>\r
31 <span id="author">Michael Stapelberg</span><br />\r
32 <span id="email"><tt>&lt;<a href="mailto:michael+i3@stapelberg.de">michael+i3@stapelberg.de</a>&gt;</tt></span><br />\r
33 <span id="revdate">March 2010</span>\r
34 <div id="toc">
35   <div id="toctitle">Table of Contents</div>
36   <noscript><p><b>JavaScript must be enabled in your browser to display the table of contents.</b></p></noscript>
37 </div>\r
38 </div>\r
39 <div id="preamble">\r
40 <div class="sectionbody">\r
41 <div class="paragraph"><p>This document describes how to interface with i3 from a separate process. This\r
42 is useful for example to remote-control i3 (to write test cases for example) or\r
43 to get various information like the current workspaces to implement an external\r
44 workspace bar.</p></div>\r
45 <div class="paragraph"><p>The method of choice for IPC in our case is a unix socket because it has very\r
46 little overhead on both sides and is usually available without headaches in\r
47 most languages. In the default configuration file, no ipc-socket path is\r
48 specified and thus no socket is created. The standard path (which <tt>i3-msg</tt> and\r
49 <tt>i3-input</tt> use) is <tt>~/.i3/ipc.sock</tt>.</p></div>\r
50 </div>\r
51 </div>\r
52 <div class="sect1">\r
53 <h2 id="_establishing_a_connection">1. Establishing a connection</h2>\r
54 <div class="sectionbody">\r
55 <div class="paragraph"><p>To establish a connection, simply open the IPC socket. The following code\r
56 snippet illustrates this in Perl:</p></div>\r
57 <div class="listingblock">\r
58 <div class="content">\r
59 <pre><tt>use IO::Socket::UNIX;\r
60 my $sock = IO::Socket::UNIX-&gt;new(Peer =&gt; '~/.i3/ipc.sock');</tt></pre>\r
61 </div></div>\r
62 </div>\r
63 </div>\r
64 <div class="sect1">\r
65 <h2 id="_sending_messages_to_i3">2. Sending messages to i3</h2>\r
66 <div class="sectionbody">\r
67 <div class="paragraph"><p>To send a message to i3, you have to format in the binary message format which\r
68 i3 expects. This format specifies a magic string in the beginning to ensure\r
69 the integrity of messages (to prevent follow-up errors). Following the magic\r
70 string comes the length of the payload of the message as 32-bit integer, and\r
71 the type of the message as 32-bit integer (the integers are not converted, so\r
72 they are in native byte order).</p></div>\r
73 <div class="paragraph"><p>The magic string currently is "i3-ipc" and will only be changed when a change\r
74 in the IPC API is done which breaks compatibility (we hope that we don’t need\r
75 to do that).</p></div>\r
76 <div class="paragraph"><p>Currently implemented message types are the following:</p></div>\r
77 <div class="dlist"><dl>\r
78 <dt class="hdlist1">\r
79 COMMAND (0)\r
80 </dt>\r
81 <dd>\r
82 <p>\r
83         The payload of the message is a command for i3 (like the commands you\r
84         can bind to keys in the configuration file) and will be executed\r
85         directly after receiving it. There is no reply to this message.\r
86 </p>\r
87 </dd>\r
88 <dt class="hdlist1">\r
89 GET_WORKSPACES (1)\r
90 </dt>\r
91 <dd>\r
92 <p>\r
93         Gets the current workspaces. The reply will be a JSON-encoded list of\r
94         workspaces (see the reply section).\r
95 </p>\r
96 </dd>\r
97 <dt class="hdlist1">\r
98 SUBSCRIBE (2)\r
99 </dt>\r
100 <dd>\r
101 <p>\r
102         Subscribes your connection to certain events. See <a href="#events">[events]</a> for a\r
103         description of this message and the concept of events.\r
104 </p>\r
105 </dd>\r
106 <dt class="hdlist1">\r
107 GET_OUTPUTS (3)\r
108 </dt>\r
109 <dd>\r
110 <p>\r
111         Gets the current outputs. The reply will be a JSON-encoded list of outputs\r
112         (see the reply section).\r
113 </p>\r
114 </dd>\r
115 </dl></div>\r
116 <div class="paragraph"><p>So, a typical message could look like this:</p></div>\r
117 <div class="listingblock">\r
118 <div class="content">\r
119 <pre><tt>"i3-ipc" &lt;message length&gt; &lt;message type&gt; &lt;payload&gt;</tt></pre>\r
120 </div></div>\r
121 <div class="paragraph"><p>Or, as a hexdump:</p></div>\r
122 <div class="listingblock">\r
123 <div class="content">\r
124 <pre><tt>00000000  69 33 2d 69 70 63 04 00  00 00 00 00 00 00 65 78  |i3-ipc........ex|\r
125 00000010  69 74 0a                                          |it.|</tt></pre>\r
126 </div></div>\r
127 <div class="paragraph"><p>To generate and send such a message, you could use the following code in Perl:</p></div>\r
128 <div class="listingblock">\r
129 <div class="content">\r
130 <pre><tt>sub format_ipc_command {\r
131     my ($msg) = @_;\r
132     my $len;\r
133     # Get the real byte count (vs. amount of characters)\r
134     { use bytes; $len = length($msg); }\r
135     return "i3-ipc" . pack("LL", $len, 0) . $msg;\r
136 }\r
137 \r
138 $sock-&gt;write(format_ipc_command("exit"));</tt></pre>\r
139 </div></div>\r
140 </div>\r
141 </div>\r
142 <div class="sect1">\r
143 <h2 id="_receiving_replies_from_i3">3. Receiving replies from i3</h2>\r
144 <div class="sectionbody">\r
145 <div class="paragraph"><p>Replies from i3 usually consist of a simple string (the length of the string\r
146 is the message_length, so you can consider them length-prefixed) which in turn\r
147 contain the JSON serialization of a data structure. For example, the\r
148 GET_WORKSPACES message returns an array of workspaces (each workspace is a map\r
149 with certain attributes).</p></div>\r
150 <div class="sect2">\r
151 <h3 id="_reply_format">3.1. Reply format</h3>\r
152 <div class="paragraph"><p>The reply format is identical to the normal message format. There also is\r
153 the magic string, then the message length, then the message type and the\r
154 payload.</p></div>\r
155 <div class="paragraph"><p>The following reply types are implemented:</p></div>\r
156 <div class="dlist"><dl>\r
157 <dt class="hdlist1">\r
158 COMMAND (0)\r
159 </dt>\r
160 <dd>\r
161 <p>\r
162         Confirmation/Error code for the COMMAND message.\r
163 </p>\r
164 </dd>\r
165 <dt class="hdlist1">\r
166 GET_WORKSPACES (1)\r
167 </dt>\r
168 <dd>\r
169 <p>\r
170         Reply to the GET_WORKSPACES message.\r
171 </p>\r
172 </dd>\r
173 <dt class="hdlist1">\r
174 SUBSCRIBE (2)\r
175 </dt>\r
176 <dd>\r
177 <p>\r
178         Confirmation/Error code for the SUBSCRIBE message.\r
179 </p>\r
180 </dd>\r
181 <dt class="hdlist1">\r
182 GET_OUTPUTS (3)\r
183 </dt>\r
184 <dd>\r
185 <p>\r
186         Reply to the GET_OUTPUTS message.\r
187 </p>\r
188 </dd>\r
189 </dl></div>\r
190 </div>\r
191 <div class="sect2">\r
192 <h3 id="_command_reply">3.2. COMMAND reply</h3>\r
193 <div class="paragraph"><p>The reply consists of a single serialized map. At the moment, the only\r
194 property is <tt>success (bool)</tt>, but this will be expanded in future versions.</p></div>\r
195 <div class="paragraph"><p><strong>Example:</strong></p></div>\r
196 <div class="listingblock">\r
197 <div class="content">\r
198 <pre><tt>{ "success": true }</tt></pre>\r
199 </div></div>\r
200 </div>\r
201 <div class="sect2">\r
202 <h3 id="_get_workspaces_reply">3.3. GET_WORKSPACES reply</h3>\r
203 <div class="paragraph"><p>The reply consists of a serialized list of workspaces. Each workspace has the\r
204 following properties:</p></div>\r
205 <div class="dlist"><dl>\r
206 <dt class="hdlist1">\r
207 num (integer)\r
208 </dt>\r
209 <dd>\r
210 <p>\r
211         The logical number of the workspace. Corresponds to the command\r
212         to switch to this workspace.\r
213 </p>\r
214 </dd>\r
215 <dt class="hdlist1">\r
216 name (string)\r
217 </dt>\r
218 <dd>\r
219 <p>\r
220         The name of this workspace (by default num+1), as changed by the\r
221         user. Encoded in UTF-8.\r
222 </p>\r
223 </dd>\r
224 <dt class="hdlist1">\r
225 visible (boolean)\r
226 </dt>\r
227 <dd>\r
228 <p>\r
229         Whether this workspace is currently visible on an output (multiple\r
230         workspaces can be visible at the same time).\r
231 </p>\r
232 </dd>\r
233 <dt class="hdlist1">\r
234 focused (boolean)\r
235 </dt>\r
236 <dd>\r
237 <p>\r
238         Whether this workspace currently has the focus (only one workspace\r
239         can have the focus at the same time).\r
240 </p>\r
241 </dd>\r
242 <dt class="hdlist1">\r
243 urgent (boolean)\r
244 </dt>\r
245 <dd>\r
246 <p>\r
247         Whether a window on this workspace has the "urgent" flag set.\r
248 </p>\r
249 </dd>\r
250 <dt class="hdlist1">\r
251 rect (map)\r
252 </dt>\r
253 <dd>\r
254 <p>\r
255         The rectangle of this workspace (equals the rect of the output it\r
256         is on), consists of x, y, width, height.\r
257 </p>\r
258 </dd>\r
259 <dt class="hdlist1">\r
260 output (string)\r
261 </dt>\r
262 <dd>\r
263 <p>\r
264         The video output this workspace is on (LVDS1, VGA1, …).\r
265 </p>\r
266 </dd>\r
267 </dl></div>\r
268 <div class="paragraph"><p><strong>Example:</strong></p></div>\r
269 <div class="listingblock">\r
270 <div class="content">\r
271 <pre><tt>[\r
272  {\r
273   "num": 0,\r
274   "name": "1",\r
275   "visible": true,\r
276   "focused": true,\r
277   "urgent": false,\r
278   "rect": {\r
279    "x": 0,\r
280    "y": 0,\r
281    "width": 1280,\r
282    "height": 800\r
283   },\r
284   "output": "LVDS1"\r
285  },\r
286  {\r
287   "num": 1,\r
288   "name": "2",\r
289   "visible": false,\r
290   "focused": false,\r
291   "urgent": false,\r
292   "rect": {\r
293    "x": 0,\r
294    "y": 0,\r
295    "width": 1280,\r
296    "height": 800\r
297   },\r
298   "output": "LVDS1"\r
299  }\r
300 ]</tt></pre>\r
301 </div></div>\r
302 </div>\r
303 <div class="sect2">\r
304 <h3 id="_subscribe_reply">3.4. SUBSCRIBE reply</h3>\r
305 <div class="paragraph"><p>The reply consists of a single serialized map. The only property is\r
306 <tt>success (bool)</tt>, indicating whether the subscription was successful (the\r
307 default) or whether a JSON parse error occurred.</p></div>\r
308 <div class="paragraph"><p><strong>Example:</strong></p></div>\r
309 <div class="listingblock">\r
310 <div class="content">\r
311 <pre><tt>{ "success": true }</tt></pre>\r
312 </div></div>\r
313 </div>\r
314 <div class="sect2">\r
315 <h3 id="_get_outputs_reply">3.5. GET_OUTPUTS reply</h3>\r
316 <div class="paragraph"><p>The reply consists of a serialized list of outputs. Each output has the\r
317 following properties:</p></div>\r
318 <div class="dlist"><dl>\r
319 <dt class="hdlist1">\r
320 name (string)\r
321 </dt>\r
322 <dd>\r
323 <p>\r
324         The name of this output (as seen in <tt>xrandr(1)</tt>). Encoded in UTF-8.\r
325 </p>\r
326 </dd>\r
327 <dt class="hdlist1">\r
328 active (boolean)\r
329 </dt>\r
330 <dd>\r
331 <p>\r
332         Whether this output is currently active (has a valid mode).\r
333 </p>\r
334 </dd>\r
335 <dt class="hdlist1">\r
336 current_workspace (integer)\r
337 </dt>\r
338 <dd>\r
339 <p>\r
340         The current workspace which is visible on this output. <tt>null</tt> if the\r
341         output is not active.\r
342 </p>\r
343 </dd>\r
344 <dt class="hdlist1">\r
345 rect (map)\r
346 </dt>\r
347 <dd>\r
348 <p>\r
349         The rectangle of this output (equals the rect of the output it\r
350         is on), consists of x, y, width, height.\r
351 </p>\r
352 </dd>\r
353 </dl></div>\r
354 <div class="paragraph"><p><strong>Example:</strong></p></div>\r
355 <div class="listingblock">\r
356 <div class="content">\r
357 <pre><tt>[\r
358  {\r
359   "name": "LVDS1",\r
360   "active": true,\r
361   "current_workspace": 4,\r
362   "rect": {\r
363    "x": 0,\r
364    "y": 0,\r
365    "width": 1280,\r
366    "height": 800\r
367   }\r
368  },\r
369  {\r
370   "name": "VGA1",\r
371   "active": true,\r
372   "current_workspace": 1,\r
373   "rect": {\r
374    "x": 1280,\r
375    "y": 0,\r
376    "width": 1280,\r
377    "height": 1024\r
378   },\r
379  }\r
380 ]</tt></pre>\r
381 </div></div>\r
382 </div>\r
383 </div>\r
384 </div>\r
385 <div class="sect1">\r
386 <h2 id="_events">4. Events</h2>\r
387 <div class="sectionbody">\r
388 <div class="paragraph" id="events"><p>To get informed when certain things happen in i3, clients can subscribe to\r
389 events. Events consist of a name (like "workspace") and an event reply type\r
390 (like I3_IPC_EVENT_WORKSPACE). The events sent by i3 are in the same format\r
391 as replies to specific commands. However, the highest bit of the message type\r
392 is set to 1 to indicate that this is an event reply instead of a normal reply.</p></div>\r
393 <div class="paragraph"><p>Caveat: As soon as you subscribe to an event, it is not guaranteed any longer\r
394 that the requests to i3 are processed in order. This means, the following\r
395 situation can happen: You send a GET_WORKSPACES request but you receive a\r
396 "workspace" event before receiving the reply to GET_WORKSPACES. If your\r
397 program does not want to cope which such kinds of race conditions (an\r
398 event based library may not have a problem here), I suggest you create a\r
399 separate connection to receive events.</p></div>\r
400 <div class="sect2">\r
401 <h3 id="_subscribing_to_events">4.1. Subscribing to events</h3>\r
402 <div class="paragraph"><p>By sending a message of type SUBSCRIBE with a JSON-encoded array as payload\r
403 you can register to an event.</p></div>\r
404 <div class="paragraph"><p><strong>Example:</strong></p></div>\r
405 <div class="listingblock">\r
406 <div class="content">\r
407 <pre><tt>type: SUBSCRIBE\r
408 payload: [ "workspace", "focus" ]</tt></pre>\r
409 </div></div>\r
410 </div>\r
411 <div class="sect2">\r
412 <h3 id="_available_events">4.2. Available events</h3>\r
413 <div class="paragraph"><p>The numbers in parenthesis is the event type (keep in mind that you need to\r
414 strip the highest bit first).</p></div>\r
415 <div class="dlist"><dl>\r
416 <dt class="hdlist1">\r
417 workspace (0)\r
418 </dt>\r
419 <dd>\r
420 <p>\r
421         Sent when the user switches to a different workspace, when a new\r
422         workspace is initialized or when a workspace is removed (because the\r
423         last client vanished).\r
424 </p>\r
425 </dd>\r
426 <dt class="hdlist1">\r
427 output (1)\r
428 </dt>\r
429 <dd>\r
430 <p>\r
431         Sent when RandR issues a change notification (of either screens,\r
432         outputs, CRTCs or output properties).\r
433 </p>\r
434 </dd>\r
435 </dl></div>\r
436 <div class="paragraph"><p><strong>Example:</strong></p></div>\r
437 <div class="listingblock">\r
438 <div class="content">\r
439 <pre><tt># the appropriate 4 bytes read from the socket are stored in $input\r
440 \r
441 # unpack a 32-bit unsigned integer\r
442 my $message_type = unpack("L", $input);\r
443 \r
444 # check if the highest bit is 1\r
445 my $is_event = (($message_type &gt;&gt; 31) == 1);\r
446 \r
447 # use the other bits\r
448 my $event_type = ($message_type &amp; 0x7F);\r
449 \r
450 if ($is_event) {\r
451   say "Received event of type $event_type";\r
452 }</tt></pre>\r
453 </div></div>\r
454 </div>\r
455 <div class="sect2">\r
456 <h3 id="_workspace_event">4.3. workspace event</h3>\r
457 <div class="paragraph"><p>This event consists of a single serialized map containing a property\r
458 <tt>change (string)</tt> which indicates the type of the change ("focus", "init",\r
459 "empty", "urgent").</p></div>\r
460 <div class="paragraph"><p><strong>Example:</strong></p></div>\r
461 <div class="listingblock">\r
462 <div class="content">\r
463 <pre><tt>{ "change": "focus" }</tt></pre>\r
464 </div></div>\r
465 </div>\r
466 <div class="sect2">\r
467 <h3 id="_output_event">4.4. output event</h3>\r
468 <div class="paragraph"><p>This event consists of a single serialized map containing a property\r
469 <tt>change (string)</tt> which indicates the type of the change (currently only\r
470 "unspecified").</p></div>\r
471 <div class="paragraph"><p><strong>Example:</strong></p></div>\r
472 <div class="listingblock">\r
473 <div class="content">\r
474 <pre><tt>{ "change": "unspecified" }</tt></pre>\r
475 </div></div>\r
476 </div>\r
477 </div>\r
478 </div>\r
479 <div class="sect1">\r
480 <h2 id="_see_also">5. See also</h2>\r
481 <div class="sectionbody">\r
482 <div class="paragraph"><p>For some languages, libraries are available (so you don’t have to implement\r
483 all this on your own). This list names some (if you wrote one, please let me\r
484 know):</p></div>\r
485 <div class="dlist"><dl>\r
486 <dt class="hdlist1">\r
487 C\r
488 </dt>\r
489 <dd>\r
490 <p>\r
491         i3 includes a headerfile <tt>i3/ipc.h</tt> which provides you all constants.\r
492         However, there is no library yet.\r
493 </p>\r
494 </dd>\r
495 <dt class="hdlist1">\r
496 Ruby\r
497 </dt>\r
498 <dd>\r
499 <p>\r
500         <a href="http://github.com/badboy/i3-ipc">http://github.com/badboy/i3-ipc</a>\r
501 </p>\r
502 </dd>\r
503 <dt class="hdlist1">\r
504 Perl\r
505 </dt>\r
506 <dd>\r
507 <p>\r
508         <a href="http://search.cpan.org/search?query=AnyEvent::I3">http://search.cpan.org/search?query=AnyEvent::I3</a>\r
509 </p>\r
510 </dd>\r
511 <dt class="hdlist1">\r
512 Python\r
513 </dt>\r
514 <dd>\r
515 <p>\r
516         <a href="http://github.com/thepub/i3ipc">http://github.com/thepub/i3ipc</a>\r
517 </p>\r
518 </dd>\r
519 </dl></div>\r
520 </div>\r
521 </div>\r
522 </div>\r
523 <div id="footnotes"><hr /></div>\r
524 <div id="footer">\r
525 <div id="footer-text">\r
526 Last updated 2011-07-22 20:28:12 CEST\r
527 </div>\r
528 </div>\r
529 </body>\r
530 </html>\r