-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c8b40e9
commit 146fa3b
Showing
7 changed files
with
155 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
termbox2-bindings-hs/src/Termbox2/Bindings/Hs/Internal/Event.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
module Termbox2.Bindings.Hs.Internal.Event | ||
( Tb_event (..), | ||
makeEvent, | ||
) | ||
where | ||
|
||
import Data.Int (Int32) | ||
import GHC.Generics (Generic) | ||
import Termbox2.Bindings.C qualified as Termbox | ||
import Termbox2.Bindings.Hs.Internal.EventMod (Tb_event_mod (..)) | ||
import Termbox2.Bindings.Hs.Internal.EventType (Tb_event_type (..)) | ||
import Termbox2.Bindings.Hs.Internal.Key (Tb_key (..)) | ||
import Termbox2.Bindings.Hs.Internal.Prelude (word32ToChar) | ||
import Prelude hiding (mod) | ||
|
||
-- | An event. | ||
data Tb_event = Tb_event | ||
{ type_ :: {-# UNPACK #-} !Tb_event_type, | ||
mod :: {-# UNPACK #-} !Tb_event_mod, | ||
key :: {-# UNPACK #-} !Tb_key, | ||
ch :: {-# UNPACK #-} !Char, | ||
w :: {-# UNPACK #-} !Int32, | ||
h :: {-# UNPACK #-} !Int32, | ||
x :: {-# UNPACK #-} !Int32, | ||
y :: {-# UNPACK #-} !Int32 | ||
} | ||
deriving stock (Eq, Generic, Ord, Show) | ||
|
||
makeEvent :: Termbox.Tb_event -> Tb_event | ||
makeEvent Termbox.Tb_event {type_, mod, key, ch, w, h, x, y} = | ||
Tb_event | ||
{ type_ = Tb_event_type type_, | ||
mod = Tb_event_mod mod, | ||
key = Tb_key key, | ||
ch = word32ToChar ch, | ||
w, | ||
h, | ||
x, | ||
y | ||
} | ||
{-# INLINE makeEvent #-} |
47 changes: 47 additions & 0 deletions
47
termbox2-bindings-hs/src/Termbox2/Bindings/Hs/Internal/EventMod.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
module Termbox2.Bindings.Hs.Internal.EventMod | ||
( Tb_event_mod (Tb_event_mod), | ||
_TB_MOD_ALT, | ||
_TB_MOD_CTRL, | ||
_TB_MOD_SHIFT, | ||
_TB_MOD_MOTION, | ||
tb_event_mod_has, | ||
) | ||
where | ||
|
||
import Data.Bits ((.&.), (.|.)) | ||
import Data.Coerce (coerce) | ||
import Data.Word (Word8) | ||
import Termbox2.Bindings.C qualified as Termbox | ||
|
||
-- | An event modifier. | ||
newtype Tb_event_mod | ||
= Tb_event_mod Word8 | ||
deriving stock (Eq, Ord, Show) | ||
|
||
instance Semigroup Tb_event_mod where | ||
(<>) = coerce ((.|.) :: Word8 -> Word8 -> Word8) | ||
|
||
_TB_MOD_ALT :: Tb_event_mod | ||
_TB_MOD_ALT = | ||
Tb_event_mod Termbox._TB_MOD_ALT | ||
|
||
_TB_MOD_CTRL :: Tb_event_mod | ||
_TB_MOD_CTRL = | ||
Tb_event_mod Termbox._TB_MOD_CTRL | ||
|
||
_TB_MOD_SHIFT :: Tb_event_mod | ||
_TB_MOD_SHIFT = | ||
Tb_event_mod Termbox._TB_MOD_SHIFT | ||
|
||
_TB_MOD_MOTION :: Tb_event_mod | ||
_TB_MOD_MOTION = | ||
Tb_event_mod Termbox._TB_MOD_MOTION | ||
|
||
-- | @tb_event_mod_has query modifier@ returns whether @modifier@ contains every modifier in @query@. | ||
-- | ||
-- For example, to query whether a modifier contains the @ALT@ and @SHIFT@ modifiers: | ||
-- | ||
-- > tb_event_mod_has (_TB_MOD_ALT <> _TB_MOD_SHIFT) | ||
tb_event_mod_has :: Tb_event_mod -> Tb_event_mod -> Bool | ||
tb_event_mod_has (Tb_event_mod query) (Tb_event_mod modifier) = | ||
modifier .&. query == query |
44 changes: 44 additions & 0 deletions
44
termbox2-bindings-hs/src/Termbox2/Bindings/Hs/Internal/EventType.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
module Termbox2.Bindings.Hs.Internal.EventType | ||
( Tb_event_type | ||
( Tb_event_type, | ||
TB_EVENT_KEY, | ||
TB_EVENT_MOUSE, | ||
TB_EVENT_RESIZE | ||
), | ||
) | ||
where | ||
|
||
import Data.Word (Word8) | ||
import Termbox2.Bindings.C | ||
|
||
-- | An event type. | ||
newtype Tb_event_type | ||
= Tb_event_type Word8 | ||
deriving stock (Eq, Ord) | ||
|
||
instance Show Tb_event_type where | ||
show = \case | ||
TB_EVENT_KEY -> "TB_EVENT_KEY" | ||
TB_EVENT_MOUSE -> "TB_EVENT_MOUSE" | ||
TB_EVENT_RESIZE -> "TB_EVENT_RESIZE" | ||
|
||
pattern TB_EVENT_KEY :: Tb_event_type | ||
pattern TB_EVENT_KEY <- | ||
((== Tb_event_type _TB_EVENT_KEY) -> True) | ||
where | ||
TB_EVENT_KEY = Tb_event_type _TB_EVENT_KEY | ||
|
||
pattern TB_EVENT_MOUSE :: Tb_event_type | ||
pattern TB_EVENT_MOUSE <- | ||
((== Tb_event_type _TB_EVENT_MOUSE) -> True) | ||
where | ||
TB_EVENT_MOUSE = Tb_event_type _TB_EVENT_MOUSE | ||
|
||
pattern TB_EVENT_RESIZE :: Tb_event_type | ||
pattern TB_EVENT_RESIZE <- | ||
((== Tb_event_type _TB_EVENT_RESIZE) -> True) | ||
where | ||
TB_EVENT_RESIZE = Tb_event_type _TB_EVENT_RESIZE | ||
|
||
-- N.B. This requires Tb_event_type to remain abstract | ||
{-# COMPLETE TB_EVENT_KEY, TB_EVENT_MOUSE, TB_EVENT_RESIZE #-} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters