Skip to content

Commit 9a621df

Browse files
halfmexicansonnyp
andauthored
Add "Gamepad" demo (#88)
Co-authored-by: Sonny Piers <sonny@fastmail.net>
1 parent 92dcc5f commit 9a621df

File tree

5 files changed

+148
-1
lines changed

5 files changed

+148
-1
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ format:
2020

2121
test:
2222
# list folders that have changed and run workbench-cli ci on them
23-
git diff --dirstat=files,0 origin/main src | sed 's/^[ 0-9.]\+% //g' | uniq | xargs -d '\n' flatpak run --command="workbench-cli" --filesystem=host re.sonny.Workbench.Devel ci
23+
# $$ is for the Makefile only - use $ otherwise
24+
git diff --name-only origin/main src | awk -F/ '{print $$1 FS $$2}' | uniq | xargs -d '\n' flatpak run --command="workbench-cli" --filesystem=host re.sonny.Workbench.Devel ci
2425

2526
all:
2627
flatpak run --command="workbench-cli" --filesystem=host re.sonny.Workbench.Devel ci src/*
Lines changed: 2 additions & 0 deletions
Loading

src/Gamepad/main.blp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using Gtk 4.0;
2+
using Adw 1;
3+
4+
Adw.StatusPage {
5+
title: _("Gamepad");
6+
description: _("Respond to gamepads and controllers input");
7+
icon-name: "gamepad-symbolic";
8+
9+
Box {
10+
orientation: vertical;
11+
halign: center;
12+
spacing: 24;
13+
14+
Stack stack {
15+
Label {
16+
label: _("Press Run");
17+
18+
styles [
19+
"title-2"
20+
]
21+
}
22+
23+
StackPage {
24+
name: "connect";
25+
26+
child: Label {
27+
label: _("Please connect a controller");
28+
29+
styles [
30+
"title-2"
31+
]
32+
};
33+
}
34+
35+
StackPage {
36+
name: "watch";
37+
38+
child: Box {
39+
orientation: vertical;
40+
spacing: 6;
41+
42+
Label {
43+
label: _("Press buttons on the controller and watch the Console");
44+
45+
styles [
46+
"title-2"
47+
]
48+
}
49+
50+
Button button_rumble {
51+
label: _("Rumble");
52+
halign: center;
53+
}
54+
};
55+
}
56+
}
57+
58+
LinkButton {
59+
label: _("API Reference");
60+
uri: "https://gnome.pages.gitlab.gnome.org/libmanette/";
61+
}
62+
}
63+
}

src/Gamepad/main.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import Manette from "gi://Manette";
2+
3+
const stack = workbench.builder.get_object("stack");
4+
const button_rumble = workbench.builder.get_object("button_rumble");
5+
6+
const devices = new Set();
7+
8+
stack.visible_child_name = "connect";
9+
button_rumble.connect("clicked", () => {
10+
for (const device of devices) {
11+
if (device.has_rumble()) {
12+
device.rumble(1000, 1500, 200);
13+
}
14+
}
15+
});
16+
17+
function onDevice(device) {
18+
console.log("Device connected:", device.get_name());
19+
20+
// Face and Shoulder Buttons
21+
device.connect("button-press-event", (device, event) => {
22+
const [success, button] = event.get_button();
23+
console.log(
24+
`${device.get_name()}: press ${success ? button : event.get_hardware_code()}`,
25+
);
26+
});
27+
28+
// Face and Shoulder Buttons
29+
device.connect("button-release-event", (device, event) => {
30+
const [success, button] = event.get_button();
31+
console.log(
32+
`${device.get_name()}: release ${success ? button : event.get_hardware_code()}`,
33+
);
34+
});
35+
36+
// D-pads
37+
device.connect("hat-axis-event", (device, event) => {
38+
const [, hat_axis, hat_value] = event.get_hat();
39+
console.log(`${device.get_name()}: moved axis ${hat_axis} to ${hat_value}`);
40+
});
41+
42+
// Analog Axis - Triggers and Joysticks
43+
device.connect("absolute-axis-event", (device, event) => {
44+
const [, axis, value] = event.get_absolute();
45+
if (Math.abs(value) > 0.2)
46+
console.log(`${device.get_name()}: moved axis ${axis} to ${value}`);
47+
});
48+
49+
devices.add(device);
50+
stack.visible_child_name = "watch";
51+
}
52+
53+
function onDeviceDisconnected(device) {
54+
console.log("Device Disconnected:", device.get_name());
55+
56+
devices.delete(device);
57+
stack.visible_child_name = devices.size < 1 ? "connect" : "watch";
58+
}
59+
60+
const monitor = new Manette.Monitor();
61+
const monitor_iter = monitor.iterate();
62+
63+
let has_next;
64+
let device;
65+
do {
66+
[has_next, device] = monitor_iter.next();
67+
if (device !== null) onDevice(device);
68+
} while (has_next);
69+
70+
monitor.connect("device-connected", (_self, device) => onDevice(device));
71+
monitor.connect("device-disconnected", (_self, device) =>
72+
onDeviceDisconnected(device),
73+
);

src/Gamepad/main.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "Gamepad",
3+
"category": "controls",
4+
"description": "Respond to gamepads and controllers input",
5+
"panels": ["code", "preview"],
6+
"autorun": true,
7+
"flatpak-finish-args": ["--device=input"]
8+
}

0 commit comments

Comments
 (0)