aboutsummarylogtreecommitdiffstats
path: root/focused_window.py
blob: f93355030309fb358d82fb5b1e2bf6087f7e3d87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from subprocess import run, PIPE
from json import loads, dumps
from sys import argv, exit

def root():
    process = run('i3-msg -t get_tree', shell=True, stdout=PIPE)
    return loads(process.stdout)

def focused_leaf(root):
    children = root['nodes']
    focused = root['focused']

    if not children and focused:
        return root['id']

    for n in children:
        id = focused_leaf(n)
        if id: return id

if __name__ == '__main__':
    STATUS_SUCCESS = 0
    STATUS_FAILURE = -1

    result = focused_leaf(root())
    print(dumps(result))

    exit(STATUS_SUCCESS)