Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add trigger rumble support for SDL2 #247

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions sdl2/.SRCINFO
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
pkgbase = sdl2
pkgdesc = A library for portable low-level access to a video framebuffer, audio output, mouse, and keyboard (Version 2)
pkgver = 2.30.4
pkgrel = 2
url = https://www.libsdl.org
arch = x86_64
license = MIT
makedepends = alsa-lib
makedepends = mesa
makedepends = libpulse
makedepends = libxrandr
makedepends = libxinerama
makedepends = wayland
makedepends = libxkbcommon
makedepends = wayland-protocols
makedepends = ibus
makedepends = fcitx5
makedepends = libxss
makedepends = cmake
makedepends = jack
makedepends = ninja
makedepends = pipewire
makedepends = libdecor
depends = glibc
depends = libxext
depends = libxrender
depends = libx11
depends = libgl
depends = libxcursor
depends = hidapi
depends = libusb
optdepends = alsa-lib: ALSA audio driver
optdepends = libpulse: PulseAudio audio driver
optdepends = jack: JACK audio driver
optdepends = pipewire: PipeWire audio driver
optdepends = libdecor: Wayland client decorations
source = https://github.com/libsdl-org/SDL/releases/download/release-2.30.4/SDL2-2.30.4.tar.gz
source = https://github.com/libsdl-org/SDL/releases/download/release-2.30.4/SDL2-2.30.4.tar.gz.sig
source = trigger-rumble.patch
validpgpkeys = 1528635D8053A57F77D1E08630A59377A7763BE6
sha512sums = dace0bfcfb9c7452786bee7082c8293199430ed0735e1efd983d73b604ab6bab8b9d2bc84530fa66c2cfa4e5c3ae6b506b2b6cb9a068c25f0f5b3352240d859b
sha512sums = SKIP
sha512sums = e95cfe8eed80b1ef4749325b62169cf367c69fbbca26a5d0780f9c18506eb08850f2ba5ff25acee774d3c5f361f7707c92c79c610f7ad41bca9278878c3fc734

pkgname = sdl2
49 changes: 49 additions & 0 deletions sdl2/PKGBUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Maintainer: Sven-Hendrik Haase <svenstaro@archlinux.org>
pkgname=sdl2
pkgver=2.30.4
pkgrel=2
pkgdesc='A library for portable low-level access to a video framebuffer, audio output, mouse, and keyboard (Version 2)'
arch=('x86_64')
url='https://www.libsdl.org'
license=('MIT')
depends=('glibc' 'libxext' 'libxrender' 'libx11' 'libgl' 'libxcursor' 'hidapi' 'libusb')
makedepends=('alsa-lib' 'mesa' 'libpulse' 'libxrandr' 'libxinerama' 'wayland' 'libxkbcommon'
'wayland-protocols' 'ibus' 'fcitx5' 'libxss' 'cmake' 'jack' 'ninja' 'pipewire'
'libdecor')
optdepends=('alsa-lib: ALSA audio driver'
'libpulse: PulseAudio audio driver'
'jack: JACK audio driver'
'pipewire: PipeWire audio driver'
'libdecor: Wayland client decorations')
source=(https://github.com/libsdl-org/SDL/releases/download/release-${pkgver}/SDL2-${pkgver}.tar.gz{,.sig}
trigger-rumble.patch)
sha512sums=('dace0bfcfb9c7452786bee7082c8293199430ed0735e1efd983d73b604ab6bab8b9d2bc84530fa66c2cfa4e5c3ae6b506b2b6cb9a068c25f0f5b3352240d859b'
'SKIP'
'e95cfe8eed80b1ef4749325b62169cf367c69fbbca26a5d0780f9c18506eb08850f2ba5ff25acee774d3c5f361f7707c92c79c610f7ad41bca9278878c3fc734')
validpgpkeys=('1528635D8053A57F77D1E08630A59377A7763BE6') # Sam Lantinga

prepare(){
cd SDL2-$pkgver
patch -Np1 < ../trigger-rumble.patch
}

build() {
CFLAGS+=" -ffat-lto-objects"
cmake -S SDL2-${pkgver} -B build -G Ninja \
-D SDL_HIDAPI_LIBUSB=ON \
-D CMAKE_INSTALL_PREFIX=/usr \
-D SDL_STATIC=OFF \
-D SDL_RPATH=OFF
cmake --build build
}

package() {
DESTDIR="${pkgdir}" cmake --install build

# For some reason, this isn't named correctly and we have to fix it to reflect the actual staticlib name.
sed -i "s/libSDL2\.a/libSDL2main.a/g" "$pkgdir"/usr/lib/cmake/SDL2/SDL2Targets-noconfig.cmake

install -Dm644 SDL2-${pkgver}/LICENSE.txt "$pkgdir/usr/share/licenses/$pkgname/LICENSE"
}

