altdesktop / i3ipc-python

🐍 An improved Python library to control i3wm and sway.
http://i3ipc-python.readthedocs.io
BSD 3-Clause "New" or "Revised" License
858 stars 108 forks source link

aio container.workspace() always returns None #176

Open wonkalous opened 3 years ago

wonkalous commented 3 years ago

Here is a repro:

#!/usr/bin/env python3

import asyncio
import logging

from i3ipc.aio import Connection

SOCKET_FILE = '/tmp/.i3-cycle-focus.sock'

class FocusWatcher:
    def __init__(self):
        self.i3 = None

    async def connect(self):
        self.i3 = await Connection().connect()
        self.i3.on('window::focus', self.on_window_focus)

    async def on_window_focus(self, i3conn, event):
        logging.info('got window focus event')
        ws = await event.container.workspace().name  # ***always returns None***

    async def run(self):
        async def handle_switch(reader, writer):
            data = await reader.read(1024)
            logging.info('received data: {}').format(data)

        server = await asyncio.start_unix_server(handle_switch, SOCKET_FILE)
        await server.serve_forever()

async def run_server():
    focus_watcher = FocusWatcher()
    await focus_watcher.connect()
    await focus_watcher.run()

if __name__ == '__main__':
    logging.basicConfig(level=logging.DEBUG)
    asyncio.run(run_server())
acrisci commented 3 years ago

The way workspace() works is it walks up the tree and finds a workspace. The way the i3ipc works, the event container does not contain the information of the tree. It's just the container without parent information.

To fix this, get the tree with get_tree(), find the container by id, and then call workspace() on that.

To fix the issue, I think we should throw an error when workspace information is not present within the tree such as in events to avoid confusion.

wonkalous commented 3 years ago

Interesting. Using get_tree was my original plan, but that was also not working, as in https://github.com/altdesktop/i3ipc-python/issues/175 .