{-# LANGUAGE TypeApplications #-}


-- | Copyright  : Will Thompson, Iñaki García Etxebarria and Jonas Platte
-- License    : LGPL-2.1
-- Maintainer : Iñaki García Etxebarria
-- 
-- An IBusKeymap defines the mapping between keyboard scancodes and
-- keyboard symbols such as numbers, alphabets, and punctuation marks.
-- 
-- Some input methods assume certain keyboard layout
-- (such as Chewing and Wubi requires an US-QWERTY layout),
-- and expect key symbols to be arranged in that order.
-- These input methods should new an IBusKeymap
-- instance and define the keyboard layout.
-- Then 'GI.IBus.Objects.Keymap.keymapLookupKeysym' can
-- convert scancodes back to the key symbols.
-- 
-- see_also: t'GI.IBus.Objects.Component.Component', t'GI.IBus.Objects.EngineDesc.EngineDesc'

#if (MIN_VERSION_haskell_gi_overloading(1,0,0) && !defined(__HADDOCK_VERSION__))
#define ENABLE_OVERLOADING
#endif

module GI.IBus.Objects.Keymap
    ( 

-- * Exported types
    Keymap(..)                              ,
    IsKeymap                                ,
    toKeymap                                ,


 -- * Methods
-- ** Overloaded methods #method:Overloaded methods#

#if defined(ENABLE_OVERLOADING)
    ResolveKeymapMethod                     ,
#endif


-- ** get #method:get#

    keymapGet                               ,


-- ** lookupKeysym #method:lookupKeysym#

#if defined(ENABLE_OVERLOADING)
    KeymapLookupKeysymMethodInfo            ,
#endif
    keymapLookupKeysym                      ,


-- ** new #method:new#

    keymapNew                               ,




    ) where

import Data.GI.Base.ShortPrelude
import qualified Data.GI.Base.ShortPrelude as SP
import qualified Data.GI.Base.Overloading as O
import qualified Prelude as P

import qualified Data.GI.Base.Attributes as GI.Attributes
import qualified Data.GI.Base.BasicTypes as B.Types
import qualified Data.GI.Base.ManagedPtr as B.ManagedPtr
import qualified Data.GI.Base.GClosure as B.GClosure
import qualified Data.GI.Base.GError as B.GError
import qualified Data.GI.Base.GVariant as B.GVariant
import qualified Data.GI.Base.GValue as B.GValue
import qualified Data.GI.Base.GParamSpec as B.GParamSpec
import qualified Data.GI.Base.CallStack as B.CallStack
import qualified Data.GI.Base.Properties as B.Properties
import qualified Data.GI.Base.Signals as B.Signals
import qualified Control.Monad.IO.Class as MIO
import qualified Data.Text as T
import qualified Data.ByteString.Char8 as B
import qualified Data.Map as Map
import qualified Foreign.Ptr as FP
import qualified GHC.OverloadedLabels as OL

import qualified GI.GObject.Objects.Object as GObject.Object
import {-# SOURCE #-} qualified GI.IBus.Objects.Object as IBus.Object

-- | Memory-managed wrapper type.
newtype Keymap = Keymap (SP.ManagedPtr Keymap)
    deriving (Keymap -> Keymap -> Bool
(Keymap -> Keymap -> Bool)
-> (Keymap -> Keymap -> Bool) -> Eq Keymap
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Keymap -> Keymap -> Bool
$c/= :: Keymap -> Keymap -> Bool
== :: Keymap -> Keymap -> Bool
$c== :: Keymap -> Keymap -> Bool
Eq)

instance SP.ManagedPtrNewtype Keymap where
    toManagedPtr :: Keymap -> ManagedPtr Keymap
toManagedPtr (Keymap ManagedPtr Keymap
p) = ManagedPtr Keymap
p

foreign import ccall "ibus_keymap_get_type"
    c_ibus_keymap_get_type :: IO B.Types.GType

instance B.Types.TypedObject Keymap where
    glibType :: IO GType
glibType = IO GType
c_ibus_keymap_get_type

instance B.Types.GObject Keymap

-- | Convert 'Keymap' to and from 'Data.GI.Base.GValue.GValue' with 'Data.GI.Base.GValue.toGValue' and 'Data.GI.Base.GValue.fromGValue'.
instance B.GValue.IsGValue Keymap where
    toGValue :: Keymap -> IO GValue
toGValue Keymap
o = do
        GType
gtype <- IO GType
c_ibus_keymap_get_type
        Keymap -> (Ptr Keymap -> IO GValue) -> IO GValue
forall a c.
(HasCallStack, ManagedPtrNewtype a) =>
a -> (Ptr a -> IO c) -> IO c
B.ManagedPtr.withManagedPtr Keymap
o (GType -> (GValue -> Ptr Keymap -> IO ()) -> Ptr Keymap -> IO GValue
forall a. GType -> (GValue -> a -> IO ()) -> a -> IO GValue
B.GValue.buildGValue GType
gtype GValue -> Ptr Keymap -> IO ()
forall a. GObject a => GValue -> Ptr a -> IO ()
B.GValue.set_object)
        
    fromGValue :: GValue -> IO Keymap
fromGValue GValue
gv = do
        Ptr Keymap
ptr <- GValue -> IO (Ptr Keymap)
forall b. GObject b => GValue -> IO (Ptr b)
B.GValue.get_object GValue
gv :: IO (Ptr Keymap)
        (ManagedPtr Keymap -> Keymap) -> Ptr Keymap -> IO Keymap
forall a b.
(HasCallStack, GObject a, GObject b) =>
(ManagedPtr a -> a) -> Ptr b -> IO a
B.ManagedPtr.newObject ManagedPtr Keymap -> Keymap
Keymap Ptr Keymap
ptr
        
    

-- | Type class for types which can be safely cast to `Keymap`, for instance with `toKeymap`.
class (SP.GObject o, O.IsDescendantOf Keymap o) => IsKeymap o
instance (SP.GObject o, O.IsDescendantOf Keymap o) => IsKeymap o

instance O.HasParentTypes Keymap
type instance O.ParentTypes Keymap = '[IBus.Object.Object, GObject.Object.Object]

-- | Cast to `Keymap`, for types for which this is known to be safe. For general casts, use `Data.GI.Base.ManagedPtr.castTo`.
toKeymap :: (MonadIO m, IsKeymap o) => o -> m Keymap
toKeymap :: o -> m Keymap
toKeymap = IO Keymap -> m Keymap
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Keymap -> m Keymap) -> (o -> IO Keymap) -> o -> m Keymap
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ManagedPtr Keymap -> Keymap) -> o -> IO Keymap
forall o o'.
(HasCallStack, ManagedPtrNewtype o, TypedObject o,
 ManagedPtrNewtype o', TypedObject o') =>
