diff --git a/src/apps/terminal/ActiveProcessInfo.cpp b/src/apps/terminal/ActiveProcessInfo.cpp new file mode 100644 index 0000000000..ba5f4c829d --- /dev/null +++ b/src/apps/terminal/ActiveProcessInfo.cpp @@ -0,0 +1,38 @@ +/* + * Copyright 2010, Ingo Weinhold, ingo_weinhold@gmx.de. + * Distributed under the terms of the MIT License. + */ + + +#include "ActiveProcessInfo.h" + + +ActiveProcessInfo::ActiveProcessInfo() + : + fID(-1), + fShellID(-1), + fName(), + fCurrentDirectory() +{ +} + + +void +ActiveProcessInfo::SetTo(pid_t id, pid_t shellID, const BString& name, + const BString& currentDirectory) +{ + fID = id; + fShellID = shellID; + fName = name; + fCurrentDirectory = currentDirectory; +} + + +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 new file mode 100644 index 0000000000..4217790716 --- /dev/null +++ b/src/apps/terminal/ActiveProcessInfo.h @@ -0,0 +1,40 @@ +/* + * Copyright 2010, Ingo Weinhold, ingo_weinhold@gmx.de. + * Distributed under the terms of the MIT License. + */ +#ifndef ACTIVE_PROCESS_INFO_H +#define ACTIVE_PROCESS_INFO_H + + +#include +#include + + +class ActiveProcessInfo { +public: + ActiveProcessInfo(); + + void SetTo(pid_t id, pid_t shellID, + 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 + { return fCurrentDirectory; } + +private: + pid_t fID; + pid_t fShellID; + BString fName; + BString fCurrentDirectory; +}; + + + +#endif // ACTIVE_PROCESS_INFO_H diff --git a/src/apps/terminal/Jamfile b/src/apps/terminal/Jamfile index 4602587f6f..5ee20c5bff 100644 --- a/src/apps/terminal/Jamfile +++ b/src/apps/terminal/Jamfile @@ -4,7 +4,10 @@ SetSubDirSupportedPlatformsBeOSCompatible ; UseHeaders [ FDirName $(HAIKU_TOP) src kits tracker ] ; +UsePrivateHeaders libroot kernel system ; + Application Terminal : + ActiveProcessInfo.cpp AppearPrefView.cpp Arguments.cpp BasicTerminalBuffer.cpp diff --git a/src/apps/terminal/Shell.cpp b/src/apps/terminal/Shell.cpp index 770889e856..ab6e82b2c1 100644 --- a/src/apps/terminal/Shell.cpp +++ b/src/apps/terminal/Shell.cpp @@ -29,9 +29,17 @@ #include #include -#include +#include #include +#include +#include +#include + +#include +#include + +#include "ActiveProcessInfo.h" #include "TermConst.h" #include "TermParse.h" #include "TerminalBuffer.h" @@ -231,13 +239,6 @@ Shell::FD() const } -pid_t -Shell::ActiveProcessGroup() const -{ - return tcgetpgrp(fFd); -} - - bool Shell::HasActiveProcesses() const { @@ -249,6 +250,45 @@ Shell::HasActiveProcesses() const } +bool +Shell::GetActiveProcessInfo(ActiveProcessInfo& _info) const +{ + _info.Unset(); + + // get the foreground process group + pid_t process = tcgetpgrp(fFd); + if (process < 0) + return false; + + // get more info on the process group leader + KMessage info; + status_t error = get_extended_team_info(process, B_TEAM_INFO_BASIC, info); + if (error != B_OK) + return false; + + // fetch the name and the current directory from the info + const char* name; + int32 cwdDevice; + int64 cwdDirectory; + if (info.FindString("name", &name) != B_OK + || info.FindInt32("cwd device", &cwdDevice) != B_OK + || info.FindInt64("cwd directory", &cwdDirectory) != B_OK) { + return false; + } + + // convert the node ref into a path + entry_ref cwdRef(cwdDevice, cwdDirectory, "."); + BPath cwdPath; + if (cwdPath.SetTo(&cwdRef) != B_OK) + return false; + + // set the result + _info.SetTo(process, fProcessID, name, cwdPath.Path()); + + return true; +} + + status_t Shell::AttachBuffer(TerminalBuffer *buffer) { diff --git a/src/apps/terminal/Shell.h b/src/apps/terminal/Shell.h index 92661d7886..6530a82186 100644 --- a/src/apps/terminal/Shell.h +++ b/src/apps/terminal/Shell.h @@ -17,6 +17,7 @@ #include +class ActiveProcessInfo; // TODO: Maybe merge TermParse and Shell classes ? class TerminalBuffer; class TermParse; @@ -44,8 +45,9 @@ public: int FD() const; pid_t ProcessID() const { return fProcessID; } - pid_t ActiveProcessGroup() const; bool HasActiveProcesses() const; + bool GetActiveProcessInfo( + ActiveProcessInfo& _info) const; virtual status_t AttachBuffer(TerminalBuffer* buffer); virtual void DetachBuffer(); diff --git a/src/apps/terminal/TermView.cpp b/src/apps/terminal/TermView.cpp index 943ed97e15..9b52707d2e 100644 --- a/src/apps/terminal/TermView.cpp +++ b/src/apps/terminal/TermView.cpp @@ -24,6 +24,7 @@ #include #include +#include "ActiveProcessInfo.h" #include #include #include @@ -628,13 +629,6 @@ TermView::~TermView() } -pid_t -TermView::ActiveProcessGroup() const -{ - return fShell != NULL ? fShell->ActiveProcessGroup() : -1; -} - - bool TermView::IsShellBusy() const { @@ -642,6 +636,18 @@ TermView::IsShellBusy() const } +bool +TermView::GetActiveProcessInfo(ActiveProcessInfo& _info) const +{ + if (fShell == NULL) { + _info.Unset(); + return false; + } + + return fShell->GetActiveProcessInfo(_info); +} + + /* static */ BArchivable * TermView::Instantiate(BMessage* data) diff --git a/src/apps/terminal/TermView.h b/src/apps/terminal/TermView.h index b127000b2d..c3ac00076a 100644 --- a/src/apps/terminal/TermView.h +++ b/src/apps/terminal/TermView.h @@ -19,6 +19,7 @@ #include "TermPos.h" +class ActiveProcessInfo; class BClipboard; class BMessageRunner; class BScrollBar; @@ -46,8 +47,9 @@ public: virtual void GetPreferredSize(float* _width, float* _height); - pid_t ActiveProcessGroup() const; bool IsShellBusy() const; + bool GetActiveProcessInfo( + ActiveProcessInfo& _info) const; const char* TerminalName() const;