]> git.sur5r.net Git - i3/i3/blob - docs/ipc
committed the wrong file
[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 0 (COMMAND)::
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 1 (GET_WORKSPACES)::
47         Gets the current workspaces. The reply will be a JSON-encoded list of
48         workspaces (see the reply section).
49
50 So, a typical message could look like this:
51 --------------------------------------------------
52 "i3-ipc" <message length> <message type> <payload>
53 --------------------------------------------------
54
55 Or, as a hexdump:
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 ------------------------------------------------------------------------------
60
61 To generate and send such a message, you could use the following code in Perl:
62 ------------------------------------------------------------
63 sub format_ipc_command {
64     my ($msg) = @_;
65     my $len;
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;
69 }
70
71 $sock->write(format_ipc_command("exit"));
72 ------------------------------------------------------------
73
74 == Receiving replies from i3
75
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).
81
82 === Reply format
83
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
86 payload.
87
88 The following reply types are implemented:
89
90 1 (GET_WORKSPACES)::
91         Reply to the GET_WORKSPACES message.
92
93 === GET_WORKSPACES reply
94
95 The reply consists of a serialized list of workspaces. Each workspace has the
96 following properties:
97
98 num (integer)::
99         The internal number of the workspace. Corresponds to the command
100         to switch to this workspace.
101 name (string)::
102         The name of this workspace (by default num+1), as changed by the
103         user. Encoded in UTF-8.
104 visible (boolean)::
105         Whether this workspace is currently visible on an output (multiple
106         workspaces can be visible at the same time).
107 focused (boolean)::
108         Whether this workspace currently has the focus (only one workspace
109         can have the focus at the same time).
110 rect (map)::
111         The rectangle of this workspace (equals the rect of the output it
112         is on), consists of x, y, width, height.
113 output (string)::
114         The video output this workspace is on (LVDS1, VGA1, …).
115
116 *Example:*
117 -------------------
118 [
119  {
120   "num": 0,
121   "name": "1",
122   "visible": true,
123   "focused": true,
124   "rect": {
125    "x": 0,
126    "y": 0,
127    "width": 1280,
128    "height": 800
129   },
130   "output": "LVDS1"
131  },
132  {
133   "num": 1,
134   "name": "2",
135   "visible": false,
136   "focused": false,
137   "rect": {
138    "x": 0,
139    "y": 0,
140    "width": 1280,
141    "height": 800
142   },
143   "output": "LVDS1"
144  }
145 ]
146 -------------------