Class WinConsoleNative

java.lang.Object
org.aesh.terminal.tty.impl.WinConsoleNative

public final class WinConsoleNative extends Object
JNI bridge to Windows console API (Kernel32). Replaces JNA for GraalVM native-image compatibility.

This class provides low-level access to Windows console functions via JNI. The native library (aesh-console.dll) is loaded from the system library path or extracted from the JAR at runtime.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    Console mode flag: enable virtual terminal processing on output handle.
    static final long
    Invalid handle sentinel value.
    static final int
    Event type constants matching Windows INPUT_RECORD.EventType.
    static final int
    Mouse event type.
    static final int
    Standard error handle identifier.
    static final int
    Standard input handle identifier.
    static final int
    Standard output handle identifier.
    static final int
    WaitForSingleObject return: the function failed.
    static final int
    WaitForSingleObject return: the object was signaled.
    static final int
    WaitForSingleObject return: the wait timed out.
    static final int
    Window buffer size event type.
  • Method Summary

    Modifier and Type
    Method
    Description
    static boolean
    Allocates a new console for the calling process.
    static boolean
    Detaches the calling process from its console.
    static int
    getConsoleMode(long handle)
    Returns the current console mode for the given handle.
    static int
    Returns the console output code page.
    static int[]
    getConsoleSize(long handle)
    Returns the console size as {columns, rows}.
    static int
    Returns the number of unread console input events.
    static long
    getStdHandle(int nStdHandle)
    Returns the handle for the specified standard device.
    static int[]
    readConsoleInputEvent(long handle)
    Read a console input event (key or window resize).
    static boolean
    setConsoleMode(long handle, int mode)
    Sets the console mode for the given handle.
    static int
    waitForSingleObject(long handle, int timeoutMs)
    Waits for the specified object to be signaled or the timeout to elapse.
    static boolean
    writeConsole(long handle, char[] buffer, int length)
    Writes characters to the console.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • STD_INPUT_HANDLE

      public static final int STD_INPUT_HANDLE
      Standard input handle identifier.
      See Also:
    • STD_OUTPUT_HANDLE

      public static final int STD_OUTPUT_HANDLE
      Standard output handle identifier.
      See Also:
    • STD_ERROR_HANDLE

      public static final int STD_ERROR_HANDLE
      Standard error handle identifier.
      See Also:
    • INVALID_HANDLE

      public static final long INVALID_HANDLE
      Invalid handle sentinel value.
      See Also:
    • KEY_EVENT

      public static final int KEY_EVENT
      Event type constants matching Windows INPUT_RECORD.EventType.
      See Also:
    • MOUSE_EVENT

      public static final int MOUSE_EVENT
      Mouse event type.
      See Also:
    • WINDOW_BUFFER_SIZE_EVENT

      public static final int WINDOW_BUFFER_SIZE_EVENT
      Window buffer size event type.
      See Also:
    • ENABLE_VIRTUAL_TERMINAL_PROCESSING

      public static final int ENABLE_VIRTUAL_TERMINAL_PROCESSING
      Console mode flag: enable virtual terminal processing on output handle.
      See Also:
    • WAIT_OBJECT_0

      public static final int WAIT_OBJECT_0
      WaitForSingleObject return: the object was signaled.
      See Also:
    • WAIT_TIMEOUT

      public static final int WAIT_TIMEOUT
      WaitForSingleObject return: the wait timed out.
      See Also:
    • WAIT_FAILED

      public static final int WAIT_FAILED
      WaitForSingleObject return: the function failed.
      See Also:
  • Method Details

    • getStdHandle

      public static long getStdHandle(int nStdHandle)
      Returns the handle for the specified standard device.
      Parameters:
      nStdHandle - the standard device identifier
      Returns:
      the device handle
    • getConsoleMode

      public static int getConsoleMode(long handle)
      Returns the current console mode for the given handle.
      Parameters:
      handle - the console handle
      Returns:
      the console mode flags
    • setConsoleMode

      public static boolean setConsoleMode(long handle, int mode)
      Sets the console mode for the given handle.
      Parameters:
      handle - the console handle
      mode - the console mode flags
      Returns:
      true if successful
    • getConsoleOutputCP

      public static int getConsoleOutputCP()
      Returns the console output code page.
      Returns:
      the output code page identifier, or -1 on error/non-Windows
    • getConsoleSize

      public static int[] getConsoleSize(long handle)
      Returns the console size as {columns, rows}.
      Parameters:
      handle - the console handle
      Returns:
      array of {columns, rows}
    • readConsoleInputEvent

      public static int[] readConsoleInputEvent(long handle)
      Read a console input event (key or window resize). Returns int[] where first element is the event type: KEY_EVENT (1): {1, keyDown, repeatCount, vKeyCode, unicodeChar, controlKeyState} MOUSE_EVENT (2): {2, x, y, buttonState, controlKeyState, eventFlags} WINDOW_BUFFER_SIZE_EVENT (4): {4, width, height} Returns null for other event types or on error.
      Parameters:
      handle - the console input handle
      Returns:
      the event data array, or null
    • writeConsole

      public static boolean writeConsole(long handle, char[] buffer, int length)
      Writes characters to the console.
      Parameters:
      handle - the console output handle
      buffer - the characters to write
      length - the number of characters to write (must be <= buffer.length)
      Returns:
      true if all characters were written successfully
    • waitForSingleObject

      public static int waitForSingleObject(long handle, int timeoutMs)
      Waits for the specified object to be signaled or the timeout to elapse.
      Parameters:
      handle - the object handle (e.g., console input handle)
      timeoutMs - timeout in milliseconds; -1 (0xFFFFFFFF) for infinite
      Returns:
      WAIT_OBJECT_0 if signaled, WAIT_TIMEOUT if timed out, or WAIT_FAILED on error
    • getNumberOfConsoleInputEvents

      public static int getNumberOfConsoleInputEvents(long handle)
      Returns the number of unread console input events.
      Parameters:
      handle - the console input handle
      Returns:
      the number of pending events, or -1 on error
    • allocConsole

      public static boolean allocConsole()
      Allocates a new console for the calling process.

      Test-only surface for live-console CI tests (#290): headless CI runners have no console, so tests allocate one to drive WinSysTerminal against a true handle. Fails (returns false) when the process already has a console — use freeConsole() only for consoles this method allocated.

      Returns:
      true if a console was allocated, false otherwise
    • freeConsole

      public static boolean freeConsole()
      Detaches the calling process from its console.

      Test-only companion to allocConsole(): call only to release a console allocated by allocConsole(). Detaching an interactive console the process did not allocate discards its output.

      Returns:
      true if detached, false otherwise