summaryrefslogtreecommitdiff
path: root/wayland/Wayland/Server/System.hsc
blob: 960caf4613abe1708969ec275d0e51248c39df64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
{-# LANGUAGE ForeignFunctionInterface #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}

-- |
-- Module     : Wayland.Server.System
-- Description: Bindings for the system-provided libwayland library.
--
-- libwayland is used to handle socket creation, implementing the protocol, and the event loop. We do not use the
-- interfaces or bindings provided by it but only its primitives like wl_resource in combination with our own code
-- generator which generates the interfaces and (de-)marshals all arguments and opcodes. This is done to keep the amount
-- of C interop small.

module Wayland.Server.System where

#include <wayland-server.h>

import Foreign (FunPtr, Ptr, Int32, Storable(..), castPtr)
import Foreign.C (CString, CSize, CInt(..))
import Data.Word (Word32)

-- * Constants

wlEventReadable :: Word32
wlEventReadable = #{const WL_EVENT_READABLE}
wlEventWritable :: Word32
wlEventWritable = #{const WL_EVENT_WRITABLE}
wlEventHangup   :: Word32
wlEventHangup   = #{const WL_EVENT_HANGUP}
wlEventError    :: Word32
wlEventError    = #{const WL_EVENT_ERROR}

-- * Callback Types

type WLEventLoopSignalFunc = CInt -> Ptr () -> IO CInt
type WLEventLoopTimerFunc  = Ptr () -> IO CInt
type WLEventLoopFdFunc     = CInt -> Word32 -> Ptr () -> IO CInt
type WLGlobalBindFunc      = WLClient -> Ptr () -> Word32 -> Word32 -> IO ()
type WLDispatcherFunc      = Ptr () -> WLResource -> Word32 -> Ptr WLMessage -> Ptr WLArgument -> IO CInt
type WLResourceDestroyFunc = WLResource -> IO ()
type WLNotifyFunc          = Ptr WLListener -> Ptr () -> IO ()
type WLUserDataDestroyFunc = Ptr () -> IO ()

foreign import ccall "wrapper"
  wrapWLEventLoopSignalFunc :: WLEventLoopSignalFunc -> IO (FunPtr WLEventLoopSignalFunc)

foreign import ccall "wrapper"
  wrapWLEventLoopTimerFunc :: WLEventLoopTimerFunc -> IO (FunPtr WLEventLoopTimerFunc)

foreign import ccall "wrapper"
  wrapWLEventLoopFdFunc :: WLEventLoopFdFunc -> IO (FunPtr WLEventLoopFdFunc)

foreign import ccall "wrapper"
  wrapWLGlobalBindFunc :: WLGlobalBindFunc -> IO (FunPtr WLGlobalBindFunc)

foreign import ccall "wrapper"
  wrapWLDispatcherFunc :: WLDispatcherFunc -> IO (FunPtr WLDispatcherFunc)

foreign import ccall "wrapper"
  wrapWLResourceDestroyFunc :: WLResourceDestroyFunc -> IO (FunPtr WLResourceDestroyFunc)

foreign import ccall "wrapper"
  wrapWLNotifyFunc :: WLNotifyFunc -> IO (FunPtr WLNotifyFunc)

foreign import ccall "wrapper"
  wrapWLUserDataDestroyFunc :: WLUserDataDestroyFunc -> IO (FunPtr WLUserDataDestroyFunc)

-- * Primitive Types

-- ** wl_message

data WLMessage
  = WLMessage { msgName      :: CString
              , msgSignature :: CString
              , msgTypes     :: Ptr (Ptr WLInterface)
              }
  deriving (Show)

instance Storable WLMessage where
  sizeOf    _ = #{size struct wl_message}
  alignment _ = #{alignment struct wl_message}
  peek p = do
    name      <- #{peek struct wl_message, name}      p
    signature <- #{peek struct wl_message, signature} p
    types     <- #{peek struct wl_message, types}     p
    return $ WLMessage { msgName      = name
                       , msgSignature = signature
                       , msgTypes     = types
                       }
  poke p m = do
    #{poke struct wl_message, name}      p $ msgName      m
    #{poke struct wl_message, signature} p $ msgSignature m
    #{poke struct wl_message, types}     p $ msgTypes     m
    return ()

-- ** wl_interface

data WLInterface
  = WLInterface { ifName        :: CString
                , ifVersion     :: CInt
                , ifMethodCount :: CInt
                , ifMethods     :: Ptr WLMessage
                , ifEventCount  :: CInt
                , ifEvents      :: Ptr WLMessage
                }
  deriving (Show)

instance Storable WLInterface where
  sizeOf    _ = #{size struct wl_interface}
  alignment _ = #{alignment struct wl_interface}
  peek p = do
    name        <- #{peek struct wl_interface, name}         p
    version     <- #{peek struct wl_interface, version}      p
    methodCount <- #{peek struct wl_interface, method_count} p
    methods     <- #{peek struct wl_interface, methods}      p
    eventCount  <- #{peek struct wl_interface, event_count}  p
    events      <- #{peek struct wl_interface, events}       p
    return $ WLInterface { ifName        = name
                         , ifVersion     = version
                         , ifMethodCount = methodCount
                         , ifMethods     = methods
                         , ifEventCount  = eventCount
                         , ifEvents      = events
                         }
  poke p m = do
    #{poke struct wl_interface, name}         p $ ifName        m
    #{poke struct wl_interface, version}      p $ ifVersion     m
    #{poke struct wl_interface, method_count} p $ ifMethodCount m
    #{poke struct wl_interface, methods}      p $ ifMethods     m
    #{poke struct wl_interface, event_count}  p $ ifEventCount  m
    #{poke struct wl_interface, events}       p $ ifEvents      m
    return ()

-- ** wl_fixed

newtype WLFixed = WLFixed #{type wl_fixed_t}
  deriving (Show)

instance Storable WLFixed where
  sizeOf    _             = sizeOf    (undefined :: #{type wl_fixed_t})
  alignment _             = alignment (undefined :: #{type wl_fixed_t})
  peek      p             = WLFixed <$> peek (castPtr p)
  poke      p (WLFixed v) = poke (castPtr p) v

wlFixedToFloat :: WLFixed -> Float
wlFixedToFloat (WLFixed v) = (fromIntegral v) / 256

-- ** wl_array

data WLArray
  = WLArray { arrSize  :: CSize
            , arrAlloc :: CSize
            , arrData  :: Ptr ()
            }
  deriving (Show)

instance Storable WLArray where
  sizeOf    _ = #{size struct wl_array}
  alignment _ = #{alignment struct wl_array}
  peek p = do
    aSize  <- #{peek struct wl_array, size}  p
    aAlloc <- #{peek struct wl_array, alloc} p
    aData  <- #{peek struct wl_array, data}  p
    return $ WLArray { arrSize  = aSize
                     , arrAlloc = aAlloc
                     , arrData  = aData
                     }
  poke p m = do
    #{poke struct wl_array, size}  p $ arrSize  m
    #{poke struct wl_array, alloc} p $ arrAlloc m
    #{poke struct wl_array, data}  p $ arrData  m
    return ()

-- ** wl_argument

-- Accessed through the wlArgumentPeek* and wlArgumentPoke* methods as the actual data type is unknown just from looking
-- at the union.
data WLArgument = WLArgument
  deriving (Show)

instance Storable WLArgument where
  sizeOf    _   = #{size union wl_argument}
  alignment _   = #{alignment union wl_argument}
  peek      _   = pure WLArgument
  poke      _ _ = pure ()

wlArgumentPeekInt    :: Ptr WLArgument -> IO Int32
wlArgumentPeekInt    = #{peek union wl_argument, i}
wlArgumentPeekUInt   :: Ptr WLArgument -> IO Word32
wlArgumentPeekUInt   = #{peek union wl_argument, u}
wlArgumentPeekFixed  :: Ptr WLArgument -> IO WLFixed
wlArgumentPeekFixed  = #{peek union wl_argument, f}
wlArgumentPeekString :: Ptr WLArgument -> IO CString
wlArgumentPeekString = #{peek union wl_argument, s}
wlArgumentPeekObject :: Ptr WLArgument -> IO WLResource
wlArgumentPeekObject = #{peek union wl_argument, o}
wlArgumentPeekNewID  :: Ptr WLArgument -> IO Word32
wlArgumentPeekNewID  = #{peek union wl_argument, n}
wlArgumentPeekArray  :: Ptr WLArgument -> IO (Ptr WLArray)
wlArgumentPeekArray  = #{peek union wl_argument, a}
wlArgumentPeekFD     :: Ptr WLArgument -> IO Int32
wlArgumentPeekFD     = #{peek union wl_argument, h}

wlArgumentPokeInt    :: Ptr WLArgument -> Int32 -> IO ()
wlArgumentPokeInt    = #{poke union wl_argument, i}
wlArgumentPokeUInt   :: Ptr WLArgument -> Word32 -> IO ()
wlArgumentPokeUInt   = #{poke union wl_argument, u}
wlArgumentPokeFixed  :: Ptr WLArgument -> WLFixed -> IO ()
wlArgumentPokeFixed  = #{poke union wl_argument, f}
wlArgumentPokeString :: Ptr WLArgument -> CString -> IO ()
wlArgumentPokeString = #{poke union wl_argument, s}
wlArgumentPokeObject :: Ptr WLArgument -> WLResource -> IO ()
wlArgumentPokeObject = #{poke union wl_argument, o}
wlArgumentPokeNewID  :: Ptr WLArgument -> Word32 -> IO ()
wlArgumentPokeNewID  = #{poke union wl_argument, n}
wlArgumentPokeArray  :: Ptr WLArgument -> Ptr WLArray -> IO ()
wlArgumentPokeArray  = #{poke union wl_argument, a}
wlArgumentPokeFD     :: Ptr WLArgument -> Int32 -> IO ()
wlArgumentPokeFD     = #{poke union wl_argument, h}

-- ** wl_listener

newtype WLListener = WLListener (FunPtr WLNotifyFunc)
  deriving (Show)

instance Storable WLListener where
  sizeOf    _ = #{size struct wl_listener}
  alignment _ = #{alignment struct wl_listener}
  peek p = do
    notify <- #{peek struct wl_listener, notify} p
    return $ WLListener notify
  poke p (WLListener notify) = do
    #{poke struct wl_listener, notify} p notify
    return ()

foreign import ccall "wl_list_remove"
  wlListenerRemove :: Ptr WLListener -> IO ()

-- * Opaque Types

-- ** wl_display

newtype WLDisplay = WLDisplay (Ptr WLDisplay)
  deriving (Show, Eq, Storable)

foreign import ccall "wl_display_create"
  wlDisplayCreate :: IO WLDisplay

foreign import ccall "wl_display_destroy"
  wlDisplayDestroy :: WLDisplay -> IO ()

foreign import ccall "wl_display_flush_clients"
  wlDisplayFlushClients :: WLDisplay -> IO ()

foreign import ccall "wl_display_add_socket_auto"
  wlDisplayAddSocketAuto :: WLDisplay -> IO CString

foreign import ccall "wl_display_run"
  wlDisplayRun :: WLDisplay -> IO ()

foreign import ccall "wl_display_terminate"
  wlDisplayTerminate :: WLDisplay -> IO ()

foreign import ccall "wl_display_get_event_loop"
  wlDisplayGetEventLoop :: WLDisplay -> IO WLEventLoop

foreign import ccall "wl_display_add_client_created_listener"
  wlDisplayAddClientCreatedListener :: WLDisplay -> Ptr WLListener -> IO ()

-- ** wl_event_loop

newtype WLEventLoop = WLEventLoop (Ptr WLEventLoop)
  deriving (Show, Eq, Storable)

foreign import ccall "wl_event_loop_add_signal"
  wlEventLoopAddSignal :: WLEventLoop -> CInt -> FunPtr WLEventLoopSignalFunc -> Ptr () -> IO WLEventSource

foreign import ccall "wl_event_loop_add_timer"
  wlEventLoopAddTimer :: WLEventLoop -> FunPtr WLEventLoopTimerFunc -> Ptr () -> IO WLEventSource

foreign import ccall "wl_event_loop_add_fd"
  wlEventLoopAddFd :: WLEventLoop -> CInt -> Word32 -> FunPtr WLEventLoopFdFunc -> Ptr () -> IO WLEventSource

-- ** wl_event_source

newtype WLEventSource = WLEventSource (Ptr WLEventSource)
  deriving (Show, Eq, Storable)

foreign import ccall "wl_event_source_remove"
  wlEventSourceRemove :: WLEventSource -> IO CInt

foreign import ccall "wl_event_source_fd_update"
  wlEventSourceFdUpdate :: WLEventSource -> Word32 -> IO CInt

foreign import ccall "wl_event_source_timer_update"
  wlEventSourceTimerUpdate :: WLEventSource -> CInt -> IO CInt

-- ** wl_global

newtype WLGlobal = WLGlobal (Ptr WLGlobal)
  deriving (Show, Eq, Storable)

foreign import ccall "wl_global_create"
  wlGlobalCreate :: WLDisplay -> Ptr WLInterface -> CInt -> Ptr () -> FunPtr WLGlobalBindFunc -> IO WLGlobal

foreign import ccall "wl_global_remove"
  wlGlobalRemove :: WLGlobal -> IO ()

foreign import ccall "wl_global_destroy"
  wlGlobalDestroy :: WLGlobal -> IO ()

-- ** wl_client

newtype WLClient = WLClient (Ptr WLClient)
  deriving (Show, Eq, Storable)

foreign import ccall "wl_client_destroy"
  wlClientDestroy :: WLClient -> IO ()

foreign import ccall "wl_client_flush"
  wlClientFlush :: WLClient -> IO ()

foreign import ccall "wl_client_set_user_data"
  wlClientSetUserData :: WLClient -> Ptr () -> FunPtr WLUserDataDestroyFunc -> IO ()

foreign import ccall "wl_client_get_user_data"
  wlClientGetUserData :: WLClient -> IO (Ptr ())

-- ** wl_resource

newtype WLResource = WLResource (Ptr WLResource)
  deriving (Show, Eq, Storable)

foreign import ccall "wl_resource_create"
  wlResourceCreate :: WLClient -> Ptr WLInterface -> CInt -> Word32 -> IO WLResource

foreign import ccall "wl_resource_set_dispatcher"
  wlResourceSetDispatcher :: WLResource -> FunPtr WLDispatcherFunc -> Ptr () -> Ptr () -> FunPtr WLResourceDestroyFunc -> IO ()

foreign import ccall "wl_resource_get_user_data"
  wlResourceGetUserData :: WLResource -> IO (Ptr ())

foreign import ccall "wl_resource_post_event_array"
  wlResourcePostEventArray :: WLResource -> Word32 -> Ptr WLArgument -> IO ()

foreign import ccall "wl_resource_destroy"
  wlResourceDestroy :: WLResource -> IO ()

foreign import ccall "wl_resource_get_client"
  wlResourceGetClient :: WLResource -> IO WLClient