This document describes how to interface with i3 from a separate process. This is useful for example to remote-control i3 (to write test cases for example) or to get various information like the current workspaces to implement an external workspace bar.

The method of choice for IPC in our case is a unix socket because it has very little overhead on both sides and is usually available without headaches in most languages. By default i3 will set the path of the IPC socket based on:

  1. The ipc-socket configuration directive if it is used

  2. The I3SOCK environmental variable if it is set

  3. $XDG_RUNTIME_DIR/i3/ipc-socket.%p if the directory is available where %p is the PID of i3 and XXXXXX is a string of random characters

  4. /tmp/i3-%u.XXXXXX/ipc-socket.%p where %u is your UNIX username

You can get the socketpath from i3 by executing i3 --get-socketpath, which will print the path to the standard output (plus a newline) or by reading the I3SOCK environmental variable.

All i3 utilities, like i3-msg and i3-input will determine the path of the IPC socket frome the I3SOCK environmental variable if it is set or the I3_SOCKET_PATH X11 property, stored on the X11 root window.

Warning
Use an existing library!
There are existing libraries for many languages. You can have a look at [libraries] or search the web if your language of choice is not mentioned. Usually, it is not necessary to implement low-level communication with i3 directly.

1. Establishing a connection

To establish a connection, simply open the IPC socket. The following code snippet illustrates this in Perl:

use IO::Socket::UNIX;
chomp(my $path = qx(i3 --get-socketpath));
my $sock = IO::Socket::UNIX->new(Peer => $path);

2. Sending messages to i3

To send a message to i3, you have to format it in the binary message format which i3 expects. This format specifies a magic string in the beginning to ensure the integrity of messages (to prevent follow-up errors). Following the magic string comes the length of the payload of the message as a 32-bit integer, and the type of the message as a 32-bit integer (the integers are not converted, so they are in native byte order).

The magic string currently is "i3-ipc" and will only be changed when a change in the IPC API is done which breaks compatibility (we hope that we don’t need to do that).

Table 1. Currently implemented message types
Type (numeric) Type (name) Reply type Purpose

0

RUN_COMMAND

COMMAND

Run the payload as an i3 command (like the commands you can bind to keys).

1

GET_WORKSPACES

WORKSPACES

Get the list of current workspaces.

2

SUBSCRIBE

SUBSCRIBE

Subscribe this IPC connection to the event types specified in the message payload. See [events].

3

GET_OUTPUTS

OUTPUTS

Get the list of current outputs.

4

GET_TREE

TREE

Get the i3 layout tree.

5

GET_MARKS

MARKS

Gets the names of all currently set marks.

6

GET_BAR_CONFIG

BAR_CONFIG

Gets the specified bar configuration or the names of all bar configurations if payload is empty.

7

GET_VERSION

VERSION

Gets the i3 version.

8

GET_BINDING_MODES

BINDING_MODES

Gets the names of all currently configured binding modes.

9

GET_CONFIG

CONFIG

Returns the last loaded i3 config.

10

SEND_TICK

TICK

Sends a tick event with the specified payload.

11

SYNC

SYNC

Sends an i3 sync event with the specified random value to the specified window.

12

GET_BINDING_STATE

BINDING_STATE

Request the current binding state, i.e. the currently active binding mode name.

So, a typical message could look like this:

"i3-ipc" <message length> <message type> <payload>

Or, as a hexdump:

00000000  69 33 2d 69 70 63 04 00  00 00 00 00 00 00 65 78  |i3-ipc........ex|
00000010  69 74                                             |it|

To generate and send such a message, you could use the following code in Perl:

sub format_ipc_command {
    my ($msg) = @_;
    my $len;
    # Get the real byte count (vs. amount of characters)
    { use bytes; $len = length($msg); }
    return "i3-ipc" . pack("LL", $len, 0) . $msg;
}

$sock->write(format_ipc_command("exit"));

3. Receiving replies from i3

Each message sent to i3 will cause exactly one reply to be sent in return. The order of the sent replies will always correspond to the order of the sent requests. The only exception to this is [events], which (once subscribed to) may be sent at any time (though never in the middle of another event or reply).

It is generally safe to send several messages to i3 without first waiting for a reply for each one (pipelining) — though, note that depending on the language / network library you use, writing to the socket without also reading from it may cause a deadlock due to the socket buffers getting full.