# vim:set ts=2 sw=2 et:
51 changes: 51 additions & 0 deletions sdl2/trigger-rumble.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
diff --git a/src/joystick/linux/SDL_sysjoystick.c b/src/joystick/linux/SDL_sysjoystick.c
index 75d2522..b020c9e 100644
--- a/src/joystick/linux/SDL_sysjoystick.c
+++ b/src/joystick/linux/SDL_sysjoystick.c
@@ -1683,7 +1683,45 @@ static int LINUX_JoystickRumble(SDL_Joystick *joystick, Uint16 low_frequency_rum

static int LINUX_JoystickRumbleTriggers(SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble)
{
- return SDL_Unsupported();
+ struct input_event event;
+
+ SDL_AssertJoysticksLocked();
+
+ if (joystick->hwdata->ff_rumble) {
+ struct ff_effect *effect = &joystick->hwdata->effect;
+
+ effect->type = FF_RUMBLE;
+ effect->replay.length = SDL_MAX_RUMBLE_DURATION_MS;
+ effect->u.rumble.trigger_left = left_rumble;
+ effect->u.rumble.trigger_right = right_rumble;
+ } else if (joystick->hwdata->ff_sine) {
+ /* Scale and average the two rumble strengths */
+ Sint16 magnitude = (Sint16)(((left_rumble / 2) + (right_rumble / 2)) / 2);
+ struct ff_effect *effect = &joystick->hwdata->effect;
+
+ effect->type = FF_PERIODIC;
+ effect->replay.length = SDL_MAX_RUMBLE_DURATION_MS;
+ effect->u.periodic.waveform = FF_SINE;
+ effect->u.periodic.magnitude = magnitude;
+ } else {
+ return SDL_Unsupported();
+ }
+
+ if (ioctl(joystick->hwdata->fd, EVIOCSFF, &joystick->hwdata->effect) < 0) {
+ /* The kernel may have lost this effect, try to allocate a new one */
+ joystick->hwdata->effect.id = -1;
+ if (ioctl(joystick->hwdata->fd, EVIOCSFF, &joystick->hwdata->effect) < 0) {
+ return SDL_SetError("Couldn't update rumble effect: %s", strerror(errno));
+ }
+ }
+
+ event.type = EV_FF;
+ event.code = joystick->hwdata->effect.id;
+ event.value = 1;
+ if (write(joystick->hwdata->fd, &event, sizeof(event)) < 0) {
+ return SDL_SetError("Couldn't start rumble effect: %s", strerror(errno));
+ }
+ return 0;
}

static Uint32 LINUX_JoystickGetCapabilities(SDL_Joystick *joystick)
4 changes: 3 additions & 1 deletion toolchain/linux-api-headers/.SRCINFO
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
pkgbase = linux-api-headers
pkgdesc = Kernel headers sanitized for use in userspace
pkgver = 6.8
pkgrel = 5
pkgrel = 6
url = https://www.gnu.org/software/libc
arch = any
license = GPL-2.0-only
makedepends = rsync
source = https://www.kernel.org/pub/linux/kernel/v6.x/linux-6.8.tar.xz
source = https://www.kernel.org/pub/linux/kernel/v6.x/linux-6.8.tar.sign
source = ntsync.patch
source = trigger-rumble.path
validpgpkeys = ABAF11C65A2970B130ABE3C479BE3E4300411886
validpgpkeys = 647F28654894E3BD457199BE38DBBDC86092693E
sha256sums = c969dea4e8bb6be991bbf7c010ba0e0a5643a3a8d8fb0a2aaa053406f1e965f3
sha256sums = SKIP
sha256sums = 8806ec229bff3d0f2189cc5455bb8ef285e7009972112dcd0c0197f6976a80cd
sha256sums = 2b566e3fd9805924a4d4bd9355dbba3786884a48a4f906ceadbdf3d8eb7d2ceb

pkgname = linux-api-headers
13 changes: 9 additions & 4 deletions toolchain/linux-api-headers/PKGBUILD
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,28 @@

pkgname=linux-api-headers
pkgver=6.8
pkgrel=5
pkgrel=6
pkgdesc='Kernel headers sanitized for use in userspace'
arch=(any)
url='https://www.gnu.org/software/libc'
license=(GPL-2.0-only)
makedepends=(rsync)
source=(https://www.kernel.org/pub/linux/kernel/v${pkgver:0:1}.x/linux-${pkgver}.tar.{xz,sign}
ntsync.patch)
source=(
https://www.kernel.org/pub/linux/kernel/v${pkgver:0:1}.x/linux-${pkgver}.tar.{xz,sign}
ntsync.patch
trigger-rumble.patch
)
sha256sums=('c969dea4e8bb6be991bbf7c010ba0e0a5643a3a8d8fb0a2aaa053406f1e965f3'
'SKIP'
'8806ec229bff3d0f2189cc5455bb8ef285e7009972112dcd0c0197f6976a80cd')
'8806ec229bff3d0f2189cc5455bb8ef285e7009972112dcd0c0197f6976a80cd'
'2b566e3fd9805924a4d4bd9355dbba3786884a48a4f906ceadbdf3d8eb7d2ceb')
validpgpkeys=('ABAF11C65A2970B130ABE3C479BE3E4300411886' # Linus Torvalds
'647F28654894E3BD457199BE38DBBDC86092693E') # Greg Kroah-Hartman

prepare(){
cd linux-$pkgver
patch -Np1 < ../ntsync.patch
patch -Np1 < ../trigger-rumble.patch
}
build() {
cd linux-$pkgver
Expand Down
21 changes: 21 additions & 0 deletions toolchain/linux-api-headers/trigger-rumble.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--- a/include/uapi/linux/input.h
+++ b/include/uapi/linux/input.h
@@ -419,6 +419,8 @@ struct ff_periodic_effect {
* struct ff_rumble_effect - defines parameters of a periodic force-feedback effect
* @strong_magnitude: magnitude of the heavy motor
* @weak_magnitude: magnitude of the light one
+ * @trigger_left: magnitude of the motor behind the left trigger
+ * @trigger_right: magnitude of the motor behind the right trigger
*
* Some rumble pads have two motors of different weight. Strong_magnitude
* represents the magnitude of the vibration generated by the heavy one.
@@ -426,6 +428,8 @@ struct ff_periodic_effect {
struct ff_rumble_effect {
__u16 strong_magnitude;
__u16 weak_magnitude;
+ __u16 trigger_left;
+ __u16 trigger_right;
};

/**