source: ogBrowser-Git/qtermwidget/lib/Session.h

jenkins
Last change on this file was 64efc22, checked in by Vadim Troshchinskiy <vtroshchinskiy@…>, 19 months ago

Update qtermwidget to modern version

  • Property mode set to 100644
File size: 19.9 KB
Line 
1/*
2    This file is part of Konsole, an X terminal.
3
4    Copyright (C) 2007 by Robert Knight <robertknight@gmail.com>
5    Copyright (C) 1997,1998 by Lars Doelle <lars.doelle@on-line.de>
6
7    Rewritten for QT4 by e_k <e_k at users.sourceforge.net>, Copyright (C)2008
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22    02110-1301  USA.
23*/
24
25#ifndef SESSION_H
26#define SESSION_H
27
28#include <QStringList>
29#include <QWidget>
30
31#include "Emulation.h"
32#include "History.h"
33
34class KProcess;
35
36namespace Konsole {
37
38class Emulation;
39class Pty;
40class TerminalDisplay;
41//class ZModemDialog;
42
43/**
44 * Represents a terminal session consisting of a pseudo-teletype and a terminal emulation.
45 * The pseudo-teletype (or PTY) handles I/O between the terminal process and Konsole.
46 * The terminal emulation ( Emulation and subclasses ) processes the output stream from the
47 * PTY and produces a character image which is then shown on views connected to the session.
48 *
49 * Each Session can be connected to one or more views by using the addView() method.
50 * The attached views can then display output from the program running in the terminal
51 * or send input to the program in the terminal in the form of keypresses and mouse
52 * activity.
53 */
54class Session : public QObject {
55    Q_OBJECT
56
57public:
58    Q_PROPERTY(QString name READ nameTitle)
59    Q_PROPERTY(int processId READ processId)
60    Q_PROPERTY(QString keyBindings READ keyBindings WRITE setKeyBindings)
61    Q_PROPERTY(QSize size READ size WRITE setSize)
62
63    /**
64     * Constructs a new session.
65     *
66     * To start the terminal process, call the run() method,
67     * after specifying the program and arguments
68     * using setProgram() and setArguments()
69     *
70     * If no program or arguments are specified explicitly, the Session
71     * falls back to using the program specified in the SHELL environment
72     * variable.
73     */
74    Session(QObject* parent = nullptr);
75    ~Session() override;
76
77    /**
78     * Returns true if the session is currently running.  This will be true
79     * after run() has been called successfully.
80     */
81    bool isRunning() const;
82
83    /**
84     * Sets the profile associated with this session.
85     *
86     * @param profileKey A key which can be used to obtain the current
87     * profile settings from the SessionManager
88     */
89    void setProfileKey(const QString & profileKey);
90    /**
91     * Returns the profile key associated with this session.
92     * This can be passed to the SessionManager to obtain the current
93     * profile settings.
94     */
95    QString profileKey() const;
96
97    /**
98     * Adds a new view for this session.
99     *
100     * The viewing widget will display the output from the terminal and
101     * input from the viewing widget (key presses, mouse activity etc.)
102     * will be sent to the terminal.
103     *
104     * Views can be removed using removeView().  The session is automatically
105     * closed when the last view is removed.
106     */
107    void addView(TerminalDisplay * widget);
108    /**
109     * Removes a view from this session.  When the last view is removed,
110     * the session will be closed automatically.
111     *
112     * @p widget will no longer display output from or send input
113     * to the terminal
114     */
115    void removeView(TerminalDisplay * widget);
116
117    /**
118     * Returns the views connected to this session
119     */
120    QList<TerminalDisplay *> views() const;
121
122    /**
123     * Returns the terminal emulation instance being used to encode / decode
124     * characters to / from the process.
125     */
126    Emulation * emulation() const;
127
128    /**
129     * Returns the environment of this session as a list of strings like
130     * VARIABLE=VALUE
131     */
132    QStringList environment() const;
133    /**
134     * Sets the environment for this session.
135     * @p environment should be a list of strings like
136     * VARIABLE=VALUE
137     */
138    void setEnvironment(const QStringList & environment);
139
140    /** Returns the unique ID for this session. */
141    int sessionId() const;
142
143    /**
144     * Return the session title set by the user (ie. the program running
145     * in the terminal), or an empty string if the user has not set a custom title
146     */
147    QString userTitle() const;
148
149    /**
150     * This enum describes the contexts for which separate
151     * tab title formats may be specified.
152     */
153    enum TabTitleContext {
154        /** Default tab title format */
155        LocalTabTitle,
156        /**
157         * Tab title format used session currently contains
158         * a connection to a remote computer (via SSH)
159         */
160        RemoteTabTitle
161    };
162    /**
163     * Sets the format used by this session for tab titles.
164     *
165     * @param context The context whose format should be set.
166     * @param format The tab title format.  This may be a mixture
167     * of plain text and dynamic elements denoted by a '%' character
168     * followed by a letter.  (eg. %d for directory).  The dynamic
169     * elements available depend on the @p context
170     */
171    void setTabTitleFormat(TabTitleContext context , const QString & format);
172    /** Returns the format used by this session for tab titles. */
173    QString tabTitleFormat(TabTitleContext context) const;
174
175
176    /** Returns the arguments passed to the shell process when run() is called. */
177    QStringList arguments() const;
178    /** Returns the program name of the shell process started when run() is called. */
179    QString program() const;
180
181    /**
182     * Sets the command line arguments which the session's program will be passed when
183     * run() is called.
184     */
185    void setArguments(const QStringList & arguments);
186    /** Sets the program to be executed when run() is called. */
187    void setProgram(const QString & program);
188
189    /** Returns the session's current working directory. */
190    QString initialWorkingDirectory() {
191        return _initialWorkingDir;
192    }
193
194    /**
195     * Sets the initial working directory for the session when it is run
196     * This has no effect once the session has been started.
197     */
198    void setInitialWorkingDirectory( const QString & dir );
199
200    /**
201     * Sets the type of history store used by this session.
202     * Lines of output produced by the terminal are added
203     * to the history store.  The type of history store
204     * used affects the number of lines which can be
205     * remembered before they are lost and the storage
206     * (in memory, on-disk etc.) used.
207     */
208    void setHistoryType(const HistoryType & type);
209    /**
210     * Returns the type of history store used by this session.
211     */
212    const HistoryType & historyType() const;
213    /**
214     * Clears the history store used by this session.
215     */
216    void clearHistory();
217
218    /**
219     * Enables monitoring for activity in the session.
220     * This will cause notifySessionState() to be emitted
221     * with the NOTIFYACTIVITY state flag when output is
222     * received from the terminal.
223     */
224    void setMonitorActivity(bool);
225    /** Returns true if monitoring for activity is enabled. */
226    bool isMonitorActivity() const;
227
228    /**
229     * Enables monitoring for silence in the session.
230     * This will cause notifySessionState() to be emitted
231     * with the NOTIFYSILENCE state flag when output is not
232     * received from the terminal for a certain period of
233     * time, specified with setMonitorSilenceSeconds()
234     */
235    void setMonitorSilence(bool);
236    /**
237     * Returns true if monitoring for inactivity (silence)
238     * in the session is enabled.
239     */
240    bool isMonitorSilence()  const;
241    /** See setMonitorSilence() */
242    void setMonitorSilenceSeconds(int seconds);
243
244    /**
245     * Sets the key bindings used by this session.  The bindings
246     * specify how input key sequences are translated into
247     * the character stream which is sent to the terminal.
248     *
249     * @param id The name of the key bindings to use.  The
250     * names of available key bindings can be determined using the
251     * KeyboardTranslatorManager class.
252     */
253    void setKeyBindings(const QString & id);
254    /** Returns the name of the key bindings used by this session. */
255    QString keyBindings() const;
256
257    /**
258     * This enum describes the available title roles.
259     */
260    enum TitleRole {
261        /** The name of the session. */
262        NameRole,
263        /** The title of the session which is displayed in tabs etc. */
264        DisplayedTitleRole
265    };
266
267    /** Sets the session's title for the specified @p role to @p title. */
268    void setTitle(TitleRole role , const QString & title);
269    /** Returns the session's title for the specified @p role. */
270    QString title(TitleRole role) const;
271    /** Convenience method used to read the name property.  Returns title(Session::NameRole). */
272    QString nameTitle() const {
273        return title(Session::NameRole);
274    }
275
276    /** Sets the name of the icon associated with this session. */
277    void setIconName(const QString & iconName);
278    /** Returns the name of the icon associated with this session. */
279    QString iconName() const;
280
281    /** Sets the text of the icon associated with this session. */
282    void setIconText(const QString & iconText);
283    /** Returns the text of the icon associated with this session. */
284    QString iconText() const;
285
286    /** Flag if the title/icon was changed by user/shell. */
287    bool isTitleChanged() const;
288
289    /** Specifies whether a utmp entry should be created for the pty used by this session. */
290    void setAddToUtmp(bool);
291
292    /** Sends the specified @p signal to the terminal process. */
293    bool sendSignal(int signal);
294
295    /**
296     * Specifies whether to close the session automatically when the terminal
297     * process terminates.
298     */
299    void setAutoClose(bool b) {
300        _autoClose = b;
301    }
302
303    /**
304     * Sets whether flow control is enabled for this terminal
305     * session.
306     */
307    void setFlowControlEnabled(bool enabled);
308
309    /** Returns whether flow control is enabled for this terminal session. */
310    bool flowControlEnabled() const;
311
312    /**
313     * Sends @p text to the current foreground terminal program.
314     */
315    void sendText(const QString & text) const;
316
317    void sendKeyEvent(QKeyEvent* e) const;
318
319    /**
320     * Returns the process id of the terminal process.
321     * This is the id used by the system API to refer to the process.
322     */
323    int processId() const;
324
325    /**
326     * Returns the process id of the terminal's foreground process.
327     * This is initially the same as processId() but can change
328     * as the user starts other programs inside the terminal.
329     */
330    int foregroundProcessId() const;
331
332    /** Returns the terminal session's window size in lines and columns. */
333    QSize size();
334    /**
335     * Emits a request to resize the session to accommodate
336     * the specified window size.
337     *
338     * @param size The size in lines and columns to request.
339     */
340    void setSize(const QSize & size);
341
342    /** Sets the text codec used by this session's terminal emulation. */
343    void setCodec(QTextCodec * codec) const;
344
345    /**
346     * Sets whether the session has a dark background or not.  The session
347     * uses this information to set the COLORFGBG variable in the process's
348     * environment, which allows the programs running in the terminal to determine
349     * whether the background is light or dark and use appropriate colors by default.
350     *
351     * This has no effect once the session is running.
352     */
353    void setDarkBackground(bool darkBackground);
354    /**
355     * Returns true if the session has a dark background.
356     * See setDarkBackground()
357     */
358    bool hasDarkBackground() const;
359
360    /**
361     * Attempts to get the shell program to redraw the current display area.
362     * This can be used after clearing the screen, for example, to get the
363     * shell to redraw the prompt line.
364     */
365    void refresh();
366
367//  void startZModem(const QString &rz, const QString &dir, const QStringList &list);
368//  void cancelZModem();
369//  bool isZModemBusy() { return _zmodemBusy; }
370
371    /**
372     * Returns a pty slave file descriptor.
373     * This can be used for display and control
374     * a remote terminal.
375     */
376    int getPtySlaveFd() const;
377
378public slots:
379
380    /**
381     * Starts the terminal session.
382     *
383     * This creates the terminal process and connects the teletype to it.
384     */
385    void run();
386
387    /**
388     * Starts the terminal session for "as is" PTY
389     * (without the direction a data to internal terminal process).
390     * It can be used for control or display a remote/external terminal.
391     */
392    void runEmptyPTY();
393
394    /**
395     * Closes the terminal session.  This sends a hangup signal
396     * (SIGHUP) to the terminal process and causes the done(Session*)
397     * signal to be emitted.
398     */
399    void close();
400
401    /**
402     * Changes the session title or other customizable aspects of the terminal
403     * emulation display. For a list of what may be changed see the
404     * Emulation::titleChanged() signal.
405     */
406    void setUserTitle( int, const QString & caption );
407
408signals:
409
410    /** Emitted when the terminal process starts. */
411    void started();
412
413    /**
414     * Emitted when the terminal process exits.
415     */
416    void finished();
417
418    /**
419     * Emitted when output is received from the terminal process.
420     */
421    void receivedData( const QString & text );
422
423    /** Emitted when the session's title has changed. */
424    void titleChanged();
425
426    /** Emitted when the session's profile has changed. */
427    void profileChanged(const QString & profile);
428
429    /**
430     * Emitted when the activity state of this session changes.
431     *
432     * @param state The new state of the session.  This may be one
433     * of NOTIFYNORMAL, NOTIFYSILENCE or NOTIFYACTIVITY
434     */
435    void stateChanged(int state);
436
437    /** Emitted when a bell event occurs in the session. */
438    void bellRequest( const QString & message );
439
440    /**
441     * Requests that the color the text for any tabs associated with
442     * this session should be changed;
443     *
444     * TODO: Document what the parameter does
445     */
446    void changeTabTextColorRequest(int);
447
448    /**
449     * Requests that the background color of views on this session
450     * should be changed.
451     */
452    void changeBackgroundColorRequest(const QColor &);
453
454    /** TODO: Document me. */
455    void openUrlRequest(const QString & url);
456
457    /** TODO: Document me. */
458//  void zmodemDetected();
459
460    /**
461     * Emitted when the terminal process requests a change
462     * in the size of the terminal window.
463     *
464     * @param size The requested window size in terms of lines and columns.
465     */
466    void resizeRequest(const QSize & size);
467
468    /**
469     * Emitted when a profile change command is received from the terminal.
470     *
471     * @param text The text of the command.  This is a string of the form
472     * "PropertyName=Value;PropertyName=Value ..."
473     */
474    void profileChangeCommandReceived(const QString & text);
475
476    /**
477     * Emitted when the flow control state changes.
478     *
479     * @param enabled True if flow control is enabled or false otherwise.
480     */
481    void flowControlEnabledChanged(bool enabled);
482
483    /**
484     * Broker for Emulation::cursorChanged() signal
485     */
486    void cursorChanged(Emulation::KeyboardCursorShape cursorShape, bool blinkingCursorEnabled);
487
488    void silence();
489    void activity();
490
491private slots:
492    void done(int);
493
494//  void fireZModemDetected();
495
496    void onReceiveBlock( const char * buffer, int len );
497    void monitorTimerDone();
498
499    void onViewSizeChange(int height, int width);
500    void onEmulationSizeChange(QSize);
501
502    void activityStateSet(int);
503
504    //automatically detach views from sessions when view is destroyed
505    void viewDestroyed(QObject * view);
506
507//  void zmodemReadStatus();
508//  void zmodemReadAndSendBlock();
509//  void zmodemRcvBlock(const char *data, int len);
510//  void zmodemFinished();
511
512private:
513
514    void updateTerminalSize();
515    WId windowId() const;
516
517    int            _uniqueIdentifier;
518
519    Pty     *_shellProcess;
520    Emulation  *  _emulation;
521
522    QList<TerminalDisplay *> _views;
523
524    bool           _monitorActivity;
525    bool           _monitorSilence;
526    bool           _notifiedActivity;
527    bool           _masterMode;
528    bool           _autoClose;
529    bool           _wantedClose;
530    QTimer    *    _monitorTimer;
531
532    int            _silenceSeconds;
533
534    QString        _nameTitle;
535    QString        _displayTitle;
536    QString        _userTitle;
537
538    QString        _localTabTitleFormat;
539    QString        _remoteTabTitleFormat;
540
541    QString        _iconName;
542    QString        _iconText; // as set by: echo -en '\033]1;IconText\007
543    bool           _isTitleChanged; ///< flag if the title/icon was changed by user
544    bool           _addToUtmp;
545    bool           _flowControl;
546    bool           _fullScripting;
547
548    QString        _program;
549    QStringList    _arguments;
550
551    QStringList    _environment;
552    int            _sessionId;
553
554    QString        _initialWorkingDir;
555
556    // ZModem
557//  bool           _zmodemBusy;
558//  KProcess*      _zmodemProc;
559//  ZModemDialog*  _zmodemProgress;
560
561    // Color/Font Changes by ESC Sequences
562
563    QColor         _modifiedBackground; // as set by: echo -en '\033]11;Color\007
564
565    QString        _profileKey;
566
567    bool _hasDarkBackground;
568
569    static int lastSessionId;
570
571    int ptySlaveFd;
572
573};
574
575/**
576 * Provides a group of sessions which is divided into master and slave sessions.
577 * Activity in master sessions can be propagated to all sessions within the group.
578 * The type of activity which is propagated and method of propagation is controlled
579 * by the masterMode() flags.
580 */
581class SessionGroup : public QObject {
582    Q_OBJECT
583
584public:
585    /** Constructs an empty session group. */
586    SessionGroup();
587    /** Destroys the session group and removes all connections between master and slave sessions. */
588    ~SessionGroup() override;
589
590    /** Adds a session to the group. */
591    void addSession( Session * session );
592    /** Removes a session from the group. */
593    void removeSession( Session * session );
594
595    /** Returns the list of sessions currently in the group. */
596    QList<Session *> sessions() const;
597
598    /**
599     * Sets whether a particular session is a master within the group.
600     * Changes or activity in the group's master sessions may be propagated
601     * to all the sessions in the group, depending on the current masterMode()
602     *
603     * @param session The session whose master status should be changed.
604     * @param master True to make this session a master or false otherwise
605     */
606    void setMasterStatus( Session * session , bool master );
607    /** Returns the master status of a session.  See setMasterStatus() */
608    bool masterStatus( Session * session ) const;
609
610    /**
611     * This enum describes the options for propagating certain activity or
612     * changes in the group's master sessions to all sessions in the group.
613     */
614    enum MasterMode {
615        /**
616         * Any input key presses in the master sessions are sent to all
617         * sessions in the group.
618         */
619        CopyInputToAll = 1
620    };
621
622    /**
623     * Specifies which activity in the group's master sessions is propagated
624     * to all sessions in the group.
625     *
626     * @param mode A bitwise OR of MasterMode flags.
627     */
628    void setMasterMode( int mode );
629    /**
630     * Returns a bitwise OR of the active MasterMode flags for this group.
631     * See setMasterMode()
632     */
633    int masterMode() const;
634
635private:
636    void connectPair(Session * master , Session * other) const;
637    void disconnectPair(Session * master , Session * other) const;
638    void connectAll(bool connect);
639    QList<Session *> masters() const;
640
641    // maps sessions to their master status
642    QHash<Session *,bool> _sessions;
643
644    int _masterMode;
645};
646
647}
648
649#endif
Note: See TracBrowser for help on using the repository browser.