aboutsummaryrefslogtreecommitdiff
path: root/desktop-utilities/lumina-terminal/TerminalWidget.cpp
blob: 5f2e581d4c7df1dd4098fd97444c14fddec00223 (plain)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
//===========================================
//  Lumina-DE source code
//  Copyright (c) 2015, Ken Moore
//  Available under the 3-clause BSD license
//  See the LICENSE file for full details
//===========================================
#include "TerminalWidget.h"

#include <QProcessEnvironment>
#include <QDebug>
#include <QApplication>
#include <QScrollBar>

TerminalWidget::TerminalWidget(QWidget *parent, QString dir) : QTextEdit(parent){
  //Setup the text widget
  this->setLineWrapMode(QTextEdit::WidgetWidth);
  this->setStyleSheet("");
  DEFFMT = this->textCursor().charFormat(); //save the default structure for later
  CFMT = this->textCursor().charFormat(); //current format
  
  //Create/open the TTY port
  PROC = new TTYProcess(this);
  qDebug() << "Open new TTY";
  //int fd;
  bool ok = PROC->startTTY( QProcessEnvironment::systemEnvironment().value("SHELL","/bin/sh") );
  qDebug() << " - opened:" << ok;
  this->setEnabled(PROC->isOpen());

  //Connect the signals/slots
  connect(PROC, SIGNAL(readyRead()), this, SLOT(UpdateText()) );
  connect(PROC, SIGNAL(processClosed()), this, SLOT(ShellClosed()) );
  
}

TerminalWidget::~TerminalWidget(){
  aboutToClose();
}

void TerminalWidget::aboutToClose(){
  if(PROC->isOpen()){ PROC->closeTTY(); } //TTY PORT
}

// ==================
//          PRIVATE
// ==================
void TerminalWidget::applyData(QByteArray data){
  //Iterate through the data and apply it when possible
  for(int i=0; i<data.size(); i++){
    if( data.at(i)=='\b' ){
      //Simple cursor backward 1 (NOT backspace in this context!!)
      //this->textCursor().deletePreviousChar();
      this->moveCursor(QTextCursor::Left, QTextCursor::MoveAnchor);
    }else if( data.at(i)=='\x1B' ){
      //ANSI Control Code start
      //Look for the end of the code
      int end = -1;
      for(int j=1; j<(data.size()-i) && end<0; j++){
        if(QChar(data.at(i+j)).isLetter()){ end = j; }
      }
      if(end<0){ return; } //skip everything else - no end to code found
      applyANSI(data.mid(i+1, end));
      i+=end; //move the final loop along - already handled these bytes
      
    }else if( data.at(i) != '\r' ) {
      //Special Check: if inserting text within a line, clear the rest of this line first
      if(i==0 && this->textCursor().position() < this->document()->characterCount()-1){
        applyANSI("[K");
      }
      //Plaintext character - just add it here
      //qDebug() << "Insert Text:" << data.at(i) << CFMT.fontWeight();
      this->textCursor().insertText( QChar(data.at(i)), CFMT );
    }
    
  } //end loop over data
}