The reply format is identical to the normal message format. There also is the magic string, then the message length, then the message type and the payload.

The payload of replies from i3 usually consists of a simple string (the length of the string is the message_length, so you can consider them length-prefixed), which in turn contain the JSON serialization of a data structure. For example, the GET_WORKSPACES message returns an array of workspaces (each workspace is a map with certain attributes).

Replies currently have a 1:1 correspondence to messages, with the message type of the reply corresponding to the message type of the message which caused the reply to be sent.

The following reply types are implemented:

COMMAND (0)

Confirmation/Error code for the RUN_COMMAND message.

WORKSPACES (1)

Reply to the GET_WORKSPACES message.

SUBSCRIBE (2)

Confirmation/Error code for the SUBSCRIBE message.

OUTPUTS (3)

Reply to the GET_OUTPUTS message.

TREE (4)

Reply to the GET_TREE message.

MARKS (5)

Reply to the GET_MARKS message.

BAR_CONFIG (6)

Reply to the GET_BAR_CONFIG message.

VERSION (7)

Reply to the GET_VERSION message.

BINDING_MODES (8)

Reply to the GET_BINDING_MODES message.

GET_CONFIG (9)

Reply to the GET_CONFIG message.

TICK (10)

Reply to the SEND_TICK message.

SYNC (11)

Reply to the SYNC message.

GET_BINDING_STATE (12)

Reply to the GET_BINDING_STATE message.

4. Messages and replies

4.1. RUN_COMMAND / COMMAND

Run the payload as an i3 command (like the commands you can bind to keys).

Message:

The message payload is the string containing the command to execute. There is no JSON encoding or trailing newline.

Reply:

The reply consists of a list of serialized maps for each command that was parsed. Each has the property success (bool) and may also include a human-readable error message in the property error (string).

Note
When sending the restart command, you will get a singular reply once the restart completed. All IPC connection states (e.g. subscriptions) will reset and all but one socket will be closed. Libraries must be able to cope with this by aligning their internal states. It is also recommended that libraries close the last remaining socket(one which replied to restart command) to achieve the full reset.
Note
It is easiest to always send the restart command alone: due to i3’s state reset, the reply messages of preceding commands are lost, and following commands will not be executed.
Note
When processing the exit command, i3 will immediately exit without sending a reply. Expect the socket to be shut down.

Example:

[{ "success": true }]

When the specified command cannot be parsed, success will be false and parse_error will be true:

Example:

[{ "success": false, "parse_error": true }]

4.2. GET_WORKSPACES / WORKSPACES

Get the list of current workspaces.

Message:

No payload.

Reply:

The reply consists of a serialized list of workspaces. Each workspace has the following properties:

id (integer)

The internal ID (actually a C pointer value) of this container. Do not make any assumptions about it. You can use it to (re-)identify and address containers when talking to i3.

num (integer)

The logical number of the workspace. Corresponds to the command to switch to this workspace. For named workspaces, this will be -1.

name (string)

The name of this workspace if changed by the user, otherwise defaults to the string representation of the num field). Encoded in UTF-8.

visible (boolean)

Whether this workspace is currently visible on an output (multiple workspaces can be visible at the same time).

focused (boolean)

Whether this workspace currently has the focus (only one workspace can have the focus at the same time).

urgent (boolean)

Whether a window on this workspace has the "urgent" flag set.

rect (map)

The rectangle of this workspace (equals the rect of the output it is on), consists of x, y, width, height.

output (string)

The video output this workspace is on (LVDS1, VGA1, …).

Example:

[
 {
  "num": 0,
  "name": "1",
  "visible": true,
  "focused": true,
  "urgent": false,
  "rect": {
   "x": 0,
   "y": 0,
   "width": 1280,
   "height": 800
  },
  "output": "LVDS1"
 },
 {
  "num": 1,
  "name": "2",
  "visible": false,
  "focused": false,
  "urgent": false,
  "rect": {
   "x": 0,
   "y": 0,
   "width": 1280,
   "height": 800
  },
  "output": "LVDS1"
 }
]

4.3. SUBSCRIBE

Subscribe this IPC connection to the event types specified in the message payload. See [events].

Message:

A JSON-encoded array of event types to subscribe to.

Reply:

The reply consists of a single serialized map. The only property is success (bool), indicating whether the subscription was successful (the default) or whether a JSON parse error occurred.

