Skip to content

Latest commit

 

History

History
1504 lines (1323 loc) · 43.9 KB

terminal.cpp

File metadata and controls

1504 lines (1323 loc) · 43.9 KB
 
Feb 20, 2013
Feb 20, 2013
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*
Copyright 2011-2012 Heikki Holstila <heikki.holstila@gmail.com>
This file is part of FingerTerm.
FingerTerm is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
FingerTerm 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with FingerTerm. If not, see <http://www.gnu.org/licenses/>.
*/
Jul 22, 2013
Jul 22, 2013
20
#include <QGuiApplication>
Feb 20, 2013
Feb 20, 2013
21
#include <QClipboard>
Aug 7, 2013
Aug 7, 2013
22
#include <QDebug>
Feb 20, 2013
Feb 20, 2013
23
24
25
26
27
28
#include "terminal.h"
#include "ptyiface.h"
#include "textrender.h"
#include "util.h"
Jun 27, 2016
Jun 27, 2016
29
30
31
32
33
34
35
36
37
38
39
40
static bool charIsHexDigit(QChar ch)
{
if (ch.isDigit()) // 0-9
return true;
else if (ch.toLatin1() >= 65 && ch.toLatin1() <= 70) // A-F
return true;
else if (ch.toLatin1() >= 97 && ch.toLatin1() <= 102) // a-f
return true;
return false;
}
Feb 20, 2013
Feb 20, 2013
41
Terminal::Terminal(QObject *parent) :
Jun 8, 2016
Jun 8, 2016
42
QObject(parent), iPtyIFace(0), iWindow(0), iUtil(0),
Feb 20, 2013
Feb 20, 2013
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
iTermSize(0,0), iEmitCursorChangeSignal(true),
iShowCursor(true), iUseAltScreenBuffer(false), iAppCursorKeys(false)
{
zeroChar.c = ' ';
zeroChar.bgColor = defaultBgColor;
zeroChar.fgColor = defaultFgColor;
zeroChar.attrib = 0;
escape = -1;
iTermAttribs.currentFgColor = defaultFgColor;
iTermAttribs.currentBgColor = defaultBgColor;
iTermAttribs.currentAttrib = 0;
iTermAttribs.cursorPos = QPoint(0,0);
iMarginBottom = 0;
iMarginTop = 0;
resetBackBufferScrollPos();
iTermAttribs_saved = iTermAttribs;
iTermAttribs_saved_alt = iTermAttribs;
resetTerminal();
}
void Terminal::setPtyIFace(PtyIFace *pty)
{
iPtyIFace = pty;
if(!pty) {
qDebug() << "warning: null pty iface";
}
}
void Terminal::setCursorPos(QPoint pos)
{
if( iTermAttribs.cursorPos != pos ) {
int tlimit = 1;
int blimit = iTermSize.height();
if(iTermAttribs.originMode) {
tlimit = iMarginTop;
blimit = iMarginBottom;
}
if(pos.x() < 1)
pos.setX(1);
if(pos.x() > iTermSize.width()+1)
pos.setX(iTermSize.width()+1);
if(pos.y() < tlimit)
pos.setY(tlimit);
if(pos.y() > blimit)
pos.setY(blimit);
iTermAttribs.cursorPos=pos;
if(iEmitCursorChangeSignal)
emit cursorPosChanged(pos);
}
}
QPoint Terminal::cursorPos()
{
return iTermAttribs.cursorPos;
}
bool Terminal::showCursor()
{
if(iBackBufferScrollPos != 0)
return false;
return iShowCursor;
}
QList<QList<TermChar> >& Terminal::buffer()
{
if(iUseAltScreenBuffer)
return iAltBuffer;
return iBuffer;
}
void Terminal::setTermSize(QSize size)
{
if( iTermSize != size ) {
iMarginTop = 1;
iMarginBottom = size.height();
iTermSize=size;
resetTabs();
emit termSizeChanged(size);
}
}
void Terminal::putString(QString str, bool unEscape)
{
if (unEscape) {
str.replace("\\r", "\r");
str.replace("\\n", "\n");
str.replace("\\e", QChar(ch_ESC));
str.replace("\\b", "\b");
str.replace("\\t", "\t");
Feb 22, 2013
Feb 22, 2013
144
//hex
Feb 20, 2013
Feb 20, 2013
145
146
147
while(str.indexOf("\\x") != -1) {
int i = str.indexOf("\\x")+2;
QString num;
Jun 27, 2016
Jun 27, 2016
148
while(num.length() < 2 && str.length()>i && charIsHexDigit(str.at(i))) {
Feb 20, 2013
Feb 20, 2013
149
150
151
152
153
154
155
num.append(str.at(i));
i++;
}
str.remove(i-2-num.length(), num.length()+2);
bool ok;
str.insert(i-2-num.length(), QChar(num.toInt(&ok,16)));
}
Feb 22, 2013
Feb 22, 2013
156
//octal
Feb 20, 2013
Feb 20, 2013
157
158
159
while(str.indexOf("\\0") != -1) {
int i = str.indexOf("\\0")+2;
QString num;
Feb 22, 2013
Feb 22, 2013
160
while(num.length() < 3 && str.length()>i &&
Jun 16, 2013
Jun 16, 2013
161
(str.at(i).toLatin1() >= 48 && str.at(i).toLatin1() <= 55)) { //accept only 0-7
Feb 20, 2013
Feb 20, 2013
162
163
164
165
166
167
168
169
170
171
172
173
174
num.append(str.at(i));
i++;
}
str.remove(i-2-num.length(), num.length()+2);
bool ok;
str.insert(i-2-num.length(), QChar(num.toInt(&ok,8)));
}
}
if(iPtyIFace)
iPtyIFace->writeTerm(str);
}
Oct 14, 2015
Oct 14, 2015
175
void Terminal::keyPress(int key, int modifiers, const QString& text)
Feb 20, 2013
Feb 20, 2013
176
{
Oct 14, 2015
Oct 14, 2015
177
QString toWrite;
Feb 20, 2013
Feb 20, 2013
178
179
180
resetBackBufferScrollPos();
Oct 14, 2015
Oct 14, 2015
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
if (key > 0xFFFF) {
int modcode = (modifiers & Qt::ShiftModifier ? 1 : 0) |
(modifiers & Qt::AltModifier ? 2 : 0) |
(modifiers & Qt::ControlModifier ? 4 : 0);
if (modcode == 0) {
QString fmt;
char cursorModif='[';
if(iAppCursorKeys)
cursorModif = 'O';
if( key==Qt::Key_Up ) fmt = QString("%2%1A").arg(cursorModif);
if( key==Qt::Key_Down ) fmt = QString("%2%1B").arg(cursorModif);
if( key==Qt::Key_Right ) fmt = QString("%2%1C").arg(cursorModif);
if( key==Qt::Key_Left ) fmt = QString("%2%1D").arg(cursorModif);
if( key==Qt::Key_PageUp ) fmt = "%1[5~";
if( key==Qt::Key_PageDown ) fmt = "%1[6~";
if( key==Qt::Key_Home ) fmt = "%1OH";
if( key==Qt::Key_End ) fmt = "%1OF";
if( key==Qt::Key_Insert ) fmt = "%1[2~";
if( key==Qt::Key_Delete ) fmt = "%1[3~";
if( key==Qt::Key_F1 ) fmt = "%1OP";
if( key==Qt::Key_F2 ) fmt = "%1OQ";
if( key==Qt::Key_F3 ) fmt = "%1OR";
if( key==Qt::Key_F4 ) fmt = "%1OS";
if( key==Qt::Key_F5 ) fmt = "%1[15~";
if( key==Qt::Key_F6 ) fmt = "%1[17~";
if( key==Qt::Key_F7 ) fmt = "%1[18~";
if( key==Qt::Key_F8 ) fmt = "%1[19~";
if( key==Qt::Key_F9 ) fmt = "%1[20~";
if( key==Qt::Key_F10 ) fmt = "%1[21~";
if( key==Qt::Key_F11 ) fmt = "%1[23~";
if( key==Qt::Key_F12 ) fmt = "%1[24~";
if (!fmt.isEmpty())
toWrite += fmt.arg(ch_ESC);
Feb 20, 2013
Feb 20, 2013
218
Oct 14, 2015
Oct 14, 2015
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
} else {
QString fmt;
char modChar = '1' + modcode;
if( key==Qt::Key_Up ) fmt = "%1[1;%2A";
if( key==Qt::Key_Down ) fmt = "%1[1;%2B";
if( key==Qt::Key_Right ) fmt = "%1[1;%2C";
if( key==Qt::Key_Left ) fmt = "%1[1;%2D";
if( key==Qt::Key_PageUp ) fmt = "%1[5;%2~";
if( key==Qt::Key_PageDown ) fmt = "%1[6;%2~";
if( key==Qt::Key_Home ) fmt = "%1[1;%2H";
if( key==Qt::Key_End ) fmt = "%1[1;%2F";
if( key==Qt::Key_Insert ) fmt = "%1[2;%2~";
if( key==Qt::Key_Delete ) fmt = "%1[3;%2~";
if( key==Qt::Key_F1 ) fmt = "%1[1;%2P";
if( key==Qt::Key_F2 ) fmt = "%1[1;%2Q";
if( key==Qt::Key_F3 ) fmt = "%1[1;%2R";
if( key==Qt::Key_F4 ) fmt = "%1[1;%2S";
if( key==Qt::Key_F5 ) fmt = "%1[15;%2~";
if( key==Qt::Key_F6 ) fmt = "%1[17;%2~";
if( key==Qt::Key_F7 ) fmt = "%1[18;%2~";
if( key==Qt::Key_F8 ) fmt = "%1[19;%2~";
if( key==Qt::Key_F9 ) fmt = "%1[20;%2~";
if( key==Qt::Key_F10 ) fmt = "%1[21;%2~";
if( key==Qt::Key_F11 ) fmt = "%1[23;%2~";
if( key==Qt::Key_F12 ) fmt = "%1[24;%2~";
if (!fmt.isEmpty())
toWrite += fmt.arg(ch_ESC).arg(modChar);
Feb 20, 2013
Feb 20, 2013
249
Jun 2, 2015
Jun 2, 2015
250
}
Feb 20, 2013
Feb 20, 2013
251
Oct 14, 2015
Oct 14, 2015
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
if( key==Qt::Key_Enter || key==Qt::Key_Return ) {
if ( (modifiers & (Qt::ShiftModifier | Qt::ControlModifier)) ==
(Qt::ShiftModifier | Qt::ControlModifier) )
toWrite += QChar(0x9E);
else if (modifiers & Qt::ControlModifier)
toWrite += QChar(0x1E); // ^^
else if (modifiers & Qt::ShiftModifier)
toWrite += "\n";
else if(iNewLineMode)
toWrite += "\r\n";
else
toWrite += "\r";
}
if( key==Qt::Key_Backspace ) {
if ( (modifiers & (Qt::ShiftModifier | Qt::ControlModifier)) ==
(Qt::ShiftModifier | Qt::ControlModifier) )
toWrite += QChar(0x9F);
else if (modifiers & Qt::ControlModifier)
toWrite += QChar(0x1F); // ^_
else
toWrite += "\x7F";
}
if( key==Qt::Key_Tab || key==Qt::Key_Backtab ) {
if ( key == Qt::Key_Backtab ) modifiers |= Qt::ShiftModifier;
if (modifiers & Qt::ControlModifier) {
char modChar = '5' + (modifiers & Qt::ShiftModifier ? 1 : 0);
toWrite += QString("%1[1;%2I").arg(ch_ESC).arg(modChar);
} else if (modifiers & Qt::ShiftModifier) {
toWrite += QString("%1[Z").arg(ch_ESC);
Jun 2, 2015
Jun 2, 2015
281
} else {
Oct 14, 2015
Oct 14, 2015
282
toWrite += "\t";
Jun 2, 2015
Jun 2, 2015
283
284
}
}
Feb 20, 2013
Feb 20, 2013
285
Oct 14, 2015
Oct 14, 2015
286
287
288
289
290
291
292
293
294
295
296
297
if( key==Qt::Key_Escape ) {
if (modifiers & Qt::ShiftModifier)
toWrite += QChar(0x9B);
else
toWrite += QString(1,ch_ESC);
}
if (!toWrite.isEmpty()) {
if(iPtyIFace)
iPtyIFace->writeTerm(toWrite);
} else {
qDebug() << "unknown special key: " << key;
Jun 2, 2015
Jun 2, 2015
298
}
Feb 20, 2013
Feb 20, 2013
299
300
301
return;
}
Oct 14, 2015
Oct 14, 2015
302
303
304
305
QChar c(key);
if (c.isLetter()) {
c = ((modifiers & Qt::ShiftModifier) != 0) ? c.toUpper() : c.toLower();
Feb 20, 2013
Feb 20, 2013
306
}
Oct 14, 2015
Oct 14, 2015
307
308
309
if((modifiers & Qt::AltModifier) != 0) {
toWrite.append(ch_ESC);
Feb 20, 2013
Feb 20, 2013
310
311
}
Oct 14, 2015
Oct 14, 2015
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
if ((modifiers & Qt::ControlModifier) != 0) {
char asciiVal = c.toUpper().toLatin1();
if (asciiVal >= 0x41 && asciiVal <= 0x5f) {
// Turn uppercase characters into their control code equivalent
toWrite.append(asciiVal - 0x40);
} else {
qWarning() << "Ctrl+" << c << " does not translate into a control code";
}
} else {
if (text.isEmpty()) {
toWrite.append(c);
} else {
toWrite.append(text);
}
}
if (iPtyIFace) {
Feb 20, 2013
Feb 20, 2013
330
iPtyIFace->writeTerm(toWrite);
Oct 14, 2015
Oct 14, 2015
331
332
}
return;
Feb 20, 2013
Feb 20, 2013
333
334
335
336
337
338
339
340
341
342
343
344
345
}
void Terminal::insertInBuffer(const QString& chars)
{
if(iTermSize.isNull()) {
qDebug() << "null size terminal";
return;
}
iEmitCursorChangeSignal = false;
for(int i=0; i<chars.size(); i++) {
QChar ch = chars.at(i);
Jun 16, 2013
Jun 16, 2013
346
if(ch.toLatin1()=='\n' || ch.toLatin1()==11 || ch.toLatin1()==12) { // line feed, vertical tab or form feed
Feb 20, 2013
Feb 20, 2013
347
348
349
350
351
352
353
354
355
356
357
358
359
if(cursorPos().y()==iMarginBottom) {
scrollFwd(1);
if(iNewLineMode)
setCursorPos(QPoint(1,cursorPos().y()));
}
else if(cursorPos().x() <= termSize().width()) // ignore newline after <termwidth> cols (terminfo: xenl)
{
if(iNewLineMode)
setCursorPos(QPoint(1,cursorPos().y()+1));
else
setCursorPos(QPoint(cursorPos().x(), cursorPos().y()+1));
}
}
Jun 16, 2013
Jun 16, 2013
360
else if(ch.toLatin1()=='\r') { // carriage return
Feb 20, 2013
Feb 20, 2013
361
362
setCursorPos(QPoint(1,cursorPos().y()));
}
Jun 16, 2013
Jun 16, 2013
363
else if(ch.toLatin1()=='\b' || ch.toLatin1()==127) { //backspace & del (only move cursor, don't erase)
Feb 20, 2013
Feb 20, 2013
364
365
setCursorPos(QPoint(cursorPos().x()-1,cursorPos().y()));
}
Jun 16, 2013
Jun 16, 2013
366
else if(ch.toLatin1()=='\a') { // BEL
Feb 20, 2013
Feb 20, 2013
367
368
369
370
371
372
373
374
if(escape==']') { // BEL also ends OSC sequence
escape=-1;
oscSequence(oscSeq);
oscSeq.clear();
} else {
iUtil->bellAlert();
}
}
Jun 16, 2013
Jun 16, 2013
375
else if(ch.toLatin1()=='\t') { //tab
Feb 20, 2013
Feb 20, 2013
376
377
378
379
380
381
382
383
384
if(cursorPos().y() <= iTabStops.size()) {
for(int i=0; i<iTabStops[cursorPos().y()-1].count(); i++) {
if(iTabStops[cursorPos().y()-1][i] > cursorPos().x()) {
setCursorPos(QPoint( iTabStops[cursorPos().y()-1][i], cursorPos().y() ));
break;
}
}
}
}
Jun 16, 2013
Jun 16, 2013
385
else if(ch.toLatin1()==14 || ch.toLatin1()==15) { //SI and SO, related to character set... ignore
Feb 20, 2013
Feb 20, 2013
386
387
388
}
else {
if( escape>=0 ) {
Jun 16, 2013
Jun 16, 2013
389
if( escape==0 && (ch.toLatin1()=='[') ) {
Feb 20, 2013
Feb 20, 2013
390
391
392
escape='['; //ansi sequence
escSeq += ch;
}
Jun 16, 2013
Jun 16, 2013
393
else if( escape==0 && (ch.toLatin1()==']') ) {
Feb 20, 2013
Feb 20, 2013
394
395
396
escape=']'; //osc sequence
oscSeq += ch;
}
Jun 16, 2013
Jun 16, 2013
397
398
else if( escape==0 && multiCharEscapes.contains(ch.toLatin1())) {
escape = ch.toLatin1();
Feb 20, 2013
Feb 20, 2013
399
400
escSeq += ch;
}
Jun 16, 2013
Jun 16, 2013
401
else if( escape==0 && ch.toLatin1()=='\\' ) { // ESC\ also ends OSC sequence
Feb 20, 2013
Feb 20, 2013
402
403
404
405
escape=-1;
oscSequence(oscSeq);
oscSeq.clear();
}
Jun 16, 2013
Jun 16, 2013
406
else if (ch.toLatin1()==ch_ESC) {
Feb 20, 2013
Feb 20, 2013
407
408
409
410
411
412
413
414
415
416
417
418
escape = 0;
}
else if( escape=='[' || multiCharEscapes.contains(escape) ) {
escSeq += ch;
}
else if( escape==']' ) {
oscSeq += ch;
}
else if( multiCharEscapes.contains(escape) ) {
escSeq += ch;
}
else {
Jun 16, 2013
Jun 16, 2013
419
escControlChar(QByteArray(1,ch.toLatin1()));
Feb 20, 2013
Feb 20, 2013
420
421
422
escape=-1;
}
Jun 16, 2013
Jun 16, 2013
423
if( escape=='[' && ch.toLatin1() >= 64 && ch.toLatin1() <= 126 && ch.toLatin1() != '[' ) {
Feb 20, 2013
Feb 20, 2013
424
425
426
427
428
429
430
431
432
433
434
435
ansiSequence(escSeq);
escape=-1;
escSeq.clear();
}
if( multiCharEscapes.contains(escape) && escSeq.length()>=2 ) {
escControlChar(escSeq);
escape=-1;
escSeq.clear();
}
} else {
if (ch.isPrint())
insertAtCursor(ch, !iReplaceMode);
Jun 16, 2013
Jun 16, 2013
436
else if (ch.toLatin1()==ch_ESC)
Feb 20, 2013
Feb 20, 2013
437
escape=0;
Oct 28, 2013
Oct 28, 2013
438
else if (ch.toLatin1() != 0)
Jun 16, 2013
Jun 16, 2013
439
qDebug() << "unprintable char" << int(ch.toLatin1());
Feb 20, 2013
Feb 20, 2013
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
}
}
}
iEmitCursorChangeSignal = true;
emit displayBufferChanged();
}
void Terminal::insertAtCursor(QChar c, bool overwriteMode, bool advanceCursor)
{
if(cursorPos().x() > iTermSize.width() && advanceCursor) {
if(iTermAttribs.wrapAroundMode) {
if(cursorPos().y()>=iMarginBottom) {
scrollFwd(1);
setCursorPos(QPoint(1, cursorPos().y()));
} else {
setCursorPos(QPoint(1, cursorPos().y()+1));
}
} else {
setCursorPos(QPoint(iTermSize.width(), cursorPos().y()));
}
}
while(currentLine().size() < cursorPos().x() )
currentLine().append(zeroChar);
if(!overwriteMode)
currentLine().insert(cursorPos().x()-1,zeroChar);
currentLine()[cursorPos().x()-1].c = c;
currentLine()[cursorPos().x()-1].fgColor = iTermAttribs.currentFgColor;
currentLine()[cursorPos().x()-1].bgColor = iTermAttribs.currentBgColor;
currentLine()[cursorPos().x()-1].attrib = iTermAttribs.currentAttrib;
if (advanceCursor) {
setCursorPos(QPoint(cursorPos().x()+1,cursorPos().y()));
}
}
void Terminal::deleteAt(QPoint pos)
{
clearAt(pos);
buffer()[pos.y()-1].removeAt(pos.x()-1);
}
void Terminal::clearAt(QPoint pos)
{
if(pos.y() <= 0 || pos.y()-1 > buffer().size() ||
pos.x() <= 0 || pos.x()-1 > buffer()[pos.y()-1].size())
{
qDebug() << "warning: trying to clear char out of bounds";
return;
}
// just in case...
while(buffer().size() < pos.y())
buffer().append(QList<TermChar>());
while(buffer()[pos.y()-1].size() < pos.x() )
buffer()[pos.y()-1].append(zeroChar);
buffer()[pos.y()-1][pos.x()-1] = zeroChar;
}
void Terminal::eraseLineAtCursor(int from, int to)
{
if(from==-1 && to==-1) {
currentLine().clear();
return;
}
if(from < 1)
from=1;
from--;
if(to < 1 || to > currentLine().size())
to=currentLine().size();
to--;
if(from>to)
return;
for(int i=from; i<=to; i++) {
currentLine()[i] = zeroChar;
}
}
void Terminal::clearAll(bool wholeBuffer)
{
clearSelection();
if(wholeBuffer) {
backBuffer().clear();
resetBackBufferScrollPos();
}
buffer().clear();
setCursorPos(QPoint(1,1));
}
void Terminal::ansiSequence(const QString& seq)
{
if(seq.length() <= 1 || seq.at(0)!='[')
return;
QChar cmdChar = seq.at(seq.length()-1);
QString extra;
QList<int> params;
int x=1;
while(x<seq.length()-1 && !QChar(seq.at(x)).isNumber())
x++;
QList<QString> tmp = seq.mid(x,seq.length()-x-1).split(';');
foreach(QString b, tmp) {
bool ok=false;
int t = b.toInt(&ok);
if(ok) {
params.append(t);
}
}
if(x>1)
extra = seq.mid(1,x-1);
bool unhandled = false;
Jun 16, 2013
Jun 16, 2013
563
switch(cmdChar.toLatin1())
Feb 20, 2013
Feb 20, 2013
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
{
case 'A': //cursor up
if(!extra.isEmpty()) {
unhandled=true;
break;
}
if(params.count()<1)
params.append(1);
if(params.at(0)==0)
params[0]=1;
setCursorPos(QPoint( cursorPos().x(), qMax(iMarginTop, cursorPos().y()-params.at(0)) ));
break;
case 'B': //cursor down
if(!extra.isEmpty()) {
unhandled=true;
break;
}
if(params.count()<1)
params.append(1);
if(params.at(0)==0)
params[0]=1;
setCursorPos(QPoint( cursorPos().x(), qMin(iMarginBottom, cursorPos().y()+params.at(0)) ));
break;
case 'C': //cursor fwd
if(!extra.isEmpty()) {
unhandled=true;
break;
}
if(params.count()<1)
params.append(1);
if(params.at(0)==0)
params[0]=1;
setCursorPos(QPoint( qMin(iTermSize.width(),cursorPos().x()+params.at(0)), cursorPos().y() ));
break;
case 'D': //cursor back
if(!extra.isEmpty()) {
unhandled=true;
break;
}
if(params.count()<1)
params.append(1);
if(params.at(0)==0)
params[0]=1;
setCursorPos(QPoint( qMax(1,cursorPos().x()-params.at(0)), cursorPos().y() ));
break;
case 'E': //cursor next line
if(!extra.isEmpty()) {
unhandled=true;
break;
}
if(params.count()<1)
params.append(1);
if(params.at(0)==0)
params[0]=1;
setCursorPos(QPoint( 1, qMin(iMarginBottom, cursorPos().y()+params.at(0)) ));
break;
case 'F': //cursor prev line
if(!extra.isEmpty()) {
unhandled=true;
break;
}
if(params.count()<1)
params.append(1);
if(params.at(0)==0)
params[0]=1;
setCursorPos(QPoint( 1, qMax(iMarginTop, cursorPos().y()-params.at(0)) ));
break;
case 'G': //go to column
if(!extra.isEmpty()) {
unhandled=true;
break;
}
if(params.count()<1)
params.append(1);
if(params.at(0)==0)
params[0]=1;
setCursorPos(QPoint( params.at(0), cursorPos().y() ));
break;
case 'H': //cursor pos
case 'f': //cursor pos
if(!extra.isEmpty()) {
unhandled=true;
break;
}
while(params.count()<2)
params.append(1);
if (iTermAttribs.originMode)
setCursorPos(QPoint( params.at(1), params.at(0)+iMarginTop-1 ));
else
setCursorPos(QPoint( params.at(1), params.at(0) ));
break;
case 'J': //erase data
if(!extra.isEmpty() && extra!="?") {
unhandled=true;
break;
}
if(params.count()>=1 && params.at(0)==1) {
eraseLineAtCursor(1,cursorPos().x());
for(int i=0; i<cursorPos().y()-1; i++) {
buffer()[i].clear();
}
} else if(params.count()>=1 && params.at(0)==2) {
clearAll();
} else {
eraseLineAtCursor(cursorPos().x());
for(int i=cursorPos().y(); i<buffer().size(); i++)
buffer()[i].clear();
}
break;
case 'K': //erase in line
if(!extra.isEmpty() && extra!="?") {
unhandled=true;
break;
}
if(params.count()>=1 && params.at(0)==1) {
eraseLineAtCursor(1,cursorPos().x());
}
else if(params.count()>=1 && params.at(0)==2) {
currentLine().clear();
} else {
eraseLineAtCursor(cursorPos().x());
}
break;
case 'L': // insert lines
if(!extra.isEmpty()) {
unhandled=true;
break;
}
if(cursorPos().y() < iMarginTop || cursorPos().y() > iMarginBottom)
break;
if(params.count()<1)
params.append(1);
if(params.at(0)==0)
params[0]=1;
if(params.at(0) > iMarginBottom-cursorPos().y())
scrollBack(iMarginBottom-cursorPos().y(), cursorPos().y());
else
scrollBack(params.at(0), cursorPos().y());
setCursorPos(QPoint(1,cursorPos().y()));
break;
case 'M': // delete lines
if(!extra.isEmpty()) {
unhandled=true;
break;
}
if(cursorPos().y() < iMarginTop || cursorPos().y() > iMarginBottom)
break;
if(params.count()<1)
params.append(1);
if(params.at(0)==0)
params[0]=1;
if(params.at(0) > iMarginBottom-cursorPos().y())
scrollFwd(iMarginBottom-cursorPos().y(), cursorPos().y());
else
scrollFwd(params.at(0), cursorPos().y());
setCursorPos(QPoint(1,cursorPos().y()));
break;
case 'P': // delete characters
if(!extra.isEmpty()) {
unhandled=true;
break;
}
if(params.count()<1)
params.append(1);
if(params.at(0)==0)
params[0]=1;
for(int i=0; i<params.at(0); i++)
deleteAt(cursorPos());
break;
case '@': // insert blank characters
if(!extra.isEmpty()) {
unhandled=true;
break;
}
if(params.count()<1)
params.append(1);
if(params.at(0)==0)
params[0] = 1;
for(int i=1; i<=params.at(0); i++)
insertAtCursor(zeroChar.c, false, false);
break;
case 'S': // scroll up n lines
if(params.count()<1)
params.append(1);
if(params.at(0)==0)
params[0]=1;
scrollFwd(params.at(0));
break;
case 'T': // scroll down n lines
if(params.count()<1)
params.append(1);
if(params.at(0)==0)
params[0]=1;
scrollBack(params.at(0));
break;
case 'c': // vt100 identification
if(params.count()==0)
params.append(0);
if(params.count()==1 && params.at(0)==0) {
Jun 16, 2013
Jun 16, 2013
767
QString toWrite = QString("%1[?1;2c").arg(ch_ESC).toLatin1();
Feb 20, 2013
Feb 20, 2013
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
if(iPtyIFace)
iPtyIFace->writeTerm(toWrite);
} else unhandled=true;
break;
case 'd': //go to row
if(!extra.isEmpty()) {
unhandled=true;
break;
}
if(params.count()<1)
params.append(1);
if(params.at(0)==0)
params[0]=1;
setCursorPos(QPoint( cursorPos().x(), params.at(0) ));
break;
case 'g': //tab stop manipulation
if(params.count()==0)
params.append(0);
if(params.at(0)==0 && extra=="") { //clear tab at current position
if(cursorPos().y() <= iTabStops.size()) {
int idx = iTabStops[cursorPos().y()-1].indexOf(cursorPos().x());
if(idx != -1)
iTabStops[cursorPos().y()-1].removeAt(idx);
}
}
else if(params.at(0)==3 && extra=="") { //clear all tabs
iTabStops.clear();
}
break;
case 'n':
if(params.count()>=1 && params.at(0)==6 && extra=="") { // write cursor pos
Jun 16, 2013
Jun 16, 2013
802
QString toWrite = QString("%1[%2;%3R").arg(ch_ESC).arg(cursorPos().y()).arg(cursorPos().x()).toLatin1();
Feb 20, 2013
Feb 20, 2013
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
if(iPtyIFace)
iPtyIFace->writeTerm(toWrite);
} else unhandled=true;
break;
case 'p':
if(extra=="!") { // reset terminal
resetTerminal();
} else unhandled=true;
break;
case 's': //save cursor
if(!extra.isEmpty()) {
unhandled=true;
break;
}
iTermAttribs_saved = iTermAttribs;
break;
case 'u': //restore cursor
if(!extra.isEmpty()) {
unhandled=true;
break;
}
iTermAttribs = iTermAttribs_saved;
break;
case 'm': //graphics mode
if(!extra.isEmpty()) {
unhandled=true;
break;
}
if(params.count() > 0) {
Feb 27, 2014
Feb 27, 2014
835
836
837
// xterm 256-colour support
if(params.count() > 1 && (params[0] == 38 || params[0] == 48)) {
if(params.count() > 2 && params[1] == 5 &&
Feb 27, 2014
Feb 27, 2014
838
839
params[2] >= 0 && params[2] <= 255) {
if(params[0] == 38)
Feb 27, 2014
Feb 27, 2014
840
841
842
843
844
845
846
847
iTermAttribs.currentFgColor = params[2];
else
iTermAttribs.currentBgColor = params[2];
}
// TODO: 2;r;g;b for 24-bit colour support (Konsole etc)
break;
}
Feb 20, 2013
Feb 20, 2013
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
if(params.contains(0)) {
iTermAttribs.currentFgColor = defaultFgColor;
iTermAttribs.currentBgColor = defaultBgColor;
iTermAttribs.currentAttrib = attribNone;
}
if(params.contains(1))
iTermAttribs.currentAttrib |= attribBold;
if(params.contains(4))
iTermAttribs.currentAttrib |= attribUnderline;
if(params.contains(7))
iTermAttribs.currentAttrib |= attribNegative;
if(params.contains(22))
iTermAttribs.currentAttrib &= ~attribBold;
if(params.contains(24))
iTermAttribs.currentAttrib &= ~attribUnderline;
if(params.contains(27))
iTermAttribs.currentAttrib &= ~attribNegative;
foreach(int p, params) {
if(p >= 30 && p<= 37) {
iTermAttribs.currentFgColor = p-30;
}
if(p >= 40 && p<= 47) {
iTermAttribs.currentBgColor = p-40;
}
}
Feb 27, 2014
Feb 27, 2014
875
Feb 28, 2014
Feb 28, 2014
876
877
878
879
880
881
882
883
884
885
// high-intensity regular-weight extension (nonstandard)
foreach(int p, params) {
if(p >= 90 && p<= 97) {
iTermAttribs.currentFgColor = p-90+8;
}
if(p >= 100 && p<= 107) {
iTermAttribs.currentBgColor = p-100+8;
}
}
Feb 20, 2013
Feb 20, 2013
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
if(params.contains(39))
iTermAttribs.currentFgColor = defaultFgColor;
if(params.contains(49))
iTermAttribs.currentBgColor = defaultBgColor;
} else {
iTermAttribs.currentFgColor = defaultFgColor;
iTermAttribs.currentBgColor = defaultBgColor;
iTermAttribs.currentAttrib = attribNone;
}
break;
case 'h':
if(params.count()>=1 && params.contains(1) && extra=="?") { // application cursor keys
iAppCursorKeys = true;
}
else if(params.count()>=1 && params.contains(3) && extra=="?") { //column mode
// not supported, just clear screen, move cursor home & reset scrolling region
clearAll();
resetTabs();
iMarginTop = 1;
iMarginBottom = iTermSize.height();
}
else if(params.count()>=1 && params.contains(6) && extra=="?") { //origin mode enable
iTermAttribs.originMode = true;
}
else if(params.count()>=1 && params.contains(7) && extra=="?") { //wraparound mode enable
iTermAttribs.wrapAroundMode = true;
}
else if(params.count()>=1 && params.contains(12) && extra=="?") { // start blinking cursor
// just ignore, we don't blink
}
else if(params.count()>=1 && params.contains(25) && extra=="?") { // show cursor
iShowCursor = true;
}
else if(params.count()>=1 && params.contains(1049) && extra=="?") { //use alt screen buffer & save cursor
iTermAttribs_saved_alt = iTermAttribs;
iUseAltScreenBuffer = true;
iMarginTop = 1;
iMarginBottom = iTermSize.height();
resetBackBufferScrollPos();
clearAll();
resetTabs();
emit displayBufferChanged();
}
else if(params.count()>=1 && params.contains(4) && extra=="") {
iReplaceMode = true;
}
else if(params.count()>=1 && params.contains(20) && extra=="") {
iNewLineMode = true;
}
else unhandled=true;
break;
case 'l':
if(params.count()>=1 && params.contains(1) && extra=="?") { // normal cursor keys
iAppCursorKeys = false;
}
else if(params.count()>=1 && params.contains(3) && extra=="?") { //column mode
// not supported, just clear screen, move cursor home & reset scrolling region
clearAll();
resetTabs();
iMarginTop = 1;
iMarginBottom = iTermSize.height();
}
else if(params.count()>=1 && params.contains(6) && extra=="?") { //origin mode disable
iTermAttribs.originMode = false;
}
else if(params.count()>=1 && params.contains(7) && extra=="?") { //wraparound mode disable
iTermAttribs.wrapAroundMode = false;
}
else if(params.count()>=1 && params.contains(12) && extra=="?") { // stop blinking cursor
// no need to do anything, we don't blink
}
else if(params.count()>=1 && params.contains(25) && extra=="?") { // hide cursor
iShowCursor = false;
}
else if(params.count()>=1 && params.contains(1049) && extra=="?") { //return from alt screen buffer & restore cursor
iUseAltScreenBuffer = false;
iTermAttribs = iTermAttribs_saved_alt;
iMarginBottom = iTermSize.height();
iMarginTop = 1;
resetBackBufferScrollPos();
resetTabs();
emit displayBufferChanged();
}
else if(params.count()>=1 && params.contains(4) && extra=="") {
iReplaceMode = false;
}
else if(params.count()>=1 && params.contains(20) && extra=="") {
iNewLineMode = false;
}
else unhandled=true;
break;
case 'r': // scrolling region
if(!extra.isEmpty()) {
unhandled=true;
break;
}
if(params.count() < 2) {
while(params.count() < 2)
params.append(1);
params[0] = 1;
params[1] = iTermSize.height();
}
if(params.at(0) < 1)
params[0] = 1;
if(params.at(1) > iTermSize.height())
params[1] = iTermSize.height();
iMarginTop = params.at(0);
iMarginBottom = params.at(1);
if(iMarginTop >= iMarginBottom) {
//invalid scroll region