diff options
Diffstat (limited to 'src-qt5/experimental')
-rw-r--r-- | src-qt5/experimental/lumina-terminal/TerminalWidget.cpp | 93 | ||||
-rw-r--r-- | src-qt5/experimental/lumina-terminal/TtyProcess.cpp | 67 | ||||
-rw-r--r-- | src-qt5/experimental/lumina-terminal/TtyProcess.h | 6 |
3 files changed, 103 insertions, 63 deletions
diff --git a/src-qt5/experimental/lumina-terminal/TerminalWidget.cpp b/src-qt5/experimental/lumina-terminal/TerminalWidget.cpp index 7601ae9f..9fe4f15e 100644 --- a/src-qt5/experimental/lumina-terminal/TerminalWidget.cpp +++ b/src-qt5/experimental/lumina-terminal/TerminalWidget.cpp @@ -162,6 +162,7 @@ void TerminalWidget::applyANSI(QByteArray code){ //KEYPAD MODES if(code.at(0)=='='){ altkeypad = true; } else if(code.at(0)=='>'){ altkeypad = false; } + else if(code.at(0)=='H'){ InsertText("\t"); } else{ qDebug() << "Unhandled ANSI Code:" << code; } @@ -176,42 +177,63 @@ void TerminalWidget::applyANSI(QByteArray code){ int num = 1; if(code.size()>2){ num = code.mid(1, code.size()-2).toInt(); } //everything in the middle QTextCursor cur = this->textCursor(); + qDebug() << "Move Cursor Up:" << num; cur.movePosition(QTextCursor::Up, QTextCursor::MoveAnchor, num); this->setTextCursor(cur); }else if(code.endsWith("B")){ //Move Down int num = 1; if(code.size()>2){ num = code.mid(1, code.size()-2).toInt(); } //everything in the middle - QTextCursor cur = this->textCursor(); - cur.movePosition(QTextCursor::Down, QTextCursor::MoveAnchor, num); + QTextCursor cur = this->textCursor(); + qDebug() << "Move Cursor Down:" << num; + for(int i=1; i<num; i++){ + if(!cur.movePosition(QTextCursor::Down, QTextCursor::MoveAnchor, 1)){ + //Need to add a new line(row) to the editor + this->document()->setPlainText(this->document()->toPlainText()+"\n"); + cur.movePosition(QTextCursor::End, QTextCursor::MoveAnchor); //now move the cursor down to the new line + } + } + //cur.movePosition(QTextCursor::Down, QTextCursor::MoveAnchor, num); this->setTextCursor(cur); }else if(code.endsWith("C")){ //Move Forward int num = 1; if(code.size()>2){ num = code.mid(1, code.size()-2).toInt(); } //everything in the middle QTextCursor cur = this->textCursor(); - cur.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, num); + qDebug() << "Move Cursor Forward:" << num; + for(int i=1; i<num; i++){ + if(!cur.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, 1)){ + //Need to add a new line(row) to the editor + this->document()->setPlainText(this->document()->toPlainText()+" "); + cur.movePosition(QTextCursor::End, QTextCursor::MoveAnchor); //now move the cursor down to the new line + } + } + //cur.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, num); this->setTextCursor(cur); }else if(code.endsWith("D")){ //Move Back int num = 1; if(code.size()>2){ num = code.mid(1, code.size()-2).toInt(); } //everything in the middle QTextCursor cur = this->textCursor(); + qDebug() << "Move Cursor Back:" << num; cur.movePosition(QTextCursor::Left, QTextCursor::MoveAnchor, num); this->setTextCursor(cur); }else if(code.endsWith("E")){ //Move Next/down Lines (go toward end) int num = 1; if(code.size()>2){ num = code.mid(1, code.size()-2).toInt(); } //everything in the middle QTextCursor cur = this->textCursor(); + qDebug() << "Move Cursor NextRow:" << num; cur.movePosition(QTextCursor::NextRow, QTextCursor::MoveAnchor, num); this->setTextCursor(cur); }else if(code.endsWith("F")){ //Move Previous/up Lines (go to beginning) int num = 1; if(code.size()>2){ num = code.mid(1, code.size()-2).toInt(); } //everything in the middle QTextCursor cur = this->textCursor(); + qDebug() << "Move Cursor PrevRow:" << num; cur.movePosition(QTextCursor::PreviousRow, QTextCursor::MoveAnchor, num); this->setTextCursor(cur); }else if(code.endsWith("G")){ //Move to specific column int num = 1; if(code.size()>2){ num = code.mid(1, code.size()-2).toInt(); } //everything in the middle QTextCursor cur = this->textCursor(); + qDebug() << "Move Cursor To Column:" << num; cur.setPosition(num); this->setTextCursor(cur); }else if(code.endsWith("H") || code.endsWith("f") ){ //Move to specific position (row/column) @@ -220,20 +242,35 @@ void TerminalWidget::applyANSI(QByteArray code){ int numR, numC; numR = numC = 1; if(mid >=2){ numR = code.mid(1,mid-1).toInt(); } if(mid < code.size()-1){ numC = code.mid(mid+1,code.size()-mid-2).toInt(); } - + //qDebug() << "NumR:" << numR << startrow << endrow << this->document()->lineCount() -1; + //qDebug() << "NumC:" << numC; if(startrow>=0 && endrow>=0){ if(numR == startrow){ numR = 0;} else if(numR==endrow){ numR = this->document()->lineCount()-1; } } - qDebug() << "Set Text Position (absolute):" << "Code:" << code << "Row:" << numR << "Col:" << numC; + //qDebug() << "Set Text Position (absolute):" << "Code:" << code << "Row:" << numR << "Col:" << numC; //qDebug() << " - Current Pos:" << this->textCursor().position() << "Line Count:" << this->document()->lineCount(); //if(!this->textCursor().movePosition(QTextCursor::Start, QTextCursor::MoveAnchor,1) ){ qDebug() << "Could not go to start"; } QTextCursor cur(this->textCursor()); cur.setPosition(QTextCursor::Start, QTextCursor::MoveAnchor); //go to start of document //qDebug() << " - Pos After Start Move:" << cur.position(); - if( !cur.movePosition(QTextCursor::Down, QTextCursor::MoveAnchor, numR) ){ qDebug() << "Could not go to row:" << numR; } + for(int i=1; i<numR; i++){ + if(!cur.movePosition(QTextCursor::Down, QTextCursor::MoveAnchor, 1)){ + //Need to add a new line(row) to the editor + this->document()->setPlainText(this->document()->toPlainText()+"\n"); + cur.movePosition(QTextCursor::End, QTextCursor::MoveAnchor); //now move the cursor down to the new line + } + } + //if( !cur.movePosition(QTextCursor::Down, QTextCursor::MoveAnchor, numR) ){ qDebug() << "Could not go to row:" << numR; } //qDebug() << " - Pos After Down Move:" << cur.position(); - if( !cur.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, numC) ){ qDebug() << "Could not go to col:" << numC; } + for(int i=1; i<numC; i++){ + if(!cur.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, 1)){ + //Need to add a new line(row) to the editor + this->document()->setPlainText(this->document()->toPlainText()+" "); + cur.movePosition(QTextCursor::End, QTextCursor::MoveAnchor); //now move the cursor down to the new line + } + } + //if( !cur.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, numC) ){ qDebug() << "Could not go to col:" << numC; } /*this->textCursor().setPosition( this->document()->findBlockByLineNumber(numR).position() ); qDebug() << " - Pos After Row Move:" << this->textCursor().position(); if( !this->textCursor().movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, numC) ){ qDebug() << "Could not go to col:" << numC; }*/ @@ -274,7 +311,7 @@ void TerminalWidget::applyANSI(QByteArray code){ cur.movePosition(QTextCursor::End, QTextCursor::KeepAnchor, 1); cur.removeSelectedText(); this->setTextCursor(cur); - } + } }else if(code.endsWith("K")){ //EL - Erase in Line int num = 0; if(code.size()>2){ num = code.mid(1, code.size()-2).toInt(); } //everything in the middle @@ -312,12 +349,12 @@ void TerminalWidget::applyANSI(QByteArray code){ }else if(num==3){ //clear all this->clear(); } - //SCROLL MOVEMENT CODES + //SCROLL MOVEMENT CODES //}else if(code.endsWith("S")){ // SU - Scroll Up //qDebug() << "Scroll Up:" << code; //}else if(code.endsWith("T")){ // SD - Scroll Down //qDebug() << "Scroll Down:" << code; - + // GRAPHICS RENDERING }else if(code.endsWith("m")){ //Format: "[<number>;<number>m" (no limit to sections separated by ";") @@ -339,17 +376,17 @@ void TerminalWidget::applyANSI(QByteArray code){ //qDebug() << "Color Code:" << code << start << end << code.mid(start, end-start); if(end>start){ applyANSIColor(code.mid(start, end-start).toInt());} else{ applyANSIColor(0); } - - + + // GRAPHICS MODES //}else if(code.endsWith("h")){ - + //}else if(code.endsWith("l")){ - + }else{ qDebug() << "Unhandled Control Code:" << code; } - + } //End VT100 control codes else{ qDebug() << "Unhandled Control Code:" << code; @@ -437,7 +474,7 @@ void TerminalWidget::sendKeyPress(int key){ ba.append("\e[3~"); break; case Qt::Key_Backspace: - ba.append("\x08"); + ba.append("\x08"); break; case Qt::Key_Left: if(altkeypad){ ba.append("^[D"); } @@ -457,10 +494,10 @@ void TerminalWidget::sendKeyPress(int key){ break; case Qt::Key_Home: ba.append("\x1b[H"); - break; + break; case Qt::Key_End: ba.append("\x1b[F"); - break; + break; } //qDebug() << "Forward Input:" << ba; if(!ba.isEmpty()){ PROC->writeTTY(ba); } @@ -490,19 +527,20 @@ void TerminalWidget::copySelection(){ void TerminalWidget::pasteSelection(){ QString text = QApplication::clipboard()->text(); - if(!text.isEmpty()){ + if(!text.isEmpty()){ QByteArray ba; ba.append(text); //avoid any byte conversions - PROC->writeTTY(ba); + PROC->writeTTY(ba); } } void TerminalWidget::updateTermSize(){ if(!PROC->isOpen()){ return; } QSize pix = this->size(); //pixels - QSize chars; + QSize chars; chars.setWidth( pix.width()/this->fontMetrics().width("W") ); chars.setHeight( pix.height()/this->fontMetrics().lineSpacing() ); - + //qDebug() << "Set Terminal Size:" << chars << pix; + if(chars.width() <2 || chars.height() <2){ return; } //skip this - cannot go less than 2 characters wide/high PROC->setTerminalSize(chars,pix); } @@ -510,7 +548,7 @@ void TerminalWidget::updateTermSize(){ // PROTECTED // ================== void TerminalWidget::keyPressEvent(QKeyEvent *ev){ - + if(ev->text().isEmpty() || ev->text()=="\b" ){ sendKeyPress(ev->key()); }else{ @@ -518,7 +556,7 @@ void TerminalWidget::keyPressEvent(QKeyEvent *ev){ //qDebug() << "Forward Input:" << ba; PROC->writeTTY(ba); } - + ev->ignore(); } @@ -526,7 +564,7 @@ void TerminalWidget::mousePressEvent(QMouseEvent *ev){ this->setFocus(); this->activateWindow(); if(ev->button()==Qt::RightButton){ - QTextEdit::mousePressEvent(ev); + QTextEdit::mousePressEvent(ev); }else if(ev->button()==Qt::MiddleButton){ pasteSelection(); }else if(ev->button()==Qt::LeftButton){ @@ -555,16 +593,17 @@ void TerminalWidget::mouseReleaseEvent(QMouseEvent *ev){ }else if(ev->button()==Qt::RightButton){ copyA->setEnabled( selCursor.hasSelection() ); pasteA->setEnabled( !QApplication::clipboard()->text().isEmpty() ); - contextMenu->popup( this->mapToGlobal(ev->pos()) ); + contextMenu->popup( this->mapToGlobal(ev->pos()) ); } Q_UNUSED(ev); } void TerminalWidget::mouseDoubleClickEvent(QMouseEvent *ev){ - Q_UNUSED(ev); + Q_UNUSED(ev); } void TerminalWidget::resizeEvent(QResizeEvent *ev){ + if(resizeTimer->isActive()){ resizeTimer->stop(); } resizeTimer->start(); QTextEdit::resizeEvent(ev); } diff --git a/src-qt5/experimental/lumina-terminal/TtyProcess.cpp b/src-qt5/experimental/lumina-terminal/TtyProcess.cpp index b6ef8f6d..fc982598 100644 --- a/src-qt5/experimental/lumina-terminal/TtyProcess.cpp +++ b/src-qt5/experimental/lumina-terminal/TtyProcess.cpp @@ -17,7 +17,7 @@ TTYProcess::TTYProcess(QObject *parent) : QObject(parent){ TTYProcess::~TTYProcess(){ closeTTY(); //make sure everything is closed properly } - + // === PUBLIC === bool TTYProcess::startTTY(QString prog, QStringList args, QString workdir){ if(workdir=="~"){ workdir = QDir::homePath(); } @@ -71,9 +71,9 @@ bool TTYProcess::startTTY(QString prog, QStringList args, QString workdir){ //Launch the process attached to a new PTY int FD = 0; pid_t tmp = LaunchProcess(FD, cprog, cargs); - if(DEBUG){ - qDebug() << " - PID:" << tmp; - qDebug() << " - FD:" << FD; + if(DEBUG){ + qDebug() << " - PID:" << tmp; + qDebug() << " - FD:" << FD; } if(tmp<0){ return false; } //error else{ @@ -94,7 +94,7 @@ bool TTYProcess::startTTY(QString prog, QStringList args, QString workdir){ void TTYProcess::closeTTY(){ int junk; if(0==waitpid(childProc, &junk, WNOHANG)){ - kill(childProc, SIGKILL); + kill(childProc, SIGKILL); } if(ttyfd!=0 && sn!=0){ sn->setEnabled(false); @@ -130,13 +130,13 @@ QByteArray TTYProcess::readTTY(){ BA = CleanANSI(BA, bad); if(bad){ //incomplete fragent - read some more first - fragBA = BA; + fragBA = BA; return readTTY(); }else{ if(DEBUG){ qDebug() << "Read Data:" << BA; } //BUG BYPASS - 12/7/16 //If the PTY gets input fairly soon after starting, the PTY will re-print the initial line(s) - if(starting && !BA.contains("\n") ){ + /*if(starting && !BA.contains("\n") ){ //qDebug() << "Starting phase 1:" << BA; writeTTY("tset\n"); //Terminal Setup utility (uses the TERM env variable) BA.clear(); @@ -144,7 +144,7 @@ QByteArray TTYProcess::readTTY(){ //qDebug() << "Starting phase 2:" << BA; BA.remove(0, BA.indexOf("\n")+1); starting = false; - } + }*/ //Apply known fixes for replies to particular inputs (mostly related to cursor position *within* the current line) // This appears to be primarily from the concept that the cursor position is always at the end of the line (old VT limitation?) // so almost all these fixes are for cursor positioning within the current line @@ -187,15 +187,16 @@ QByteArray TTYProcess::readTTY(){ } void TTYProcess::setTerminalSize(QSize chars, QSize pixels){ - if(ttyfd==0){ return; } + if(ttyfd==0 || chars.isNull() || chars.width()<2 || chars.height()<2 ){ return; } struct winsize c_sz; c_sz.ws_row = chars.height(); c_sz.ws_col = chars.width(); c_sz.ws_xpixel = pixels.width(); c_sz.ws_ypixel = pixels.height(); - if( ioctl(ttyfd, TIOCSWINSZ, &ws) ){ - qDebug() << "Error settings terminal size"; + if( ioctl(ttyfd, TIOCGWINSZ, &ws) ){ + qDebug() << "Error settings terminal size:" << chars << pixels; }else{ + //qDebug() <<"Set Terminal Size:" << pixels << chars; } } @@ -209,7 +210,7 @@ QByteArray TTYProcess::CleanANSI(QByteArray raw, bool &incomplete){ //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){ @@ -234,49 +235,49 @@ QByteArray TTYProcess::CleanANSI(QByteArray raw, bool &incomplete){ } index = raw.indexOf("\x1B",index+1); //now find the next one } - + // SYSTEM BELL index = raw.indexOf("\x07"); - while(index>=0){ + while(index>=0){ //qDebug() << "Remove Bell:" << index; - raw = raw.remove(index,1); + raw = raw.remove(index,1); index = raw.indexOf("\x07"); } //VT220(?) print character code (cut out the code, leave the character) index=raw.indexOf("\x1b[@"); - while(index>=0){ - raw = raw.remove(index,3); + while(index>=0){ + raw = raw.remove(index,3); index = raw.indexOf("\x1b[@"); } //VT102 Identify request index = raw.indexOf("\x1b[Z"); - while(index>=0){ - raw = raw.remove(index,3); + while(index>=0){ + raw = raw.remove(index,3); index = raw.indexOf("\x1b[Z"); //Also send the proper reply to this identify request right away writeTTY("\x1b[/Z"); } //Terminal Status request index = raw.indexOf("\x1b[5n"); - while(index>=0){ - raw = raw.remove(index,4); + while(index>=0){ + raw = raw.remove(index,4); index = raw.indexOf("\x1b[5n"); //Also send the proper reply to this identify request right away writeTTY("\x1b[c"); //everything ok } //Terminal Identify request index = raw.indexOf("\x1b[c"); - while(index>=0){ - raw = raw.remove(index,3); + while(index>=0){ + raw = raw.remove(index,3); index = raw.indexOf("\x1b[c"); //Also send the proper reply to this identify request right away writeTTY("\x1b[1c"); //VT220 reply code } //Terminal Identify request (xterm/termcap?) /*index = raw.indexOf("\x1b[P"); - while(index>=0){ - raw = raw.remove(index,3); + while(index>=0){ + raw = raw.remove(index,3); index = raw.indexOf("\x1b[P"); //Also send the proper reply to this identify request right away qDebug() << " - Got XTERM/TERMCAP identify request ([P)"; @@ -289,7 +290,7 @@ QByteArray TTYProcess::CleanANSI(QByteArray raw, bool &incomplete){ // === 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 + //Returns: -1 for errors, positive value (file descriptor) for the master side of the TTY to watch //First open/setup a new pseudo-terminal file/device on the system (master side) //fd = posix_openpt(O_RDWR | O_NOCTTY); //open read/write pid_t PID = forkpty( &fd, 0, 0, 0); @@ -300,7 +301,7 @@ pid_t TTYProcess::LaunchProcess(int& fd, char *prog, char **child_args){ exit(rc); }else{ //Master process - return PID; + return PID; } /*if(fd<0){ return -1; } //could not create pseudo-terminal int rc = grantpt(fd); //set permissions @@ -308,7 +309,7 @@ pid_t TTYProcess::LaunchProcess(int& fd, char *prog, char **child_args){ rc = unlockpt(fd); //unlock file (ready for use) //rc = fchown(fd, getuid(), getgid()); setupTtyFd(fd); - if(rc!=0){ return -1; } + if(rc!=0){ return -1; } //Now fork, return the Master device and setup the child pid_t PID = fork(); if(PID==0){ @@ -316,7 +317,7 @@ pid_t TTYProcess::LaunchProcess(int& fd, char *prog, char **child_args){ int fds = ::open(ptsname(fd), O_RDWR | O_NOCTTY); //open slave side read/write rc = fchown(fds, getuid(), getgid()); ::close(fd); //close the master side from the slave thread - + //Adjust the slave side mode to SANE setupTtyFd(fds); //Change the controlling terminal in child thread to the slave PTY @@ -326,10 +327,10 @@ pid_t TTYProcess::LaunchProcess(int& fd, char *prog, char **child_args){ dup(fds); // Set slave PTY as standard input (0); dup(fds); // Set slave PTY as standard output (1); dup(fds); // Set slave PTY as standard error (2); - + setsid(); //Make current process new session leader (so we can set controlling terminal) ioctl(0,TIOCSCTTY, 1); //Set the controlling terminal to the slave PTY - + //Execute the designated program //rc = execvp("tset", NULL); rc = execvp(prog, child_args); @@ -349,7 +350,7 @@ void TTYProcess::setupTtyFd(int fd){ //TSET.c_iflag &= ~(IGNBRK | PARMRK | ISTRIP | ICRNL | IXON | IXANY | IXOFF); //ignore special characters //TSET.c_iflag &= IUTF8; //enable UTF-8 support //Set Local Modes - //TSET.c_lflag &= (ECHO | ECHONL | ECHOKE); //Echo inputs (normal, newline, and KILL character line break) + TSET.c_lflag &= (ECHO | ECHONL | ECHOKE); //Echo inputs (normal, newline, and KILL character line break) //TSET.c_lflag &= ~ICANON ; //non-canonical mode (individual inputs - not a line-at-a-time) //Set Control Modes //TSET.c_cflag |= CLOCAL; //Local Terminal Connection (non-modem) @@ -369,7 +370,7 @@ void TTYProcess::setupTtyFd(int fd){ void TTYProcess::checkStatus(int sock){ //This is run when the socket gets activated if(sock!=ttyfd){ - + } //Make sure the child PID is still active int junk; diff --git a/src-qt5/experimental/lumina-terminal/TtyProcess.h b/src-qt5/experimental/lumina-terminal/TtyProcess.h index 42684112..ae5e3f7a 100644 --- a/src-qt5/experimental/lumina-terminal/TtyProcess.h +++ b/src-qt5/experimental/lumina-terminal/TtyProcess.h @@ -52,7 +52,7 @@ public: //Status update checks bool isOpen(); - + //Functions for handling ANSI escape codes (typically not used by hand) QByteArray CleanANSI(QByteArray, bool &incomplete); @@ -63,7 +63,7 @@ private: QByteArray fragBA; //fragment ByteArray bool starting; int fixReply; //flag for detecting particular inputs and "fixing" the reply to it as needed - + //==================================== // C Library function for setting up the PTY // Inputs: @@ -73,7 +73,7 @@ private: // Returns: // -1 for errors, child process PID (positive integer) if successful //==================================== - pid_t LaunchProcess(int& fd, char *prog, char **child_args); + pid_t LaunchProcess(int& fd, char *prog, char **child_args); void setupTtyFd(pid_t fd); private slots: |