Moved JNA to consolidate in utils

This commit is contained in:
nathan 2017-07-11 22:31:51 +02:00
parent 0f66d0ce1c
commit 1960beb699
17 changed files with 102 additions and 924 deletions

View File

@ -32,6 +32,7 @@
<orderEntry type="module" module-name="ObjectPool" />
<orderEntry type="library" name="logging slf4j-api" level="application" />
<orderEntry type="library" name="jna" level="application" />
<orderEntry type="library" name="jna platform" level="application" />
<orderEntry type="library" name="junit-4.12" level="application" />
</component>
</module>

View File

@ -29,6 +29,11 @@
Copyright 2002-2012, the original author or authors
- JNA - Apache 2.0 License
https://github.com/twall/jna
Copyright 2011, Timothy Wall
- SLF4J - MIT License
http://www.slf4j.org
Copyright 2004-2008, QOS.ch

View File

@ -20,8 +20,8 @@ import java.nio.ByteBuffer;
import com.sun.jna.ptr.IntByReference;
import dorkbox.console.util.posix.CLibraryPosix;
import dorkbox.console.util.posix.Termios;
import dorkbox.util.jna.linux.CLibraryPosix;
import dorkbox.util.jna.linux.structs.Termios;
/**
* Terminal that is used for unix platforms. Terminal initialization is handled via JNA and ioctl/tcgetattr/tcsetattr/cfmakeraw.
@ -42,12 +42,14 @@ class PosixTerminal extends SupportedTerminal {
if (CLibraryPosix.tcgetattr(0, this.original) != 0) {
throw new IOException(CONSOLE_ERROR_INIT);
}
this.original.read();
// CTRL-I (tab), CTRL-M (enter) do not work
if (CLibraryPosix.tcgetattr(0, this.termInfo) != 0) {
throw new IOException(CONSOLE_ERROR_INIT);
}
this.termInfo.read();
this.termInfo.inputFlags &= ~Termios.Input.IXON; // DISABLE - output flow control mediated by ^S and ^Q
this.termInfo.inputFlags &= ~Termios.Input.IXOFF; // DISABLE - input flow control mediated by ^S and ^Q

View File

@ -11,25 +11,27 @@
*/
package dorkbox.console.input;
import static dorkbox.console.util.windows.Kernel32.ASSERT;
import static dorkbox.console.util.windows.Kernel32.CloseHandle;
import static dorkbox.console.util.windows.Kernel32.GetConsoleMode;
import static dorkbox.console.util.windows.Kernel32.GetConsoleScreenBufferInfo;
import static dorkbox.console.util.windows.Kernel32.GetStdHandle;
import static dorkbox.console.util.windows.Kernel32.STD_INPUT_HANDLE;
import static dorkbox.console.util.windows.Kernel32.STD_OUTPUT_HANDLE;
import static dorkbox.console.util.windows.Kernel32.SetConsoleMode;
import static com.sun.jna.platform.win32.WinBase.INVALID_HANDLE_VALUE;
import static com.sun.jna.platform.win32.WinNT.HANDLE;
import static com.sun.jna.platform.win32.Wincon.STD_INPUT_HANDLE;
import static com.sun.jna.platform.win32.Wincon.STD_OUTPUT_HANDLE;
import static dorkbox.util.jna.windows.Kernel32.ASSERT;
import static dorkbox.util.jna.windows.Kernel32.CloseHandle;
import static dorkbox.util.jna.windows.Kernel32.GetConsoleMode;
import static dorkbox.util.jna.windows.Kernel32.GetConsoleScreenBufferInfo;
import static dorkbox.util.jna.windows.Kernel32.GetStdHandle;
import static dorkbox.util.jna.windows.Kernel32.ReadConsoleInput;
import static dorkbox.util.jna.windows.Kernel32.SetConsoleMode;
import java.io.IOException;
import java.io.PrintStream;
import com.sun.jna.ptr.IntByReference;
import dorkbox.console.util.windows.CONSOLE_SCREEN_BUFFER_INFO;
import dorkbox.console.util.windows.HANDLE;
import dorkbox.console.util.windows.INPUT_RECORD;
import dorkbox.console.util.windows.KEY_EVENT_RECORD;
import dorkbox.console.util.windows.Kernel32;
import dorkbox.util.jna.windows.structs.CONSOLE_SCREEN_BUFFER_INFO;
import dorkbox.util.jna.windows.structs.INPUT_RECORD;
import dorkbox.util.jna.windows.structs.KEY_EVENT_RECORD;
/**
* Terminal implementation for Microsoft Windows.
@ -62,12 +64,12 @@ class WindowsTerminal extends SupportedTerminal {
public
WindowsTerminal() throws IOException {
console = GetStdHandle(STD_INPUT_HANDLE);
if (console == HANDLE.INVALID_HANDLE_VALUE) {
if (console == INVALID_HANDLE_VALUE) {
throw new IOException("Unable to get input console handle.");
}
outputConsole = GetStdHandle(STD_OUTPUT_HANDLE);
if (outputConsole == HANDLE.INVALID_HANDLE_VALUE) {
if (outputConsole == INVALID_HANDLE_VALUE) {
throw new IOException("Unable to get output console handle.");
}
@ -162,7 +164,7 @@ class WindowsTerminal extends SupportedTerminal {
// keep reading input events until we find one that we are interested in (ie: keyboard input)
while (true) {
// blocks until there is (at least) 1 event on the buffer
Kernel32.ReadConsoleInputW(console, inputRecords, 1, reference);
ReadConsoleInput(console, inputRecords, 1, reference);
for (int i = 0; i < reference.getValue(); ++i) {
if (inputRecords.EventType == INPUT_RECORD.KEY_EVENT) {

View File

@ -39,9 +39,9 @@ import java.io.PrintStream;
import java.util.ArrayList;
import dorkbox.console.Console;
import dorkbox.console.util.posix.CLibraryPosix;
import dorkbox.console.util.windows.Kernel32;
import dorkbox.util.OS;
import dorkbox.util.OSUtil;
import dorkbox.util.jna.linux.CLibraryPosix;
/**
* Provides a fluent API for generating ANSI escape sequences and providing access to streams that support it.
@ -943,7 +943,7 @@ class Ansi {
if (!isXterm()) {
if (OS.isWindows()) {
// check if windows10+ (which natively supports ANSI)
if (Kernel32.isWindows10OrGreater()) {
if (OSUtil.Windows.isWindows10_plus()) {
// Just wrap it up so that when we get closed, we reset the attributes.
return defaultPrintStream(stream, type);
}

View File

@ -31,23 +31,46 @@
*/
package dorkbox.console.output;
import static dorkbox.console.util.windows.Kernel32.ASSERT;
import static dorkbox.console.util.windows.Kernel32.FillConsoleOutputAttribute;
import static dorkbox.console.util.windows.Kernel32.FillConsoleOutputCharacterW;
import static dorkbox.console.util.windows.Kernel32.GetConsoleScreenBufferInfo;
import static dorkbox.console.util.windows.Kernel32.SetConsoleCursorPosition;
import static dorkbox.console.util.windows.Kernel32.SetConsoleTextAttribute;
import static com.sun.jna.platform.win32.WinBase.INVALID_HANDLE_VALUE;
import static com.sun.jna.platform.win32.WinNT.HANDLE;
import static com.sun.jna.platform.win32.Wincon.STD_ERROR_HANDLE;
import static com.sun.jna.platform.win32.Wincon.STD_OUTPUT_HANDLE;
import static dorkbox.util.jna.windows.Kernel32.ASSERT;
import static dorkbox.util.jna.windows.Kernel32.BACKGROUND_BLACK;
import static dorkbox.util.jna.windows.Kernel32.BACKGROUND_BLUE;
import static dorkbox.util.jna.windows.Kernel32.BACKGROUND_CYAN;
import static dorkbox.util.jna.windows.Kernel32.BACKGROUND_GREEN;
import static dorkbox.util.jna.windows.Kernel32.BACKGROUND_GREY;
import static dorkbox.util.jna.windows.Kernel32.BACKGROUND_INTENSITY;
import static dorkbox.util.jna.windows.Kernel32.BACKGROUND_MAGENTA;
import static dorkbox.util.jna.windows.Kernel32.BACKGROUND_RED;
import static dorkbox.util.jna.windows.Kernel32.BACKGROUND_YELLOW;
import static dorkbox.util.jna.windows.Kernel32.CloseHandle;
import static dorkbox.util.jna.windows.Kernel32.FOREGROUND_BLACK;
import static dorkbox.util.jna.windows.Kernel32.FOREGROUND_BLUE;
import static dorkbox.util.jna.windows.Kernel32.FOREGROUND_CYAN;
import static dorkbox.util.jna.windows.Kernel32.FOREGROUND_GREEN;
import static dorkbox.util.jna.windows.Kernel32.FOREGROUND_GREY;
import static dorkbox.util.jna.windows.Kernel32.FOREGROUND_INTENSITY;
import static dorkbox.util.jna.windows.Kernel32.FOREGROUND_MAGENTA;
import static dorkbox.util.jna.windows.Kernel32.FOREGROUND_RED;
import static dorkbox.util.jna.windows.Kernel32.FOREGROUND_YELLOW;
import static dorkbox.util.jna.windows.Kernel32.FillConsoleOutputAttribute;
import static dorkbox.util.jna.windows.Kernel32.FillConsoleOutputCharacter;
import static dorkbox.util.jna.windows.Kernel32.GetConsoleScreenBufferInfo;
import static dorkbox.util.jna.windows.Kernel32.GetStdHandle;
import static dorkbox.util.jna.windows.Kernel32.ScrollConsoleScreenBuffer;
import static dorkbox.util.jna.windows.Kernel32.SetConsoleCursorPosition;
import static dorkbox.util.jna.windows.Kernel32.SetConsoleTextAttribute;
import java.io.IOException;
import java.io.OutputStream;
import com.sun.jna.ptr.IntByReference;
import dorkbox.console.util.windows.CONSOLE_SCREEN_BUFFER_INFO;
import dorkbox.console.util.windows.COORD;
import dorkbox.console.util.windows.HANDLE;
import dorkbox.console.util.windows.Kernel32;
import dorkbox.console.util.windows.SMALL_RECT;
import dorkbox.util.jna.windows.structs.CONSOLE_SCREEN_BUFFER_INFO;
import dorkbox.util.jna.windows.structs.COORD;
import dorkbox.util.jna.windows.structs.SMALL_RECT;
/**
* A Windows ANSI escape processor, uses JNA direct-mapping to access native platform API's to change the console attributes.
@ -65,24 +88,24 @@ public final class WindowsAnsiOutputStream extends AnsiOutputStream {
static {
ANSI_FOREGROUND_COLOR_MAP = new short[8];
ANSI_FOREGROUND_COLOR_MAP[BLACK] = Kernel32.FOREGROUND_BLACK;
ANSI_FOREGROUND_COLOR_MAP[RED] = Kernel32.FOREGROUND_RED;
ANSI_FOREGROUND_COLOR_MAP[GREEN] = Kernel32.FOREGROUND_GREEN;
ANSI_FOREGROUND_COLOR_MAP[YELLOW] = Kernel32.FOREGROUND_YELLOW;
ANSI_FOREGROUND_COLOR_MAP[BLUE] = Kernel32.FOREGROUND_BLUE;
ANSI_FOREGROUND_COLOR_MAP[MAGENTA] = Kernel32.FOREGROUND_MAGENTA;
ANSI_FOREGROUND_COLOR_MAP[CYAN] = Kernel32.FOREGROUND_CYAN;
ANSI_FOREGROUND_COLOR_MAP[WHITE] = Kernel32.FOREGROUND_GREY;
ANSI_FOREGROUND_COLOR_MAP[BLACK] = FOREGROUND_BLACK;
ANSI_FOREGROUND_COLOR_MAP[RED] = FOREGROUND_RED;
ANSI_FOREGROUND_COLOR_MAP[GREEN] = FOREGROUND_GREEN;
ANSI_FOREGROUND_COLOR_MAP[YELLOW] = FOREGROUND_YELLOW;
ANSI_FOREGROUND_COLOR_MAP[BLUE] = FOREGROUND_BLUE;
ANSI_FOREGROUND_COLOR_MAP[MAGENTA] = FOREGROUND_MAGENTA;
ANSI_FOREGROUND_COLOR_MAP[CYAN] = FOREGROUND_CYAN;
ANSI_FOREGROUND_COLOR_MAP[WHITE] = FOREGROUND_GREY;
ANSI_BACKGROUND_COLOR_MAP = new short[8];
ANSI_BACKGROUND_COLOR_MAP[BLACK] = Kernel32.BACKGROUND_BLACK;
ANSI_BACKGROUND_COLOR_MAP[RED] = Kernel32.BACKGROUND_RED;
ANSI_BACKGROUND_COLOR_MAP[GREEN] = Kernel32.BACKGROUND_GREEN;
ANSI_BACKGROUND_COLOR_MAP[YELLOW] = Kernel32.BACKGROUND_YELLOW;
ANSI_BACKGROUND_COLOR_MAP[BLUE] = Kernel32.BACKGROUND_BLUE;
ANSI_BACKGROUND_COLOR_MAP[MAGENTA] = Kernel32.BACKGROUND_MAGENTA;
ANSI_BACKGROUND_COLOR_MAP[CYAN] = Kernel32.BACKGROUND_CYAN;
ANSI_BACKGROUND_COLOR_MAP[WHITE] = Kernel32.BACKGROUND_GREY;
ANSI_BACKGROUND_COLOR_MAP[BLACK] = BACKGROUND_BLACK;
ANSI_BACKGROUND_COLOR_MAP[RED] = BACKGROUND_RED;
ANSI_BACKGROUND_COLOR_MAP[GREEN] = BACKGROUND_GREEN;
ANSI_BACKGROUND_COLOR_MAP[YELLOW] = BACKGROUND_YELLOW;
ANSI_BACKGROUND_COLOR_MAP[BLUE] = BACKGROUND_BLUE;
ANSI_BACKGROUND_COLOR_MAP[MAGENTA] = BACKGROUND_MAGENTA;
ANSI_BACKGROUND_COLOR_MAP[CYAN] = BACKGROUND_CYAN;
ANSI_BACKGROUND_COLOR_MAP[WHITE] = BACKGROUND_GREY;
}
private final HANDLE console;
@ -96,20 +119,19 @@ public final class WindowsAnsiOutputStream extends AnsiOutputStream {
// reused vars
private IntByReference written = new IntByReference();
public
WindowsAnsiOutputStream(final OutputStream os, int fileHandle) throws IOException {
super(os);
if (fileHandle == 1) { // STDOUT_FILENO
fileHandle = Kernel32.STD_OUTPUT_HANDLE;
fileHandle = STD_OUTPUT_HANDLE;
} else if (fileHandle == 2) { // STDERR_FILENO
fileHandle = Kernel32.STD_ERROR_HANDLE;
fileHandle = STD_ERROR_HANDLE;
} else {
throw new IllegalArgumentException("Invalid file handle " + fileHandle);
}
console = Kernel32.GetStdHandle(fileHandle);
if (console == HANDLE.INVALID_HANDLE_VALUE) {
console = GetStdHandle(fileHandle);
if (console == INVALID_HANDLE_VALUE) {
throw new IOException("Unable to get input console handle.");
}
@ -180,7 +202,7 @@ public final class WindowsAnsiOutputStream extends AnsiOutputStream {
int screenLength = info.window.height() * info.size.x;
ASSERT(FillConsoleOutputAttribute(console, originalInfo.attributes, screenLength, topLeft.asValue(), written), "Could not fill console");
ASSERT(FillConsoleOutputCharacterW(console, ' ', screenLength, topLeft.asValue(), written), "Could not fill console");
ASSERT(FillConsoleOutputCharacter(console, ' ', screenLength, topLeft.asValue(), written), "Could not fill console");
break;
case ERASE_TO_BEGINNING:
COORD topLeft2 = new COORD();
@ -189,13 +211,13 @@ public final class WindowsAnsiOutputStream extends AnsiOutputStream {
int lengthToCursor = (info.cursorPosition.y - info.window.top) * info.size.x + info.cursorPosition.x;
ASSERT(FillConsoleOutputAttribute(console, originalInfo.attributes, lengthToCursor, topLeft2.asValue(), written), "Could not fill console");
ASSERT(FillConsoleOutputCharacterW(console, ' ', lengthToCursor, topLeft2.asValue(), written), "Could not fill console");
ASSERT(FillConsoleOutputCharacter(console, ' ', lengthToCursor, topLeft2.asValue(), written), "Could not fill console");
break;
case ERASE_TO_END:
int lengthToEnd = (info.window.bottom - info.cursorPosition.y) * info.size.x + info.size.x - info.cursorPosition.x;
ASSERT(FillConsoleOutputAttribute(console, originalInfo.attributes, lengthToEnd, info.cursorPosition.asValue(), written), "Could not fill console");
ASSERT(FillConsoleOutputCharacterW(console, ' ', lengthToEnd, info.cursorPosition.asValue(), written), "Could not fill console");
ASSERT(FillConsoleOutputCharacter(console, ' ', lengthToEnd, info.cursorPosition.asValue(), written), "Could not fill console");
}
}
@ -210,20 +232,20 @@ public final class WindowsAnsiOutputStream extends AnsiOutputStream {
currentRow.x = (short) 0;
ASSERT(FillConsoleOutputAttribute(console, originalInfo.attributes, info.size.x, currentRow.asValue(), written), "Could not fill console");
ASSERT(FillConsoleOutputCharacterW(console, ' ', info.size.x, currentRow.asValue(), written), "Could not fill console");
ASSERT(FillConsoleOutputCharacter(console, ' ', info.size.x, currentRow.asValue(), written), "Could not fill console");
break;
case ERASE_TO_BEGINNING:
COORD leftColCurrRow2 = info.cursorPosition.asValue();
leftColCurrRow2.x = (short) 0;
ASSERT(FillConsoleOutputAttribute(console, originalInfo.attributes, info.cursorPosition.x, leftColCurrRow2.asValue(), written), "Could not fill console");
ASSERT(FillConsoleOutputCharacterW(console, ' ', info.cursorPosition.x, leftColCurrRow2.asValue(), written), "Could not fill console");
ASSERT(FillConsoleOutputCharacter(console, ' ', info.cursorPosition.x, leftColCurrRow2.asValue(), written), "Could not fill console");
break;
case ERASE_TO_END:
int lengthToLastCol = info.size.x - info.cursorPosition.x;
ASSERT(FillConsoleOutputAttribute(console, originalInfo.attributes, lengthToLastCol, info.cursorPosition.asValue(), written), "Could not fill console");
ASSERT(FillConsoleOutputCharacterW(console, ' ', lengthToLastCol, info.cursorPosition.asValue(), written), "Could not fill console");
ASSERT(FillConsoleOutputCharacter(console, ' ', lengthToLastCol, info.cursorPosition.asValue(), written), "Could not fill console");
}
}
@ -233,34 +255,34 @@ public final class WindowsAnsiOutputStream extends AnsiOutputStream {
if (90 <= attribute && attribute <= 97) {
// foreground bright
info.attributes = (short) (info.attributes & ~0x000F | ANSI_FOREGROUND_COLOR_MAP[attribute - 90]);
info.attributes = (short) (info.attributes | Kernel32.FOREGROUND_INTENSITY);
info.attributes = (short) (info.attributes | FOREGROUND_INTENSITY);
applyAttributes();
return;
} else if (100 <= attribute && attribute <= 107) {
// background bright
info.attributes = (short) (info.attributes & ~0x00F0 | ANSI_BACKGROUND_COLOR_MAP[attribute - 100]);
info.attributes = (short) (info.attributes | Kernel32.BACKGROUND_INTENSITY);
info.attributes = (short) (info.attributes | BACKGROUND_INTENSITY);
applyAttributes();
return;
}
switch (attribute) {
case ATTRIBUTE_BOLD:
info.attributes = (short) (info.attributes | Kernel32.FOREGROUND_INTENSITY);
info.attributes = (short) (info.attributes | FOREGROUND_INTENSITY);
applyAttributes();
break;
case ATTRIBUTE_NORMAL:
info.attributes = (short) (info.attributes & ~Kernel32.FOREGROUND_INTENSITY);
info.attributes = (short) (info.attributes & ~FOREGROUND_INTENSITY);
applyAttributes();
break;
// Yeah, setting the background intensity is not underlining.. but it's best we can do using the Windows console API
case ATTRIBUTE_UNDERLINE:
info.attributes = (short) (info.attributes | Kernel32.BACKGROUND_INTENSITY);
info.attributes = (short) (info.attributes | BACKGROUND_INTENSITY);
applyAttributes();
break;
case ATTRIBUTE_UNDERLINE_OFF:
info.attributes = (short) (info.attributes & ~Kernel32.BACKGROUND_INTENSITY);
info.attributes = (short) (info.attributes & ~BACKGROUND_INTENSITY);
applyAttributes();
break;
@ -324,6 +346,7 @@ public final class WindowsAnsiOutputStream extends AnsiOutputStream {
scroll((short) count);
}
@Override
protected
void processCursorUpLine(final int count) throws IOException {
getConsoleInfo();
@ -332,6 +355,7 @@ public final class WindowsAnsiOutputStream extends AnsiOutputStream {
applyCursorPosition();
}
@Override
protected
void processCursorDownLine(final int count) throws IOException {
getConsoleInfo();
@ -401,6 +425,7 @@ public final class WindowsAnsiOutputStream extends AnsiOutputStream {
* and show THAT instead of blank lines. If the content is moved enough so that it runs OFF the
* buffer, blank lines will be shown.
*/
@SuppressWarnings("RedundantCast")
private void scroll(short rowsToScroll) throws IOException {
if (rowsToScroll == 0) {
return;
@ -427,7 +452,7 @@ public final class WindowsAnsiOutputStream extends AnsiOutputStream {
attribs.setValue(info.attributes);
// The clipping rectangle is the same as the scrolling rectangle, so we pass NULL
ASSERT(Kernel32.ScrollConsoleScreenBufferW(console, scrollRect, null, coordDest, attribs), "Could not scroll console");
ASSERT(ScrollConsoleScreenBuffer(console, scrollRect, null, coordDest, attribs), "Could not scroll console");
}
@Override
@ -435,7 +460,7 @@ public final class WindowsAnsiOutputStream extends AnsiOutputStream {
super.close();
if (console != null) {
Kernel32.CloseHandle(console);
CloseHandle(console);
}
}
}

View File

@ -1,63 +0,0 @@
/*
* Copyright 2010 dorkbox, llc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.console.util.posix;
import java.nio.ByteBuffer;
import com.sun.jna.Native;
import com.sun.jna.ptr.IntByReference;
@SuppressWarnings("ALL")
public
class CLibraryPosix {
static {
Native.register("c");
}
// MAGIC!
public static final int TIOCGWINSZ = System.getProperty("os.name").equalsIgnoreCase("linux") ? 0x5413 : 1074295912;
public static native
int isatty(int fd);
public static native
int read(int fd, IntByReference c, int count);
/**
* Original signature : <code>int ioctl(int, int, char*)</code><br>
*/
public static native
int ioctl(int d, int request, ByteBuffer data);
/**
* Put the state of FD into *TERMIOS_P.<br>
* <p>
* Original signature : <code>int tcgetattr(int, char*)</code><br>
*/
public static native
int tcgetattr(int fd, Termios termios_p);
/**
* Set the state of FD to *TERMIOS_P.<br>
* <p>
* Values for OPTIONAL_ACTIONS (TCSA*) are in <bits/termios.h>.<br>
* <p>
* Original signature : <code>int tcsetattr(int, int, char*)</code><br>
*/
public static native
int tcsetattr(int fd, int optional_actions, Termios termios_p);
}

