Vt102Emulation.cpp 64.9 KB
Newer Older
1
2
/*
    This file is part of Konsole, an X terminal.
3

4
5
    Copyright 2007-2008 by Robert Knight <robert.knight@gmail.com>
    Copyright 1997,1998 by Lars Doelle <lars.doelle@on-line.de>
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

    This program 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.

    This program 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 this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301  USA.
*/
22

23
24
25
// Own
#include "Vt102Emulation.h"

Jekyll Wu's avatar
Jekyll Wu committed
26
// Standard
Kurt Hindenburg's avatar
Kurt Hindenburg committed
27
#include <cstdio>
28
29
#include <unistd.h>

30
// Qt
31
32
33
#include <QEvent>
#include <QTimer>
#include <QKeyEvent>
34

35
// KDE
36
#include <KLocalizedString>
37
38

// Konsole
39
#include "KeyboardTranslator.h"
40
#include "SessionController.h"
41
#include "TerminalDisplay.h"
42

43
using Konsole::Vt102Emulation;
44

45
46
47
48
49
50
51
/*
   The VT100 has 32 special graphical characters. The usual vt100 extended
   xterm fonts have these at 0x00..0x1f.

   QT's iso mapping leaves 0x00..0x7f without any changes. But the graphicals
   come in here as proper unicode characters.

52
   We treat non-iso10646 fonts as VT100 extended and do the required mapping
53
54
55
56
57
58
   from unicode to 0x00..0x1f. The remaining translation is then left to the
   QCodec.
*/

// assert for i in [0..31] : vt100extended(vt100_graphics[i]) == i.

59
60
61
62
63
64
unsigned short Konsole::vt100_graphics[32] = {
    // 0/8     1/9    2/10    3/11    4/12    5/13    6/14    7/15
    0x0020, 0x25C6, 0x2592, 0x2409, 0x240c, 0x240d, 0x240a, 0x00b0,
    0x00b1, 0x2424, 0x240b, 0x2518, 0x2510, 0x250c, 0x2514, 0x253c,
    0xF800, 0xF801, 0x2500, 0xF803, 0xF804, 0x251c, 0x2524, 0x2534,
    0x252c, 0x2502, 0x2264, 0x2265, 0x03C0, 0x2260, 0x00A3, 0x00b7
65
66
};

Kurt Hindenburg's avatar
Kurt Hindenburg committed
67
68
Vt102Emulation::Vt102Emulation() :
    Emulation(),
69
70
    _currentModes(TerminalState()),
    _savedModes(TerminalState()),
71
72
    _pendingSessionAttributesUpdates(QHash<int, QString>()),
    _sessionAttributesUpdateTimer(new QTimer(this)),
Kurt Hindenburg's avatar
Kurt Hindenburg committed
73
    _reportFocusEvents(false)
74
{
75
76
77
    _sessionAttributesUpdateTimer->setSingleShot(true);
    QObject::connect(_sessionAttributesUpdateTimer, &QTimer::timeout, this,
                     &Konsole::Vt102Emulation::updateSessionAttributes);
78

79
80
    initTokenizer();
    reset();
81
82
}

83
Vt102Emulation::~Vt102Emulation() = default;
84

85
void Vt102Emulation::clearEntireScreen()
86
{
87
88
    _currentScreen->clearEntireScreen();
    bufferedUpdate();
89
90
}

91
void Vt102Emulation::reset()
92
{
93
94
    // Save the current codec so we can set it later.
    // Ideally we would want to use the profile setting
Kurt Hindenburg's avatar
Kurt Hindenburg committed
95
    const QTextCodec *currentCodec = codec();
96
97
98
99
100
101
102
103

    resetTokenizer();
    resetModes();
    resetCharset(0);
    _screen[0]->reset();
    resetCharset(1);
    _screen[1]->reset();

Kurt Hindenburg's avatar
Kurt Hindenburg committed
104
    if (currentCodec != nullptr) {
105
        setCodec(currentCodec);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
106
    } else {
107
        setCodec(LocaleCodec);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
108
    }
109

110
111
    emit resetCursorStyleRequest();

112
    bufferedUpdate();
113
114
115
116
117
118
119
120
121
122
123
}

/* ------------------------------------------------------------------------- */
/*                                                                           */
/*                     Processing the incoming byte stream                   */
/*                                                                           */
/* ------------------------------------------------------------------------- */

/* Incoming Bytes Event pipeline

   This section deals with decoding the incoming character stream.
Thorsten Roeder's avatar
Thorsten Roeder committed
124
   Decoding means here, that the stream is first separated into `tokens'
125
   which are then mapped to a `meaning' provided as operations by the
126
   `Screen' class or by the emulation class itself.
127
128
129

   The pipeline proceeds as follows:

130
   - Tokenizing the ESC codes (onReceiveChar)
131
   - VT100 code page translation of plain characters (applyCharset)
132
   - Interpretation of ESC codes (processToken)
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

   The escape codes and their meaning are described in the
   technical reference of this program.
*/

// Tokens ------------------------------------------------------------------ --

/*
   Since the tokens are the central notion if this section, we've put them
   in front. They provide the syntactical elements used to represent the
   terminals operations as byte sequences.

   They are encodes here into a single machine word, so that we can later
   switch over them easily. Depending on the token itself, additional
   argument variables are filled with parameter values.

   The tokens are defined below:

   - CHR        - Printable characters     (32..255 but DEL (=127))
   - CTL        - Control characters       (0..31 but ESC (= 27), DEL)
   - ESC        - Escape codes of the form <ESC><CHR but `[]()+*#'>
   - ESC_DE     - Escape codes of the form <ESC><any of `()+*#%'> C
   - CSI_PN     - Escape codes of the form <ESC>'['     {Pn} ';' {Pn} C
   - CSI_PS     - Escape codes of the form <ESC>'['     {Pn} ';' ...  C
   - CSI_PR     - Escape codes of the form <ESC>'[' '?' {Pn} ';' ...  C
Waldo Bastian's avatar
Waldo Bastian committed
158
   - CSI_PE     - Escape codes of the form <ESC>'[' '!' {Pn} ';' ...  C
159
160
161
162
   - CSI_SP     - Escape codes of the form <ESC>'[' ' ' C
                  (3rd field is a space)
   - CSI_PSP    - Escape codes of the form <ESC>'[' '{Pn}' ' ' C
                  (4th field is a space)
163
164
165
   - VT52       - VT52 escape codes
                  - <ESC><Chr>
                  - <ESC>'Y'{Pc}{Pc}
166
   - XTE_HA     - Xterm window/terminal attribute commands
167
168
                  of the form <ESC>`]' {Pn} `;' {Text} <BEL>
                  (Note that these are handled differently to the other formats)
169
170
171
172

   The last two forms allow list of arguments. Since the elements of
   the lists are treated individually the same way, they are passed
   as individual tokens to the interpretation. Further, because the
173
   meaning of the parameters are names (although represented as numbers),
174
175
176
   they are includes within the token ('N').

*/
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
constexpr int token_construct(int t, int a, int n)
{
    return (((n & 0xffff) << 16) | ((a & 0xff) << 8) | (t & 0xff));
}
constexpr int token_chr()
{
    return token_construct(0, 0, 0);
}
constexpr int token_ctl(int a)
{
    return token_construct(1, a, 0);
}
constexpr int token_esc(int a)
{
    return token_construct(2, a, 0);
}
constexpr int token_esc_cs(int a, int b)
{
    return token_construct(3, a, b);
}
constexpr int token_esc_de(int a)
{
    return token_construct(4, a, 0);
}
constexpr int token_csi_ps(int a, int n)
{
    return token_construct(5, a, n);
}
constexpr int token_csi_pn(int a)
{
    return token_construct(6, a, 0);
}
constexpr int token_csi_pr(int a, int n)
{
    return token_construct(7, a, n);
}
constexpr int token_vt52(int a)
{
    return token_construct(8, a, 0);
}
constexpr int token_csi_pg(int a)
{
    return token_construct(9, a, 0);
}
constexpr int token_csi_pe(int a)
{
    return token_construct(10, a, 0);
}
225
226
227
228
229
230
231
232
constexpr int token_csi_sp(int a)
{
    return token_construct(11, a, 0);
}
constexpr int token_csi_psp(int a, int n)
{
    return token_construct(12, a, n);
}
233

