{-# LINE 1 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}

{-# LINE 2 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
{-# LANGUAGE Safe #-}

{-# LINE 6 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
-----------------------------------------------------------------------------

-- |

-- Module      :  System.Win32.Console.Internal

-- Copyright   :  (c) University of Glasgow 2023

-- License     :  BSD-style (see the file LICENSE)

--

-- Maintainer  :  Esa Ilari Vuokko <ei@vuokko.info>

-- Stability   :  provisional

-- Portability :  portable

--

-- Internals for Console modules.

--

-----------------------------------------------------------------------------


module System.Win32.Console.Internal where



#include "windows_cconv.h"


import System.Win32.Types
import Graphics.Win32.GDI.Types (COLORREF)

import Foreign.C.Types (CInt(..), CWchar)
import Foreign.C.String (CWString)
import Foreign.Ptr (Ptr, plusPtr)
import Foreign.Storable (Storable(..))
import Foreign.Marshal.Array (peekArray, pokeArray)

foreign import WINDOWS_CCONV unsafe "windows.h GetConsoleMode"
        c_GetConsoleMode :: HANDLE -> LPDWORD -> IO BOOL

foreign import WINDOWS_CCONV unsafe "windows.h SetConsoleMode"
        c_SetConsoleMode :: HANDLE -> DWORD -> IO BOOL

foreign import WINDOWS_CCONV unsafe "windows.h GetConsoleCP"
        getConsoleCP :: IO UINT

foreign import WINDOWS_CCONV unsafe "windows.h SetConsoleCP"
        setConsoleCP :: UINT -> IO ()

foreign import WINDOWS_CCONV unsafe "windows.h GetConsoleOutputCP"
        getConsoleOutputCP :: IO UINT

foreign import WINDOWS_CCONV unsafe "windows.h SetConsoleOutputCP"
        setConsoleOutputCP :: UINT -> IO ()

type CtrlEvent = DWORD
cTRL_C_EVENT       :: CtrlEvent
cTRL_C_EVENT       =  0
cTRL_BREAK_EVENT   :: CtrlEvent
cTRL_BREAK_EVENT   =  1

{-# LINE 59 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}

foreign import WINDOWS_CCONV safe "windows.h GenerateConsoleCtrlEvent"
    c_GenerateConsoleCtrlEvent :: CtrlEvent -> DWORD -> IO BOOL

foreign import WINDOWS_CCONV unsafe "Shellapi.h CommandLineToArgvW"
     c_CommandLineToArgvW :: CWString -> Ptr CInt -> IO (Ptr CWString)

foreign import WINDOWS_CCONV unsafe "processenv.h GetCommandLineW"
        getCommandLineW :: IO LPWSTR

foreign import WINDOWS_CCONV unsafe "processenv.h GetEnvironmentVariableW"
        c_GetEnvironmentVariableW :: LPCWSTR -> LPWSTR -> DWORD -> IO DWORD

foreign import WINDOWS_CCONV unsafe "processenv.h GetEnvironmentStringsW"
        c_GetEnvironmentStringsW :: IO LPWSTR

foreign import WINDOWS_CCONV unsafe "processenv.h FreeEnvironmentStringsW"
  c_FreeEnvironmentStrings :: LPWSTR -> IO Bool

data CONSOLE_SCREEN_BUFFER_INFO = CONSOLE_SCREEN_BUFFER_INFO
    { dwSize              :: COORD
    , dwCursorPosition    :: COORD
    , wAttributes         :: WORD
    , srWindow            :: SMALL_RECT
    , dwMaximumWindowSize :: COORD
    } deriving (Show, Eq)

instance Storable CONSOLE_SCREEN_BUFFER_INFO where
    sizeOf = const (22)
{-# LINE 88 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
    alignment _ = 2
{-# LINE 89 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
    peek buf = do
        dwSize'              <- ((\hsc_ptr -> peekByteOff hsc_ptr 0)) buf
{-# LINE 91 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
        dwCursorPosition'    <- ((\hsc_ptr -> peekByteOff hsc_ptr 4)) buf
{-# LINE 92 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
        wAttributes'         <- ((\hsc_ptr -> peekByteOff hsc_ptr 8)) buf
{-# LINE 93 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
        srWindow'            <- ((\hsc_ptr -> peekByteOff hsc_ptr 10)) buf
{-# LINE 94 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
        dwMaximumWindowSize' <- ((\hsc_ptr -> peekByteOff hsc_ptr 18)) buf
{-# LINE 95 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
        return $ CONSOLE_SCREEN_BUFFER_INFO dwSize' dwCursorPosition' wAttributes' srWindow' dwMaximumWindowSize'
    poke buf info = do
        ((\hsc_ptr -> pokeByteOff hsc_ptr 0)) buf (dwSize info)
{-# LINE 98 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
        ((\hsc_ptr -> pokeByteOff hsc_ptr 4)) buf (dwCursorPosition info)
{-# LINE 99 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
        ((\hsc_ptr -> pokeByteOff hsc_ptr 8)) buf (wAttributes info)
{-# LINE 100 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
        ((\hsc_ptr -> pokeByteOff hsc_ptr 10)) buf (srWindow info)
{-# LINE 101 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
        ((\hsc_ptr -> pokeByteOff hsc_ptr 18)) buf (dwMaximumWindowSize info)
{-# LINE 102 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}

data CONSOLE_SCREEN_BUFFER_INFOEX = CONSOLE_SCREEN_BUFFER_INFOEX
    { dwSizeEx              :: COORD
    , dwCursorPositionEx    :: COORD
    , wAttributesEx         :: WORD
    , srWindowEx            :: SMALL_RECT
    , dwMaximumWindowSizeEx :: COORD
    , wPopupAttributes      :: WORD
    , bFullscreenSupported  :: BOOL
    , colorTable            :: [COLORREF]
      -- ^ Only the first 16 'COLORREF' values passed to the Windows Console

      -- API. If fewer than 16 values, the remainder are padded with @0@ when

      -- passed to the API.

    } deriving (Show, Eq)

instance Storable CONSOLE_SCREEN_BUFFER_INFOEX where
    sizeOf = const (96)
{-# LINE 119 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
    alignment = const 4
{-# LINE 120 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
    peek buf = do
        dwSize'               <- ((\hsc_ptr -> peekByteOff hsc_ptr 4)) buf
{-# LINE 122 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
        dwCursorPosition'     <- ((\hsc_ptr -> peekByteOff hsc_ptr 8)) buf
{-# LINE 123 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
        wAttributes'          <- ((\hsc_ptr -> peekByteOff hsc_ptr 12)) buf
{-# LINE 124 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
        srWindow'             <- ((\hsc_ptr -> peekByteOff hsc_ptr 14)) buf
{-# LINE 125 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
        dwMaximumWindowSize'  <- ((\hsc_ptr -> peekByteOff hsc_ptr 22)) buf
{-# LINE 126 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
        wPopupAttributes'     <- ((\hsc_ptr -> peekByteOff hsc_ptr 26)) buf
{-# LINE 127 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
        bFullscreenSupported' <- ((\hsc_ptr -> peekByteOff hsc_ptr 28)) buf
{-# LINE 128 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
        colorTable'           <- peekArray 16 (((\hsc_ptr -> hsc_ptr `plusPtr` 32)) buf)
{-# LINE 129 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
        return $ CONSOLE_SCREEN_BUFFER_INFOEX dwSize' dwCursorPosition'
          wAttributes' srWindow' dwMaximumWindowSize' wPopupAttributes'
          bFullscreenSupported' colorTable'
    poke buf info = do
        ((\hsc_ptr -> pokeByteOff hsc_ptr 0)) buf cbSize
{-# LINE 134 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
        ((\hsc_ptr -> pokeByteOff hsc_ptr 4)) buf (dwSizeEx info)
{-# LINE 135 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
        ((\hsc_ptr -> pokeByteOff hsc_ptr 8)) buf (dwCursorPositionEx info)
{-# LINE 136 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
        ((\hsc_ptr -> pokeByteOff hsc_ptr 12)) buf (wAttributesEx info)
{-# LINE 137 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
        ((\hsc_ptr -> pokeByteOff hsc_ptr 14)) buf (srWindowEx info)
{-# LINE 138 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
        ((\hsc_ptr -> pokeByteOff hsc_ptr 22)) buf (dwMaximumWindowSizeEx info)
{-# LINE 139 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
        ((\hsc_ptr -> pokeByteOff hsc_ptr 26)) buf (wPopupAttributes info)
{-# LINE 140 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
        ((\hsc_ptr -> pokeByteOff hsc_ptr 28)) buf (bFullscreenSupported info)
{-# LINE 141 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
        pokeArray (((\hsc_ptr -> hsc_ptr `plusPtr` 32)) buf) colorTable'
{-# LINE 142 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
      where
        cbSize :: ULONG
        cbSize = (96)
{-# LINE 145 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
        colorTable' = take 16 $ colorTable info ++ repeat 0

data COORD = COORD
    { xPos :: SHORT
    , yPos :: SHORT
    } deriving (Show, Eq)

instance Storable COORD where
    sizeOf = const (4)
{-# LINE 154 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
    alignment _ = 2
{-# LINE 155 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
    peek buf = do
        x' <- ((\hsc_ptr -> peekByteOff hsc_ptr 0)) buf
{-# LINE 157 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
        y' <- ((\hsc_ptr -> peekByteOff hsc_ptr 2)) buf
{-# LINE 158 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
        return $ COORD x' y'
    poke buf coord = do
        ((\hsc_ptr -> pokeByteOff hsc_ptr 0)) buf (xPos coord)
{-# LINE 161 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
        ((\hsc_ptr -> pokeByteOff hsc_ptr 2)) buf (yPos coord)
{-# LINE 162 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}

data SMALL_RECT = SMALL_RECT
    { leftPos   :: SHORT
    , topPos    :: SHORT
    , rightPos  :: SHORT
    , bottomPos :: SHORT
    } deriving (Show, Eq)

instance Storable SMALL_RECT where
    sizeOf _ = (8)
{-# LINE 172 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
    alignment _ = 2
{-# LINE 173 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
    peek buf = do
        left'   <- ((\hsc_ptr -> peekByteOff hsc_ptr 0)) buf
{-# LINE 175 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
        top'    <- ((\hsc_ptr -> peekByteOff hsc_ptr 2)) buf
{-# LINE 176 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
        right'  <- ((\hsc_ptr -> peekByteOff hsc_ptr 4)) buf
{-# LINE 177 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
        bottom' <- ((\hsc_ptr -> peekByteOff hsc_ptr 6)) buf
{-# LINE 178 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
        return $ SMALL_RECT left' top' right' bottom'
    poke buf small_rect = do
        ((\hsc_ptr -> pokeByteOff hsc_ptr 0)) buf (leftPos small_rect)
{-# LINE 181 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
        ((\hsc_ptr -> pokeByteOff hsc_ptr 2)) buf (topPos small_rect)
{-# LINE 182 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
        ((\hsc_ptr -> pokeByteOff hsc_ptr 4)) buf (rightPos small_rect)
{-# LINE 183 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
        ((\hsc_ptr -> pokeByteOff hsc_ptr 6)) buf (bottomPos small_rect)
{-# LINE 184 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}

foreign import WINDOWS_CCONV safe "windows.h GetConsoleScreenBufferInfo"
    c_GetConsoleScreenBufferInfo :: HANDLE -> Ptr CONSOLE_SCREEN_BUFFER_INFO -> IO BOOL

foreign import WINDOWS_CCONV safe "windows.h GetConsoleScreenBufferInfoEx"
    c_GetConsoleScreenBufferInfoEx :: HANDLE -> Ptr CONSOLE_SCREEN_BUFFER_INFOEX -> IO BOOL

-- | This type represents a keyboard input event. The structure is documented here:

-- https://learn.microsoft.com/en-us/windows/console/key-event-record-str

data KEY_EVENT_RECORD = KEY_EVENT_RECORD
    { keyDown          :: BOOL
    , repeatCount      :: WORD
    , virtualKeyCode   :: WORD
    , virtualScanCode  :: WORD
    , uChar            :: CWchar
    , controlKeyStateK :: DWORD
    } deriving (Eq, Show)

-- | This type represents a mouse event. The structure is documented here:

-- https://learn.microsoft.com/en-us/windows/console/mouse-event-record-str

data MOUSE_EVENT_RECORD = MOUSE_EVENT_RECORD
  { mousePosition    :: COORD
  , buttonState      :: DWORD
  , controlKeyStateM :: DWORD
  , eventFlags       :: DWORD
  } deriving (Eq, Show)

-- | This type represents a window size change event. The structure is documented here:

-- https://learn.microsoft.com/en-us/windows/console/window-buffer-size-record-str

newtype WINDOW_BUFFER_SIZE_RECORD = WINDOW_BUFFER_SIZE_RECORD
  { windowSize :: COORD
  } deriving (Eq, Show)

-- | This type represents a window menu event. (Current ignored by VTY). The structure

-- is documented here: https://learn.microsoft.com/en-us/windows/console/menu-event-record-str

newtype MENU_EVENT_RECORD = MENU_EVENT_RECORD
  { commandId :: UINT
  } deriving (Eq, Show)

-- | This type represents a window focus change event. The structure is documented here:

-- https://learn.microsoft.com/en-us/windows/console/focus-event-record-str

newtype FOCUS_EVENT_RECORD = FOCUS_EVENT_RECORD
  { setFocus :: BOOL
  } deriving (Eq, Show)

-- | Description of a Windows console input event. Documented here:

-- https://learn.microsoft.com/en-us/windows/console/input-record-str

data INPUT_RECORD =
    KeyEvent KEY_EVENT_RECORD
  | MouseEvent MOUSE_EVENT_RECORD
  | WindowBufferSizeEvent WINDOW_BUFFER_SIZE_RECORD
  | MenuEvent MENU_EVENT_RECORD
  | FocusEvent FOCUS_EVENT_RECORD
  deriving (Eq, Show)

instance Storable KEY_EVENT_RECORD where
    sizeOf = const (16)
{-# LINE 241 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
    alignment _ = 4
{-# LINE 242 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
    poke buf input = do
        ((\hsc_ptr -> pokeByteOff hsc_ptr 0))          buf (keyDown input)
{-# LINE 244 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
        ((\hsc_ptr -> pokeByteOff hsc_ptr 4))      buf (repeatCount input)
{-# LINE 245 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
        ((\hsc_ptr -> pokeByteOff hsc_ptr 6))   buf (virtualKeyCode input)
{-# LINE 246 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
        ((\hsc_ptr -> pokeByteOff hsc_ptr 8))  buf (virtualScanCode input)
{-# LINE 247 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
        ((\hsc_ptr -> pokeByteOff hsc_ptr 10))             buf (uChar input)
{-# LINE 248 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
        ((\hsc_ptr -> pokeByteOff hsc_ptr 12)) buf (controlKeyStateK input)
{-# LINE 249 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
    peek buf = do
        keyDown'          <- ((\hsc_ptr -> peekByteOff hsc_ptr 0)) buf
{-# LINE 251 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
        repeatCount'      <- ((\hsc_ptr -> peekByteOff hsc_ptr 4)) buf
{-# LINE 252 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
        virtualKeyCode'   <- ((\hsc_ptr -> peekByteOff hsc_ptr 6)) buf
{-# LINE 253 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
        virtualScanCode'  <- ((\hsc_ptr -> peekByteOff hsc_ptr 8)) buf
{-# LINE 254 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
        uChar'            <- ((\hsc_ptr -> peekByteOff hsc_ptr 10)) buf
{-# LINE 255 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
        controlKeyStateK' <- ((\hsc_ptr -> peekByteOff hsc_ptr 12)) buf
{-# LINE 256 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
        return $ KEY_EVENT_RECORD keyDown' repeatCount' virtualKeyCode' virtualScanCode' uChar' controlKeyStateK'

instance Storable MOUSE_EVENT_RECORD where
    sizeOf = const (16)
{-# LINE 260 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
    alignment _ = 4
{-# LINE 261 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
    poke buf input = do
        ((\hsc_ptr -> pokeByteOff hsc_ptr 0))   buf (mousePosition input)
{-# LINE 263 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
        ((\hsc_ptr -> pokeByteOff hsc_ptr 4))     buf (buttonState input)
{-# LINE 264 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
        ((\hsc_ptr -> pokeByteOff hsc_ptr 8)) buf (controlKeyStateM input)
{-# LINE 265 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
        ((\hsc_ptr -> pokeByteOff hsc_ptr 12))      buf (eventFlags input)
{-# LINE 266 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
    peek buf = do
        mousePosition'    <- ((\hsc_ptr -> peekByteOff hsc_ptr 0)) buf
{-# LINE 268 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
        buttonState'      <- ((\hsc_ptr -> peekByteOff hsc_ptr 4)) buf
{-# LINE 269 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
        controlKeyStateM' <- ((\hsc_ptr -> peekByteOff hsc_ptr 8)) buf
{-# LINE 270 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
        eventFlags'       <- ((\hsc_ptr -> peekByteOff hsc_ptr 12)) buf
{-# LINE 271 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
        return $ MOUSE_EVENT_RECORD mousePosition' buttonState' controlKeyStateM' eventFlags'

instance Storable WINDOW_BUFFER_SIZE_RECORD where
    sizeOf = const (4)
{-# LINE 275 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
    alignment _ = 2
{-# LINE 276 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
    poke buf input = do
        ((\hsc_ptr -> pokeByteOff hsc_ptr 0)) buf (windowSize input)
{-# LINE 278 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
    peek buf = do
        size' <- ((\hsc_ptr -> peekByteOff hsc_ptr 0)) buf
{-# LINE 280 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
        return $ WINDOW_BUFFER_SIZE_RECORD size'

instance Storable MENU_EVENT_RECORD where
    sizeOf = const (4)
{-# LINE 284 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
    alignment _ = 4
{-# LINE 285 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
    poke buf input = do
        ((\hsc_ptr -> pokeByteOff hsc_ptr 0)) buf (commandId input)
{-# LINE 287 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
    peek buf = do
        commandId' <- ((\hsc_ptr -> peekByteOff hsc_ptr 0)) buf
{-# LINE 289 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
        return $ MENU_EVENT_RECORD commandId'

instance Storable FOCUS_EVENT_RECORD where
    sizeOf = const (4)
{-# LINE 293 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
    alignment _ = 4
{-# LINE 294 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
    poke buf input = do
        ((\hsc_ptr -> pokeByteOff hsc_ptr 0)) buf (setFocus input)
{-# LINE 296 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
    peek buf = do
        setFocus' <- ((\hsc_ptr -> peekByteOff hsc_ptr 0)) buf
{-# LINE 298 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
        return $ FOCUS_EVENT_RECORD setFocus'

instance Storable INPUT_RECORD where
    sizeOf = const (20)
{-# LINE 302 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
    alignment _ = 4
{-# LINE 303 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}

    poke buf (KeyEvent key) = do
        ((\hsc_ptr -> pokeByteOff hsc_ptr 0)) buf (1 :: WORD)
{-# LINE 306 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
        ((\hsc_ptr -> pokeByteOff hsc_ptr 4)) buf key
{-# LINE 307 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
    poke buf (MouseEvent mouse) = do
        ((\hsc_ptr -> pokeByteOff hsc_ptr 0)) buf (2 :: WORD)
{-# LINE 309 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
        ((\hsc_ptr -> pokeByteOff hsc_ptr 4)) buf mouse
{-# LINE 310 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
    poke buf (WindowBufferSizeEvent window) = do
        ((\hsc_ptr -> pokeByteOff hsc_ptr 0)) buf (4 :: WORD)
{-# LINE 312 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
        ((\hsc_ptr -> pokeByteOff hsc_ptr 4)) buf window
{-# LINE 313 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
    poke buf (MenuEvent menu) = do
        ((\hsc_ptr -> pokeByteOff hsc_ptr 0)) buf (8 :: WORD)
{-# LINE 315 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
        ((\hsc_ptr -> pokeByteOff hsc_ptr 4)) buf menu
{-# LINE 316 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
    poke buf (FocusEvent focus) = do
        ((\hsc_ptr -> pokeByteOff hsc_ptr 0)) buf (16 :: WORD)
{-# LINE 318 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
        ((\hsc_ptr -> pokeByteOff hsc_ptr 4)) buf focus
{-# LINE 319 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}

    peek buf = do
        event <- ((\hsc_ptr -> peekByteOff hsc_ptr 0)) buf :: IO WORD
{-# LINE 322 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
        case event of
          1 ->
{-# LINE 324 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
              KeyEvent `fmap` ((\hsc_ptr -> peekByteOff hsc_ptr 4)) buf
{-# LINE 325 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
          2 ->
{-# LINE 326 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
              MouseEvent `fmap` ((\hsc_ptr -> peekByteOff hsc_ptr 4)) buf
{-# LINE 327 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
          4 ->
{-# LINE 328 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
              WindowBufferSizeEvent `fmap` ((\hsc_ptr -> peekByteOff hsc_ptr 4)) buf
{-# LINE 329 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
          8 ->
{-# LINE 330 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
              MenuEvent `fmap` ((\hsc_ptr -> peekByteOff hsc_ptr 4)) buf
{-# LINE 331 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
          16 ->
{-# LINE 332 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
              FocusEvent `fmap` ((\hsc_ptr -> peekByteOff hsc_ptr 4)) buf
{-# LINE 333 "libraries\\Win32\\System\\Win32\\Console\\Internal.hsc" #-}
          _ -> error $ "Unknown input event type " ++ show event

foreign import WINDOWS_CCONV unsafe "windows.h ReadConsoleInputW"
    c_ReadConsoleInput :: HANDLE -> Ptr INPUT_RECORD -> DWORD -> LPDWORD -> IO BOOL