]> git.sur5r.net Git - i3/i3.github.io/blob - docs/ipc.html
3a3cb87a47051a5f96b48a47ff39f044040d04a9
[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, 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
53 </div>\r
54 </div>\r
55 <div class="sect1">\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-&gt;new(Peer =&gt; '/tmp/i3-ipc.sock');</tt></pre>\r
64 </div></div>\r
65 </div>\r
66 </div>\r
67 <div class="sect1">\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
82 COMMAND (0)\r
83 </dt>\r
84 <dd>\r
85 <p>\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
89 </p>\r
90 </dd>\r
91 <dt class="hdlist1">\r
92 GET_WORKSPACES (1)\r
93 </dt>\r
94 <dd>\r
95 <p>\r
96         Gets the current workspaces. The reply will be a JSON-encoded list of\r
97         workspaces (see the reply section).\r
98 </p>\r
99 </dd>\r
100 <dt class="hdlist1">\r
101 SUBSCRIBE (2)\r
102 </dt>\r
103 <dd>\r
104 <p>\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
107 </p>\r
108 </dd>\r
109 <dt class="hdlist1">\r
110 GET_OUTPUTS (3)\r
111 </dt>\r
112 <dd>\r
113 <p>\r
114         Gets the current outputs. The reply will be a JSON-encoded list of outputs\r
115         (see the reply section).\r
116 </p>\r
117 </dd>\r
118 <dt class="hdlist1">\r
119 GET_TREE (4)\r
120 </dt>\r
121 <dd>\r
122 <p>\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
125         section).\r
126 </p>\r
127 </dd>\r
128 </dl></div>\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" &lt;message length&gt; &lt;message type&gt; &lt;payload&gt;</tt></pre>\r
133 </div></div>\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
139 </div></div>\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
144     my ($msg) = @_;\r
145     my $len;\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
149 }\r
150 \r
151 $sock-&gt;write(format_ipc_command("exit"));</tt></pre>\r
152 </div></div>\r
153 </div>\r
154 </div>\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
167 payload.</p></div>\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
171 COMMAND (0)\r
172 </dt>\r
173 <dd>\r
174 <p>\r
175         Confirmation/Error code for the COMMAND message.\r
176 </p>\r
177 </dd>\r
178 <dt class="hdlist1">\r
179 GET_WORKSPACES (1)\r
180 </dt>\r
181 <dd>\r
182 <p>\r
183         Reply to the GET_WORKSPACES message.\r
184 </p>\r
185 </dd>\r
186 <dt class="hdlist1">\r
187 SUBSCRIBE (2)\r
188 </dt>\r
189 <dd>\r
190 <p>\r
191         Confirmation/Error code for the SUBSCRIBE message.\r
192 </p>\r
193 </dd>\r
194 <dt class="hdlist1">\r
195 GET_OUTPUTS (3)\r
196 </dt>\r
197 <dd>\r
198 <p>\r
199         Reply to the GET_OUTPUTS message.\r
200 </p>\r
201 </dd>\r
202 <dt class="hdlist1">\r
203 GET_TREE (4)\r
204 </dt>\r
205 <dd>\r
206 <p>\r
207         Reply to the GET_TREE message.\r
208 </p>\r
209 </dd>\r
210 </dl></div>\r
211 </div>\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
220 </div></div>\r
221 </div>\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
228 num (integer)\r
229 </dt>\r
230 <dd>\r
231 <p>\r
232         The logical number of the workspace. Corresponds to the command\r
233         to switch to this workspace.\r
234 </p>\r
235 </dd>\r
236 <dt class="hdlist1">\r
237 name (string)\r
238 </dt>\r
239 <dd>\r
240 <p>\r
241         The name of this workspace (by default num+1), as changed by the\r
242         user. Encoded in UTF-8.\r
243 </p>\r
244 </dd>\r
245 <dt class="hdlist1">\r
246 visible (boolean)\r
247 </dt>\r
248 <dd>\r
249 <p>\r
250         Whether this workspace is currently visible on an output (multiple\r
251         workspaces can be visible at the same time).\r
252 </p>\r
253 </dd>\r
254 <dt class="hdlist1">\r
255 focused (boolean)\r
256 </dt>\r
257 <dd>\r
258 <p>\r
259         Whether this workspace currently has the focus (only one workspace\r
260         can have the focus at the same time).\r
261 </p>\r
262 </dd>\r
263 <dt class="hdlist1">\r
264 urgent (boolean)\r
265 </dt>\r
266 <dd>\r
267 <p>\r
268         Whether a window on this workspace has the "urgent" flag set.\r
269 </p>\r
270 </dd>\r
271 <dt class="hdlist1">\r
272 rect (map)\r
273 </dt>\r
274 <dd>\r
275 <p>\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
278 </p>\r
279 </dd>\r
280 <dt class="hdlist1">\r
281 output (string)\r
282 </dt>\r
283 <dd>\r
284 <p>\r
285         The video output this workspace is on (LVDS1, VGA1, …).\r
286 </p>\r
287 </dd>\r
288 </dl></div>\r
289 <div class="paragraph"><p><strong>Example:</strong></p></div>\r
290 <div class="listingblock">\r
291 <div class="content">\r
292 <pre><tt>[\r
293  {\r
294   "num": 0,\r
295   "name": "1",\r
296   "visible": true,\r
297   "focused": true,\r
298   "urgent": false,\r
299   "rect": {\r
300    "x": 0,\r
301    "y": 0,\r
302    "width": 1280,\r
303    "height": 800\r
304   },\r
305   "output": "LVDS1"\r
306  },\r
307  {\r
308   "num": 1,\r
309   "name": "2",\r
310   "visible": false,\r
311   "focused": false,\r
312   "urgent": false,\r
313   "rect": {\r
314    "x": 0,\r
315    "y": 0,\r
316    "width": 1280,\r
317    "height": 800\r
318   },\r
319   "output": "LVDS1"\r
320  }\r
321 ]</tt></pre>\r
322 </div></div>\r
323 </div>\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
333 </div></div>\r
334 </div>\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
341 name (string)\r
342 </dt>\r
343 <dd>\r
344 <p>\r
345         The name of this output (as seen in <tt>xrandr(1)</tt>). Encoded in UTF-8.\r
346 </p>\r
347 </dd>\r
348 <dt class="hdlist1">\r
349 active (boolean)\r
350 </dt>\r
351 <dd>\r
352 <p>\r
353         Whether this output is currently active (has a valid mode).\r
354 </p>\r
355 </dd>\r
356 <dt class="hdlist1">\r
357 current_workspace (integer)\r
358 </dt>\r
359 <dd>\r
360 <p>\r
361         The current workspace which is visible on this output. <tt>null</tt> if the\r
362         output is not active.\r
363 </p>\r
364 </dd>\r
365 <dt class="hdlist1">\r
366 rect (map)\r
367 </dt>\r
368 <dd>\r
369 <p>\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
372 </p>\r
373 </dd>\r
374 </dl></div>\r
375 <div class="paragraph"><p><strong>Example:</strong></p></div>\r
376 <div class="listingblock">\r
377 <div class="content">\r
378 <pre><tt>[\r
379  {\r
380   "name": "LVDS1",\r
381   "active": true,\r
382   "current_workspace": 4,\r
383   "rect": {\r
384    "x": 0,\r
385    "y": 0,\r
386    "width": 1280,\r
387    "height": 800\r
388   }\r
389  },\r
390  {\r
391   "name": "VGA1",\r
392   "active": true,\r
393   "current_workspace": 1,\r
394   "rect": {\r
395    "x": 1280,\r
396    "y": 0,\r
397    "width": 1280,\r
398    "height": 1024\r
399   },\r
400  }\r
401 ]</tt></pre>\r
402 </div></div>\r
403 </div>\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
412 id (integer)\r
413 </dt>\r
414 <dd>\r
415 <p>\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
419 </p>\r
420 </dd>\r
421 <dt class="hdlist1">\r
422 name (string)\r
423 </dt>\r
424 <dd>\r
425 <p>\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
430 </p>\r
431 </dd>\r
432 <dt class="hdlist1">\r
433 border (string)\r
434 </dt>\r
435 <dd>\r
436 <p>\r
437         Can be either "normal", "none" or "1pixel", dependending on the\r
438         container’s border style.\r
439 </p>\r
440 </dd>\r
441 <dt class="hdlist1">\r
442 layout (string)\r
443 </dt>\r
444 <dd>\r
445 <p>\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
448         layouts.\r
449 </p>\r
450 </dd>\r
451 <dt class="hdlist1">\r
452 orientation (string)\r
453 </dt>\r
454 <dd>\r
455 <p>\r
456         Can be either "none" (for non-split containers), "horizontal" or\r
457         "vertical".\r
458 </p>\r
459 </dd>\r
460 <dt class="hdlist1">\r
461 percent (float)\r
462 </dt>\r
463 <dd>\r
464 <p>\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
468 </p>\r
469 </dd>\r
470 <dt class="hdlist1">\r
471 rect (map)\r
472 </dt>\r
473 <dd>\r
474 <p>\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
479         1200 }</tt>.\r
480 </p>\r
481 </dd>\r
482 <dt class="hdlist1">\r
483 window_rect (map)\r
484 </dt>\r
485 <dd>\r
486 <p>\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
493 </p>\r
494 </dd>\r
495 <dt class="hdlist1">\r
496 geometry (map)\r
497 </dt>\r
498 <dd>\r
499 <p>\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
502 </p>\r
503 </dd>\r
504 <dt class="hdlist1">\r
505 urgent (bool)\r
506 </dt>\r
507 <dd>\r
508 <p>\r
509         Whether this container (window or workspace) has the urgency hint set.\r
510 </p>\r
511 </dd>\r
512 <dt class="hdlist1">\r
513 focused (bool)\r
514 </dt>\r
515 <dd>\r
516 <p>\r
517         Whether this container is currently focused.\r
518 </p>\r
519 </dd>\r
520 </dl></div>\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
528 <li>\r
529 <p>\r
530 root\r
531 </p>\r
532 <div class="ulist"><ul>\r
533 <li>\r
534 <p>\r
535 LVDS1\r
536 </p>\r
537 <div class="ulist"><ul>\r
538 <li>\r
539 <p>\r
540 topdock\r
541 </p>\r
542 </li>\r
543 <li>\r
544 <p>\r
545 content\r
546 </p>\r
547 <div class="ulist"><ul>\r
548 <li>\r
549 <p>\r
550 workspace 1\r
551 </p>\r
552 <div class="ulist"><ul>\r
553 <li>\r
554 <p>\r
555 window 1\r
556 </p>\r
557 </li>\r
558 </ul></div>\r
559 </li>\r
560 </ul></div>\r
561 </li>\r
562 <li>\r
563 <p>\r
564 bottomdock\r
565 </p>\r
566 <div class="ulist"><ul>\r
567 <li>\r
568 <p>\r
569 dock window 1\r
570 </p>\r
571 </li>\r
572 </ul></div>\r
573 </li>\r
574 </ul></div>\r
575 </li>\r
576 <li>\r
577 <p>\r
578 VGA1\r
579 </p>\r
580 </li>\r
581 </ul></div>\r
582 </li>\r
583 </ul></div>\r
584 <div class="paragraph"><p><strong>Example:</strong></p></div>\r
585 <div class="listingblock">\r
586 <div class="content">\r
587 <pre><tt>{\r
588  "id": 6875648,\r
589  "name": "root",\r
590  "rect": {\r
591    "x": 0,\r
592    "y": 0,\r
593    "width": 1280,\r
594    "height": 800\r
595  },\r
596  "nodes": [\r
597 \r
598    {\r
599     "id": 6878320,\r
600     "name": "LVDS1",\r
601     "layout": "output",\r
602     "rect": {\r
603       "x": 0,\r
604       "y": 0,\r
605       "width": 1280,\r
606       "height": 800\r
607     },\r
608     "nodes": [\r
609 \r
610       {\r
611        "id": 6878784,\r
612        "name": "topdock",\r
613        "layout": "dockarea",\r
614        "orientation": "vertical",\r
615        "rect": {\r
616          "x": 0,\r
617          "y": 0,\r
618          "width": 1280,\r
619          "height": 0\r
620        },\r
621       },\r
622 \r
623       {\r
624        "id": 6879344,\r
625        "name": "content",\r
626        "rect": {\r
627          "x": 0,\r
628          "y": 0,\r
629          "width": 1280,\r
630          "height": 782\r
631        },\r
632        "nodes": [\r
633 \r
634          {\r
635           "id": 6880464,\r
636           "name": "1",\r
637           "orientation": "horizontal",\r
638           "rect": {\r
639             "x": 0,\r
640             "y": 0,\r
641             "width": 1280,\r
642             "height": 782\r
643           },\r
644           "floating_nodes": [],\r
645           "nodes": [\r
646 \r
647             {\r
648              "id": 6929968,\r
649              "name": "#aa0000",\r
650              "border": "normal",\r
651              "percent": 1,\r
652              "rect": {\r
653                "x": 0,\r
654                "y": 18,\r
655                "width": 1280,\r
656                "height": 782\r
657              }\r
658             }\r
659 \r
660           ]\r
661          }\r
662 \r
663        ]\r
664       },\r
665 \r
666       {\r
667        "id": 6880208,\r
668        "name": "bottomdock",\r
669        "layout": "dockarea",\r
670        "orientation": "vertical",\r
671        "rect": {\r
672          "x": 0,\r
673          "y": 782,\r
674          "width": 1280,\r
675          "height": 18\r
676        },\r
677        "nodes": [\r
678 \r
679          {\r
680           "id": 6931312,\r
681           "name": "#00aa00",\r
682           "percent": 1,\r
683           "rect": {\r
684             "x": 0,\r
685             "y": 782,\r
686             "width": 1280,\r
687             "height": 18\r
688           }\r
689          }\r
690 \r
691        ]\r
692       }\r
693     ]\r
694    }\r
695  ]\r
696 }</tt></pre>\r
697 </div></div>\r
698 </div>\r
699 </div>\r
700 </div>\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
725 </div></div>\r
726 </div>\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
733 workspace (0)\r
734 </dt>\r
735 <dd>\r
736 <p>\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
740 </p>\r
741 </dd>\r
742 <dt class="hdlist1">\r
743 output (1)\r
744 </dt>\r
745 <dd>\r
746 <p>\r
747         Sent when RandR issues a change notification (of either screens,\r
748         outputs, CRTCs or output properties).\r
749 </p>\r
750 </dd>\r
751 </dl></div>\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
756 \r
757 # unpack a 32-bit unsigned integer\r
758 my $message_type = unpack("L", $input);\r
759 \r
760 # check if the highest bit is 1\r
761 my $is_event = (($message_type &gt;&gt; 31) == 1);\r
762 \r
763 # use the other bits\r
764 my $event_type = ($message_type &amp; 0x7F);\r
765 \r
766 if ($is_event) {\r
767   say "Received event of type $event_type";\r
768 }</tt></pre>\r
769 </div></div>\r
770 </div>\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
780 </div></div>\r
781 </div>\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
791 </div></div>\r
792 </div>\r
793 </div>\r
794 </div>\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
800 know):</p></div>\r
801 <div class="dlist"><dl>\r
802 <dt class="hdlist1">\r
803 C\r
804 </dt>\r
805 <dd>\r
806 <p>\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
809 </p>\r
810 </dd>\r
811 <dt class="hdlist1">\r
812 Ruby\r
813 </dt>\r
814 <dd>\r
815 <p>\r
816         <a href="http://github.com/badboy/i3-ipc">http://github.com/badboy/i3-ipc</a>\r
817 </p>\r
818 </dd>\r
819 <dt class="hdlist1">\r
820 Perl\r
821 </dt>\r
822 <dd>\r
823 <p>\r
824         <a href="http://search.cpan.org/search?query=AnyEvent::I3">http://search.cpan.org/search?query=AnyEvent::I3</a>\r
825 </p>\r
826 </dd>\r
827 <dt class="hdlist1">\r
828 Python\r
829 </dt>\r
830 <dd>\r
831 <p>\r
832         <a href="http://github.com/thepub/i3ipc">http://github.com/thepub/i3ipc</a>\r
833 </p>\r
834 </dd>\r
835 </dl></div>\r
836 </div>\r
837 </div>\r
838 </div>\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
842 </div>\r
843 </body>\r
844 </html>\r