234
const int MAX_ARGUMENT = 4096;
235

236
237
// Tokenizer --------------------------------------------------------------- --

238
/* The tokenizer's state
239

240
   The state is represented by the buffer (tokenBuffer, tokenBufferPos),
241
242
243
244
   and accompanied by decoded arguments kept in (argv,argc).
   Note that they are kept internal in the tokenizer.
*/

245
void Vt102Emulation::resetTokenizer()
246
{
247
248
249
250
    tokenBufferPos = 0;
    argc = 0;
    argv[0] = 0;
    argv[1] = 0;
251
252
}

253
void Vt102Emulation::addDigit(int digit)
254
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
255
    if (argv[argc] < MAX_ARGUMENT) {
256
        argv[argc] = 10 * argv[argc] + digit;
Kurt Hindenburg's avatar
Kurt Hindenburg committed
257
    }
258
259
}

260
void Vt102Emulation::addArgument()
261
{
262
263
    argc = qMin(argc + 1, MAXARGS - 1);
    argv[argc] = 0;
264
265
}

266
void Vt102Emulation::addToCurrentToken(uint cc)
267
{
Mariusz Glebocki's avatar
Mariusz Glebocki committed
268
    tokenBufferPos = qMin(tokenBufferPos, MAX_TOKEN_LENGTH - 1);
269
    tokenBuffer[tokenBufferPos] = cc;
Mariusz Glebocki's avatar
Mariusz Glebocki committed
270
    tokenBufferPos++;
271
272
}

273
// Character Class flags used while decoding
Kurt Hindenburg's avatar
Kurt Hindenburg committed
274
275
276
277
const int CTL = 1;   // Control character
const int CHR = 2;   // Printable character
const int CPN = 4;   // TODO: Document me
const int DIG = 8;   // Digit
278
const int SCS = 16;  // Select Character Set
Kurt Hindenburg's avatar
Kurt Hindenburg committed
279
280
const int GRP = 32;  // TODO: Document me
const int CPS = 64;  // Character which indicates end of window resize
281

282
void Vt102Emulation::initTokenizer()
283
284
{
    int i;
Kurt Hindenburg's avatar
Kurt Hindenburg committed
285
286
    quint8 *s;
    for (i = 0; i < 256; ++i) {
287
        charClass[i] = 0;
Kurt Hindenburg's avatar
Kurt Hindenburg committed
288
289
    }
    for (i = 0; i < 32; ++i) {
290
        charClass[i] |= CTL;
Kurt Hindenburg's avatar
Kurt Hindenburg committed
291
292
    }
    for (i = 32; i < 256; ++i) {
293
        charClass[i] |= CHR;
Kurt Hindenburg's avatar
Kurt Hindenburg committed
294
    }
295
    for (s = (quint8 *)"@ABCDGHILMPSTXZbcdfry"; *s != 0u; ++s) {
296
        charClass[*s] |= CPN;
Kurt Hindenburg's avatar
Kurt Hindenburg committed
297
    }
298
    // resize = \e[8;<row>;<col>t
Kurt Hindenburg's avatar
Kurt Hindenburg committed
299
    for (s = (quint8 *)"t"; *s != 0u; ++s) {
300
        charClass[*s] |= CPS;
Kurt Hindenburg's avatar
Kurt Hindenburg committed
301
302
    }
    for (s = (quint8 *)"0123456789"; *s != 0u; ++s) {
303
        charClass[*s] |= DIG;
Kurt Hindenburg's avatar
Kurt Hindenburg committed
304
305
    }
    for (s = (quint8 *)"()+*%"; *s != 0u; ++s) {
306
        charClass[*s] |= SCS;
Kurt Hindenburg's avatar
Kurt Hindenburg committed
307
308
    }
    for (s = (quint8 *)"()+*#[]%"; *s != 0u; ++s) {
309
        charClass[*s] |= GRP;
Kurt Hindenburg's avatar
Kurt Hindenburg committed
310
    }
311
312

    resetTokenizer();
313
314
315
316
317
318
319
320
321
322
323
324
}

/* Ok, here comes the nasty part of the decoder.

   Instead of keeping an explicit state, we deduce it from the
   token scanned so far. It is then immediately combined with
   the current character to form a scanning decision.

   This is done by the following defines.

   - P is the length of the token scanned so far.
   - L (often P-1) is the position on which contents we base a decision.
325
   - C is a character or a group of characters (taken from 'charClass').
326

327
328
   - 'cc' is the current character
   - 's' is a pointer to the start of the token buffer
329
   - 'p' is the current position within the token buffer
330
331
332
333

   Note that they need to applied in proper order.
*/

334
335
336
337
338
339
340
341
342
#define lec(P,L,C) (p == (P) && s[(L)] == (C))
#define lun(     ) (p ==  1  && cc >= 32 )
#define les(P,L,C) (p == (P) && s[L] < 256 && (charClass[s[(L)]] & (C)) == (C))
#define eec(C)     (p >=  3  && cc == (C))
#define ees(C)     (p >=  3  && cc < 256 && (charClass[cc] & (C)) == (C))
#define eps(C)     (p >=  3  && s[2] != '?' && s[2] != '!' && s[2] != '>' && cc < 256 && (charClass[cc] & (C)) == (C))
#define epp( )     (p >=  3  && s[2] == '?')
#define epe( )     (p >=  3  && s[2] == '!')
#define egt( )     (p >=  3  && s[2] == '>')
343
344
#define esp( )     (p >=  4  && s[2] == SP )
#define epsp( )    (p >=  5  && s[3] == SP )
Mariusz Glebocki's avatar
Mariusz Glebocki committed
345
346
#define osc        (tokenBufferPos >= 2 && tokenBuffer[1] == ']')
#define ces(C)     (cc < 256 && (charClass[cc] & (C)) == (C))
Albert Astals Cid's avatar
Albert Astals Cid committed
347
#define dcs        (p >= 2   && s[0] == ESC && s[1] == 'P')
348
349

#define CNTL(c) ((c)-'@')
Kurt Hindenburg's avatar
Kurt Hindenburg committed
350
const int ESC = 27;
Kurt Hindenburg's avatar
Kurt Hindenburg committed
351
const int DEL = 127;
352
const int SP  = 32;
353
354

// process an incoming unicode character
355
void Vt102Emulation::receiveChar(uint cc)
356
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
357
  if (cc == DEL) {
358
    return; //VT100: ignore.
Kurt Hindenburg's avatar
Kurt Hindenburg committed
359
  }
360

361
  if (ces(CTL))
