2 This file is part of Konsole, an X terminal.
4 Copyright 2007-2008 by Robert Knight <robertknight@gmail.com>
5 Copyright 1997,1998 by Lars Doelle <lars.doelle@on-line.de>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
27 #include <QtCore/QStringList>
28 #include <QtCore/QByteRef>
31 #include <KApplication>
32 #include <KMainWindow>
33 #include <kdemacros.h>
47 class TerminalDisplay
;
51 * Represents a terminal session consisting of a pseudo-teletype and a terminal emulation.
52 * The pseudo-teletype (or PTY) handles I/O between the terminal process and Konsole.
53 * The terminal emulation ( Emulation and subclasses ) processes the output stream from the
54 * PTY and produces a character image which is then shown on views connected to the session.
56 * Each Session can be connected to one or more views by using the addView() method.
57 * The attached views can then display output from the program running in the terminal
58 * or send input to the program in the terminal in the form of keypresses and mouse
61 class KDE_EXPORT Session
: public QObject
66 Q_PROPERTY(QString name READ nameTitle
)
67 Q_PROPERTY(int processId READ processId
)
68 Q_PROPERTY(QString keyBindings READ keyBindings WRITE setKeyBindings
)
69 Q_PROPERTY(QSize size READ size WRITE setSize
)
72 * Constructs a new session.
74 * To start the terminal process, call the run() method,
75 * after specifying the program and arguments
76 * using setProgram() and setArguments()
78 * If no program or arguments are specified explicitly, the Session
79 * falls back to using the program specified in the SHELL environment
82 explicit Session(QObject
* parent
= 0);
86 * Connect to an existing terminal. When a new Session() is constructed it
87 * automatically searches for and opens a new teletype. If you want to
88 * use an existing teletype (given its file descriptor) call this after
89 * constructing the session.
91 * Calling openTeletype() while a session is running has no effect.
93 * @param masterFd The file descriptor of the pseudo-teletype master (See KPtyProcess::KPtyProcess())
95 void openTeletype(int masterFd
);
98 * Returns true if the session is currently running. This will be true
99 * after run() has been called successfully.
101 bool isRunning() const;
104 * Adds a new view for this session.
106 * The viewing widget will display the output from the terminal and
107 * input from the viewing widget (key presses, mouse activity etc.)
108 * will be sent to the terminal.
110 * Views can be removed using removeView(). The session is automatically
111 * closed when the last view is removed.
113 void addView(TerminalDisplay
* widget
);
115 * Removes a view from this session. When the last view is removed,
116 * the session will be closed automatically.
118 * @p widget will no longer display output from or send input
121 void removeView(TerminalDisplay
* widget
);
124 * Returns the views connected to this session
126 QList
<TerminalDisplay
*> views() const;
129 * Returns the terminal emulation instance being used to encode / decode
130 * characters to / from the process.
132 Emulation
* emulation() const;
135 * Returns the environment of this session as a list of strings like
138 QStringList
environment() const;
140 * Sets the environment for this session.
141 * @p environment should be a list of strings like
144 void setEnvironment(const QStringList
& environment
);
146 /** Returns the unique ID for this session. */
147 int sessionId() const;
150 * Return the session title set by the user (ie. the program running
151 * in the terminal), or an empty string if the user has not set a custom title
153 QString
userTitle() const;
156 * This enum describes the contexts for which separate
157 * tab title formats may be specified.
161 /** Default tab title format */
164 * Tab title format used session currently contains
165 * a connection to a remote computer (via SSH)
170 * Sets the format used by this session for tab titles.
172 * @param context The context whoose format should be set.
173 * @param format The tab title format. This may be a mixture
174 * of plain text and dynamic elements denoted by a '%' character
175 * followed by a letter. (eg. %d for directory). The dynamic
176 * elements available depend on the @p context
178 void setTabTitleFormat(TabTitleContext context
, const QString
& format
);
179 /** Returns the format used by this session for tab titles. */
180 QString
tabTitleFormat(TabTitleContext context
) const;
183 /** Returns the arguments passed to the shell process when run() is called. */
184 QStringList
arguments() const;
185 /** Returns the program name of the shell process started when run() is called. */
186 QString
program() const;
189 * Sets the command line arguments which the session's program will be passed when
192 void setArguments(const QStringList
& arguments
);
193 /** Sets the program to be executed when run() is called. */
194 void setProgram(const QString
& program
);
196 /** Returns the session's current working directory. */
197 QString
initialWorkingDirectory() { return _initialWorkingDir
; }
200 * Sets the initial working directory for the session when it is run
201 * This has no effect once the session has been started.
203 void setInitialWorkingDirectory( const QString
& dir
);
206 * Returns the current directory of the foreground process in the session
208 QString
currentWorkingDirectory();
211 * Sets the type of history store used by this session.
212 * Lines of output produced by the terminal are added
213 * to the history store. The type of history store
214 * used affects the number of lines which can be
215 * remembered before they are lost and the storage
216 * (in memory, on-disk etc.) used.
218 void setHistoryType(const HistoryType
& type
);
220 * Returns the type of history store used by this session.
222 const HistoryType
& historyType() const;
224 * Clears the history store used by this session.
229 * Enables monitoring for activity in the session.
230 * This will cause notifySessionState() to be emitted
231 * with the NOTIFYACTIVITY state flag when output is
232 * received from the terminal.
234 void setMonitorActivity(bool);
235 /** Returns true if monitoring for activity is enabled. */
236 bool isMonitorActivity() const;
239 * Enables monitoring for silence in the session.
240 * This will cause notifySessionState() to be emitted
241 * with the NOTIFYSILENCE state flag when output is not
242 * received from the terminal for a certain period of
243 * time, specified with setMonitorSilenceSeconds()
245 void setMonitorSilence(bool);
247 * Returns true if monitoring for inactivity (silence)
248 * in the session is enabled.
250 bool isMonitorSilence() const;
251 /** See setMonitorSilence() */
252 void setMonitorSilenceSeconds(int seconds
);
255 * Sets the key bindings used by this session. The bindings
256 * specify how input key sequences are translated into
257 * the character stream which is sent to the terminal.
259 * @param id The name of the key bindings to use. The
260 * names of available key bindings can be determined using the
261 * KeyboardTranslatorManager class.
263 void setKeyBindings(const QString
& id
);
264 /** Returns the name of the key bindings used by this session. */
265 QString
keyBindings() const;
268 * This enum describes the available title roles.
272 /** The name of the session. */
274 /** The title of the session which is displayed in tabs etc. */
278 /** Sets the session's title for the specified @p role to @p title. */
279 void setTitle(TitleRole role
, const QString
& title
);
280 /** Returns the session's title for the specified @p role. */
281 QString
title(TitleRole role
) const;
282 /** Convenience method used to read the name property. Returns title(Session::NameRole). */
283 QString
nameTitle() const { return title(Session::NameRole
); }
284 /** Returns a title generated from tab format and process information. */
285 QString
getDynamicTitle();
287 /** Sets the name of the icon associated with this session. */
288 void setIconName(const QString
& iconName
);
289 /** Returns the name of the icon associated with this session. */
290 QString
iconName() const;
292 /** Return URL for the session. */
295 /** Sets the text of the icon associated with this session. */
296 void setIconText(const QString
& iconText
);
297 /** Returns the text of the icon associated with this session. */
298 QString
iconText() const;
301 * Specifies whether a utmp entry should be created for the pty used by this session.
302 * If true, KPty::login() is called when the session is started.
304 void setAddToUtmp(bool);
307 * Specifies whether to close the session automatically when the terminal
308 * process terminates.
310 void setAutoClose(bool b
) { _autoClose
= b
; }
313 * Sets whether flow control is enabled for this terminal
316 void setFlowControlEnabled(bool enabled
);
318 /** Returns whether flow control is enabled for this terminal session. */
319 bool flowControlEnabled() const;
322 * Sends @p text to the current foreground terminal program.
324 void sendText(const QString
& text
) const;
327 * Returns the process id of the terminal process.
328 * This is the id used by the system API to refer to the process.
330 int processId() const;
332 /** Returns true if the user has started a program in the session. */
333 bool isChildActive();
335 /** Returns the name of the current foreground process. */
338 /** Returns the terminal session's window size in lines and columns. */
341 * Emits a request to resize the session to accommodate
342 * the specified window size.
344 * @param size The size in lines and columns to request.
346 void setSize(const QSize
& size
);
348 /** Sets the text codec used by this session's terminal emulation. */
349 void setCodec(QTextCodec
* codec
);
352 * Sets whether the session has a dark background or not. The session
353 * uses this information to set the COLORFGBG variable in the process's
354 * environment, which allows the programs running in the terminal to determine
355 * whether the background is light or dark and use appropriate colors by default.
357 * This has no effect once the session is running.
359 void setDarkBackground(bool darkBackground
);
361 * Returns true if the session has a dark background.
362 * See setDarkBackground()
364 bool hasDarkBackground() const;
367 * Attempts to get the shell program to redraw the current display area.
368 * This can be used after clearing the screen, for example, to get the
369 * shell to redraw the prompt line.
373 void startZModem(const QString
&rz
, const QString
&dir
, const QStringList
&list
);
375 bool isZModemBusy() { return _zmodemBusy
; }
378 * Possible values of the @p what parameter for setUserTitle()
379 * See "Operating System Controls" section on http://rtfm.etla.org/xterm/ctlseq.html
383 IconNameAndWindowTitle
= 0,
387 BackgroundColor
= 11,
389 ProfileChange
= 50 // this clashes with Xterm's font change command
392 // session management
393 void saveSession(KConfigGroup
& group
);
394 void restoreSession(KConfigGroup
& group
);
399 * Starts the terminal session.
401 * This creates the terminal process and connects the teletype to it.
406 * Closes the terminal session. This sends a hangup signal
407 * (SIGHUP) to the terminal process and causes the finished()
408 * signal to be emitted. If the process does not respond to the SIGHUP signal
409 * then the terminal connection (the pty) is closed and Konsole waits for the
415 * Changes the session title or other customizable aspects of the terminal
416 * emulation display. For a list of what may be changed see the
417 * Emulation::titleChanged() signal.
419 * @param what The feature being changed. Value is one of UserTitleChange
420 * @param caption The text part of the terminal command
422 void setUserTitle( int what
, const QString
&caption
);
426 /** Emitted when the terminal process starts. */
430 * Emitted when the terminal process exits.
435 * Emitted when output is received from the terminal process.
437 void receivedData( const QString
& text
);
439 /** Emitted when the session's title has changed. */
443 * Emitted when the activity state of this session changes.
445 * @param state The new state of the session. This may be one
446 * of NOTIFYNORMAL, NOTIFYSILENCE or NOTIFYACTIVITY
448 void stateChanged(int state
);
450 /** Emitted when a bell event occurs in the session. */
451 void bellRequest( const QString
& message
);
454 * Requests that the color the text for any tabs associated with
455 * this session should be changed;
457 * TODO: Document what the parameter does
459 void changeTabTextColorRequest(int);
462 * Requests that the background color of views on this session
465 void changeBackgroundColorRequest(const QColor
&);
467 * Requests that the text color of views on this session should
468 * be changed to @p color.
470 void changeForegroundColorRequest(const QColor
&);
472 /** TODO: Document me. */
473 void openUrlRequest(const QString
& url
);
475 /** TODO: Document me. */
476 void zmodemDetected();
479 * Emitted when the terminal process requests a change
480 * in the size of the terminal window.
482 * @param size The requested window size in terms of lines and columns.
484 void resizeRequest(const QSize
& size
);
487 * Emitted when a profile change command is received from the terminal.
489 * @param text The text of the command. This is a string of the form
490 * "PropertyName=Value;PropertyName=Value ..."
492 void profileChangeCommandReceived(const QString
& text
);
495 * Emitted when the flow control state changes.
497 * @param enabled True if flow control is enabled or false otherwise.
499 void flowControlEnabledChanged(bool enabled
);
504 void fireZModemDetected();
506 void onReceiveBlock( const char* buffer
, int len
);
507 void monitorTimerDone();
509 void onViewSizeChange(int height
, int width
);
511 void activityStateSet(int);
513 //automatically detach views from sessions when view is destroyed
514 void viewDestroyed(QObject
* view
);
516 void zmodemReadStatus();
517 void zmodemReadAndSendBlock();
518 void zmodemRcvBlock(const char *data
, int len
);
519 void zmodemFinished();
521 void updateFlowControlState(bool suspended
);
522 void updateWindowSize(int lines
, int columns
);
525 void updateTerminalSize();
526 WId
windowId() const;
527 bool kill(int signal
);
528 // print a warning message in the terminal. This is used
529 // if the program fails to start, or if the shell exits in
530 // an unsuccessful manner
531 void terminalWarning(const QString
& message
);
532 // checks that the binary 'program' is available and can be executed
533 // returns the binary name if available or an empty string otherwise
534 QString
checkProgram(const QString
& program
) const;
535 ProcessInfo
* getProcessInfo();
536 void updateSessionProcessInfo();
537 bool updateForegroundProcessInfo();
538 ProcessInfo
* updateWorkingDirectory();
540 int _uniqueIdentifier
;
543 Emulation
* _emulation
;
545 QList
<TerminalDisplay
*> _views
;
547 bool _monitorActivity
;
548 bool _monitorSilence
;
549 bool _notifiedActivity
;
553 QTimer
* _monitorTimer
;
558 QString _displayTitle
;
561 QString _localTabTitleFormat
;
562 QString _remoteTabTitleFormat
;
565 QString _iconText
; // as set by: echo -en '\033]1;IconText\007
571 QStringList _arguments
;
573 QStringList _environment
;
576 QString _initialWorkingDir
;
577 QString _currentWorkingDir
;
579 ProcessInfo
* _sessionProcessInfo
;
580 ProcessInfo
* _foregroundProcessInfo
;
585 KProcess
* _zmodemProc
;
586 ZModemDialog
* _zmodemProgress
;
588 // Color/Font Changes by ESC Sequences
590 QColor _modifiedBackground
; // as set by: echo -en '\033]11;Color\007
594 bool _hasDarkBackground
;
596 static int lastSessionId
;
601 * Provides a group of sessions which is divided into master and slave sessions.
602 * Activity in master sessions can be propagated to all sessions within the group.
603 * The type of activity which is propagated and method of propagation is controlled
604 * by the masterMode() flags.
606 class SessionGroup
: public QObject
611 /** Constructs an empty session group. */
612 SessionGroup(QObject
* parent
);
613 /** Destroys the session group and removes all connections between master and slave sessions. */
616 /** Adds a session to the group. */
617 void addSession( Session
* session
);
618 /** Removes a session from the group. */
619 void removeSession( Session
* session
);
621 /** Returns the list of sessions currently in the group. */
622 QList
<Session
*> sessions() const;
625 * Sets whether a particular session is a master within the group.
626 * Changes or activity in the group's master sessions may be propagated
627 * to all the sessions in the group, depending on the current masterMode()
629 * @param session The session whoose master status should be changed.
630 * @param master True to make this session a master or false otherwise
632 void setMasterStatus( Session
* session
, bool master
);
633 /** Returns the master status of a session. See setMasterStatus() */
634 bool masterStatus( Session
* session
) const;
637 * This enum describes the options for propagating certain activity or
638 * changes in the group's master sessions to all sessions in the group.
643 * Any input key presses in the master sessions are sent to all
644 * sessions in the group.
650 * Specifies which activity in the group's master sessions is propagated
651 * to all sessions in the group.
653 * @param mode A bitwise OR of MasterMode flags.
655 void setMasterMode( int mode
);
657 * Returns a bitwise OR of the active MasterMode flags for this group.
658 * See setMasterMode()
660 int masterMode() const;
663 void sessionFinished();
666 void connectPair(Session
* master
, Session
* other
);
667 void disconnectPair(Session
* master
, Session
* other
);
668 void connectAll(bool connect
);
669 QList
<Session
*> masters() const;
671 // maps sessions to their master status
672 QHash
<Session
*,bool> _sessions
;
684 c-file-style: "stroustrup"
685 indent-tabs-mode: nil