summaryrefslogtreecommitdiff
path: root/qtermwidget/Pty.h
blob: f3e943228c6ceafc76f78a7eaf8546f8bb5b1a92 (plain)
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
/*
    This file is part of Konsole, KDE's terminal emulator. 
    
    Copyright (C) 2007 by Robert Knight <robertknight@gmail.com>
    Copyright (C) 1997,1998 by Lars Doelle <lars.doelle@on-line.de>

    Rewritten for QT4 by e_k <e_k at users.sourceforge.net>, Copyright (C)2008

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program 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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301  USA.
*/

#ifndef PTY_H
#define PTY_H

// Qt
#include <QtCore/QStringList>
#include <QtCore/QVector>
#include <QtCore/QList>
#include <QtCore>

#include "k3process.h"


namespace Konsole
{

/**
 * The Pty class is used to start the terminal process, 
 * send data to it, receive data from it and manipulate 
 * various properties of the pseudo-teletype interface
 * used to communicate with the process.
 *
 * To use this class, construct an instance and connect
 * to the sendData slot and receivedData signal to
 * send data to or receive data from the process.
 *
 * To start the terminal process, call the start() method
 * with the program name and appropriate arguments. 
 */
class Pty: public K3Process
{
Q_OBJECT

  public:
    
    /** 
     * Constructs a new Pty.
     * 
     * Connect to the sendData() slot and receivedData() signal to prepare
     * for sending and receiving data from the terminal process.
     *
     * To start the terminal process, call the run() method with the 
     * name of the program to start and appropriate arguments.
     */
    Pty();
    ~Pty();

    /**
     * Starts the terminal process.  
     *
     * Returns 0 if the process was started successfully or non-zero
     * otherwise.
     *
     * @param program Path to the program to start
     * @param arguments Arguments to pass to the program being started
     * @param environment A list of key=value pairs which will be added
     * to the environment for the new process.  At the very least this
     * should include an assignment for the TERM environment variable.
     * @param winid Specifies the value of the WINDOWID environment variable
     * in the process's environment.
     * @param addToUtmp Specifies whether a utmp entry should be created for
     * the pty used.  See K3Process::setUsePty() 
     * @param dbusService Specifies the value of the KONSOLE_DBUS_SERVICE 
     * environment variable in the process's environment.
     * @param dbusSession Specifies the value of the KONSOLE_DBUS_SESSION
     * environment variable in the process's environment. 
     */
    int start( const QString& program, 
               const QStringList& arguments, 
               const QStringList& environment, 
               ulong winid, 
               bool addToUtmp
//               const QString& dbusService,
//               const QString& dbusSession
             );

    /** TODO: Document me */
    void setWriteable(bool writeable);

    /** 
     * Enables or disables Xon/Xoff flow control.
     */
    void setXonXoff(bool on);

    /** 
     * Sets the size of the window (in lines and columns of characters) 
     * used by this teletype.
     */
    void setWindowSize(int lines, int cols);
    
    /** Returns the size of the window used by this teletype.  See setWindowSize() */
    QSize windowSize() const;

    /** TODO Document me */
    void setErase(char erase);

	/** */
	char erase() const;

    /**
     * Returns the process id of the teletype's current foreground
     * process.  This is the process which is currently reading
     * input sent to the terminal via. sendData()
     *
     * If there is a problem reading the foreground process group,
     * 0 will be returned.
     */
    int foregroundProcessGroup() const;
   
    /**
     * Returns whether the buffer used to send data to the
     * terminal process is full.
     */
    bool bufferFull() const { return _bufferFull; }


  public slots:

    /**
     * Put the pty into UTF-8 mode on systems which support it.
     */
    void setUtf8Mode(bool on);

    /**
     * Suspend or resume processing of data from the standard 
     * output of the terminal process.
     *
     * See K3Process::suspend() and K3Process::resume()
     *
     * @param lock If true, processing of output is suspended,
     * otherwise processing is resumed.
     */
    void lockPty(bool lock);
    
    /** 
     * Sends data to the process currently controlling the 
     * teletype ( whose id is returned by foregroundProcessGroup() )
     *
     * @param buffer Pointer to the data to send.
     * @param length Length of @p buffer.
     */
    void sendData(const char* buffer, int length);

  signals:

    /**
     * Emitted when the terminal process terminates.
     *
     * @param exitCode The status code which the process exited with.
     */
    void done(int exitCode);

    /**
     * Emitted when a new block of data is received from
     * the teletype.
     *
     * @param buffer Pointer to the data received.
     * @param length Length of @p buffer
     */
    void receivedData(const char* buffer, int length);
    
    /**
     * Emitted when the buffer used to send data to the terminal
     * process becomes empty, i.e. all data has been sent.
     */
    void bufferEmpty();
    

  private slots:
    
    // called when terminal process exits
    void donePty();
    // called when data is received from the terminal process 
    void dataReceived(K3Process*, char* buffer, int length);
    // sends the first enqueued buffer of data to the
    // terminal process
    void doSendJobs();
    // called when the terminal process is ready to
    // receive more data
    void writeReady();

  private:
    // takes a list of key=value pairs and adds them
    // to the environment for the process
    void addEnvironmentVariables(const QStringList& environment);

    // enqueues a buffer of data to be sent to the 
    // terminal process
    void appendSendJob(const char* buffer, int length);
   
    // a buffer of data in the queue to be sent to the 
    // terminal process 
    class SendJob {
	public:
      		SendJob() {}
      		SendJob(const char* b, int len) : buffer(len)
		{
			memcpy( buffer.data() , b , len );
        }
	
		const char* data() const { return buffer.constData(); }
		int length() const { return buffer.size(); }	
	private:
      		QVector<char> buffer;
    };

    QList<SendJob> _pendingSendJobs;
    bool _bufferFull;

    int  _windowColumns; 
    int  _windowLines;
    char _eraseChar;
    bool _xonXoff;
    bool _utf8;
    KPty *_pty;
};

}

#endif // PTY_H