packages feed

nano-ui-sdl-0.2.0.0: lib/NanoUI/Sdl/Internal/Cursor.hs

{-# OPTIONS_HADDOCK not-home #-}

-- | Own SDL system cursors and apply nano-ui cursor requests on the display thread.
module NanoUI.Sdl.Internal.Cursor
  ( SdlCursors (..)
  , initCursors
  , destroyCursors
  , syncPointerCursor
  , showCursorKind
  , sdlSystemCursor
  ) where

import Control.Monad (void, when)
import Data.IORef (IORef, modifyIORef', newIORef, readIORef)
import Data.Maybe (isJust)
import Foreign.Ptr (Ptr, nullPtr)
import NanoUI (Input (..))
import NanoUI.Backend (cursorFallback, syncCursorKind)
import NanoUI.Backend (Context, UiCursorKind (..), uiCursorKind)
import System.Environment (lookupEnv)
import System.IO (hPutStrLn, stderr)
import qualified SDL3.Sys.Bindgen.Mouse as Mouse
import SDL3.Sys.Mouse
  ( createSystemCursorSafe
  , destroyCursorSafe
  , getDefaultCursorSafe
  , hideCursorSafe
  , setCursorSafe
  , showCursorSafe
  )

-- | Session-owned cache of SDL system cursors and the cursor kind currently
-- shown on screen.
data SdlCursors = SdlCursors
  { scDefault :: Ptr Mouse.SDL_Cursor
  -- ^ The platform's default arrow, which SDL owns.
  , scSystem :: IORef [(Mouse.SDL_SystemCursor, Ptr Mouse.SDL_Cursor)]
  -- ^ System cursors created so far, each on first use. A NULL entry is one
  -- the platform could not create (headless and dummy video drivers have
  -- none) and is not retried.
  , scCurrent :: IORef UiCursorKind
  , scTrace :: Bool
  }

-- | Initialize the session's cursor cache from the platform default arrow.
initCursors :: IO SdlCursors
initCursors =
  SdlCursors
    <$> getDefaultCursorSafe
    <*> newIORef []
    <*> newIORef UiCursorDefault
    -- Debug aid, read once here so cursor changes stay allocation-free:
    -- NANO_CURSOR_TRACE, set to anything, logs every cursor change to stderr.
    <*> (isJust <$> lookupEnv "NANO_CURSOR_TRACE")

-- | Destroy the cursors this session created. SDL owns the default one.
destroyCursors :: SdlCursors -> IO ()
destroyCursors SdlCursors {scSystem} =
  mapM_ destroyCursorSafe . filter (/= nullPtr) . map snd =<< readIORef scSystem

-- | The SDL system cursor for @kind@ (or its 'cursorFallback'); 'Nothing'
-- for the default arrow. Platforms without one-way resize arrows show
-- two-way ones; SDL handles that.
sdlSystemCursor :: UiCursorKind -> Maybe Mouse.SDL_SystemCursor
sdlSystemCursor kind = case cursorFallback kind of
  UiCursorPointer -> Just Mouse.SDL_SYSTEM_CURSOR_POINTER
  UiCursorText -> Just Mouse.SDL_SYSTEM_CURSOR_TEXT
  UiCursorNsResize -> Just Mouse.SDL_SYSTEM_CURSOR_NS_RESIZE
  UiCursorEwResize -> Just Mouse.SDL_SYSTEM_CURSOR_EW_RESIZE
  UiCursorNwseResize -> Just Mouse.SDL_SYSTEM_CURSOR_NWSE_RESIZE
  UiCursorNeswResize -> Just Mouse.SDL_SYSTEM_CURSOR_NESW_RESIZE
  UiCursorNotAllowed -> Just Mouse.SDL_SYSTEM_CURSOR_NOT_ALLOWED
  UiCursorWait -> Just Mouse.SDL_SYSTEM_CURSOR_WAIT
  UiCursorProgress -> Just Mouse.SDL_SYSTEM_CURSOR_PROGRESS
  UiCursorCrosshair -> Just Mouse.SDL_SYSTEM_CURSOR_CROSSHAIR
  UiCursorMove -> Just Mouse.SDL_SYSTEM_CURSOR_MOVE
  UiCursorNResize -> Just Mouse.SDL_SYSTEM_CURSOR_N_RESIZE
  UiCursorNeResize -> Just Mouse.SDL_SYSTEM_CURSOR_NE_RESIZE
  UiCursorEResize -> Just Mouse.SDL_SYSTEM_CURSOR_E_RESIZE
  UiCursorSeResize -> Just Mouse.SDL_SYSTEM_CURSOR_SE_RESIZE
  UiCursorSResize -> Just Mouse.SDL_SYSTEM_CURSOR_S_RESIZE
  UiCursorSwResize -> Just Mouse.SDL_SYSTEM_CURSOR_SW_RESIZE
  UiCursorWResize -> Just Mouse.SDL_SYSTEM_CURSOR_W_RESIZE
  UiCursorNwResize -> Just Mouse.SDL_SYSTEM_CURSOR_NW_RESIZE
  _ -> Nothing

-- | The cursor for @kind@: its 'sdlSystemCursor', created on first use, or
-- the default arrow if the platform lacks it.
cursorFor :: SdlCursors -> UiCursorKind -> IO (Ptr Mouse.SDL_Cursor)
cursorFor SdlCursors {scDefault, scSystem} kind = case sdlSystemCursor kind of
  Nothing -> pure scDefault
  Just sys -> do
    c <- maybe (create sys) pure . lookup sys =<< readIORef scSystem
    pure (if c == nullPtr then scDefault else c)
  where
    create sys = do
      c <- createSystemCursorSafe sys
      c <$ modifyIORef' scSystem ((sys, c) :)

-- | Resolve the frame's desired pointer cursor and update the active SDL
-- cursor when it changes.
syncPointerCursor :: SdlCursors -> Context -> Input -> IO ()
syncPointerCursor cursors ctx inp = do
  want <- uiCursorKind ctx inp
  when (scTrace cursors) $ do
    cur <- readIORef (scCurrent cursors)
    when (want /= cur) $
      hPutStrLn stderr ("cursor: " ++ show want ++ " at " ++ show (inputMousePos inp))
  showCursorKind cursors want

-- | Show the cursor for @kind@ unless it is the one shown, or hide it for
-- 'UiCursorHidden' until another kind is shown ('syncCursorKind'). If even
-- the default arrow is NULL, SDL_SetCursor(NULL) just redraws the current
-- cursor.
showCursorKind :: SdlCursors -> UiCursorKind -> IO ()
showCursorKind cursors =
  syncCursorKind (scCurrent cursors)
    (\visible -> void (if visible then showCursorSafe else hideCursorSafe))
    (\kind -> void . setCursorSafe =<< cursorFor cursors kind)