]> git.sur5r.net Git - i3/i3.github.io/blob - docs/ipc.html
88b13b7d331aa0684a1998c4feddc2e169896a33
[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 <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
12 /*<![CDATA[*/\r
13 window.onload = function(){asciidoc.footnotes(); asciidoc.toc(2);}\r
14 /*]]>*/\r
15 </script>\r
16 <script type="text/javascript" src="/js/asciidoc-xhtml11.js"></script>\r
17 </head>\r
18 <body class="article">\r
19 \r
20         <div id="main">\r
21             <a href="/"><h1 id="title">i3 - improved tiling WM</h1></a>\r
22                         <ul id="nav">\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
27                         </ul>\r
28         <br style="clear: both">\r
29 <div id="content">\r
30 <div id="header">\r
31 <h1>IPC interface (interprocess communication)</h1>\r
32 <span id="author">Michael Stapelberg</span><br />\r
33 <span id="email"><tt>&lt;<a href="mailto:michael+i3@stapelberg.de">michael+i3@stapelberg.de</a>&gt;</tt></span><br />\r
34 <span id="revdate">March 2010</span>\r
35 <div id="toc">
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>
38 </div>\r
39 </div>\r
40 <div id="preamble">\r
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
51 </div>\r
52 </div>\r
53 <div class="sect1">\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-&gt;new(Peer =&gt; '~/.i3/ipc.sock');</tt></pre>\r
62 </div></div>\r
63 </div>\r
64 </div>\r
65 <div class="sect1">\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
80 COMMAND (0)\r
81 </dt>\r
82 <dd>\r
83 <p>\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
87 </p>\r
88 </dd>\r
89 <dt class="hdlist1">\r
90 GET_WORKSPACES (1)\r
91 </dt>\r
92 <dd>\r
93 <p>\r
94         Gets the current workspaces. The reply will be a JSON-encoded list of\r
95         workspaces (see the reply section).\r
96 </p>\r
97 </dd>\r
98 <dt class="hdlist1">\r
99 SUBSCRIBE (2)\r
100 </dt>\r
101 <dd>\r
102 <p>\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
105 </p>\r
106 </dd>\r
107 <dt class="hdlist1">\r
108 GET_OUTPUTS (3)\r
109 </dt>\r
110 <dd>\r
111 <p>\r
112         Gets the current outputs. The reply will be a JSON-encoded list of outputs\r
113         (see the reply section).\r
114 </p>\r
115 </dd>\r
116 </dl></div>\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" &lt;message length&gt; &lt;message type&gt; &lt;payload&gt;</tt></pre>\r
121 </div></div>\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
127 </div></div>\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
132     my ($msg) = @_;\r
133     my $len;\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
137 }\r
138 \r
139 $sock-&gt;write(format_ipc_command("exit"));</tt></pre>\r
140 </div></div>\r
141 </div>\r
142 </div>\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
155 payload.</p></div>\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
159 COMMAND (0)\r
160 </dt>\r
161 <dd>\r
162 <p>\r
163         Confirmation/Error code for the COMMAND message.\r
164 </p>\r
165 </dd>\r
166 <dt class="hdlist1">\r
167 GET_WORKSPACES (1)\r
168 </dt>\r
169 <dd>\r
170 <p>\r
171         Reply to the GET_WORKSPACES message.\r
172 </p>\r
173 </dd>\r
174 <dt class="hdlist1">\r
175 SUBSCRIBE (2)\r
176 </dt>\r
177 <dd>\r
178 <p>\r
179         Confirmation/Error code for the SUBSCRIBE message.\r
180 </p>\r
181 </dd>\r
182 <dt class="hdlist1">\r
183 GET_OUTPUTS (3)\r
184 </dt>\r
185 <dd>\r
186 <p>\r
187         Reply to the GET_OUTPUTS message.\r
188 </p>\r
189 </dd>\r
190 </dl></div>\r
191 </div>\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
200 </div></div>\r
201 </div>\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
208 num (integer)\r
209 </dt>\r
210 <dd>\r
211 <p>\r
212         The logical number of the workspace. Corresponds to the command\r
213         to switch to this workspace.\r
214 </p>\r
215 </dd>\r
216 <dt class="hdlist1">\r
217 name (string)\r
218 </dt>\r
219 <dd>\r
220 <p>\r
221         The name of this workspace (by default num+1), as changed by the\r
222         user. Encoded in UTF-8.\r
223 </p>\r
224 </dd>\r
225 <dt class="hdlist1">\r
226 visible (boolean)\r
227 </dt>\r
228 <dd>\r
229 <p>\r
230         Whether this workspace is currently visible on an output (multiple\r
231         workspaces can be visible at the same time).\r
232 </p>\r
233 </dd>\r
234 <dt class="hdlist1">\r
235 focused (boolean)\r
236 </dt>\r
237 <dd>\r
238 <p>\r
239         Whether this workspace currently has the focus (only one workspace\r
240         can have the focus at the same time).\r
241 </p>\r
242 </dd>\r
243 <dt class="hdlist1">\r
244 urgent (boolean)\r
245 </dt>\r
246 <dd>\r
247 <p>\r
248         Whether a window on this workspace has the "urgent" flag set.\r
249 </p>\r
250 </dd>\r
251 <dt class="hdlist1">\r
252 rect (map)\r
253 </dt>\r
254 <dd>\r
255 <p>\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
258 </p>\r
259 </dd>\r
260 <dt class="hdlist1">\r
261 output (string)\r
262 </dt>\r
263 <dd>\r
264 <p>\r
265         The video output this workspace is on (LVDS1, VGA1, …).\r
266 </p>\r
267 </dd>\r
268 </dl></div>\r
269 <div class="paragraph"><p><strong>Example:</strong></p></div>\r
270 <div class="listingblock">\r
271 <div class="content">\r
272 <pre><tt>[\r
273  {\r
274   "num": 0,\r
275   "name": "1",\r
276   "visible": true,\r
277   "focused": true,\r
278   "urgent": false,\r
279   "rect": {\r
280    "x": 0,\r
281    "y": 0,\r
282    "width": 1280,\r
283    "height": 800\r
284   },\r
285   "output": "LVDS1"\r
286  },\r
287  {\r
288   "num": 1,\r
289   "name": "2",\r
290   "visible": false,\r
291   "focused": false,\r
292   "urgent": false,\r
293   "rect": {\r
294    "x": 0,\r
295    "y": 0,\r
296    "width": 1280,\r
297    "height": 800\r
298   },\r
299   "output": "LVDS1"\r
300  }\r
301 ]</tt></pre>\r
302 </div></div>\r
303 </div>\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
313 </div></div>\r
314 </div>\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
321 name (string)\r
322 </dt>\r
323 <dd>\r
324 <p>\r
325         The name of this output (as seen in <tt>xrandr(1)</tt>). Encoded in UTF-8.\r
326 </p>\r
327 </dd>\r
328 <dt class="hdlist1">\r
329 active (boolean)\r
330 </dt>\r
331 <dd>\r
332 <p>\r
333         Whether this output is currently active (has a valid mode).\r
334 </p>\r
335 </dd>\r
336 <dt class="hdlist1">\r
337 current_workspace (integer)\r
338 </dt>\r
339 <dd>\r
340 <p>\r
341         The current workspace which is visible on this output. <tt>null</tt> if the\r
342         output is not active.\r
343 </p>\r
344 </dd>\r
345 <dt class="hdlist1">\r
346 rect (map)\r
347 </dt>\r
348 <dd>\r
349 <p>\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
352 </p>\r
353 </dd>\r
354 </dl></div>\r
355 <div class="paragraph"><p><strong>Example:</strong></p></div>\r
356 <div class="listingblock">\r
357 <div class="content">\r
358 <pre><tt>[\r
359  {\r
360   "name": "LVDS1",\r
361   "active": true,\r
362   "current_workspace": 4,\r
363   "rect": {\r
364    "x": 0,\r
365    "y": 0,\r
366    "width": 1280,\r
367    "height": 800\r
368   }\r
369  },\r
370  {\r
371   "name": "VGA1",\r
372   "active": true,\r
373   "current_workspace": 1,\r
374   "rect": {\r
375    "x": 1280,\r
376    "y": 0,\r
377    "width": 1280,\r
378    "height": 1024\r
379   },\r
380  }\r
381 ]</tt></pre>\r
382 </div></div>\r
383 </div>\r
384 </div>\r
385 </div>\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
410 </div></div>\r
411 </div>\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
418 workspace (0)\r
419 </dt>\r
420 <dd>\r
421 <p>\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
425 </p>\r
426 </dd>\r
427 <dt class="hdlist1">\r
428 output (1)\r
429 </dt>\r
430 <dd>\r
431 <p>\r
432         Sent when RandR issues a change notification (of either screens,\r
433         outputs, CRTCs or output properties).\r
434 </p>\r
435 </dd>\r
436 </dl></div>\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
441 \r
442 # unpack a 32-bit unsigned integer\r
443 my $message_type = unpack("L", $input);\r
444 \r
445 # check if the highest bit is 1\r
446 my $is_event = (($message_type &gt;&gt; 31) == 1);\r
447 \r
448 # use the other bits\r
449 my $event_type = ($message_type &amp; 0x7F);\r
450 \r
451 if ($is_event) {\r
452   say "Received event of type $event_type";\r
453 }</tt></pre>\r
454 </div></div>\r
455 </div>\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
465 </div></div>\r
466 </div>\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
476 </div></div>\r
477 </div>\r
478 </div>\r
479 </div>\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
485 know):</p></div>\r
486 <div class="dlist"><dl>\r
487 <dt class="hdlist1">\r
488 C\r
489 </dt>\r
490 <dd>\r
491 <p>\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
494 </p>\r
495 </dd>\r
496 <dt class="hdlist1">\r
497 Ruby\r
498 </dt>\r
499 <dd>\r
500 <p>\r
501         <a href="http://github.com/badboy/i3-ipc">http://github.com/badboy/i3-ipc</a>\r
502 </p>\r
503 </dd>\r
504 <dt class="hdlist1">\r
505 Perl\r
506 </dt>\r
507 <dd>\r
508 <p>\r
509         <a href="http://search.cpan.org/search?query=AnyEvent::I3">http://search.cpan.org/search?query=AnyEvent::I3</a>\r
510 </p>\r
511 </dd>\r
512 <dt class="hdlist1">\r
513 Python\r
514 </dt>\r
515 <dd>\r
516 <p>\r
517         <a href="http://github.com/thepub/i3ipc">http://github.com/thepub/i3ipc</a>\r
518 </p>\r
519 </dd>\r
520 </dl></div>\r
521 </div>\r
522 </div>\r
523 </div>\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
527 </div>\r
528 </body>\r
529 </html>\r