File indexing completed on 2025-01-19 04:23:33

0001 /*
0002     This file is part of Konsole, an X terminal.
0003 
0004     Copyright 2007-2008 by Robert Knight <robertknight@gmail.com>
0005     Copyright 1997,1998 by Lars Doelle <lars.doelle@on-line.de>
0006 
0007     This program is free software; you can redistribute it and/or modify
0008     it under the terms of the GNU General Public License as published by
0009     the Free Software Foundation; either version 2 of the License, or
0010     (at your option) any later version.
0011 
0012     This program is distributed in the hope that it will be useful,
0013     but WITHOUT ANY WARRANTY; without even the implied warranty of
0014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0015     GNU General Public License for more details.
0016 
0017     You should have received a copy of the GNU General Public License
0018     along with this program; if not, write to the Free Software
0019     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
0020     02110-1301  USA.
0021 */
0022 
0023 #ifndef VT102EMULATION_H
0024 #define VT102EMULATION_H
0025 
0026 // Standard Library
0027 #include <stdio.h>
0028 
0029 // Qt
0030 #include <QKeyEvent>
0031 #include <QHash>
0032 #include <QTimer>
0033 
0034 // Konsole
0035 #include "Emulation.h"
0036 #include "Screen.h"
0037 
0038 #define MODE_AppScreen       (MODES_SCREEN+0)   // Mode #1
0039 #define MODE_AppCuKeys       (MODES_SCREEN+1)   // Application cursor keys (DECCKM)
0040 #define MODE_AppKeyPad       (MODES_SCREEN+2)   //
0041 #define MODE_Mouse1000       (MODES_SCREEN+3)   // Send mouse X,Y position on press and release
0042 #define MODE_Mouse1001       (MODES_SCREEN+4)   // Use Hilight mouse tracking
0043 #define MODE_Mouse1002       (MODES_SCREEN+5)   // Use cell motion mouse tracking
0044 #define MODE_Mouse1003       (MODES_SCREEN+6)   // Use all motion mouse tracking
0045 #define MODE_Mouse1005       (MODES_SCREEN+7)   // Xterm-style extended coordinates
0046 #define MODE_Mouse1006       (MODES_SCREEN+8)   // 2nd Xterm-style extended coordinates
0047 #define MODE_Mouse1015       (MODES_SCREEN+9)   // Urxvt-style extended coordinates
0048 #define MODE_Ansi            (MODES_SCREEN+10)   // Use US Ascii for character sets G0-G3 (DECANM)
0049 #define MODE_132Columns      (MODES_SCREEN+11)  // 80 <-> 132 column mode switch (DECCOLM)
0050 #define MODE_Allow132Columns (MODES_SCREEN+12)  // Allow DECCOLM mode
0051 #define MODE_BracketedPaste  (MODES_SCREEN+13)  // Xterm-style bracketed paste mode
0052 #define MODE_total           (MODES_SCREEN+14)
0053 
0054 namespace Konsole
0055 {
0056 
0057 struct CharCodes
0058 {
0059   // coding info
0060   char charset[4]; //
0061   int  cu_cs;      // actual charset.
0062   bool graphic;    // Some VT100 tricks
0063   bool pound  ;    // Some VT100 tricks
0064   bool sa_graphic; // saved graphic
0065   bool sa_pound;   // saved pound
0066 };
0067 
0068 /**
0069  * Provides an xterm compatible terminal emulation based on the DEC VT102 terminal.
0070  * A full description of this terminal can be found at http://vt100.net/docs/vt102-ug/
0071  *
0072  * In addition, various additional xterm escape sequences are supported to provide
0073  * features such as mouse input handling.
0074  * See http://rtfm.etla.org/xterm/ctlseq.html for a description of xterm's escape
0075  * sequences.
0076  *
0077  */
0078 class Vt102Emulation : public Emulation
0079 {
0080 Q_OBJECT
0081 
0082 public:
0083   /** Constructs a new emulation */
0084   Vt102Emulation();
0085   ~Vt102Emulation();
0086 
0087   // reimplemented from Emulation
0088   virtual void clearEntireScreen();
0089   virtual void reset();
0090   virtual char eraseChar() const;
0091 
0092 public Q_SLOTS:
0093   // reimplemented from Emulation
0094   virtual void sendString(const char*,int length = -1);
0095   virtual void sendText(const QString& text);
0096   virtual void sendKeyEvent(QKeyEvent*);
0097   virtual void sendMouseEvent(int buttons, int column, int line, int eventType);
0098   virtual void focusLost();
0099   virtual void focusGained();
0100 
0101 protected:
0102   // reimplemented from Emulation
0103   virtual void setMode(int mode);
0104   virtual void resetMode(int mode);
0105   virtual void receiveChar(wchar_t cc);
0106 
0107 private Q_SLOTS:
0108   //causes changeTitle() to be emitted for each (int,QString) pair in pendingTitleUpdates
0109   //used to buffer multiple title updates
0110   void updateTitle();
0111 
0112 private:
0113   wchar_t applyCharset(wchar_t c);
0114   void setCharset(int n, int cs);
0115   void useCharset(int n);
0116   void setAndUseCharset(int n, int cs);
0117   void saveCursor();
0118   void restoreCursor();
0119   void resetCharset(int scrno);
0120   QKeyEvent * remapKeyModifiersForMac(QKeyEvent *event);
0121 
0122 
0123   void setMargins(int top, int bottom);
0124   //set margins for all screens back to their defaults
0125   void setDefaultMargins();
0126 
0127   // returns true if 'mode' is set or false otherwise
0128   bool getMode    (int mode);
0129   // saves the current boolean value of 'mode'
0130   void saveMode   (int mode);
0131   // restores the boolean value of 'mode'
0132   void restoreMode(int mode);
0133   // resets all modes
0134   // (except MODE_Allow132Columns)
0135   void resetModes();
0136 
0137   void resetTokenizer();
0138   #define MAX_TOKEN_LENGTH 256 // Max length of tokens (e.g. window title)
0139   void addToCurrentToken(wchar_t cc);
0140   wchar_t tokenBuffer[MAX_TOKEN_LENGTH]; //FIXME: overflow?
0141   int tokenBufferPos;
0142 #define MAXARGS 15
0143   void addDigit(int dig);
0144   void addArgument();
0145   int argv[MAXARGS];
0146   int argc;
0147   void initTokenizer();
0148   int prevCC;
0149 
0150   // Set of flags for each of the ASCII characters which indicates
0151   // what category they fall into (printable character, control, digit etc.)
0152   // for the purposes of decoding terminal output
0153   int charClass[256];
0154 
0155   void reportDecodingError();
0156 
0157   void processToken(int code, wchar_t p, int q);
0158   void processWindowAttributeChange();
0159   void requestWindowAttribute(int);
0160 
0161   void reportTerminalType();
0162   void reportSecondaryAttributes();
0163   void reportStatus();
0164   void reportAnswerBack();
0165   void reportCursorPosition();
0166   void reportTerminalParms(int p);
0167 
0168   void onScrollLock();
0169   void scrollLock(const bool lock);
0170 
0171   // clears the screen and resizes it to the specified
0172   // number of columns
0173   void clearScreenAndSetColumns(int columnCount);
0174 
0175   CharCodes _charset[2];
0176 
0177   class TerminalState
0178   {
0179   public:
0180     // Initializes all modes to false
0181     TerminalState()
0182     { memset(&mode,false,MODE_total * sizeof(bool)); }
0183 
0184     bool mode[MODE_total];
0185   };
0186 
0187   TerminalState _currentModes;
0188   TerminalState _savedModes;
0189 
0190   //hash table and timer for buffering calls to the session instance
0191   //to update the name of the session
0192   //or window title.
0193   //these calls occur when certain escape sequences are seen in the
0194   //output from the terminal
0195   QHash<int,QString> _pendingTitleUpdates;
0196   QTimer* _titleUpdateTimer;
0197 
0198     bool _reportFocusEvents;
0199 };
0200 
0201 }
0202 
0203 #endif // VT102EMULATION_H