Skip to content
Snippets Groups Projects
Commit 047ab399 authored by Jens Nolte's avatar Jens Nolte
Browse files

Add runBarHost to prepare for the implmementation of other hosting types

parent f4bbea7a
No related branches found
No related tags found
No related merge requests found
module QBar.Host where
import QBar.Core
import Control.Concurrent.Event as Event
import Control.Concurrent.STM.TChan (TChan, newTChanIO)
runBarHost :: (TChan CachedBlock -> BarUpdateEvent -> BarIO ()) -> IO ()
runBarHost host = do
-- Create an event used to signal bar updates
barUpdateEvent <- Event.newSet
let requestBarUpdate = Event.set barUpdateEvent
-- Create channel to send new block producers to render loop
newBlockChan <- newTChanIO
let bar = Bar { requestBarUpdate, newBlockChan }
runBarIO bar (host newBlockChan barUpdateEvent)
module QBar.Server where module QBar.Server where
import QBar.Blocks import QBar.Blocks
import QBar.BlockText
import QBar.Core import QBar.Core
import QBar.Cli import QBar.Cli
import QBar.ControlSocket import QBar.ControlSocket
import QBar.Filter import QBar.Filter
import QBar.BlockText import QBar.Host
import QBar.Themes import QBar.Themes
import Control.Monad (forever, when, unless) import Control.Monad (forever, when, unless)
...@@ -189,62 +190,59 @@ renderInitialBlocks options handle blockFilter = do ...@@ -189,62 +190,59 @@ renderInitialBlocks options handle blockFilter = do
runBarServer :: BarIO () -> MainOptions -> IO () runBarServer :: BarIO () -> MainOptions -> IO ()
runBarServer defaultBarConfig options = do runBarServer defaultBarConfig options = do
putStrLn "{\"version\":1,\"click_events\":true}" putStrLn "{\"version\":1,\"click_events\":true}"
putStrLn "[" putStrLn "["
(requestBarUpdate, barUpdateEvent) <- createBarUpdateChannel runBarHost (\newBlockChan barUpdateEvent -> do
-- Create channel to send new block producers to render loop
newBlockChan <- newTChanIO
let bar = Bar { requestBarUpdate, newBlockChan } -- Create IORef to contain the active filter
let initialBlockFilter = StaticFilter None
activeFilter <- liftIO $ newIORef initialBlockFilter
-- Create IORef to contain the active filter -- Create IORef for event handlers
let initialBlockFilter = StaticFilter None eventHandlerListIORef <- liftIO $ newIORef []
activeFilter <- newIORef initialBlockFilter
-- Create IORef for event handlers let handle = Handle {
eventHandlerListIORef <- newIORef [] handleActionList = eventHandlerListIORef,
handleActiveFilter = activeFilter
}
let handle = Handle { initialOutput <- liftIO $ renderInitialBlocks options handle initialBlockFilter
handleActionList = eventHandlerListIORef,
handleActiveFilter = activeFilter
}
initialOutput <- renderInitialBlocks options handle initialBlockFilter bar <- askBar
-- Fork stdin handler
liftIO $ void $ forkFinally (runBarIO bar (handleStdin options eventHandlerListIORef)) (\result -> hPutStrLn stderr $ "handleStdin failed: " <> show result)
-- Fork stdin handler loadBlocks
void $ forkFinally (runBarIO bar (handleStdin options eventHandlerListIORef)) (\result -> hPutStrLn stderr $ "handleStdin failed: " <> show result)
-- Install signal handler for SIGCONT
installSignalHandlers
runBarIO bar loadBlocks -- Create control socket
commandChan <- liftIO createCommandChan
controlSocketAsync <- liftIO $ listenUnixSocketAsync options commandChan
liftIO $ link controlSocketAsync
-- Install signal handler for SIGCONT -- Update bar on control socket messages
runBarIO bar installSignalHandlers socketUpdateAsync <- liftIO $ async $ forever $ do
command <- atomically $ readTChan commandChan
-- Create control socket case command of
commandChan <- createCommandChan SetFilter blockFilter -> atomicWriteIORef activeFilter blockFilter
controlSocketAsync <- listenUnixSocketAsync options commandChan Block -> error "TODO"
link controlSocketAsync updateBar' bar
liftIO $ link socketUpdateAsync
-- Update bar on control socket messages renderLoop options handle barUpdateEvent initialOutput newBlockChan
socketUpdateAsync <- async $ forever $ do )
command <- atomically $ readTChan commandChan where
case command of loadBlocks :: BarIO ()
SetFilter blockFilter -> atomicWriteIORef activeFilter blockFilter loadBlocks = do
Block -> error "TODO" when (indicator options) $ addBlock renderIndicator
updateBar' bar
link socketUpdateAsync
runBarIO bar (renderLoop options handle barUpdateEvent initialOutput newBlockChan) defaultBarConfig
where
loadBlocks :: BarIO ()
loadBlocks = do
when (indicator options) $ addBlock renderIndicator
defaultBarConfig
createCommandChan :: IO CommandChan createCommandChan :: IO CommandChan
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment