Debian Codegraph @

lib/kptydevice.h

raw symbols SCIP
1 /*
2 * This file is a part of QTerminal - http://gitorious.org/qterminal
3 *
4 * This file was un-linked from KDE and modified
5 * by Maxim Bourmistrov <[email protected]>
6 *
7 */
8
9 /* This file is part of the KDE libraries
10
11 Copyright (C) 2007 Oswald Buddenhagen <[email protected]>
12
13 This library is free software; you can redistribute it and/or
14 modify it under the terms of the GNU Library General Public
15 License as published by the Free Software Foundation; either
16 version 2 of the License, or (at your option) any later version.
17
18 This library is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 Library General Public License for more details.
22
23 You should have received a copy of the GNU Library General Public License
24 along with this library; see the file COPYING.LIB. If not, write to
25 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 Boston, MA 02110-1301, USA.
27 */
28
29 #ifndef kptydev_h
30 #define kptydev_hcxx . . $ `lib/kptydevice.h:30:9`!
31
32 #include "kpty_p.h"cxx . . $ `<file>/lib/kpty_p.h`/
33
34 #include <QIODevice>
35
36 #define KMAXINTcxx . . $ `lib/kptydevice.h:36:9`! ((int)(~0U >> 1))
37
38 struct KPtyDevicePrivatecxx . . $ KPtyDevicePrivate#;
39 class QSocketNotifiercxx . . $ QSocketNotifier#;
40
41 #define Q_DECLARE_PRIVATE_MIcxx . . $ `lib/kptydevice.h:41:9`!(Class, SuperClass) \
42 inline Class##Private* d_func() { return reinterpret_cast<Class##Private *>(SuperClass::d_ptr); } \
43 inline const Class##Private* d_func() const { return reinterpret_cast<const Class##Private *>(SuperClass::d_ptr); } \
44 friend struct Class##Private;
45
46 /**
47 * Encapsulates KPty into a QIODevice, so it can be used with Q*Stream, etc.
48 */
49 class KPtyDevicecxx . . $ KPtyDevice# : public QIODevicecxx . . $ QIODevice#, public KPtycxx . . $ KPty# {
50 Q_OBJECTcxx . . $ KPtyDevice#staticMetaObject.
51 Q_DECLARE_PRIVATE_MIcxx . . $ KPty#d_ptr.(KPtyDevice, KPty)
52
53 public:
54
55 /**
56 * Constructor
57 */
58 KPtyDevicecxx . . $ KPtyDevice#KPtyDevice(749de1b6ae65bddd).(QObjectcxx . . $ QObject# *parent = nullptr);
59
60 /**
61 * Destructor:
62 *
63 * If the pty is still open, it will be closed. Note, however, that
64 * an utmp registration is @em not undone.
65 */
66 ~cxx . . $ KPtyDevice#`~KPtyDevice`(ced63f7c635d850d).KPtyDevicecxx . . $ KPtyDevice#() override;
67
68 /**
69 * Create a pty master/slave pair.
70 *
71 * @return true if a pty pair was successfully opened
72 */
73 bool opencxx . . $ KPtyDevice#open(dca164408be960c8).(OpenModecxx . . $ QIODevice#OpenMode# mode = ReadWritecxx . . $ QIODevice#ReadWrite. |cxx . . $ `operator|`(c9e45f682471e48d). Unbufferedcxx . . $ QIODevice#Unbuffered.) override;
74
75 /**
76 * Open using an existing pty master. The ownership of the fd
77 * remains with the caller, i.e., close() will not close the fd.
78 *
79 * This is useful if you wish to attach a secondary "controller" to an
80 * existing pty device such as a terminal widget.
81 * Note that you will need to use setSuspended() on both devices to
82 * control which one gets the incoming data from the pty.
83 *
84 * @param fd an open pty master file descriptor.
85 * @param mode the device mode to open the pty with.
86 * @return true if a pty pair was successfully opened
87 */
88 bool opencxx . . $ KPtyDevice#open(461c37ce8fbaff32).(int fd, OpenModecxx . . $ QIODevice#OpenMode# mode = ReadWritecxx . . $ QIODevice#ReadWrite. |cxx . . $ `operator|`(c9e45f682471e48d). Unbufferedcxx . . $ QIODevice#Unbuffered.);
89
90 /**
91 * Close the pty master/slave pair.
92 */
93 void closecxx . . $ KPtyDevice#close(49f6e7a06ebc5aa8).() override;
94
95 /**
96 * Sets whether the KPtyDevice monitors the pty for incoming data.
97 *
98 * When the KPtyDevice is suspended, it will no longer attempt to buffer
99 * data that becomes available from the pty and it will not emit any
100 * signals.
101 *
102 * Do not use on closed ptys.
103 * After a call to open(), the pty is not suspended. If you need to
104 * ensure that no data is read, call this function before the main loop
105 * is entered again (i.e., immediately after opening the pty).
106 */
107 void setSuspendedcxx . . $ KPtyDevice#setSuspended(b45ddcbc8423892c).(bool suspended);
108
109 /**
110 * Returns true if the KPtyDevice is not monitoring the pty for incoming
111 * data.
112 *
113 * Do not use on closed ptys.
114 *
115 * See setSuspended()
116 */
117 bool isSuspendedcxx . . $ KPtyDevice#isSuspended(50ce9a9e25b4a850).() const;
118
119 /**
120 * @return always true
121 */
122 bool isSequentialcxx . . $ KPtyDevice#isSequential(50ce9a9e25b4a850).() const override;
123
124 /**
125 * @reimp
126 */
127 bool canReadLinecxx . . $ KPtyDevice#canReadLine(50ce9a9e25b4a850).() const override;
128
129 /**
130 * @reimp
131 */
132 bool atEndcxx . . $ KPtyDevice#atEnd(50ce9a9e25b4a850).() const override;
133
134 /**
135 * @reimp
136 */
137 qint64cxx . . $ qint64# bytesAvailablecxx . . $ KPtyDevice#bytesAvailable(98833e9ab7de59a4).() const override;
138
139 /**
140 * @reimp
141 */
142 qint64cxx . . $ qint64# bytesToWritecxx . . $ KPtyDevice#bytesToWrite(98833e9ab7de59a4).() const override;
143
144 bool waitForBytesWrittencxx . . $ KPtyDevice#waitForBytesWritten(83c267b20bac841a).(int msecs = -1) override;
145 bool waitForReadyReadcxx . . $ KPtyDevice#waitForReadyRead(83c267b20bac841a).(int msecs = -1) override;
146
147
148 Q_SIGNALScxx . . $ `<external>/1208926524888476972/qobjectdefs.h:93:10`!:
149 /**
150 * Emitted when EOF is read from the PTY.
151 *
152 * Data may still remain in the buffers.
153 */
154 void readEofcxx . . $ KPtyDevice#readEof(49f6e7a06ebc5aa8).();
155
156 protected:
157 qint64cxx . . $ qint64# readDatacxx . . $ KPtyDevice#readData(2e638bc8ed65be6c).(char *data, qint64cxx . . $ qint64# maxSize) override;
158 qint64cxx . . $ qint64# readLineDatacxx . . $ KPtyDevice#readLineData(2e638bc8ed65be6c).(char *data, qint64cxx . . $ qint64# maxSize) override;
159 qint64cxx . . $ qint64# writeDatacxx . . $ KPtyDevice#writeData(93d4069f65218ea4).(const char *data, qint64cxx . . $ qint64# maxSize) override;
160
161 private:
162 Q_PRIVATE_SLOTcxx . . $ `<external>/1208926524888476972/qobjectdefs.h:94:10`!(d_func(), bool _k_canRead())
163 Q_PRIVATE_SLOTcxx . . $ `<external>/1208926524888476972/qobjectdefs.h:94:10`!(d_func(), bool _k_canWrite())
164 };
165
166 /////////////////////////////////////////////////////
167 // Helper. Remove when QRingBuffer becomes public. //
168 /////////////////////////////////////////////////////
169
170 #include <QByteArray>
171 #include <list>
172
173 #define CHUNKSIZEcxx . . $ `lib/kptydevice.h:173:9`! 4096
174
175 class KRingBuffercxx . . $ KRingBuffer#
176 {
177 public:
178 KRingBuffercxx . . $ KRingBuffer#KRingBuffer(49f6e7a06ebc5aa8).()
179 {
180 clearcxx . . $ KRingBuffer#clear(49f6e7a06ebc5aa8).();
181 }
182
183 void clearcxx . . $ KRingBuffer#clear(49f6e7a06ebc5aa8).()
184 {
185 bufferscxx . . $ KRingBuffer#buffers..clearcxx . . $ std/__cxx11/list#clear(ced63f7c635d850d).();
186 QByteArraycxx . . $ QByteArray# tmpcxx . . $ QByteArray#QByteArray(ced63f7c635d850d).;
187 tmp.resizecxx . . $ QByteArray#resize(d4f767463ce0a6b3).(CHUNKSIZEcxx . . $ `lib/kptydevice.h:173:9`!);
188 bufferscxx . . $ KRingBuffer#buffers..push_backcxx . . $ std/__cxx11/list#push_back(77d9f17553126ecb).(tmp);
189 headcxx . . $ KRingBuffer#head. = tailcxx . . $ KRingBuffer#tail. = 0;
190 totalSizecxx . . $ KRingBuffer#totalSize. = 0;
191 }
192
193 inline bool isEmptycxx . . $ KRingBuffer#isEmpty(50ce9a9e25b4a850).() const
194 {
195 return bufferscxx . . $ KRingBuffer#buffers..sizecxx . . $ std/__cxx11/list#size(c9b1e749b64d5edc).() == 1 && !tailcxx . . $ KRingBuffer#tail.;
196 }
197
198 inline int sizecxx . . $ KRingBuffer#size(455f465bc33b4cdf).() const
199 {
200 return totalSizecxx . . $ KRingBuffer#totalSize.;
201 }
202
203 inline int readSizecxx . . $ KRingBuffer#readSize(455f465bc33b4cdf).() const
204 {
205 return (bufferscxx . . $ KRingBuffer#buffers..sizecxx . . $ std/__cxx11/list#size(c9b1e749b64d5edc).() == 1 ? tailcxx . . $ KRingBuffer#tail. : bufferscxx . . $ KRingBuffer#buffers..frontcxx . . $ std/__cxx11/list#front(2eed24e44c70155c).().sizecxx . . $ QByteArray#size(455f465bc33b4cdf).()) - headcxx . . $ KRingBuffer#head.;
206 }
207
208 inline const char *readPointercxx . . $ KRingBuffer#readPointer(a9f41ea0e82d88cf).() const
209 {
210 Q_ASSERTcxx . . $ `<external>/15045578679651060896/qglobal.h:859:13`!(totalSize > 0);
211 return bufferscxx . . $ KRingBuffer#buffers..frontcxx . . $ std/__cxx11/list#front(2eed24e44c70155c).().constDatacxx . . $ QByteArray#constData(a9f41ea0e82d88cf).() + headcxx . . $ KRingBuffer#head.;
212 }
213
214 void freecxx . . $ KRingBuffer#free(d4f767463ce0a6b3).(int bytes)
215 {
216 totalSizecxx . . $ KRingBuffer#totalSize. -= bytes;
217 Q_ASSERTcxx . . $ KRingBuffer#totalSize.(totalSize >= 0);
218
219 forevercxx . . $ `<external>/15045578679651060896/qglobal.h:1149:13`! {
220 int nbs = readSizecxx . . $ KRingBuffer#readSize(455f465bc33b4cdf).();
221
222 if (bytes < nbs) {
223 headcxx . . $ KRingBuffer#head. += bytes;
224 if (headcxx . . $ KRingBuffer#head. == tailcxx . . $ KRingBuffer#tail. && bufferscxx . . $ KRingBuffer#buffers..sizecxx . . $ std/__cxx11/list#size(c9b1e749b64d5edc).() == 1) {
225 bufferscxx . . $ KRingBuffer#buffers..frontcxx . . $ std/__cxx11/list#front(d5f1ba0ed5a2655c).().resizecxx . . $ QByteArray#resize(d4f767463ce0a6b3).(CHUNKSIZEcxx . . $ `lib/kptydevice.h:173:9`!);
226 headcxx . . $ KRingBuffer#head. = tailcxx . . $ KRingBuffer#tail. = 0;
227 }
228 break;
229 }
230
231 bytes -= nbs;
232 if (bufferscxx . . $ KRingBuffer#buffers..sizecxx . . $ std/__cxx11/list#size(c9b1e749b64d5edc).() == 1) {
233 bufferscxx . . $ KRingBuffer#buffers..frontcxx . . $ std/__cxx11/list#front(d5f1ba0ed5a2655c).().resizecxx . . $ QByteArray#resize(d4f767463ce0a6b3).(CHUNKSIZEcxx . . $ `lib/kptydevice.h:173:9`!);
234 headcxx . . $ KRingBuffer#head. = tailcxx . . $ KRingBuffer#tail. = 0;
235 break;
236 }
237
238 bufferscxx . . $ KRingBuffer#buffers..pop_frontcxx . . $ std/__cxx11/list#pop_front(ced63f7c635d850d).();
239 headcxx . . $ KRingBuffer#head. = 0;
240 }
241 }
242
243 char *reservecxx . . $ KRingBuffer#reserve(ff224f080444ee45).(int bytes)
244 {
245 totalSizecxx . . $ KRingBuffer#totalSize. += bytes;
246
247 char *ptr;
248 if (tailcxx . . $ KRingBuffer#tail. + bytes <= bufferscxx . . $ KRingBuffer#buffers..backcxx . . $ std/__cxx11/list#back(d5f1ba0ed5a2655c).().sizecxx . . $ QByteArray#size(455f465bc33b4cdf).()) {
249 ptr = bufferscxx . . $ KRingBuffer#buffers..backcxx . . $ std/__cxx11/list#back(d5f1ba0ed5a2655c).().datacxx . . $ QByteArray#data(41208ce6ca65dd58).() + tailcxx . . $ KRingBuffer#tail.;
250 tailcxx . . $ KRingBuffer#tail. += bytes;
251 } else {
252 bufferscxx . . $ KRingBuffer#buffers..backcxx . . $ std/__cxx11/list#back(d5f1ba0ed5a2655c).().resizecxx . . $ QByteArray#resize(d4f767463ce0a6b3).(tailcxx . . $ KRingBuffer#tail.);
253 QByteArraycxx . . $ QByteArray# tmpcxx . . $ QByteArray#QByteArray(ced63f7c635d850d).;
254 tmp.resizecxx . . $ QByteArray#resize(d4f767463ce0a6b3).(qMaxcxx . . $ qMax(2a338fce5842aa6a).(CHUNKSIZEcxx . . $ `lib/kptydevice.h:173:9`!, bytes));
255 ptr = tmp.datacxx . . $ QByteArray#data(41208ce6ca65dd58).();
256 bufferscxx . . $ KRingBuffer#buffers..push_backcxx . . $ std/__cxx11/list#push_back(77d9f17553126ecb).(tmp);
257 tailcxx . . $ KRingBuffer#tail. = bytes;
258 }
259 return ptr;
260 }
261
262 // release a trailing part of the last reservation
263 inline void unreservecxx . . $ KRingBuffer#unreserve(d4f767463ce0a6b3).(int bytes)
264 {
265 totalSizecxx . . $ KRingBuffer#totalSize. -= bytes;
266 tailcxx . . $ KRingBuffer#tail. -= bytes;
267 }
268
269 inline void writecxx . . $ KRingBuffer#write(34d26762b42c433a).(const char *data, int len)
270 {
271 memcpy(reservecxx . . $ KRingBuffer#reserve(ff224f080444ee45).(len), data, len);
272 }
273
274 // Find the first occurrence of c and return the index after it.
275 // If c is not found until maxLength, maxLength is returned, provided
276 // it is smaller than the buffer size. Otherwise -1 is returned.
277 int indexAftercxx . . $ KRingBuffer#indexAfter(a8c99bbe05347276).(char c, int maxLength = KMAXINTcxx . . $ `lib/kptydevice.h:36:9`!) const
278 {
279 int index = 0;
280 int start = headcxx . . $ KRingBuffer#head.;
281 stdcxx . . $ std/::listcxx . . $ std/__cxx11/list#<QByteArray>::const_iteratorcxx . . $ std/__cxx11/list#const_iterator# it = bufferscxx . . $ KRingBuffer#buffers..cbegincxx . . $ std/__cxx11/list#cbegin(9afe7339530a999).();
282 forevercxx . . $ `<external>/15045578679651060896/qglobal.h:1149:13`! {
283 if (!maxLength)
284 return index;
285 if (index == sizecxx . . $ KRingBuffer#size(455f465bc33b4cdf).())
286 return -1;
287 const QByteArraycxx . . $ QByteArray# &buf = *cxx . . $ std/_List_const_iterator#`operator*`(2042464fc1fea03e).it;
288 ++cxx . . $ std/_List_const_iterator#operator++(aee61632d924a923).it;
289 int len = qMincxx . . $ qMin(2a338fce5842aa6a).((it ==cxx . . $ std/`operator==`(1e6c2fe662e4ed08). bufferscxx . . $ KRingBuffer#buffers..cendcxx . . $ std/__cxx11/list#cend(9afe7339530a999).() ? tailcxx . . $ KRingBuffer#tail. : buf.sizecxx . . $ QByteArray#size(455f465bc33b4cdf).()) - start,
290 maxLength);
291 const char *ptr = buf.datacxx . . $ QByteArray#data(a9f41ea0e82d88cf).() + start;
292 if (const char *rptr = (const char *)memchr(ptr, c, len))
293 return index + (rptr - ptr) + 1;
294 index += len;
295 maxLength -= len;
296 start = 0;
297 }
298 }
299
300 inline int lineSizecxx . . $ KRingBuffer#lineSize(7ccb52b1bfe01045).(int maxLength = KMAXINTcxx . . $ `lib/kptydevice.h:36:9`!) const
301 {
302 return indexAftercxx . . $ KRingBuffer#indexAfter(a8c99bbe05347276).('\n', maxLength);
303 }
304
305 inline bool canReadLinecxx . . $ KRingBuffer#canReadLine(50ce9a9e25b4a850).() const
306 {
307 return lineSizecxx . . $ KRingBuffer#lineSize(7ccb52b1bfe01045).() != -1;
308 }
309
310 int readcxx . . $ KRingBuffer#read(6abd07ddb1a912f0).(char *data, int maxLength)
311 {
312 int bytesToRead = qMincxx . . $ qMin(2a338fce5842aa6a).(sizecxx . . $ KRingBuffer#size(455f465bc33b4cdf).(), maxLength);
313 int readSoFar = 0;
314 while (readSoFar < bytesToRead) {
315 const char *ptr = readPointercxx . . $ KRingBuffer#readPointer(a9f41ea0e82d88cf).();
316 int bs = qMincxx . . $ qMin(2a338fce5842aa6a).(bytesToRead - readSoFar, readSizecxx . . $ KRingBuffer#readSize(455f465bc33b4cdf).());
317 memcpy(data + readSoFar, ptr, bs);
318 readSoFar += bs;
319 freecxx . . $ KRingBuffer#free(d4f767463ce0a6b3).(bs);
320 }
321 return readSoFar;
322 }
323
324 int readLinecxx . . $ KRingBuffer#readLine(6abd07ddb1a912f0).(char *data, int maxLength)
325 {
326 return readcxx . . $ KRingBuffer#read(6abd07ddb1a912f0).(data, lineSizecxx . . $ KRingBuffer#lineSize(7ccb52b1bfe01045).(qMincxx . . $ qMin(2a338fce5842aa6a).(maxLength, sizecxx . . $ KRingBuffer#size(455f465bc33b4cdf).())));
327 }
328
329 private:
330 stdcxx . . $ std/::listcxx . . $ std/__cxx11/list#<QByteArraycxx . . $ QByteArray#> bufferscxx . . $ KRingBuffer#buffers.;
331 int headcxx . . $ KRingBuffer#head., tailcxx . . $ KRingBuffer#tail.;
332 int totalSizecxx . . $ KRingBuffer#totalSize.;
333 };
334
335 struct KPtyDevicePrivatecxx . . $ KPtyDevicePrivate# : public KPtyPrivatecxx . . $ KPtyPrivate# {
336
337 Q_DECLARE_PUBLICcxx . . $ KPtyDevice#(KPtyDevice)
338
339 KPtyDevicePrivatecxx . . $ KPtyDevicePrivate#KPtyDevicePrivate(24a2b0b8e23ae922).(KPtycxx . . $ KPty#* parent) :
340 KPtyPrivatecxx . . $ KPtyPrivate#KPtyPrivate(24a2b0b8e23ae922).(parent),
341 emittedReadyReadcxx . . $ KPtyDevicePrivate#emittedReadyRead.(false), emittedBytesWrittencxx . . $ KPtyDevicePrivate#emittedBytesWritten.(false),
342 readNotifiercxx . . $ KPtyDevicePrivate#readNotifier.(nullptr), writeNotifiercxx . . $ KPtyDevicePrivate#writeNotifier.(nullptr)
343 {
344 }
345
346 bool _k_canReadcxx . . $ KPtyDevicePrivate#_k_canRead(c9857a73d68e59ba).();
347 bool _k_canWritecxx . . $ KPtyDevicePrivate#_k_canWrite(c9857a73d68e59ba).();
348
349 bool doWaitcxx . . $ KPtyDevicePrivate#doWait(9609253e83e89946).(int msecs, bool reading);
350 void finishOpencxx . . $ KPtyDevicePrivate#finishOpen(6a9e152534c2b05f).(QIODevicecxx . . $ QIODevice#::OpenModecxx . . $ QIODevice#OpenMode# mode);
351
352 bool emittedReadyReadcxx . . $ KPtyDevicePrivate#emittedReadyRead.;
353 bool emittedBytesWrittencxx . . $ KPtyDevicePrivate#emittedBytesWritten.;
354 QSocketNotifiercxx . . $ QSocketNotifier# *readNotifiercxx . . $ KPtyDevicePrivate#readNotifier.;
355 QSocketNotifiercxx . . $ QSocketNotifier# *writeNotifiercxx . . $ KPtyDevicePrivate#writeNotifier.;
356 KRingBuffercxx . . $ KRingBuffer# readBuffercxx . . $ KPtyDevicePrivate#readBuffer.;
357 KRingBuffercxx . . $ KRingBuffer# writeBuffercxx . . $ KPtyDevicePrivate#writeBuffer.;
358 };
359
360 #endif
361