|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +""" |
| 4 | +focus-next-visible.py - cycles input focus between visible windows on workspace |
| 5 | +
|
| 6 | +- requires the `xprop` utility |
| 7 | +
|
| 8 | + Usage: |
| 9 | +
|
| 10 | + # focus next visible window |
| 11 | + bindsym $mod+n exec --no-startup-id focus-next-visible.py |
| 12 | +
|
| 13 | + # focus previous visible window |
| 14 | + bindsym $mod+Shift+n exec --no-startup-id focus-next-visible.py reverse |
| 15 | +
|
| 16 | +
|
| 17 | +https://faq.i3wm.org/question/6937/move-focus-from-tabbed-container-to-win... |
| 18 | +
|
| 19 | +""" |
| 20 | + |
| 21 | +from sys import argv |
| 22 | +from itertools import cycle |
| 23 | +from subprocess import check_output |
| 24 | + |
| 25 | +import i3ipc |
| 26 | + |
| 27 | + |
| 28 | +def get_windows_on_ws(conn): |
| 29 | + return filter(lambda x: x.window, |
| 30 | + conn.get_tree().find_focused().workspace().descendents()) |
| 31 | + |
| 32 | + |
| 33 | +def find_visible_windows(windows_on_workspace): |
| 34 | + visible_windows = [] |
| 35 | + for w in windows_on_workspace: |
| 36 | + |
| 37 | + try: |
| 38 | + xprop = check_output(['xprop', '-id', str(w.window)]).decode() |
| 39 | + except FileNotFoundError: |
| 40 | + raise SystemExit("The `xprop` utility is not found!" |
| 41 | + " Please install it and retry.") |
| 42 | + |
| 43 | + if '_NET_WM_STATE_HIDDEN' not in xprop: |
| 44 | + visible_windows.append(w) |
| 45 | + |
| 46 | + return visible_windows |
| 47 | + |
| 48 | + |
| 49 | +if __name__ == '__main__': |
| 50 | + |
| 51 | + conn = i3ipc.Connection() |
| 52 | + |
| 53 | + visible_windows = find_visible_windows(get_windows_on_ws(conn)) |
| 54 | + |
| 55 | + if len(argv) > 1 and argv[1] == "reverse": |
| 56 | + cycle_windows = cycle(reversed(visible_windows)) |
| 57 | + else: |
| 58 | + cycle_windows = cycle(visible_windows) |
| 59 | + |
| 60 | + for window in cycle_windows: |
| 61 | + if window.focused: |
| 62 | + focus_to = next(cycle_windows) |
| 63 | + conn.command('[id="%d"] focus' % focus_to.window) |
| 64 | + break |
0 commit comments