]> git.sur5r.net Git - i3/i3/blob - docs/ipc
Update IPC documentation
[i3/i3] / docs / ipc
1 IPC interface (interprocess communication)
2 ==========================================
3 Michael Stapelberg <michael+i3@stapelberg.de>
4 March 2010
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, 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+.
16
17 == Establishing a connection
18
19 To establish a connection, simply open the IPC socket. The following code
20 snippet illustrates this in Perl:
21
22 -------------------------------------------------------------
23 use IO::Socket::UNIX;
24 my $sock = IO::Socket::UNIX->new(Peer => '/tmp/i3-ipc.sock');
25 -------------------------------------------------------------
26
27 == Sending messages to i3
28
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).
35
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
38 to do that).
39
40 Currently implemented message types are the following:
41
42 COMMAND (0)::
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.
46 GET_WORKSPACES (1)::
47         Gets the current workspaces. The reply will be a JSON-encoded list of
48         workspaces (see the reply section).
49 SUBSCRIBE (2)::
50         Subscribes your connection to certain events. See <<events>> for a
51         description of this message and the concept of events.
52
53 So, a typical message could look like this:
54 --------------------------------------------------
55 "i3-ipc" <message length> <message type> <payload>
56 --------------------------------------------------
57
58 Or, as a hexdump:
59 ------------------------------------------------------------------------------
60 00000000  69 33 2d 69 70 63 04 00  00 00 00 00 00 00 65 78  |i3-ipc........ex|
61 00000010  69 74 0a                                          |it.|
62 ------------------------------------------------------------------------------
63
64 To generate and send such a message, you could use the following code in Perl:
65 ------------------------------------------------------------
66 sub format_ipc_command {
67     my ($msg) = @_;
68     my $len;
69     # Get the real byte count (vs. amount of characters)
70     { use bytes; $len = length($msg); }
71     return "i3-ipc" . pack("LL", $len, 0) . $msg;
72 }
73
74 $sock->write(format_ipc_command("exit"));
75 ------------------------------------------------------------
76
77 == Receiving replies from i3
78
79 Replies of i3 usually consist of a simple string (the length of the string
80 is the message_length, so you can consider them length-prefixed) which in turn
81 contain the JSON serialization of a data structure. For example, the
82 GET_WORKSPACES message returns an array of workspaces (each workspace is a map
83 with certain attributes).
84
85 === Reply format
86
87 The reply format is identical to the normal message format. There also is
88 the magic string, then the message length, then the message type and the
89 payload.
90
91 The following reply types are implemented:
92
93 COMMAND (0)::
94         Confirmation/Error code for the COMMAND message.
95 GET_WORKSPACES (1)::
96         Reply to the GET_WORKSPACES message.
97 SUBSCRIBE (2)::
98         Confirmation/Error code for the SUBSCRIBE message.
99
100 === COMMAND reply
101
102 The reply consists of a single serialized map. At the moment, the only
103 property is +success (bool)+, but this will be expanded in future versions.
104
105 *Example:*
106 -------------------
107 { "success": true }
108 -------------------
109
110 === GET_WORKSPACES reply
111
112 The reply consists of a serialized list of workspaces. Each workspace has the
113 following properties:
114
115 num (integer)::
116         The logical number of the workspace. Corresponds to the command
117         to switch to this workspace.
118 name (string)::
119         The name of this workspace (by default num+1), as changed by the
120         user. Encoded in UTF-8.
121 visible (boolean)::
122         Whether this workspace is currently visible on an output (multiple
123         workspaces can be visible at the same time).
124 focused (boolean)::
125         Whether this workspace currently has the focus (only one workspace
126         can have the focus at the same time).
127 rect (map)::
128         The rectangle of this workspace (equals the rect of the output it
129         is on), consists of x, y, width, height.
130 output (string)::
131         The video output this workspace is on (LVDS1, VGA1, …).
132
133 *Example:*
134 -------------------
135 [
136  {
137   "num": 0,
138   "name": "1",
139   "visible": true,
140   "focused": true,
141   "rect": {
142    "x": 0,
143    "y": 0,
144    "width": 1280,
145    "height": 800
146   },
147   "output": "LVDS1"
148  },
149  {
150   "num": 1,
151   "name": "2",
152   "visible": false,
153   "focused": false,
154   "rect": {
155    "x": 0,
156    "y": 0,
157    "width": 1280,
158    "height": 800
159   },
160   "output": "LVDS1"
161  }
162 ]
163 -------------------
164
165 === SUBSCRIBE reply
166
167 The reply consists of a single serialized map. The only property is
168 +success (bool)+, indicating whether the subscription was successful (the
169 default) or whether a JSON parse error occurred.
170
171 *Example:*
172 -------------------
173 { "success": true }
174 -------------------
175
176 == Events
177
178 [[events]]
179
180 To get informed when certain things happen in i3, clients can subscribe to
181 events. Events consist of a name (like "workspace") and an event reply type
182 (like I3_IPC_EVENT_WORKSPACE). The events sent by i3 are in the same format
183 as replies to specific commands.
184
185 Caveat: As soon as you subscribe to an event, it is not guaranteed any longer
186 that the requests to i3 are processed in order. This means, the following
187 situation can happen: You send a GET_WORKSPACES request but you receive a
188 "workspace" event before receiving the reply to GET_WORKSPACES. If your
189 program does not want to cope which such kinds of race conditions (an
190 event based library may not have a problem here), I advise to create a separate
191 connection to receive events.
192
193 === Subscribing to events
194
195 By sending a message of type SUBSCRIBE with a JSON-encoded array as payload
196 you can register to an event.
197
198 *Example:*
199 ---------------------------------
200 type: SUBSCRIBE
201 payload: [ "workspace", "focus" ]
202 ---------------------------------
203
204 === Available events
205
206 workspace::
207         Sent when the user switches to a different workspace, when a new
208         workspace is initialized or when a workspace is removed (because the
209         last client vanished).
210
211 === workspace event
212
213 This event consists of a single serialized map containing a property
214 +change (string)+ which indicates the type of the change ("focus", "init",
215 "empty").
216
217 *Example:*
218 ---------------------
219 { "change": "focus" }
220 ---------------------
221
222 == See also
223
224 For some languages, libraries are available (so you don’t have to implement
225 all this on your own). This list names some (if you wrote one, please let me
226 know):
227
228 Ruby::
229         http://github.com/badboy/i3-ipc
230 Perl::
231         http://search.cpan.org/search?query=AnyEvent::I3