1 IPC interface (interprocess communication)
2 ==========================================
3 Michael Stapelberg <michael+i3@stapelberg.de>
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
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, no ipc-socket path is
14 specified and thus no socket is created. The standard path (which +i3-msg+ and
15 +i3-input+ use) is +/tmp/i3-ipc.sock+.
17 == Establishing a connection
19 To establish a connection, simply open the IPC socket. The following code
20 snippet illustrates this in Perl:
22 -------------------------------------------------------------
24 my $sock = IO::Socket::UNIX->new(Peer => '/tmp/i3-ipc.sock');
25 -------------------------------------------------------------
27 == Sending messages to i3
29 To send a message to i3, you have to format in the binary message format which
30 i3 expects. This format specifies a magic string in the beginning to ensure
31 the integrity of messages (to prevent follow-up errors). Afterwards follows
32 the length of the payload of the message as 32-bit integer and the type of
33 the message as 32-bit integer (the integers are not converted, so they are
34 in native byte order).
36 The magic string currently is "i3-ipc" and will only be changed when a change
37 in the IPC API is done which breaks compatibility (we hope that we don’t need
40 Currently implemented message types are the following:
43 The payload of the message is a command for i3 (like the commands you
44 can bind to keys in the configuration file) and will be executed
45 directly after receiving it. There is no reply to this message.
47 Gets the current workspaces. The reply will be a JSON-encoded list of
48 workspaces (see the reply section).
50 So, a typical message could look like this:
51 --------------------------------------------------
52 "i3-ipc" <message length> <message type> <payload>
53 --------------------------------------------------
56 ------------------------------------------------------------------------------
57 00000000 69 33 2d 69 70 63 04 00 00 00 00 00 00 00 65 78 |i3-ipc........ex|
58 00000010 69 74 0a |it.|
59 ------------------------------------------------------------------------------
61 To generate and send such a message, you could use the following code in Perl:
62 ------------------------------------------------------------
63 sub format_ipc_command {
66 # Get the real byte count (vs. amount of characters)
67 { use bytes; $len = length($msg); }
68 return "i3-ipc" . pack("LL", $len, 0) . $msg;
71 $sock->write(format_ipc_command("exit"));
72 ------------------------------------------------------------
74 == Receiving replies from i3
76 Replies of i3 usually consist of a simple string (the length of the string
77 is the message_length, so you can consider them length-prefixed) which in turn
78 contain the JSON serialization of a data structure. For example, the
79 GET_WORKSPACES message returns an array of workspaces (each workspace is a map
80 with certain attributes).
84 The reply format is identical to the normal message format. There also is
85 the magic string, then the message length, then the message type and the
88 The following reply types are implemented:
91 Reply to the GET_WORKSPACES message.
93 === GET_WORKSPACES reply
95 The reply consists of a serialized list of workspaces. Each workspace has the
99 The internal number of the workspace. Corresponds to the command
100 to switch to this workspace.
102 The name of this workspace (by default num+1), as changed by the
103 user. Encoded in UTF-8.
105 Whether this workspace is currently visible on an output (multiple
106 workspaces can be visible at the same time).
108 Whether this workspace currently has the focus (only one workspace
109 can have the focus at the same time).
111 The rectangle of this workspace (equals the rect of the output it
112 is on), consists of x, y, width, height.
114 The video output this workspace is on (LVDS1, VGA1, …).