-
Notifications
You must be signed in to change notification settings - Fork 242
/
NativeGNULinuxTerminal.java
126 lines (108 loc) · 3.92 KB
/
NativeGNULinuxTerminal.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
* This file is part of lanterna (https://github.com/mabe02/lanterna).
*
* lanterna is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright (C) 2010-2020 Martin Berglund
*/
package com.googlecode.lanterna.terminal;
import com.googlecode.lanterna.TerminalSize;
import com.googlecode.lanterna.terminal.ansi.UnixLikeTerminal;
import com.sun.jna.Native;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import static com.googlecode.lanterna.terminal.PosixLibC.*;
/**
* Terminal implementation that uses native libraries
*/
public class NativeGNULinuxTerminal extends UnixLikeTerminal {
private final PosixLibC libc;
private PosixLibC.termios savedTerminalState;
public NativeGNULinuxTerminal() throws IOException {
this(System.in,
System.out,
Charset.defaultCharset(),
CtrlCBehaviour.CTRL_C_KILLS_APPLICATION);
}
public NativeGNULinuxTerminal(
InputStream terminalInput,
OutputStream terminalOutput,
Charset terminalCharset,
CtrlCBehaviour terminalCtrlCBehaviour) throws IOException {
super(terminalInput,
terminalOutput,
terminalCharset,
terminalCtrlCBehaviour);
this.libc = (PosixLibC) Native.loadLibrary("c", PosixLibC.class);
this.savedTerminalState = null;
}
public void saveTerminalSettings() throws IOException {
savedTerminalState = getTerminalState();
}
public void restoreTerminalSettings() throws IOException {
if(savedTerminalState != null) {
libc.tcsetattr(STDIN_FILENO, TCSANOW, savedTerminalState);
}
}
public void keyEchoEnabled(boolean b) throws IOException {
PosixLibC.termios state = getTerminalState();
if(b) {
state.c_lflag |= ECHO;
}
else {
state.c_lflag &= ~ECHO;
}
libc.tcsetattr(STDIN_FILENO, TCSANOW, state);
}
public void canonicalMode(boolean b) throws IOException {
PosixLibC.termios state = getTerminalState();
if(b) {
state.c_lflag |= ICANON;
}
else {
state.c_lflag &= ~ICANON;
}
libc.tcsetattr(STDIN_FILENO, TCSANOW, state);
}
public void keyStrokeSignalsEnabled(boolean b) throws IOException {
PosixLibC.termios state = getTerminalState();
if(b) {
state.c_lflag |= ISIG;
}
else {
state.c_lflag &= ~ISIG;
}
libc.tcsetattr(STDIN_FILENO, TCSANOW, state);
}
public void registerTerminalResizeListener(final Runnable runnable) throws IOException {
libc.signal(SIGWINCH, new sig_t() {
public synchronized void invoke(int signal) {
runnable.run();
}
});
}
@Override
protected TerminalSize findTerminalSize() throws IOException {
PosixLibC.winsize winsize = new winsize();
libc.ioctl(PosixLibC.STDOUT_FILENO, PosixLibC.TIOCGWINSZ, winsize);
return new TerminalSize(winsize.ws_col, winsize.ws_row);
}
private PosixLibC.termios getTerminalState() {
PosixLibC.termios termios = new PosixLibC.termios();
libc.tcgetattr(STDIN_FILENO, termios);
return termios;
}
}