terminal pipe

This commit is contained in:
Vadim Lopatin 2016-05-31 16:21:39 +03:00
parent c3293a6d09
commit 3ca6e5ea6e
2 changed files with 101 additions and 34 deletions

View File

@ -181,10 +181,10 @@ class OutputPanel : DockWindow {
_terminalWidget.write("\x1b[34;45m blue on magenta "d); _terminalWidget.write("\x1b[34;45m blue on magenta "d);
_terminalWidget.write("\x1b[31;46m red on cyan "d); _terminalWidget.write("\x1b[31;46m red on cyan "d);
//_terminalWidget.write("\x1b[2Jerased screen"d); //_terminalWidget.write("\x1b[2Jerased screen"d);
TerminalDevice term = new TerminalDevice(); //TerminalDevice term = new TerminalDevice();
if (!term.create()) { //if (!term.create()) {
Log.e("Cannot create terminal device"); // Log.e("Cannot create terminal device");
} //}
return _tabs; return _tabs;
} }

View File

@ -655,19 +655,85 @@ class TerminalWidget : WidgetGroup, OnScrollHandler {
} }
class TerminalDevice { import core.thread;
class TerminalDevice : Thread {
version (Windows) {
import win32.windows;
HANDLE hpipe;
} else {
int masterfd; int masterfd;
int slavefd; }
string name; string name;
bool closed;
bool connected;
this() {
super(&threadProc);
}
~this() {
close();
}
void threadProc() {
}
bool write(string msg) {
if (!msg.length)
return true;
if (!closed)
return false;
version (Windows) {
for (;;) {
DWORD bytesWritten = 0;
if (WriteFile(hpipe, cast(char*)msg.ptr, msg.length, &bytesWritten, null) != TRUE) {
return false;
}
if (bytesWritten < msg.length)
msg = msg[bytesWritten .. $];
else
break;
}
} else {
// linux/posix
}
return true;
}
void close() { void close() {
version (Windows) {
if (hpipe && hpipe != INVALID_HANDLE_VALUE) {
CloseHandle(hpipe);
hpipe = null;
}
} else {
if (masterfd && masterfd != -1) { if (masterfd && masterfd != -1) {
import core.sys.posix.unistd; import core.sys.posix.unistd;
close(masterfd); close(masterfd);
masterfd = 0; masterfd = 0;
} }
} }
name = null;
closed = true;
}
bool create() { bool create() {
import std.string; import std.string;
version (Windows) {
import std.uuid;
name = "\\\\.\\pipe\\dlangide-terminal-" ~ randomUUID().toString;
hpipe = CreateNamedPipeA(cast(const(char)*)name.toStringz,
PIPE_ACCESS_DUPLEX,
cast(uint)PIPE_TYPE_BYTE,
1,
16384,
16384,
50,
null);
if (hpipe == INVALID_HANDLE_VALUE) {
Log.e("Failed to create named pipe for terminal, error=", GetLastError());
close();
return false;
}
} else {
const(char) * s = null; const(char) * s = null;
{ {
import core.sys.posix.fcntl; import core.sys.posix.fcntl;
@ -677,24 +743,25 @@ class TerminalDevice {
masterfd = posix_openpt(O_RDWR | O_NOCTTY); masterfd = posix_openpt(O_RDWR | O_NOCTTY);
if (masterfd == -1) { if (masterfd == -1) {
Log.e("posix_openpt failed - cannot open terminal"); Log.e("posix_openpt failed - cannot open terminal");
close();
return false; return false;
} }
if (grantpt(masterfd) == -1 || unlockpt(masterfd) == -1) { if (grantpt(masterfd) == -1 || unlockpt(masterfd) == -1) {
Log.e("grantpt / unlockpt failed - cannot open terminal"); Log.e("grantpt / unlockpt failed - cannot open terminal");
close(masterfd); close();
masterfd = 0;
return false; return false;
} }
s = ptsname(masterfd); s = ptsname(masterfd);
if (!s) { if (!s) {
Log.e("ptsname failed - cannot open terminal"); Log.e("ptsname failed - cannot open terminal");
close(masterfd); close();
masterfd = 0;
return false; return false;
} }
} }
name = fromStringz(s).dup; name = fromStringz(s).dup;
}
Log.i("ptty device created: ", name); Log.i("ptty device created: ", name);
start();
return true; return true;
} }
} }