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
|
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE OverloadedRecordDot #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE StrictData #-}
{-# LANGUAGE TypeFamilies #-}
{-# OPTIONS_GHC -fno-warn-name-shadowing #-}
module Main where
import Control.Concurrent
import Control.Exception
import Control.Monad
import Control.Monad.Trans
import Data.Bits
import Data.List
import Data.Map qualified as M
import Data.Maybe
import Data.Sensor qualified as S
import GHC.Ptr (Ptr)
import Graphics.X11 qualified as X
import Graphics.X11.Xft qualified as X
import Graphics.X11.Xlib.Extras qualified as X
import Options.Applicative qualified as O
import Pretty qualified as P
import Sensor
import Ui hiding (Env)
import Ui qualified
import UnliftIO.STM
data Args = Args
{ icons :: Bool,
spacing :: Int
}
args :: O.ParserInfo Args
args = O.info (Args <$> iconsArg <*> spacingArg) O.idm
iconsArg :: O.Parser Bool
iconsArg = O.switch (O.long "icons")
spacingArg :: O.Parser Int
spacingArg = O.option O.auto (O.long "spacing" <> O.value 0)
data Env = Env
{ dpy :: X.Display,
win :: X.Window,
swidth :: Int,
sheight :: Int,
wwidth :: Int,
wheight :: Int,
wleft :: Int,
gc :: X.GC,
pixm :: X.Pixmap,
fnt :: X.XftFont,
drw :: X.XftDraw,
cmap :: X.Colormap,
vis :: X.Visual,
xcolors :: M.Map P.XColor String
}
type Colors = M.Map (P.Intensity, P.Color) X.XftColor
data State = State
{ dirty :: Bool,
ui :: Ui (S.Sensor (S.SensorT IO) () (Block P.Doc))
}
main :: IO ()
main = do
O.execParser args >>= \args -> do
bracket (createWindow args) destroyWindow $ \(env, stateT) ->
withColors env (run env stateT)
run :: Env -> TVar State -> Colors -> IO ()
run env stateT colors = do
-- XXX `ui` lives in state, but we treat is as if it was constant
-- XXX this is supposed to NOT wait for the next event if `state.dirty`
S.runSensorT . S.sample' go . pollUi . (.ui) =<< atomically do readTVar stateT
where
go Nothing pUi = do
atomically do
writeTVar stateT . (\state -> state {dirty = False})
=<< readTVar stateT
lUi <- liftIO (paint env colors Nothing pUi)
pure lUi
go (Just lUi') pUi = do
dirty <- atomically do
state@State {..} <- readTVar stateT
writeTVar stateT state {dirty = False}
pure dirty
lUi <- liftIO (paint env colors (if dirty then Nothing else Just lUi') pUi)
pure lUi
processEvents :: Ptr X.XEvent -> Env -> TVar State -> IO ()
processEvents ev env@(Env {..}) stateT = do
timeOut <- X.waitForEvent dpy 1_000_000_000
unless timeOut do
processEvent ev env stateT
processEvents ev env stateT
processEvent :: Ptr X.XEvent -> Env -> TVar State -> IO ()
processEvent ev (Env {..}) stateT = do
X.nextEvent dpy ev
e <- X.getEvent ev
if
| X.ExposeEvent {} <- e -> atomically do
state <- readTVar stateT
writeTVar stateT state {dirty = True}
| otherwise -> pure ()
paint :: Env -> Colors -> Maybe Ui' -> Ui (Block P.Doc) -> IO Ui'
paint Env {..} colors Nothing ui = do
let env' = Ui.Env {..}
ui' <- layOutUi env' ui
renderUi env' colors ui'
X.copyArea dpy pixm win gc 0 0 (fi wwidth) (fi wheight) 0 0
X.sync dpy False
pure ui'
paint Env {..} colors (Just ui') ui = do
let env' = Ui.Env {..}
ui'' <- layOutUi env' ui
renderUi' env' colors ui' ui''
X.copyArea dpy pixm win gc 0 0 (fi wwidth) (fi wheight) 0 0
X.sync dpy False
pure ui''
destroyWindow :: (Env, TVar State) -> IO ()
destroyWindow (Env {..}, _) = do
X.destroyWindow dpy win
createWindow :: Args -> IO (Env, TVar State)
createWindow args = do
dpy <- X.openDisplay ""
let scrn = X.defaultScreen dpy
scr = X.defaultScreenOfDisplay dpy
root = X.defaultRootWindow dpy
trueColor = 4
Just vinfo <- X.matchVisualInfo dpy scrn 32 trueColor
let cls = X.inputOutput
dpth = X.visualInfo_depth vinfo
vis = X.visualInfo_visual vinfo
vmsk = X.cWColormap .|. X.cWBorderPixel .|. X.cWBackingPixel .|. X.cWOverrideRedirect
swidth = fi (X.displayWidth dpy scrn)
sheight = fi (X.displayHeight dpy scrn)
wwidth = swidth - 2 * wspac
wspac = args.spacing
wleft = wspac
fnt <- X.xftFontOpen dpy scr "IosevkaTerm Nerd Font:size=14"
ascent <- X.xftfont_ascent fnt
descent <- X.xftfont_descent fnt
let wheight = ascent + descent
cmap <- X.createColormap dpy root vis X.allocNone
win <- X.allocaSetWindowAttributes $ \attr -> do
X.set_colormap attr cmap
X.set_border_pixel attr 0
X.set_background_pixel attr 0
X.set_override_redirect attr True
X.createWindow dpy root (fi wleft) 0 (fi wwidth) (fi wheight) 0 dpth cls vis vmsk attr
atom <- X.internAtom dpy "ATOM" True
wmState <- X.internAtom dpy "_NET_WM_STATE" False
wmStateSticky <- X.internAtom dpy "_NET_WM_STATE_STICKY" False
wmStateAbove <- X.internAtom dpy "_NET_WM_STATE_ABOVE" False
wmWindowType <- X.internAtom dpy "_NET_WM_WINDOW_TYPE" False
wmWindowTypeDock <- X.internAtom dpy "_NET_WM_WINDOW_TYPE_DOCK" False
wmStrut <- X.internAtom dpy "_NET_WM_STRUT" False
wmStrutPartial <- X.internAtom dpy "_NET_WM_STRUT_PARTIAL" False
X.changeProperty32 dpy win wmState atom X.propModeReplace $
[ fi wmStateAbove,
fi wmStateSticky
]
X.changeProperty32 dpy win wmWindowType atom X.propModeReplace $
[ fi wmWindowTypeDock
]
X.changeProperty32 dpy win wmStrut atom X.propModeReplace $
[0, 0, fi (wheight + wspac), 0]
X.changeProperty32 dpy win wmStrutPartial atom X.propModeReplace $
[0, 0, fi (wheight + wspac), 0, 0, 0, 0, 0, fi wleft, fi wwidth, 0, 0]
pixm <- X.createPixmap dpy win (fi wwidth) (fi wheight) dpth
gc <- X.createGC dpy win
drw <- X.xftDrawCreate dpy pixm vis cmap
X.mapWindow dpy win
let dirty = True
let ui :: Ui (S.Sensor (S.SensorT IO) () (Block P.Doc))
ui =
Ui $
intercalate [pure (lit " ")] $
[ [lit <$> wmWorkspaces],
[lit <$> wmName, pure fill],
[pure (lit (if args.icons then "\xf4bc " else "cpu ")), lit <$> cpu],
[pure (lit (if args.icons then "\xf035b " else "mem ")), lit <$> mem],
[pure (lit (if args.icons then "\xf0a0 " else "disk ")), lit <$> disk],
[pure (lit (if args.icons then "\xf1638 " else "io ")), lit <$> io],
[pure (lit (if args.icons then "\xf0200 " else "net ")), lit <$> net],
[pure (lit " "), lit <$> Sensor.snd],
[pure (lit " "), lit <$> thermal],
[pure (lit " "), lit <$> bat],
[pure (lit " "), lit <$> weatherForecast],
[pure (lit " "), lit <$> date, pure (lit ", "), lit <$> time],
[]
]
xcolors <- do
X.rmInitialize
rdb <- X.rmGetStringDatabase (X.resourceManagerString dpy)
M.fromList
<$> mapM
( \(n, def) -> do
(toEnum n,) . fromMaybe def
<$> X.rmGetResource rdb ("astatusbar*color" ++ show n)
)
[ (0 {- black -}, "rgb:0/0/0"),
(1 {- red3 -}, "rgb:205/0/0"),
(2 {- green3 -}, "rgb:0/205/0"),
(3 {- yellow3 -}, "rgb:205/205/0"),
(4 {- blue2 -}, "rgb:0/0/238"),
(5 {- meganta3 -}, "rgb:205/0/205"),
(6 {- cyan3 -}, "rgb:0/205/205"),
(7 {- gray90 -}, "rgb:229/229/229"),
(8 {- gray50 -}, "rgb:127/127/127"),
(9 {- red -}, "rgb:255/0/0"),
(10 {- green -}, "rgb:0/255/0"),
(11 {- yellow -}, "rgb:255/255/0"),
(12, "rgb:5c/5c/ff"),
(13 {- meganta -}, "rgb:255/0/255"),
(14 {- cyan -}, "rgb:0/255/255"),
(15 {- white -}, "rgb:255/255/255")
]
let env = Env {..}
stateT <- newTVarIO State {..}
void $ forkIO $ X.allocaXEvent $ \ev -> forever do
processEvents ev env stateT
pure (env, stateT)
withColors :: Env -> (Colors -> IO a) -> IO a
withColors Env {..} act = do
X.withXftColorName dpy vis cmap (xcolors M.! P.toXColor (P.Vivid, P.Black)) $ \black -> do
X.withXftColorName dpy vis cmap (xcolors M.! P.toXColor (P.Vivid, P.Red)) $ \red -> do
X.withXftColorName dpy vis cmap (xcolors M.! P.toXColor (P.Vivid, P.Green)) $ \green -> do
X.withXftColorName dpy vis cmap (xcolors M.! P.toXColor (P.Vivid, P.Yellow)) $ \yellow -> do
X.withXftColorName dpy vis cmap (xcolors M.! P.toXColor (P.Vivid, P.Blue)) $ \blue -> do
X.withXftColorName dpy vis cmap (xcolors M.! P.toXColor (P.Vivid, P.Magenta)) $ \magenta -> do
X.withXftColorName dpy vis cmap (xcolors M.! P.toXColor (P.Vivid, P.Cyan)) $ \cyan -> do
X.withXftColorName dpy vis cmap (xcolors M.! P.toXColor (P.Vivid, P.White)) $ \white -> do
X.withXftColorName dpy vis cmap (xcolors M.! P.toXColor (P.Dull, P.Black)) $ \dullBlack -> do
X.withXftColorName dpy vis cmap (xcolors M.! P.toXColor (P.Dull, P.Red)) $ \dullRred -> do
X.withXftColorName dpy vis cmap (xcolors M.! P.toXColor (P.Dull, P.Green)) $ \dullGreen -> do
X.withXftColorName dpy vis cmap (xcolors M.! P.toXColor (P.Dull, P.Yellow)) $ \dullYellow -> do
X.withXftColorName dpy vis cmap (xcolors M.! P.toXColor (P.Dull, P.Blue)) $ \dullBlue -> do
X.withXftColorName dpy vis cmap (xcolors M.! P.toXColor (P.Dull, P.Magenta)) $ \dullMagenta -> do
X.withXftColorName dpy vis cmap (xcolors M.! P.toXColor (P.Dull, P.Cyan)) $ \dullCyan -> do
X.withXftColorName dpy vis cmap (xcolors M.! P.toXColor (P.Dull, P.White)) $ \dullWhite -> do
let colors =
M.fromList
[ ((P.Vivid, P.Black), black),
((P.Vivid, P.Red), red),
((P.Vivid, P.Green), green),
((P.Vivid, P.Yellow), yellow),
((P.Vivid, P.Blue), blue),
((P.Vivid, P.Magenta), magenta),
((P.Vivid, P.Cyan), cyan),
((P.Vivid, P.White), white),
((P.Dull, P.Black), dullBlack),
((P.Dull, P.Red), dullRred),
((P.Dull, P.Green), dullGreen),
((P.Dull, P.Yellow), dullYellow),
((P.Dull, P.Blue), dullBlue),
((P.Dull, P.Magenta), dullMagenta),
((P.Dull, P.Cyan), dullCyan),
((P.Dull, P.White), dullWhite)
]
act colors
fi :: (Integral a, Num b) => a -> b
fi = fromIntegral
clamp :: (Ord a) => a -> a -> a -> a
clamp mi ma = max mi . min ma
|