-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhypr.py
More file actions
107 lines (95 loc) · 3.07 KB
/
hypr.py
File metadata and controls
107 lines (95 loc) · 3.07 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import os
import subprocess
from collections import OrderedDict
import utils
is_hypr = "HYPRLAND_INSTANCE_SIGNATURE" in os.environ
class Window:
id = None
bbox = None
details = None
def __init__(self, id):
self.id = id
self.details = {}
def __repr__(self):
return f"{{ id: {self.id}, bbox: {self.bbox}, floating: {self.details.get("floating")}, fullscreen: {self.details.get("fullscreen")}, ... }}"
# Get window list, sorted by fullscreen then floating
def get_windows():
windows = {}
window = None
for line in subprocess.check_output(["hyprctl", "clients"]).decode().splitlines():
if line.startswith("Window "):
window = line.split(" ")[1]
windows[window] = Window(window)
continue
if not line.startswith("\t"):
continue
windows[window].details[line.split("\t")[1].split(":")[0]] = line.split(": ")[1]
if line.startswith("\tat:"):
at = True
elif line.startswith("\tsize:"):
at = False
else:
continue
a_str, b_str = line.split(" ")[1].split(",")
a, b = int(a_str), int(b_str)
if at:
x, y = a, b
windows[window].bbox = (x, y)
else:
x, y = windows[window].bbox
w, h = a, b
windows[window].bbox = (x, y, x + w, y + h)
def sort(window):
fullscreen = window.details.get("fullscreen")
floating = window.details.get("floating")
return (
-int(fullscreen) if fullscreen else 0,
-int(floating) if floating else 0,
)
windows = OrderedDict(sorted(windows.items(), key=lambda x: sort(x[1])))
return windows
def get_window_of_bbox(bbox):
current_workspace = subprocess.check_output(["hyprctl", "activeworkspace"]).decode().split(" ")[2]
x1, y1, x2, y2 = bbox
for window in get_windows().values():
if window.details["workspace"].split(" ")[0] != current_workspace:
continue
wx1, wy1, wx2, wy2 = window.bbox
if x2 >= wx1 and x1 <= wx2 and y2 >= wy1 and y1 <= wy2:
return window
window_id = None
bbox = None
size = None
offset = None
def unregister_window():
global window_id, size, offset
window_id = None
size = None
offset = None
def get_area():
global window_id, size, offset
bbox = utils.get_area() # not global
size = (bbox[2] - bbox[0], bbox[3] - bbox[1])
window = get_window_of_bbox(bbox)
if window:
window_id = window.id
offset = (
bbox[0] - window.bbox[0],
bbox[1] - window.bbox[1],
)
else:
unregister_window()
def get_bbox():
global window_id, bbox, offset
if window_id:
window = get_windows().get(window_id)
if window:
x = window.bbox[0] + offset[0]
y = window.bbox[1] + offset[1]
bbox = (x, y, x + size[0], y + size[1])
else:
unregister_window()
return bbox
if __name__ == "__main__":
bbox = utils.get_area()
print(get_window_of_bbox(bbox))