-
Notifications
You must be signed in to change notification settings - Fork 0
/
deskr.py
262 lines (209 loc) · 8.13 KB
/
deskr.py
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
"""Main module implementing deskr and its CLI."""
import os
import re
import time
from argparse import ArgumentParser
from concurrent.futures import ThreadPoolExecutor
from functools import partial
import yaml
from util import collect, pp, ps_tree, ps_tree_query, retry, sh
@collect(list)
def get_connected_monitors():
xrandr_output = sh("xrandr --query").split("\n")
for line in xrandr_output:
if " connected " not in line:
continue
monitor = {"_xrandr_raw": line}
monitor["labels"] = ["primary" if " primary " in line else "secondary"]
(width, height) = map(int, re.findall(r"\d+x\d+", line)[0].split("x"))
(x, y) = map(int, re.findall(r"\+\d+", line))
monitor.update({"width": width, "height": height, "x": x, "y": y})
yield monitor
@collect(dict)
def get_monitors_by_label(monitors):
for monitor in monitors:
for label in monitor["labels"]:
yield label, monitor
def to_pixels(value, relative_value):
if type(value) == int:
return value
if type(value) != str:
raise ValueError(f"Invalid pixel value specifier: {value}")
if "%" in value:
fraction = float(value[:-1]) / 100
return int(relative_value * fraction)
return int(value)
def get_absolute_location(window_spec, monitors_by_label):
monitor = monitors_by_label[window_spec["monitor"]]
position = window_spec["position"]
top = to_pixels(position[0], monitor["height"])
right = to_pixels(position[1], monitor["width"])
bottom = to_pixels(position[2], monitor["height"])
left = to_pixels(position[3], monitor["width"])
return {
"x": monitor["x"] + left,
"y": monitor["y"] + top,
"width": monitor["width"] - left - right,
"height": monitor["height"] - top - bottom,
}
def get_matching_window(window_spec):
open_windows = None
locator = window_spec["window_locator"]
title = locator.get("title")
title_regex = locator.get("title_regex")
if title_regex or title:
open_windows = get_open_windows()
for window in open_windows:
if (title_regex and re.search(title_regex, window["title"])) or (
title and window["title"] == title
):
return window
pstree_query = locator.get("pstree")
if pstree_query:
try:
window_process = next(ps_tree_query(ps_tree(), **pstree_query))
except StopIteration:
return None
if not window_process:
return None
if not open_windows:
open_windows = get_open_windows()
for window in open_windows:
if window["pid"] == window_process["pid"]:
return window
return None
def wait_for_window(window_spec):
@retry(error_message=f"Timed out waiting for window to be spawned: {window_spec}")
def get_window():
return get_matching_window(window_spec)
return get_window()
@retry(predicate=bool)
def poll(f):
return f()
def check_preconditions(preconditions):
env = {key: globals()[key] for key in ["sh", "poll", "time"]}
if not preconditions:
return
for precondition in preconditions:
if not eval(precondition, env):
raise (f"Precondition not met: {precondition}")
def execute_spec(window_spec, monitors_by_label):
window = get_matching_window(window_spec)
if not window:
command_preconditions = window_spec.get("command_preconditions")
check_preconditions(command_preconditions)
sh(window_spec["command"], print_command=True, disown=True)
window = wait_for_window(window_spec)
rect = get_absolute_location(window_spec, monitors_by_label)
wmctrl_location = f'0,{rect["x"]},{rect["y"]},{rect["width"]},{rect["height"]}'
reposition_preconditions = window_spec.get("reposition_preconditions")
check_preconditions(reposition_preconditions)
sh(f'wmctrl -ia {window["id"]}', print_command=True)
sh(f'wmctrl -i -r {window["id"]} -e {wmctrl_location}', print_command=True)
@collect(dict)
def get_running_commands_by_pid():
for line in sh("ps -aux").split("\n"):
if "COMMAND" in line:
command_index = line.index("COMMAND")
continue
if not line.strip():
continue
pid = int(re.search(r".*?\s+(\d+)", line).group(1))
command = line[command_index:]
yield pid, command
@collect(list)
def get_open_windows():
# TODO: add -p flag to get PID and avoid xprop invocation.
# TODO: use xwininfo -tree -root to get app title.
lines = sh("wmctrl -l").split("\n")
hostname = sh("hostname").strip()
commands_by_pid = get_running_commands_by_pid()
for line in lines:
if not line.strip():
continue
title = re.sub(f".*? {hostname} ", "", line)
window_id = re.findall(r"^0x[^\s]+", line)[0]
xprop = sh(f"xprop -id {window_id}")
pid = int(re.search(r"_NET_WM_PID\(CARDINAL\) = (\d+)", xprop).group(1))
yield {
"_wmctrl_raw": line,
"_xprop_raw": xprop,
"title": title,
"id": window_id,
"pid": pid,
# pylint: disable=unsubscriptable-object
"command": commands_by_pid[pid],
}
def layout(config_path):
monitors_by_label = get_monitors_by_label(get_connected_monitors())
with open(config_path, "r") as config_file:
config = yaml.load(config_file, Loader=yaml.FullLoader)
with ThreadPoolExecutor(max_workers=16) as executor:
for window_spec in config:
executor.submit(partial(execute_spec, window_spec, monitors_by_label))
def pstree():
pp(ps_tree())
def test_locators(config_path):
with open(config_path, "r") as config_file:
config = yaml.load(config_file, Loader=yaml.FullLoader)
for spec in config:
tree = ps_tree()
print("Testing locator:")
pp(spec["window_locator"])
try:
result = next(ps_tree_query(tree, **spec["window_locator"]))
if result["children"]:
result["children"] = "[...]"
except StopIteration:
result = None
pp(result)
def create_shortcut(config_path, icon_url=None):
icon_url = (
icon_url
or "https://icons.iconarchive.com/icons/vexels/office/512/desktop-icon.png"
)
deskr_path = os.path.abspath(__file__)
absolute_config_path = os.path.abspath(config_path)
name = os.path.basename(config_path).replace(".deskr.yaml", "")
desktop_folder = os.path.abspath(
os.path.join(os.path.expanduser("~"), ".local/share/applications")
)
downloaded_icon_path = f"{desktop_folder}/{name}_icon.png"
sh(f"wget -qO- {icon_url} > {downloaded_icon_path}")
desktop_contents = f"""#!/usr/bin/env xdg-open
[Desktop Entry]
Version=1.0
Terminal=false
Type=Application
Name={name}
Exec=/usr/bin/env python3 {deskr_path} layout {absolute_config_path}
Icon={downloaded_icon_path}
"""
desktop_path = os.path.join(desktop_folder, name + ".desktop")
with open(desktop_path, "w") as desktop_file:
print(f"Writing {desktop_path}")
desktop_file.write(desktop_contents)
sh(f"sudo chmod +x {desktop_path}", print_command=True)
if __name__ == "__main__":
parser = ArgumentParser()
subparsers = parser.add_subparsers(dest="subparser")
layout_parser = subparsers.add_parser("layout")
layout_parser.add_argument(
"config_path", type=str, help="Path to layout config (YAML)",
)
create_shortcut_parser = subparsers.add_parser("create-shortcut")
create_shortcut_parser.add_argument(
"config_path", type=str, help="Path to layout config (YAML)",
)
create_shortcut_parser.add_argument(
"-i", "--icon-url", help="Url for the icon to use"
)
pstree_parser = subparsers.add_parser("pstree")
test_locators_parser = subparsers.add_parser("test-locators")
test_locators_parser.add_argument(
"config_path", type=str, help="Path to layout config (YAML)",
)
kwargs = vars(parser.parse_args())
globals()[kwargs.pop("subparser").replace("-", "_")](
**{k.replace("-", "_"): v for k, v in kwargs.items()}
)