module I3IPC.Message
( MessageType(..)
, createMsg
, createMsgPayload
, sendMsg
, sendMsgPayload
)
where
import Network.Socket.ByteString.Lazy
import Network.Socket ( Socket )
import qualified Data.ByteString.Lazy as BSL
import Data.Binary.Put
import Data.Function ( (&) )
import Data.Int
data MessageType =
RunCommand
| Workspaces
| Subscribe
| Outputs
| Tree
| Marks
| BarConfig
| Version
| BindingModes
| Config
| Tick
| Sync
deriving (Enum, Show, Eq)
createMsgPayload :: MessageType -> BSL.ByteString -> BSL.ByteString
createMsgPayload msgtype msg = runPut $ do
putByteString "i3-ipc"
putWord32host $ fromIntegral (BSL.length msg)
putWord32host $ fromIntegral (fromEnum msgtype)
putLazyByteString msg
createMsg :: MessageType -> BSL.ByteString
createMsg msgtype = runPut $ do
putByteString "i3-ipc"
putWord32host $ fromIntegral @Int 0
putWord32host $ fromIntegral (fromEnum msgtype)
sendMsgPayload :: Socket -> MessageType -> BSL.ByteString -> IO Int64
sendMsgPayload soc msgtype msg = createMsgPayload msgtype msg & send soc
sendMsg :: Socket -> MessageType -> IO Int64
sendMsg soc msgtype = createMsg msgtype & send soc