#include "joystick_linux.h" module System.Hardware.Joystick.Linux.FFI ( JsEvent(..) , JsEventType(..) , toJsEventType , c_js_version , c_js_buttons , c_js_axes , c_js_name , c_js_event_read ) where import Foreign import Foreign.C import Foreign.C.String import System.Hardware.Joystick.Internal import System.Hardware.Joystick.Types data JsEvent = JsEvent { jsEventTime :: !Word32 , jsEventValue :: !Int16 , jsEventType :: !Word8 , jsEventNumber :: !Word8 } deriving (Show, Eq) data JsEventType = JsEventButton | JsEventAxis deriving (Eq, Ord, Show, Read, Bounded, Enum) -- | Ignores JS_EVENT_INIT toJsEventType :: Word8 -> Maybe JsEventType toJsEventType w | eq #{const JS_EVENT_INIT} = Nothing | eq #{const JS_EVENT_BUTTON} = Just JsEventButton | eq #{const JS_EVENT_AXIS} = Just JsEventAxis | otherwise = Nothing where eq b = w .&. b == b instance Storable JsEvent where sizeOf _ = (#size struct js_event) alignment = sizeOf peek ptr = do etime <- (#peek struct js_event, time) ptr evalue <- (#peek struct js_event, value) ptr etype <- (#peek struct js_event, type) ptr enumber <- (#peek struct js_event, number) ptr return $ JsEvent { jsEventTime = etime , jsEventValue = evalue , jsEventType = etype , jsEventNumber = enumber } poke ptr (JsEvent etime evalue etype enumber) = do (#poke struct js_event, time) ptr etime (#poke struct js_event, value) ptr evalue (#poke struct js_event, type) ptr etype (#poke struct js_event, number) ptr enumber -- | Linux FFI foreign import ccall unsafe "js_version" c_js_version :: CInt -> IO CUInt foreign import ccall unsafe "js_buttons" c_js_buttons :: CInt -> IO CInt foreign import ccall unsafe "js_axes" c_js_axes :: CInt -> IO CInt foreign import ccall unsafe "js_name" c_js_name :: CInt -> Ptr Word8 -> CInt -> IO CInt foreign import ccall unsafe "js_event_read" c_js_event_read :: CInt -> Ptr JsEvent -> IO CInt