-
Notifications
You must be signed in to change notification settings - Fork 0
/
remapper.py
executable file
·228 lines (183 loc) · 6.42 KB
/
remapper.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
#!/usr/bin/python3
import evdev
import json
import sys
import argparse
from PySide2.QtWidgets import QApplication
import outputdevice
import inputdevice
import remap
import util
import config
import gui
CONFIG = config.Config()
def main():
print("You have the following options:")
print("1.) create\t\tcreates a new remap")
print("2.) edit\t\tedit a remap")
print("3.) delete\t\tdelete a remap")
print("4.) run\t\tjust runs the remapper")
action = input().strip().lower()
if action == "1" or action == "create":
create()
elif action == "2" or action == "edit":
edit()
elif action == "3" or action == "delete":
delete()
elif action == "4" or action == "run":
run()
def create():
device = util.select_evdev_via_console()
print("We are now starting to remap buttons.")
print("Two modes are available:")
print("1.) interactive (unavailable)")
print("2.) manual")
selected_mode = input()
if selected_mode == '1':
interactive_mode(device)
elif selected_mode == '2':
manual_mode(device)
else:
print("wrong input!")
exit(-1)
def interactive_mode(device):
print("not implemented yet!")
def manual_mode(device):
print("You've selected the manual mode for remap creation!")
print("You woll now be asked multiple questions.")
print("--------------------")
print("How do you want to call your new device? (no space or special characters are allowed!)")
device_name = input()
print("You entered:", device_name)
print("--------------------")
print("Do you want to 'grab' the source device?")
print("If you grab the source device, remapper will be the only application recieving events from this device!")
grab_device = util.str2bool(input("grab?"))
print("You entered:", grab_device)
print("--------------------")
print("Now, we're already at the last step!")
print("You will be asked pairs of inputs. The first one is the input of the source device.")
print("The second one, will be the mapped input.")
print("You can enter the integer or the name of the code!")
print("eg: BTN_Y or 308")
event_map = dict()
while True:
source = input("source:")
source = util.value_to_ecode(source)
print("You entered:", source)
dest = input("mapped:")
dest = util.value_to_ecode(dest)
print("You entered:", dest)
event_map[source] = dest
cont = util.str2bool(input("Continue? [y/n]"))
if not cont:
break
print("--------------------")
print("Do you want to change the bus type?")
change_bustype = util.str2bool(input("Change?"))
bustype = evdev.ecodes.BUS_USB
if change_bustype:
print("not implemented yet!")
print("--------------------")
print("Now you can change the vendor, product and version, which should be emulated.")
print("There are also presets available:")
idx = 0
presets = util.uinput_presets()
for preset in presets:
print("[", idx, "]", preset['name'], preset['vendor'],
preset['product'], preset['version'])
idx = idx + 1
print("If you want to apply a preset, enter the number of it.")
print("To copy the values from source, enter -1")
print("To manually enter values, enter -2")
choice = int(input("Your choice?"))
vendor = -1
product = -1
version = -1
if choice == -1:
vendor = device.vendor()
product = device.product()
version = device.version()
elif choice == -2:
vendor = int(input("vendor:"))
product = int(input("product:"))
version = int(input("version:"))
pass
elif choice > -1 and choice < len(presets):
vendor = presets[choice]['vendor']
product = presets[choice]['product']
version = presets[choice]['version']
else:
print("Wrong pick! I'll copy it.")
vendor = device.vendor()
product = device.product()
version = device.version()
name_for_code = dict()
for key, code in evdev.ecodes.ecodes.items():
name_for_code[code] = key
capabilities = {}
for eventA, eventB in event_map.items():
code_name = name_for_code[eventB]
if 'BTN' in code_name or 'KEY' in code_name:
if evdev.ecodes.EV_KEY not in capabilities:
capabilities[evdev.ecodes.EV_KEY] = []
capabilities[evdev.ecodes.EV_KEY].append(eventB)
if 'ABS' in code_name:
if evdev.ecodes.EV_KEY not in capabilities:
capabilities[evdev.ecodes.EV_ABS] = []
capabilities[evdev.ecodes.EV_ABS].append(eventB)
output = outputdevice.OutputDevice(
device_name, capabilities, bustype, vendor, product, version)
remapper = remap.Remap(event_map, device, output, grab_device)
CONFIG.add_remapper(remapper)
def edit():
pass
def delete():
pass
def run():
remappers = CONFIG.get_remappers()
for remapper in remappers:
remapper.start()
def debug():
device = util.select_evdev_via_console()
print(device.info_string())
for event in device.read_loop():
print(event)
def capture():
device = util.select_evdev_via_console()
print(device.info_string())
events = {}
print("Event capture starting... [CTRL+C to stop]")
try:
for event in device.read_loop():
if event.type == 0 and event.code == 0:
continue
if event.type not in events:
events[event.type] = []
events[event.type].append(event.code)
except KeyboardInterrupt:
for key in events:
events[key] = list(set(events[key]))
print("")
print(events)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='remapper')
parser.add_argument('--cli', help='run application in gui mode')
parser.add_argument('--debug', help='check events of a evdev')
parser.add_argument('--run', help='run remapper')
parser.add_argument('--capture', help='capture events from a device')
args = parser.parse_args()
if args.cli:
print("Welcome to remapper!")
print("--------------------")
print("This software allows you to remap a evdev device to your needs!")
print("")
main()
elif args.debug:
debug()
elif args.run:
run()
elif args.capture:
capture()
else:
gui.show()