(ManagedPtr o' -> o') -> o -> IO o'
unsafeCastTo ManagedPtr Keymap -> Keymap
Keymap

#if defined(ENABLE_OVERLOADING)
type family ResolveKeymapMethod (t :: Symbol) (o :: *) :: * where
    ResolveKeymapMethod "bindProperty" o = GObject.Object.ObjectBindPropertyMethodInfo
    ResolveKeymapMethod "bindPropertyFull" o = GObject.Object.ObjectBindPropertyFullMethodInfo
    ResolveKeymapMethod "destroy" o = IBus.Object.ObjectDestroyMethodInfo
    ResolveKeymapMethod "forceFloating" o = GObject.Object.ObjectForceFloatingMethodInfo
    ResolveKeymapMethod "freezeNotify" o = GObject.Object.ObjectFreezeNotifyMethodInfo
    ResolveKeymapMethod "getv" o = GObject.Object.ObjectGetvMethodInfo
    ResolveKeymapMethod "isFloating" o = GObject.Object.ObjectIsFloatingMethodInfo
    ResolveKeymapMethod "lookupKeysym" o = KeymapLookupKeysymMethodInfo
    ResolveKeymapMethod "notify" o = GObject.Object.ObjectNotifyMethodInfo
    ResolveKeymapMethod "notifyByPspec" o = GObject.Object.ObjectNotifyByPspecMethodInfo
    ResolveKeymapMethod "ref" o = GObject.Object.ObjectRefMethodInfo
    ResolveKeymapMethod "refSink" o = GObject.Object.ObjectRefSinkMethodInfo
    ResolveKeymapMethod "runDispose" o = GObject.Object.ObjectRunDisposeMethodInfo
    ResolveKeymapMethod "stealData" o = GObject.Object.ObjectStealDataMethodInfo
    ResolveKeymapMethod "stealQdata" o = GObject.Object.ObjectStealQdataMethodInfo
    ResolveKeymapMethod "thawNotify" o = GObject.Object.ObjectThawNotifyMethodInfo
    ResolveKeymapMethod "unref" o = GObject.Object.ObjectUnrefMethodInfo
    ResolveKeymapMethod "watchClosure" o = GObject.Object.ObjectWatchClosureMethodInfo
    ResolveKeymapMethod "getData" o = GObject.Object.ObjectGetDataMethodInfo
    ResolveKeymapMethod "getProperty" o = GObject.Object.ObjectGetPropertyMethodInfo
    ResolveKeymapMethod "getQdata" o = GObject.Object.ObjectGetQdataMethodInfo
    ResolveKeymapMethod "setData" o = GObject.Object.ObjectSetDataMethodInfo
    ResolveKeymapMethod "setDataFull" o = GObject.Object.ObjectSetDataFullMethodInfo
    ResolveKeymapMethod "setProperty" o = GObject.Object.ObjectSetPropertyMethodInfo
    ResolveKeymapMethod l o = O.MethodResolutionFailed l o

