forked from ecraven/g13
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathg13xml2config
More file actions
executable file
·266 lines (232 loc) · 7.48 KB
/
g13xml2config
File metadata and controls
executable file
·266 lines (232 loc) · 7.48 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
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
#!/usr/bin/python
# Converts Logitech XML profiles into a G13 command file.
# Usage: xml2config [-h] [-o <FILE>] [<FILE> ...]
#
# G13 keys L1-L4 selects the first 4 profiles and resets state to 1.
# G13 keys M1-M3 change the current state.
from __future__ import print_function
import sys
import os
import io
import argparse
import xml.etree.ElementTree as et
g13_button_map = {
'G23': 'LEFT',
'G24': 'DOWN',
'G25': 'TOP',
'G26': 'STICK_UP',
'G27': 'STICK_RIGHT',
'G28': 'STICK_DOWN',
'G29': 'STICK_LEFT'
}
keyboard_key_map = {
'LSHIFT': 'LEFTSHIFT',
'LCTRL': 'LEFTCTRL',
'LALT': 'LEFTALT',
'LBRACKET': 'LEFTBRACE',
'RSHIFT': 'RIGHTSHIFT',
'RCTRL': 'RIGHTCTRL',
'RALT': 'RIGHTALT',
'RBRACKET': 'RIGHTBRACE',
'NUM0': 'NUMERIC_0',
'NUM1': 'NUMERIC_1',
'NUM2': 'NUMERIC_2',
'NUM3': 'NUMERIC_3',
'NUM4': 'NUMERIC_4',
'NUM5': 'NUMERIC_5',
'NUM6': 'NUMERIC_6',
'NUM7': 'NUMERIC_7',
'NUM8': 'NUMERIC_8',
'NUM9': 'NUMERIC_9',
'NUM11': 'NUMERIC_11',
'NUM12': 'NUMERIC_12',
'SPACEBAR': 'SPACE',
'TILDE': 'GRAVE',
'ESCAPE': 'ESC',
'NON_US_SLASH': '102ND',
}
keyboard_modifiers = {
'LSHIFT',
'LCTRL',
'LALT',
'RSHIFT',
'RCTRL',
'RALT',
}
me = os.path.splitext(os.path.basename(sys.argv[0]))[0]
outstream = sys.stdout;
def out(*args, **kwargs):
kwargs['file'] = outstream
print(*args, **kwargs)
def load_profiles(db, f, fn='<stdin>'):
def drop_namespaces(el):
i = el.tag.rfind('}')
if i >= 0:
el.tag = el.tag[i+1:]
for child in el:
drop_namespaces(child)
try:
doc = et.parse(f)
except Exception as e:
print('Cannot parse %s: %s' % (fn, str(e)), file=sys.stderr)
return;
root = doc.getroot()
drop_namespaces(root)
if not root.tag == 'profiles':
print(fn + ' is not a Logitech G13 XML file', file=sys.stderr)
return
for profile in root.findall('profile'):
db.append(profile)
def keyboard_keys(macro):
def stroke2keys(stroke):
key = None
modifier = set()
for el in stroke:
if el.tag == 'key':
key = el.get('value')
elif el.tag == 'modifier':
modifier.add(el.get('value'))
# Transform into a multikey.
if key is None:
return []
keys = list(modifier)
keys.append(key)
keys = [keyboard_key_map.get(x, x) for x in keys]
return keys + ['-%s' % x for x in reversed(keys)]
def multikey2keys(multikey):
def press(key):
keys.append(keyboard_key_map.get(key, key))
down.add(key)
def release(key):
keys.append('-' + keyboard_key_map.get(key, key))
down.discard(key)
keys = []
down = set()
dirty = {}
for el in multikey.findall('key'):
key = el.get('value')
up = el.get('direction') == 'up'
if up:
if key in keyboard_modifiers:
dirty[key] = key in down
elif not key in keyboard_modifiers:
if key in down:
release(key)
for m in dirty:
if dirty[m]:
release(m)
dirty = {}
press(key)
else:
if not key in down:
press(key)
dirty[key] = False
for key in list(down):
release(key)
return keys
keys = []
for el in macro:
if el.tag == 'keystroke':
keys += stroke2keys(el)
elif el.tag == 'multikey':
keys += multikey2keys(el)
while keys and keys[-1][0] == '-':
keys.pop()
return keys
# Parse command line.
parser = argparse.ArgumentParser(me,
description='Convert G13 XML profiles to Linux configuration file')
parser.add_argument('-o', metavar='<FILE>', help='output file name')
parser.add_argument('file', nargs='*', metavar='<FILE>',
help='input file name(s)')
args = parser.parse_args()
# Load XML input files.
db = et.XML('<profiles />')
if not args.file:
load_profiles(db, sys.stdin)
else:
for file in args.file:
with open(file, 'r') as f:
load_profiles(db, f, file)
# Get all usable profiles.
profiles = []
for profile in db:
profile_guid = profile.get('guid')
macros = profile.find('macros')
assignments = profile.find("assignments[@devicecategory='Logitech.Gaming.LeftHandedController']")
if profile_guid and not (macros is None or assignments is None):
profiles.append(profile)
# Redirect output if needed.
if args.o:
outstream = open(args.o, 'w')
# Output configuration preamble.
out('delete profile *')
out('delete key *')
out('delete zone *')
out('profile default')
out('stickzone add STICK_UP')
out('stickzone bounds STICK_UP 0.0 0.0 1.0 0.3')
out('stickzone add STICK_DOWN')
out('stickzone bounds STICK_DOWN 0.0 0.7 1.0 1.0')
out('stickzone add STICK_LEFT')
out('stickzone bounds STICK_LEFT 0.0 0.0 0.3 1.0')
out('stickzone add STICK_RIGHT')
out('stickzone bounds STICK_RIGHT 0.7 0.0 1.0 1.0')
out('stickmode KEYS')
# Bind the first four profiles to L1-L4 keys.
for n, profile in enumerate(profiles):
if n <= 4:
name = profile.get('name')
if name:
name = ' # ' + name
out('bind L%u !profile %s-1%s' % (n + 1, profile.get('guid'), name))
# Generate each profile.
for profile in profiles:
name = profile.get('name')
description = profile.find('description')
if not description is None:
description = ''.join(description.itertext()).strip()
else:
description = ''
description = description.splitlines()
out('')
if name or description:
if name:
out('#\t' + name)
if description:
out('#')
if description:
for l in description:
out('# ' + l)
guid = profile.get('guid')
macros = profile.find('macros')
assignments = profile.find("assignments[@devicecategory='Logitech.Gaming.LeftHandedController']")
# Generate each state.
for state in '123':
out('profile ' + guid + '-' + state)
# Generate key bindings.
active = set()
asgs = assignments.findall("assignment[@shiftstate='%s']" % state)
asgs.sort(key=lambda x: (x.get('contextid')[1:],
x.get('backup') == 'true'))
for asg in asgs:
button = asg.get('contextid')
macro = macros.find("macro[@guid='%s']" % asg.get('macroguid'))
if not (macro is None or button in active):
keys = keyboard_keys(macro)
if keys:
active.add(button)
name = macro.get('name')
if name:
name = ' # ' + name
button = g13_button_map.get(button, button)
out('bind %s %s%s' %
(button, '+'.join(str(x) for x in keys), name))
# Generate state change action bindings on M1-M3.
for st in '123':
out('bind M%s !profile %s-%s' % (st, guid, st))
out('profile default')
# Select first profile in state 1.
for profile in profiles:
out('\nprofile %s-1' % profile.get('guid'))
break