View File

@ -1,184 +0,0 @@
/*
* Copyright 2010 dorkbox, llc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.console.util.posix;
import java.util.Arrays;
import java.util.List;
import com.sun.jna.Structure;
@SuppressWarnings("ALL")
public
class Termios extends Structure {
// NOTE: MUST BE BITS!! from: /usr/include/x86_64-linux-gnu/bits/termios.h
// the one in octal WILL NOT WORK!! (you have been warned)
// Definitions at: http://linux.die.net/man/3/termios
// Input flags - software input processing
public static class Input {
public static final int IGNBRK = 0000001; // ignore BREAK condition
public static final int BRKINT = 0000002; // map BREAK to SIGINTR
public static final int IGNPAR = 0000004; // ignore (discard) parity errors
public static final int PARMRK = 0000010; // mark parity and framing errors
public static final int INPCK = 0000020; // enable checking of parity errors
public static final int ISTRIP = 0000040; // strip 8th bit off chars
public static final int INLCR = 0000100; // map NL into CR
public static final int IGNCR = 0000200; // ignore CR
public static final int ICRNL = 0000400; // map CR to NL (ala CRMOD)
// public static final int IUCLC = 0001000; // (not in POSIX) Map uppercase characters to lowercase on input.
public static final int IXON = 0002000; // enable output flow control
public static final int IXANY = 0004000; // any char will restart after stop
public static final int IXOFF = 0010000; // enable input flow control
public static final int IMAXBEL = 0020000; // ring bell on input queue full
// public static final int IUTF8 = 0040000; // (since Linux 2.6.4) (not in POSIX) Input is UTF8; this allows character-erase to be correctly performed in cooked mode.
}
public static class Output {
// Output flags - software output processing
public static final int OPOST = 0000001; // enable following output processing (not set = raw output)
// public static final int OLCUC = 0000002; // (not in POSIX) Map lowercase characters to uppercase on output.
public static final int ONLCR = 0000004; // map NL to CR-NL (ala CRMOD)
public static final int OCRNL = 0000010; // map CR to NL on output
public static final int ONOCR = 0000020; // no CR output at column 0
public static final int ONLRET = 0000040; // NL performs CR function
public static final int OFILL = 0000100; // Send fill characters for a delay, rather than using a timed delay.
public static final int OFDEL = 0000200; // Fill character is ASCII DEL (0177). If unset, fill character is ASCII NUL ('\0'). (Not implemented on Linux.)
}
public static class Control {
// Control flags - hardware control of terminal
public static final int CSIZE = 0000060; // character size mask
public static final int CS5 = 0000000; // 5 bits (pseudo)
public static final int CS6 = 0000020; // 6 bits
public static final int CS7 = 0000040; // 7 bits
public static final int CS8 = 0000060; // 8 bits
public static final int CSTOPB = 0000100; // send 2 stop bits
public static final int CREAD = 0000200; // enable receiver
public static final int PARENB = 0000400; // parity enable
public static final int PARODD = 0001000; // odd parity, else even
public static final int HUPCL = 0002000; // hang up on last close
public static final int CLOCAL = 0004000; // ignore modem status lines
}
public static class Local {
// "Local" flags - dumping ground for other state
// Warning: some flags in this structure begin with the letter "I" and look like they belong in the input flag.
public static final int ISIG = 0000001; // enable signals INTR, QUIT, [D]SUSP
public static final int ICANON = 0000002; // canonicalize input lines
//public static final int XCASE = 0000004; // (not in POSIX; not supported under Linux)
public static final int ECHO = 0000010; // enable echoing
public static final int ECHOE = 0000020; // visually erase chars
public static final int ECHOK = 0000040; // echo NL after line kill
public static final int ECHONL = 0000100; // echo NL even if ECHO is off
public static final int NOFLSH = 0000200; // don't flush after interrupt
public static final int TOSTOP = 0000400; // stop background jobs from output
public static final int ECHOCTL = 0001000; // echo control chars as ^(Char)
public static final int ECHOPRT = 0002000; // visual erase mode for hardcopy
public static final int ECHOKE = 0004000; // visual erase for line kill
public static final int FLUSHO = 0001000; // output being flushed (state)
public static final int PENDIN = 0004000; // XXX retype pending input (state)
public static final int IEXTEN = 0100000; // enable DISCARD and LNEXT
public static final int EXTPROC = 0200000; // external processing
}
public static class ControlChars {
// Special Control Characters
//
// the value is the index into c_cc[] character array.
public static final int VINTR = 0; // ISIG
public static final int VQUIT = 1; // ISIG
public static final int VERASE = 2; // ICANON
public static final int VKILL = 3; // ICANON
public static final int VEOF = 4; // ICANON
public static final int VTIME = 5; // !ICANON
public static final int VMIN = 6; // !ICANON
public static final int VSWTC = 7;
public static final int VSTART = 8; // IXON, IXOFF
public static final int VSTOP = 9; // IXON, IXOFF
public static final int VSUSP = 10;// ISIG
public static final int VEOL = 11;// ICANON
public static final int VREPRINT = 12;// ICANON together with IEXTEN
public static final int VDISCARD = 13;
public static final int VWERASE = 14;// ICANON together with IEXTEN
public static final int VLNEXT = 15;// IEXTEN
public static final int VEOL2 = 16;// ICANON together with IEXTEN
}
public static final int TCSANOW = 0;
/**
* input mode flags
*/
public int inputFlags;
/**
* output mode flags
*/
public int outputFlags;
/**
* control mode flags
*/
public int controlFlags;
/**
* local mode flags
*/
public int localFlags;
/**
* line discipline
*/
public char lineDiscipline;
/**
* control characters
*/
public byte[] controlChars = new byte[32];
/**
* input speed
*/
public int inputSpeed;
/**
* output speed
*/
public int outputSpeed;
public
Termios() {}
@Override
protected
List<String> getFieldOrder() {
return Arrays.asList("inputFlags",
"outputFlags",
"controlFlags",
"localFlags",
"lineDiscipline",
"controlChars",
"inputSpeed",
"outputSpeed");
}
}