Example:

{ "success": true }

4.4. GET_OUTPUTS / OUTPUTS

Get the list of current outputs.

Message:

No payload.

Reply:

The reply consists of a serialized list of outputs. Each output has the following properties:

name (string)

The name of this output (as seen in xrandr(1)). Encoded in UTF-8.

active (boolean)

Whether this output is currently active (has a valid mode).

primary (boolean)

Whether this output is currently the primary output.

current_workspace (string or null)

The name of the current workspace that is visible on this output. null if the output is not active.

rect (map)

The rectangle of this output (equals the rect of the output it is on), consists of x, y, width, height.

Example:

[
 {
  "name": "LVDS1",
  "active": true,
  "current_workspace": "4",
  "rect": {
   "x": 0,
   "y": 0,
   "width": 1280,
   "height": 800
  }
 },
 {
  "name": "VGA1",
  "active": true,
  "current_workspace": "1",
  "rect": {
   "x": 1280,
   "y": 0,
   "width": 1280,
   "height": 1024
  }
 }
]

4.5. GET_TREE / TREE

Get the i3 layout tree.

Message:

No payload.

Reply:

The reply consists of a serialized tree. Each node in the tree (representing one container) has at least the properties listed below. While the nodes might have more properties, please do not use any properties which are not documented here. They are not yet finalized and will probably change!

id (integer)

The internal ID (actually a C pointer value) of this container. Do not make any assumptions about it. You can use it to (re-)identify and address containers when talking to i3.

name (string)

The internal name of this container. For all containers which are part of the tree structure down to the workspace contents, this is set to a nice human-readable name of the container. For containers that have an X11 window, the content is the title (_NET_WM_NAME property) of that window. For all other containers, the content is not defined (yet).

type (string)

Type of this container. Can be one of "root", "output", "con", "floating_con", "workspace" or "dockarea".

border (string)

Can be either "normal", "none" or "pixel", depending on the container’s border style.

current_border_width (integer)

Number of pixels of the border width.

layout (string)

Can be either "splith", "splitv", "stacked", "tabbed", "dockarea" or "output". Other values might be possible in the future, should we add new layouts.

orientation (string)

Can be either "none" (for non-split containers), "horizontal" or "vertical". THIS FIELD IS OBSOLETE. It is still present, but your code should not use it. Instead, rely on the layout field.

percent (float or null)

The percentage which this container takes in its parent. A value of null means that the percent property does not make sense for this container, for example for the root container.

rect (map)

The absolute display coordinates for this container. Display coordinates means that when you have two 1600x1200 monitors on a single X11 Display (the standard way), the coordinates of the first window on the second monitor are { "x": 1600, "y": 0, "width": 1600, "height": 1200 }.

window_rect (map)

The coordinates of the actual client window inside its container. These coordinates are relative to the container and do not include the window decoration (which is actually rendered on the parent container). So, when using the default layout, you will have a 2 pixel border on each side, making the window_rect { "x": 2, "y": 0, "width": 632, "height": 366 } (for example).

deco_rect (map)

The coordinates of the window decoration inside its container. These coordinates are relative to the container and do not include the actual client window.

actual_deco_rect (map)

See deco_rect. i3 v4.22 changed the way title bars are rendered. Before i3 v4.22, the deco_rect was always relative to the parent coordinates. Starting with i3 v4.22, this remains true for tabbed/stacked containers (actual_deco_rect is identical to deco_rect), but for normal-border leaf containers within vertical/horizontal split containers, actual_deco_rect is relative to the container itself. For more background, see https://github.com/i3/i3/issues/1966

geometry (map)

The original geometry the window specified when i3 mapped it. Used when switching a window to floating mode, for example.

window (integer or null)

The X11 window ID of the actual client window inside this container. This field is set to null for split containers or otherwise empty containers. This ID corresponds to what xwininfo(1) and other X11-related tools display (usually in hex).

window_properties (map)

This optional field contains all available X11 window properties from the following list: title, instance, class, window_role, machine and transient_for.

window_type (string)

The window type (_NET_WM_WINDOW_TYPE). Possible values are undefined, unknown, normal, dialog, utility, toolbar, splash, menu, dropdown_menu, popup_menu, tooltip and notification.

urgent (bool)

