174158bb4SFrançois Revol/*
26f6ba875SAxel Dörfler * Copyright 2008, Fran��ois Revol, revol@free.fr. All rights reserved.
374158bb4SFrançois Revol * Distributed under the terms of the MIT License.
474158bb4SFrançois Revol */
574158bb4SFrançois Revol
674158bb4SFrançois Revol
774158bb4SFrançois Revol#include "SystemInfoHandler.h"
874158bb4SFrançois Revol
96f6ba875SAxel Dörfler#include <stdlib.h>
106f6ba875SAxel Dörfler#include <string.h>
116f6ba875SAxel Dörfler#include <unistd.h>
126f6ba875SAxel Dörfler
13e034d18fSFrançois Revol#include <Clipboard.h>
1474158bb4SFrançois Revol#include <Handler.h>
15dc1ffc79SFrançois Revol#include <Input.h>
1674158bb4SFrançois Revol#include <List.h>
17dc1ffc79SFrançois Revol#include <MediaRoster.h>
1874158bb4SFrançois Revol#include <Messenger.h>
1974158bb4SFrançois Revol#include <Roster.h>
2074158bb4SFrançois Revol
2174158bb4SFrançois Revol
2274158bb4SFrançois RevolSystemInfoHandler::SystemInfoHandler()
2374158bb4SFrançois Revol	: BHandler("SystemInfoHandler")
2474158bb4SFrançois Revol{
25dc1ffc79SFrançois Revol	fRunningApps = 0;
26dc1ffc79SFrançois Revol	fClipboardSize = 0;
27dc1ffc79SFrançois Revol	fClipboardTextSize = 0;
28dc1ffc79SFrançois Revol	fMediaNodes = 0;
29dc1ffc79SFrançois Revol	fMediaConnections = 0;
30dc1ffc79SFrançois Revol	fMediaBuffers = 0;
3174158bb4SFrançois Revol}
3274158bb4SFrançois Revol
3374158bb4SFrançois Revol
3474158bb4SFrançois RevolSystemInfoHandler::~SystemInfoHandler()
3574158bb4SFrançois Revol{
3674158bb4SFrançois Revol}
3774158bb4SFrançois Revol
3874158bb4SFrançois Revol
3974158bb4SFrançois Revolstatus_t
406f6ba875SAxel DörflerSystemInfoHandler::Archive(BMessage* data, bool deep) const
4174158bb4SFrançois Revol{
4274158bb4SFrançois Revol	// we don't want ourselves to be archived at all...
4374158bb4SFrançois Revol	// return BHandler::Archive(data, deep);
4474158bb4SFrançois Revol	return B_OK;
4574158bb4SFrançois Revol}
4674158bb4SFrançois Revol
4774158bb4SFrançois Revol
4874158bb4SFrançois Revolvoid
496f6ba875SAxel DörflerSystemInfoHandler::StartWatching()
5074158bb4SFrançois Revol{
51dc1ffc79SFrançois Revol	status_t status;
5274158bb4SFrançois Revol	fRunningApps = 0;
53e034d18fSFrançois Revol	fClipboardSize = 0;
54e034d18fSFrançois Revol	fClipboardTextSize = 0;
55dc1ffc79SFrançois Revol	fMediaNodes = 0;
56dc1ffc79SFrançois Revol	fMediaConnections = 0;
57dc1ffc79SFrançois Revol	fMediaBuffers = 0;
586f6ba875SAxel Dörfler
5974158bb4SFrançois Revol	// running applications count
6074158bb4SFrançois Revol	BList teamList;
61e034d18fSFrançois Revol	if (be_roster) {
626f6ba875SAxel Dörfler		be_roster->StartWatching(BMessenger(this),
63e034d18fSFrançois Revol			B_REQUEST_LAUNCHED | B_REQUEST_QUIT);
64e034d18fSFrançois Revol		be_roster->GetAppList(&teamList);
65e034d18fSFrançois Revol		fRunningApps = teamList.CountItems();
66e034d18fSFrançois Revol		teamList.MakeEmpty();
67e034d18fSFrançois Revol	}
6874158bb4SFrançois Revol
69e034d18fSFrançois Revol	// useless clipboard size
70e034d18fSFrançois Revol	if (be_clipboard) {
71e034d18fSFrançois Revol		be_clipboard->StartWatching(BMessenger(this));
72e034d18fSFrançois Revol		_UpdateClipboardData();
73e034d18fSFrançois Revol	}
74dc1ffc79SFrançois Revol
75dc1ffc79SFrançois Revol	if (BMediaRoster::Roster(&status) && (status >= B_OK)) {
76dc1ffc79SFrançois Revol		BMediaRoster::Roster()->StartWatching(BMessenger(this), B_MEDIA_NODE_CREATED);
77dc1ffc79SFrançois Revol		BMediaRoster::Roster()->StartWatching(BMessenger(this), B_MEDIA_NODE_DELETED);
78dc1ffc79SFrançois Revol		BMediaRoster::Roster()->StartWatching(BMessenger(this), B_MEDIA_CONNECTION_MADE);
79dc1ffc79SFrançois Revol		BMediaRoster::Roster()->StartWatching(BMessenger(this), B_MEDIA_CONNECTION_BROKEN);
80dc1ffc79SFrançois Revol		BMediaRoster::Roster()->StartWatching(BMessenger(this), B_MEDIA_BUFFER_CREATED);
81dc1ffc79SFrançois Revol		BMediaRoster::Roster()->StartWatching(BMessenger(this), B_MEDIA_BUFFER_DELETED);
82dc1ffc79SFrançois Revol		// XXX: this won't survive a media_server restart...
83dc1ffc79SFrançois Revol
84dc1ffc79SFrançois Revol		live_node_info nodeInfo; // I just need one
85dc1ffc79SFrançois Revol		int32 nodeCount = 1;
86dc1ffc79SFrançois Revol		if (BMediaRoster::Roster()->GetLiveNodes(&nodeInfo, &nodeCount)) {
87dc1ffc79SFrançois Revol			if (nodeCount > 0)
88dc1ffc79SFrançois Revol				fMediaNodes = (uint32)nodeCount;
89dc1ffc79SFrançois Revol			// TODO: retry with an array, and use GetNodeInput/Output
90dc1ffc79SFrançois Revol			// to find initial connection count
91dc1ffc79SFrançois Revol		}
92dc1ffc79SFrançois Revol		// TODO: get initial buffer count
93dc1ffc79SFrançois Revol
94dc1ffc79SFrançois Revol	}
95dc1ffc79SFrançois Revol
96dc1ffc79SFrançois Revol	// doesn't work on R5
97dc1ffc79SFrançois Revol	watch_input_devices(BMessenger(this), true);
9874158bb4SFrançois Revol}
9974158bb4SFrançois Revol
10074158bb4SFrançois Revol
10174158bb4SFrançois Revolvoid
1026f6ba875SAxel DörflerSystemInfoHandler::StopWatching()
10374158bb4SFrançois Revol{
104dc1ffc79SFrançois Revol	status_t status;
105dc1ffc79SFrançois Revol	watch_input_devices(BMessenger(this), false);
106dc1ffc79SFrançois Revol	if (BMediaRoster::Roster(&status) && (status >= B_OK)) {
107dc1ffc79SFrançois Revol		BMediaRoster::Roster()->StopWatching(BMessenger(this), B_MEDIA_NODE_CREATED);
108dc1ffc79SFrançois Revol		BMediaRoster::Roster()->StopWatching(BMessenger(this), B_MEDIA_NODE_DELETED);
109dc1ffc79SFrançois Revol		BMediaRoster::Roster()->StopWatching(BMessenger(this), B_MEDIA_CONNECTION_MADE);
110dc1ffc79SFrançois Revol		BMediaRoster::Roster()->StopWatching(BMessenger(this), B_MEDIA_CONNECTION_BROKEN);
111dc1ffc79SFrançois Revol		BMediaRoster::Roster()->StopWatching(BMessenger(this), B_MEDIA_BUFFER_CREATED);
112dc1ffc79SFrançois Revol		BMediaRoster::Roster()->StopWatching(BMessenger(this), B_MEDIA_BUFFER_DELETED);
113dc1ffc79SFrançois Revol	}
114e034d18fSFrançois Revol	if (be_clipboard)
115e034d18fSFrançois Revol		be_clipboard->StopWatching(BMessenger(this));
116dc1ffc79SFrançois Revol	if (be_roster)
117dc1ffc79SFrançois Revol		be_roster->StopWatching(BMessenger(this));
11874158bb4SFrançois Revol}
11974158bb4SFrançois Revol
12074158bb4SFrançois Revol
12174158bb4SFrançois Revolvoid
1226f6ba875SAxel DörflerSystemInfoHandler::MessageReceived(BMessage* message)
12374158bb4SFrançois Revol{
12474158bb4SFrançois Revol	switch (message->what) {
1256f6ba875SAxel Dörfler		case B_SOME_APP_LAUNCHED:
1266f6ba875SAxel Dörfler			fRunningApps++;
1276f6ba875SAxel Dörfler			// TODO: maybe resync periodically in case we miss one
1286f6ba875SAxel Dörfler			break;
1296f6ba875SAxel Dörfler		case B_SOME_APP_QUIT:
1306f6ba875SAxel Dörfler			fRunningApps--;
1316f6ba875SAxel Dörfler			// TODO: maybe resync periodically in case we miss one
1326f6ba875SAxel Dörfler			break;
1336f6ba875SAxel Dörfler		case B_CLIPBOARD_CHANGED:
1346f6ba875SAxel Dörfler			_UpdateClipboardData();
1356f6ba875SAxel Dörfler			break;
136dc1ffc79SFrançois Revol		case B_MEDIA_NODE_CREATED:
137dc1ffc79SFrançois Revol			fMediaNodes++;
138dc1ffc79SFrançois Revol			break;
139dc1ffc79SFrançois Revol		case B_MEDIA_NODE_DELETED:
140dc1ffc79SFrançois Revol			fMediaNodes--;
141dc1ffc79SFrançois Revol			break;
142dc1ffc79SFrançois Revol		case B_MEDIA_CONNECTION_MADE:
143dc1ffc79SFrançois Revol			fMediaConnections++;
144dc1ffc79SFrançois Revol			break;
145dc1ffc79SFrançois Revol		case B_MEDIA_CONNECTION_BROKEN:
146dc1ffc79SFrançois Revol			fMediaConnections--;
147dc1ffc79SFrançois Revol			break;
148dc1ffc79SFrançois Revol		case B_MEDIA_BUFFER_CREATED:
149dc1ffc79SFrançois Revol			fMediaBuffers++;
150dc1ffc79SFrançois Revol			break;
151dc1ffc79SFrançois Revol		case B_MEDIA_BUFFER_DELETED:
152dc1ffc79SFrançois Revol			fMediaBuffers--;
153dc1ffc79SFrançois Revol			break;
1546f6ba875SAxel Dörfler		default:
155dc1ffc79SFrançois Revol			message->PrintToStream();
1566f6ba875SAxel Dörfler			BHandler::MessageReceived(message);
15774158bb4SFrançois Revol	}
15874158bb4SFrançois Revol}
15974158bb4SFrançois Revol
16074158bb4SFrançois Revol
16174158bb4SFrançois Revoluint32
16274158bb4SFrançois RevolSystemInfoHandler::RunningApps() const
16374158bb4SFrançois Revol{
16474158bb4SFrançois Revol	return fRunningApps;
16574158bb4SFrançois Revol}
16674158bb4SFrançois Revol
167e034d18fSFrançois Revol
168e034d18fSFrançois Revoluint32
169e034d18fSFrançois RevolSystemInfoHandler::ClipboardSize() const
170e034d18fSFrançois Revol{
171e034d18fSFrançois Revol	return fClipboardSize;
172e034d18fSFrançois Revol}
173e034d18fSFrançois Revol
174e034d18fSFrançois Revol
175e034d18fSFrançois Revoluint32
176e034d18fSFrançois RevolSystemInfoHandler::ClipboardTextSize() const
177e034d18fSFrançois Revol{
178e034d18fSFrançois Revol	return fClipboardTextSize;
179e034d18fSFrançois Revol}
180e034d18fSFrançois Revol
181e034d18fSFrançois Revol
182dc1ffc79SFrançois Revoluint32
183dc1ffc79SFrançois RevolSystemInfoHandler::MediaNodes() const
184dc1ffc79SFrançois Revol{
185dc1ffc79SFrançois Revol	return fMediaNodes;
186dc1ffc79SFrançois Revol}
187dc1ffc79SFrançois Revol
188dc1ffc79SFrançois Revol
189dc1ffc79SFrançois Revoluint32
190dc1ffc79SFrançois RevolSystemInfoHandler::MediaConnections() const
191dc1ffc79SFrançois Revol{
192dc1ffc79SFrançois Revol	return fMediaConnections;
193dc1ffc79SFrançois Revol}
194dc1ffc79SFrançois Revol
195dc1ffc79SFrançois Revol
196dc1ffc79SFrançois Revoluint32
197dc1ffc79SFrançois RevolSystemInfoHandler::MediaBuffers() const
198dc1ffc79SFrançois Revol{
199dc1ffc79SFrançois Revol	return fMediaBuffers;
200dc1ffc79SFrançois Revol}
201dc1ffc79SFrançois Revol
202dc1ffc79SFrançois Revol
203e034d18fSFrançois Revolvoid
204e034d18fSFrançois RevolSystemInfoHandler::_UpdateClipboardData()
205e034d18fSFrançois Revol{
206e034d18fSFrançois Revol	fClipboardSize = 0;
207e034d18fSFrançois Revol	fClipboardTextSize = 0;
2086f6ba875SAxel Dörfler
2096f6ba875SAxel Dörfler	if (be_clipboard == NULL || !be_clipboard->Lock())
2106f6ba875SAxel Dörfler		return;
2116f6ba875SAxel Dörfler
2126f6ba875SAxel Dörfler	BMessage* data = be_clipboard->Data();
2136f6ba875SAxel Dörfler	if (data) {
2146f6ba875SAxel Dörfler		ssize_t size = data->FlattenedSize();
2156f6ba875SAxel Dörfler		fClipboardSize = size < 0 ? 0 : (uint32)size;
2166f6ba875SAxel Dörfler
2176f6ba875SAxel Dörfler		const void* text;
2186f6ba875SAxel Dörfler		ssize_t textSize;
2196f6ba875SAxel Dörfler		if (data->FindData("text/plain", B_MIME_TYPE, &text, &textSize) >= B_OK)
2206f6ba875SAxel Dörfler			fClipboardTextSize = textSize;
221e034d18fSFrançois Revol	}
2226f6ba875SAxel Dörfler
2236f6ba875SAxel Dörfler	be_clipboard->Unlock();
224e034d18fSFrançois Revol}
225e034d18fSFrançois Revol
226