diff options
Diffstat (limited to 'desktop-utilities')
-rw-r--r-- | desktop-utilities/lumina-terminal/TerminalWidget.cpp | 20 | ||||
-rw-r--r-- | desktop-utilities/lumina-terminal/TtyProcess.cpp | 59 | ||||
-rw-r--r-- | desktop-utilities/lumina-terminal/TtyProcess.h | 6 |
3 files changed, 69 insertions, 16 deletions
diff --git a/desktop-utilities/lumina-terminal/TerminalWidget.cpp b/desktop-utilities/lumina-terminal/TerminalWidget.cpp index 6818201a..fa67093b 100644 --- a/desktop-utilities/lumina-terminal/TerminalWidget.cpp +++ b/desktop-utilities/lumina-terminal/TerminalWidget.cpp @@ -9,6 +9,7 @@ #include <QProcessEnvironment> #include <QDebug> #include <QApplication> +#include <QScrollBar> TerminalWidget::TerminalWidget(QWidget *parent, QString dir) : QTextEdit(parent){ //Setup the text widget @@ -43,20 +44,14 @@ void TerminalWidget::aboutToClose(){ // ================== void TerminalWidget::UpdateText(){ //read the data from the process - qDebug() << "UpdateText"; + //qDebug() << "UpdateText"; if(!PROC->isOpen()){ return; } - //if ( PROC->bytesAvailable() <= 0 ) - // return; - - /*qDebug() << "Reading all data"; - char buffer[64]; - ssize_t rtot = read(sn->socket(),&buffer,64); - buffer[rtot]='\0';*/ QByteArray buffer = PROC->readTTY(); - qDebug() << "Process Data:" << QString(buffer); - this->insertPlainText(QString(buffer)); + QString text = QString(buffer); + text.replace("\r\n","\n"); + this->insertPlainText(text); //adjust the scrollbar as needed - + this->verticalScrollBar()->setValue(this->verticalScrollBar()->maximum()); } void TerminalWidget::ShellClosed(){ @@ -85,8 +80,9 @@ void TerminalWidget::keyPressEvent(QKeyEvent *ev){ //QTextEdit::keyPressEvent(ev); //echo the input on the widget }*/ QByteArray ba; ba.append(txt); //avoid any byte conversions - qDebug() << "Forward Input:" << txt << ev->key() << ba; + //qDebug() << "Forward Input:" << txt << ev->key() << ba; PROC->writeTTY(ba); + ev->ignore(); } void TerminalWidget::mousePressEvent(QMouseEvent *ev){ diff --git a/desktop-utilities/lumina-terminal/TtyProcess.cpp b/desktop-utilities/lumina-terminal/TtyProcess.cpp index b3c6aebc..8d59cdde 100644 --- a/desktop-utilities/lumina-terminal/TtyProcess.cpp +++ b/desktop-utilities/lumina-terminal/TtyProcess.cpp @@ -63,8 +63,9 @@ void TTYProcess::closeTTY(){ } void TTYProcess::writeTTY(QByteArray output){ - int written = ::write(ttyfd, output.data(), output.size()); - qDebug() << "Wrote:" << written; + //int written = + ::write(ttyfd, output.data(), output.size()); + //qDebug() << "Wrote:" << written; } QByteArray TTYProcess::readTTY(){ @@ -74,13 +75,65 @@ QByteArray TTYProcess::readTTY(){ ssize_t rtot = read(sn->socket(),&buffer,64); buffer[rtot]='\0'; BA = QByteArray(buffer, rtot); - return BA; + if(!fragBA.isEmpty()){ + //Have a leftover fragment, include this too + BA = BA.prepend(fragBA); + fragBA.clear(); + } + bool bad = true; + BA = CleanANSI(BA, bad); + if(bad){ + //incomplete fragent - read some more first + fragBA = BA; + return readTTY(); + }else{ + return BA; + } } bool TTYProcess::isOpen(){ return (ttyfd!=0); } +QByteArray TTYProcess::CleanANSI(QByteArray raw, bool &incomplete){ + incomplete = true; + qDebug() << "Clean ANSI Data:" << raw; + //IN_LINE TERMINAL COLOR CODES (ANSI Escape Codes) "\x1B[<colorcode>m" + // - Just remove them for now + + //Special XTERM encoding (legacy support) + int index = raw.indexOf("\x1B]"); + while(index>=0){ + //The end character of this sequence is the Bell command ("\x07") + int end = raw.indexOf("\x07"); + if(end<0){ return raw; } //incomplete raw array + raw = raw.remove(index, end-index+1); + index = raw.indexOf("\x1B]"); + } + + // COLOR CODES + index = raw.indexOf("\x1B["); + while(index>=0){ + int end = raw.indexOf("m",index); + if(end<0){ return raw; } //incomplete raw array + raw = raw.remove(index, end-index+1); + index = raw.indexOf("\x1B["); + } + qDebug() << " - Removed Color Codes:" << raw; + + // SYSTEM BELL + index = raw.indexOf("\x07"); + while(index>=0){ + //qDebug() << "Remove Bell:" << index; + raw = raw.remove(index,1); + index = raw.indexOf("\x07"); + } + qDebug() << " - Fully Cleaned:" << raw; + + incomplete = false; + return raw; +} + // === PRIVATE === pid_t TTYProcess::LaunchProcess(int& fd, char *prog, char **child_args){ //Returns: -1 for errors, positive value (file descriptor) for the master side of the TTY to watch diff --git a/desktop-utilities/lumina-terminal/TtyProcess.h b/desktop-utilities/lumina-terminal/TtyProcess.h index 45cdedd2..26cf3ed5 100644 --- a/desktop-utilities/lumina-terminal/TtyProcess.h +++ b/desktop-utilities/lumina-terminal/TtyProcess.h @@ -41,11 +41,15 @@ public: //Status update checks bool isOpen(); + //Functions for handling ANSI escape codes (typically not used by hand) + QByteArray CleanANSI(QByteArray, bool &incomplete); + private: pid_t childProc; int ttyfd; QSocketNotifier *sn; - + QByteArray fragBA; //fragment ByteArray + //==================================== // C Library function for setting up the PTY // Inputs: |