-- 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/Server.chs" #-}
-- Trying my best to piss off the Safe Haskell guys
{-# LANGUAGE TemplateHaskell, GeneralizedNewtypeDeriving #-}
module Graphics.Wayland.Internal.Server (
  ClientState(..), clientStateReadable, clientStateWritable, clientStateHangup,
  clientStateError,

  EventLoop, EventSource,

  EventLoopFdFunc, EventLoopTimerFunc, EventLoopSignalFunc, EventLoopIdleFunc,

  eventLoopCreate, eventLoopDestroy, eventLoopAddFd, eventSourceFdUpdate,
  eventLoopAddTimer, eventLoopAddSignal, eventSourceTimerUpdate, eventSourceRemove,
  eventSourceCheck, eventLoopDispatch, eventLoopDispatchIdle, eventLoopAddIdle, eventLoopGetFd,

  DisplayServer, displayCreate, displayDestroy, displayGetEventLoop, displayAddSocket,
  displayTerminate, displayRun, displayFlushClients, displayGetSerial, displayNextSerial,

  clientCreate, clientDestroy, clientFlush, clientGetCredentials, clientPostNoMemory,

  ShmBuffer, shmBufferBeginAccess, shmBufferEndAccess, shmBufferGet, shmBufferGetData,
  shmBufferGetStride, shmBufferGetFormat, shmBufferGetWidth, shmBufferGetHeight,
  displayInitShm, displayAddShmFormat, shmBufferCreate
  ) where

import Control.Monad (liftM)
import Data.Functor ((<$>))
import Data.Flags
import Data.Flags.TH
import Foreign
import Foreign.C.Types
import Foreign.C.String
import System.Posix.Types

import Graphics.Wayland.Internal.ServerClientState -- for the WL_EVENT_* constants
import Graphics.Wayland.Internal.SpliceServerInternal
import Graphics.Wayland.Internal.SpliceServer
import Graphics.Wayland.Internal.SpliceServerTypes
import Graphics.Wayland.Internal.Util (Client(..))
import Graphics.Wayland





{-# LINE 44 "./Graphics/Wayland/Internal/Server.chs" #-}






boolToCInt :: Bool -> CInt
boolToCInt True = 1
boolToCInt False = 0



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

makeWith :: (a -> IO b) -> (a -> (b -> IO c) -> IO c)
makeWith func = \ a f -> do
  b <- func a
  f b

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

withNullPtr = makeWith' nullPtr

-- | enum {
-- 	WL_EVENT_READABLE = 0x01,
-- 	WL_EVENT_WRITABLE = 0x02,
-- 	WL_EVENT_HANGUP   = 0x04,
-- 	WL_EVENT_ERROR    = 0x08
-- };
--
-- The "uint32_t mask" argument passed to a variety of functions in this file is a bitmask
-- detailing the state of the client.
$(bitmaskWrapper "ClientState" ''CUInt [''Num, ''Integral, ''Real, ''Enum, ''Ord] [
  ("clientStateReadable", fromIntegral $ fromEnum ClientReadable),
  ("clientStateWritable", fromIntegral $  fromEnum ClientWritable),
  ("clientStateHangup",   fromIntegral $  fromEnum ClientHangup),
  ("clientStateError",    fromIntegral $  fromEnum ClientError)
  ])

-- | struct wl_event_loop;
newtype EventLoop = EventLoop (Ptr (EventLoop))
{-# LINE 86 "./Graphics/Wayland/Internal/Server.chs" #-}


-- | struct wl_event_source;
newtype EventSource = EventSource (Ptr (EventSource))
{-# LINE 89 "./Graphics/Wayland/Internal/Server.chs" #-}


type CEventLoopFdFunc = CInt -> (CUInt) -> Ptr () -> IO CInt
-- | typedef int (*wl_event_loop_fd_func_t)(int fd, uint32_t mask, void *data);
type EventLoopFdFunc = Int -> ClientState -> IO Bool
foreign import ccall unsafe "wrapper" makeFdFunPtr :: CEventLoopFdFunc -> IO (FunPtr CEventLoopFdFunc)
marshallEventLoopFdFunc :: EventLoopFdFunc -> IO (FunPtr CEventLoopFdFunc)
marshallEventLoopFdFunc func = makeFdFunPtr $ \ fd mask _ -> boolToCInt <$> func (fromIntegral fd) (fromIntegral mask)
melff = makeWith marshallEventLoopFdFunc

type CEventLoopTimerFunc = Ptr () -> IO CInt
-- | typedef int (*wl_event_loop_timer_func_t)(void *data);
type EventLoopTimerFunc = IO Bool
foreign import ccall unsafe "wrapper" makeTimerFunPtr :: CEventLoopTimerFunc -> IO (FunPtr CEventLoopTimerFunc)
marshallEventLoopTimerFunc :: EventLoopTimerFunc -> IO (FunPtr CEventLoopTimerFunc)
marshallEventLoopTimerFunc func = makeTimerFunPtr $ \ _ -> boolToCInt <$> func
meltf = makeWith marshallEventLoopTimerFunc

type CEventLoopSignalFunc = CInt -> Ptr () -> IO CInt
-- | typedef int (*wl_event_loop_signal_func_t)(int signal_number, void *data);
type EventLoopSignalFunc = Int -> IO Bool
foreign import ccall unsafe "wrapper" makeSignalFunPtr :: CEventLoopSignalFunc -> IO (FunPtr CEventLoopSignalFunc)
marshallEventLoopSignalFunc :: EventLoopSignalFunc -> IO (FunPtr CEventLoopSignalFunc)
marshallEventLoopSignalFunc func = makeSignalFunPtr $ \ x _ -> boolToCInt <$> func (fromIntegral x)
melsf = makeWith marshallEventLoopSignalFunc

-- typedef void (*wl_event_loop_idle_func_t)(void *data);
type CEventLoopIdleFunc = Ptr () -> IO ()
type EventLoopIdleFunc = IO ()
foreign import ccall unsafe "wrapper" makeIdleFunPtr :: CEventLoopIdleFunc -> IO (FunPtr CEventLoopIdleFunc)
marshallEventLoopIdleFunc :: EventLoopIdleFunc -> IO (FunPtr CEventLoopIdleFunc)
marshallEventLoopIdleFunc func = makeIdleFunPtr $ \ _ -> func
melif = makeWith marshallEventLoopIdleFunc

-- |struct wl_event_loop *wl_event_loop_create(void);
eventLoopCreate :: IO ((EventLoop))
eventLoopCreate =
  eventLoopCreate'_ >>= \res ->
  let {res' = id res} in
  return (res')

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


-- |void wl_event_loop_destroy(struct wl_event_loop *loop);
eventLoopDestroy :: (EventLoop) -> IO ()
eventLoopDestroy a1 =
  let {a1' = id a1} in 
  eventLoopDestroy'_ a1' >>
  return ()

{-# LINE 127 "./Graphics/Wayland/Internal/Server.chs" #-}


-- | struct wl_event_source *wl_event_loop_add_fd(struct wl_event_loop *loop,
-- 					     int fd, uint32_t mask,
-- 					     wl_event_loop_fd_func_t func,
-- 					     void *data);
eventLoopAddFd :: (EventLoop) -> (Fd) -> (ClientState) -> (EventLoopFdFunc) -> IO ((EventSource))
eventLoopAddFd a1 a2 a3 a4 =
  let {a1' = id a1} in 
  let {a2' = unFd a2} in 
  let {a3' = fromIntegral a3} in 
  melff a4 $ \a4' -> 
  withNullPtr $ \a5' -> 
  eventLoopAddFd'_ a1' a2' a3' a4' a5' >>= \res ->
  let {res' = id res} in
  return (res')

{-# LINE 133 "./Graphics/Wayland/Internal/Server.chs" #-}


-- | int wl_event_source_fd_update(struct wl_event_source *source, uint32_t mask);
eventSourceFdUpdate :: (EventSource) -> (ClientState) -> IO ((Result))
eventSourceFdUpdate a1 a2 =
  let {a1' = id a1} in 
  let {a2' = fromIntegral a2} in 
  eventSourceFdUpdate'_ a1' a2' >>= \res ->
  let {res' = errToResult res} in
  return (res')

{-# LINE 136 "./Graphics/Wayland/Internal/Server.chs" #-}


-- | struct wl_event_source *wl_event_loop_add_timer(struct wl_event_loop *loop,
-- 						wl_event_loop_timer_func_t func,
-- 						void *data);
eventLoopAddTimer :: (EventLoop) -> (EventLoopTimerFunc) -> IO ((EventSource))
eventLoopAddTimer a1 a2 =
  let {a1' = id a1} in 
  meltf a2 $ \a2' -> 
  withNullPtr $ \a3' -> 
  eventLoopAddTimer'_ a1' a2' a3' >>= \res ->
  let {res' = id res} in
  return (res')

{-# LINE 141 "./Graphics/Wayland/Internal/Server.chs" #-}


-- | struct wl_event_source *
-- wl_event_loop_add_signal(struct wl_event_loop *loop,
-- 			int signal_number,
-- 			wl_event_loop_signal_func_t func,
-- 			void *data);
eventLoopAddSignal :: (EventLoop) -> (Int) -> (EventLoopSignalFunc) -> IO ((EventSource))
eventLoopAddSignal a1 a2 a3 =
  let {a1' = id a1} in 
  let {a2' = fromIntegral a2} in 
  melsf a3 $ \a3' -> 
  withNullPtr $ \a4' -> 
  eventLoopAddSignal'_ a1' a2' a3' a4' >>= \res ->
  let {res' = id res} in
  return (res')

{-# LINE 148 "./Graphics/Wayland/Internal/Server.chs" #-}


-- | int wl_event_source_timer_update(struct wl_event_source *source,
-- 				 int ms_delay);
eventSourceTimerUpdate :: (EventSource) -> (Int) -> IO ((Result))
eventSourceTimerUpdate a1 a2 =
  let {a1' = id a1} in 
  let {a2' = fromIntegral a2} in 
  eventSourceTimerUpdate'_ a1' a2' >>= \res ->
  let {res' = errToResult res} in
  return (res')

{-# LINE 152 "./Graphics/Wayland/Internal/Server.chs" #-}


-- | int wl_event_source_remove(struct wl_event_source *source);
eventSourceRemove :: (EventSource) -> IO ()
eventSourceRemove a1 =
  let {a1' = id a1} in 
  eventSourceRemove'_ a1' >>
  return ()

{-# LINE 155 "./Graphics/Wayland/Internal/Server.chs" #-}


-- | void wl_event_source_check(struct wl_event_source *source);
eventSourceCheck :: (EventSource) -> IO ()
eventSourceCheck a1 =
  let {a1' = id a1} in 
  eventSourceCheck'_ a1' >>
  return ()

{-# LINE 158 "./Graphics/Wayland/Internal/Server.chs" #-}


-- | int wl_event_loop_dispatch(struct wl_event_loop *loop, int timeout);
--
-- SAFE!!
eventLoopDispatch :: (EventLoop) -> (Int) -> IO ((Result))
eventLoopDispatch a1 a2 =
  let {a1' = id a1} in 
  let {a2' = fromIntegral a2} in 
  eventLoopDispatch'_ a1' a2' >>= \res ->
  let {res' = errToResult res} in
  return (res')

{-# LINE 163 "./Graphics/Wayland/Internal/Server.chs" #-}


-- | void wl_event_loop_dispatch_idle(struct wl_event_loop *loop);
eventLoopDispatchIdle :: (EventLoop) -> IO ()
eventLoopDispatchIdle a1 =
  let {a1' = id a1} in 
  eventLoopDispatchIdle'_ a1' >>
  return ()

{-# LINE 166 "./Graphics/Wayland/Internal/Server.chs" #-}


-- | struct wl_event_source *wl_event_loop_add_idle(struct wl_event_loop *loop,
-- 					       wl_event_loop_idle_func_t func,
-- 					       void *data);
eventLoopAddIdle :: (EventLoop) -> (EventLoopIdleFunc) -> IO ((EventSource))
eventLoopAddIdle a1 a2 =
  let {a1' = id a1} in 
  melif a2 $ \a2' -> 
  withNullPtr $ \a3' -> 
  eventLoopAddIdle'_ a1' a2' a3' >>= \res ->
  let {res' = id res} in
  return (res')

{-# LINE 171 "./Graphics/Wayland/Internal/Server.chs" #-}


-- | int wl_event_loop_get_fd(struct wl_event_loop *loop);
eventLoopGetFd :: (EventLoop) -> IO ((Fd))
eventLoopGetFd a1 =
  let {a1' = id a1} in 
  eventLoopGetFd'_ a1' >>= \res ->
  let {res' = Fd res} in
  return (res')

{-# LINE 174 "./Graphics/Wayland/Internal/Server.chs" #-}



-- EXPOSED UNTIL HERE


-- struct wl_client;
-- defined in .Util

{-# LINE 182 "./Graphics/Wayland/Internal/Server.chs" #-}


receiveMaybeClient :: Client -> Maybe Client
receiveMaybeClient (Client x)
  | x == nullPtr = Nothing
  | otherwise    = Just (Client x)

-- |struct wl_display;
--
-- this is called a Compositor in e.g weston, QtWayland
--
-- this is NOT an instance of a wl_resource or a wl_proxy! it is a global server status singleton listing e.g. connected clients.
newtype DisplayServer = DisplayServer (Ptr (DisplayServer))
{-# LINE 194 "./Graphics/Wayland/Internal/Server.chs" #-}


-- struct wl_listener;
-- struct wl_resource;
-- struct wl_global;

-- typedef void (*wl_notify_func_t)(struct wl_listener *listener, void *data);
-- void wl_event_loop_add_destroy_listener(struct wl_event_loop *loop,
-- 					struct wl_listener * listener);
-- struct wl_listener *wl_event_loop_get_destroy_listener(
-- 					struct wl_event_loop *loop,
-- 					wl_notify_func_t notify);

-- | struct wl_display *wl_display_create(void);
displayCreate :: IO ((DisplayServer))
displayCreate =
  displayCreate'_ >>= \res ->
  let {res' = id res} in
  return (res')

{-# LINE 208 "./Graphics/Wayland/Internal/Server.chs" #-}


-- | void wl_display_destroy(struct wl_display *display);
displayDestroy :: (DisplayServer) -> IO ()
displayDestroy a1 =
  let {a1' = id a1} in 
  displayDestroy'_ a1' >>
  return ()

{-# LINE 211 "./Graphics/Wayland/Internal/Server.chs" #-}


-- | struct wl_event_loop *wl_display_get_event_loop(struct wl_display *display);
displayGetEventLoop :: (DisplayServer) -> IO ((EventLoop))
displayGetEventLoop a1 =
  let {a1' = id a1} in 
  displayGetEventLoop'_ a1' >>= \res ->
  let {res' = id res} in
  return (res')

{-# LINE 214 "./Graphics/Wayland/Internal/Server.chs" #-}


-- | int wl_display_add_socket(struct wl_display *display, const char *name);
withMaybeCString :: Maybe String -> (CString -> IO a) -> IO a
withMaybeCString Nothing fun = fun nullPtr
withMaybeCString (Just str) fun = withCString str fun
displayAddSocket :: (DisplayServer) -> (Maybe String) -> IO ((Result))
displayAddSocket a1 a2 =
  let {a1' = id a1} in 
  withMaybeCString a2 $ \a2' -> 
  displayAddSocket'_ a1' a2' >>= \res ->
  let {res' = errToResult res} in
  return (res')

{-# LINE 220 "./Graphics/Wayland/Internal/Server.chs" #-}


-- | void wl_display_terminate(struct wl_display *display);
displayTerminate :: (DisplayServer) -> IO ()
displayTerminate a1 =
  let {a1' = id a1} in 
  displayTerminate'_ a1' >>
  return ()

{-# LINE 223 "./Graphics/Wayland/Internal/Server.chs" #-}


-- | void wl_display_run(struct wl_display *display);
--
-- STRICTLY SAFE!!!
displayRun :: (DisplayServer) -> IO ()
displayRun a1 =
  let {a1' = id a1} in 
  displayRun'_ a1' >>
  return ()

{-# LINE 228 "./Graphics/Wayland/Internal/Server.chs" #-}


-- | void wl_display_flush_clients(struct wl_display *display);
displayFlushClients :: (DisplayServer) -> IO ()
displayFlushClients a1 =
  let {a1' = id a1} in 
  displayFlushClients'_ a1' >>
  return ()

{-# LINE 231 "./Graphics/Wayland/Internal/Server.chs" #-}


-- typedef void (*wl_global_bind_func_t)(struct wl_client *client, void *data,
-- 				      uint32_t version, uint32_t id);

-- not sure what these two functions are for
-- | uint32_t wl_display_get_serial(struct wl_display *display);
displayGetSerial :: (DisplayServer) -> IO ((Word))
displayGetSerial a1 =
  let {a1' = id a1} in 
  displayGetSerial'_ a1' >>= \res ->
  let {res' = fromIntegral res} in
  return (res')

{-# LINE 238 "./Graphics/Wayland/Internal/Server.chs" #-}


-- | uint32_t wl_display_next_serial(struct wl_display *display);
displayNextSerial :: (DisplayServer) -> IO ((Word))
displayNextSerial a1 =
  let {a1' = id a1} in 
  displayNextSerial'_ a1' >>= \res ->
  let {res' = fromIntegral res} in
  return (res')

{-# LINE 241 "./Graphics/Wayland/Internal/Server.chs" #-}


-- void wl_display_add_destroy_listener(struct wl_display *display,
-- 				     struct wl_listener *listener);
-- struct wl_listener *wl_display_get_destroy_listener(struct wl_display *display,
-- 						    wl_notify_func_t notify);

-- struct wl_global *wl_global_create(struct wl_display *display,
-- 				   const struct wl_interface *interface,
-- 				   int version,
-- 				   void *data, wl_global_bind_func_t bind);
-- void wl_global_destroy(struct wl_global *global);

-- | struct wl_client *wl_client_create(struct wl_display *display, int fd);
clientCreate :: (DisplayServer) -> (Fd) -> IO ((Maybe Client))
clientCreate a1 a2 =
  let {a1' = id a1} in 
  let {a2' = unFd a2} in 
  clientCreate'_ a1' a2' >>= \res ->
  let {res' = receiveMaybeClient res} in
  return (res')

{-# LINE 255 "./Graphics/Wayland/Internal/Server.chs" #-}


-- | void wl_client_destroy(struct wl_client *client);
clientDestroy :: (Client) -> IO ()
clientDestroy a1 =
  let {a1' = id a1} in 
  clientDestroy'_ a1' >>
  return ()

{-# LINE 258 "./Graphics/Wayland/Internal/Server.chs" #-}


-- | void wl_client_flush(struct wl_client *client);
clientFlush :: (Client) -> IO ()
clientFlush a1 =
  let {a1' = id a1} in 
  clientFlush'_ a1' >>
  return ()

{-# LINE 261 "./Graphics/Wayland/Internal/Server.chs" #-}


peekPid = liftM CPid . liftM fromIntegral . peek
peekUid = liftM CUid . liftM fromIntegral . peek
peekGid = liftM CGid . liftM fromIntegral . peek
-- | void wl_client_get_credentials(struct wl_client *client,
-- 			       pid_t *pid, uid_t *uid, gid_t *gid);
clientGetCredentials :: (Client) -> IO ((ProcessID), (UserID), (GroupID))
clientGetCredentials a1 =
  let {a1' = id a1} in 
  alloca $ \a2' -> 
  alloca $ \a3' -> 
  alloca $ \a4' -> 
  clientGetCredentials'_ a1' a2' a3' a4' >>
  peekPid  a2'>>= \a2'' -> 
  peekUid  a3'>>= \a3'' -> 
  peekGid  a4'>>= \a4'' -> 
  return (a2'', a3'', a4'')

{-# LINE 268 "./Graphics/Wayland/Internal/Server.chs" #-}


-- void wl_client_add_destroy_listener(struct wl_client *client,
-- 				    struct wl_listener *listener);
-- struct wl_listener *wl_client_get_destroy_listener(struct wl_client *client,
-- 						   wl_notify_func_t notify);

-- this function should not be needed
-- struct wl_resource *
-- wl_client_get_object(struct wl_client *client, uint32_t id);
-- void
-- | wl_client_post_no_memory(struct wl_client *client);
clientPostNoMemory :: (Client) -> IO ()
clientPostNoMemory a1 =
  let {a1' = id a1} in 
  clientPostNoMemory'_ a1' >>
  return ()

{-# LINE 280 "./Graphics/Wayland/Internal/Server.chs" #-}


-- /** \class wl_listener
--  *
--  * \brief A single listener for Wayland signals
--  *
--  * wl_listener provides the means to listen for wl_signal notifications. Many
--  * Wayland objects use wl_listener for notification of significant events like
--  * object destruction.
--  *
--  * Clients should create wl_listener objects manually and can register them as
--  * listeners to signals using #wl_signal_add, assuming the signal is
--  * directly accessible. For opaque structs like wl_event_loop, adding a
--  * listener should be done through provided accessor methods. A listener can
--  * only listen to one signal at a time.
--  *
--  * ~~~
--  * struct wl_listener your_listener;
--  *
--  * your_listener.notify = your_callback_method;
--  *
--  * \comment{Direct access}
--  * wl_signal_add(&some_object->destroy_signal, &your_listener);
--  *
--  * \comment{Accessor access}
--  * wl_event_loop *loop = ...;
--  * wl_event_loop_add_destroy_listener(loop, &your_listener);
--  * ~~~
--  *
--  * If the listener is part of a larger struct, #wl_container_of can be used
--  * to retrieve a pointer to it:
--  *
--  * \code
--  * void your_listener(struct wl_listener *listener, void *data)
--  * {
--  * 	struct your_data *data;
--  *
--  * 	your_data = wl_container_of(listener, data, your_member_name);
--  * }
--  * \endcode
--  *
--  * If you need to remove a listener from a signal, use #wl_list_remove.
--  *
--  * \code
--  * wl_list_remove(&your_listener.link);
--  * \endcode
--  *
--  * \sa wl_signal
--  */
-- struct wl_listener {
-- 	struct wl_list link;
-- 	wl_notify_func_t notify;
-- };

-- /** \class wl_signal
--  *
--  * \brief A source of a type of observable event
--  *
--  * Signals are recognized points where significant events can be observed.
--  * Compositors as well as the server can provide signals. Observers are
--  * wl_listener's that are added through #wl_signal_add. Signals are emitted
--  * using #wl_signal_emit, which will invoke all listeners until that
--  * listener is removed by #wl_list_remove (or whenever the signal is
--  * destroyed).
--  *
--  * \sa wl_listener for more information on using wl_signal
--  */
-- struct wl_signal {
-- 	struct wl_list listener_list;
-- };

-- /** Initialize a new \ref wl_signal for use.
--  *
--  * \param signal The signal that will be initialized
--  *
--  * \memberof wl_signal
--  */
-- static inline void
-- wl_signal_init(struct wl_signal *signal)
-- {
-- 	wl_list_init(&signal->listener_list);
-- }

-- /** Add the specified listener to this signal.
--  *
--  * \param signal The signal that will emit events to the listener
--  * \param listener The listener to add
--  *
--  * \memberof wl_signal
--  */
-- static inline void
-- wl_signal_add(struct wl_signal *signal, struct wl_listener *listener)
-- {
-- 	wl_list_insert(signal->listener_list.prev, &listener->link);
-- }

-- /** Gets the listener struct for the specified callback.
--  *
--  * \param signal The signal that contains the specified listener
--  * \param notify The listener that is the target of this search
--  * \return the list item that corresponds to the specified listener, or NULL
--  * if none was found
--  *
--  * \memberof wl_signal
--  */
-- static inline struct wl_listener *
-- wl_signal_get(struct wl_signal *signal, wl_notify_func_t notify)
-- {
-- 	struct wl_listener *l;

-- 	wl_list_for_each(l, &signal->listener_list, link)
-- 		if (l->notify == notify)
-- 			return l;

-- 	return NULL;
-- }

-- /** Emits this signal, notifying all registered listeners.
--  *
--  * \param signal The signal object that will emit the signal
--  * \param data The data that will be emitted with the signal
--  *
--  * \memberof wl_signal
--  */
-- static inline void
-- wl_signal_emit(struct wl_signal *signal, void *data)
-- {
-- 	struct wl_listener *l, *next;

-- 	wl_list_for_each_safe(l, next, &signal->listener_list, link)
-- 		l->notify(l, data);
-- }

-- typedef void (*wl_resource_destroy_func_t)(struct wl_resource *resource);

-- NOTE not binding to deprecated stuff, cause wayland is bad enough as it is.

-- /*
--  * Post an event to the client's object referred to by 'resource'.
--  * 'opcode' is the event number generated from the protocol XML
--  * description (the event name). The variable arguments are the event
--  * parameters, in the order they appear in the protocol XML specification.
--  *
--  * The variable arguments' types are:
--  * - type=uint: 	uint32_t
--  * - type=int:		int32_t
--  * - type=fixed:	wl_fixed_t
--  * - type=string:	(const char *) to a nil-terminated string
--  * - type=array:	(struct wl_array *)
--  * - type=fd:		int, that is an open file descriptor
--  * - type=new_id:	(struct wl_object *) or (struct wl_resource *)
--  * - type=object:	(struct wl_object *) or (struct wl_resource *)
--  */
-- void wl_resource_post_event(struct wl_resource *resource,
-- 			    uint32_t opcode, ...);
-- void wl_resource_post_event_array(struct wl_resource *resource,
-- 				  uint32_t opcode, union wl_argument *args);
-- void wl_resource_queue_event(struct wl_resource *resource,
-- 			     uint32_t opcode, ...);
-- void wl_resource_queue_event_array(struct wl_resource *resource,
-- 				   uint32_t opcode, union wl_argument *args);

-- /* msg is a printf format string, variable args are its args. */
-- void wl_resource_post_error(struct wl_resource *resource,
-- 			    uint32_t code, const char *msg, ...)
-- 	__attribute__ ((format (printf, 3, 4)));
-- void wl_resource_post_no_memory(struct wl_resource *resource);

-- #include "wayland-server-protocol.h"

-- struct wl_display *
-- wl_client_get_display(struct wl_client *client);

-- struct wl_resource *
-- wl_resource_create(struct wl_client *client,
-- 		   const struct wl_interface *interface,
-- 		   int version, uint32_t id);
-- void
-- wl_resource_set_implementation(struct wl_resource *resource,
-- 			       const void *implementation,
-- 			       void *data,
-- 			       wl_resource_destroy_func_t destroy);
-- void
-- wl_resource_set_dispatcher(struct wl_resource *resource,
-- 			   wl_dispatcher_func_t dispatcher,
-- 			   const void *implementation,
-- 			   void *data,
-- 			   wl_resource_destroy_func_t destroy);

-- void
-- wl_resource_destroy(struct wl_resource *resource);
-- uint32_t
-- wl_resource_get_id(struct wl_resource *resource);
-- struct wl_list *
-- wl_resource_get_link(struct wl_resource *resource);
-- struct wl_resource *
-- wl_resource_from_link(struct wl_list *resource);
-- struct wl_resource *
-- wl_resource_find_for_client(struct wl_list *list, struct wl_client *client);
-- struct wl_client *
-- wl_resource_get_client(struct wl_resource *resource);
-- void
-- wl_resource_set_user_data(struct wl_resource *resource, void *data);
-- void *
-- wl_resource_get_user_data(struct wl_resource *resource);
-- int
-- wl_resource_get_version(struct wl_resource *resource);
-- void
-- wl_resource_set_destructor(struct wl_resource *resource,
-- 			   wl_resource_destroy_func_t destroy);
-- int
-- wl_resource_instance_of(struct wl_resource *resource,
-- 			const struct wl_interface *interface,
-- 			const void *implementation);

-- void
-- wl_resource_add_destroy_listener(struct wl_resource *resource,
-- 				 struct wl_listener * listener);
-- struct wl_listener *
-- wl_resource_get_destroy_listener(struct wl_resource *resource,
-- 				 wl_notify_func_t notify);

-- #define wl_resource_for_each(resource, list)					\
-- 	for (resource = 0, resource = wl_resource_from_link((list)->next);	\
-- 	     wl_resource_get_link(resource) != (list);				\
-- 	     resource = wl_resource_from_link(wl_resource_get_link(resource)->next))

-- #define wl_resource_for_each_safe(resource, tmp, list)					\
-- 	for (resource = 0, tmp = 0,							\
-- 	     resource = wl_resource_from_link((list)->next),	\
-- 	     tmp = wl_resource_from_link((list)->next->next);	\
-- 	     wl_resource_get_link(resource) != (list);				\
-- 	     resource = tmp,							\
-- 	     tmp = wl_resource_from_link(wl_resource_get_link(resource)->next))


-- this is a dirty hack to make shmBufferGet accept a Buffer

{-# LINE 517 "./Graphics/Wayland/Internal/Server.chs" #-}

-- struct wl_shm_buffer;
newtype ShmBuffer = ShmBuffer (Ptr (ShmBuffer))
{-# LINE 519 "./Graphics/Wayland/Internal/Server.chs" #-}

receiveMaybeShmBuffer :: ShmBuffer -> Maybe ShmBuffer
receiveMaybeShmBuffer (ShmBuffer x)
  | x == nullPtr = Nothing
  | otherwise    = Just (ShmBuffer x)

-- | void
-- wl_shm_buffer_begin_access(struct wl_shm_buffer *buffer);
--
-- Lock the memory for reading. Needed to protect the server against SIGBUS signals
-- caused by the client resizing the buffer.
shmBufferBeginAccess :: (ShmBuffer) -> IO ()
shmBufferBeginAccess a1 =
  let {a1' = id a1} in 
  shmBufferBeginAccess'_ a1' >>
  return ()

{-# LINE 530 "./Graphics/Wayland/Internal/Server.chs" #-}


-- |void
-- wl_shm_buffer_end_access(struct wl_shm_buffer *buffer);
--
-- Unlock the memory.
shmBufferEndAccess :: (ShmBuffer) -> IO ()
shmBufferEndAccess a1 =
  let {a1' = id a1} in 
  shmBufferEndAccess'_ a1' >>
  return ()

{-# LINE 536 "./Graphics/Wayland/Internal/Server.chs" #-}


-- | struct wl_shm_buffer *
-- wl_shm_buffer_get(struct wl_resource *resource);
shmBufferGet :: (Buffer) -> IO ((Maybe ShmBuffer))
shmBufferGet a1 =
  let {a1' = id a1} in 
  shmBufferGet'_ a1' >>= \res ->
  let {res' = receiveMaybeShmBuffer res} in
  return (res')

{-# LINE 540 "./Graphics/Wayland/Internal/Server.chs" #-}


-- | void *
-- wl_shm_buffer_get_data(struct wl_shm_buffer *buffer);
shmBufferGetData :: (ShmBuffer) -> IO ((Ptr ()))
shmBufferGetData a1 =
  let {a1' = id a1} in 
  shmBufferGetData'_ a1' >>= \res ->
  let {res' = id res} in
  return (res')

{-# LINE 544 "./Graphics/Wayland/Internal/Server.chs" #-}


-- | int32_t
-- wl_shm_buffer_get_stride(struct wl_shm_buffer *buffer);
shmBufferGetStride :: (ShmBuffer) -> IO ((Int))
shmBufferGetStride a1 =
  let {a1' = id a1} in 
  shmBufferGetStride'_ a1' >>= \res ->
  let {res' = fromIntegral res} in
  return (res')

{-# LINE 548 "./Graphics/Wayland/Internal/Server.chs" #-}


-- | uint32_t
-- wl_shm_buffer_get_format(struct wl_shm_buffer *buffer);
shmBufferGetFormat :: (ShmBuffer) -> IO ((Word))
shmBufferGetFormat a1 =
  let {a1' = id a1} in 
  shmBufferGetFormat'_ a1' >>= \res ->
  let {res' = fromIntegral res} in
  return (res')

{-# LINE 552 "./Graphics/Wayland/Internal/Server.chs" #-}


-- | int32_t
-- wl_shm_buffer_get_width(struct wl_shm_buffer *buffer);
shmBufferGetWidth :: (ShmBuffer) -> IO ((Int))
shmBufferGetWidth a1 =
  let {a1' = id a1} in 
  shmBufferGetWidth'_ a1' >>= \res ->
  let {res' = fromIntegral res} in
  return (res')

{-# LINE 556 "./Graphics/Wayland/Internal/Server.chs" #-}


-- | int32_t
-- wl_shm_buffer_get_height(struct wl_shm_buffer *buffer);
shmBufferGetHeight :: (ShmBuffer) -> IO ((Int))
shmBufferGetHeight a1 =
  let {a1' = id a1} in 
  shmBufferGetHeight'_ a1' >>= \res ->
  let {res' = fromIntegral res} in
  return (res')

{-# LINE 560 "./Graphics/Wayland/Internal/Server.chs" #-}


-- | int
-- wl_display_init_shm(struct wl_display *display);
displayInitShm :: (DisplayServer) -> IO ((Result))
displayInitShm a1 =
  let {a1' = id a1} in 
  displayInitShm'_ a1' >>= \res ->
  let {res' = errToResult res} in
  return (res')

{-# LINE 564 "./Graphics/Wayland/Internal/Server.chs" #-}


-- | uint32_t *
-- wl_display_add_shm_format(struct wl_display *display, uint32_t format);
displayAddShmFormat :: (DisplayServer) -> (Word) -> IO ()
displayAddShmFormat a1 a2 =
  let {a1' = id a1} in 
  let {a2' = fromIntegral a2} in 
  displayAddShmFormat'_ a1' a2' >>
  return ()

{-# LINE 568 "./Graphics/Wayland/Internal/Server.chs" #-}


-- | struct wl_shm_buffer *
-- wl_shm_buffer_create(struct wl_client *client,
-- 		     uint32_t id, int32_t width, int32_t height,
-- 		     int32_t stride, uint32_t format);
shmBufferCreate :: (Client) -> (Word) -> (Word) -> (Int) -> (Int) -> (Word) -> IO ((Maybe ShmBuffer))
shmBufferCreate a1 a2 a3 a4 a5 a6 =
  let {a1' = id a1} in 
  let {a2' = fromIntegral a2} in 
  let {a3' = fromIntegral a3} in 
  let {a4' = fromIntegral a4} in 
  let {a5' = fromIntegral a5} in 
  let {a6' = fromIntegral a6} in 
  shmBufferCreate'_ a1' a2' a3' a4' a5' a6' >>= \res ->
  let {res' = receiveMaybeShmBuffer res} in
  return (res')

{-# LINE 574 "./Graphics/Wayland/Internal/Server.chs" #-}


-- void wl_log_set_handler_server(wl_log_func_t handler);

foreign import ccall unsafe "Graphics/Wayland/Internal/Server.chs.h wl_event_loop_create"
  eventLoopCreate'_ :: (IO (EventLoop))

foreign import ccall unsafe "Graphics/Wayland/Internal/Server.chs.h wl_event_loop_destroy"
  eventLoopDestroy'_ :: ((EventLoop) -> (IO ()))

foreign import ccall unsafe "Graphics/Wayland/Internal/Server.chs.h wl_event_loop_add_fd"
  eventLoopAddFd'_ :: ((EventLoop) -> (CInt -> (CUInt -> ((FunPtr (CInt -> (CUInt -> ((Ptr ()) -> (IO CInt))))) -> ((Ptr ()) -> (IO (EventSource)))))))

foreign import ccall unsafe "Graphics/Wayland/Internal/Server.chs.h wl_event_source_fd_update"
  eventSourceFdUpdate'_ :: ((EventSource) -> (CUInt -> (IO CInt)))

foreign import ccall unsafe "Graphics/Wayland/Internal/Server.chs.h wl_event_loop_add_timer"
  eventLoopAddTimer'_ :: ((EventLoop) -> ((FunPtr ((Ptr ()) -> (IO CInt))) -> ((Ptr ()) -> (IO (EventSource)))))

foreign import ccall unsafe "Graphics/Wayland/Internal/Server.chs.h wl_event_loop_add_signal"
  eventLoopAddSignal'_ :: ((EventLoop) -> (CInt -> ((FunPtr (CInt -> ((Ptr ()) -> (IO CInt)))) -> ((Ptr ()) -> (IO (EventSource))))))

foreign import ccall unsafe "Graphics/Wayland/Internal/Server.chs.h wl_event_source_timer_update"
  eventSourceTimerUpdate'_ :: ((EventSource) -> (CInt -> (IO CInt)))

foreign import ccall unsafe "Graphics/Wayland/Internal/Server.chs.h wl_event_source_remove"
  eventSourceRemove'_ :: ((EventSource) -> (IO CInt))

foreign import ccall unsafe "Graphics/Wayland/Internal/Server.chs.h wl_event_source_check"
  eventSourceCheck'_ :: ((EventSource) -> (IO ()))

foreign import ccall safe "Graphics/Wayland/Internal/Server.chs.h wl_event_loop_dispatch"
  eventLoopDispatch'_ :: ((EventLoop) -> (CInt -> (IO CInt)))

foreign import ccall safe "Graphics/Wayland/Internal/Server.chs.h wl_event_loop_dispatch_idle"
  eventLoopDispatchIdle'_ :: ((EventLoop) -> (IO ()))

foreign import ccall safe "Graphics/Wayland/Internal/Server.chs.h wl_event_loop_add_idle"
  eventLoopAddIdle'_ :: ((EventLoop) -> ((FunPtr ((Ptr ()) -> (IO ()))) -> ((Ptr ()) -> (IO (EventSource)))))

foreign import ccall unsafe "Graphics/Wayland/Internal/Server.chs.h wl_event_loop_get_fd"
  eventLoopGetFd'_ :: ((EventLoop) -> (IO CInt))

foreign import ccall unsafe "Graphics/Wayland/Internal/Server.chs.h wl_display_create"
  displayCreate'_ :: (IO (DisplayServer))

foreign import ccall unsafe "Graphics/Wayland/Internal/Server.chs.h wl_display_destroy"
  displayDestroy'_ :: ((DisplayServer) -> (IO ()))

foreign import ccall unsafe "Graphics/Wayland/Internal/Server.chs.h wl_display_get_event_loop"
  displayGetEventLoop'_ :: ((DisplayServer) -> (IO (EventLoop)))

foreign import ccall unsafe "Graphics/Wayland/Internal/Server.chs.h wl_display_add_socket"
  displayAddSocket'_ :: ((DisplayServer) -> ((Ptr CChar) -> (IO CInt)))

foreign import ccall unsafe "Graphics/Wayland/Internal/Server.chs.h wl_display_terminate"
  displayTerminate'_ :: ((DisplayServer) -> (IO ()))

foreign import ccall safe "Graphics/Wayland/Internal/Server.chs.h wl_display_run"
  displayRun'_ :: ((DisplayServer) -> (IO ()))

foreign import ccall safe "Graphics/Wayland/Internal/Server.chs.h wl_display_flush_clients"
  displayFlushClients'_ :: ((DisplayServer) -> (IO ()))

foreign import ccall unsafe "Graphics/Wayland/Internal/Server.chs.h wl_display_get_serial"
  displayGetSerial'_ :: ((DisplayServer) -> (IO CUInt))

foreign import ccall unsafe "Graphics/Wayland/Internal/Server.chs.h wl_display_next_serial"
  displayNextSerial'_ :: ((DisplayServer) -> (IO CUInt))

foreign import ccall unsafe "Graphics/Wayland/Internal/Server.chs.h wl_client_create"
  clientCreate'_ :: ((DisplayServer) -> (CInt -> (IO (Client))))

foreign import ccall unsafe "Graphics/Wayland/Internal/Server.chs.h wl_client_destroy"
  clientDestroy'_ :: ((Client) -> (IO ()))

foreign import ccall unsafe "Graphics/Wayland/Internal/Server.chs.h wl_client_flush"
  clientFlush'_ :: ((Client) -> (IO ()))

foreign import ccall unsafe "Graphics/Wayland/Internal/Server.chs.h wl_client_get_credentials"
  clientGetCredentials'_ :: ((Client) -> ((Ptr CInt) -> ((Ptr CUInt) -> ((Ptr CUInt) -> (IO ())))))

foreign import ccall unsafe "Graphics/Wayland/Internal/Server.chs.h wl_client_post_no_memory"
  clientPostNoMemory'_ :: ((Client) -> (IO ()))

foreign import ccall unsafe "Graphics/Wayland/Internal/Server.chs.h wl_shm_buffer_begin_access"
  shmBufferBeginAccess'_ :: ((ShmBuffer) -> (IO ()))

foreign import ccall unsafe "Graphics/Wayland/Internal/Server.chs.h wl_shm_buffer_end_access"
  shmBufferEndAccess'_ :: ((ShmBuffer) -> (IO ()))

foreign import ccall unsafe "Graphics/Wayland/Internal/Server.chs.h wl_shm_buffer_get"
  shmBufferGet'_ :: ((Buffer) -> (IO (ShmBuffer)))

foreign import ccall unsafe "Graphics/Wayland/Internal/Server.chs.h wl_shm_buffer_get_data"
  shmBufferGetData'_ :: ((ShmBuffer) -> (IO (Ptr ())))

foreign import ccall unsafe "Graphics/Wayland/Internal/Server.chs.h wl_shm_buffer_get_stride"
  shmBufferGetStride'_ :: ((ShmBuffer) -> (IO CInt))

foreign import ccall unsafe "Graphics/Wayland/Internal/Server.chs.h wl_shm_buffer_get_format"
  shmBufferGetFormat'_ :: ((ShmBuffer) -> (IO CUInt))

foreign import ccall unsafe "Graphics/Wayland/Internal/Server.chs.h wl_shm_buffer_get_width"
  shmBufferGetWidth'_ :: ((ShmBuffer) -> (IO CInt))

foreign import ccall unsafe "Graphics/Wayland/Internal/Server.chs.h wl_shm_buffer_get_height"
  shmBufferGetHeight'_ :: ((ShmBuffer) -> (IO CInt))

foreign import ccall unsafe "Graphics/Wayland/Internal/Server.chs.h wl_display_init_shm"
  displayInitShm'_ :: ((DisplayServer) -> (IO CInt))

foreign import ccall unsafe "Graphics/Wayland/Internal/Server.chs.h wl_display_add_shm_format"
  displayAddShmFormat'_ :: ((DisplayServer) -> (CUInt -> (IO (Ptr CUInt))))

foreign import ccall unsafe "Graphics/Wayland/Internal/Server.chs.h wl_shm_buffer_create"
  shmBufferCreate'_ :: ((Client) -> (CUInt -> (CInt -> (CInt -> (CInt -> (CUInt -> (IO (ShmBuffer))))))))