From fa2501021c50054e80465f4300ea2371d6d85db2 Mon Sep 17 00:00:00 2001 From: nek0 Date: Wed, 10 Oct 2018 14:13:21 +0200 Subject: [PATCH] fix joystick disconnect crash --- src/Affection/Subsystems/AffectionJoystick.hs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/Affection/Subsystems/AffectionJoystick.hs b/src/Affection/Subsystems/AffectionJoystick.hs index e9e1fae..05d40ca 100644 --- a/src/Affection/Subsystems/AffectionJoystick.hs +++ b/src/Affection/Subsystems/AffectionJoystick.hs @@ -101,11 +101,16 @@ joystickAutoDisconnect js (MsgJoystickDevice _ which SDL.JoyDeviceRemoved) = liftIO $ do joyIds <- mapM SDL.getJoystickID js logIO Verbose $ "These are the Joysticks connected: " ++ show joyIds - [d] <- filterM (\j -> fmap (== which) (SDL.getJoystickID j)) js - logIO Verbose $ "disconnected joysticks: " ++ show d - logIO Verbose $ "Disconnecting Joystick " ++ show which - SDL.closeJoystick d - njoys <- filterM (\j -> return $ d /= j) js - logIO Verbose $ "returning joysticks: " ++ show njoys - return njoys + d <- filterM (\j -> fmap (== which) (SDL.getJoystickID j)) js + if not (null d) + then do + logIO Verbose $ "disconnected joysticks: " ++ show (head d) + logIO Verbose $ "Disconnecting Joystick " ++ show which + SDL.closeJoystick (head d) + njoys <- filterM (\j -> return $ head d /= j) js + logIO Verbose $ "returning joysticks: " ++ show njoys + return njoys + else do + logIO Error $ "Error while disconnecting Joystick" + return js joystickAutoDisconnect js _ = return js