View File

@ -1,47 +0,0 @@
/*
* Copyright 2016 dorkbox, llc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.console.util.windows;
import java.util.Arrays;
import java.util.List;
import com.sun.jna.Structure;
/**
* https://msdn.microsoft.com/en-us/library/ms682093%28VS.85%29.aspx
*/
public
class CONSOLE_SCREEN_BUFFER_INFO extends Structure {
public COORD size = new COORD();
public COORD cursorPosition = new COORD();
public short attributes = (short) 0;
public SMALL_RECT window = new SMALL_RECT();
public COORD maximumWindowSize = new COORD();
@Override
protected
List<String> getFieldOrder() {
return Arrays.asList("size", "cursorPosition", "attributes", "window", "maximumWindowSize");
}
@Override
public
String toString() {
return "Size: " + size + " CursorPos: " + cursorPosition + " Attribs: " + attributes + " Window: " + window + " MaxWindowSize: " +
maximumWindowSize;
}
}

View File

@ -1,54 +0,0 @@
/*
* Copyright 2016 dorkbox, llc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.console.util.windows;
import java.util.Arrays;
import java.util.List;
import com.sun.jna.Structure;
/**
* https://msdn.microsoft.com/en-us/library/ms682119(v=vs.85).aspx
*/
public
class COORD extends Structure {
public short x;
public short y;
public
COORD.ByValue asValue() {
COORD.ByValue copy = new COORD.ByValue();
copy.x = this.x;
copy.y = this.y;
return copy;
}
@Override
protected
List<String> getFieldOrder() {
return Arrays.asList("x", "y");
}
@Override
public
String toString() {
return x + ":" + y;
}
static public
class ByValue extends COORD implements Structure.ByValue {}
}

