-
Notifications
You must be signed in to change notification settings - Fork 1
/
srws.py
executable file
·288 lines (255 loc) · 9.48 KB
/
srws.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#! /usr/bin/env python3
from typing import Any, cast
import os
import re
import sys
import argparse
import json
import time
from socket import socket
from subprocess import Popen
from sway_ipc import (
get_socket,
get_tree,
get_workspaces,
command,
find_con_parent_workspace,
)
class WorkspaceManager:
LAUNCH_TIMOUT_SECONDS = 10
EXECUTABLES = {
'Alacritty': 'alacritty',
}
cwd: str | None = None
layout_rect: dict[str, int] | None = None
workspace_rect: dict[str, int] | None = None
extracted_app_con_ids: list[int] = []
def save_workspace(self, save_path: str, workspace_name: str) -> None:
if os.path.exists(save_path) and not os.path.isfile(save_path):
print(f'The specified path {save_path} is not a file')
sys.exit(1)
sock = get_socket()
tree = get_tree(sock)
search_name_fn = lambda n: n['name'] == workspace_name
search_focused_fn = lambda n: n['focused']
callback_fn = search_name_fn if workspace_name else search_focused_fn
_, _, workspace_con = find_con_parent_workspace(tree, callback_fn)
if workspace_con is None:
print('No workspace found.')
sys.exit(1)
with open(save_path, 'w') as f:
json.dump(workspace_con, f, indent=4)
print(f'Saved JSON to {save_path}')
def run_detached(self, cmd: str) -> None:
Popen(
cmd,
shell=True,
cwd=self.cwd,
stdin=None,
stdout=None,
stderr=None,
close_fds=True,
start_new_session=True,
)
def get_scaled_size(
self, node: dict[str, Any],
) -> tuple[int | None, int | None]:
if self.workspace_rect is None or self.layout_rect is None:
print('No workspace data to compute size!')
return None, None
l_rect = self.layout_rect
w_rect = self.workspace_rect
n_rect = node['rect']
node_height = n_rect['height'] + node['deco_rect']['height']
width_px = int(n_rect['width'] / l_rect['width'] * w_rect['width'])
height_px = int(node_height / l_rect['height'] * w_rect['height'])
return width_px, height_px
def split(self, sock: socket, node: dict[str, Any]) -> None:
layout = node['layout']
orientation = node['orientation']
if orientation == 'none':
orientation = 'horizontal'
res = command(sock, f'split {orientation}')
print('Split result:', res)
if layout != 'none':
res = command(sock, f'layout {layout}')
print('Layout result:', res)
def find_apps(
self,
nodes: list[dict[str, Any]],
apps: list[dict[str, Any]],
) -> list[dict[str, Any]]:
for node in nodes:
if 'app_id' in node:
apps.append(node)
self.find_apps(node['nodes'], apps)
return apps
def count_real_apps(self, sock: socket) -> int:
tree = get_tree(sock)
_, _, workspace = find_con_parent_workspace(
tree, lambda n: n['focused'])
apps = self.find_apps(workspace['nodes'], [])
return len(apps)
def launch(self, sock: socket, node: dict[str, Any]) -> None:
cmd = node['app_id']
if cmd in self.EXECUTABLES:
cmd = self.EXECUTABLES[cmd]
args = node.get('app_args', None)
if args:
cmd += ' ' + args
print('Launching app:', cmd)
apps_count = self.count_real_apps(sock)
self.run_detached(cmd)
new_apps_count = apps_count
start_time = time.time()
while new_apps_count <= apps_count:
time.sleep(0.1)
new_apps_count = self.count_real_apps(sock)
end_time = time.time()
if end_time - start_time > self.LAUNCH_TIMOUT_SECONDS:
print('Launching app takes too long. Exiting.')
sys.exit(1)
tree = get_tree(sock)
real_app_node, _, _ = find_con_parent_workspace(
tree, lambda n: n['focused'])
node['real_app_node'] = real_app_node
def focus(self, sock: socket, node: dict[str, Any]) -> None:
real_app_node = node.get('real_app_node', None)
if real_app_node is None:
print("Error: can't find real app node. Exiting.")
sys.exit(1)
con_id = real_app_node['id']
res = command(sock, f'[con_id={con_id}] focus')
print('Focus result:', res)
def resize_real_app_nodes(
self,
sock: socket,
subtree: dict[str, Any],
) -> None:
for node in subtree['nodes']:
app_node = node.get('app_node', None)
if app_node and 'real_app_node' in app_node:
real_app_node = app_node['real_app_node']
con_id = real_app_node['id']
width, height = self.get_scaled_size(app_node)
res = command(
sock,
f'[con_id={con_id}] resize set {width} px {height} px',
)
print('Resize result:', res)
for node in subtree['nodes']:
self.resize_real_app_nodes(sock, node)
def get_and_remember_app(
self,
node: dict[str, Any],
) -> dict[str, Any] | None:
app_id = node.get('app_id', None)
con_id = node.get('id', None)
if app_id and con_id not in self.extracted_app_con_ids:
self.extracted_app_con_ids.append(con_id)
return node
else:
return None
def find_first_deepest_app_node(
self,
subtree: dict[str, Any],
) -> dict[str, Any] | None:
if not len(subtree['nodes']):
return self.get_and_remember_app(subtree)
node = subtree['nodes'][0]
if len(node['nodes']):
deepest_app_node = self.find_first_deepest_app_node(node)
if deepest_app_node:
return deepest_app_node
else:
app_node = self.get_and_remember_app(node)
if app_node is not None:
return app_node
return self.get_and_remember_app(subtree)
def find_parent_app_node(
self,
node: dict[str, Any],
) -> dict[str, Any] | None:
if 'app_node' in node:
return cast(dict[str, Any], node['app_node'])
elif 'parent' in node:
return self.find_parent_app_node(node['parent'])
else:
return None
def set_parents(self, subtree: dict[str, Any]) -> None:
for node in subtree['nodes']:
node['parent'] = subtree
self.set_parents(node)
def traverse(self, sock: socket, parent: dict[str, Any]) -> Any:
nodes = parent['nodes']
app_nodes = []
for node in nodes:
app_node = self.find_first_deepest_app_node(node)
if app_node:
node['app_node'] = app_node
app_nodes.append(app_node)
if len(app_nodes):
focus_node = self.find_parent_app_node(parent)
if focus_node:
# print('focus', focus_node['app_id'])
self.focus(sock, focus_node)
if 'app_id' not in parent:
# print('split', parent['orientation'])
self.split(sock, parent)
for app_node in app_nodes:
# print('launch', app_node['app_id'])
self.launch(sock, app_node)
for node in nodes:
self.traverse(sock, node)
def load_workspace(self, load_path: str) -> None:
if not os.path.isfile(load_path):
print(f'The specified file {load_path} does not exist')
sys.exit(1)
workspace_con = None
with open(load_path, 'r') as f:
workspace_con = json.load(f)
if workspace_con is None:
print('Could not load JSON')
sys.exit(1)
if len(workspace_con['nodes']) == 0:
print('The workspace has no nodes')
sys.exit(1)
workspace_name = workspace_con['name']
new_name = re.sub('^[0-9]+', '', workspace_name)
sock = get_socket()
workspaces = get_workspaces(sock)
num = 0
current_workspace: dict[str, Any] | None = None
for w in workspaces:
num = w['num'] if num < w['num'] else num
current_workspace = w
if current_workspace is None:
print('Could not find current workspace')
sys.exit(1)
res = command(sock, 'workspace {}'.format(str(num + 1) + new_name))
print('Create workspace result:', res)
self.cwd = os.path.dirname(os.path.realpath(load_path))
self.layout_rect = workspace_con['rect']
self.workspace_rect = cast(dict[str, int], current_workspace['rect'])
self.set_parents(workspace_con)
self.traverse(sock, workspace_con)
self.resize_real_app_nodes(sock, workspace_con)
def main(self) -> None:
parser = argparse.ArgumentParser()
parser.add_argument('--workspace')
parser.add_argument('--save')
parser.add_argument('--load')
args = parser.parse_args()
save_path = args.save
load_path = args.load
workspace_name = args.workspace
if save_path:
self.save_workspace(save_path, workspace_name)
sys.exit(0)
if load_path:
self.load_workspace(load_path)
sys.exit(0)
print('Neither save or load files specified.')
sys.exit(1)
if __name__ == '__main__':
WorkspaceManager().main()