-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwooting_mouse.py
255 lines (218 loc) · 7.24 KB
/
wooting_mouse.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
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
"""
Script to use Wooting Gamepad as Mouse
"""
import asyncio
import collections
import signal
import sys
import math
import colorsys
import time
import d0da.device_linux as d0da_device
import d0da.d0da_feature
import d0da.d0da_report
import evdev
import pyudev
import hsluv
class Mouse:
"""
Virtual Mouse
"""
def __init__(self, state, device):
self.ui_mouse = evdev.UInput(
evdev.util.find_ecodes_by_regex(
r"(REL_X|REL_Y|REL_WHEEL|REL_WHEEL_HI_RES|REL_HWHEEL|"
r"REL_HWHEEL_HI_RES|BTN_RIGHT|BTN_MIDDLE|BTN_LEFT|"
r"KEY_CAPSLOCK|KEY_LEFTCTRL|KEY_LEFTALT|BTN_BACK|BTN_FORWARD)$"
),
name="Wooting Virtual Mouse for Gamepad",
)
self.state = state
self.device = device
self.enabled = False
self.enable_event = asyncio.Event()
def handle(self, from_event, to_event):
"""
Handle event, translate analog keypress to relative movement
"""
value = self.state[evdev.ecodes.ecodes["EV_ABS"]][from_event]
self.ui_mouse.write(
evdev.ecodes.ecodes["EV_REL"],
to_event,
int(math.tan(value * (math.pi / 2) / 34000) * 10),
)
def write(self, typ: int, code: int, value: int) -> None:
"""
Write event
"""
self.ui_mouse.write(typ, code, value)
async def run(self) -> None:
"""
Main loop
"""
while True:
if self.enabled:
await asyncio.sleep(0.05)
else:
await self.enable_event.wait()
self.handle(evdev.ecodes.ecodes["ABS_X"], evdev.ecodes.ecodes["REL_X"])
self.handle(evdev.ecodes.ecodes["ABS_Y"], evdev.ecodes.ecodes["REL_Y"])
self.handle(
evdev.ecodes.ecodes["ABS_RX"], evdev.ecodes.ecodes["REL_HWHEEL_HI_RES"]
)
self.handle(
evdev.ecodes.ecodes["ABS_RY"], evdev.ecodes.ecodes["REL_WHEEL_HI_RES"]
)
self.ui_mouse.syn()
def enable(self):
"""
Enable mouse
"""
self.enabled = True
self.enable_event.set()
def disable(self):
"""
Disable mouse
"""
self.device.send_feature(d0da.d0da_feature.activate_profile(0))
self.enabled = False
self.enable_event.clear()
async def gamepad(
wooting_gamepad,
state,
mouse,
rgb_lighting,
ev_key_mouse_write_map,
ev_abs_mouse_write_map,
):
"""
Watch Gamepad events
"""
ev_key = evdev.ecodes.ecodes["EV_KEY"]
ev_abs = evdev.ecodes.ecodes["EV_ABS"]
pressed_keys = {
evdev.ecodes.ecodes["BTN_BACK"]: False,
evdev.ecodes.ecodes["BTN_FORWARD"]: False,
}
async for event in wooting_gamepad.async_read_loop():
if event.value > 0:
mouse.enable()
rgb_lighting.mouse()
state[event.type][event.code] = event.value
if event.type == ev_key and event.code in ev_key_mouse_write_map:
mouse.write(
ev_key,
evdev.ecodes.ecodes[ev_key_mouse_write_map[event.code]],
event.value,
)
elif (
event.type == ev_key
and event.code == evdev.ecodes.ecodes["BTN_START"]
and event.value > 0
):
mouse.disable()
time.sleep(0.1)
rgb_lighting.time_of_day()
elif event.type == ev_abs and event.code in ev_abs_mouse_write_map:
if event.value == 0:
for pressed_key, pressed in pressed_keys.items():
if pressed:
mouse.write(ev_key, pressed_key, 0)
pressed_keys[pressed_key] = False
else:
if event.value < 0:
pressed_key = evdev.ecodes.ecodes[
ev_abs_mouse_write_map[event.code][0]
]
else:
pressed_key = evdev.ecodes.ecodes[
ev_abs_mouse_write_map[event.code][1]
]
mouse.write(ev_key, pressed_key, 1)
pressed_keys[pressed_key] = True
class RGBLighting:
"""
RGB Lighting
"""
def __init__(self, device):
self.device = device
self.time_of_day()
self.do_time_of_day = True
def time_of_day(self):
"""
Color keyboard according to time of day
"""
values = []
localtime = time.localtime()
hue = (localtime.tm_hour * 60 + localtime.tm_min) / (24 * 60)
rgb = colorsys.hls_to_rgb(hue, 0.5, 1)
hslv = hsluv.rgb_to_hsluv(rgb)
self.do_time_of_day = True
for i in range(21):
if i < 10:
rgbv = hsluv.hsluv_to_rgb((hslv[0] + 0, hslv[1], hslv[2]))
elif i < 12:
rgbv = hsluv.hsluv_to_rgb((hslv[0] + 10, hslv[1], hslv[2]))
elif i < 14:
rgbv = hsluv.hsluv_to_rgb((hslv[0] + 20, hslv[1], hslv[2]))
values.append(
(int(rgbv[0] * 255), int(rgbv[1] * 255), int(rgbv[2] * 255)),
)
payload = d0da.d0da_report.set_upper_rows_rgb(values, values, values)
self.device.send_buffer(payload)
payload = d0da.d0da_report.set_lower_rows_rgb(values, values, values)
self.device.send_buffer(payload)
def mouse(self):
"""
Mouse mode (disable time of day)
"""
self.do_time_of_day = False
async def run(self) -> None:
"""
Main loop
"""
while 1:
await asyncio.sleep(60)
if self.do_time_of_day:
self.time_of_day()
def main():
"""
Main function
"""
context = pyudev.Context()
# run: udevadm info -t
# search for "event-joystick"
# Use the child of this device (event*) (P:):
# /devices/pci0000:00/0000:00:14.0/usb2/2-5/2-5.2/2-5.2:1.0/input/input33/event3
udev = pyudev.Devices.from_path(context, sys.argv[1])
wooting_gamepad = evdev.InputDevice(udev.device_node)
state = {
evdev.ecodes.ecodes["EV_SYN"]: collections.defaultdict(int),
evdev.ecodes.ecodes["EV_ABS"]: collections.defaultdict(int),
evdev.ecodes.ecodes["EV_KEY"]: collections.defaultdict(int),
}
device = d0da_device.get_device("/".join(sys.argv[1].split("/")[:-4]))
mouse = Mouse(state, device)
rgb_lighting = RGBLighting(device)
tasks = asyncio.gather(
mouse.run(),
gamepad(
wooting_gamepad,
state,
mouse,
rgb_lighting,
{
evdev.ecodes.ecodes["BTN_SOUTH"]: "KEY_CAPSLOCK",
evdev.ecodes.ecodes["BTN_TL"]: "KEY_LEFTCTRL",
evdev.ecodes.ecodes["BTN_TR"]: "KEY_LEFTALT",
evdev.ecodes.ecodes["BTN_EAST"]: "BTN_LEFT",
evdev.ecodes.ecodes["BTN_NORTH"]: "BTN_MIDDLE",
evdev.ecodes.ecodes["BTN_WEST"]: "BTN_RIGHT",
},
{evdev.ecodes.ecodes["ABS_HAT0X"]: ("BTN_BACK", "BTN_FORWARD")},
),
rgb_lighting.run(),
)
loop = asyncio.get_event_loop()
loop.add_signal_handler(signal.SIGTERM, tasks.cancel)
loop.run_until_complete(tasks)