instance (info ~ ResolveKeymapMethod t Keymap, O.MethodInfo info Keymap p) => OL.IsLabel t (Keymap -> p) where
#if MIN_VERSION_base(4,10,0)
    fromLabel = O.overloadedMethod @info
#else
    fromLabel _ = O.overloadedMethod @info
#endif

#endif

#if defined(ENABLE_OVERLOADING)
instance O.HasAttributeList Keymap
type instance O.AttributeList Keymap = KeymapAttributeList
type KeymapAttributeList = ('[ ] :: [(Symbol, *)])
#endif

#if defined(ENABLE_OVERLOADING)
#endif

#if defined(ENABLE_OVERLOADING)
type instance O.SignalList Keymap = KeymapSignalList
type KeymapSignalList = ('[ '("destroy", IBus.Object.ObjectDestroySignalInfo), '("notify", GObject.Object.ObjectNotifySignalInfo)] :: [(Symbol, *)])

#endif

-- method Keymap::new
-- method type : Constructor
-- Args: [ Arg
--           { argCName = "name"
--           , argType = TBasicType TUTF8
--           , direction = DirectionIn
--           , mayBeNull = False
--           , argDoc =
--               Documentation
--                 { rawDocText =
--                     Just "The keymap file to be loaded, such as 'us', 'jp'."
--                 , sinceVersion = Nothing
--                 }
--           , argScope = ScopeTypeInvalid
--           , argClosure = -1
--           , argDestroy = -1
--           , argCallerAllocates = False
--           , transfer = TransferNothing
--           }
--       ]
-- Lengths: []
-- returnType: Just (TInterface Name { namespace = "IBus" , name = "Keymap" })
-- throws : False
-- Skip return : False

foreign import ccall "ibus_keymap_new" ibus_keymap_new :: 
    CString ->                              -- name : TBasicType TUTF8
    IO (Ptr Keymap)

{-# DEPRECATED keymapNew ["This function has been deprecated and should","not be used in newly written code. Please use 'GI.IBus.Objects.Keymap.keymapGet'."] #-}
-- | Get an t'GI.IBus.Objects.Keymap.Keymap' associated with the giving name.
-- 
-- This function loads the keymap file specified in /@name@/
-- in the IBUS_DATA_DIR\/keymaps directory.
keymapNew ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    T.Text
    -- ^ /@name@/: The keymap file to be loaded, such as \'us\', \'jp\'.
    -> m Keymap
    -- ^ __Returns:__ An t'GI.IBus.Objects.Keymap.Keymap' associated with the giving name; or 'P.Nothing' if failed.
keymapNew :: Text -> m Keymap
keymapNew Text
name = IO Keymap -> m Keymap
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Keymap -> m Keymap) -> IO Keymap -> m Keymap
forall a b. (a -> b) -> a -> b
$ do
    CString
name' <- Text -> IO CString
textToCString Text
name
    Ptr Keymap
result <- CString -> IO (Ptr Keymap)
ibus_keymap_new CString
name'
    Text -> Ptr Keymap -> IO ()
forall a. HasCallStack => Text -> Ptr a -> IO ()
checkUnexpectedReturnNULL Text
"keymapNew" Ptr Keymap
result
    Keymap
result' <- ((ManagedPtr Keymap -> Keymap) -> Ptr Keymap -> IO Keymap
forall a b.
(HasCallStack, GObject a, GObject b) =>
(ManagedPtr a -> a) -> Ptr b -> IO a
newObject ManagedPtr Keymap -> Keymap
Keymap) Ptr Keymap
result
    CString -> IO ()
forall a. Ptr a -> IO ()
freeMem CString
name'
    Keymap -> IO Keymap
forall (m :: * -> *) a. Monad m => a -> m a
return Keymap
result'

#if defined(ENABLE_OVERLOADING)
#endif

-- method Keymap::lookup_keysym
-- method type : OrdinaryMethod
-- Args: [ Arg
--           { argCName = "keymap"
--           , argType =
--               TInterface Name { namespace = "IBus" , name = "Keymap" }
--           , direction = DirectionIn
--           , mayBeNull = False
--           , argDoc =
--               Documentation
--                 { rawDocText = Just "An IBusKeymap." , sinceVersion = Nothing }
--           , argScope = ScopeTypeInvalid
--           , argClosure = -1
--           , argDestroy = -1
--           , argCallerAllocates = False
--           , transfer = TransferNothing
--           }
--       , Arg
--           { argCName = "keycode"
--           , argType = TBasicType TUInt16
--           , direction = DirectionIn
--           , mayBeNull = False
--           , argDoc =
--               Documentation
--                 { rawDocText = Just "A scancode to be converted."
--                 , sinceVersion = Nothing
--                 }
--           , argScope = ScopeTypeInvalid
--           , argClosure = -1
--           , argDestroy = -1
--           , argCallerAllocates = False
--           , transfer = TransferNothing
--           }
--       , Arg
--           { argCName = "state"
--           , argType = TBasicType TUInt32
--           , direction = DirectionIn
--           , mayBeNull = False
--           , argDoc =
--               Documentation
--                 { rawDocText = Just "Modifier flags(such as Ctrl, Shift)."
--                 , sinceVersion = Nothing
--                 }
--           , argScope = ScopeTypeInvalid
--           , argClosure = -1
--           , argDestroy = -1
--           , argCallerAllocates = False
--           , transfer = TransferNothing
--           }
--       ]
-- Lengths: []
-- returnType: Just (TBasicType TUInt)
-- throws : False
-- Skip return : False

foreign import ccall "ibus_keymap_lookup_keysym" ibus_keymap_lookup_keysym :: 
    Ptr Keymap ->                           -- keymap : TInterface (Name {namespace = "IBus", name = "Keymap"})
    Word16 ->                               -- keycode : TBasicType TUInt16
    Word32 ->                               -- state : TBasicType TUInt32
    IO Word32

-- | Converts the scancode to keysym, given the keymap.
keymapLookupKeysym ::
    (B.CallStack.HasCallStack, MonadIO m, IsKeymap a) =>
    a
    -- ^ /@keymap@/: An IBusKeymap.
    -> Word16
    -- ^ /@keycode@/: A scancode to be converted.
    -> Word32
    -- ^ /@state@/: Modifier flags(such as Ctrl, Shift).
    -> m Word32
    -- ^ __Returns:__ Corresponding keysym.
keymapLookupKeysym :: a -> Word16 -> Word32 -> m Word32
keymapLookupKeysym a
keymap Word16
keycode Word32
state = IO Word32 -> m Word32
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Word32 -> m Word32) -> IO Word32 -> m Word32
forall a b. (a -> b) -> a -> b
$ do
    Ptr Keymap
