Skip to main content

IPC library for i3wm

Project description

i3pie

i3wm ipc library for python.

Installation

pip install i3pie

Usage

>>> import i3pie
>>> with i3pie.Connection() as i3:
...     tree = i3.get_tree()
...     tree._pprint()
...
type='root' name='root'
├─ type='output' name='HDMI-A-0'
  ├─ type='dockarea' name='bottomdock'
  ├─ type='con' name='content'
    ├─ type='workspace' name='2'
      └─ type='con'
         └─ window=50331658 type='con' class='Nemo'
    └─ type='workspace' name='1'
       └─ type='con'
          ├─ window=37748745 type='con' class='URxvt'
          └─ window=25165827 type='con' class='firefox'
  └─ type='dockarea' name='topdock'
     └─ window=23068674 type='con' class='Polybar'
└─ type='output' name='__i3'
   └─ type='con' name='content'
      └─ type='workspace' name='__i3_scratch'
         └─ type='floating_con'
            └─ window=44040194 type='con' class='mpv'

Sending commands

>>> import i3pie
>>> i3 = i3pie.Connection()
>>> tree = i3.get_tree()

>>> focused = tree.focused_window()
>>> focused.command('move to workspace 2')
CommandReply(success=True)

>>> windows = list(tree.current_workspace().windows())
>>> i3.command('kill', windows)
CommandReply(success=True)

Working with the tree

>>> import i3pie
>>> i3 = i3pie.Connection()
>>> tree = i3.get_tree()

>>> focused = tree.focused_window()
>>> print(focused)
<Container type='con' class='URxvt'>

>>> focused.workspace()
Container(type='workspace', name='1')

>>> focused.output()
Container(type='output', name='HDMI-A-0')

>>> tree = focused.root()
>>> print(tree)
<Container type='root' name='root'>

>>> list(tree.workspaces())
[Container(type='workspace', name='1'), Container(type='workspace', name='2')]

>>> list(tree.workspaces(scratchpad=True))
[Container(type='workspace', name='__i3_scratch'), Container(type='workspace', name='1'), Container(type='workspace', name='2')]

>>> scratchpad = tree.scratchpad()
>>> print(scratchpad)
<Container type='workspace' name='__i3_scratch'>

>>> list(scratchpad.windows())
[Container(window=44040194, type='con', class='mpv')]

>>> print(list(scratchpad.windows()))
[Container(window=44040194, type='con', class='mpv')]

>>> list(tree.outputs())
[Container(type='output', name='HDMI-A-0')]

>>> tree.find_window(fn=lambda con: 'Firefox' in con.name)
Container(window=25165827, type='con', class='firefox')

>>> tree.find_workspace(fn=lambda w: w.num == 2)
Container(type='workspace', name='2')

>>> for win in tree.windows():
...     if win.window_class == 'URxvt':
...             print(win)
...
<Container type='con' class='URxvt'>

>>> list(tree.filter(fn=lambda con: con.is_window and con.window_class == 'mpv'))
[]

>>> list(tree.filter(fn=lambda con: con.is_window and con.window_class == 'mpv', i3=True))
[Container(window=44040194, type='con', class='mpv')]

Getting workspaces and outputs

>>> import i3pie
>>> i3 = i3pie.Connection()
>>> for workspace in i3.get_workspaces():
...     print(workspace.name)
...
1
2

>>> for output in i3.get_outputs():
...     print(output.name)
...
HDMI-A-0
xroot-0

Getting marks and binding modes

>>> import i3pie
>>> i3 = i3pie.Connection()

>>> print(i3.get_marks())
<MarksReply ['a', '_mark']>

>>> print(i3.get_binding_modes())
<BindingModesReply ['open', 'i3', 'default']>

Subscribe to events

from i3pie import Event, Connection

def callback(self, conn, event, data):
    window = data['container']
    if data['change'] != 'new' or not window:
	     return
    window.command('floating enable')

with Connection() as i3:
    i3.subscribe(Event.WINDOW, callback)
    i3.listen()

Examples

Cycling workspaces

import i3pie, argparse

def main(i3):

    parser = argparse.ArgumentParser(description="Cycle i3 workspaces")
    group = parser.add_mutually_exclusive_group(required=True)
    group.add_argument("-next", action="store_true", help="focus next workspace")
    group.add_argument("-prev", action="store_true", help="focus previous workspace")
    args = parser.parse_args()

    tree = i3.get_tree()
    workspaces = list(tree.workspaces())
    current = tree.current_workspace()

    idx = workspaces.index(current)
    direction = 1 if args.next else -1
    target = workspaces[(idx + direction) % len(workspaces)]
    i3.command(f'workspace "{target.name}"')

if __name__ == "__main__":
    with i3pie.Connection() as i3:
        main(i3)

Project details


Release history Release notifications | RSS feed

This version

1.0

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

i3pie-1.0.tar.gz (15.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

i3pie-1.0-py3-none-any.whl (16.6 kB view details)

Uploaded Python 3

File details

Details for the file i3pie-1.0.tar.gz.

File metadata

  • Download URL: i3pie-1.0.tar.gz
  • Upload date:
  • Size: 15.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.25.0 CPython/3.7.4

File hashes

Hashes for i3pie-1.0.tar.gz
Algorithm Hash digest
SHA256 d248df5d22ee4b06cd24ccd6088575bebd0a62be89b9bbabe1d49834ca7ef270
MD5 1a532e3e97fc22d3f16ac9a5db7654f7
BLAKE2b-256 308c65ffa589696e4f5c05fd202d513ce0445a4c05452f08fe80b482a4db2751

See more details on using hashes here.

File details

Details for the file i3pie-1.0-py3-none-any.whl.

File metadata

  • Download URL: i3pie-1.0-py3-none-any.whl
  • Upload date:
  • Size: 16.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.25.0 CPython/3.7.4

File hashes

Hashes for i3pie-1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 107cd2bd8dbcde91964b5a701055748f53eaa5207e35fd7b95141031b7f66764
MD5 2e965c931ee0bf482771c1a1e69d13ca
BLAKE2b-256 da6a73a9c3ca2ef8ce1e17d0b6346202a20b233d948e33183518f48a2d0cb57c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page