From ca598670fb927e196eb2a5ab90fa03bfb5dac0a2 Mon Sep 17 00:00:00 2001 From: Ingo Weinhold Date: Mon, 22 Nov 2010 15:30:26 +0000 Subject: [PATCH] * Removed the shell process ID from ActiveProcessInfo and moved it to new class ShellInfo, which also contains a flag whether the shell is the default shell. * If the Terminal has been started with a custom shell, also replace "%p" in the title by its name, when active. * Also show the on-close alert for the custom shell. Fixes #6844. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@39573 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/apps/terminal/ActiveProcessInfo.cpp | 5 +-- src/apps/terminal/ActiveProcessInfo.h | 5 +-- src/apps/terminal/Jamfile | 1 + src/apps/terminal/Shell.cpp | 22 ++++++++------ src/apps/terminal/Shell.h | 8 +++-- src/apps/terminal/ShellInfo.cpp | 15 +++++++++ src/apps/terminal/ShellInfo.h | 32 ++++++++++++++++++++ src/apps/terminal/TermView.cpp | 13 ++++++++ src/apps/terminal/TermView.h | 2 ++ src/apps/terminal/TermWindow.cpp | 29 +++++++++++++----- src/apps/terminal/TitlePlaceholderMapper.cpp | 21 +++++++------ src/apps/terminal/TitlePlaceholderMapper.h | 5 +++ 12 files changed, 122 insertions(+), 36 deletions(-) create mode 100644 src/apps/terminal/ShellInfo.cpp create mode 100644 src/apps/terminal/ShellInfo.h diff --git a/src/apps/terminal/ActiveProcessInfo.cpp b/src/apps/terminal/ActiveProcessInfo.cpp index ba5f4c829d..d930a2139e 100644 --- a/src/apps/terminal/ActiveProcessInfo.cpp +++ b/src/apps/terminal/ActiveProcessInfo.cpp @@ -10,7 +10,6 @@ ActiveProcessInfo::ActiveProcessInfo() : fID(-1), - fShellID(-1), fName(), fCurrentDirectory() { @@ -18,11 +17,10 @@ ActiveProcessInfo::ActiveProcessInfo() void -ActiveProcessInfo::SetTo(pid_t id, pid_t shellID, const BString& name, +ActiveProcessInfo::SetTo(pid_t id, const BString& name, const BString& currentDirectory) { fID = id; - fShellID = shellID; fName = name; fCurrentDirectory = currentDirectory; } @@ -32,7 +30,6 @@ void ActiveProcessInfo::Unset() { fID = -1; - fShellID = -1; fName = BString(); fCurrentDirectory = BString(); } diff --git a/src/apps/terminal/ActiveProcessInfo.h b/src/apps/terminal/ActiveProcessInfo.h index 4217790716..8861c54f2c 100644 --- a/src/apps/terminal/ActiveProcessInfo.h +++ b/src/apps/terminal/ActiveProcessInfo.h @@ -14,15 +14,13 @@ class ActiveProcessInfo { public: ActiveProcessInfo(); - void SetTo(pid_t id, pid_t shellID, - const BString& name, + void SetTo(pid_t id, const BString& name, const BString& currentDirectory); void Unset(); bool IsValid() const { return fID >= 0; } pid_t ID() const { return fID; } - pid_t ShellProcessID() const { return fShellID; } const BString& Name() const { return fName; } const BString& CurrentDirectory() const @@ -30,7 +28,6 @@ public: private: pid_t fID; - pid_t fShellID; BString fName; BString fCurrentDirectory; }; diff --git a/src/apps/terminal/Jamfile b/src/apps/terminal/Jamfile index 0da6f422f3..c032250bc4 100644 --- a/src/apps/terminal/Jamfile +++ b/src/apps/terminal/Jamfile @@ -22,6 +22,7 @@ Application Terminal : PrefWindow.cpp SetTitleDialog.cpp Shell.cpp + ShellInfo.cpp ShellParameters.cpp SmartTabView.cpp TermApp.cpp diff --git a/src/apps/terminal/Shell.cpp b/src/apps/terminal/Shell.cpp index d1914c0c9a..4436971867 100644 --- a/src/apps/terminal/Shell.cpp +++ b/src/apps/terminal/Shell.cpp @@ -126,7 +126,6 @@ typedef struct Shell::Shell() : fFd(-1), - fProcessID(-1), fTermParse(NULL), fAttached(false) { @@ -167,8 +166,8 @@ Shell::Close() if (fFd >= 0) { close(fFd); - kill(-fProcessID, SIGHUP); - fProcessID = -1; + kill(-fShellInfo.ProcessID(), SIGHUP); + fShellInfo.SetProcessID(-1); int status; wait(&status); fFd = -1; @@ -244,7 +243,7 @@ bool Shell::HasActiveProcesses() const { pid_t running = tcgetpgrp(fFd); - if (running == fProcessID || running == -1) + if (running == fShellInfo.ProcessID() || running == -1) return false; return true; @@ -284,7 +283,7 @@ Shell::GetActiveProcessInfo(ActiveProcessInfo& _info) const return false; // set the result - _info.SetTo(process, fProcessID, name, cwdPath.Path()); + _info.SetTo(process, name, cwdPath.Path()); return true; } @@ -391,6 +390,7 @@ Shell::_Spawn(int row, int col, const ShellParameters& parameters) struct passwd passwdStruct; struct passwd *passwdResult; char stringBuffer[256]; + if (argv == NULL || argc == 0) { if (!getpwuid_r(getuid(), &passwdStruct, stringBuffer, sizeof(stringBuffer), &passwdResult)) { @@ -399,7 +399,10 @@ Shell::_Spawn(int row, int col, const ShellParameters& parameters) argv = defaultArgs; argc = 2; - } + + fShellInfo.SetDefaultShell(true); + } else + fShellInfo.SetDefaultShell(false); signal(SIGTTOU, SIG_IGN); @@ -427,14 +430,15 @@ Shell::_Spawn(int row, int col, const ShellParameters& parameters) thread_id terminalThread = find_thread(NULL); /* Fork a child process. */ - if ((fProcessID = fork()) < 0) { + fShellInfo.SetProcessID(fork()); + if (fShellInfo.ProcessID() < 0) { close(master); return B_ERROR; } handshake_t handshake; - if (fProcessID == 0) { + if (fShellInfo.ProcessID() == 0) { // Now in child process. // close the PTY master side @@ -595,7 +599,7 @@ Shell::_Spawn(int row, int col, const ShellParameters& parameters) handshake.row = row; handshake.col = col; handshake.status = PTY_WS; - send_handshake_message(fProcessID, handshake); + send_handshake_message(fShellInfo.ProcessID(), handshake); break; } } diff --git a/src/apps/terminal/Shell.h b/src/apps/terminal/Shell.h index 825fce10b4..bc01fc9e42 100644 --- a/src/apps/terminal/Shell.h +++ b/src/apps/terminal/Shell.h @@ -14,7 +14,7 @@ #define _SHELL_H -#include +#include "ShellInfo.h" class ActiveProcessInfo; @@ -44,7 +44,10 @@ public: status_t SetAttr(const struct termios& attr); int FD() const; - pid_t ProcessID() const { return fProcessID; } + pid_t ProcessID() const + { return fShellInfo.ProcessID(); } + const ShellInfo& Info() const + { return fShellInfo; } bool HasActiveProcesses() const; bool GetActiveProcessInfo( @@ -58,6 +61,7 @@ private: const ShellParameters& parameters); private: + ShellInfo fShellInfo; int fFd; pid_t fProcessID; TermParse* fTermParse; diff --git a/src/apps/terminal/ShellInfo.cpp b/src/apps/terminal/ShellInfo.cpp new file mode 100644 index 0000000000..c4dcf97bee --- /dev/null +++ b/src/apps/terminal/ShellInfo.cpp @@ -0,0 +1,15 @@ +/* + * Copyright 2010, Ingo Weinhold, ingo_weinhold@gmx.de. + * Distributed under the terms of the MIT License. + */ + + +#include "ShellInfo.h" + + +ShellInfo::ShellInfo() + : + fProcessID(-1), + fIsDefaultShell(true) +{ +} diff --git a/src/apps/terminal/ShellInfo.h b/src/apps/terminal/ShellInfo.h new file mode 100644 index 0000000000..540868527d --- /dev/null +++ b/src/apps/terminal/ShellInfo.h @@ -0,0 +1,32 @@ +/* + * Copyright 2010, Ingo Weinhold, ingo_weinhold@gmx.de. + * Distributed under the terms of the MIT License. + */ +#ifndef SHELL_INFO_H +#define SHELL_INFO_H + + +#include + + +class ShellInfo { +public: + ShellInfo(); + + pid_t ProcessID() const + { return fProcessID; } + void SetProcessID(pid_t processID) + { fProcessID = processID; } + + bool IsDefaultShell() const + { return fIsDefaultShell; } + void SetDefaultShell(bool isDefault) + { fIsDefaultShell = isDefault; } + +private: + pid_t fProcessID; + bool fIsDefaultShell; +}; + + +#endif // SHELL_INFO_H diff --git a/src/apps/terminal/TermView.cpp b/src/apps/terminal/TermView.cpp index 1df2030790..1e4b6b8e81 100644 --- a/src/apps/terminal/TermView.cpp +++ b/src/apps/terminal/TermView.cpp @@ -661,6 +661,19 @@ TermView::GetActiveProcessInfo(ActiveProcessInfo& _info) const } +bool +TermView::GetShellInfo(ShellInfo& _info) const +{ + if (fShell == NULL) { + _info = ShellInfo(); + return false; + } + + _info = fShell->Info(); + return true; +} + + /* static */ BArchivable * TermView::Instantiate(BMessage* data) diff --git a/src/apps/terminal/TermView.h b/src/apps/terminal/TermView.h index f9dc6228a7..182ecfb2fb 100644 --- a/src/apps/terminal/TermView.h +++ b/src/apps/terminal/TermView.h @@ -31,6 +31,7 @@ class BStringView; class BasicTerminalBuffer; class InlineInput; class ResizeWindow; +class ShellInfo; class ShellParameters; class TermBuffer; class TerminalBuffer; @@ -58,6 +59,7 @@ public: bool IsShellBusy() const; bool GetActiveProcessInfo( ActiveProcessInfo& _info) const; + bool GetShellInfo(ShellInfo& _info) const; const char* TerminalName() const; diff --git a/src/apps/terminal/TermWindow.cpp b/src/apps/terminal/TermWindow.cpp index 1469ef1d2d..20b3d1673d 100644 --- a/src/apps/terminal/TermWindow.cpp +++ b/src/apps/terminal/TermWindow.cpp @@ -302,17 +302,25 @@ TermWindow::_CanClose(int32 index) // all names, separated by "\n\t" if (index != -1) { + ShellInfo shellInfo; ActiveProcessInfo info; - if (_TermViewAt(index)->GetActiveProcessInfo(info) - && info.ID() != info.ShellProcessID()) { + TermView* termView = _TermViewAt(index); + if (termView->GetShellInfo(shellInfo) + && termView->GetActiveProcessInfo(info) + && (info.ID() != shellInfo.ProcessID() + || !shellInfo.IsDefaultShell())) { busyProcessCount++; busyProcessNames = info.Name(); } } else { for (int32 i = 0; i < fSessions.CountItems(); i++) { + ShellInfo shellInfo; ActiveProcessInfo info; - if (_TermViewAt(i)->GetActiveProcessInfo(info) - && info.ID() != info.ShellProcessID()) { + TermView* termView = _TermViewAt(i); + if (termView->GetShellInfo(shellInfo) + && termView->GetActiveProcessInfo(info) + && (info.ID() != shellInfo.ProcessID() + || !shellInfo.IsDefaultShell())) { if (++busyProcessCount > 1) busyProcessNames << "\n\t"; busyProcessNames << info.Name(); @@ -1576,15 +1584,20 @@ TermWindow::_UpdateSessionTitle(int32 index) if (session == NULL) return; - // get the active process info + // get the shell and active process infos + ShellInfo shellInfo; ActiveProcessInfo activeProcessInfo; - if (!_TermViewAt(index)->GetActiveProcessInfo(activeProcessInfo)) + TermView* termView = _TermViewAt(index); + if (!termView->GetShellInfo(shellInfo) + || !termView->GetActiveProcessInfo(activeProcessInfo)) { return; + } // evaluate the session title pattern BString sessionTitlePattern = session->title.patternUserDefined ? session->title.pattern : fSessionTitlePattern; - TabTitlePlaceholderMapper tabMapper(activeProcessInfo, session->index); + TabTitlePlaceholderMapper tabMapper(shellInfo, activeProcessInfo, + session->index); const BString& sessionTitle = PatternEvaluator::Evaluate( sessionTitlePattern, tabMapper); @@ -1602,7 +1615,7 @@ TermWindow::_UpdateSessionTitle(int32 index) return; // evaluate the window title pattern - WindowTitlePlaceholderMapper windowMapper(activeProcessInfo, + WindowTitlePlaceholderMapper windowMapper(shellInfo, activeProcessInfo, fTerminalRoster.ID() + 1, sessionTitle); const BString& windowTitle = PatternEvaluator::Evaluate(fTitle.pattern, windowMapper); diff --git a/src/apps/terminal/TitlePlaceholderMapper.cpp b/src/apps/terminal/TitlePlaceholderMapper.cpp index be73227b8f..304270ba76 100644 --- a/src/apps/terminal/TitlePlaceholderMapper.cpp +++ b/src/apps/terminal/TitlePlaceholderMapper.cpp @@ -10,9 +10,10 @@ // #pragma mark - TitlePlaceholderMapper -TitlePlaceholderMapper::TitlePlaceholderMapper( +TitlePlaceholderMapper::TitlePlaceholderMapper(const ShellInfo& shellInfo, const ActiveProcessInfo& processInfo) : + fShellInfo(shellInfo), fProcessInfo(processInfo) { } @@ -46,10 +47,12 @@ TitlePlaceholderMapper::MapPlaceholder(char placeholder, int64 number, } case 'p': - // process name -- use "--", if the shell is active - if (fProcessInfo.ID() == fProcessInfo.ShellProcessID()) + // process name -- use "--", if the shell is active and it is the + // default shell + if (fProcessInfo.ID() == fShellInfo.ProcessID() + && fShellInfo.IsDefaultShell()) { _string = "--"; - else + } else _string = fProcessInfo.Name(); return true; } @@ -62,10 +65,10 @@ TitlePlaceholderMapper::MapPlaceholder(char placeholder, int64 number, WindowTitlePlaceholderMapper::WindowTitlePlaceholderMapper( - const ActiveProcessInfo& processInfo, int32 windowIndex, - const BString& tabTitle) + const ShellInfo& shellInfo, const ActiveProcessInfo& processInfo, + int32 windowIndex, const BString& tabTitle) : - TitlePlaceholderMapper(processInfo), + TitlePlaceholderMapper(shellInfo, processInfo), fWindowIndex(windowIndex), fTabTitle(tabTitle) { @@ -97,10 +100,10 @@ WindowTitlePlaceholderMapper::MapPlaceholder(char placeholder, int64 number, // #pragma mark - TabTitlePlaceholderMapper -TabTitlePlaceholderMapper::TabTitlePlaceholderMapper( +TabTitlePlaceholderMapper::TabTitlePlaceholderMapper(const ShellInfo& shellInfo, const ActiveProcessInfo& processInfo, int32 tabIndex) : - TitlePlaceholderMapper(processInfo), + TitlePlaceholderMapper(shellInfo, processInfo), fTabIndex(tabIndex) { } diff --git a/src/apps/terminal/TitlePlaceholderMapper.h b/src/apps/terminal/TitlePlaceholderMapper.h index a341c69822..0f4327e2d9 100644 --- a/src/apps/terminal/TitlePlaceholderMapper.h +++ b/src/apps/terminal/TitlePlaceholderMapper.h @@ -8,6 +8,7 @@ #include "ActiveProcessInfo.h" #include "PatternEvaluator.h" +#include "ShellInfo.h" /*! Class mapping the placeholders common for window and tab titles. @@ -15,6 +16,7 @@ class TitlePlaceholderMapper : public PatternEvaluator::PlaceholderMapper { public: TitlePlaceholderMapper( + const ShellInfo& shellInfo, const ActiveProcessInfo& processInfo); virtual bool MapPlaceholder(char placeholder, @@ -22,6 +24,7 @@ public: BString& _string); private: + ShellInfo fShellInfo; ActiveProcessInfo fProcessInfo; }; @@ -29,6 +32,7 @@ private: class WindowTitlePlaceholderMapper : public TitlePlaceholderMapper { public: WindowTitlePlaceholderMapper( + const ShellInfo& shellInfo, const ActiveProcessInfo& processInfo, int32 windowIndex, const BString& tabTitle); @@ -45,6 +49,7 @@ private: class TabTitlePlaceholderMapper : public TitlePlaceholderMapper { public: TabTitlePlaceholderMapper( + const ShellInfo& shellInfo, const ActiveProcessInfo& processInfo, int32 tabIndex);