View File

@ -1,52 +0,0 @@
/*
* Copyright 2016 dorkbox, llc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.console.util.windows;
import com.sun.jna.Union;
public
class CharUnion extends Union {
public char unicodeChar;
public byte asciiChar;
public
CharUnion() {
}
public
CharUnion(char c) {
setType(char.class);
unicodeChar = c;
}
public
CharUnion(byte c) {
setType(byte.class);
asciiChar = c;
}
public
void set(char c) {
setType(char.class);
unicodeChar = c;
}
public
void set(byte c) {
setType(byte.class);
asciiChar = c;
}
}

View File

@ -1,76 +0,0 @@
/*
*Copyright (c) 2010 Daniel Doubrovkine, All Rights Reserved
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
*
* This library is licensed under the LGPL, version 2.1 or later, and
* (from version 4.0 onward) the Apache Software License, version 2.0.
* Commercial license arrangements are negotiable.
*/
package dorkbox.console.util.windows;
import com.sun.jna.FromNativeContext;
import com.sun.jna.Pointer;
import com.sun.jna.PointerType;
/**
* Handle to an object.
*/
public
class HANDLE extends PointerType {
/**
* Constant value representing an invalid HANDLE.
*/
public static final HANDLE INVALID_HANDLE_VALUE = new HANDLE(Pointer.createConstant(Pointer.SIZE == 8 ? -1 : 0xFFFFFFFFL));
private boolean immutable;
public
HANDLE() {
}
public
HANDLE(Pointer p) {
setPointer(p);
immutable = true;
}
@Override
public
void setPointer(Pointer p) {
if (immutable) {
throw new UnsupportedOperationException("immutable reference");
}
super.setPointer(p);
}
/**
* Override to the appropriate object for INVALID_HANDLE_VALUE.
*/
@Override
public
Object fromNative(Object nativeValue, FromNativeContext context) {
Object o = super.fromNative(nativeValue, context);
if (INVALID_HANDLE_VALUE.equals(o)) {
return INVALID_HANDLE_VALUE;
}
return o;
}
@Override
public
String toString() {
return String.valueOf(getPointer());
}
}

