A Python library to interact with the Sway window manager via its IPC interface.
- Query the current layout tree of Sway.
- Get information about views (windows), outputs, inputs, and seats.
- Run Sway commands programmatically.
- Subscribe to real-time events like workspace changes, view focus, output updates, and more.
- Python 3.6+
- Running inside a Sway session
install from source:
git clone https://github.com/yourusername/pysway.git
cd pysway
pip install -e .
from pysway.ipc import SwayIPC
sway = SwayIPC()
tree = sway.get_tree()
print(tree)
focused = sway.get_focused_view()
if focused:
print(f"Focused View ID: {focused['id']}")
print(f"Title: {focused.get('name')}")
views = sway.list_views()
for v in views:
print(f"ID: {v['id']}, Title: {v['name']}, App ID: {v.get('app_id', 'N/A')}")
Run the built-in event monitor:
python -m scripts.event_monitor
Or use it in code:
from scripts.event_monitor import SwayIPC
ipc = SwayIPC()
ipc.subscribe(["workspace", "window"]) # or ipc.subscribe() to watch all events
while True:
event = ipc.read_next_event()
if event:
print("Event received:", event)
Method
Description
get_tree()
Get full layout tree
get_focused_view()
Get currently focused window
list_views()
List all open views
run_command(cmd)
Execute raw Sway command
list_outputs()
List connected displays
focus_output(id)
Focus an output by ID
subscribe(events)
Subscribe to one or more IPC events
You can subscribe to these event types:
workspacewindowoutputmodebarconfig_updatebindingshutdowntickbar_state_updateinput
Contributions are welcome! Please read our contribution guidelines before submitting a PR.
This project is licensed under the MIT License.