File indexing completed on 2024-04-14 03:54:25
0001 /* 0002 This file is part of the KDE libraries 0003 SPDX-FileCopyrightText: 2007 Oswald Buddenhagen <ossi@kde.org> 0004 0005 SPDX-License-Identifier: LGPL-2.0-or-later 0006 */ 0007 0008 #ifndef kptydev_h 0009 #define kptydev_h 0010 0011 #include "kpty.h" 0012 0013 #include <QIODevice> 0014 0015 class KPtyDevicePrivate; 0016 0017 /** 0018 * Encapsulates KPty into a QIODevice, so it can be used with Q*Stream, etc. 0019 */ 0020 class KPTY_EXPORT KPtyDevice : public QIODevice, public KPty // krazy:exclude=dpointer (via macro) 0021 { 0022 Q_OBJECT 0023 Q_DECLARE_PRIVATE_D(KPty::d_ptr, KPtyDevice) 0024 0025 public: 0026 /** 0027 * Constructor 0028 */ 0029 explicit KPtyDevice(QObject *parent = nullptr); 0030 0031 /** 0032 * Destructor: 0033 * 0034 * If the pty is still open, it will be closed. Note, however, that 0035 * an utmp registration is @em not undone. 0036 */ 0037 ~KPtyDevice() override; 0038 0039 /** 0040 * Create a pty master/slave pair. 0041 * 0042 * @return true if a pty pair was successfully opened 0043 */ 0044 bool open(OpenMode mode = ReadWrite | Unbuffered) override; 0045 0046 /** 0047 * Open using an existing pty master. The ownership of the fd 0048 * remains with the caller, i.e., close() will not close the fd. 0049 * 0050 * This is useful if you wish to attach a secondary "controller" to an 0051 * existing pty device such as a terminal widget. 0052 * Note that you will need to use setSuspended() on both devices to 0053 * control which one gets the incoming data from the pty. 0054 * 0055 * @param fd an open pty master file descriptor. 0056 * @param mode the device mode to open the pty with. 0057 * @return true if a pty pair was successfully opened 0058 */ 0059 bool open(int fd, OpenMode mode = ReadWrite | Unbuffered); 0060 0061 /** 0062 * Close the pty master/slave pair. 0063 */ 0064 void close() override; 0065 0066 /** 0067 * Sets whether the KPtyDevice monitors the pty for incoming data. 0068 * 0069 * When the KPtyDevice is suspended, it will no longer attempt to buffer 0070 * data that becomes available from the pty and it will not emit any 0071 * signals. 0072 * 0073 * Do not use on closed ptys. 0074 * After a call to open(), the pty is not suspended. If you need to 0075 * ensure that no data is read, call this function before the main loop 0076 * is entered again (i.e., immediately after opening the pty). 0077 */ 0078 void setSuspended(bool suspended); 0079 0080 /** 0081 * Returns true if the KPtyDevice is not monitoring the pty for incoming 0082 * data. 0083 * 0084 * Do not use on closed ptys. 0085 * 0086 * See setSuspended() 0087 */ 0088 bool isSuspended() const; 0089 0090 /** 0091 * @return always true 0092 */ 0093 bool isSequential() const override; 0094 0095 /** 0096 * @reimp 0097 */ 0098 bool canReadLine() const override; 0099 0100 /** 0101 * @reimp 0102 */ 0103 bool atEnd() const override; 0104 0105 /** 0106 * @reimp 0107 */ 0108 qint64 bytesAvailable() const override; 0109 0110 /** 0111 * @reimp 0112 */ 0113 qint64 bytesToWrite() const override; 0114 0115 bool waitForBytesWritten(int msecs = -1) override; 0116 bool waitForReadyRead(int msecs = -1) override; 0117 0118 Q_SIGNALS: 0119 /** 0120 * Emitted when EOF is read from the PTY. 0121 * 0122 * Data may still remain in the buffers. 0123 */ 0124 void readEof(); 0125 0126 protected: 0127 qint64 readData(char *data, qint64 maxSize) override; 0128 qint64 readLineData(char *data, qint64 maxSize) override; 0129 qint64 writeData(const char *data, qint64 maxSize) override; 0130 0131 private: 0132 Q_PRIVATE_SLOT(d_func(), bool _k_canRead()) 0133 Q_PRIVATE_SLOT(d_func(), bool _k_canWrite()) 0134 }; 0135 0136 #endif