{-# language CPP #-}
module Vulkan.Extensions.VK_EXT_acquire_xlib_display  ( acquireXlibDisplayEXT
                                                      , getRandROutputDisplayEXT
                                                      , EXT_ACQUIRE_XLIB_DISPLAY_SPEC_VERSION
                                                      , pattern EXT_ACQUIRE_XLIB_DISPLAY_SPEC_VERSION
                                                      , EXT_ACQUIRE_XLIB_DISPLAY_EXTENSION_NAME
                                                      , pattern EXT_ACQUIRE_XLIB_DISPLAY_EXTENSION_NAME
                                                      , DisplayKHR(..)
                                                      , Display
                                                      , RROutput
                                                      ) where

import Control.Exception.Base (bracket)
import Control.Monad (unless)
import Control.Monad.IO.Class (liftIO)
import Foreign.Marshal.Alloc (callocBytes)
import Foreign.Marshal.Alloc (free)
import GHC.Base (when)
import GHC.IO (throwIO)
import GHC.Ptr (nullFunPtr)
import Control.Monad.Trans.Class (lift)
import Control.Monad.Trans.Cont (evalContT)
import Control.Monad.IO.Class (MonadIO)
import Data.String (IsString)
import Foreign.Storable (Storable(peek))
import GHC.IO.Exception (IOErrorType(..))
import GHC.IO.Exception (IOException(..))
import Foreign.Ptr (FunPtr)
import Foreign.Ptr (Ptr)
import Control.Monad.Trans.Cont (ContT(..))
import Vulkan.NamedType ((:::))
import Vulkan.Extensions.WSITypes (Display)
import Vulkan.Extensions.Handles (DisplayKHR)
import Vulkan.Extensions.Handles (DisplayKHR(..))
import Vulkan.Dynamic (InstanceCmds(pVkAcquireXlibDisplayEXT))
import Vulkan.Dynamic (InstanceCmds(pVkGetRandROutputDisplayEXT))
import Vulkan.Core10.Handles (PhysicalDevice)
import Vulkan.Core10.Handles (PhysicalDevice(..))
import Vulkan.Core10.Handles (PhysicalDevice_T)
import Vulkan.Extensions.WSITypes (RROutput)
import Vulkan.Core10.Enums.Result (Result)
import Vulkan.Core10.Enums.Result (Result(..))
import Vulkan.Exception (VulkanException(..))
import Vulkan.Core10.Enums.Result (Result(SUCCESS))
import Vulkan.Extensions.WSITypes (Display)
import Vulkan.Extensions.Handles (DisplayKHR(..))
import Vulkan.Extensions.WSITypes (RROutput)
foreign import ccall
#if !defined(SAFE_FOREIGN_CALLS)
  unsafe
#endif
  "dynamic" mkVkAcquireXlibDisplayEXT
  :: FunPtr (Ptr PhysicalDevice_T -> Ptr Display -> DisplayKHR -> IO Result) -> Ptr PhysicalDevice_T -> Ptr Display -> DisplayKHR -> IO Result

-- | vkAcquireXlibDisplayEXT - Acquire access to a VkDisplayKHR using Xlib
--
-- = Parameters
--
-- -   @physicalDevice@ The physical device the display is on.
--
-- -   @dpy@ A connection to the X11 server that currently owns @display@.
--
-- -   @display@ The display the caller wishes to control in Vulkan.
--
-- = Description
--
-- All permissions necessary to control the display are granted to the
-- Vulkan instance associated with @physicalDevice@ until the display is
-- released or the X11 connection specified by @dpy@ is terminated.
-- Permission to access the display /may/ be temporarily revoked during
-- periods when the X11 server from which control was acquired itself loses
-- access to @display@. During such periods, operations which require
-- access to the display /must/ fail with an approriate error code. If the
-- X11 server associated with @dpy@ does not own @display@, or if
-- permission to access it has already been acquired by another entity, the
-- call /must/ return the error code
-- 'Vulkan.Core10.Enums.Result.ERROR_INITIALIZATION_FAILED'.
--
-- Note
--
-- One example of when an X11 server loses access to a display is when it
-- loses ownership of its virtual terminal.
--
-- == Return Codes
--
-- [<https://www.khronos.org/registry/vulkan/specs/1.2-extensions/html/vkspec.html#fundamentals-successcodes Success>]
--
--     -   'Vulkan.Core10.Enums.Result.SUCCESS'
--
-- [<https://www.khronos.org/registry/vulkan/specs/1.2-extensions/html/vkspec.html#fundamentals-errorcodes Failure>]
--
--     -   'Vulkan.Core10.Enums.Result.ERROR_INITIALIZATION_FAILED'
--
-- = See Also
--
-- 'Vulkan.Extensions.Handles.DisplayKHR',
-- 'Vulkan.Core10.Handles.PhysicalDevice'
acquireXlibDisplayEXT :: forall io . MonadIO io => PhysicalDevice -> ("dpy" ::: Ptr Display) -> DisplayKHR -> io ()
acquireXlibDisplayEXT :: PhysicalDevice -> ("dpy" ::: Ptr Display) -> DisplayKHR -> io ()
acquireXlibDisplayEXT physicalDevice :: PhysicalDevice
physicalDevice dpy :: "dpy" ::: Ptr Display
dpy display :: DisplayKHR
display = IO () -> io ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> io ()) -> IO () -> io ()
forall a b. (a -> b) -> a -> b
$ do
  let vkAcquireXlibDisplayEXTPtr :: FunPtr
  (Ptr PhysicalDevice_T
   -> ("dpy" ::: Ptr Display) -> DisplayKHR -> IO Result)
vkAcquireXlibDisplayEXTPtr = InstanceCmds
-> FunPtr
     (Ptr PhysicalDevice_T
      -> ("dpy" ::: Ptr Display) -> DisplayKHR -> IO Result)
pVkAcquireXlibDisplayEXT (PhysicalDevice -> InstanceCmds
instanceCmds (PhysicalDevice
physicalDevice :: PhysicalDevice))
  Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (FunPtr
  (Ptr PhysicalDevice_T
   -> ("dpy" ::: Ptr Display) -> DisplayKHR -> IO Result)
vkAcquireXlibDisplayEXTPtr FunPtr
  (Ptr PhysicalDevice_T
   -> ("dpy" ::: Ptr Display) -> DisplayKHR -> IO Result)
-> FunPtr
     (Ptr PhysicalDevice_T
      -> ("dpy" ::: Ptr Display) -> DisplayKHR -> IO Result)
-> Bool
forall a. Eq a => a -> a -> Bool
/= FunPtr
  (Ptr PhysicalDevice_T
   -> ("dpy" ::: Ptr Display) -> DisplayKHR -> IO Result)
forall a. FunPtr a
nullFunPtr) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$
    IOException -> IO ()
forall e a. Exception e => e -> IO a
throwIO (IOException -> IO ()) -> IOException -> IO ()
forall a b. (a -> b) -> a -> b
$ Maybe Handle
-> IOErrorType
-> String
-> String
-> Maybe CInt
-> Maybe String
-> IOException
IOError Maybe Handle
forall a. Maybe a
Nothing IOErrorType
InvalidArgument "" "The function pointer for vkAcquireXlibDisplayEXT is null" Maybe CInt
forall a. Maybe a
Nothing Maybe String
forall a. Maybe a
Nothing
  let vkAcquireXlibDisplayEXT' :: Ptr PhysicalDevice_T
-> ("dpy" ::: Ptr Display) -> DisplayKHR -> IO Result
vkAcquireXlibDisplayEXT' = FunPtr
  (Ptr PhysicalDevice_T
   -> ("dpy" ::: Ptr Display) -> DisplayKHR -> IO Result)
-> Ptr PhysicalDevice_T
-> ("dpy" ::: Ptr Display)
-> DisplayKHR
-> IO Result
mkVkAcquireXlibDisplayEXT FunPtr
  (Ptr PhysicalDevice_T
   -> ("dpy" ::: Ptr Display) -> DisplayKHR -> IO Result)
vkAcquireXlibDisplayEXTPtr
  Result
r <- Ptr PhysicalDevice_T
-> ("dpy" ::: Ptr Display) -> DisplayKHR -> IO Result
vkAcquireXlibDisplayEXT' (PhysicalDevice -> Ptr PhysicalDevice_T
physicalDeviceHandle (PhysicalDevice
physicalDevice)) ("dpy" ::: Ptr Display
dpy) (DisplayKHR
display)
  Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Result
r Result -> Result -> Bool
forall a. Ord a => a -> a -> Bool
< Result
SUCCESS) (VulkanException -> IO ()
forall e a. Exception e => e -> IO a
throwIO (Result -> VulkanException
VulkanException Result
r))


