diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 169a1924..891f5f24 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -30,7 +30,7 @@ jobs: strategy: matrix: os: [ ubuntu-latest, windows-latest, macos-latest ] - java: [ '11', '17' ] + java: [ '21' ] steps: - uses: actions/checkout@v2 with: @@ -46,4 +46,4 @@ jobs: key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }} restore-keys: ${{ runner.os }}-m2 - name: mvn verify - run: mvn test + run: mvn verify -Dnosign diff --git a/Makefile b/Makefile deleted file mode 100644 index 4e2b2c85..00000000 --- a/Makefile +++ /dev/null @@ -1,164 +0,0 @@ -# -# 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. -# -include Makefile.common - -.PHONY: all package native native-all deploy crossbuild ducible clean-native - -linux-armv6-digest:=@sha256:7bad6ab302af34bdf6634c8c2b02c8dc6ac932c67da9ecc199c549ab405e971e -linux-x86-digest:=@sha256:7a8fda5ff1bb436ac1f2e7d40043deb630800fce33d123d04779d48f85702dcd -windows-static-x86-digest:=@sha256:896bd4a43bbc89502904afdc8d00e6f2422f8f35852cc59777d6426bfc8491e8 -windows-static-x64-digest:=@sha256:f159861bc80b29e5dafb223477167bec53ecec6cdacb051d31e90c5823542100 -windows-arm64-digest:=@sha256:f4b3c1a49ec8b53418cef1499dc3f9a54a5570b7a3ecdf42fc8c83eb94b01b7d -cross-build-digest:=@sha256:8dbaa86462270db93ae1b1b319bdd88d89272faf3a68632daf4fa36b414a326e -freebsd-crossbuild-digest:=@sha256:cda62697a15d8bdc0bc26e780b1771ee78f12c55e7d5813e62c478af5a747c43 -mcandre-snek-digest:=@sha256:9f84e9fcdf66daafc1f1c3fb772a6c97977714e17800aeac2e3bbe5dc5039dd0 - -all: package - -JANSI_OUT:=target/native-$(OS_NAME)-$(OS_ARCH) - -CCFLAGS:= -I$(JANSI_OUT) $(CCFLAGS) - -target: - @test -d target || mkdir target - -download-includes: target - @test -d target/inc || mkdir target/inc - @test -d target/inc/unix || mkdir target/inc/unix - @test -d target/inc/windows || mkdir target/inc/windows - test -f target/inc/jni.h || wget -O target/inc/jni.h https://raw.githubusercontent.com/openjdk/jdk/jdk-11%2B28/src/java.base/share/native/include/jni.h - test -f target/inc/unix/jni_md.h || wget -O target/inc/unix/jni_md.h https://raw.githubusercontent.com/openjdk/jdk/jdk-11%2B28/src/java.base/unix/native/include/jni_md.h - test -f target/inc/windows/jni_md.h || wget -O target/inc/windows/jni_md.h https://raw.githubusercontent.com/openjdk/jdk/jdk-11%2B28/src/java.base/windows/native/include/jni_md.h - -dockcross: target - @test -d target/dockcross || mkdir target/dockcross - -# This target does not generate the same image digest that the one uploaded -#crossbuild: target -# test -d target/crossbuild || git clone https://github.com/multiarch/crossbuild.git target/crossbuild -# git -C target/crossbuild reset --hard d06cdc31fce0c85ad78408b44794366dafd59554 -# docker build target/crossbuild -t multiarch/crossbuild - -ducible: target - test -d target/ducible || git clone --branch v1.2.2 https://github.com/jasonwhite/ducible.git target/ducible - make --directory=target/ducible ducible CROSS_PREFIX= CXX=g++ CC=gcc - -clean-native: - rm -rf $(JANSI_OUT) - -$(JANSI_OUT)/%.o: src/main/native/%.c - @mkdir -p $(@D) - $(info running: $(CC) $(CCFLAGS) -c $< -o $@) - $(CC) $(CCFLAGS) -c $< -o $@ - -ifeq ($(OS_NAME), Windows) -$(JANSI_OUT)/$(LIBNAME): ducible -endif -$(JANSI_OUT)/$(LIBNAME): $(JANSI_OUT)/jansi.o $(JANSI_OUT)/jansi_isatty.o $(JANSI_OUT)/jansi_structs.o $(JANSI_OUT)/jansi_ttyname.o - @mkdir -p $(@D) - $(CC) $(CCFLAGS) -o $@ $(JANSI_OUT)/jansi.o $(JANSI_OUT)/jansi_isatty.o $(JANSI_OUT)/jansi_structs.o $(JANSI_OUT)/jansi_ttyname.o $(LINKFLAGS) -ifeq ($(OS_NAME), Windows) - target/ducible/ducible $(JANSI_OUT)/$(LIBNAME) -endif - -NATIVE_DIR=src/main/resources/org/fusesource/jansi/internal/native/$(OS_NAME)/$(OS_ARCH) -NATIVE_TARGET_DIR:=target/classes/org/fusesource/jansi/internal/native/$(OS_NAME)/$(OS_ARCH) -NATIVE_DLL:=$(NATIVE_DIR)/$(LIBNAME) - -# For cross-compilation, install docker. See also https://github.com/dockcross/dockcross -# Disabled linux-armv6 build because of this issue; https://github.com/dockcross/dockcross/issues/190 -native-all: linux-x86 linux-x86_64 linux-arm linux-armv6 linux-armv7 \ - linux-arm64 linux-ppc64 win-x86 win-x86_64 win-arm64 mac-x86 mac-x86_64 mac-arm64 freebsd-x86 freebsd-x86_64 - -native: $(NATIVE_DLL) - -$(NATIVE_DLL): $(JANSI_OUT)/$(LIBNAME) - @mkdir -p $(@D) - cp $< $@ - @mkdir -p $(NATIVE_TARGET_DIR) - cp $< $(NATIVE_TARGET_DIR)/$(LIBNAME) - -target/dockcross/dockcross-linux-x86: dockcross - docker run --rm dockcross/linux-x86$(linux-x86-digest) > target/dockcross/dockcross-linux-x86 - chmod +x target/dockcross/dockcross-linux-x86 -linux-x86: download-includes target/dockcross/dockcross-linux-x86 - target/dockcross/dockcross-linux-x86 bash -c 'make clean-native native OS_NAME=Linux OS_ARCH=x86' - -linux-x86_64: download-includes - docker run -it --rm -v $$PWD:/workdir --user $$(id -u):$$(id -g) \ - -e CROSS_TRIPLE=x86_64-linux-gnu multiarch/crossbuild$(cross-build-digest) make clean-native native OS_NAME=Linux OS_ARCH=x86_64 - -linux-arm: download-includes - docker run -it --rm -v $$PWD:/workdir --user $$(id -u):$$(id -g) \ - -e CROSS_TRIPLE=arm-linux-gnueabi multiarch/crossbuild$(cross-build-digest) make clean-native native OS_NAME=Linux OS_ARCH=arm - -target/dockcross/dockcross-linux-armv6: dockcross - docker run --rm dockcross/linux-armv6$(linux-armv6-digest) > target/dockcross/dockcross-linux-armv6 - chmod +x target/dockcross/dockcross-linux-armv6 -linux-armv6: download-includes target/dockcross/dockcross-linux-armv6 - target/dockcross/dockcross-linux-armv6 bash -c 'make clean-native native CROSS_PREFIX=armv6-unknown-linux-gnueabihf- OS_NAME=Linux OS_ARCH=armv6' - -linux-armv7: download-includes - docker run -it --rm -v $$PWD:/workdir --user $$(id -u):$$(id -g) \ - -e CROSS_TRIPLE=arm-linux-gnueabihf multiarch/crossbuild$(cross-build-digest) make clean-native native OS_NAME=Linux OS_ARCH=armv7 - -linux-arm64: download-includes - docker run -it --rm -v $$PWD:/workdir --user $$(id -u):$$(id -g) \ - -e CROSS_TRIPLE=aarch64-linux-gnu multiarch/crossbuild$(cross-build-digest) make clean-native native OS_NAME=Linux OS_ARCH=arm64 - -linux-ppc64: download-includes - docker run -it --rm -v $$PWD:/workdir --user $$(id -u):$$(id -g) \ - -e CROSS_TRIPLE=powerpc64le-linux-gnu multiarch/crossbuild$(cross-build-digest) make clean-native native OS_NAME=Linux OS_ARCH=ppc64 - -target/dockcross/dockcross-windows-static-x86: dockcross - docker run --rm dockcross/windows-static-x86$(windows-static-x86-digest) > target/dockcross/dockcross-windows-static-x86 - chmod +x target/dockcross/dockcross-windows-static-x86 -win-x86: download-includes target/dockcross/dockcross-windows-static-x86 - target/dockcross/dockcross-windows-static-x86 bash -c 'make clean-native native CROSS_PREFIX=i686-w64-mingw32.static- OS_NAME=Windows OS_ARCH=x86' - -target/dockcross/dockcross-windows-static-x64: dockcross - docker run --rm dockcross/windows-static-x64$(windows-static-x64-digest) > target/dockcross/dockcross-windows-static-x64 - chmod +x target/dockcross/dockcross-windows-static-x64 -win-x86_64: download-includes target/dockcross/dockcross-windows-static-x64 - target/dockcross/dockcross-windows-static-x64 bash -c 'make clean-native native CROSS_PREFIX=x86_64-w64-mingw32.static- OS_NAME=Windows OS_ARCH=x86_64' - -target/dockcross/dockcross-windows-arm64: dockcross - docker run --rm dockcross/windows-arm64$(windows-arm64-digest) > target/dockcross/dockcross-windows-arm64 - chmod +x target/dockcross/dockcross-windows-arm64 -win-arm64: download-includes target/dockcross/dockcross-windows-arm64 - target/dockcross/dockcross-windows-arm64 bash -c 'make clean-native native CROSS_PREFIX=aarch64-w64-mingw32- OS_NAME=Windows OS_ARCH=arm64' - -mac-x86: download-includes - docker run -it --rm -v $$PWD:/workdir --user $$(id -u):$$(id -g) \ - -e CROSS_TRIPLE=i386-apple-darwin multiarch/crossbuild$(cross-build-digest) make clean-native native OS_NAME=Mac OS_ARCH=x86 - -mac-x86_64: download-includes - docker run -it --rm -v $$PWD:/workdir --user $$(id -u):$$(id -g) \ - -e CROSS_TRIPLE=x86_64-apple-darwin multiarch/crossbuild$(cross-build-digest) make clean-native native OS_NAME=Mac OS_ARCH=x86_64 - -mac-arm64: download-includes - docker run -it --rm -v $$PWD:/src --user $$(id -u):$$(id -g) \ - -e TARGET=arm64-apple-darwin mcandre/snek$(mcandre-snek-digest) sh -c "make clean-native native CROSS_PREFIX=arm64-apple-darwin20.4- OS_NAME=Mac OS_ARCH=arm64" - -freebsd-x86: download-includes - docker run -it --rm -v $$PWD:/workdir --user $$(id -u):$$(id -g) \ - empterdose/freebsd-cross-build$(freebsd-crossbuild-digest) make clean-native native CROSS_PREFIX=i386-freebsd9- OS_NAME=FreeBSD OS_ARCH=x86 - -freebsd-x86_64: download-includes - docker run -it --rm -v $$PWD:/workdir --user $$(id -u):$$(id -g) \ - empterdose/freebsd-cross-build$(freebsd-crossbuild-digest) make clean-native native CROSS_PREFIX=x86_64-freebsd9- OS_NAME=FreeBSD OS_ARCH=x86_64 - -#sparcv9: -# $(MAKE) native OS_NAME=SunOS OS_ARCH=sparcv9 - diff --git a/Makefile.common b/Makefile.common deleted file mode 100644 index d67eb0e6..00000000 --- a/Makefile.common +++ /dev/null @@ -1,171 +0,0 @@ -# -# 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. -# - -# os=Default is meant to be generic unix/linux - -known_targets := Linux-x86 Linux-x86_64 Linux-arm Linux-armv6 Linux-armv7 Linux-android-arm Linux-ppc64 Mac-x86 Mac-x86_64 Mac-arm64 DragonFly-x86_64 FreeBSD-x86_64 OpenBSD-x86_64 Windows-x86 Windows-x86_64 SunOS-sparcv9 HPUX-ia64_32 -target := $(OS_NAME)-$(OS_ARCH) - -ifeq (,$(findstring $(strip $(target)),$(known_targets))) - target := Default -endif - -# cross-compilation toolchain prefix (e.g. "arm-linux-gnueabi-") -CROSS_PREFIX := - -Default_CC := $(CROSS_PREFIX)gcc -Default_STRIP := $(CROSS_PREFIX)strip -Default_CCFLAGS := -I$(JAVA_HOME)/include -Itarget/inc -Itarget/inc/unix -Os -fPIC -fvisibility=hidden -Default_LINKFLAGS := -shared -Default_LIBNAME := libjansi.so -Default_JANSI_FLAGS := - -Linux-x86_CC := $(CROSS_PREFIX)gcc -Linux-x86_STRIP := $(CROSS_PREFIX)strip -Linux-x86_CCFLAGS := -I$(JAVA_HOME)/include -Itarget/inc -Itarget/inc/unix -Os -fPIC -m32 -fvisibility=hidden -Linux-x86_LINKFLAGS := -shared -static-libgcc -Linux-x86_LIBNAME := libjansi.so -Linux-x86_JANSI_FLAGS := - -Linux-x86_64_CC := $(CROSS_PREFIX)gcc -Linux-x86_64_STRIP := $(CROSS_PREFIX)strip -Linux-x86_64_CCFLAGS := -I$(JAVA_HOME)/include -Itarget/inc -Itarget/inc/unix -Os -fPIC -m64 -fvisibility=hidden -Linux-x86_64_LINKFLAGS := -shared -static-libgcc -Linux-x86_64_LIBNAME := libjansi.so -Linux-x86_64_JANSI_FLAGS := - -Linux-arm_CC := $(CROSS_PREFIX)gcc -Linux-arm_STRIP := $(CROSS_PREFIX)strip -Linux-arm_CCFLAGS := -I$(JAVA_HOME)/include -Itarget/inc -Itarget/inc/unix -Os -fPIC -mfloat-abi=softfp -mfpu=vfp -fvisibility=hidden -Linux-arm_LINKFLAGS := -shared -static-libgcc -Linux-arm_LIBNAME := libjansi.so -Linux-arm_JANSI_FLAGS := - -Linux-armv6_CC := $(CROSS_PREFIX)gcc -Linux-armv6_STRIP := $(CROSS_PREFIX)strip -Linux-armv6_CCFLAGS := -I$(JAVA_HOME)/include -Itarget/inc -Itarget/inc/unix -Os -mfloat-abi=hard -mfpu=vfp -fPIC -fvisibility=hidden -Linux-armv6_LINKFLAGS := -shared -static-libgcc -Linux-armv6_LIBNAME := libjansi.so -Linux-armv6_JANSI_FLAGS := - -Linux-armv7_CC := $(CROSS_PREFIX)gcc -Linux-armv7_STRIP := $(CROSS_PREFIX)strip -Linux-armv7_CCFLAGS := -I$(JAVA_HOME)/include -Itarget/inc -Itarget/inc/unix -Os -mfloat-abi=hard -mfpu=vfp -fPIC -fvisibility=hidden -Linux-armv7_LINKFLAGS := -shared -static-libgcc -Linux-armv7_LIBNAME := libjansi.so -Linux-armv7_JANSI_FLAGS := - -Linux-arm64_CC := $(CROSS_PREFIX)gcc -Linux-arm64_STRIP := $(CROSS_PREFIX)strip -Linux-arm64_CCFLAGS := -I$(JAVA_HOME)/include -Itarget/inc -Itarget/inc/unix -Os -mfloat-abi=hard -mfpu=vfp -fPIC -fvisibility=hidden -Linux-arm64_LINKFLAGS := -shared -static-libgcc -Linux-arm64_LIBNAME := libjansi.so -Linux-arm64_JANSI_FLAGS := - -Linux-ppc64_CC := $(CROSS_PREFIX)gcc -Linux-ppc64_STRIP := $(CROSS_PREFIX)strip -Linux-ppc64_CCFLAGS := -I$(JAVA_HOME)/include -Itarget/inc -Itarget/inc/unix -Os -fPIC -fvisibility=hidden -Linux-ppc64_LINKFLAGS := -shared -static-libgcc -Linux-ppc64_LIBNAME := libjansi.so -Linux-ppc64_JANSI_FLAGS := - -DragonFly-x86_64_CC := $(CROSS_PREFIX)cc -DragonFly-x86_64_STRIP := $(CROSS_PREFIX)strip -DragonFly-x86_64_CCFLAGS := -I$(JAVA_HOME)/include -Itarget/inc -Itarget/inc/unix -O2 -fPIC -fvisibility=hidden -DragonFly-x86_64_LINKFLAGS := -shared -DragonFly-x86_64_LIBNAME := libjansi.so -DragonFly-x86_64_JANSI_FLAGS := - -FreeBSD-x86_CC := $(CROSS_PREFIX)gcc -FreeBSD-x86_STRIP := $(CROSS_PREFIX)strip -FreeBSD-x86_CCFLAGS := -I$(JAVA_HOME)/include -Itarget/inc -Itarget/inc/unix -Os -fPIC -fvisibility=hidden -FreeBSD-x86_LINKFLAGS := -shared -FreeBSD-x86_LIBNAME := libjansi.so -FreeBSD-x86_JANSI_FLAGS := - -FreeBSD-x86_64_CC := $(CROSS_PREFIX)gcc -FreeBSD-x86_64_STRIP := $(CROSS_PREFIX)strip -FreeBSD-x86_64_CCFLAGS := -I$(JAVA_HOME)/include -Itarget/inc -Itarget/inc/unix -Os -fPIC -fvisibility=hidden -FreeBSD-x86_64_LINKFLAGS := -shared -FreeBSD-x86_64_LIBNAME := libjansi.so -FreeBSD-x86_64_JANSI_FLAGS := - -OpenBSD-x86_64_CC := $(CROSS_PREFIX)gcc -OpenBSD-x86_64_STRIP := $(CROSS_PREFIX)strip -OpenBSD-x86_64_CCFLAGS := -I$(JAVA_HOME)/include -Itarget/inc -Itarget/inc/unix -Os -fPIC -fvisibility=hidden -OpenBSD-x86_64_LINKFLAGS := -shared -OpenBSD-x86_64_LIBNAME := libjansi.so -OpenBSD-x86_64_JANSI_FLAGS := - -SunOS-sparcv9_CC := $(CROSS_PREFIX)gcc -SunOS-sparcv9_STRIP := $(CROSS_PREFIX)strip -SunOS-sparcv9_CCFLAGS := -I$(JAVA_HOME)/include -Itarget/inc -Itarget/inc/unix -O2s -fPIC -m64 -fvisibility=hidden -SunOS-sparcv9_LINKFLAGS := -shared -static-libgcc -SunOS-sparcv9_LIBNAME := libjansi.so -SunOS-sparcv9_JANSI_FLAGS := - -HPUX-ia64_32_CC := cc -HPUX-ia64_32_STRIP := strip -HPUX-ia64_32_CCFLAGS := -Itarget/inc -Itarget/inc/unix +Osize +z -Bhidden -HPUX-ia64_32_LINKFLAGS := -b -HPUX-ia64_32_LIBNAME := libjansi.so -HPUX-ia64_32_JANSI_FLAGS := - -Mac-x86_CC := gcc -Mac-x86_STRIP := strip -x -Mac-x86_CCFLAGS := -I$(JAVA_HOME)/include -Itarget/inc -Itarget/inc/unix -Os -fPIC -mmacosx-version-min=10.4 -fvisibility=hidden -Mac-x86_LINKFLAGS := -dynamiclib -Mac-x86_LIBNAME := libjansi.jnilib -Mac-x86_JANSI_FLAGS := -DJANSI_ENABLE_LOCKING_STYLE=0 - -Mac-x86_64_CC := gcc -arch $(OS_ARCH) -Mac-x86_64_STRIP := strip -x -MAC_SDK := /Developer/SDKs/MacOSX10.10.sdk -ifeq ($(wildcard MAC_SDK),) - MAC_SDK := /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk -endif -Mac-x86_64_CCFLAGS := -I$(MAC_SDK)/System/Library/Frameworks/JavaVM.framework/Headers -Itarget/inc -Itarget/inc/unix -Os -fPIC -mmacosx-version-min=10.6 -fvisibility=hidden -Mac-x86_64_LINKFLAGS := -dynamiclib -Mac-x86_64_LIBNAME := libjansi.jnilib -Mac-x86_64_JANSI_FLAGS := - -Mac-arm64_CC := $(CROSS_PREFIX)clang -v -Mac-arm64_STRIP := $(CROSS_PREFIX)strip -x -MAC_SDK := /usr/local/osxcross/SDK/MacOSX11.3.sdk/ -Mac-arm64_CCFLAGS := -I$(MAC_SDK)/System/Library/Frameworks/JavaVM.framework/Headers -Itarget/inc -Itarget/inc/unix -Os -fPIC -mmacosx-version-min=11.0 -fvisibility=hidden -Mac-arm64_LINKFLAGS := -shared -Mac-arm64_LIBNAME := libjansi.jnilib -Mac-arm64_JANSI_FLAGS := - -Windows-x86_CC := $(CROSS_PREFIX)gcc -Windows-x86_STRIP := $(CROSS_PREFIX)strip -Windows-x86_CCFLAGS := -D_JNI_IMPLEMENTATION_ -Itarget/inc -Itarget/inc/windows -Os -Windows-x86_LINKFLAGS := -Wl,--kill-at -shared -static-libgcc -Windows-x86_LIBNAME := jansi.dll -Windows-x86_JANSI_FLAGS := - -Windows-x86_64_CC := $(CROSS_PREFIX)gcc -Windows-x86_64_STRIP := $(CROSS_PREFIX)strip -Windows-x86_64_CCFLAGS := -D_JNI_IMPLEMENTATION_ -Itarget/inc -Itarget/inc/windows -Os -Windows-x86_64_LINKFLAGS := -Wl,--kill-at -shared -static-libgcc -Windows-x86_64_LIBNAME := jansi.dll -Windows-x86_64_JANSI_FLAGS := - - -CC := $($(target)_CC) -STRIP := $($(target)_STRIP) -CCFLAGS := $($(target)_CCFLAGS) -LINKFLAGS := $($(target)_LINKFLAGS) -LIBNAME := $($(target)_LIBNAME) -CCFLAGS := $(CCFLAGS) diff --git a/pom.xml b/pom.xml index 9cf9578f..1bc16e7f 100644 --- a/pom.xml +++ b/pom.xml @@ -25,7 +25,7 @@ org.fusesource.jansi jansi - 2.4.1-SNAPSHOT + 3.0.0-SNAPSHOT jar Jansi @@ -109,6 +109,23 @@ + + org.jline + jline-terminal-ffm + 3.23.1-SNAPSHOT + + + org.jline + jline-terminal-jni + 3.23.1-SNAPSHOT + + + org.graalvm.sdk + nativeimage + 23.1.0 + provided + true + org.junit.jupiter junit-jupiter @@ -160,13 +177,67 @@ + + org.apache.maven.plugins + maven-enforcer-plugin + 3.4.1 + + + enforce-java + + enforce + + + + + 21 + + + + + + org.apache.maven.plugins maven-compiler-plugin - 3.8.1 + 3.11.0 + true + ${jdkTarget} + ${jdkTarget} ${jdkTarget} + + -Xlint:-options + + + + default-compile + + + **/ffm/*.java + + + + + jdk-21 + + compile + + + 21 + + **/ffm/*.java + + + --enable-preview + + + + + default-testCompile + + org.apache.felix @@ -206,12 +277,14 @@ 9 + org.fusesource.jansi.AnsiMain org.fusesource.jansi org.fusesource.jansi; org.fusesource.jansi.io; + true @@ -324,20 +397,6 @@ maven-release-plugin 3.0.0-M1 - - org.apache.maven.plugins - maven-gpg-plugin - 1.6 - - - sign-artifacts - - sign - - verify - - - org.sonatype.plugins nexus-staging-maven-plugin @@ -351,12 +410,12 @@ com.diffplug.spotless spotless-maven-plugin - 2.38.0 + 2.39.0 - 2.35.0 + 2.38.0 java|javax,org,,\# @@ -410,4 +469,33 @@ + + + sign + + + !nosign + + + + + + org.apache.maven.plugins + maven-gpg-plugin + 1.6 + + + sign-artifacts + + sign + + verify + + + + + + + + diff --git a/src/main/java/org/fusesource/jansi/Ansi.java b/src/main/java/org/fusesource/jansi/Ansi.java index c1054742..576d8d53 100644 --- a/src/main/java/org/fusesource/jansi/Ansi.java +++ b/src/main/java/org/fusesource/jansi/Ansi.java @@ -149,17 +149,14 @@ public int value() { } } + @FunctionalInterface public interface Consumer { void apply(Ansi ansi); } public static final String DISABLE = Ansi.class.getName() + ".disable"; - private static Callable detector = new Callable() { - public Boolean call() throws Exception { - return !Boolean.getBoolean(DISABLE); - } - }; + private static Callable detector = () -> !Boolean.getBoolean(DISABLE); public static void setDetector(final Callable detector) { if (detector == null) throw new IllegalArgumentException(); @@ -374,7 +371,7 @@ public Ansi reset() { } private final StringBuilder builder; - private final ArrayList attributeOptions = new ArrayList(5); + private final ArrayList attributeOptions = new ArrayList<>(5); public Ansi() { this(new StringBuilder(80)); @@ -716,19 +713,45 @@ public Ansi scrollDown(final int rows) { return rows > 0 ? appendEscapeSequence('T', rows) : rows < 0 ? scrollUp(-rows) : this; } + @Deprecated + public Ansi restorCursorPosition() { + return restoreCursorPosition(); + } + public Ansi saveCursorPosition() { + saveCursorPositionSCO(); + return saveCursorPositionDEC(); + } + + // SCO command + public Ansi saveCursorPositionSCO() { return appendEscapeSequence('s'); } - @Deprecated - public Ansi restorCursorPosition() { - return appendEscapeSequence('u'); + // DEC command + public Ansi saveCursorPositionDEC() { + builder.append(FIRST_ESC_CHAR); + builder.append('7'); + return this; } public Ansi restoreCursorPosition() { + restoreCursorPositionSCO(); + return restoreCursorPositionDEC(); + } + + // SCO command + public Ansi restoreCursorPositionSCO() { return appendEscapeSequence('u'); } + // DEC command + public Ansi restoreCursorPositionDEC() { + builder.append(FIRST_ESC_CHAR); + builder.append('8'); + return this; + } + public Ansi reset() { return a(Attribute.RESET); } diff --git a/src/main/java/org/fusesource/jansi/AnsiConsole.java b/src/main/java/org/fusesource/jansi/AnsiConsole.java index 3b7b0032..e7120e23 100644 --- a/src/main/java/org/fusesource/jansi/AnsiConsole.java +++ b/src/main/java/org/fusesource/jansi/AnsiConsole.java @@ -24,24 +24,17 @@ import java.io.UnsupportedEncodingException; import java.nio.charset.Charset; import java.nio.charset.UnsupportedCharsetException; -import java.util.Locale; -import org.fusesource.jansi.internal.CLibrary; -import org.fusesource.jansi.internal.CLibrary.WinSize; -import org.fusesource.jansi.internal.Kernel32.CONSOLE_SCREEN_BUFFER_INFO; import org.fusesource.jansi.io.AnsiOutputStream; import org.fusesource.jansi.io.AnsiProcessor; import org.fusesource.jansi.io.FastBufferedOutputStream; -import org.fusesource.jansi.io.WindowsAnsiProcessor; - -import static org.fusesource.jansi.internal.CLibrary.ioctl; -import static org.fusesource.jansi.internal.CLibrary.isatty; -import static org.fusesource.jansi.internal.Kernel32.GetConsoleMode; -import static org.fusesource.jansi.internal.Kernel32.GetConsoleScreenBufferInfo; -import static org.fusesource.jansi.internal.Kernel32.GetStdHandle; -import static org.fusesource.jansi.internal.Kernel32.STD_ERROR_HANDLE; -import static org.fusesource.jansi.internal.Kernel32.STD_OUTPUT_HANDLE; -import static org.fusesource.jansi.internal.Kernel32.SetConsoleMode; +import org.jline.terminal.Terminal; +import org.jline.terminal.TerminalBuilder; +import org.jline.terminal.impl.DumbTerminal; +import org.jline.terminal.spi.SystemStream; +import org.jline.terminal.spi.TerminalExt; +import org.jline.terminal.spi.TerminalProvider; +import org.jline.utils.OSUtils; /** * Provides consistent access to an ANSI aware console PrintStream or an ANSI codes stripping PrintStream @@ -167,6 +160,33 @@ public class AnsiConsole { */ public static final String JANSI_GRACEFUL = "jansi.graceful"; + /** + * The {@code jansi.providers} system property can be set to control which internal provider + * will be used. If this property is not set, the {@code ffm} provider will be used if available, + * else the {@code jni} one will be used. If set, this property is interpreted as a comma + * separated list of provider names to try in order. + */ + public static final String JANSI_PROVIDERS = "jansi.providers"; + /** + * The name of the {@code jni} provider. + */ + public static final String JANSI_PROVIDER_JNI = "jni"; + /** + * The name of the {@code ffm} provider. + */ + public static final String JANSI_PROVIDER_FFM = "ffm"; + /** + * The name of the {@code native-image} provider. + *

