| 1 | /* |
| 2 | This file is part of Konsole |
| 3 | |
| 4 | Copyright (C) 2006-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 | // Own |
| 26 | #include "Session.h" |
| 27 | |
| 28 | // Standard |
| 29 | #include <cstdlib> |
| 30 | |
| 31 | // Qt |
| 32 | #include <QApplication> |
| 33 | #include <QByteRef> |
| 34 | #include <QDir> |
| 35 | #include <QFile> |
| 36 | #include <QRegExp> |
| 37 | #include <QStringList> |
| 38 | #include <QFile> |
| 39 | #include <QtDebug> |
| 40 | |
| 41 | #include "Pty.h" |
| 42 | //#include "kptyprocess.h" |
| 43 | #include "TerminalDisplay.h" |
| 44 | #include "ShellCommand.h" |
| 45 | #include "Vt102Emulation.h" |
| 46 | |
| 47 | // QMLTermWidget |
| 48 | #include <QQuickWindow> |
| 49 | |
| 50 | using namespace Konsole; |
| 51 | |
| 52 | int Session::lastSessionId = 0; |
| 53 | |
| 54 | Session::Session(QObject* parent) : |
| 55 | QObject(parent), |
| 56 | _shellProcess(nullptr) |
| 57 | , _emulation(nullptr) |
| 58 | , _monitorActivity(false) |
| 59 | , _monitorSilence(false) |
| 60 | , _notifiedActivity(false) |
| 61 | , _autoClose(true) |
| 62 | , _wantedClose(false) |
| 63 | , _silenceSeconds(10) |
| 64 | , _isTitleChanged(false) |
| 65 | , _addToUtmp(false) // disabled by default because of a bug encountered on certain systems |
| 66 | // which caused Konsole to hang when closing a tab and then opening a new |
| 67 | // one. A 'QProcess destroyed while still running' warning was being |
| 68 | // printed to the terminal. Likely a problem in KPty::logout() |
| 69 | // or KPty::login() which uses a QProcess to start /usr/bin/utempter |
| 70 | , _flowControl(true) |
| 71 | , _fullScripting(false) |
| 72 | , _sessionId(0) |
| 73 | // , _zmodemBusy(false) |
| 74 | // , _zmodemProc(0) |
| 75 | // , _zmodemProgress(0) |
| 76 | , _hasDarkBackground(false) |
| 77 | , _foregroundProcessInfo(NULL) |
| 78 | , _foregroundPid(0) |
| 79 | { |
| 80 | //prepare DBus communication |
| 81 | // new SessionAdaptor(this); |
| 82 | _sessionId = ++lastSessionId; |
| 83 | // QDBusConnection::sessionBus().registerObject(QLatin1String("/Sessions/")+QString::number(_sessionId), this); |
| 84 | |
| 85 | //create teletype for I/O with shell process |
| 86 | _shellProcess = new Pty(); |
| 87 | ptySlaveFd = _shellProcess->pty()->slaveFd(); |
| 88 | |
| 89 | //create emulation backend |
| 90 | _emulation = new Vt102Emulation(); |
| 91 | |
| 92 | connect( _emulation, SIGNAL( titleChanged( int, const QString & ) ), |
| 93 | this, SLOT( setUserTitle( int, const QString & ) ) ); |
| 94 | connect( _emulation, SIGNAL( stateSet(int) ), |
| 95 | this, SLOT( activityStateSet(int) ) ); |
| 96 | // connect( _emulation, SIGNAL( zmodemDetected() ), this , |
| 97 | // SLOT( fireZModemDetected() ) ); |
| 98 | connect( _emulation, SIGNAL( changeTabTextColorRequest( int ) ), |
| 99 | this, SIGNAL( changeTabTextColorRequest( int ) ) ); |
| 100 | connect( _emulation, SIGNAL(profileChangeCommandReceived(const QString &)), |
| 101 | this, SIGNAL( profileChangeCommandReceived(const QString &)) ); |
| 102 | |
| 103 | connect(_emulation, SIGNAL(imageResizeRequest(QSize)), |
| 104 | this, SLOT(onEmulationSizeChange(QSize))); |
| 105 | connect(_emulation, SIGNAL(imageSizeChanged(int, int)), |
| 106 | this, SLOT(onViewSizeChange(int, int))); |
| 107 | connect(_emulation, &Vt102Emulation::cursorChanged, |
| 108 | this, &Session::cursorChanged); |
| 109 | |
| 110 | //connect teletype to emulation backend |
| 111 | _shellProcess->setUtf8Mode(_emulation->utf8()); |
| 112 | |
| 113 | connect( _shellProcess,SIGNAL(receivedData(const char *,int)),this, |
| 114 | SLOT(onReceiveBlock(const char *,int)) ); |
| 115 | connect( _emulation,SIGNAL(sendData(const char *,int)),_shellProcess, |
| 116 | SLOT(sendData(const char *,int)) ); |
| 117 | connect( _emulation,SIGNAL(lockPtyRequest(bool)),_shellProcess,SLOT(lockPty(bool)) ); |
| 118 | connect( _emulation,SIGNAL(useUtf8Request(bool)),_shellProcess,SLOT(setUtf8Mode(bool)) ); |
| 119 | |
| 120 | connect( _shellProcess,SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(done(int)) ); |
| 121 | // not in kprocess anymore connect( _shellProcess,SIGNAL(done(int)), this, SLOT(done(int)) ); |
| 122 | |
| 123 | //setup timer for monitoring session activity |
| 124 | _monitorTimer = new QTimer(this); |
| 125 | _monitorTimer->setSingleShot(true); |
| 126 | connect(_monitorTimer, SIGNAL(timeout()), this, SLOT(monitorTimerDone())); |
| 127 | } |
| 128 | |
| 129 | WId Session::windowId() const |
| 130 | { |
| 131 | // On Qt5, requesting window IDs breaks QQuickWidget and the likes, |
| 132 | // for example, see the following bug reports: |
| 133 | // https://bugreports.qt.io/browse/QTBUG-40765 |
| 134 | // https://codereview.qt-project.org/#/c/94880/ |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | void Session::setDarkBackground(bool darkBackground) |
| 139 | { |
| 140 | _hasDarkBackground = darkBackground; |
| 141 | } |
| 142 | bool Session::hasDarkBackground() const |
| 143 | { |
| 144 | return _hasDarkBackground; |
| 145 | } |
| 146 | bool Session::isRunning() const |
| 147 | { |
| 148 | return _shellProcess->state() == QProcess::Running; |
| 149 | } |
| 150 | |
| 151 | void Session::setCodec(QTextCodec * codec) const |
| 152 | { |
| 153 | emulation()->setCodec(codec); |
| 154 | } |
| 155 | |
| 156 | void Session::setProgram(const QString & program) |
| 157 | { |
| 158 | _program = ShellCommand::expand(program); |
| 159 | } |
| 160 | void Session::setInitialWorkingDirectory(const QString & dir) |
| 161 | { |
| 162 | _initialWorkingDir = ShellCommand::expand(dir); |
| 163 | } |
| 164 | void Session::setArguments(const QStringList & arguments) |
| 165 | { |
| 166 | _arguments = ShellCommand::expand(arguments); |
| 167 | } |
| 168 | |
| 169 | QList<TerminalDisplay *> Session::views() const |
| 170 | { |
| 171 | return _views; |
| 172 | } |
| 173 | |
| 174 | void Session::addView(TerminalDisplay * widget) |
| 175 | { |
| 176 | Q_ASSERT( !_views.contains(widget) ); |
| 177 | |
| 178 | _views.append(widget); |
| 179 | |
| 180 | if ( _emulation != nullptr ) { |
| 181 | // connect emulation - view signals and slots |
| 182 | connect( widget , &TerminalDisplay::keyPressedSignal, _emulation , |
| 183 | &Emulation::sendKeyEvent); |
| 184 | connect( widget , SIGNAL(mouseSignal(int,int,int,int)) , _emulation , |
| 185 | SLOT(sendMouseEvent(int,int,int,int)) ); |
| 186 | connect( widget , SIGNAL(sendStringToEmu(const char *)) , _emulation , |
| 187 | SLOT(sendString(const char *)) ); |
| 188 | |
| 189 | // allow emulation to notify view when the foreground process |
| 190 | // indicates whether or not it is interested in mouse signals |
| 191 | connect( _emulation , SIGNAL(programUsesMouseChanged(bool)) , widget , |
| 192 | SLOT(setUsesMouse(bool)) ); |
| 193 | |
| 194 | widget->setUsesMouse( _emulation->programUsesMouse() ); |
| 195 | |
| 196 | connect( _emulation , SIGNAL(programBracketedPasteModeChanged(bool)) , |
| 197 | widget , SLOT(setBracketedPasteMode(bool)) ); |
| 198 | |
| 199 | widget->setBracketedPasteMode(_emulation->programBracketedPasteMode()); |
| 200 | |
| 201 | widget->setScreenWindow(_emulation->createWindow()); |
| 202 | } |
| 203 | |
| 204 | //connect view signals and slots |
| 205 | QObject::connect( widget ,SIGNAL(changedContentSizeSignal(int,int)),this, |
| 206 | SLOT(onViewSizeChange(int,int))); |
| 207 | |
| 208 | QObject::connect( widget ,SIGNAL(destroyed(QObject *)) , this , |
| 209 | SLOT(viewDestroyed(QObject *)) ); |
| 210 | //slot for close |
| 211 | //QObject::connect(this, SIGNAL(finished()), widget, SLOT(close())); |
| 212 | |
| 213 | } |
| 214 | |
| 215 | void Session::viewDestroyed(QObject * view) |
| 216 | { |
| 217 | TerminalDisplay * display = (TerminalDisplay *)view; |
| 218 | |
| 219 | Q_ASSERT( _views.contains(display) ); |
| 220 | |
| 221 | removeView(display); |
| 222 | } |
| 223 | |
| 224 | void Session::removeView(TerminalDisplay * widget) |
| 225 | { |
| 226 | _views.removeAll(widget); |
| 227 | |
| 228 | disconnect(widget,nullptr,this,nullptr); |
| 229 | |
| 230 | if ( _emulation != nullptr ) { |
| 231 | // disconnect |
| 232 | // - key presses signals from widget |
| 233 | // - mouse activity signals from widget |
| 234 | // - string sending signals from widget |
| 235 | // |
| 236 | // ... and any other signals connected in addView() |
| 237 | disconnect( widget, nullptr, _emulation, nullptr); |
| 238 | |
| 239 | // disconnect state change signals emitted by emulation |
| 240 | disconnect( _emulation , nullptr , widget , nullptr); |
| 241 | } |
| 242 | |
| 243 | // close the session automatically when the last view is removed |
| 244 | if ( _views.count() == 0 ) { |
| 245 | close(); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | void Session::run() |
| 250 | { |
| 251 | // Upon a KPty error, there is no description on what that error was... |
| 252 | // Check to see if the given program is executable. |
| 253 | |
| 254 | /* ok iam not exactly sure where _program comes from - however it was set to /bin/bash on my system |
| 255 | * Thats bad for BSD as its /usr/local/bin/bash there - its also bad for arch as its /usr/bin/bash there too! |
| 256 | * So i added a check to see if /bin/bash exists - if no then we use $SHELL - if that does not exist either, we fall back to /bin/sh |
| 257 | * As far as i know /bin/sh exists on every unix system.. You could also just put some ifdef __FREEBSD__ here but i think these 2 filechecks are worth |
| 258 | * their computing time on any system - especially with the problem on arch linux beeing there too. |
| 259 | */ |
| 260 | QString exec = QString::fromLocal8Bit(QFile::encodeName(_program)); |
| 261 | // if 'exec' is not specified, fall back to default shell. if that |
| 262 | // is not set then fall back to /bin/sh |
| 263 | |
| 264 | // here we expect full path. If there is no fullpath let's expect it's |
| 265 | // a custom shell (eg. python, etc.) available in the PATH. |
| 266 | if (exec.startsWith(QLatin1Char('/')) || exec.isEmpty()) |
| 267 | { |
| 268 | const QString defaultShell{QLatin1String("/bin/sh")}; |
| 269 | |
| 270 | QFile excheck(exec); |
| 271 | if ( exec.isEmpty() || !excheck.exists() ) { |
| 272 | exec = QString::fromLocal8Bit(qgetenv("SHELL")); |
| 273 | } |
| 274 | excheck.setFileName(exec); |
| 275 | |
| 276 | if ( exec.isEmpty() || !excheck.exists() ) { |
| 277 | qWarning() << "Neither default shell nor $SHELL is set to a correct path. Fallback to" << defaultShell; |
| 278 | exec = defaultShell; |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | // _arguments sometimes contain ("") so isEmpty() |
| 283 | // or count() does not work as expected... |
| 284 | QString argsTmp(_arguments.join(QLatin1Char(' ')).trimmed()); |
| 285 | QStringList arguments; |
| 286 | arguments << exec; |
| 287 | if (argsTmp.length()) |
| 288 | arguments << _arguments; |
| 289 | |
| 290 | QString cwd = QDir::currentPath(); |
| 291 | if (!_initialWorkingDir.isEmpty()) { |
| 292 | _shellProcess->setWorkingDirectory(_initialWorkingDir); |
| 293 | } else { |
| 294 | _shellProcess->setWorkingDirectory(cwd); |
| 295 | } |
| 296 | |
| 297 | _shellProcess->setFlowControlEnabled(_flowControl); |
| 298 | _shellProcess->setErase(_emulation->eraseChar()); |
| 299 | |
| 300 | // this is not strictly accurate use of the COLORFGBG variable. This does not |
| 301 | // tell the terminal exactly which colors are being used, but instead approximates |
| 302 | // the color scheme as "black on white" or "white on black" depending on whether |
| 303 | // the background color is deemed dark or not |
| 304 | QString backgroundColorHint = _hasDarkBackground ? QLatin1String("COLORFGBG=15;0") : QLatin1String("COLORFGBG=0;15"); |
| 305 | |
| 306 | /* if we do all the checking if this shell exists then we use it ;) |
| 307 | * Dont know about the arguments though.. maybe youll need some more checking im not sure |
| 308 | * However this works on Arch and FreeBSD now. |
| 309 | */ |
| 310 | int result = _shellProcess->start(exec, |
| 311 | arguments, |
| 312 | _environment << backgroundColorHint, |
| 313 | windowId(), |
| 314 | _addToUtmp); |
| 315 | |
| 316 | if (result < 0) { |
| 317 | qDebug() << "CRASHED! result: " << result; |
| 318 | return; |
| 319 | } |
| 320 | |
| 321 | _shellProcess->setWriteable(false); // We are reachable via kwrited. |
| 322 | emit started(); |
| 323 | } |
| 324 | |
| 325 | void Session::runEmptyPTY() |
| 326 | { |
| 327 | _shellProcess->setFlowControlEnabled(_flowControl); |
| 328 | _shellProcess->setErase(_emulation->eraseChar()); |
| 329 | _shellProcess->setWriteable(false); |
| 330 | |
| 331 | // disconnet send data from emulator to internal terminal process |
| 332 | disconnect( _emulation,SIGNAL(sendData(const char *,int)), |
| 333 | _shellProcess, SLOT(sendData(const char *,int)) ); |
| 334 | |
| 335 | _shellProcess->setEmptyPTYProperties(); |
| 336 | emit started(); |
| 337 | } |
| 338 | |
| 339 | void Session::setUserTitle( int what, const QString & caption ) |
| 340 | { |
| 341 | //set to true if anything is actually changed (eg. old _nameTitle != new _nameTitle ) |
| 342 | bool modified = false; |
| 343 | |
| 344 | // (btw: what=0 changes _userTitle and icon, what=1 only icon, what=2 only _nameTitle |
| 345 | if ((what == 0) || (what == 2)) { |
| 346 | _isTitleChanged = true; |
| 347 | if ( _userTitle != caption ) { |
| 348 | _userTitle = caption; |
| 349 | modified = true; |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | if ((what == 0) || (what == 1)) { |
| 354 | _isTitleChanged = true; |
| 355 | if ( _iconText != caption ) { |
| 356 | _iconText = caption; |
| 357 | modified = true; |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | if (what == 11) { |
| 362 | QString colorString = caption.section(QLatin1Char(';'),0,0); |
| 363 | //qDebug() << __FILE__ << __LINE__ << ": setting background colour to " << colorString; |
| 364 | QColor backColor = QColor(colorString); |
| 365 | if (backColor.isValid()) { // change color via \033]11;Color\007 |
| 366 | if (backColor != _modifiedBackground) { |
| 367 | _modifiedBackground = backColor; |
| 368 | |
| 369 | // bail out here until the code to connect the terminal display |
| 370 | // to the changeBackgroundColor() signal has been written |
| 371 | // and tested - just so we don't forget to do this. |
| 372 | Q_ASSERT( 0 ); |
| 373 | |
| 374 | emit changeBackgroundColorRequest(backColor); |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | if (what == 30) { |
| 380 | _isTitleChanged = true; |
| 381 | if ( _nameTitle != caption ) { |
| 382 | setTitle(Session::NameRole,caption); |
| 383 | return; |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | if (what == 31) { |
| 388 | QString cwd=caption; |
| 389 | cwd=cwd.replace( QRegExp(QLatin1String("^~")), QDir::homePath() ); |
| 390 | emit openUrlRequest(cwd); |
| 391 | } |
| 392 | |
| 393 | // change icon via \033]32;Icon\007 |
| 394 | if (what == 32) { |
| 395 | _isTitleChanged = true; |
| 396 | if ( _iconName != caption ) { |
| 397 | _iconName = caption; |
| 398 | |
| 399 | modified = true; |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | if (what == 50) { |
| 404 | emit profileChangeCommandReceived(caption); |
| 405 | return; |
| 406 | } |
| 407 | |
| 408 | if ( modified ) { |
| 409 | emit titleChanged(); |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | QString Session::userTitle() const |
| 414 | { |
| 415 | return _userTitle; |
| 416 | } |
| 417 | void Session::setTabTitleFormat(TabTitleContext context , const QString & format) |
| 418 | { |
| 419 | if ( context == LocalTabTitle ) { |
| 420 | _localTabTitleFormat = format; |
| 421 | } else if ( context == RemoteTabTitle ) { |
| 422 | _remoteTabTitleFormat = format; |
| 423 | } |
| 424 | } |
| 425 | QString Session::tabTitleFormat(TabTitleContext context) const |
| 426 | { |
| 427 | if ( context == LocalTabTitle ) { |
| 428 | return _localTabTitleFormat; |
| 429 | } else if ( context == RemoteTabTitle ) { |
| 430 | return _remoteTabTitleFormat; |
| 431 | } |
| 432 | |
| 433 | return QString(); |
| 434 | } |
| 435 | |
| 436 | void Session::monitorTimerDone() |
| 437 | { |
| 438 | //FIXME: The idea here is that the notification popup will appear to tell the user than output from |
| 439 | //the terminal has stopped and the popup will disappear when the user activates the session. |
| 440 | // |
| 441 | //This breaks with the addition of multiple views of a session. The popup should disappear |
| 442 | //when any of the views of the session becomes active |
| 443 | |
| 444 | |
| 445 | //FIXME: Make message text for this notification and the activity notification more descriptive. |
| 446 | if (_monitorSilence) { |
| 447 | emit silence(); |
| 448 | emit stateChanged(NOTIFYSILENCE); |
| 449 | } else { |
| 450 | emit stateChanged(NOTIFYNORMAL); |
| 451 | } |
| 452 | |
| 453 | _notifiedActivity=false; |
| 454 | } |
| 455 | |
| 456 | void Session::activityStateSet(int state) |
| 457 | { |
| 458 | if (state==NOTIFYBELL) { |
| 459 | emit bellRequest(tr("Bell in session '%1'").arg(_nameTitle)); |
| 460 | } else if (state==NOTIFYACTIVITY) { |
| 461 | if (_monitorSilence) { |
| 462 | _monitorTimer->start(_silenceSeconds*1000); |
| 463 | } |
| 464 | |
| 465 | if ( _monitorActivity ) { |
| 466 | //FIXME: See comments in Session::monitorTimerDone() |
| 467 | if (!_notifiedActivity) { |
| 468 | _notifiedActivity=true; |
| 469 | emit activity(); |
| 470 | } |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | if ( state==NOTIFYACTIVITY && !_monitorActivity ) { |
| 475 | state = NOTIFYNORMAL; |
| 476 | } |
| 477 | if ( state==NOTIFYSILENCE && !_monitorSilence ) { |
| 478 | state = NOTIFYNORMAL; |
| 479 | } |
| 480 | |
| 481 | emit stateChanged(state); |
| 482 | } |
| 483 | |
| 484 | void Session::onViewSizeChange(int /*height*/, int /*width*/) |
| 485 | { |
| 486 | updateTerminalSize(); |
| 487 | } |
| 488 | void Session::onEmulationSizeChange(QSize size) |
| 489 | { |
| 490 | setSize(size); |
| 491 | } |
| 492 | |
| 493 | void Session::updateTerminalSize() |
| 494 | { |
| 495 | QListIterator<TerminalDisplay *> viewIter(_views); |
| 496 | |
| 497 | int minLines = -1; |
| 498 | int minColumns = -1; |
| 499 | |
| 500 | // minimum number of lines and columns that views require for |
| 501 | // their size to be taken into consideration ( to avoid problems |
| 502 | // with new view widgets which haven't yet been set to their correct size ) |
| 503 | const int VIEW_LINES_THRESHOLD = 2; |
| 504 | const int VIEW_COLUMNS_THRESHOLD = 2; |
| 505 | |
| 506 | //select largest number of lines and columns that will fit in all visible views |
| 507 | while ( viewIter.hasNext() ) { |
| 508 | TerminalDisplay * view = viewIter.next(); |
| 509 | if ( //!view->isVisible() == false && QMLTermWidget: We need to disable this check. It has issues when resizing invisible terminal. |
| 510 | view->lines() >= VIEW_LINES_THRESHOLD && |
| 511 | view->columns() >= VIEW_COLUMNS_THRESHOLD ) { |
| 512 | minLines = (minLines == -1) ? view->lines() : qMin( minLines , view->lines() ); |
| 513 | minColumns = (minColumns == -1) ? view->columns() : qMin( minColumns , view->columns() ); |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | // backend emulation must have a _terminal of at least 1 column x 1 line in size |
| 518 | if ( minLines > 0 && minColumns > 0 ) { |
| 519 | _emulation->setImageSize( minLines , minColumns ); |
| 520 | _shellProcess->setWindowSize( minLines , minColumns ); |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | void Session::refresh() |
| 525 | { |
| 526 | // attempt to get the shell process to redraw the display |
| 527 | // |
| 528 | // this requires the program running in the shell |
| 529 | // to cooperate by sending an update in response to |
| 530 | // a window size change |
| 531 | // |
| 532 | // the window size is changed twice, first made slightly larger and then |
| 533 | // resized back to its normal size so that there is actually a change |
| 534 | // in the window size (some shells do nothing if the |
| 535 | // new and old sizes are the same) |
| 536 | // |
| 537 | // if there is a more 'correct' way to do this, please |
| 538 | // send an email with method or patches to [email protected] |
| 539 | |
| 540 | const QSize existingSize = _shellProcess->windowSize(); |
| 541 | _shellProcess->setWindowSize(existingSize.height(),existingSize.width()+1); |
| 542 | _shellProcess->setWindowSize(existingSize.height(),existingSize.width()); |
| 543 | } |
| 544 | |
| 545 | bool Session::sendSignal(int signal) |
| 546 | { |
| 547 | int result = ::kill(static_cast<pid_t>(_shellProcess->processId()),signal); |
| 548 | |
| 549 | if ( result == 0 ) |
| 550 | { |
| 551 | _shellProcess->waitForFinished(); |
| 552 | return true; |
| 553 | } |
| 554 | else |
| 555 | return false; |
| 556 | } |
| 557 | |
| 558 | void Session::close() |
| 559 | { |
| 560 | _autoClose = true; |
| 561 | _wantedClose = true; |
| 562 | if (!_shellProcess->isRunning() || !sendSignal(SIGHUP)) { |
| 563 | // Forced close. |
| 564 | QTimer::singleShot(1, this, SIGNAL(finished())); |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | void Session::sendText(const QString & text) const |
| 569 | { |
| 570 | _emulation->sendText(text); |
| 571 | } |
| 572 | |
| 573 | void Session::sendKeyEvent(QKeyEvent* e) const |
| 574 | { |
| 575 | _emulation->sendKeyEvent(e, false); |
| 576 | } |
| 577 | |
| 578 | Session::~Session() |
| 579 | { |
| 580 | delete _emulation; |
| 581 | delete _shellProcess; |
| 582 | // delete _zmodemProc; |
| 583 | } |
| 584 | |
| 585 | void Session::setProfileKey(const QString & key) |
| 586 | { |
| 587 | _profileKey = key; |
| 588 | emit profileChanged(key); |
| 589 | } |
| 590 | QString Session::profileKey() const |
| 591 | { |
| 592 | return _profileKey; |
| 593 | } |
| 594 | |
| 595 | void Session::done(int exitStatus) |
| 596 | { |
| 597 | if (!_autoClose) { |
| 598 | _userTitle = QString::fromLatin1("This session is done. Finished"); |
| 599 | emit titleChanged(); |
| 600 | return; |
| 601 | } |
| 602 | |
| 603 | // message is not being used. But in the original kpty.cpp file |
| 604 | // (https://cgit.kde.org/kpty.git/) it's part of a notification. |
| 605 | // So, we make it translatable, hoping that in the future it will |
| 606 | // be used in some kind of notification. |
| 607 | QString message; |
| 608 | if (!_wantedClose || exitStatus != 0) { |
| 609 | |
| 610 | if (_shellProcess->exitStatus() == QProcess::NormalExit) { |
| 611 | message = tr("Session '%1' exited with status %2.").arg(_nameTitle).arg(exitStatus); |
| 612 | } else { |
| 613 | message = tr("Session '%1' crashed.").arg(_nameTitle); |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | if ( !_wantedClose && _shellProcess->exitStatus() != QProcess::NormalExit ) |
| 618 | message = tr("Session '%1' exited unexpectedly.").arg(_nameTitle); |
| 619 | else |
| 620 | emit finished(); |
| 621 | } |
| 622 | |
| 623 | Emulation * Session::emulation() const |
| 624 | { |
| 625 | return _emulation; |
| 626 | } |
| 627 | |
| 628 | QString Session::keyBindings() const |
| 629 | { |
| 630 | return _emulation->keyBindings(); |
| 631 | } |
| 632 | |
| 633 | QStringList Session::environment() const |
| 634 | { |
| 635 | return _environment; |
| 636 | } |
| 637 | |
| 638 | void Session::setEnvironment(const QStringList & environment) |
| 639 | { |
| 640 | _environment = environment; |
| 641 | } |
| 642 | |
| 643 | int Session::sessionId() const |
| 644 | { |
| 645 | return _sessionId; |
| 646 | } |
| 647 | |
| 648 | void Session::setKeyBindings(const QString & id) |
| 649 | { |
| 650 | _emulation->setKeyBindings(id); |
| 651 | } |
| 652 | |
| 653 | void Session::setTitle(TitleRole role , const QString & newTitle) |
| 654 | { |
| 655 | if ( title(role) != newTitle ) { |
| 656 | if ( role == NameRole ) { |
| 657 | _nameTitle = newTitle; |
| 658 | } else if ( role == DisplayedTitleRole ) { |
| 659 | _displayTitle = newTitle; |
| 660 | } |
| 661 | |
| 662 | emit titleChanged(); |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | QString Session::title(TitleRole role) const |
| 667 | { |
| 668 | if ( role == NameRole ) { |
| 669 | return _nameTitle; |
| 670 | } else if ( role == DisplayedTitleRole ) { |
| 671 | return _displayTitle; |
| 672 | } else { |
| 673 | return QString(); |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | void Session::setIconName(const QString & iconName) |
| 678 | { |
| 679 | if ( iconName != _iconName ) { |
| 680 | _iconName = iconName; |
| 681 | emit titleChanged(); |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | void Session::setIconText(const QString & iconText) |
| 686 | { |
| 687 | _iconText = iconText; |
| 688 | //kDebug(1211)<<"Session setIconText " << _iconText; |
| 689 | } |
| 690 | |
| 691 | QString Session::iconName() const |
| 692 | { |
| 693 | return _iconName; |
| 694 | } |
| 695 | |
| 696 | QString Session::iconText() const |
| 697 | { |
| 698 | return _iconText; |
| 699 | } |
| 700 | |
| 701 | bool Session::isTitleChanged() const |
| 702 | { |
| 703 | return _isTitleChanged; |
| 704 | } |
| 705 | |
| 706 | void Session::setHistoryType(const HistoryType & hType) |
| 707 | { |
| 708 | _emulation->setHistory(hType); |
| 709 | } |
| 710 | |
| 711 | const HistoryType & Session::historyType() const |
| 712 | { |
| 713 | return _emulation->history(); |
| 714 | } |
| 715 | |
| 716 | void Session::clearHistory() |
| 717 | { |
| 718 | _emulation->clearHistory(); |
| 719 | } |
| 720 | |
| 721 | QStringList Session::arguments() const |
| 722 | { |
| 723 | return _arguments; |
| 724 | } |
| 725 | |
| 726 | QString Session::program() const |
| 727 | { |
| 728 | return _program; |
| 729 | } |
| 730 | |
| 731 | // unused currently |
| 732 | bool Session::isMonitorActivity() const |
| 733 | { |
| 734 | return _monitorActivity; |
| 735 | } |
| 736 | // unused currently |
| 737 | bool Session::isMonitorSilence() const |
| 738 | { |
| 739 | return _monitorSilence; |
| 740 | } |
| 741 | |
| 742 | void Session::setMonitorActivity(bool _monitor) |
| 743 | { |
| 744 | _monitorActivity=_monitor; |
| 745 | _notifiedActivity=false; |
| 746 | |
| 747 | activityStateSet(NOTIFYNORMAL); |
| 748 | } |
| 749 | |
| 750 | void Session::setMonitorSilence(bool _monitor) |
| 751 | { |
| 752 | if (_monitorSilence==_monitor) { |
| 753 | return; |
| 754 | } |
| 755 | |
| 756 | _monitorSilence=_monitor; |
| 757 | if (_monitorSilence) { |
| 758 | _monitorTimer->start(_silenceSeconds*1000); |
| 759 | } else { |
| 760 | _monitorTimer->stop(); |
| 761 | } |
| 762 | |
| 763 | activityStateSet(NOTIFYNORMAL); |
| 764 | } |
| 765 | |
| 766 | void Session::setMonitorSilenceSeconds(int seconds) |
| 767 | { |
| 768 | _silenceSeconds=seconds; |
| 769 | if (_monitorSilence) { |
| 770 | _monitorTimer->start(_silenceSeconds*1000); |
| 771 | } |
| 772 | } |
| 773 | |
| 774 | void Session::setAddToUtmp(bool set) |
| 775 | { |
| 776 | _addToUtmp = set; |
| 777 | } |
| 778 | |
| 779 | void Session::setFlowControlEnabled(bool enabled) |
| 780 | { |
| 781 | if (_flowControl == enabled) { |
| 782 | return; |
| 783 | } |
| 784 | |
| 785 | _flowControl = enabled; |
| 786 | |
| 787 | if (_shellProcess) { |
| 788 | _shellProcess->setFlowControlEnabled(_flowControl); |
| 789 | } |
| 790 | |
| 791 | emit flowControlEnabledChanged(enabled); |
| 792 | } |
| 793 | bool Session::flowControlEnabled() const |
| 794 | { |
| 795 | return _flowControl; |
| 796 | } |
| 797 | //void Session::fireZModemDetected() |
| 798 | //{ |
| 799 | // if (!_zmodemBusy) |
| 800 | // { |
| 801 | // QTimer::singleShot(10, this, SIGNAL(zmodemDetected())); |
| 802 | // _zmodemBusy = true; |
| 803 | // } |
| 804 | //} |
| 805 | |
| 806 | //void Session::cancelZModem() |
| 807 | //{ |
| 808 | // _shellProcess->sendData("\030\030\030\030", 4); // Abort |
| 809 | // _zmodemBusy = false; |
| 810 | //} |
| 811 | |
| 812 | //void Session::startZModem(const QString &zmodem, const QString &dir, const QStringList &list) |
| 813 | //{ |
| 814 | // _zmodemBusy = true; |
| 815 | // _zmodemProc = new KProcess(); |
| 816 | // _zmodemProc->setOutputChannelMode( KProcess::SeparateChannels ); |
| 817 | // |
| 818 | // *_zmodemProc << zmodem << "-v" << list; |
| 819 | // |
| 820 | // if (!dir.isEmpty()) |
| 821 | // _zmodemProc->setWorkingDirectory(dir); |
| 822 | // |
| 823 | // _zmodemProc->start(); |
| 824 | // |
| 825 | // connect(_zmodemProc,SIGNAL (readyReadStandardOutput()), |
| 826 | // this, SLOT(zmodemReadAndSendBlock())); |
| 827 | // connect(_zmodemProc,SIGNAL (readyReadStandardError()), |
| 828 | // this, SLOT(zmodemReadStatus())); |
| 829 | // connect(_zmodemProc,SIGNAL (finished(int,QProcess::ExitStatus)), |
| 830 | // this, SLOT(zmodemFinished())); |
| 831 | // |
| 832 | // disconnect( _shellProcess,SIGNAL(block_in(const char*,int)), this, SLOT(onReceiveBlock(const char*,int)) ); |
| 833 | // connect( _shellProcess,SIGNAL(block_in(const char*,int)), this, SLOT(zmodemRcvBlock(const char*,int)) ); |
| 834 | // |
| 835 | // _zmodemProgress = new ZModemDialog(QApplication::activeWindow(), false, |
| 836 | // i18n("ZModem Progress")); |
| 837 | // |
| 838 | // connect(_zmodemProgress, SIGNAL(user1Clicked()), |
| 839 | // this, SLOT(zmodemDone())); |
| 840 | // |
| 841 | // _zmodemProgress->show(); |
| 842 | //} |
| 843 | |
| 844 | /*void Session::zmodemReadAndSendBlock() |
| 845 | { |
| 846 | _zmodemProc->setReadChannel( QProcess::StandardOutput ); |
| 847 | QByteArray data = _zmodemProc->readAll(); |
| 848 | |
| 849 | if ( data.count() == 0 ) |
| 850 | return; |
| 851 | |
| 852 | _shellProcess->sendData(data.constData(),data.count()); |
| 853 | } |
| 854 | */ |
| 855 | /* |
| 856 | void Session::zmodemReadStatus() |
| 857 | { |
| 858 | _zmodemProc->setReadChannel( QProcess::StandardError ); |
| 859 | QByteArray msg = _zmodemProc->readAll(); |
| 860 | while(!msg.isEmpty()) |
| 861 | { |
| 862 | int i = msg.indexOf('\015'); |
| 863 | int j = msg.indexOf('\012'); |
| 864 | QByteArray txt; |
| 865 | if ((i != -1) && ((j == -1) || (i < j))) |
| 866 | { |
| 867 | msg = msg.mid(i+1); |
| 868 | } |
| 869 | else if (j != -1) |
| 870 | { |
| 871 | txt = msg.left(j); |
| 872 | msg = msg.mid(j+1); |
| 873 | } |
| 874 | else |
| 875 | { |
| 876 | txt = msg; |
| 877 | msg.truncate(0); |
| 878 | } |
| 879 | if (!txt.isEmpty()) |
| 880 | _zmodemProgress->addProgressText(QString::fromLocal8Bit(txt)); |
| 881 | } |
| 882 | } |
| 883 | */ |
| 884 | /* |
| 885 | void Session::zmodemRcvBlock(const char *data, int len) |
| 886 | { |
| 887 | QByteArray ba( data, len ); |
| 888 | |
| 889 | _zmodemProc->write( ba ); |
| 890 | } |
| 891 | */ |
| 892 | /* |
| 893 | void Session::zmodemFinished() |
| 894 | { |
| 895 | if (_zmodemProc) |
| 896 | { |
| 897 | delete _zmodemProc; |
| 898 | _zmodemProc = 0; |
| 899 | _zmodemBusy = false; |
| 900 | |
| 901 | disconnect( _shellProcess,SIGNAL(block_in(const char*,int)), this ,SLOT(zmodemRcvBlock(const char*,int)) ); |
| 902 | connect( _shellProcess,SIGNAL(block_in(const char*,int)), this, SLOT(onReceiveBlock(const char*,int)) ); |
| 903 | |
| 904 | _shellProcess->sendData("\030\030\030\030", 4); // Abort |
| 905 | _shellProcess->sendData("\001\013\n", 3); // Try to get prompt back |
| 906 | _zmodemProgress->transferDone(); |
| 907 | } |
| 908 | } |
| 909 | */ |
| 910 | void Session::onReceiveBlock( const char * buf, int len ) |
| 911 | { |
| 912 | _emulation->receiveData( buf, len ); |
| 913 | emit receivedData( QString::fromLatin1( buf, len ) ); |
| 914 | } |
| 915 | |
| 916 | QSize Session::size() |
| 917 | { |
| 918 | return _emulation->imageSize(); |
| 919 | } |
| 920 | |
| 921 | void Session::setSize(const QSize & size) |
| 922 | { |
| 923 | if ((size.width() <= 1) || (size.height() <= 1)) { |
| 924 | return; |
| 925 | } |
| 926 | |
| 927 | emit resizeRequest(size); |
| 928 | } |
| 929 | int Session::foregroundProcessId() const |
| 930 | { |
| 931 | return _shellProcess->foregroundProcessGroup(); |
| 932 | } |
| 933 | |
| 934 | QString Session::foregroundProcessName() |
| 935 | { |
| 936 | QString name; |
| 937 | |
| 938 | if (updateForegroundProcessInfo()) { |
| 939 | bool ok = false; |
| 940 | name = _foregroundProcessInfo->name(&ok); |
| 941 | if (!ok) |
| 942 | name.clear(); |
| 943 | } |
| 944 | |
| 945 | return name; |
| 946 | } |
| 947 | |
| 948 | QString Session::currentDir() |
| 949 | { |
| 950 | QString path; |
| 951 | if (updateForegroundProcessInfo()) { |
| 952 | bool ok = false; |
| 953 | path= _foregroundProcessInfo->currentDir(&ok); |
| 954 | if (!ok) |
| 955 | path.clear(); |
| 956 | } |
| 957 | return path; |
| 958 | } |
| 959 | |
| 960 | bool Session::updateForegroundProcessInfo() |
| 961 | { |
| 962 | Q_ASSERT(_shellProcess); |
| 963 | |
| 964 | const int foregroundPid = _shellProcess->foregroundProcessGroup(); |
| 965 | if (foregroundPid != _foregroundPid) { |
| 966 | delete _foregroundProcessInfo; |
| 967 | _foregroundProcessInfo = ProcessInfo::newInstance(foregroundPid); |
| 968 | _foregroundPid = foregroundPid; |
| 969 | } |
| 970 | |
| 971 | if (_foregroundProcessInfo) { |
| 972 | _foregroundProcessInfo->update(); |
| 973 | return _foregroundProcessInfo->isValid(); |
| 974 | } else { |
| 975 | return false; |
| 976 | } |
| 977 | } |
| 978 | |
| 979 | |
| 980 | int Session::processId() const |
| 981 | { |
| 982 | return static_cast<int>(_shellProcess->processId()); |
| 983 | } |
| 984 | int Session::getPtySlaveFd() const |
| 985 | { |
| 986 | return ptySlaveFd; |
| 987 | } |
| 988 | |
| 989 | SessionGroup::SessionGroup() |
| 990 | : _masterMode(0) |
| 991 | { |
| 992 | } |
| 993 | SessionGroup::~SessionGroup() |
| 994 | { |
| 995 | // disconnect all |
| 996 | connectAll(false); |
| 997 | } |
| 998 | int SessionGroup::masterMode() const |
| 999 | { |
| 1000 | return _masterMode; |
| 1001 | } |
| 1002 | QList<Session *> SessionGroup::sessions() const |
| 1003 | { |
| 1004 | return _sessions.keys(); |
| 1005 | } |
| 1006 | bool SessionGroup::masterStatus(Session * session) const |
| 1007 | { |
| 1008 | return _sessions[session]; |
| 1009 | } |
| 1010 | |
| 1011 | void SessionGroup::addSession(Session * session) |
| 1012 | { |
| 1013 | _sessions.insert(session,false); |
| 1014 | |
| 1015 | QListIterator<Session *> masterIter(masters()); |
| 1016 | |
| 1017 | while ( masterIter.hasNext() ) { |
| 1018 | connectPair(masterIter.next(),session); |
| 1019 | } |
| 1020 | } |
| 1021 | void SessionGroup::removeSession(Session * session) |
| 1022 | { |
| 1023 | setMasterStatus(session,false); |
| 1024 | |
| 1025 | QListIterator<Session *> masterIter(masters()); |
| 1026 | |
| 1027 | while ( masterIter.hasNext() ) { |
| 1028 | disconnectPair(masterIter.next(),session); |
| 1029 | } |
| 1030 | |
| 1031 | _sessions.remove(session); |
| 1032 | } |
| 1033 | void SessionGroup::setMasterMode(int mode) |
| 1034 | { |
| 1035 | _masterMode = mode; |
| 1036 | |
| 1037 | connectAll(false); |
| 1038 | connectAll(true); |
| 1039 | } |
| 1040 | QList<Session *> SessionGroup::masters() const |
| 1041 | { |
| 1042 | return _sessions.keys(true); |
| 1043 | } |
| 1044 | void SessionGroup::connectAll(bool connect) |
| 1045 | { |
| 1046 | QListIterator<Session *> masterIter(masters()); |
| 1047 | |
| 1048 | while ( masterIter.hasNext() ) { |
| 1049 | Session * master = masterIter.next(); |
| 1050 | |
| 1051 | QListIterator<Session *> otherIter(_sessions.keys()); |
| 1052 | while ( otherIter.hasNext() ) { |
| 1053 | Session * other = otherIter.next(); |
| 1054 | |
| 1055 | if ( other != master ) { |
| 1056 | if ( connect ) { |
| 1057 | connectPair(master,other); |
| 1058 | } else { |
| 1059 | disconnectPair(master,other); |
| 1060 | } |
| 1061 | } |
| 1062 | } |
| 1063 | } |
| 1064 | } |
| 1065 | void SessionGroup::setMasterStatus(Session * session, bool master) |
| 1066 | { |
| 1067 | bool wasMaster = _sessions[session]; |
| 1068 | _sessions[session] = master; |
| 1069 | |
| 1070 | if (wasMaster == master) { |
| 1071 | return; |
| 1072 | } |
| 1073 | |
| 1074 | QListIterator<Session *> iter(_sessions.keys()); |
| 1075 | while (iter.hasNext()) { |
| 1076 | Session * other = iter.next(); |
| 1077 | |
| 1078 | if (other != session) { |
| 1079 | if (master) { |
| 1080 | connectPair(session, other); |
| 1081 | } else { |
| 1082 | disconnectPair(session, other); |
| 1083 | } |
| 1084 | } |
| 1085 | } |
| 1086 | } |
| 1087 | |
| 1088 | void SessionGroup::connectPair(Session * master , Session * other) const |
| 1089 | { |
| 1090 | // qDebug() << k_funcinfo; |
| 1091 | |
| 1092 | if ( _masterMode & CopyInputToAll ) { |
| 1093 | qDebug() << "Connection session " << master->nameTitle() << "to" << other->nameTitle(); |
| 1094 | |
| 1095 | connect( master->emulation() , SIGNAL(sendData(const char *,int)) , other->emulation() , |
| 1096 | SLOT(sendString(const char *,int)) ); |
| 1097 | } |
| 1098 | } |
| 1099 | void SessionGroup::disconnectPair(Session * master , Session * other) const |
| 1100 | { |
| 1101 | // qDebug() << k_funcinfo; |
| 1102 | |
| 1103 | if ( _masterMode & CopyInputToAll ) { |
| 1104 | qDebug() << "Disconnecting session " << master->nameTitle() << "from" << other->nameTitle(); |
| 1105 | |
| 1106 | disconnect( master->emulation() , SIGNAL(sendData(const char *,int)) , other->emulation() , |
| 1107 | SLOT(sendString(const char *,int)) ); |
| 1108 | } |
| 1109 | } |
| 1110 | |
| 1111 | //#include "moc_Session.cpp" |