-- | Copyright  : Will Thompson and Iñaki García Etxebarria
-- License    : LGPL-2.1
-- Maintainer : Iñaki García Etxebarria

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

module GI.Keybinder.Functions
    ( 

 -- * Methods


-- ** bind #method:bind#

    bind                                    ,


-- ** getCurrentEventTime #method:getCurrentEventTime#

    getCurrentEventTime                     ,


-- ** init #method:init#

    init                                    ,


-- ** setUseCookedAccelerators #method:setUseCookedAccelerators#

    setUseCookedAccelerators                ,


-- ** supported #method:supported#

    supported                               ,


-- ** unbind #method:unbind#

    unbind                                  ,




    ) 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.GArray as B.GArray
import qualified Data.GI.Base.GClosure as B.GClosure
import qualified Data.GI.Base.GError as B.GError
import qualified Data.GI.Base.GHashTable as B.GHT
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.Coerce as Coerce
import qualified Data.Text as T
import qualified Data.Kind as DK
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 GHC.Records as R
import qualified Data.Word as DW
import qualified Data.Int as DI
import qualified System.Posix.Types as SPT
import qualified Foreign.C.Types as FCT

-- Workaround for https://gitlab.haskell.org/ghc/ghc/-/issues/23392
#if MIN_VERSION_base(4,18,0)
import qualified GI.GLib.Callbacks as GLib.Callbacks
import qualified GI.Keybinder.Callbacks as Keybinder.Callbacks

#else
import qualified GI.GLib.Callbacks as GLib.Callbacks
import qualified GI.Keybinder.Callbacks as Keybinder.Callbacks

#endif

-- function unbind
-- Args: [ Arg
--           { argCName = "keystring"
--           , argType = TBasicType TUTF8
--           , direction = DirectionIn
--           , mayBeNull = False
--           , argDoc =
--               Documentation
--                 { rawDocText =
--                     Just "an accelerator description (gtk_accelerator_parse() format)"
--                 , sinceVersion = Nothing
--                 }
--           , argScope = ScopeTypeInvalid
--           , argClosure = -1
--           , argDestroy = -1
--           , argCallerAllocates = False
--           , argCallbackUserData = False
--           , transfer = TransferNothing
--           }
--       ]
-- Lengths: []
-- returnType: Nothing
-- throws : False
-- Skip return : False

foreign import ccall "keybinder_unbind_all" keybinder_unbind_all :: 
    CString ->                              -- keystring : TBasicType TUTF8
    IO ()

-- | Unregister all previously bound callbacks for this keystring.
-- 
-- /Since: 0.3.0/
unbind ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    T.Text
    -- ^ /@keystring@/: an accelerator description (@/gtk_accelerator_parse()/@ format)
    -> m ()
unbind :: forall (m :: * -> *). (HasCallStack, MonadIO m) => Text -> m ()
unbind Text
keystring = IO () -> m ()
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$ do
    CString
keystring' <- Text -> IO CString
textToCString Text
keystring
    CString -> IO ()
keybinder_unbind_all CString
keystring'
    CString -> IO ()
forall a. Ptr a -> IO ()
freeMem CString
keystring'
    () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()


-- function supported
-- Args: []
-- Lengths: []
-- returnType: Just (TBasicType TBoolean)
-- throws : False
-- Skip return : False

foreign import ccall "keybinder_supported" keybinder_supported :: 
    IO CInt

-- | /No description available in the introspection data./
supported ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    m Bool
    -- ^ __Returns:__ TRUE if keybindings are supported
supported :: forall (m :: * -> *). (HasCallStack, MonadIO m) => m Bool
supported  = IO Bool -> m Bool
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Bool -> m Bool) -> IO Bool -> m Bool
forall a b. (a -> b) -> a -> b
$ do
    CInt
result <- IO CInt
keybinder_supported
    let result' :: Bool
result' = (CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
/= CInt
0) CInt
result
    Bool -> IO Bool
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
result'


-- function set_use_cooked_accelerators
-- Args: [ Arg
--           { argCName = "use_cooked"
--           , argType = TBasicType TBoolean
--           , direction = DirectionIn
--           , mayBeNull = False
--           , argDoc =
--               Documentation
--                 { rawDocText = Just "if %FALSE disable cooked accelerators"
--                 , sinceVersion = Nothing
--                 }
--           , argScope = ScopeTypeInvalid
--           , argClosure = -1
--           , argDestroy = -1
--           , argCallerAllocates = False
--           , argCallbackUserData = False
--           , transfer = TransferNothing
--           }
--       ]
-- Lengths: []
-- returnType: Nothing
-- throws : False
-- Skip return : False

