summaryrefslogtreecommitdiffhomepage
path: root/src/Xmobar/Text/Loop.hs
blob: 62dd5c87a2a12511ac7632cb5dfb5c9680182933 (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
------------------------------------------------------------------------------
-- |
-- Module: Xmobar.Text.Loop
-- Copyright: (c) 2022, 2025 Jose Antonio Ortega Ruiz
-- License: BSD3-style (see LICENSE)
--
-- Maintainer: jao@gnu.org
-- Stability: unstable
-- Portability: unportable
-- Created: Fri Jan 28, 2022 01:21
--
--
-- Text-only event loop
--
------------------------------------------------------------------------------

module Xmobar.Text.Loop (textLoop) where

import Prelude hiding (lookup)
import System.IO (hSetBuffering, stdin, stdout, BufferMode(LineBuffering))

import Control.Concurrent.STM

import Xmobar.System.Signal
import Xmobar.Config.Types (Config)
import Xmobar.Run.Loop (loop)
import Xmobar.Run.Runnable (Runnable)
import Xmobar.Text.Output (initLoop, format)

-- | Starts the main event loop and threads
textLoop :: Config -> IO ()
textLoop conf = do
  hSetBuffering stdin LineBuffering
  hSetBuffering stdout LineBuffering
  initLoop conf
  loop conf (eventLoop conf)

-- | Continuously wait for a signal from a thread or a interrupt handler
eventLoop :: Config -> [Runnable] -> TMVar SignalType -> TVar [String] -> IO ()
eventLoop cfg rs signal tv = do
  typ <- atomically $ takeTMVar signal
  case typ of
    Wakeup -> updateString cfg tv >>= putStrLn >> eventLoop cfg rs signal tv
    _ -> eventLoop cfg rs signal tv

updateString :: Config -> TVar [String] -> IO String
updateString conf v = do
  s <- readTVarIO v
  return $ format conf (concat s)