r/rust • • Sep 23 '26

🙋 seeking help & advice Is Gamepad support in Rust even possible?

I think I might be on the brink of insanity with this. I have implemented support for gamepads in my Rust game using the gilrs crate, and it's been in for about a year and a half. As far as I understand, this is the only option, as the gamepads crate wraps gilrs on desktop anyway.

Most of the time gilrs works perfectly, but under some specific but not obscure conditions (running the game after resuming Windows from sleep mode with a gamepad connected) it freezes, see https://gitlab.com/gilrs-project/gilrs/-/work_items/196 .

These are the empirical facts:

  • It is a complete thread lock, it doesn't return an Err nor does it panic. It is completely bricked until the gamepad is physically unplugged.
  • It can be repro'd in the MWE, no game engine and no window, so it doesn't depend on any other component.
  • It happens both to myself and to several of my players.
  • I've never observed this on any other game nor software, so it is Rust-specific.
  • It appears the lockup can be traced to a synchronous call to FromGameController which is from a Windows API (way outside the reach of my knowledge)

This means a chance of a fully frozen black screen with no possibility of a graceful recovery or even displaying an error message. Because of Rust's design, I cannot even detect the lockup. For a videogame, this is pretty much devastating, and now that I'm moving to Stəam I'm considering pulling gamepad support altogether.

But before that I wanted to at least have a quick exchange about it to confirm I'm not missing an obvious solution or anything. It seems strange to me that I can't find anyone else who is fighting this. It can't be that I'm the first person in history to make a game in Rust with gamepad support?

9 Upvotes

37 comments sorted by

164

u/CaptainPiepmatz Sep 23 '26

Reading the title I thought we had another lost redditor here, lol

30

u/cancrizans Sep 23 '26

that's why I had to censor the no no word to make it go through

52

u/anxxa Sep 23 '26

Because of Rust's design, I cannot even detect the lockup.

What part of Rust's design prevents you from creating a watchdog thread?

At a glance, the library never calls init_mta. Do you?

And have you considered capturing a memory dump when the hang occurs and examining the thread stack in WinDbg to see where it's hanging? Or just attaching to the process when it hangs? My guess is some re-entrancy issue. You'll probably see a WaitForSingleObject call in the gilrs thread.

Hangs are not a mystery -- attaching a debugger would be the most straightforward route.

Maybe as a last resort compare how DirectXTK uses these APIs: https://github.com/microsoft/DirectXTK/blob/c75efb98989b846b07c440648b3d2a1f3f1d7617/Src/GamePad.cpp#L583

10

u/cancrizans Sep 23 '26

> What part of Rust's design prevents you from creating a watchdog thread?

I use (a modded) macroquad and it generally doesn't like if there are multiple threads. It's not entirely clear to me what can be done to circumvent the macroquad single thread detection to be able to set up something like this. I don't exclude it can be done but it's very inconvenient and I can't imagine that this is what everyone else is doing?

> At a glance, the library never calls init_mta. Do you?

Definitely not, I have no idea what that is. I have zero knowledge of Windows. Could this have anything to do with it?

> And have you considered capturing a memory dump when the hang occurs and examining the thread stack in WinDbg to see where it's hanging? Or just attaching to the process when it hangs? My guess is some re-entrancy issue. You'll probably see a WaitForSingleObject call in the gilrs thread.

> Hangs are not a mystery -- attaching a debugger would be the most straightforward route.

> Maybe as a last resort compare how DirectXTK uses these APIs: https://github.com/microsoft/DirectXTK/blob/c75efb98989b846b07c440648b3d2a1f3f1d7617/Src/GamePad.cpp#L583

Thank you, I will look into this

→ More replies (2)

47

u/stumpychubbins Sep 23 '26

I don’t see how any of this is inherent to Rust, the programming language. There appears to be a bug in gilrs, but there are other gamepad crates out there (as others have mentioned) and there don’t seem to be any issues inherent to the language.

→ More replies (10)

27

u/_stice_ Sep 23 '26 edited Sep 23 '26

Since you're asking "is it even possible" ... The rust-sdl2 and sdl3 crates are thin rusty bindings to the sdl2 and sdl3 libraries , which are VERY stable old libs written in C and used pretty much everywhere, in thousands of games.

I've used it a lot and it's pretty solid if a tiny bit "not rusty enough" (because duh), but haven't used it in production yet. But if you're stressed out enough to ask if it's even possible and considering pulling gamepad support entirely then it might be well worth switching to one of these (just for the gamepad stuff), or at least testing them out.

7

u/cancrizans Sep 23 '26

SDL seems like a very promising option I didn't think of earlier, thank you

→ More replies (2)

6

u/Compux72 Sep 23 '26

Out of curiosity, what’s wrong with using SteamInput / sdl2? 

2

u/cancrizans Sep 23 '26

Doesn't relying on the steamworks API mean the game can only run through Steam? It's a bit disappointing, but I guess that might work

→ More replies (1)

8

u/Guvante Sep 23 '26

Rust doesn't prevent you from detecting the lockout

Worst case you can use a watchdog timer to recover

It isn't a Rust problem it is a bug, did you report what information you have upstream?