diff --git a/src/dorkbox/util/jna/linux/CLibraryPosix.java b/src/dorkbox/util/jna/linux/CLibraryPosix.java
new file mode 100644
index 0000000..39584bf
--- /dev/null
+++ b/src/dorkbox/util/jna/linux/CLibraryPosix.java
@@ -0,0 +1,65 @@
+/*
+ * 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.util.jna.linux;
+
+import java.nio.ByteBuffer;
+
+import com.sun.jna.Native;
+import com.sun.jna.ptr.IntByReference;
+
+import dorkbox.util.jna.linux.structs.Termios;
+
+@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 : int ioctl(int, int, char*)
+ */
+ public static native
+ int ioctl(int d, int request, ByteBuffer data);
+
+ /**
+ * Put the state of FD into *TERMIOS_P.
+ *
+ * Original signature : int tcgetattr(int, char*)
+ */
+ public static native
+ int tcgetattr(int fd, Termios termios_p);
+
+ /**
+ * Set the state of FD to *TERMIOS_P.
+ *
+ * Values for OPTIONAL_ACTIONS (TCSA*) are in
+ * Original signature :
+ * https://msdn.microsoft.com/en-us/library/dd144877(v=vs.85).aspx
+ *
+ * @param handle A handle to the DC.
+ * @param nIndex The item to be returned.
+ */
+ public static native
+ int GetDeviceCaps(HDC handle, int nIndex);
/**
* http://msdn.microsoft.com/en-us/library/windows/desktop/dd144938(v=vs.85).aspx
diff --git a/src/dorkbox/util/jna/windows/Gdi32.java b/src/dorkbox/util/jna/windows/Gdi32.java
deleted file mode 100644
index 46c6e9d..0000000
--- a/src/dorkbox/util/jna/windows/Gdi32.java
+++ /dev/null
@@ -1,50 +0,0 @@
-/*
- * Copyright 2015 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.util.jna.windows;
-
-import static com.sun.jna.platform.win32.WinDef.HDC;
-
-import dorkbox.util.jna.JnaHelper;
-
-/**
- * bindings for GDI32
- *
- * Direct-mapping, See: https://github.com/java-native-access/jna/blob/master/www/DirectMapping.md
- */
-public
-class Gdi32 {
-
- static {
- JnaHelper.register("gdi32", Gdi32.class);
- }
-
- /**
- * Number of pixels per logical inch along the screen width. In a system with multiple display monitors, this value is the same for
- * all monitors.
- */
- public static final int LOGPIXELSX = 88;
-
-
- /**
- * The GetDeviceCaps function retrieves device-specific information for the specified device.
- *
- * https://msdn.microsoft.com/en-us/library/dd144877(v=vs.85).aspx
- *
- * @param handle A handle to the DC.
- * @param nIndex The item to be returned.
- */
- public static native int GetDeviceCaps(HDC handle, int nIndex);
-}
diff --git a/src/dorkbox/util/jna/windows/Kernel32.java b/src/dorkbox/util/jna/windows/Kernel32.java
index 3971c07..28ed2da 100644
--- a/src/dorkbox/util/jna/windows/Kernel32.java
+++ b/src/dorkbox/util/jna/windows/Kernel32.java
@@ -15,29 +15,190 @@
*/
package dorkbox.util.jna.windows;
+import static com.sun.jna.platform.win32.WinNT.HANDLE;
+
import com.sun.jna.Memory;
+import com.sun.jna.Native;
+import com.sun.jna.NativeLibrary;
+import com.sun.jna.Pointer;
+import com.sun.jna.ptr.IntByReference;
import com.sun.jna.ptr.PointerByReference;
+import com.sun.jna.win32.W32APIOptions;
+
+import dorkbox.util.jna.windows.structs.CONSOLE_SCREEN_BUFFER_INFO;
+import dorkbox.util.jna.windows.structs.COORD;
+import dorkbox.util.jna.windows.structs.INPUT_RECORD;
+import dorkbox.util.jna.windows.structs.SMALL_RECT;
public
class Kernel32 {
+ static {
+ Native.register(NativeLibrary.getInstance("kernel32", W32APIOptions.DEFAULT_OPTIONS));
+ }
+
+ // 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;
+
+ public static final int FORMAT_MESSAGE_FROM_SYSTEM = 0x1000;
+
+// public static
+// String getLastErrorMessage() {
+// int errorCode = Native.getLastError();
+// int bufferSize = 160;
+// byte data[] = new byte[bufferSize];
+// FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, Pointer.NULL, errorCode, 0, data, bufferSize, null);
+// return new String(data);
+// }
+
+ public static void ASSERT(final int returnValue, final String message) {
+ // if returnValue == 0, throw assertion error
+ assert returnValue != 0 : message + " : " + getLastErrorMessage();
+ }
+
public static
String getLastErrorMessage() {
- // has to be Kernel32.INSTANCE, otherwise it will crash the JVM
- int hresult = com.sun.jna.platform.win32.Kernel32.INSTANCE.GetLastError();
- if (hresult == 0) {
- return "HRESULT: 0x0 [No Error]";
+ int errorCode = Native.getLastError();
+ if (errorCode == 0) {
+ return "ErrorCode: 0x0 [No Error]";
} else {
Memory memory = new Memory(1024);
PointerByReference reference = new PointerByReference(memory);
- // Must be Kernel32.INSTANCE because of how it pulls in variety arguments.
- com.sun.jna.platform.win32.Kernel32.INSTANCE.FormatMessage(com.sun.jna.platform.win32.Kernel32.FORMAT_MESSAGE_FROM_SYSTEM, null, hresult, 0, reference, (int) memory.size(), null);
+// // Must be Kernel32.INSTANCE because of how it pulls in variety arguments.
+// com.sun.jna.platform.win32.Kernel32.INSTANCE.FormatMessage(com.sun.jna.platform.win32.Kernel32.FORMAT_MESSAGE_FROM_SYSTEM, null, errorCode, 0, reference, (int) memory.size(), null);
+ FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, null, errorCode, 0, reference, (int) memory.size(), null);
String memoryMessage = reference.getPointer()
.getString(0, true);
memoryMessage = memoryMessage.trim();
- return String.format("HRESULT: 0x%08x [%s]", hresult, memoryMessage);
+ return String.format("ErrorCode: 0x%08x [%s]", errorCode, memoryMessage);
}
}
+
+ private interface Win10 {
+ boolean IsWindows10OrGreater();
+ }
+
+ /**
+ * 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;
+ }
+ }
+
+ /**
+ * 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 FormatMessage(int flags, Pointer source, int messageId, int languageId, byte[] buffer, int size, long[] args);
+ int FormatMessage(int flags, Pointer source, int messageId, int languageId, PointerByReference 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 ScrollConsoleScreenBuffer(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 FillConsoleOutputCharacter(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 ReadConsoleInput(HANDLE handle, INPUT_RECORD.ByReference inputRecords, int length, IntByReference eventsCount);
}
diff --git a/src/dorkbox/util/jna/windows/structs/CONSOLE_SCREEN_BUFFER_INFO.java b/src/dorkbox/util/jna/windows/structs/CONSOLE_SCREEN_BUFFER_INFO.java
new file mode 100644
index 0000000..8236738
--- /dev/null
+++ b/src/dorkbox/util/jna/windows/structs/CONSOLE_SCREEN_BUFFER_INFO.java
@@ -0,0 +1,47 @@
+/*
+ * 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.util.jna.windows.structs;
+
+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
+ * int tcsetattr(int, int, char*)
+ */
+ public static native
+ int tcsetattr(int fd, int optional_actions, Termios termios_p);
+}
diff --git a/src/dorkbox/util/jna/linux/structs/Termios.java b/src/dorkbox/util/jna/linux/structs/Termios.java
new file mode 100644
index 0000000..2e8996e
--- /dev/null
+++ b/src/dorkbox/util/jna/linux/structs/Termios.java
@@ -0,0 +1,184 @@
+/*
+ * 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.util.jna.linux.structs;
+
+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