foreign import ccall
#if !defined(SAFE_FOREIGN_CALLS)
  unsafe
#endif
  "dynamic" mkVkGetRandROutputDisplayEXT
  :: FunPtr (Ptr PhysicalDevice_T -> Ptr Display -> RROutput -> Ptr DisplayKHR -> IO Result) -> Ptr PhysicalDevice_T -> Ptr Display -> RROutput -> Ptr DisplayKHR -> IO Result

-- | vkGetRandROutputDisplayEXT - Query the VkDisplayKHR corresponding to an
-- X11 RandR Output
--
-- = Parameters
--
-- -   @physicalDevice@ The physical device to query the display handle on.
--
-- -   @dpy@ A connection to the X11 server from which @rrOutput@ was
--     queried.
--
-- -   @rrOutput@ An X11 RandR output ID.
--
-- -   @pDisplay@ The corresponding 'Vulkan.Extensions.Handles.DisplayKHR'
--     handle will be returned here.
--
-- = Description
--
-- If there is no 'Vulkan.Extensions.Handles.DisplayKHR' corresponding to
-- @rrOutput@ on @physicalDevice@, 'Vulkan.Core10.APIConstants.NULL_HANDLE'
-- /must/ be returned in @pDisplay@.
--
-- == Return Codes
--
-- [<https://www.khronos.org/registry/vulkan/specs/1.2-extensions/html/vkspec.html#fundamentals-successcodes Success>]
--
--     -   'Vulkan.Core10.Enums.Result.SUCCESS'
--
-- = See Also
--
-- 'Vulkan.Extensions.Handles.DisplayKHR',
-- 'Vulkan.Core10.Handles.PhysicalDevice'
getRandROutputDisplayEXT :: forall io . MonadIO io => PhysicalDevice -> ("dpy" ::: Ptr Display) -> RROutput -> io (DisplayKHR)
getRandROutputDisplayEXT :: PhysicalDevice
-> ("dpy" ::: Ptr Display) -> RROutput -> io DisplayKHR
getRandROutputDisplayEXT physicalDevice :: PhysicalDevice
physicalDevice dpy :: "dpy" ::: Ptr Display
dpy rrOutput :: RROutput
rrOutput = IO DisplayKHR -> io DisplayKHR
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO DisplayKHR -> io DisplayKHR)
-> (ContT DisplayKHR IO DisplayKHR -> IO DisplayKHR)
-> ContT DisplayKHR IO DisplayKHR
-> io DisplayKHR
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ContT DisplayKHR IO DisplayKHR -> IO DisplayKHR
forall (m :: * -> *) r. Monad m => ContT r m r -> m r
evalContT (ContT DisplayKHR IO DisplayKHR -> io DisplayKHR)
-> ContT DisplayKHR IO DisplayKHR -> io DisplayKHR
forall a b. (a -> b) -> a -> b
$ do
  let vkGetRandROutputDisplayEXTPtr :: FunPtr
  (Ptr PhysicalDevice_T
   -> ("dpy" ::: Ptr Display)
   -> RROutput
   -> ("pDisplay" ::: Ptr DisplayKHR)
   -> IO Result)
