Debian Codegraph @

lib/Session.h

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