View File

@ -1,65 +0,0 @@
/*
* Copyright 2016 dorkbox, llc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.console.util.windows;
import java.util.Arrays;
import java.util.List;
import com.sun.jna.Structure;
import com.sun.jna.Union;
/**
* https://msdn.microsoft.com/en-us/library/ms683499(v=VS.85).aspx
*/
public
class INPUT_RECORD extends Structure {
public static final short KEY_EVENT = 0x0001;
public static final short MOUSE_EVENT = 0x0002;
public short EventType;
public EventUnion Event;
@Override
public
void read() {
readField("EventType");
switch (EventType) {
case KEY_EVENT:
Event.setType(KEY_EVENT_RECORD.class);
break;
case MOUSE_EVENT:
Event.setType(MOUSE_EVENT_RECORD.class);
break;
}
super.read();
}
@Override
protected
List<String> getFieldOrder() {
return Arrays.asList("EventType", "Event");
}
static public
class ByReference extends INPUT_RECORD implements Structure.ByReference {}
public static
class EventUnion extends Union {
public KEY_EVENT_RECORD KeyEvent;
public MOUSE_EVENT_RECORD MouseEvent;
}
}

View File

@ -1,40 +0,0 @@
/*
* Copyright 2016 dorkbox, llc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.console.util.windows;
import java.util.Arrays;
import java.util.List;
import com.sun.jna.Structure;
/**
* https://msdn.microsoft.com/en-us/library/ms684166(v=VS.85).aspx
*/
public
class KEY_EVENT_RECORD extends Structure {
public boolean keyDown;
public short repeatCount;
public short virtualKeyCode;
public short virtualScanCode;
public CharUnion uChar;
public int controlKeyState;
@Override
protected
List<String> getFieldOrder() {
return Arrays.asList("keyDown", "repeatCount", "virtualKeyCode", "virtualScanCode", "uChar", "controlKeyState");
}
}

