1/*
2 This file is part of the KDE libraries
3 SPDX-FileCopyrightText: 2007 Oswald Buddenhagen <ossi@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#ifndef kptydev_h
9#define kptydev_h
10
11#include "kpty.h"
12
13#include <QIODevice>
14
15class KPtyDevicePrivate;
16
17/*!
18 * \class KPtyDevice
19 * \inmodule KPty
20 *
21 * \brief Encapsulates KPty into a QIODevice, so it can be used with Q*Stream, etc.
22 */
23class KPTY_EXPORT KPtyDevice : public QIODevice, public KPty // krazy:exclude=dpointer (via macro)
24{
25 Q_OBJECT
26 Q_DECLARE_PRIVATE_D(KPty::d_ptr, KPtyDevice)
27
28public:
29 /*!
30 * Constructor
31 */
32 explicit KPtyDevice(QObject *parent = nullptr);
33
34 /*!
35 * Destructor:
36 *
37 * If the pty is still open, it will be closed. Note, however, that
38 * an utmp registration is not undone.
39 */
40 ~KPtyDevice() override;
41
42 /*!
43 * Create a pty master/slave pair.
44 *
45 * Returns true if a pty pair was successfully opened
46 */
47 bool open(OpenMode mode = ReadWrite | Unbuffered) override;
48
49 /*!
50 * Open using an existing pty master. The ownership of the fd
51 * remains with the caller, i.e., close() will not close the fd.
52 *
53 * This is useful if you wish to attach a secondary "controller" to an
54 * existing pty device such as a terminal widget.
55 * Note that you will need to use setSuspended() on both devices to
56 * control which one gets the incoming data from the pty.
57 *
58 * \a fd an open pty master file descriptor.
59 *
60 * \a mode the device mode to open the pty with.
61 *
62 * Returns true if a pty pair was successfully opened
63 */
64 bool open(int fd, OpenMode mode = ReadWrite | Unbuffered);
65
66 /*!
67 * Close the pty master/slave pair.
68 */
69 void close() override;
70
71 /*!
72 * Sets whether the KPtyDevice monitors the pty for incoming data.
73 *
74 * When the KPtyDevice is suspended, it will no longer attempt to buffer
75 * data that becomes available from the pty and it will not emit any
76 * signals.
77 *
78 * Do not use on closed ptys.
79 * After a call to open(), the pty is not suspended. If you need to
80 * ensure that no data is read, call this function before the main loop
81 * is entered again (i.e., immediately after opening the pty).
82 */
83 void setSuspended(bool suspended);
84
85 /*!
86 * Returns true if the KPtyDevice is not monitoring the pty for incoming
87 * data.
88 *
89 * Do not use on closed ptys.
90 *
91 * See setSuspended()
92 */
93 bool isSuspended() const;
94
95 /*!
96 * Returns always true
97 */
98 bool isSequential() const override;
99
100 bool canReadLine() const override;
101
102 bool atEnd() const override;
103
104 qint64 bytesAvailable() const override;
105
106 qint64 bytesToWrite() const override;
107
108 bool waitForBytesWritten(int msecs = -1) override;
109 bool waitForReadyRead(int msecs = -1) override;
110
111Q_SIGNALS:
112 /*!
113 * Emitted when EOF is read from the PTY.
114 *
115 * Data may still remain in the buffers.
116 */
117 void readEof();
118
119protected:
120 qint64 readData(char *data, qint64 maxSize) override;
121 qint64 readLineData(char *data, qint64 maxSize) override;
122 qint64 writeData(const char *data, qint64 maxSize) override;
123};
124
125#endif
126

source code of kpty/src/kptydevice.h