]> git.sur5r.net Git - i3/i3/blob - docs/ipc
76643573fdf151daa2a8086b8438d6dbf41fdd06
[i3/i3] / docs / ipc
1 IPC interface (interprocess communication)
2 ==========================================
3 Michael Stapelberg <michael+i3@stapelberg.de>
4 October 2011
5
6 This document describes how to interface with i3 from a separate process. This
7 is useful for example to remote-control i3 (to write test cases for example) or
8 to get various information like the current workspaces to implement an external
9 workspace bar.
10
11 The method of choice for IPC in our case is a unix socket because it has very
12 little overhead on both sides and is usually available without headaches in
13 most languages. In the default configuration file, the ipc-socket gets created
14 in +/tmp/i3-%u/ipc-socket.%p+ where +%u+ is your UNIX username and +%p+ is the
15 PID of i3. You can get the socketpath from i3 by calling +i3 --get-socketpath+.
16
17 All i3 utilities, like +i3-msg+ and +i3-input+ will read the +I3_SOCKET_PATH+
18 X11 property, stored on the X11 root window.
19
20 == Establishing a connection
21
22 To establish a connection, simply open the IPC socket. The following code
23 snippet illustrates this in Perl:
24
25 -------------------------------------------------------------
26 use IO::Socket::UNIX;
27 chomp(my $path = qx(i3 --get-socketpath));
28 my $sock = IO::Socket::UNIX->new(Peer => $path);
29 -------------------------------------------------------------
30
31 == Sending messages to i3
32
33 To send a message to i3, you have to format in the binary message format which
34 i3 expects. This format specifies a magic string in the beginning to ensure
35 the integrity of messages (to prevent follow-up errors). Following the magic
36 string comes the length of the payload of the message as 32-bit integer, and
37 the type of the message as 32-bit integer (the integers are not converted, so
38 they are in native byte order).
39
40 The magic string currently is "i3-ipc" and will only be changed when a change
41 in the IPC API is done which breaks compatibility (we hope that we don’t need
42 to do that).
43
44 Currently implemented message types are the following:
45
46 COMMAND (0)::
47         The payload of the message is a command for i3 (like the commands you
48         can bind to keys in the configuration file) and will be executed
49         directly after receiving it. There is no reply to this message.
50 GET_WORKSPACES (1)::
51         Gets the current workspaces. The reply will be a JSON-encoded list of
52         workspaces (see the reply section).
53 SUBSCRIBE (2)::
54         Subscribes your connection to certain events. See <<events>> for a
55         description of this message and the concept of events.
56 GET_OUTPUTS (3)::
57         Gets the current outputs. The reply will be a JSON-encoded list of outputs
58         (see the reply section).
59 GET_TREE (4)::
60         Gets the layout tree. i3 uses a tree as data structure which includes
61         every container. The reply will be the JSON-encoded tree (see the reply
62         section).
63 GET_MARKS (5)::
64         Gets a list of marks (identifiers for containers to easily jump to them
65         later). The reply will be a JSON-encoded list of window marks (see
66         reply section).
67
68 So, a typical message could look like this:
69 --------------------------------------------------
70 "i3-ipc" <message length> <message type> <payload>
71 --------------------------------------------------
72
73 Or, as a hexdump:
74 ------------------------------------------------------------------------------
75 00000000  69 33 2d 69 70 63 04 00  00 00 00 00 00 00 65 78  |i3-ipc........ex|
76 00000010  69 74 0a                                          |it.|
77 ------------------------------------------------------------------------------
78
79 To generate and send such a message, you could use the following code in Perl:
80 ------------------------------------------------------------
81 sub format_ipc_command {
82     my ($msg) = @_;
83     my $len;
84     # Get the real byte count (vs. amount of characters)
85     { use bytes; $len = length($msg); }
86     return "i3-ipc" . pack("LL", $len, 0) . $msg;
87 }
88
89 $sock->write(format_ipc_command("exit"));
90 ------------------------------------------------------------------------------
91
92 == Receiving replies from i3
93
94 Replies from i3 usually consist of a simple string (the length of the string
95 is the message_length, so you can consider them length-prefixed) which in turn
96 contain the JSON serialization of a data structure. For example, the
97 GET_WORKSPACES message returns an array of workspaces (each workspace is a map
98 with certain attributes).
99
100 === Reply format
101
102 The reply format is identical to the normal message format. There also is
103 the magic string, then the message length, then the message type and the
104 payload.
105
106 The following reply types are implemented:
107
108 COMMAND (0)::
109         Confirmation/Error code for the COMMAND message.
110 GET_WORKSPACES (1)::
111         Reply to the GET_WORKSPACES message.
112 SUBSCRIBE (2)::
113         Confirmation/Error code for the SUBSCRIBE message.
114 GET_OUTPUTS (3)::
115         Reply to the GET_OUTPUTS message.
116 GET_TREE (4)::
117         Reply to the GET_TREE message.
118 GET_MARKS (5)::
119         Reply to the GET_MARKS message.
120
121 === COMMAND reply
122
123 The reply consists of a single serialized map. At the moment, the only
124 property is +success (bool)+, but this will be expanded in future versions.
125
126 *Example:*
127 -------------------
128 { "success": true }
129 -------------------
130
131 === GET_WORKSPACES reply
132
133 The reply consists of a serialized list of workspaces. Each workspace has the
134 following properties:
135
136 num (integer)::
137         The logical number of the workspace. Corresponds to the command
138         to switch to this workspace.
139 name (string)::
140         The name of this workspace (by default num+1), as changed by the
141         user. Encoded in UTF-8.
142 visible (boolean)::
143         Whether this workspace is currently visible on an output (multiple
144         workspaces can be visible at the same time).
145 focused (boolean)::
146         Whether this workspace currently has the focus (only one workspace
147         can have the focus at the same time).
148 urgent (boolean)::
149         Whether a window on this workspace has the "urgent" flag set.
150 rect (map)::
151         The rectangle of this workspace (equals the rect of the output it
152         is on), consists of x, y, width, height.
153 output (string)::
154         The video output this workspace is on (LVDS1, VGA1, …).
155
156 *Example:*
157 -------------------
158 [
159  {
160   "num": 0,
161   "name": "1",
162   "visible": true,
163   "focused": true,
164   "urgent": false,
165   "rect": {
166    "x": 0,
167    "y": 0,
168    "width": 1280,
169    "height": 800
170   },
171   "output": "LVDS1"
172  },
173  {
174   "num": 1,
175   "name": "2",
176   "visible": false,
177   "focused": false,
178   "urgent": false,
179   "rect": {
180    "x": 0,
181    "y": 0,
182    "width": 1280,
183    "height": 800
184   },
185   "output": "LVDS1"
186  }
187 ]
188 -------------------
189
190 === SUBSCRIBE reply
191
192 The reply consists of a single serialized map. The only property is
193 +success (bool)+, indicating whether the subscription was successful (the
194 default) or whether a JSON parse error occurred.
195
196 *Example:*
197 -------------------
198 { "success": true }
199 -------------------
200
201 === GET_OUTPUTS reply
202
203 The reply consists of a serialized list of outputs. Each output has the
204 following properties:
205
206 name (string)::
207         The name of this output (as seen in +xrandr(1)+). Encoded in UTF-8.
208 active (boolean)::
209         Whether this output is currently active (has a valid mode).
210 current_workspace (integer)::
211         The current workspace which is visible on this output. +null+ if the
212         output is not active.
213 rect (map)::
214         The rectangle of this output (equals the rect of the output it
215         is on), consists of x, y, width, height.
216
217 *Example:*
218 -------------------
219 [
220  {
221   "name": "LVDS1",
222   "active": true,
223   "current_workspace": 4,
224   "rect": {
225    "x": 0,
226    "y": 0,
227    "width": 1280,
228    "height": 800
229   }
230  },
231  {
232   "name": "VGA1",
233   "active": true,
234   "current_workspace": 1,
235   "rect": {
236    "x": 1280,
237    "y": 0,
238    "width": 1280,
239    "height": 1024
240   },
241  }
242 ]
243 -------------------
244
245 === GET_TREE reply
246
247 The reply consists of a serialized tree. Each node in the tree (representing
248 one container) has at least the properties listed below. While the nodes might
249 have more properties, please do not use any properties which are not documented
250 here. They are not yet finalized and will probably change!
251
252 id (integer)::
253         The internal ID (actually a C pointer value) of this container. Do not
254         make any assumptions about it. You can use it to (re-)identify and
255         address containers when talking to i3.
256 name (string)::
257         The internal name of this container. For all containers which are part
258         of the tree structure down to the workspace contents, this is set to a
259         nice human-readable name of the container.
260         For all other containers, the content is not defined (yet).
261 border (string)::
262         Can be either "normal", "none" or "1pixel", dependending on the
263         container’s border style.
264 layout (string)::
265         Can be either "default", "stacked", "tabbed", "dockarea" or "output".
266         Other values might be possible in the future, should we add new
267         layouts.
268 orientation (string)::
269         Can be either "none" (for non-split containers), "horizontal" or
270         "vertical".
271 percent (float)::
272         The percentage which this container takes in its parent. A value of
273         +null+ means that the percent property does not make sense for this
274         container, for example for the root container.
275 rect (map)::
276         The absolute display coordinates for this container. Display
277         coordinates means that when you have two 1600x1200 monitors on a single
278         X11 Display (the standard way), the coordinates of the first window on
279         the second monitor are +{ "x": 1600, "y": 0, "width": 1600, "height":
280         1200 }+.
281 window_rect (map)::
282         The coordinates of the *actual client window* inside its container.
283         These coordinates are relative to the container and do not include the
284         window decoration (which is actually rendered on the parent container).
285         So, when using the +default+ layout, you will have a 2 pixel border on
286         each side, making the window_rect +{ "x": 2, "y": 0, "width": 632,
287         "height": 366 }+ (for example).
288 geometry (map)::
289         The original geometry the window specified when i3 mapped it. Used when
290         switching a window to floating mode, for example.
291 urgent (bool)::
292         Whether this container (window or workspace) has the urgency hint set.
293 focused (bool)::
294         Whether this container is currently focused.
295
296 Please note that in the following example, I have left out some keys/values
297 which are not relevant for the type of the node. Otherwise, the example would
298 be by far too long (it already is quite long, despite showing only 1 window and
299 one dock window).
300
301 It is useful to have an overview of the structure before taking a look at the
302 JSON dump:
303
304 * root
305 ** LVDS1
306 *** topdock
307 *** content
308 **** workspace 1
309 ***** window 1
310 *** bottomdock
311 **** dock window 1
312 ** VGA1
313
314 *Example:*
315 -----------------------
316 {
317  "id": 6875648,
318  "name": "root",
319  "rect": {
320    "x": 0,
321    "y": 0,
322    "width": 1280,
323    "height": 800
324  },
325  "nodes": [
326
327    {
328     "id": 6878320,
329     "name": "LVDS1",
330     "layout": "output",
331     "rect": {
332       "x": 0,
333       "y": 0,
334       "width": 1280,
335       "height": 800
336     },
337     "nodes": [
338
339       {
340        "id": 6878784,
341        "name": "topdock",
342        "layout": "dockarea",
343        "orientation": "vertical",
344        "rect": {
345          "x": 0,
346          "y": 0,
347          "width": 1280,
348          "height": 0
349        },
350       },
351
352       {
353        "id": 6879344,
354        "name": "content",
355        "rect": {
356          "x": 0,
357          "y": 0,
358          "width": 1280,
359          "height": 782
360        },
361        "nodes": [
362
363          {
364           "id": 6880464,
365           "name": "1",
366           "orientation": "horizontal",
367           "rect": {
368             "x": 0,
369             "y": 0,
370             "width": 1280,
371             "height": 782
372           },
373           "floating_nodes": [],
374           "nodes": [
375
376             {
377              "id": 6929968,
378              "name": "#aa0000",
379              "border": "normal",
380              "percent": 1,
381              "rect": {
382                "x": 0,
383                "y": 18,
384                "width": 1280,
385                "height": 782
386              }
387             }
388
389           ]
390          }
391
392        ]
393       },
394
395       {
396        "id": 6880208,
397        "name": "bottomdock",
398        "layout": "dockarea",
399        "orientation": "vertical",
400        "rect": {
401          "x": 0,
402          "y": 782,
403          "width": 1280,
404          "height": 18
405        },
406        "nodes": [
407
408          {
409           "id": 6931312,
410           "name": "#00aa00",
411           "percent": 1,
412           "rect": {
413             "x": 0,
414             "y": 782,
415             "width": 1280,
416             "height": 18
417           }
418          }
419
420        ]
421       }
422     ]
423    }
424  ]
425 }
426 ------------------------
427
428 === GET_MARKS reply
429
430 The reply consists of a single array of strings for each container that has a
431 mark. The order of that array is undefined. If more than one container has the
432 same mark, it will be represented multiple times in the reply (the array
433 contents are not unique).
434
435 If no window has a mark the response will be the empty array [].
436
437
438 == Events
439
440 [[events]]
441
442 To get informed when certain things happen in i3, clients can subscribe to
443 events. Events consist of a name (like "workspace") and an event reply type
444 (like I3_IPC_EVENT_WORKSPACE). The events sent by i3 are in the same format
445 as replies to specific commands. However, the highest bit of the message type
446 is set to 1 to indicate that this is an event reply instead of a normal reply.
447
448 Caveat: As soon as you subscribe to an event, it is not guaranteed any longer
449 that the requests to i3 are processed in order. This means, the following
450 situation can happen: You send a GET_WORKSPACES request but you receive a
451 "workspace" event before receiving the reply to GET_WORKSPACES. If your
452 program does not want to cope which such kinds of race conditions (an
453 event based library may not have a problem here), I suggest you create a
454 separate connection to receive events.
455
456 === Subscribing to events
457
458 By sending a message of type SUBSCRIBE with a JSON-encoded array as payload
459 you can register to an event.
460
461 *Example:*
462 ---------------------------------
463 type: SUBSCRIBE
464 payload: [ "workspace", "focus" ]
465 ---------------------------------
466
467
468 === Available events
469
470 The numbers in parenthesis is the event type (keep in mind that you need to
471 strip the highest bit first).
472
473 workspace (0)::
474         Sent when the user switches to a different workspace, when a new
475         workspace is initialized or when a workspace is removed (because the
476         last client vanished).
477 output (1)::
478         Sent when RandR issues a change notification (of either screens,
479         outputs, CRTCs or output properties).
480
481 *Example:*
482 --------------------------------------------------------------------
483 # the appropriate 4 bytes read from the socket are stored in $input
484
485 # unpack a 32-bit unsigned integer
486 my $message_type = unpack("L", $input);
487
488 # check if the highest bit is 1
489 my $is_event = (($message_type >> 31) == 1);
490
491 # use the other bits
492 my $event_type = ($message_type & 0x7F);
493
494 if ($is_event) {
495   say "Received event of type $event_type";
496 }
497 --------------------------------------------------------------------
498
499 === workspace event
500
501 This event consists of a single serialized map containing a property
502 +change (string)+ which indicates the type of the change ("focus", "init",
503 "empty", "urgent").
504
505 *Example:*
506 ---------------------
507 { "change": "focus" }
508 ---------------------
509
510 === output event
511
512 This event consists of a single serialized map containing a property
513 +change (string)+ which indicates the type of the change (currently only
514 "unspecified").
515
516 *Example:*
517 ---------------------------
518 { "change": "unspecified" }
519 ---------------------------
520
521 == See also
522
523 For some languages, libraries are available (so you don’t have to implement
524 all this on your own). This list names some (if you wrote one, please let me
525 know):
526
527 C::
528         i3 includes a headerfile +i3/ipc.h+ which provides you all constants.
529         However, there is no library yet.
530 Ruby::
531         http://github.com/badboy/i3-ipc
532 Perl::
533         http://search.cpan.org/search?query=AnyEvent::I3
534 Python::
535         http://github.com/thepub/i3ipc