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