-
Notifications
You must be signed in to change notification settings - Fork 0
/
State.hs
176 lines (147 loc) · 6.45 KB
/
State.hs
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
--
-- Copyright (c) 2004-2008 Don Stewart - http://www.cse.unsw.edu.au/~dons
-- Copyright (c) 2019-2024 Galen Huntington
--
-- This program is free software; you can redistribute it and/or
-- modify it under the terms of the GNU General Public License as
-- published by the Free Software Foundation; either version 2 of
-- the License, or (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-- General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program; if not, write to the Free Software
-- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
-- 02111-1307, USA.
--
--
-- | The top level application state, and operations on that value.
--
module State where
import Base
import FastIO (FiltHandle(..))
import Syntax (Status(Stopped), Mode(..), Frame, Info,Id3)
import Tree (FileArray, DirArray)
import Style (StringA(Fast), defaultSty, UIStyle)
import qualified Config (defaultStyle)
import Text.Regex.PCRE.Light (Regex)
import Data.Array (listArray)
import Data.Sequence (Seq)
import System.Clock (TimeSpec(..))
import System.Process (ProcessHandle)
------------------------------------------------------------------------
-- A state monad over IO would be another option.
-- type ST = StateT HState IO
------------------------------------------------------------------------
-- | The editor state type
data HState = HState {
music :: !FileArray
,folders :: !DirArray
,size :: !Int -- cache size of list
,current :: !Int -- currently playing mp3
,cursor :: !Int -- mp3 under the cursor
,clock :: !(Maybe Frame) -- current clock value
,clockUpdate :: !Bool
,mp3pid :: !(Maybe ProcessHandle) -- pid of decoder
,writeh :: !(MVar Handle) -- handle to mp3 (should be MVars?)
,errh :: !(MVar FiltHandle) -- error handle to mp3
,readf :: !(MVar FiltHandle) -- r/w pipe to mp3
,threads :: ![ThreadId] -- all our threads
,id3 :: !(Maybe Id3) -- maybe mp3 id3 info
,info :: !(Maybe Info) -- mp3 info
,status :: !Status
,minibuffer :: !StringA -- contents of minibuffer
,helpVisible :: !Bool -- is the help window shown?
,histVisible :: !(Maybe [(String, String)]) -- history pop-up if shown
,exitVisible :: !Bool -- confirm exit modal shown
,miniFocused :: !Bool -- is the mini buffer focused?
,mode :: !Mode -- random mode
,uptime :: !ByteString
,boottime :: !TimeSpec
,regex :: !(Maybe (Regex,Bool)) -- most recent search pattern and direction
,xterm :: !Bool
,doNotResuscitate :: !Bool -- should we just let mpg123 die?
,playHist :: Seq (TimeSpec, Int) -- limited history of songs played
,config :: !UIStyle -- config values
,modified :: !(MVar ()) -- Set when redrawable components of
-- the state are modified. The ui
-- refresh thread waits on this.
,drawLock :: !(MVar ()) -- simple semaphore for display
}
------------------------------------------------------------------------
--
-- | The initial state
--
emptySt :: HState
emptySt = HState {
music = listArray (0,0) []
,folders = listArray (0,0) []
,size = 0
,current = 0
,cursor = 0
,threads = []
,modified = unsafePerformIO newEmptyMVar
,writeh = unsafePerformIO newEmptyMVar
,errh = unsafePerformIO newEmptyMVar
,readf = unsafePerformIO newEmptyMVar
,mp3pid = Nothing
,clock = Nothing
,info = Nothing
,id3 = Nothing
,regex = Nothing
,clockUpdate = False
,helpVisible = False
,histVisible = Nothing
,exitVisible = False
,miniFocused = False
,xterm = False
,doNotResuscitate = False -- mpg123 should be restarted
,playHist = mempty
,config = Config.defaultStyle
,boottime = 0
,status = Stopped
,mode = Normal
,minibuffer = Fast mempty defaultSty
,uptime = mempty
,drawLock = unsafePerformIO (newMVar ())
}
--
-- | A global variable holding the state. Todo StateT
--
state :: MVar HState
state = unsafePerformIO $ newMVar emptySt
{-# NOINLINE state #-}
------------------------------------------------------------------------
-- state accessor functions
-- | Access a component of the state with a projection function
getsST :: (HState -> a) -> IO a
getsST f = withST (pure . f)
-- | Perform a (read-only) IO action on the state
withST :: (HState -> IO a) -> IO a
withST f = readMVar state >>= f
-- | Modify the state with a pure function
silentlyModifyST :: (HState -> HState) -> IO ()
silentlyModifyST f = modifyMVar_ state (pure . f)
------------------------------------------------------------------------
modifyST :: (HState -> HState) -> IO ()
modifyST f = silentlyModifyST f <* touchST
-- | Modify the state with an IO action, triggering a refresh
modifySTM :: (HState -> IO HState) -> IO ()
modifySTM f = modifyMVar_ state f <* touchST
-- | Modify the state with an IO action, returning a value
modifySTM_ :: (HState -> IO (HState,a)) -> IO a
modifySTM_ f = modifyMVar state f <* touchST
-- | Trigger a refresh. This is the only way to update the screen
touchST :: IO ()
touchST = withMVar state \st -> void $ tryPutMVar (modified st) ()
forceNextPacket :: IO ()
forceNextPacket = do
fh <- readMVar =<< getsST readf
writeIORef (frameCount fh) 0
withDrawLock :: IO () -> IO ()
withDrawLock io = do
lock <- getsST drawLock
withMVar lock $ const io