-
Notifications
You must be signed in to change notification settings - Fork 21
/
Curses.hsc
1552 lines (1291 loc) · 48.8 KB
/
Curses.hsc
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{-# LANGUAGE ForeignFunctionInterface, GeneralizedNewtypeDeriving #-}
-- glaexts needed for newtype deriving
-- Copyright (c) 2002-2004 John Meacham (john at repetae dot net)
-- Copyright (c) 2004 Don Stewart - http://www.cse.unsw.edu.au/~dons
-- Copyright (c) 2005-2011 Stefan Wehr - http://www.stefanwehr.de
--
-- This library is free software; you can redistribute it and/or
-- modify it under the terms of the GNU Lesser General Public
-- License as published by the Free Software Foundation; either
-- version 2.1 of the License, or (at your option) any later version.
--
-- This library 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
-- Lesser General Public License for more details.
--
-- You should have received a copy of the GNU Lesser General Public
-- License along with this library; if not, write to the Free Software
-- Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
--
-- | Binding to the [wn]curses library. From the ncurses man page:
--
-- > The curses library routines give the user a terminal-inde-
-- > pendent method of updating character screens with reason-
-- > able optimization.
--
-- Sections of the quoted documentation are from the OpenBSD man pages, which
-- are distributed under a BSD license.
--
-- A useful reference is:
-- /Writing Programs with NCURSES/, by Eric S. Raymond and Zeyd
-- M. Ben-Halim, <http://dickey.his.com/ncurses/>
--
-- N.B attrs don't work with Irix curses.h. This should be fixed.
--
module UI.HSCurses.Curses (
-- * Basic Functions
stdScr, -- :: Window
initScr, -- :: IO Window
initCurses, -- :: IO ()
resetParams,
endWin, -- :: IO ()
scrSize, -- :: IO (Int, Int)
-- * Windows and Pads
Window, -- data Window deriving Eq
Border(..), -- data Border
touchWin,
newPad, pRefresh, delWin, newWin, wRefresh, wnoutRefresh, wBorder, defaultBorder,
-- * Refresh Routines
refresh, -- :: IO ()
update,
resizeTerminal,
timeout, -- :: Int -> IO ()
noqiflush, -- :: IO ()
-- * Navigation
move, -- :: Int -> Int -> IO ()
getYX,
-- * Input
getCh, getch, decodeKey, ungetCh, keyResizeCode,
-- * Input Options
cBreak, -- :: Bool -> IO ()
raw, -- :: Bool -> IO ()
echo, -- :: Bool -> IO ()
intrFlush, -- :: Bool -> IO ()
keypad, -- :: Window -> Bool -> IO ()
noDelay, -- :: Window -> Bool -> IO ()
-- * Output
wAddStr, -- :: Window -> String -> IO ()
addLn, -- :: IO ()
mvWAddStr,
mvAddCh, -- :: Int -> Int -> ChType -> IO ()
wMove,
bkgrndSet, -- :: Attr -> Pair -> IO ()
erase, -- :: IO ()
wclear, -- :: Window -> IO ()
werase,
clrToEol, -- :: IO ()
wClrToEol,
beep,
waddch,
winsch,
waddchnstr, -- :: Window -> CString -> CInt -> IO CInt
-- * Output Options
clearOk,
leaveOk,
nl, -- :: Bool -> IO ()
-- * Cursor Routines
CursorVisibility(..), cursSet,
-- * Color Support
hasColors, -- :: IO Bool
startColor, -- :: IO ()
useDefaultColors, -- :: IO ()
Pair(..), -- newtype Pair = Pair Int deriving (Eq, Ord, Ix)
colorPairs, -- :: IO Int
Color(..), -- newtype Color = Color Int deriving (Eq, Ord, Ix)
colors, -- :: IO Int
color, -- :: String -> Maybe Color
-- black, red, green, yellow, blue, magenta, cyan, white, -- :: Color
initPair, -- :: Pair -> Color -> Color -> IO ()
pairContent, -- :: Pair -> IO (Color, Color)
canChangeColor, -- :: IO Bool
initColor, -- :: Color -> (Int, Int, Int) -> IO ()
colorContent, -- :: Color -> IO (Int, Int, Int)
defaultBackground, defaultForeground,
-- * Attributes
attrPlus,
Attr,
attr0, -- :: Attr
isAltCharset, isBlink, isBold, isDim, isHorizontal, isInvis,
isLeft, isLow, isProtect, isReverse, isRight, isStandout, isTop,
isUnderline, isVertical,
-- :: Attr -> Bool
setAltCharset, setBlink, setBold, setDim, setHorizontal, setInvis,
setLeft, setLow, setProtect, setReverse, setRight, setStandout,
setTop, setUnderline, setVertical,
-- :: Attr -> Bool -> Attr
attrSet, -- :: Attr -> Pair -> IO ()
attrOn, attrOff,
standout,standend,
attrDim, attrBold,
attrDimOn, attrDimOff,
attrBoldOn, attrBoldOff,
wAttrOn,
wAttrOff,
wAttrSet, wAttrGet,
-- * Mouse Routines
withMouseEventMask,
ButtonEvent(..),
MouseEvent(..),
-- * Keys
Key(..),
cERR,
cKEY_UP,
cKEY_DOWN,
cKEY_LEFT,
cKEY_RIGHT,
cTRUE,
-- cACS_BLOCK,
-- * Lines
vline,
ulCorner, llCorner, urCorner, lrCorner, rTee, lTee, bTee, tTee, hLine,
vLine, plus, s1, s9, diamond, ckBoard, degree, plMinus, bullet,
lArrow, rArrow, dArrow, uArrow, board, lantern, block,
s3, s7, lEqual, gEqual, pi, nEqual, sterling,
-- * Signals
cursesSigWinch,
-- * Misc
cursesTest,
throwIfErr, throwIfErr_,
errI, -- :: IO CInt -> IO ()
flushinp,
recognize,
ChType,
NBool
) where
#include <HSCurses.h>
#if HAVE_SIGNAL_H
# include <signal.h>
#endif
import UI.HSCurses.CWString ( withLCStringLen )
import UI.HSCurses.Logging
import Prelude hiding ( pi )
import Data.Char
import Data.List
import Data.Ix ( Ix )
import System.IO.Unsafe ( unsafePerformIO )
import Control.Monad ( when, liftM, void )
import Control.Monad.Trans
import Control.Concurrent
#if !MIN_VERSION_base(4,7,0)
import Foreign hiding ( unsafePerformIO, void )
#else
import Foreign hiding ( void )
#endif
import Foreign.C.String
import Foreign.C.Types
import Foreign.C.Error
#ifndef mingw32_HOST_OS
import System.Posix.Signals
#endif
#if __GLASGOW_HASKELL__ < 603
import Data.Bits
#endif
------------------------------------------------------------------------
--
-- | @initCurses fn@ does all initialization necessary for a Curses
-- application.
--
initCurses :: IO ()
initCurses = do
initScr
b <- hasColors
when b $ startColor >> useDefaultColors
resetParams :: IO ()
resetParams = do
raw True -- raw mode please
echo False
nl False
intrFlush True
leaveOk False
keypad stdScr True
#ifdef NCURSES_EXT_FUNCS
defineKey (#const KEY_UP) "\x1b[1;2A"
defineKey (#const KEY_DOWN) "\x1b[1;2B"
defineKey (#const KEY_SLEFT) "\x1b[1;2D"
defineKey (#const KEY_SRIGHT) "\x1b[1;2C"
defineKey (#const KEY_B2) "\x1b[E" -- xterm seems to emit B2, not BEG
defineKey (#const KEY_END) "\x1b[F"
defineKey (#const KEY_END) "\x1b[4~"
defineKey (#const KEY_HOME) "\x1b[H"
defineKey (#const KEY_HOME) "\x1b[1~"
#endif
------------------------------------------------------------------------
fi :: (Integral a, Num b) => a -> b
fi = fromIntegral
throwIfErr :: (Eq a, Show a, Num a) => String -> IO a -> IO a
--throwIfErr name act = do
-- res <- act
-- if res == (cERR)
-- then ioError (userError ("Curses: "++name++" failed"))
-- else return res
throwIfErr s = throwIf (== (#const ERR)) (\a -> "Curses[" ++ show a ++ "]:" ++ s)
throwIfErr_ :: (Eq a, Show a, Num a) => String -> IO a -> IO ()
throwIfErr_ name act = void $ throwIfErr name act
errI :: IO CInt -> IO ()
errI f = do r <- f
if r == cERR then do _ <- endwin
error "curses returned an error"
else return ()
------------------------------------------------------------------------
type WindowTag = ()
type Window = Ptr WindowTag
type ChType = #type chtype
type NBool = #type bool
{-# NOINLINE stdScr #-}
--
-- | The standard screen
--
stdScr :: Window
stdScr = unsafePerformIO (peek stdscr)
foreign import ccall "static HSCurses.h &stdscr"
stdscr :: Ptr Window
--
-- | initscr is normally the first curses routine to call when
-- initializing a program. curs_initscr(3):
--
-- > To initialize the routines, the routine initscr or newterm
-- > must be called before any of the other routines that deal
-- > with windows and screens are used.
--
-- > The initscr code determines the terminal type and initial-
-- > izes all curses data structures. initscr also causes the
-- > first call to refresh to clear the screen. If errors
-- > occur, initscr writes an appropriate error message to
-- > standard error and exits; otherwise, a pointer is returned
-- > to stdscr.
--
initScr :: IO Window
initScr = throwIfNull "initscr" initscr
foreign import ccall unsafe "HSCurses.h initscr" initscr :: IO Window
--
-- | > The cbreak routine
-- > disables line buffering and erase/kill character-process-
-- > ing (interrupt and flow control characters are unaf-
-- > fected), making characters typed by the user immediately
-- > available to the program. The nocbreak routine returns
-- > the terminal to normal (cooked) mode.
--
cBreak :: Bool -> IO ()
cBreak True = throwIfErr_ "cbreak" cbreak
cBreak False = throwIfErr_ "nocbreak" nocbreak
foreign import ccall unsafe "HSCurses.h cbreak" cbreak :: IO CInt
foreign import ccall unsafe "HSCurses.h nocbreak" nocbreak :: IO CInt
--
-- |> The raw and noraw routines place the terminal into or out
-- > of raw mode. Raw mode is similar to cbreak mode, in that
-- > characters typed are immediately passed through to the
-- > user program. The differences are that in raw mode, the
-- > interrupt, quit, suspend, and flow control characters are
-- > all passed through uninterpreted, instead of generating a
-- > signal. The behavior of the BREAK key depends on other
-- > bits in the tty driver that are not set by curses.
--
raw :: Bool -> IO ()
raw False = throwIfErr_ "noraw" noraw
raw True = throwIfErr_ "raw" raw_c
foreign import ccall unsafe "HSCurses.h noraw" noraw :: IO CInt
foreign import ccall unsafe "HSCurses.h raw" raw_c :: IO CInt
--
-- |> The echo and noecho routines control whether characters
-- > typed by the user are echoed by getch as they are typed.
-- > Echoing by the tty driver is always disabled, but ini-
-- > tially getch is in echo mode, so characters typed are
-- > echoed. Authors of most interactive programs prefer to do
-- > their own echoing in a controlled area of the screen, or
-- > not to echo at all, so they disable echoing by calling
-- > noecho. [See curs_getch(3) for a discussion of how these
-- > routines interact with cbreak and nocbreak.]
-- >
--
echo :: Bool -> IO ()
echo False = throwIfErr_ "noecho" noecho
echo True = throwIfErr_ "echo" echo_c
foreign import ccall unsafe "HSCurses.h noecho" noecho :: IO CInt
foreign import ccall unsafe "HSCurses.h echo" echo_c :: IO CInt
--
-- |> The nl and nonl routines control whether the underlying
-- > display device translates the return key into newline on
-- > input, and whether it translates newline into return and
-- > line-feed on output (in either case, the call addch('\n')
-- > does the equivalent of return and line feed on the virtual
-- > screen). Initially, these translations do occur. If you
-- > disable them using nonl, curses will be able to make bet-
-- > ter use of the line-feed capability, resulting in faster
-- > cursor motion. Also, curses will then be able to detect
-- > the return key.
-- >
nl :: Bool -> IO ()
nl True = throwIfErr_ "nl" nl_c
nl False = throwIfErr_ "nonl" nonl
foreign import ccall unsafe "HSCurses.h nl" nl_c :: IO CInt
foreign import ccall unsafe "HSCurses.h nonl" nonl :: IO CInt
-- |> If the intrflush option is enabled, (bf is TRUE), when an
-- > interrupt key is pressed on the keyboard (interrupt,
-- > break, quit) all output in the tty driver queue will be
-- > flushed, giving the effect of faster response to the
-- > interrupt, but causing curses to have the wrong idea of
-- > what is on the screen. Disabling (bf is FALSE), the
-- > option prevents the flush.
-- >
intrFlush :: Bool -> IO ()
intrFlush bf =
throwIfErr_ "intrflush" $ intrflush stdScr (if bf then 1 else 0)
foreign import ccall unsafe "HSCurses.h intrflush" intrflush :: Window -> (#type bool) -> IO CInt
--
-- | Enable the keypad of the user's terminal.
--
keypad :: Window -> Bool -> IO ()
keypad win bf = throwIfErr_ "keypad" $ keypad_c win (if bf then 1 else 0)
foreign import ccall unsafe "HSCurses.h keypad"
keypad_c :: Window -> (#type bool) -> IO CInt
noDelay :: Window -> Bool -> IO ()
noDelay win bf =
throwIfErr_ "nodelay" $ nodelay win (if bf then 1 else 0)
foreign import ccall unsafe "HSCurses.h nodelay"
nodelay :: Window -> (#type bool) -> IO CInt
--
-- | Normally, the hardware cursor is left at the location of
-- the window cursor being refreshed. The leaveok option
-- allows the cursor to be left wherever the update happens
-- to leave it. It is useful for applications where the cur-
-- sor is not used, since it reduces the need for cursor
-- motions. If possible, the cursor is made invisible when
-- this option is enabled.
--
leaveOk :: Bool -> IO CInt
leaveOk True = leaveok_c stdScr 1
leaveOk False = leaveok_c stdScr 0
foreign import ccall unsafe "HSCurses.h leaveok"
leaveok_c :: Window -> (#type bool) -> IO CInt
clearOk :: Bool -> IO CInt
clearOk True = clearok_c stdScr 1
clearOk False = clearok_c stdScr 0
foreign import ccall unsafe "HSCurses.h clearok"
clearok_c :: Window -> (#type bool) -> IO CInt
------------------------------------------------------------------------
foreign import ccall unsafe "HSCurses.h use_default_colors"
useDefaultColors :: IO ()
defaultBackground, defaultForeground :: Color
defaultBackground = Color (-1)
defaultForeground = Color (-1)
------------------------------------------------------------------------
#ifdef NCURSES_EXT_FUNCS
defineKey :: CInt -> String -> IO ()
defineKey k s = withCString s (\s' -> define_key s' k) >> return ()
foreign import ccall unsafe "HSCurses.h define_key"
define_key :: Ptr CChar -> CInt -> IO ()
#endif
--
-- | > The program must call endwin for each terminal being used before
-- > exiting from curses.
--
endWin :: IO ()
endWin = throwIfErr_ "endwin" endwin
foreign import ccall unsafe "HSCurses.h endwin" endwin :: IO CInt
------------------------------------------------------------------------
--
-- | get the dimensions of the screen (lines,cols)
--
scrSize :: IO (Int, Int)
-- Note, per the documentation:
-- http://invisible-island.net/ncurses/ncurses-intro.html#caution
-- It is not recommended to peek at the LINES and COLS global variables. This code
-- was previously doing exactly that, but now it is fixed to use getmaxyx.
-- -Ryan Newton [2013.03.31]
scrSize = do
yfp <- mallocForeignPtr
xfp <- mallocForeignPtr
withForeignPtr yfp $ \yp ->
withForeignPtr xfp $ \xp -> do
getMaxYX stdScr yp xp
y <- peek yp
x <- peek xp
return (fromIntegral y, fromIntegral x)
foreign import ccall "HSCurses.h getmaxyx_fun" getMaxYX
:: Window -> Ptr CInt -> Ptr CInt -> IO ()
--
-- | refresh curses windows and lines. curs_refresh(3)
--
refresh :: IO ()
refresh = throwIfErr_ "refresh" refresh_c
foreign import ccall unsafe "HSCurses.h refresh"
refresh_c :: IO CInt
--
-- | wRefresh refreshes the specified window, copying the data
-- | from the virtual screen to the physical screen.
--
wRefresh :: Window -> IO ()
wRefresh w = throwIfErr_ "wrefresh" $ wrefresh_c w
foreign import ccall unsafe "HSCurses.h wrefresh"
wrefresh_c :: Window -> IO CInt
-- | Stage an update to a window, but don't actually do the refresh until update is
-- called. This allows multiple windows to be updated together more smoothly.
--
wnoutRefresh :: Window -> IO ()
wnoutRefresh w = throwIfErr_ "wnoutrefresh" $ wnoutrefresh_c w
foreign import ccall safe "HSCurses.h wnoutrefresh"
wnoutrefresh_c :: Window -> IO CInt
--
-- | Do an actual update. Used after endWin on linux to restore the terminal
--
update :: IO ()
update = throwIfErr_ "update" update_c
foreign import ccall unsafe "HSCurses.h doupdate" update_c :: IO CInt
foreign import ccall unsafe "static curses.h timeout" timeout_c :: CInt -> IO ()
-- | Set a delay in milliseconds.
timeout :: Int -> IO ()
timeout = timeout_c . fromIntegral
------------------------------------------------------------------------
hasColors :: IO Bool
hasColors = liftM (/= 0) has_colors
foreign import ccall unsafe "HSCurses.h has_colors" has_colors :: IO (#type bool)
--
-- | Initialise the color settings, also sets the screen to the
-- default colors (white on black)
--
startColor :: IO ()
startColor = throwIfErr_ "start_color" start_color
foreign import ccall unsafe start_color :: IO CInt
newtype Pair = Pair Int deriving (Eq, Ord, Ix, Show)
--
-- | colorPairs defines the maximum number of color-pairs the terminal
-- can support).
--
colorPairs :: IO Int
colorPairs = fmap fromIntegral $ peek colorPairsPtr
foreign import ccall "HSCurses.h &COLOR_PAIRS"
colorPairsPtr :: Ptr CInt
newtype Color = Color Int deriving (Eq, Ord, Ix)
colors :: IO Int
colors = liftM fromIntegral $ peek colorsPtr
foreign import ccall "HSCurses.h &COLORS" colorsPtr :: Ptr CInt
--black, red, green, yellow, blue, magenta, cyan, white :: Color
color :: String -> Maybe Color
color "default" = Just $ Color (-1)
color "black" = Just $ Color (#const COLOR_BLACK)
color "red" = Just $ Color (#const COLOR_RED)
color "green" = Just $ Color (#const COLOR_GREEN)
color "yellow" = Just $ Color (#const COLOR_YELLOW)
color "blue" = Just $ Color (#const COLOR_BLUE)
color "magenta" = Just $ Color (#const COLOR_MAGENTA)
color "cyan" = Just $ Color (#const COLOR_CYAN)
color "white" = Just $ Color (#const COLOR_WHITE)
color _ = Nothing
--
-- | curses support color attributes on terminals with that
-- capability. To use these routines start_color must be
-- called, usually right after initscr. Colors are always
-- used in pairs (referred to as color-pairs). A color-pair
-- consists of a foreground color (for characters) and a
-- background color (for the blank field on which the charac-
-- ters are displayed). A programmer initializes a color-
-- pair with the routine init_pair. After it has been ini-
-- tialized, COLOR_PAIR(n), a macro defined in <curses.h>,
-- can be used as a new video attribute.
--
-- If a terminal is capable of redefining colors, the pro-
-- grammer can use the routine init_color to change the defi-
-- nition of a color.
--
-- The init_pair routine changes the definition of a color-
-- pair. It takes three arguments: the number of the color-
-- pair to be changed, the foreground color number, and the
-- background color number. For portable applications:
--
-- - The value of the first argument must be between 1 and
-- COLOR_PAIRS-1.
--
-- - The value of the second and third arguments must be
-- between 0 and COLORS (the 0 color pair is wired to
-- white on black and cannot be changed).
--
--
initPair :: Pair -> Color -> Color -> IO ()
initPair (Pair p) (Color f) (Color b) =
throwIfErr_ "init_pair" $
init_pair (fromIntegral p) (fromIntegral f) (fromIntegral b)
foreign import ccall unsafe
init_pair :: CShort -> CShort -> CShort -> IO CInt
pairContent :: Pair -> IO (Color, Color)
pairContent (Pair p) =
alloca $ \fPtr ->
alloca $ \bPtr -> do
throwIfErr "pair_content" $ pair_content (fromIntegral p) fPtr bPtr
f <- peek fPtr
b <- peek bPtr
return (Color (fromIntegral f), Color (fromIntegral b))
foreign import ccall unsafe pair_content :: CShort -> Ptr CShort -> Ptr CShort -> IO CInt
canChangeColor :: IO Bool
canChangeColor = liftM (/= 0) can_change_color
foreign import ccall unsafe can_change_color :: IO (#type bool)
initColor :: Color -> (Int, Int, Int) -> IO ()
initColor (Color c) (r, g, b) =
throwIfErr_ "init_color" $
init_color (fromIntegral c) (fromIntegral r) (fromIntegral g) (fromIntegral b)
foreign import ccall unsafe init_color :: CShort -> CShort -> CShort -> CShort -> IO CInt
colorContent :: Color -> IO (Int, Int, Int)
colorContent (Color c) =
alloca $ \rPtr ->
alloca $ \gPtr ->
alloca $ \bPtr -> do
throwIfErr "color_content" $ color_content (fromIntegral c) rPtr gPtr bPtr
r <- peek rPtr
g <- peek gPtr
b <- peek bPtr
return (fromIntegral r, fromIntegral g, fromIntegral b)
foreign import ccall unsafe
color_content :: CShort -> Ptr CShort -> Ptr CShort -> Ptr CShort -> IO CInt
foreign import ccall unsafe "HSCurses.h hs_curses_color_pair" colorPair :: Pair -> ChType
-------------
-- Attributes
-------------
foreign import ccall unsafe "HSCurses.h attr_set"
attr_set :: Attr -> CShort -> Ptr a -> IO Int
-- foreign import ccall unsafe "HSCurses.h attr_get" :: Attr -> CShort -> Ptr a -> IO Int
foreign import ccall unsafe "HSCurses.h wattr_set"
wattr_set :: Window -> Attr -> CInt -> Ptr a -> IO CInt
foreign import ccall unsafe "HSCurses.h wattr_get"
wattr_get :: Window -> Ptr Attr -> Ptr CShort -> Ptr a -> IO CInt
foreign import ccall "HSCurses.h attr_on" attr_on :: (#type attr_t) -> Ptr a -> IO Int
foreign import ccall "HSCurses.h attr_off" attr_off :: (#type attr_t) -> Ptr a -> IO Int
foreign import ccall "HSCurses.h attron" attron :: Int -> IO Int
foreign import ccall "HSCurses.h attroff" attroff :: Int -> IO Int
foreign import ccall unsafe "HSCurses.h wattron" wattron :: Window -> CInt -> IO CInt
foreign import ccall unsafe "HSCurses.h wattroff" wattroff :: Window -> CInt -> IO CInt
foreign import ccall standout :: IO Int
foreign import ccall standend :: IO Int
--
-- |
--
wAttrSet :: Window -> (Attr,Pair) -> IO ()
wAttrSet w (a,(Pair p)) = throwIfErr_ "wattr_set" $
wattr_set w a (fromIntegral p) nullPtr
--
-- | manipulate the current attributes of the named window. see curs_attr(3)
--
wAttrGet :: Window -> IO (Attr,Pair)
wAttrGet w =
alloca $ \pa ->
alloca $ \pp -> do
throwIfErr_ "wattr_get" $ wattr_get w pa pp nullPtr
a <- peek pa
p <- peek pp
return (a,Pair $ fromIntegral p)
newtype Attr = Attr (#type attr_t) deriving (Eq,Storable,Bits, Num, Show)
--
-- | Normal display (no highlight)
--
attr0 :: Attr
#ifdef WA_NORMAL
attr0 = Attr (#const WA_NORMAL)
#else
attr0 = Attr (#const A_NORMAL)
#endif
isAltCharset, isBlink, isBold, isDim, isHorizontal, isInvis, isLeft,
isLow, isProtect, isReverse, isRight, isStandout, isTop,
isUnderline, isVertical :: Attr -> Bool
isAltCharset = isAttr (#const WA_ALTCHARSET)
isBlink = isAttr (#const WA_BLINK)
isBold = isAttr (#const WA_BOLD)
isDim = isAttr (#const WA_DIM)
isHorizontal = isAttr (#const WA_HORIZONTAL)
isInvis = isAttr (#const WA_INVIS)
isLeft = isAttr (#const WA_LEFT)
isLow = isAttr (#const WA_LOW)
isProtect = isAttr (#const WA_PROTECT)
isReverse = isAttr (#const WA_REVERSE)
isRight = isAttr (#const WA_RIGHT)
isStandout = isAttr (#const WA_STANDOUT)
isTop = isAttr (#const WA_TOP)
isUnderline = isAttr (#const WA_UNDERLINE)
isVertical = isAttr (#const WA_VERTICAL)
isAttr :: (#type attr_t) -> Attr -> Bool
isAttr b (Attr a) = a .&. b /= 0
--
-- | Setting attributes
--
setAltCharset, setBlink, setBold, setDim, setHorizontal, setInvis,
setLeft, setLow, setProtect, setReverse, setRight, setStandout,
setTop, setUnderline, setVertical :: Attr -> Bool -> Attr
setAltCharset = setAttr (#const WA_ALTCHARSET)
setBlink = setAttr (#const WA_BLINK)
setBold = setAttr (#const WA_BOLD)
setDim = setAttr (#const WA_DIM)
setHorizontal = setAttr (#const WA_HORIZONTAL)
setInvis = setAttr (#const WA_INVIS)
setLeft = setAttr (#const WA_LEFT)
setLow = setAttr (#const WA_LOW)
setProtect = setAttr (#const WA_PROTECT)
setReverse = setAttr (#const WA_REVERSE)
setRight = setAttr (#const WA_RIGHT)
setStandout = setAttr (#const WA_STANDOUT)
setTop = setAttr (#const WA_TOP)
setUnderline = setAttr (#const WA_UNDERLINE)
setVertical = setAttr (#const WA_VERTICAL)
setAttr :: (#type attr_t) -> Attr -> Bool -> Attr
setAttr b (Attr a) False = Attr (a .&. complement b)
setAttr b (Attr a) True = Attr (a .|. b)
attrPlus :: Attr -> Attr -> Attr
attrPlus (Attr a) (Attr b) = Attr (a .|. b)
attrSet :: Attr -> Pair -> IO ()
attrSet attr (Pair p) = throwIfErr_ "attrset" $
attr_set attr (fromIntegral p) nullPtr
attrOn :: Attr -> IO ()
attrOn (Attr attr) = throwIfErr_ "attr_on" $
attr_on attr nullPtr
attrOff :: Attr -> IO ()
attrOff (Attr attr) = throwIfErr_ "attr_off" $
attr_off attr nullPtr
wAttrOn :: Window -> Int -> IO ()
wAttrOn w x = throwIfErr_ "wattron" $ wattron w (fi x)
wAttrOff :: Window -> Int -> IO ()
wAttrOff w x = throwIfErr_ "wattroff" $ wattroff w (fi x)
attrDimOn :: IO ()
attrDimOn = throwIfErr_ "attron A_DIM" $
attron (#const A_DIM)
attrDimOff :: IO ()
attrDimOff = throwIfErr_ "attroff A_DIM" $
attroff (#const A_DIM)
attrBoldOn :: IO ()
attrBoldOn = throwIfErr_ "attron A_BOLD" $
attron (#const A_BOLD)
attrBoldOff :: IO ()
attrBoldOff = throwIfErr_ "attroff A_BOLD" $
attroff (#const A_BOLD)
attrDim :: Int
attrDim = (#const A_DIM)
attrBold :: Int
attrBold = (#const A_BOLD)
------------------------------------------------------------------------
-- | Raw NCurses routine.
foreign import ccall safe
waddch :: Window -> ChType -> IO CInt
-- | Raw NCurses routine.
foreign import ccall safe
winsch :: Window -> ChType -> IO CInt
-- | Raw NCurses routine.
foreign import ccall safe
waddchnstr :: Window -> CString -> CInt -> IO CInt
foreign import ccall safe "static curses.h mvaddch" mvaddch_c :: CInt -> CInt -> ChType -> IO ()
mvWAddStr :: Window -> Int -> Int -> String -> IO ()
mvWAddStr w y x str = wMove w y x >> wAddStr w str
mvAddCh :: Int -> Int -> ChType -> IO ()
mvAddCh l m n = mvaddch_c (fromIntegral l) (fromIntegral m) (fromIntegral n)
addLn :: IO ()
addLn = wAddStr stdScr "\n"
--
-- | normalise the string, stripping \\r and making control chars
-- printable. Called over all output(?)
{-
normalise :: String -> String
normalise [] = []
normalise ('\r':cs) = normalise cs
normalise (c:cs) | isControl c = '@' : normalise cs
| otherwise = c : normalise cs
{-# INLINE normalise #-}
-}
{-
normalise s = map f . filter (/= '\r') s
where
f c | isPrint c = c
f c = '@'
-}
------------------------------------------------------------------------
#if defined(HAVE_WCHAR_H) && (defined(HAVE_LIBNCURSESW) || defined(HAVE_LIBPDCURSESW))
--wAddStr :: Window -> String -> IO ()
--wAddStr w str = throwIfErr_ ("waddnwstr: " ++ show str) $ withCWStringLen (normalise str) (\(ws,len) -> waddnwstr w ws (fi len))
foreign import ccall unsafe
waddnwstr :: Window -> CWString -> CInt -> IO CInt
wAddStr :: Window -> String -> IO ()
wAddStr win str = do
let
convStr f = case f [] of
[] -> return ()
s -> throwIfErr_ "waddnstr" $
withCWStringLen (s) (\(ws,len) -> (waddnwstr win ws (fi len)))
loop [] acc = convStr acc
loop (ch:str') acc = recognize
ch
(loop str' (acc . (ch:)))
(\ch' -> do
convStr acc
throwIfErr "waddch" $ waddch win ch'
loop str' id)
loop str id
#else
--
-- This is heavily called, and does a lot of allocs. We walk over all
-- the string accumulating a list of characters to be drawn.
--
-- Got it down to:
--
-- wAddStr Yi.Curses 20.0 38.1
-- wAddStr Yi.Curses 10.0 32.5
--
-- TODO make this way less expensive. That accum sucks.
-- use difference lists for O(1) append
--
wAddStr :: Window -> [Char] -> IO ()
wAddStr _ [] = return ()
wAddStr win s = throwIfErr_ ("waddnstr: <" ++ s ++ ">") $
withLCStringLen (s) (\(ws,len) -> waddnstr win ws (fi len))
foreign import ccall safe
waddnstr :: Window -> CString -> CInt -> IO CInt
#endif
foreign import ccall safe
vline :: Char -> Int -> IO ()
------------------------------------------------------------------------
--
-- what ?
--
#let translate_attr attr = \
"(if a .&. %lu /= 0 then %lu else 0) .|.", \
(unsigned long) WA_##attr, (unsigned long) A_##attr
bkgrndSet :: Attr -> Pair -> IO ()
bkgrndSet (Attr a) p = bkgdset $
fromIntegral (ord ' ') .|.
#translate_attr ALTCHARSET
#translate_attr BLINK
#translate_attr BOLD
#translate_attr DIM
#translate_attr INVIS
#translate_attr PROTECT
#translate_attr REVERSE
#translate_attr STANDOUT
#translate_attr UNDERLINE
colorPair p
foreign import ccall unsafe bkgdset :: ChType -> IO ()
-- | Copy blanks to every position in the screen.
erase :: IO ()
erase = throwIfErr_ "erase" $ werase_c stdScr
foreign import ccall unsafe "werase" werase_c :: Window -> IO CInt
-- | Copy blanks to every position in a window.
werase :: Window -> IO ()
werase w = throwIfErr_ "werase" $ werase_c w
-- | Copy blanks to a window and set clearOk for that window.
wclear :: Window -> IO ()
wclear w = throwIfErr_ "wclear" $ wclear_c w
foreign import ccall unsafe "wclear" wclear_c :: Window -> IO CInt
clrToEol :: IO ()
clrToEol = throwIfErr_ "clrtoeol" clrtoeol
foreign import ccall unsafe clrtoeol :: IO CInt
--
-- | > move the cursor associated with the window
-- > to line y and column x. This routine does not move the
-- > physical cursor of the terminal until refresh is called.
-- > The position specified is relative to the upper left-hand
-- > corner of the window, which is (0,0).
--
-- Note that 'move_c' may be a macro.
--
move :: Int -> Int -> IO ()
move y x = throwIfErr_ "move" $ move_c (fromIntegral y) (fromIntegral x)
foreign import ccall unsafe "move"
move_c :: CInt -> CInt -> IO CInt
--
-- | > move the cursor associated with the window
-- > to line y and column x. This routine does not move the
-- > physical cursor of the terminal until refresh is called.
-- > The position specified is relative to the upper left-hand
-- > corner of the window, which is (0,0).
--
wMove :: Window -> Int -> Int -> IO ()
wMove w y x = throwIfErr_ "wmove" $ wmove w (fi y) (fi x)
foreign import ccall unsafe
wmove :: Window -> CInt -> CInt -> IO CInt
------------------
-- Cursor routines
------------------
data CursorVisibility = CursorInvisible | CursorVisible | CursorVeryVisible
vis_c :: CursorVisibility -> CInt
vis_c vis = case vis of
CursorInvisible -> 0
CursorVisible -> 1
CursorVeryVisible -> 2
c_vis :: CInt -> CursorVisibility
c_vis 0 = CursorInvisible
c_vis 1 = CursorVisible
c_vis 2 = CursorVeryVisible
c_vis n = error ("Illegal C value for cursor visibility: " ++ show n)
--
-- | Set the cursor state
--
-- > The curs_set routine sets the cursor state is set to
-- > invisible, normal, or very visible for visibility equal to
-- > 0, 1, or 2 respectively. If the terminal supports the
-- > visibility requested, the previous cursor state is
-- > returned; otherwise, ERR is returned.
--
cursSet :: CursorVisibility -> IO CursorVisibility
cursSet CursorInvisible =
do leaveOk True