File indexing completed on 2025-03-16 08:11:26
0001 /* 0002 SPDX-FileCopyrightText: 2007-2008 Robert Knight <robertknight@gmail.com> 0003 SPDX-FileCopyrightText: 1997, 1998 Lars Doelle <lars.doelle@on-line.de> 0004 0005 SPDX-License-Identifier: GPL-2.0-or-later 0006 */ 0007 0008 #ifndef PTY_H 0009 #define PTY_H 0010 0011 // Qt 0012 #include <QSize> 0013 0014 // KDE 0015 #include <KPtyProcess> 0016 0017 namespace Konsole 0018 { 0019 /** 0020 * The Pty class is used to start the terminal process, 0021 * send data to it, receive data from it and manipulate 0022 * various properties of the pseudo-teletype interface 0023 * used to communicate with the process. 0024 * 0025 * To use this class, construct an instance and connect 0026 * to the sendData slot and receivedData signal to 0027 * send data to or receive data from the process. 0028 * 0029 * To start the terminal process, call the start() method 0030 * with the program name and appropriate arguments. 0031 */ 0032 class Pty : public KPtyProcess 0033 { 0034 Q_OBJECT 0035 0036 public: 0037 /** 0038 * Constructs a new Pty. 0039 * 0040 * Connect to the sendData() slot and receivedData() signal to prepare 0041 * for sending and receiving data from the terminal process. 0042 * 0043 * To start the terminal process, call the run() method with the 0044 * name of the program to start and appropriate arguments. 0045 */ 0046 explicit Pty(QObject *parent = nullptr); 0047 0048 /** 0049 * Construct a process using an open pty master. 0050 * See KPtyProcess::KPtyProcess() 0051 */ 0052 explicit Pty(int ptyMasterFd, QObject *parent = nullptr); 0053 0054 ~Pty() override; 0055 0056 /** 0057 * Starts the terminal process. 0058 * 0059 * Returns 0 if the process was started successfully or non-zero 0060 * otherwise. 0061 * 0062 * @param program Path to the program to start 0063 * @param arguments Arguments to pass to the program being started 0064 * @param environment A list of key=value pairs which will be added 0065 * to the environment for the new process. At the very least this 0066 * should include an assignment for the TERM environment variable. 0067 */ 0068 int start(const QString &program, const QStringList &arguments, const QStringList &environment); 0069 0070 /** Control whether the pty device is writeable by group members. */ 0071 void setWriteable(bool writeable); 0072 0073 /** 0074 * Enables or disables Xon/Xoff flow control. The flow control setting 0075 * may be changed later by a terminal application, so flowControlEnabled() 0076 * may not equal the value of @p on in the previous call to setFlowControlEnabled() 0077 */ 0078 void setFlowControlEnabled(bool on); 0079 0080 /** Queries the terminal state and returns true if Xon/Xoff flow control is enabled. */ 0081 bool flowControlEnabled() const; 0082 0083 /** 0084 * Sets the size of the window (in columns and lines of characters, 0085 * and width and height in pixels) used by this teletype. 0086 */ 0087 void setWindowSize(int columns, int lines, int width, int height); 0088 0089 /** Returns the size of the window used by this teletype in characters. See setWindowSize() */ 0090 QSize windowSize() const; 0091 0092 /** Returns the size of the window used by this teletype in pixels. See setWindowSize() */ 0093 QSize pixelSize() const; 0094 0095 /** 0096 * Sets the special character for erasing previous not-yet-erased character. 0097 * See termios(3) for detailed description. 0098 */ 0099 void setEraseChar(char eraseChar); 0100 0101 /** */ 0102 char eraseChar() const; 0103 0104 /** 0105 * Sets the initial working directory. 0106 */ 0107 void setInitialWorkingDirectory(const QString &dir); 0108 0109 /** 0110 * Returns the process id of the teletype's current foreground 0111 * process. This is the process which is currently reading 0112 * input sent to the terminal via. sendData() 0113 * 0114 * If there is a problem reading the foreground process group, 0115 * 0 will be returned. 0116 */ 0117 int foregroundProcessGroup() const; 0118 0119 /** 0120 * Close the underlying pty master/slave pair. 0121 */ 0122 void closePty(); 0123 0124 public Q_SLOTS: 0125 /** 0126 * Put the pty into UTF-8 mode on systems which support it. 0127 */ 0128 void setUtf8Mode(bool on); 0129 0130 /** 0131 * Sends data to the process currently controlling the 0132 * teletype ( whose id is returned by foregroundProcessGroup() ) 0133 * 0134 * @param data the data to send. 0135 */ 0136 void sendData(const QByteArray &data); 0137 0138 Q_SIGNALS: 0139 /** 0140 * Emitted when a new block of data is received from 0141 * the teletype. 0142 * 0143 * @param buffer Pointer to the data received. 0144 * @param length Length of @p buffer 0145 */ 0146 void receivedData(const char *buffer, int length); 0147 0148 protected: 0149 // TODO: remove this; the method is removed from QProcess in Qt6 0150 // instead use setChildProcessModifier() in the constructor 0151 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) 0152 void setupChildProcess() override; 0153 #endif 0154 0155 private Q_SLOTS: 0156 // called when data is received from the terminal process 0157 void dataReceived(); 0158 0159 private: 0160 void init(); 0161 0162 // takes a list of key=value pairs and adds them 0163 // to the environment for the process 0164 void addEnvironmentVariables(const QStringList &environment); 0165 0166 int _windowColumns; 0167 int _windowLines; 0168 int _windowWidth; 0169 int _windowHeight; 0170 char _eraseChar; 0171 bool _xonXoff; 0172 bool _utf8; 0173 }; 0174 } 0175 0176 #endif // PTY_H