Skip to content

Commit 9ad259c

Browse files
authored
Port "Gamepad" to python (#211)
1 parent db555f7 commit 9ad259c

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

src/Gamepad/main.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import gi
2+
3+
gi.require_version("Manette", "0.2")
4+
from gi.repository import Manette
5+
import workbench
6+
7+
stack = workbench.builder.get_object("stack")
8+
button_rumble = workbench.builder.get_object("button_rumble")
9+
10+
devices = set()
11+
12+
stack.set_visible_child_name("connect")
13+
14+
15+
def on_rumble_button_clicked(*_):
16+
for device in devices:
17+
if device.has_rumble():
18+
device.rumble(1000, 1500, 200)
19+
20+
21+
button_rumble.connect("clicked", on_rumble_button_clicked)
22+
23+
24+
def on_button_press_event(device, event):
25+
success, button = event.get_button()
26+
print(
27+
f"{device.get_name()}: press {button if success else event.get_hardware_code()}"
28+
)
29+
30+
31+
def on_button_release_event(device, event):
32+
success, button = event.get_button()
33+
print(
34+
f"{device.get_name()}: release {button if success else event.get_hardware_code()}"
35+
)
36+
37+
38+
def on_hat_axis_event(device, event):
39+
_, hat_axis, hat_value = event.get_hat()
40+
print(f"{device.get_name()}: moved axis {hat_axis} to {hat_value}")
41+
42+
43+
def on_absolute_axis_event(device, event):
44+
_, axis, value = event.get_absolute()
45+
if abs(value) > 0.2:
46+
print(f"{device.get_name()}: moved axis {axis} to {value}")
47+
48+
49+
def on_device(_, device):
50+
print("Device connected:", device.get_name())
51+
52+
# Face and Shoulder Buttons
53+
device.connect("button-press-event", on_button_press_event)
54+
55+
# Face and Shoulder Buttons
56+
device.connect("button-release-event", on_button_release_event)
57+
58+
# D-pads
59+
device.connect("hat-axis-event", on_hat_axis_event)
60+
61+
# Analog Axis - Triggers and Joysticks
62+
device.connect("absolute-axis-event", on_absolute_axis_event)
63+
64+
devices.add(device)
65+
stack.set_visible_child_name("watch")
66+
67+
68+
def on_device_disconnected(_, device):
69+
print(f"Device Disconnected: {device.get_name()}")
70+
71+
devices.remove(device)
72+
stack.set_visible_child_name("connect" if len(devices) < 1 else "watch")
73+
74+
75+
monitor = Manette.Monitor()
76+
monitor_iter = monitor.iterate()
77+
78+
while True:
79+
has_next, device = monitor_iter.next()
80+
if device:
81+
on_device(None, device)
82+
if not has_next:
83+
break
84+
85+
monitor.connect("device-connected", on_device)
86+
monitor.connect("device-disconnected", on_device_disconnected)

0 commit comments

Comments
 (0)