keymap' <- a -> IO (Ptr Keymap)
forall a b. (HasCallStack, ManagedPtrNewtype a) => a -> IO (Ptr b)
unsafeManagedPtrCastPtr a
keymap
    Word32
result <- Ptr Keymap -> Word16 -> Word32 -> IO Word32
ibus_keymap_lookup_keysym Ptr Keymap
keymap' Word16
keycode Word32
state
    a -> IO ()
forall a. ManagedPtrNewtype a => a -> IO ()
touchManagedPtr a
keymap
    Word32 -> IO Word32
forall (m :: * -> *) a. Monad m => a -> m a
return Word32
result

#if defined(ENABLE_OVERLOADING)
data KeymapLookupKeysymMethodInfo
instance (signature ~ (Word16 -> Word32 -> m Word32), MonadIO m, IsKeymap a) => O.MethodInfo KeymapLookupKeysymMethodInfo a signature where
    overloadedMethod = keymapLookupKeysym

#endif

-- method Keymap::get
-- method type : MemberFunction
-- Args: [ Arg
--           { argCName = "name"
--           , argType = TBasicType TUTF8
--           , direction = DirectionIn
--           , mayBeNull = False
--           , argDoc =
--               Documentation
--                 { rawDocText =
--                     Just "The keymap file to be loaded, such as 'us', 'jp'."
--                 , sinceVersion = Nothing
--                 }
--           , argScope = ScopeTypeInvalid
--           , argClosure = -1
--           , argDestroy = -1
--           , argCallerAllocates = False
--           , transfer = TransferNothing
--           }
--       ]
-- Lengths: []
-- returnType: Just (TInterface Name { namespace = "IBus" , name = "Keymap" })
-- throws : False
-- Skip return : False