Whether this container (window, split container, floating container or workspace) has the urgency hint set, directly or indirectly. All parent containers up until the workspace container will be marked urgent if they have at least one urgent child.

marks (array of string)

List of marks assigned to container

focused (bool)

Whether this container is currently focused.

focus (array of integer)

List of child node IDs (see nodes, floating_nodes and id) in focus order. Traversing the tree by following the first entry in this array will result in eventually reaching the one node with focused set to true.

sticky (bool)

Whether this window is "sticky". If it is also floating, this window will be present on all workspaces on the same output.

fullscreen_mode (integer)

Whether this container is in fullscreen state or not. Possible values are 0 (no fullscreen), 1 (fullscreened on output) or 2 (fullscreened globally). Note that all workspaces are considered fullscreened on their respective output.

floating (string)

Floating state of container. Can be either "auto_on", "auto_off", "user_on" or "user_off"

nodes (array of node)

The tiling (i.e. non-floating) child containers of this node.

floating_nodes (array of node)

The floating child containers of this node. Only non-empty on nodes with type workspace.

scratchpad_state (string)

Whether the window is not in the scratchpad ("none"), freshly moved to the scratchpad but not yet resized ("fresh") or moved to the scratchpad and resized ("changed").

Please note that in the following example, I have left out some keys/values which are not relevant for the type of the node. Otherwise, the example would be by far too long (it already is quite long, despite showing only 1 window and one dock window).

It is useful to have an overview of the structure before taking a look at the JSON dump:

  • root

    • LVDS1

      • topdock

      • content

        • workspace 1

          • window 1

      • bottomdock

        • dock window 1

    • VGA1

Example:

{
 "id": 6875648,
 "name": "root",
 "rect": {
   "x": 0,
   "y": 0,
   "width": 1280,
   "height": 800
 },
 "nodes": [

   {
    "id": 6878320,
    "name": "LVDS1",
    "layout": "output",
    "rect": {
      "x": 0,
      "y": 0,
      "width": 1280,
      "height": 800
    },
    "nodes": [

      {
       "id": 6878784,
       "name": "topdock",
       "layout": "dockarea",
       "orientation": "vertical",
       "rect": {
         "x": 0,
         "y": 0,
         "width": 1280,
         "height": 0
       }
      },

      {
       "id": 6879344,
       "name": "content",
       "rect": {
         "x": 0,
         "y": 0,
         "width": 1280,
         "height": 782
       },
       "nodes": [

         {
          "id": 6880464,
          "name": "1",
          "orientation": "horizontal",
          "rect": {
            "x": 0,
            "y": 0,
            "width": 1280,
            "height": 782
          },
          "window_properties": {
            "class": "Evince",
            "instance": "evince",
            "title": "Properties",
            "transient_for": 52428808
          },
          "floating_nodes": [],
          "nodes": [

            {
             "id": 6929968,
             "name": "#aa0000",
             "border": "normal",
             "percent": 1,
             "rect": {
               "x": 0,
               "y": 18,
               "width": 1280,
               "height": 782
             }
            }

          ]
         }

       ]
      },

      {
       "id": 6880208,
       "name": "bottomdock",
       "layout": "dockarea",
       "orientation": "vertical",
       "rect": {
         "x": 0,
         "y": 782,
         "width": 1280,
         "height": 18
       },
       "nodes": [

         {
          "id": 6931312,
          "name": "#00aa00",
          "percent": 1,
          "rect": {
            "x": 0,
            "y": 782,
            "width": 1280,
            "height": 18
          }
         }

       ]
      }
    ]
   }
 ]
}

4.6. GET_MARKS / MARKS

Gets the names of all currently set marks.

Message:

No payload.

Reply:

The reply consists of a single array of strings for each container that has a mark. A mark can only be set on one container, so the array is unique. The order of that array is undefined.

If no window has a mark the response will be the empty array [].

4.7. GET_BAR_CONFIG / BAR_CONFIG

Gets the specified bar configuration or the names of all bar configurations if payload is empty.

Message:

No payload, or the ID of the bar whose configuration to retrieve.

Reply:

This can be used by third-party workspace bars (especially i3bar, but others are free to implement compatible alternatives) to get the bar block configuration from i3.

Depending on the input, the reply is either:

empty input

An array of configured bar IDs

Bar ID

A JSON map containing the configuration for the specified bar.