This provider uses the + * Native Image C API + * to call native functions, so it is only available when building to native image. + * Additionally, this provider currently does not support Windows. + *

Note: This is not the only provider available on Native Image, + * and it is usually recommended to use ffm or jni provider. + * This provider is mainly used when building static native images linked to musl libc. + */ + public static final String JANSI_PROVIDER_NATIVE_IMAGE = "native-image"; + /** * @deprecated this field will be made private in a future release, use {@link #sysOut()} instead */ @@ -202,8 +222,7 @@ public static int getTerminalWidth() { return w; } - static final boolean IS_WINDOWS = - System.getProperty("os.name").toLowerCase(Locale.ENGLISH).contains("win"); + static final boolean IS_WINDOWS = OSUtils.IS_WINDOWS; static final boolean IS_CYGWIN = IS_WINDOWS && System.getenv("PWD") != null && System.getenv("PWD").startsWith("/"); @@ -223,127 +242,86 @@ public static int getTerminalWidth() { static { if (getBoolean(JANSI_EAGER)) { - initStreams(); + doInstall(); } } - private static boolean initialized; private static int installed; - private static int virtualProcessing; + static Terminal terminal; private AnsiConsole() {} - private static AnsiPrintStream ansiStream(boolean stdout) { - FileDescriptor descriptor = stdout ? FileDescriptor.out : FileDescriptor.err; - final OutputStream out = new FastBufferedOutputStream(new FileOutputStream(descriptor)); - - String enc = System.getProperty(stdout ? "sun.stdout.encoding" : "sun.stderr.encoding"); + /** + * Initialize the out/err ansi-enabled streams + */ + static synchronized void doInstall() { + try { + if (terminal == null) { + TerminalBuilder builder = TerminalBuilder.builder() + .system(true) + .name("jansi") + .providers(System.getProperty(JANSI_PROVIDERS)); + String graceful = System.getProperty(JANSI_GRACEFUL); + if (graceful != null) { + builder.dumb(Boolean.parseBoolean(graceful)); + } + terminal = builder.build(); + out = ansiStream(true); + err = ansiStream(false); + } + } catch (IOException e) { + throw new IOError(e); + } + } - final boolean isatty; - boolean isAtty; - boolean withException; - // Do not use the CLibrary.STDOUT_FILENO to avoid errors in case - // the library can not be loaded on unsupported platforms - final int fd = stdout ? STDOUT_FILENO : STDERR_FILENO; + static synchronized void doUninstall() { try { - // If we can detect that stdout is not a tty.. then setup - // to strip the ANSI sequences.. - isAtty = isatty(fd) != 0; - String term = System.getenv("TERM"); - String emacs = System.getenv("INSIDE_EMACS"); - if (isAtty && "dumb".equals(term) && emacs != null && !emacs.contains("comint")) { - isAtty = false; + if (terminal != null) { + terminal.close(); } - withException = false; - } catch (Throwable ignore) { - // These errors happen if the JNI lib is not available for your platform. - // But since we are on ANSI friendly platform, assume the user is on the console. - isAtty = false; - withException = true; + } catch (IOException e) { + throw new IOError(e); + } finally { + terminal = null; + out = null; + err = null; } - isatty = isAtty; + } + private static AnsiPrintStream ansiStream(boolean stdout) throws IOException { + final OutputStream out; final AnsiOutputStream.WidthSupplier width; - final AnsiProcessor processor; + final AnsiProcessor processor = null; final AnsiType type; - final AnsiOutputStream.IoRunnable installer; - final AnsiOutputStream.IoRunnable uninstaller; - if (!isatty) { - processor = null; - type = withException ? AnsiType.Unsupported : AnsiType.Redirected; - installer = uninstaller = null; + final AnsiOutputStream.IoRunnable installer = null; + final AnsiOutputStream.IoRunnable uninstaller = null; + + // TerminalBuilder builder = TerminalBuilder.builder() + // .system(true) + // .name("jansi") + // .providers(System.getProperty(JANSI_PROVIDERS)) + // .systemOutput(stdout ? TerminalBuilder.SystemOutput.SysOut : + // TerminalBuilder.SystemOutput.SysErr); + // String graceful = System.getProperty(JANSI_GRACEFUL); + // if (graceful != null) { + // builder.dumb(Boolean.parseBoolean(graceful)); + // } + // Terminal terminal = builder.build(); + final TerminalProvider provider = ((TerminalExt) terminal).getProvider(); + final boolean isatty = + provider != null && provider.isSystemStream(stdout ? SystemStream.Output : SystemStream.Error); + if (isatty) { + out = terminal.output(); + width = terminal::getWidth; + type = terminal instanceof DumbTerminal ? AnsiType.Unsupported : AnsiType.Native; + } else { + out = new FastBufferedOutputStream(new FileOutputStream(stdout ? FileDescriptor.out : FileDescriptor.err)); width = new AnsiOutputStream.ZeroWidthSupplier(); - } else if (IS_WINDOWS) { - final long console = GetStdHandle(stdout ? STD_OUTPUT_HANDLE : STD_ERROR_HANDLE); - final int[] mode = new int[1]; - final boolean isConsole = GetConsoleMode(console, mode) != 0; - if (isConsole && SetConsoleMode(console, mode[0] | ENABLE_VIRTUAL_TERMINAL_PROCESSING) != 0) { - SetConsoleMode(console, mode[0]); // set it back for now, but we know it works - processor = null; - type = AnsiType.VirtualTerminal; - installer = new AnsiOutputStream.IoRunnable() { - @Override - public void run() throws IOException { - virtualProcessing++; - SetConsoleMode(console, mode[0] | ENABLE_VIRTUAL_TERMINAL_PROCESSING); - } - }; - uninstaller = new AnsiOutputStream.IoRunnable() { - @Override - public void run() throws IOException { - if (--virtualProcessing == 0) { - SetConsoleMode(console, mode[0]); - } - } - }; - } else if ((IS_CONEMU || IS_CYGWIN || IS_MSYSTEM) && !isConsole) { - // ANSI-enabled ConEmu, Cygwin or MSYS(2) on Windows... - processor = null; - type = AnsiType.Native; - installer = uninstaller = null; - } else { - // On Windows, when no ANSI-capable terminal is used, we know the console does not natively interpret - // ANSI - // codes but we can use jansi Kernel32 API for console - AnsiProcessor proc; - AnsiType ttype; - try { - proc = new WindowsAnsiProcessor(out, console); - ttype = AnsiType.Emulation; - } catch (Throwable ignore) { - // this happens when the stdout is being redirected to a file. - // Use the AnsiProcessor to strip out the ANSI escape sequences. - proc = new AnsiProcessor(out); - ttype = AnsiType.Unsupported; - } - processor = proc; - type = ttype; - installer = uninstaller = null; - } - width = new AnsiOutputStream.WidthSupplier() { - @Override - public int getTerminalWidth() { - CONSOLE_SCREEN_BUFFER_INFO info = new CONSOLE_SCREEN_BUFFER_INFO(); - GetConsoleScreenBufferInfo(console, info); - return info.windowWidth(); - } - }; + type = ((TerminalExt) terminal).getSystemStream() != null ? AnsiType.Redirected : AnsiType.Unsupported; } - - // We must be on some Unix variant... - else { - // ANSI-enabled ConEmu, Cygwin or MSYS(2) on Windows... - processor = null; - type = AnsiType.Native; - installer = uninstaller = null; - width = new AnsiOutputStream.WidthSupplier() { - @Override - public int getTerminalWidth() { - WinSize sz = new WinSize(); - ioctl(fd, CLibrary.TIOCGWINSZ, sz); - return sz.ws_col; - } - }; + String enc = System.getProperty(stdout ? "stdout.encoding" : "stderr.encoding"); + if (enc == null) { + enc = System.getProperty(stdout ? "sun.stdout.encoding" : "sun.stderr.encoding"); } AnsiMode mode; @@ -446,8 +424,7 @@ static boolean getBoolean(String name) { try { String val = System.getProperty(name); result = val.isEmpty() || Boolean.parseBoolean(val); - } catch (IllegalArgumentException e) { - } catch (NullPointerException e) { + } catch (IllegalArgumentException | NullPointerException ignored) { } return result; } @@ -461,7 +438,7 @@ static boolean getBoolean(String name) { * @return a PrintStream which is ANSI aware. */ public static AnsiPrintStream out() { - initStreams(); + doInstall(); return (AnsiPrintStream) out; } @@ -483,7 +460,7 @@ public static PrintStream sysOut() { * @return a PrintStream which is ANSI aware. */ public static AnsiPrintStream err() { - initStreams(); + doInstall(); return (AnsiPrintStream) err; } @@ -503,13 +480,7 @@ public static PrintStream sysErr() { */ public static synchronized void systemInstall() { if (installed == 0) { - initStreams(); - try { - ((AnsiPrintStream) out).install(); - ((AnsiPrintStream) err).install(); - } catch (IOException e) { - throw new IOError(e); - } + doInstall(); System.setOut(out); System.setErr(err); } @@ -531,26 +502,9 @@ public static synchronized boolean isInstalled() { public static synchronized void systemUninstall() { installed--; if (installed == 0) { - try { - ((AnsiPrintStream) out).uninstall(); - ((AnsiPrintStream) err).uninstall(); - } catch (IOException e) { - throw new IOError(e); - } - initialized = false; + doUninstall(); System.setOut(system_out); System.setErr(system_err); } } - - /** - * Initialize the out/err ansi-enabled streams - */ - static synchronized void initStreams() { - if (!initialized) { - out = ansiStream(true); - err = ansiStream(false); - initialized = true; - } - } } diff --git a/src/main/java/org/fusesource/jansi/AnsiMain.java b/src/main/java/org/fusesource/jansi/AnsiMain.java index c2a845d6..89ca722f 100644 --- a/src/main/java/org/fusesource/jansi/AnsiMain.java +++ b/src/main/java/org/fusesource/jansi/AnsiMain.java @@ -26,9 +26,8 @@ import java.util.Properties; import org.fusesource.jansi.Ansi.Attribute; -import org.fusesource.jansi.internal.CLibrary; -import org.fusesource.jansi.internal.JansiLoader; +import static java.nio.charset.StandardCharsets.UTF_8; import static org.fusesource.jansi.Ansi.ansi; /** @@ -54,30 +53,37 @@ public static void main(String... args) throws IOException { System.out.println(); - // info on native library - System.out.println("library.jansi.path= " + System.getProperty("library.jansi.path", "")); - System.out.println("library.jansi.version= " + System.getProperty("library.jansi.version", "")); - boolean loaded = JansiLoader.initialize(); - if (loaded) { - System.out.println("Jansi native library loaded from " + JansiLoader.getNativeLibraryPath()); - if (JansiLoader.getNativeLibrarySourceUrl() != null) { - System.out.println(" which was auto-extracted from " + JansiLoader.getNativeLibrarySourceUrl()); - } - } else { - String prev = System.getProperty(AnsiConsole.JANSI_GRACEFUL); - try { - System.setProperty(AnsiConsole.JANSI_GRACEFUL, "false"); - JansiLoader.initialize(); - } catch (Throwable e) { - e.printStackTrace(System.out); - } finally { - if (prev != null) { - System.setProperty(AnsiConsole.JANSI_GRACEFUL, prev); - } else { - System.clearProperty(AnsiConsole.JANSI_GRACEFUL); - } - } - } + System.out.println("jansi.providers= " + System.getProperty(AnsiConsole.JANSI_PROVIDERS, "")); + // String provider = ((TerminalExt) AnsiConsole.terminal).getProvider().name(); + // System.out.println("Selected provider: " + provider); + // + // if (AnsiConsole.JANSI_PROVIDER_JNI.equals(provider)) { + // // info on native library + // System.out.println("library.jansi.path= " + System.getProperty("library.jansi.path", "")); + // System.out.println("library.jansi.version= " + System.getProperty("library.jansi.version", "")); + // boolean loaded = JansiLoader.initialize(); + // if (loaded) { + // System.out.println("Jansi native library loaded from " + JansiLoader.getNativeLibraryPath()); + // if (JansiLoader.getNativeLibrarySourceUrl() != null) { + // System.out.println(" which was auto-extracted from " + + // JansiLoader.getNativeLibrarySourceUrl()); + // } + // } else { + // String prev = System.getProperty(AnsiConsole.JANSI_GRACEFUL); + // try { + // System.setProperty(AnsiConsole.JANSI_GRACEFUL, "false"); + // JansiLoader.initialize(); + // } catch (Throwable e) { + // e.printStackTrace(System.out); + // } finally { + // if (prev != null) { + // System.setProperty(AnsiConsole.JANSI_GRACEFUL, prev); + // } else { + // System.clearProperty(AnsiConsole.JANSI_GRACEFUL); + // } + // } + // } + // } System.out.println(); @@ -85,9 +91,14 @@ public static void main(String... args) throws IOException { + "os.version= " + System.getProperty("os.version") + ", " + "os.arch= " + System.getProperty("os.arch")); System.out.println("file.encoding= " + System.getProperty("file.encoding")); + System.out.println("sun.stdout.encoding= " + System.getProperty("sun.stdout.encoding") + ", " + + "sun.stderr.encoding= " + System.getProperty("sun.stderr.encoding")); + System.out.println("stdout.encoding= " + System.getProperty("stdout.encoding") + ", " + "stderr.encoding= " + + System.getProperty("stderr.encoding")); System.out.println("java.version= " + System.getProperty("java.version") + ", " + "java.vendor= " + System.getProperty("java.vendor") + "," + " java.home= " + System.getProperty("java.home")); + System.out.println("Console: " + System.console()); System.out.println(); @@ -188,11 +199,34 @@ private static String getJansiVersion() { } private static void diagnoseTty(boolean stderr) { - int fd = stderr ? CLibrary.STDERR_FILENO : CLibrary.STDOUT_FILENO; - int isatty = CLibrary.LOADED ? CLibrary.isatty(fd) : 0; - - System.out.println("isatty(STD" + (stderr ? "ERR" : "OUT") + "_FILENO): " + isatty + ", System." - + (stderr ? "err" : "out") + " " + ((isatty == 0) ? "is *NOT*" : "is") + " a terminal"); + // int isatty; + // int width; + // if (AnsiConsole.IS_WINDOWS) { + // long console = AnsiConsoleSupportHolder.getKernel32().getStdHandle(!stderr); + // isatty = AnsiConsoleSupportHolder.getKernel32().isTty(console); + // if ((AnsiConsole.IS_CONEMU || AnsiConsole.IS_CYGWIN || AnsiConsole.IS_MSYSTEM) && isatty == 0) { + // MingwSupport mingw = new MingwSupport(); + // String name = mingw.getConsoleName(!stderr); + // if (name != null && !name.isEmpty()) { + // isatty = 1; + // width = mingw.getTerminalWidth(name); + // } else { + // isatty = 0; + // width = 0; + // } + // } else { + // width = AnsiConsoleSupportHolder.getKernel32().getTerminalWidth(console); + // } + // } else { + // int fd = stderr ? AnsiConsoleSupport.CLibrary.STDERR_FILENO : + // AnsiConsoleSupport.CLibrary.STDOUT_FILENO; + // isatty = AnsiConsoleSupportHolder.getCLibrary().isTty(fd); + // width = AnsiConsoleSupportHolder.getCLibrary().getTerminalWidth(fd); + // } + // + // System.out.println("isatty(STD" + (stderr ? "ERR" : "OUT") + "_FILENO): " + isatty + ", System." + // + (stderr ? "err" : "out") + " " + ((isatty == 0) ? "is *NOT*" : "is") + " a terminal"); + // System.out.println("width(STD" + (stderr ? "ERR" : "OUT") + "_FILENO): " + width); } private static void testAnsi(boolean stderr) { @@ -274,7 +308,7 @@ private static String getPomPropertiesVersion(String path) throws IOException { private static void printJansiLogoDemo() throws IOException { BufferedReader in = - new BufferedReader(new InputStreamReader(AnsiMain.class.getResourceAsStream("jansi.txt"), "UTF-8")); + new BufferedReader(new InputStreamReader(AnsiMain.class.getResourceAsStream("jansi.txt"), UTF_8)); try { String l; while ((l = in.readLine()) != null) { diff --git a/src/main/java/org/fusesource/jansi/WindowsSupport.java b/src/main/java/org/fusesource/jansi/WindowsSupport.java index 010f527e..d55606de 100644 --- a/src/main/java/org/fusesource/jansi/WindowsSupport.java +++ b/src/main/java/org/fusesource/jansi/WindowsSupport.java @@ -15,27 +15,18 @@ */ package org.fusesource.jansi; -import java.io.UnsupportedEncodingException; - -import static org.fusesource.jansi.internal.Kernel32.FORMAT_MESSAGE_FROM_SYSTEM; -import static org.fusesource.jansi.internal.Kernel32.FormatMessageW; -import static org.fusesource.jansi.internal.Kernel32.GetLastError; +// import org.fusesource.jansi.internal.AnsiConsoleSupportHolder; public class WindowsSupport { public static String getLastErrorMessage() { - int errorCode = GetLastError(); - return getErrorMessage(errorCode); + // int errorCode = AnsiConsoleSupportHolder.getKernel32().getLastError(); + // return getErrorMessage(errorCode); + throw new UnsupportedOperationException(); } public static String getErrorMessage(int errorCode) { - int bufferSize = 160; - byte data[] = new byte[bufferSize]; - FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, 0, errorCode, 0, data, bufferSize, null); - try { - return new String(data, "UTF-16LE").trim(); - } catch (UnsupportedEncodingException e) { - throw new IllegalStateException(e); - } + // return AnsiConsoleSupportHolder.getKernel32().getErrorMessage(errorCode); + throw new UnsupportedOperationException(); } } diff --git a/src/main/java/org/fusesource/jansi/internal/CLibrary.java b/src/main/java/org/fusesource/jansi/internal/CLibrary.java deleted file mode 100644 index 2e2285c3..00000000 --- a/src/main/java/org/fusesource/jansi/internal/CLibrary.java +++ /dev/null @@ -1,160 +0,0 @@ -/* - * Copyright (C) 2009-2023 the original author(s). - * - * 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 org.fusesource.jansi.internal; - -/** - * Interface to access some low level POSIX functions, loaded by - * HawtJNI Runtime - * as jansi library. - * - * @see JansiLoader - */ -@SuppressWarnings("unused") -public class CLibrary { - - // - // Initialization - // - - public static final boolean LOADED; - - static { - LOADED = JansiLoader.initialize(); - if (LOADED) { - init(); - } - } - - private static native void init(); - - // - // Constants - // - - public static int STDOUT_FILENO = 1; - - public static int STDERR_FILENO = 2; - - public static boolean HAVE_ISATTY; - - public static boolean HAVE_TTYNAME; - - public static int TCSANOW; - public static int TCSADRAIN; - public static int TCSAFLUSH; - public static long TIOCGETA; - public static long TIOCSETA; - public static long TIOCGETD; - public static long TIOCSETD; - public static long TIOCGWINSZ; - public static long TIOCSWINSZ; - - /** - * test whether a file descriptor refers to a terminal - * - * @param fd file descriptor - * @return isatty() returns 1 if fd is an open file descriptor referring to a - * terminal; otherwise 0 is returned, and errno is set to indicate the - * error - * @see ISATTY(3) man-page - * @see ISATTY(3P) man-page - */ - public static native int isatty(int fd); - - public static native String ttyname(int filedes); - - /** - * The openpty() function finds an available pseudoterminal and returns - * file descriptors for the master and slave in amaster and aslave. - * - * @param amaster master return value - * @param aslave slave return value - * @param name filename return value - * @param termios optional pty attributes - * @param winsize optional size - * @return 0 on success - * @see OPENPTY(3) man-page - */ - public static native int openpty(int[] amaster, int[] aslave, byte[] name, Termios termios, WinSize winsize); - - public static native int tcgetattr(int filedes, Termios termios); - - public static native int tcsetattr(int filedes, int optional_actions, Termios termios); - - /** - * Control a STREAMS device. - * - * @see IOCTL(3P) man-page - */ - public static native int ioctl(int filedes, long request, int[] params); - - public static native int ioctl(int filedes, long request, WinSize params); - - /** - * Window sizes. - * - * @see IOCTL_TTY(2) man-page - */ - public static class WinSize { - - static { - JansiLoader.initialize(); - init(); - } - - private static native void init(); - - public static int SIZEOF; - - public short ws_row; - public short ws_col; - public short ws_xpixel; - public short ws_ypixel; - - public WinSize() {} - - public WinSize(short ws_row, short ws_col) { - this.ws_row = ws_row; - this.ws_col = ws_col; - } - } - - /** - * termios structure for termios functions, describing a general terminal interface that is - * provided to control asynchronous communications ports - * - * @see TERMIOS(3) man-page - */ - public static class Termios { - - static { - JansiLoader.initialize(); - init(); - } - - private static native void init(); - - public static int SIZEOF; - - public long c_iflag; - public long c_oflag; - public long c_cflag; - public long c_lflag; - public byte[] c_cc = new byte[32]; - public long c_ispeed; - public long c_ospeed; - } -} diff --git a/src/main/java/org/fusesource/jansi/internal/JansiLoader.java b/src/main/java/org/fusesource/jansi/internal/JansiLoader.java deleted file mode 100644 index f705620c..00000000 --- a/src/main/java/org/fusesource/jansi/internal/JansiLoader.java +++ /dev/null @@ -1,416 +0,0 @@ -/* - * Copyright (C) 2009-2023 the original author(s). - * - * 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 org.fusesource.jansi.internal; - -/*-------------------------------------------------------------------------- - * Copyright 2007 Taro L. Saito - * - * 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. - *--------------------------------------------------------------------------*/ - -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.FilenameFilter; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.net.URL; -import java.util.Arrays; -import java.util.LinkedList; -import java.util.List; -import java.util.Properties; -import java.util.Random; - -import org.fusesource.jansi.AnsiConsole; - -/** - * Set the system properties, org.jansi.lib.path, org.jansi.lib.name, - * appropriately so that jansi can find *.dll, *.jnilib and - * *.so files, according to the current OS (win, linux, mac). - *

- * The library files are automatically extracted from this project's package - * (JAR). - *

- * usage: call {@link #initialize()} before using Jansi. - */ -public class JansiLoader { - - private static boolean loaded = false; - private static String nativeLibraryPath; - private static String nativeLibrarySourceUrl; - - /** - * Loads Jansi native library. - * - * @return True if jansi native library is successfully loaded; false - * otherwise. - */ - public static synchronized boolean initialize() { - // only cleanup before the first extract - if (!loaded) { - Thread cleanup = new Thread(JansiLoader::cleanup, "cleanup"); - cleanup.setPriority(Thread.MIN_PRIORITY); - cleanup.setDaemon(true); - cleanup.start(); - } - try { - loadJansiNativeLibrary(); - } catch (Exception e) { - if (!Boolean.parseBoolean(System.getProperty(AnsiConsole.JANSI_GRACEFUL, "true"))) { - throw new RuntimeException( - "Unable to load jansi native library. You may want set the `jansi.graceful` system property to true to be able to use Jansi on your platform", - e); - } - } - return loaded; - } - - public static String getNativeLibraryPath() { - return nativeLibraryPath; - } - - public static String getNativeLibrarySourceUrl() { - return nativeLibrarySourceUrl; - } - - private static File getTempDir() { - return new File(System.getProperty("jansi.tmpdir", System.getProperty("java.io.tmpdir"))); - } - - /** - * Deleted old native libraries e.g. on Windows the DLL file is not removed - * on VM-Exit (bug #80) - */ - static void cleanup() { - String tempFolder = getTempDir().getAbsolutePath(); - File dir = new File(tempFolder); - - File[] nativeLibFiles = dir.listFiles(new FilenameFilter() { - private final String searchPattern = "jansi-" + getVersion(); - - public boolean accept(File dir, String name) { - return name.startsWith(searchPattern) && !name.endsWith(".lck"); - } - }); - if (nativeLibFiles != null) { - for (File nativeLibFile : nativeLibFiles) { - File lckFile = new File(nativeLibFile.getAbsolutePath() + ".lck"); - if (!lckFile.exists()) { - try { - nativeLibFile.delete(); - } catch (SecurityException e) { - System.err.println("Failed to delete old native lib" + e.getMessage()); - } - } - } - } - } - - private static int readNBytes(InputStream in, byte[] b) throws IOException { - int n = 0; - int len = b.length; - while (n < len) { - int count = in.read(b, n, len - n); - if (count <= 0) break; - n += count; - } - return n; - } - - private static String contentsEquals(InputStream in1, InputStream in2) throws IOException { - byte[] buffer1 = new byte[8192]; - byte[] buffer2 = new byte[8192]; - int numRead1; - int numRead2; - while (true) { - numRead1 = readNBytes(in1, buffer1); - numRead2 = readNBytes(in2, buffer2); - if (numRead1 > 0) { - if (numRead2 <= 0) { - return "EOF on second stream but not first"; - } - if (numRead2 != numRead1) { - return "Read size different (" + numRead1 + " vs " + numRead2 + ")"; - } - // Otherwise same number of bytes read - if (!Arrays.equals(buffer1, buffer2)) { - return "Content differs"; - } - // Otherwise same bytes read, so continue ... - } else { - // Nothing more in stream 1 ... - if (numRead2 > 0) { - return "EOF on first stream but not second"; - } else { - return null; - } - } - } - } - - /** - * Extracts and loads the specified library file to the target folder - * - * @param libFolderForCurrentOS Library path. - * @param libraryFileName Library name. - * @param targetFolder Target folder. - * @return - */ - private static boolean extractAndLoadLibraryFile( - String libFolderForCurrentOS, String libraryFileName, String targetFolder) { - String nativeLibraryFilePath = libFolderForCurrentOS + "/" + libraryFileName; - // Include architecture name in temporary filename in order to avoid conflicts - // when multiple JVMs with different architectures running at the same time - String uuid = randomUUID(); - String extractedLibFileName = String.format("jansi-%s-%s-%s", getVersion(), uuid, libraryFileName); - String extractedLckFileName = extractedLibFileName + ".lck"; - - File extractedLibFile = new File(targetFolder, extractedLibFileName); - File extractedLckFile = new File(targetFolder, extractedLckFileName); - - try { - // Extract a native library file into the target directory - try (InputStream in = JansiLoader.class.getResourceAsStream(nativeLibraryFilePath)) { - if (!extractedLckFile.exists()) { - new FileOutputStream(extractedLckFile).close(); - } - try (OutputStream out = new FileOutputStream(extractedLibFile)) { - copy(in, out); - } - } finally { - // Delete the extracted lib file on JVM exit. - extractedLibFile.deleteOnExit(); - extractedLckFile.deleteOnExit(); - } - - // Set executable (x) flag to enable Java to load the native library - extractedLibFile.setReadable(true); - extractedLibFile.setWritable(true); - extractedLibFile.setExecutable(true); - - // Check whether the contents are properly copied from the resource folder - try (InputStream nativeIn = JansiLoader.class.getResourceAsStream(nativeLibraryFilePath)) { - try (InputStream extractedLibIn = new FileInputStream(extractedLibFile)) { - String eq = contentsEquals(nativeIn, extractedLibIn); - if (eq != null) { - throw new RuntimeException(String.format( - "Failed to write a native library file at %s because %s", extractedLibFile, eq)); - } - } - } - - // Load library - if (loadNativeLibrary(extractedLibFile)) { - nativeLibrarySourceUrl = - JansiLoader.class.getResource(nativeLibraryFilePath).toExternalForm(); - return true; - } - } catch (IOException e) { - System.err.println(e.getMessage()); - } - return false; - } - - private static String randomUUID() { - return Long.toHexString(new Random().nextLong()); - } - - private static void copy(InputStream in, OutputStream out) throws IOException { - byte[] buf = new byte[8192]; - int n; - while ((n = in.read(buf)) > 0) { - out.write(buf, 0, n); - } - } - - /** - * Loads native library using the given path and name of the library. - * - * @param libPath Path of the native library. - * @return True for successfully loading; false otherwise. - */ - private static boolean loadNativeLibrary(File libPath) { - if (libPath.exists()) { - try { - String path = libPath.getAbsolutePath(); - System.load(path); - nativeLibraryPath = path; - return true; - } catch (UnsatisfiedLinkError e) { - if (!libPath.canExecute()) { - // NOTE: this can be tested using something like: - // docker run --rm --tmpfs /tmp -v $PWD:/jansi openjdk:11 java -jar - // /jansi/target/jansi-xxx-SNAPSHOT.jar - System.err.printf( - "Failed to load native library:%s. The native library file at %s is not executable, " - + "make sure that the directory is mounted on a partition without the noexec flag, or set the " - + "jansi.tmpdir system property to point to a proper location. osinfo: %s%n", - libPath.getName(), libPath, OSInfo.getNativeLibFolderPathForCurrentOS()); - } else { - System.err.printf( - "Failed to load native library:%s. osinfo: %s%n", - libPath.getName(), OSInfo.getNativeLibFolderPathForCurrentOS()); - } - System.err.println(e); - return false; - } - - } else { - return false; - } - } - - /** - * Loads jansi library using given path and name of the library. - * - * @throws - */ - private static void loadJansiNativeLibrary() throws Exception { - if (loaded) { - return; - } - - List triedPaths = new LinkedList(); - - // Try loading library from library.jansi.path library path */ - String jansiNativeLibraryPath = System.getProperty("library.jansi.path"); - String jansiNativeLibraryName = System.getProperty("library.jansi.name"); - if (jansiNativeLibraryName == null) { - jansiNativeLibraryName = System.mapLibraryName("jansi"); - assert jansiNativeLibraryName != null; - if (jansiNativeLibraryName.endsWith(".dylib")) { - jansiNativeLibraryName = jansiNativeLibraryName.replace(".dylib", ".jnilib"); - } - } - - if (jansiNativeLibraryPath != null) { - String withOs = jansiNativeLibraryPath + "/" + OSInfo.getNativeLibFolderPathForCurrentOS(); - if (loadNativeLibrary(new File(withOs, jansiNativeLibraryName))) { - loaded = true; - return; - } else { - triedPaths.add(withOs); - } - - if (loadNativeLibrary(new File(jansiNativeLibraryPath, jansiNativeLibraryName))) { - loaded = true; - return; - } else { - triedPaths.add(jansiNativeLibraryPath); - } - } - - // Load the os-dependent library from the jar file - String packagePath = JansiLoader.class.getPackage().getName().replace('.', '/'); - jansiNativeLibraryPath = - String.format("/%s/native/%s", packagePath, OSInfo.getNativeLibFolderPathForCurrentOS()); - boolean hasNativeLib = hasResource(jansiNativeLibraryPath + "/" + jansiNativeLibraryName); - - if (hasNativeLib) { - // temporary library folder - String tempFolder = getTempDir().getAbsolutePath(); - // Try extracting the library from jar - if (extractAndLoadLibraryFile(jansiNativeLibraryPath, jansiNativeLibraryName, tempFolder)) { - loaded = true; - return; - } else { - triedPaths.add(jansiNativeLibraryPath); - } - } - - // As a last resort try from java.library.path - String javaLibraryPath = System.getProperty("java.library.path", ""); - for (String ldPath : javaLibraryPath.split(File.pathSeparator)) { - if (ldPath.isEmpty()) { - continue; - } - if (loadNativeLibrary(new File(ldPath, jansiNativeLibraryName))) { - loaded = true; - return; - } else { - triedPaths.add(ldPath); - } - } - - throw new Exception(String.format( - "No native library found for os.name=%s, os.arch=%s, paths=[%s]", - OSInfo.getOSName(), OSInfo.getArchName(), join(triedPaths, File.pathSeparator))); - } - - private static boolean hasResource(String path) { - return JansiLoader.class.getResource(path) != null; - } - - /** - * @return The major version of the jansi library. - */ - public static int getMajorVersion() { - String[] c = getVersion().split("\\."); - return (c.length > 0) ? Integer.parseInt(c[0]) : 1; - } - - /** - * @return The minor version of the jansi library. - */ - public static int getMinorVersion() { - String[] c = getVersion().split("\\."); - return (c.length > 1) ? Integer.parseInt(c[1]) : 0; - } - - /** - * @return The version of the jansi library. - */ - public static String getVersion() { - - URL versionFile = JansiLoader.class.getResource("/org/fusesource/jansi/jansi.properties"); - - String version = "unknown"; - try { - if (versionFile != null) { - Properties versionData = new Properties(); - versionData.load(versionFile.openStream()); - version = versionData.getProperty("version", version); - version = version.trim().replaceAll("[^0-9.]", ""); - } - } catch (IOException e) { - System.err.println(e); - } - return version; - } - - private static String join(List list, String separator) { - StringBuilder sb = new StringBuilder(); - boolean first = true; - for (String item : list) { - if (first) first = false; - else sb.append(separator); - - sb.append(item); - } - return sb.toString(); - } -} diff --git a/src/main/java/org/fusesource/jansi/internal/Kernel32.java b/src/main/java/org/fusesource/jansi/internal/Kernel32.java deleted file mode 100644 index 1d45de1f..00000000 --- a/src/main/java/org/fusesource/jansi/internal/Kernel32.java +++ /dev/null @@ -1,521 +0,0 @@ -/* - * Copyright (C) 2009-2023 the original author(s). - * - * 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 org.fusesource.jansi.internal; - -import java.io.IOException; - -import org.fusesource.jansi.WindowsSupport; - -/** - * Interface to access Win32 base APIs. - * - * @see JansiLoader - */ -@SuppressWarnings("unused") -public class Kernel32 { - - static { - if (JansiLoader.initialize()) { - init(); - } - } - - private static native void init(); - - public static short FOREGROUND_BLUE; - public static short FOREGROUND_GREEN; - public static short FOREGROUND_RED; - public static short FOREGROUND_INTENSITY; - public static short BACKGROUND_BLUE; - public static short BACKGROUND_GREEN; - public static short BACKGROUND_RED; - public static short BACKGROUND_INTENSITY; - public static short COMMON_LVB_LEADING_BYTE; - public static short COMMON_LVB_TRAILING_BYTE; - public static short COMMON_LVB_GRID_HORIZONTAL; - public static short COMMON_LVB_GRID_LVERTICAL; - public static short COMMON_LVB_GRID_RVERTICAL; - public static short COMMON_LVB_REVERSE_VIDEO; - public static short COMMON_LVB_UNDERSCORE; - public static int FORMAT_MESSAGE_FROM_SYSTEM; - public static int STD_INPUT_HANDLE; - public static int STD_OUTPUT_HANDLE; - public static int STD_ERROR_HANDLE; - public static int INVALID_HANDLE_VALUE; - - public static native long malloc(long size); - - public static native void free(long ptr); - - /** - * http://msdn.microsoft.com/en-us/library/ms686311%28VS.85%29.aspx - */ - public static class SMALL_RECT { - static { - JansiLoader.initialize(); - init(); - } - - private static native void init(); - - public static int SIZEOF; - - public short left; - public short top; - public short right; - public short bottom; - - public short width() { - return (short) (right - left); - } - - public short height() { - return (short) (bottom - top); - } - - public SMALL_RECT copy() { - SMALL_RECT rc = new SMALL_RECT(); - rc.left = left; - rc.top = top; - rc.right = right; - rc.bottom = bottom; - return rc; - } - } - - /** - * see http://msdn.microsoft.com/en-us/library/ms686047%28VS.85%29.aspx - */ - public static native int SetConsoleTextAttribute(long consoleOutput, short attributes); - - public static class COORD { - - static { - JansiLoader.initialize(); - init(); - } - - private static native void init(); - - public static int SIZEOF; - - public short x; - public short y; - - public COORD copy() { - COORD rc = new COORD(); - rc.x = x; - rc.y = y; - return rc; - } - } - - /** - * http://msdn.microsoft.com/en-us/library/ms682093%28VS.85%29.aspx - */ - public static class CONSOLE_SCREEN_BUFFER_INFO { - - static { - JansiLoader.initialize(); - init(); - } - - private static native void init(); - - public static int SIZEOF; - - public COORD size = new COORD(); - public COORD cursorPosition = new COORD(); - public short attributes; - public SMALL_RECT window = new SMALL_RECT(); - public COORD maximumWindowSize = new COORD(); - - public int windowWidth() { - return window.width() + 1; - } - - public int windowHeight() { - return window.height() + 1; - } - } - - // DWORD WINAPI WaitForSingleObject( - // _In_ HANDLE hHandle, - // _In_ DWORD dwMilliseconds - // ); - public static native int WaitForSingleObject(long hHandle, int dwMilliseconds); - - /** - * see: http://msdn.microsoft.com/en-us/library/ms724211%28VS.85%29.aspx - */ - public static native int CloseHandle(long handle); - - /** - * see: http://msdn.microsoft.com/en-us/library/ms679360(VS.85).aspx - */ - public static native int GetLastError(); - - public static native int FormatMessageW( - int flags, long source, int messageId, int languageId, byte[] buffer, int size, long[] args); - - /** - * See: http://msdn.microsoft.com/en-us/library/ms683171%28VS.85%29.aspx - */ - public static native int GetConsoleScreenBufferInfo( - long consoleOutput, CONSOLE_SCREEN_BUFFER_INFO consoleScreenBufferInfo); - - /** - * see: http://msdn.microsoft.com/en-us/library/ms683231%28VS.85%29.aspx - */ - public static native long GetStdHandle(int stdHandle); - - /** - * http://msdn.microsoft.com/en-us/library/ms686025%28VS.85%29.aspx - */ - public static native int SetConsoleCursorPosition(long consoleOutput, COORD cursorPosition); - - /** - * see: http://msdn.microsoft.com/en-us/library/ms682663%28VS.85%29.aspx - */ - public static native int FillConsoleOutputCharacterW( - long consoleOutput, char character, int length, COORD writeCoord, int[] numberOfCharsWritten); - - /** - * see: https://msdn.microsoft.com/en-us/library/ms682662%28VS.85%29.aspx - */ - public static native int FillConsoleOutputAttribute( - long consoleOutput, short attribute, int length, COORD writeCoord, int[] numberOfAttrsWritten); - - /** - * see: http://msdn.microsoft.com/en-us/library/ms687401(v=VS.85).aspx - */ - public static native int WriteConsoleW( - long consoleOutput, char[] buffer, int numberOfCharsToWrite, int[] numberOfCharsWritten, long reserved); - - /** - * see: http://msdn.microsoft.com/en-us/library/ms683167%28VS.85%29.aspx - */ - public static native int GetConsoleMode(long handle, int[] mode); - - /** - * see: http://msdn.microsoft.com/en-us/library/ms686033%28VS.85%29.aspx - */ - public static native int SetConsoleMode(long handle, int mode); - - /** - * see: http://msdn.microsoft.com/en-us/library/078sfkak(VS.80).aspx - */ - public static native int _getch(); - - /** - * see: http://msdn.microsoft.com/en-us/library/ms686050%28VS.85%29.aspx - * - * @return 0 if title was set successfully - */ - public static native int SetConsoleTitle(String title); - - /** - * see: http://msdn.microsoft.com/en-us/library/ms683169(v=VS.85).aspx - * - * @return the current output code page - */ - public static native int GetConsoleOutputCP(); - - /** - * see: http://msdn.microsoft.com/en-us/library/ms686036(v=VS.85).aspx - * - * @return non 0 if code page was set - */ - public static native int SetConsoleOutputCP(int codePageID); - - /** - * see: https://msdn.microsoft.com/en-us/library/windows/desktop/ms682013(v=vs.85).aspx - */ - public static class CHAR_INFO { - - static { - JansiLoader.initialize(); - init(); - } - - private static native void init(); - - public static int SIZEOF; - - public short attributes; - public char unicodeChar; - } - - /** - * see: https://msdn.microsoft.com/en-us/library/windows/desktop/ms685107(v=vs.85).aspx - */ - public static native int ScrollConsoleScreenBuffer( - long consoleOutput, - SMALL_RECT scrollRectangle, - SMALL_RECT clipRectangle, - COORD destinationOrigin, - CHAR_INFO fill); - - /** - * see: http://msdn.microsoft.com/en-us/library/ms684166(v=VS.85).aspx - */ - public static class KEY_EVENT_RECORD { - - static { - JansiLoader.initialize(); - init(); - } - - private static native void init(); - - public static int SIZEOF; - public static int CAPSLOCK_ON; - public static int NUMLOCK_ON; - public static int SCROLLLOCK_ON; - public static int ENHANCED_KEY; - public static int LEFT_ALT_PRESSED; - public static int LEFT_CTRL_PRESSED; - public static int RIGHT_ALT_PRESSED; - public static int RIGHT_CTRL_PRESSED; - public static int SHIFT_PRESSED; - - public boolean keyDown; - public short repeatCount; - public short keyCode; - public short scanCode; - public char uchar; - public int controlKeyState; - - public String toString() { - return "KEY_EVENT_RECORD{" + "keyDown=" - + keyDown + ", repeatCount=" - + repeatCount + ", keyCode=" - + keyCode + ", scanCode=" - + scanCode + ", uchar=" - + uchar + ", controlKeyState=" - + controlKeyState + '}'; - } - } - - /** - * see: http://msdn.microsoft.com/en-us/library/ms684239(v=VS.85).aspx - */ - public static class MOUSE_EVENT_RECORD { - - static { - JansiLoader.initialize(); - init(); - } - - private static native void init(); - - public static int SIZEOF; - public static int FROM_LEFT_1ST_BUTTON_PRESSED; - public static int FROM_LEFT_2ND_BUTTON_PRESSED; - public static int FROM_LEFT_3RD_BUTTON_PRESSED; - public static int FROM_LEFT_4TH_BUTTON_PRESSED; - public static int RIGHTMOST_BUTTON_PRESSED; - - public static int CAPSLOCK_ON; - public static int NUMLOCK_ON; - public static int SCROLLLOCK_ON; - public static int ENHANCED_KEY; - public static int LEFT_ALT_PRESSED; - public static int LEFT_CTRL_PRESSED; - public static int RIGHT_ALT_PRESSED; - public static int RIGHT_CTRL_PRESSED; - public static int SHIFT_PRESSED; - - public static int DOUBLE_CLICK; - public static int MOUSE_HWHEELED; - public static int MOUSE_MOVED; - public static int MOUSE_WHEELED; - - public COORD mousePosition = new COORD(); - public int buttonState; - public int controlKeyState; - public int eventFlags; - - public String toString() { - return "MOUSE_EVENT_RECORD{" + "mousePosition=" - + mousePosition + ", buttonState=" - + buttonState + ", controlKeyState=" - + controlKeyState + ", eventFlags=" - + eventFlags + '}'; - } - } - - /** - * see: http://msdn.microsoft.com/en-us/library/ms687093(v=VS.85).aspx - */ - public static class WINDOW_BUFFER_SIZE_RECORD { - - static { - JansiLoader.initialize(); - init(); - } - - private static native void init(); - - public static int SIZEOF; - - public COORD size = new COORD(); - - public String toString() { - return "WINDOW_BUFFER_SIZE_RECORD{size=" + size + '}'; - } - } - - /** - * see: http://msdn.microsoft.com/en-us/library/ms683149(v=VS.85).aspx - */ - public static class FOCUS_EVENT_RECORD { - static { - JansiLoader.initialize(); - init(); - } - - private static native void init(); - - public static int SIZEOF; - public boolean setFocus; - } - - /** - * see: http://msdn.microsoft.com/en-us/library/ms684213(v=VS.85).aspx - */ - public static class MENU_EVENT_RECORD { - static { - JansiLoader.initialize(); - init(); - } - - private static native void init(); - - public static int SIZEOF; - public int commandId; - } - - /** - * see: http://msdn.microsoft.com/en-us/library/ms683499(v=VS.85).aspx - */ - public static class INPUT_RECORD { - - static { - JansiLoader.initialize(); - init(); - } - - private static native void init(); - - public static int SIZEOF; - public static short KEY_EVENT; - public static short MOUSE_EVENT; - public static short WINDOW_BUFFER_SIZE_EVENT; - public static short FOCUS_EVENT; - public static short MENU_EVENT; - public short eventType; - public KEY_EVENT_RECORD keyEvent = new KEY_EVENT_RECORD(); - public MOUSE_EVENT_RECORD mouseEvent = new MOUSE_EVENT_RECORD(); - public WINDOW_BUFFER_SIZE_RECORD windowBufferSizeEvent = new WINDOW_BUFFER_SIZE_RECORD(); - public MENU_EVENT_RECORD menuEvent = new MENU_EVENT_RECORD(); - public FOCUS_EVENT_RECORD focusEvent = new FOCUS_EVENT_RECORD(); - - public static native void memmove(INPUT_RECORD dest, long src, long size); - } - - /** - * see: http://msdn.microsoft.com/en-us/library/ms684961(v=VS.85).aspx - */ - private static native int ReadConsoleInputW(long handle, long inputRecord, int length, int[] eventsCount); - - /** - * see: http://msdn.microsoft.com/en-us/library/ms684344(v=VS.85).aspx - */ - private static native int PeekConsoleInputW(long handle, long inputRecord, int length, int[] eventsCount); - - /** - * see: http://msdn.microsoft.com/en-us/library/ms683207(v=VS.85).aspx - */ - public static native int GetNumberOfConsoleInputEvents(long handle, int[] numberOfEvents); - - /** - * see: http://msdn.microsoft.com/en-us/library/ms683147(v=VS.85).aspx - */ - public static native int FlushConsoleInputBuffer(long handle); - - /** - * Return console input events. - */ - public static INPUT_RECORD[] readConsoleInputHelper(long handle, int count, boolean peek) throws IOException { - int[] length = new int[1]; - int res; - long inputRecordPtr = 0; - try { - inputRecordPtr = malloc(INPUT_RECORD.SIZEOF * count); - if (inputRecordPtr == 0) { - throw new IOException("cannot allocate memory with JNI"); - } - res = peek - ? PeekConsoleInputW(handle, inputRecordPtr, count, length) - : ReadConsoleInputW(handle, inputRecordPtr, count, length); - if (res == 0) { - throw new IOException("ReadConsoleInputW failed: " + WindowsSupport.getLastErrorMessage()); - } - if (length[0] <= 0) { - return new INPUT_RECORD[0]; - } - INPUT_RECORD[] records = new INPUT_RECORD[length[0]]; - for (int i = 0; i < records.length; i++) { - records[i] = new INPUT_RECORD(); - INPUT_RECORD.memmove(records[i], inputRecordPtr + i * INPUT_RECORD.SIZEOF, INPUT_RECORD.SIZEOF); - } - return records; - } finally { - if (inputRecordPtr != 0) { - free(inputRecordPtr); - } - } - } - - /** - * Return console input key events (discard other events). - * - * @param count requested number of events - * @return array possibly of size smaller then count - */ - public static INPUT_RECORD[] readConsoleKeyInput(long handle, int count, boolean peek) throws IOException { - while (true) { - // read events until we have keyboard events, the queue could be full - // of mouse events. - INPUT_RECORD[] evts = readConsoleInputHelper(handle, count, peek); - int keyEvtCount = 0; - for (INPUT_RECORD evt : evts) { - if (evt.eventType == INPUT_RECORD.KEY_EVENT) keyEvtCount++; - } - if (keyEvtCount > 0) { - INPUT_RECORD[] res = new INPUT_RECORD[keyEvtCount]; - int i = 0; - for (INPUT_RECORD evt : evts) { - if (evt.eventType == INPUT_RECORD.KEY_EVENT) { - res[i++] = evt; - } - } - return res; - } - } - } -} diff --git a/src/main/java/org/fusesource/jansi/internal/OSInfo.java b/src/main/java/org/fusesource/jansi/internal/OSInfo.java deleted file mode 100644 index 8c9999ca..00000000 --- a/src/main/java/org/fusesource/jansi/internal/OSInfo.java +++ /dev/null @@ -1,231 +0,0 @@ -/* - * Copyright (C) 2009-2023 the original author(s). - * - * 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 org.fusesource.jansi.internal; - -/*-------------------------------------------------------------------------- - * Copyright 2008 Taro L. Saito - * - * 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. - *--------------------------------------------------------------------------*/ - -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.util.HashMap; -import java.util.Locale; - -/** - * Provides OS name and architecture name. - * - */ -public class OSInfo { - - public static final String X86 = "x86"; - public static final String X86_64 = "x86_64"; - public static final String IA64_32 = "ia64_32"; - public static final String IA64 = "ia64"; - public static final String PPC = "ppc"; - public static final String PPC64 = "ppc64"; - public static final String ARM64 = "arm64"; - - private static final HashMap archMapping = new HashMap(); - - static { - // x86 mappings - archMapping.put(X86, X86); - archMapping.put("i386", X86); - archMapping.put("i486", X86); - archMapping.put("i586", X86); - archMapping.put("i686", X86); - archMapping.put("pentium", X86); - - // x86_64 mappings - archMapping.put(X86_64, X86_64); - archMapping.put("amd64", X86_64); - archMapping.put("em64t", X86_64); - archMapping.put("universal", X86_64); // Needed for openjdk7 in Mac - - // Itenium 64-bit mappings - archMapping.put(IA64, IA64); - archMapping.put("ia64w", IA64); - - // Itenium 32-bit mappings, usually an HP-UX construct - archMapping.put(IA64_32, IA64_32); - archMapping.put("ia64n", IA64_32); - - // PowerPC mappings - archMapping.put(PPC, PPC); - archMapping.put("power", PPC); - archMapping.put("powerpc", PPC); - archMapping.put("power_pc", PPC); - archMapping.put("power_rs", PPC); - - // TODO: PowerPC 64bit mappings - archMapping.put(PPC64, PPC64); - archMapping.put("power64", PPC64); - archMapping.put("powerpc64", PPC64); - archMapping.put("power_pc64", PPC64); - archMapping.put("power_rs64", PPC64); - - // aarch64 mappings - archMapping.put("aarch64", ARM64); - } - - public static void main(String[] args) { - if (args.length >= 1) { - if ("--os".equals(args[0])) { - System.out.print(getOSName()); - return; - } else if ("--arch".equals(args[0])) { - System.out.print(getArchName()); - return; - } - } - - System.out.print(getNativeLibFolderPathForCurrentOS()); - } - - public static String getNativeLibFolderPathForCurrentOS() { - return getOSName() + "/" + getArchName(); - } - - public static String getOSName() { - return translateOSNameToFolderName(System.getProperty("os.name")); - } - - public static boolean isAndroid() { - return System.getProperty("java.runtime.name", "").toLowerCase().contains("android"); - } - - public static boolean isAlpine() { - try { - Process p = Runtime.getRuntime().exec("cat /etc/os-release | grep ^ID"); - p.waitFor(); - - InputStream in = p.getInputStream(); - try { - return readFully(in).toLowerCase().contains("alpine"); - } finally { - in.close(); - } - - } catch (Throwable e) { - return false; - } - } - - static String getHardwareName() { - try { - Process p = Runtime.getRuntime().exec("uname -m"); - p.waitFor(); - - InputStream in = p.getInputStream(); - try { - return readFully(in); - } finally { - in.close(); - } - } catch (Throwable e) { - System.err.println("Error while running uname -m: " + e.getMessage()); - return "unknown"; - } - } - - private static String readFully(InputStream in) throws IOException { - int readLen = 0; - ByteArrayOutputStream b = new ByteArrayOutputStream(); - byte[] buf = new byte[32]; - while ((readLen = in.read(buf, 0, buf.length)) >= 0) { - b.write(buf, 0, readLen); - } - return b.toString(); - } - - static String resolveArmArchType() { - if (System.getProperty("os.name").contains("Linux")) { - String armType = getHardwareName(); - // armType (uname -m) can be armv5t, armv5te, armv5tej, armv5tejl, armv6, armv7, armv7l, aarch64, i686 - if (armType.startsWith("armv6")) { - // Raspberry PI - return "armv6"; - } else if (armType.startsWith("armv7")) { - // Generic - return "armv7"; - } else if (armType.startsWith("armv5")) { - // Use armv5, soft-float ABI - return "arm"; - } else if (armType.equals("aarch64")) { - // Use arm64 - return "arm64"; - } - - // Java 1.8 introduces a system property to determine armel or armhf - // http://bugs.java.com/bugdatabase/view_bug.do?bug_id=8005545 - String abi = System.getProperty("sun.arch.abi"); - if (abi != null && abi.startsWith("gnueabihf")) { - return "armv7"; - } - } - // Use armv5, soft-float ABI - return "arm"; - } - - public static String getArchName() { - String osArch = System.getProperty("os.arch"); - // For Android - if (isAndroid()) { - return "android-arm"; - } - - if (osArch.startsWith("arm")) { - osArch = resolveArmArchType(); - } else { - String lc = osArch.toLowerCase(Locale.US); - if (archMapping.containsKey(lc)) return archMapping.get(lc); - } - return translateArchNameToFolderName(osArch); - } - - static String translateOSNameToFolderName(String osName) { - if (osName.contains("Windows")) { - return "Windows"; - } else if (osName.contains("Mac") || osName.contains("Darwin")) { - return "Mac"; - // } else if (isAlpine()) { - // return "Linux-Alpine"; - } else if (osName.contains("Linux")) { - return "Linux"; - } else if (osName.contains("AIX")) { - return "AIX"; - } else { - return osName.replaceAll("\\W", ""); - } - } - - static String translateArchNameToFolderName(String archName) { - return archName.replaceAll("\\W", ""); - } -} diff --git a/src/main/java/org/fusesource/jansi/io/AnsiOutputStream.java b/src/main/java/org/fusesource/jansi/io/AnsiOutputStream.java index d925d728..9c45afef 100644 --- a/src/main/java/org/fusesource/jansi/io/AnsiOutputStream.java +++ b/src/main/java/org/fusesource/jansi/io/AnsiOutputStream.java @@ -25,6 +25,8 @@ import org.fusesource.jansi.AnsiMode; import org.fusesource.jansi.AnsiType; +import static java.nio.charset.StandardCharsets.US_ASCII; + /** * A ANSI print stream extracts ANSI escape codes written to * an output stream and calls corresponding AnsiProcessor.process* methods. @@ -38,12 +40,14 @@ */ public class AnsiOutputStream extends FilterOutputStream { - public static final byte[] RESET_CODE = "\033[0m".getBytes(); + public static final byte[] RESET_CODE = "\033[0m".getBytes(US_ASCII); + @FunctionalInterface public interface IoRunnable { void run() throws IOException; } + @FunctionalInterface public interface WidthSupplier { int getTerminalWidth(); } @@ -79,7 +83,7 @@ public int getTerminalWidth() { private final byte[] buffer = new byte[MAX_ESCAPE_SEQUENCE_LENGTH]; private int pos = 0; private int startOfValue; - private final ArrayList options = new ArrayList(); + private final ArrayList options = new ArrayList<>(); private int state = LOOKING_FOR_FIRST_ESC_CHAR; private final Charset cs; diff --git a/src/main/java/org/fusesource/jansi/io/FastBufferedOutputStream.java b/src/main/java/org/fusesource/jansi/io/FastBufferedOutputStream.java index 823e8019..e436c35e 100644 --- a/src/main/java/org/fusesource/jansi/io/FastBufferedOutputStream.java +++ b/src/main/java/org/fusesource/jansi/io/FastBufferedOutputStream.java @@ -24,7 +24,7 @@ */ public class FastBufferedOutputStream extends FilterOutputStream { - protected final byte buf[] = new byte[8192]; + protected final byte[] buf = new byte[8192]; protected int count; public FastBufferedOutputStream(OutputStream out) { diff --git a/src/main/java/org/fusesource/jansi/io/WindowsAnsiProcessor.java b/src/main/java/org/fusesource/jansi/io/WindowsAnsiProcessor.java index 74a178a5..0b51d0fb 100644 --- a/src/main/java/org/fusesource/jansi/io/WindowsAnsiProcessor.java +++ b/src/main/java/org/fusesource/jansi/io/WindowsAnsiProcessor.java @@ -18,31 +18,6 @@ import java.io.IOException; import java.io.OutputStream; -import org.fusesource.jansi.WindowsSupport; -import org.fusesource.jansi.internal.Kernel32.CONSOLE_SCREEN_BUFFER_INFO; -import org.fusesource.jansi.internal.Kernel32.COORD; - -import static org.fusesource.jansi.internal.Kernel32.BACKGROUND_BLUE; -import static org.fusesource.jansi.internal.Kernel32.BACKGROUND_GREEN; -import static org.fusesource.jansi.internal.Kernel32.BACKGROUND_INTENSITY; -import static org.fusesource.jansi.internal.Kernel32.BACKGROUND_RED; -import static org.fusesource.jansi.internal.Kernel32.CHAR_INFO; -import static org.fusesource.jansi.internal.Kernel32.FOREGROUND_BLUE; -import static org.fusesource.jansi.internal.Kernel32.FOREGROUND_GREEN; -import static org.fusesource.jansi.internal.Kernel32.FOREGROUND_INTENSITY; -import static org.fusesource.jansi.internal.Kernel32.FOREGROUND_RED; -import static org.fusesource.jansi.internal.Kernel32.FillConsoleOutputAttribute; -import static org.fusesource.jansi.internal.Kernel32.FillConsoleOutputCharacterW; -import static org.fusesource.jansi.internal.Kernel32.GetConsoleScreenBufferInfo; -import static org.fusesource.jansi.internal.Kernel32.GetStdHandle; -import static org.fusesource.jansi.internal.Kernel32.SMALL_RECT; -import static org.fusesource.jansi.internal.Kernel32.STD_ERROR_HANDLE; -import static org.fusesource.jansi.internal.Kernel32.STD_OUTPUT_HANDLE; -import static org.fusesource.jansi.internal.Kernel32.ScrollConsoleScreenBuffer; -import static org.fusesource.jansi.internal.Kernel32.SetConsoleCursorPosition; -import static org.fusesource.jansi.internal.Kernel32.SetConsoleTextAttribute; -import static org.fusesource.jansi.internal.Kernel32.SetConsoleTitle; - /** * A Windows ANSI escape processor, that uses JNA to access native platform * API's to change the console attributes (see @@ -51,374 +26,20 @@ * Library * * @since 1.19 + * @author Hiram Chirino + * @author Joris Kuipers */ public final class WindowsAnsiProcessor extends AnsiProcessor { - private final long console; - - private static final short FOREGROUND_BLACK = 0; - private static final short FOREGROUND_YELLOW = (short) (FOREGROUND_RED | FOREGROUND_GREEN); - private static final short FOREGROUND_MAGENTA = (short) (FOREGROUND_BLUE | FOREGROUND_RED); - private static final short FOREGROUND_CYAN = (short) (FOREGROUND_BLUE | FOREGROUND_GREEN); - private static final short FOREGROUND_WHITE = (short) (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); - - private static final short BACKGROUND_BLACK = 0; - private static final short BACKGROUND_YELLOW = (short) (BACKGROUND_RED | BACKGROUND_GREEN); - private static final short BACKGROUND_MAGENTA = (short) (BACKGROUND_BLUE | BACKGROUND_RED); - private static final short BACKGROUND_CYAN = (short) (BACKGROUND_BLUE | BACKGROUND_GREEN); - private static final short BACKGROUND_WHITE = (short) (BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE); - - private static final short[] ANSI_FOREGROUND_COLOR_MAP = { - FOREGROUND_BLACK, - FOREGROUND_RED, - FOREGROUND_GREEN, - FOREGROUND_YELLOW, - FOREGROUND_BLUE, - FOREGROUND_MAGENTA, - FOREGROUND_CYAN, - FOREGROUND_WHITE, - }; - - private static final short[] ANSI_BACKGROUND_COLOR_MAP = { - BACKGROUND_BLACK, - BACKGROUND_RED, - BACKGROUND_GREEN, - BACKGROUND_YELLOW, - BACKGROUND_BLUE, - BACKGROUND_MAGENTA, - BACKGROUND_CYAN, - BACKGROUND_WHITE, - }; - - private final CONSOLE_SCREEN_BUFFER_INFO info = new CONSOLE_SCREEN_BUFFER_INFO(); - private final short originalColors; - - private boolean negative; - private short savedX = -1; - private short savedY = -1; - public WindowsAnsiProcessor(OutputStream ps, long console) throws IOException { super(ps); - this.console = console; - getConsoleInfo(); - originalColors = info.attributes; } public WindowsAnsiProcessor(OutputStream ps, boolean stdout) throws IOException { - this(ps, GetStdHandle(stdout ? STD_OUTPUT_HANDLE : STD_ERROR_HANDLE)); + super(ps); } public WindowsAnsiProcessor(OutputStream ps) throws IOException { - this(ps, true); - } - - private void getConsoleInfo() throws IOException { - os.flush(); - if (GetConsoleScreenBufferInfo(console, info) == 0) { - throw new IOException("Could not get the screen info: " + WindowsSupport.getLastErrorMessage()); - } - if (negative) { - info.attributes = invertAttributeColors(info.attributes); - } - } - - private void applyAttribute() throws IOException { - os.flush(); - short attributes = info.attributes; - if (negative) { - attributes = invertAttributeColors(attributes); - } - if (SetConsoleTextAttribute(console, attributes) == 0) { - throw new IOException(WindowsSupport.getLastErrorMessage()); - } - } - - private short invertAttributeColors(short attributes) { - // Swap the the Foreground and Background bits. - int fg = 0x000F & attributes; - fg <<= 4; - int bg = 0X00F0 & attributes; - bg >>= 4; - attributes = (short) ((attributes & 0xFF00) | fg | bg); - return attributes; - } - - private void applyCursorPosition() throws IOException { - if (SetConsoleCursorPosition(console, info.cursorPosition.copy()) == 0) { - throw new IOException(WindowsSupport.getLastErrorMessage()); - } - } - - @Override - protected void processEraseScreen(int eraseOption) throws IOException { - getConsoleInfo(); - int[] written = new int[1]; - switch (eraseOption) { - case ERASE_SCREEN: - COORD topLeft = new COORD(); - topLeft.x = 0; - topLeft.y = info.window.top; - int screenLength = info.window.height() * info.size.x; - FillConsoleOutputAttribute(console, info.attributes, screenLength, topLeft, written); - FillConsoleOutputCharacterW(console, ' ', screenLength, topLeft, written); - break; - case ERASE_SCREEN_TO_BEGINING: - COORD topLeft2 = new COORD(); - topLeft2.x = 0; - topLeft2.y = info.window.top; - int lengthToCursor = (info.cursorPosition.y - info.window.top) * info.size.x + info.cursorPosition.x; - FillConsoleOutputAttribute(console, info.attributes, lengthToCursor, topLeft2, written); - FillConsoleOutputCharacterW(console, ' ', lengthToCursor, topLeft2, written); - break; - case ERASE_SCREEN_TO_END: - int lengthToEnd = (info.window.bottom - info.cursorPosition.y) * info.size.x - + (info.size.x - info.cursorPosition.x); - FillConsoleOutputAttribute(console, info.attributes, lengthToEnd, info.cursorPosition.copy(), written); - FillConsoleOutputCharacterW(console, ' ', lengthToEnd, info.cursorPosition.copy(), written); - break; - default: - break; - } - } - - @Override - protected void processEraseLine(int eraseOption) throws IOException { - getConsoleInfo(); - int[] written = new int[1]; - switch (eraseOption) { - case ERASE_LINE: - COORD leftColCurrRow = info.cursorPosition.copy(); - leftColCurrRow.x = 0; - FillConsoleOutputAttribute(console, info.attributes, info.size.x, leftColCurrRow, written); - FillConsoleOutputCharacterW(console, ' ', info.size.x, leftColCurrRow, written); - break; - case ERASE_LINE_TO_BEGINING: - COORD leftColCurrRow2 = info.cursorPosition.copy(); - leftColCurrRow2.x = 0; - FillConsoleOutputAttribute(console, info.attributes, info.cursorPosition.x, leftColCurrRow2, written); - FillConsoleOutputCharacterW(console, ' ', info.cursorPosition.x, leftColCurrRow2, written); - break; - case ERASE_LINE_TO_END: - int lengthToLastCol = info.size.x - info.cursorPosition.x; - FillConsoleOutputAttribute( - console, info.attributes, lengthToLastCol, info.cursorPosition.copy(), written); - FillConsoleOutputCharacterW(console, ' ', lengthToLastCol, info.cursorPosition.copy(), written); - break; - default: - break; - } - } - - @Override - protected void processCursorLeft(int count) throws IOException { - getConsoleInfo(); - info.cursorPosition.x = (short) Math.max(0, info.cursorPosition.x - count); - applyCursorPosition(); - } - - @Override - protected void processCursorRight(int count) throws IOException { - getConsoleInfo(); - info.cursorPosition.x = (short) Math.min(info.window.width(), info.cursorPosition.x + count); - applyCursorPosition(); - } - - @Override - protected void processCursorDown(int count) throws IOException { - getConsoleInfo(); - info.cursorPosition.y = (short) Math.min(Math.max(0, info.size.y - 1), info.cursorPosition.y + count); - applyCursorPosition(); - } - - @Override - protected void processCursorUp(int count) throws IOException { - getConsoleInfo(); - info.cursorPosition.y = (short) Math.max(info.window.top, info.cursorPosition.y - count); - applyCursorPosition(); - } - - @Override - protected void processCursorTo(int row, int col) throws IOException { - getConsoleInfo(); - info.cursorPosition.y = (short) Math.max(info.window.top, Math.min(info.size.y, info.window.top + row - 1)); - info.cursorPosition.x = (short) Math.max(0, Math.min(info.window.width(), col - 1)); - applyCursorPosition(); - } - - @Override - protected void processCursorToColumn(int x) throws IOException { - getConsoleInfo(); - info.cursorPosition.x = (short) Math.max(0, Math.min(info.window.width(), x - 1)); - applyCursorPosition(); - } - - @Override - protected void processCursorUpLine(int count) throws IOException { - getConsoleInfo(); - info.cursorPosition.x = 0; - info.cursorPosition.y = (short) Math.max(info.window.top, info.cursorPosition.y - count); - applyCursorPosition(); - } - - @Override - protected void processCursorDownLine(int count) throws IOException { - getConsoleInfo(); - info.cursorPosition.x = 0; - info.cursorPosition.y = (short) Math.max(info.window.top, info.cursorPosition.y + count); - applyCursorPosition(); - } - - @Override - protected void processSetForegroundColor(int color, boolean bright) throws IOException { - info.attributes = (short) ((info.attributes & ~0x0007) | ANSI_FOREGROUND_COLOR_MAP[color]); - if (bright) { - info.attributes |= FOREGROUND_INTENSITY; - } - applyAttribute(); - } - - @Override - protected void processSetForegroundColorExt(int paletteIndex) throws IOException { - int round = Colors.roundColor(paletteIndex, 16); - processSetForegroundColor(round >= 8 ? round - 8 : round, round >= 8); - } - - @Override - protected void processSetForegroundColorExt(int r, int g, int b) throws IOException { - int round = Colors.roundRgbColor(r, g, b, 16); - processSetForegroundColor(round >= 8 ? round - 8 : round, round >= 8); - } - - @Override - protected void processSetBackgroundColor(int color, boolean bright) throws IOException { - info.attributes = (short) ((info.attributes & ~0x0070) | ANSI_BACKGROUND_COLOR_MAP[color]); - if (bright) { - info.attributes |= BACKGROUND_INTENSITY; - } - applyAttribute(); - } - - @Override - protected void processSetBackgroundColorExt(int paletteIndex) throws IOException { - int round = Colors.roundColor(paletteIndex, 16); - processSetBackgroundColor(round >= 8 ? round - 8 : round, round >= 8); - } - - @Override - protected void processSetBackgroundColorExt(int r, int g, int b) throws IOException { - int round = Colors.roundRgbColor(r, g, b, 16); - processSetBackgroundColor(round >= 8 ? round - 8 : round, round >= 8); - } - - @Override - protected void processDefaultTextColor() throws IOException { - info.attributes = (short) ((info.attributes & ~0x000F) | (originalColors & 0xF)); - info.attributes = (short) (info.attributes & ~FOREGROUND_INTENSITY); - applyAttribute(); - } - - @Override - protected void processDefaultBackgroundColor() throws IOException { - info.attributes = (short) ((info.attributes & ~0x00F0) | (originalColors & 0xF0)); - info.attributes = (short) (info.attributes & ~BACKGROUND_INTENSITY); - applyAttribute(); - } - - @Override - protected void processAttributeReset() throws IOException { - info.attributes = (short) ((info.attributes & ~0x00FF) | originalColors); - this.negative = false; - applyAttribute(); - } - - @Override - protected void processSetAttribute(int attribute) throws IOException { - switch (attribute) { - case ATTRIBUTE_INTENSITY_BOLD: - info.attributes = (short) (info.attributes | FOREGROUND_INTENSITY); - applyAttribute(); - break; - case ATTRIBUTE_INTENSITY_NORMAL: - info.attributes = (short) (info.attributes & ~FOREGROUND_INTENSITY); - applyAttribute(); - 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 | BACKGROUND_INTENSITY); - applyAttribute(); - break; - case ATTRIBUTE_UNDERLINE_OFF: - info.attributes = (short) (info.attributes & ~BACKGROUND_INTENSITY); - applyAttribute(); - break; - - case ATTRIBUTE_NEGATIVE_ON: - negative = true; - applyAttribute(); - break; - case ATTRIBUTE_NEGATIVE_OFF: - negative = false; - applyAttribute(); - break; - default: - break; - } - } - - @Override - protected void processSaveCursorPosition() throws IOException { - getConsoleInfo(); - savedX = info.cursorPosition.x; - savedY = info.cursorPosition.y; - } - - @Override - protected void processRestoreCursorPosition() throws IOException { - // restore only if there was a save operation first - if (savedX != -1 && savedY != -1) { - os.flush(); - info.cursorPosition.x = savedX; - info.cursorPosition.y = savedY; - applyCursorPosition(); - } - } - - @Override - protected void processInsertLine(int optionInt) throws IOException { - getConsoleInfo(); - SMALL_RECT scroll = info.window.copy(); - scroll.top = info.cursorPosition.y; - COORD org = new COORD(); - org.x = 0; - org.y = (short) (info.cursorPosition.y + optionInt); - CHAR_INFO info = new CHAR_INFO(); - info.attributes = originalColors; - info.unicodeChar = ' '; - if (ScrollConsoleScreenBuffer(console, scroll, scroll, org, info) == 0) { - throw new IOException(WindowsSupport.getLastErrorMessage()); - } - } - - @Override - protected void processDeleteLine(int optionInt) throws IOException { - getConsoleInfo(); - SMALL_RECT scroll = info.window.copy(); - scroll.top = info.cursorPosition.y; - COORD org = new COORD(); - org.x = 0; - org.y = (short) (info.cursorPosition.y - optionInt); - CHAR_INFO info = new CHAR_INFO(); - info.attributes = originalColors; - info.unicodeChar = ' '; - if (ScrollConsoleScreenBuffer(console, scroll, scroll, org, info) == 0) { - throw new IOException(WindowsSupport.getLastErrorMessage()); - } - } - - @Override - protected void processChangeWindowTitle(String label) { - SetConsoleTitle(label); + super(ps); } } diff --git a/src/main/native/jansi.c b/src/main/native/jansi.c deleted file mode 100644 index 2bd59f26..00000000 --- a/src/main/native/jansi.c +++ /dev/null @@ -1,602 +0,0 @@ -/******************************************************************************* - * Copyright (C) 2009-2017 the original author(s). - * - * 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. - *******************************************************************************/ -#include "jansi.h" -#include "jansi_structs.h" - -JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) { - return JNI_VERSION_1_2; -} - -#define CLibrary_NATIVE(func) Java_org_fusesource_jansi_internal_CLibrary_##func - -JNIEXPORT void JNICALL CLibrary_NATIVE(init)(JNIEnv *env, jclass that) -{ -#if defined(HAVE_ISATTY) - (*env)->SetStaticBooleanField(env, that, (*env)->GetStaticFieldID(env, that, "HAVE_ISATTY", "Z"), (jboolean)1); -#endif -#if defined(HAVE_TTYNAME) - (*env)->SetStaticBooleanField(env, that, (*env)->GetStaticFieldID(env, that, "HAVE_TTYNAME", "Z"), (jboolean)1); -#endif -#if defined(TCSANOW) - (*env)->SetStaticIntField(env, that, (*env)->GetStaticFieldID(env, that, "TCSANOW", "I"), (jint)TCSANOW); -#endif -#if defined(TCSADRAIN) - (*env)->SetStaticIntField(env, that, (*env)->GetStaticFieldID(env, that, "TCSADRAIN", "I"), (jint)TCSADRAIN); -#endif -#if defined(TCSAFLUSH) - (*env)->SetStaticIntField(env, that, (*env)->GetStaticFieldID(env, that, "TCSAFLUSH", "I"), (jint)TCSAFLUSH); -#endif -#if defined(TIOCGETA) - (*env)->SetStaticLongField(env, that, (*env)->GetStaticFieldID(env, that, "TIOCGETA", "J"), (jlong)TIOCGETA); -#endif -#if defined(TIOCSETA) - (*env)->SetStaticLongField(env, that, (*env)->GetStaticFieldID(env, that, "TIOCSETA", "J"), (jlong)TIOCSETA); -#endif -#if defined(TIOCGETD) - (*env)->SetStaticLongField(env, that, (*env)->GetStaticFieldID(env, that, "TIOCGETD", "J"), (jlong)TIOCGETD); -#endif -#if defined(TIOCSETD) - (*env)->SetStaticLongField(env, that, (*env)->GetStaticFieldID(env, that, "TIOCSETD", "J"), (jlong)TIOCSETD); -#endif -#if defined(TIOCGWINSZ) - (*env)->SetStaticLongField(env, that, (*env)->GetStaticFieldID(env, that, "TIOCGWINSZ", "J"), (jlong)TIOCGWINSZ); -#endif -#if defined(TIOCSWINSZ) - (*env)->SetStaticLongField(env, that, (*env)->GetStaticFieldID(env, that, "TIOCSWINSZ", "J"), (jlong)TIOCSWINSZ); -#endif - return; -} - -#if defined(HAVE_IOCTL) -JNIEXPORT jint JNICALL CLibrary_NATIVE(ioctl__IJLorg_fusesource_jansi_internal_CLibrary_00024WinSize_2) - (JNIEnv *env, jclass that, jint arg0, jlong arg1, jobject arg2) -{ - struct winsize _arg2, *lparg2=NULL; - jint rc = 0; - - if (arg2) if ((lparg2 = getWinSizeFields(env, arg2, &_arg2)) == NULL) goto fail; - rc = (jint)ioctl(arg0, arg1, (intptr_t)lparg2); -fail: - if (arg2 && lparg2) setWinSizeFields(env, arg2, lparg2); - - return rc; -} - -JNIEXPORT jint JNICALL CLibrary_NATIVE(ioctl__IJ_3I) - (JNIEnv *env, jclass that, jint arg0, jlong arg1, jintArray arg2) -{ - jint *lparg2=NULL; - jint rc = 0; - - if (arg2) if ((lparg2 = (*env)->GetIntArrayElements(env, arg2, NULL)) == NULL) goto fail; - rc = (jint)ioctl(arg0, arg1, lparg2); -fail: - if (arg2 && lparg2) (*env)->ReleaseIntArrayElements(env, arg2, lparg2, 0); - - return rc; -} -#endif - -#if defined(HAVE_OPENPTY) -JNIEXPORT jint JNICALL CLibrary_NATIVE(openpty) - (JNIEnv *env, jclass that, jintArray arg0, jintArray arg1, jbyteArray arg2, jobject arg3, jobject arg4) -{ - jint *lparg0=NULL; - jint *lparg1=NULL; - jbyte *lparg2=NULL; - struct termios _arg3, *lparg3=NULL; - struct winsize _arg4, *lparg4=NULL; - jint rc = 0; - - if (arg0) if ((lparg0 = (*env)->GetIntArrayElements(env, arg0, NULL)) == NULL) goto fail; - if (arg1) if ((lparg1 = (*env)->GetIntArrayElements(env, arg1, NULL)) == NULL) goto fail; - if (arg2) if ((lparg2 = (*env)->GetByteArrayElements(env, arg2, NULL)) == NULL) goto fail; - if (arg3) if ((lparg3 = getTermiosFields(env, arg3, &_arg3)) == NULL) goto fail; - if (arg4) if ((lparg4 = getWinSizeFields(env, arg4, &_arg4)) == NULL) goto fail; - rc = (jint)openpty((int *)lparg0, (int *)lparg1, (char *)lparg2, (struct termios *)lparg3, (struct winsize *)lparg4); -fail: - if (arg2 && lparg2) (*env)->ReleaseByteArrayElements(env, arg2, lparg2, 0); - if (arg1 && lparg1) (*env)->ReleaseIntArrayElements(env, arg1, lparg1, 0); - if (arg0 && lparg0) (*env)->ReleaseIntArrayElements(env, arg0, lparg0, 0); - - return rc; -} -#endif - -#if defined(HAVE_TCGETATTR) -JNIEXPORT jint JNICALL CLibrary_NATIVE(tcgetattr) - (JNIEnv *env, jclass that, jint arg0, jobject arg1) -{ - struct termios _arg1, *lparg1=NULL; - jint rc = 0; - - if (arg1) if ((lparg1 = &_arg1) == NULL) goto fail; - rc = (jint)tcgetattr(arg0, (struct termios *)lparg1); -fail: - if (arg1 && lparg1) setTermiosFields(env, arg1, lparg1); - - return rc; -} -#endif - -#if defined(HAVE_TCSETATTR) -JNIEXPORT jint JNICALL CLibrary_NATIVE(tcsetattr) - (JNIEnv *env, jclass that, jint arg0, jint arg1, jobject arg2) -{ - struct termios _arg2, *lparg2=NULL; - jint rc = 0; - - if (arg2) if ((lparg2 = getTermiosFields(env, arg2, &_arg2)) == NULL) goto fail; - rc = (jint)tcsetattr(arg0, arg1, (struct termios *)lparg2); -fail: - - return rc; -} -#endif - -#define Termios_NATIVE(func) Java_org_fusesource_jansi_internal_CLibrary_00024Termios_##func - -JNIEXPORT void JNICALL Termios_NATIVE(init)(JNIEnv *env, jclass that) -{ -#if defined(HAVE_IOCTL) - (*env)->SetStaticIntField(env, that, (*env)->GetStaticFieldID(env, that, "SIZEOF", "I"), (jint)sizeof(struct termios)); -#endif - return; -} - -#define WinSize_NATIVE(func) Java_org_fusesource_jansi_internal_CLibrary_00024WinSize_##func - -JNIEXPORT void JNICALL WinSize_NATIVE(init)(JNIEnv *env, jclass that) -{ -#if defined(HAVE_IOCTL) - (*env)->SetStaticIntField(env, that, (*env)->GetStaticFieldID(env, that, "SIZEOF", "I"), (jint)sizeof(struct winsize)); -#endif - return; -} -#define Kernel32_NATIVE(func) Java_org_fusesource_jansi_internal_Kernel32_##func - -#if defined(_WIN32) || defined(_WIN64) -JNIEXPORT jint JNICALL Kernel32_NATIVE(CloseHandle) - (JNIEnv *env, jclass that, jlong arg0) -{ - jint rc = 0; - rc = (jint)CloseHandle((HANDLE)arg0); - return rc; -} - -JNIEXPORT jint JNICALL Kernel32_NATIVE(FillConsoleOutputAttribute) - (JNIEnv *env, jclass that, jlong arg0, jshort arg1, jint arg2, jobject arg3, jintArray arg4) -{ - COORD _arg3, *lparg3=NULL; - jint *lparg4=NULL; - jint rc = 0; - if (arg3) if ((lparg3 = getCOORDFields(env, arg3, &_arg3)) == NULL) goto fail; - if (arg4) if ((lparg4 = (*env)->GetIntArrayElements(env, arg4, NULL)) == NULL) goto fail; - rc = (jint)FillConsoleOutputAttribute((HANDLE)(intptr_t)arg0, arg1, arg2, *lparg3, lparg4); -fail: - if (arg4 && lparg4) (*env)->ReleaseIntArrayElements(env, arg4, lparg4, 0); - return rc; -} - -JNIEXPORT jint JNICALL Kernel32_NATIVE(FillConsoleOutputCharacterW) - (JNIEnv *env, jclass that, jlong arg0, jchar arg1, jint arg2, jobject arg3, jintArray arg4) -{ - COORD _arg3, *lparg3=NULL; - jint *lparg4=NULL; - jint rc = 0; - if (arg3) if ((lparg3 = getCOORDFields(env, arg3, &_arg3)) == NULL) goto fail; - if (arg4) if ((lparg4 = (*env)->GetIntArrayElements(env, arg4, NULL)) == NULL) goto fail; - rc = (jint)FillConsoleOutputCharacterW((HANDLE)(intptr_t)arg0, arg1, arg2, *lparg3, lparg4); -fail: - if (arg4 && lparg4) (*env)->ReleaseIntArrayElements(env, arg4, lparg4, 0); - return rc; -} - -JNIEXPORT jint JNICALL Kernel32_NATIVE(FlushConsoleInputBuffer) - (JNIEnv *env, jclass that, jlong arg0) -{ - jint rc = 0; - rc = (jint)FlushConsoleInputBuffer((HANDLE)(intptr_t)arg0); - return rc; -} - -JNIEXPORT jint JNICALL Kernel32_NATIVE(FormatMessageW) - (JNIEnv *env, jclass that, jint arg0, jlong arg1, jint arg2, jint arg3, jbyteArray arg4, jint arg5, jlongArray arg6) -{ - jbyte *lparg4=NULL; - jlong *lparg6=NULL; - jint rc = 0; - if (arg4) if ((lparg4 = (*env)->GetPrimitiveArrayCritical(env, arg4, NULL)) == NULL) goto fail; - if (arg6) if ((lparg6 = (*env)->GetPrimitiveArrayCritical(env, arg6, NULL)) == NULL) goto fail; - rc = (jint)FormatMessageW(arg0, (void *)(intptr_t)arg1, arg2, arg3, (void *)lparg4, arg5, (void *)NULL); -fail: - if (arg6 && lparg6) (*env)->ReleasePrimitiveArrayCritical(env, arg6, lparg6, 0); - if (arg4 && lparg4) (*env)->ReleasePrimitiveArrayCritical(env, arg4, lparg4, 0); - return rc; -} - -JNIEXPORT jint JNICALL Kernel32_NATIVE(GetConsoleMode) - (JNIEnv *env, jclass that, jlong arg0, jintArray arg1) -{ - jint *lparg1=NULL; - jint rc = 0; - if (arg1) if ((lparg1 = (*env)->GetIntArrayElements(env, arg1, NULL)) == NULL) goto fail; - rc = (jint)GetConsoleMode((HANDLE)(intptr_t)arg0, lparg1); -fail: - if (arg1 && lparg1) (*env)->ReleaseIntArrayElements(env, arg1, lparg1, 0); - return rc; -} - -JNIEXPORT jint JNICALL Kernel32_NATIVE(GetConsoleOutputCP) - (JNIEnv *env, jclass that) -{ - jint rc = 0; - rc = (jint)GetConsoleOutputCP(); - return rc; -} - -JNIEXPORT jint JNICALL Kernel32_NATIVE(GetConsoleScreenBufferInfo) - (JNIEnv *env, jclass that, jlong arg0, jobject arg1) -{ - CONSOLE_SCREEN_BUFFER_INFO _arg1, *lparg1=NULL; - jint rc = 0; - if (arg1) if ((lparg1 = &_arg1) == NULL) goto fail; - rc = (jint)GetConsoleScreenBufferInfo((HANDLE)(intptr_t)arg0, lparg1); -fail: - if (arg1 && lparg1) setCONSOLE_SCREEN_BUFFER_INFOFields(env, arg1, lparg1); - return rc; -} - -JNIEXPORT jint JNICALL Kernel32_NATIVE(GetLastError) - (JNIEnv *env, jclass that) -{ - jint rc = 0; - rc = (jint)GetLastError(); - return rc; -} - -JNIEXPORT jint JNICALL Kernel32_NATIVE(GetNumberOfConsoleInputEvents) - (JNIEnv *env, jclass that, jlong arg0, jintArray arg1) -{ - jint *lparg1=NULL; - jint rc = 0; - if (arg1) if ((lparg1 = (*env)->GetIntArrayElements(env, arg1, NULL)) == NULL) goto fail; - rc = (jint)GetNumberOfConsoleInputEvents((HANDLE)(intptr_t)arg0, lparg1); -fail: - if (arg1 && lparg1) (*env)->ReleaseIntArrayElements(env, arg1, lparg1, 0); - return rc; -} - -JNIEXPORT jlong JNICALL Kernel32_NATIVE(GetStdHandle) - (JNIEnv *env, jclass that, jint arg0) -{ - jlong rc = 0; - rc = (intptr_t)(HANDLE)GetStdHandle(arg0); - return rc; -} - -JNIEXPORT jint JNICALL Kernel32_NATIVE(PeekConsoleInputW) - (JNIEnv *env, jclass that, jlong arg0, jlong arg1, jint arg2, jintArray arg3) -{ - jint *lparg3=NULL; - jint rc = 0; - if (arg3) if ((lparg3 = (*env)->GetIntArrayElements(env, arg3, NULL)) == NULL) goto fail; - rc = (jint)PeekConsoleInputW((HANDLE)(intptr_t)arg0, (PINPUT_RECORD)(intptr_t)arg1, arg2, lparg3); -fail: - if (arg3 && lparg3) (*env)->ReleaseIntArrayElements(env, arg3, lparg3, 0); - return rc; -} - -JNIEXPORT jint JNICALL Kernel32_NATIVE(ReadConsoleInputW) - (JNIEnv *env, jclass that, jlong arg0, jlong arg1, jint arg2, jintArray arg3) -{ - jint *lparg3=NULL; - jint rc = 0; - if (arg3) if ((lparg3 = (*env)->GetIntArrayElements(env, arg3, NULL)) == NULL) goto fail; - rc = (jint)ReadConsoleInputW((HANDLE)(intptr_t)arg0, (PINPUT_RECORD)(intptr_t)arg1, arg2, lparg3); -fail: - if (arg3 && lparg3) (*env)->ReleaseIntArrayElements(env, arg3, lparg3, 0); - return rc; -} - -JNIEXPORT jint JNICALL Kernel32_NATIVE(ScrollConsoleScreenBuffer) - (JNIEnv *env, jclass that, jlong arg0, jobject arg1, jobject arg2, jobject arg3, jobject arg4) -{ - SMALL_RECT _arg1, *lparg1=NULL; - SMALL_RECT _arg2, *lparg2=NULL; - COORD _arg3, *lparg3=NULL; - CHAR_INFO _arg4, *lparg4=NULL; - jint rc = 0; - if (arg1) if ((lparg1 = getSMALL_RECTFields(env, arg1, &_arg1)) == NULL) goto fail; - if (arg2) if ((lparg2 = getSMALL_RECTFields(env, arg2, &_arg2)) == NULL) goto fail; - if (arg3) if ((lparg3 = getCOORDFields(env, arg3, &_arg3)) == NULL) goto fail; - if (arg4) if ((lparg4 = getCHAR_INFOFields(env, arg4, &_arg4)) == NULL) goto fail; - rc = (jint)ScrollConsoleScreenBuffer((HANDLE)(intptr_t)arg0, lparg1, lparg2, *lparg3, lparg4); -fail: - return rc; -} - -JNIEXPORT jint JNICALL Kernel32_NATIVE(SetConsoleCursorPosition) - (JNIEnv *env, jclass that, jlong arg0, jobject arg1) -{ - COORD _arg1, *lparg1=NULL; - jint rc = 0; - if (arg1) if ((lparg1 = getCOORDFields(env, arg1, &_arg1)) == NULL) goto fail; - rc = (jint)SetConsoleCursorPosition((HANDLE)(intptr_t)arg0, *lparg1); -fail: - return rc; -} - -JNIEXPORT jint JNICALL Kernel32_NATIVE(SetConsoleMode) - (JNIEnv *env, jclass that, jlong arg0, jint arg1) -{ - return (jint)SetConsoleMode((HANDLE)(intptr_t)arg0, arg1); -} - -JNIEXPORT jint JNICALL Kernel32_NATIVE(SetConsoleOutputCP) - (JNIEnv *env, jclass that, jint arg0) -{ - return (jint)SetConsoleOutputCP(arg0); -} - -JNIEXPORT jint JNICALL Kernel32_NATIVE(SetConsoleTextAttribute) - (JNIEnv *env, jclass that, jlong arg0, jshort arg1) -{ - return (jint)SetConsoleTextAttribute((HANDLE)arg0, arg1); -} - -JNIEXPORT jint JNICALL Kernel32_NATIVE(SetConsoleTitle) - (JNIEnv *env, jclass that, jstring arg0) -{ - const jchar *lparg0= NULL; - jint rc = 0; - if (arg0) if ((lparg0 = (*env)->GetStringChars(env, arg0, NULL)) == NULL) goto fail; - rc = (jint)SetConsoleTitle(lparg0); -fail: - if (arg0 && lparg0) (*env)->ReleaseStringChars(env, arg0, lparg0); - return rc; -} - -JNIEXPORT jint JNICALL Kernel32_NATIVE(WaitForSingleObject) - (JNIEnv *env, jclass that, jlong arg0, jint arg1) -{ - return (jint)WaitForSingleObject((HANDLE)arg0, arg1); -} - -JNIEXPORT jint JNICALL Kernel32_NATIVE(WriteConsoleW) - (JNIEnv *env, jclass that, jlong arg0, jcharArray arg1, jint arg2, jintArray arg3, jlong arg4) -{ - jchar *lparg1=NULL; - jint *lparg3=NULL; - jint rc = 0; - if (arg1) if ((lparg1 = (*env)->GetCharArrayElements(env, arg1, NULL)) == NULL) goto fail; - if (arg3) if ((lparg3 = (*env)->GetIntArrayElements(env, arg3, NULL)) == NULL) goto fail; - rc = (jint)WriteConsoleW((HANDLE)(intptr_t)arg0, lparg1, arg2, lparg3, (LPVOID)(intptr_t)arg4); -fail: - if (arg3 && lparg3) (*env)->ReleaseIntArrayElements(env, arg3, lparg3, 0); - if (arg1 && lparg1) (*env)->ReleaseCharArrayElements(env, arg1, lparg1, JNI_ABORT); - return rc; -} - -JNIEXPORT jint JNICALL Kernel32_NATIVE(_1getch) - (JNIEnv *env, jclass that) -{ - jint rc = 0; - rc = (jint)_getch(); - return rc; -} - -JNIEXPORT void JNICALL Kernel32_NATIVE(free) - (JNIEnv *env, jclass that, jlong arg0) -{ - free((void *)(intptr_t)arg0); -} -#endif - -JNIEXPORT void JNICALL Kernel32_NATIVE(init)(JNIEnv *env, jclass that) -{ -#if defined(_WIN32) || defined(_WIN64) - (*env)->SetStaticShortField(env, that, (*env)->GetStaticFieldID(env, that, "FOREGROUND_BLUE", "S"), (jshort)FOREGROUND_BLUE); - (*env)->SetStaticShortField(env, that, (*env)->GetStaticFieldID(env, that, "FOREGROUND_GREEN", "S"), (jshort)FOREGROUND_GREEN); - (*env)->SetStaticShortField(env, that, (*env)->GetStaticFieldID(env, that, "FOREGROUND_RED", "S"), (jshort)FOREGROUND_RED); - (*env)->SetStaticShortField(env, that, (*env)->GetStaticFieldID(env, that, "FOREGROUND_INTENSITY", "S"), (jshort)FOREGROUND_INTENSITY); - (*env)->SetStaticShortField(env, that, (*env)->GetStaticFieldID(env, that, "BACKGROUND_BLUE", "S"), (jshort)BACKGROUND_BLUE); - (*env)->SetStaticShortField(env, that, (*env)->GetStaticFieldID(env, that, "BACKGROUND_GREEN", "S"), (jshort)BACKGROUND_GREEN); - (*env)->SetStaticShortField(env, that, (*env)->GetStaticFieldID(env, that, "BACKGROUND_RED", "S"), (jshort)BACKGROUND_RED); - (*env)->SetStaticShortField(env, that, (*env)->GetStaticFieldID(env, that, "BACKGROUND_INTENSITY", "S"), (jshort)BACKGROUND_INTENSITY); - (*env)->SetStaticShortField(env, that, (*env)->GetStaticFieldID(env, that, "COMMON_LVB_LEADING_BYTE", "S"), (jshort)COMMON_LVB_LEADING_BYTE); - (*env)->SetStaticShortField(env, that, (*env)->GetStaticFieldID(env, that, "COMMON_LVB_TRAILING_BYTE", "S"), (jshort)COMMON_LVB_TRAILING_BYTE); - (*env)->SetStaticShortField(env, that, (*env)->GetStaticFieldID(env, that, "COMMON_LVB_GRID_HORIZONTAL", "S"), (jshort)COMMON_LVB_GRID_HORIZONTAL); - (*env)->SetStaticShortField(env, that, (*env)->GetStaticFieldID(env, that, "COMMON_LVB_GRID_LVERTICAL", "S"), (jshort)COMMON_LVB_GRID_LVERTICAL); - (*env)->SetStaticShortField(env, that, (*env)->GetStaticFieldID(env, that, "COMMON_LVB_GRID_RVERTICAL", "S"), (jshort)COMMON_LVB_GRID_RVERTICAL); - (*env)->SetStaticShortField(env, that, (*env)->GetStaticFieldID(env, that, "COMMON_LVB_REVERSE_VIDEO", "S"), (jshort)COMMON_LVB_REVERSE_VIDEO); - (*env)->SetStaticShortField(env, that, (*env)->GetStaticFieldID(env, that, "COMMON_LVB_UNDERSCORE", "S"), (jshort)COMMON_LVB_UNDERSCORE); - (*env)->SetStaticIntField(env, that, (*env)->GetStaticFieldID(env, that, "FORMAT_MESSAGE_FROM_SYSTEM", "I"), (jint)FORMAT_MESSAGE_FROM_SYSTEM); - (*env)->SetStaticIntField(env, that, (*env)->GetStaticFieldID(env, that, "STD_INPUT_HANDLE", "I"), (jint)STD_INPUT_HANDLE); - (*env)->SetStaticIntField(env, that, (*env)->GetStaticFieldID(env, that, "STD_OUTPUT_HANDLE", "I"), (jint)STD_OUTPUT_HANDLE); - (*env)->SetStaticIntField(env, that, (*env)->GetStaticFieldID(env, that, "STD_ERROR_HANDLE", "I"), (jint)STD_ERROR_HANDLE); - (*env)->SetStaticIntField(env, that, (*env)->GetStaticFieldID(env, that, "INVALID_HANDLE_VALUE", "I"), (jint)INVALID_HANDLE_VALUE); -#endif - return; -} - -#if defined(_WIN32) || defined(_WIN64) -JNIEXPORT jlong JNICALL Kernel32_NATIVE(malloc) - (JNIEnv *env, jclass that, jlong arg0) -{ - jlong rc = 0; - rc = (intptr_t)(void *)malloc((size_t)arg0); - return rc; -} -#endif - -#define CHAR_INFO_NATIVE(func) Java_org_fusesource_jansi_internal_Kernel32_00024CHAR_1INFO_##func - -JNIEXPORT void JNICALL CHAR_INFO_NATIVE(init)(JNIEnv *env, jclass that) -{ -#if defined(_WIN32) || defined(_WIN64) - (*env)->SetStaticIntField(env, that, (*env)->GetStaticFieldID(env, that, "SIZEOF", "I"), (jint)sizeof(CHAR_INFO)); -#endif - return; -} -#define CONSOLE_SCREEN_BUFFER_INFO_NATIVE(func) Java_org_fusesource_jansi_internal_Kernel32_00024CONSOLE_1SCREEN_1BUFFER_1INFO_##func - -JNIEXPORT void JNICALL CONSOLE_SCREEN_BUFFER_INFO_NATIVE(init)(JNIEnv *env, jclass that) -{ -#if defined(_WIN32) || defined(_WIN64) - (*env)->SetStaticIntField(env, that, (*env)->GetStaticFieldID(env, that, "SIZEOF", "I"), (jint)sizeof(CONSOLE_SCREEN_BUFFER_INFO)); -#endif - return; -} -#define COORD_NATIVE(func) Java_org_fusesource_jansi_internal_Kernel32_00024COORD_##func - -JNIEXPORT void JNICALL COORD_NATIVE(init)(JNIEnv *env, jclass that) -{ -#if defined(_WIN32) || defined(_WIN64) - (*env)->SetStaticIntField(env, that, (*env)->GetStaticFieldID(env, that, "SIZEOF", "I"), (jint)sizeof(COORD)); -#endif - return; -} -#define FOCUS_EVENT_RECORD_NATIVE(func) Java_org_fusesource_jansi_internal_Kernel32_00024FOCUS_1EVENT_1RECORD_##func - -JNIEXPORT void JNICALL FOCUS_EVENT_RECORD_NATIVE(init)(JNIEnv *env, jclass that) -{ -#if defined(_WIN32) || defined(_WIN64) - (*env)->SetStaticIntField(env, that, (*env)->GetStaticFieldID(env, that, "SIZEOF", "I"), (jint)sizeof(WINDOW_BUFFER_SIZE_RECORD)); -#endif - return; -} -#define INPUT_RECORD_NATIVE(func) Java_org_fusesource_jansi_internal_Kernel32_00024INPUT_1RECORD_##func - -JNIEXPORT void JNICALL INPUT_RECORD_NATIVE(init)(JNIEnv *env, jclass that) -{ -#if defined(_WIN32) || defined(_WIN64) - (*env)->SetStaticIntField(env, that, (*env)->GetStaticFieldID(env, that, "SIZEOF", "I"), (jint)sizeof(INPUT_RECORD)); - (*env)->SetStaticShortField(env, that, (*env)->GetStaticFieldID(env, that, "KEY_EVENT", "S"), (jshort)KEY_EVENT); - (*env)->SetStaticShortField(env, that, (*env)->GetStaticFieldID(env, that, "MOUSE_EVENT", "S"), (jshort)MOUSE_EVENT); - (*env)->SetStaticShortField(env, that, (*env)->GetStaticFieldID(env, that, "WINDOW_BUFFER_SIZE_EVENT", "S"), (jshort)WINDOW_BUFFER_SIZE_EVENT); - (*env)->SetStaticShortField(env, that, (*env)->GetStaticFieldID(env, that, "FOCUS_EVENT", "S"), (jshort)FOCUS_EVENT); - (*env)->SetStaticShortField(env, that, (*env)->GetStaticFieldID(env, that, "MENU_EVENT", "S"), (jshort)MENU_EVENT); -#endif - return; -} -#if defined(_WIN32) || defined(_WIN64) -JNIEXPORT void JNICALL INPUT_RECORD_NATIVE(memmove) - (JNIEnv *env, jclass that, jobject arg0, jlong arg1, jlong arg2) -{ - INPUT_RECORD _arg0, *lparg0=NULL; - if (arg0) if ((lparg0 = &_arg0) == NULL) goto fail; - memmove((void *)lparg0, (const void *)(intptr_t)arg1, (size_t)arg2); -fail: - if (arg0 && lparg0) setINPUT_RECORDFields(env, arg0, lparg0); -} -#endif - -#define KEY_EVENT_RECORD_NATIVE(func) Java_org_fusesource_jansi_internal_Kernel32_00024KEY_1EVENT_1RECORD_##func - -JNIEXPORT void JNICALL KEY_EVENT_RECORD_NATIVE(init)(JNIEnv *env, jclass that) -{ -#if defined(_WIN32) || defined(_WIN64) - (*env)->SetStaticIntField(env, that, (*env)->GetStaticFieldID(env, that, "SIZEOF", "I"), (jint)sizeof(KEY_EVENT_RECORD)); - (*env)->SetStaticIntField(env, that, (*env)->GetStaticFieldID(env, that, "CAPSLOCK_ON", "I"), (jint)CAPSLOCK_ON); - (*env)->SetStaticIntField(env, that, (*env)->GetStaticFieldID(env, that, "NUMLOCK_ON", "I"), (jint)NUMLOCK_ON); - (*env)->SetStaticIntField(env, that, (*env)->GetStaticFieldID(env, that, "SCROLLLOCK_ON", "I"), (jint)SCROLLLOCK_ON); - (*env)->SetStaticIntField(env, that, (*env)->GetStaticFieldID(env, that, "ENHANCED_KEY", "I"), (jint)ENHANCED_KEY); - (*env)->SetStaticIntField(env, that, (*env)->GetStaticFieldID(env, that, "LEFT_ALT_PRESSED", "I"), (jint)LEFT_ALT_PRESSED); - (*env)->SetStaticIntField(env, that, (*env)->GetStaticFieldID(env, that, "LEFT_CTRL_PRESSED", "I"), (jint)LEFT_CTRL_PRESSED); - (*env)->SetStaticIntField(env, that, (*env)->GetStaticFieldID(env, that, "RIGHT_ALT_PRESSED", "I"), (jint)RIGHT_ALT_PRESSED); - (*env)->SetStaticIntField(env, that, (*env)->GetStaticFieldID(env, that, "RIGHT_CTRL_PRESSED", "I"), (jint)RIGHT_CTRL_PRESSED); - (*env)->SetStaticIntField(env, that, (*env)->GetStaticFieldID(env, that, "SHIFT_PRESSED", "I"), (jint)SHIFT_PRESSED); -#endif - return; -} -#define MENU_EVENT_RECORD_NATIVE(func) Java_org_fusesource_jansi_internal_Kernel32_00024MENU_1EVENT_1RECORD_##func - -JNIEXPORT void JNICALL MENU_EVENT_RECORD_NATIVE(init)(JNIEnv *env, jclass that) -{ -#if defined(_WIN32) || defined(_WIN64) - (*env)->SetStaticIntField(env, that, (*env)->GetStaticFieldID(env, that, "SIZEOF", "I"), (jint)sizeof(MENU_EVENT_RECORD)); -#endif - return; -} -#define MOUSE_EVENT_RECORD_NATIVE(func) Java_org_fusesource_jansi_internal_Kernel32_00024MOUSE_1EVENT_1RECORD_##func - -JNIEXPORT void JNICALL MOUSE_EVENT_RECORD_NATIVE(init)(JNIEnv *env, jclass that) -{ -#if defined(_WIN32) || defined(_WIN64) - (*env)->SetStaticIntField(env, that, (*env)->GetStaticFieldID(env, that, "SIZEOF", "I"), (jint)sizeof(MOUSE_EVENT_RECORD)); - (*env)->SetStaticIntField(env, that, (*env)->GetStaticFieldID(env, that, "FROM_LEFT_1ST_BUTTON_PRESSED", "I"), (jint)FROM_LEFT_1ST_BUTTON_PRESSED); - (*env)->SetStaticIntField(env, that, (*env)->GetStaticFieldID(env, that, "FROM_LEFT_2ND_BUTTON_PRESSED", "I"), (jint)FROM_LEFT_2ND_BUTTON_PRESSED); - (*env)->SetStaticIntField(env, that, (*env)->GetStaticFieldID(env, that, "FROM_LEFT_3RD_BUTTON_PRESSED", "I"), (jint)FROM_LEFT_3RD_BUTTON_PRESSED); - (*env)->SetStaticIntField(env, that, (*env)->GetStaticFieldID(env, that, "FROM_LEFT_4TH_BUTTON_PRESSED", "I"), (jint)FROM_LEFT_4TH_BUTTON_PRESSED); - (*env)->SetStaticIntField(env, that, (*env)->GetStaticFieldID(env, that, "RIGHTMOST_BUTTON_PRESSED", "I"), (jint)RIGHTMOST_BUTTON_PRESSED); - (*env)->SetStaticIntField(env, that, (*env)->GetStaticFieldID(env, that, "CAPSLOCK_ON", "I"), (jint)CAPSLOCK_ON); - (*env)->SetStaticIntField(env, that, (*env)->GetStaticFieldID(env, that, "NUMLOCK_ON", "I"), (jint)NUMLOCK_ON); - (*env)->SetStaticIntField(env, that, (*env)->GetStaticFieldID(env, that, "SCROLLLOCK_ON", "I"), (jint)SCROLLLOCK_ON); - (*env)->SetStaticIntField(env, that, (*env)->GetStaticFieldID(env, that, "ENHANCED_KEY", "I"), (jint)ENHANCED_KEY); - (*env)->SetStaticIntField(env, that, (*env)->GetStaticFieldID(env, that, "LEFT_ALT_PRESSED", "I"), (jint)LEFT_ALT_PRESSED); - (*env)->SetStaticIntField(env, that, (*env)->GetStaticFieldID(env, that, "LEFT_CTRL_PRESSED", "I"), (jint)LEFT_CTRL_PRESSED); - (*env)->SetStaticIntField(env, that, (*env)->GetStaticFieldID(env, that, "RIGHT_ALT_PRESSED", "I"), (jint)RIGHT_ALT_PRESSED); - (*env)->SetStaticIntField(env, that, (*env)->GetStaticFieldID(env, that, "RIGHT_CTRL_PRESSED", "I"), (jint)RIGHT_CTRL_PRESSED); - (*env)->SetStaticIntField(env, that, (*env)->GetStaticFieldID(env, that, "SHIFT_PRESSED", "I"), (jint)SHIFT_PRESSED); - (*env)->SetStaticIntField(env, that, (*env)->GetStaticFieldID(env, that, "DOUBLE_CLICK", "I"), (jint)DOUBLE_CLICK); - (*env)->SetStaticIntField(env, that, (*env)->GetStaticFieldID(env, that, "MOUSE_HWHEELED", "I"), (jint)MOUSE_HWHEELED); - (*env)->SetStaticIntField(env, that, (*env)->GetStaticFieldID(env, that, "MOUSE_MOVED", "I"), (jint)MOUSE_MOVED); - (*env)->SetStaticIntField(env, that, (*env)->GetStaticFieldID(env, that, "MOUSE_WHEELED", "I"), (jint)MOUSE_WHEELED); -#endif - return; -} -#define SMALL_RECT_NATIVE(func) Java_org_fusesource_jansi_internal_Kernel32_00024SMALL_1RECT_##func - -JNIEXPORT void JNICALL SMALL_RECT_NATIVE(init)(JNIEnv *env, jclass that) -{ -#if defined(_WIN32) || defined(_WIN64) - (*env)->SetStaticIntField(env, that, (*env)->GetStaticFieldID(env, that, "SIZEOF", "I"), (jint)sizeof(SMALL_RECT)); -#endif - return; -} -#define WINDOW_BUFFER_SIZE_RECORD_NATIVE(func) Java_org_fusesource_jansi_internal_Kernel32_00024WINDOW_1BUFFER_1SIZE_1RECORD_##func - -JNIEXPORT void JNICALL WINDOW_BUFFER_SIZE_RECORD_NATIVE(init)(JNIEnv *env, jclass that) -{ -#if defined(_WIN32) || defined(_WIN64) - (*env)->SetStaticIntField(env, that, (*env)->GetStaticFieldID(env, that, "SIZEOF", "I"), (jint)sizeof(WINDOW_BUFFER_SIZE_RECORD)); -#endif - return; -} - -#if defined(_WIN32) || defined(_WIN64) - -wchar_t* java_to_wchar(JNIEnv* env, jstring string) { - jsize len = (*env)->GetStringLength(env, string); - wchar_t* str = (wchar_t*) malloc(sizeof(wchar_t) * (len + 1)); - (*env)->GetStringRegion(env, string, 0, len, (jchar*) str); - str[len] = L'\0'; - return str; -} - -#else - -char* java_to_char(JNIEnv* env, jstring string) { - size_t len = (*env)->GetStringLength(env, string); - size_t bytes = (*env)->GetStringUTFLength(env, string); - char* chars = (char*) malloc(bytes + 1); - (*env)->GetStringUTFRegion(env, string, 0, len, chars); - chars[bytes] = 0; - return chars; -} - -#endif diff --git a/src/main/native/jansi.h b/src/main/native/jansi.h deleted file mode 100644 index cd18c8e6..00000000 --- a/src/main/native/jansi.h +++ /dev/null @@ -1,192 +0,0 @@ -/******************************************************************************* - * Copyright (C) 2009-2017 the original author(s). - * - * 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. - *******************************************************************************/ -#ifndef JANSI_H -#define JANSI_H - -#ifdef __linux__ - - #define HAVE_ISATTY 1 - #define HAVE_TTYNAME 1 - #define HAVE_TCGETATTR 1 - #define HAVE_TCSETATTR 1 - #define HAVE_IOCTL 1 - #define HAVE_OPENPTY 1 - - #include - #include - #include - #include - #include - - #include "jni.h" - #include "jni_md.h" -#endif - -#ifdef __FreeBSD__ - - #define HAVE_ISATTY 1 - #define HAVE_TTYNAME 1 - #define HAVE_TCGETATTR 1 - #define HAVE_TCSETATTR 1 - #define HAVE_IOCTL 1 - #define HAVE_OPENPTY 1 - - #include - #include - #include - - #include - #include - #include - #include - - #include "jni.h" - #include "jni_md.h" -#endif - -/* Windows based build */ -#if defined(_WIN32) || defined(_WIN64) - - #include - #include - #include - #include - #include - - #define STDIN_FILENO 0 - #define STDOUT_FILENO 1 - #define STDERR_FILENO 2 - - #define isatty _isatty - #define getch _getch - - #ifndef MOUSE_HWHEELED - #define MOUSE_HWHEELED 0x0008 - #endif - - #include "jni.h" - #include "jni_md.h" -#endif - -#if defined(__APPLE__) && defined(__MACH__) - - #define HAVE_ISATTY 1 - #define HAVE_TTYNAME 1 - #define HAVE_TCGETATTR 1 - #define HAVE_TCSETATTR 1 - #define HAVE_IOCTL 1 - #define HAVE_OPENPTY 1 - - #include - #include - #include - #include - #include - - #include "jni.h" - #include "jni_md.h" -#endif - -#include - - -#ifndef JNI64 -#if defined(_LP64) -#define JNI64 -#endif -#endif - -/* 64 bit support */ -#ifndef JNI64 - -/* int/long defines */ -#define GetIntLongField GetIntField -#define SetIntLongField SetIntField -#define GetIntLongArrayElements GetIntArrayElements -#define ReleaseIntLongArrayElements ReleaseIntArrayElements -#define GetIntLongArrayRegion GetIntArrayRegion -#define SetIntLongArrayRegion SetIntArrayRegion -#define NewIntLongArray NewIntArray -#define CallStaticIntLongMethod CallStaticIntMethod -#define CallIntLongMethod CallIntMethod -#define CallStaticIntLongMethodV CallStaticIntMethodV -#define CallIntLongMethodV CallIntMethodV -#define jintLongArray jintArray -#define jintLong jint -#define I_J "I" -#define I_JArray "[I" - -/* float/double defines */ -#define GetFloatDoubleField GetFloatField -#define SetFloatDoubleField SetFloatField -#define GetFloatDoubleArrayElements GetFloatArrayElements -#define ReleaseFloatDoubleArrayElements ReleaseFloatArrayElements -#define GetFloatDoubleArrayRegion GetFloatArrayRegion -#define jfloatDoubleArray jfloatArray -#define jfloatDouble jfloat -#define F_D "F" -#define F_DArray "[F" - -#else - -/* int/long defines */ -#define GetIntLongField GetLongField -#define SetIntLongField SetLongField -#define GetIntLongArrayElements GetLongArrayElements -#define ReleaseIntLongArrayElements ReleaseLongArrayElements -#define GetIntLongArrayRegion GetLongArrayRegion -#define SetIntLongArrayRegion SetLongArrayRegion -#define NewIntLongArray NewLongArray -#define CallStaticIntLongMethod CallStaticLongMethod -#define CallIntLongMethod CallLongMethod -#define CallStaticIntLongMethodV CallStaticLongMethodV -#define CallIntLongMethodV CallLongMethodV -#define jintLongArray jlongArray -#define jintLong jlong -#define I_J "J" -#define I_JArray "[J" - -/* float/double defines */ -#define GetFloatDoubleField GetDoubleField -#define SetFloatDoubleField SetDoubleField -#define GetFloatDoubleArrayElements GetDoubleArrayElements -#define ReleaseFloatDoubleArrayElements ReleaseDoubleArrayElements -#define GetFloatDoubleArrayRegion GetDoubleArrayRegion -#define jfloatDoubleArray jdoubleArray -#define jfloatDouble jdouble -#define F_D "D" -#define F_DArray "[D" - -#endif - - -#ifdef __GNUC__ - #define hawtjni_w_barrier() __sync_synchronize() -#elif defined(SOLARIS2) && SOLARIS2 >= 10 - #include - #define hawtjni_w_barrier() __machine_w_barrier() -#elif defined(__APPLE__) - #include - #define hawtjni_w_barrier() OSMemoryBarrier() -#elif defined(_WIN32) || defined(_WIN64) - #include - #define hawtjni_w_barrier() _mm_sfence(); _WriteBarrier() -#else - #pragma message ( "Don't know how to do a memory barrier on this platform" ) - #define hawtjni_w_barrier() -#endif - - #endif /* JANSI_H */ diff --git a/src/main/native/jansi_isatty.c b/src/main/native/jansi_isatty.c deleted file mode 100644 index 0fc7d348..00000000 --- a/src/main/native/jansi_isatty.c +++ /dev/null @@ -1,122 +0,0 @@ -/******************************************************************************* - * Copyright (C) 2017, the original author(s). - * - * 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. - *******************************************************************************/ -#include "jansi.h" -#include "jansi_structs.h" - -#define CLibrary_NATIVE(func) Java_org_fusesource_jansi_internal_CLibrary_##func - -#if defined(_WIN32) || defined(_WIN64) - -typedef struct _UNICODE_STRING { - USHORT Length; - USHORT MaximumLength; - PWSTR Buffer; -} UNICODE_STRING, *PUNICODE_STRING; - -typedef struct _OBJECT_NAME_INFORMATION { - UNICODE_STRING Name; - WCHAR NameBuffer[0]; -} OBJECT_NAME_INFORMATION, *POBJECT_NAME_INFORMATION; - -typedef enum { - ObjectBasicInformation, - ObjectNameInformation, - ObjectTypeInformation, - ObjectAllInformation, - ObjectDataInformation -} OBJECT_INFORMATION_CLASS; - -typedef NTSTATUS (NTAPI *TFNNtQueryObject)(HANDLE, OBJECT_INFORMATION_CLASS, PVOID, ULONG, PULONG); -TFNNtQueryObject NtQueryObject = 0; - -HANDLE hModuleNtDll = 0; - -JNIEXPORT jint JNICALL CLibrary_NATIVE(isatty) - (JNIEnv *env, jclass that, jint arg0) -{ - jint rc; - - ULONG result; - BYTE buffer[1024]; - POBJECT_NAME_INFORMATION nameinfo = (POBJECT_NAME_INFORMATION) buffer; - PWSTR name; - DWORD mode; - - /* check if fd is a pipe */ - HANDLE h = (HANDLE) _get_osfhandle(arg0); - DWORD t = GetFileType(h); - if (t == FILE_TYPE_CHAR) { - // check that this is a real tty because the /dev/null - // and /dev/zero streams are also of type FILE_TYPE_CHAR - rc = GetConsoleMode(h, &mode) != 0; - } - else { - if (hModuleNtDll == 0) { - hModuleNtDll = LoadLibraryW(L"ntdll.dll"); - } - if (hModuleNtDll == 0) { - rc = 0; - } - else { - if (NtQueryObject == 0) { - NtQueryObject = (TFNNtQueryObject) GetProcAddress(hModuleNtDll, "NtQueryObject"); - } - if (NtQueryObject == 0) { - rc = 0; - } - /* get pipe name */ - else if (NtQueryObject(h, ObjectNameInformation, buffer, sizeof(buffer) - 2, &result) != 0) { - rc = 0; - } - else { - - name = nameinfo->Name.Buffer; - name[nameinfo->Name.Length / 2] = 0; - - //fprintf( stderr, "Standard stream %d: pipe name: %S\n", arg0, name); - - /* - * Check if this could be a MSYS2 pty pipe ('msys-XXXX-ptyN-XX') - * or a cygwin pty pipe ('cygwin-XXXX-ptyN-XX') - */ - if ((wcsstr(name, L"msys-") || wcsstr(name, L"cygwin-")) && wcsstr(name, L"-pty")) { - rc = 1; - } else { - // This is definitely not a tty - rc = 0; - } - } - } - } - - return rc; -} - -#else -#if defined(HAVE_ISATTY) - -JNIEXPORT jint JNICALL CLibrary_NATIVE(isatty) - (JNIEnv *env, jclass that, jint arg0) -{ - jint rc = 0; - - rc = (jint)isatty(arg0); - - return rc; -} - -#endif -#endif diff --git a/src/main/native/jansi_structs.c b/src/main/native/jansi_structs.c deleted file mode 100644 index b04276d5..00000000 --- a/src/main/native/jansi_structs.c +++ /dev/null @@ -1,695 +0,0 @@ -/******************************************************************************* - * Copyright (C) 2009-2017 the original author(s). - * - * 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. - *******************************************************************************/ -#include "jansi.h" -#include "jansi_structs.h" - -#if defined(HAVE_IOCTL) -typedef struct Termios_FID_CACHE { - int cached; - jclass clazz; - jfieldID c_iflag, c_oflag, c_cflag, c_lflag, c_cc, c_ispeed, c_ospeed; -} Termios_FID_CACHE; - -Termios_FID_CACHE TermiosFc; - -void cacheTermiosFields(JNIEnv *env, jobject lpObject) -{ - if (TermiosFc.cached) return; - TermiosFc.clazz = (*env)->GetObjectClass(env, lpObject); - TermiosFc.c_iflag = (*env)->GetFieldID(env, TermiosFc.clazz, "c_iflag", "J"); - TermiosFc.c_oflag = (*env)->GetFieldID(env, TermiosFc.clazz, "c_oflag", "J"); - TermiosFc.c_cflag = (*env)->GetFieldID(env, TermiosFc.clazz, "c_cflag", "J"); - TermiosFc.c_lflag = (*env)->GetFieldID(env, TermiosFc.clazz, "c_lflag", "J"); - TermiosFc.c_cc = (*env)->GetFieldID(env, TermiosFc.clazz, "c_cc", "[B"); - TermiosFc.c_ispeed = (*env)->GetFieldID(env, TermiosFc.clazz, "c_ispeed", "J"); - TermiosFc.c_ospeed = (*env)->GetFieldID(env, TermiosFc.clazz, "c_ospeed", "J"); - hawtjni_w_barrier(); - TermiosFc.cached = 1; -} - -struct termios *getTermiosFields(JNIEnv *env, jobject lpObject, struct termios *lpStruct) -{ - if (!TermiosFc.cached) cacheTermiosFields(env, lpObject); -#if defined(HAVE_IOCTL) - lpStruct->c_iflag = (*env)->GetLongField(env, lpObject, TermiosFc.c_iflag); -#endif -#if defined(HAVE_IOCTL) - lpStruct->c_oflag = (*env)->GetLongField(env, lpObject, TermiosFc.c_oflag); -#endif -#if defined(HAVE_IOCTL) - lpStruct->c_cflag = (*env)->GetLongField(env, lpObject, TermiosFc.c_cflag); -#endif -#if defined(HAVE_IOCTL) - lpStruct->c_lflag = (*env)->GetLongField(env, lpObject, TermiosFc.c_lflag); -#endif -#if defined(HAVE_IOCTL) - { - jbyteArray lpObject1 = (jbyteArray)(*env)->GetObjectField(env, lpObject, TermiosFc.c_cc); - (*env)->GetByteArrayRegion(env, lpObject1, 0, sizeof(lpStruct->c_cc), (jbyte *)lpStruct->c_cc); - } -#endif -#if defined(HAVE_IOCTL) - lpStruct->c_ispeed = (*env)->GetLongField(env, lpObject, TermiosFc.c_ispeed); -#endif -#if defined(HAVE_IOCTL) - lpStruct->c_ospeed = (*env)->GetLongField(env, lpObject, TermiosFc.c_ospeed); -#endif - return lpStruct; -} - -void setTermiosFields(JNIEnv *env, jobject lpObject, struct termios *lpStruct) -{ - if (!TermiosFc.cached) cacheTermiosFields(env, lpObject); -#if defined(HAVE_IOCTL) - (*env)->SetLongField(env, lpObject, TermiosFc.c_iflag, (jlong)lpStruct->c_iflag); -#endif -#if defined(HAVE_IOCTL) - (*env)->SetLongField(env, lpObject, TermiosFc.c_oflag, (jlong)lpStruct->c_oflag); -#endif -#if defined(HAVE_IOCTL) - (*env)->SetLongField(env, lpObject, TermiosFc.c_cflag, (jlong)lpStruct->c_cflag); -#endif -#if defined(HAVE_IOCTL) - (*env)->SetLongField(env, lpObject, TermiosFc.c_lflag, (jlong)lpStruct->c_lflag); -#endif -#if defined(HAVE_IOCTL) - { - jbyteArray lpObject1 = (jbyteArray)(*env)->GetObjectField(env, lpObject, TermiosFc.c_cc); - (*env)->SetByteArrayRegion(env, lpObject1, 0, sizeof(lpStruct->c_cc), (jbyte *)lpStruct->c_cc); - } -#endif -#if defined(HAVE_IOCTL) - (*env)->SetLongField(env, lpObject, TermiosFc.c_ispeed, (jlong)lpStruct->c_ispeed); -#endif -#if defined(HAVE_IOCTL) - (*env)->SetLongField(env, lpObject, TermiosFc.c_ospeed, (jlong)lpStruct->c_ospeed); -#endif -} -#endif - -#if defined(HAVE_IOCTL) -typedef struct WinSize_FID_CACHE { - int cached; - jclass clazz; - jfieldID ws_row, ws_col, ws_xpixel, ws_ypixel; -} WinSize_FID_CACHE; - -WinSize_FID_CACHE WinSizeFc; - -void cacheWinSizeFields(JNIEnv *env, jobject lpObject) -{ - if (WinSizeFc.cached) return; - WinSizeFc.clazz = (*env)->GetObjectClass(env, lpObject); - WinSizeFc.ws_row = (*env)->GetFieldID(env, WinSizeFc.clazz, "ws_row", "S"); - WinSizeFc.ws_col = (*env)->GetFieldID(env, WinSizeFc.clazz, "ws_col", "S"); - WinSizeFc.ws_xpixel = (*env)->GetFieldID(env, WinSizeFc.clazz, "ws_xpixel", "S"); - WinSizeFc.ws_ypixel = (*env)->GetFieldID(env, WinSizeFc.clazz, "ws_ypixel", "S"); - hawtjni_w_barrier(); - WinSizeFc.cached = 1; -} - -struct winsize *getWinSizeFields(JNIEnv *env, jobject lpObject, struct winsize *lpStruct) -{ - if (!WinSizeFc.cached) cacheWinSizeFields(env, lpObject); -#if defined(HAVE_IOCTL) - lpStruct->ws_row = (*env)->GetShortField(env, lpObject, WinSizeFc.ws_row); -#endif -#if defined(HAVE_IOCTL) - lpStruct->ws_col = (*env)->GetShortField(env, lpObject, WinSizeFc.ws_col); -#endif -#if defined(HAVE_IOCTL) - lpStruct->ws_xpixel = (*env)->GetShortField(env, lpObject, WinSizeFc.ws_xpixel); -#endif -#if defined(HAVE_IOCTL) - lpStruct->ws_ypixel = (*env)->GetShortField(env, lpObject, WinSizeFc.ws_ypixel); -#endif - return lpStruct; -} - -void setWinSizeFields(JNIEnv *env, jobject lpObject, struct winsize *lpStruct) -{ - if (!WinSizeFc.cached) cacheWinSizeFields(env, lpObject); -#if defined(HAVE_IOCTL) - (*env)->SetShortField(env, lpObject, WinSizeFc.ws_row, (jshort)lpStruct->ws_row); -#endif -#if defined(HAVE_IOCTL) - (*env)->SetShortField(env, lpObject, WinSizeFc.ws_col, (jshort)lpStruct->ws_col); -#endif -#if defined(HAVE_IOCTL) - (*env)->SetShortField(env, lpObject, WinSizeFc.ws_xpixel, (jshort)lpStruct->ws_xpixel); -#endif -#if defined(HAVE_IOCTL) - (*env)->SetShortField(env, lpObject, WinSizeFc.ws_ypixel, (jshort)lpStruct->ws_ypixel); -#endif -} -#endif - -#if defined(_WIN32) || defined(_WIN64) -typedef struct CHAR_INFO_FID_CACHE { - int cached; - jclass clazz; - jfieldID attributes, unicodeChar; -} CHAR_INFO_FID_CACHE; - -CHAR_INFO_FID_CACHE CHAR_INFOFc; - -void cacheCHAR_INFOFields(JNIEnv *env, jobject lpObject) -{ - if (CHAR_INFOFc.cached) return; - CHAR_INFOFc.clazz = (*env)->GetObjectClass(env, lpObject); - CHAR_INFOFc.attributes = (*env)->GetFieldID(env, CHAR_INFOFc.clazz, "attributes", "S"); - CHAR_INFOFc.unicodeChar = (*env)->GetFieldID(env, CHAR_INFOFc.clazz, "unicodeChar", "C"); - hawtjni_w_barrier(); - CHAR_INFOFc.cached = 1; -} - -CHAR_INFO *getCHAR_INFOFields(JNIEnv *env, jobject lpObject, CHAR_INFO *lpStruct) -{ - if (!CHAR_INFOFc.cached) cacheCHAR_INFOFields(env, lpObject); -#if defined(_WIN32) || defined(_WIN64) - lpStruct->Attributes = (*env)->GetShortField(env, lpObject, CHAR_INFOFc.attributes); -#endif -#if defined(_WIN32) || defined(_WIN64) - lpStruct->Char.UnicodeChar = (*env)->GetCharField(env, lpObject, CHAR_INFOFc.unicodeChar); -#endif - return lpStruct; -} - -void setCHAR_INFOFields(JNIEnv *env, jobject lpObject, CHAR_INFO *lpStruct) -{ - if (!CHAR_INFOFc.cached) cacheCHAR_INFOFields(env, lpObject); -#if defined(_WIN32) || defined(_WIN64) - (*env)->SetShortField(env, lpObject, CHAR_INFOFc.attributes, (jshort)lpStruct->Attributes); -#endif -#if defined(_WIN32) || defined(_WIN64) - (*env)->SetCharField(env, lpObject, CHAR_INFOFc.unicodeChar, (jchar)lpStruct->Char.UnicodeChar); -#endif -} -#endif - -#if defined(_WIN32) || defined(_WIN64) -typedef struct CONSOLE_SCREEN_BUFFER_INFO_FID_CACHE { - int cached; - jclass clazz; - jfieldID size, cursorPosition, attributes, window, maximumWindowSize; -} CONSOLE_SCREEN_BUFFER_INFO_FID_CACHE; - -CONSOLE_SCREEN_BUFFER_INFO_FID_CACHE CONSOLE_SCREEN_BUFFER_INFOFc; - -void cacheCONSOLE_SCREEN_BUFFER_INFOFields(JNIEnv *env, jobject lpObject) -{ - if (CONSOLE_SCREEN_BUFFER_INFOFc.cached) return; - CONSOLE_SCREEN_BUFFER_INFOFc.clazz = (*env)->GetObjectClass(env, lpObject); - CONSOLE_SCREEN_BUFFER_INFOFc.size = (*env)->GetFieldID(env, CONSOLE_SCREEN_BUFFER_INFOFc.clazz, "size", "Lorg/fusesource/jansi/internal/Kernel32$COORD;"); - CONSOLE_SCREEN_BUFFER_INFOFc.cursorPosition = (*env)->GetFieldID(env, CONSOLE_SCREEN_BUFFER_INFOFc.clazz, "cursorPosition", "Lorg/fusesource/jansi/internal/Kernel32$COORD;"); - CONSOLE_SCREEN_BUFFER_INFOFc.attributes = (*env)->GetFieldID(env, CONSOLE_SCREEN_BUFFER_INFOFc.clazz, "attributes", "S"); - CONSOLE_SCREEN_BUFFER_INFOFc.window = (*env)->GetFieldID(env, CONSOLE_SCREEN_BUFFER_INFOFc.clazz, "window", "Lorg/fusesource/jansi/internal/Kernel32$SMALL_RECT;"); - CONSOLE_SCREEN_BUFFER_INFOFc.maximumWindowSize = (*env)->GetFieldID(env, CONSOLE_SCREEN_BUFFER_INFOFc.clazz, "maximumWindowSize", "Lorg/fusesource/jansi/internal/Kernel32$COORD;"); - hawtjni_w_barrier(); - CONSOLE_SCREEN_BUFFER_INFOFc.cached = 1; -} - -CONSOLE_SCREEN_BUFFER_INFO *getCONSOLE_SCREEN_BUFFER_INFOFields(JNIEnv *env, jobject lpObject, CONSOLE_SCREEN_BUFFER_INFO *lpStruct) -{ - if (!CONSOLE_SCREEN_BUFFER_INFOFc.cached) cacheCONSOLE_SCREEN_BUFFER_INFOFields(env, lpObject); -#if defined(_WIN32) || defined(_WIN64) - { - jobject lpObject1 = (*env)->GetObjectField(env, lpObject, CONSOLE_SCREEN_BUFFER_INFOFc.size); - if (lpObject1 != NULL) getCOORDFields(env, lpObject1, &lpStruct->dwSize); - } -#endif -#if defined(_WIN32) || defined(_WIN64) - { - jobject lpObject1 = (*env)->GetObjectField(env, lpObject, CONSOLE_SCREEN_BUFFER_INFOFc.cursorPosition); - if (lpObject1 != NULL) getCOORDFields(env, lpObject1, &lpStruct->dwCursorPosition); - } -#endif -#if defined(_WIN32) || defined(_WIN64) - lpStruct->wAttributes = (*env)->GetShortField(env, lpObject, CONSOLE_SCREEN_BUFFER_INFOFc.attributes); -#endif -#if defined(_WIN32) || defined(_WIN64) - { - jobject lpObject1 = (*env)->GetObjectField(env, lpObject, CONSOLE_SCREEN_BUFFER_INFOFc.window); - if (lpObject1 != NULL) getSMALL_RECTFields(env, lpObject1, &lpStruct->srWindow); - } -#endif -#if defined(_WIN32) || defined(_WIN64) - { - jobject lpObject1 = (*env)->GetObjectField(env, lpObject, CONSOLE_SCREEN_BUFFER_INFOFc.maximumWindowSize); - if (lpObject1 != NULL) getCOORDFields(env, lpObject1, &lpStruct->dwMaximumWindowSize); - } -#endif - return lpStruct; -} - -void setCONSOLE_SCREEN_BUFFER_INFOFields(JNIEnv *env, jobject lpObject, CONSOLE_SCREEN_BUFFER_INFO *lpStruct) -{ - if (!CONSOLE_SCREEN_BUFFER_INFOFc.cached) cacheCONSOLE_SCREEN_BUFFER_INFOFields(env, lpObject); -#if defined(_WIN32) || defined(_WIN64) - { - jobject lpObject1 = (*env)->GetObjectField(env, lpObject, CONSOLE_SCREEN_BUFFER_INFOFc.size); - if (lpObject1 != NULL) setCOORDFields(env, lpObject1, &lpStruct->dwSize); - } -#endif -#if defined(_WIN32) || defined(_WIN64) - { - jobject lpObject1 = (*env)->GetObjectField(env, lpObject, CONSOLE_SCREEN_BUFFER_INFOFc.cursorPosition); - if (lpObject1 != NULL) setCOORDFields(env, lpObject1, &lpStruct->dwCursorPosition); - } -#endif -#if defined(_WIN32) || defined(_WIN64) - (*env)->SetShortField(env, lpObject, CONSOLE_SCREEN_BUFFER_INFOFc.attributes, (jshort)lpStruct->wAttributes); -#endif -#if defined(_WIN32) || defined(_WIN64) - { - jobject lpObject1 = (*env)->GetObjectField(env, lpObject, CONSOLE_SCREEN_BUFFER_INFOFc.window); - if (lpObject1 != NULL) setSMALL_RECTFields(env, lpObject1, &lpStruct->srWindow); - } -#endif -#if defined(_WIN32) || defined(_WIN64) - { - jobject lpObject1 = (*env)->GetObjectField(env, lpObject, CONSOLE_SCREEN_BUFFER_INFOFc.maximumWindowSize); - if (lpObject1 != NULL) setCOORDFields(env, lpObject1, &lpStruct->dwMaximumWindowSize); - } -#endif -} -#endif - -#if defined(_WIN32) || defined(_WIN64) -typedef struct COORD_FID_CACHE { - int cached; - jclass clazz; - jfieldID x, y; -} COORD_FID_CACHE; - -COORD_FID_CACHE COORDFc; - -void cacheCOORDFields(JNIEnv *env, jobject lpObject) -{ - if (COORDFc.cached) return; - COORDFc.clazz = (*env)->GetObjectClass(env, lpObject); - COORDFc.x = (*env)->GetFieldID(env, COORDFc.clazz, "x", "S"); - COORDFc.y = (*env)->GetFieldID(env, COORDFc.clazz, "y", "S"); - hawtjni_w_barrier(); - COORDFc.cached = 1; -} - -COORD *getCOORDFields(JNIEnv *env, jobject lpObject, COORD *lpStruct) -{ - if (!COORDFc.cached) cacheCOORDFields(env, lpObject); -#if defined(_WIN32) || defined(_WIN64) - lpStruct->X = (*env)->GetShortField(env, lpObject, COORDFc.x); -#endif -#if defined(_WIN32) || defined(_WIN64) - lpStruct->Y = (*env)->GetShortField(env, lpObject, COORDFc.y); -#endif - return lpStruct; -} - -void setCOORDFields(JNIEnv *env, jobject lpObject, COORD *lpStruct) -{ - if (!COORDFc.cached) cacheCOORDFields(env, lpObject); -#if defined(_WIN32) || defined(_WIN64) - (*env)->SetShortField(env, lpObject, COORDFc.x, (jshort)lpStruct->X); -#endif -#if defined(_WIN32) || defined(_WIN64) - (*env)->SetShortField(env, lpObject, COORDFc.y, (jshort)lpStruct->Y); -#endif -} -#endif - -#if defined(_WIN32) || defined(_WIN64) -typedef struct FOCUS_EVENT_RECORD_FID_CACHE { - int cached; - jclass clazz; - jfieldID setFocus; -} FOCUS_EVENT_RECORD_FID_CACHE; - -FOCUS_EVENT_RECORD_FID_CACHE FOCUS_EVENT_RECORDFc; - -void cacheFOCUS_EVENT_RECORDFields(JNIEnv *env, jobject lpObject) -{ - if (FOCUS_EVENT_RECORDFc.cached) return; - FOCUS_EVENT_RECORDFc.clazz = (*env)->GetObjectClass(env, lpObject); - FOCUS_EVENT_RECORDFc.setFocus = (*env)->GetFieldID(env, FOCUS_EVENT_RECORDFc.clazz, "setFocus", "Z"); - hawtjni_w_barrier(); - FOCUS_EVENT_RECORDFc.cached = 1; -} - -FOCUS_EVENT_RECORD *getFOCUS_EVENT_RECORDFields(JNIEnv *env, jobject lpObject, FOCUS_EVENT_RECORD *lpStruct) -{ - if (!FOCUS_EVENT_RECORDFc.cached) cacheFOCUS_EVENT_RECORDFields(env, lpObject); -#if defined(_WIN32) || defined(_WIN64) - lpStruct->bSetFocus = (*env)->GetBooleanField(env, lpObject, FOCUS_EVENT_RECORDFc.setFocus); -#endif - return lpStruct; -} - -void setFOCUS_EVENT_RECORDFields(JNIEnv *env, jobject lpObject, FOCUS_EVENT_RECORD *lpStruct) -{ - if (!FOCUS_EVENT_RECORDFc.cached) cacheFOCUS_EVENT_RECORDFields(env, lpObject); -#if defined(_WIN32) || defined(_WIN64) - (*env)->SetBooleanField(env, lpObject, FOCUS_EVENT_RECORDFc.setFocus, (jboolean)lpStruct->bSetFocus); -#endif -} -#endif - -#if defined(_WIN32) || defined(_WIN64) -typedef struct INPUT_RECORD_FID_CACHE { - int cached; - jclass clazz; - jfieldID eventType, keyEvent, mouseEvent, windowBufferSizeEvent, menuEvent, focusEvent; -} INPUT_RECORD_FID_CACHE; - -INPUT_RECORD_FID_CACHE INPUT_RECORDFc; - -void cacheINPUT_RECORDFields(JNIEnv *env, jobject lpObject) -{ - if (INPUT_RECORDFc.cached) return; - INPUT_RECORDFc.clazz = (*env)->GetObjectClass(env, lpObject); - INPUT_RECORDFc.eventType = (*env)->GetFieldID(env, INPUT_RECORDFc.clazz, "eventType", "S"); - INPUT_RECORDFc.keyEvent = (*env)->GetFieldID(env, INPUT_RECORDFc.clazz, "keyEvent", "Lorg/fusesource/jansi/internal/Kernel32$KEY_EVENT_RECORD;"); - INPUT_RECORDFc.mouseEvent = (*env)->GetFieldID(env, INPUT_RECORDFc.clazz, "mouseEvent", "Lorg/fusesource/jansi/internal/Kernel32$MOUSE_EVENT_RECORD;"); - INPUT_RECORDFc.windowBufferSizeEvent = (*env)->GetFieldID(env, INPUT_RECORDFc.clazz, "windowBufferSizeEvent", "Lorg/fusesource/jansi/internal/Kernel32$WINDOW_BUFFER_SIZE_RECORD;"); - INPUT_RECORDFc.menuEvent = (*env)->GetFieldID(env, INPUT_RECORDFc.clazz, "menuEvent", "Lorg/fusesource/jansi/internal/Kernel32$MENU_EVENT_RECORD;"); - INPUT_RECORDFc.focusEvent = (*env)->GetFieldID(env, INPUT_RECORDFc.clazz, "focusEvent", "Lorg/fusesource/jansi/internal/Kernel32$FOCUS_EVENT_RECORD;"); - hawtjni_w_barrier(); - INPUT_RECORDFc.cached = 1; -} - -INPUT_RECORD *getINPUT_RECORDFields(JNIEnv *env, jobject lpObject, INPUT_RECORD *lpStruct) -{ - if (!INPUT_RECORDFc.cached) cacheINPUT_RECORDFields(env, lpObject); -#if defined(_WIN32) || defined(_WIN64) - lpStruct->EventType = (*env)->GetShortField(env, lpObject, INPUT_RECORDFc.eventType); -#endif -#if defined(_WIN32) || defined(_WIN64) - { - jobject lpObject1 = (*env)->GetObjectField(env, lpObject, INPUT_RECORDFc.keyEvent); - if (lpObject1 != NULL) getKEY_EVENT_RECORDFields(env, lpObject1, &lpStruct->Event.KeyEvent); - } -#endif -#if defined(_WIN32) || defined(_WIN64) - { - jobject lpObject1 = (*env)->GetObjectField(env, lpObject, INPUT_RECORDFc.mouseEvent); - if (lpObject1 != NULL) getMOUSE_EVENT_RECORDFields(env, lpObject1, &lpStruct->Event.MouseEvent); - } -#endif -#if defined(_WIN32) || defined(_WIN64) - { - jobject lpObject1 = (*env)->GetObjectField(env, lpObject, INPUT_RECORDFc.windowBufferSizeEvent); - if (lpObject1 != NULL) getWINDOW_BUFFER_SIZE_RECORDFields(env, lpObject1, &lpStruct->Event.WindowBufferSizeEvent); - } -#endif -#if defined(_WIN32) || defined(_WIN64) - { - jobject lpObject1 = (*env)->GetObjectField(env, lpObject, INPUT_RECORDFc.menuEvent); - if (lpObject1 != NULL) getMENU_EVENT_RECORDFields(env, lpObject1, &lpStruct->Event.MenuEvent); - } -#endif -#if defined(_WIN32) || defined(_WIN64) - { - jobject lpObject1 = (*env)->GetObjectField(env, lpObject, INPUT_RECORDFc.focusEvent); - if (lpObject1 != NULL) getFOCUS_EVENT_RECORDFields(env, lpObject1, &lpStruct->Event.FocusEvent); - } -#endif - return lpStruct; -} - -void setINPUT_RECORDFields(JNIEnv *env, jobject lpObject, INPUT_RECORD *lpStruct) -{ - if (!INPUT_RECORDFc.cached) cacheINPUT_RECORDFields(env, lpObject); -#if defined(_WIN32) || defined(_WIN64) - (*env)->SetShortField(env, lpObject, INPUT_RECORDFc.eventType, (jshort)lpStruct->EventType); -#endif -#if defined(_WIN32) || defined(_WIN64) - { - jobject lpObject1 = (*env)->GetObjectField(env, lpObject, INPUT_RECORDFc.keyEvent); - if (lpObject1 != NULL) setKEY_EVENT_RECORDFields(env, lpObject1, &lpStruct->Event.KeyEvent); - } -#endif -#if defined(_WIN32) || defined(_WIN64) - { - jobject lpObject1 = (*env)->GetObjectField(env, lpObject, INPUT_RECORDFc.mouseEvent); - if (lpObject1 != NULL) setMOUSE_EVENT_RECORDFields(env, lpObject1, &lpStruct->Event.MouseEvent); - } -#endif -#if defined(_WIN32) || defined(_WIN64) - { - jobject lpObject1 = (*env)->GetObjectField(env, lpObject, INPUT_RECORDFc.windowBufferSizeEvent); - if (lpObject1 != NULL) setWINDOW_BUFFER_SIZE_RECORDFields(env, lpObject1, &lpStruct->Event.WindowBufferSizeEvent); - } -#endif -#if defined(_WIN32) || defined(_WIN64) - { - jobject lpObject1 = (*env)->GetObjectField(env, lpObject, INPUT_RECORDFc.menuEvent); - if (lpObject1 != NULL) setMENU_EVENT_RECORDFields(env, lpObject1, &lpStruct->Event.MenuEvent); - } -#endif -#if defined(_WIN32) || defined(_WIN64) - { - jobject lpObject1 = (*env)->GetObjectField(env, lpObject, INPUT_RECORDFc.focusEvent); - if (lpObject1 != NULL) setFOCUS_EVENT_RECORDFields(env, lpObject1, &lpStruct->Event.FocusEvent); - } -#endif -} -#endif - -#if defined(_WIN32) || defined(_WIN64) -typedef struct KEY_EVENT_RECORD_FID_CACHE { - int cached; - jclass clazz; - jfieldID keyDown, repeatCount, keyCode, scanCode, uchar, controlKeyState; -} KEY_EVENT_RECORD_FID_CACHE; - -KEY_EVENT_RECORD_FID_CACHE KEY_EVENT_RECORDFc; - -void cacheKEY_EVENT_RECORDFields(JNIEnv *env, jobject lpObject) -{ - if (KEY_EVENT_RECORDFc.cached) return; - KEY_EVENT_RECORDFc.clazz = (*env)->GetObjectClass(env, lpObject); - KEY_EVENT_RECORDFc.keyDown = (*env)->GetFieldID(env, KEY_EVENT_RECORDFc.clazz, "keyDown", "Z"); - KEY_EVENT_RECORDFc.repeatCount = (*env)->GetFieldID(env, KEY_EVENT_RECORDFc.clazz, "repeatCount", "S"); - KEY_EVENT_RECORDFc.keyCode = (*env)->GetFieldID(env, KEY_EVENT_RECORDFc.clazz, "keyCode", "S"); - KEY_EVENT_RECORDFc.scanCode = (*env)->GetFieldID(env, KEY_EVENT_RECORDFc.clazz, "scanCode", "S"); - KEY_EVENT_RECORDFc.uchar = (*env)->GetFieldID(env, KEY_EVENT_RECORDFc.clazz, "uchar", "C"); - KEY_EVENT_RECORDFc.controlKeyState = (*env)->GetFieldID(env, KEY_EVENT_RECORDFc.clazz, "controlKeyState", "I"); - hawtjni_w_barrier(); - KEY_EVENT_RECORDFc.cached = 1; -} - -KEY_EVENT_RECORD *getKEY_EVENT_RECORDFields(JNIEnv *env, jobject lpObject, KEY_EVENT_RECORD *lpStruct) -{ - if (!KEY_EVENT_RECORDFc.cached) cacheKEY_EVENT_RECORDFields(env, lpObject); -#if defined(_WIN32) || defined(_WIN64) - lpStruct->bKeyDown = (*env)->GetBooleanField(env, lpObject, KEY_EVENT_RECORDFc.keyDown); - lpStruct->wRepeatCount = (*env)->GetShortField(env, lpObject, KEY_EVENT_RECORDFc.repeatCount); - lpStruct->wVirtualKeyCode = (*env)->GetShortField(env, lpObject, KEY_EVENT_RECORDFc.keyCode); - lpStruct->wVirtualScanCode = (*env)->GetShortField(env, lpObject, KEY_EVENT_RECORDFc.scanCode); - lpStruct->uChar.UnicodeChar = (*env)->GetCharField(env, lpObject, KEY_EVENT_RECORDFc.uchar); - lpStruct->dwControlKeyState = (*env)->GetIntField(env, lpObject, KEY_EVENT_RECORDFc.controlKeyState); -#endif - return lpStruct; -} - -void setKEY_EVENT_RECORDFields(JNIEnv *env, jobject lpObject, KEY_EVENT_RECORD *lpStruct) -{ - if (!KEY_EVENT_RECORDFc.cached) cacheKEY_EVENT_RECORDFields(env, lpObject); -#if defined(_WIN32) || defined(_WIN64) - (*env)->SetBooleanField(env, lpObject, KEY_EVENT_RECORDFc.keyDown, (jboolean)lpStruct->bKeyDown); - (*env)->SetShortField(env, lpObject, KEY_EVENT_RECORDFc.repeatCount, (jshort)lpStruct->wRepeatCount); - (*env)->SetShortField(env, lpObject, KEY_EVENT_RECORDFc.keyCode, (jshort)lpStruct->wVirtualKeyCode); - (*env)->SetShortField(env, lpObject, KEY_EVENT_RECORDFc.scanCode, (jshort)lpStruct->wVirtualScanCode); - (*env)->SetCharField(env, lpObject, KEY_EVENT_RECORDFc.uchar, (jchar)lpStruct->uChar.UnicodeChar); - (*env)->SetIntField(env, lpObject, KEY_EVENT_RECORDFc.controlKeyState, (jint)lpStruct->dwControlKeyState); -#endif -} -#endif - -#if defined(_WIN32) || defined(_WIN64) -typedef struct MENU_EVENT_RECORD_FID_CACHE { - int cached; - jclass clazz; - jfieldID commandId; -} MENU_EVENT_RECORD_FID_CACHE; - -MENU_EVENT_RECORD_FID_CACHE MENU_EVENT_RECORDFc; - -void cacheMENU_EVENT_RECORDFields(JNIEnv *env, jobject lpObject) -{ - if (MENU_EVENT_RECORDFc.cached) return; - MENU_EVENT_RECORDFc.clazz = (*env)->GetObjectClass(env, lpObject); - MENU_EVENT_RECORDFc.commandId = (*env)->GetFieldID(env, MENU_EVENT_RECORDFc.clazz, "commandId", "I"); - hawtjni_w_barrier(); - MENU_EVENT_RECORDFc.cached = 1; -} - -MENU_EVENT_RECORD *getMENU_EVENT_RECORDFields(JNIEnv *env, jobject lpObject, MENU_EVENT_RECORD *lpStruct) -{ - if (!MENU_EVENT_RECORDFc.cached) cacheMENU_EVENT_RECORDFields(env, lpObject); -#if defined(_WIN32) || defined(_WIN64) - lpStruct->dwCommandId = (*env)->GetIntField(env, lpObject, MENU_EVENT_RECORDFc.commandId); -#endif - return lpStruct; -} - -void setMENU_EVENT_RECORDFields(JNIEnv *env, jobject lpObject, MENU_EVENT_RECORD *lpStruct) -{ - if (!MENU_EVENT_RECORDFc.cached) cacheMENU_EVENT_RECORDFields(env, lpObject); -#if defined(_WIN32) || defined(_WIN64) - (*env)->SetIntField(env, lpObject, MENU_EVENT_RECORDFc.commandId, (jint)lpStruct->dwCommandId); -#endif -} -#endif - -#if defined(_WIN32) || defined(_WIN64) -typedef struct MOUSE_EVENT_RECORD_FID_CACHE { - int cached; - jclass clazz; - jfieldID mousePosition, buttonState, controlKeyState, eventFlags; -} MOUSE_EVENT_RECORD_FID_CACHE; - -MOUSE_EVENT_RECORD_FID_CACHE MOUSE_EVENT_RECORDFc; - -void cacheMOUSE_EVENT_RECORDFields(JNIEnv *env, jobject lpObject) -{ - if (MOUSE_EVENT_RECORDFc.cached) return; - MOUSE_EVENT_RECORDFc.clazz = (*env)->GetObjectClass(env, lpObject); - MOUSE_EVENT_RECORDFc.mousePosition = (*env)->GetFieldID(env, MOUSE_EVENT_RECORDFc.clazz, "mousePosition", "Lorg/fusesource/jansi/internal/Kernel32$COORD;"); - MOUSE_EVENT_RECORDFc.buttonState = (*env)->GetFieldID(env, MOUSE_EVENT_RECORDFc.clazz, "buttonState", "I"); - MOUSE_EVENT_RECORDFc.controlKeyState = (*env)->GetFieldID(env, MOUSE_EVENT_RECORDFc.clazz, "controlKeyState", "I"); - MOUSE_EVENT_RECORDFc.eventFlags = (*env)->GetFieldID(env, MOUSE_EVENT_RECORDFc.clazz, "eventFlags", "I"); - hawtjni_w_barrier(); - MOUSE_EVENT_RECORDFc.cached = 1; -} - -MOUSE_EVENT_RECORD *getMOUSE_EVENT_RECORDFields(JNIEnv *env, jobject lpObject, MOUSE_EVENT_RECORD *lpStruct) -{ - if (!MOUSE_EVENT_RECORDFc.cached) cacheMOUSE_EVENT_RECORDFields(env, lpObject); -#if defined(_WIN32) || defined(_WIN64) - { - jobject lpObject1 = (*env)->GetObjectField(env, lpObject, MOUSE_EVENT_RECORDFc.mousePosition); - if (lpObject1 != NULL) getCOORDFields(env, lpObject1, &lpStruct->dwMousePosition); - } - lpStruct->dwButtonState = (*env)->GetIntField(env, lpObject, MOUSE_EVENT_RECORDFc.buttonState); - lpStruct->dwControlKeyState = (*env)->GetIntField(env, lpObject, MOUSE_EVENT_RECORDFc.controlKeyState); - lpStruct->dwEventFlags = (*env)->GetIntField(env, lpObject, MOUSE_EVENT_RECORDFc.eventFlags); -#endif - return lpStruct; -} - -void setMOUSE_EVENT_RECORDFields(JNIEnv *env, jobject lpObject, MOUSE_EVENT_RECORD *lpStruct) -{ - if (!MOUSE_EVENT_RECORDFc.cached) cacheMOUSE_EVENT_RECORDFields(env, lpObject); -#if defined(_WIN32) || defined(_WIN64) - { - jobject lpObject1 = (*env)->GetObjectField(env, lpObject, MOUSE_EVENT_RECORDFc.mousePosition); - if (lpObject1 != NULL) setCOORDFields(env, lpObject1, &lpStruct->dwMousePosition); - } - (*env)->SetIntField(env, lpObject, MOUSE_EVENT_RECORDFc.buttonState, (jint)lpStruct->dwButtonState); - (*env)->SetIntField(env, lpObject, MOUSE_EVENT_RECORDFc.controlKeyState, (jint)lpStruct->dwControlKeyState); - (*env)->SetIntField(env, lpObject, MOUSE_EVENT_RECORDFc.eventFlags, (jint)lpStruct->dwEventFlags); -#endif -} -#endif - -#if defined(_WIN32) || defined(_WIN64) -typedef struct SMALL_RECT_FID_CACHE { - int cached; - jclass clazz; - jfieldID left, top, right, bottom; -} SMALL_RECT_FID_CACHE; - -SMALL_RECT_FID_CACHE SMALL_RECTFc; - -void cacheSMALL_RECTFields(JNIEnv *env, jobject lpObject) -{ - if (SMALL_RECTFc.cached) return; - SMALL_RECTFc.clazz = (*env)->GetObjectClass(env, lpObject); - SMALL_RECTFc.left = (*env)->GetFieldID(env, SMALL_RECTFc.clazz, "left", "S"); - SMALL_RECTFc.top = (*env)->GetFieldID(env, SMALL_RECTFc.clazz, "top", "S"); - SMALL_RECTFc.right = (*env)->GetFieldID(env, SMALL_RECTFc.clazz, "right", "S"); - SMALL_RECTFc.bottom = (*env)->GetFieldID(env, SMALL_RECTFc.clazz, "bottom", "S"); - hawtjni_w_barrier(); - SMALL_RECTFc.cached = 1; -} - -SMALL_RECT *getSMALL_RECTFields(JNIEnv *env, jobject lpObject, SMALL_RECT *lpStruct) -{ - if (!SMALL_RECTFc.cached) cacheSMALL_RECTFields(env, lpObject); -#if defined(_WIN32) || defined(_WIN64) - lpStruct->Left = (*env)->GetShortField(env, lpObject, SMALL_RECTFc.left); - lpStruct->Top = (*env)->GetShortField(env, lpObject, SMALL_RECTFc.top); - lpStruct->Right = (*env)->GetShortField(env, lpObject, SMALL_RECTFc.right); - lpStruct->Bottom = (*env)->GetShortField(env, lpObject, SMALL_RECTFc.bottom); -#endif - return lpStruct; -} - -void setSMALL_RECTFields(JNIEnv *env, jobject lpObject, SMALL_RECT *lpStruct) -{ - if (!SMALL_RECTFc.cached) cacheSMALL_RECTFields(env, lpObject); -#if defined(_WIN32) || defined(_WIN64) - (*env)->SetShortField(env, lpObject, SMALL_RECTFc.left, (jshort)lpStruct->Left); - (*env)->SetShortField(env, lpObject, SMALL_RECTFc.top, (jshort)lpStruct->Top); - (*env)->SetShortField(env, lpObject, SMALL_RECTFc.right, (jshort)lpStruct->Right); - (*env)->SetShortField(env, lpObject, SMALL_RECTFc.bottom, (jshort)lpStruct->Bottom); -#endif -} -#endif - -#if defined(_WIN32) || defined(_WIN64) -typedef struct WINDOW_BUFFER_SIZE_RECORD_FID_CACHE { - int cached; - jclass clazz; - jfieldID size; -} WINDOW_BUFFER_SIZE_RECORD_FID_CACHE; - -WINDOW_BUFFER_SIZE_RECORD_FID_CACHE WINDOW_BUFFER_SIZE_RECORDFc; - -void cacheWINDOW_BUFFER_SIZE_RECORDFields(JNIEnv *env, jobject lpObject) -{ - if (WINDOW_BUFFER_SIZE_RECORDFc.cached) return; - WINDOW_BUFFER_SIZE_RECORDFc.clazz = (*env)->GetObjectClass(env, lpObject); - WINDOW_BUFFER_SIZE_RECORDFc.size = (*env)->GetFieldID(env, WINDOW_BUFFER_SIZE_RECORDFc.clazz, "size", "Lorg/fusesource/jansi/internal/Kernel32$COORD;"); - hawtjni_w_barrier(); - WINDOW_BUFFER_SIZE_RECORDFc.cached = 1; -} - -WINDOW_BUFFER_SIZE_RECORD *getWINDOW_BUFFER_SIZE_RECORDFields(JNIEnv *env, jobject lpObject, WINDOW_BUFFER_SIZE_RECORD *lpStruct) -{ - if (!WINDOW_BUFFER_SIZE_RECORDFc.cached) cacheWINDOW_BUFFER_SIZE_RECORDFields(env, lpObject); -#if defined(_WIN32) || defined(_WIN64) - { - jobject lpObject1 = (*env)->GetObjectField(env, lpObject, WINDOW_BUFFER_SIZE_RECORDFc.size); - if (lpObject1 != NULL) getCOORDFields(env, lpObject1, &lpStruct->dwSize); - } -#endif - return lpStruct; -} - -void setWINDOW_BUFFER_SIZE_RECORDFields(JNIEnv *env, jobject lpObject, WINDOW_BUFFER_SIZE_RECORD *lpStruct) -{ - if (!WINDOW_BUFFER_SIZE_RECORDFc.cached) cacheWINDOW_BUFFER_SIZE_RECORDFields(env, lpObject); -#if defined(_WIN32) || defined(_WIN64) - { - jobject lpObject1 = (*env)->GetObjectField(env, lpObject, WINDOW_BUFFER_SIZE_RECORDFc.size); - if (lpObject1 != NULL) setCOORDFields(env, lpObject1, &lpStruct->dwSize); - } -#endif -} -#endif - diff --git a/src/main/native/jansi_structs.h b/src/main/native/jansi_structs.h deleted file mode 100644 index e1c8d30b..00000000 --- a/src/main/native/jansi_structs.h +++ /dev/null @@ -1,137 +0,0 @@ -/******************************************************************************* - * Copyright (C) 2009-2017 the original author(s). - * - * 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. - *******************************************************************************/ -#include "jansi.h" - -#if defined(HAVE_IOCTL) -void cacheTermiosFields(JNIEnv *env, jobject lpObject); -struct termios *getTermiosFields(JNIEnv *env, jobject lpObject, struct termios *lpStruct); -void setTermiosFields(JNIEnv *env, jobject lpObject, struct termios *lpStruct); -#else -#define cacheTermiosFields(a,b) -#define getTermiosFields(a,b,c) NULL -#define setTermiosFields(a,b,c) -#endif - -#if defined(HAVE_IOCTL) -void cacheWinSizeFields(JNIEnv *env, jobject lpObject); -struct winsize *getWinSizeFields(JNIEnv *env, jobject lpObject, struct winsize *lpStruct); -void setWinSizeFields(JNIEnv *env, jobject lpObject, struct winsize *lpStruct); -#else -#define cacheWinSizeFields(a,b) -#define getWinSizeFields(a,b,c) NULL -#define setWinSizeFields(a,b,c) -#endif - -#if defined(_WIN32) || defined(_WIN64) -void cacheCHAR_INFOFields(JNIEnv *env, jobject lpObject); -CHAR_INFO *getCHAR_INFOFields(JNIEnv *env, jobject lpObject, CHAR_INFO *lpStruct); -void setCHAR_INFOFields(JNIEnv *env, jobject lpObject, CHAR_INFO *lpStruct); -#else -#define cacheCHAR_INFOFields(a,b) -#define getCHAR_INFOFields(a,b,c) NULL -#define setCHAR_INFOFields(a,b,c) -#endif - -#if defined(_WIN32) || defined(_WIN64) -void cacheCONSOLE_SCREEN_BUFFER_INFOFields(JNIEnv *env, jobject lpObject); -CONSOLE_SCREEN_BUFFER_INFO *getCONSOLE_SCREEN_BUFFER_INFOFields(JNIEnv *env, jobject lpObject, CONSOLE_SCREEN_BUFFER_INFO *lpStruct); -void setCONSOLE_SCREEN_BUFFER_INFOFields(JNIEnv *env, jobject lpObject, CONSOLE_SCREEN_BUFFER_INFO *lpStruct); -#else -#define cacheCONSOLE_SCREEN_BUFFER_INFOFields(a,b) -#define getCONSOLE_SCREEN_BUFFER_INFOFields(a,b,c) NULL -#define setCONSOLE_SCREEN_BUFFER_INFOFields(a,b,c) -#endif - -#if defined(_WIN32) || defined(_WIN64) -void cacheCOORDFields(JNIEnv *env, jobject lpObject); -COORD *getCOORDFields(JNIEnv *env, jobject lpObject, COORD *lpStruct); -void setCOORDFields(JNIEnv *env, jobject lpObject, COORD *lpStruct); -#else -#define cacheCOORDFields(a,b) -#define getCOORDFields(a,b,c) NULL -#define setCOORDFields(a,b,c) -#endif - -#if defined(_WIN32) || defined(_WIN64) -void cacheFOCUS_EVENT_RECORDFields(JNIEnv *env, jobject lpObject); -FOCUS_EVENT_RECORD *getFOCUS_EVENT_RECORDFields(JNIEnv *env, jobject lpObject, FOCUS_EVENT_RECORD *lpStruct); -void setFOCUS_EVENT_RECORDFields(JNIEnv *env, jobject lpObject, FOCUS_EVENT_RECORD *lpStruct); -#else -#define cacheFOCUS_EVENT_RECORDFields(a,b) -#define getFOCUS_EVENT_RECORDFields(a,b,c) NULL -#define setFOCUS_EVENT_RECORDFields(a,b,c) -#endif - -#if defined(_WIN32) || defined(_WIN64) -void cacheINPUT_RECORDFields(JNIEnv *env, jobject lpObject); -INPUT_RECORD *getINPUT_RECORDFields(JNIEnv *env, jobject lpObject, INPUT_RECORD *lpStruct); -void setINPUT_RECORDFields(JNIEnv *env, jobject lpObject, INPUT_RECORD *lpStruct); -#else -#define cacheINPUT_RECORDFields(a,b) -#define getINPUT_RECORDFields(a,b,c) NULL -#define setINPUT_RECORDFields(a,b,c) -#endif - -#if defined(_WIN32) || defined(_WIN64) -void cacheKEY_EVENT_RECORDFields(JNIEnv *env, jobject lpObject); -KEY_EVENT_RECORD *getKEY_EVENT_RECORDFields(JNIEnv *env, jobject lpObject, KEY_EVENT_RECORD *lpStruct); -void setKEY_EVENT_RECORDFields(JNIEnv *env, jobject lpObject, KEY_EVENT_RECORD *lpStruct); -#else -#define cacheKEY_EVENT_RECORDFields(a,b) -#define getKEY_EVENT_RECORDFields(a,b,c) NULL -#define setKEY_EVENT_RECORDFields(a,b,c) -#endif - -#if defined(_WIN32) || defined(_WIN64) -void cacheMENU_EVENT_RECORDFields(JNIEnv *env, jobject lpObject); -MENU_EVENT_RECORD *getMENU_EVENT_RECORDFields(JNIEnv *env, jobject lpObject, MENU_EVENT_RECORD *lpStruct); -void setMENU_EVENT_RECORDFields(JNIEnv *env, jobject lpObject, MENU_EVENT_RECORD *lpStruct); -#else -#define cacheMENU_EVENT_RECORDFields(a,b) -#define getMENU_EVENT_RECORDFields(a,b,c) NULL -#define setMENU_EVENT_RECORDFields(a,b,c) -#endif - -#if defined(_WIN32) || defined(_WIN64) -void cacheMOUSE_EVENT_RECORDFields(JNIEnv *env, jobject lpObject); -MOUSE_EVENT_RECORD *getMOUSE_EVENT_RECORDFields(JNIEnv *env, jobject lpObject, MOUSE_EVENT_RECORD *lpStruct); -void setMOUSE_EVENT_RECORDFields(JNIEnv *env, jobject lpObject, MOUSE_EVENT_RECORD *lpStruct); -#else -#define cacheMOUSE_EVENT_RECORDFields(a,b) -#define getMOUSE_EVENT_RECORDFields(a,b,c) NULL -#define setMOUSE_EVENT_RECORDFields(a,b,c) -#endif - -#if defined(_WIN32) || defined(_WIN64) -void cacheSMALL_RECTFields(JNIEnv *env, jobject lpObject); -SMALL_RECT *getSMALL_RECTFields(JNIEnv *env, jobject lpObject, SMALL_RECT *lpStruct); -void setSMALL_RECTFields(JNIEnv *env, jobject lpObject, SMALL_RECT *lpStruct); -#else -#define cacheSMALL_RECTFields(a,b) -#define getSMALL_RECTFields(a,b,c) NULL -#define setSMALL_RECTFields(a,b,c) -#endif - -#if defined(_WIN32) || defined(_WIN64) -void cacheWINDOW_BUFFER_SIZE_RECORDFields(JNIEnv *env, jobject lpObject); -WINDOW_BUFFER_SIZE_RECORD *getWINDOW_BUFFER_SIZE_RECORDFields(JNIEnv *env, jobject lpObject, WINDOW_BUFFER_SIZE_RECORD *lpStruct); -void setWINDOW_BUFFER_SIZE_RECORDFields(JNIEnv *env, jobject lpObject, WINDOW_BUFFER_SIZE_RECORD *lpStruct); -#else -#define cacheWINDOW_BUFFER_SIZE_RECORDFields(a,b) -#define getWINDOW_BUFFER_SIZE_RECORDFields(a,b,c) NULL -#define setWINDOW_BUFFER_SIZE_RECORDFields(a,b,c) -#endif - diff --git a/src/main/native/jansi_ttyname.c b/src/main/native/jansi_ttyname.c deleted file mode 100644 index d65e86cb..00000000 --- a/src/main/native/jansi_ttyname.c +++ /dev/null @@ -1,34 +0,0 @@ -/******************************************************************************* - * Copyright (C) 2017, the original author(s). - * - * 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. - *******************************************************************************/ -#include "jansi.h" -#include "jansi_structs.h" - -#define CLibrary_NATIVE(func) Java_org_fusesource_jansi_internal_CLibrary_##func - -#if defined(HAVE_TTYNAME) -JNIEXPORT jstring JNICALL CLibrary_NATIVE(ttyname) - (JNIEnv *env, jclass that, jint arg0) -{ - jstring rc = 0; - char s[256] = { 0 }; - int r = 0; - - r = ttyname_r(arg0, s, 256); - if (!r) rc = (*env)->NewStringUTF(env,s); - - return rc; -} -#endif diff --git a/src/main/resources/META-INF/native-image/jansi/native-image.properties b/src/main/resources/META-INF/native-image/jansi/native-image.properties new file mode 100644 index 00000000..6cc8f4e4 --- /dev/null +++ b/src/main/resources/META-INF/native-image/jansi/native-image.properties @@ -0,0 +1 @@ +Args=--features=org.fusesource.jansi.internal.NativeImageFeature \ No newline at end of file diff --git a/src/main/resources/META-INF/native-image/jansi/resource-config.json b/src/main/resources/META-INF/native-image/jansi/resource-config.json index e062c81e..794d8996 100644 --- a/src/main/resources/META-INF/native-image/jansi/resource-config.json +++ b/src/main/resources/META-INF/native-image/jansi/resource-config.json @@ -1,7 +1,6 @@ { "resources": [ {"pattern": "org/fusesource/jansi/jansi.properties"}, - {"pattern": "org/fusesource/jansi/jansi.txt"}, - {"pattern": "org/fusesource/jansi/internal/native/.*"} + {"pattern": "org/fusesource/jansi/jansi.txt"} ] } \ No newline at end of file diff --git a/src/main/resources/org/fusesource/jansi/internal/native/FreeBSD/x86/libjansi.so b/src/main/resources/org/fusesource/jansi/internal/native/FreeBSD/x86/libjansi.so deleted file mode 100755 index 3e680e50..00000000 Binary files a/src/main/resources/org/fusesource/jansi/internal/native/FreeBSD/x86/libjansi.so and /dev/null differ diff --git a/src/main/resources/org/fusesource/jansi/internal/native/FreeBSD/x86_64/libjansi.so b/src/main/resources/org/fusesource/jansi/internal/native/FreeBSD/x86_64/libjansi.so deleted file mode 100755 index 17212586..00000000 Binary files a/src/main/resources/org/fusesource/jansi/internal/native/FreeBSD/x86_64/libjansi.so and /dev/null differ diff --git a/src/main/resources/org/fusesource/jansi/internal/native/Linux/arm/libjansi.so b/src/main/resources/org/fusesource/jansi/internal/native/Linux/arm/libjansi.so deleted file mode 100755 index d782352b..00000000 Binary files a/src/main/resources/org/fusesource/jansi/internal/native/Linux/arm/libjansi.so and /dev/null differ diff --git a/src/main/resources/org/fusesource/jansi/internal/native/Linux/arm64/libjansi.so b/src/main/resources/org/fusesource/jansi/internal/native/Linux/arm64/libjansi.so deleted file mode 100755 index 447b13cd..00000000 Binary files a/src/main/resources/org/fusesource/jansi/internal/native/Linux/arm64/libjansi.so and /dev/null differ diff --git a/src/main/resources/org/fusesource/jansi/internal/native/Linux/armv6/libjansi.so b/src/main/resources/org/fusesource/jansi/internal/native/Linux/armv6/libjansi.so deleted file mode 100755 index 3d9631eb..00000000 Binary files a/src/main/resources/org/fusesource/jansi/internal/native/Linux/armv6/libjansi.so and /dev/null differ diff --git a/src/main/resources/org/fusesource/jansi/internal/native/Linux/armv7/libjansi.so b/src/main/resources/org/fusesource/jansi/internal/native/Linux/armv7/libjansi.so deleted file mode 100755 index 823799cb..00000000 Binary files a/src/main/resources/org/fusesource/jansi/internal/native/Linux/armv7/libjansi.so and /dev/null differ diff --git a/src/main/resources/org/fusesource/jansi/internal/native/Linux/ppc64/libjansi.so b/src/main/resources/org/fusesource/jansi/internal/native/Linux/ppc64/libjansi.so deleted file mode 100755 index 8dd27d97..00000000 Binary files a/src/main/resources/org/fusesource/jansi/internal/native/Linux/ppc64/libjansi.so and /dev/null differ diff --git a/src/main/resources/org/fusesource/jansi/internal/native/Linux/x86/libjansi.so b/src/main/resources/org/fusesource/jansi/internal/native/Linux/x86/libjansi.so deleted file mode 100755 index b28635a2..00000000 Binary files a/src/main/resources/org/fusesource/jansi/internal/native/Linux/x86/libjansi.so and /dev/null differ diff --git a/src/main/resources/org/fusesource/jansi/internal/native/Linux/x86_64/libjansi.so b/src/main/resources/org/fusesource/jansi/internal/native/Linux/x86_64/libjansi.so deleted file mode 100755 index fa51f8ad..00000000 Binary files a/src/main/resources/org/fusesource/jansi/internal/native/Linux/x86_64/libjansi.so and /dev/null differ diff --git a/src/main/resources/org/fusesource/jansi/internal/native/Mac/arm64/libjansi.jnilib b/src/main/resources/org/fusesource/jansi/internal/native/Mac/arm64/libjansi.jnilib deleted file mode 100755 index 47da878b..00000000 Binary files a/src/main/resources/org/fusesource/jansi/internal/native/Mac/arm64/libjansi.jnilib and /dev/null differ diff --git a/src/main/resources/org/fusesource/jansi/internal/native/Mac/x86/libjansi.jnilib b/src/main/resources/org/fusesource/jansi/internal/native/Mac/x86/libjansi.jnilib deleted file mode 100755 index 468bb86e..00000000 Binary files a/src/main/resources/org/fusesource/jansi/internal/native/Mac/x86/libjansi.jnilib and /dev/null differ diff --git a/src/main/resources/org/fusesource/jansi/internal/native/Mac/x86_64/libjansi.jnilib b/src/main/resources/org/fusesource/jansi/internal/native/Mac/x86_64/libjansi.jnilib deleted file mode 100755 index 8c75b2c6..00000000 Binary files a/src/main/resources/org/fusesource/jansi/internal/native/Mac/x86_64/libjansi.jnilib and /dev/null differ diff --git a/src/main/resources/org/fusesource/jansi/internal/native/Windows/arm64/libjansi.so b/src/main/resources/org/fusesource/jansi/internal/native/Windows/arm64/libjansi.so deleted file mode 100755 index b7e3527b..00000000 Binary files a/src/main/resources/org/fusesource/jansi/internal/native/Windows/arm64/libjansi.so and /dev/null differ diff --git a/src/main/resources/org/fusesource/jansi/internal/native/Windows/x86/jansi.dll b/src/main/resources/org/fusesource/jansi/internal/native/Windows/x86/jansi.dll deleted file mode 100755 index 298e99d2..00000000 Binary files a/src/main/resources/org/fusesource/jansi/internal/native/Windows/x86/jansi.dll and /dev/null differ diff --git a/src/main/resources/org/fusesource/jansi/internal/native/Windows/x86_64/jansi.dll b/src/main/resources/org/fusesource/jansi/internal/native/Windows/x86_64/jansi.dll deleted file mode 100755 index 8649e32c..00000000 Binary files a/src/main/resources/org/fusesource/jansi/internal/native/Windows/x86_64/jansi.dll and /dev/null differ diff --git a/src/main/resources/org/fusesource/jansi/jansi.txt b/src/main/resources/org/fusesource/jansi/jansi.txt index 247afd25..a62a6f40 100644 --- a/src/main/resources/org/fusesource/jansi/jansi.txt +++ b/src/main/resources/org/fusesource/jansi/jansi.txt @@ -1,5 +1,5 @@ -[?7h -┌──┐┌─────┐ ┌─────┐ ┌──────┬──┐ +[?7h +┌──┐┌─────┐ ┌─────┐ ┌──────┬──┐ │██├┘█████└┬┘█████└┬┘██████│▐▌│ ┌──┐ │██│██▄▄▄██│██┌─┐██│██▄▄▄▄ │▄▄│ │▒▒└─┘▒█│▒█┌─┐▒█│▒█│ │▒█│ ▀▀▀▀▒█│▒█│ diff --git a/src/test/java/org/fusesource/jansi/AnsiTest.java b/src/test/java/org/fusesource/jansi/AnsiTest.java index 2e4a6453..1fbd5bc3 100644 --- a/src/test/java/org/fusesource/jansi/AnsiTest.java +++ b/src/test/java/org/fusesource/jansi/AnsiTest.java @@ -15,12 +15,22 @@ */ package org.fusesource.jansi; +import java.io.ByteArrayOutputStream; +import java.io.InputStream; +import java.nio.file.Path; +import java.nio.file.Paths; + import org.fusesource.jansi.Ansi.Color; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.EnabledOnOs; +import org.junit.jupiter.api.condition.OS; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * Tests for the {@link Ansi} class. @@ -30,20 +40,10 @@ public class AnsiTest { @Test public void testSetEnabled() throws Exception { Ansi.setEnabled(false); - new Thread() { - @Override - public void run() { - assertEquals(false, Ansi.isEnabled()); - } - }.run(); + new Thread(() -> assertFalse(Ansi.isEnabled())).run(); Ansi.setEnabled(true); - new Thread() { - @Override - public void run() { - assertEquals(true, Ansi.isEnabled()); - } - }.run(); + new Thread(() -> assertTrue(Ansi.isEnabled())).run(); } @Test @@ -56,15 +56,7 @@ public void testClone() throws CloneNotSupportedException { @Test public void testApply() { - assertEquals( - "test", - Ansi.ansi() - .apply(new Ansi.Consumer() { - public void apply(Ansi ansi) { - ansi.a("test"); - } - }) - .toString()); + assertEquals("test", Ansi.ansi().apply(ansi -> ansi.a("test")).toString()); } @ParameterizedTest @@ -181,6 +173,34 @@ public void testColorDisabled() { } } + @Test + @EnabledOnOs(OS.WINDOWS) + @Disabled("Does not really fail: launch `javaw -jar jansi-xxx.jar` directly instead") + public void testAnsiMainWithNoConsole() throws Exception { + Path javaHome = Paths.get(System.getProperty("java.home")); + Path java = javaHome.resolve("bin\\javaw.exe"); + String cp = System.getProperty("java.class.path"); + + Process process = new ProcessBuilder() + .command(java.toString(), "-cp", cp, AnsiMain.class.getName()) + .start(); + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + try (InputStream in = process.getInputStream()) { + byte[] buffer = new byte[8192]; + while (true) { + int nb = in.read(buffer); + if (nb > 0) { + baos.write(buffer, 0, nb); + } else { + break; + } + } + } + + assertTrue(baos.toString().contains("test on System.out"), baos.toString()); + } + private static void assertAnsi(String expected, Ansi actual) { assertEquals(expected.replace("ESC", "\033"), actual.toString()); } diff --git a/src/test/java/org/fusesource/jansi/EncodingTest.java b/src/test/java/org/fusesource/jansi/EncodingTest.java index 4cccc88f..d6befd2d 100644 --- a/src/test/java/org/fusesource/jansi/EncodingTest.java +++ b/src/test/java/org/fusesource/jansi/EncodingTest.java @@ -18,7 +18,7 @@ import java.io.ByteArrayOutputStream; import java.io.PrintStream; import java.io.UnsupportedEncodingException; -import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.concurrent.atomic.AtomicReference; import org.fusesource.jansi.io.AnsiOutputStream; @@ -32,7 +32,7 @@ public class EncodingTest { @Test public void testEncoding8859() throws UnsupportedEncodingException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); - final AtomicReference newLabel = new AtomicReference(); + final AtomicReference newLabel = new AtomicReference<>(); PrintStream ansi = new AnsiPrintStream( new AnsiOutputStream( baos, @@ -46,7 +46,7 @@ protected void processChangeWindowTitle(String label) { }, AnsiType.Emulation, AnsiColors.TrueColor, - Charset.forName("ISO-8859-1"), + StandardCharsets.ISO_8859_1, null, null, false), @@ -61,7 +61,7 @@ protected void processChangeWindowTitle(String label) { @Test public void testEncodingUtf8() throws UnsupportedEncodingException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); - final AtomicReference newLabel = new AtomicReference(); + final AtomicReference newLabel = new AtomicReference<>(); PrintStream ansi = new PrintStream( new AnsiOutputStream( baos, @@ -75,7 +75,7 @@ protected void processChangeWindowTitle(String label) { }, AnsiType.Emulation, AnsiColors.TrueColor, - Charset.forName("UTF-8"), + StandardCharsets.UTF_8, null, null, false), diff --git a/src/test/java/org/fusesource/jansi/internal/JansiLoaderTest.java b/src/test/java/org/fusesource/jansi/internal/JansiLoaderTest.java index a9a3c15c..8bcab3e5 100644 --- a/src/test/java/org/fusesource/jansi/internal/JansiLoaderTest.java +++ b/src/test/java/org/fusesource/jansi/internal/JansiLoaderTest.java @@ -15,12 +15,10 @@ */ package org.fusesource.jansi.internal; -import org.junit.jupiter.api.Test; - public class JansiLoaderTest { - @Test - public void testLoadJansi() { - JansiLoader.initialize(); - } + // @Test + // public void testLoadJansi() { + // JansiLoader.initialize(); + // } } diff --git a/src/test/java/org/fusesource/jansi/io/AnsiOutputStreamTest.java b/src/test/java/org/fusesource/jansi/io/AnsiOutputStreamTest.java index 49e6dae1..96353408 100644 --- a/src/test/java/org/fusesource/jansi/io/AnsiOutputStreamTest.java +++ b/src/test/java/org/fusesource/jansi/io/AnsiOutputStreamTest.java @@ -17,7 +17,7 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; -import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import org.fusesource.jansi.AnsiColors; import org.fusesource.jansi.AnsiMode; @@ -38,7 +38,7 @@ void canHandleSgrsWithMultipleOptions() throws IOException { null, AnsiType.Emulation, AnsiColors.TrueColor, - Charset.forName("UTF-8"), + StandardCharsets.UTF_8, null, null, false);