vkGetRandROutputDisplayEXTPtr = InstanceCmds
-> FunPtr
     (Ptr PhysicalDevice_T
      -> ("dpy" ::: Ptr Display)
      -> RROutput
      -> ("pDisplay" ::: Ptr DisplayKHR)
      -> IO Result)
pVkGetRandROutputDisplayEXT (PhysicalDevice -> InstanceCmds
instanceCmds (PhysicalDevice
physicalDevice :: PhysicalDevice))
  IO () -> ContT DisplayKHR IO ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (IO () -> ContT DisplayKHR IO ())
-> IO () -> ContT DisplayKHR IO ()
forall a b. (a -> b) -> a -> b
$ Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (FunPtr
  (Ptr PhysicalDevice_T
   -> ("dpy" ::: Ptr Display)
   -> RROutput
   -> ("pDisplay" ::: Ptr DisplayKHR)
   -> IO Result)
vkGetRandROutputDisplayEXTPtr FunPtr
  (Ptr PhysicalDevice_T
   -> ("dpy" ::: Ptr Display)
   -> RROutput
   -> ("pDisplay" ::: Ptr DisplayKHR)
   -> IO Result)
-> FunPtr
     (Ptr PhysicalDevice_T
      -> ("dpy" ::: Ptr Display)
      -> RROutput
      -> ("pDisplay" ::: Ptr DisplayKHR)
      -> IO Result)
-> Bool
forall a. Eq a => a -> a -> Bool
/= FunPtr
  (Ptr PhysicalDevice_T
   -> ("dpy" ::: Ptr Display)
   -> RROutput
   -> ("pDisplay" ::: Ptr DisplayKHR)
   -> IO Result)
forall a. FunPtr a
nullFunPtr) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$
    IOException -> IO ()
