2
This file is part of Konsole, an X terminal.
4
Copyright (C) 2007 by Robert Knight <robertknight@gmail.com>
5
Copyright (C) 1997,1998 by Lars Doelle <lars.doelle@on-line.de>
7
Rewritten for QT4 by e_k <e_k at users.sourceforge.net>, Copyright (C) 2008
8
Rewritten for QT5/QML by Dmitry Zagnoyko <hiroshidi@gmail.com>, Copyright (C) 2013
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.
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.
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
30
#include <QStringList>
32
#include <QtCore/QTimer>
33
#include <QtGui/QWindow>
42
class KTerminalDisplay;
46
* Represents a terminal session consisting of a pseudo-teletype and a terminal emulation.
47
* The pseudo-teletype (or PTY) handles I/O between the terminal process and Konsole.
48
* The terminal emulation ( Emulation and subclasses ) processes the output stream from the
49
* PTY and produces a character image which is then shown on views connected to the session.
51
* Each Session can be connected to one or more views by using the addView() method.
52
* The attached views can then display output from the program running in the terminal
53
* or send input to the program in the terminal in the form of keypresses and mouse
56
class Session : public QObject {
60
Q_PROPERTY(QString name READ nameTitle)
61
Q_PROPERTY(int processId READ processId)
62
Q_PROPERTY(QString keyBindings READ keyBindings WRITE setKeyBindings)
63
Q_PROPERTY(QSize size READ size WRITE setSize)
66
* Constructs a new session.
68
* To start the terminal process, call the run() method,
69
* after specifying the program and arguments
70
* using setProgram() and setArguments()
72
* If no program or arguments are specified explicitly, the Session
73
* falls back to using the program specified in the SHELL environment
80
* Returns true if the session is currently running. This will be true
81
* after run() has been called successfully.
83
bool isRunning() const;
86
* Sets the profile associated with this session.
88
* @param profileKey A key which can be used to obtain the current
89
* profile settings from the SessionManager
91
void setProfileKey(const QString & profileKey);
93
* Returns the profile key associated with this session.
94
* This can be passed to the SessionManager to obtain the current
97
QString profileKey() const;
100
* Adds a new view for this session.
102
* The viewing widget will display the output from the terminal and
103
* input from the viewing widget (key presses, mouse activity etc.)
104
* will be sent to the terminal.
106
* Views can be removed using removeView(). The session is automatically
107
* closed when the last view is removed.
109
void addView(KTerminalDisplay * widget);
111
* Removes a view from this session. When the last view is removed,
112
* the session will be closed automatically.
114
* @p widget will no longer display output from or send input
117
void removeView(KTerminalDisplay * widget);
120
* Returns the views connected to this session
122
QList<KTerminalDisplay *> views() const;
125
* Returns the terminal emulation instance being used to encode / decode
126
* characters to / from the process.
128
Emulation * emulation() const;
131
* Returns the environment of this session as a list of strings like
134
QStringList environment() const;
136
* Sets the environment for this session.
137
* @p environment should be a list of strings like
140
void setEnvironment(const QStringList & environment);
142
/** Returns the unique ID for this session. */
143
int sessionId() const;
146
* Return the session title set by the user (ie. the program running
147
* in the terminal), or an empty string if the user has not set a custom title
149
QString userTitle() const;
152
* This enum describes the contexts for which separate
153
* tab title formats may be specified.
155
enum TabTitleContext {
156
/** Default tab title format */
159
* Tab title format used session currently contains
160
* a connection to a remote computer (via SSH)
165
* Sets the format used by this session for tab titles.
167
* @param context The context whoose format should be set.
168
* @param format The tab title format. This may be a mixture
169
* of plain text and dynamic elements denoted by a '%' character
170
* followed by a letter. (eg. %d for directory). The dynamic
171
* elements available depend on the @p context
173
void setTabTitleFormat(TabTitleContext context , const QString & format);
174
/** Returns the format used by this session for tab titles. */
175
QString tabTitleFormat(TabTitleContext context) const;
178
/** Returns the arguments passed to the shell process when run() is called. */
179
QStringList arguments() const;
180
/** Returns the program name of the shell process started when run() is called. */
181
QString program() const;
184
* Sets the command line arguments which the session's program will be passed when
187
void setArguments(const QStringList & arguments);
188
/** Sets the program to be executed when run() is called. */
189
void setProgram(const QString & program);
191
/** Returns the session's current working directory. */
192
QString initialWorkingDirectory() {
193
return _initialWorkingDir;
197
* Sets the initial working directory for the session when it is run
198
* This has no effect once the session has been started.
200
void setInitialWorkingDirectory( const QString & dir );
203
* Sets the type of history store used by this session.
204
* Lines of output produced by the terminal are added
205
* to the history store. The type of history store
206
* used affects the number of lines which can be
207
* remembered before they are lost and the storage
208
* (in memory, on-disk etc.) used.
210
void setHistoryType(const HistoryType & type);
212
* Returns the type of history store used by this session.
214
const HistoryType & historyType() const;
216
* Clears the history store used by this session.
221
* Enables monitoring for activity in the session.
222
* This will cause notifySessionState() to be emitted
223
* with the NOTIFYACTIVITY state flag when output is
224
* received from the terminal.
226
void setMonitorActivity(bool);
227
/** Returns true if monitoring for activity is enabled. */
228
bool isMonitorActivity() const;
231
* Enables monitoring for silence in the session.
232
* This will cause notifySessionState() to be emitted
233
* with the NOTIFYSILENCE state flag when output is not
234
* received from the terminal for a certain period of
235
* time, specified with setMonitorSilenceSeconds()
237
void setMonitorSilence(bool);
239
* Returns true if monitoring for inactivity (silence)
240
* in the session is enabled.
242
bool isMonitorSilence() const;
243
/** See setMonitorSilence() */
244
void setMonitorSilenceSeconds(int seconds);
247
* Sets the key bindings used by this session. The bindings
248
* specify how input key sequences are translated into
249
* the character stream which is sent to the terminal.
251
* @param id The name of the key bindings to use. The
252
* names of available key bindings can be determined using the
253
* KeyboardTranslatorManager class.
255
void setKeyBindings(const QString & id);
256
/** Returns the name of the key bindings used by this session. */
257
QString keyBindings() const;
260
* This enum describes the available title roles.
263
/** The name of the session. */
265
/** The title of the session which is displayed in tabs etc. */
269
/** Sets the session's title for the specified @p role to @p title. */
270
void setTitle(TitleRole role , const QString & title);
271
/** Returns the session's title for the specified @p role. */
272
QString title(TitleRole role) const;
273
/** Convenience method used to read the name property. Returns title(Session::NameRole). */
274
QString nameTitle() const {
275
return title(Session::NameRole);
278
/** Sets the name of the icon associated with this session. */
279
void setIconName(const QString & iconName);
280
/** Returns the name of the icon associated with this session. */
281
QString iconName() const;
283
/** Sets the text of the icon associated with this session. */
284
void setIconText(const QString & iconText);
285
/** Returns the text of the icon associated with this session. */
286
QString iconText() const;
288
/** Specifies whether a utmp entry should be created for the pty used by this session. */
289
void setAddToUtmp(bool);
291
/** Sends the specified @p signal to the terminal process. */
292
bool sendSignal(int signal);
295
* Specifies whether to close the session automatically when the terminal
296
* process terminates.
298
void setAutoClose(bool b) {
303
* Sets whether flow control is enabled for this terminal
306
void setFlowControlEnabled(bool enabled);
308
/** Returns whether flow control is enabled for this terminal session. */
309
bool flowControlEnabled() const;
312
* Sends @p text to the current foreground terminal program.
314
void sendText(const QString &text) const;
317
* Sends @p QKeyEvent to the current foreground terminal program.
319
void sendKey(QKeyEvent *key);
322
* Returns the process id of the terminal process.
323
* This is the id used by the system API to refer to the process.
325
int processId() const;
328
* Returns the process id of the terminal's foreground process.
329
* This is initially the same as processId() but can change
330
* as the user starts other programs inside the terminal.
332
int foregroundProcessId() const;
334
/** Returns the terminal session's window size in lines and columns. */
337
* Emits a request to resize the session to accommodate
338
* the specified window size.
340
* @param size The size in lines and columns to request.
342
void setSize(const QSize & size);
344
/** Sets the text codec used by this session's terminal emulation. */
345
void setCodec(QTextCodec * codec);
348
* Sets whether the session has a dark background or not. The session
349
* uses this information to set the COLORFGBG variable in the process's
350
* environment, which allows the programs running in the terminal to determine
351
* whether the background is light or dark and use appropriate colors by default.
353
* This has no effect once the session is running.
355
void setDarkBackground(bool darkBackground);
357
* Returns true if the session has a dark background.
358
* See setDarkBackground()
360
bool hasDarkBackground() const;
363
* Attempts to get the shell program to redraw the current display area.
364
* This can be used after clearing the screen, for example, to get the
365
* shell to redraw the prompt line.
369
// void startZModem(const QString &rz, const QString &dir, const QStringList &list);
370
// void cancelZModem();
371
// bool isZModemBusy() { return _zmodemBusy; }
376
* Starts the terminal session.
378
* This creates the terminal process and connects the teletype to it.
383
* Closes the terminal session. This sends a hangup signal
384
* (SIGHUP) to the terminal process and causes the done(Session*)
385
* signal to be emitted.
390
* Changes the session title or other customizable aspects of the terminal
391
* emulation display. For a list of what may be changed see the
392
* Emulation::titleChanged() signal.
394
void setUserTitle( int, const QString & caption );
398
/** Emitted when the terminal process starts. */
402
* Emitted when the terminal process exits.
407
* Emitted when output is received from the terminal process.
409
void receivedData( const QString & text );
411
/** Emitted when the session's title has changed. */
414
/** Emitted when the session's profile has changed. */
415
void profileChanged(const QString & profile);
418
* Emitted when the activity state of this session changes.
420
* @param state The new state of the session. This may be one
421
* of NOTIFYNORMAL, NOTIFYSILENCE or NOTIFYACTIVITY
423
void stateChanged(int state);
425
/** Emitted when a bell event occurs in the session. */
426
void bellRequest( const QString & message );
429
* Requests that the color the text for any tabs associated with
430
* this session should be changed;
432
* TODO: Document what the parameter does
434
void changeTabTextColorRequest(int);
437
* Requests that the background color of views on this session
440
void changeBackgroundColorRequest(const QColor &);
442
/** TODO: Document me. */
443
void openUrlRequest(const QString & url);
445
/** TODO: Document me. */
446
// void zmodemDetected();
449
* Emitted when the terminal process requests a change
450
* in the size of the terminal window.
452
* @param size The requested window size in terms of lines and columns.
454
void resizeRequest(const QSize & size);
457
* Emitted when a profile change command is received from the terminal.
459
* @param text The text of the command. This is a string of the form
460
* "PropertyName=Value;PropertyName=Value ..."
462
void profileChangeCommandReceived(const QString & text);
465
* Emitted when the flow control state changes.
467
* @param enabled True if flow control is enabled or false otherwise.
469
void flowControlEnabledChanged(bool enabled);
474
// void fireZModemDetected();
476
void onReceiveBlock( const char * buffer, int len );
477
void monitorTimerDone();
479
void onViewSizeChange(int height, int width);
480
void onEmulationSizeChange(int lines , int columns);
482
void activityStateSet(int);
484
//automatically detach views from sessions when view is destroyed
485
void viewDestroyed(QObject * view);
487
// void zmodemReadStatus();
488
// void zmodemReadAndSendBlock();
489
// void zmodemRcvBlock(const char *data, int len);
490
// void zmodemFinished();
494
void updateTerminalSize();
495
WId windowId() const;
497
int _uniqueIdentifier;
500
Emulation * _emulation;
502
QList<KTerminalDisplay *> _views;
504
bool _monitorActivity;
505
bool _monitorSilence;
506
bool _notifiedActivity;
510
QTimer * _monitorTimer;
515
QString _displayTitle;
518
QString _localTabTitleFormat;
519
QString _remoteTabTitleFormat;
522
QString _iconText; // as set by: echo -en '\033]1;IconText\007
528
QStringList _arguments;
530
QStringList _environment;
533
QString _initialWorkingDir;
537
// KProcess* _zmodemProc;
538
// ZModemDialog* _zmodemProgress;
540
// Color/Font Changes by ESC Sequences
542
QColor _modifiedBackground; // as set by: echo -en '\033]11;Color\007
546
bool _hasDarkBackground;
548
static int lastSessionId;
553
* Provides a group of sessions which is divided into master and slave sessions.
554
* Activity in master sessions can be propagated to all sessions within the group.
555
* The type of activity which is propagated and method of propagation is controlled
556
* by the masterMode() flags.
558
class SessionGroup : public QObject {
562
/** Constructs an empty session group. */
564
/** Destroys the session group and removes all connections between master and slave sessions. */
567
/** Adds a session to the group. */
568
void addSession( Session * session );
569
/** Removes a session from the group. */
570
void removeSession( Session * session );
572
/** Returns the list of sessions currently in the group. */
573
QList<Session *> sessions() const;
576
* Sets whether a particular session is a master within the group.
577
* Changes or activity in the group's master sessions may be propagated
578
* to all the sessions in the group, depending on the current masterMode()
580
* @param session The session whoose master status should be changed.
581
* @param master True to make this session a master or false otherwise
583
void setMasterStatus( Session * session , bool master );
584
/** Returns the master status of a session. See setMasterStatus() */
585
bool masterStatus( Session * session ) const;
588
* This enum describes the options for propagating certain activity or
589
* changes in the group's master sessions to all sessions in the group.
593
* Any input key presses in the master sessions are sent to all
594
* sessions in the group.
600
* Specifies which activity in the group's master sessions is propagated
601
* to all sessions in the group.
603
* @param mode A bitwise OR of MasterMode flags.
605
void setMasterMode( int mode );
607
* Returns a bitwise OR of the active MasterMode flags for this group.
608
* See setMasterMode()
610
int masterMode() const;
613
void connectPair(Session * master , Session * other);
614
void disconnectPair(Session * master , Session * other);
615
void connectAll(bool connect);
616
QList<Session *> masters() const;
618
// maps sessions to their master status
619
QHash<Session *,bool> _sessions;