View File

@ -1,182 +0,0 @@
/*
* Copyright 2016 dorkbox, llc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.console.util.windows;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.IntByReference;
public class Kernel32 {
static {
Native.register("kernel32");
}
// see: http://msdn.microsoft.com/en-us/library/ms682013%28VS.85%29.aspx
public static final short FOREGROUND_BLACK = (short) 0x0000;
public static final short FOREGROUND_BLUE = (short) 0x0001;
public static final short FOREGROUND_GREEN = (short) 0x0002;
public static final short FOREGROUND_CYAN = (short) 0x0003;
public static final short FOREGROUND_RED = (short) 0x0004;
public static final short FOREGROUND_MAGENTA = (short) 0x0005;
public static final short FOREGROUND_YELLOW = (short) 0x0006;
public static final short FOREGROUND_GREY = (short) 0x0007;
public static final short FOREGROUND_INTENSITY = (short) 0x0008; // foreground color is intensified.
public static final short BACKGROUND_BLACK = (short) 0x0000;
public static final short BACKGROUND_BLUE = (short) 0x0010;
public static final short BACKGROUND_GREEN = (short) 0x0020;
public static final short BACKGROUND_CYAN = (short) 0x0030;
public static final short BACKGROUND_RED = (short) 0x0040;
public static final short BACKGROUND_MAGENTA = (short) 0x0050;
public static final short BACKGROUND_YELLOW = (short) 0x0060;
public static final short BACKGROUND_GREY = (short) 0x0070;
public static final short BACKGROUND_INTENSITY = (short) 0x0080; // background color is intensified.
public static final short COMMON_LVB_LEADING_BYTE = (short) 0x0100;
public static final short COMMON_LVB_TRAILING_BYTE = (short) 0x0200;
public static final short COMMON_LVB_GRID_HORIZONTAL = (short) 0x0400;
public static final short COMMON_LVB_GRID_LVERTICAL = (short) 0x0800;
public static final short COMMON_LVB_GRID_RVERTICAL = (short) 0x1000;
public static final short COMMON_LVB_REVERSE_VIDEO = (short) 0x4000;
public static final short COMMON_LVB_UNDERSCORE = (short) 0x8000;
private static final int FORMAT_MESSAGE_FROM_SYSTEM = 0x1000;
public static final int STD_INPUT_HANDLE = -10;
public static final int STD_OUTPUT_HANDLE = -11;
public static final int STD_ERROR_HANDLE = -12;
/**
* https://msdn.microsoft.com/en-us/library/ms683231%28VS.85%29.aspx
*/
public static native
HANDLE GetStdHandle(int stdHandle);
/**
* https://msdn.microsoft.com/en-us/library/ms724211%28VS.85%29.aspx
*/
public static native
int CloseHandle(HANDLE handle);
/**
* https://msdn.microsoft.com/en-us/library/ms686047%28VS.85%29.aspx
*/
public static native
int SetConsoleTextAttribute(HANDLE consoleOutput, short attributes);
/**
* https://msdn.microsoft.com/en-us/library/windows/desktop/ms679351(v=vs.85).aspx
*/
public static native
int FormatMessageW(int flags, Pointer source, int messageId, int languageId, byte[] buffer, int size, long[] args);
/**
* https://msdn.microsoft.com/en-us/library/ms683171%28VS.85%29.aspx
*/
public static native
int GetConsoleScreenBufferInfo(HANDLE consoleOutput, CONSOLE_SCREEN_BUFFER_INFO consoleScreenBufferInfo);
/**
* https://msdn.microsoft.com/en-us/library/windows/desktop/ms686025(v=vs.85).aspx
*/
public static native
int SetConsoleCursorPosition(HANDLE consoleOutput, COORD.ByValue cursorPosition);
/**
* https://msdn.microsoft.com/en-us/library/windows/desktop/ms685107(v=vs.85).aspx
*/
public static native
int ScrollConsoleScreenBufferW(HANDLE consoleOutput, SMALL_RECT.ByReference scrollRect, SMALL_RECT.ByReference clipRect, COORD.ByValue destinationOrigin, IntByReference fillAttributes);
/**
* https://msdn.microsoft.com/en-us/library/ms682662%28VS.85%29.aspx
*/
public static native
int FillConsoleOutputAttribute(HANDLE consoleOutput, short attribute, int length, COORD.ByValue writeCoord, IntByReference numberOfAttrsWritten);
/**
* https://msdn.microsoft.com/en-us/library/ms682663%28VS.85%29.aspx
*/
public static native
int FillConsoleOutputCharacterW(HANDLE consoleOutput, char character, int length, COORD.ByValue writeCoord, IntByReference numberOfCharsWritten);
/**
* https://msdn.microsoft.com/en-us/library/ms683167%28VS.85%29.aspx
*/
public static native
int GetConsoleMode(HANDLE handle, IntByReference mode);
/**
* https://msdn.microsoft.com/en-us/library/ms686033%28VS.85%29.aspx
*/
public static native
int SetConsoleMode(HANDLE handle, int mode);
/**
* https://msdn.microsoft.com/en-us/library/ms684961(v=VS.85).aspx
*/
public static native
int ReadConsoleInputW(HANDLE handle, INPUT_RECORD.ByReference inputRecords, int length, IntByReference eventsCount);
public static void ASSERT(final int returnValue, final String message) {
// if returnValue == 0, throw assertion error
assert returnValue != 0 : message + " : " + getLastErrorMessage();
}
private interface Win10 {
boolean IsWindows10OrGreater();
}
private static
String getLastErrorMessage() {
int errorCode = Native.getLastError();
int bufferSize = 160;
byte data[] = new byte[bufferSize];
FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, Pointer.NULL, errorCode, 0, data, bufferSize, null);
return new String(data);
}
/**
* Windows 10+ supports ANSI according to microsoft
*/
public static
boolean isWindows10OrGreater() {
try {
final Object kernel32 = Native.loadLibrary("kernel32", Win10.class);
if (kernel32 != null) {
boolean isWin10Plus = ((Win10)kernel32).IsWindows10OrGreater();
Native.unregister(Win10.class);
return isWin10Plus;
}
return false;
} catch (Exception e) {
return false;
}
}
}

