-- GENERATED by C->Haskell Compiler, version 0.17.2 Crystal Seed, 24 Jan 2009 (Haskell)
-- Edit the ORIGNAL .chs file instead!


{-# LINE 1 "./Graphics/Wayland/Internal/Client.chs" #-}
module Graphics.Wayland.Internal.Client (
  Result(..),

  displayConnect, displayConnectName, displayConnectFd, displayDisconnect, displayGetFd,
  displayDispatch, displayDispatchPending,

  displayGetError, displayFlush, displayRoundtrip,

  displayPrepareRead, displayCancelRead, displayReadEvents
  ) where

import Foreign
import Foreign.C.Types
import Foreign.C.String
import System.Posix.Types

import Graphics.Wayland.Internal.SpliceClientInternal
import Graphics.Wayland.Internal.SpliceClient
import Graphics.Wayland.Internal.SpliceClientTypes
import Graphics.Wayland





{-# LINE 25 "./Graphics/Wayland/Internal/Client.chs" #-}


unFd :: Fd -> CInt
unFd (Fd k) = k

makeWith' :: b -> (b -> IO c) -> IO c
makeWith' b f = f b

withNullPtr = makeWith' nullPtr

codeToNothing :: Int -> Int -> Maybe Int
codeToNothing j k
  | j == k    = Nothing
  | otherwise = Just k

codeNeg1ToNothing :: CInt -> Maybe Int
codeNeg1ToNothing = codeToNothing (-1) . fromIntegral

code0ToNothing    :: CInt -> Maybe Int
code0ToNothing    = codeToNothing 0    . fromIntegral

-- Data types

-- In the case of the Client side, these are all just abstract pointer objects.

-- struct wl_display pointer (nocode since its interface is generated in SpliceProtocol)

{-# LINE 51 "./Graphics/Wayland/Internal/Client.chs" #-}


-- -- | struct wl_event_queue pointer (generate type since this is not an interface)
-- {#pointer * event_queue as EventQueue newtype#}

-- -- | struct wl_interface pointer. for internal use only. (proxy typing)
-- {#pointer * interface as Interface newtype#}



-- Functions/methods


-- -- void wl_event_queue_destroy(struct wl_event_queue *queue);
-- {#fun unsafe event_queue_destroy as ^ {`EventQueue'} -> `()'#}

-- void wl_proxy_marshal(struct wl_proxy *p, uint32_t opcode, ...);

-- void wl_proxy_marshal_array(struct wl_proxy *p, uint32_t opcode,
--                               union wl_argument *args);

-- struct wl_proxy *wl_proxy_create(struct wl_proxy *factory,
--                                    const struct wl_interface *interface);

-- struct wl_proxy *wl_proxy_marshal_constructor(struct wl_proxy *proxy,
--                                               uint32_t opcode,
--                                               const struct wl_interface *interface,
--                                               ...);
-- struct wl_proxy *
-- wl_proxy_marshal_array_constructor(struct wl_proxy *proxy,
--                                    uint32_t opcode, union wl_argument *args,
--                                    const struct wl_interface *interface);

-- void wl_proxy_destroy(struct wl_proxy *proxy);
-- int wl_proxy_add_listener(struct wl_proxy *proxy,
--                           void (**implementation)(void), void *data);
-- const void *wl_proxy_get_listener(struct wl_proxy *proxy);
-- int wl_proxy_add_dispatcher(struct wl_proxy *proxy,
--                             wl_dispatcher_func_t dispatcher_func,
--                             const void * dispatcher_data, void *data);
-- void wl_proxy_set_user_data(struct wl_proxy *proxy, void *user_data);
-- void *wl_proxy_get_user_data(struct wl_proxy *proxy);
-- uint32_t wl_proxy_get_id(struct wl_proxy *proxy);
-- const char *wl_proxy_get_class(struct wl_proxy *proxy);
-- void wl_proxy_set_queue(struct wl_proxy *proxy, struct wl_event_queue *queue);


receiveMaybeDisplay :: Display -> Maybe Display
receiveMaybeDisplay (Display x)
  | x == nullPtr = Nothing
  | otherwise    = Just (Display x)

-- struct wl_display *wl_display_connect(const char *name);
-- | Connect to a display with a specified name
displayConnectName :: (String) -> IO ((Maybe Display))
displayConnectName a1 =
  withCString a1 $ \a1' -> 
  displayConnectName'_ a1' >>= \res ->
  let {res' = receiveMaybeDisplay res} in
  return (res')

{-# LINE 105 "./Graphics/Wayland/Internal/Client.chs" #-}


-- | Connect to the default display by passing a null pointer
displayConnect :: IO ((Maybe Display))
displayConnect =
  withNullPtr $ \a1' -> 
  displayConnect'_ a1' >>= \res ->
  let {res' = receiveMaybeDisplay res} in
  return (res')

{-# LINE 108 "./Graphics/Wayland/Internal/Client.chs" #-}


-- struct wl_display *wl_display_connect_to_fd(int fd);
-- | Connect to a display by file descriptor
displayConnectFd :: (Fd) -> IO ((Maybe Display))
displayConnectFd a1 =
  let {a1' = unFd a1} in 
  displayConnectFd'_ a1' >>= \res ->
  let {res' = receiveMaybeDisplay res} in
  return (res')

{-# LINE 112 "./Graphics/Wayland/Internal/Client.chs" #-}


-- void wl_display_disconnect(struct wl_display *display);
displayDisconnect :: (Display) -> IO ()
displayDisconnect a1 =
  let {a1' = id a1} in 
  displayDisconnect'_ a1' >>
  return ()

{-# LINE 115 "./Graphics/Wayland/Internal/Client.chs" #-}


-- int wl_display_get_fd(struct wl_display *display);
displayGetFd :: (Display) -> IO ((Fd))
displayGetFd a1 =
  let {a1' = id a1} in 
  displayGetFd'_ a1' >>= \res ->
  let {res' = Fd res} in
  return (res')

{-# LINE 118 "./Graphics/Wayland/Internal/Client.chs" #-}


-- int wl_display_dispatch(struct wl_display *display);
-- | wl_display_dispatch. Returns @Nothing@ on failure or @Just k@ if k events were processed.
--
-- Strictly safe!!! This *will* call back into Haskell code!
displayDispatch :: (Display) -> IO ((Maybe Int))
displayDispatch a1 =
  let {a1' = id a1} in 
  displayDispatch'_ a1' >>= \res ->
  let {res' = codeNeg1ToNothing res} in
  return (res')

{-# LINE 124 "./Graphics/Wayland/Internal/Client.chs" #-}


-- -- int wl_display_dispatch_queue(struct wl_display *display,
-- --                               struct wl_event_queue *queue);
-- -- | wl_display_dispatch_queue. Returns @Nothing@ on failure or @Just k@ if k events were processed.
-- --
-- -- Strictly safe!!! This *will* call back into Haskell code!
-- {#fun display_dispatch_queue as displayDispatchQueue {`Display', `EventQueue'} -> `Maybe Int' codeNeg1ToNothing #}

-- -- int wl_display_dispatch_queue_pending(struct wl_display *display,
-- --                                       struct wl_event_queue *queue);
-- -- | wl_display_dispatch_queue_pending. Returns @Nothing@ on failure or @Just k@ if k events were processed.
-- --
-- -- Strictly safe!!! This *will* call back into Haskell code!
-- {#fun display_dispatch_queue_pending as displayDispatchQueuePending {`Display', `EventQueue'} -> `Maybe Int' codeNeg1ToNothing #}

-- int wl_display_dispatch_pending(struct wl_display *display);
-- | wl_display_dispatch_pending. Returns @Nothing@ on failure or @Just k@ if k events were processed.
--
-- Strictly safe!!! This *will* call back into Haskell code!
displayDispatchPending :: (Display) -> IO ((Maybe Int))
displayDispatchPending a1 =
  let {a1' = id a1} in 
  displayDispatchPending'_ a1' >>= \res ->
  let {res' = codeNeg1ToNothing res} in
  return (res')

{-# LINE 144 "./Graphics/Wayland/Internal/Client.chs" #-}


-- int wl_display_get_error(struct wl_display *display);
-- | @Nothing@ if no error occurred or @Just k@ if the latest error had code k
--
-- Note (from the wayland documentation): errors are fatal. If this function returns a @Just@ value, the display can no longer be used.
displayGetError :: (Display) -> IO ((Maybe Int))
displayGetError a1 =
  let {a1' = id a1} in 
  displayGetError'_ a1' >>= \res ->
  let {res' = code0ToNothing res} in
  return (res')

{-# LINE 150 "./Graphics/Wayland/Internal/Client.chs" #-}


-- int wl_display_flush(struct wl_display *display);
-- | @Nothing@ on failure or @Just k@ if k bytes were sent
--
-- __It is not clear to me if this is can be unsafe (ie. can this call back into haskell code?).__
displayFlush :: (Display) -> IO ((Maybe Int))
displayFlush a1 =
  let {a1' = id a1} in 
  displayFlush'_ a1' >>= \res ->
  let {res' = codeNeg1ToNothing res} in
  return (res')

{-# LINE 156 "./Graphics/Wayland/Internal/Client.chs" #-}


-- int wl_display_roundtrip(struct wl_display *display);
-- | @Nothing@ on failure or @Just k@ if k events were dispatched.
--
-- __It is not clear to me if this is can be unsafe (ie. can this call back into haskell code?).__
displayRoundtrip :: (Display) -> IO ((Maybe Int))
displayRoundtrip a1 =
  let {a1' = id a1} in 
  displayRoundtrip'_ a1' >>= \res ->
  let {res' = codeNeg1ToNothing res} in
  return (res')

{-# LINE 162 "./Graphics/Wayland/Internal/Client.chs" #-}


-- -- struct wl_event_queue *wl_display_create_queue(struct wl_display *display);
-- -- | Docs say that wl_display_create_queue may return NULL on failure, but that only happens when it's out of memory
-- {#fun unsafe display_create_queue as displayCreateQueue {`Display'} -> `EventQueue' #}

-- -- int wl_display_prepare_read_queue(struct wl_display *display,
-- --                                   struct wl_event_queue *queue);
-- {#fun unsafe display_prepare_read_queue as displayPrepareReadQueue {`Display', `EventQueue'} -> `Result' errToResult #}

-- int wl_display_prepare_read(struct wl_display *display);
displayPrepareRead :: (Display) -> IO ((Result))
displayPrepareRead a1 =
  let {a1' = id a1} in 
  displayPrepareRead'_ a1' >>= \res ->
  let {res' = errToResult res} in
  return (res')

{-# LINE 173 "./Graphics/Wayland/Internal/Client.chs" #-}


-- void wl_display_cancel_read(struct wl_display *display);
displayCancelRead :: (Display) -> IO ()
displayCancelRead a1 =
  let {a1' = id a1} in 
  displayCancelRead'_ a1' >>
  return ()

{-# LINE 176 "./Graphics/Wayland/Internal/Client.chs" #-}


-- int wl_display_read_events(struct wl_display *display);
-- | This will read events from the file descriptor for the display.
--   This function does not dispatch events, it only reads and queues events into their corresponding event queues.
--
--   Before calling this function, wl_display_prepare_read() must be called first.
displayReadEvents :: (Display) -> IO ((Result))
displayReadEvents a1 =
  let {a1' = id a1} in 
  displayReadEvents'_ a1' >>= \res ->
  let {res' = errToResult res} in
  return (res')

{-# LINE 183 "./Graphics/Wayland/Internal/Client.chs" #-}


-- void wl_log_set_handler_client(wl_log_func_t handler);

foreign import ccall unsafe "Graphics/Wayland/Internal/Client.chs.h wl_display_connect"
  displayConnectName'_ :: ((Ptr CChar) -> (IO (Display)))

foreign import ccall unsafe "Graphics/Wayland/Internal/Client.chs.h wl_display_connect"
  displayConnect'_ :: ((Ptr CChar) -> (IO (Display)))

foreign import ccall unsafe "Graphics/Wayland/Internal/Client.chs.h wl_display_connect_to_fd"
  displayConnectFd'_ :: (CInt -> (IO (Display)))

foreign import ccall unsafe "Graphics/Wayland/Internal/Client.chs.h wl_display_disconnect"
  displayDisconnect'_ :: ((Display) -> (IO ()))

foreign import ccall unsafe "Graphics/Wayland/Internal/Client.chs.h wl_display_get_fd"
  displayGetFd'_ :: ((Display) -> (IO CInt))

foreign import ccall safe "Graphics/Wayland/Internal/Client.chs.h wl_display_dispatch"
  displayDispatch'_ :: ((Display) -> (IO CInt))

foreign import ccall safe "Graphics/Wayland/Internal/Client.chs.h wl_display_dispatch_pending"
  displayDispatchPending'_ :: ((Display) -> (IO CInt))

foreign import ccall unsafe "Graphics/Wayland/Internal/Client.chs.h wl_display_get_error"
  displayGetError'_ :: ((Display) -> (IO CInt))

foreign import ccall safe "Graphics/Wayland/Internal/Client.chs.h wl_display_flush"
  displayFlush'_ :: ((Display) -> (IO CInt))

foreign import ccall safe "Graphics/Wayland/Internal/Client.chs.h wl_display_roundtrip"
  displayRoundtrip'_ :: ((Display) -> (IO CInt))

foreign import ccall unsafe "Graphics/Wayland/Internal/Client.chs.h wl_display_prepare_read"
  displayPrepareRead'_ :: ((Display) -> (IO CInt))

foreign import ccall unsafe "Graphics/Wayland/Internal/Client.chs.h wl_display_cancel_read"
  displayCancelRead'_ :: ((Display) -> (IO ()))

foreign import ccall unsafe "Graphics/Wayland/Internal/Client.chs.h wl_display_read_events"
  displayReadEvents'_ :: ((Display) -> (IO CInt))