forall e a. Exception e => e -> IO a
throwIO (IOException -> IO ()) -> IOException -> IO ()
forall a b. (a -> b) -> a -> b
$ Maybe Handle
-> IOErrorType
-> String
-> String
-> Maybe CInt
-> Maybe String
-> IOException
IOError Maybe Handle
forall a. Maybe a
Nothing IOErrorType
InvalidArgument "" "The function pointer for vkGetRandROutputDisplayEXT is null" Maybe CInt
forall a. Maybe a
Nothing Maybe String
forall a. Maybe a
Nothing
  let vkGetRandROutputDisplayEXT' :: Ptr PhysicalDevice_T
-> ("dpy" ::: Ptr Display)
-> RROutput
-> ("pDisplay" ::: Ptr DisplayKHR)
-> IO Result
vkGetRandROutputDisplayEXT' = FunPtr
  (Ptr PhysicalDevice_T
   -> ("dpy" ::: Ptr Display)
   -> RROutput
   -> ("pDisplay" ::: Ptr DisplayKHR)
   -> IO Result)
-> Ptr PhysicalDevice_T
-> ("dpy" ::: Ptr Display)
-> RROutput
-> ("pDisplay" ::: Ptr DisplayKHR)
-> IO Result
mkVkGetRandROutputDisplayEXT FunPtr
  (Ptr PhysicalDevice_T
   -> ("dpy" ::: Ptr Display)
   -> RROutput
   -> ("pDisplay" ::: Ptr DisplayKHR)
   -> IO Result)
vkGetRandROutputDisplayEXTPtr
  "pDisplay" ::: Ptr DisplayKHR
pPDisplay <- ((("pDisplay" ::: Ptr DisplayKHR) -> IO DisplayKHR)
 -> IO DisplayKHR)
-> ContT DisplayKHR IO ("pDisplay" ::: Ptr DisplayKHR)
forall k (r :: k) (m :: k -> *) a.
((a -> m r) -> m r) -> ContT r m a
ContT (((("pDisplay" ::: Ptr DisplayKHR) -> IO DisplayKHR)
  -> IO DisplayKHR)
 -> ContT DisplayKHR IO ("pDisplay" ::: Ptr DisplayKHR))
-> ((("pDisplay" ::: Ptr DisplayKHR) -> IO DisplayKHR)
    -> IO DisplayKHR)
