summaryrefslogtreecommitdiff
path: root/src/terminal/internal/fqterm_decode.h
blob: 08806683d9c3edeba95a26fbd04c0830bcbda6bb (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
// SPDX-License-Identifier: GPL-2.0-or-later

#ifndef FQTERM_DECODE_H
#define FQTERM_DECODE_H

#include <QObject>
#include "fqterm_text_line.h"

namespace FQTerm {

class FQTermDecode;
class FQTermBuffer;
class FQTermTelnet;

// this for FSM
typedef void(FQTermDecode:: *StateFunc)();

struct StateOption {
  int byte; // char value to look for; -1==end/default
  StateFunc action;
  StateOption *nextState;
};

class FQTermDecode: public QObject {
  Q_OBJECT;
 public:
  FQTermDecode(FQTermBuffer *, int server_encoding);
  ~FQTermDecode();


  // translate data from telnet socket to our own buffer
  // return how many bytes are processed.
  int decode(const char *cstr, int length);

  bool bellReceive() {
    return isBell_;
  }

  void setServerEncoding(int encoding) {
    server_encoding_ = encoding;
  }


  //signals:
  //	void decodeFinished();
 signals:
  void mouseMode(bool);
  void enqReceived();
  void onTitleSet(const QString&);
  public slots:
    void onCaretChangeRow();
 private:
  // escape sequence actions
  // you'd better see FSM structure array in FQTermDecode.cpp

  void setAttr();
  void setMargins();
  void termReset();
  
  // char screen functions
  void deleteStr();
  void deleteLine();
  void insertStr();
  void insertLine();
  void eraseStr();
  void eraseLine();
  void eraseScreen();

  // cursor functions
  void saveCursor();
  void restoreCursor();
  void cursorPosition();

  // Move cursor, stop at boundary.
  void cursorLeft();
  void cursorDown();
  void cursorRight();
  void cursorUp();

  // Move cursor, scroll at boundary.
  void moveCursorUp();
  void moveCursorDown();
  void nextLine();

  void selectG0A();
  void selectG0B();
  void selectG00();
  void selectG01();
  void selectG02();

  void selectG1A();
  void selectG1B();
  void selectG10();
  void selectG11();
  void selectG12();

  // other escape sequence actions
  void normalInput();

  //title settings
  void setTitle();
  void collectText();
  void clearText();

  // action parameters
  void clearParam();
  void paramDigit();
  void nextParam();

  // non-printing characters
  void cr(), lf(), ff(), bell(), tab(), bs(), del(), g0(), g1(), enq();

  void addTabStop();
  void clearTabStop();
  
  void setMode();
  void resetMode();
  void setDecPrivateMode();
  void resetDecPrivateMode();
  void setNumericKeypad();
  void setAppKeypad();

  void saveMode();
  void restoreMode();

  // video alignment test - fill screen with E's.
  void fillScreen();

  void test();

 private:

  bool isBell_;
  // short currentAttr_, defaultAttr_;
  unsigned char current_color_, default_color_;
  unsigned char current_attr_, default_attr_;

  // ********** ansi decoder states ****************
  StateOption *current_state_;

  struct VT100StateMachine {
    static StateOption normal_state_[], esc_state_[], bracket_state_[];
    static StateOption private_state_[], sharp_state_[];
    static StateOption title_state_[], title_text_state_[];
    static StateOption select_g0_charset_state_[], select_g1_charset_state_[];
  };

  static const char *getStateName(const StateOption *state);

  void logParam() const;
  

  bool interrupt_decode_;

  // ********** decoder		*****************
  const char *inputData_;
  int inputLength_, dataIndex_;

  int paramIndex_, param_[30];
  bool isParamAvailable_;

  QByteArray textParam_;

  bool savedMode_[30];
  bool currentMode_[30];

  FQTermBuffer *termBuffer_;
  int server_encoding_;

  QString bbs2unicode(const QByteArray &text);
  int processInput(QByteArray& result);
  QByteArray leftToDecode_;
};

}  // namespace FQTerm

#endif  // FQTERM_DECODE_H