Skip to content

Commit

Permalink
fix Tb_cell attribute sizes
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellwrosen committed Nov 7, 2023
1 parent 290e1a8 commit d451cc1
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions termbox2-bindings-c/src/Termbox2/Bindings/C/Internal/Cell.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Termbox2.Bindings.C.Internal.Cell
)
where

import Data.Word (Word16, Word32)
import Data.Word (Word32, Word64)
import Foreign.C (CSize)
import Foreign.Ptr (Ptr)
import Foreign.Storable (Storable)
Expand All @@ -15,22 +15,22 @@ data {-# CTYPE "termbox2.h" "struct tb_cell" #-} Tb_cell = Tb_cell
{ -- | A unicode character.
ch :: {-# UNPACK #-} !Word32,
-- | Foreground attribute.
fg :: {-# UNPACK #-} !Word16,
fg :: {-# UNPACK #-} !Word64,
-- | Background attribute.
bg :: {-# UNPACK #-} !Word16,
bg :: {-# UNPACK #-} !Word64,
-- | A grapheme cluster.
ech :: {-# UNPACK #-} !(Ptr Word32),
-- | Length of @ech@, in bytes; 0 means use @ch@.
-- | Length of @ech@; 0 means use @ch@.
nech :: {-# UNPACK #-} !CSize,
-- | Capacity of @ech@, in bytes.
-- | Capacity of @ech@.
cech :: {-# UNPACK #-} !CSize
}
deriving stock (Eq, Generic, Ord, Show)

instance Storable Tb_cell where
sizeOf :: Tb_cell -> Int
sizeOf _ =
8 + sizeofPtr + sizeofCSize + sizeofCSize
20 + sizeofPtr + sizeofCSize + sizeofCSize

alignment :: Tb_cell -> Int
alignment _ =
Expand All @@ -41,23 +41,23 @@ instance Storable Tb_cell where
Tb_cell
<$> Storable.peekByteOff ptr 0
<*> Storable.peekByteOff ptr 4
<*> Storable.peekByteOff ptr 6
<*> Storable.peekByteOff ptr 8
<*> Storable.peekByteOff ptr (8 + sizeofPtr)
<*> Storable.peekByteOff ptr (8 + sizeofPtr + sizeofCSize)
<*> Storable.peekByteOff ptr 12
<*> Storable.peekByteOff ptr 20
<*> Storable.peekByteOff ptr (20 + sizeofPtr)
<*> Storable.peekByteOff ptr (20 + sizeofPtr + sizeofCSize)

poke :: Ptr Tb_cell -> Tb_cell -> IO ()
poke ptr Tb_cell {ch, fg, bg, ech, nech, cech} = do
Storable.pokeByteOff ptr 0 ch
Storable.pokeByteOff ptr 4 fg
Storable.pokeByteOff ptr 6 bg
Storable.pokeByteOff ptr 8 ech
Storable.pokeByteOff ptr (8 + sizeofPtr) nech
Storable.pokeByteOff ptr (8 + sizeofPtr + sizeofCSize) cech
Storable.pokeByteOff ptr 12 bg
Storable.pokeByteOff ptr 20 ech
Storable.pokeByteOff ptr (20 + sizeofPtr) nech
Storable.pokeByteOff ptr (20 + sizeofPtr + sizeofCSize) cech

sizeofPtr :: Int
sizeofPtr =
Storable.sizeOf (undefined :: Ptr Word32)
Storable.sizeOf (undefined :: Ptr ())

sizeofCSize :: Int
sizeofCSize =
Expand Down

0 comments on commit d451cc1

Please sign in to comment.