foreign import ccall "keybinder_set_use_cooked_accelerators" keybinder_set_use_cooked_accelerators :: 
    CInt ->                                 -- use_cooked : TBasicType TBoolean
    IO ()

-- | \"Cooked\" accelerators use symbols produced by using modifiers such
-- as shift or altgr, for example if \"!\" is produced by \"Shift+1\".
-- 
-- If cooked accelerators are enabled, use \"&lt;Ctrl&gt;exclam\" to bind
-- \"Ctrl+!\" If disabled, use \"&lt;Ctrl&gt;&lt;Shift&gt;1\" to bind
-- \"Ctrl+Shift+1\". These two examples are not equal on all keymaps.
-- 
-- The cooked accelerator keyvalue and modifiers are provided by the
-- function @/gdk_keymap_translate_keyboard_state()/@
-- 
-- Cooked accelerators are useful if you receive keystrokes from GTK to bind,
-- but raw accelerators can be useful if you or the user inputs accelerators as
-- text.
-- 
-- Default: Enabled. Should be set before binding anything.
setUseCookedAccelerators ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    Bool
    -- ^ /@useCooked@/: if 'P.False' disable cooked accelerators
    -> m ()
setUseCookedAccelerators :: forall (m :: * -> *). (HasCallStack, MonadIO m) => Bool -> m ()
setUseCookedAccelerators Bool
useCooked = IO () -> m ()
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$ do
    let useCooked' :: CInt
useCooked' = (Int -> CInt
forall a b. (Integral a, Num b) => a -> b
P.fromIntegral (Int -> CInt) -> (Bool -> Int) -> Bool -> CInt
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> Int
forall a. Enum a => a -> Int
P.fromEnum) Bool
useCooked
    CInt -> IO ()
keybinder_set_use_cooked_accelerators CInt
useCooked'
    () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()


-- function init
-- Args: []
-- Lengths: []
-- returnType: Nothing
-- throws : False
-- Skip return : False

foreign import ccall "keybinder_init" keybinder_init :: 
    IO ()

-- | Initialize the keybinder library.
-- 
-- This function must be called after initializing GTK, before calling any
-- other function in the library. Can only be called once.
init ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    m ()
init :: forall (m :: * -> *). (HasCallStack, MonadIO m) => m ()
init  = IO () -> m ()
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$ do
    IO ()
keybinder_init
    () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()


-- function get_current_event_time
-- Args: []
-- Lengths: []
-- returnType: Just (TBasicType TUInt32)
-- throws : False
-- Skip return : False

foreign import ccall "keybinder_get_current_event_time" keybinder_get_current_event_time :: 
    IO Word32

-- | /No description available in the introspection data./
getCurrentEventTime ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    m Word32
    -- ^ __Returns:__ the current event timestamp
getCurrentEventTime :: forall (m :: * -> *). (HasCallStack, MonadIO m) => m Word32
getCurrentEventTime  = IO Word32 -> m Word32
forall a. IO a -> m a
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
    Word32
result <- IO Word32
keybinder_get_current_event_time
    Word32 -> IO Word32
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Word32
result


