diff --git a/Makefile b/Makefile index 65cbbde8..d3528cf7 100644 --- a/Makefile +++ b/Makefile @@ -20,7 +20,8 @@ format: test: # list folders that have changed and run workbench-cli ci on them - 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 +# $$ is for the Makefile only - use $ otherwise + 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 all: flatpak run --command="workbench-cli" --filesystem=host re.sonny.Workbench.Devel ci src/* diff --git a/src/Gamepad/icons/gamepad-symbolic.svg b/src/Gamepad/icons/gamepad-symbolic.svg new file mode 100644 index 00000000..ff2aa1e8 --- /dev/null +++ b/src/Gamepad/icons/gamepad-symbolic.svg @@ -0,0 +1,2 @@ + + diff --git a/src/Gamepad/main.blp b/src/Gamepad/main.blp new file mode 100644 index 00000000..567d16ce --- /dev/null +++ b/src/Gamepad/main.blp @@ -0,0 +1,63 @@ +using Gtk 4.0; +using Adw 1; + +Adw.StatusPage { + title: _("Gamepad"); + description: _("Respond to gamepads and controllers input"); + icon-name: "gamepad-symbolic"; + + Box { + orientation: vertical; + halign: center; + spacing: 24; + + Stack stack { + Label { + label: _("Press Run"); + + styles [ + "title-2" + ] + } + + StackPage { + name: "connect"; + + child: Label { + label: _("Please connect a controller"); + + styles [ + "title-2" + ] + }; + } + + StackPage { + name: "watch"; + + child: Box { + orientation: vertical; + spacing: 6; + + Label { + label: _("Press buttons on the controller and watch the Console"); + + styles [ + "title-2" + ] + } + + Button button_rumble { + label: _("Rumble"); + halign: center; + } + }; + } + } + + LinkButton { + label: _("API Reference"); + uri: "https://gnome.pages.gitlab.gnome.org/libmanette/"; + } + } +} diff --git a/src/Gamepad/main.js b/src/Gamepad/main.js new file mode 100644 index 00000000..949b436d --- /dev/null +++ b/src/Gamepad/main.js @@ -0,0 +1,73 @@ +import Manette from "gi://Manette"; + +const stack = workbench.builder.get_object("stack"); +const button_rumble = workbench.builder.get_object("button_rumble"); + +const devices = new Set(); + +stack.visible_child_name = "connect"; +button_rumble.connect("clicked", () => { + for (const device of devices) { + if (device.has_rumble()) { + device.rumble(1000, 1500, 200); + } + } +}); + +function onDevice(device) { + console.log("Device connected:", device.get_name()); + + // Face and Shoulder Buttons + device.connect("button-press-event", (device, event) => { + const [success, button] = event.get_button(); + console.log( + `${device.get_name()}: press ${success ? button : event.get_hardware_code()}`, + ); + }); + + // Face and Shoulder Buttons + device.connect("button-release-event", (device, event) => { + const [success, button] = event.get_button(); + console.log( + `${device.get_name()}: release ${success ? button : event.get_hardware_code()}`, + ); + }); + + // D-pads + device.connect("hat-axis-event", (device, event) => { + const [, hat_axis, hat_value] = event.get_hat(); + console.log(`${device.get_name()}: moved axis ${hat_axis} to ${hat_value}`); + }); + + // Analog Axis - Triggers and Joysticks + device.connect("absolute-axis-event", (device, event) => { + const [, axis, value] = event.get_absolute(); + if (Math.abs(value) > 0.2) + console.log(`${device.get_name()}: moved axis ${axis} to ${value}`); + }); + + devices.add(device); + stack.visible_child_name = "watch"; +} + +function onDeviceDisconnected(device) { + console.log("Device Disconnected:", device.get_name()); + + devices.delete(device); + stack.visible_child_name = devices.size < 1 ? "connect" : "watch"; +} + +const monitor = new Manette.Monitor(); +const monitor_iter = monitor.iterate(); + +let has_next; +let device; +do { + [has_next, device] = monitor_iter.next(); + if (device !== null) onDevice(device); +} while (has_next); + +monitor.connect("device-connected", (_self, device) => onDevice(device)); +monitor.connect("device-disconnected", (_self, device) => + onDeviceDisconnected(device), +); diff --git a/src/Gamepad/main.json b/src/Gamepad/main.json new file mode 100644 index 00000000..1d9f054d --- /dev/null +++ b/src/Gamepad/main.json @@ -0,0 +1,8 @@ +{ + "name": "Gamepad", + "category": "controls", + "description": "Respond to gamepads and controllers input", + "panels": ["code", "preview"], + "autorun": true, + "flatpak-finish-args": ["--device=input"] +}