-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
154 lines (132 loc) · 4.33 KB
/
util.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
import json
import evdev
import inputdevice
def str2bool(v):
return v.lower() in ("y", "yes", "true", "t", "1")
def ecode_to_value(ecode):
if ecode is None:
return None
elif str(ecode).isnumeric():
value = int(ecode)
for key, code in evdev.ecodes.ecodes.items():
if value == code:
return key
else:
if ecode.upper() in evdev.ecodes.ecodes:
return ecode.upper()
return None
def value_to_ecode(value):
if value is None:
return None
elif str(value).isnumeric():
value = int(value)
for key, code in evdev.ecodes.ecodes.items():
if value == code:
return code
else:
if value.upper() in evdev.ecodes.ecodes:
return evdev.ecodes.ecodes[value.upper()]
return None
def ev_to_codes(ev):
if ev == evdev.ecodes.EV_SYN:
return evdev.ecodes.SYN
if ev == evdev.ecodes.EV_KEY:
return evdev.ecodes.KEY
if ev == evdev.ecodes.EV_REL:
return evdev.ecodes.REL
if ev == evdev.ecodes.EV_ABS:
return evdev.ecodes.ABS
if ev == evdev.ecodes.EV_MSC:
return evdev.ecodes.MSC
if ev == evdev.ecodes.EV_SW:
return evdev.ecodes.SW
if ev == evdev.ecodes.EV_LED:
return evdev.ecodes.LED
if ev == evdev.ecodes.EV_SND:
return evdev.ecodes.SND
if ev == evdev.ecodes.EV_REP:
return evdev.ecodes.REP
if ev == evdev.ecodes.EV_FF:
return evdev.ecodes.FF
if ev == evdev.ecodes.EV_PWR:
return {}
if ev == evdev.ecodes.EV_FF_STATUS:
return evdev.ecodes.FF_STATUS
if ev == evdev.ecodes.EV_MAX:
return {}
if ev == evdev.ecodes.EV_CNT:
return {}
if ev == evdev.ecodes.EV_UINPUT:
return {}
if ev == evdev.ecodes.EV_VERSION:
return {}
def bus_types():
types = [
evdev.ecodes.BUS_PCI,
evdev.ecodes.BUS_ISAPNP,
evdev.ecodes.BUS_USB,
evdev.ecodes.BUS_HIL,
evdev.ecodes.BUS_BLUETOOTH,
evdev.ecodes.BUS_VIRTUAL,
evdev.ecodes.BUS_ISA,
evdev.ecodes.BUS_I8042,
evdev.ecodes.BUS_XTKBD,
evdev.ecodes.BUS_RS232,
evdev.ecodes.BUS_GAMEPORT,
evdev.ecodes.BUS_PARPORT,
evdev.ecodes.BUS_AMIGA,
evdev.ecodes.BUS_ADB,
evdev.ecodes.BUS_I2C,
evdev.ecodes.BUS_HOST,
evdev.ecodes.BUS_GSC,
evdev.ecodes.BUS_ATARI,
evdev.ecodes.BUS_SPI,
evdev.ecodes.BUS_RMI,
evdev.ecodes.BUS_CEC,
evdev.ecodes.BUS_INTEL_ISHTP
]
return types
def uinput_presets():
presets = []
presets.append({"name": "Xbox Gamepad", "vendor": 1118,
"product": 654, "version": 272})
return presets
def translate(value, leftMin, leftMax, rightMin, rightMax):
leftSpan = leftMax - leftMin
rightSpan = rightMax - rightMin
valueScaled = float(value - leftMin) / float(leftSpan)
return rightMin + (valueScaled * rightSpan)
def select_evdev_via_console():
print("The following devices are available:")
devices = [evdev.InputDevice(path) for path in evdev.list_devices()]
counter = 0
for device in devices:
input_device = inputdevice.InputDevice(device)
print("[", str(counter), "]", input_device.info_string())
counter = counter + 1
print("Select the device to remap by entering the number in front of the desired entry.")
entered_idx = input()
valid_input = False
device = None
while not valid_input:
if entered_idx.isnumeric():
input_idx = int(entered_idx)
if input_idx < 0 or input_idx > len(devices) - 1:
valid_input = False
print("Input was not valid. Please try again!")
entered_idx = input()
else:
valid_input = True
entered_idx = input_idx
device = inputdevice.InputDevice(devices[entered_idx])
print("Selected device:", device.info_string())
print("Do you want to continue? [y/n]")
confirm = input()
if confirm == 'y':
break
else:
print("Enter new index:")
entered_idx = input()
valid_input = False
continue
return device