void TerminalWidget::applyANSI(QByteArray code){
  //Note: the first byte is often the "[" character
  //qDebug() << "Handle ANSI:" << code;
	
  //CURSOR MOVEMENT
  if( code.endsWith("A") ){ //Move Up
    int num = 1;
    if(code.size()>2){ num = code.mid(1, code.size()-2).toInt(); } //everything in the middle
    this->textCursor().movePosition(QTextCursor::Up, QTextCursor::MoveAnchor, num);
  }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
    this->textCursor().movePosition(QTextCursor::Down, QTextCursor::MoveAnchor, num);
  }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
    for(int i=0; i<num; i++){ this->moveCursor(QTextCursor::Right, QTextCursor::MoveAnchor); }
    //this->textCursor().movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, num);
  }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
    for(int i=0; i<num; i++){ this->moveCursor(QTextCursor::Left, QTextCursor::MoveAnchor); }
    //this->textCursor().movePosition(QTextCursor::Left, QTextCursor::MoveAnchor, num);
  }else if(code.endsWith("E")){ //Move Next/down Lines (go to beginning)
    int num = 1;
    if(code.size()>2){ num = code.mid(1, code.size()-2).toInt(); } //everything in the middle
    this->textCursor().movePosition(QTextCursor::NextRow, QTextCursor::MoveAnchor, num);
  }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
    this->textCursor().movePosition(QTextCursor::PreviousRow, QTextCursor::MoveAnchor, num);
  }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
    this->textCursor().setPosition(num);
  }else if(code.endsWith("H") || code.endsWith("f") ){ //Move to specific position (row/column)
    int mid = code.indexOf(";");
    if(mid>0){
      int numR, numC; numR = numC = 1;
      if(mid >=3){ numR = code.mid(1,mid-1).toInt(); }
      if(mid < code.size()-1){ numC = code.mid(mid+1,code.size()-mid-1).toInt(); }
      //this->textCursor().setPosition(
      qDebug() << "Set Text Position (absolute):" << "Row:" << numR << "Col:" << numC;
      // TO-DO
    }
    
   // DISPLAY CLEAR CODES
  }else if(code.endsWith("J")){ //ED - Erase Display
    int num = 0;
    if(code.size()>2){ num = code.mid(1, code.size()-2).toInt(); } //everything in the middle
    if(num==1){
      //Clear from cursor to beginning of screen
      for(int i=this->textCursor().position(); i>=0; i--){
        this->textCursor().deletePreviousChar();
      }
    }else if(num==2){
      //Clear the whole screen
      this->clear();
    }else{
      //Clear from cursor to end of screen
      for(int i=this->textCursor().position(); i<this->document()->characterCount()+1; i++){
        this->textCursor().deleteChar();
      } 
    }	    
  }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
    //qDebug() << "Erase Number" << num;
    //Now determine what should be cleared based on code
    if(num==1){
      //Clear from current cursor to beginning of line
      for(int i=this->textCursor().position(); i>=0; i--){
	if(this->document()->characterAt(this->textCursor().position())=='\n'){ break; }
        this->textCursor().deleteChar();
      }	    
    }else if(num==2){
      //Clear the entire line
      //  rewind the starting point to the beginning of the line
      int start = this->document()->find("\n", this->textCursor().position(), QTextDocument::FindBackward).position();
      for(int i=start+1; i<this->document()->characterCount()+1; i++){
	if(this->document()->characterAt(this->textCursor().position())=='\n'){ break; }
        this->textCursor().deleteChar();
      }
    }else{
      //Clear from current cursor to end of line
      for(int i=this->textCursor().position(); i<this->document()->characterCount()+1; i++){
	if(this->document()->characterAt(this->textCursor().position())=='\n'){ break; }
        this->textCursor().deleteChar();
      }
    }
    
   //SCROLL MOVEMENT CODES 
  }else if(code.endsWith("S")){ // SU - Scrolll Up
	  
  }else if(code.endsWith("T")){ // SD - Scroll Down
	  
	  
  // GRAPHICS RENDERING
  }else if(code.endsWith("m")){
    //Format: "[<number>;<number>m" (no limit to sections separated by ";")
    int start = 1;
    int end = code.indexOf(";");
    while(end>start){
      applyANSIColor(code.mid(start, end-start).toInt());
      //Now update the iterators and try again
      start = end;
      end = code.indexOf(";",start+1); //go to the next one
    }
    //Need the last section as well
    end = code.size()-1;
    if(end>start){ applyANSIColor(code.mid(start, end-start).toInt());}
    else{ applyANSIColor(0); }
  }
  
  
}

