Skip to content

Commit

Permalink
more termbox2 hs bindings work
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellwrosen committed Nov 14, 2023
1 parent a2f6f42 commit b757d19
Show file tree
Hide file tree
Showing 8 changed files with 471 additions and 112 deletions.
137 changes: 137 additions & 0 deletions termbox2-bindings-c/src/Termbox2/Bindings/C/Internal/Functions.hs
Original file line number Diff line number Diff line change
Expand Up @@ -42,48 +42,149 @@ foreign import capi unsafe "termbox2.h tb_cell_buffer"
tb_cell_buffer :: IO (Ptr Tb_cell)

-- | Clear the back buffer.
--
-- Can fail with:
--
-- * __TB_ERR_MEM__
-- * __TB_ERR_NOT_INIT__
foreign import capi unsafe "termbox2.h tb_clear"
tb_clear :: IO CInt

-- | Append a code point to a cell in the back buffer.
--
-- Can fail with:
--
-- * __TB_ERR_MEM__
-- * __TB_ERR_NOT_INIT__
-- * __TB_ERR_OUT_OF_BOUNDS__
foreign import capi unsafe "termbox2.h tb_extend_cell"
tb_extend_cell :: CInt -> CInt -> Word32 -> IO CInt

-- | Get the terminal and resize file descriptors.
--
-- Can fail with:
--
-- * __TB_ERR_NOT_INIT__
foreign import capi unsafe "termbox2.h tb_get_fds"
tb_get_fds :: Ptr CInt -> Ptr CInt -> IO CInt

-- | Get the terminal height.
--
-- Can fail with:
--
-- * __TB_ERR_NOT_INIT__
foreign import capi unsafe "termbox2.h tb_height"
tb_height :: IO CInt

-- | Hide the cursor in the back buffer.
--
-- Can fail with:
--
-- * __TB_ERR_MEM__
-- * __TB_ERR_NOT_INIT__
foreign import capi unsafe "termbox2.h tb_hide_cursor"
tb_hide_cursor :: IO CInt

-- | Initialize the @termbox@ library.
--
-- Can fail with:
--
-- * __TB_ERR__
-- * __TB_ERR_INIT_ALREADY__
-- * __TB_ERR_INIT_OPEN__
-- * __TB_ERR_MEM__
-- * __TB_ERR_NO_TERM__
-- * __TB_ERR_RESIZE_IOCTL__
-- * __TB_ERR_RESIZE_PIPE__
-- * __TB_ERR_RESIZE_POLL__
-- * __TB_ERR_RESIZE_READ__
-- * __TB_ERR_RESIZE_SIGACTION__
-- * __TB_ERR_RESIZE_SSCANF__
-- * __TB_ERR_RESIZE_WRITE__
-- * __TB_ERR_TCGETATTR__
-- * __TB_ERR_TCSETATTR__
-- * __TB_ERR_UNSUPPORTED_TERM__
foreign import capi unsafe "termbox2.h tb_init"
tb_init :: IO CInt

-- | Initialize the @termbox@ library.
--
-- > tb_init = tb_init_fd(0)
--
-- Can fail with:
--
-- * __TB_ERR__
-- * __TB_ERR_INIT_ALREADY__
-- * __TB_ERR_INIT_OPEN__
-- * __TB_ERR_MEM__
-- * __TB_ERR_NO_TERM__
-- * __TB_ERR_RESIZE_IOCTL__
-- * __TB_ERR_RESIZE_PIPE__
-- * __TB_ERR_RESIZE_POLL__
-- * __TB_ERR_RESIZE_READ__
-- * __TB_ERR_RESIZE_SIGACTION__
-- * __TB_ERR_RESIZE_SSCANF__
-- * __TB_ERR_RESIZE_WRITE__
-- * __TB_ERR_TCGETATTR__
-- * __TB_ERR_TCSETATTR__
-- * __TB_ERR_UNSUPPORTED_TERM__
foreign import capi unsafe "termbox2.h tb_init_fd"
tb_init_fd :: CInt -> IO CInt

