-
Notifications
You must be signed in to change notification settings - Fork 41
/
keymap_img_adjuster.py
executable file
·167 lines (127 loc) · 4.18 KB
/
keymap_img_adjuster.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
#!/usr/bin/env python3
import copy
import argparse
import sys
import oyaml as yaml
# Settings
# To get key numbers, check...
# zmk-nodefree-config/keypos_def/keypos_36keys.h
#
# Increment the numbers from there by 1
pressed_buttons = [
# ('Nav', 35),
# ('Sym', 32),
# ('Num', 36),
# ('Func', 32),
# ('Func', 36),
# ('Adjust', 33),
]
delete_layers = [
# 'QWERTY',
# 'Nav Word',
# 'Sym Word',
# 'Num Word',
# 'Lower',
]
combo_locations = {
# (30, 31, 32): {'a': 'bottom', 'o': 0.3},
# (2, 3, 4): {'a': 'top', 'o': 0.3},
# (11, 12, 13): {'a': 'bottom', 'o': -0.25},
# (21, 22, 23): {'a': 'bottom', 'o': 0.0},
(13, 16): {'k': "Caps\nWord"},
}
def get_keymap_yaml(file_path):
with open(file_path, 'r') as f:
keymap = yaml.safe_load(f)
return keymap
def write_keymap_yaml(file_path, keymap):
with open(file_path, 'w') as f:
yaml.dump(keymap, f)
def adjust_combo(keymap, combo_locations):
keymap = copy.deepcopy(keymap)
for combo in keymap['combos']:
# if len(combo['p']) > 2:
# combo['a'] = 'top'
combo_location = combo_locations.get(tuple(combo['p']), None)
if combo_location is not None:
combo.update(combo_location)
return keymap
def remove_layers(keymap, remove_layers):
keymap = copy.deepcopy(keymap)
for layer in remove_layers:
keymap['layers'].pop(layer, None)
for combo in keymap['combos']:
for layer in remove_layers:
try:
combo['l'].remove(layer)
except (ValueError, KeyError):
pass
return keymap
def highlight_buttons(keymap, buttons):
keymap = copy.deepcopy(keymap)
for button in buttons:
b_layer = button[0]
b_num = button[1] - 1
layer = keymap['layers'].get(b_layer, None)
if layer is None:
print(f"Can't highlight button: {button}: Layer not found")
continue
# The buttons aren't stored as rows that correspond to the rows on the
# keyboard. Instead it seems that they are stored in rows of 10 buttons
# We loop through them until we find the button with the right number.
b_row = None
b_index = None
prev_layer_buttons = 0
for layer_row in layer:
if prev_layer_buttons + len(layer_row) > b_num:
b_row = layer_row
b_index = b_num - prev_layer_buttons
break
else:
prev_layer_buttons += len(layer_row)
button_item = b_row[b_index]
if type(button_item) is dict:
button_item['type'] = 'held'
else:
button_dict = {
't': button_item,
'type': 'held',
}
b_row[b_index] = button_dict
return keymap
def check_undefined_behaviors(keymap):
undefined_behaviors = []
for layer in keymap['layers'].values():
for row in layer:
for key in row:
if type(key) is str and key.startswith('&') and len(key) > 1:
undefined_behaviors.append(key)
# Dedupe list
undefined_behaviors = list(set(undefined_behaviors))
if len(undefined_behaviors) > 0:
print('=================== WARNING ===================')
print('You have the following undefined behaviors:')
for behavior in undefined_behaviors:
print(f' - {behavior}')
print('===============================================')
return len(undefined_behaviors)
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
'keymap_yaml_path',
help='Path to the YAML keymap file.',
)
if len(sys.argv) == 1:
parser.print_help()
exit(1)
args = parser.parse_args()
keymap = get_keymap_yaml(args.keymap_yaml_path)
keymap = adjust_combo(keymap, combo_locations)
keymap = remove_layers(keymap, delete_layers)
keymap = highlight_buttons(keymap, pressed_buttons)
undefined_behaviors = check_undefined_behaviors(keymap)
write_keymap_yaml(args.keymap_yaml_path, keymap)
if(undefined_behaviors > 0):
exit(1)
if __name__ == "__main__":
main()