{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
{- |
 - Module      : Haskengl.Internal.IO.Interrupts
 - Description : manages interrupts
 - Copyright   : Andromeda 2025
 - License     : WTFPL
 - Maintainer  : Matrix @Andromeda:tchncs.de
 - Stability   : Experimental
 -}
module Haskengl.Internal.IO.Interrupts where

import Relude

import qualified Haskengl.Internal.Bindings.OpenGL as GL
import           Haskengl.Internal.Bindings.OpenGL         (($=))
import qualified Haskengl.Internal.Bindings.GLFW   as GLFW

-- | handles keypresses
keyPressed :: GLFW.KeyCallback
keyPressed window GLFW.Key'Escape _ GLFW.KeyState'Pressed _ = shutdownWindow window
keyPressed _ _ _ _ _ = return ()

-- | close callback
shutdownWindow :: GLFW.WindowCloseCallback
shutdownWindow window =
  do
    closeWindow window
    _ <- exitSuccess
    return ()

-- | resize callback
resizeWindow :: GLFW.WindowSizeCallback
resizeWindow _ w h =
  do
    GL.viewport $= (GL.Position 0 0, GL.Size (fromIntegral w) (fromIntegral h))

-- | close window inentionally
closeWindow :: GLFW.Window -> IO ()
closeWindow window =
  do
    GLFW.destroyWindow window
    GLFW.terminate
