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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
|
/*
* This file is a part of QTerminal - http://gitorious.org/qterminal
*
* This file was un-linked from KDE and modified
* by Maxim Bourmistrov <maxim@unixconn.com>
*
*/
/* This file is part of the KDE libraries
Copyright (C) 2007 Oswald Buddenhagen <ossi@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#ifndef kptydev_h
#define kptydev_h
#include "kpty_p.h"
#include <QIODevice>
#define KMAXINT ((int)(~0U >> 1))
struct KPtyDevicePrivate;
class QSocketNotifier;
#define Q_DECLARE_PRIVATE_MI(Class, SuperClass) \
inline Class##Private* d_func() { return reinterpret_cast<Class##Private *>(SuperClass::d_ptr); } \
inline const Class##Private* d_func() const { return reinterpret_cast<const Class##Private *>(SuperClass::d_ptr); } \
friend struct Class##Private;
/**
* Encapsulates KPty into a QIODevice, so it can be used with Q*Stream, etc.
*/
class KPtyDevice : public QIODevice, public KPty {
Q_OBJECT
Q_DECLARE_PRIVATE_MI(KPtyDevice, KPty)
public:
/**
* Constructor
*/
KPtyDevice(QObject *parent = nullptr);
/**
* Destructor:
*
* If the pty is still open, it will be closed. Note, however, that
* an utmp registration is @em not undone.
*/
~KPtyDevice() override;
/**
* Create a pty master/slave pair.
*
* @return true if a pty pair was successfully opened
*/
bool open(OpenMode mode = ReadWrite | Unbuffered) override;
/**
* Open using an existing pty master. The ownership of the fd
* remains with the caller, i.e., close() will not close the fd.
*
* This is useful if you wish to attach a secondary "controller" to an
* existing pty device such as a terminal widget.
* Note that you will need to use setSuspended() on both devices to
* control which one gets the incoming data from the pty.
*
* @param fd an open pty master file descriptor.
* @param mode the device mode to open the pty with.
* @return true if a pty pair was successfully opened
*/
bool open(int fd, OpenMode mode = ReadWrite | Unbuffered);
/**
* Close the pty master/slave pair.
*/
void close() override;
/**
* Sets whether the KPtyDevice monitors the pty for incoming data.
*
* When the KPtyDevice is suspended, it will no longer attempt to buffer
* data that becomes available from the pty and it will not emit any
* signals.
*
* Do not use on closed ptys.
* After a call to open(), the pty is not suspended. If you need to
* ensure that no data is read, call this function before the main loop
* is entered again (i.e., immediately after opening the pty).
*/
void setSuspended(bool suspended);
/**
* Returns true if the KPtyDevice is not monitoring the pty for incoming
* data.
*
* Do not use on closed ptys.
*
* See setSuspended()
*/
bool isSuspended() const;
/**
* @return always true
*/
bool isSequential() const override;
/**
* @reimp
*/
bool canReadLine() const override;
/**
* @reimp
*/
bool atEnd() const override;
/**
* @reimp
*/
qint64 bytesAvailable() const override;
/**
* @reimp
*/
qint64 bytesToWrite() const override;
bool waitForBytesWritten(int msecs = -1) override;
bool waitForReadyRead(int msecs = -1) override;
Q_SIGNALS:
/**
* Emitted when EOF is read from the PTY.
*
* Data may still remain in the buffers.
*/
void readEof();
protected:
qint64 readData(char *data, qint64 maxSize) override;
qint64 readLineData(char *data, qint64 maxSize) override;
qint64 writeData(const char *data, qint64 maxSize) override;
private:
Q_PRIVATE_SLOT(d_func(), bool _k_canRead())
Q_PRIVATE_SLOT(d_func(), bool _k_canWrite())
};
/////////////////////////////////////////////////////
// Helper. Remove when QRingBuffer becomes public. //
/////////////////////////////////////////////////////
#include <QByteArray>
#include <list>
#define CHUNKSIZE 4096
class KRingBuffer
{
public:
KRingBuffer()
{
clear();
}
void clear()
{
buffers.clear();
QByteArray tmp;
tmp.resize(CHUNKSIZE);
buffers.push_back(tmp);
head = tail = 0;
totalSize = 0;
}
inline bool isEmpty() const
{
return buffers.size() == 1 && !tail;
}
inline int size() const
{
return totalSize;
}
inline int readSize() const
{
return (buffers.size() == 1 ? tail : buffers.front().size()) - head;
}
inline const char *readPointer() const
{
Q_ASSERT(totalSize > 0);
return buffers.front().constData() + head;
}
void free(int bytes)
{
totalSize -= bytes;
Q_ASSERT(totalSize >= 0);
forever {
int nbs = readSize();
if (bytes < nbs) {
head += bytes;
if (head == tail && buffers.size() == 1) {
buffers.front().resize(CHUNKSIZE);
head = tail = 0;
}
break;
}
bytes -= nbs;
if (buffers.size() == 1) {
buffers.front().resize(CHUNKSIZE);
head = tail = 0;
break;
}
buffers.pop_front();
head = 0;
}
}
char *reserve(int bytes)
{
totalSize += bytes;
char *ptr;
if (tail + bytes <= buffers.back().size()) {
ptr = buffers.back().data() + tail;
tail += bytes;
} else {
buffers.back().resize(tail);
QByteArray tmp;
tmp.resize(qMax(CHUNKSIZE, bytes));
ptr = tmp.data();
buffers.push_back(tmp);
tail = bytes;
}
return ptr;
}
// release a trailing part of the last reservation
inline void unreserve(int bytes)
{
totalSize -= bytes;
tail -= bytes;
}
inline void write(const char *data, int len)
{
memcpy(reserve(len), data, len);
}
// Find the first occurrence of c and return the index after it.
// If c is not found until maxLength, maxLength is returned, provided
// it is smaller than the buffer size. Otherwise -1 is returned.
int indexAfter(char c, int maxLength = KMAXINT) const
{
int index = 0;
int start = head;
std::list<QByteArray>::const_iterator it = buffers.cbegin();
forever {
if (!maxLength)
return index;
if (index == size())
return -1;
const QByteArray &buf = *it;
++it;
int len = qMin((it == buffers.cend() ? tail : buf.size()) - start,
maxLength);
const char *ptr = buf.data() + start;
if (const char *rptr = (const char *)memchr(ptr, c, len))
return index + (rptr - ptr) + 1;
index += len;
maxLength -= len;
start = 0;
}
}
inline int lineSize(int maxLength = KMAXINT) const
{
return indexAfter('\n', maxLength);
}
inline bool canReadLine() const
{
return lineSize() != -1;
}
int read(char *data, int maxLength)
{
int bytesToRead = qMin(size(), maxLength);
int readSoFar = 0;
while (readSoFar < bytesToRead) {
const char *ptr = readPointer();
int bs = qMin(bytesToRead - readSoFar, readSize());
memcpy(data + readSoFar, ptr, bs);
readSoFar += bs;
free(bs);
}
return readSoFar;
}
int readLine(char *data, int maxLength)
{
return read(data, lineSize(qMin(maxLength, size())));
}
private:
std::list<QByteArray> buffers;
int head, tail;
int totalSize;
};
struct KPtyDevicePrivate : public KPtyPrivate {
Q_DECLARE_PUBLIC(KPtyDevice)
KPtyDevicePrivate(KPty* parent) :
KPtyPrivate(parent),
emittedReadyRead(false), emittedBytesWritten(false),
readNotifier(nullptr), writeNotifier(nullptr)
{
}
bool _k_canRead();
bool _k_canWrite();
bool doWait(int msecs, bool reading);
void finishOpen(QIODevice::OpenMode mode);
bool emittedReadyRead;
bool emittedBytesWritten;
QSocketNotifier *readNotifier;
QSocketNotifier *writeNotifier;
KRingBuffer readBuffer;
KRingBuffer writeBuffer;
};
#endif
|