362
  {
Mariusz Glebocki's avatar
Mariusz Glebocki committed
363
    // ignore control characters in the text part of osc (aka OSC) "ESC]"
364
    // escape sequences; this matches what XTERM docs say
Mariusz Glebocki's avatar
Mariusz Glebocki committed
365
366
    // Allow BEL and ESC here, it will either end the text or be removed later.
    if (osc && cc != 0x1b && cc != 0x07) {
367
368
369
        return;
    }

Mariusz Glebocki's avatar
Mariusz Glebocki committed
370
371
372
373
374
375
376
377
378
379
380
381
382
    if (!osc) {
      // DEC HACK ALERT! Control Characters are allowed *within* esc sequences in VT100
      // This means, they do neither a resetTokenizer() nor a pushToToken(). Some of them, do
      // of course. Guess this originates from a weakly layered handling of the X-on
      // X-off protocol, which comes really below this level.
      if (cc == CNTL('X') || cc == CNTL('Z') || cc == ESC) {
          resetTokenizer(); //VT100: CAN or SUB
      }
      if (cc != ESC)
      {
          processToken(token_ctl(cc+'@'), 0, 0);
          return;
      }
383
    }
384
  }
385
  // advance the state
386
  addToCurrentToken(cc);
387

388
389

  uint* s = tokenBuffer;
390
  const int  p = tokenBufferPos;
391

392
  if (getMode(MODE_Ansi))
393
  {
394
395
396
    if (lec(1,0,ESC)) { return; }
    if (lec(1,0,ESC+128)) { s[0] = ESC; receiveChar('['); return; }
    if (les(2,1,GRP)) { return; }
Mariusz Glebocki's avatar
Mariusz Glebocki committed
397
398
399
400
401
402
403
404
405
406
407
    // Operating System Command
    if (p > 2 && s[1] == ']') {
      // <ESC> ']' ... <ESC> '\'
      if (s[p-2] == ESC && s[p-1] == '\\') { processSessionAttributeRequest(p-1); resetTokenizer(); return; }
      // <ESC> ']' ... <ESC> + one character for reprocessing
      if (s[p-2] == ESC) { processSessionAttributeRequest(p-1); resetTokenizer(); receiveChar(cc); return; }
      // <ESC> ']' ... <BEL>
      if (s[p-1] == 0x07) { processSessionAttributeRequest(p); resetTokenizer(); return; }
    }
    // <ESC> ']' ...
    if (osc         ) { return; }
408
409
410
    if (lec(3,2,'?')) { return; }
    if (lec(3,2,'>')) { return; }
    if (lec(3,2,'!')) { return; }
411
412
    if (lec(3,2,SP )) { return; }
    if (lec(4,3,SP )) { return; }
413
    if (lun(       )) { processToken(token_chr(), applyCharset(cc), 0);   resetTokenizer(); return; }
Albert Astals Cid's avatar
Albert Astals Cid committed
414
    if (dcs         ) { return; /* TODO We don't xterm DCS, so we just eat it */ }
415
416
417
418
    if (lec(2,0,ESC)) { processToken(token_esc(s[1]), 0, 0);              resetTokenizer(); return; }
    if (les(3,1,SCS)) { processToken(token_esc_cs(s[1],s[2]), 0, 0);      resetTokenizer(); return; }
    if (lec(3,1,'#')) { processToken(token_esc_de(s[2]), 0, 0);           resetTokenizer(); return; }
    if (eps(    CPN)) { processToken(token_csi_pn(cc), argv[0],argv[1]);  resetTokenizer(); return; }
419
420

    // resize = \e[8;<row>;<col>t
421
422
    if (eps(CPS))
    {
423
        processToken(token_csi_ps(cc, argv[0]), argv[1], argv[2]);
424
425
        resetTokenizer();
        return;
Kurt Hindenburg's avatar
Kurt Hindenburg committed
426
    }
427

428
    if (epe(   )) { processToken(token_csi_pe(cc), 0, 0); resetTokenizer(); return; }
429
430
431
432

    if (esp (   )) { processToken(token_csi_sp(cc), 0, 0);           resetTokenizer(); return; }
    if (epsp(   )) { processToken(token_csi_psp(cc, argv[0]), 0, 0); resetTokenizer(); return; }

433
434
    if (ees(DIG)) { addDigit(cc-'0'); return; }
    if (eec(';')) { addArgument();    return; }
435
    for (int i = 0; i <= argc; i++)
436
    {
Kurt Hindenburg's avatar
Kurt Hindenburg committed
437
        if (epp()) {
438
            processToken(token_csi_pr(cc,argv[i]), 0, 0);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
439
        } else if (egt()) {
440
            processToken(token_csi_pg(cc), 0, 0); // spec. case for ESC]>0c or ESC]>c
Kurt Hindenburg's avatar
Kurt Hindenburg committed
441
        } else if (cc == 'm' && argc - i >= 4 && (argv[i] == 38 || argv[i] == 48) && argv[i+1] == 2)
442
        {
443
444
            // ESC[ ... 48;2;<red>;<green>;<blue> ... m -or- ESC[ ... 38;2;<red>;<green>;<blue> ... m
            i += 2;
445
            processToken(token_csi_ps(cc, argv[i-2]), COLOR_SPACE_RGB, (argv[i] << 16) | (argv[i+1] << 8) | argv[i+2]);
446
447
448
            i += 2;
        }
        else if (cc == 'm' && argc - i >= 2 && (argv[i] == 38 || argv[i] == 48) && argv[i+1] == 5)
449
        {
450
451
            // ESC[ ... 48;5;<index> ... m -or- ESC[ ... 38;5;<index> ... m
            i += 2;
452
            processToken(token_csi_ps(cc, argv[i-2]), COLOR_SPACE_256, argv[i]);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
453
        } else {
454
            processToken(token_csi_ps(cc,argv[i]), 0, 0);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
455
        }
Kurt Hindenburg's avatar
Kurt Hindenburg committed
456
    }
457
    resetTokenizer();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
458
  } else {
459
    // VT52 Mode
Kurt Hindenburg's avatar
Kurt Hindenburg committed
460
    if (lec(1,0,ESC)) {
461
        return;
Kurt Hindenburg's avatar
Kurt Hindenburg committed
462
    }
463
464
    if (les(1,0,CHR))
    {
465
        processToken(token_chr(), s[0], 0);
466
        resetTokenizer();
467
468
        return;
    }
Kurt Hindenburg's avatar
Kurt Hindenburg committed
469
    if (lec(2,1,'Y')) {
470
        return;
Kurt Hindenburg's avatar
Kurt Hindenburg committed
471
472
    }
    if (lec(3,1,'Y')) {
473
        return;
Kurt Hindenburg's avatar
Kurt Hindenburg committed
474
    }
475
476
    if (p < 4)
    {
477
        processToken(token_vt52(s[1] ), 0, 0);
478
        resetTokenizer();
479
480
        return;
    }
481
    processToken(token_vt52(s[1]), s[2], s[3]);
482
    resetTokenizer();
483
    return;
484
485
  }
}
486

Mariusz Glebocki's avatar
Mariusz Glebocki committed
487
void Vt102Emulation::processSessionAttributeRequest(int tokenSize)
488
489
{
  // Describes the window or terminal session attribute to change
490
  // See Session::SessionAttributes for possible values
491
  int attribute = 0;
492
  int i;
Mariusz Glebocki's avatar
Mariusz Glebocki committed
493
494
495
496
497
498

  // ignore last character (ESC or BEL)
  --tokenSize;

  // skip first two characters (ESC, ']')
  for (i = 2; i < tokenSize &&
499
              tokenBuffer[i] >= '0'  &&
500
              tokenBuffer[i] <= '9'; i++)
501
  {
502
    attribute = 10 * attribute + (tokenBuffer[i]-'0');
503
  }
504

505
506
507
508
  if (tokenBuffer[i] != ';')
  {
    reportDecodingError();
    return;
509
  }
Mariusz Glebocki's avatar
Mariusz Glebocki committed
510
511
  // skip ';'
  ++i;
512

Mariusz Glebocki's avatar
Mariusz Glebocki committed
513
  const QString value = QString::fromUcs4(&tokenBuffer[i], tokenSize - i);
514

515
  if (value == QLatin1String("?")) {
516
517
518
      emit sessionAttributeRequest(attribute);
      return;
  }
519

520
521
  _pendingSessionAttributesUpdates[attribute] = value;
  _sessionAttributesUpdateTimer->start(20);
522
523
}

