Skip to content

🐍 An improved Python library to control i3wm and sway.

License

Notifications You must be signed in to change notification settings

smurfix/asway

 
 

Repository files navigation

asway

An async Python library to control sway (and i3wm).

About

Sway's interprocess communication (or IPC) is the interface sway uses to receive commands from client applications such as swaymsg. It also features a publish/subscribe mechanism for notifying interested parties of window manager events.

asway is an asynchronous Python library for controlling the window manager. This project is intended to be useful for general scripting, and for applications that interact with the window manager like status line generators, notification daemons, and window pagers.

If you have an idea for a script to extend asway, you can add your script to the examples folder.

For details on how to use the library, see the reference documentation.

asway is based on asway. It was forked because structured async code requires a couple of modifications (and enables some improvements) that didn't work well with the original code base.

Installation

asway is on PyPI.

pip3 install asway

Example

from asway import Connection, Event
import anyio

# Create the Connection object that can be used to send commands and subscribe
# to events.
async def main():
  async with Connection() as wm:

    # Print the name of the focused window
    focused = (await wm.get_tree()).find_focused()
    print('Focused window %s is on workspace %s' %
        (focused.name, focused.workspace().name))

    # Query the ipc for outputs. The result is a list that represents the parsed
    # reply of a command like `swaymsg -t get_outputs`.
    outputs = await wm.get_outputs()

    print('Active outputs:')
    for output in filter(lambda o: o.active, outputs):
        print(output.name)

    # Send a command to be executed synchronously.
    await wm.command('focus left')

    # Take all fullscreen windows out of fullscreen
    for container in (await wm.get_tree()).find_fullscreen():
        container.command('fullscreen')

    # Print the names of all the containers in the tree
    print('Containers:')
    root = await wm.get_tree()
    print(root.name)
    for con in root:
        print(con.name)

    # Define a callback to be called when you switch workspaces.
    def on_workspace_focus(e):
        if e.current:
            print('Windows on this workspace:')
            for w in e.current.leaves():
                print(w.name)

    # Dynamically name your workspaces after the current window class
    async def on_window_focus(e):
        focused = e.container
        breakpoint()
        print("Focus:", focused.window_class)
        ws = focused.workspace()
        if ws is not None:
            ws_name = "%s:%s" % (ws.num, focused.window_class)
            await wm.command('rename workspace to "%s"' % ws_name)

    # Subscribe to events
    wm.on(Event.WORKSPACE_FOCUS, on_workspace_focus)
    wm.on(Event.WINDOW_FOCUS, on_window_focus)

    # just wait for events to come in.
    import math
    await anyio.sleep(math.inf)

# You can use the asyncio backend if you must, but I recommend Trio
# if you don't depend on libraries that are asyncio-only.
anyio.run(main, backend="trio")

Debug Logging

asway uses the standard logging module under the asway namespace.

import logging
logging.basicConfig(level=logging.DEBUG)

Contributing

Development happens on Github. Please feel free to report bugs, request features or add examples by submitting a pull request.

License

This work is available under a BSD-3-Clause license (see LICENSE).

Copyright © 2015, Tony Crisci Copyright © 2023, Matthias Urlichs (and contributors)

About

🐍 An improved Python library to control i3wm and sway.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 97.3%
  • Dockerfile 1.3%
  • Other 1.4%