-- | Initialize the @termbox@ library.
--
-- > tb_init = tb_init_file("/dev/tty")
--
-- Can fail with:
--
-- * __TB_ERR__
-- * __TB_ERR_INIT_ALREADY__
-- * __TB_ERR_INIT_OPEN__
-- * __TB_ERR_MEM__
-- * __TB_ERR_NO_TERM__
-- * __TB_ERR_RESIZE_IOCTL__
-- * __TB_ERR_RESIZE_PIPE__
-- * __TB_ERR_RESIZE_POLL__
-- * __TB_ERR_RESIZE_READ__
-- * __TB_ERR_RESIZE_SIGACTION__
-- * __TB_ERR_RESIZE_SSCANF__
-- * __TB_ERR_RESIZE_WRITE__
-- * __TB_ERR_TCGETATTR__
-- * __TB_ERR_TCSETATTR__
-- * __TB_ERR_UNSUPPORTED_TERM__
foreign import capi unsafe "termbox2.h tb_init_file"
tb_init_file :: CString -> IO CInt

-- | Initialize the @termbox@ library.
--
-- > tb_init = tb_init_rwfd(0, 0)
--
-- Can fail with:
--
-- * __TB_ERR__
-- * __TB_ERR_INIT_ALREADY__
-- * __TB_ERR_INIT_OPEN__
-- * __TB_ERR_MEM__
-- * __TB_ERR_NO_TERM__
-- * __TB_ERR_RESIZE_IOCTL__
-- * __TB_ERR_RESIZE_PIPE__
-- * __TB_ERR_RESIZE_POLL__
-- * __TB_ERR_RESIZE_READ__
-- * __TB_ERR_RESIZE_SIGACTION__
-- * __TB_ERR_RESIZE_SSCANF__
-- * __TB_ERR_RESIZE_WRITE__
-- * __TB_ERR_TCGETATTR__
-- * __TB_ERR_TCSETATTR__
-- * __TB_ERR_UNSUPPORTED_TERM__
foreign import capi unsafe "termbox2.h tb_init_rwfd"
tb_init_rwfd :: CInt -> CInt -> IO CInt

-- | Invalidate the screen, causing a redraw. This is mainly used after switching output modes.
--
-- Can fail with:
--
-- * __TB_ERR_MEM__
-- * __TB_ERR_NOT_INIT__
foreign import capi unsafe "termbox2.h tb_invalidate"
tb_invalidate :: IO CInt

Expand All @@ -92,14 +193,46 @@ foreign import capi unsafe "termbox2.h tb_last_errno"
tb_last_errno :: IO CInt

-- | Wait up to a number of milliseconds for an event.
--
-- Can fail with:
--
-- * __TB_ERR_MEM__
-- * __TB_ERR_NOT_INIT__
-- * __TB_ERR_NO_EVENT__
-- * __TB_ERR_POLL__
-- * __TB_ERR_READ__
-- * __TB_ERR_RESIZE_IOCTL__
-- * __TB_ERR_RESIZE_POLL__
-- * __TB_ERR_RESIZE_READ__
-- * __TB_ERR_RESIZE_SSCANF__
-- * __TB_ERR_RESIZE_WRITE__
foreign import capi interruptible "termbox2.h tb_peek_event"
tb_peek_event :: Ptr Tb_event -> CInt -> IO CInt

-- | Wait for an event.
--
-- Can fail with:
--
-- * __TB_ERR_MEM__
-- * __TB_ERR_NOT_INIT__
-- * __TB_ERR_POLL__
-- * __TB_ERR_READ__
-- * __TB_ERR_RESIZE_IOCTL__
-- * __TB_ERR_RESIZE_POLL__
-- * __TB_ERR_RESIZE_READ__
-- * __TB_ERR_RESIZE_SSCANF__
-- * __TB_ERR_RESIZE_WRITE__
foreign import capi interruptible "termbox2.h tb_poll_event"
tb_poll_event :: Ptr Tb_event -> IO CInt

