1 | /* This file is part of the KDE libraries |
2 | |
3 | Copyright (C) 2007 Oswald Buddenhagen <ossi@kde.org> |
4 | |
5 | This library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Library General Public |
7 | License as published by the Free Software Foundation; either |
8 | version 2 of the License, or (at your option) any later version. |
9 | |
10 | This library is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Library General Public License for more details. |
14 | |
15 | You should have received a copy of the GNU Library General Public License |
16 | along with this library; see the file COPYING.LIB. If not, write to |
17 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
18 | Boston, MA 02110-1301, USA. |
19 | */ |
20 | |
21 | #ifndef kptydev_h |
22 | #define kptydev_h |
23 | |
24 | #include "kpty.h" |
25 | |
26 | #include <QtCore/QIODevice> |
27 | |
28 | struct KPtyDevicePrivate; |
29 | |
30 | #define Q_DECLARE_PRIVATE_MI(Class, SuperClass) \ |
31 | inline Class##Private* d_func() { return reinterpret_cast<Class##Private *>(SuperClass::d_ptr); } \ |
32 | inline const Class##Private* d_func() const { return reinterpret_cast<const Class##Private *>(SuperClass::d_ptr); } \ |
33 | friend class Class##Private; |
34 | |
35 | /** |
36 | * Encapsulates KPty into a QIODevice, so it can be used with Q*Stream, etc. |
37 | */ |
38 | class KPTY_EXPORT KPtyDevice : public QIODevice, public KPty { //krazy:exclude=dpointer (via macro) |
39 | Q_OBJECT |
40 | Q_DECLARE_PRIVATE_MI(KPtyDevice, KPty) |
41 | |
42 | public: |
43 | |
44 | /** |
45 | * Constructor |
46 | */ |
47 | KPtyDevice(QObject *parent = 0); |
48 | |
49 | /** |
50 | * Destructor: |
51 | * |
52 | * If the pty is still open, it will be closed. Note, however, that |
53 | * an utmp registration is @em not undone. |
54 | */ |
55 | virtual ~KPtyDevice(); |
56 | |
57 | /** |
58 | * Create a pty master/slave pair. |
59 | * |
60 | * @return true if a pty pair was successfully opened |
61 | */ |
62 | virtual bool open(OpenMode mode = ReadWrite | Unbuffered); |
63 | |
64 | /** |
65 | * Open using an existing pty master. The ownership of the fd |
66 | * remains with the caller, i.e., close() will not close the fd. |
67 | * |
68 | * This is useful if you wish to attach a secondary "controller" to an |
69 | * existing pty device such as a terminal widget. |
70 | * Note that you will need to use setSuspended() on both devices to |
71 | * control which one gets the incoming data from the pty. |
72 | * |
73 | * @param fd an open pty master file descriptor. |
74 | * @param mode the device mode to open the pty with. |
75 | * @return true if a pty pair was successfully opened |
76 | */ |
77 | bool open(int fd, OpenMode mode = ReadWrite | Unbuffered); |
78 | |
79 | /** |
80 | * Close the pty master/slave pair. |
81 | */ |
82 | virtual void close(); |
83 | |
84 | /** |
85 | * Sets whether the KPtyDevice monitors the pty for incoming data. |
86 | * |
87 | * When the KPtyDevice is suspended, it will no longer attempt to buffer |
88 | * data that becomes available from the pty and it will not emit any |
89 | * signals. |
90 | * |
91 | * Do not use on closed ptys. |
92 | * After a call to open(), the pty is not suspended. If you need to |
93 | * ensure that no data is read, call this function before the main loop |
94 | * is entered again (i.e., immediately after opening the pty). |
95 | */ |
96 | void setSuspended(bool suspended); |
97 | |
98 | /** |
99 | * Returns true if the KPtyDevice is not monitoring the pty for incoming |
100 | * data. |
101 | * |
102 | * Do not use on closed ptys. |
103 | * |
104 | * See setSuspended() |
105 | */ |
106 | bool isSuspended() const; |
107 | |
108 | /** |
109 | * @return always true |
110 | */ |
111 | virtual bool isSequential() const; |
112 | |
113 | /** |
114 | * @reimp |
115 | */ |
116 | bool canReadLine() const; |
117 | |
118 | /** |
119 | * @reimp |
120 | */ |
121 | bool atEnd() const; |
122 | |
123 | /** |
124 | * @reimp |
125 | */ |
126 | qint64 bytesAvailable() const; |
127 | |
128 | /** |
129 | * @reimp |
130 | */ |
131 | qint64 bytesToWrite() const; |
132 | |
133 | bool waitForBytesWritten(int msecs = -1); |
134 | bool waitForReadyRead(int msecs = -1); |
135 | |
136 | |
137 | Q_SIGNALS: |
138 | /** |
139 | * Emitted when EOF is read from the PTY. |
140 | * |
141 | * Data may still remain in the buffers. |
142 | */ |
143 | void readEof(); |
144 | |
145 | protected: |
146 | virtual qint64 readData(char *data, qint64 maxSize); |
147 | virtual qint64 readLineData(char *data, qint64 maxSize); |
148 | virtual qint64 writeData(const char *data, qint64 maxSize); |
149 | |
150 | private: |
151 | Q_PRIVATE_SLOT(d_func(), bool _k_canRead()) |
152 | Q_PRIVATE_SLOT(d_func(), bool _k_canWrite()) |
153 | }; |
154 | |
155 | #endif |
156 | |
157 | |