Dear SDL users,
I have developped a program to get buttons states and axis values of a USB 2.0 HID compliant “game controller” with SDL joystick. The joystick I use is an APEM IP Desktop.
Everythings is OK but I encounter problems when I plugged or unplugged USB connector. I get some SDL_JOYAXISMOTION events before receiving SDL_JOYDEVICEADDED or SDL_JOYDEVICEREMOVED events.
It is problematic because I use this joystick to drive a motor and I don’t want that motor could turn when I unplugged joystick.
Have you got ideas ?
Thanks a lot
You can see below an example of my program
SDL_Joystick* joystick = nullptr;
if (SDL_Init(SDL_INIT_JOYSTICK) != -1) {
// autorisation des evenements joystick
SDL_Event event;
SDL_JoystickEventState(SDL_ENABLE);
while(!mExit)
{
if (SDL_WaitEventTimeout(&event, 20) == 1) {
switch (event.type) {
case SDL_JOYDEVICEADDED:
OPEN_JOYSTICK()
qDebug() << "joystick added";
break;
case SDL_JOYBUTTONDOWN:
qDebug() << QString("Bouton : %1 down").arg(event.jbutton.button);
break;
case SDL_JOYBUTTONUP:
qDebug() << QString("Bouton : %1 up").arg(event.jbutton.button);
break;
case SDL_JOYAXISMOTION:
qDebug() << QString("axis n° %1 | value : %2").arg(event.jaxis.axis).arg(event.jaxis.value);
SDL_PumpEvents();
SDL_FlushEvent(SDL_JOYAXISMOTION);
break;
case SDL_JOYDEVICEREMOVED:
qDebug() << "event.jdevice.which : " << event.jdevice.which;
if (nullptr != joystick && SDL_JoystickInstanceID(joystick) == event.jdevice.which) {
CLOSE_JOYSTICK()
}
break;
default:
LOG_INFO << tr("Evenement n° %1 non traite").arg(event.type).toStdString();
break;
}
}