module Text.XkbCommon.Keysym
( keysymFromName, keysymFromNameCaseInsensitive, keysymName, keysymUtf8
) where
import Foreign
import Foreign.C
import Foreign.Marshal.Array (peekArray0)
import Data.Text (unpack)
import Data.Text.Encoding (decodeUtf8)
import Data.ByteString (ByteString, pack)
import System.IO.Unsafe as S
import Control.Monad (liftM)
import Text.XkbCommon.InternalTypes
keysymFromName :: String -> Maybe Keysym
keysymFromName str = S.unsafePerformIO $ withCString str $ \ cstr ->
liftM safeToKeysym $ c_keysym_from_name cstr 0
keysymFromNameCaseInsensitive :: String -> Maybe Keysym
keysymFromNameCaseInsensitive str = S.unsafePerformIO $ withCString str $ \ cstr ->
liftM safeToKeysym $ c_keysym_from_name cstr 1
keysymName :: Keysym -> String
keysymName ks = S.unsafePerformIO $ do
let buflen = 64
withCString (replicate buflen ' ') $ \ cstr -> do
len <- c_keysym_name (fromKeysym ks) cstr (fromIntegral buflen)
return =<< peekCString cstr
keysymUtf8 :: Keysym -> String
keysymUtf8 ks = S.unsafePerformIO $ do
let buflen = 64
withCString (replicate buflen ' ') $ \ cstr -> do
len <- c_keysym_utf8_name (fromKeysym ks) cstr (fromIntegral buflen)
bs <- charPtrToByteString0 cstr
return $ unpack $ decodeUtf8 bs
charPtrToByteString0 :: Ptr CChar -> IO ByteString
charPtrToByteString0 ptr = do
array <- peekArray0 0 ptr
return $ pack $ map fromIntegral array
foreign import ccall unsafe "xkbcommon/xkbcommon.h xkb_keysym_get_name"
c_keysym_name :: CKeysym -> CString -> Word64 -> IO CInt
foreign import ccall unsafe "xkbcommon/xkbcommon.h xkb_keysym_from_name"
c_keysym_from_name :: CString -> CInt -> IO CKeysym
foreign import ccall unsafe "xkbcommon/xkbcommon.h xkb_keysym_to_utf8"
c_keysym_utf8_name :: CKeysym -> CString -> Word64 -> IO CInt
foreign import ccall unsafe "xkbcommon/xkbcommon.h xkb_keysym_to_utf32"
c_keysym_utf32_name :: CKeysym -> Word32