View File

@ -1,35 +0,0 @@
/*
* Copyright 2016 dorkbox, llc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.console.util.windows;
import java.util.Arrays;
import java.util.List;
import com.sun.jna.Structure;
public
class MOUSE_EVENT_RECORD extends Structure {
public COORD mousePosition;
public int buttonState;
public int controlKeyState;
public int eventFlags;
@Override
protected
List<String> getFieldOrder() {
return Arrays.asList("mousePosition", "buttonState", "controlKeyState", "eventFlags");
}
}

View File

@ -1,59 +0,0 @@
/*
* Copyright 2016 dorkbox, llc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.console.util.windows;
import java.util.Arrays;
import java.util.List;
import com.sun.jna.Structure;
/**
* https://msdn.microsoft.com/en-us/library/ms686311%28VS.85%29.aspx
*/
@SuppressWarnings("NumericCastThatLosesPrecision")
public
class SMALL_RECT extends Structure {
public short left;
public short top;
public short right;
public short bottom;
public
short width() {
return (short) (this.right - this.left);
}
public
short height() {
return (short) (this.bottom - this.top);
}
@Override
protected
List<String> getFieldOrder() {
return Arrays.asList("left", "top", "right", "bottom");
}
@Override
public
String toString() {
return "LTRB: " + left + "," + top + "," + right + "," + bottom;
}
static public
class ByReference extends SMALL_RECT implements Structure.ByReference {}
}