524
void Vt102Emulation::updateSessionAttributes()
525
{
526
    QListIterator<int> iter(_pendingSessionAttributesUpdates.keys());
Jekyll Wu's avatar
Jekyll Wu committed
527
528
    while (iter.hasNext()) {
        int arg = iter.next();
529
        emit sessionAttributeChanged(arg , _pendingSessionAttributesUpdates[arg]);
530
    }
531
    _pendingSessionAttributesUpdates.clear();
532
533
}

534
535
536
537
538
// Interpreting Codes ---------------------------------------------------------

/*
   Now that the incoming character stream is properly tokenized,
   meaning is assigned to them. These are either operations of
539
   the current _screen, or of the emulation class itself.
540

541
   The token to be interpreted comes in as a machine word
542
543
544
545
546
547
   possibly accompanied by two parameters.

   Likewise, the operations assigned to, come with up to two
   arguments. One could consider to make up a proper table
   from the function below.

Thorsten Roeder's avatar
Thorsten Roeder committed
548
   The technical reference manual provides more information
549
550
551
   about this mapping.
*/

552
void Vt102Emulation::processToken(int token, int p, int q)
Waldo Bastian's avatar
Waldo Bastian committed
553
{
554
555
  switch (token)
  {
556
    case token_chr(         ) : _currentScreen->displayCharacter     (p         ); break; //UTF16
557
558
559

    //             127 DEL    : ignored on input

560
561
562
563
564
565
566
567
    case token_ctl('@'      ) : /* NUL: ignored                      */ break;
    case token_ctl('A'      ) : /* SOH: ignored                      */ break;
    case token_ctl('B'      ) : /* STX: ignored                      */ break;
    case token_ctl('C'      ) : /* ETX: ignored                      */ break;
    case token_ctl('D'      ) : /* EOT: ignored                      */ break;
    case token_ctl('E'      ) :      reportAnswerBack     (          ); break; //VT100
    case token_ctl('F'      ) : /* ACK: ignored                      */ break;
    case token_ctl('G'      ) : emit stateSet(NOTIFYBELL);
568
                                break; //VT100
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
    case token_ctl('H'      ) : _currentScreen->backspace            (          ); break; //VT100
    case token_ctl('I'      ) : _currentScreen->tab                  (          ); break; //VT100
    case token_ctl('J'      ) : _currentScreen->newLine              (          ); break; //VT100
    case token_ctl('K'      ) : _currentScreen->newLine              (          ); break; //VT100
    case token_ctl('L'      ) : _currentScreen->newLine              (          ); break; //VT100
    case token_ctl('M'      ) : _currentScreen->toStartOfLine        (          ); break; //VT100

    case token_ctl('N'      ) :      useCharset           (         1); break; //VT100
    case token_ctl('O'      ) :      useCharset           (         0); break; //VT100

    case token_ctl('P'      ) : /* DLE: ignored                      */ break;
    case token_ctl('Q'      ) : /* DC1: XON continue                 */ break; //VT100
    case token_ctl('R'      ) : /* DC2: ignored                      */ break;
    case token_ctl('S'      ) : /* DC3: XOFF halt                    */ break; //VT100
    case token_ctl('T'      ) : /* DC4: ignored                      */ break;
    case token_ctl('U'      ) : /* NAK: ignored                      */ break;
    case token_ctl('V'      ) : /* SYN: ignored                      */ break;
    case token_ctl('W'      ) : /* ETB: ignored                      */ break;
    case token_ctl('X'      ) : _currentScreen->displayCharacter     (    0x2592); break; //VT100
    case token_ctl('Y'      ) : /* EM : ignored                      */ break;
    case token_ctl('Z'      ) : _currentScreen->displayCharacter     (    0x2592); break; //VT100
    case token_ctl('['      ) : /* ESC: cannot be seen here.         */ break;
    case token_ctl('\\'     ) : /* FS : ignored                      */ break;
    case token_ctl(']'      ) : /* GS : ignored                      */ break;
    case token_ctl('^'      ) : /* RS : ignored                      */ break;
    case token_ctl('_'      ) : /* US : ignored                      */ break;

    case token_esc('D'      ) : _currentScreen->index                (          ); break; //VT100
    case token_esc('E'      ) : _currentScreen->nextLine             (          ); break; //VT100
    case token_esc('H'      ) : _currentScreen->changeTabStop        (true      ); break; //VT100
    case token_esc('M'      ) : _currentScreen->reverseIndex         (          ); break; //VT100
    case token_esc('Z'      ) :      reportTerminalType   (          ); break;
    case token_esc('c'      ) :      reset                (          ); break;

    case token_esc('n'      ) :      useCharset           (         2); break;
    case token_esc('o'      ) :      useCharset           (         3); break;
    case token_esc('7'      ) :      saveCursor           (          ); break;
    case token_esc('8'      ) :      restoreCursor        (          ); break;

    case token_esc('='      ) :          setMode      (MODE_AppKeyPad); break;
    case token_esc('>'      ) :        resetMode      (MODE_AppKeyPad); break;
    case token_esc('<'      ) :          setMode      (MODE_Ansi     ); break; //VT100

    case token_esc_cs('(', '0') :      setCharset           (0,    '0'); break; //VT100
    case token_esc_cs('(', 'A') :      setCharset           (0,    'A'); break; //VT100
    case token_esc_cs('(', 'B') :      setCharset           (0,    'B'); break; //VT100

    case token_esc_cs(')', '0') :      setCharset           (1,    '0'); break; //VT100
    case token_esc_cs(')', 'A') :      setCharset           (1,    'A'); break; //VT100
    case token_esc_cs(')', 'B') :      setCharset           (1,    'B'); break; //VT100

    case token_esc_cs('*', '0') :      setCharset           (2,    '0'); break; //VT100
    case token_esc_cs('*', 'A') :      setCharset           (2,    'A'); break; //VT100
    case token_esc_cs('*', 'B') :      setCharset           (2,    'B'); break; //VT100

    case token_esc_cs('+', '0') :      setCharset           (3,    '0'); break; //VT100
    case token_esc_cs('+', 'A') :      setCharset           (3,    'A'); break; //VT100
    case token_esc_cs('+', 'B') :      setCharset           (3,    'B'); break; //VT100

    case token_esc_cs('%', 'G') :      setCodec             (Utf8Codec   ); break; //LINUX
    case token_esc_cs('%', '@') :      setCodec             (LocaleCodec ); break; //LINUX

    case token_esc_de('3'      ) : /* Double height line, top half    */
632
633
634
                                _currentScreen->setLineProperty( LINE_DOUBLEWIDTH , true );
                                _currentScreen->setLineProperty( LINE_DOUBLEHEIGHT , true );
                                    break;
635
    case token_esc_de('4'      ) : /* Double height line, bottom half */
636
637
638
                                _currentScreen->setLineProperty( LINE_DOUBLEWIDTH , true );
                                _currentScreen->setLineProperty( LINE_DOUBLEHEIGHT , true );
                                    break;
639
    case token_esc_de('5'      ) : /* Single width, single height line*/
640
641
642
                                _currentScreen->setLineProperty( LINE_DOUBLEWIDTH , false);
                                _currentScreen->setLineProperty( LINE_DOUBLEHEIGHT , false);
                                break;
643
    case token_esc_de('6'      ) : /* Double width, single height line*/
644
                                _currentScreen->setLineProperty( LINE_DOUBLEWIDTH , true);
645
646
                                _currentScreen->setLineProperty( LINE_DOUBLEHEIGHT , false);
                                break;
647
    case token_esc_de('8'      ) : _currentScreen->helpAlign            (          ); break;
648

649
// resize = \e[8;<row>;<col>t
650
    case token_csi_ps('t',   8) : setImageSize( p /*lines */, q /* columns */ );
651
652
                               emit imageResizeRequest(QSize(q, p));
                               break;
653

Kurt Hindenburg's avatar
   
Kurt Hindenburg committed
654
// change tab text color : \e[28;<color>t  color: 0-16,777,215
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
    case token_csi_ps('t',   28) : emit changeTabTextColorRequest      ( p        );          break;

    case token_csi_ps('K',   0) : _currentScreen->clearToEndOfLine     (          ); break;
    case token_csi_ps('K',   1) : _currentScreen->clearToBeginOfLine   (          ); break;
    case token_csi_ps('K',   2) : _currentScreen->clearEntireLine      (          ); break;
    case token_csi_ps('J',   0) : _currentScreen->clearToEndOfScreen   (          ); break;
    case token_csi_ps('J',   1) : _currentScreen->clearToBeginOfScreen (          ); break;
    case token_csi_ps('J',   2) : _currentScreen->clearEntireScreen    (          ); break;
    case token_csi_ps('J',      3) : clearHistory();                            break;
    case token_csi_ps('g',   0) : _currentScreen->changeTabStop        (false     ); break; //VT100
    case token_csi_ps('g',   3) : _currentScreen->clearTabStops        (          ); break; //VT100
    case token_csi_ps('h',   4) : _currentScreen->    setMode      (MODE_Insert   ); break;
    case token_csi_ps('h',  20) :          setMode      (MODE_NewLine  ); break;
    case token_csi_ps('i',   0) : /* IGNORE: attached printer          */ break; //VT100
    case token_csi_ps('l',   4) : _currentScreen->  resetMode      (MODE_Insert   ); break;
    case token_csi_ps('l',  20) :        resetMode      (MODE_NewLine  ); break;
    case token_csi_ps('s',   0) :      saveCursor           (          ); break;
    case token_csi_ps('u',   0) :      restoreCursor        (          ); break;

    case token_csi_ps('m',   0) : _currentScreen->setDefaultRendition  (          ); break;
    case token_csi_ps('m',   1) : _currentScreen->  setRendition     (RE_BOLD     ); break; //VT100
    case token_csi_ps('m',   2) : _currentScreen->  setRendition     (RE_FAINT    ); break;
    case token_csi_ps('m',   3) : _currentScreen->  setRendition     (RE_ITALIC   ); break; //VT100
    case token_csi_ps('m',   4) : _currentScreen->  setRendition     (RE_UNDERLINE); break; //VT100
    case token_csi_ps('m',   5) : _currentScreen->  setRendition     (RE_BLINK    ); break; //VT100
    case token_csi_ps('m',   7) : _currentScreen->  setRendition     (RE_REVERSE  ); break;
    case token_csi_ps('m',   8) : _currentScreen->  setRendition     (RE_CONCEAL  ); break;
    case token_csi_ps('m',   9) : _currentScreen->  setRendition     (RE_STRIKEOUT); break;
    case token_csi_ps('m',  53) : _currentScreen->  setRendition     (RE_OVERLINE ); break;
    case token_csi_ps('m',  10) : /* IGNORED: mapping related          */ break; //LINUX
    case token_csi_ps('m',  11) : /* IGNORED: mapping related          */ break; //LINUX
    case token_csi_ps('m',  12) : /* IGNORED: mapping related          */ break; //LINUX
    case token_csi_ps('m',  21) : _currentScreen->resetRendition     (RE_BOLD     ); break;
    case token_csi_ps('m',  22) : _currentScreen->resetRendition     (RE_BOLD     );
689
                               _currentScreen->resetRendition     (RE_FAINT    ); break;
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
767
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
802
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
835
836
837
838
    case token_csi_ps('m',  23) : _currentScreen->resetRendition     (RE_ITALIC   ); break; //VT100
    case token_csi_ps('m',  24) : _currentScreen->resetRendition     (RE_UNDERLINE); break;
    case token_csi_ps('m',  25) : _currentScreen->resetRendition     (RE_BLINK    ); break;
    case token_csi_ps('m',  27) : _currentScreen->resetRendition     (RE_REVERSE  ); break;
    case token_csi_ps('m',  28) : _currentScreen->resetRendition     (RE_CONCEAL  ); break;
    case token_csi_ps('m',  29) : _currentScreen->resetRendition     (RE_STRIKEOUT); break;
    case token_csi_ps('m',  55) : _currentScreen->resetRendition     (RE_OVERLINE ); break;

    case token_csi_ps('m',   30) : _currentScreen->setForeColor         (COLOR_SPACE_SYSTEM,  0); break;
    case token_csi_ps('m',   31) : _currentScreen->setForeColor         (COLOR_SPACE_SYSTEM,  1); break;
    case token_csi_ps('m',   32) : _currentScreen->setForeColor         (COLOR_SPACE_SYSTEM,  2); break;
    case token_csi_ps('m',   33) : _currentScreen->setForeColor         (COLOR_SPACE_SYSTEM,  3); break;
    case token_csi_ps('m',   34) : _currentScreen->setForeColor         (COLOR_SPACE_SYSTEM,  4); break;
    case token_csi_ps('m',   35) : _currentScreen->setForeColor         (COLOR_SPACE_SYSTEM,  5); break;
    case token_csi_ps('m',   36) : _currentScreen->setForeColor         (COLOR_SPACE_SYSTEM,  6); break;
    case token_csi_ps('m',   37) : _currentScreen->setForeColor         (COLOR_SPACE_SYSTEM,  7); break;

    case token_csi_ps('m',   38) : _currentScreen->setForeColor         (p,       q); break;

    case token_csi_ps('m',   39) : _currentScreen->setForeColor         (COLOR_SPACE_DEFAULT,  0); break;

    case token_csi_ps('m',   40) : _currentScreen->setBackColor         (COLOR_SPACE_SYSTEM,  0); break;
    case token_csi_ps('m',   41) : _currentScreen->setBackColor         (COLOR_SPACE_SYSTEM,  1); break;
    case token_csi_ps('m',   42) : _currentScreen->setBackColor         (COLOR_SPACE_SYSTEM,  2); break;
    case token_csi_ps('m',   43) : _currentScreen->setBackColor         (COLOR_SPACE_SYSTEM,  3); break;
    case token_csi_ps('m',   44) : _currentScreen->setBackColor         (COLOR_SPACE_SYSTEM,  4); break;
    case token_csi_ps('m',   45) : _currentScreen->setBackColor         (COLOR_SPACE_SYSTEM,  5); break;
    case token_csi_ps('m',   46) : _currentScreen->setBackColor         (COLOR_SPACE_SYSTEM,  6); break;
    case token_csi_ps('m',   47) : _currentScreen->setBackColor         (COLOR_SPACE_SYSTEM,  7); break;

    case token_csi_ps('m',   48) : _currentScreen->setBackColor         (p,       q); break;

    case token_csi_ps('m',   49) : _currentScreen->setBackColor         (COLOR_SPACE_DEFAULT,  1); break;

    case token_csi_ps('m',   90) : _currentScreen->setForeColor         (COLOR_SPACE_SYSTEM,  8); break;
    case token_csi_ps('m',   91) : _currentScreen->setForeColor         (COLOR_SPACE_SYSTEM,  9); break;
    case token_csi_ps('m',   92) : _currentScreen->setForeColor         (COLOR_SPACE_SYSTEM, 10); break;
    case token_csi_ps('m',   93) : _currentScreen->setForeColor         (COLOR_SPACE_SYSTEM, 11); break;
    case token_csi_ps('m',   94) : _currentScreen->setForeColor         (COLOR_SPACE_SYSTEM, 12); break;
    case token_csi_ps('m',   95) : _currentScreen->setForeColor         (COLOR_SPACE_SYSTEM, 13); break;
    case token_csi_ps('m',   96) : _currentScreen->setForeColor         (COLOR_SPACE_SYSTEM, 14); break;
    case token_csi_ps('m',   97) : _currentScreen->setForeColor         (COLOR_SPACE_SYSTEM, 15); break;

    case token_csi_ps('m',  100) : _currentScreen->setBackColor         (COLOR_SPACE_SYSTEM,  8); break;
    case token_csi_ps('m',  101) : _currentScreen->setBackColor         (COLOR_SPACE_SYSTEM,  9); break;
    case token_csi_ps('m',  102) : _currentScreen->setBackColor         (COLOR_SPACE_SYSTEM, 10); break;
    case token_csi_ps('m',  103) : _currentScreen->setBackColor         (COLOR_SPACE_SYSTEM, 11); break;
    case token_csi_ps('m',  104) : _currentScreen->setBackColor         (COLOR_SPACE_SYSTEM, 12); break;
    case token_csi_ps('m',  105) : _currentScreen->setBackColor         (COLOR_SPACE_SYSTEM, 13); break;
    case token_csi_ps('m',  106) : _currentScreen->setBackColor         (COLOR_SPACE_SYSTEM, 14); break;
    case token_csi_ps('m',  107) : _currentScreen->setBackColor         (COLOR_SPACE_SYSTEM, 15); break;

    case token_csi_ps('n',   5) :      reportStatus         (          ); break;
    case token_csi_ps('n',   6) :      reportCursorPosition (          ); break;
    case token_csi_ps('q',   0) : /* IGNORED: LEDs off                 */ break; //VT100
    case token_csi_ps('q',   1) : /* IGNORED: LED1 on                  */ break; //VT100
    case token_csi_ps('q',   2) : /* IGNORED: LED2 on                  */ break; //VT100
    case token_csi_ps('q',   3) : /* IGNORED: LED3 on                  */ break; //VT100
    case token_csi_ps('q',   4) : /* IGNORED: LED4 on                  */ break; //VT100
    case token_csi_ps('x',   0) :      reportTerminalParms  (         2); break; //VT100
    case token_csi_ps('x',   1) :      reportTerminalParms  (         3); break; //VT100

    case token_csi_pn('@'      ) : _currentScreen->insertChars          (p         ); break;
    case token_csi_pn('A'      ) : _currentScreen->cursorUp             (p         ); break; //VT100
    case token_csi_pn('B'      ) : _currentScreen->cursorDown           (p         ); break; //VT100
    case token_csi_pn('C'      ) : _currentScreen->cursorRight          (p         ); break; //VT100
    case token_csi_pn('D'      ) : _currentScreen->cursorLeft           (p         ); break; //VT100
    case token_csi_pn('E'      ) : /* Not implemented: cursor next p lines */         break; //VT100
    case token_csi_pn('F'      ) : /* Not implemented: cursor preceding p lines */    break; //VT100
    case token_csi_pn('G'      ) : _currentScreen->setCursorX           (p         ); break; //LINUX
    case token_csi_pn('H'      ) : _currentScreen->setCursorYX          (p,      q); break; //VT100
    case token_csi_pn('I'      ) : _currentScreen->tab                  (p         ); break;
    case token_csi_pn('L'      ) : _currentScreen->insertLines          (p         ); break;
    case token_csi_pn('M'      ) : _currentScreen->deleteLines          (p         ); break;
    case token_csi_pn('P'      ) : _currentScreen->deleteChars          (p         ); break;
    case token_csi_pn('S'      ) : _currentScreen->scrollUp             (p         ); break;
    case token_csi_pn('T'      ) : _currentScreen->scrollDown           (p         ); break;
    case token_csi_pn('X'      ) : _currentScreen->eraseChars           (p         ); break;
    case token_csi_pn('Z'      ) : _currentScreen->backtab              (p         ); break;
    case token_csi_pn('b'      ) : _currentScreen->repeatChars          (p         ); break;
    case token_csi_pn('c'      ) :      reportTerminalType   (          ); break; //VT100
    case token_csi_pn('d'      ) : _currentScreen->setCursorY           (p         ); break; //LINUX
    case token_csi_pn('f'      ) : _currentScreen->setCursorYX          (p,      q); break; //VT100
    case token_csi_pn('r'      ) :      setMargins           (p,      q); break; //VT100
    case token_csi_pn('y'      ) : /* IGNORED: Confidence test          */ break; //VT100

    case token_csi_pr('h',   1) :          setMode      (MODE_AppCuKeys); break; //VT100
    case token_csi_pr('l',   1) :        resetMode      (MODE_AppCuKeys); break; //VT100
    case token_csi_pr('s',   1) :         saveMode      (MODE_AppCuKeys); break; //FIXME
    case token_csi_pr('r',   1) :      restoreMode      (MODE_AppCuKeys); break; //FIXME

    case token_csi_pr('l',   2) :        resetMode      (MODE_Ansi     ); break; //VT100

    case token_csi_pr('h',   3) :          setMode      (MODE_132Columns); break; //VT100
    case token_csi_pr('l',   3) :        resetMode      (MODE_132Columns); break; //VT100

    case token_csi_pr('h',   4) : /* IGNORED: soft scrolling           */ break; //VT100
    case token_csi_pr('l',   4) : /* IGNORED: soft scrolling           */ break; //VT100

    case token_csi_pr('h',   5) : _currentScreen->    setMode      (MODE_Screen   ); break; //VT100
    case token_csi_pr('l',   5) : _currentScreen->  resetMode      (MODE_Screen   ); break; //VT100

    case token_csi_pr('h',   6) : _currentScreen->    setMode      (MODE_Origin   ); break; //VT100
    case token_csi_pr('l',   6) : _currentScreen->  resetMode      (MODE_Origin   ); break; //VT100
    case token_csi_pr('s',   6) : _currentScreen->   saveMode      (MODE_Origin   ); break; //FIXME
    case token_csi_pr('r',   6) : _currentScreen->restoreMode      (MODE_Origin   ); break; //FIXME

    case token_csi_pr('h',   7) : _currentScreen->    setMode      (MODE_Wrap     ); break; //VT100
    case token_csi_pr('l',   7) : _currentScreen->  resetMode      (MODE_Wrap     ); break; //VT100
    case token_csi_pr('s',   7) : _currentScreen->   saveMode      (MODE_Wrap     ); break; //FIXME
    case token_csi_pr('r',   7) : _currentScreen->restoreMode      (MODE_Wrap     ); break; //FIXME

    case token_csi_pr('h',   8) : /* IGNORED: autorepeat on            */ break; //VT100
    case token_csi_pr('l',   8) : /* IGNORED: autorepeat off           */ break; //VT100
    case token_csi_pr('s',   8) : /* IGNORED: autorepeat on            */ break; //VT100
    case token_csi_pr('r',   8) : /* IGNORED: autorepeat off           */ break; //VT100

    case token_csi_pr('h',   9) : /* IGNORED: interlace                */ break; //VT100
    case token_csi_pr('l',   9) : /* IGNORED: interlace                */ break; //VT100
    case token_csi_pr('s',   9) : /* IGNORED: interlace                */ break; //VT100
    case token_csi_pr('r',   9) : /* IGNORED: interlace                */ break; //VT100

    case token_csi_pr('h',  12) : /* IGNORED: Cursor blink             */ break; //att610
    case token_csi_pr('l',  12) : /* IGNORED: Cursor blink             */ break; //att610
    case token_csi_pr('s',  12) : /* IGNORED: Cursor blink             */ break; //att610
    case token_csi_pr('r',  12) : /* IGNORED: Cursor blink             */ break; //att610

    case token_csi_pr('h',  25) :          setMode      (MODE_Cursor   ); break; //VT100
    case token_csi_pr('l',  25) :        resetMode      (MODE_Cursor   ); break; //VT100
    case token_csi_pr('s',  25) :         saveMode      (MODE_Cursor   ); break; //VT100
    case token_csi_pr('r',  25) :      restoreMode      (MODE_Cursor   ); break; //VT100

    case token_csi_pr('h',  40) :         setMode(MODE_Allow132Columns ); break; // XTERM
    case token_csi_pr('l',  40) :       resetMode(MODE_Allow132Columns ); break; // XTERM

    case token_csi_pr('h',  41) : /* IGNORED: obsolete more(1) fix     */ break; //XTERM
    case token_csi_pr('l',  41) : /* IGNORED: obsolete more(1) fix     */ break; //XTERM
    case token_csi_pr('s',  41) : /* IGNORED: obsolete more(1) fix     */ break; //XTERM
    case token_csi_pr('r',  41) : /* IGNORED: obsolete more(1) fix     */ break; //XTERM

    case token_csi_pr('h',  47) :          setMode      (MODE_AppScreen); break; //VT100
    case token_csi_pr('l',  47) :        resetMode      (MODE_AppScreen); break; //VT100
    case token_csi_pr('s',  47) :         saveMode      (MODE_AppScreen); break; //XTERM
    case token_csi_pr('r',  47) :      restoreMode      (MODE_AppScreen); break; //XTERM

    case token_csi_pr('h',  67) : /* IGNORED: DECBKM                   */ break; //XTERM
    case token_csi_pr('l',  67) : /* IGNORED: DECBKM                   */ break; //XTERM
    case token_csi_pr('s',  67) : /* IGNORED: DECBKM                   */ break; //XTERM
    case token_csi_pr('r',  67) : /* IGNORED: DECBKM                   */ break; //XTERM
Waldo Bastian's avatar
Waldo Bastian committed
839

840
841
842
843
844
845
    // XTerm defines the following modes:
    // SET_VT200_MOUSE             1000
    // SET_VT200_HIGHLIGHT_MOUSE   1001
    // SET_BTN_EVENT_MOUSE         1002
    // SET_ANY_EVENT_MOUSE         1003
    //
846

847
848
849
    //Note about mouse modes:
    //There are four mouse modes which xterm-compatible terminals can support - 1000,1001,1002,1003
    //Konsole currently supports mode 1000 (basic mouse press and release) and mode 1002 (dragging the mouse).
850
    //TODO:  Implementation of mouse modes 1001 (something called hilight tracking) and
851
852
    //1003 (a slight variation on dragging the mouse)
    //
853

854
855
856
857
    case token_csi_pr('h', 1000) :          setMode      (MODE_Mouse1000); break; //XTERM
    case token_csi_pr('l', 1000) :        resetMode      (MODE_Mouse1000); break; //XTERM
    case token_csi_pr('s', 1000) :         saveMode      (MODE_Mouse1000); break; //XTERM
    case token_csi_pr('r', 1000) :      restoreMode      (MODE_Mouse1000); break; //XTERM
858

859
860
861
862
    case token_csi_pr('h', 1001) : /* IGNORED: hilite mouse tracking    */ break; //XTERM
    case token_csi_pr('l', 1001) :        resetMode      (MODE_Mouse1001); break; //XTERM
    case token_csi_pr('s', 1001) : /* IGNORED: hilite mouse tracking    */ break; //XTERM
    case token_csi_pr('r', 1001) : /* IGNORED: hilite mouse tracking    */ break; //XTERM
863

864
865
866
867
    case token_csi_pr('h', 1002) :          setMode      (MODE_Mouse1002); break; //XTERM
    case token_csi_pr('l', 1002) :        resetMode      (MODE_Mouse1002); break; //XTERM
    case token_csi_pr('s', 1002) :         saveMode      (MODE_Mouse1002); break; //XTERM
    case token_csi_pr('r', 1002) :      restoreMode      (MODE_Mouse1002); break; //XTERM
868

869
870
871
872
    case token_csi_pr('h', 1003) :          setMode      (MODE_Mouse1003); break; //XTERM
    case token_csi_pr('l', 1003) :        resetMode      (MODE_Mouse1003); break; //XTERM
    case token_csi_pr('s', 1003) :         saveMode      (MODE_Mouse1003); break; //XTERM
    case token_csi_pr('r', 1003) :      restoreMode      (MODE_Mouse1003); break; //XTERM
873

874
875
    case token_csi_pr('h',  1004) : _reportFocusEvents = true; break;
    case token_csi_pr('l',  1004) : _reportFocusEvents = false; break;
876

877
878
879
880
    case token_csi_pr('h', 1005) :          setMode      (MODE_Mouse1005); break; //XTERM
    case token_csi_pr('l', 1005) :        resetMode      (MODE_Mouse1005); break; //XTERM
    case token_csi_pr('s', 1005) :         saveMode      (MODE_Mouse1005); break; //XTERM
    case token_csi_pr('r', 1005) :      restoreMode      (MODE_Mouse1005); break; //XTERM
881

882
883
884
885
    case token_csi_pr('h', 1006) :          setMode      (MODE_Mouse1006); break; //XTERM
    case token_csi_pr('l', 1006) :        resetMode      (MODE_Mouse1006); break; //XTERM
    case token_csi_pr('s', 1006) :         saveMode      (MODE_Mouse1006); break; //XTERM
    case token_csi_pr('r', 1006) :      restoreMode      (MODE_Mouse1006); break; //XTERM
886

887
888
889
890
891
    case token_csi_pr('h', 1007) :          setMode      (MODE_Mouse1007); break; //XTERM
    case token_csi_pr('l', 1007) :        resetMode      (MODE_Mouse1007); break; //XTERM
    case token_csi_pr('s', 1007) :         saveMode      (MODE_Mouse1007); break; //XTERM
    case token_csi_pr('r', 1007) :      restoreMode      (MODE_Mouse1007); break; //XTERM

892
893
894
895
    case token_csi_pr('h', 1015) :          setMode      (MODE_Mouse1015); break; //URXVT
    case token_csi_pr('l', 1015) :        resetMode      (MODE_Mouse1015); break; //URXVT
    case token_csi_pr('s', 1015) :         saveMode      (MODE_Mouse1015); break; //URXVT
    case token_csi_pr('r', 1015) :      restoreMode      (MODE_Mouse1015); break; //URXVT
896

897
    case token_csi_pr('h', 1034) : /* IGNORED: 8bitinput activation     */ break; //XTERM
898

899
900
901
902
    case token_csi_pr('h', 1047) :          setMode      (MODE_AppScreen); break; //XTERM
    case token_csi_pr('l', 1047) : _screen[1]->clearEntireScreen(); resetMode(MODE_AppScreen); break; //XTERM
    case token_csi_pr('s', 1047) :         saveMode      (MODE_AppScreen); break; //XTERM
    case token_csi_pr('r', 1047) :      restoreMode      (MODE_AppScreen); break; //XTERM
903
904

    //FIXME: Unitoken: save translations
905
906
907
908
    case token_csi_pr('h', 1048) :      saveCursor           (          ); break; //XTERM
    case token_csi_pr('l', 1048) :      restoreCursor        (          ); break; //XTERM
    case token_csi_pr('s', 1048) :      saveCursor           (          ); break; //XTERM
    case token_csi_pr('r', 1048) :      restoreCursor        (          ); break; //XTERM
909
910
911

    //FIXME: every once new sequences like this pop up in xterm.
    //       Here's a guess of what they could mean.
912
913
    case token_csi_pr('h', 1049) : saveCursor(); _screen[1]->clearEntireScreen(); setMode(MODE_AppScreen); break; //XTERM
    case token_csi_pr('l', 1049) : resetMode(MODE_AppScreen); restoreCursor(); break; //XTERM
914

915
916
917
918
    case token_csi_pr('h', 2004) :          setMode      (MODE_BracketedPaste); break; //XTERM
    case token_csi_pr('l', 2004) :        resetMode      (MODE_BracketedPaste); break; //XTERM
    case token_csi_pr('s', 2004) :         saveMode      (MODE_BracketedPaste); break; //XTERM
    case token_csi_pr('r', 2004) :      restoreMode      (MODE_BracketedPaste); break; //XTERM
919

920
921
    // Set Cursor Style (DECSCUSR), VT520, with the extra xterm sequences
    // the first one is a special case, 'ESC[ q', which mimics 'ESC[1 q'
922
    // Using 0 to reset to default is matching VTE, but not any official standard.
923
    case token_csi_sp ('q'    ) : emit setCursorStyleRequest(Enum::BlockCursor,     true);  break;
924
    case token_csi_psp('q',  0) : emit resetCursorStyleRequest();                           break;
925
926
927
928
929
930
931
    case token_csi_psp('q',  1) : emit setCursorStyleRequest(Enum::BlockCursor,     true);  break;
    case token_csi_psp('q',  2) : emit setCursorStyleRequest(Enum::BlockCursor,     false); break;
    case token_csi_psp('q',  3) : emit setCursorStyleRequest(Enum::UnderlineCursor, true);  break;
    case token_csi_psp('q',  4) : emit setCursorStyleRequest(Enum::UnderlineCursor, false); break;
    case token_csi_psp('q',  5) : emit setCursorStyleRequest(Enum::IBeamCursor,     true);  break;
    case token_csi_psp('q',  6) : emit setCursorStyleRequest(Enum::IBeamCursor,     false); break;

Waldo Bastian's avatar
Waldo Bastian committed
932
    //FIXME: weird DEC reset sequence
933
    case token_csi_pe('p'      ) : /* IGNORED: reset         (        ) */ break;
Waldo Bastian's avatar
Waldo Bastian committed
934

935
    //FIXME: when changing between vt52 and ansi mode evtl do some resetting.
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
    case token_vt52('A'      ) : _currentScreen->cursorUp             (         1); break; //VT52
    case token_vt52('B'      ) : _currentScreen->cursorDown           (         1); break; //VT52
    case token_vt52('C'      ) : _currentScreen->cursorRight          (         1); break; //VT52
    case token_vt52('D'      ) : _currentScreen->cursorLeft           (         1); break; //VT52

    case token_vt52('F'      ) :      setAndUseCharset     (0,    '0'); break; //VT52
    case token_vt52('G'      ) :      setAndUseCharset     (0,    'B'); break; //VT52

    case token_vt52('H'      ) : _currentScreen->setCursorYX          (1,1       ); break; //VT52
    case token_vt52('I'      ) : _currentScreen->reverseIndex         (          ); break; //VT52
    case token_vt52('J'      ) : _currentScreen->clearToEndOfScreen   (          ); break; //VT52
    case token_vt52('K'      ) : _currentScreen->clearToEndOfLine     (          ); break; //VT52
    case token_vt52('Y'      ) : _currentScreen->setCursorYX          (p-31,q-31 ); break; //VT52
    case token_vt52('Z'      ) :      reportTerminalType   (           ); break; //VT52
    case token_vt52('<'      ) :          setMode      (MODE_Ansi     ); break; //VT52
    case token_vt52('='      ) :          setMode      (MODE_AppKeyPad); break; //VT52
    case token_vt52('>'      ) :        resetMode      (MODE_AppKeyPad); break; //VT52

    case token_csi_pg('c'      ) :  reportSecondaryAttributes(          ); break; //VT100
955

956
957
    default:
        reportDecodingError();
958
        break;
Kurt Hindenburg's avatar
Kurt Hindenburg committed
959
  }
960
961
}

