- 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
This commit is contained in:
François Revol 2008-04-17 22:04:18 +00:00
parent 03aaa278a5
commit 74158bb474
9 changed files with 220 additions and 4 deletions

View File

@ -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!

View File

@ -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

View File

@ -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),

View File

@ -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);

View File

@ -11,6 +11,7 @@ Application ActivityMonitor :
ActivityWindow.cpp
DataSource.cpp
SystemInfo.cpp
SystemInfoHandler.cpp
: be tracker $(TARGET_LIBSTDC++) $(TARGET_NETWORK_LIBS)
: ActivityMonitor.rdef

View File

@ -5,6 +5,7 @@
#include "SystemInfo.h"
#include "SystemInfoHandler.h"
#include <net/if.h>
#include <stdlib.h>
@ -14,12 +15,17 @@
#include <sys/sockio.h>
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()
{

View File

@ -8,10 +8,12 @@
#include <OS.h>
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

View File

@ -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 <Handler.h>
#include <List.h>
#include <Messenger.h>
#include <Roster.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
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;
}

View File

@ -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 <OS.h>
#include <Handler.h>
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