Haskell SDL library event handling

I am trying to write a small program that detects button inputs on a video game controller using the Haskell SDL bindings. My program is pretty simple:

import Control.Monad (forever, when)
import System.Exit (exitSuccess)
import Data.Time.Clock.POSIX (getPOSIXTime)

import qualified Graphics.UI.SDL.General  as SG
import qualified Graphics.UI.SDL.Events   as SE
import qualified Graphics.UI.SDL.Joystick as SJ
import qualified Graphics.UI.SDL.Video    as SV
import qualified Graphics.UI.SDL.Types    as ST


main :: IO ()
main =
  SG.withInit [SG.InitVideo, SG.InitJoystick] $ do
    -- Open joystick.  We assume it always present.
    numJoysticks <- SJ.countAvailable
    putStrLn $ show numJoysticks ++ " joystick(s) available"
    js <- SJ.open 0

    -- Create window
    SV.setVideoMode 320 240 24 [ST.SWSurface, ST.Resizable]

    -- Handle events
    forever $ do
      evt <- SE.waitEvent
      case evt of
        SE.JoyButtonDown dev btn -> do
                 t <- getPOSIXTime
                 print (show t ++ " - Button pressed: " ++ show dev ++ " " ++ show btn)
        SE.Quit -> exitSuccess
        _ -> return ()

On my system (stable Debian, GHC 7.4, libghc-sdl-dev 0.6.3), button events are captured during the first second (more or less) of program execution, and then nothing. Is there something wrong with my code?

+4
source share
1 answer

Add SJ.close jsafter your block forever. I believe the Joystick closes automatically when Haskell thinks it is no longer needed.

+1
source

Source: https://habr.com/ru/post/1531874/


All Articles