]> git.sur5r.net Git - i3/i3/blob - docs/ipc
docs/ipc: add reference to ThePub’s python library
[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. Alternatively you can set the
15 environment-variable +I3SOCK+. Setting a path in the configfile will override
16 +I3SOCK+.
17
18 +i3-msg+ and +i3-input+ will use +I3SOCK+ to connect to i3, unless -s is passed.
19 If neither is given, they will default to +/tmp/i3-ipc.sock+.
20
21 == Establishing a connection
22
23 To establish a connection, simply open the IPC socket. The following code
24 snippet illustrates this in Perl:
25
26 -------------------------------------------------------------
27 use IO::Socket::UNIX;
28 my $sock = IO::Socket::UNIX->new(Peer => '/tmp/i3-ipc.sock');
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
60 So, a typical message could look like this:
61 --------------------------------------------------
62 "i3-ipc" <message length> <message type> <payload>
63 --------------------------------------------------
64
65 Or, as a hexdump:
66 ------------------------------------------------------------------------------
67 00000000  69 33 2d 69 70 63 04 00  00 00 00 00 00 00 65 78  |i3-ipc........ex|
68 00000010  69 74 0a                                          |it.|
69 ------------------------------------------------------------------------------
70
71 To generate and send such a message, you could use the following code in Perl:
72 ------------------------------------------------------------
73 sub format_ipc_command {
74     my ($msg) = @_;
75     my $len;
76     # Get the real byte count (vs. amount of characters)
77     { use bytes; $len = length($msg); }
78     return "i3-ipc" . pack("LL", $len, 0) . $msg;
79 }
80
81 $sock->write(format_ipc_command("exit"));
82 ------------------------------------------------------------------------------
83
84 == Receiving replies from i3
85
86 Replies from i3 usually consist of a simple string (the length of the string
87 is the message_length, so you can consider them length-prefixed) which in turn
88 contain the JSON serialization of a data structure. For example, the
89 GET_WORKSPACES message returns an array of workspaces (each workspace is a map
90 with certain attributes).
91
92 === Reply format
93
94 The reply format is identical to the normal message format. There also is
95 the magic string, then the message length, then the message type and the
96 payload.
97
98 The following reply types are implemented:
99
100 COMMAND (0)::
101         Confirmation/Error code for the COMMAND message.
102 GET_WORKSPACES (1)::
103         Reply to the GET_WORKSPACES message.
104 SUBSCRIBE (2)::
105         Confirmation/Error code for the SUBSCRIBE message.
106 GET_OUTPUTS (3)::
107         Reply to the GET_OUTPUTS message.
108
109 === COMMAND reply
110
111 The reply consists of a single serialized map. At the moment, the only
112 property is +success (bool)+, but this will be expanded in future versions.
113
114 *Example:*
115 -------------------
116 { "success": true }
117 -------------------
118
119 === GET_WORKSPACES reply
120
121 The reply consists of a serialized list of workspaces. Each workspace has the
122 following properties:
123
124 num (integer)::
125         The logical number of the workspace. Corresponds to the command
126         to switch to this workspace.
127 name (string)::
128         The name of this workspace (by default num+1), as changed by the
129         user. Encoded in UTF-8.
130 visible (boolean)::
131         Whether this workspace is currently visible on an output (multiple
132         workspaces can be visible at the same time).
133 focused (boolean)::
134         Whether this workspace currently has the focus (only one workspace
135         can have the focus at the same time).
136 urgent (boolean)::
137         Whether a window on this workspace has the "urgent" flag set.
138 rect (map)::
139         The rectangle of this workspace (equals the rect of the output it
140         is on), consists of x, y, width, height.
141 output (string)::
142         The video output this workspace is on (LVDS1, VGA1, …).
143
144 *Example:*
145 -------------------
146 [
147  {
148   "num": 0,
149   "name": "1",
150   "visible": true,
151   "focused": true,
152   "urgent": false,
153   "rect": {
154    "x": 0,
155    "y": 0,
156    "width": 1280,
157    "height": 800
158   },
159   "output": "LVDS1"
160  },
161  {
162   "num": 1,
163   "name": "2",
164   "visible": false,
165   "focused": false,
166   "urgent": false,
167   "rect": {
168    "x": 0,
169    "y": 0,
170    "width": 1280,
171    "height": 800
172   },
173   "output": "LVDS1"
174  }
175 ]
176 -------------------
177
178 === SUBSCRIBE reply
179
180 The reply consists of a single serialized map. The only property is
181 +success (bool)+, indicating whether the subscription was successful (the
182 default) or whether a JSON parse error occurred.
183
184 *Example:*
185 -------------------
186 { "success": true }
187 -------------------
188
189 === GET_OUTPUTS reply
190
191 The reply consists of a serialized list of outputs. Each output has the
192 following properties:
193
194 name (string)::
195         The name of this output (as seen in +xrandr(1)+). Encoded in UTF-8.
196 active (boolean)::
197         Whether this output is currently active (has a valid mode).
198 current_workspace (integer)::
199         The current workspace which is visible on this output. +null+ if the
200         output is not active.
201 rect (map)::
202         The rectangle of this output (equals the rect of the output it
203         is on), consists of x, y, width, height.
204
205 *Example:*
206 -------------------
207 [
208  {
209   "name": "LVDS1",
210   "active": true,
211   "current_workspace": 4,
212   "rect": {
213    "x": 0,
214    "y": 0,
215    "width": 1280,
216    "height": 800
217   }
218  },
219  {
220   "name": "VGA1",
221   "active": true,
222   "current_workspace": 1,
223   "rect": {
224    "x": 1280,
225    "y": 0,
226    "width": 1280,
227    "height": 1024
228   },
229  }
230 ]
231 -------------------
232
233 == Events
234
235 [[events]]
236
237 To get informed when certain things happen in i3, clients can subscribe to
238 events. Events consist of a name (like "workspace") and an event reply type
239 (like I3_IPC_EVENT_WORKSPACE). The events sent by i3 are in the same format
240 as replies to specific commands.
241
242 Caveat: As soon as you subscribe to an event, it is not guaranteed any longer
243 that the requests to i3 are processed in order. This means, the following
244 situation can happen: You send a GET_WORKSPACES request but you receive a
245 "workspace" event before receiving the reply to GET_WORKSPACES. If your
246 program does not want to cope which such kinds of race conditions (an
247 event based library may not have a problem here), I suggest you create a
248 separate connection to receive events.
249
250 === Subscribing to events
251
252 By sending a message of type SUBSCRIBE with a JSON-encoded array as payload
253 you can register to an event.
254
255 *Example:*
256 ---------------------------------
257 type: SUBSCRIBE
258 payload: [ "workspace", "focus" ]
259 ---------------------------------
260
261 === Available events
262
263 workspace::
264         Sent when the user switches to a different workspace, when a new
265         workspace is initialized or when a workspace is removed (because the
266         last client vanished).
267 output::
268         Sent when RandR issues a change notification (of either screens,
269         outputs, CRTCs or output properties).
270
271 === workspace event
272
273 This event consists of a single serialized map containing a property
274 +change (string)+ which indicates the type of the change ("focus", "init",
275 "empty", "urgent").
276
277 *Example:*
278 ---------------------
279 { "change": "focus" }
280 ---------------------
281
282 === output event
283
284 This event consists of a single serialized map containing a property
285 +change (string)+ which indicates the type of the change (currently only
286 "unspecified").
287
288 *Example:*
289 ---------------------------
290 { "change": "unspecified" }
291 ---------------------------
292
293 == See also
294
295 For some languages, libraries are available (so you don’t have to implement
296 all this on your own). This list names some (if you wrote one, please let me
297 know):
298
299 C::
300         i3 includes a headerfile +i3/ipc.h+ which provides you all constants.
301         However, there is no library yet.
302 Ruby::
303         http://github.com/badboy/i3-ipc
304 Perl::
305         http://search.cpan.org/search?query=AnyEvent::I3
306 Python::
307         http://github.com/thepub/i3ipc