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
|
{-# HLINT ignore "Use camelCase" #-}
module Wetterhorn.Core
( WState (..),
WConfig (..),
SurfaceState (..),
W,
getWConfig,
getWState,
runW,
Wetterhorn,
initWetterhorn,
wio,
incrementState,
readWState,
defaultConfig,
requestHotReload,
ctxConfig,
)
where
import Control.Arrow (first)
import Control.Exception
import Control.Monad (when)
import Data.ByteString (ByteString)
import qualified Data.ByteString.Char8 as CH
import Foreign (Ptr, StablePtr, Word32, newStablePtr, ptrToIntPtr, castForeignPtr)
import Numeric (showHex)
import Text.Printf
import Wetterhorn.Core.ForeignInterface (ForeignInterface)
import qualified Wetterhorn.Core.ForeignInterface as ForeignInterface
data WContext = WContext
{ ctxForeignInterface :: ForeignInterface,
ctxConfig :: WConfig
}
-- This is the OpaqueState passed to the harness.
type Wetterhorn = StablePtr (WContext, WState)
requestHotReload :: W ()
requestHotReload = do
fi <- ctxForeignInterface <$> getWContext
wio $ ForeignInterface.requestHotReload fi
requestLog :: String -> W ()
requestLog str = do
fi <- ctxForeignInterface <$> getWContext
wio $ ForeignInterface.requestLog fi str
initWetterhorn :: WConfig -> IO Wetterhorn
initWetterhorn conf = do
foreignInterface <- ForeignInterface.getForeignInterface
newStablePtr (WContext foreignInterface conf, WState "this is a string" 0)
data WState = WState
{ someString :: String,
integer :: Int
}
deriving (Show, Read)
data SurfaceState = Map | Unmap | Destroy deriving (Eq, Ord, Show, Enum)
data WConfig = WConfig
{ keybindingHandler :: Word32 -> W (),
surfaceHandler :: SurfaceState -> Ptr () -> W ()
}
defaultConfig :: WConfig
defaultConfig =
WConfig
{ keybindingHandler = \sym -> do
i <- incrementState
wio (printf "[%d] Got key: %d\n" i sym)
when (sym == 111) requestHotReload
when (sym == 112) (requestLog "Hey daddy ths is a log statement.\n"),
surfaceHandler = \state ptr -> wio (printf "Surface %s is %s\n" (showHex (ptrToIntPtr ptr) "") (show state))
}
readWState :: ByteString -> IO WState
readWState bs =
catch
(return $ read (CH.unpack bs))
( \e ->
let _ = (e :: SomeException) in return (WState "" 0)
)
newtype W a = W ((WContext, WState) -> IO (a, WState))
instance Functor W where
fmap mfn (W fn) = W $ fmap (first mfn) <$> fn
instance Applicative W where
pure a = W $ \(_, s) -> return (a, s)
mfn <*> ma = do
fn <- mfn
fn <$> ma
instance Monad W where
(W fntoa) >>= fnmb = W $ \(config, state) -> do
(a, state') <- fntoa (config, state)
let W fntob = fnmb a
fntob (config, state')
getWContext :: W WContext
getWContext = W pure
getWConfig :: W WConfig
getWConfig = ctxConfig <$> getWContext
getWState :: W WState
getWState = W $ \(_, s) -> pure (s, s)
runW :: W a -> (WContext, WState) -> IO (a, WState)
runW (W fn) = fn
incrementState :: W Int
incrementState = W $ \(_, WState s i) -> return (i, WState s (i + 1))
wio :: IO a -> W a
wio fn = W $ \(_, b) -> fn >>= \a -> return (a, b)
|