-> ContT DisplayKHR IO ("pDisplay" ::: Ptr DisplayKHR)
forall a b. (a -> b) -> a -> b
$ IO ("pDisplay" ::: Ptr DisplayKHR)
-> (("pDisplay" ::: Ptr DisplayKHR) -> IO ())
-> (("pDisplay" ::: Ptr DisplayKHR) -> IO DisplayKHR)
-> IO DisplayKHR
forall a b c. IO a -> (a -> IO b) -> (a -> IO c) -> IO c
bracket (Int -> IO ("pDisplay" ::: Ptr DisplayKHR)
forall a. Int -> IO (Ptr a)
callocBytes @DisplayKHR 8) ("pDisplay" ::: Ptr DisplayKHR) -> IO ()
forall a. Ptr a -> IO ()
free
  Result
_ <- IO Result -> ContT DisplayKHR IO Result
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (IO Result -> ContT DisplayKHR IO Result)
-> IO Result -> ContT DisplayKHR IO Result
forall a b. (a -> b) -> a -> b
$ Ptr PhysicalDevice_T
-> ("dpy" ::: Ptr Display)
-> RROutput
-> ("pDisplay" ::: Ptr DisplayKHR)
-> IO Result
vkGetRandROutputDisplayEXT' (PhysicalDevice -> Ptr PhysicalDevice_T
physicalDeviceHandle (PhysicalDevice
physicalDevice)) ("dpy" ::: Ptr Display
dpy) (RROutput
rrOutput) ("pDisplay" ::: Ptr DisplayKHR
pPDisplay)
  DisplayKHR
pDisplay <- IO DisplayKHR -> ContT DisplayKHR IO DisplayKHR
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (IO DisplayKHR -> ContT DisplayKHR IO DisplayKHR)
-> IO DisplayKHR -> ContT DisplayKHR IO DisplayKHR
forall a b. (a -> b) -> a -> b
$ ("pDisplay" ::: Ptr DisplayKHR) -> IO DisplayKHR
forall a. Storable a => Ptr a -> IO a
peek @DisplayKHR "pDisplay" ::: Ptr DisplayKHR
pPDisplay
  DisplayKHR -> ContT DisplayKHR IO DisplayKHR
forall (f :: * -> *) a. Applicative f => a -> f a
pure (DisplayKHR -> ContT DisplayKHR IO DisplayKHR)
-> DisplayKHR -> ContT DisplayKHR IO DisplayKHR
forall a b. (a -> b) -> a -> b
$ (DisplayKHR
pDisplay)


type EXT_ACQUIRE_XLIB_DISPLAY_SPEC_VERSION = 1

-- No documentation found for TopLevel "VK_EXT_ACQUIRE_XLIB_DISPLAY_SPEC_VERSION"
pattern EXT_ACQUIRE_XLIB_DISPLAY_SPEC_VERSION :: forall a . Integral a => a
pattern $bEXT_ACQUIRE_XLIB_DISPLAY_SPEC_VERSION :: a
$mEXT_ACQUIRE_XLIB_DISPLAY_SPEC_VERSION :: forall r a. Integral a => a -> (Void# -> r) -> (Void# -> r) -> r
EXT_ACQUIRE_XLIB_DISPLAY_SPEC_VERSION = 1


type EXT_ACQUIRE_XLIB_DISPLAY_EXTENSION_NAME = "VK_EXT_acquire_xlib_display"

-- No documentation found for TopLevel "VK_EXT_ACQUIRE_XLIB_DISPLAY_EXTENSION_NAME"
pattern EXT_ACQUIRE_XLIB_DISPLAY_EXTENSION_NAME :: forall a . (Eq a, IsString a) => a
pattern $bEXT_ACQUIRE_XLIB_DISPLAY_EXTENSION_NAME :: a
$mEXT_ACQUIRE_XLIB_DISPLAY_EXTENSION_NAME :: forall r a.
(Eq a, IsString a) =>
a -> (Void# -> r) -> (Void# -> r) -> r
EXT_ACQUIRE_XLIB_DISPLAY_EXTENSION_NAME = "VK_EXT_acquire_xlib_display"