-- function bind
-- Args: [ Arg
--           { argCName = "keystring"
--           , argType = TBasicType TUTF8
--           , direction = DirectionIn
--           , mayBeNull = False
--           , argDoc =
--               Documentation
--                 { rawDocText =
--                     Just "an accelerator description (gtk_accelerator_parse() format)"
--                 , sinceVersion = Nothing
--                 }
--           , argScope = ScopeTypeInvalid
--           , argClosure = -1
--           , argDestroy = -1
--           , argCallerAllocates = False
--           , argCallbackUserData = False
--           , transfer = TransferNothing
--           }
--       , Arg
--           { argCName = "handler"
--           , argType =
--               TInterface Name { namespace = "Keybinder" , name = "Handler" }
--           , direction = DirectionIn
--           , mayBeNull = False
--           , argDoc =
--               Documentation
--                 { rawDocText = Just "callback function" , sinceVersion = Nothing }
--           , argScope = ScopeTypeNotified
--           , argClosure = 2
--           , argDestroy = 3
--           , argCallerAllocates = False
--           , argCallbackUserData = False
--           , transfer = TransferNothing
--           }
--       , Arg
--           { argCName = "user_data"
--           , argType = TBasicType TPtr
--           , direction = DirectionIn
--           , mayBeNull = True
--           , argDoc =
--               Documentation
--                 { rawDocText = Just "data to pass to @handler"
--                 , sinceVersion = Nothing
--                 }
--           , argScope = ScopeTypeInvalid
--           , argClosure = -1
--           , argDestroy = -1
--           , argCallerAllocates = False
--           , argCallbackUserData = False
--           , transfer = TransferNothing
--           }
--       , Arg
--           { argCName = "notify"
--           , argType =
--               TInterface Name { namespace = "GLib" , name = "DestroyNotify" }
--           , direction = DirectionIn
--           , mayBeNull = True
--           , argDoc =
--               Documentation
--                 { rawDocText = Just "called when @handler is unregistered"
--                 , sinceVersion = Nothing
--                 }
--           , argScope = ScopeTypeAsync
--           , argClosure = -1
--           , argDestroy = -1
--           , argCallerAllocates = False
--           , argCallbackUserData = False
--           , transfer = TransferNothing
--           }
--       ]
-- Lengths: []
-- returnType: Just (TBasicType TBoolean)
-- throws : False
-- Skip return : False

foreign import ccall "keybinder_bind_full" keybinder_bind_full :: 
    CString ->                              -- keystring : TBasicType TUTF8
    FunPtr Keybinder.Callbacks.C_Handler -> -- handler : TInterface (Name {namespace = "Keybinder", name = "Handler"})
    Ptr () ->                               -- user_data : TBasicType TPtr
    FunPtr GLib.Callbacks.C_DestroyNotify -> -- notify : TInterface (Name {namespace = "GLib", name = "DestroyNotify"})
    IO CInt

-- | Grab a key combination globally and register a callback to be called each
-- time the key combination is pressed.
-- 
-- /Since: 0.3.0/
bind ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    T.Text
    -- ^ /@keystring@/: an accelerator description (@/gtk_accelerator_parse()/@ format)
    -> Keybinder.Callbacks.Handler
    -- ^ /@handler@/: callback function
    -> m Bool
    -- ^ __Returns:__ 'P.True' if the accelerator could be grabbed
bind :: forall (m :: * -> *).
(HasCallStack, MonadIO m) =>
Text -> Handler -> m Bool
bind Text
keystring Handler
handler = IO Bool -> m Bool
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Bool -> m Bool) -> IO Bool -> m Bool
forall a b. (a -> b) -> a -> b
$ do
    CString
keystring' <- Text -> IO CString
textToCString Text
keystring
    FunPtr C_Handler
handler' <- C_Handler -> IO (FunPtr C_Handler)
Keybinder.Callbacks.mk_Handler (Maybe (Ptr (FunPtr C_Handler)) -> Handler_WithClosures -> C_Handler
Keybinder.Callbacks.wrap_Handler Maybe (Ptr (FunPtr C_Handler))
forall a. Maybe a
Nothing (Handler -> Handler_WithClosures
Keybinder.Callbacks.drop_closures_Handler Handler
handler))
    let userData :: Ptr ()
userData = FunPtr C_Handler -> Ptr ()
forall a b. FunPtr a -> Ptr b
castFunPtrToPtr FunPtr C_Handler
handler'
    let notify :: FunPtr (Ptr a -> IO ())
notify = FunPtr (Ptr a -> IO ())
forall a. FunPtr (Ptr a -> IO ())
SP.safeFreeFunPtrPtr
    CInt
result <- CString
-> FunPtr C_Handler -> Ptr () -> FunPtr C_DestroyNotify -> IO CInt
keybinder_bind_full CString
keystring' FunPtr C_Handler
handler' Ptr ()
userData FunPtr C_DestroyNotify
forall a. FunPtr (Ptr a -> IO ())
notify
    let result' :: Bool
result' = (CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
/= CInt
0) CInt
result
    CString -> IO ()
forall a. Ptr a -> IO ()
freeMem CString
keystring'
    Bool -> IO Bool
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
result'