GNU Octave  4.0.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
TerminalModel.h
Go to the documentation of this file.
1 /*
2  This file is part of Konsole, an X terminal.
3 
4  Copyright (C) 2007 by Robert Knight <[email protected]>
5  Copyright (C) 1997,1998 by Lars Doelle <[email protected]>
6 
7  Rewritten for QT4 by e_k <e_k at users.sourceforge.net>, Copyright (C)2008
8  Copyright (C) 2012-2015 Jacob Dawid <[email protected]>
9 
10  This program is free software; you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation; either version 2 of the License, or
13  (at your option) any later version.
14 
15  This program is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with this program; if not, write to the Free Software
22  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23  02110-1301 USA.
24 */
25 
26 #ifndef TERMINALMODEL_H
27 #define TERMINALMODEL_H
28 
29 // Qt
30 #include <QtCore/QStringList>
31 #include <QtCore>
32 #include <QWidget>
33 
34 #include "unix/SelfListener.h"
35 
36 // Konsole
37 #include "unix/History.h"
38 #include "unix/kpty.h"
39 
40 class KProcess;
41 
42 class Emulation;
43 class PseudoTerminal;
44 class TerminalView;
45 
46 /**
47  * Represents a terminal session consisting of a pseudo-teletype and a terminal emulation.
48  * The pseudo-teletype (or PTY) handles I/O between the terminal process and Konsole.
49  * The terminal emulation ( Emulation and subclasses ) processes the output stream from the
50  * PTY and produces a character image which is then shown on views connected to the session.
51  *
52  * Each Session can be connected to one or more views by using the addView() method.
53  * The attached views can then display output from the program running in the terminal
54  * or send input to the program in the terminal in the form of keypresses and mouse
55  * activity.
56  */
57 class TerminalModel : public QObject {
58 Q_OBJECT
59 
60 public:
61  Q_PROPERTY(QString keyBindings READ keyBindings WRITE setKeyBindings)
62  Q_PROPERTY(QSize size READ size WRITE setSize)
63 
64  /**
65  * Constructs a new session.
66  *
67  * To start the terminal process, call the run() method,
68  * after specifying the program and arguments
69  * using setProgram() and setArguments()
70  *
71  * If no program or arguments are specified explicitly, the Session
72  * falls back to using the program specified in the SHELL environment
73  * variable.
74  */
75  TerminalModel(KPty *kpty);
76  ~TerminalModel();
77 
78 
79  /**
80  * Sets the profile associated with this session.
81  *
82  * @param profileKey A key which can be used to obtain the current
83  * profile settings from the SessionManager
84  */
85  void setProfileKey(const QString& profileKey);
86  /**
87  * Returns the profile key associated with this session.
88  * This can be passed to the SessionManager to obtain the current
89  * profile settings.
90  */
91  QString profileKey() const;
92 
93  /**
94  * Adds a new view for this session.
95  *
96  * The viewing widget will display the output from the terminal and
97  * input from the viewing widget (key presses, mouse activity etc.)
98  * will be sent to the terminal.
99  *
100  * Views can be removed using removeView(). The session is automatically
101  * closed when the last view is removed.
102  */
103  void addView(TerminalView* widget);
104  /**
105  * Removes a view from this session. When the last view is removed,
106  * the session will be closed automatically.
107  *
108  * @p widget will no longer display output from or send input
109  * to the terminal
110  */
111  void removeView(TerminalView* widget);
112 
113  /**
114  * Returns the views connected to this session
115  */
116  QList<TerminalView*> views() const;
117 
118  /**
119  * Returns the terminal emulation instance being used to encode / decode
120  * characters to / from the process.
121  */
122  Emulation* emulation() const;
123 
124 
125 
126  /**
127  * Sets the type of history store used by this session.
128  * Lines of output produced by the terminal are added
129  * to the history store. The type of history store
130  * used affects the number of lines which can be
131  * remembered before they are lost and the storage
132  * (in memory, on-disk etc.) used.
133  */
134  void setHistoryType(const HistoryType& type);
135  /**
136  * Returns the type of history store used by this session.
137  */
138  const HistoryType& historyType() const;
139  /**
140  * Clears the history store used by this session.
141  */
142  void clearHistory();
143 
144  /**
145  * Enables monitoring for activity in the session.
146  * This will cause notifySessionState() to be emitted
147  * with the NOTIFYACTIVITY state flag when output is
148  * received from the terminal.
149  */
150  void setMonitorActivity(bool);
151  /** Returns true if monitoring for activity is enabled. */
152  bool isMonitorActivity() const;
153 
154  /**
155  * Enables monitoring for silence in the session.
156  * This will cause notifySessionState() to be emitted
157  * with the NOTIFYSILENCE state flag when output is not
158  * received from the terminal for a certain period of
159  * time, specified with setMonitorSilenceSeconds()
160  */
161  void setMonitorSilence(bool);
162  /**
163  * Returns true if monitoring for inactivity (silence)
164  * in the session is enabled.
165  */
166  bool isMonitorSilence() const;
167  /** See setMonitorSilence() */
168  void setMonitorSilenceSeconds(int seconds);
169 
170  /**
171  * Sets the key bindings used by this session. The bindings
172  * specify how input key sequences are translated into
173  * the character stream which is sent to the terminal.
174  *
175  * @param id The name of the key bindings to use. The
176  * names of available key bindings can be determined using the
177  * KeyboardTranslatorManager class.
178  */
179  void setKeyBindings(const QString& id);
180  /** Returns the name of the key bindings used by this session. */
181  QString keyBindings() const;
182 
183 
184  /** Specifies whether a utmp entry should be created for the pty used by this session. */
185  void setAddToUtmp(bool);
186 
187  /**
188  * Specifies whether to close the session automatically when the terminal
189  * process terminates.
190  */
191  void setAutoClose(bool b) { _autoClose = b; }
192 
193  /**
194  * Sends @p text to the current foreground terminal program.
195  */
196  void sendText(const QString& text) const;
197 
198 
199  /** Returns the terminal session's window size in lines and columns. */
200  QSize size();
201  /**
202  * Emits a request to resize the session to accommodate
203  * the specified window size.
204  *
205  * @param size The size in lines and columns to request.
206  */
207  void setSize(const QSize& size);
208 
209  /** Sets the text codec used by this session's terminal emulation. */
210  void setCodec(QTextCodec* codec);
211 
212  /**
213  * Sets whether the session has a dark background or not. The session
214  * uses this information to set the COLORFGBG variable in the process's
215  * environment, which allows the programs running in the terminal to determine
216  * whether the background is light or dark and use appropriate colors by default.
217  *
218  * This has no effect once the session is running.
219  */
220  void setDarkBackground(bool darkBackground);
221  /**
222  * Returns true if the session has a dark background.
223  * See setDarkBackground()
224  */
225  bool hasDarkBackground() const;
226 
227  /**
228  * Attempts to get the shell program to redraw the current display area.
229  * This can be used after clearing the screen, for example, to get the
230  * shell to redraw the prompt line.
231  */
232  void refresh();
233 
234 public slots:
235 
236  /**
237  * Starts the terminal session.
238  *
239  * This creates the terminal process and connects the teletype to it.
240  */
241  void run();
242 
243  /**
244  * Closes the terminal session. This sends a hangup signal
245  * (SIGHUP) to the terminal process and causes the done(Session*)
246  * signal to be emitted.
247  */
248  void close();
249 
250 signals:
251 
252  /** Emitted when the terminal process starts. */
253  void started();
254 
255  /**
256  * Emitted when the terminal process exits.
257  */
258  void finished();
259 
260  /**
261  * Emitted when output is received from the terminal process.
262  */
263  void receivedData( const QString& text );
264 
265  /** Emitted when the session's title has changed. */
266  void titleChanged();
267 
268  /** Emitted when the session's profile has changed. */
269  void profileChanged(const QString& profile);
270 
271  /**
272  * Emitted when the activity state of this session changes.
273  *
274  * @param state The new state of the session. This may be one
275  * of NOTIFYNORMAL, NOTIFYSILENCE or NOTIFYACTIVITY
276  */
277  void stateChanged(int state);
278 
279  /** Emitted when a bell event occurs in the session. */
280  void bellRequest( const QString& message );
281 
282  /**
283  * Requests that the color the text for any tabs associated with
284  * this session should be changed;
285  *
286  * TODO: Document what the parameter does
287  */
288  void changeTabTextColorRequest(int);
289 
290  /**
291  * Requests that the background color of views on this session
292  * should be changed.
293  */
294  void changeBackgroundColorRequest(const QColor&);
295 
296  /** TODO: Document me. */
297  void openUrlRequest(const QString& url);
298 
299  /**
300  * Emitted when the terminal process requests a change
301  * in the size of the terminal window.
302  *
303  * @param size The requested window size in terms of lines and columns.
304  */
305  void resizeRequest(const QSize& size);
306 
307  /**
308  * Emitted when a profile change command is received from the terminal.
309  *
310  * @param text The text of the command. This is a string of the form
311  * "PropertyName=Value;PropertyName=Value ..."
312  */
313  void profileChangeCommandReceived(const QString& text);
314 
315 private slots:
316  void done(int);
317 
318  void onReceiveBlock(const char* buffer, int len );
319  void monitorTimerDone();
320 
321  void onViewSizeChange(int height, int width);
322  void onEmulationSizeChange(int lines , int columns);
323 
324  void activityStateSet(int);
325 
326  //automatically detach views from sessions when view is destroyed
327  void viewDestroyed(QObject* view);
328 
329  void sendData(const char* buf, int len);
330 
331 private:
332 
333  void updateTerminalSize();
334  WId windowId() const;
335 
337 
338  PseudoTerminal* _shellProcess;
340 
342 
349  QTimer* _monitorTimer;
350 
352 
355 
357  int _slaveFd;
358 
361 
362 
363  QColor _modifiedBackground; // as set by: echo -en '\033]11;Color\007
364 
365  QString _profileKey;
366 
368 };
369 
370 
371 #endif // TERMINALMODEL_H
bool isMonitorSilence() const
Returns true if monitoring for inactivity (silence) in the session is enabled.
void onViewSizeChange(int height, int width)
void stateChanged(int state)
Emitted when the activity state of this session changes.
void addView(TerminalView *widget)
Adds a new view for this session.
void resizeRequest(const QSize &size)
Emitted when the terminal process requests a change in the size of the terminal window.
Represents a terminal session consisting of a pseudo-teletype and a terminal emulation.
Definition: TerminalModel.h:57
void onReceiveBlock(const char *buffer, int len)
void viewDestroyed(QObject *view)
SelfListener * _selfListener
void sendText(const QString &text) const
Sends text to the current foreground terminal program.
void run()
Starts the terminal session.
void titleChanged()
Emitted when the session's title has changed.
void clearHistory()
Clears the history store used by this session.
static uint32_t state[624]
Definition: randmtzig.c:188
void profileChanged(const QString &profile)
Emitted when the session's profile has changed.
void receivedData(const QString &text)
Emitted when output is received from the terminal process.
WId windowId() const
void sendData(const char *buf, int len)
Base class for terminal emulation back-ends.
Definition: Emulation.h:118
PseudoTerminal * _shellProcess
void setKeyBindings(const QString &id)
Sets the key bindings used by this session.
void bellRequest(const QString &message)
Emitted when a bell event occurs in the session.
bool hasDarkBackground() const
Returns true if the session has a dark background.
void finished()
Emitted when the terminal process exits.
Emulation * emulation() const
Returns the terminal emulation instance being used to encode / decode characters to / from the proces...
bool _notifiedActivity
QString profileKey() const
Returns the profile key associated with this session.
void setHistoryType(const HistoryType &type)
Sets the type of history store used by this session.
bool _hasDarkBackground
void onEmulationSizeChange(int lines, int columns)
void close()
Closes the terminal session.
void refresh()
Attempts to get the shell program to redraw the current display area.
void changeBackgroundColorRequest(const QColor &)
Requests that the background color of views on this session should be changed.
void message(const char *name, const char *fmt,...)
Definition: error.cc:380
void updateTerminalSize()
QColor _modifiedBackground
void profileChangeCommandReceived(const QString &text)
Emitted when a profile change command is received from the terminal.
void monitorTimerDone()
void removeView(TerminalView *widget)
Removes a view from this session.
const HistoryType & historyType() const
Returns the type of history store used by this session.
QList< TerminalView * > _views
void setSize(const QSize &size)
Emits a request to resize the session to accommodate the specified window size.
QString keyBindings() const
Returns the name of the key bindings used by this session.
void started()
Emitted when the terminal process starts.
void setAddToUtmp(bool)
Specifies whether a utmp entry should be created for the pty used by this session.
void setDarkBackground(bool darkBackground)
Sets whether the session has a dark background or not.
void setMonitorSilence(bool)
Enables monitoring for silence in the session.
QList< TerminalView * > views() const
Returns the views connected to this session.
void changeTabTextColorRequest(int)
Requests that the color the text for any tabs associated with this session should be changed;...
void setCodec(QTextCodec *codec)
Sets the text codec used by this session's terminal emulation.
void setMonitorSilenceSeconds(int seconds)
See setMonitorSilence()
void openUrlRequest(const QString &url)
TODO: Document me.
void setProfileKey(const QString &profileKey)
Sets the profile associated with this session.
void setAutoClose(bool b)
Specifies whether to close the session automatically when the terminal process terminates.
Emulation * _emulation
QString _profileKey
void setMonitorActivity(bool)
Enables monitoring for activity in the session.
bool isMonitorActivity() const
Returns true if monitoring for activity is enabled.
A widget which displays output from a terminal emulation and sends input keypresses and mouse activit...
Definition: TerminalView.h:63
void activityStateSet(int)
Provides primitives for opening & closing a pseudo TTY pair, assigning the controlling TTY...
Definition: kpty.h:35
QSize size()
Returns the terminal session's window size in lines and columns.
QTimer * _monitorTimer