aboutsummarylogtreecommitdiffstats
path: root/i3-lru-daemon.py
blob: 58728b3682ceef261580c3136ebc9adc802a7064 (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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python3
import os
import sys
from pathlib import Path

import i3ipc

# Where we store the LRU order
CACHE_FILE = Path.home() / ".cache" / "i3-lru-order"

# Global list that tracks windows in MRU order (front = most recently used)
lru_list = []


def save_lru_list():
    """Save the current LRU list to a file so the 'i3-lru' script can read it."""
    with open(CACHE_FILE, "w", encoding="utf-8") as f:
        for wid in lru_list:
            f.write(f"{wid}\n")


def load_lru_list():
    """Load LRU list from file (if it exists) when daemon starts."""
    global lru_list
    if CACHE_FILE.is_file():
        with open(CACHE_FILE, "r", encoding="utf-8") as f:
            lru_list = [line.strip() for line in f if line.strip()]


def update_lru(window_id):
    """Move window_id to front of the lru_list."""
    # Remove if already present
    try:
        lru_list.remove(window_id)
    except ValueError:
        pass
    # Insert at front
    lru_list.insert(0, window_id)
    save_lru_list()


def on_window_focus(i3, event):
    # event.container is the newly-focused container
    focused_id = str(event.container.id)
    update_lru(focused_id)


def main():
    # Start up i3 connection
    i3 = i3ipc.Connection()

    # Load any existing list from file
    load_lru_list()

    # Subscribe to focus events
    i3.on("window::focus", on_window_focus)

    # Main loop
    i3.main()


if __name__ == "__main__":
    main()