From 74158bb474dca9b4236178b7c48224979244c142 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Revol?= Date: Thu, 17 Apr 2008 22:04:18 +0000 Subject: [PATCH] - introduced a SystemInfoHandler class used to watch for stuff when polling isn't needed. - used it to add a count of running apps (not exactly the same as teams). git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@25012 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/apps/activitymonitor/ActivityView.cpp | 10 ++- src/apps/activitymonitor/ActivityView.h | 2 + src/apps/activitymonitor/DataSource.cpp | 48 +++++++++++ src/apps/activitymonitor/DataSource.h | 13 +++ src/apps/activitymonitor/Jamfile | 1 + src/apps/activitymonitor/SystemInfo.cpp | 24 +++++- src/apps/activitymonitor/SystemInfo.h | 8 +- .../activitymonitor/SystemInfoHandler.cpp | 85 +++++++++++++++++++ src/apps/activitymonitor/SystemInfoHandler.h | 33 +++++++ 9 files changed, 220 insertions(+), 4 deletions(-) create mode 100644 src/apps/activitymonitor/SystemInfoHandler.cpp create mode 100644 src/apps/activitymonitor/SystemInfoHandler.h diff --git a/src/apps/activitymonitor/ActivityView.cpp b/src/apps/activitymonitor/ActivityView.cpp index 73da319667..bf59a05199 100644 --- a/src/apps/activitymonitor/ActivityView.cpp +++ b/src/apps/activitymonitor/ActivityView.cpp @@ -21,6 +21,7 @@ #include "ActivityMonitor.h" #include "DataSource.h" #include "SystemInfo.h" +#include "SystemInfoHandler.h" struct data_item { @@ -167,6 +168,7 @@ ActivityView::ActivityView(BMessage* archive) ActivityView::~ActivityView() { delete fOffscreen; + delete fSystemInfoHandler; } @@ -182,6 +184,8 @@ ActivityView::_Init(const BMessage* settings) fDrawInterval = kInitialRefreshInterval * 2; fLastRefresh = 0; fDrawResolution = 1; + + fSystemInfoHandler = new SystemInfoHandler; if (settings == NULL || settings->FindBool("show legend", &fShowLegend) != B_OK) @@ -349,6 +353,8 @@ ActivityView::RemoveAllDataSources() void ActivityView::AttachedToWindow() { + Looper()->AddHandler(fSystemInfoHandler); + fSystemInfoHandler->StartWatchingStuff(); BMessage refresh(kMsgRefresh); fRunner = new BMessageRunner(this, &refresh, fRefreshInterval); @@ -359,6 +365,8 @@ ActivityView::AttachedToWindow() void ActivityView::DetachedFromWindow() { + fSystemInfoHandler->StopWatchingStuff(); + Looper()->RemoveHandler(fSystemInfoHandler); delete fRunner; } @@ -738,7 +746,7 @@ ActivityView::Draw(BRect /*updateRect*/) void ActivityView::_Refresh() { - SystemInfo info; + SystemInfo info(fSystemInfoHandler); // TODO: run refresh in another thread to decouple it from the UI! diff --git a/src/apps/activitymonitor/ActivityView.h b/src/apps/activitymonitor/ActivityView.h index 5f4b11a877..5ee2aa76cb 100644 --- a/src/apps/activitymonitor/ActivityView.h +++ b/src/apps/activitymonitor/ActivityView.h @@ -14,6 +14,7 @@ class BBitmap; class BMessageRunner; class DataSource; +class SystemInfoHandler; struct data_item; @@ -98,6 +99,7 @@ private: bigtime_t fDrawInterval; int32 fDrawResolution; bool fShowLegend; + SystemInfoHandler* fSystemInfoHandler; }; #endif // ACTIVITY_VIEW_H diff --git a/src/apps/activitymonitor/DataSource.cpp b/src/apps/activitymonitor/DataSource.cpp index 01092576d4..a32d6a2fd8 100644 --- a/src/apps/activitymonitor/DataSource.cpp +++ b/src/apps/activitymonitor/DataSource.cpp @@ -21,6 +21,7 @@ const DataSource* kSources[] = { new PortsDataSource(), new ThreadsDataSource(), new TeamsDataSource(), + new RunningAppsDataSource(), new CPUUsageDataSource(), new CPUCombinedUsageDataSource(), new NetworkUsageDataSource(true), @@ -530,6 +531,53 @@ TeamsDataSource::AdaptiveScale() const // #pragma mark - +RunningAppsDataSource::RunningAppsDataSource() +{ + SystemInfo info; + + fMinimum = 0; + fMaximum = info.MaxRunningApps(); + + fColor = (rgb_color){100, 150, 255}; +} + + +RunningAppsDataSource::~RunningAppsDataSource() +{ +} + + +DataSource* +RunningAppsDataSource::Copy() const +{ + return new RunningAppsDataSource(*this); +} + + +int64 +RunningAppsDataSource::NextValue(SystemInfo& info) +{ + return info.UsedRunningApps(); +} + + +const char* +RunningAppsDataSource::Label() const +{ + return "Running Applications"; +} + + +bool +RunningAppsDataSource::AdaptiveScale() const +{ + return true; +} + + +// #pragma mark - + + CPUUsageDataSource::CPUUsageDataSource(int32 cpu) : fPreviousActive(0), diff --git a/src/apps/activitymonitor/DataSource.h b/src/apps/activitymonitor/DataSource.h index 19271c8cde..9c2ec428b6 100644 --- a/src/apps/activitymonitor/DataSource.h +++ b/src/apps/activitymonitor/DataSource.h @@ -141,6 +141,19 @@ public: }; +class RunningAppsDataSource : public DataSource { +public: + RunningAppsDataSource(); + virtual ~RunningAppsDataSource(); + + virtual DataSource* Copy() const; + + virtual int64 NextValue(SystemInfo& info); + virtual const char* Label() const; + virtual bool AdaptiveScale() const; +}; + + class CPUUsageDataSource : public DataSource { public: CPUUsageDataSource(int32 cpu = 0); diff --git a/src/apps/activitymonitor/Jamfile b/src/apps/activitymonitor/Jamfile index 07832f3def..03162b83bf 100644 --- a/src/apps/activitymonitor/Jamfile +++ b/src/apps/activitymonitor/Jamfile @@ -11,6 +11,7 @@ Application ActivityMonitor : ActivityWindow.cpp DataSource.cpp SystemInfo.cpp + SystemInfoHandler.cpp : be tracker $(TARGET_LIBSTDC++) $(TARGET_NETWORK_LIBS) : ActivityMonitor.rdef diff --git a/src/apps/activitymonitor/SystemInfo.cpp b/src/apps/activitymonitor/SystemInfo.cpp index ff7257ab62..d29747f498 100644 --- a/src/apps/activitymonitor/SystemInfo.cpp +++ b/src/apps/activitymonitor/SystemInfo.cpp @@ -5,6 +5,7 @@ #include "SystemInfo.h" +#include "SystemInfoHandler.h" #include #include @@ -14,12 +15,17 @@ #include -SystemInfo::SystemInfo() +SystemInfo::SystemInfo(SystemInfoHandler *handler) : fTime(system_time()), - fRetrievedNetwork(false) + fRetrievedNetwork(false), + fRunningApps(0) { get_system_info(&fSystemInfo); + + if (handler) { + fRunningApps = handler->RunningApps(); + } } @@ -109,6 +115,20 @@ SystemInfo::MaxTeams() const } +uint32 +SystemInfo::UsedRunningApps() const +{ + return fRunningApps; +} + + +uint32 +SystemInfo::MaxRunningApps() const +{ + return fSystemInfo.max_teams; +} + + void SystemInfo::_RetrieveNetwork() { diff --git a/src/apps/activitymonitor/SystemInfo.h b/src/apps/activitymonitor/SystemInfo.h index a30ae91118..60fb7a2b92 100644 --- a/src/apps/activitymonitor/SystemInfo.h +++ b/src/apps/activitymonitor/SystemInfo.h @@ -8,10 +8,12 @@ #include +class SystemInfoHandler; + class SystemInfo { public: - SystemInfo(); + SystemInfo(SystemInfoHandler *handler=NULL); ~SystemInfo(); uint64 CachedMemory() const; @@ -30,6 +32,9 @@ public: uint32 UsedTeams() const; uint32 MaxTeams() const; + uint32 UsedRunningApps() const; + uint32 MaxRunningApps() const; + bigtime_t Time() const { return fTime; } uint32 CPUCount() const { return fSystemInfo.cpu_count; } const system_info& Info() const { return fSystemInfo; } @@ -45,6 +50,7 @@ private: bool fRetrievedNetwork; uint64 fBytesReceived; uint64 fBytesSent; + uint32 fRunningApps; }; #endif // SYSTEM_INFO_H diff --git a/src/apps/activitymonitor/SystemInfoHandler.cpp b/src/apps/activitymonitor/SystemInfoHandler.cpp new file mode 100644 index 0000000000..0fff496107 --- /dev/null +++ b/src/apps/activitymonitor/SystemInfoHandler.cpp @@ -0,0 +1,85 @@ +/* + * Copyright 2008, Axel Dörfler, axeld@pinc-software.de. All rights reserved. + * Distributed under the terms of the MIT License. + */ + + +#include "SystemInfoHandler.h" + +#include +#include +#include +#include +#include +#include +#include + + +SystemInfoHandler::SystemInfoHandler() + : BHandler("SystemInfoHandler") +{ +} + + +SystemInfoHandler::~SystemInfoHandler() +{ +} + + +status_t +SystemInfoHandler::Archive(BMessage *data, bool deep) const +{ + // we don't want ourselves to be archived at all... + // return BHandler::Archive(data, deep); + return B_OK; +} + + +void +SystemInfoHandler::StartWatchingStuff() +{ + fRunningApps = 0; + + // running applications count + BList teamList; + be_roster->StartWatching(BMessenger(this), + B_REQUEST_LAUNCHED | B_REQUEST_QUIT); + be_roster->GetAppList(&teamList); + fRunningApps = teamList.CountItems(); + teamList.MakeEmpty(); + + // +} + + +void +SystemInfoHandler::StopWatchingStuff() +{ + be_roster->StopWatching(BMessenger(this)); +} + + +void +SystemInfoHandler::MessageReceived(BMessage *message) +{ + switch (message->what) { + case B_SOME_APP_LAUNCHED: + fRunningApps++; + // XXX: maybe resync periodically in case we miss one + break; + case B_SOME_APP_QUIT: + fRunningApps--; + // XXX: maybe resync periodically in case we miss one + break; + default: + BHandler::MessageReceived(message); + } +} + + +uint32 +SystemInfoHandler::RunningApps() const +{ + return fRunningApps; +} + diff --git a/src/apps/activitymonitor/SystemInfoHandler.h b/src/apps/activitymonitor/SystemInfoHandler.h new file mode 100644 index 0000000000..1ab1e1d160 --- /dev/null +++ b/src/apps/activitymonitor/SystemInfoHandler.h @@ -0,0 +1,33 @@ +/* + * Copyright 2008, Axel Dörfler, axeld@pinc-software.de. All rights reserved. + * Distributed under the terms of the MIT License. + */ +#ifndef SYSTEM_INFO_HANDLER_H +#define SYSTEM_INFO_HANDLER_H + + +#include +#include + + +class SystemInfoHandler : public BHandler { +public: + SystemInfoHandler(); + //SystemInfoHandler(BMessage *data); + virtual ~SystemInfoHandler(); + + virtual status_t Archive(BMessage *data, bool deep = true) const; + + void StartWatchingStuff(); + void StopWatchingStuff(); + + void MessageReceived(BMessage *message); + + uint32 RunningApps() const; + +private: + + uint32 fRunningApps; +}; + +#endif // SYSTEM_INFO_HANDLER_H