Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for FFM on JDK 21 (fixes #230) #259

Merged
merged 6 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
60 changes: 57 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,67 @@
</resources>

<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>3.4.1</version>
<executions>
<execution>
<id>enforce-java</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requireJavaVersion>
<version>21</version>
</requireJavaVersion>
</rules>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<version>3.11.0</version>
<configuration>
<fork>true</fork>
<source>${jdkTarget}</source>
<target>${jdkTarget}</target>
<release>${jdkTarget}</release>
<compilerArgs>
<arg>-Xlint:-options</arg>
</compilerArgs>
</configuration>
<executions>
<execution>
<id>default-compile</id>
<configuration>
<excludes>
<exclude>**/ffm/*.java</exclude>
</excludes>
</configuration>
</execution>
<execution>
<id>jdk-21</id>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<release>21</release>
<includes>
<include>**/ffm/*.java</include>
</includes>
<compilerArgs>
<arg>--enable-preview</arg>
</compilerArgs>
</configuration>
</execution>
<execution>
<id>default-testCompile</id>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
Expand Down Expand Up @@ -351,12 +405,12 @@
<plugin>
<groupId>com.diffplug.spotless</groupId>
<artifactId>spotless-maven-plugin</artifactId>
<version>2.38.0</version>
<version>2.39.0</version>
<configuration>
<java>
<toggleOffOn />
<palantirJavaFormat>
<version>2.35.0</version>
<version>2.38.0</version>
</palantirJavaFormat>
<importOrder>
<order>java|javax,org,,\#</order>
Expand Down
65 changes: 26 additions & 39 deletions src/main/java/org/fusesource/jansi/AnsiConsole.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,9 @@
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;

/**
* Provides consistent access to an ANSI aware console PrintStream or an ANSI codes stripping PrintStream
Expand Down Expand Up @@ -167,6 +154,11 @@ public class AnsiConsole {
*/
public static final String JANSI_GRACEFUL = "jansi.graceful";

public static final String JANSI_PROVIDERS = "jansi.providers";
public static final String JANSI_PROVIDER_JNI = "jni";
public static final String JANSI_PROVIDER_FFM = "ffm";
public static final String JANSI_PROVIDERS_DEFAULT = JANSI_PROVIDER_FFM + "," + JANSI_PROVIDER_JNI;

/**
* @deprecated this field will be made private in a future release, use {@link #sysOut()} instead
*/
Expand Down Expand Up @@ -246,9 +238,9 @@ private static AnsiPrintStream ansiStream(boolean stdout) {
// the library can not be loaded on unsupported platforms
final int fd = stdout ? STDOUT_FILENO : STDERR_FILENO;
try {
// If we can detect that stdout is not a tty.. then setup
// to strip the ANSI sequences..
isAtty = isatty(fd) != 0;
// If we can detect that stdout is not a tty, then setup
// to strip the ANSI sequences...
isAtty = getCLibrary().isTty(fd) != 0;
String term = System.getenv("TERM");
String emacs = System.getenv("INSIDE_EMACS");
if (isAtty && "dumb".equals(term) && emacs != null && !emacs.contains("comint")) {
Expand All @@ -274,25 +266,26 @@ private static AnsiPrintStream ansiStream(boolean stdout) {
installer = uninstaller = null;
width = new AnsiOutputStream.ZeroWidthSupplier();
} else if (IS_WINDOWS) {
final long console = GetStdHandle(stdout ? STD_OUTPUT_HANDLE : STD_ERROR_HANDLE);
final long console = getKernel32().getStdHandle(stdout);
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
final boolean isConsole = getKernel32().getConsoleMode(console, mode) != 0;
if (isConsole && getKernel32().setConsoleMode(console, mode[0] | ENABLE_VIRTUAL_TERMINAL_PROCESSING) != 0) {
// set it back for now, but we know it works
getKernel32().setConsoleMode(console, mode[0]);
processor = null;
type = AnsiType.VirtualTerminal;
installer = new AnsiOutputStream.IoRunnable() {
@Override
public void run() throws IOException {
virtualProcessing++;
SetConsoleMode(console, mode[0] | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
getKernel32().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]);
getKernel32().setConsoleMode(console, mode[0]);
}
}
};
Expand All @@ -308,7 +301,7 @@ public void run() throws IOException {
AnsiProcessor proc;
AnsiType ttype;
try {
proc = new WindowsAnsiProcessor(out, console);
proc = getKernel32().newProcessor(out, console);
ttype = AnsiType.Emulation;
} catch (Throwable ignore) {
// this happens when the stdout is being redirected to a file.
Expand All @@ -320,14 +313,7 @@ public void run() throws IOException {
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();
}
};
width = () -> getKernel32().getTerminalWidth(console);
}

// We must be on some Unix variant...
Expand All @@ -336,14 +322,7 @@ public int getTerminalWidth() {
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;
}
};
width = () -> getCLibrary().getTerminalWidth(fd);
}

AnsiMode mode;
Expand Down Expand Up @@ -553,4 +532,12 @@ static synchronized void initStreams() {
initialized = true;
}
}

private static AnsiConsoleSupport.Kernel32 getKernel32() {
return AnsiConsoleSupport.getInstance().getKernel32();
}

private static AnsiConsoleSupport.CLibrary getCLibrary() {
return AnsiConsoleSupport.getInstance().getCLibrary();
}
}
63 changes: 63 additions & 0 deletions src/main/java/org/fusesource/jansi/AnsiConsoleSupport.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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;