void TerminalWidget::applyANSIColor(int code){
  qDebug() << "Apply Color code:" << code;
  if(code <=0){ CFMT = DEFFMT; } //Reset back to default
  else if(code==1){  CFMT.setFontWeight(75); } //BOLD font
  else if(code==2){ CFMT.setFontWeight(25); } //Faint font (smaller than normal by a bit)
  else if(code==3){ CFMT.setFontWeight(75); } //Italic font
  else if(code==4){ CFMT.setFontUnderline(true); } //Underline
  //5-6: Blink text (unsupported)
  //7: Reverse foreground/background (unsupported)
  //8: Conceal (unsupported)
  else if(code==9){ CFMT.setFontStrikeOut(true); } //Crossed out
  //10-19: Change font family (unsupported)
  //20: Fraktur Font (unsupported)
  //21: Bold:off or Underline:Double (unsupported)
  else if(code==22){ CFMT.setFontWeight(50); } //Normal weight
  //23: Reset font (unsupported)
  else if(code==24){ CFMT.setFontUnderline(false); } //disable underline
  //25: Disable blinking (unsupported)
  //26: Reserved
  //27: Reset reversal (7) (unsupported)
  //28: Reveal (cancel 8) (unsupported)
  else if(code==29){ CFMT.setFontStrikeOut(false); } //Not Crossed out
  else if(code>=30 && code<=39){
    //Set the font color
    QBrush brush = CFMT.foreground();
    if(code==30){ brush.setColor(Qt::black); }
    else if(code==31){ brush.setColor(Qt::red); }
    else if(code==32){ brush.setColor(Qt::green); }
    else if(code==33){ brush.setColor(Qt::yellow); }
    else if(code==34){ brush.setColor(Qt::blue); }
    else if(code==35){ brush.setColor(Qt::magenta); }
    else if(code==36){ brush.setColor(Qt::cyan); }
    else if(code==37){ brush.setColor(Qt::white); }
    //48: Special extended color setting (unsupported)
    else if(code==39){ brush.setColor( DEFFMT.foreground().color() ); } //reset to default color
    CFMT.setForeground( brush );
  }
  else if(code>=40 && code<=49){
    //Set the font color
    QBrush brush = CFMT.background();
    if(code==40){ brush.setColor(Qt::black); }
    else if(code==41){ brush.setColor(Qt::red); }
    else if(code==42){ brush.setColor(Qt::green); }
    else if(code==43){ brush.setColor(Qt::yellow); }
    else if(code==44){ brush.setColor(Qt::blue); }
    else if(code==45){ brush.setColor(Qt::magenta); }
    else if(code==46){ brush.setColor(Qt::cyan); }
    else if(code==47){ brush.setColor(Qt::white); }
    //48: Special extended color setting (unsupported)
    else if(code==49){ brush.setColor( DEFFMT.background().color() ); } //reset to default color
    CFMT.setBackground( brush );
  }
  
}

//Outgoing Data parsing
void TerminalWidget::sendKeyPress(int key){
  QByteArray ba;
  //Check for special keys
  switch(key){
    case Qt::Key_Delete:
	ba.append("\x7F");
        break;
    case Qt::Key_Backspace:
	ba.append("\x08");    
        break;
    case Qt::Key_Left:
	ba.append("\x1b[D");
        break;
    case Qt::Key_Right:
	ba.append("\x1b[C");
        break;
    case Qt::Key_Up:
        ba.append("\x1b[A");
        break;
    case Qt::Key_Down:
        ba.append("\x1b[B");
        break;
  }
   
  //qDebug() << "Forward Input:" << txt << ev->key() << ba;
  if(!ba.isEmpty()){ PROC->writeTTY(ba); }
}

// ==================
//    PRIVATE SLOTS
// ==================
void TerminalWidget::UpdateText(){
  //read the data from the process
  //qDebug() << "UpdateText";
  if(!PROC->isOpen()){ return; }
  applyData(PROC->readTTY());
  //adjust the scrollbar as needed
  this->verticalScrollBar()->setValue(this->verticalScrollBar()->maximum());
}

void TerminalWidget::ShellClosed(){
  emit ProcessClosed(this->whatsThis());
}

// ==================
//       PROTECTED
// ==================
void TerminalWidget::keyPressEvent(QKeyEvent *ev){
	
  if(ev->text().isEmpty() || ev->text()=="\b" ){
    sendKeyPress(ev->key());
  }else{
    QByteArray ba; ba.append(ev->text()); //avoid any byte conversions
    PROC->writeTTY(ba);
  }
  
  ev->ignore();
}

void TerminalWidget::mousePressEvent(QMouseEvent *ev){
  this->setFocus();
  Q_UNUSED(ev);
}

void TerminalWidget::mouseDoubleClickEvent(QMouseEvent *ev){
  Q_UNUSED(ev);	
}

void TerminalWidget::contextMenuEvent(QContextMenuEvent *ev){
  Q_UNUSED(ev);	
}

void TerminalWidget::resizeEvent(QResizeEvent *ev){
  if(!PROC->isOpen()){ return; }
  QSize pix = ev->size(); //pixels
  QSize chars; 
    chars.setWidth( pix.width()/this->fontMetrics().width("W") );
    chars.setHeight( pix.height()/this->fontMetrics().lineSpacing() );
  
  PROC->setTerminalSize(chars,pix);
  QTextEdit::resizeEvent(ev);
}
bgstack15