### Install i3ipc-python Source: https://github.com/altdesktop/i3ipc-python/blob/master/docs/index.rst Installs the i3ipc-python library using pip. This command should be run in a terminal environment. ```bash pip3 install i3ipc ``` -------------------------------- ### i3ipc-python: Basic Window Management and Event Handling Source: https://github.com/altdesktop/i3ipc-python/blob/master/README.rst Demonstrates how to connect to i3, retrieve window information, send commands, and subscribe to events like workspace focus and window focus changes. It includes examples of finding focused windows, listing outputs, changing fullscreen status, and renaming workspaces based on focused window class. ```python from i3ipc import Connection, Event # Create the Connection object that can be used to send commands and subscribe # to events. i3 = Connection() # Print the name of the focused window focused = i3.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 `i3-msg -t get_outputs`. outputs = i3.get_outputs() print('Active outputs:') for output in filter(lambda o: o.active, outputs): print(output.name) # Send a command to be executed synchronously. i3.command('focus left') # Take all fullscreen windows out of fullscreen for container in i3.get_tree().find_fullscreen(): container.command('fullscreen') # Print the names of all the containers in the tree root = i3.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(self, e): # The first parameter is the connection to the ipc and the second is an object # with the data of the event sent from i3. 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 def on_window_focus(i3, e): focused = i3.get_tree().find_focused() ws_name = "%s:%s" % (focused.workspace().num, focused.window_class) i3.command('rename workspace to "%s"' % ws_name) # Subscribe to events i3.on(Event.WORKSPACE_FOCUS, on_workspace_focus) i3.on(Event.WINDOW_FOCUS, on_window_focus) # Start the main loop and wait for events to come in. i3.main() ``` -------------------------------- ### Send Commands to i3 Source: https://github.com/altdesktop/i3ipc-python/blob/master/docs/index.rst Executes commands directly to the i3 window manager, similar to using 'i3-msg'. Examples include switching workspaces, focusing windows, and closing them. ```python await i3.command('workspace 5') await i3.command('focus left') await i3.command('kill') ``` -------------------------------- ### i3ipc-python: Asyncio Window Management and Event Handling Source: https://github.com/altdesktop/i3ipc-python/blob/master/README.rst Provides an asynchronous interface for controlling i3wm and sway using Python's asyncio. It demonstrates connecting to the IPC, retrieving workspaces, subscribing to window events, and running the main event loop asynchronously. ```python from i3ipc.aio import Connection from i3ipc import Event import asyncio async def main(): def on_window(self, e): print(e) c = await Connection(auto_reconnect=True).connect() workspaces = await c.get_workspaces() c.on(Event.WINDOW, on_window) await c.main() asyncio.get_event_loop().run_until_complete(main()) ``` -------------------------------- ### Subscribe to Window Manager Events Source: https://github.com/altdesktop/i3ipc-python/blob/master/docs/index.rst Sets up event handlers for various i3 events, such as new windows or workspace focus changes. The library then enters a main loop to process these events. ```python from i3ipc import Event def on_new_window(i3, e): print(f'a new window opened: {e.container.name}') def on_workspace_focus(i3, e): print(f'workspace just got focus: {e.current.name}') i3.on(Event.WINDOW_NEW, on_new_window) i3.on(Event.WORKSPACE_FOCUS, on_workspace_focus) await i3.main() ``` -------------------------------- ### Query Workspaces and Outputs Source: https://github.com/altdesktop/i3ipc-python/blob/master/docs/index.rst Retrieves a list of all workspaces and outputs currently managed by i3. It demonstrates how to access properties like workspace names and output names. ```python workspaces = await i3.get_workspaces() outputs = await i3.get_outputs() for workspace in workspaces: print(f'workspace: {workspace.name}') for output in outputs: print(f'output: {output.name}') ``` -------------------------------- ### Connect to i3 Window Manager Source: https://github.com/altdesktop/i3ipc-python/blob/master/docs/index.rst Establishes a connection to the i3 window manager's IPC interface using the asynchronous Connection class. This is the primary entry point for interacting with the library. ```python from i3ipc.aio import Connection i3 = await Connection().connect() ``` -------------------------------- ### i3ipc-python: Enable Debug Logging Source: https://github.com/altdesktop/i3ipc-python/blob/master/README.rst Shows how to enable debug logging for the i3ipc-python library using Python's standard logging module. This is useful for troubleshooting and understanding the library's internal operations. ```python import logging logging.basicConfig(level=logging.DEBUG) ``` -------------------------------- ### Interact with Window Layout Tree Source: https://github.com/altdesktop/i3ipc-python/blob/master/docs/index.rst Accesses and manipulates the i3 layout tree using the Con class. It shows how to find focused windows, windows by class, and iterate through containers on a workspace. ```python # get_tree() returns the root container tree = await i3.get_tree() # get some information about the focused window focused = tree.find_focused() print(f'Focused window: {focused.name}') workspace = focused.workspace() print(f'Focused workspace: {workspace.name}') # focus firefox and set it to fullscreen mode ff = tree.find_classed('Firefox')[0] await ff.command('focus') await ff.command('fullscreen') # iterate through all the container windows (or use tree.leaves() for just # application windows) for container in workspace: print(f'On the focused workspace: {container.name}') ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.