wgois / OIS

Official OIS repository. Object oriented Input System
https://wgois.github.io/OIS/
zlib License
254 stars 86 forks source link

Detect when a controller is connected/disconnected? #76

Open BioAbnerUAD opened 2 years ago

BioAbnerUAD commented 2 years ago

Is there a way to detect when a controller is connected/disconnected? If not, are there any ideas on how I could implement a connected/disconnected event?

Socke81 commented 2 years ago

I think this project is dead. But I'm playing around with the console demo right now. I think you can create a new InputManger in the background (own thread) every x seconds and check if the list of joysticks matches the list of the old manger. If not, a new joystick has been added or removed.

pjcast commented 2 years ago

I would say - doing this in a thread/spinning up various instances of the manager would not be ideal. When I created OIS way back (haven't done any work on it in many years myself), I envisioned disconnect/reconnect support in the lib (e.g. for console's, less common for PC back then) -

It should be possible to add a new event type, and periodically scan for new devices, and detect reconnects inside the manager itself. Some bit of OS specific logic needed perhaps in some parts. I may look into this one day - if you have time now, would be a great addition.

Socke81 commented 2 years ago

I would also destroy the instance again if nothing has changed and if something has changed overwrite the old one with the new one. Is also not very clean. I looked at the code and there are many places where lists and variables are filled when the manager is initialized. It's not just a new event. Implementing that is a lot of work. Probably that's why the function doesn't exist.

pjcast commented 2 years ago

The main issue, and why it was never fully realized, is none of the APIs are great (directinput, xinput) for handling this. They are not geared towards this so much. I may tackle it though, definitely wanting to get back into C++ a bit more, been doing C# almost exclusively for years now.

I may take a stab at the Linux side - they are just standard HID devices in udev.

Socke81 commented 2 years ago

The detection of connected/disconnected works under Windows with SDL2 without problems. But unfortunately I have other problems with SDL2 and can't use it on Linux and OSX for some reasons. I put this workaround on a keyboard key in the console demo and it worked just fine. But I only tested this workaround on Windows. But doing that every second actually sounds horrible. I'd have to take a look at how SDL2 does it.