foreign import ccall "ibus_keymap_get" ibus_keymap_get :: 
    CString ->                              -- name : TBasicType TUTF8
    IO (Ptr Keymap)

-- | Get an IBusKeymap associated with the giving name.
-- 
-- This function loads the keymap file specified in /@name@/
-- in the IBUS_DATA_DIR\/keymaps directory.
keymapGet ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    T.Text
    -- ^ /@name@/: The keymap file to be loaded, such as \'us\', \'jp\'.
    -> m Keymap
    -- ^ __Returns:__ An t'GI.IBus.Objects.Keymap.Keymap' associated with the giving name;
    -- or 'P.Nothing' if failed.
keymapGet :: Text -> m Keymap
keymapGet Text
name = IO Keymap -> m Keymap
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Keymap -> m Keymap) -> IO Keymap -> m Keymap
forall a b. (a -> b) -> a -> b
$ do
    CString
name' <- Text -> IO CString
textToCString Text
name
    Ptr Keymap
result <- CString -> IO (Ptr Keymap)
ibus_keymap_get CString
name'
    Text -> Ptr Keymap -> IO ()
forall a. HasCallStack => Text -> Ptr a -> IO ()
checkUnexpectedReturnNULL Text
"keymapGet" Ptr Keymap
result
    Keymap
result' <- ((ManagedPtr Keymap -> Keymap) -> Ptr Keymap -> IO Keymap
forall a b.
(HasCallStack, GObject a, GObject b) =>
(ManagedPtr a -> a) -> Ptr b -> IO a
wrapObject ManagedPtr Keymap -> Keymap
Keymap) Ptr Keymap
result
    CString -> IO ()
forall a. Ptr a -> IO ()
freeMem CString
name'
    Keymap -> IO Keymap
forall (m :: * -> *) a. Monad m => a -> m a
return Keymap
result'

#if defined(ENABLE_OVERLOADING)
#endif