962
963
void Vt102Emulation::clearScreenAndSetColumns(int columnCount)
{
964
    setImageSize(_currentScreen->getLines(),columnCount);
965
    clearEntireScreen();
966
    setDefaultMargins();
967
968
969
    _currentScreen->setCursorYX(0,0);
}

970
void Vt102Emulation::sendString(const QByteArray& s)
971
{
972
    emit sendData(s);
973
974
}

975
void Vt102Emulation::reportCursorPosition()
976
{
977
  char tmp[30];
978
  snprintf(tmp, sizeof(tmp), "\033[%d;%dR", _currentScreen->getCursorY()+1, _currentScreen->getCursorX()+1);
979
  sendString(tmp);
980
981
}

982
void Vt102Emulation::reportTerminalType()
983
{
984
  // Primary device attribute response (Request was: ^[[0c or ^[[c (from TT321 Users Guide))
985
986
987
988
  // VT220:  ^[[?63;1;2;3;6;7;8c   (list deps on emul. capabilities)
  // VT100:  ^[[?1;2c
  // VT101:  ^[[?1;0c
  // VT102:  ^[[?6v
Kurt Hindenburg's avatar
Kurt Hindenburg committed
989
  if (getMode(MODE_Ansi)) {
990
    sendString("\033[?1;2c"); // I'm a VT100
Kurt Hindenburg's avatar
Kurt Hindenburg committed
991
  } else {
992
    sendString("\033/Z"); // I'm a VT52
Kurt Hindenburg's avatar
Kurt Hindenburg committed
993
  }
994
995
}

