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