-- | Synchronize the back buffer with the terminal.
--
-- Can fail with:
--
-- * __TB_ERR__
-- * __TB_ERR_MEM__
-- * __TB_ERR_NOT_INIT__
-- * __TB_ERR_OUT_OF_BOUNDS__
foreign import capi unsafe "termbox2.h tb_present"
tb_present :: IO CInt

Expand Down Expand Up @@ -152,5 +285,9 @@ foreign import capi unsafe "termbox2.h tb_strerror"
tb_strerror :: CInt -> IO (ConstPtr CChar)

-- | Get the terminal width.
--
-- Can fail with:
--
-- * __TB_ERR_NOT_INIT__
foreign import capi unsafe "termbox2.h tb_width"
tb_width :: IO CInt
43 changes: 43 additions & 0 deletions termbox2-bindings-hs/examples/Demo.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{-# LANGUAGE OverloadedRecordDot #-}
{-# LANGUAGE OverloadedStrings #-}
{-# OPTIONS_GHC -Wno-unused-do-bind #-}

module Main (main) where

import Data.Text qualified as Text
import Termbox2.Bindings.Hs
import Text.Printf (printf)

-- This simple example ignores all result codes :)

main :: IO ()
main = do
tb_init
tb_print 0 0 (TB_RED <> TB_BOLD) TB_DEFAULT "hello from haskell"
tb_print 0 1 TB_GREEN TB_DEFAULT "press any key"
tb_present
Right event <- tb_poll_event
tb_print
0
2
TB_YELLOW
TB_DEFAULT
( Text.pack
( printf
"event: type=%s mod=%s key=%s ch=%d w=%d h=%d x=%d y=%d"
(show event.type_)
(show event.mod)
(show event.key)
event.ch
event.w
event.h
event.x
event.y
)
)
tb_present
tb_print 0 3 TB_BLUE TB_DEFAULT "press any key to quit"
tb_present
tb_poll_event
tb_shutdown
pure ()
24 changes: 10 additions & 14 deletions termbox2-bindings-hs/src/Termbox2/Bindings/Hs.hs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ module Termbox2.Bindings.Hs
-- * Types
Tb_attr
( Tb_attr,
TB_DEFAULT,
TB_BLACK,
TB_BLUE,
TB_CYAN,
TB_GREEN,
TB_HI_BLACK,
TB_MAGENTA,
TB_RED,
TB_WHITE,
TB_YELLOW,
TB_BLINK,
TB_BOLD,
TB_BRIGHT,
Expand All @@ -64,19 +74,6 @@ module Termbox2.Bindings.Hs
TB_UNDERLINE,
TB_UNDERLINE_2
),
Tb_color
( Tb_color,
TB_DEFAULT,
TB_BLACK,
TB_BLUE,
TB_CYAN,
TB_GREEN,
TB_HI_BLACK,
TB_MAGENTA,
TB_RED,
TB_WHITE,
TB_YELLOW
),
Tb_error
( Tb_error,
TB_ERR,
Expand Down Expand Up @@ -205,7 +202,6 @@ module Termbox2.Bindings.Hs
where

import Termbox2.Bindings.Hs.Internal.Attr (Tb_attr (..))
import Termbox2.Bindings.Hs.Internal.Color (Tb_color (..))
import Termbox2.Bindings.Hs.Internal.Error (Tb_error (..))
import Termbox2.Bindings.Hs.Internal.Event (Tb_event (..))
import Termbox2.Bindings.Hs.Internal.EventMod (Tb_event_mod (..), _TB_MOD_ALT, _TB_MOD_CTRL, _TB_MOD_MOTION, _TB_MOD_SHIFT)
Expand Down
Loading

0 comments on commit b757d19

Please sign in to comment.