5 Commits

Author SHA1 Message Date
rudi 1212b2cceb Add tn3270(no e) and fix animations
Release j3270 / Build & Publish Release (push) Successful in 42s
Build and Test j3270 / Build JAR & Run Tests (push) Successful in 1m10s
2026-08-21 16:32:48 -04:00
rudi bf5e4410e6 Additional logging to debug a3270
Release j3270 / Build & Publish Release (push) Successful in 41s
Build and Test j3270 / Build JAR & Run Tests (push) Successful in 1m6s
2026-08-21 12:51:31 -04:00
rudi 7e658a6c20 Update readme
Build and Test j3270 / Build JAR & Run Tests (push) Successful in 1m4s
2026-08-21 11:57:14 -04:00
rudi c32521fd4b Cleaner rendering
Build and Test j3270 / Build JAR & Run Tests (push) Successful in 1m6s
2026-08-21 11:51:37 -04:00
rudi d7d2f8bad5 TLS and Graphics, what more?
Build and Test j3270 / Build JAR & Run Tests (push) Successful in 43s
Release j3270 / Build & Publish Release (push) Successful in 1m7s
2026-08-21 02:51:24 -04:00
60 changed files with 4439 additions and 244 deletions
+1
View File
@@ -1,3 +1,4 @@
j3270.log.*
*.log *.log
.DS_Store .DS_Store
*.jar *.jar
+25 -4
View File
@@ -1,7 +1,7 @@
# j3270 # j3270
[![Build & Test](https://git.hugfreevikings.wtf/rudi/j3270/actions/workflows/build.yaml/badge.svg)](https://git.hugfreevikings.wtf/rudi/j3270/actions) [![Build & Test](https://git.hugfreevikings.wtf/rudi/j3270/actions/workflows/build.yaml/badge.svg)](https://git.hugfreevikings.wtf/rudi/j3270/actions)
[![Latest Release](https://git.hugfreevikings.wtf/rudi/j3270/badges/release.svg)](https://git.hugfreevikings.wtf/rudi/j3270/releases) [![Releases](https://img.shields.io/badge/Releases-Gitea-blue.svg)](https://git.hugfreevikings.wtf/rudi/j3270/releases)
[![Java](https://img.shields.io/badge/Java-11%20%7C%2017%20%7C%2021-blue.svg)](https://adoptium.net) [![Java](https://img.shields.io/badge/Java-11%20%7C%2017%20%7C%2021-blue.svg)](https://adoptium.net)
[![License](https://img.shields.io/badge/License-MIT%20%2F%20BSD-green.svg)](LICENSE) [![License](https://img.shields.io/badge/License-MIT%20%2F%20BSD-green.svg)](LICENSE)
@@ -24,6 +24,19 @@ Requires **Java 11 or higher** (Java 11, 17, 21+):
# Launch interactive GUI # Launch interactive GUI
java -jar j3270.jar java -jar j3270.jar
# Connect to TLS/SSL mainframe with custom port
java -jar j3270.jar -s mainframe.example.com 992
# Or using standard x3270 L: prefix
java -jar j3270.jar L:mainframe.example.com:992
# Connect via Plain TN3270 (Non-E)
java -jar j3270.jar -P mainframe.example.com 23
# Or using standard x3270 P: prefix
java -jar j3270.jar P:mainframe.example.com:23
# Connect with unverified/self-signed certificate verification bypass
java -jar j3270.jar --tls --insecure mainframe.example.com 992
# Launch with custom configuration file # Launch with custom configuration file
java -jar j3270.jar -c config.ini java -jar j3270.jar -c config.ini
@@ -35,7 +48,11 @@ java -jar j3270.jar -c config.ini
## ✨ Features ## ✨ Features
- **TN3270 & TN3270E Protocol Support**: RFC 2355 compliant state machine, negotiation, Device-Type query, and SSL/TLS encryption. - **TN3270 & TN3270E Protocol Support**: RFC 2355 compliant state machine, negotiation, Device-Type query, plain TN3270 fallback, and SSL/TLS encryption.
- **SSL/TLS Security**:
- Encrypted TN3270 over TLS connections on standard port `992` or custom ports.
- Interactive certificate verification prompt for self-signed or untrusted certificates with fingerprint, subject, issuer, and validity inspection.
- Optional unverified/insecure mode for testing and headless scripting.
- **IND$FILE File Transfer**: Full support for both **CUT** and **DFT (DDM)** high-speed structured field transfers with ASCII/binary translation, CRLF handling, and recfm/lrecl formatting for TSO, VM/CMS, and CICS. - **IND$FILE File Transfer**: Full support for both **CUT** and **DFT (DDM)** high-speed structured field transfers with ASCII/binary translation, CRLF handling, and recfm/lrecl formatting for TSO, VM/CMS, and CICS.
- **z/VM & Line-Mode Support**: Proper SSCP-LU and unformatted line handling (`CP TERM CONMODE 3270` supported). - **z/VM & Line-Mode Support**: Proper SSCP-LU and unformatted line handling (`CP TERM CONMODE 3270` supported).
- **APL & Graphic Escape**: Comprehensive box-drawing and math symbol character rendering. - **APL & Graphic Escape**: Comprehensive box-drawing and math symbol character rendering.
@@ -59,7 +76,7 @@ The project includes self-contained, portable build scripts:
# Build executable JAR in 2 seconds: # Build executable JAR in 2 seconds:
sh ./build_all.sh sh ./build_all.sh
# Run all 25 automated unit tests in ~130ms: # Run all 56 automated unit tests in ~500ms:
sh ./test_all.sh sh ./test_all.sh
``` ```
@@ -82,6 +99,10 @@ Contributions and issue reports are welcome! Please feel free to open a bug repo
## 📜 Acknowledgements ## 📜 Acknowledgements
- [x3270](https://x3270.miraheze.org/wiki/Main_Page) — Reference C implementation and protocol specifications - [x3270](https://x3270.miraheze.org/wiki/Main_Page) — Reference C implementation and protocol specifications
- [Antigravity](https://antigravity.google) - [Antigravity](https://antigravity.google/)
- Claude Opus 4.6 - Claude Opus 4.6
- Gemini 3.1 Pro - Gemini 3.1 Pro
- Gemini 3.7 Flash
- Gemma4 12B and 26B
- GPT-OSS 120B
- Qwen3 4B and 32B
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+113 -39
View File
@@ -34,6 +34,7 @@ public class J3270App extends JFrame implements ConnectionListener, ScreenUpdate
private int lastPort = 23; private int lastPort = 23;
private FileTransfer fileTransfer; private FileTransfer fileTransfer;
private final java.util.concurrent.atomic.AtomicBoolean screenUpdatePending = new java.util.concurrent.atomic.AtomicBoolean(false);
public J3270App() { public J3270App() {
super("j3270 — Java TN3270 Terminal Emulator"); super("j3270 — Java TN3270 Terminal Emulator");
@@ -227,8 +228,12 @@ public class J3270App extends JFrame implements ConnectionListener, ScreenUpdate
private void showConnectDialog() { private void showConnectDialog() {
ConnectDialog dialog = new ConnectDialog(this); ConnectDialog dialog = new ConnectDialog(this);
dialog.setInitialHost(lastHost); String initialHost = lastHost != null && !lastHost.isEmpty() ? lastHost : org.pubvm.j3270.config.Settings.getAutoConnectHost();
dialog.setInitialPort(lastPort); int initialPort = lastPort > 0 ? lastPort : org.pubvm.j3270.config.Settings.getAutoConnectPort();
dialog.setInitialHost(initialHost);
dialog.setInitialPort(initialPort);
dialog.setInitialTls(org.pubvm.j3270.config.Settings.getAutoConnectTls());
dialog.setInitialVerifyCert(org.pubvm.j3270.config.Settings.getAutoConnectVerifyCert());
dialog.setVisible(true); dialog.setVisible(true);
if (dialog.isConfirmed()) { if (dialog.isConfirmed()) {
@@ -255,10 +260,19 @@ public class J3270App extends JFrame implements ConnectionListener, ScreenUpdate
// Save for auto-connect // Save for auto-connect
org.pubvm.j3270.config.Settings.setAutoConnectHost(config.getHost()); org.pubvm.j3270.config.Settings.setAutoConnectHost(config.getHost());
org.pubvm.j3270.config.Settings.setAutoConnectPort(config.getPort()); org.pubvm.j3270.config.Settings.setAutoConnectPort(config.getPort());
org.pubvm.j3270.config.Settings.setAutoConnectTls(config.isUseTls());
org.pubvm.j3270.config.Settings.setAutoConnectVerifyCert(config.isTlsVerifyCert());
// Disconnect existing connection // Disconnect existing connection
disconnect(); disconnect();
// Wire interactive certificate verifier if none configured
if (config.getCertificateVerifier() == null) {
config.setCertificateVerifier((chain, authType, exception) ->
org.pubvm.j3270.ui.UntrustedCertificateDialog.showPrompt(this, config.getHost(), config.getPort(), chain, exception)
);
}
client = new Telnet3270Client(config); client = new Telnet3270Client(config);
client.addConnectionListener(this); client.addConnectionListener(this);
client.addScreenUpdateListener(this); client.addScreenUpdateListener(this);
@@ -266,7 +280,8 @@ public class J3270App extends JFrame implements ConnectionListener, ScreenUpdate
terminalPanel.setClient(client); terminalPanel.setClient(client);
statusBar.setClient(client); statusBar.setClient(client);
setTitle("j3270 — " + config.getHost() + ":" + config.getPort()); String tlsIndicator = config.isUseTls() ? (config.isTlsVerifyCert() ? " [TLS]" : " [TLS/Unverified]") : "";
setTitle("j3270 — " + config.getHost() + ":" + config.getPort() + tlsIndicator);
// Resize window to match model // Resize window to match model
terminalPanel.guardedPack(); terminalPanel.guardedPack();
@@ -349,20 +364,23 @@ public class J3270App extends JFrame implements ConnectionListener, ScreenUpdate
@Override @Override
public void onScreenUpdated() { public void onScreenUpdated() {
SwingUtilities.invokeLater(() -> { if (screenUpdatePending.compareAndSet(false, true)) {
// During an active file transfer, let the CUT/DFT handler drive SwingUtilities.invokeLater(() -> {
// keyboard state. In x3270, ft_cut_data() runs before WCC screenUpdatePending.set(false);
// keyboard-restore is applied — the keyboard stays locked for the // During an active file transfer, let the CUT/DFT handler drive
// entire CUT transfer. // keyboard state. In x3270, ft_cut_data() runs before WCC
if (fileTransfer != null) { // keyboard-restore is applied — the keyboard stays locked for the
fileTransfer.onScreenUpdated(); // entire CUT transfer.
} if (fileTransfer != null) {
if (client != null && (fileTransfer == null || !fileTransfer.isTransferActive())) { fileTransfer.onScreenUpdated();
client.getInputProcessor().setKeyboardLocked(false); }
} if (client != null && (fileTransfer == null || !fileTransfer.isTransferActive())) {
terminalPanel.repaint(); client.getInputProcessor().setKeyboardLocked(false);
statusBar.updateStatus(); }
}); terminalPanel.repaint();
statusBar.updateStatus();
});
}
} }
@Override @Override
@@ -426,11 +444,29 @@ public class J3270App extends JFrame implements ConnectionListener, ScreenUpdate
public static void main(String[] args) { public static void main(String[] args) {
boolean debug = false; boolean debug = false;
boolean cliTls = false;
boolean cliNoVerifyCert = false;
Boolean cliTn3270e = null;
org.lib3270j.graphics.GraphicsMode cliGraphicsMode = null;
String configFile = null; String configFile = null;
java.util.List<String> remainingArgs = new java.util.ArrayList<>(); java.util.List<String> remainingArgs = new java.util.ArrayList<>();
for (int i = 0; i < args.length; i++) { for (int i = 0; i < args.length; i++) {
if ("--debug".equals(args[i]) || "-d".equals(args[i])) { if ("--debug".equals(args[i]) || "-d".equals(args[i])) {
debug = true; debug = true;
} else if ("--tls".equals(args[i]) || "--ssl".equals(args[i]) || "-s".equals(args[i])) {
cliTls = true;
} else if ("--no-verify-cert".equals(args[i]) || "--insecure".equals(args[i]) || "-k".equals(args[i])) {
cliNoVerifyCert = true;
} else if ("--no-tn3270e".equals(args[i]) || "--plain-tn3270".equals(args[i]) || "--plain".equals(args[i]) || "-P".equals(args[i]) || "-p".equals(args[i]) || "--non-e".equals(args[i])) {
cliTn3270e = false;
} else if ("--tn3270e".equals(args[i])) {
cliTn3270e = true;
} else if (args[i].startsWith("--graphics=")) {
cliGraphicsMode = org.lib3270j.graphics.GraphicsMode.fromString(args[i].substring(11));
} else if (("--graphics".equals(args[i]) || "-g".equals(args[i])) && i + 1 < args.length && !args[i + 1].startsWith("-")) {
cliGraphicsMode = org.lib3270j.graphics.GraphicsMode.fromString(args[++i]);
} else if ("--no-graphics".equals(args[i])) {
cliGraphicsMode = org.lib3270j.graphics.GraphicsMode.NONE;
} else if (("-c".equals(args[i]) || "--config".equals(args[i])) && i + 1 < args.length) { } else if (("-c".equals(args[i]) || "--config".equals(args[i])) && i + 1 < args.length) {
configFile = args[++i]; configFile = args[++i];
} else { } else {
@@ -439,33 +475,41 @@ public class J3270App extends JFrame implements ConnectionListener, ScreenUpdate
} }
// Configure logging for application packages // Configure logging for application packages
Level logLevel = debug ? Level.ALL : Level.INFO;
Logger globalRoot = Logger.getLogger(""); Logger globalRoot = Logger.getLogger("");
for (java.util.logging.Handler h : globalRoot.getHandlers()) { for (java.util.logging.Handler h : globalRoot.getHandlers()) {
globalRoot.removeHandler(h); globalRoot.removeHandler(h);
} }
globalRoot.setLevel(Level.WARNING); globalRoot.setLevel(Level.ALL);
ConsoleHandler handler = new ConsoleHandler(); java.util.logging.Filter appFilter = record -> {
handler.setLevel(debug ? Level.ALL : Level.INFO); String name = record.getLoggerName();
handler.setFormatter(new SimpleFormatter()); return name != null && (name.startsWith("org.lib3270j") || name.startsWith("org.pubvm.j3270"));
};
Logger appLogger = Logger.getLogger("org.pubvm.j3270"); ConsoleHandler consoleHandler = new ConsoleHandler();
appLogger.setLevel(Level.ALL); consoleHandler.setLevel(logLevel);
appLogger.addHandler(handler); consoleHandler.setFormatter(new SimpleFormatter());
appLogger.setUseParentHandlers(false); consoleHandler.setFilter(appFilter);
globalRoot.addHandler(consoleHandler);
Logger libLogger = Logger.getLogger("org.lib3270j"); Logger.getLogger("org.pubvm").setLevel(logLevel);
libLogger.setLevel(Level.ALL); Logger.getLogger("org.pubvm.j3270").setLevel(logLevel);
libLogger.addHandler(handler); Logger.getLogger("org.lib3270j").setLevel(logLevel);
libLogger.setUseParentHandlers(false);
try { try {
java.util.logging.FileHandler fileHandler = new java.util.logging.FileHandler("j3270.log", 10 * 1024 * 1024, 1, false); java.util.logging.FileHandler fileHandler = new java.util.logging.FileHandler("j3270.log", 10 * 1024 * 1024, 1, false) {
@Override
public synchronized void publish(java.util.logging.LogRecord record) {
super.publish(record);
flush();
}
};
fileHandler.setLevel(Level.ALL); fileHandler.setLevel(Level.ALL);
fileHandler.setFormatter(new SimpleFormatter()); fileHandler.setFormatter(new SimpleFormatter());
appLogger.addHandler(fileHandler); fileHandler.setFilter(appFilter);
libLogger.addHandler(fileHandler); globalRoot.addHandler(fileHandler);
log.info("Logging protocol trace to j3270.log"); log.info("Logging protocol trace to j3270.log (debug=" + debug + ", level=" + logLevel + ")");
} catch (Exception e) { } catch (Exception e) {
System.err.println("Could not create j3270.log: " + e.getMessage()); System.err.println("Could not create j3270.log: " + e.getMessage());
} }
@@ -492,18 +536,23 @@ public class J3270App extends JFrame implements ConnectionListener, ScreenUpdate
System.setProperty("apple.laf.useScreenMenuBar", "true"); System.setProperty("apple.laf.useScreenMenuBar", "true");
System.setProperty("apple.awt.application.name", "j3270"); System.setProperty("apple.awt.application.name", "j3270");
final boolean finalTls = cliTls;
final boolean finalNoVerify = cliNoVerifyCert;
final Boolean finalTn3270e = cliTn3270e;
final org.lib3270j.graphics.GraphicsMode finalGraphicsMode = cliGraphicsMode;
SwingUtilities.invokeLater(() -> { SwingUtilities.invokeLater(() -> {
J3270App app = new J3270App(); J3270App app = new J3270App();
app.setVisible(true); app.setVisible(true);
// If host:port given on command line, connect directly // If host:port given on command line, connect directly
if (!remainingArgs.isEmpty()) { if (!remainingArgs.isEmpty()) {
String host = remainingArgs.get(0); String hostArg = remainingArgs.get(0);
int port = 23; int port = finalTls ? 992 : 23;
if (remainingArgs.size() >= 2) { if (remainingArgs.size() >= 2) {
try { try {
port = Integer.parseInt(remainingArgs.get(1)); port = Integer.parseInt(remainingArgs.get(1));
} catch (NumberFormatException e) { } catch (NumberFormatException ignored) {
} }
} }
TerminalModel model = TerminalModel.IBM_3279_4; TerminalModel model = TerminalModel.IBM_3279_4;
@@ -511,10 +560,24 @@ public class J3270App extends JFrame implements ConnectionListener, ScreenUpdate
try { try {
int modelNum = Integer.parseInt(remainingArgs.get(2)); int modelNum = Integer.parseInt(remainingArgs.get(2));
model = TerminalModel.forModel(modelNum, true); model = TerminalModel.forModel(modelNum, true);
} catch (Exception e) { } catch (Exception ignored) {
} }
} }
ConnectionConfig config = new ConnectionConfig(host, port, model); ConnectionConfig config = ConnectionConfig.parseHostString(hostArg, port, model);
if (finalTls) {
config.setUseTls(true);
}
if (finalNoVerify) {
config.setTlsVerifyCert(false);
}
if (finalTn3270e != null) {
config.setTn3270eEnabled(finalTn3270e);
}
if (finalGraphicsMode != null) {
config.setGraphicsMode(finalGraphicsMode);
} else {
config.setGraphicsMode(org.pubvm.j3270.config.Settings.getGraphicsMode());
}
app.connect(config); app.connect(config);
} else { } else {
org.pubvm.j3270.config.Settings.StartupBehavior behavior = org.pubvm.j3270.config.Settings org.pubvm.j3270.config.Settings.StartupBehavior behavior = org.pubvm.j3270.config.Settings
@@ -526,8 +589,19 @@ public class J3270App extends JFrame implements ConnectionListener, ScreenUpdate
case AUTO_CONNECT: case AUTO_CONNECT:
String host = org.pubvm.j3270.config.Settings.getAutoConnectHost(); String host = org.pubvm.j3270.config.Settings.getAutoConnectHost();
int port = org.pubvm.j3270.config.Settings.getAutoConnectPort(); int port = org.pubvm.j3270.config.Settings.getAutoConnectPort();
boolean tls = org.pubvm.j3270.config.Settings.getAutoConnectTls();
boolean verify = org.pubvm.j3270.config.Settings.getAutoConnectVerifyCert();
boolean tn3270e = org.pubvm.j3270.config.Settings.getAutoConnectTn3270e();
if (host != null && !host.isEmpty()) { if (host != null && !host.isEmpty()) {
app.connect(new ConnectionConfig(host, port, TerminalModel.IBM_3279_4)); ConnectionConfig config = new ConnectionConfig(host, port, TerminalModel.IBM_3279_4, tls);
config.setTlsVerifyCert(verify);
config.setTn3270eEnabled(finalTn3270e != null ? finalTn3270e : tn3270e);
if (finalGraphicsMode != null) {
config.setGraphicsMode(finalGraphicsMode);
} else {
config.setGraphicsMode(org.pubvm.j3270.config.Settings.getGraphicsMode());
}
app.connect(config);
} else { } else {
SwingUtilities.invokeLater(app::showConnectDialog); SwingUtilities.invokeLater(app::showConnectDialog);
} }
@@ -62,6 +62,39 @@ public class Settings {
prefs.putInt("autoConnectPort", port); prefs.putInt("autoConnectPort", port);
} }
public static boolean getAutoConnectTls() {
return prefs.getBoolean("autoConnectTls", false);
}
public static void setAutoConnectTls(boolean tls) {
prefs.putBoolean("autoConnectTls", tls);
}
public static boolean getAutoConnectVerifyCert() {
return prefs.getBoolean("autoConnectVerifyCert", true);
}
public static void setAutoConnectVerifyCert(boolean verify) {
prefs.putBoolean("autoConnectVerifyCert", verify);
}
public static boolean getAutoConnectTn3270e() {
return prefs.getBoolean("autoConnectTn3270e", true);
}
public static void setAutoConnectTn3270e(boolean tn3270e) {
prefs.putBoolean("autoConnectTn3270e", tn3270e);
}
public static org.lib3270j.graphics.GraphicsMode getGraphicsMode() {
String modeStr = prefs.get("graphicsMode", org.lib3270j.graphics.GraphicsMode.BOTH.name());
return org.lib3270j.graphics.GraphicsMode.fromString(modeStr);
}
public static void setGraphicsMode(org.lib3270j.graphics.GraphicsMode mode) {
prefs.put("graphicsMode", (mode != null ? mode : org.lib3270j.graphics.GraphicsMode.BOTH).name());
}
public static Color getColorOverride(int index, Color defaultColor) { public static Color getColorOverride(int index, Color defaultColor) {
String hex = prefs.get("color_" + index, null); String hex = prefs.get("color_" + index, null);
try { try {
@@ -194,15 +227,38 @@ public class Settings {
break; break;
case "behavior": case "behavior":
case "connection":
switch (key) { switch (key) {
case "startupBehavior": case "startupBehavior":
setStartupBehavior(StartupBehavior.valueOf(value.toUpperCase())); setStartupBehavior(StartupBehavior.valueOf(value.toUpperCase()));
break; break;
case "autoConnectHost": setAutoConnectHost(value); break; case "autoConnectHost":
case "autoConnectPort": setAutoConnectPort(Integer.parseInt(value)); break; case "host":
setAutoConnectHost(value);
break;
case "autoConnectPort":
case "port":
setAutoConnectPort(Integer.parseInt(value));
break;
case "autoConnectTls":
case "tls":
case "ssl":
case "useTls":
setAutoConnectTls(Boolean.parseBoolean(value));
break;
case "autoConnectVerifyCert":
case "verifyCert":
case "tlsVerifyCert":
setAutoConnectVerifyCert(Boolean.parseBoolean(value));
break;
case "autoConnectTn3270e":
case "tn3270e":
case "enableTn3270e":
setAutoConnectTn3270e(Boolean.parseBoolean(value));
break;
case "blockSelectMode": setBlockSelectMode(Boolean.parseBoolean(value)); break; case "blockSelectMode": setBlockSelectMode(Boolean.parseBoolean(value)); break;
default: default:
log.warning("Unknown behavior key: " + key); log.warning("Unknown behavior/connection key: " + key);
} }
break; break;
@@ -222,6 +278,14 @@ public class Settings {
setKeyBinding(key, value); setKeyBinding(key, value);
break; break;
case "graphics":
if ("graphicsMode".equalsIgnoreCase(key) || "mode".equalsIgnoreCase(key)) {
setGraphicsMode(org.lib3270j.graphics.GraphicsMode.fromString(value));
} else {
log.warning("Unknown graphics key: " + key);
}
break;
default: default:
// Allow bare keys outside any section — treat as raw prefs // Allow bare keys outside any section — treat as raw prefs
log.fine("Setting raw preference: " + key + " = " + value); log.fine("Setting raw preference: " + key + " = " + value);
@@ -256,10 +320,18 @@ public class Settings {
if (acHost != null && !acHost.isEmpty()) { if (acHost != null && !acHost.isEmpty()) {
w.println("autoConnectHost = " + acHost); w.println("autoConnectHost = " + acHost);
w.println("autoConnectPort = " + getAutoConnectPort()); w.println("autoConnectPort = " + getAutoConnectPort());
w.println("autoConnectTls = " + getAutoConnectTls());
w.println("autoConnectVerifyCert = " + getAutoConnectVerifyCert());
w.println("autoConnectTn3270e = " + getAutoConnectTn3270e());
} }
w.println("blockSelectMode = " + getBlockSelectMode()); w.println("blockSelectMode = " + getBlockSelectMode());
w.println(); w.println();
// [graphics]
w.println("[graphics]");
w.println("graphicsMode = " + getGraphicsMode().name());
w.println();
// [colors] // [colors]
w.println("[colors]"); w.println("[colors]");
// Host colors 0-15 // Host colors 0-15
@@ -14,7 +14,11 @@ public class ConnectDialog extends JDialog {
private JTextField hostField; private JTextField hostField;
private JTextField portField; private JTextField portField;
private JComboBox<TerminalModel> modelCombo; private JComboBox<TerminalModel> modelCombo;
private JComboBox<org.lib3270j.graphics.GraphicsMode> graphicsCombo;
private JTextField luField; private JTextField luField;
private JCheckBox tlsCheckBox;
private JCheckBox verifyCertCheckBox;
private JCheckBox tn3270eCheckBox;
private boolean confirmed; private boolean confirmed;
private ConnectionConfig result; private ConnectionConfig result;
@@ -93,6 +97,67 @@ public class ConnectDialog extends JDialog {
luField = createDarkField(12); luField = createDarkField(12);
mainPanel.add(luField, gbc); mainPanel.add(luField, gbc);
// Graphics Mode
gbc.gridx = 0;
gbc.gridy = 4;
gbc.weightx = 0;
JLabel graphicsLabel = new JLabel("Graphics:");
graphicsLabel.setForeground(fg);
graphicsLabel.setFont(labelFont);
mainPanel.add(graphicsLabel, gbc);
gbc.gridx = 1;
gbc.weightx = 1.0;
graphicsCombo = new JComboBox<>(org.lib3270j.graphics.GraphicsMode.values());
graphicsCombo.setSelectedItem(org.pubvm.j3270.config.Settings.getGraphicsMode());
graphicsCombo.setBackground(new Color(45, 45, 45));
graphicsCombo.setForeground(fg);
graphicsCombo.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 13));
mainPanel.add(graphicsCombo, gbc);
// TLS / SSL Checkbox
gbc.gridx = 1;
gbc.gridy = 5;
gbc.weightx = 1.0;
tlsCheckBox = new JCheckBox("Enable TLS/SSL");
tlsCheckBox.setBackground(new Color(30, 30, 30));
tlsCheckBox.setForeground(fg);
tlsCheckBox.setFont(labelFont);
tlsCheckBox.setFocusPainted(false);
tlsCheckBox.addActionListener(e -> {
boolean isTls = tlsCheckBox.isSelected();
verifyCertCheckBox.setEnabled(isTls);
String currentPort = portField.getText().trim();
if (isTls && "23".equals(currentPort)) {
portField.setText("992");
} else if (!isTls && "992".equals(currentPort)) {
portField.setText("23");
}
});
mainPanel.add(tlsCheckBox, gbc);
// Verify Certificate Checkbox
gbc.gridx = 1;
gbc.gridy = 6;
verifyCertCheckBox = new JCheckBox("Verify Server Certificate");
verifyCertCheckBox.setBackground(new Color(30, 30, 30));
verifyCertCheckBox.setForeground(new Color(160, 160, 160));
verifyCertCheckBox.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 13));
verifyCertCheckBox.setSelected(true);
verifyCertCheckBox.setEnabled(false);
verifyCertCheckBox.setFocusPainted(false);
mainPanel.add(verifyCertCheckBox, gbc);
// TN3270E Checkbox
gbc.gridx = 1;
gbc.gridy = 7;
tn3270eCheckBox = new JCheckBox("Enable TN3270E (Extended 3270)");
tn3270eCheckBox.setBackground(new Color(30, 30, 30));
tn3270eCheckBox.setForeground(fg);
tn3270eCheckBox.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 13));
tn3270eCheckBox.setSelected(org.pubvm.j3270.config.Settings.getAutoConnectTn3270e());
tn3270eCheckBox.setFocusPainted(false);
mainPanel.add(tn3270eCheckBox, gbc);
// Buttons // Buttons
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
buttonPanel.setBackground(new Color(30, 30, 30)); buttonPanel.setBackground(new Color(30, 30, 30));
@@ -116,7 +181,7 @@ public class ConnectDialog extends JDialog {
buttonPanel.add(connectBtn); buttonPanel.add(connectBtn);
gbc.gridx = 0; gbc.gridx = 0;
gbc.gridy = 4; gbc.gridy = 8;
gbc.gridwidth = 2; gbc.gridwidth = 2;
mainPanel.add(buttonPanel, gbc); mainPanel.add(buttonPanel, gbc);
@@ -158,6 +223,10 @@ public class ConnectDialog extends JDialog {
if (!lu.isEmpty()) { if (!lu.isEmpty()) {
result.setLuName(lu); result.setLuName(lu);
} }
result.setGraphicsMode((org.lib3270j.graphics.GraphicsMode) graphicsCombo.getSelectedItem());
result.setUseTls(tlsCheckBox.isSelected());
result.setTlsVerifyCert(verifyCertCheckBox.isSelected());
result.setTn3270eEnabled(tn3270eCheckBox.isSelected());
confirmed = true; confirmed = true;
dispose(); dispose();
} }
@@ -178,4 +247,17 @@ public class ConnectDialog extends JDialog {
public void setInitialPort(int port) { public void setInitialPort(int port) {
portField.setText(String.valueOf(port)); portField.setText(String.valueOf(port));
} }
public void setInitialTls(boolean tls) {
tlsCheckBox.setSelected(tls);
verifyCertCheckBox.setEnabled(tls);
}
public void setInitialVerifyCert(boolean verify) {
verifyCertCheckBox.setSelected(verify);
}
public void setInitialTn3270e(boolean tn3270e) {
tn3270eCheckBox.setSelected(tn3270e);
}
} }
@@ -31,6 +31,7 @@ public class SettingsDialog extends JDialog {
// Appearance tab // Appearance tab
private JComboBox<String> fontBox; private JComboBox<String> fontBox;
private JSpinner fontSizeSpinner; private JSpinner fontSizeSpinner;
private JComboBox<org.lib3270j.graphics.GraphicsMode> graphicsModeBox;
// Behavior tab // Behavior tab
private JComboBox<Settings.StartupBehavior> startupBehaviorBox; private JComboBox<Settings.StartupBehavior> startupBehaviorBox;
@@ -264,8 +265,21 @@ public class SettingsDialog extends JDialog {
gbc.fill = GridBagConstraints.HORIZONTAL; gbc.fill = GridBagConstraints.HORIZONTAL;
panel.add(fontSizeSpinner, gbc); panel.add(fontSizeSpinner, gbc);
// Fill remaining space // Graphics Mode
gbc.gridx = 0;
gbc.gridy = 2; gbc.gridy = 2;
gbc.fill = GridBagConstraints.NONE;
JLabel graphicsLabel = new JLabel("Graphics Mode:");
panel.add(graphicsLabel, gbc);
graphicsModeBox = new JComboBox<>(org.lib3270j.graphics.GraphicsMode.values());
graphicsModeBox.setSelectedItem(Settings.getGraphicsMode());
gbc.gridx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
panel.add(graphicsModeBox, gbc);
// Fill remaining space
gbc.gridy = 3;
gbc.weighty = 1.0; gbc.weighty = 1.0;
panel.add(Box.createGlue(), gbc); panel.add(Box.createGlue(), gbc);
@@ -611,6 +625,9 @@ public class SettingsDialog extends JDialog {
Settings.setFontFamily(fontFam); Settings.setFontFamily(fontFam);
} }
Settings.setFontSize((Integer) fontSizeSpinner.getValue()); Settings.setFontSize((Integer) fontSizeSpinner.getValue());
if (graphicsModeBox.getSelectedItem() != null) {
Settings.setGraphicsMode((org.lib3270j.graphics.GraphicsMode) graphicsModeBox.getSelectedItem());
}
// Apply Behavior // Apply Behavior
Settings.StartupBehavior behavior = (Settings.StartupBehavior) startupBehaviorBox.getSelectedItem(); Settings.StartupBehavior behavior = (Settings.StartupBehavior) startupBehaviorBox.getSelectedItem();
@@ -15,6 +15,7 @@ import java.awt.*;
public class StatusBar extends JPanel { public class StatusBar extends JPanel {
private final JLabel connectionStatus; private final JLabel connectionStatus;
private final JLabel tlsStatus;
private final JLabel cursorPosition; private final JLabel cursorPosition;
private final JLabel luName; private final JLabel luName;
private final JLabel lockStatus; private final JLabel lockStatus;
@@ -37,6 +38,7 @@ public class StatusBar extends JPanel {
Font oiaFont = new Font(Font.MONOSPACED, Font.PLAIN, 12); Font oiaFont = new Font(Font.MONOSPACED, Font.PLAIN, 12);
connectionStatus = createLabel("Not Connected", oiaFont, OIA_DIM); connectionStatus = createLabel("Not Connected", oiaFont, OIA_DIM);
tlsStatus = createLabel("", oiaFont, OIA_FG);
luName = createLabel("", oiaFont, OIA_FG); luName = createLabel("", oiaFont, OIA_FG);
lockStatus = createLabel("", oiaFont, OIA_ALERT); lockStatus = createLabel("", oiaFont, OIA_ALERT);
modelInfo = createLabel("", oiaFont, OIA_DIM); modelInfo = createLabel("", oiaFont, OIA_DIM);
@@ -44,6 +46,8 @@ public class StatusBar extends JPanel {
add(Box.createHorizontalStrut(6)); add(Box.createHorizontalStrut(6));
add(connectionStatus); add(connectionStatus);
add(Box.createHorizontalStrut(10));
add(tlsStatus);
add(Box.createHorizontalStrut(12)); add(Box.createHorizontalStrut(12));
add(luName); add(luName);
add(Box.createHorizontalStrut(12)); add(Box.createHorizontalStrut(12));
@@ -110,6 +114,26 @@ public class StatusBar extends JPanel {
break; break;
} }
// TLS Status
if (state.isConnected() && client.getConfig().isUseTls()) {
boolean verified = client.getConfig().isTlsVerifyCert();
javax.net.ssl.SSLSession session = client.getSslSession();
String cipher = session != null ? session.getCipherSuite() : "TLS";
String protocol = session != null ? session.getProtocol() : "TLS";
if (verified) {
tlsStatus.setText("🔒 TLS");
tlsStatus.setForeground(OIA_FG);
tlsStatus.setToolTipText(protocol + " / " + cipher + " (Verified)");
} else {
tlsStatus.setText("🔓 TLS (Unverified)");
tlsStatus.setForeground(new Color(255, 180, 80));
tlsStatus.setToolTipText(protocol + " / " + cipher + " (Verification Bypassed)");
}
} else {
tlsStatus.setText("");
tlsStatus.setToolTipText(null);
}
// LU name // LU name
String lu = ""; String lu = "";
if (client.getConnectionState().isTn3270e()) { if (client.getConnectionState().isTn3270e()) {
@@ -22,6 +22,7 @@ public class TerminalPanel extends JPanel {
// Font and cell dimensions // Font and cell dimensions
private Font terminalFont; private Font terminalFont;
private Font boldTerminalFont;
private int cellWidth; private int cellWidth;
private int cellHeight; private int cellHeight;
private int fontAscent; private int fontAscent;
@@ -47,6 +48,14 @@ public class TerminalPanel extends JPanel {
// ========== Selection / Copy-Paste state ========== // ========== Selection / Copy-Paste state ==========
private boolean blockSelectMode = false; private boolean blockSelectMode = false;
private static final String[] CHAR_STRINGS = new String[128];
static {
for (int i = 0; i < 128; i++) {
CHAR_STRINGS[i] = String.valueOf((char) i);
}
}
private static final Color CURSOR_COLOR = new Color(255, 255, 255, 180);
private int selectionStartRow = -1, selectionStartCol = -1; private int selectionStartRow = -1, selectionStartCol = -1;
private int selectionEndRow = -1, selectionEndCol = -1; private int selectionEndRow = -1, selectionEndCol = -1;
private boolean isDragging = false; private boolean isDragging = false;
@@ -779,6 +788,7 @@ public class TerminalPanel extends JPanel {
cellHeight = fm.getHeight(); cellHeight = fm.getHeight();
fontAscent = fm.getAscent(); fontAscent = fm.getAscent();
fontDescent = fm.getDescent(); fontDescent = fm.getDescent();
boldTerminalFont = terminalFont.deriveFont(Font.BOLD);
} }
private void setupCursorBlink() { private void setupCursorBlink() {
@@ -818,6 +828,8 @@ public class TerminalPanel extends JPanel {
Graphics2D g2 = (Graphics2D) g; Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB); g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED);
// Clear entire panel with background color // Clear entire panel with background color
g2.setColor(bgColor); g2.setColor(bgColor);
@@ -828,8 +840,8 @@ public class TerminalPanel extends JPanel {
int oy = getRenderOffsetY(); int oy = getRenderOffsetY();
ScreenBuffer sb = client.getScreenBuffer(); ScreenBuffer sb = client.getScreenBuffer();
int rows = sb.getRows(); int rows = sb.getDisplayRows();
int cols = sb.getCols(); int cols = sb.getDisplayCols();
boolean isColorModel = client.getConfig().getModel().isColor(); boolean isColorModel = client.getConfig().getModel().isColor();
// Track current field attribute for monochrome color decisions // Track current field attribute for monochrome color decisions
@@ -839,7 +851,7 @@ public class TerminalPanel extends JPanel {
for (int row = 0; row < rows; row++) { for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) { for (int col = 0; col < cols; col++) {
int baddr = row * cols + col; int baddr = row * cols + col;
ExtendedAttribute ea = sb.getCell(baddr); ExtendedAttribute ea = sb.getDisplayCell(baddr);
int x = ox + col * cellWidth; int x = ox + col * cellWidth;
int y = oy + row * cellHeight; int y = oy + row * cellHeight;
@@ -854,10 +866,7 @@ public class TerminalPanel extends JPanel {
if (ea.isFieldAttribute()) { if (ea.isFieldAttribute()) {
currentFA = ea.fa; currentFA = ea.fa;
currentFieldEa = ea; currentFieldEa = ea;
// Field attributes display as blanks // Selection highlight on field attribute cells
g2.setColor(this.bgColor);
g2.fillRect(x, y, cellWidth, cellHeight);
// Selection highlight on field attribute cells too
if (isCellSelected(row, col)) { if (isCellSelected(row, col)) {
g2.setColor(SELECTION_COLOR); g2.setColor(SELECTION_COLOR);
g2.fillRect(x, y, cellWidth, cellHeight); g2.fillRect(x, y, cellWidth, cellHeight);
@@ -892,12 +901,9 @@ public class TerminalPanel extends JPanel {
// Handle invisible fields (zero intensity / password fields) // Handle invisible fields (zero intensity / password fields)
// Modern UX: render '*' for typed characters so user sees length/digit count // Modern UX: render '*' for typed characters so user sees length/digit count
if (faIsZero(currentFA & 0xFF)) { if (faIsZero(currentFA & 0xFF)) {
g2.setColor(this.bgColor);
g2.fillRect(x, y, cellWidth, cellHeight);
char ch = ea.ucs4; char ch = ea.ucs4;
if (ch > 0x20 && ch != 0xFF) { if (ch > 0x20 && ch != 0xFF) {
Font f = bold ? terminalFont.deriveFont(Font.BOLD) : terminalFont; Font f = bold ? boldTerminalFont : terminalFont;
g2.setFont(f); g2.setFont(f);
g2.setColor(fgColor); g2.setColor(fgColor);
g2.drawString("*", x, y + fontAscent); g2.drawString("*", x, y + fontAscent);
@@ -917,17 +923,38 @@ public class TerminalPanel extends JPanel {
bgColor = tmp; bgColor = tmp;
} }
// Draw background // Draw background only if different from default panel bgColor or if inverted
g2.setColor(bgColor); if (!bgColor.equals(this.bgColor) || reverse) {
g2.fillRect(x, y, cellWidth, cellHeight); g2.setColor(bgColor);
g2.fillRect(x, y, cellWidth, cellHeight);
}
// Draw character // Draw character or Programmed Symbol
char ch = ea.ucs4; int cs = (ea.cs != 0) ? (ea.cs & 0xFF) : (currentFieldEa != null ? (currentFieldEa.cs & 0xFF) : 0);
if (ch > 0x20 && ch != 0xFF) { boolean drawnAsPs = false;
Font f = bold ? terminalFont.deriveFont(Font.BOLD) : terminalFont; if (cs >= 0x40 && client.getProgramSymbolManager() != null) {
g2.setFont(f); org.lib3270j.graphics.ProgramSymbolSet.SymbolSlot slot = client.getProgramSymbolManager().getSymbol(cs, ea.ec & 0xFF);
g2.setColor(fgColor); if (slot != null) {
g2.drawString(String.valueOf(ch), x, y + fontAscent); java.awt.image.BufferedImage img = slot.getScaledImage(cellWidth, cellHeight, fgColor.getRGB(), bgColor.getRGB());
if (img != null) {
g2.drawImage(img, x, y, null);
drawnAsPs = true;
}
}
}
if (!drawnAsPs) {
char ch = ea.ucs4;
if (ch > 0x20 && ch != 0xFF) {
Font f = bold ? boldTerminalFont : terminalFont;
g2.setFont(f);
g2.setColor(fgColor);
if (ch < 128) {
g2.drawString(CHAR_STRINGS[ch], x, y + fontAscent);
} else {
g2.drawString(String.valueOf(ch), x, y + fontAscent);
}
}
} }
// Draw underline // Draw underline
@@ -945,15 +972,28 @@ public class TerminalPanel extends JPanel {
} }
} }
// Draw Vector Graphics Plane overlay if present
if (client.getGraphicsPlane() != null && client.getGraphicsPlane().hasContent()) {
int gridW = cols * cellWidth;
int gridH = rows * cellHeight;
client.getGraphicsPlane().resize(gridW, gridH);
int[] rgb = client.getGraphicsPlane().getRgbBuffer();
if (rgb != null) {
java.awt.image.BufferedImage img = new java.awt.image.BufferedImage(gridW, gridH, java.awt.image.BufferedImage.TYPE_INT_ARGB);
img.setRGB(0, 0, gridW, gridH, rgb, 0, gridW);
g2.drawImage(img, ox, oy, gridW, gridH, null);
}
}
// Draw cursor // Draw cursor
if (cursorVisible && client.getConnectionState().isFullSession()) { if (cursorVisible && client.getConnectionState().isFullSession()) {
int curAddr = sb.getCursorAddress(); int curAddr = sb.getDisplayCursorAddress();
int curRow = curAddr / cols; int curRow = curAddr / cols;
int curCol = curAddr % cols; int curCol = curAddr % cols;
int cx = ox + curCol * cellWidth; int cx = ox + curCol * cellWidth;
int cy = oy + curRow * cellHeight; int cy = oy + curRow * cellHeight;
g2.setColor(new Color(255, 255, 255, 180)); g2.setColor(CURSOR_COLOR);
g2.setXORMode(bgColor); g2.setXORMode(bgColor);
g2.fillRect(cx, cy, cellWidth, cellHeight); g2.fillRect(cx, cy, cellWidth, cellHeight);
g2.setPaintMode(); g2.setPaintMode();
@@ -0,0 +1,153 @@
package org.pubvm.j3270.ui;
import javax.swing.*;
import java.awt.*;
import java.security.MessageDigest;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.text.SimpleDateFormat;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* Modal dialog for inspecting and confirming untrusted TLS/SSL server certificates.
*/
public class UntrustedCertificateDialog extends JDialog {
private boolean accepted = false;
public UntrustedCertificateDialog(Frame parent, String host, int port, X509Certificate[] chain, CertificateException exception) {
super(parent, "Untrusted SSL/TLS Certificate", true);
buildUI(host, port, chain, exception);
pack();
setLocationRelativeTo(parent);
setResizable(false);
}
private void buildUI(String host, int port, X509Certificate[] chain, CertificateException exception) {
JPanel mainPanel = new JPanel(new BorderLayout(12, 12));
mainPanel.setBorder(BorderFactory.createEmptyBorder(16, 16, 16, 16));
mainPanel.setBackground(new Color(30, 30, 30));
// Header
JPanel headerPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 0));
headerPanel.setBackground(new Color(30, 30, 30));
JLabel iconLabel = new JLabel("⚠️");
iconLabel.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 28));
headerPanel.add(iconLabel);
JPanel titleBox = new JPanel(new GridLayout(2, 1, 0, 2));
titleBox.setBackground(new Color(30, 30, 30));
JLabel titleLabel = new JLabel("Untrusted SSL Certificate");
titleLabel.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 16));
titleLabel.setForeground(new Color(255, 180, 80));
titleBox.add(titleLabel);
JLabel subtitleLabel = new JLabel("The server certificate for " + host + ":" + port + " could not be verified.");
subtitleLabel.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
subtitleLabel.setForeground(new Color(180, 180, 180));
titleBox.add(subtitleLabel);
headerPanel.add(titleBox);
mainPanel.add(headerPanel, BorderLayout.NORTH);
// Certificate Details Area
X509Certificate cert = (chain != null && chain.length > 0) ? chain[0] : null;
StringBuilder sb = new StringBuilder();
if (exception != null) {
sb.append("Validation Error:\n ").append(exception.getMessage() != null ? exception.getMessage() : exception.toString()).append("\n\n");
}
if (cert != null) {
sb.append("Subject:\n ").append(cert.getSubjectX500Principal().getName()).append("\n\n");
sb.append("Issuer:\n ").append(cert.getIssuerX500Principal().getName()).append("\n\n");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
sb.append("Validity:\n From: ").append(sdf.format(cert.getNotBefore()))
.append("\n To: ").append(sdf.format(cert.getNotAfter())).append("\n\n");
sb.append("Serial Number:\n ").append(cert.getSerialNumber().toString(16).toUpperCase()).append("\n\n");
sb.append("SHA-256 Fingerprint:\n ").append(computeFingerprint(cert, "SHA-256")).append("\n\n");
sb.append("SHA-1 Fingerprint:\n ").append(computeFingerprint(cert, "SHA-1"));
} else {
sb.append("No peer certificate information available.");
}
JTextArea detailsArea = new JTextArea(sb.toString());
detailsArea.setEditable(false);
detailsArea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));
detailsArea.setBackground(new Color(20, 20, 20));
detailsArea.setForeground(new Color(210, 210, 210));
detailsArea.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
JScrollPane scrollPane = new JScrollPane(detailsArea);
scrollPane.setPreferredSize(new Dimension(520, 260));
scrollPane.setBorder(BorderFactory.createLineBorder(new Color(60, 60, 60)));
mainPanel.add(scrollPane, BorderLayout.CENTER);
// Buttons
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 10, 0));
buttonPanel.setBackground(new Color(30, 30, 30));
JButton cancelBtn = new JButton("Cancel Connection");
cancelBtn.setBackground(new Color(60, 60, 60));
cancelBtn.setForeground(new Color(220, 220, 220));
cancelBtn.addActionListener(e -> {
accepted = false;
dispose();
});
JButton trustBtn = new JButton("Connect Anyway");
trustBtn.setBackground(new Color(180, 100, 40));
trustBtn.setForeground(Color.WHITE);
trustBtn.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 13));
trustBtn.addActionListener(e -> {
accepted = true;
dispose();
});
buttonPanel.add(cancelBtn);
buttonPanel.add(trustBtn);
mainPanel.add(buttonPanel, BorderLayout.SOUTH);
setContentPane(mainPanel);
getRootPane().setDefaultButton(trustBtn);
}
public boolean isAccepted() {
return accepted;
}
private static String computeFingerprint(X509Certificate cert, String algorithm) {
try {
MessageDigest md = MessageDigest.getInstance(algorithm);
byte[] digest = md.digest(cert.getEncoded());
StringBuilder sb = new StringBuilder();
for (int i = 0; i < digest.length; i++) {
if (i > 0) sb.append(':');
sb.append(String.format("%02X", digest[i]));
}
return sb.toString();
} catch (Exception e) {
return "Unable to compute fingerprint: " + e.getMessage();
}
}
/**
* Show the prompt modal dialog on the Swing EDT and return user choice.
*/
public static boolean showPrompt(Frame parent, String host, int port, X509Certificate[] chain, CertificateException exception) {
if (SwingUtilities.isEventDispatchThread()) {
UntrustedCertificateDialog dialog = new UntrustedCertificateDialog(parent, host, port, chain, exception);
dialog.setVisible(true);
return dialog.isAccepted();
} else {
AtomicBoolean result = new AtomicBoolean(false);
try {
SwingUtilities.invokeAndWait(() -> {
UntrustedCertificateDialog dialog = new UntrustedCertificateDialog(parent, host, port, chain, exception);
dialog.setVisible(true);
result.set(dialog.isAccepted());
});
} catch (Exception e) {
return false;
}
return result.get();
}
}
}
+6
View File
@@ -3,6 +3,12 @@ plugins {
} }
dependencies { dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter:5.10.2'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}
test {
useJUnitPlatform()
} }
jar { jar {
@@ -11,9 +11,14 @@ public class ConnectionConfig {
private String luName = null; private String luName = null;
private boolean extendedDataStream = true; private boolean extendedDataStream = true;
private boolean useTls = false; private boolean useTls = false;
private boolean tlsVerifyCert = true;
private org.lib3270j.tls.TlsCertificateVerifier certificateVerifier = null;
private String sslProtocol = "TLS";
private int connectTimeoutMs = 15000; private int connectTimeoutMs = 15000;
private int nopIntervalSeconds = 0; private int nopIntervalSeconds = 0;
private String terminalName = null; // override terminal type string private String terminalName = null; // override terminal type string
private boolean tn3270eEnabled = true;
private org.lib3270j.graphics.GraphicsMode graphicsMode = org.lib3270j.graphics.GraphicsMode.BOTH;
public ConnectionConfig() {} public ConnectionConfig() {}
@@ -22,12 +27,25 @@ public class ConnectionConfig {
this.port = port; this.port = port;
} }
public ConnectionConfig(String host, int port, boolean useTls) {
this.host = host;
this.port = port;
this.useTls = useTls;
}
public ConnectionConfig(String host, int port, TerminalModel model) { public ConnectionConfig(String host, int port, TerminalModel model) {
this.host = host; this.host = host;
this.port = port; this.port = port;
this.model = model; this.model = model;
} }
public ConnectionConfig(String host, int port, TerminalModel model, boolean useTls) {
this.host = host;
this.port = port;
this.model = model;
this.useTls = useTls;
}
public String getHost() { return host; } public String getHost() { return host; }
public void setHost(String host) { this.host = host; } public void setHost(String host) { this.host = host; }
@@ -46,15 +64,101 @@ public class ConnectionConfig {
public boolean isUseTls() { return useTls; } public boolean isUseTls() { return useTls; }
public void setUseTls(boolean useTls) { this.useTls = useTls; } public void setUseTls(boolean useTls) { this.useTls = useTls; }
public boolean isTlsVerifyCert() { return tlsVerifyCert; }
public void setTlsVerifyCert(boolean verify) { this.tlsVerifyCert = verify; }
public boolean isTn3270eEnabled() { return tn3270eEnabled; }
public void setTn3270eEnabled(boolean enabled) { this.tn3270eEnabled = enabled; }
public org.lib3270j.tls.TlsCertificateVerifier getCertificateVerifier() { return certificateVerifier; }
public void setCertificateVerifier(org.lib3270j.tls.TlsCertificateVerifier verifier) { this.certificateVerifier = verifier; }
public String getSslProtocol() { return sslProtocol; }
public void setSslProtocol(String protocol) { this.sslProtocol = protocol; }
public int getConnectTimeoutMs() { return connectTimeoutMs; } public int getConnectTimeoutMs() { return connectTimeoutMs; }
public void setConnectTimeoutMs(int ms) { this.connectTimeoutMs = ms; } public void setConnectTimeoutMs(int ms) { this.connectTimeoutMs = ms; }
public int getNopIntervalSeconds() { return nopIntervalSeconds; } public int getNopIntervalSeconds() { return nopIntervalSeconds; }
public void setNopIntervalSeconds(int s) { this.nopIntervalSeconds = s; } public void setNopIntervalSeconds(int s) { this.nopIntervalSeconds = s; }
public org.lib3270j.graphics.GraphicsMode getGraphicsMode() { return graphicsMode; }
public void setGraphicsMode(org.lib3270j.graphics.GraphicsMode mode) {
this.graphicsMode = (mode != null) ? mode : org.lib3270j.graphics.GraphicsMode.NONE;
}
public String getTerminalName() { return terminalName; } public String getTerminalName() { return terminalName; }
public void setTerminalName(String name) { this.terminalName = name; } public void setTerminalName(String name) { this.terminalName = name; }
/**
* Parse a host connection string which may include prefixes for TLS (e.g. "L:host:port", "ssl:host:port", "y:host:port"),
* plain TN3270 (e.g. "P:host:port", "plain:host:port", "non-e:host:port"), or standard "host:port" formats.
*/
public static ConnectionConfig parseHostString(String hostStr, int defaultPort, TerminalModel defaultModel) {
if (hostStr == null || hostStr.trim().isEmpty()) {
return new ConnectionConfig("localhost", defaultPort, defaultModel);
}
String s = hostStr.trim();
boolean tls = false;
boolean tn3270e = true;
// Parse chained x3270-style prefixes (e.g. "L:P:host:port" or "P:host:port")
boolean prefixFound = true;
while (prefixFound) {
prefixFound = false;
if (s.startsWith("L:") || s.startsWith("l:") || s.startsWith("Y:") || s.startsWith("y:")) {
tls = true;
s = s.substring(2);
prefixFound = true;
} else if (s.toLowerCase().startsWith("ssl:") || s.toLowerCase().startsWith("tls:")) {
tls = true;
s = s.substring(4);
prefixFound = true;
} else if (s.startsWith("N:") || s.startsWith("n:") || s.toLowerCase().startsWith("notls:") || s.toLowerCase().startsWith("nossl:")) {
tls = false;
int colon = s.indexOf(':');
s = s.substring(colon + 1);
prefixFound = true;
} else if (s.startsWith("P:") || s.startsWith("p:")) {
tn3270e = false;
s = s.substring(2);
prefixFound = true;
} else if (s.toLowerCase().startsWith("plain:") || s.toLowerCase().startsWith("non-e:")) {
tn3270e = false;
int colon = s.indexOf(':');
s = s.substring(colon + 1);
prefixFound = true;
}
}
String host = s;
int port = (defaultPort > 0) ? defaultPort : (tls ? 992 : 23);
// Check for host:port (handle IPv6 [::1]:port)
if (s.startsWith("[") && s.contains("]")) {
int closeBracket = s.indexOf(']');
host = s.substring(1, closeBracket);
if (s.length() > closeBracket + 1 && s.charAt(closeBracket + 1) == ':') {
try {
port = Integer.parseInt(s.substring(closeBracket + 2));
} catch (NumberFormatException ignored) {}
}
} else {
int colon = s.lastIndexOf(':');
if (colon > 0 && colon < s.length() - 1) {
try {
port = Integer.parseInt(s.substring(colon + 1));
host = s.substring(0, colon);
} catch (NumberFormatException ignored) {}
}
}
ConnectionConfig config = new ConnectionConfig(host, port, defaultModel != null ? defaultModel : TerminalModel.IBM_3279_4);
config.setUseTls(tls);
config.setTn3270eEnabled(tn3270e);
return config;
}
/** /**
* Get the effective terminal type string to send during negotiation. * Get the effective terminal type string to send during negotiation.
*/ */
@@ -46,12 +46,14 @@ public class Telnet3270Client {
this.translator = new EbcdicTranslator(); this.translator = new EbcdicTranslator();
this.screenBuffer = new ScreenBuffer(config.getModel(), translator); this.screenBuffer = new ScreenBuffer(config.getModel(), translator);
this.dsProcessor = new DataStreamProcessor(screenBuffer, translator); this.dsProcessor = new DataStreamProcessor(screenBuffer, translator);
this.dsProcessor.getQueryReplyBuilder().setGraphicsMode(config.getGraphicsMode());
this.fsm = new TelnetFSM(config, screenBuffer, dsProcessor); this.fsm = new TelnetFSM(config, screenBuffer, dsProcessor);
this.inputProcessor = new InputProcessor(screenBuffer, translator, fsm); this.inputProcessor = new InputProcessor(screenBuffer, translator, fsm);
// Wire up the output sender: DSProcessor -> FSM -> TelnetConnection // Wire up the output sender: DSProcessor -> FSM -> TelnetConnection
dsProcessor.setOutputSender(fsm::send3270Data); dsProcessor.setOutputSender(fsm::send3270Data);
dsProcessor.setInputProcessor(inputProcessor); dsProcessor.setInputProcessor(inputProcessor);
inputProcessor.setGraphicsPlane(dsProcessor.getGraphicsPlane());
} }
/** /**
@@ -122,6 +124,16 @@ public class Telnet3270Client {
/** Get the connection config. */ /** Get the connection config. */
public ConnectionConfig getConfig() { return config; } public ConnectionConfig getConfig() { return config; }
/** Set a custom or interactive TLS certificate verifier callback. */
public void setTlsCertificateVerifier(org.lib3270j.tls.TlsCertificateVerifier verifier) {
config.setCertificateVerifier(verifier);
}
/** Get the active SSLSession if connected over TLS, or null. */
public javax.net.ssl.SSLSession getSslSession() {
return connection.getSslSession();
}
// ========== Convenience input methods ========== // ========== Convenience input methods ==========
/** Type a character at the cursor position. */ /** Type a character at the cursor position. */
@@ -210,4 +222,16 @@ public class Telnet3270Client {
public void sysReq() { inputProcessor.sysReq(); } public void sysReq() { inputProcessor.sysReq(); }
/** Reset (unlock keyboard). */ /** Reset (unlock keyboard). */
public void reset() { inputProcessor.reset(); } public void reset() { inputProcessor.reset(); }
public org.lib3270j.graphics.ProgramSymbolManager getProgramSymbolManager() {
return dsProcessor.getProgramSymbolManager();
}
public org.lib3270j.graphics.GraphicsPlane getGraphicsPlane() {
return dsProcessor.getGraphicsPlane();
}
public org.lib3270j.graphics.GocaDecoder getGocaDecoder() {
return dsProcessor.getGocaDecoder();
}
} }
@@ -85,6 +85,20 @@ public class EbcdicTranslator {
return (char) CP037_TO_UNICODE[ebc & 0xFF]; return (char) CP037_TO_UNICODE[ebc & 0xFF];
} }
/**
* Static helper to translate EBCDIC byte to Unicode character.
*/
public static char toUnicode(int ebc) {
return (char) CP037_TO_UNICODE[ebc & 0xFF];
}
/**
* Static helper to translate EBCDIC byte to ASCII character.
*/
public static char ebcdicToAscii(int ebc) {
return toUnicode(ebc);
}
/** /**
* Translate Unicode character to EBCDIC byte. * Translate Unicode character to EBCDIC byte.
* Returns -1 if the character cannot be mapped. * Returns -1 if the character cannot be mapped.
@@ -9,6 +9,7 @@ import static org.lib3270j.protocol.DS3270Constants.*;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.util.List; import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArrayList;
import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
/** /**
@@ -38,6 +39,13 @@ public class DataStreamProcessor {
// Input processor reference to manage keyboard locking state // Input processor reference to manage keyboard locking state
private org.lib3270j.input.InputProcessor inputProcessor; private org.lib3270j.input.InputProcessor inputProcessor;
// Graphics & Programmed Symbols
private final org.lib3270j.graphics.ProgramSymbolManager programSymbolManager = new org.lib3270j.graphics.ProgramSymbolManager();
private final org.lib3270j.graphics.GraphicsPlane graphicsPlane = new org.lib3270j.graphics.GraphicsPlane(800, 600);
private final org.lib3270j.graphics.GocaDecoder gocaDecoder = new org.lib3270j.graphics.GocaDecoder(graphicsPlane);
private final java.io.ByteArrayOutputStream gocaAccumulator = new java.io.ByteArrayOutputStream();
private int currentGocaSubtype = 0;
/** Functional interface for sending output back through the telnet stack. */ /** Functional interface for sending output back through the telnet stack. */
@FunctionalInterface @FunctionalInterface
public interface OutputSender { public interface OutputSender {
@@ -50,6 +58,23 @@ public class DataStreamProcessor {
this.qrBuilder = new QueryReplyBuilder(screen); this.qrBuilder = new QueryReplyBuilder(screen);
this.outputBuffer = new byte[32768]; this.outputBuffer = new byte[32768];
this.outputPos = 0; this.outputPos = 0;
this.gocaDecoder.setProgramSymbolManager(programSymbolManager);
}
public QueryReplyBuilder getQueryReplyBuilder() {
return qrBuilder;
}
public org.lib3270j.graphics.ProgramSymbolManager getProgramSymbolManager() {
return programSymbolManager;
}
public org.lib3270j.graphics.GraphicsPlane getGraphicsPlane() {
return graphicsPlane;
}
public org.lib3270j.graphics.GocaDecoder getGocaDecoder() {
return gocaDecoder;
} }
public void setOutputSender(OutputSender sender) { public void setOutputSender(OutputSender sender) {
@@ -81,72 +106,92 @@ public class DataStreamProcessor {
return; return;
int cmd = data[offset] & 0xFF; int cmd = data[offset] & 0xFF;
log.info(">>> CMD: " + commandName(cmd) + " (0x" + String.format("%02x", cmd) + ") " + length + " bytes"); synchronized (screen.getRenderLock()) {
if (log.isLoggable(Level.FINE)) {
log.fine(">>> CMD: " + commandName(cmd) + " (0x" + String.format("%02x", cmd) + ") " + length + " bytes");
}
switch (cmd) { switch (cmd) {
case CMD_W: case CMD_W:
case SNA_CMD_W: case SNA_CMD_W:
processWrite(data, offset, length, false); programSymbolManager.commitStagedSymbols();
break; processWrite(data, offset, length, false);
case CMD_EW: break;
case SNA_CMD_EW: case CMD_EW:
log.info(">>> ERASE/WRITE: clearing screen (default size)"); { case SNA_CMD_EW:
int oldRows = screen.getRows(); programSymbolManager.commitStagedSymbols();
int oldCols = screen.getCols(); if (log.isLoggable(Level.FINE)) {
screen.erase(false); log.fine(">>> ERASE/WRITE: clearing screen (default size)");
processWrite(data, offset, length, true); }
if (oldRows != screen.getRows() || oldCols != screen.getCols()) { {
notifyScreenSizeChanged(); int oldRows = screen.getRows();
} int oldCols = screen.getCols();
screen.erase(false);
graphicsPlane.clear();
processWrite(data, offset, length, true);
if (oldRows != screen.getRows() || oldCols != screen.getCols()) {
notifyScreenSizeChanged();
}
}
break;
case CMD_EWA:
case SNA_CMD_EWA:
programSymbolManager.commitStagedSymbols();
if (log.isLoggable(Level.FINE)) {
log.fine(">>> ERASE/WRITE ALTERNATE: clearing screen (alt size)");
}
{
int oldRows = screen.getRows();
int oldCols = screen.getCols();
screen.erase(true);
graphicsPlane.clear();
processWrite(data, offset, length, true);
if (oldRows != screen.getRows() || oldCols != screen.getCols()) {
notifyScreenSizeChanged();
}
}
break;
case CMD_RB:
case SNA_CMD_RB:
programSymbolManager.commitStagedSymbols();
processReadBuffer();
break;
case CMD_RM:
case SNA_CMD_RM:
programSymbolManager.commitStagedSymbols();
processReadModified(false);
break;
case CMD_RMA:
case SNA_CMD_RMA:
programSymbolManager.commitStagedSymbols();
processReadModified(true);
break;
case CMD_EAU:
case SNA_CMD_EAU:
programSymbolManager.commitStagedSymbols();
log.info(">>> EAU: erasing all unprotected fields");
screen.eraseAllUnprotected();
break;
case CMD_WSF:
case SNA_CMD_WSF:
processWriteStructuredField(data, offset, length);
break;
case CMD_NOP:
log.info(">>> NOP command");
break;
default:
log.warning(">>> UNKNOWN 3270 command: " + String.format("0x%02x", cmd));
break;
} }
break;
case CMD_EWA: // Translate EBCDIC to Unicode for display
case SNA_CMD_EWA: screen.translateToUnicode();
log.info(">>> ERASE/WRITE ALTERNATE: clearing screen (alt size)"); { screen.markAllChanged();
int oldRows = screen.getRows(); screen.updateDisplaySnapshot();
int oldCols = screen.getCols();
screen.erase(true);
processWrite(data, offset, length, true);
if (oldRows != screen.getRows() || oldCols != screen.getCols()) {
notifyScreenSizeChanged();
}
}
break;
case CMD_RB:
case SNA_CMD_RB:
processReadBuffer();
break;
case CMD_RM:
case SNA_CMD_RM:
processReadModified(false);
break;
case CMD_RMA:
case SNA_CMD_RMA:
processReadModified(true);
break;
case CMD_EAU:
case SNA_CMD_EAU:
log.info(">>> EAU: erasing all unprotected fields");
screen.eraseAllUnprotected();
break;
case CMD_WSF:
case SNA_CMD_WSF:
processWriteStructuredField(data, offset, length);
break;
case CMD_NOP:
log.info(">>> NOP command");
break;
default:
log.warning(">>> UNKNOWN 3270 command: " + String.format("0x%02x", cmd));
break;
} }
// Translate EBCDIC to Unicode for display
screen.translateToUnicode();
screen.markAllChanged();
// Debug: dump non-empty screen lines // Debug: dump non-empty screen lines
if (cmd == SNA_CMD_EWA || cmd == CMD_EWA || cmd == SNA_CMD_EW || cmd == CMD_EW) { if (log.isLoggable(Level.FINE) && (cmd == SNA_CMD_EWA || cmd == CMD_EWA || cmd == SNA_CMD_EW || cmd == CMD_EW)) {
int r = screen.getRows(); int r = screen.getRows();
int c = screen.getCols(); int c = screen.getCols();
StringBuilder dump = new StringBuilder(); StringBuilder dump = new StringBuilder();
@@ -179,7 +224,7 @@ public class DataStreamProcessor {
} }
} }
if (dump.length() > 0) { if (dump.length() > 0) {
log.info("Screen content after " + commandName(cmd) + ":\n" + dump.toString()); log.fine("Screen content after " + commandName(cmd) + ":\n" + dump.toString());
} }
} }
} }
@@ -741,6 +786,7 @@ public class DataStreamProcessor {
if (fieldLen >= 4) { if (fieldLen >= 4) {
boolean alt = (data[pos + 3] & 0xFF) == SF_ER_ALT; boolean alt = (data[pos + 3] & 0xFF) == SF_ER_ALT;
screen.erase(alt); screen.erase(alt);
graphicsPlane.clear();
notifyScreenSizeChanged(); notifyScreenSizeChanged();
} }
break; break;
@@ -751,6 +797,7 @@ public class DataStreamProcessor {
break; break;
case SF_CREATE_PART: case SF_CREATE_PART:
// Acknowledged — we use implicit partition // Acknowledged — we use implicit partition
graphicsPlane.clear();
break; break;
case SF_OUTBOUND_DS: case SF_OUTBOUND_DS:
if (fieldLen > 5) { if (fieldLen > 5) {
@@ -765,6 +812,81 @@ public class DataStreamProcessor {
log.fine("SF_TRANSFER_DATA received but ftDft is null"); log.fine("SF_TRANSFER_DATA received but ftDft is null");
} }
break; break;
case org.lib3270j.graphics.GocaConstants.SF_LOADPS_DIRECT: // 0x06: Load Programmed Symbols direct
if (fieldLen > 3) {
byte[] psData = new byte[fieldLen - 3];
System.arraycopy(data, pos + 3, psData, 0, psData.length);
programSymbolManager.loadps(psData);
}
break;
case org.lib3270j.graphics.GocaConstants.SF_LOADPS: { // 0x0F: 2-byte Structured Field prefix
if (fieldLen >= 4) {
int sfSubId = data[pos + 3] & 0xFF;
switch (sfSubId) {
case org.lib3270j.graphics.GocaConstants.SF_LOADPS_SUB: // 0x06: Load Programmed Symbols
if (fieldLen > 4) {
byte[] psData = new byte[fieldLen - 4];
System.arraycopy(data, pos + 4, psData, 0, psData.length);
programSymbolManager.loadps(psData);
}
break;
case org.lib3270j.graphics.GocaConstants.SF_OBJCNTL_SUB: // 0x11: Object Control (Procedure orders)
case org.lib3270j.graphics.GocaConstants.SF_OBJPICT_SUB: // 0x10: Object Picture (Picture segments)
case org.lib3270j.graphics.GocaConstants.SF_OBJDATA_SUB: { // 0x0F: Object Data (GOCA draw orders)
int flags = (fieldLen >= 6) ? (data[pos + 5] & 0xC0) : 0xC0;
int orderOffset = (fieldLen >= 7) ? (pos + 7) : (pos + 4);
int orderLen = Math.max(0, fieldLen - (orderOffset - pos));
graphicsPlane.setScreenDimensions(screen.getCols(), screen.getRows());
if (flags == 0x80) { // SPAN_FIRST
gocaAccumulator.reset();
currentGocaSubtype = sfSubId;
if (orderLen > 0) {
gocaAccumulator.write(data, orderOffset, orderLen);
}
} else if (flags == 0x00) { // SPAN_MIDDLE
if (orderLen > 0) {
gocaAccumulator.write(data, orderOffset, orderLen);
}
} else if (flags == 0x40) { // SPAN_LAST
if (orderLen > 0) {
gocaAccumulator.write(data, orderOffset, orderLen);
}
byte[] fullStream = gocaAccumulator.toByteArray();
gocaAccumulator.reset();
if (currentGocaSubtype == org.lib3270j.graphics.GocaConstants.SF_OBJCNTL_SUB) {
gocaDecoder.processProcedureOrders(fullStream, 0, fullStream.length);
} else {
gocaDecoder.decodeStream(fullStream, 0, fullStream.length);
}
notifyScreenUpdated();
} else { // SPAN_ONLY (0xC0)
gocaAccumulator.reset();
if (sfSubId == org.lib3270j.graphics.GocaConstants.SF_OBJCNTL_SUB) {
gocaDecoder.processProcedureOrders(data, orderOffset, orderLen);
} else {
gocaDecoder.decodeStream(data, orderOffset, orderLen);
}
notifyScreenUpdated();
}
break;
}
default:
log.fine("Unknown SF 0x0F subtype: " + String.format("0x%02x", sfSubId));
break;
}
}
break;
}
case org.lib3270j.graphics.GocaConstants.SF_OBJDATA: // 0x85: Graphics Object Data / GOCA
case org.lib3270j.graphics.GocaConstants.SF_3270_G: // 0x20: 3270 Graphics
if (fieldLen > 3) {
graphicsPlane.setScreenDimensions(screen.getCols(), screen.getRows());
gocaDecoder.decodeStream(data, pos + 3, fieldLen - 3);
notifyScreenUpdated();
}
break;
default: default:
log.fine("Unknown SF id: " + String.format("0x%02x", sfId)); log.fine("Unknown SF id: " + String.format("0x%02x", sfId));
break; break;
@@ -841,7 +963,7 @@ public class DataStreamProcessor {
if ((i + 1) % 32 == 0) if ((i + 1) % 32 == 0)
sb.append("\n "); sb.append("\n ");
} }
log.info("Query reply (" + qr.length + " bytes):\n " + sb.toString().trim()); log.warning(">>> SENDING Query Reply (" + qr.length + " bytes):\n " + sb.toString().trim());
if (outputSender != null) { if (outputSender != null) {
outputSender.send3270Data(qr); outputSender.send3270Data(qr);
} }
@@ -856,7 +978,7 @@ public class DataStreamProcessor {
if ((i + 1) % 32 == 0) if ((i + 1) % 32 == 0)
sb.append("\n "); sb.append("\n ");
} }
log.info("Requested query reply (" + qr.length + " bytes):\n " + sb.toString().trim()); log.warning(">>> SENDING Requested Query Reply (" + qr.length + " bytes):\n " + sb.toString().trim());
if (outputSender != null) { if (outputSender != null) {
outputSender.send3270Data(qr); outputSender.send3270Data(qr);
} }
@@ -887,4 +1009,10 @@ public class DataStreamProcessor {
l.onScreenSizeChanged(screen.getRows(), screen.getCols()); l.onScreenSizeChanged(screen.getRows(), screen.getCols());
} }
} }
private void notifyScreenUpdated() {
for (ScreenUpdateListener l : screenListeners) {
l.onScreenUpdated();
}
}
} }
@@ -3,12 +3,13 @@ package org.lib3270j.datastream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.lib3270j.graphics.GraphicsMode;
import org.lib3270j.screen.ScreenBuffer; import org.lib3270j.screen.ScreenBuffer;
import static org.lib3270j.protocol.DS3270Constants.*; import static org.lib3270j.protocol.DS3270Constants.*;
/** /**
* Builds Query Reply structured fields in response to host Read Partition queries. * Builds Query Reply structured fields in response to host Read Partition queries.
* Equivalent to the do_qr_* functions in sf.c. * Matches IBM 3270 Architecture (GA23-0059) and x3270 sf.c exact binary layout.
*/ */
public class QueryReplyBuilder { public class QueryReplyBuilder {
@@ -21,9 +22,10 @@ public class QueryReplyBuilder {
private static final int Yr_3279_2 = 0x0002006f; private static final int Yr_3279_2 = 0x0002006f;
private final ScreenBuffer screen; private final ScreenBuffer screen;
private GraphicsMode graphicsMode = GraphicsMode.BOTH;
// Supported query reply codes (must match what we send in summary) // Base query reply codes (text mode)
private static final int[] SUPPORTED_QR = { private static final int[] SUPPORTED_QR_BASE = {
QR_SUMMARY, // 0x80 summary must list itself QR_SUMMARY, // 0x80 summary must list itself
QR_USABLE_AREA, // 0x81 QR_USABLE_AREA, // 0x81
QR_ALPHA_PART, // 0x84 QR_ALPHA_PART, // 0x84
@@ -35,10 +37,45 @@ public class QueryReplyBuilder {
QR_IMP_PART, // 0xa6 QR_IMP_PART, // 0xa6
}; };
// Vector graphics query reply codes matching HOD DS3270.java line 1723
private static final int[] SUPPORTED_QR_VECTOR = {
QR_SUMMARY, // 0x80
QR_USABLE_AREA, // 0x81
QR_ALPHA_PART, // 0x84
QR_CHARSETS, // 0x85
QR_COLOR, // 0x86
QR_HIGHLIGHTING, // 0x87
QR_REPLY_MODES, // 0x88
QR_SAVE_RESTORE, // 0x8c
QR_DDM, // 0x95
QR_TRANSPARENCY, // 0x99
QR_IMP_PART, // 0xa6
QR_RPQ_NAMES, // 0xa8
QR_GRAPHICS, // 0xb0
QR_GIMAGE, // 0xb1
QR_AUX_DEV, // 0xb2
QR_OEM_FMT, // 0xb3
QR_GCOLOR, // 0xb4
QR_GSYMBOLS, // 0xb6
};
public QueryReplyBuilder(ScreenBuffer screen) { public QueryReplyBuilder(ScreenBuffer screen) {
this.screen = screen; this.screen = screen;
} }
public QueryReplyBuilder(ScreenBuffer screen, GraphicsMode graphicsMode) {
this.screen = screen;
this.graphicsMode = (graphicsMode != null) ? graphicsMode : GraphicsMode.NONE;
}
public GraphicsMode getGraphicsMode() {
return graphicsMode;
}
public void setGraphicsMode(GraphicsMode graphicsMode) {
this.graphicsMode = (graphicsMode != null) ? graphicsMode : GraphicsMode.NONE;
}
/** /**
* Build all query replies as a single AID_SF + structured field response. * Build all query replies as a single AID_SF + structured field response.
*/ */
@@ -66,16 +103,37 @@ public class QueryReplyBuilder {
// Highlighting // Highlighting
appendQueryReply(out, QR_HIGHLIGHTING, buildHighlighting()); appendQueryReply(out, QR_HIGHLIGHTING, buildHighlighting());
// Reply Modes // Reply Modes (0x88)
appendQueryReply(out, QR_REPLY_MODES, buildReplyModes()); appendQueryReply(out, QR_REPLY_MODES, buildReplyModes());
// Distributed Data Management (DFT File Transfer) if (graphicsMode.isVectorGraphicsEnabled()) {
// Save/Restore (0x8C)
appendQueryReply(out, QR_SAVE_RESTORE, buildSaveRestore());
}
// Distributed Data Management (0x95)
appendQueryReply(out, QR_DDM, buildDdm(4096)); appendQueryReply(out, QR_DDM, buildDdm(4096));
// Implicit Partition if (graphicsMode.isVectorGraphicsEnabled()) {
// Transparency (0x99)
appendQueryReply(out, QR_TRANSPARENCY, buildTransparency());
}
// Implicit Partition (0xA6)
appendQueryReply(out, QR_IMP_PART, buildImplicitPartition(maxCols, maxRows)); appendQueryReply(out, QR_IMP_PART, buildImplicitPartition(maxCols, maxRows));
log.info("Built " + out.size() + " bytes of all query replies"); // Vector Graphics QRs if enabled
if (graphicsMode.isVectorGraphicsEnabled()) {
appendQueryReply(out, QR_RPQ_NAMES, buildRpqNames()); // 0xA8
appendQueryReply(out, QR_GRAPHICS, buildGraphics()); // 0xB0
appendQueryReply(out, QR_GIMAGE, buildGImage()); // 0xB1
appendQueryReply(out, QR_AUX_DEV, buildAuxDev()); // 0xB2
appendOemFmt(out); // 0xB3
appendQueryReply(out, QR_GCOLOR, buildGColor()); // 0xB4
appendQueryReply(out, QR_GSYMBOLS, buildGSymbols()); // 0xB6
}
log.info("Built " + out.size() + " bytes of all query replies (graphicsMode=" + graphicsMode + ")");
return out.toByteArray(); return out.toByteArray();
} }
@@ -116,14 +174,75 @@ public class QueryReplyBuilder {
case QR_REPLY_MODES: case QR_REPLY_MODES:
appendQueryReply(out, QR_REPLY_MODES, buildReplyModes()); appendQueryReply(out, QR_REPLY_MODES, buildReplyModes());
break; break;
case QR_SAVE_RESTORE:
if (graphicsMode.isVectorGraphicsEnabled()) {
appendQueryReply(out, QR_SAVE_RESTORE, buildSaveRestore());
} else {
appendQueryReply(out, QR_NULL, new byte[0]);
}
break;
case QR_DDM: case QR_DDM:
appendQueryReply(out, QR_DDM, buildDdm(4096)); appendQueryReply(out, QR_DDM, buildDdm(4096));
break; break;
case QR_TRANSPARENCY:
if (graphicsMode.isVectorGraphicsEnabled()) {
appendQueryReply(out, QR_TRANSPARENCY, buildTransparency());
} else {
appendQueryReply(out, QR_NULL, new byte[0]);
}
break;
case QR_IMP_PART: case QR_IMP_PART:
appendQueryReply(out, QR_IMP_PART, buildImplicitPartition(maxCols, maxRows)); appendQueryReply(out, QR_IMP_PART, buildImplicitPartition(maxCols, maxRows));
break; break;
case QR_RPQ_NAMES:
case QR_RPQNAMES: case QR_RPQNAMES:
appendQueryReply(out, QR_RPQNAMES, new byte[0]); if (graphicsMode.isVectorGraphicsEnabled()) {
appendQueryReply(out, QR_RPQ_NAMES, buildRpqNames());
} else {
appendQueryReply(out, QR_NULL, new byte[0]);
}
break;
case QR_GRAPHICS:
if (graphicsMode.isVectorGraphicsEnabled()) {
appendQueryReply(out, QR_GRAPHICS, buildGraphics());
} else {
appendQueryReply(out, QR_NULL, new byte[0]);
}
break;
case QR_GIMAGE:
if (graphicsMode.isVectorGraphicsEnabled()) {
appendQueryReply(out, QR_GIMAGE, buildGImage());
} else {
appendQueryReply(out, QR_NULL, new byte[0]);
}
break;
case QR_AUX_DEV:
if (graphicsMode.isVectorGraphicsEnabled()) {
appendQueryReply(out, QR_AUX_DEV, buildAuxDev());
} else {
appendQueryReply(out, QR_NULL, new byte[0]);
}
break;
case QR_OEM_FMT:
if (graphicsMode.isVectorGraphicsEnabled()) {
appendOemFmt(out);
} else {
appendQueryReply(out, QR_NULL, new byte[0]);
}
break;
case QR_GCOLOR:
if (graphicsMode.isVectorGraphicsEnabled()) {
appendQueryReply(out, QR_GCOLOR, buildGColor());
} else {
appendQueryReply(out, QR_NULL, new byte[0]);
}
break;
case QR_GSYMBOLS:
if (graphicsMode.isVectorGraphicsEnabled()) {
appendQueryReply(out, QR_GSYMBOLS, buildGSymbols());
} else {
appendQueryReply(out, QR_NULL, new byte[0]);
}
break; break;
default: default:
// Unsupported query reply code emit QR_NULL // Unsupported query reply code emit QR_NULL
@@ -148,7 +267,8 @@ public class QueryReplyBuilder {
private byte[] buildSummary() { private byte[] buildSummary() {
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
for (int code : SUPPORTED_QR) { int[] codes = graphicsMode.isVectorGraphicsEnabled() ? SUPPORTED_QR_VECTOR : SUPPORTED_QR_BASE;
for (int code : codes) {
out.write(code); out.write(code);
} }
return out.toByteArray(); return out.toByteArray();
@@ -184,66 +304,81 @@ public class QueryReplyBuilder {
private byte[] buildAlphaPartitions(int maxRows) { private byte[] buildAlphaPartitions(int maxRows) {
int bufSize = screen.getMaxCols() * screen.getMaxRows(); int bufSize = screen.getMaxCols() * screen.getMaxRows();
ByteArrayOutputStream out = new ByteArrayOutputStream(4); ByteArrayOutputStream out = new ByteArrayOutputStream(4);
out.write(0x00); // max partitions (1 partition) out.write(0x00); // max partitions (1 byte: 0x00 = 1 partition)
out.write((bufSize >> 8) & 0xFF); // total partition storage high out.write((bufSize >> 8) & 0xFF); // total partition storage high
out.write(bufSize & 0xFF); // total partition storage low out.write(bufSize & 0xFF); // total partition storage low
out.write(0x00); // no special features out.write(0x00); // flags
return out.toByteArray(); return out.toByteArray();
} }
private byte[] buildCharsets() { private byte[] buildCharsets() {
ByteArrayOutputStream out = new ByteArrayOutputStream(32); if (graphicsMode.isProgrammedSymbolsEnabled()) {
// Programmed Symbols mode (3279 PS with LoadPS 0x0A, flags1 = 0xA2 for GE + PS + CGCSGID)
ByteArrayOutputStream out = new ByteArrayOutputStream(65);
out.write(0xa2); // flags: GE (0x80), PS/Loadable Charsets (0x20), CGCSGID present (0x02)
out.write(0x00); // more flags
out.write(SW_3279_2); // SDW (9)
out.write(SH_3279_2); // SDH (12)
out.write(0x0a); // Load PS format types supported: Format 1 and Format 3
out.write(0x00); // Load PS device type (high)
out.write(0x00); // Load PS device type (low)
out.write(0x00); // reserved
out.write(0x07); // DL = 7 bytes per descriptor
// Descriptor 1 (SET 0): default character set (non-loadable, single plane, CP037)
out.write(0x00); out.write(0x10); out.write(0x00); out.write(0x02); out.write(0xb9); out.write(0x00); out.write(0x25);
// Descriptor 2 (SET 1): APL/GE character set
out.write(0x01); out.write(0x00); out.write(0xf1); out.write(0x03); out.write(0xc3); out.write(0x01); out.write(0x36);
// Loadable Single-Plane PS Sets (PSA, PSB: Slots 2, 3) - Flags = 0x80 (Loadable, single plane)
out.write(0x02); out.write(0x80); out.write(0x40); out.write(0x00); out.write(0x00); out.write(0x00); out.write(0x00);
out.write(0x03); out.write(0x80); out.write(0x41); out.write(0x00); out.write(0x00); out.write(0x00); out.write(0x00);
// Loadable Triple-Plane PS Sets (PSC, PSD, PSE, PSF: Slots 4, 5, 6, 7) - Flags = 0xC0 (0x80 Loadable | 0x40 Triple-plane)
out.write(0x04); out.write(0xc0); out.write(0x42); out.write(0x00); out.write(0x00); out.write(0x00); out.write(0x00);
out.write(0x05); out.write(0xc0); out.write(0x43); out.write(0x00); out.write(0x00); out.write(0x00); out.write(0x00);
out.write(0x06); out.write(0xc0); out.write(0x44); out.write(0x00); out.write(0x00); out.write(0x00); out.write(0x00);
out.write(0x07); out.write(0xc0); out.write(0x45); out.write(0x00); out.write(0x00); out.write(0x00); out.write(0x00);
return out.toByteArray();
}
// Standard 3179G / Base character sets (matches sf.c / HOD)
ByteArrayOutputStream out = new ByteArrayOutputStream(23);
out.write(0x82); // flags: GE, CGCSGID present out.write(0x82); // flags: GE, CGCSGID present
out.write(0x00); // more flags out.write(0x00); // more flags
out.write(SW_3279_2); // SDW - default char width out.write(SW_3279_2); // SDW - default char width (9)
out.write(SH_3279_2); // SDH - default char height out.write(SH_3279_2); // SDH - default char height (12)
out.write(0x00); // Load PS format types supported: none out.write(0x00); // LoadPS format (0x00)
out.write(0x00); // Load PS device type (high)
out.write(0x00); // Load PS device type (low)
out.write(0x00); // reserved
out.write(0x07); // DL = 7 bytes per descriptor (non-DBCS)
// Descriptor 1 (SET 0): default character set
out.write(0x00); // SET 0
out.write(0x10); // FLAGS: non-loadable, single-plane, single-byte, no compare
out.write(0x00); // LCID 0
out.write(0x02); // CGCSGID (4 bytes) = 0x02b90025 (CGEN|CSET)
out.write(0xb9);
out.write(0x00); out.write(0x00);
out.write(0x25); out.write(0x00);
// Descriptor 2 (SET 1): APL/GE character set out.write(0x00);
out.write(0x01); // SET 1 out.write(0x07); // DL = 7
out.write(0x00); // FLAGS: non-loadable, single-plane, single-byte, no compare // Set 0 (Base EBCDIC - Non-loadable, single plane, CP037)
out.write(0xf1); // LCID 0xf1 out.write(0x00); out.write(0x10); out.write(0x00); out.write(0x02); out.write(0xb9); out.write(0x00); out.write(0x25);
out.write(0x03); // CGCSGID: 3179-style APL2 = 0x03c30136 // Set 1 (APL/Text)
out.write(0xc3); out.write(0x01); out.write(0x00); out.write(0xf1); out.write(0x03); out.write(0xc3); out.write(0x01); out.write(0x36);
out.write(0x01);
out.write(0x36);
return out.toByteArray(); return out.toByteArray();
} }
private byte[] buildColor() { private byte[] buildColor() {
ByteArrayOutputStream out = new ByteArrayOutputStream(36); // Alphanumeric Color (matches HOD: 8 pairs, F1..F7 + default F4 green, 18 bytes payload / 22 bytes total)
int colorMax = 16; return new byte[] {
out.write(0x00); // no options 0x00, 0x08, 0x00, (byte) 0xF4,
out.write(colorMax); // number of colors (byte) 0xF1, (byte) 0xF1, // Blue
out.write(0x00); // default color pair: attribute (byte) 0xF2, (byte) 0xF2, // Red
out.write(0xf0 + HOST_COLOR_GREEN); // default color: green (byte) 0xF3, (byte) 0xF3, // Pink
for (int i = 0xf1; i < 0xf1 + colorMax - 1; i++) { (byte) 0xF4, (byte) 0xF4, // Green
out.write(i); // color attribute value (byte) 0xF5, (byte) 0xF5, // Turquoise
out.write(i); // maps to itself (color mode) (byte) 0xF6, (byte) 0xF6, // Yellow
} (byte) 0xF7, (byte) 0xF7 // Neutral/White
return out.toByteArray(); };
} }
private byte[] buildHighlighting() { private byte[] buildHighlighting() {
ByteArrayOutputStream out = new ByteArrayOutputStream(11); // Highlighting (matches HOD: 4 pairs: default F0, Blink F1, Reverse F2, Underscore F4, 9 bytes payload / 13 bytes total)
out.write(5); // 5 pairs return new byte[] {
out.write(XAH_DEFAULT); out.write(XAH_NORMAL); 0x04, 0x00, (byte) 0xF0,
out.write(XAH_BLINK); out.write(XAH_BLINK); (byte) 0xF1, (byte) 0xF1,
out.write(XAH_REVERSE); out.write(XAH_REVERSE); (byte) 0xF2, (byte) 0xF2,
out.write(XAH_UNDERSCORE); out.write(XAH_UNDERSCORE); (byte) 0xF4, (byte) 0xF4
out.write(XAH_INTENSIFY); out.write(XAH_INTENSIFY); };
return out.toByteArray();
} }
private byte[] buildReplyModes() { private byte[] buildReplyModes() {
@@ -265,21 +400,20 @@ public class QueryReplyBuilder {
private byte[] buildImplicitPartition(int maxCols, int maxRows) { private byte[] buildImplicitPartition(int maxCols, int maxRows) {
ByteArrayOutputStream out = new ByteArrayOutputStream(22); ByteArrayOutputStream out = new ByteArrayOutputStream(22);
// Implicit partition sizes SDP (Self-Defining Parameter) // Implicit partition sizes, 2 self-defining parameters
// SDP 1: Default screen size (Model 2: 80x24)
out.write(0x00); // flags out.write(0x00); // flags
out.write(0x00); // reserved out.write(0x00); // reserved
// SDP 1: Implicit partition sizes out.write(0x0b); // SDP length (11 bytes)
out.write(0x00); // SDP length high byte
out.write(0x0b); // SDP length low byte (11 bytes: 2 len + 1 type + 1 res + 8 dims)
out.write(0x01); // SDP type: implicit partition sizes out.write(0x01); // SDP type: implicit partition sizes
out.write(0x00); // reserved out.write(0x00); // reserved
// Default dimensions (Model 2: 80x24) // Default size
out.write((MODEL_2_COLS >> 8) & 0xFF); out.write((MODEL_2_COLS >> 8) & 0xFF);
out.write(MODEL_2_COLS & 0xFF); out.write(MODEL_2_COLS & 0xFF);
out.write((MODEL_2_ROWS >> 8) & 0xFF); out.write((MODEL_2_ROWS >> 8) & 0xFF);
out.write(MODEL_2_ROWS & 0xFF); out.write(MODEL_2_ROWS & 0xFF);
// Alternate dimensions // Alternate size (Model 4: 80x43, Model 5: 132x27, etc.)
out.write((maxCols >> 8) & 0xFF); out.write((maxCols >> 8) & 0xFF);
out.write(maxCols & 0xFF); out.write(maxCols & 0xFF);
out.write((maxRows >> 8) & 0xFF); out.write((maxRows >> 8) & 0xFF);
@@ -287,4 +421,83 @@ public class QueryReplyBuilder {
return out.toByteArray(); return out.toByteArray();
} }
private byte[] buildGraphics() {
return new byte[]{ (byte) 0x80, 0x02, 0x00, 0x00, 0x00, (byte) 0xFC, 0x00 };
}
private byte[] buildGImage() {
return new byte[]{
0x00, 0x01, 0x00, 0x00, 0x00, (byte) 0xFC, 0x00,
0x06, 0x40, 0x06, 0x40, 0x06, 0x01,
(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xF0
};
}
private byte[] buildAuxDev() {
return new byte[]{
0x00, 0x09, 0x00, 0x07,
0x01, 0x01, 0x02, 0x02, 0x03, 0x03, 0x04, 0x04,
0x05, 0x05, 0x06, 0x06, 0x07, 0x07, 0x08, 0x08
};
}
private byte[] buildSaveRestore() {
// HOD DS3270.java line 1768: 6 bytes payload
return new byte[]{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
}
private byte[] buildTransparency() {
// HOD DS3270.java line 1782: 2 bytes payload
return new byte[]{ 0x00, 0x00 };
}
private byte[] buildRpqNames() {
// HOD DS3270.java line 1798: 5 bytes payload
return new byte[]{ 0x02, 0x00, (byte) 0xF0, (byte) 0xFF, (byte) 0xFF };
}
private void appendOemFmt(ByteArrayOutputStream out) {
// HOD DS3270.java line 1814: 4 distinct OEM format sub-fields
appendQueryReply(out, QR_OEM_FMT, new byte[]{
0x00, 0x03, 0x02, (byte) 0x90, 0x09, 0x01, 0x00, 0x03, 0x02, (byte) 0x80, 0x00, 0x7F, (byte) 0xFF
});
appendQueryReply(out, QR_OEM_FMT, new byte[]{
0x00, 0x04, 0x08, 0x40, 0x07, 0x03, 0x00, 0x03, (byte) 0x80, 0x00, 0x02
});
appendQueryReply(out, QR_OEM_FMT, new byte[]{
0x00, 0x05, 0x02, (byte) 0x80, 0x09, 0x01, 0x00, 0x01, 0x02, (byte) 0x80, 0x00, 0x7F, (byte) 0xFF
});
appendQueryReply(out, QR_OEM_FMT, new byte[]{
0x00, 0x07, 0x08, 0x40, 0x07, 0x03, 0x00, 0x01, 0x00, 0x00, 0x1C
});
}
private byte[] buildGColor() {
ByteArrayOutputStream out = new ByteArrayOutputStream(110);
out.write(0x00); out.write(0x04); out.write(0x00); out.write(0xFF); out.write(0xFF);
out.write(0x00); out.write(0x10); out.write(0x00); out.write(0x10);
for (int i = 0; i < 16; i++) {
out.write(0x00);
out.write(i);
int argb = org.lib3270j.graphics.GocaConstants.GOCA_COLORS[i];
int r = (argb >> 16) & 0xFF;
int g = (argb >> 8) & 0xFF;
int b = argb & 0xFF;
out.write(r);
out.write(g);
out.write(b);
out.write(0x00); // 6th byte in HOD color table
}
return out.toByteArray();
}
private byte[] buildGSymbols() {
return new byte[]{
0x00, 0x00, 0x0C, 0x18, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x12,
0x01, 0x00, 0x00, (byte) 0xF0, (byte) 0xC1, (byte) 0xC1, 0x00, 0x00,
0x0C, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
}
} }
@@ -0,0 +1,184 @@
package org.lib3270j.graphics;
/**
* Constants for IBM 3179G / 3270-PC GOCA (Graphics Object Content Architecture)
* vector graphics and Programmed Symbols (PS).
*/
public final class GocaConstants {
private GocaConstants() {}
// Structured Field IDs
public static final int SF_LOADPS = 0x0F; // 2-byte Structured Field prefix (or 0x06 for direct Load PS)
public static final int SF_LOADPS_DIRECT = 0x06; // Load Programmed Symbols direct SFID
public static final int SF_OBJCNTL = 0x24; // Object Control
public static final int SF_OBJDATA = 0x85; // Graphics Object Data
public static final int SF_3270_G = 0x20; // 3270 Graphics / Picture
// Structured Field Sub-IDs (for SF 0x0F)
public static final int SF_LOADPS_SUB = 0x06; // Load Programmed Symbols
public static final int SF_LOADLT_SUB = 0x07; // Load Line Type / Symbol Set
public static final int SF_OBJDATA_SUB = 0x0F; // Graphics Object Data (GOCA draw orders)
public static final int SF_OBJPICT_SUB = 0x10; // Graphics Object Picture (Segment draw orders)
public static final int SF_OBJCNTL_SUB = 0x11; // Graphics Object Control (Procedure orders)
// Procedure Orders (for SF_OBJCNTL_SUB 0x11)
public static final int P_NOP1 = 0x00; // Procedure NOOP
public static final int P_COMT = 0x01; // Procedure Comment
public static final int P_ATTCUR = 0x08; // Attach Graphic Cursor
public static final int P_DETCUR = 0x09; // Detach Graphic Cursor
public static final int P_ERASE = 0x0A; // Erase Graphics Presentation Space
public static final int P_STOPDR = 0x0F; // Stop Draw
public static final int P_SCUDEF = 0x21; // Set Current Defaults
public static final int P_BEGPROC = 0x30; // Begin Procedure
public static final int P_SETCUR = 0x31; // Set Graphic Cursor Position
// Coordinate space
public static final int VIRTUAL_COORD_MAX = 4096;
// GOCA Drawing / Segment Orders
public static final int G_NOP1 = 0x00; // NOOP 1-byte
public static final int G_COMT = 0x01; // Comment
public static final int G_GSMC = 0x07; // Set Marker Color
public static final int G_GSPS = 0x08; // Set Pattern Set
public static final int G_GSCOL = 0x0A; // Set Color
public static final int G_GSMX = 0x0C; // Set Foreground Mix
public static final int G_GSBMX = 0x0D; // Set Background Mix
public static final int G_GSFLW = 0x11; // Set Fractional Line Width
public static final int G_GSLT = 0x18; // Set Line Type
public static final int G_GSLW = 0x19; // Set Line Width
public static final int G_GSMS = 0x1B; // Set Marker Size
public static final int G_GSCP = 0x21; // Set Current Position
public static final int G_GSAP = 0x22; // Arc Parameters
public static final int G_GSECOL = 0x26; // Set Extended Color
public static final int G_GSVW = 0x27; // Set Viewing Window
public static final int G_GSPT = 0x28; // Set Pattern Symbol
public static final int G_GSMT = 0x29; // Set Marker Symbol / Type
public static final int G_GSCH = 0x33; // Set Character Cell
public static final int G_GSCA = 0x34; // Set Character Angle
public static final int G_GSCR = 0x35; // Set Character Shear
public static final int G_GSMCEL = 0x37; // Set Marker Cell
public static final int G_GSCS = 0x38; // Set Character Set
public static final int G_GSMP = 0x39; // Set Marker Precision
public static final int G_GSCD = 0x3A; // Set Character Direction
public static final int G_GSCC = 0x3B; // Set Character Precision
public static final int G_GSMS_SET = 0x3C; // Set Marker Set
public static final int G_ENDPROLOGUE = 0x3E; // End Prologue
public static final int G_GPOP = 0x3F; // Pop Attribute
public static final int G_GEAR = 0x60; // End Area
public static final int G_GBAR = 0x68; // Begin Area
public static final int G_BEGSEGM = 0x70; // Begin Segment
public static final int G_ENDSEGM = 0x71; // End Segment
public static final int G_GERASE = 0x7E; // Erase Graphics Plane
public static final int G_GCLINE = 0x81; // Line at Current Position
public static final int G_GCMRK = 0x82; // Marker at Current Position
public static final int G_GCCHST = 0x83; // Character String at Current Position
public static final int G_GCFLT = 0x85; // Fillet at Current Position
public static final int G_GCARC = 0x86; // Partial Arc at Current Position
public static final int G_GCFARC = 0x87; // Full Arc at Current Position
public static final int G_GEIMG = 0x91; // End Image
public static final int G_GIMD = 0x92; // Image Data
public static final int G_GCRLIN = 0xA1; // Relative Line at Current Position
public static final int G_GLINE = 0xC1; // Line (Absolute)
public static final int G_GMRK = 0xC2; // Marker (Absolute)
public static final int G_GCHST = 0xC3; // Character String (Absolute)
public static final int G_GFLT = 0xC5; // Fillet (Absolute)
public static final int G_GARC = 0xC6; // Partial Arc (Absolute)
public static final int G_GFARC = 0xC7; // Full Arc (Absolute)
public static final int G_GBIMG = 0xD1; // Begin Image
public static final int G_GRLINE = 0xE1; // Relative Line (Absolute Start)
// Line Types (GSLT)
public static final int LT_DEFAULT = 0;
public static final int LT_DOT = 1;
public static final int LT_SHORTDASH= 2;
public static final int LT_DASHDOT = 3;
public static final int LT_DOUBLEDOT= 4;
public static final int LT_LONGDASH = 5;
public static final int LT_DASHDOUBLEDOT = 6;
public static final int LT_SOLID = 7;
// Line Widths (GSLW)
public static final int LW_DEFAULT = 0;
public static final int LW_NORMAL = 1;
public static final int LW_THICK = 2;
// Fill Patterns (GSPT)
public static final int PT_DEFAULT = 0;
public static final int PT_D1 = 1;
public static final int PT_D2 = 2;
public static final int PT_D3 = 3;
public static final int PT_D4 = 4;
public static final int PT_D5 = 5;
public static final int PT_D6 = 6;
public static final int PT_D7 = 7;
public static final int PT_D8 = 8;
public static final int PT_VERT_LINE = 9;
public static final int PT_HORIZ_LINE = 10;
public static final int PT_DIAG_BLTR = 11;
public static final int PT_DIAG_BLTR2 = 12;
public static final int PT_DIAG_TLBR = 13;
public static final int PT_DIAG_TLBR2 = 14;
public static final int PT_EMPTY = 15;
public static final int PT_SOLID = 16;
// Marker Symbols (GSMT)
public static final int MK_DEFAULT = 0;
public static final int MK_CROSS = 1; // x
public static final int MK_PLUS = 2; // +
public static final int MK_DIAMOND = 3; // <>
public static final int MK_SQUARE = 4; // []
public static final int MK_6STAR = 5; // * 6-point
public static final int MK_8STAR = 6; // * 8-point
public static final int MK_SDIAMOND = 7; // solid diamond
public static final int MK_SSQUARE = 8; // solid square
public static final int MK_DOT = 9; // .
public static final int MK_CIRCLE = 10;// o
// Character Direction (GSCD)
public static final int CD_DEFAULT = 0;
public static final int CD_LR = 1; // Left to Right
public static final int CD_TB = 2; // Top to Bottom
public static final int CD_RL = 3; // Right to Left
public static final int CD_BT = 4; // Bottom to Top
// Foreground / Background Mix Modes (GSMX / GSBMX)
public static final int MIX_DEFAULT = 0;
public static final int MIX_OR = 1;
public static final int MIX_OVER = 2;
public static final int MIX_LEAVE = 3;
public static final int MIX_XOR = 4;
public static final int MIX_UNDER = 5;
// Graphic Colors (IBM 3179G / HOD 16-color table in 32-bit ARGB)
public static final int[] GOCA_COLORS = new int[] {
0xFF00FF00, // 0: Default (Green)
0xFF7890F0, // 1: Blue (120, 144, 240)
0xFFFF0000, // 2: Red (255, 0, 0)
0xFFFF00FF, // 3: Pink / Magenta (255, 0, 255)
0xFF00FF00, // 4: Green (0, 255, 0)
0xFF00FFFF, // 5: Turquoise / Cyan (0, 255, 255)
0xFFFFFF00, // 6: Yellow (255, 255, 0)
0xFFFFFFFF, // 7: Neutral White (255, 255, 255)
0xFF000000, // 8: Black (0, 0, 0)
0xFF000080, // 9: Deep Blue (0, 0, 128)
0xFF800000, // 10: Orange / Dark Red (128, 0, 0)
0xFF800080, // 11: Purple (128, 0, 128)
0xFF008000, // 12: Pale Green (0, 128, 0)
0xFF008080, // 13: Pale Cyan (0, 128, 128)
0xFFD79700, // 14: Mustard (215, 151, 0)
0xFFC0C0C0, // 15: Grey / Light White (192, 192, 192)
0xFF492400 // 16: Brown (73, 36, 0)
};
/**
* Retrieves the 32-bit ARGB color value for a given GOCA color index (0-16).
* Returns Green (0xFF00FF00) if the index is out of range.
*/
public static int getGocaColorArgb(int colorIndex) {
if (colorIndex >= 0 && colorIndex < GOCA_COLORS.length) {
return GOCA_COLORS[colorIndex];
}
return GOCA_COLORS[0];
}
}
@@ -0,0 +1,827 @@
package org.lib3270j.graphics;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import org.lib3270j.charset.EbcdicTranslator;
/**
* Interprets GOCA (Graphics Object Content Architecture) drawing orders
* and updates the GraphicsPlane.
*/
public class GocaDecoder {
private static final Logger logger = Logger.getLogger(GocaDecoder.class.getName());
private final GraphicsPlane plane;
// Drawing state
private int curX = 0;
private int curY = 0;
private int curColor = GocaConstants.GOCA_COLORS[0];
private int lineType = GocaConstants.LT_SOLID;
private int lineWidth = GocaConstants.LW_NORMAL;
private int markerType = GocaConstants.MK_PLUS;
private int markerSize = 5;
private int markerColor = GocaConstants.GOCA_COLORS[0];
private int pattern = GocaConstants.PT_SOLID;
private int fillColor = GocaConstants.GOCA_COLORS[0];
private int charDir = GocaConstants.CD_LR;
private double charAngle = 0.0;
private int charWidth = 9;
private int charHeight = 16;
private int charSet = 0;
private int arcParamP = 1;
private int arcParamQ = 1;
private int arcParamR = 0;
private int arcParamS = 0;
private ProgramSymbolManager programSymbolManager;
// Area accumulation
private boolean inArea = false;
private boolean areaDrawBoundary = true;
private final List<Integer> areaPointsX = new ArrayList<>();
private final List<Integer> areaPointsY = new ArrayList<>();
// Image accumulation
private boolean inImage = false;
private int imgX = 0;
private int imgY = 0;
private int imgWidth = 0;
private int imgHeight = 0;
private final List<Byte> imgBuffer = new ArrayList<>();
public GocaDecoder(GraphicsPlane plane) {
this.plane = plane;
}
public void setProgramSymbolManager(ProgramSymbolManager psm) {
this.programSymbolManager = psm;
}
public GraphicsPlane getGraphicsPlane() {
return plane;
}
public int getCurX() {
return curX;
}
public int getCurY() {
return curY;
}
public synchronized void resetDefaults() {
curX = 0;
curY = 0;
resetAttributes();
}
public synchronized void resetAttributes() {
curColor = getColor(0);
lineType = GocaConstants.LT_SOLID;
lineWidth = GocaConstants.LW_NORMAL;
markerType = GocaConstants.MK_PLUS;
markerSize = 5;
markerColor = curColor;
pattern = GocaConstants.PT_SOLID;
fillColor = curColor;
charDir = GocaConstants.CD_LR;
charAngle = 0.0;
charSet = 0;
inArea = false;
areaPointsX.clear();
areaPointsY.clear();
inImage = false;
imgBuffer.clear();
}
private byte[] partialOrderBuffer = new byte[0];
private int getOrderLength(byte[] data, int idx, int end) {
int order = data[idx] & 0xFF;
if (order == GocaConstants.G_NOP1 || order == 0xFF) {
return 1;
}
if (order == GocaConstants.G_GEAR ||
order == GocaConstants.G_ENDSEGM || order == GocaConstants.G_ENDPROLOGUE ||
order == GocaConstants.G_GEIMG || order == GocaConstants.G_GPOP) {
return (idx + 1 < end && data[idx + 1] == 0x00) ? 2 : 1;
}
if (order == 0x04 || order == GocaConstants.G_GSMC || order == GocaConstants.G_GSPS ||
order == GocaConstants.G_GSCOL || order == GocaConstants.G_GSMX ||
order == GocaConstants.G_GSBMX || order == GocaConstants.G_GSFLW ||
order == GocaConstants.G_GSLT || order == GocaConstants.G_GSLW ||
order == GocaConstants.G_GSMS || order == GocaConstants.G_GSPT ||
order == GocaConstants.G_GSMT || order == GocaConstants.G_GSCS ||
order == GocaConstants.G_GSMP || order == GocaConstants.G_GSCD ||
order == GocaConstants.G_GSCC || order == GocaConstants.G_GSMS_SET ||
order == GocaConstants.G_GBAR) {
return 2;
}
if (order == GocaConstants.G_GSAP || order == GocaConstants.G_GBIMG || order == 0x91) {
return 10;
}
if (idx + 1 >= end) {
return -1;
}
return (data[idx + 1] & 0xFF) + 2;
}
/**
* Decodes a stream of GOCA drawing orders.
*/
public synchronized void decodeStream(byte[] data, int offset, int length) {
if (data == null || length <= 0 || offset < 0 || offset + length > data.length) {
return;
}
byte[] inputData;
int idx;
int end;
if (partialOrderBuffer.length > 0) {
inputData = new byte[partialOrderBuffer.length + length];
System.arraycopy(partialOrderBuffer, 0, inputData, 0, partialOrderBuffer.length);
System.arraycopy(data, offset, inputData, partialOrderBuffer.length, length);
idx = 0;
end = inputData.length;
partialOrderBuffer = new byte[0];
} else {
inputData = data;
idx = offset;
end = offset + length;
}
while (idx < end) {
int order = inputData[idx] & 0xFF;
int orderLen = getOrderLength(inputData, idx, end);
if (orderLen == -1 || idx + orderLen > end) {
int remaining = end - idx;
partialOrderBuffer = new byte[remaining];
System.arraycopy(inputData, idx, partialOrderBuffer, 0, remaining);
break;
}
int payloadLen = (orderLen >= 2) ? (inputData[idx + 1] & 0xFF) : 0;
switch (order) {
case GocaConstants.G_NOP1:
case 0xFF: {
idx++;
break;
}
case GocaConstants.G_GEAR: {
endArea();
idx += orderLen;
break;
}
case GocaConstants.G_GEIMG: {
endImage();
idx += orderLen;
break;
}
case GocaConstants.G_BEGSEGM: { // Begin Segment (0x70)
if (idx + 7 < end && (inputData[idx + 7] & 0x06) == 0) {
resetAttributes();
}
idx += orderLen;
break;
}
case GocaConstants.G_ENDSEGM: { // End Segment (0x71)
idx += orderLen;
break;
}
case GocaConstants.G_ENDPROLOGUE: { // End Prologue (0x3E)
idx += orderLen;
break;
}
case GocaConstants.G_COMT: { // Comment (0x01)
idx += orderLen;
break;
}
case GocaConstants.G_GSCP: { // Set Current Position (0x21)
if (payloadLen >= 4 && idx + 5 < end) {
curX = readCoord(inputData, idx + 2);
curY = readCoord(inputData, idx + 4);
}
idx += orderLen;
break;
}
case GocaConstants.G_GSAP: { // Arc Parameters (0x22)
if (idx + 9 < end) {
arcParamP = readCoord(inputData, idx + 2);
arcParamQ = readCoord(inputData, idx + 4);
arcParamR = readCoord(inputData, idx + 6);
arcParamS = readCoord(inputData, idx + 8);
}
idx += orderLen;
break;
}
case GocaConstants.G_GSVW: { // Set Viewing Window (0x27)
idx += orderLen;
break;
}
case GocaConstants.G_GSCA: { // Set Character Angle (0x34)
if (payloadLen >= 4 && idx + 5 < end) {
int ax = readCoord(inputData, idx + 2);
int ay = readCoord(inputData, idx + 4);
if (ax != 0 || ay != 0) {
charAngle = Math.toDegrees(Math.atan2(ay, ax));
}
}
idx += orderLen;
break;
}
case GocaConstants.G_GSCH: { // Set Character Cell (0x33)
if (payloadLen >= 4 && idx + 5 < end) {
charWidth = readCoord(inputData, idx + 2);
charHeight = readCoord(inputData, idx + 4);
}
idx += orderLen;
break;
}
case GocaConstants.G_GSCR: { // Set Character Shear (0x35)
idx += orderLen;
break;
}
case GocaConstants.G_GSCOL: { // Set Color (0x0A)
int colIdx = inputData[idx + 1] & 0xFF;
curColor = getColor(colIdx);
fillColor = curColor;
idx += orderLen;
break;
}
case GocaConstants.G_GSECOL: { // Set Extended Color (0x26)
if (payloadLen >= 2 && idx + 3 < end) {
int colIdx = inputData[idx + 3] & 0xFF;
curColor = getColor(colIdx);
fillColor = curColor;
}
idx += orderLen;
break;
}
case GocaConstants.G_GSLT: { // Set Line Type (0x18)
lineType = inputData[idx + 1] & 0xFF;
idx += orderLen;
break;
}
case GocaConstants.G_GSLW: { // Set Line Width (0x19)
lineWidth = inputData[idx + 1] & 0xFF;
idx += orderLen;
break;
}
case GocaConstants.G_GSMC: { // Set Marker Color (0x07)
int colIdx = inputData[idx + 1] & 0xFF;
markerColor = getColor(colIdx);
idx += orderLen;
break;
}
case GocaConstants.G_GSMS: { // Set Marker Size (0x1B)
markerSize = inputData[idx + 1] & 0xFF;
idx += orderLen;
break;
}
case GocaConstants.G_GSMT: { // Set Marker Type (0x29)
markerType = inputData[idx + 1] & 0xFF;
idx += orderLen;
break;
}
case GocaConstants.G_GSPS: { // Set Pattern Set (0x08)
pattern = inputData[idx + 1] & 0xFF;
idx += orderLen;
break;
}
case GocaConstants.G_GSPT: { // Set Pattern Symbol (0x28)
pattern = inputData[idx + 1] & 0xFF;
idx += orderLen;
break;
}
case GocaConstants.G_GSCD: { // Set Character Direction (0x3A)
charDir = inputData[idx + 1] & 0xFF;
idx += orderLen;
break;
}
case GocaConstants.G_GSCS: { // Set Character Set (0x38)
charSet = inputData[idx + 1] & 0xFF;
idx += orderLen;
break;
}
case 0x04:
case GocaConstants.G_GSMX:
case GocaConstants.G_GSBMX:
case GocaConstants.G_GSFLW:
case GocaConstants.G_GSMP:
case GocaConstants.G_GSCC:
case GocaConstants.G_GSMS_SET:
case GocaConstants.G_GPOP: {
idx += orderLen;
break;
}
case GocaConstants.G_GBAR: { // Begin Area (0x68)
int flags = inputData[idx + 1] & 0xFF;
beginArea((flags & 0x40) != 0);
idx += orderLen;
break;
}
case GocaConstants.G_GLINE: { // Line Absolute (0xC1)
if (payloadLen >= 4) {
processLine(inputData, idx + 2, payloadLen, false);
}
idx += orderLen;
break;
}
case GocaConstants.G_GCLINE: { // Line Current Position (0x81)
if (payloadLen >= 4) {
processLine(inputData, idx + 2, payloadLen, true);
}
idx += orderLen;
break;
}
case GocaConstants.G_GRLINE: { // Relative Line Absolute Start (0xE1)
if (payloadLen >= 4) {
processRelativeLine(inputData, idx + 2, payloadLen, false);
}
idx += orderLen;
break;
}
case GocaConstants.G_GCRLIN: { // Relative Line Current Position (0xA1)
if (payloadLen >= 2) {
processRelativeLine(inputData, idx + 2, payloadLen, true);
}
idx += orderLen;
break;
}
case GocaConstants.G_GFARC: { // Full Arc Absolute (0xC7)
if (payloadLen >= 4) {
processArc(inputData, idx + 2, payloadLen, false, true);
}
idx += orderLen;
break;
}
case GocaConstants.G_GCFARC: { // Full Arc Current Position (0x87)
if (payloadLen >= 2) {
processArc(inputData, idx + 2, payloadLen, true, true);
}
idx += orderLen;
break;
}
case GocaConstants.G_GARC: { // Partial Arc Absolute (0xC6)
if (payloadLen >= 8) {
processArc(inputData, idx + 2, payloadLen, false, false);
}
idx += orderLen;
break;
}
case GocaConstants.G_GCARC: { // Partial Arc Current Position (0x86)
if (payloadLen >= 4) {
processArc(inputData, idx + 2, payloadLen, true, false);
}
idx += orderLen;
break;
}
case GocaConstants.G_GFLT: { // Fillet Absolute (0xC5)
if (payloadLen >= 4) {
processFillet(inputData, idx + 2, payloadLen, false);
}
idx += orderLen;
break;
}
case GocaConstants.G_GCFLT: { // Fillet Current Position (0x85)
if (payloadLen >= 4) {
processFillet(inputData, idx + 2, payloadLen, true);
}
idx += orderLen;
break;
}
case GocaConstants.G_GMRK: { // Marker Absolute (0xC2)
if (payloadLen >= 4) {
processMarker(inputData, idx + 2, payloadLen, false);
}
idx += orderLen;
break;
}
case GocaConstants.G_GCMRK: { // Marker Current Position (0x82)
if (payloadLen >= 4) {
processMarker(inputData, idx + 2, payloadLen, true);
}
idx += orderLen;
break;
}
case GocaConstants.G_GCHST: { // Character String Absolute (0xC3)
if (payloadLen >= 4) {
processText(inputData, idx + 2, payloadLen, false);
}
idx += orderLen;
break;
}
case GocaConstants.G_GCCHST: { // Character String Current Position (0x83)
if (payloadLen >= 0) {
processText(inputData, idx + 2, payloadLen, true);
}
idx += orderLen;
break;
}
case GocaConstants.G_GBIMG: { // Begin Image (0xD1)
if (idx + 9 < end) {
int x = readCoord(inputData, idx + 2);
int y = readCoord(inputData, idx + 4);
int w = readCoord(inputData, idx + 6);
int h = readCoord(inputData, idx + 8);
beginImage(x, y, w, h);
}
idx += orderLen;
break;
}
case GocaConstants.G_GIMD: { // Image Data (0x92)
if (inImage) {
for (int k = 0; k < payloadLen; k++) {
imgBuffer.add(inputData[idx + 2 + k]);
}
}
idx += orderLen;
break;
}
default: {
idx += orderLen;
break;
}
}
}
}
/**
* Decodes a stream of Object Control Procedure Orders (subtype 0x11).
*/
public synchronized void processProcedureOrders(byte[] data, int offset, int length) {
if (data == null || length <= 0 || offset < 0 || offset + length > data.length) {
return;
}
int idx = offset;
int end = offset + length;
while (idx < end) {
int order = data[idx] & 0xFF;
switch (order) {
case GocaConstants.P_NOP1:
case GocaConstants.P_ATTCUR:
case GocaConstants.P_DETCUR:
case GocaConstants.P_STOPDR: {
idx++;
break;
}
case GocaConstants.P_ERASE: { // 0x0A: Erase Graphics Presentation Space
plane.clear();
resetDefaults();
idx++;
break;
}
case GocaConstants.P_BEGPROC: { // 0x30: Begin Procedure (length 12)
int len = 12;
if (idx + 1 < end && data[idx + 1] != 0) {
len = (data[idx + 1] & 0xFF) + 2;
}
idx += len;
break;
}
case GocaConstants.P_SCUDEF: { // 0x21: Set Current Defaults
if (idx + 1 < end) {
int len = data[idx + 1] & 0xFF;
if (idx + 2 + len <= end) {
decodeStream(data, idx + 2, len);
}
idx += 2 + len;
} else {
idx++;
}
break;
}
case GocaConstants.P_COMT:
case GocaConstants.P_SETCUR: {
if (idx + 1 < end) {
int len = data[idx + 1] & 0xFF;
idx += 2 + len;
} else {
idx++;
}
break;
}
default: {
if (idx + 1 < end) {
int len = data[idx + 1] & 0xFF;
idx += 2 + len;
} else {
idx++;
}
break;
}
}
}
}
private void beginArea(boolean drawBoundary) {
this.inArea = true;
this.areaDrawBoundary = drawBoundary;
this.fillColor = this.curColor;
this.areaPointsX.clear();
this.areaPointsY.clear();
}
private void endArea() {
if (!inArea || areaPointsX.size() < 3) {
inArea = false;
areaPointsX.clear();
areaPointsY.clear();
return;
}
int n = areaPointsX.size();
int[] px = new int[n];
int[] py = new int[n];
for (int i = 0; i < n; i++) {
px[i] = plane.mapX(areaPointsX.get(i));
py[i] = plane.mapY(areaPointsY.get(i));
}
plane.fillArea(px, py, n, fillColor, pattern, areaDrawBoundary, curColor, lineType, lineWidth);
inArea = false;
areaPointsX.clear();
areaPointsY.clear();
}
private void addAreaPoint(int x, int y) {
if (inArea) {
areaPointsX.add(x);
areaPointsY.add(y);
}
}
private void beginImage(int x, int y, int w, int h) {
this.inImage = true;
this.imgX = x;
this.imgY = y;
this.imgWidth = w;
this.imgHeight = h;
this.imgBuffer.clear();
}
private void endImage() {
if (!inImage || imgWidth <= 0 || imgHeight <= 0 || imgBuffer.isEmpty()) {
inImage = false;
return;
}
byte[] bytes = new byte[imgBuffer.size()];
for (int i = 0; i < bytes.length; i++) {
bytes[i] = imgBuffer.get(i);
}
int px = plane.mapX(imgX);
int py = plane.mapY(imgY);
plane.drawImage(px, py, imgWidth, imgHeight, bytes, curColor);
inImage = false;
imgBuffer.clear();
}
private void processLine(byte[] data, int off, int len, boolean fromCurPos) {
int pos = off;
int end = off + len;
int startX = curX;
int startY = curY;
if (!fromCurPos && pos + 4 <= end) {
startX = readCoord(data, pos);
startY = readCoord(data, pos + 2);
pos += 4;
}
if (inArea) {
addAreaPoint(startX, startY);
}
while (pos + 4 <= end) {
int nextX = readCoord(data, pos);
int nextY = readCoord(data, pos + 2);
pos += 4;
if (inArea) {
addAreaPoint(nextX, nextY);
} else {
plane.drawLine(plane.mapX(startX), plane.mapY(startY),
plane.mapX(nextX), plane.mapY(nextY),
curColor, lineType, lineWidth);
}
startX = nextX;
startY = nextY;
}
curX = startX;
curY = startY;
}
private void processRelativeLine(byte[] data, int off, int len, boolean fromCurPos) {
int pos = off;
int end = off + len;
int startX = curX;
int startY = curY;
if (!fromCurPos && pos + 4 <= end) {
startX = readCoord(data, pos);
startY = readCoord(data, pos + 2);
pos += 4;
}
if (inArea) {
addAreaPoint(startX, startY);
}
while (pos + 2 <= end) {
int dx = (byte) data[pos];
int dy = (byte) data[pos + 1];
pos += 2;
int nextX = startX + dx;
int nextY = startY + dy;
if (inArea) {
addAreaPoint(nextX, nextY);
} else {
plane.drawLine(plane.mapX(startX), plane.mapY(startY),
plane.mapX(nextX), plane.mapY(nextY),
curColor, lineType, lineWidth);
}
startX = nextX;
startY = nextY;
}
curX = startX;
curY = startY;
}
private void processArc(byte[] data, int off, int len, boolean fromCurPos, boolean isFull) {
int pos = off;
int startX = curX;
int startY = curY;
if (!fromCurPos && pos + 4 <= off + len) {
startX = readCoord(data, pos);
startY = readCoord(data, pos + 2);
pos += 4;
}
double multiplier = 1.0;
if (pos + 2 <= off + len) {
multiplier = (data[pos] & 0xFF) + ((data[pos + 1] & 0xFF) / 255.0);
if (multiplier == 0.0) multiplier = 1.0;
}
int dxP = Math.abs(arcParamP - arcParamR);
int dyQ = Math.abs(arcParamQ - arcParamS);
if (dxP == 0) dxP = 10;
if (dyQ == 0) dyQ = 10;
int rxVirtual = (int) (dxP * multiplier);
int ryVirtual = (int) (dyQ * multiplier);
int rx = Math.abs(plane.mapX(rxVirtual) - plane.mapX(0));
int ry = Math.abs(plane.mapY(ryVirtual) - plane.mapY(0));
if (rx <= 0) rx = 10;
if (ry <= 0) ry = 10;
plane.drawArc(plane.mapX(startX), plane.mapY(startY), rx, ry, 0.0, 360.0,
curColor, lineType, lineWidth, isFull);
curX = startX;
curY = startY;
}
private void processFillet(byte[] data, int off, int len, boolean fromCurPos) {
int pos = off;
int end = off + len;
List<Integer> ptsX = new ArrayList<>();
List<Integer> ptsY = new ArrayList<>();
if (fromCurPos) {
ptsX.add(plane.mapX(curX));
ptsY.add(plane.mapY(curY));
}
while (pos + 4 <= end) {
int x = readCoord(data, pos);
int y = readCoord(data, pos + 2);
ptsX.add(plane.mapX(x));
ptsY.add(plane.mapY(y));
curX = x;
curY = y;
pos += 4;
}
if (ptsX.size() >= 2) {
int n = ptsX.size();
int[] px = new int[n];
int[] py = new int[n];
for (int i = 0; i < n; i++) {
px[i] = ptsX.get(i);
py[i] = ptsY.get(i);
}
plane.drawFillet(px, py, n, curColor, lineType, lineWidth);
}
}
private void processMarker(byte[] data, int off, int len, boolean fromCurPos) {
int pos = off;
int end = off + len;
if (fromCurPos) {
plane.drawMarker(plane.mapX(curX), plane.mapY(curY), markerType, markerSize, markerColor);
}
while (pos + 4 <= end) {
int x = readCoord(data, pos);
int y = readCoord(data, pos + 2);
plane.drawMarker(plane.mapX(x), plane.mapY(y), markerType, markerSize, markerColor);
curX = x;
curY = y;
pos += 4;
}
}
private void processText(byte[] data, int off, int len, boolean fromCurPos) {
int pos = off;
int end = off + len;
int startX = curX;
int startY = curY;
if (!fromCurPos && pos + 4 <= end) {
startX = readCoord(data, pos);
startY = readCoord(data, pos + 2);
pos += 4;
}
int textLen = end - pos;
if (textLen <= 0) return;
int cw = charWidth > 0 ? (int) Math.round((double) charWidth * plane.getCanvasWidth() / (plane.getScreenCols() * 9.0)) : 12;
int ch = charHeight > 0 ? (int) Math.round((double) charHeight * plane.getCanvasHeight() / (plane.getScreenRows() * 16.0)) : 20;
if (charSet != 0 && programSymbolManager != null) {
for (int i = 0; i < textLen; i++) {
int code = data[pos + i] & 0xFF;
int px = plane.mapX(startX);
int py = plane.mapY(startY) - ch;
ProgramSymbolSet.SymbolSlot slot = programSymbolManager.getSymbol(charSet, code);
if (slot != null) {
int[] rgb = slot.getRgbPixels(curColor, 0);
int symW = slot.getWidth();
int symH = slot.getHeight();
for (int dy = 0; dy < ch; dy++) {
int sy = (dy * symH) / ch;
for (int dx = 0; dx < cw; dx++) {
int sx = (dx * symW) / cw;
int pixelArgb = rgb[sy * symW + sx];
if ((pixelArgb >>> 24) != 0) {
plane.setPixel(px + dx, py + dy, pixelArgb);
}
}
}
} else {
char c = EbcdicTranslator.ebcdicToAscii(data[pos + i]);
plane.drawVectorText(px, py, String.valueOf(c), curColor, cw, ch, charDir, charAngle);
}
startX += (charWidth > 0 ? charWidth : 9);
}
curX = startX;
curY = startY;
return;
}
char[] chars = new char[textLen];
for (int i = 0; i < textLen; i++) {
chars[i] = EbcdicTranslator.ebcdicToAscii(data[pos + i]);
}
String text = new String(chars);
plane.drawVectorText(plane.mapX(startX), plane.mapY(startY) - ch, text,
curColor, cw, ch, charDir, charAngle);
curX = startX + (textLen * (charWidth > 0 ? charWidth : 9));
curY = startY;
}
private int readCoord(byte[] data, int off) {
return (short) (((data[off] & 0xFF) << 8) | (data[off + 1] & 0xFF));
}
private int getColor(int colorIndex) {
return GocaConstants.getGocaColorArgb(colorIndex);
}
}
@@ -0,0 +1,70 @@
package org.lib3270j.graphics;
/**
* Graphics modes supported by j3270 / lib3270j.
*/
public enum GraphicsMode {
/** Text only (no graphics query replies, classic 3279-4 terminal behavior). */
NONE("None (Text Only)"),
/** Programmed Symbols only (custom character matrices and APL, single & triple plane). */
PROGRAMMED_SYMBOLS("Programmed Symbols Only"),
/** Vector graphics only (GOCA / 3179G drawing orders). */
VECTOR_GRAPHICS("Vector Graphics Only"),
/** Full graphics support (both Programmed Symbols and Vector Graphics). */
BOTH("Both (Programmed Symbols & Vector Graphics)");
private final String description;
GraphicsMode(String description) {
this.description = description;
}
public String getDescription() {
return description;
}
public boolean isProgrammedSymbolsEnabled() {
return this == PROGRAMMED_SYMBOLS || this == BOTH;
}
public boolean isVectorGraphicsEnabled() {
return this == VECTOR_GRAPHICS || this == BOTH;
}
public static GraphicsMode fromString(String str) {
if (str == null || str.trim().isEmpty()) {
return NONE;
}
String s = str.trim().toUpperCase();
switch (s) {
case "BOTH":
case "ALL":
case "FULL":
case "ON":
case "TRUE":
return BOTH;
case "PS":
case "PROGRAMMED_SYMBOLS":
case "PROGRAMMEDSYMBOLS":
case "SYMBOLS":
case "APL":
return PROGRAMMED_SYMBOLS;
case "VECTOR":
case "VECTOR_GRAPHICS":
case "VECTORGRAPHICS":
case "GOCA":
case "GDDM":
return VECTOR_GRAPHICS;
case "NONE":
case "OFF":
case "FALSE":
case "DISABLED":
case "TEXT":
default:
return NONE;
}
}
}
@@ -0,0 +1,524 @@
package org.lib3270j.graphics;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.logging.Logger;
/**
* Offscreen rendering surface for GOCA vector graphics.
* Maintained as an ARGB 32-bit integer pixel buffer that overlays the 3270 character cell matrix.
* Pure Java software rasterizer compatible with standard Java SE (Swing) and Android (Bitmap).
*/
public class GraphicsPlane {
private static final Logger logger = Logger.getLogger(GraphicsPlane.class.getName());
// 17 Standard GOCA 8x8 Fill Patterns
public static final byte[][] PATTERN_DATA = new byte[][]{
{-1, -1, -1, -1, -1, -1, -1, -1}, // 0: Solid (all 1s)
{-1, -1, -1, -18, -1, -1, -1, -18}, // 1: D1
{-1, -69, -1, -18, -1, -69, -1, -18}, // 2: D2
{119, -35, -69, -18, 119, -35, -69, -18}, // 3: D3
{-69, -52, 51, -18, -69, -52, 51, -18}, // 4: D4
{85, -86, 85, -86, 85, -86, 85, -86}, // 5: D5 (50% checker)
{68, 51, -52, 17, 68, 51, -52, 17}, // 6: D6
{-120, 34, 68, 17, -120, 34, 68, 17}, // 7: D7
{0, 68, 0, 17, 0, 68, 0, 17}, // 8: D8 (sparse dots)
{-128, -128, -128, -128, -128, -128, -128, -128}, // 9: Vertical line
{-1, 0, 0, 0, 0, 0, 0, 0}, // 10: Horizontal line
{1, 2, 4, 8, 16, 32, 64, -128}, // 11: Diagonal bottom-left to top-right
{3, 12, 48, -64, 3, 12, 48, -64}, // 12: Diagonal BL-TR dense
{-128, 64, 32, 16, 8, 4, 2, 1}, // 13: Diagonal top-left to bottom-right
{-64, 48, 12, 3, -64, 48, 12, 3}, // 14: Diagonal TL-BR dense
{0, 0, 0, 0, 0, 0, 0, 0}, // 15: Empty (transparent)
{-1, -1, -1, -1, -1, -1, -1, -1} // 16: Solid
};
private int canvasWidth = 800;
private int canvasHeight = 600;
private int[] rgbBuffer;
private boolean hasContent = false;
private int screenCols = 80;
private int screenRows = 24;
public GraphicsPlane(int width, int height) {
this.canvasWidth = Math.max(1, width);
this.canvasHeight = Math.max(1, height);
this.rgbBuffer = new int[canvasWidth * canvasHeight];
}
public synchronized void resize(int width, int height) {
int w = Math.max(1, width);
int h = Math.max(1, height);
if (w == canvasWidth && h == canvasHeight && rgbBuffer != null) {
return;
}
int[] newBuffer = new int[w * h];
if (rgbBuffer != null && hasContent) {
// Scale existing content to new dimensions using nearest-neighbor
for (int dy = 0; dy < h; dy++) {
int sy = (dy * canvasHeight) / h;
for (int dx = 0; dx < w; dx++) {
int sx = (dx * canvasWidth) / w;
newBuffer[dy * w + dx] = rgbBuffer[sy * canvasWidth + sx];
}
}
}
this.canvasWidth = w;
this.canvasHeight = h;
this.rgbBuffer = newBuffer;
}
public synchronized void clear() {
if (rgbBuffer != null) {
Arrays.fill(rgbBuffer, 0);
}
hasContent = false;
}
public synchronized boolean hasContent() {
return hasContent;
}
public synchronized int[] getRgbBuffer() {
return rgbBuffer;
}
public int getCanvasWidth() {
return canvasWidth;
}
public int getCanvasHeight() {
return canvasHeight;
}
public void setScreenDimensions(int cols, int rows) {
this.screenCols = cols > 0 ? cols : 80;
this.screenRows = rows > 0 ? rows : 24;
}
public int getScreenCols() {
return screenCols;
}
public int getScreenRows() {
return screenRows;
}
/**
* Maps a 3179G / GOCA signed coordinate (centered at screen midpoint) to canvas pixel X.
*/
public int mapX(int gocaX) {
int nominalWidth = screenCols * 9;
int xMax = (nominalWidth - 1) / 2 + ((nominalWidth - 1) % 2 != 0 ? 1 : 0);
int nx = gocaX + xMax;
return (int) Math.round((double) nx * canvasWidth / nominalWidth);
}
/**
* Maps a 3179G / GOCA signed coordinate (centered at screen midpoint, bottom-up) to canvas pixel Y (top-down).
*/
public int mapY(int gocaY) {
int nominalHeight = screenRows * 16;
int yMax = (nominalHeight - 1) / 2;
int ny = yMax - gocaY;
return (int) Math.round((double) ny * canvasHeight / nominalHeight);
}
/**
* Safely plots a pixel at (x, y).
*/
public synchronized void setPixel(int x, int y, int colorArgb) {
if (x >= 0 && x < canvasWidth && y >= 0 && y < canvasHeight) {
rgbBuffer[y * canvasWidth + x] = colorArgb;
hasContent = true;
}
}
/**
* Draws an absolute or relative line using Bresenham's algorithm with line styles and widths.
*/
public synchronized void drawLine(int x1, int y1, int x2, int y2, int colorArgb, int lineType, int lineWidth) {
int color = (colorArgb != 0) ? colorArgb : 0xFFFFFFFF;
int thickness = (lineWidth == GocaConstants.LW_THICK) ? 2 : 1;
int dx = Math.abs(x2 - x1);
int dy = Math.abs(y2 - y1);
int sx = x1 < x2 ? 1 : -1;
int sy = y1 < y2 ? 1 : -1;
int err = dx - dy;
int curX = x1;
int curY = y1;
int stepIndex = 0;
while (true) {
if (shouldPlotLinePixel(stepIndex, lineType)) {
drawPixelWithThickness(curX, curY, color, thickness);
}
stepIndex++;
if (curX == x2 && curY == y2) {
break;
}
int e2 = 2 * err;
if (e2 > -dy) {
err -= dy;
curX += sx;
}
if (e2 < dx) {
err += dx;
curY += sy;
}
}
hasContent = true;
}
private boolean shouldPlotLinePixel(int step, int lineType) {
switch (lineType) {
case GocaConstants.LT_DOT:
return (step % 4) < 2;
case GocaConstants.LT_SHORTDASH:
return (step % 6) < 4;
case GocaConstants.LT_DASHDOT:
int m12 = step % 12;
return m12 < 6 || (m12 >= 8 && m12 < 10);
case GocaConstants.LT_DOUBLEDOT:
int m10 = step % 10;
return m10 < 2 || (m10 >= 4 && m10 < 6);
case GocaConstants.LT_LONGDASH:
return (step % 11) < 8;
case GocaConstants.LT_DASHDOUBLEDOT:
int m18 = step % 18;
return m18 < 8 || (m18 >= 10 && m18 < 12) || (m18 >= 14 && m18 < 16);
case GocaConstants.LT_SOLID:
case GocaConstants.LT_DEFAULT:
default:
return true;
}
}
private void drawPixelWithThickness(int x, int y, int color, int thickness) {
if (thickness <= 1) {
setPixel(x, y, color);
} else {
for (int dy = -(thickness - 1); dy <= (thickness - 1); dy++) {
for (int dx = -(thickness - 1); dx <= (thickness - 1); dx++) {
setPixel(x + dx, y + dy, color);
}
}
}
}
/**
* Draws a full or partial arc / ellipse.
*/
public synchronized void drawArc(int cx, int cy, int rx, int ry, double startAngleDeg, double sweepAngleDeg,
int colorArgb, int lineType, int lineWidth, boolean isFull) {
if (rx <= 0) rx = 1;
if (ry <= 0) ry = 1;
int numSteps = Math.max(24, Math.max(rx, ry) * 4);
double startRad = Math.toRadians(startAngleDeg);
double sweepRad = isFull ? (2.0 * Math.PI) : Math.toRadians(sweepAngleDeg);
double stepRad = sweepRad / numSteps;
int prevX = (int) Math.round(cx + rx * Math.cos(startRad));
int prevY = (int) Math.round(cy - ry * Math.sin(startRad));
for (int i = 1; i <= numSteps; i++) {
double angle = startRad + i * stepRad;
int nextX = (int) Math.round(cx + rx * Math.cos(angle));
int nextY = (int) Math.round(cy - ry * Math.sin(angle));
drawLine(prevX, prevY, nextX, nextY, colorArgb, lineType, lineWidth);
prevX = nextX;
prevY = nextY;
}
hasContent = true;
}
/**
* Draws a Fillet (spline / curve approximation across control points).
*/
public synchronized void drawFillet(int[] px, int[] py, int numPoints, int colorArgb, int lineType, int lineWidth) {
if (px == null || py == null || numPoints < 2) return;
if (numPoints == 2) {
drawLine(px[0], py[0], px[1], py[1], colorArgb, lineType, lineWidth);
return;
}
int prevX = px[0];
int prevY = py[0];
for (int i = 0; i < numPoints - 1; i++) {
double p0x = (i == 0) ? px[0] : (px[i - 1] + px[i]) / 2.0;
double p0y = (i == 0) ? py[0] : (py[i - 1] + py[i]) / 2.0;
double p1x = px[i];
double p1y = py[i];
double p2x = (i == numPoints - 2) ? px[numPoints - 1] : (px[i] + px[i + 1]) / 2.0;
double p2y = (i == numPoints - 2) ? py[numPoints - 1] : (py[i] + py[i + 1]) / 2.0;
int steps = 20;
for (int s = 1; s <= steps; s++) {
double t = (double) s / steps;
double oneMinusT = 1.0 - t;
double bx = oneMinusT * oneMinusT * p0x + 2.0 * oneMinusT * t * p1x + t * t * p2x;
double by = oneMinusT * oneMinusT * p0y + 2.0 * oneMinusT * t * p1y + t * t * p2y;
int nextX = (int) Math.round(bx);
int nextY = (int) Math.round(by);
drawLine(prevX, prevY, nextX, nextY, colorArgb, lineType, lineWidth);
prevX = nextX;
prevY = nextY;
}
}
hasContent = true;
}
/**
* Fills a closed polygon area with a solid color or hatching pattern.
*/
public synchronized void fillArea(int[] px, int[] py, int numPoints, int fillColorArgb, int pattern,
boolean drawBoundary, int boundaryColorArgb, int lineType, int lineWidth) {
if (px == null || py == null || numPoints < 3) return;
int fill = (fillColorArgb != 0) ? fillColorArgb : 0xFFFFFFFF;
if (pattern != GocaConstants.PT_EMPTY) {
// Find polygon vertical bounds
int minY = py[0];
int maxY = py[0];
for (int i = 1; i < numPoints; i++) {
if (py[i] < minY) minY = py[i];
if (py[i] > maxY) maxY = py[i];
}
minY = Math.max(0, minY);
maxY = Math.min(canvasHeight - 1, maxY);
List<Integer> nodeX = new ArrayList<>();
byte[] patRows = (pattern >= 0 && pattern < PATTERN_DATA.length) ? PATTERN_DATA[pattern] : PATTERN_DATA[0];
for (int y = minY; y <= maxY; y++) {
nodeX.clear();
int j = numPoints - 1;
for (int i = 0; i < numPoints; i++) {
if ((py[i] < y && py[j] >= y) || (py[j] < y && py[i] >= y)) {
int x = px[i] + (int) Math.round((double) (y - py[i]) / (py[j] - py[i]) * (px[j] - px[i]));
nodeX.add(x);
}
j = i;
}
Collections.sort(nodeX);
for (int i = 0; i < nodeX.size(); i += 2) {
if (i + 1 >= nodeX.size()) break;
int leftX = Math.max(0, nodeX.get(i));
int rightX = Math.min(canvasWidth - 1, nodeX.get(i + 1));
for (int x = leftX; x <= rightX; x++) {
if (pattern == GocaConstants.PT_SOLID || pattern == GocaConstants.PT_DEFAULT || pattern > 16) {
setPixel(x, y, fill);
} else {
int b = patRows[y & 7] & 0xFF;
if (((b >> (7 - (x & 7))) & 1) != 0) {
setPixel(x, y, fill);
}
}
}
}
}
}
if (drawBoundary && boundaryColorArgb != 0) {
for (int i = 0; i < numPoints; i++) {
int next = (i + 1) % numPoints;
drawLine(px[i], py[i], px[next], py[next], boundaryColorArgb, lineType, lineWidth);
}
}
hasContent = true;
}
/**
* Draws a GOCA marker symbol (+, x, diamond, square, star, dot, circle).
*/
public synchronized void drawMarker(int x, int y, int markerType, int size, int colorArgb) {
int color = (colorArgb != 0) ? colorArgb : 0xFFFFFFFF;
int s = Math.max(3, size > 0 ? size : 5);
switch (markerType) {
case GocaConstants.MK_CROSS: // x
drawLine(x - s, y - s, x + s, y + s, color, GocaConstants.LT_SOLID, GocaConstants.LW_NORMAL);
drawLine(x - s, y + s, x + s, y - s, color, GocaConstants.LT_SOLID, GocaConstants.LW_NORMAL);
break;
case GocaConstants.MK_PLUS: // +
case GocaConstants.MK_DEFAULT:
drawLine(x - s, y, x + s, y, color, GocaConstants.LT_SOLID, GocaConstants.LW_NORMAL);
drawLine(x, y - s, x, y + s, color, GocaConstants.LT_SOLID, GocaConstants.LW_NORMAL);
break;
case GocaConstants.MK_DIAMOND: // <>
drawLine(x, y - s, x + s, y, color, GocaConstants.LT_SOLID, GocaConstants.LW_NORMAL);
drawLine(x + s, y, x, y + s, color, GocaConstants.LT_SOLID, GocaConstants.LW_NORMAL);
drawLine(x, y + s, x - s, y, color, GocaConstants.LT_SOLID, GocaConstants.LW_NORMAL);
drawLine(x - s, y, x, y - s, color, GocaConstants.LT_SOLID, GocaConstants.LW_NORMAL);
break;
case GocaConstants.MK_SQUARE: // []
drawLine(x - s, y - s, x + s, y - s, color, GocaConstants.LT_SOLID, GocaConstants.LW_NORMAL);
drawLine(x + s, y - s, x + s, y + s, color, GocaConstants.LT_SOLID, GocaConstants.LW_NORMAL);
drawLine(x + s, y + s, x - s, y + s, color, GocaConstants.LT_SOLID, GocaConstants.LW_NORMAL);
drawLine(x - s, y + s, x - s, y - s, color, GocaConstants.LT_SOLID, GocaConstants.LW_NORMAL);
break;
case GocaConstants.MK_6STAR: // 6-point star
drawLine(x - s, y, x + s, y, color, GocaConstants.LT_SOLID, GocaConstants.LW_NORMAL);
drawLine(x - s / 2, y - s, x + s / 2, y + s, color, GocaConstants.LT_SOLID, GocaConstants.LW_NORMAL);
drawLine(x - s / 2, y + s, x + s / 2, y - s, color, GocaConstants.LT_SOLID, GocaConstants.LW_NORMAL);
break;
case GocaConstants.MK_8STAR: // 8-point star
drawLine(x - s, y, x + s, y, color, GocaConstants.LT_SOLID, GocaConstants.LW_NORMAL);
drawLine(x, y - s, x, y + s, color, GocaConstants.LT_SOLID, GocaConstants.LW_NORMAL);
drawLine(x - s, y - s, x + s, y + s, color, GocaConstants.LT_SOLID, GocaConstants.LW_NORMAL);
drawLine(x - s, y + s, x + s, y - s, color, GocaConstants.LT_SOLID, GocaConstants.LW_NORMAL);
break;
case GocaConstants.MK_SDIAMOND: // solid diamond
fillArea(new int[]{x, x + s, x, x - s}, new int[]{y - s, y, y + s, y}, 4,
color, GocaConstants.PT_SOLID, false, 0, 0, 0);
break;
case GocaConstants.MK_SSQUARE: // solid square
fillArea(new int[]{x - s, x + s, x + s, x - s}, new int[]{y - s, y - s, y + s, y + s}, 4,
color, GocaConstants.PT_SOLID, false, 0, 0, 0);
break;
case GocaConstants.MK_DOT: // dot
for (int dy = -2; dy <= 2; dy++) {
for (int dx = -2; dx <= 2; dx++) {
if (dx * dx + dy * dy <= 4) {
setPixel(x + dx, y + dy, color);
}
}
}
break;
case GocaConstants.MK_CIRCLE: // circle
default:
drawArc(x, y, s, s, 0.0, 360.0, color, GocaConstants.LT_SOLID, GocaConstants.LW_NORMAL, true);
break;
}
hasContent = true;
}
private static final int[] VSS_OFFSETS = new int[256];
static {
Arrays.fill(VSS_OFFSETS, -1);
int sym = VectorSymbolData.VSS_SYMBOL_START; // 33
if (sym < 256) {
VSS_OFFSETS[sym] = 0;
}
for (int i = 0; i < VectorSymbolData.vss_data.length; i++) {
if (VectorSymbolData.vss_data[i] == VectorSymbolData.END_DEFAULT) { // 0xFF
sym++;
if (sym < 256 && i + 1 < VectorSymbolData.vss_data.length) {
VSS_OFFSETS[sym] = i + 1;
}
}
}
}
/**
* Draws stroked vector text using IBM Vector Symbol Set (VSS).
*/
public synchronized void drawVectorText(int x, int y, String text, int colorArgb,
int cellWidth, int cellHeight, int dir, double angle) {
if (text == null || text.isEmpty()) return;
int color = (colorArgb != 0) ? colorArgb : 0xFFFFFFFF;
int curX = x;
int curY = y;
int cw = cellWidth > 0 ? cellWidth : 12;
int ch = cellHeight > 0 ? cellHeight : 20;
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
drawVssChar(curX, curY, c, color, cw, ch);
switch (dir) {
case GocaConstants.CD_TB: curY += ch; break;
case GocaConstants.CD_RL: curX -= cw; break;
case GocaConstants.CD_BT: curY -= ch; break;
case GocaConstants.CD_LR:
case GocaConstants.CD_DEFAULT:
default:
curX += cw;
break;
}
}
hasContent = true;
}
private void drawVssChar(int x, int y, char c, int color, int cw, int ch) {
int code = (int) c;
if (code < VectorSymbolData.VSS_SYMBOL_START || code >= 256) {
return;
}
int offset = VSS_OFFSETS[code];
if (offset < 0) {
return;
}
int ptr = offset;
while (ptr < VectorSymbolData.vss_data.length && VectorSymbolData.vss_data[ptr] != VectorSymbolData.END_DEFAULT) {
int order = VectorSymbolData.vss_data[ptr] & 0xFF;
if (order == 0xC1) {
int byteLen = VectorSymbolData.vss_data[ptr + 1] & 0xFF;
int numPoints = byteLen / 4;
int dataPtr = ptr + 2;
if (numPoints >= 2) {
int prevVx = ((VectorSymbolData.vss_data[dataPtr] & 0xFF) << 8) | (VectorSymbolData.vss_data[dataPtr + 1] & 0xFF);
int prevVy = ((VectorSymbolData.vss_data[dataPtr + 2] & 0xFF) << 8) | (VectorSymbolData.vss_data[dataPtr + 3] & 0xFF);
int prevPx = x + (int) Math.round(((double) prevVx / VectorSymbolData.VSS_WIDTH) * cw);
int prevPy = y + (int) Math.round(((double)(VectorSymbolData.VSS_HEIGHT - prevVy) / VectorSymbolData.VSS_HEIGHT) * ch);
for (int p = 1; p < numPoints; p++) {
int vx = ((VectorSymbolData.vss_data[dataPtr + p * 4] & 0xFF) << 8) | (VectorSymbolData.vss_data[dataPtr + p * 4 + 1] & 0xFF);
int vy = ((VectorSymbolData.vss_data[dataPtr + p * 4 + 2] & 0xFF) << 8) | (VectorSymbolData.vss_data[dataPtr + p * 4 + 3] & 0xFF);
int px = x + (int) Math.round(((double) vx / VectorSymbolData.VSS_WIDTH) * cw);
int py = y + (int) Math.round(((double)(VectorSymbolData.VSS_HEIGHT - vy) / VectorSymbolData.VSS_HEIGHT) * ch);
drawLine(prevPx, prevPy, px, py, color, GocaConstants.LT_SOLID, GocaConstants.LW_NORMAL);
prevPx = px;
prevPy = py;
}
}
ptr += 2 + byteLen;
} else {
ptr++;
}
}
}
/**
* Draws raw image pixel bitmap.
*/
public synchronized void drawImage(int x, int y, int width, int height, byte[] imageData, int fgColorArgb) {
if (imageData == null || width <= 0 || height <= 0) return;
int fgColor = (fgColorArgb != 0) ? fgColorArgb : 0xFFFFFFFF;
for (int row = 0; row < height; row++) {
for (int col = 0; col < width; col++) {
int bitIndex = row * width + col;
int byteIdx = bitIndex / 8;
if (byteIdx < imageData.length) {
boolean bit = ((imageData[byteIdx] >> (7 - (bitIndex % 8))) & 1) != 0;
if (bit) {
setPixel(x + col, y + row, fgColor);
}
}
}
}
hasContent = true;
}
}
@@ -0,0 +1,311 @@
package org.lib3270j.graphics;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Manages IBM 3270 Programmed Symbols (PS / APL) character sets.
* Handles the Load Programmed Symbols (LOADPS structured field 0x0F).
*/
public class ProgramSymbolManager {
private static final Logger logger = Logger.getLogger(ProgramSymbolManager.class.getName());
public static final int NUMBER_SYMBOL_SETS = 10;
public static final int NUMBER_SINGLE_PLANE_PS_SETS = 2; // RWS 2..3 (Sets 0..1)
public static final int NUMBER_TRIPLE_PLANE_PS_SETS = 4; // RWS 4..7 (Sets 2..5)
public static final int NUMBER_GRAPHICS_SYMBOL_SETS = 4; // RWS 8..11 (Sets 6..9)
private int defaultCellWidth = 9;
private int defaultCellHeight = 12; // Standard IBM 3279 PS Slot Default Height (SDH = 0x0C = 12)
public void setDefaultCellDimensions(int width, int height) {
this.defaultCellWidth = (width > 0) ? width : 9;
this.defaultCellHeight = (height > 0) ? height : 12;
}
public int getDefaultCellWidth() {
return defaultCellWidth;
}
public int getDefaultCellHeight() {
return defaultCellHeight;
}
private final ProgramSymbolSet[] sets = new ProgramSymbolSet[NUMBER_SYMBOL_SETS];
private final ProgramSymbolSet[] stagingSets = new ProgramSymbolSet[NUMBER_SYMBOL_SETS];
private final ProgramSymbolSet[] lcidMap = new ProgramSymbolSet[256];
public ProgramSymbolManager() {
for (int i = 0; i < NUMBER_SYMBOL_SETS; i++) {
boolean isTriple = (i >= NUMBER_SINGLE_PLANE_PS_SETS && i < NUMBER_SINGLE_PLANE_PS_SETS + NUMBER_TRIPLE_PLANE_PS_SETS);
sets[i] = new ProgramSymbolSet(isTriple);
stagingSets[i] = new ProgramSymbolSet(isTriple);
}
}
/**
* Resets all symbol sets.
*/
public synchronized void clearAll() {
Arrays.fill(lcidMap, null);
for (int i = 0; i < NUMBER_SYMBOL_SETS; i++) {
sets[i].clear();
sets[i].setLcid(0);
stagingSets[i].clear();
stagingSets[i].setLcid(0);
}
}
/**
* Commits all staged multi-plane symbol sets to the active presentation sets atomically.
*/
public synchronized void commitStagedSymbols() {
for (int i = 0; i < NUMBER_SYMBOL_SETS; i++) {
int lcid = stagingSets[i].getLcid();
if (lcid > 0) {
ProgramSymbolSet staged = stagingSets[i];
ProgramSymbolSet active = sets[i];
active.setLcid(lcid);
for (int slot = 0; slot < ProgramSymbolSet.NUM_SLOTS; slot++) {
active.setSlot(slot, staged.getSlot(slot));
}
if (lcid < 256) {
lcidMap[lcid] = active;
}
}
}
}
/**
* Retrieves the symbol set corresponding to a given LCID (0x40 - 0xFE).
*/
public ProgramSymbolSet getSymbolSet(int lcid) {
if (lcid <= 0 || lcid >= 256) {
return null;
}
ProgramSymbolSet set = lcidMap[lcid];
if (set == null) {
for (ProgramSymbolSet s : stagingSets) {
if (s.getLcid() == lcid) return s;
}
}
return set;
}
/**
* Retrieves a specific symbol glyph by LCID and character code point (0x40 - 0xFE).
*/
public ProgramSymbolSet.SymbolSlot getSymbol(int lcid, int codePoint) {
if (lcid <= 0 || lcid >= 256) {
return null;
}
ProgramSymbolSet set = lcidMap[lcid];
if (set == null) {
for (ProgramSymbolSet s : stagingSets) {
if (s.getLcid() == lcid) {
set = s;
break;
}
}
}
if (set == null) {
return null;
}
int index = (codePoint >= 0x40) ? (codePoint - 0x40) : codePoint;
return set.getSlot(index);
}
/**
* Processes a Load Programmed Symbols (LOADPS structured field 0x0F) payload.
*/
public synchronized void loadps(byte[] data) {
if (data == null || data.length < 4) {
logger.warning("LOADPS: Payload too short (" + (data == null ? 0 : data.length) + " bytes)");
return;
}
int flags = data[0] & 0xFF;
int loadFormat = flags & 0x1F;
boolean clearAll = (flags & 0x40) != 0;
boolean hasExtHeader = (flags & 0x80) != 0;
int lcid = data[1] & 0xFF;
int startCodePoint = data[2] & 0xFF;
int rws = data[3] & 0xFF;
int setIndex;
switch (rws) {
case 2: setIndex = 0; break;
case 3: setIndex = 1; break;
case 4: setIndex = 2; break;
case 5: setIndex = 3; break;
case 6: setIndex = 4; break;
case 7: setIndex = 5; break;
case 8: setIndex = 6; break;
case 9: setIndex = 7; break;
case 10: setIndex = 8; break;
case 11: setIndex = 9; break;
default:
logger.warning("LOADPS: Invalid RWS slot 0x" + Integer.toHexString(rws));
return;
}
boolean isTriplePlane = (rws >= 4 && rws <= 7);
ProgramSymbolSet set = isTriplePlane ? stagingSets[setIndex] : sets[setIndex];
int extHeaderLen = 0;
int cellWidth = defaultCellWidth;
int cellHeight = defaultCellHeight;
int colorPlane = 0; // 0 = all planes, 1 = Red, 2 = Green, 4 = Blue
if (hasExtHeader && data.length > 4) {
extHeaderLen = data[4] & 0xFF;
if (extHeaderLen > 3 && data.length > 6) {
int lw = data[6] & 0xFF;
if (lw > 0) cellWidth = lw;
}
if (extHeaderLen > 4 && data.length > 7) {
int lh = data[7] & 0xFF;
if (lh > 0) cellHeight = lh;
}
if (extHeaderLen >= 6 && data.length > 9) {
colorPlane = data[9] & 0xFF;
}
}
set.setLcid(lcid);
if (!isTriplePlane && lcid > 0 && lcid < 256) {
lcidMap[lcid] = set;
}
int offset = 4 + (hasExtHeader ? extHeaderLen : 0);
int remaining = data.length - offset;
int codeIndex = (startCodePoint >= 0x40) ? (startCodePoint - 0x40) : startCodePoint;
int bytesPerSymbol;
if (loadFormat == 1) {
bytesPerSymbol = 18; // 9x16 cell in standard transmission format
} else {
bytesPerSymbol = (cellWidth * cellHeight + 7) / 8;
}
if (bytesPerSymbol <= 0) {
bytesPerSymbol = 18;
}
while (remaining >= bytesPerSymbol && codeIndex < ProgramSymbolSet.NUM_SLOTS) {
byte[] pixelData = new byte[cellWidth * cellHeight];
ProgramSymbolSet.SymbolSlot existing = set.getSlot(codeIndex);
if (!clearAll && existing != null && existing.getWidth() == cellWidth && existing.getHeight() == cellHeight) {
System.arraycopy(existing.getPixelData(), 0, pixelData, 0, Math.min(existing.getPixelData().length, pixelData.length));
}
if (loadFormat == 1) {
unpackFormat1(data, offset, pixelData, cellWidth, cellHeight, isTriplePlane, colorPlane);
} else {
unpackFormat3(data, offset, pixelData, cellWidth, cellHeight, isTriplePlane, colorPlane);
}
set.setSlot(codeIndex, new ProgramSymbolSet.SymbolSlot(cellWidth, cellHeight, pixelData, isTriplePlane));
codeIndex++;
offset += bytesPerSymbol;
remaining -= bytesPerSymbol;
}
if (clearAll) {
for (int i = codeIndex; i < ProgramSymbolSet.NUM_SLOTS; i++) {
set.clearSlot(i);
}
}
if (logger.isLoggable(Level.FINE)) {
logger.fine(String.format("LOADPS: Loaded PS Set LCID=0x%02X (RWS=%d, %s, %dx%d, %d glyphs)",
lcid, rws, isTriplePlane ? "Triple-Plane" : "Single-Plane",
cellWidth, cellHeight, codeIndex - ((startCodePoint >= 0x40) ? (startCodePoint - 0x40) : startCodePoint)));
}
}
/**
* Unpacks Format 1 (9x16) symbol slice bit-pattern.
*/
private void unpackFormat1(byte[] src, int srcOff, byte[] dst, int width, int height,
boolean isTriplePlane, int colorPlane) {
// Format 1 transmits 18 bytes:
// Byte 0-1: contains column 0 for each of the 16 rows
// Bytes 2-17: contains columns 1-8 for each of the 16 rows
int planeMask = (colorPlane != 0) ? colorPlane : (isTriplePlane ? 7 : 1);
for (int row = 0; row < 16 && row < height; row++) {
// Column 0 bit from byte 0 or byte 1
int b0 = (row < 8) ? (src[srcOff] & 0xFF) : (src[srcOff + 1] & 0xFF);
int bitShift0 = 7 - (row % 8);
boolean bit0 = ((b0 >> bitShift0) & 1) != 0;
int dstIdx0 = row * width;
if (dstIdx0 < dst.length) {
if (!isTriplePlane) {
dst[dstIdx0] = bit0 ? (byte) 1 : 0;
} else if (colorPlane == 0) {
dst[dstIdx0] = bit0 ? (byte) 7 : 0;
} else {
if (bit0) {
dst[dstIdx0] |= (byte) colorPlane;
} else {
dst[dstIdx0] &= (byte) ~colorPlane;
}
}
}
// Columns 1..8 from bytes 2..17
if (srcOff + 2 + row < src.length) {
int rowByte = src[srcOff + 2 + row] & 0xFF;
for (int col = 1; col < 9 && col < width; col++) {
int dstIdx = row * width + col;
if (dstIdx < dst.length) {
boolean bit = ((rowByte >> (8 - col)) & 1) != 0;
if (!isTriplePlane) {
dst[dstIdx] = bit ? (byte) 1 : 0;
} else if (colorPlane == 0) {
dst[dstIdx] = bit ? (byte) 7 : 0;
} else {
if (bit) {
dst[dstIdx] |= (byte) colorPlane;
} else {
dst[dstIdx] &= (byte) ~colorPlane;
}
}
}
}
}
}
}
/**
* Unpacks Format 3 variable dimension bit-pattern.
*/
private void unpackFormat3(byte[] src, int srcOff, byte[] dst, int width, int height,
boolean isTriplePlane, int colorPlane) {
int totalPixels = width * height;
for (int i = 0; i < totalPixels; i++) {
int byteIndex = srcOff + (i / 8);
if (byteIndex >= src.length) break;
int bitIndex = 7 - (i % 8);
boolean bit = ((src[byteIndex] >> bitIndex) & 1) != 0;
if (!isTriplePlane) {
dst[i] = bit ? (byte) 1 : 0;
} else if (colorPlane == 0) {
dst[i] = bit ? (byte) 7 : 0;
} else {
if (bit) {
dst[i] |= (byte) colorPlane;
} else {
dst[i] &= (byte) ~colorPlane;
}
}
}
}
}
@@ -0,0 +1,189 @@
package org.lib3270j.graphics;
/**
* Represents a set of up to 191 custom bitmapped symbols (LCID 0x40 - 0xFE).
* Supports both Single-Plane (monochrome) and Triple-Plane (7-color RGB composite).
*/
public class ProgramSymbolSet {
public static final int NUM_SLOTS = 191; // Code points 0x40 - 0xFE (0..190)
private int lcid = 0;
private final boolean isTriplePlane;
private final SymbolSlot[] slots = new SymbolSlot[NUM_SLOTS];
public ProgramSymbolSet(boolean isTriplePlane) {
this.isTriplePlane = isTriplePlane;
}
public int getLcid() {
return lcid;
}
public void setLcid(int lcid) {
this.lcid = lcid;
}
public boolean isTriplePlane() {
return isTriplePlane;
}
public void clear() {
for (int i = 0; i < NUM_SLOTS; i++) {
slots[i] = null;
}
}
public void clearSlot(int index) {
if (index >= 0 && index < NUM_SLOTS) {
slots[index] = null;
}
}
public void setSlot(int index, SymbolSlot slot) {
if (index >= 0 && index < NUM_SLOTS) {
slots[index] = slot;
}
}
public SymbolSlot getSlot(int index) {
if (index >= 0 && index < NUM_SLOTS) {
return slots[index];
}
return null;
}
/**
* Represents a single custom symbol bitmap.
*/
public static class SymbolSlot {
private final int width;
private final int height;
private final byte[] pixelData; // 1 byte per pixel: 0 = background, 1..7 = color index (or 1 for monochrome)
private final boolean isTriplePlane;
private int[] cachedRgbArray;
private int cachedFgRgb = -1;
private int cachedBgRgb = -1;
private java.awt.image.BufferedImage cachedImage;
private java.awt.image.BufferedImage cachedScaledImage;
private int cachedTargetW = 0;
private int cachedTargetH = 0;
private int cachedScaledFgRgb = -1;
private int cachedScaledBgRgb = -1;
public SymbolSlot(int width, int height, byte[] pixelData, boolean isTriplePlane) {
this.width = width > 0 ? width : 9;
this.height = height > 0 ? height : 16;
this.pixelData = pixelData;
this.isTriplePlane = isTriplePlane;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public byte[] getPixelData() {
return pixelData;
}
public boolean isTriplePlane() {
return isTriplePlane;
}
/**
* Returns an image scaled directly to target dimensions (cellWidth x cellHeight).
* Enables unscaled 1:1 hardware blitting in Java2D.
*/
public synchronized java.awt.image.BufferedImage getScaledImage(int targetW, int targetH, int fgArgb, int bgArgb) {
if (targetW <= 0 || targetH <= 0) {
return getImage(fgArgb, bgArgb);
}
if (targetW == width && targetH == height) {
return getImage(fgArgb, bgArgb);
}
if (cachedScaledImage != null && cachedTargetW == targetW && cachedTargetH == targetH
&& cachedScaledFgRgb == fgArgb && cachedScaledBgRgb == bgArgb) {
return cachedScaledImage;
}
int[] srcRgb = getRgbPixels(fgArgb, bgArgb);
java.awt.image.BufferedImage scaled = new java.awt.image.BufferedImage(targetW, targetH, java.awt.image.BufferedImage.TYPE_INT_ARGB);
int[] dstRgb = ((java.awt.image.DataBufferInt) scaled.getRaster().getDataBuffer()).getData();
for (int dy = 0; dy < targetH; dy++) {
int sy = dy * height / targetH;
int srcRowOffset = sy * width;
int dstRowOffset = dy * targetW;
for (int dx = 0; dx < targetW; dx++) {
int sx = dx * width / targetW;
dstRgb[dstRowOffset + dx] = srcRgb[srcRowOffset + sx];
}
}
this.cachedScaledImage = scaled;
this.cachedTargetW = targetW;
this.cachedTargetH = targetH;
this.cachedScaledFgRgb = fgArgb;
this.cachedScaledBgRgb = bgArgb;
return scaled;
}
/**
* Computes and returns the cached BufferedImage for this symbol glyph.
* Eliminates per-cell heap allocations during high frame rate rendering.
*/
public synchronized java.awt.image.BufferedImage getImage(int fgArgb, int bgArgb) {
if (cachedImage != null && cachedFgRgb == fgArgb && cachedBgRgb == bgArgb) {
return cachedImage;
}
int[] rgb = getRgbPixels(fgArgb, bgArgb);
java.awt.image.BufferedImage img = new java.awt.image.BufferedImage(width, height, java.awt.image.BufferedImage.TYPE_INT_ARGB);
int[] imgData = ((java.awt.image.DataBufferInt) img.getRaster().getDataBuffer()).getData();
System.arraycopy(rgb, 0, imgData, 0, rgb.length);
this.cachedImage = img;
this.cachedFgRgb = fgArgb;
this.cachedBgRgb = bgArgb;
return img;
}
/**
* Computes and returns the 32-bit ARGB pixel array for this symbol.
* The returned array has length (width * height).
*/
public synchronized int[] getRgbPixels(int fgArgb, int bgArgb) {
if (cachedRgbArray != null && cachedFgRgb == fgArgb && cachedBgRgb == bgArgb) {
return cachedRgbArray;
}
int[] rgbArray = new int[width * height];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int idx = y * width + x;
int val = (pixelData != null && idx < pixelData.length) ? (pixelData[idx] & 0xFF) : 0;
if (val == 0) {
rgbArray[idx] = bgArgb;
} else if (!isTriplePlane) {
rgbArray[idx] = fgArgb;
} else {
// Triple-Plane RGB composite:
// val is bitmask: bit 0 (0x01) = Red, bit 1 (0x02) = Green, bit 2 (0x04) = Blue
int r = (val & 0x01) != 0 ? 255 : 0;
int g = (val & 0x02) != 0 ? 255 : 0;
int b = (val & 0x04) != 0 ? 255 : 0;
rgbArray[idx] = (0xFF << 24) | (r << 16) | (g << 8) | b;
}
}
}
this.cachedRgbArray = rgbArray;
this.cachedFgRgb = fgArgb;
this.cachedBgRgb = bgArgb;
return rgbArray;
}
}
}
File diff suppressed because one or more lines are too long
@@ -31,6 +31,12 @@ public class InputProcessor {
this.fsm = fsm; this.fsm = fsm;
} }
private org.lib3270j.graphics.GraphicsPlane graphicsPlane;
public void setGraphicsPlane(org.lib3270j.graphics.GraphicsPlane gp) {
this.graphicsPlane = gp;
}
public enum OiaStatus { public enum OiaStatus {
NOT_CONNECTED("OFFLINE"), NOT_CONNECTED("OFFLINE"),
X_SYSTEM("X SYSTEM"), X_SYSTEM("X SYSTEM"),
@@ -160,6 +166,9 @@ public class InputProcessor {
if (aidCode == AID_CLEAR) { if (aidCode == AID_CLEAR) {
screen.clear(); screen.clear();
screen.markAllChanged(); screen.markAllChanged();
if (graphicsPlane != null) {
graphicsPlane.clear();
}
// Send just the AID // Send just the AID
byte[] data = new byte[] { (byte) aidCode }; byte[] data = new byte[] { (byte) aidCode };
sendAidResponse(data); sendAidResponse(data);
@@ -202,16 +202,25 @@ public final class DS3270Constants {
public static final int QR_SUMMARY = 0x80; public static final int QR_SUMMARY = 0x80;
public static final int QR_USABLE_AREA = 0x81; public static final int QR_USABLE_AREA = 0x81;
public static final int QR_IMAGE = 0x82; public static final int QR_IMAGE = 0x82;
public static final int QR_TEXT_PART = 0x83; public static final int QR_TEXT_PART = 0x83;
public static final int QR_ALPHA_PART = 0x84; public static final int QR_ALPHA_PART = 0x84;
public static final int QR_CHARSETS = 0x85; public static final int QR_CHARSETS = 0x85;
public static final int QR_COLOR = 0x86; public static final int QR_COLOR = 0x86;
public static final int QR_HIGHLIGHTING = 0x87; public static final int QR_HIGHLIGHTING = 0x87;
public static final int QR_REPLY_MODES = 0x88; public static final int QR_REPLY_MODES = 0x88;
public static final int QR_SAVE_RESTORE = 0x8c;
public static final int QR_DBCS_ASIA = 0x91; public static final int QR_DBCS_ASIA = 0x91;
public static final int QR_DDM = 0x95; public static final int QR_DDM = 0x95;
public static final int QR_TRANSPARENCY = 0x99;
public static final int QR_RPQNAMES = 0xa1; public static final int QR_RPQNAMES = 0xa1;
public static final int QR_IMP_PART = 0xa6; public static final int QR_IMP_PART = 0xa6;
public static final int QR_RPQ_NAMES = 0xa8;
public static final int QR_GRAPHICS = 0xb0; // 3270-PC Vector Graphics
public static final int QR_GIMAGE = 0xb1; // Image / Graphics Planes
public static final int QR_AUX_DEV = 0xb2; // Auxiliary Device
public static final int QR_OEM_FMT = 0xb3; // OEM Format
public static final int QR_GCOLOR = 0xb4; // Graphic Color Table
public static final int QR_GSYMBOLS = 0xb6; // Graphic Symbol Sets
public static final int QR_NULL = 0xff; public static final int QR_NULL = 0xff;
// ========== Screen model sizes ========== // ========== Screen model sizes ==========
@@ -36,8 +36,12 @@ public class ScreenBuffer {
private byte defaultGr = 0x00; private byte defaultGr = 0x00;
private byte defaultCs = 0x00; private byte defaultCs = 0x00;
private byte defaultIc = 0x00; private byte defaultIc = 0x00;
private final EbcdicTranslator translator; private final EbcdicTranslator translator;
private final Object renderLock = new Object();
public Object getRenderLock() {
return renderLock;
}
public ScreenBuffer(TerminalModel model, EbcdicTranslator translator) { public ScreenBuffer(TerminalModel model, EbcdicTranslator translator) {
this.translator = translator; this.translator = translator;
@@ -79,6 +83,50 @@ public class ScreenBuffer {
return buffer[addr]; return buffer[addr];
} }
private ExtendedAttribute[] displaySnapshot;
private int displayRows;
private int displayCols;
private int displayCursorAddress;
/**
* Atomically creates a snapshot of the current presentation buffer for tear-free rendering.
* Takes ~2 microseconds and eliminates mutual thread contention with the UI thread.
*/
public synchronized void updateDisplaySnapshot() {
int size = rows * cols;
if (displaySnapshot == null || displaySnapshot.length < size) {
displaySnapshot = new ExtendedAttribute[size];
for (int i = 0; i < size; i++) {
displaySnapshot[i] = new ExtendedAttribute();
}
}
for (int i = 0; i < size; i++) {
displaySnapshot[i].copyFrom(buffer[i]);
}
this.displayRows = rows;
this.displayCols = cols;
this.displayCursorAddress = cursorAddress;
}
public synchronized ExtendedAttribute getDisplayCell(int addr) {
if (displaySnapshot == null || addr < 0 || addr >= displayRows * displayCols) {
return getCell(addr);
}
return displaySnapshot[addr];
}
public synchronized int getDisplayRows() {
return displayRows > 0 ? displayRows : rows;
}
public synchronized int getDisplayCols() {
return displayCols > 0 ? displayCols : cols;
}
public synchronized int getDisplayCursorAddress() {
return displayRows > 0 ? displayCursorAddress : cursorAddress;
}
// ========== Dimension accessors ========== // ========== Dimension accessors ==========
public int getRows() { return rows; } public int getRows() { return rows; }
public int getCols() { return cols; } public int getCols() { return cols; }
@@ -28,26 +28,42 @@ public class TelnetConnection {
private final TelnetFSM fsm; private final TelnetFSM fsm;
private final ConnectionConfig config; private final ConnectionConfig config;
private javax.net.ssl.SSLSession sslSession;
public TelnetConnection(ConnectionConfig config, TelnetFSM fsm) { public TelnetConnection(ConnectionConfig config, TelnetFSM fsm) {
this.config = config; this.config = config;
this.fsm = fsm; this.fsm = fsm;
} }
public javax.net.ssl.SSLSession getSslSession() {
return sslSession;
}
/** /**
* Connect to the host. Blocks until connection is established or fails. * Connect to the host. Blocks until connection is established or fails.
*/ */
public void connect() throws IOException { public void connect() throws IOException {
if (config.isUseTls()) { if (config.isUseTls()) {
log.info("Connecting with TLS to " + config.getHost() + ":" + config.getPort()); log.info("Connecting with TLS to " + config.getHost() + ":" + config.getPort() +
javax.net.ssl.SSLSocketFactory ssf = (javax.net.ssl.SSLSocketFactory) javax.net.ssl.SSLSocketFactory.getDefault(); " (verifyCert=" + config.isTlsVerifyCert() + ")");
javax.net.ssl.SSLSocket sslSocket = (javax.net.ssl.SSLSocket) ssf.createSocket(); try {
sslSocket.setKeepAlive(true); javax.net.ssl.SSLContext sslContext = org.lib3270j.tls.TlsTrustManager.createSSLContext(config);
sslSocket.setOOBInline(true); javax.net.ssl.SSLSocketFactory ssf = sslContext.getSocketFactory();
sslSocket.setTcpNoDelay(true); javax.net.ssl.SSLSocket sslSocket = (javax.net.ssl.SSLSocket) ssf.createSocket();
sslSocket.connect(new InetSocketAddress(config.getHost(), config.getPort()), sslSocket.setKeepAlive(true);
config.getConnectTimeoutMs()); sslSocket.setTcpNoDelay(true);
sslSocket.startHandshake(); sslSocket.connect(new InetSocketAddress(config.getHost(), config.getPort()),
socket = sslSocket; config.getConnectTimeoutMs());
sslSocket.startHandshake();
socket = sslSocket;
sslSession = sslSocket.getSession();
log.info("TLS session active: protocol=" + sslSession.getProtocol() +
" cipher=" + sslSession.getCipherSuite());
} catch (IOException e) {
throw e;
} catch (Exception e) {
throw new IOException("TLS setup failure: " + e.getMessage(), e);
}
} else { } else {
log.info("Connecting to " + config.getHost() + ":" + config.getPort()); log.info("Connecting to " + config.getHost() + ":" + config.getPort());
socket = new Socket(); socket = new Socket();
@@ -133,9 +149,7 @@ public class TelnetConnection {
log.fine("RCVD " + n + " bytes: " + formatHex(buf, 0, n)); log.fine("RCVD " + n + " bytes: " + formatHex(buf, 0, n));
} }
try { try {
for (int i = 0; i < n; i++) { fsm.feedBytes(buf, 0, n);
fsm.feedByte(buf[i] & 0xFF);
}
fsm.endOfNetworkData(); fsm.endOfNetworkData();
} catch (Throwable t) { } catch (Throwable t) {
log.log(Level.SEVERE, "Exception processing incoming data stream", t); log.log(Level.SEVERE, "Exception processing incoming data stream", t);
@@ -58,6 +58,7 @@ public class TelnetFSM {
private int eXmitSeq; private int eXmitSeq;
private int responseRequired = RSF_NO_RESPONSE; private int responseRequired = RSF_NO_RESPONSE;
private boolean deferredWillTtype; private boolean deferredWillTtype;
private boolean tn3270eDeviceTypeSent;
// Connection references // Connection references
private TelnetConnection connection; private TelnetConnection connection;
@@ -103,6 +104,7 @@ public class TelnetFSM {
java.util.Arrays.fill(hisOpts, false); java.util.Arrays.fill(hisOpts, false);
java.util.Arrays.fill(eFuncs, false); java.util.Arrays.fill(eFuncs, false);
tn3270eNegotiated = false; tn3270eNegotiated = false;
tn3270eDeviceTypeSent = false;
tn3270eSubmode = TN3270ESubmode.UNBOUND; tn3270eSubmode = TN3270ESubmode.UNBOUND;
tn3270eBound = false; tn3270eBound = false;
eXmitSeq = 0; eXmitSeq = 0;
@@ -118,6 +120,37 @@ public class TelnetFSM {
changeState(ConnectionState.TELNET_PENDING); changeState(ConnectionState.TELNET_PENDING);
} }
/**
* Feed a bulk buffer of bytes from the network into the FSM.
*/
public void feedBytes(byte[] buf, int offset, int len) {
int end = offset + len;
int i = offset;
while (i < end) {
if (state == TNS_DATA) {
int start = i;
while (i < end && (buf[i] & 0xFF) != IAC) {
i++;
}
if (i > start) {
if (connectionState == ConnectionState.TELNET_PENDING) {
changeState(ConnectionState.CONNECTED_NVT);
}
if (connectionState.is3270() || connectionState.isTn3270e() || connectionState == ConnectionState.TELNET_PENDING) {
ibuf.write(buf, start, i - start);
}
}
if (i < end) {
state = TNS_IAC;
i++;
}
} else {
feedByte(buf[i] & 0xFF);
i++;
}
}
}
/** /**
* Feed a single byte from the network into the FSM. * Feed a single byte from the network into the FSM.
*/ */
@@ -172,8 +205,8 @@ public class TelnetFSM {
changeState(ConnectionState.CONNECTED_NVT); changeState(ConnectionState.CONNECTED_NVT);
} }
// Accumulate data for 3270, TN3270E (including SSCP-LU and unbound states) // Accumulate data for 3270, TN3270E (including SSCP-LU and unbound states, and pending)
if (connectionState.is3270() || connectionState.isTn3270e()) { if (connectionState.is3270() || connectionState.isTn3270e() || connectionState == ConnectionState.TELNET_PENDING) {
ibuf.write(c); ibuf.write(c);
} }
// NVT data would go to NVT processor (not implemented in initial version) // NVT data would go to NVT processor (not implemented in initial version)
@@ -189,7 +222,7 @@ public class TelnetFSM {
break; break;
case EOR: // End of record process accumulated 3270 data case EOR: // End of record process accumulated 3270 data
log.fine("RCVD EOR"); log.fine("RCVD EOR");
if (connectionState.is3270() || connectionState.isTn3270e()) { if (connectionState.is3270() || connectionState.isTn3270e() || connectionState == ConnectionState.TELNET_PENDING) {
processEndOfRecord(); processEndOfRecord();
} }
ibuf.reset(); ibuf.reset();
@@ -235,7 +268,9 @@ public class TelnetFSM {
break; break;
case TELOPT_TN3270E: case TELOPT_TN3270E:
if (!hisOpts[opt]) { if (!config.isTn3270eEnabled()) {
sendCommand(DONT, opt);
} else if (!hisOpts[opt]) {
hisOpts[opt] = true; hisOpts[opt] = true;
sendCommand(DO, opt); sendCommand(DO, opt);
} }
@@ -278,7 +313,7 @@ public class TelnetFSM {
case TELOPT_TTYPE: case TELOPT_TTYPE:
if (!myOpts[opt]) { if (!myOpts[opt]) {
myOpts[opt] = true; myOpts[opt] = true;
if (hisOpts[TELOPT_TN3270E]) { if (config.isTn3270eEnabled() && hisOpts[TELOPT_TN3270E]) {
// Defer TTYPE response until TN3270E negotiation completes // Defer TTYPE response until TN3270E negotiation completes
deferredWillTtype = true; deferredWillTtype = true;
} else { } else {
@@ -288,11 +323,16 @@ public class TelnetFSM {
break; break;
case TELOPT_TN3270E: case TELOPT_TN3270E:
if (!myOpts[opt]) { if (!config.isTn3270eEnabled()) {
sendCommand(WONT, opt);
} else if (!myOpts[opt]) {
myOpts[opt] = true; myOpts[opt] = true;
sendCommand(WILL, opt); sendCommand(WILL, opt);
// Start TN3270E sub-negotiation: send device type request // Start TN3270E sub-negotiation: send device type request
sendTN3270EDeviceTypeRequest(); if (!tn3270eDeviceTypeSent) {
sendTN3270EDeviceTypeRequest();
tn3270eDeviceTypeSent = true;
}
} }
break; break;
@@ -401,7 +441,7 @@ public class TelnetFSM {
out.write(IAC); out.write(IAC);
out.write(SE); out.write(SE);
sendBytes(out.toByteArray()); sendBytes(out.toByteArray());
log.info("SENT SB TTYPE IS " + termType + " SE"); log.warning(">>> SENT SB TTYPE IS " + termType + " SE");
} }
} }
@@ -428,7 +468,10 @@ public class TelnetFSM {
switch (op) { switch (op) {
case OP_SEND: case OP_SEND:
// Host asks us to send device-type request // Host asks us to send device-type request
sendTN3270EDeviceTypeRequest(); if (!tn3270eDeviceTypeSent) {
sendTN3270EDeviceTypeRequest();
tn3270eDeviceTypeSent = true;
}
break; break;
case OP_DEVICE_TYPE: case OP_DEVICE_TYPE:
@@ -475,7 +518,7 @@ public class TelnetFSM {
out.write(IAC); out.write(IAC);
out.write(SE); out.write(SE);
sendBytes(out.toByteArray()); sendBytes(out.toByteArray());
log.info("SENT SB TN3270E DEVICE-TYPE REQUEST " + termType + log.warning(">>> SENT SB TN3270E DEVICE-TYPE REQUEST " + termType +
(config.getLuName() != null ? " CONNECT " + config.getLuName() : "") + " SE"); (config.getLuName() != null ? " CONNECT " + config.getLuName() : "") + " SE");
} }
@@ -488,14 +531,19 @@ public class TelnetFSM {
pos++; pos++;
} }
// Check if REJECT
if (pos < data.length && (data[pos] & 0xFF) == OP_REJECT) { if (pos < data.length && (data[pos] & 0xFF) == OP_REJECT) {
// Rejection
pos++; pos++;
int reason = -1; int reason = (pos < data.length) ? (data[pos] & 0xFF) : REASON_UNSUPPORTED_REQ;
if (pos < data.length && (data[pos] & 0xFF) == OP_REASON) { if (reason == REASON_INV_DEVICE_TYPE || reason == REASON_UNSUPPORTED_REQ) {
pos++; // Try fallback model 2 if we were requesting something else
if (pos < data.length) { if (config.getModel() != TerminalModel.IBM_3278_2 &&
reason = data[pos] & 0xFF; config.getModel() != TerminalModel.IBM_3279_2) {
log.warning("TN3270E device-type rejected (" +
TN3270EConstants.reasonName(reason) + "), retrying with IBM-3278-2");
config.setModel(TerminalModel.IBM_3278_2);
sendTN3270EDeviceTypeRequest();
return;
} }
} }
log.warning("TN3270E device-type rejected: " + TN3270EConstants.reasonName(reason)); log.warning("TN3270E device-type rejected: " + TN3270EConstants.reasonName(reason));
@@ -563,33 +611,86 @@ public class TelnetFSM {
log.info("SENT SB TN3270E FUNCTIONS REQUEST " + funcNames + " SE"); log.info("SENT SB TN3270E FUNCTIONS REQUEST " + funcNames + " SE");
} }
private void handleTN3270EFunctions(byte[] data) { private void sendTN3270EFunctionsIs() {
// Parse: TN3270E FUNCTIONS IS [func...] ByteArrayOutputStream out = new ByteArrayOutputStream();
int pos = 2; // Skip TN3270E, FUNCTIONS out.write(IAC);
out.write(SB);
out.write(TELOPT_TN3270E);
out.write(OP_FUNCTIONS);
out.write(OP_IS);
if (pos < data.length && (data[pos] & 0xFF) == OP_IS) { StringBuilder funcNames = new StringBuilder();
pos++; // Skip IS for (int i = 0; i < eFuncs.length; i++) {
if (eFuncs[i]) {
out.write(i);
if (funcNames.length() > 0) funcNames.append(" ");
funcNames.append(TN3270EConstants.functionName(i));
}
} }
// The remaining bytes are the agreed-upon functions out.write(IAC);
java.util.Arrays.fill(eFuncs, false); out.write(SE);
StringBuilder funcNames = new StringBuilder(); sendBytes(out.toByteArray());
log.info("SENT SB TN3270E FUNCTIONS IS " + funcNames + " SE");
}
private void handleTN3270EFunctions(byte[] data) {
// Parse: TN3270E FUNCTIONS REQUEST [func...] or TN3270E FUNCTIONS IS [func...]
int pos = 1;
boolean isRequest = false;
while (pos < data.length) {
int b = data[pos] & 0xFF;
if (b == OP_FUNCTIONS) {
pos++;
} else if (b == OP_REQUEST) {
isRequest = true;
pos++;
break;
} else if (b == OP_IS) {
isRequest = false;
pos++;
break;
} else {
pos++;
}
}
// The remaining bytes are the proposed/agreed functions
boolean[] hostFuncs = new boolean[8];
while (pos < data.length) { while (pos < data.length) {
int func = data[pos] & 0xFF; int func = data[pos] & 0xFF;
if (func <= FUNC_SNA_SENSE) { if (func <= FUNC_SNA_SENSE) {
eFuncs[func] = true; hostFuncs[func] = true;
if (funcNames.length() > 0) funcNames.append(" ");
funcNames.append(TN3270EConstants.functionName(func));
} }
pos++; pos++;
} }
if (isRequest) {
// Host sent FUNCTIONS REQUEST -> We reply with FUNCTIONS IS (intersection of functions)
for (int i = 0; i < eFuncs.length; i++) {
eFuncs[i] = eFuncs[i] && hostFuncs[i];
}
sendTN3270EFunctionsIs();
} else {
// Host sent FUNCTIONS IS -> Accept host's agreed function list
for (int i = 0; i < eFuncs.length; i++) {
eFuncs[i] = hostFuncs[i];
}
}
tn3270eNegotiated = true; tn3270eNegotiated = true;
log.info("TN3270E functions IS: " + funcNames); log.info("TN3270E functions negotiated: " + getNegotiatedFunctionNames());
log.info("TN3270E negotiation complete"); log.info("TN3270E negotiation complete");
// Move to CONNECTED_UNBOUND or CONNECTED_SSCP // RFC 2355: If BIND-IMAGE function is not negotiated, the emulation session is considered
changeState(ConnectionState.CONNECTED_UNBOUND); // to be bound immediately upon completion of the FUNCTIONS negotiation.
if (eFuncs[FUNC_BIND_IMAGE]) {
changeState(ConnectionState.CONNECTED_UNBOUND);
} else {
changeState(ConnectionState.CONNECTED_TN3270E);
tn3270eSubmode = TN3270ESubmode.E_3270;
}
// Notify listeners // Notify listeners
for (ConnectionListener l : connectionListeners) { for (ConnectionListener l : connectionListeners) {
@@ -597,12 +698,29 @@ public class TelnetFSM {
} }
} }
private String getNegotiatedFunctionNames() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < eFuncs.length; i++) {
if (eFuncs[i]) {
if (sb.length() > 0) sb.append(" ");
sb.append(TN3270EConstants.functionName(i));
}
}
return sb.length() > 0 ? sb.toString() : "<none>";
}
// ========== End of Record processing ========== // ========== End of Record processing ==========
private void processEndOfRecord() { private void processEndOfRecord() {
byte[] data = ibuf.toByteArray(); byte[] data = ibuf.toByteArray();
ibuf.reset();
if (data.length == 0) return; if (data.length == 0) return;
if (connectionState == ConnectionState.TELNET_PENDING && !tn3270eNegotiated) {
log.info("Received EOR during TELNET_PENDING - transitioning to plain TN3270 mode");
changeState(ConnectionState.CONNECTED_3270);
}
if (tn3270eNegotiated) { if (tn3270eNegotiated) {
// TN3270E mode: data starts with 5-byte header // TN3270E mode: data starts with 5-byte header
processTN3270ERecord(data); processTN3270ERecord(data);
@@ -701,11 +819,11 @@ public class TelnetFSM {
bindRa = screenBuffer.getMaxRows(); bindRa = screenBuffer.getMaxRows();
bindCa = screenBuffer.getMaxCols(); bindCa = screenBuffer.getMaxCols();
break; break;
case 0x7e: case 0x7E:
// Both default and alternate = specified values // Both default and alternate = specified values
bindRa = bindRd; bindCa = bindCd; bindRa = bindRd; bindCa = bindCd;
break; break;
case 0x7f: case 0x7F:
// Default and alternate are both specified separately // Default and alternate are both specified separately
bindRa = data[EH_SIZE + BIND_OFF_RA] & 0xFF; bindRa = data[EH_SIZE + BIND_OFF_RA] & 0xFF;
bindCa = data[EH_SIZE + BIND_OFF_CA] & 0xFF; bindCa = data[EH_SIZE + BIND_OFF_CA] & 0xFF;
@@ -767,7 +885,20 @@ public class TelnetFSM {
break; break;
default: default:
log.info("Unhandled TN3270E data type: " + dataType); // Check if the host sent a raw 3270 data stream (e.g. 0xF5 EraseWrite, 0x7E EW Alternate, 0xF1 Write, etc.)
// This happens when the server ignores or drops TN3270E framing and speaks plain 3270 data stream.
if (dataType == 0xF5 || dataType == 0x7E || dataType == 0xF1 || dataType == 0x6F ||
dataType == 0x6E || dataType == 0xF2 || dataType == 0xF6 || dataType == 0x05 ||
dataType == 0x0D || dataType == 0x01 || (data.length >= 2 && (data[0] & 0xFF) == 0x11)) {
log.warning("Received plain 3270 command (0x" + Integer.toHexString(dataType) +
") in TN3270E mode — automatically switching to plain TN3270 mode");
tn3270eNegotiated = false;
changeState(ConnectionState.CONNECTED_3270);
dsProcessor.processRecord(data, 0, data.length, true);
notifyScreenUpdate();
} else {
log.info("Unhandled TN3270E data type: " + dataType);
}
break; break;
} }
} }
@@ -790,7 +921,7 @@ public class TelnetFSM {
if (connectionState != ConnectionState.TELNET_PENDING) return; if (connectionState != ConnectionState.TELNET_PENDING) return;
// For TN3270E, we wait for TN3270E negotiation to complete // For TN3270E, we wait for TN3270E negotiation to complete
if (myOpts[TELOPT_TN3270E] && hisOpts[TELOPT_TN3270E]) { if (config.isTn3270eEnabled() && myOpts[TELOPT_TN3270E] && hisOpts[TELOPT_TN3270E]) {
return; // TN3270E in progress return; // TN3270E in progress
} }
@@ -0,0 +1,22 @@
package org.lib3270j.tls;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
/**
* Callback interface for validating TLS server certificates.
* Used when standard certificate path validation fails or when custom verification is needed.
*/
@FunctionalInterface
public interface TlsCertificateVerifier {
/**
* Determine whether to trust an unverified server certificate chain.
*
* @param chain The peer certificate chain presented by the server.
* @param authType The key exchange algorithm (e.g., "RSA", "ECDHE_RSA").
* @param exception The CertificateException thrown by standard validation (or null if called proactively).
* @return true to trust the certificate and proceed with the connection; false to abort.
*/
boolean shouldTrust(X509Certificate[] chain, String authType, CertificateException exception);
}
@@ -0,0 +1,114 @@
package org.lib3270j.tls;
import org.lib3270j.ConnectionConfig;
import javax.net.ssl.*;
import java.security.KeyStore;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Custom X509TrustManager that supports:
* 1. Standard certificate verification using the JVM default TrustManager.
* 2. Unverified / trust-all mode when tlsVerifyCert is false.
* 3. Interactive/custom certificate verifier callbacks (e.g. GUI prompts for self-signed certificates).
*/
public class TlsTrustManager implements X509TrustManager {
private static final Logger log = Logger.getLogger(TlsTrustManager.class.getName());
private final ConnectionConfig config;
private X509TrustManager defaultTrustManager;
public TlsTrustManager(ConnectionConfig config) {
this.config = config;
try {
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init((KeyStore) null);
for (TrustManager tm : tmf.getTrustManagers()) {
if (tm instanceof X509TrustManager) {
this.defaultTrustManager = (X509TrustManager) tm;
break;
}
}
} catch (Exception e) {
log.log(Level.WARNING, "Failed to initialize default TrustManagerFactory", e);
}
}
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
if (defaultTrustManager != null) {
defaultTrustManager.checkClientTrusted(chain, authType);
}
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
if (config != null && !config.isTlsVerifyCert()) {
log.fine("Certificate verification bypassed (tlsVerifyCert=false)");
return;
}
if (chain == null || chain.length == 0) {
CertificateException ex = new CertificateException("null or zero-length certificate chain");
if (config != null && config.getCertificateVerifier() != null) {
if (config.getCertificateVerifier().shouldTrust(chain, authType, ex)) {
log.info("Server certificate accepted via TlsCertificateVerifier callback");
return;
}
}
throw ex;
}
try {
if (defaultTrustManager != null) {
defaultTrustManager.checkServerTrusted(chain, authType);
} else {
throw new CertificateException("No default X509TrustManager available");
}
} catch (Exception ex) {
CertificateException certEx = (ex instanceof CertificateException)
? (CertificateException) ex
: new CertificateException("Certificate validation failed: " + ex.getMessage(), ex);
log.log(Level.FINE, "Standard certificate validation failed: " + certEx.getMessage(), certEx);
if (config != null && config.getCertificateVerifier() != null) {
boolean accepted = config.getCertificateVerifier().shouldTrust(chain, authType, certEx);
if (accepted) {
log.info("Server certificate accepted via TlsCertificateVerifier callback");
return;
} else {
log.warning("Server certificate rejected by TlsCertificateVerifier callback");
throw new CertificateException("Certificate rejected by user/verifier: " + certEx.getMessage(), certEx);
}
}
throw certEx;
}
}
@Override
public X509Certificate[] getAcceptedIssuers() {
if (defaultTrustManager != null) {
return defaultTrustManager.getAcceptedIssuers();
}
return new X509Certificate[0];
}
/**
* Create an initialized SSLContext configured for the given ConnectionConfig.
*/
public static SSLContext createSSLContext(ConnectionConfig config) throws Exception {
String protocol = (config != null && config.getSslProtocol() != null)
? config.getSslProtocol()
: "TLS";
SSLContext sslContext = SSLContext.getInstance(protocol);
TlsTrustManager trustManager = new TlsTrustManager(config);
sslContext.init(null, new TrustManager[] { trustManager }, new SecureRandom());
return sslContext;
}
}
@@ -3,6 +3,7 @@ package org.lib3270j.datastream;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.lib3270j.TerminalModel; import org.lib3270j.TerminalModel;
import org.lib3270j.charset.EbcdicTranslator; import org.lib3270j.charset.EbcdicTranslator;
import org.lib3270j.graphics.GraphicsMode;
import org.lib3270j.screen.ScreenBuffer; import org.lib3270j.screen.ScreenBuffer;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
import static org.lib3270j.protocol.DS3270Constants.*; import static org.lib3270j.protocol.DS3270Constants.*;
@@ -14,11 +15,108 @@ public class QueryReplyBuilderTest {
private final QueryReplyBuilder qrBuilder = new QueryReplyBuilder(screen); private final QueryReplyBuilder qrBuilder = new QueryReplyBuilder(screen);
@Test @Test
public void testBuildAllQueryReplies() { public void testBuildAllQueryRepliesDefaultBoth() {
assertEquals(GraphicsMode.BOTH, qrBuilder.getGraphicsMode());
byte[] replies = qrBuilder.buildAllQueryReplies(80, 43, 80 * 43); byte[] replies = qrBuilder.buildAllQueryReplies(80, 43, 80 * 43);
assertNotNull(replies); assertNotNull(replies);
assertTrue(replies.length > 0); assertTrue(replies.length > 0);
assertEquals((byte) AID_SF, replies[0]); assertEquals((byte) AID_SF, replies[0]);
// In GraphicsMode.BOTH, Vector Graphics QR 0xB0 must be present
boolean hasB0 = false;
for (int i = 0; i < replies.length - 3; i++) {
if ((replies[i] & 0xFF) == 0x81 && (replies[i + 1] & 0xFF) == QR_GRAPHICS) {
hasB0 = true;
break;
}
}
assertTrue(hasB0);
}
@Test
public void testBuildAllQueryRepliesExplicitNone() {
qrBuilder.setGraphicsMode(GraphicsMode.NONE);
byte[] replies = qrBuilder.buildAllQueryReplies(80, 43, 80 * 43);
assertNotNull(replies);
assertTrue(replies.length > 0);
assertEquals((byte) AID_SF, replies[0]);
// In GraphicsMode.NONE, Vector Graphics QR 0xB0 must NOT be present
boolean hasB0 = false;
for (int i = 0; i < replies.length - 3; i++) {
if ((replies[i] & 0xFF) == 0x81 && (replies[i + 1] & 0xFF) == QR_GRAPHICS) {
hasB0 = true;
break;
}
}
assertFalse(hasB0);
}
@Test
public void testBuildAllQueryRepliesWithVectorGraphics() {
qrBuilder.setGraphicsMode(GraphicsMode.VECTOR_GRAPHICS);
byte[] replies = qrBuilder.buildAllQueryReplies(80, 43, 80 * 43);
assertNotNull(replies);
// Vector Graphics QR 0xB0 and 0xB4 must be present
boolean hasB0 = false;
boolean hasB4 = false;
for (int i = 0; i < replies.length - 3; i++) {
if ((replies[i] & 0xFF) == 0x81 && (replies[i + 1] & 0xFF) == QR_GRAPHICS) {
hasB0 = true;
}
if ((replies[i] & 0xFF) == 0x81 && (replies[i + 1] & 0xFF) == QR_GCOLOR) {
hasB4 = true;
}
}
assertTrue(hasB0);
assertTrue(hasB4);
}
@Test
public void testBuildAllQueryRepliesWithBoth() {
qrBuilder.setGraphicsMode(GraphicsMode.BOTH);
byte[] replies = qrBuilder.buildAllQueryReplies(80, 43, 80 * 43);
assertNotNull(replies);
// Vector Graphics (0xB0) must be present and Charsets must have LoadPS (0x0A)
boolean hasB0 = false;
boolean hasCharsetsWithLoadPs = false;
for (int i = 0; i < replies.length - 3; i++) {
if ((replies[i] & 0xFF) == 0x81 && (replies[i + 1] & 0xFF) == QR_GRAPHICS) {
hasB0 = true;
}
if ((replies[i] & 0xFF) == 0x81 && (replies[i + 1] & 0xFF) == QR_CHARSETS) {
if (i + 6 < replies.length && (replies[i + 6] & 0xFF) == 0x0A) {
hasCharsetsWithLoadPs = true;
}
}
}
assertTrue(hasB0);
assertTrue(hasCharsetsWithLoadPs);
}
@Test
public void testBuildAllQueryRepliesWithProgrammedSymbols() {
qrBuilder.setGraphicsMode(GraphicsMode.PROGRAMMED_SYMBOLS);
byte[] replies = qrBuilder.buildAllQueryReplies(80, 43, 80 * 43);
assertNotNull(replies);
// Charsets with LoadPS (0x0A) must be present, and QR_GRAPHICS must NOT be present
boolean hasB0 = false;
boolean hasCharsetsWithLoadPs = false;
for (int i = 0; i < replies.length - 3; i++) {
if ((replies[i] & 0xFF) == 0x81 && (replies[i + 1] & 0xFF) == QR_GRAPHICS) {
hasB0 = true;
}
if ((replies[i] & 0xFF) == 0x81 && (replies[i + 1] & 0xFF) == QR_CHARSETS) {
if (i + 6 < replies.length && (replies[i + 6] & 0xFF) == 0x0A) {
hasCharsetsWithLoadPs = true;
}
}
}
assertFalse(hasB0);
assertTrue(hasCharsetsWithLoadPs);
} }
@Test @Test
@@ -52,4 +150,86 @@ public class QueryReplyBuilderTest {
assertEquals(0x81, replies[3] & 0xFF); assertEquals(0x81, replies[3] & 0xFF);
assertEquals(QR_NULL, replies[4] & 0xFF); assertEquals(QR_NULL, replies[4] & 0xFF);
} }
@Test
public void testImplicitPartitionModel4() {
byte[] requested = new byte[] { (byte) QR_IMP_PART };
byte[] replies = qrBuilder.buildQueryReplies(requested, 80, 43, 80 * 43);
assertNotNull(replies);
assertEquals((byte) AID_SF, replies[0]);
int len = ((replies[1] & 0xFF) << 8) | (replies[2] & 0xFF);
assertEquals(0x81, replies[3] & 0xFF); // SFID_QREPLY
assertEquals(QR_IMP_PART, replies[4] & 0xFF); // 0xA6
// Payload starts at index 5: 22 bytes total
// Default size = 80 x 24
int defCols = ((replies[10] & 0xFF) << 8) | (replies[11] & 0xFF);
int defRows = ((replies[12] & 0xFF) << 8) | (replies[13] & 0xFF);
assertEquals(80, defCols);
assertEquals(24, defRows);
// Alt size = 80 x 43
int altCols = ((replies[14] & 0xFF) << 8) | (replies[15] & 0xFF);
int altRows = ((replies[16] & 0xFF) << 8) | (replies[17] & 0xFF);
assertEquals(80, altCols);
assertEquals(43, altRows);
}
@Test
public void testProgrammedSymbolsDescriptorsSingleAndTriplePlane() {
qrBuilder.setGraphicsMode(GraphicsMode.PROGRAMMED_SYMBOLS);
byte[] requested = new byte[] { (byte) QR_CHARSETS };
byte[] replies = qrBuilder.buildQueryReplies(requested, 80, 43, 80 * 43);
assertNotNull(replies);
int offset = -1;
for (int i = 0; i < replies.length - 3; i++) {
if ((replies[i] & 0xFF) == 0x81 && (replies[i + 1] & 0xFF) == QR_CHARSETS) {
offset = i + 2;
break;
}
}
assertTrue(offset >= 0, "QR_CHARSETS must be present");
// QR_CHARSETS payload structure:
// Flags (2 bytes), SDW (1 byte), SDH (1 byte), Form (1 byte), DevType (2 bytes), Res (1 byte), DL (1 byte)
// DL is at offset + 8, and is 7 bytes per descriptor.
int dl = replies[offset + 8] & 0xFF;
assertEquals(7, dl);
int descOffset = offset + 9;
// Descriptor 1: SET 0 (Base) -> flags 0x10
assertEquals(0x00, replies[descOffset] & 0xFF);
assertEquals(0x10, replies[descOffset + 1] & 0xFF);
// Descriptor 2: SET 1 (APL) -> flags 0x00
assertEquals(0x01, replies[descOffset + 7] & 0xFF);
assertEquals(0x00, replies[descOffset + 7 + 1] & 0xFF);
// Descriptor 3: PSA (Single plane) -> flags 0x80 (Loadable, single-plane)
assertEquals(0x02, replies[descOffset + 14] & 0xFF);
assertEquals(0x80, replies[descOffset + 14 + 1] & 0xFF);
// Descriptor 4: PSB (Single plane) -> flags 0x80 (Loadable, single-plane)
assertEquals(0x03, replies[descOffset + 21] & 0xFF);
assertEquals(0x80, replies[descOffset + 21 + 1] & 0xFF);
// Descriptor 5: PSC (Triple plane) -> flags 0xC0 (Loadable | Triple-plane)
assertEquals(0x04, replies[descOffset + 28] & 0xFF);
assertEquals(0xC0, replies[descOffset + 28 + 1] & 0xFF);
// Descriptor 6: PSD (Triple plane) -> flags 0xC0 (Loadable | Triple-plane)
assertEquals(0x05, replies[descOffset + 35] & 0xFF);
assertEquals(0xC0, replies[descOffset + 35 + 1] & 0xFF);
// Descriptor 7: PSE (Triple plane) -> flags 0xC0 (Loadable | Triple-plane)
assertEquals(0x06, replies[descOffset + 42] & 0xFF);
assertEquals(0xC0, replies[descOffset + 42 + 1] & 0xFF);
// Descriptor 8: PSF (Triple plane) -> flags 0xC0 (Loadable | Triple-plane)
assertEquals(0x07, replies[descOffset + 49] & 0xFF);
assertEquals(0xC0, replies[descOffset + 49 + 1] & 0xFF);
}
} }
@@ -0,0 +1,237 @@
package org.lib3270j.graphics;
import org.junit.jupiter.api.Test;
import java.io.ByteArrayOutputStream;
import static org.junit.jupiter.api.Assertions.*;
public class GocaDecoderTest {
@Test
public void testBasicLineAndColorOrders() {
GraphicsPlane plane = new GraphicsPlane(400, 300);
GocaDecoder decoder = new GocaDecoder(plane);
assertFalse(plane.hasContent());
ByteArrayOutputStream out = new ByteArrayOutputStream();
// Set Color to Red (GOCA Color 2) - 2 bytes: order, color
out.write(GocaConstants.G_GSCOL);
out.write(0x02); // Red
// Line from (1000, 1000) to (2000, 2000) - long order: order, len, x1, y1, x2, y2
out.write(GocaConstants.G_GLINE);
out.write(0x08); // 8 bytes: (x1, y1, x2, y2)
out.write((1000 >> 8) & 0xFF); out.write(1000 & 0xFF);
out.write((1000 >> 8) & 0xFF); out.write(1000 & 0xFF);
out.write((2000 >> 8) & 0xFF); out.write(2000 & 0xFF);
out.write((2000 >> 8) & 0xFF); out.write(2000 & 0xFF);
byte[] stream = out.toByteArray();
decoder.decodeStream(stream, 0, stream.length);
assertTrue(plane.hasContent());
}
@Test
public void testAreaAndPatternFill() {
GraphicsPlane plane = new GraphicsPlane(400, 300);
GocaDecoder decoder = new GocaDecoder(plane);
ByteArrayOutputStream out = new ByteArrayOutputStream();
// Set Pattern to Solid - 2 bytes: order, pattern
out.write(GocaConstants.G_GSPT);
out.write(GocaConstants.PT_SOLID);
// Begin Area - 2 bytes: order, flags
out.write(GocaConstants.G_GBAR);
out.write(0x00);
// Polyline forming a triangle: (500, 500) -> (1500, 500) -> (1000, 1500) -> (500, 500)
out.write(GocaConstants.G_GLINE);
out.write(0x10); // 4 points * 4 bytes = 16 bytes
out.write((500 >> 8) & 0xFF); out.write(500 & 0xFF);
out.write((500 >> 8) & 0xFF); out.write(500 & 0xFF);
out.write((1500 >> 8) & 0xFF); out.write(1500 & 0xFF);
out.write((500 >> 8) & 0xFF); out.write(500 & 0xFF);
out.write((1000 >> 8) & 0xFF); out.write(1000 & 0xFF);
out.write((1500 >> 8) & 0xFF); out.write(1500 & 0xFF);
out.write((500 >> 8) & 0xFF); out.write(500 & 0xFF);
out.write((500 >> 8) & 0xFF); out.write(500 & 0xFF);
// End Area - 1 byte
out.write(GocaConstants.G_GEAR);
byte[] stream = out.toByteArray();
decoder.decodeStream(stream, 0, stream.length);
assertTrue(plane.hasContent());
}
@Test
public void testMarkerAndVectorText() {
GraphicsPlane plane = new GraphicsPlane(400, 300);
GocaDecoder decoder = new GocaDecoder(plane);
ByteArrayOutputStream out = new ByteArrayOutputStream();
// Set Marker Type to Circle - 2 bytes: order, type
out.write(GocaConstants.G_GSMT);
out.write(GocaConstants.MK_CIRCLE);
// Draw Marker at (2000, 2000)
out.write(GocaConstants.G_GMRK);
out.write(0x04);
out.write((2000 >> 8) & 0xFF); out.write(2000 & 0xFF);
out.write((2000 >> 8) & 0xFF); out.write(2000 & 0xFF);
// Draw Vector Stroked Character String: "IBM" (EBCDIC: 0xC9, 0xC2, 0xD4)
out.write(GocaConstants.G_GCHST);
out.write(0x07); // 4 bytes pos + 3 bytes chars
out.write((1000 >> 8) & 0xFF); out.write(1000 & 0xFF);
out.write((1000 >> 8) & 0xFF); out.write(1000 & 0xFF);
out.write(0xC9); out.write(0xC2); out.write(0xD4);
byte[] stream = out.toByteArray();
decoder.decodeStream(stream, 0, stream.length);
assertTrue(plane.hasContent());
}
@Test
public void testProcedureOrdersAndErase() {
GraphicsPlane plane = new GraphicsPlane(400, 300);
GocaDecoder decoder = new GocaDecoder(plane);
// Draw something first
ByteArrayOutputStream out = new ByteArrayOutputStream();
out.write(GocaConstants.G_GLINE);
out.write(0x08);
out.write(0x00); out.write(0x00);
out.write(0x00); out.write(0x00);
out.write(0x00); out.write(0x64);
out.write(0x00); out.write(0x64);
byte[] drawStream = out.toByteArray();
decoder.decodeStream(drawStream, 0, drawStream.length);
assertTrue(plane.hasContent());
// Process procedure order 0x0A (Erase presentation space)
byte[] procOrders = new byte[] { (byte) GocaConstants.P_ERASE };
decoder.processProcedureOrders(procOrders, 0, procOrders.length);
assertFalse(plane.hasContent());
}
@Test
public void testSegmentAndRelativeLineOrders() {
GraphicsPlane plane = new GraphicsPlane(720, 688);
plane.setScreenDimensions(80, 43);
GocaDecoder decoder = new GocaDecoder(plane);
ByteArrayOutputStream out = new ByteArrayOutputStream();
// 0x70: Begin Segment (14 bytes total)
out.write(GocaConstants.G_BEGSEGM);
out.write(0x0C);
out.write(0x00); out.write(0x00); out.write(0x00); out.write(0x01); // Seg ID 1
out.write(0x74); out.write(0x70);
out.write(0x00); out.write(0x1E); out.write(0x00); out.write(0x00); out.write(0x00); out.write(0x00);
// 0x3E: End Prologue
out.write(GocaConstants.G_ENDPROLOGUE);
out.write(0x00);
// 0x0A: Color (7 = White)
out.write(GocaConstants.G_GSCOL);
out.write(0x07);
// 0xE1: Relative Line (Absolute Start (300, 200), deltas: (+10, -5), (-10, +5))
out.write(GocaConstants.G_GRLINE);
out.write(0x08); // 4 bytes start coord + 4 bytes (2 deltas) = 8 bytes
out.write(0x01); out.write(0x2C); // Start X = 300
out.write(0x00); out.write(0xC8); // Start Y = 200
out.write(0x0A); out.write(0xFB); // dx = +10, dy = -5
out.write(0xF6); out.write(0x05); // dx = -10, dy = +5
// 0x71: End Segment
out.write(GocaConstants.G_ENDSEGM);
out.write(0x00);
byte[] stream = out.toByteArray();
decoder.decodeStream(stream, 0, stream.length);
assertTrue(plane.hasContent());
}
@Test
public void testPartialOrderBufferingAcrossStreams() {
GraphicsPlane plane = new GraphicsPlane(400, 300);
GocaDecoder decoder = new GocaDecoder(plane);
// First packet sends G_GLINE order code and half of its coordinates
ByteArrayOutputStream chunk1 = new ByteArrayOutputStream();
chunk1.write(GocaConstants.G_GLINE);
chunk1.write(0x08); // 8 bytes: 2 points
chunk1.write(0x00); chunk1.write(0x64); // x1 = 100
chunk1.write(0x00); chunk1.write(0x64); // y1 = 100
byte[] b1 = chunk1.toByteArray();
decoder.decodeStream(b1, 0, b1.length);
// Order is not complete yet, so no line drawn yet
assertFalse(plane.hasContent());
// Second packet sends the rest of the order: x2 = 200, y2 = 200
ByteArrayOutputStream chunk2 = new ByteArrayOutputStream();
chunk2.write(0x00); chunk2.write(0xC8); // x2 = 200
chunk2.write(0x00); chunk2.write(0xC8); // y2 = 200
byte[] b2 = chunk2.toByteArray();
decoder.decodeStream(b2, 0, b2.length);
// Now the combined order executes!
assertTrue(plane.hasContent());
}
@Test
public void testSetCurrentPositionAndRelativeLine() {
GraphicsPlane plane = new GraphicsPlane(400, 300);
GocaDecoder decoder = new GocaDecoder(plane);
ByteArrayOutputStream out = new ByteArrayOutputStream();
// 0x21: Set Current Position to (50, 50)
out.write(GocaConstants.G_GSCP);
out.write(0x04); // len = 4
out.write(0x00); out.write(0x32); // x = 50
out.write(0x00); out.write(0x32); // y = 50
// 0xA1: Relative Line from Current Position: (+10, +20), (-5, -10)
out.write(GocaConstants.G_GCRLIN);
out.write(0x04); // len = 4 (2 steps)
out.write(0x0A); out.write(0x14); // dx = +10, dy = +20
out.write(0xFB); out.write(0xF6); // dx = -5, dy = -10
byte[] stream = out.toByteArray();
decoder.decodeStream(stream, 0, stream.length);
assertTrue(plane.hasContent());
assertEquals(55, decoder.getCurX());
assertEquals(60, decoder.getCurY());
}
@Test
public void test3179GCoordinateMapping() {
GraphicsPlane plane = new GraphicsPlane(720, 688);
plane.setScreenDimensions(80, 43);
// Screen center (0, 0) should map to canvas center (360, 343)
assertEquals(360, plane.mapX(0));
assertEquals(343, plane.mapY(0));
// Left edge (-360) should map to 0
assertEquals(0, plane.mapX(-360));
// Right edge (+359) should map to 719
assertEquals(719, plane.mapX(359));
// Top edge (+343) should map to 0
assertEquals(0, plane.mapY(343));
// Bottom edge (-344) should map to 687
assertEquals(687, plane.mapY(-344));
}
}
@@ -0,0 +1,57 @@
package org.lib3270j.graphics;
import org.junit.jupiter.api.Test;
import org.lib3270j.ConnectionConfig;
import static org.junit.jupiter.api.Assertions.*;
public class GraphicsModeTest {
@Test
public void testDefaultsAndParsing() {
assertEquals(GraphicsMode.NONE, GraphicsMode.fromString(null));
assertEquals(GraphicsMode.NONE, GraphicsMode.fromString(""));
assertEquals(GraphicsMode.NONE, GraphicsMode.fromString("NONE"));
assertEquals(GraphicsMode.NONE, GraphicsMode.fromString("off"));
assertEquals(GraphicsMode.NONE, GraphicsMode.fromString("false"));
assertEquals(GraphicsMode.PROGRAMMED_SYMBOLS, GraphicsMode.fromString("ps"));
assertEquals(GraphicsMode.PROGRAMMED_SYMBOLS, GraphicsMode.fromString("programmed_symbols"));
assertEquals(GraphicsMode.PROGRAMMED_SYMBOLS, GraphicsMode.fromString("apl"));
assertEquals(GraphicsMode.VECTOR_GRAPHICS, GraphicsMode.fromString("vector"));
assertEquals(GraphicsMode.VECTOR_GRAPHICS, GraphicsMode.fromString("goca"));
assertEquals(GraphicsMode.VECTOR_GRAPHICS, GraphicsMode.fromString("vector_graphics"));
assertEquals(GraphicsMode.BOTH, GraphicsMode.fromString("both"));
assertEquals(GraphicsMode.BOTH, GraphicsMode.fromString("all"));
assertEquals(GraphicsMode.BOTH, GraphicsMode.fromString("true"));
assertEquals(GraphicsMode.BOTH, GraphicsMode.fromString("on"));
}
@Test
public void testFlags() {
assertFalse(GraphicsMode.NONE.isProgrammedSymbolsEnabled());
assertFalse(GraphicsMode.NONE.isVectorGraphicsEnabled());
assertTrue(GraphicsMode.PROGRAMMED_SYMBOLS.isProgrammedSymbolsEnabled());
assertFalse(GraphicsMode.PROGRAMMED_SYMBOLS.isVectorGraphicsEnabled());
assertFalse(GraphicsMode.VECTOR_GRAPHICS.isProgrammedSymbolsEnabled());
assertTrue(GraphicsMode.VECTOR_GRAPHICS.isVectorGraphicsEnabled());
assertTrue(GraphicsMode.BOTH.isProgrammedSymbolsEnabled());
assertTrue(GraphicsMode.BOTH.isVectorGraphicsEnabled());
}
@Test
public void testConnectionConfigDefault() {
ConnectionConfig config = new ConnectionConfig();
assertEquals(GraphicsMode.BOTH, config.getGraphicsMode());
config.setGraphicsMode(GraphicsMode.NONE);
assertEquals(GraphicsMode.NONE, config.getGraphicsMode());
config.setGraphicsMode(null);
assertEquals(GraphicsMode.NONE, config.getGraphicsMode());
}
}
@@ -0,0 +1,167 @@
package org.lib3270j.graphics;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class ProgramSymbolManagerTest {
@Test
public void testSetAllocationAndLookup() {
ProgramSymbolManager manager = new ProgramSymbolManager();
// Loadable set can be queried after loadps or clearAll
assertNull(manager.getSymbolSet(0x40));
assertNull(manager.getSymbol(0x40, 0x41));
manager.clearAll();
assertNull(manager.getSymbolSet(0x40));
}
@Test
public void testSinglePlaneLoadPsFormat1() {
ProgramSymbolManager manager = new ProgramSymbolManager();
// Build Format 1 Load PS payload for LCID 0x40, code point 0x41 ('A')
// Format 1 header:
// byte 0: flags (0x01: format 1, single plane)
// byte 1: LCID (0x40)
// byte 2: Start code point (0x41)
// byte 3: RWS (0x02: loadable slot 2)
// followed by 18 bytes per symbol for 9x16 cell:
// Byte 0-1: Col 0 for rows 0..15 (0x80 = bit for row 0 col 0)
// Bytes 2-17: Cols 1..8 for rows 0..15 (0xFF = bits for row 0 cols 1..8)
byte[] payload = new byte[4 + 18];
payload[0] = 0x01; // Format 1
payload[1] = 0x40; // LCID 0x40
payload[2] = 0x41; // Code point 0x41
payload[3] = 0x02; // RWS 2
payload[4] = (byte) 0x80; // Row 0 Col 0 bit
payload[5] = (byte) 0x00; // Rows 8..15 Col 0
for (int r = 0; r < 16; r++) {
payload[6 + r] = (byte) 0xFF; // Cols 1..8 on all rows
}
manager.loadps(payload);
ProgramSymbolSet set = manager.getSymbolSet(0x40);
assertNotNull(set);
assertEquals(0x40, set.getLcid());
assertFalse(set.isTriplePlane());
ProgramSymbolSet.SymbolSlot slot = manager.getSymbol(0x40, 0x41);
assertNotNull(slot);
assertEquals(9, slot.getWidth());
assertEquals(12, slot.getHeight());
assertFalse(slot.isTriplePlane());
// Verify pixel data (Row 0 Col 0 is 1)
byte[] pixels = slot.getPixelData();
assertNotNull(pixels);
assertEquals(1, pixels[0]); // Row 0 Col 0 is 1
// Test rendering into 32-bit ARGB pixels
int fg = 0xFF00FF00; // Green
int bg = 0xFF000000; // Black
int[] rgb = slot.getRgbPixels(fg, bg);
assertNotNull(rgb);
assertEquals(9 * 12, rgb.length);
assertEquals(fg, rgb[0]); // Row 0 Col 0 pixel should be foreground Green
}
@Test
public void testExplicitCellDimensions() {
ProgramSymbolManager manager = new ProgramSymbolManager();
manager.setDefaultCellDimensions(9, 16);
byte[] payload = new byte[4 + 18];
payload[0] = 0x01; // Format 1
payload[1] = 0x40; // LCID 0x40
payload[2] = 0x41; // Code point 0x41
payload[3] = 0x02; // RWS 2
payload[4] = (byte) 0x80;
payload[5] = (byte) 0x00;
for (int r = 0; r < 16; r++) {
payload[6 + r] = (byte) 0xFF;
}
manager.loadps(payload);
ProgramSymbolSet.SymbolSlot slot = manager.getSymbol(0x40, 0x41);
assertNotNull(slot);
assertEquals(9, slot.getWidth());
assertEquals(16, slot.getHeight());
}
@Test
public void testTriplePlaneMultiColorComposite() {
ProgramSymbolManager manager = new ProgramSymbolManager();
// Build Format 1 Load PS payload for LCID 0x42, triple plane
// flags = 0x01 (Format 1)
// RWS = 4 (Triple Plane)
byte[] payload = new byte[4 + 18];
payload[0] = 0x01; // Format 1
payload[1] = 0x42; // LCID 0x42
payload[2] = 0x45; // Code point 0x45
payload[3] = 0x04; // RWS 4 (Triple Plane)
// Set pixel at row 0 col 0
payload[4] = (byte) 0x80; // Row 0 Col 0
payload[5] = (byte) 0x00;
for (int r = 0; r < 16; r++) {
payload[6 + r] = (byte) 0x00;
}
manager.loadps(payload);
ProgramSymbolSet set = manager.getSymbolSet(0x42);
assertNotNull(set);
assertTrue(set.isTriplePlane());
ProgramSymbolSet.SymbolSlot slot = manager.getSymbol(0x42, 0x45);
assertNotNull(slot);
assertTrue(slot.isTriplePlane());
// When loaded with colorPlane=0 (default), all 3 planes are set (val 7 = white composite)
byte[] pixels = slot.getPixelData();
assertEquals(7, pixels[0] & 0xFF);
// Render ARGB pixels
int[] rgb = slot.getRgbPixels(0xFFFFFFFF, 0xFF000000);
assertNotNull(rgb);
// Pixel (0,0) should be white composite (0xFFFFFFFF)
int pixelArgb = rgb[0];
int r = (pixelArgb >> 16) & 0xFF;
int g = (pixelArgb >> 8) & 0xFF;
int b = pixelArgb & 0xFF;
assertEquals(255, r);
assertEquals(255, g);
assertEquals(255, b);
}
@Test
public void testFormat3Bitstream() {
ProgramSymbolManager manager = new ProgramSymbolManager();
// Format 3 payload
// byte 0: 0x03 (Format 3)
// byte 1: LCID (0x44)
// byte 2: code point (0x43)
// byte 3: RWS (2)
// followed by (9 * 16 + 7) / 8 = 18 bytes
byte[] payload = new byte[4 + 18];
payload[0] = 0x03; // Format 3
payload[1] = 0x44; // LCID 0x44
payload[2] = 0x43; // Code point 0x43
payload[3] = 0x02; // RWS 2
payload[4] = (byte) 0x80; // First pixel is set
manager.loadps(payload);
ProgramSymbolSet set = manager.getSymbolSet(0x44);
assertNotNull(set);
ProgramSymbolSet.SymbolSlot slot = manager.getSymbol(0x44, 0x43);
assertNotNull(slot);
assertEquals(1, slot.getPixelData()[0]);
}
}
@@ -0,0 +1,108 @@
package org.lib3270j.tls;
import org.junit.jupiter.api.Test;
import org.lib3270j.ConnectionConfig;
import org.lib3270j.TerminalModel;
import javax.net.ssl.SSLContext;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.concurrent.atomic.AtomicBoolean;
import static org.junit.jupiter.api.Assertions.*;
public class TlsConfigTest {
@Test
public void testConnectionConfigDefaults() {
ConnectionConfig config = new ConnectionConfig("localhost", 23);
assertFalse(config.isUseTls());
assertTrue(config.isTlsVerifyCert());
assertEquals("TLS", config.getSslProtocol());
assertNull(config.getCertificateVerifier());
}
@Test
public void testParseHostStringTlsPrefixes() {
// L: prefix
ConnectionConfig c1 = ConnectionConfig.parseHostString("L:mainframe.example.com", 0, TerminalModel.IBM_3279_4);
assertTrue(c1.isUseTls());
assertEquals("mainframe.example.com", c1.getHost());
assertEquals(992, c1.getPort());
// ssl: prefix with explicit port
ConnectionConfig c2 = ConnectionConfig.parseHostString("ssl:zos.local:2323", 0, TerminalModel.IBM_3279_2);
assertTrue(c2.isUseTls());
assertEquals("zos.local", c2.getHost());
assertEquals(2323, c2.getPort());
// y: prefix
ConnectionConfig c3 = ConnectionConfig.parseHostString("y:vm370.net:992", 23, TerminalModel.IBM_3279_4);
assertTrue(c3.isUseTls());
assertEquals("vm370.net", c3.getHost());
assertEquals(992, c3.getPort());
// Plain host
ConnectionConfig c4 = ConnectionConfig.parseHostString("plain.host.com", 23, TerminalModel.IBM_3279_4);
assertFalse(c4.isUseTls());
assertEquals("plain.host.com", c4.getHost());
assertEquals(23, c4.getPort());
// IPv6 host with brackets
ConnectionConfig c5 = ConnectionConfig.parseHostString("L:[2001:db8::1]:992", 0, TerminalModel.IBM_3279_4);
assertTrue(c5.isUseTls());
assertEquals("2001:db8::1", c5.getHost());
assertEquals(992, c5.getPort());
}
@Test
public void testTlsTrustManagerBypass() {
ConnectionConfig config = new ConnectionConfig("untrusted.host", 992);
config.setUseTls(true);
config.setTlsVerifyCert(false);
TlsTrustManager trustManager = new TlsTrustManager(config);
// With tlsVerifyCert=false, checkServerTrusted must not throw even with null/empty chain
assertDoesNotThrow(() -> trustManager.checkServerTrusted(new X509Certificate[0], "RSA"));
}
@Test
public void testTlsTrustManagerVerifierCallback() {
ConnectionConfig config = new ConnectionConfig("selfsigned.host", 992);
config.setUseTls(true);
config.setTlsVerifyCert(true);
AtomicBoolean verifierInvoked = new AtomicBoolean(false);
config.setCertificateVerifier((chain, authType, exception) -> {
verifierInvoked.set(true);
return true; // Accept certificate
});
TlsTrustManager trustManager = new TlsTrustManager(config);
// Standard verification will fail on empty cert chain, triggering our callback
assertDoesNotThrow(() -> trustManager.checkServerTrusted(new X509Certificate[0], "RSA"));
assertTrue(verifierInvoked.get(), "Expected verifier callback to be invoked");
}
@Test
public void testTlsTrustManagerVerifierRejection() {
ConnectionConfig config = new ConnectionConfig("badcert.host", 992);
config.setUseTls(true);
config.setTlsVerifyCert(true);
config.setCertificateVerifier((chain, authType, exception) -> false); // Reject certificate
TlsTrustManager trustManager = new TlsTrustManager(config);
assertThrows(CertificateException.class, () -> trustManager.checkServerTrusted(new X509Certificate[0], "RSA"));
}
@Test
public void testCreateSSLContext() throws Exception {
ConnectionConfig config = new ConnectionConfig("secure.host", 992);
config.setUseTls(true);
SSLContext context = TlsTrustManager.createSSLContext(config);
assertNotNull(context);
assertNotNull(context.getSocketFactory());
}
}