996
void Vt102Emulation::reportSecondaryAttributes()
997
{
998
  // Secondary device attribute response (Request was: ^[[>0c or ^[[>c)
Kurt Hindenburg's avatar
Kurt Hindenburg committed
999
  if (getMode(MODE_Ansi)) {
1000
    sendString("\033[>0;115;0c"); // Why 115?  ;)
Kurt Hindenburg's avatar
Kurt Hindenburg committed
1001
  } else {
1002
    sendString("\033/Z");         // FIXME I don't think VT52 knows about it but kept for
Kurt Hindenburg's avatar
Kurt Hindenburg committed
1003
  }
1004
1005
1006
                                  // konsoles backward compatibility.
}

1007
1008
1009
/* DECREPTPARM – Report Terminal Parameters
    ESC [ <sol>; <par>; <nbits>; <xspeed>; <rspeed>; <clkmul>; <flags> x

1010
    https://vt100.net/docs/vt100-ug/chapter3.html
1011
*/
1012
void Vt102Emulation::reportTerminalParms(int p)
1013
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
1014
    char tmp[100];
1015
1016
1017
1018
1019
1020
1021
1022
1023
/*
   sol=1: This message is a request; report in response to a request.
   par=1: No parity set
   nbits=1: 8 bits per character
   xspeed=112: 9600
   rspeed=112: 9600
   clkmul=1: The bit rate multiplier is 16.
   flags=0: None
*/
Kurt Hindenburg's avatar
Kurt Hindenburg committed
1024
1025
    snprintf(tmp, sizeof(tmp), "\033[%d;1;1;112;112;1;0x", p); // not really true.
    sendString(tmp);
1026
1027
}

1028
void Vt102Emulation::reportStatus()
1029
{
1030
    sendString("\033[0n"); //VT100. Device status report. 0 = Ready.
1031
1032
}

1033
void Vt102Emulation::reportAnswerBack()
1034
{
Kurt Hindenburg's avatar