{-# LINE 1 "Data/GI/Base/GParamSpec.hsc" #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
module Data.GI.Base.GParamSpec
(
wrapGParamSpecPtr
, newGParamSpecFromPtr
, unrefGParamSpec
, disownGParamSpec
, PropertyInfo(..)
, gParamSpecValue
, CStringPropertyInfo(..)
, gParamSpecCString
, CIntPropertyInfo(..)
, gParamSpecCInt
, GParamFlag(..)
, PropGetSetter(..)
, getGParamSpecGetterSetter
) where
import Foreign.C (CInt(..), CString)
import Foreign.Ptr (Ptr, FunPtr, castPtr, nullPtr)
import Foreign.StablePtr (newStablePtr, deRefStablePtr,
castStablePtrToPtr, castPtrToStablePtr)
import Control.Monad (void)
import Data.Coerce (coerce)
import Data.Maybe (fromMaybe)
import Data.Text (Text)
import Data.GI.Base.ManagedPtr (newManagedPtr', withManagedPtr,
disownManagedPtr,
newObject, withTransient)
import Data.GI.Base.BasicConversions (gflagsToWord, withTextCString)
import Data.GI.Base.BasicTypes (GObject, GParamSpec(..),
GType(..), IsGFlag, ManagedPtr)
import Data.GI.Base.GQuark (GQuark(..), gQuarkFromString)
import Data.GI.Base.GType (gtypeStablePtr)
import qualified Data.GI.Base.GValue as GV
import Data.GI.Base.GValue (GValue(..), IsGValue(..), take_stablePtr)
foreign import ccall "g_param_spec_ref_sink" g_param_spec_ref_sink ::
Ptr GParamSpec -> IO (Ptr GParamSpec)
foreign import ccall "g_param_spec_ref" g_param_spec_ref ::
Ptr GParamSpec -> IO (Ptr GParamSpec)
foreign import ccall "g_param_spec_unref" g_param_spec_unref ::
Ptr GParamSpec -> IO ()
foreign import ccall "&g_param_spec_unref" ptr_to_g_param_spec_unref ::
FunPtr (Ptr GParamSpec -> IO ())
wrapGParamSpecPtr :: Ptr GParamSpec -> IO GParamSpec
wrapGParamSpecPtr ptr = do
void $ g_param_spec_ref_sink ptr
fPtr <- newManagedPtr' ptr_to_g_param_spec_unref ptr
return $! GParamSpec fPtr
newGParamSpecFromPtr :: Ptr GParamSpec -> IO GParamSpec
newGParamSpecFromPtr ptr = do
fPtr <- g_param_spec_ref ptr >>= newManagedPtr' ptr_to_g_param_spec_unref
return $! GParamSpec fPtr
unrefGParamSpec :: GParamSpec -> IO ()
unrefGParamSpec ps = withManagedPtr ps g_param_spec_unref
disownGParamSpec :: GParamSpec -> IO (Ptr GParamSpec)
disownGParamSpec = disownManagedPtr
data GParamFlag = GParamReadable
| GParamWritable
| GParamConstruct
| GParamConstructOnly
| GParamExplicitNotify
| AnotherGParamFlag Int
deriving (Show, Eq)
instance Enum GParamFlag where
fromEnum GParamReadable = 1
{-# LINE 106 "Data/GI/Base/GParamSpec.hsc" #-}
fromEnum GParamWritable = 2
{-# LINE 107 "Data/GI/Base/GParamSpec.hsc" #-}
fromEnum GParamConstruct = 4
{-# LINE 108 "Data/GI/Base/GParamSpec.hsc" #-}
fromEnum GParamConstructOnly = 8
{-# LINE 109 "Data/GI/Base/GParamSpec.hsc" #-}
fromEnum GParamExplicitNotify = 1073741824
{-# LINE 110 "Data/GI/Base/GParamSpec.hsc" #-}
fromEnum (AnotherGParamFlag k) = k
toEnum (1) = GParamReadable
{-# LINE 113 "Data/GI/Base/GParamSpec.hsc" #-}
toEnum (2) = GParamWritable
{-# LINE 114 "Data/GI/Base/GParamSpec.hsc" #-}
toEnum (4) = GParamConstruct
{-# LINE 115 "Data/GI/Base/GParamSpec.hsc" #-}
toEnum (8) = GParamConstructOnly
{-# LINE 116 "Data/GI/Base/GParamSpec.hsc" #-}
toEnum (1073741824) = GParamExplicitNotify
{-# LINE 117 "Data/GI/Base/GParamSpec.hsc" #-}
toEnum k = AnotherGParamFlag k
instance Ord GParamFlag where
compare a b = compare (fromEnum a) (fromEnum b)
instance IsGFlag GParamFlag
defaultFlags :: Num a => a
defaultFlags = gflagsToWord [GParamReadable, GParamWritable,
GParamExplicitNotify]
data PropGetSetter o = PropGetSetter
{ propGetter :: Ptr o -> Ptr GValue -> IO ()
, propSetter :: Ptr o -> Ptr GValue -> IO ()
}
pspecQuark :: IO (GQuark (PropGetSetter o))
pspecQuark = gQuarkFromString "haskell-gi-get-set"
newtype GObjectConstructor = GObjectConstructor (ManagedPtr GObjectConstructor)
objectFromPtr :: forall a o. GObject o => Ptr a -> IO o
objectFromPtr objPtr = newObject @o @o (coerce @_ @(ManagedPtr o -> o) GObjectConstructor) (castPtr objPtr)
wrapGetSet :: forall o a. (GObject o, IsGValue a) =>
(o -> IO a)
-> (o -> a -> IO ())
-> (Ptr GValue -> a -> IO ())
-> PropGetSetter o
wrapGetSet getter setter gvalueSetter = PropGetSetter {
propGetter = \objPtr destPtr -> do
value <- objectFromPtr objPtr >>= getter
gvalueSetter destPtr value
, propSetter = \objPtr newGValuePtr ->
withTransient newGValuePtr $ \newGValue -> do
obj <- objectFromPtr objPtr
value <- GV.fromGValue newGValue
setter obj value
}
data PropertyInfo o a = PropertyInfo
{ name :: Text
, nick :: Text
, blurb :: Text
, setter :: o -> a -> IO ()
, getter :: o -> IO a
, flags :: Maybe [GParamFlag]
}
foreign import ccall g_param_spec_boxed ::
CString -> CString -> CString -> GType -> CInt -> IO (Ptr GParamSpec)
gParamSpecValue :: forall o a. GObject o => PropertyInfo o a -> IO GParamSpec
gParamSpecValue (PropertyInfo {..}) =
withTextCString name $ \cname ->
withTextCString nick $ \cnick ->
withTextCString blurb $ \cblurb -> do
pspecPtr <- g_param_spec_boxed cname cnick cblurb
gtypeStablePtr
(maybe defaultFlags gflagsToWord flags)
quark <- pspecQuark @o
gParamSpecSetQData pspecPtr quark
(PropGetSetter { propGetter = getter', propSetter = setter'})
wrapGParamSpecPtr pspecPtr
where
getter' :: Ptr o -> Ptr GValue -> IO ()
getter' objPtr destPtr = do
stablePtr <- objectFromPtr objPtr >>= getter >>= newStablePtr
take_stablePtr destPtr stablePtr
setter' :: Ptr o -> (Ptr GValue) -> IO ()
setter' objPtr gvPtr = withTransient gvPtr $ \gv -> do
obj <- objectFromPtr objPtr
val <- GV.fromGValue gv >>= deRefStablePtr
setter obj val
data CIntPropertyInfo o = CIntPropertyInfo
{ name :: Text
, nick :: Text
, blurb :: Text
, defaultValue :: CInt
, setter :: o -> CInt -> IO ()
, getter :: o -> IO CInt
, flags :: Maybe [GParamFlag]
, minValue :: Maybe CInt
, maxValue :: Maybe CInt
}
foreign import ccall g_param_spec_int ::
CString -> CString -> CString -> CInt -> CInt -> CInt -> CInt
-> IO (Ptr GParamSpec)
gParamSpecCInt :: GObject o => CIntPropertyInfo o -> IO GParamSpec
gParamSpecCInt (CIntPropertyInfo {..}) =
withTextCString name $ \cname ->
withTextCString nick $ \cnick ->
withTextCString blurb $ \cblurb -> do
pspecPtr <- g_param_spec_int cname cnick cblurb
(fromMaybe minBound minValue)
(fromMaybe maxBound maxValue)
defaultValue
(maybe defaultFlags gflagsToWord flags)
quark <- pspecQuark
gParamSpecSetQData pspecPtr quark (wrapGetSet getter setter gvalueSet_)
wrapGParamSpecPtr pspecPtr
data CStringPropertyInfo o = CStringPropertyInfo
{ name :: Text
, nick :: Text
, blurb :: Text
, defaultValue :: Maybe Text
, flags :: Maybe [GParamFlag]
, setter :: o -> Maybe Text -> IO ()
, getter :: o -> IO (Maybe Text)
}
foreign import ccall g_param_spec_string ::
CString -> CString -> CString -> CString -> CInt -> IO (Ptr GParamSpec)
gParamSpecCString :: GObject o => CStringPropertyInfo o -> IO GParamSpec
gParamSpecCString (CStringPropertyInfo {..}) =
withTextCString name $ \cname ->
withTextCString nick $ \cnick ->
withTextCString blurb $ \cblurb -> do
pspecPtr <- case defaultValue of
Nothing -> g_param_spec_string cname cnick cblurb nullPtr
(maybe defaultFlags gflagsToWord flags)
Just value ->
withTextCString value $ \cdefault ->
g_param_spec_string cname cnick cblurb cdefault
(maybe defaultFlags gflagsToWord flags)
quark <- pspecQuark
gParamSpecSetQData pspecPtr quark (wrapGetSet getter setter gvalueSet_)
wrapGParamSpecPtr pspecPtr
foreign import ccall g_param_spec_set_qdata_full ::
Ptr GParamSpec -> GQuark a -> Ptr b -> FunPtr (Ptr c -> IO ()) -> IO ()
foreign import ccall "&hs_free_stable_ptr" ptr_to_hs_free_stable_ptr ::
FunPtr (Ptr a -> IO ())
gParamSpecSetQData :: Ptr GParamSpec -> GQuark a -> a -> IO ()
gParamSpecSetQData pspecPtr quark d = do
ptr <- newStablePtr d
g_param_spec_set_qdata_full pspecPtr quark
(castStablePtrToPtr ptr)
ptr_to_hs_free_stable_ptr
foreign import ccall g_param_spec_get_qdata ::
Ptr GParamSpec -> GQuark a -> IO (Ptr b)
gParamSpecGetQData :: Ptr GParamSpec -> GQuark a -> IO (Maybe a)
gParamSpecGetQData pspecPtr quark = do
ptr <- g_param_spec_get_qdata pspecPtr quark
if ptr /= nullPtr
then Just <$> deRefStablePtr (castPtrToStablePtr ptr)
else return Nothing
getGParamSpecGetterSetter :: forall o. Ptr GParamSpec ->
IO (Maybe (PropGetSetter o))
getGParamSpecGetterSetter pspecPtr = do
quark <- pspecQuark @o
gParamSpecGetQData pspecPtr quark