]> git.sur5r.net Git - i3/i3/blob - docs/ipc
docs/ipc: update socket path section
[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, the ipc-socket gets created
14 in +/tmp/i3-%u/ipc-socket.%p+ where +%u+ is your UNIX username and +%p+ is the
15 PID of i3.
16
17 All i3 utilities, like +i3-msg+ and +i3-input+ will read the +I3_SOCKET_PATH+
18 X11 property, stored on the X11 root window.
19
20 == Establishing a connection
21
22 To establish a connection, simply open the IPC socket. The following code
23 snippet illustrates this in Perl:
24
25 -------------------------------------------------------------
26 use IO::Socket::UNIX;
27 my $sock = IO::Socket::UNIX->new(Peer => '/tmp/i3-ipc.sock');
28 -------------------------------------------------------------
29
30 == Sending messages to i3
31
32 To send a message to i3, you have to format in the binary message format which
33 i3 expects. This format specifies a magic string in the beginning to ensure
34 the integrity of messages (to prevent follow-up errors). Following the magic
35 string comes the length of the payload of the message as 32-bit integer, and
36 the type of the message as 32-bit integer (the integers are not converted, so
37 they are in native byte order).
38
39 The magic string currently is "i3-ipc" and will only be changed when a change
40 in the IPC API is done which breaks compatibility (we hope that we don’t need
41 to do that).
42
43 Currently implemented message types are the following:
44
45 COMMAND (0)::
46         The payload of the message is a command for i3 (like the commands you
47         can bind to keys in the configuration file) and will be executed
48         directly after receiving it. There is no reply to this message.
49 GET_WORKSPACES (1)::
50         Gets the current workspaces. The reply will be a JSON-encoded list of
51         workspaces (see the reply section).
52 SUBSCRIBE (2)::
53         Subscribes your connection to certain events. See <<events>> for a
54         description of this message and the concept of events.
55 GET_OUTPUTS (3)::
56         Gets the current outputs. The reply will be a JSON-encoded list of outputs
57         (see the reply section).
58
59 So, a typical message could look like this:
60 --------------------------------------------------
61 "i3-ipc" <message length> <message type> <payload>
62 --------------------------------------------------
63
64 Or, as a hexdump:
65 ------------------------------------------------------------------------------
66 00000000  69 33 2d 69 70 63 04 00  00 00 00 00 00 00 65 78  |i3-ipc........ex|
67 00000010  69 74 0a                                          |it.|
68 ------------------------------------------------------------------------------
69
70 To generate and send such a message, you could use the following code in Perl:
71 ------------------------------------------------------------
72 sub format_ipc_command {
73     my ($msg) = @_;
74     my $len;
75     # Get the real byte count (vs. amount of characters)
76     { use bytes; $len = length($msg); }
77     return "i3-ipc" . pack("LL", $len, 0) . $msg;
78 }
79
80 $sock->write(format_ipc_command("exit"));
81 ------------------------------------------------------------------------------
82
83 == Receiving replies from i3
84
85 Replies from i3 usually consist of a simple string (the length of the string
86 is the message_length, so you can consider them length-prefixed) which in turn
87 contain the JSON serialization of a data structure. For example, the
88 GET_WORKSPACES message returns an array of workspaces (each workspace is a map
89 with certain attributes).
90
91 === Reply format
92
93 The reply format is identical to the normal message format. There also is
94 the magic string, then the message length, then the message type and the
95 payload.
96
97 The following reply types are implemented:
98
99 COMMAND (0)::
100         Confirmation/Error code for the COMMAND message.
101 GET_WORKSPACES (1)::
102         Reply to the GET_WORKSPACES message.
103 SUBSCRIBE (2)::
104         Confirmation/Error code for the SUBSCRIBE message.
105 GET_OUTPUTS (3)::
106         Reply to the GET_OUTPUTS message.
107
108 === COMMAND reply
109
110 The reply consists of a single serialized map. At the moment, the only
111 property is +success (bool)+, but this will be expanded in future versions.
112
113 *Example:*
114 -------------------
115 { "success": true }
116 -------------------
117
118 === GET_WORKSPACES reply
119
120 The reply consists of a serialized list of workspaces. Each workspace has the
121 following properties:
122
123 num (integer)::
124         The logical number of the workspace. Corresponds to the command
125         to switch to this workspace.
126 name (string)::
127         The name of this workspace (by default num+1), as changed by the
128         user. Encoded in UTF-8.
129 visible (boolean)::
130         Whether this workspace is currently visible on an output (multiple
131         workspaces can be visible at the same time).
132 focused (boolean)::
133         Whether this workspace currently has the focus (only one workspace
134         can have the focus at the same time).
135 urgent (boolean)::
136         Whether a window on this workspace has the "urgent" flag set.
137 rect (map)::
138         The rectangle of this workspace (equals the rect of the output it
139         is on), consists of x, y, width, height.
140 output (string)::
141         The video output this workspace is on (LVDS1, VGA1, …).
142
143 *Example:*
144 -------------------
145 [
146  {
147   "num": 0,
148   "name": "1",
149   "visible": true,
150   "focused": true,
151   "urgent": false,
152   "rect": {
153    "x": 0,
154    "y": 0,
155    "width": 1280,
156    "height": 800
157   },
158   "output": "LVDS1"
159  },
160  {
161   "num": 1,
162   "name": "2",
163   "visible": false,
164   "focused": false,
165   "urgent": false,
166   "rect": {
167    "x": 0,
168    "y": 0,
169    "width": 1280,
170    "height": 800
171   },
172   "output": "LVDS1"
173  }
174 ]
175 -------------------
176
177 === SUBSCRIBE reply
178
179 The reply consists of a single serialized map. The only property is
180 +success (bool)+, indicating whether the subscription was successful (the
181 default) or whether a JSON parse error occurred.
182
183 *Example:*
184 -------------------
185 { "success": true }
186 -------------------
187
188 === GET_OUTPUTS reply
189
190 The reply consists of a serialized list of outputs. Each output has the
191 following properties:
192
193 name (string)::
194         The name of this output (as seen in +xrandr(1)+). Encoded in UTF-8.
195 active (boolean)::
196         Whether this output is currently active (has a valid mode).
197 current_workspace (integer)::
198         The current workspace which is visible on this output. +null+ if the
199         output is not active.
200 rect (map)::
201         The rectangle of this output (equals the rect of the output it
202         is on), consists of x, y, width, height.
203
204 *Example:*
205 -------------------
206 [
207  {
208   "name": "LVDS1",
209   "active": true,
210   "current_workspace": 4,
211   "rect": {
212    "x": 0,
213    "y": 0,
214    "width": 1280,
215    "height": 800
216   }
217  },
218  {
219   "name": "VGA1",
220   "active": true,
221   "current_workspace": 1,
222   "rect": {
223    "x": 1280,
224    "y": 0,
225    "width": 1280,
226    "height": 1024
227   },
228  }
229 ]
230 -------------------
231
232 == Events
233
234 [[events]]
235
236 To get informed when certain things happen in i3, clients can subscribe to
237 events. Events consist of a name (like "workspace") and an event reply type
238 (like I3_IPC_EVENT_WORKSPACE). The events sent by i3 are in the same format
239 as replies to specific commands. However, the highest bit of the message type
240 is set to 1 to indicate that this is an event reply instead of a normal reply.
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
262 === Available events
263
264 The numbers in parenthesis is the event type (keep in mind that you need to
265 strip the highest bit first).
266
267 workspace (0)::
268         Sent when the user switches to a different workspace, when a new
269         workspace is initialized or when a workspace is removed (because the
270         last client vanished).
271 output (1)::
272         Sent when RandR issues a change notification (of either screens,
273         outputs, CRTCs or output properties).
274
275 *Example:*
276 --------------------------------------------------------------------
277 # the appropriate 4 bytes read from the socket are stored in $input
278
279 # unpack a 32-bit unsigned integer
280 my $message_type = unpack("L", $input);
281
282 # check if the highest bit is 1
283 my $is_event = (($message_type >> 31) == 1);
284
285 # use the other bits
286 my $event_type = ($message_type & 0x7F);
287
288 if ($is_event) {
289   say "Received event of type $event_type";
290 }
291 --------------------------------------------------------------------
292
293 === workspace event
294
295 This event consists of a single serialized map containing a property
296 +change (string)+ which indicates the type of the change ("focus", "init",
297 "empty", "urgent").
298
299 *Example:*
300 ---------------------
301 { "change": "focus" }
302 ---------------------
303
304 === output event
305
306 This event consists of a single serialized map containing a property
307 +change (string)+ which indicates the type of the change (currently only
308 "unspecified").
309
310 *Example:*
311 ---------------------------
312 { "change": "unspecified" }
313 ---------------------------
314
315 == See also
316
317 For some languages, libraries are available (so you don’t have to implement
318 all this on your own). This list names some (if you wrote one, please let me
319 know):
320
321 C::
322         i3 includes a headerfile +i3/ipc.h+ which provides you all constants.
323         However, there is no library yet.
324 Ruby::
325         http://github.com/badboy/i3-ipc
326 Perl::
327         http://search.cpan.org/search?query=AnyEvent::I3
328 Python::
329         http://github.com/thepub/i3ipc