@@ -45,6 +45,8 @@ module System.Win32.Console (
45
45
generateConsoleCtrlEvent ,
46
46
-- * Command line
47
47
commandLineToArgv ,
48
+ getCommandLineW ,
49
+ getArgs ,
48
50
-- * Screen buffer
49
51
CONSOLE_SCREEN_BUFFER_INFO (.. ),
50
52
CONSOLE_SCREEN_BUFFER_INFOEX (.. ),
@@ -63,21 +65,15 @@ module System.Win32.Console (
63
65
#include "wincon_compat.h"
64
66
65
67
import System.Win32.Types
68
+ import System.Win32.Console.Internal
66
69
import Graphics.Win32.Misc
67
70
import Graphics.Win32.GDI.Types (COLORREF )
68
71
69
- import Foreign.C.Types (CInt (.. ))
70
- import Foreign.C.String (withCWString , CWString )
71
- import Foreign.Ptr (Ptr , plusPtr )
72
+ import Foreign.C.String (withCWString )
72
73
import Foreign.Storable (Storable (.. ))
73
- import Foreign.Marshal.Array (peekArray , pokeArray )
74
+ import Foreign.Marshal.Array (peekArray )
74
75
import Foreign.Marshal.Alloc (alloca )
75
76
76
- foreign import WINDOWS_CCONV unsafe " windows.h GetConsoleMode"
77
- c_GetConsoleMode :: HANDLE -> LPDWORD -> IO BOOL
78
-
79
- foreign import WINDOWS_CCONV unsafe " windows.h SetConsoleMode"
80
- c_SetConsoleMode :: HANDLE -> DWORD -> IO BOOL
81
77
82
78
getConsoleMode :: HANDLE -> IO DWORD
83
79
getConsoleMode h = alloca $ \ ptr -> do
@@ -107,36 +103,12 @@ eNABLE_VIRTUAL_TERMINAL_PROCESSING = 4
107
103
dISABLE_NEWLINE_AUTO_RETURN = 8
108
104
eNABLE_LVB_GRID_WORLDWIDE = 16
109
105
110
- foreign import WINDOWS_CCONV unsafe " windows.h GetConsoleCP"
111
- getConsoleCP :: IO UINT
112
-
113
- foreign import WINDOWS_CCONV unsafe " windows.h SetConsoleCP"
114
- setConsoleCP :: UINT -> IO ()
115
-
116
- foreign import WINDOWS_CCONV unsafe " windows.h GetConsoleOutputCP"
117
- getConsoleOutputCP :: IO UINT
118
-
119
- foreign import WINDOWS_CCONV unsafe " windows.h SetConsoleOutputCP"
120
- setConsoleOutputCP :: UINT -> IO ()
121
-
122
- type CtrlEvent = DWORD
123
- #{enum CtrlEvent,
124
- , cTRL_C_EVENT = 0
125
- , cTRL_BREAK_EVENT = 1
126
- }
127
-
128
106
generateConsoleCtrlEvent :: CtrlEvent -> DWORD -> IO ()
129
107
generateConsoleCtrlEvent e p
130
108
= failIfFalse_
131
109
" generateConsoleCtrlEvent"
132
110
$ c_GenerateConsoleCtrlEvent e p
133
111
134
- foreign import WINDOWS_CCONV safe " windows.h GenerateConsoleCtrlEvent"
135
- c_GenerateConsoleCtrlEvent :: CtrlEvent -> DWORD -> IO BOOL
136
-
137
- foreign import WINDOWS_CCONV unsafe " Shellapi.h CommandLineToArgvW"
138
- c_CommandLineToArgvW :: CWString -> Ptr CInt -> IO (Ptr CWString )
139
-
140
112
-- | This function can be used to parse command line arguments and return
141
113
-- the split up arguments as elements in a list.
142
114
commandLineToArgv :: String -> IO [String ]
@@ -150,118 +122,12 @@ commandLineToArgv arg =
150
122
_ <- localFree res
151
123
mapM peekTString args
152
124
153
- data CONSOLE_SCREEN_BUFFER_INFO = CONSOLE_SCREEN_BUFFER_INFO
154
- { dwSize :: COORD
155
- , dwCursorPosition :: COORD
156
- , wAttributes :: WORD
157
- , srWindow :: SMALL_RECT
158
- , dwMaximumWindowSize :: COORD
159
- } deriving (Show , Eq )
160
-
161
- instance Storable CONSOLE_SCREEN_BUFFER_INFO where
162
- sizeOf = const # {size CONSOLE_SCREEN_BUFFER_INFO }
163
- alignment _ = # alignment CONSOLE_SCREEN_BUFFER_INFO
164
- peek buf = do
165
- dwSize' <- (# peek CONSOLE_SCREEN_BUFFER_INFO , dwSize) buf
166
- dwCursorPosition' <- (# peek CONSOLE_SCREEN_BUFFER_INFO , dwCursorPosition) buf
167
- wAttributes' <- (# peek CONSOLE_SCREEN_BUFFER_INFO , wAttributes) buf
168
- srWindow' <- (# peek CONSOLE_SCREEN_BUFFER_INFO , srWindow) buf
169
- dwMaximumWindowSize' <- (# peek CONSOLE_SCREEN_BUFFER_INFO , dwMaximumWindowSize) buf
170
- return $ CONSOLE_SCREEN_BUFFER_INFO dwSize' dwCursorPosition' wAttributes' srWindow' dwMaximumWindowSize'
171
- poke buf info = do
172
- (# poke CONSOLE_SCREEN_BUFFER_INFO , dwSize) buf (dwSize info)
173
- (# poke CONSOLE_SCREEN_BUFFER_INFO , dwCursorPosition) buf (dwCursorPosition info)
174
- (# poke CONSOLE_SCREEN_BUFFER_INFO , wAttributes) buf (wAttributes info)
175
- (# poke CONSOLE_SCREEN_BUFFER_INFO , srWindow) buf (srWindow info)
176
- (# poke CONSOLE_SCREEN_BUFFER_INFO , dwMaximumWindowSize) buf (dwMaximumWindowSize info)
177
-
178
- data CONSOLE_SCREEN_BUFFER_INFOEX = CONSOLE_SCREEN_BUFFER_INFOEX
179
- { dwSizeEx :: COORD
180
- , dwCursorPositionEx :: COORD
181
- , wAttributesEx :: WORD
182
- , srWindowEx :: SMALL_RECT
183
- , dwMaximumWindowSizeEx :: COORD
184
- , wPopupAttributes :: WORD
185
- , bFullscreenSupported :: BOOL
186
- , colorTable :: [COLORREF ]
187
- -- ^ Only the first 16 'COLORREF' values passed to the Windows Console
188
- -- API. If fewer than 16 values, the remainder are padded with @0@ when
189
- -- passed to the API.
190
- } deriving (Show , Eq )
191
-
192
- instance Storable CONSOLE_SCREEN_BUFFER_INFOEX where
193
- sizeOf = const # {size CONSOLE_SCREEN_BUFFER_INFOEX }
194
- alignment = const # {alignment CONSOLE_SCREEN_BUFFER_INFOEX }
195
- peek buf = do
196
- dwSize' <- (# peek CONSOLE_SCREEN_BUFFER_INFOEX , dwSize) buf
197
- dwCursorPosition' <- (# peek CONSOLE_SCREEN_BUFFER_INFOEX , dwCursorPosition) buf
198
- wAttributes' <- (# peek CONSOLE_SCREEN_BUFFER_INFOEX , wAttributes) buf
199
- srWindow' <- (# peek CONSOLE_SCREEN_BUFFER_INFOEX , srWindow) buf
200
- dwMaximumWindowSize' <- (# peek CONSOLE_SCREEN_BUFFER_INFOEX , dwMaximumWindowSize) buf
201
- wPopupAttributes' <- (# peek CONSOLE_SCREEN_BUFFER_INFOEX , wPopupAttributes) buf
202
- bFullscreenSupported' <- (# peek CONSOLE_SCREEN_BUFFER_INFOEX , bFullscreenSupported) buf
203
- colorTable' <- peekArray 16 ((# ptr CONSOLE_SCREEN_BUFFER_INFOEX , ColorTable ) buf)
204
- return $ CONSOLE_SCREEN_BUFFER_INFOEX dwSize' dwCursorPosition'
205
- wAttributes' srWindow' dwMaximumWindowSize' wPopupAttributes'
206
- bFullscreenSupported' colorTable'
207
- poke buf info = do
208
- (# poke CONSOLE_SCREEN_BUFFER_INFOEX , cbSize) buf cbSize
209
- (# poke CONSOLE_SCREEN_BUFFER_INFOEX , dwSize) buf (dwSizeEx info)
210
- (# poke CONSOLE_SCREEN_BUFFER_INFOEX , dwCursorPosition) buf (dwCursorPositionEx info)
211
- (# poke CONSOLE_SCREEN_BUFFER_INFOEX , wAttributes) buf (wAttributesEx info)
212
- (# poke CONSOLE_SCREEN_BUFFER_INFOEX , srWindow) buf (srWindowEx info)
213
- (# poke CONSOLE_SCREEN_BUFFER_INFOEX , dwMaximumWindowSize) buf (dwMaximumWindowSizeEx info)
214
- (# poke CONSOLE_SCREEN_BUFFER_INFOEX , wPopupAttributes) buf (wPopupAttributes info)
215
- (# poke CONSOLE_SCREEN_BUFFER_INFOEX , bFullscreenSupported) buf (bFullscreenSupported info)
216
- pokeArray ((# ptr CONSOLE_SCREEN_BUFFER_INFOEX , ColorTable ) buf) colorTable'
217
- where
218
- cbSize :: ULONG
219
- cbSize = # {size CONSOLE_SCREEN_BUFFER_INFOEX }
220
- colorTable' = take 16 $ colorTable info ++ repeat 0
221
-
222
- data COORD = COORD
223
- { xPos :: SHORT
224
- , yPos :: SHORT
225
- } deriving (Show , Eq )
226
-
227
- instance Storable COORD where
228
- sizeOf = const # {size COORD }
229
- alignment _ = # alignment COORD
230
- peek buf = do
231
- x' <- (# peek COORD , X ) buf
232
- y' <- (# peek COORD , Y ) buf
233
- return $ COORD x' y'
234
- poke buf coord = do
235
- (# poke COORD , X ) buf (xPos coord)
236
- (# poke COORD , Y ) buf (yPos coord)
237
-
238
- data SMALL_RECT = SMALL_RECT
239
- { leftPos :: SHORT
240
- , topPos :: SHORT
241
- , rightPos :: SHORT
242
- , bottomPos :: SHORT
243
- } deriving (Show , Eq )
244
-
245
- instance Storable SMALL_RECT where
246
- sizeOf _ = # {size SMALL_RECT }
247
- alignment _ = # alignment SMALL_RECT
248
- peek buf = do
249
- left' <- (# peek SMALL_RECT , Left ) buf
250
- top' <- (# peek SMALL_RECT , Top ) buf
251
- right' <- (# peek SMALL_RECT , Right ) buf
252
- bottom' <- (# peek SMALL_RECT , Bottom ) buf
253
- return $ SMALL_RECT left' top' right' bottom'
254
- poke buf small_rect = do
255
- (# poke SMALL_RECT , Left ) buf (leftPos small_rect)
256
- (# poke SMALL_RECT , Top ) buf (topPos small_rect)
257
- (# poke SMALL_RECT , Right ) buf (rightPos small_rect)
258
- (# poke SMALL_RECT , Bottom ) buf (bottomPos small_rect)
259
-
260
- foreign import WINDOWS_CCONV safe " windows.h GetConsoleScreenBufferInfo"
261
- c_GetConsoleScreenBufferInfo :: HANDLE -> Ptr CONSOLE_SCREEN_BUFFER_INFO -> IO BOOL
262
-
263
- foreign import WINDOWS_CCONV safe " windows.h GetConsoleScreenBufferInfoEx"
264
- c_GetConsoleScreenBufferInfoEx :: HANDLE -> Ptr CONSOLE_SCREEN_BUFFER_INFOEX -> IO BOOL
125
+ -- | Based on 'GetCommandLineW'. This behaves slightly different
126
+ -- than 'System.Environment.getArgs'. See the online documentation:
127
+ -- <https://learn.microsoft.com/en-us/windows/win32/api/processenv/nf-processenv-getcommandlinew>
128
+ getArgs :: IO [String ]
129
+ getArgs = do
130
+ getCommandLineW >>= peekTString >>= commandLineToArgv
265
131
266
132
getConsoleScreenBufferInfo :: HANDLE -> IO CONSOLE_SCREEN_BUFFER_INFO
267
133
getConsoleScreenBufferInfo h = alloca $ \ ptr -> do
0 commit comments