-
Notifications
You must be signed in to change notification settings - Fork 1
/
i3-automark.py
executable file
·150 lines (134 loc) · 4.65 KB
/
i3-automark.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
#!/usr/bin/env python3
import socket
import struct
import json
import subprocess
import sys
import time
import itertools
from collections import deque
class SocketClosedException(Exception): pass
MARKS = 'QWERTYUIOP'
COMMANDS = [
'run_command',
'get_workspaces',
'subscribe',
'get_outputs',
'get_tree',
'get_marks',
'get_bar_config',
'get_version',
'get_binding_modes',
'send_tick',
]
EVENTS = [
'workspace',
'output',
'mode',
'window',
'barconfig_update',
'binding',
'shutdown',
'tick',
]
def recv(sock, length):
buf = b''
while length:
data = sock.recv(length)
if not data:
break
buf += data
length -= len(data)
return buf
msg_backlog = deque()
def send_msg(sock, command, payload=''):
payload = payload.encode('utf-8')
msg = struct.pack('II', len(payload), COMMANDS.index(command))
sock.sendall(b'i3-ipc' + msg + payload)
while True:
type, response = read_msg(sock)
if type == command:
if command == 'run_command':
response = response[0]
if isinstance(response, dict) and not response.get('success', True):
raise Exception(response.get('error'))
return response
msg_backlog.append((type, response))
def read_msg(sock):
reply = recv(sock, 14)
if not reply:
raise SocketClosedException
length, type = struct.unpack('ii', reply[6:])
if type & 0x80000000:
type = EVENTS[type & 0x7fffffff]
else:
type = COMMANDS[type]
return type, json.loads(recv(sock, length))
def refresh_all_marks(sock, marks, focused_only=False):
tree = send_msg(sock, 'get_tree')
workspaces = send_msg(sock, 'get_workspaces')
if focused_only:
send_msg(sock, 'run_command', 'unmark')
focused_ws = next((w['name'] for w in workspaces if w['focused']), None)
if focused_ws is None:
return
windows = get_windows(tree, focused_ws)
else:
# sort left to right, top to bottom
workspaces = sorted(workspaces, key=lambda w: (w['rect']['y'], w['rect']['x']))
visible_ws = [w['name'] for w in workspaces if w['visible']]
windows = itertools.chain.from_iterable(get_windows(tree, workspace) for workspace in visible_ws)
for mark, id in zip(marks, windows):
try:
send_msg(sock, 'run_command', '[con_id="{}"] mark --replace {}'.format(id, mark))
except Exception as e:
if str(e) in {"Given criteria don't match a window", "No matching node."}:
pass
else:
raise
def get_windows(node, workspace):
if node['window_rect']['height'] and node['window_rect']['width']:
yield node['id']
elif node['type'] == 'dockarea':
return
elif node['type'] == 'workspace' and node['name'] != workspace:
return
else:
for child in node['nodes'] + node['floating_nodes']:
yield from get_windows(child, workspace)
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--focused-only', action='store_true', help='Only mark windows on the focused workspace')
parser.add_argument('--wm', choices=('i3', 'sway'), default='i3', help='Default: %(default)s')
parser.add_argument('marks', nargs='?', default=MARKS, help='Default: %(default)s')
args = parser.parse_args()
events = ['workspace', 'window']
if args.wm == 'i3':
# sway does not support output
events.append('output')
no_socket_counter = 0
while True:
try:
socketpath = subprocess.check_output([args.wm, '--get-socketpath']).rstrip(b'\n')
with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as sock:
sock.connect(socketpath)
no_socket_counter = 0
send_msg(sock, 'subscribe', json.dumps(events))
refresh_all_marks(sock, args.marks, args.focused_only)
while True:
if msg_backlog:
type, event = msg_backlog.popleft()
else:
type, event = read_msg(sock)
if type in {'workspace', 'output'} or (type == 'window' and event['change'] in {'new', 'close', 'move', 'floating'}):
refresh_all_marks(sock, args.marks, args.focused_only)
except SocketClosedException:
pass
except FileNotFoundError:
no_socket_counter += 1
if no_socket_counter > 10:
raise
time.sleep(0.1)
except KeyboardInterrupt:
break