]> git.sur5r.net Git - i3/i3/blob - docs/ipc
Merge branch 'master' into next
[i3/i3] / docs / ipc
1 IPC interface (interprocess communication)
2 ==========================================
3 Michael Stapelberg <michael@i3wm.org>
4 February 2014
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.XXXXXX/ipc-socket.%p+ where +%u+ is your UNIX username, +%p+ is
15 the PID of i3 and XXXXXX is a string of random characters from the portable
16 filename character set (see mkdtemp(3)). You can get the socketpath from i3 by
17 calling +i3 --get-socketpath+.
18
19 All i3 utilities, like +i3-msg+ and +i3-input+ will read the +I3_SOCKET_PATH+
20 X11 property, stored on the X11 root window.
21
22 [WARNING]
23 .Use an existing library!
24 There are existing libraries for many languages. You can have a look at
25 <<libraries>> or search the web if your language of choice is not mentioned.
26 Usually, it is not necessary to implement low-level communication with i3
27 directly.
28
29 == Establishing a connection
30
31 To establish a connection, simply open the IPC socket. The following code
32 snippet illustrates this in Perl:
33
34 -------------------------------------------------------------
35 use IO::Socket::UNIX;
36 chomp(my $path = qx(i3 --get-socketpath));
37 my $sock = IO::Socket::UNIX->new(Peer => $path);
38 -------------------------------------------------------------
39
40 == Sending messages to i3
41
42 To send a message to i3, you have to format in the binary message format which
43 i3 expects. This format specifies a magic string in the beginning to ensure
44 the integrity of messages (to prevent follow-up errors). Following the magic
45 string comes the length of the payload of the message as 32-bit integer, and
46 the type of the message as 32-bit integer (the integers are not converted, so
47 they are in native byte order).
48
49 The magic string currently is "i3-ipc" and will only be changed when a change
50 in the IPC API is done which breaks compatibility (we hope that we don’t need
51 to do that).
52
53 Currently implemented message types are the following:
54
55 COMMAND (0)::
56         The payload of the message is a command for i3 (like the commands you
57         can bind to keys in the configuration file) and will be executed
58         directly after receiving it.
59 GET_WORKSPACES (1)::
60         Gets the current workspaces. The reply will be a JSON-encoded list of
61         workspaces (see the reply section).
62 SUBSCRIBE (2)::
63         Subscribes your connection to certain events. See <<events>> for a
64         description of this message and the concept of events.
65 GET_OUTPUTS (3)::
66         Gets the current outputs. The reply will be a JSON-encoded list of outputs
67         (see the reply section).
68 GET_TREE (4)::
69         Gets the layout tree. i3 uses a tree as data structure which includes
70         every container. The reply will be the JSON-encoded tree (see the reply
71         section).
72 GET_MARKS (5)::
73         Gets a list of marks (identifiers for containers to easily jump to them
74         later). The reply will be a JSON-encoded list of window marks (see
75         reply section).
76 GET_BAR_CONFIG (6)::
77         Gets the configuration (as JSON map) of the workspace bar with the
78         given ID. If no ID is provided, an array with all configured bar IDs is
79         returned instead.
80 GET_VERSION (7)::
81         Gets the version of i3. The reply will be a JSON-encoded dictionary
82         with the major, minor, patch and human-readable version.
83
84 So, a typical message could look like this:
85 --------------------------------------------------
86 "i3-ipc" <message length> <message type> <payload>
87 --------------------------------------------------
88
89 Or, as a hexdump:
90 ------------------------------------------------------------------------------
91 00000000  69 33 2d 69 70 63 04 00  00 00 00 00 00 00 65 78  |i3-ipc........ex|
92 00000010  69 74                                             |it|
93 ------------------------------------------------------------------------------
94
95 To generate and send such a message, you could use the following code in Perl:
96 ------------------------------------------------------------
97 sub format_ipc_command {
98     my ($msg) = @_;
99     my $len;
100     # Get the real byte count (vs. amount of characters)
101     { use bytes; $len = length($msg); }
102     return "i3-ipc" . pack("LL", $len, 0) . $msg;
103 }
104
105 $sock->write(format_ipc_command("exit"));
106 ------------------------------------------------------------------------------
107
108 == Receiving replies from i3
109
110 Replies from i3 usually consist of a simple string (the length of the string
111 is the message_length, so you can consider them length-prefixed) which in turn
112 contain the JSON serialization of a data structure. For example, the
113 GET_WORKSPACES message returns an array of workspaces (each workspace is a map
114 with certain attributes).
115
116 === Reply format
117
118 The reply format is identical to the normal message format. There also is
119 the magic string, then the message length, then the message type and the
120 payload.
121
122 The following reply types are implemented:
123
124 COMMAND (0)::
125         Confirmation/Error code for the COMMAND message.
126 WORKSPACES (1)::
127         Reply to the GET_WORKSPACES message.
128 SUBSCRIBE (2)::
129         Confirmation/Error code for the SUBSCRIBE message.
130 OUTPUTS (3)::
131         Reply to the GET_OUTPUTS message.
132 TREE (4)::
133         Reply to the GET_TREE message.
134 MARKS (5)::
135         Reply to the GET_MARKS message.
136 BAR_CONFIG (6)::
137         Reply to the GET_BAR_CONFIG message.
138 VERSION (7)::
139         Reply to the GET_VERSION message.
140
141 === COMMAND reply
142
143 The reply consists of a list of serialized maps for each command that was
144 parsed. Each has the property +success (bool)+ and may also include a
145 human-readable error message in the property +error (string)+.
146
147 *Example:*
148 -------------------
149 [{ "success": true }]
150 -------------------
151
152 === WORKSPACES reply
153
154 The reply consists of a serialized list of workspaces. Each workspace has the
155 following properties:
156
157 num (integer)::
158         The logical number of the workspace. Corresponds to the command
159         to switch to this workspace.
160 name (string)::
161         The name of this workspace (by default num+1), as changed by the
162         user. Encoded in UTF-8.
163 visible (boolean)::
164         Whether this workspace is currently visible on an output (multiple
165         workspaces can be visible at the same time).
166 focused (boolean)::
167         Whether this workspace currently has the focus (only one workspace
168         can have the focus at the same time).
169 urgent (boolean)::
170         Whether a window on this workspace has the "urgent" flag set.
171 rect (map)::
172         The rectangle of this workspace (equals the rect of the output it
173         is on), consists of x, y, width, height.
174 output (string)::
175         The video output this workspace is on (LVDS1, VGA1, …).
176
177 *Example:*
178 -------------------
179 [
180  {
181   "num": 0,
182   "name": "1",
183   "visible": true,
184   "focused": true,
185   "urgent": false,
186   "rect": {
187    "x": 0,
188    "y": 0,
189    "width": 1280,
190    "height": 800
191   },
192   "output": "LVDS1"
193  },
194  {
195   "num": 1,
196   "name": "2",
197   "visible": false,
198   "focused": false,
199   "urgent": false,
200   "rect": {
201    "x": 0,
202    "y": 0,
203    "width": 1280,
204    "height": 800
205   },
206   "output": "LVDS1"
207  }
208 ]
209 -------------------
210
211 === SUBSCRIBE reply
212
213 The reply consists of a single serialized map. The only property is
214 +success (bool)+, indicating whether the subscription was successful (the
215 default) or whether a JSON parse error occurred.
216
217 *Example:*
218 -------------------
219 { "success": true }
220 -------------------
221
222 === OUTPUTS reply
223
224 The reply consists of a serialized list of outputs. Each output has the
225 following properties:
226
227 name (string)::
228         The name of this output (as seen in +xrandr(1)+). Encoded in UTF-8.
229 active (boolean)::
230         Whether this output is currently active (has a valid mode).
231 current_workspace (string)::
232         The name of the current workspace that is visible on this output. +null+ if
233         the output is not active.
234 rect (map)::
235         The rectangle of this output (equals the rect of the output it
236         is on), consists of x, y, width, height.
237
238 *Example:*
239 -------------------
240 [
241  {
242   "name": "LVDS1",
243   "active": true,
244   "current_workspace": "4",
245   "rect": {
246    "x": 0,
247    "y": 0,
248    "width": 1280,
249    "height": 800
250   }
251  },
252  {
253   "name": "VGA1",
254   "active": true,
255   "current_workspace": "1",
256   "rect": {
257    "x": 1280,
258    "y": 0,
259    "width": 1280,
260    "height": 1024
261   },
262  }
263 ]
264 -------------------
265
266 === TREE reply
267
268 The reply consists of a serialized tree. Each node in the tree (representing
269 one container) has at least the properties listed below. While the nodes might
270 have more properties, please do not use any properties which are not documented
271 here. They are not yet finalized and will probably change!
272
273 id (integer)::
274         The internal ID (actually a C pointer value) of this container. Do not
275         make any assumptions about it. You can use it to (re-)identify and
276         address containers when talking to i3.
277 name (string)::
278         The internal name of this container. For all containers which are part
279         of the tree structure down to the workspace contents, this is set to a
280         nice human-readable name of the container.
281         For containers that have an X11 window, the content is the title
282         (_NET_WM_NAME property) of that window.
283         For all other containers, the content is not defined (yet).
284 type (string)::
285         Type of this container. Can be one of "root", "output", "con",
286         "floating_con", "workspace" or "dockarea".
287 border (string)::
288         Can be either "normal", "none" or "1pixel", dependending on the
289         container’s border style.
290 current_border_width (integer)::
291         Number of pixels of the border width.
292 layout (string)::
293         Can be either "splith", "splitv", "stacked", "tabbed", "dockarea" or
294         "output".
295         Other values might be possible in the future, should we add new
296         layouts.
297 orientation (string)::
298         Can be either "none" (for non-split containers), "horizontal" or
299         "vertical".
300         THIS FIELD IS OBSOLETE. It is still present, but your code should not
301         use it. Instead, rely on the layout field.
302 percent (float)::
303         The percentage which this container takes in its parent. A value of
304         +null+ means that the percent property does not make sense for this
305         container, for example for the root container.
306 rect (map)::
307         The absolute display coordinates for this container. Display
308         coordinates means that when you have two 1600x1200 monitors on a single
309         X11 Display (the standard way), the coordinates of the first window on
310         the second monitor are +{ "x": 1600, "y": 0, "width": 1600, "height":
311         1200 }+.
312 window_rect (map)::
313         The coordinates of the *actual client window* inside its container.
314         These coordinates are relative to the container and do not include the
315         window decoration (which is actually rendered on the parent container).
316         So, when using the +default+ layout, you will have a 2 pixel border on
317         each side, making the window_rect +{ "x": 2, "y": 0, "width": 632,
318         "height": 366 }+ (for example).
319 geometry (map)::
320         The original geometry the window specified when i3 mapped it. Used when
321         switching a window to floating mode, for example.
322 window (integer)::
323         The X11 window ID of the *actual client window* inside this container.
324         This field is set to null for split containers or otherwise empty
325         containers. This ID corresponds to what xwininfo(1) and other
326         X11-related tools display (usually in hex).
327 urgent (bool)::
328         Whether this container (window or workspace) has the urgency hint set.
329 focused (bool)::
330         Whether this container is currently focused.
331
332 Please note that in the following example, I have left out some keys/values
333 which are not relevant for the type of the node. Otherwise, the example would
334 be by far too long (it already is quite long, despite showing only 1 window and
335 one dock window).
336
337 It is useful to have an overview of the structure before taking a look at the
338 JSON dump:
339
340 * root
341 ** LVDS1
342 *** topdock
343 *** content
344 **** workspace 1
345 ***** window 1
346 *** bottomdock
347 **** dock window 1
348 ** VGA1
349
350 *Example:*
351 -----------------------
352 {
353  "id": 6875648,
354  "name": "root",
355  "rect": {
356    "x": 0,
357    "y": 0,
358    "width": 1280,
359    "height": 800
360  },
361  "nodes": [
362
363    {
364     "id": 6878320,
365     "name": "LVDS1",
366     "layout": "output",
367     "rect": {
368       "x": 0,
369       "y": 0,
370       "width": 1280,
371       "height": 800
372     },
373     "nodes": [
374
375       {
376        "id": 6878784,
377        "name": "topdock",
378        "layout": "dockarea",
379        "orientation": "vertical",
380        "rect": {
381          "x": 0,
382          "y": 0,
383          "width": 1280,
384          "height": 0
385        },
386       },
387
388       {
389        "id": 6879344,
390        "name": "content",
391        "rect": {
392          "x": 0,
393          "y": 0,
394          "width": 1280,
395          "height": 782
396        },
397        "nodes": [
398
399          {
400           "id": 6880464,
401           "name": "1",
402           "orientation": "horizontal",
403           "rect": {
404             "x": 0,
405             "y": 0,
406             "width": 1280,
407             "height": 782
408           },
409           "floating_nodes": [],
410           "nodes": [
411
412             {
413              "id": 6929968,
414              "name": "#aa0000",
415              "border": "normal",
416              "percent": 1,
417              "rect": {
418                "x": 0,
419                "y": 18,
420                "width": 1280,
421                "height": 782
422              }
423             }
424
425           ]
426          }
427
428        ]
429       },
430
431       {
432        "id": 6880208,
433        "name": "bottomdock",
434        "layout": "dockarea",
435        "orientation": "vertical",
436        "rect": {
437          "x": 0,
438          "y": 782,
439          "width": 1280,
440          "height": 18
441        },
442        "nodes": [
443
444          {
445           "id": 6931312,
446           "name": "#00aa00",
447           "percent": 1,
448           "rect": {
449             "x": 0,
450             "y": 782,
451             "width": 1280,
452             "height": 18
453           }
454          }
455
456        ]
457       }
458     ]
459    }
460  ]
461 }
462 ------------------------
463
464 === MARKS reply
465
466 The reply consists of a single array of strings for each container that has a
467 mark. A mark can only be set on one container, so the array is unique.
468 The order of that array is undefined.
469
470 If no window has a mark the response will be the empty array [].
471
472 === BAR_CONFIG reply
473
474 This can be used by third-party workspace bars (especially i3bar, but others
475 are free to implement compatible alternatives) to get the +bar+ block
476 configuration from i3.
477
478 Depending on the input, the reply is either:
479
480 empty input::
481         An array of configured bar IDs
482 Bar ID::
483         A JSON map containing the configuration for the specified bar.
484
485 Each bar configuration has the following properties:
486
487 id (string)::
488         The ID for this bar. Included in case you request multiple
489         configurations and want to differentiate the different replies.
490 mode (string)::
491         Either +dock+ (the bar sets the dock window type) or +hide+ (the bar
492         does not show unless a specific key is pressed).
493 position (string)::
494         Either +bottom+ or +top+ at the moment.
495 status_command (string)::
496         Command which will be run to generate a statusline. Each line on stdout
497         of this command will be displayed in the bar. At the moment, no
498         formatting is supported.
499 font (string)::
500         The font to use for text on the bar.
501 workspace_buttons (boolean)::
502         Display workspace buttons or not? Defaults to true.
503 binding_mode_indicator (boolean)::
504         Display the mode indicator or not? Defaults to true.
505 verbose (boolean)::
506         Should the bar enable verbose output for debugging? Defaults to false.
507 colors (map)::
508         Contains key/value pairs of colors. Each value is a color code in hex,
509         formatted #rrggbb (like in HTML).
510
511 The following colors can be configured at the moment:
512
513 background::
514         Background color of the bar.
515 statusline::
516         Text color to be used for the statusline.
517 separator::
518         Text color to be used for the separator.
519 focused_workspace_text/focused_workspace_bg::
520         Text color/background color for a workspace button when the workspace
521         has focus.
522 active_workspace_text/active_workspace_bg::
523         Text color/background color for a workspace button when the workspace
524         is active (visible) on some output, but the focus is on another one.
525         You can only tell this apart from the focused workspace when you are
526         using multiple monitors.
527 inactive_workspace_text/inactive_workspace_bg::
528         Text color/background color for a workspace button when the workspace
529         does not have focus and is not active (visible) on any output. This
530         will be the case for most workspaces.
531 urgent_workspace_text/urgent_workspace_bar::
532         Text color/background color for workspaces which contain at least one
533         window with the urgency hint set.
534
535
536 *Example of configured bars:*
537 --------------
538 ["bar-bxuqzf"]
539 --------------
540
541 *Example of bar configuration:*
542 --------------
543 {
544  "id": "bar-bxuqzf",
545  "mode": "dock",
546  "position": "bottom",
547  "status_command": "i3status",
548  "font": "-misc-fixed-medium-r-normal--13-120-75-75-C-70-iso10646-1",
549  "workspace_buttons": true,
550  "binding_mode_indicator": true,
551  "verbose": false,
552  "colors": {
553    "background": "#c0c0c0",
554    "statusline": "#00ff00",
555    "focused_workspace_text": "#ffffff",
556    "focused_workspace_bg": "#000000"
557  }
558 }
559 --------------
560
561 === VERSION reply
562
563 The reply consists of a single JSON dictionary with the following keys:
564
565 major (integer)::
566         The major version of i3, such as +4+.
567 minor (integer)::
568         The minor version of i3, such as +2+. Changes in the IPC interface (new
569         features) will only occur with new minor (or major) releases. However,
570         bugfixes might be introduced in patch releases, too.
571 patch (integer)::
572         The patch version of i3, such as +1+ (when the complete version is
573         +4.2.1+). For versions such as +4.2+, patch will be set to +0+.
574 human_readable (string)::
575         A human-readable version of i3 containing the precise git version,
576         build date and branch name. When you need to display the i3 version to
577         your users, use the human-readable version whenever possible (since
578         this is what +i3 --version+ displays, too).
579
580 *Example:*
581 -------------------
582 {
583    "human_readable" : "4.2-169-gf80b877 (2012-08-05, branch \"next\")",
584    "minor" : 2,
585    "patch" : 0,
586    "major" : 4
587 }
588 -------------------
589
590 == Events
591
592 [[events]]
593
594 To get informed when certain things happen in i3, clients can subscribe to
595 events. Events consist of a name (like "workspace") and an event reply type
596 (like I3_IPC_EVENT_WORKSPACE). The events sent by i3 are in the same format
597 as replies to specific commands. However, the highest bit of the message type
598 is set to 1 to indicate that this is an event reply instead of a normal reply.
599
600 Caveat: As soon as you subscribe to an event, it is not guaranteed any longer
601 that the requests to i3 are processed in order. This means, the following
602 situation can happen: You send a GET_WORKSPACES request but you receive a
603 "workspace" event before receiving the reply to GET_WORKSPACES. If your
604 program does not want to cope which such kinds of race conditions (an
605 event based library may not have a problem here), I suggest you create a
606 separate connection to receive events.
607
608 === Subscribing to events
609
610 By sending a message of type SUBSCRIBE with a JSON-encoded array as payload
611 you can register to an event.
612
613 *Example:*
614 ---------------------------------
615 type: SUBSCRIBE
616 payload: [ "workspace", "focus" ]
617 ---------------------------------
618
619
620 === Available events
621
622 The numbers in parenthesis is the event type (keep in mind that you need to
623 strip the highest bit first).
624
625 workspace (0)::
626         Sent when the user switches to a different workspace, when a new
627         workspace is initialized or when a workspace is removed (because the
628         last client vanished).
629 output (1)::
630         Sent when RandR issues a change notification (of either screens,
631         outputs, CRTCs or output properties).
632 mode (2)::
633         Sent whenever i3 changes its binding mode.
634 window (3)::
635         Sent when a client's window is successfully reparented (that is when i3
636         has finished fitting it into a container), when a window received input
637         focus or when certain properties of the window have changed.
638 barconfig_update (4)::
639     Sent when the hidden_state or mode field in the barconfig of any bar
640     instance was updated and when the config is reloaded.
641
642 *Example:*
643 --------------------------------------------------------------------
644 # the appropriate 4 bytes read from the socket are stored in $input
645
646 # unpack a 32-bit unsigned integer
647 my $message_type = unpack("L", $input);
648
649 # check if the highest bit is 1
650 my $is_event = (($message_type >> 31) == 1);
651
652 # use the other bits
653 my $event_type = ($message_type & 0x7F);
654
655 if ($is_event) {
656   say "Received event of type $event_type";
657 }
658 --------------------------------------------------------------------
659
660 === workspace event
661
662 This event consists of a single serialized map containing a property
663 +change (string)+ which indicates the type of the change ("focus", "init",
664 "empty", "urgent").
665
666 Moreover, when the change is "focus", an +old (object)+ and a +current
667 (object)+ properties will be present with the previous and current
668 workspace respectively.  When the first switch occurs (when i3 focuses
669 the workspace visible at the beginning) there is no previous
670 workspace, and the +old+ property will be set to +null+.  Also note
671 that if the previous is empty it will get destroyed when switching,
672 but will still be present in the "old" property.
673
674 *Example:*
675 ---------------------
676 {
677  "change": "focus",
678  "current": {
679   "id": 28489712,
680   "type": "workspace",
681   ...
682  }
683  "old": {
684   "id": 28489715,
685   "type": "workspace",
686   ...
687  }
688 }
689 ---------------------
690
691 === output event
692
693 This event consists of a single serialized map containing a property
694 +change (string)+ which indicates the type of the change (currently only
695 "unspecified").
696
697 *Example:*
698 ---------------------------
699 { "change": "unspecified" }
700 ---------------------------
701
702 === mode event
703
704 This event consists of a single serialized map containing a property
705 +change (string)+ which holds the name of current mode in use. The name
706 is the same as specified in config when creating a mode. The default
707 mode is simply named default.
708
709 *Example:*
710 ---------------------------
711 { "change": "default" }
712 ---------------------------
713
714 === window event
715
716 This event consists of a single serialized map containing a property
717 +change (string)+ which indicates the type of the change
718
719 * +new+ - the window has become managed by i3
720 * +close+ - the window has closed
721 * +focus+ - the window has received input focus
722 * +title+ - the window's title has changed
723 * +fullscreen_mode+ - the window has entered or exited fullscreen mode
724 * +move+ - the window has changed its position in the tree
725 * +floating+ - the window has transitioned to or from floating
726 * +urgent+ - the window has become urgent or lost its urgent status
727
728 Additionally a +container (object)+ field will be present, which consists
729 of the window's parent container. Be aware that for the "new" event, the
730 container will hold the initial name of the newly reparented window (e.g.
731 if you run urxvt with a shell that changes the title, you will still at
732 this point get the window title as "urxvt").
733
734 *Example:*
735 ---------------------------
736 {
737  "change": "new",
738  "container": {
739   "id": 35569536,
740   "type": "con",
741   ...
742  }
743 }
744 ---------------------------
745
746 === barconfig_update event
747
748 This event consists of a single serialized map reporting on options from the
749 barconfig of the specified bar_id that were updated in i3. This event is the
750 same as a +GET_BAR_CONFIG+ reply for the bar with the given id.
751
752 == See also (existing libraries)
753
754 [[libraries]]
755
756 For some languages, libraries are available (so you don’t have to implement
757 all this on your own). This list names some (if you wrote one, please let me
758 know):
759
760 C::
761         * i3 includes a headerfile +i3/ipc.h+ which provides you all constants.
762         * https://github.com/acrisci/i3ipc-glib
763 Go::
764         * https://github.com/proxypoke/i3ipc
765 JavaScript::
766         * https://github.com/acrisci/i3ipc-gjs
767 Lua::
768         * https://github.com/acrisci/i3ipc-lua
769 Perl::
770         * https://metacpan.org/module/AnyEvent::I3
771 Python::
772         * https://github.com/acrisci/i3ipc-python
773         * https://github.com/whitelynx/i3ipc (not maintained)
774         * https://github.com/ziberna/i3-py (not maintained)
775 Ruby::
776         * http://github.com/badboy/i3-ipc