import java.io.IOException;
import java.io.OutputStream;

import org.fusesource.jansi.io.AnsiProcessor;

public interface AnsiConsoleSupport {

interface CLibrary {

int STDOUT_FILENO = 1;
int STDERR_FILENO = 2;

short getTerminalWidth(int fd);

int isTty(int fd);
}

interface Kernel32 {

int isTty(long console);

int getTerminalWidth(long console);

long getStdHandle(boolean stdout);

int getConsoleMode(long console, int[] mode);

int setConsoleMode(long console, int mode);

int getLastError();

String getErrorMessage(int errorCode);

AnsiProcessor newProcessor(OutputStream os, long console) throws IOException;
}

String getProviderName();

CLibrary getCLibrary();

Kernel32 getKernel32();

static AnsiConsoleSupport getInstance() {
return AnsiConsoleSupportHolder.get();
}
}
60 changes: 60 additions & 0 deletions src/main/java/org/fusesource/jansi/AnsiConsoleSupportHolder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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;

import org.fusesource.jansi.internal.AnsiConsoleSupportJni;

import static org.fusesource.jansi.AnsiConsole.JANSI_PROVIDERS;
import static org.fusesource.jansi.AnsiConsole.JANSI_PROVIDERS_DEFAULT;
import static org.fusesource.jansi.AnsiConsole.JANSI_PROVIDER_FFM;
import static org.fusesource.jansi.AnsiConsole.JANSI_PROVIDER_JNI;

class AnsiConsoleSupportHolder {
static volatile AnsiConsoleSupport instance;

static AnsiConsoleSupport get() {
if (instance == null) {
synchronized (AnsiConsoleSupportHolder.class) {
if (instance == null) {
instance = doGet();
}
}
}
return instance;
}

static AnsiConsoleSupport doGet() {
RuntimeException error = new RuntimeException("Unable to create AnsiConsoleSupport provider");
String[] providers =
System.getProperty(JANSI_PROVIDERS, JANSI_PROVIDERS_DEFAULT).split(",");
for (String provider : providers) {
try {
if (JANSI_PROVIDER_FFM.equals(provider)) {
return (AnsiConsoleSupport) AnsiConsoleSupport.class
.getClassLoader()
.loadClass("org.fusesource.jansi.ffm.AnsiConsoleSupportFfm")
.getConstructor()
.newInstance();
} else if (JANSI_PROVIDER_JNI.equals(provider)) {
return new AnsiConsoleSupportJni();
}
} catch (Throwable t) {
error.addSuppressed(t);
}
}
throw error;
}
}
Loading