1 | /* |
---|
2 | This file is part of Konsole, KDE's terminal emulator. |
---|
3 | |
---|
4 | Copyright (C) 2007 by Robert Knight <robertknight@gmail.com> |
---|
5 | Copyright (C) 1997,1998 by Lars Doelle <lars.doelle@on-line.de> |
---|
6 | |
---|
7 | Rewritten for QT4 by e_k <e_k at users.sourceforge.net>, Copyright (C)2008 |
---|
8 | |
---|
9 | This program is free software; you can redistribute it and/or modify |
---|
10 | it under the terms of the GNU General Public License as published by |
---|
11 | the Free Software Foundation; either version 2 of the License, or |
---|
12 | (at your option) any later version. |
---|
13 | |
---|
14 | This program is distributed in the hope that it will be useful, |
---|
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | GNU General Public License for more details. |
---|
18 | |
---|
19 | You should have received a copy of the GNU General Public License |
---|
20 | along with this program; if not, write to the Free Software |
---|
21 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
---|
22 | 02110-1301 USA. |
---|
23 | */ |
---|
24 | |
---|
25 | #ifndef PTY_H |
---|
26 | #define PTY_H |
---|
27 | |
---|
28 | // Qt |
---|
29 | #include <QtCore/QStringList> |
---|
30 | #include <QtCore/QVector> |
---|
31 | #include <QtCore/QList> |
---|
32 | #include <QtCore> |
---|
33 | |
---|
34 | #include "k3process.h" |
---|
35 | |
---|
36 | |
---|
37 | namespace Konsole |
---|
38 | { |
---|
39 | |
---|
40 | /** |
---|
41 | * The Pty class is used to start the terminal process, |
---|
42 | * send data to it, receive data from it and manipulate |
---|
43 | * various properties of the pseudo-teletype interface |
---|
44 | * used to communicate with the process. |
---|
45 | * |
---|
46 | * To use this class, construct an instance and connect |
---|
47 | * to the sendData slot and receivedData signal to |
---|
48 | * send data to or receive data from the process. |
---|
49 | * |
---|
50 | * To start the terminal process, call the start() method |
---|
51 | * with the program name and appropriate arguments. |
---|
52 | */ |
---|
53 | class Pty: public K3Process |
---|
54 | { |
---|
55 | Q_OBJECT |
---|
56 | |
---|
57 | public: |
---|
58 | |
---|
59 | /** |
---|
60 | * Constructs a new Pty. |
---|
61 | * |
---|
62 | * Connect to the sendData() slot and receivedData() signal to prepare |
---|
63 | * for sending and receiving data from the terminal process. |
---|
64 | * |
---|
65 | * To start the terminal process, call the run() method with the |
---|
66 | * name of the program to start and appropriate arguments. |
---|
67 | */ |
---|
68 | Pty(); |
---|
69 | ~Pty(); |
---|
70 | |
---|
71 | /** |
---|
72 | * Starts the terminal process. |
---|
73 | * |
---|
74 | * Returns 0 if the process was started successfully or non-zero |
---|
75 | * otherwise. |
---|
76 | * |
---|
77 | * @param program Path to the program to start |
---|
78 | * @param arguments Arguments to pass to the program being started |
---|
79 | * @param environment A list of key=value pairs which will be added |
---|
80 | * to the environment for the new process. At the very least this |
---|
81 | * should include an assignment for the TERM environment variable. |
---|
82 | * @param winid Specifies the value of the WINDOWID environment variable |
---|
83 | * in the process's environment. |
---|
84 | * @param addToUtmp Specifies whether a utmp entry should be created for |
---|
85 | * the pty used. See K3Process::setUsePty() |
---|
86 | * @param dbusService Specifies the value of the KONSOLE_DBUS_SERVICE |
---|
87 | * environment variable in the process's environment. |
---|
88 | * @param dbusSession Specifies the value of the KONSOLE_DBUS_SESSION |
---|
89 | * environment variable in the process's environment. |
---|
90 | */ |
---|
91 | int start( const QString& program, |
---|
92 | const QStringList& arguments, |
---|
93 | const QStringList& environment, |
---|
94 | ulong winid, |
---|
95 | bool addToUtmp |
---|
96 | // const QString& dbusService, |
---|
97 | // const QString& dbusSession |
---|
98 | ); |
---|
99 | |
---|
100 | /** TODO: Document me */ |
---|
101 | void setWriteable(bool writeable); |
---|
102 | |
---|
103 | /** |
---|
104 | * Enables or disables Xon/Xoff flow control. |
---|
105 | */ |
---|
106 | void setXonXoff(bool on); |
---|
107 | |
---|
108 | /** |
---|
109 | * Sets the size of the window (in lines and columns of characters) |
---|
110 | * used by this teletype. |
---|
111 | */ |
---|
112 | void setWindowSize(int lines, int cols); |
---|
113 | |
---|
114 | /** Returns the size of the window used by this teletype. See setWindowSize() */ |
---|
115 | QSize windowSize() const; |
---|
116 | |
---|
117 | /** TODO Document me */ |
---|
118 | void setErase(char erase); |
---|
119 | |
---|
120 | /** */ |
---|
121 | char erase() const; |
---|
122 | |
---|
123 | /** |
---|
124 | * Returns the process id of the teletype's current foreground |
---|
125 | * process. This is the process which is currently reading |
---|
126 | * input sent to the terminal via. sendData() |
---|
127 | * |
---|
128 | * If there is a problem reading the foreground process group, |
---|
129 | * 0 will be returned. |
---|
130 | */ |
---|
131 | int foregroundProcessGroup() const; |
---|
132 | |
---|
133 | /** |
---|
134 | * Returns whether the buffer used to send data to the |
---|
135 | * terminal process is full. |
---|
136 | */ |
---|
137 | bool bufferFull() const { return _bufferFull; } |
---|
138 | |
---|
139 | |
---|
140 | public slots: |
---|
141 | |
---|
142 | /** |
---|
143 | * Put the pty into UTF-8 mode on systems which support it. |
---|
144 | */ |
---|
145 | void setUtf8Mode(bool on); |
---|
146 | |
---|
147 | /** |
---|
148 | * Suspend or resume processing of data from the standard |
---|
149 | * output of the terminal process. |
---|
150 | * |
---|
151 | * See K3Process::suspend() and K3Process::resume() |
---|
152 | * |
---|
153 | * @param lock If true, processing of output is suspended, |
---|
154 | * otherwise processing is resumed. |
---|
155 | */ |
---|
156 | void lockPty(bool lock); |
---|
157 | |
---|
158 | /** |
---|
159 | * Sends data to the process currently controlling the |
---|
160 | * teletype ( whose id is returned by foregroundProcessGroup() ) |
---|
161 | * |
---|
162 | * @param buffer Pointer to the data to send. |
---|
163 | * @param length Length of @p buffer. |
---|
164 | */ |
---|
165 | void sendData(const char* buffer, int length); |
---|
166 | |
---|
167 | signals: |
---|
168 | |
---|
169 | /** |
---|
170 | * Emitted when the terminal process terminates. |
---|
171 | * |
---|
172 | * @param exitCode The status code which the process exited with. |
---|
173 | */ |
---|
174 | void done(int exitCode); |
---|
175 | |
---|
176 | /** |
---|
177 | * Emitted when a new block of data is received from |
---|
178 | * the teletype. |
---|
179 | * |
---|
180 | * @param buffer Pointer to the data received. |
---|
181 | * @param length Length of @p buffer |
---|
182 | */ |
---|
183 | void receivedData(const char* buffer, int length); |
---|
184 | |
---|
185 | /** |
---|
186 | * Emitted when the buffer used to send data to the terminal |
---|
187 | * process becomes empty, i.e. all data has been sent. |
---|
188 | */ |
---|
189 | void bufferEmpty(); |
---|
190 | |
---|
191 | |
---|
192 | private slots: |
---|
193 | |
---|
194 | // called when terminal process exits |
---|
195 | void donePty(); |
---|
196 | // called when data is received from the terminal process |
---|
197 | void dataReceived(K3Process*, char* buffer, int length); |
---|
198 | // sends the first enqueued buffer of data to the |
---|
199 | // terminal process |
---|
200 | void doSendJobs(); |
---|
201 | // called when the terminal process is ready to |
---|
202 | // receive more data |
---|
203 | void writeReady(); |
---|
204 | |
---|
205 | private: |
---|
206 | // takes a list of key=value pairs and adds them |
---|
207 | // to the environment for the process |
---|
208 | void addEnvironmentVariables(const QStringList& environment); |
---|
209 | |
---|
210 | // enqueues a buffer of data to be sent to the |
---|
211 | // terminal process |
---|
212 | void appendSendJob(const char* buffer, int length); |
---|
213 | |
---|
214 | // a buffer of data in the queue to be sent to the |
---|
215 | // terminal process |
---|
216 | class SendJob { |
---|
217 | public: |
---|
218 | SendJob() {} |
---|
219 | SendJob(const char* b, int len) : buffer(len) |
---|
220 | { |
---|
221 | memcpy( buffer.data() , b , len ); |
---|
222 | } |
---|
223 | |
---|
224 | const char* data() const { return buffer.constData(); } |
---|
225 | int length() const { return buffer.size(); } |
---|
226 | private: |
---|
227 | QVector<char> buffer; |
---|
228 | }; |
---|
229 | |
---|
230 | QList<SendJob> _pendingSendJobs; |
---|
231 | bool _bufferFull; |
---|
232 | |
---|
233 | int _windowColumns; |
---|
234 | int _windowLines; |
---|
235 | char _eraseChar; |
---|
236 | bool _xonXoff; |
---|
237 | bool _utf8; |
---|
238 | KPty *_pty; |
---|
239 | }; |
---|
240 | |
---|
241 | } |
---|
242 | |
---|
243 | #endif // PTY_H |
---|