2 * Copyright 2008, François Revol, revol@free.fr. All rights reserved.
3 * Distributed under the terms of the MIT License.
7 #include "SystemInfoHandler.h"
13 #include <Clipboard.h>
17 #include <MediaRoster.h>
18 #include <Messenger.h>
22 SystemInfoHandler::SystemInfoHandler()
23 : BHandler("SystemInfoHandler")
27 fClipboardTextSize
= 0;
29 fMediaConnections
= 0;
34 SystemInfoHandler::~SystemInfoHandler()
40 SystemInfoHandler::Archive(BMessage
* data
, bool deep
) const
42 // we don't want ourselves to be archived at all...
43 // return BHandler::Archive(data, deep);
49 SystemInfoHandler::StartWatching()
54 fClipboardTextSize
= 0;
56 fMediaConnections
= 0;
59 // running applications count
62 be_roster
->StartWatching(BMessenger(this),
63 B_REQUEST_LAUNCHED
| B_REQUEST_QUIT
);
64 be_roster
->GetAppList(&teamList
);
65 fRunningApps
= teamList
.CountItems();
69 // useless clipboard size
71 be_clipboard
->StartWatching(BMessenger(this));
72 _UpdateClipboardData();
75 if (BMediaRoster::Roster(&status
) && (status
>= B_OK
)) {
76 BMediaRoster::Roster()->StartWatching(BMessenger(this), B_MEDIA_NODE_CREATED
);
77 BMediaRoster::Roster()->StartWatching(BMessenger(this), B_MEDIA_NODE_DELETED
);
78 BMediaRoster::Roster()->StartWatching(BMessenger(this), B_MEDIA_CONNECTION_MADE
);
79 BMediaRoster::Roster()->StartWatching(BMessenger(this), B_MEDIA_CONNECTION_BROKEN
);
80 BMediaRoster::Roster()->StartWatching(BMessenger(this), B_MEDIA_BUFFER_CREATED
);
81 BMediaRoster::Roster()->StartWatching(BMessenger(this), B_MEDIA_BUFFER_DELETED
);
82 // XXX: this won't survive a media_server restart...
84 live_node_info nodeInfo
; // I just need one
86 if (BMediaRoster::Roster()->GetLiveNodes(&nodeInfo
, &nodeCount
)) {
88 fMediaNodes
= (uint32
)nodeCount
;
89 // TODO: retry with an array, and use GetNodeInput/Output
90 // to find initial connection count
92 // TODO: get initial buffer count
97 watch_input_devices(BMessenger(this), true);
102 SystemInfoHandler::StopWatching()
105 watch_input_devices(BMessenger(this), false);
106 if (BMediaRoster::Roster(&status
) && (status
>= B_OK
)) {
107 BMediaRoster::Roster()->StopWatching(BMessenger(this), B_MEDIA_NODE_CREATED
);
108 BMediaRoster::Roster()->StopWatching(BMessenger(this), B_MEDIA_NODE_DELETED
);
109 BMediaRoster::Roster()->StopWatching(BMessenger(this), B_MEDIA_CONNECTION_MADE
);
110 BMediaRoster::Roster()->StopWatching(BMessenger(this), B_MEDIA_CONNECTION_BROKEN
);
111 BMediaRoster::Roster()->StopWatching(BMessenger(this), B_MEDIA_BUFFER_CREATED
);
112 BMediaRoster::Roster()->StopWatching(BMessenger(this), B_MEDIA_BUFFER_DELETED
);
115 be_clipboard
->StopWatching(BMessenger(this));
117 be_roster
->StopWatching(BMessenger(this));
122 SystemInfoHandler::MessageReceived(BMessage
* message
)
124 switch (message
->what
) {
125 case B_SOME_APP_LAUNCHED
:
127 // TODO: maybe resync periodically in case we miss one
129 case B_SOME_APP_QUIT
:
131 // TODO: maybe resync periodically in case we miss one
133 case B_CLIPBOARD_CHANGED
:
134 _UpdateClipboardData();
136 case B_MEDIA_NODE_CREATED
:
139 case B_MEDIA_NODE_DELETED
:
142 case B_MEDIA_CONNECTION_MADE
:
145 case B_MEDIA_CONNECTION_BROKEN
:
148 case B_MEDIA_BUFFER_CREATED
:
151 case B_MEDIA_BUFFER_DELETED
:
155 message
->PrintToStream();
156 BHandler::MessageReceived(message
);
162 SystemInfoHandler::RunningApps() const
169 SystemInfoHandler::ClipboardSize() const
171 return fClipboardSize
;
176 SystemInfoHandler::ClipboardTextSize() const
178 return fClipboardTextSize
;
183 SystemInfoHandler::MediaNodes() const
190 SystemInfoHandler::MediaConnections() const
192 return fMediaConnections
;
197 SystemInfoHandler::MediaBuffers() const
199 return fMediaBuffers
;
204 SystemInfoHandler::_UpdateClipboardData()
207 fClipboardTextSize
= 0;
209 if (be_clipboard
== NULL
|| !be_clipboard
->Lock())
212 BMessage
* data
= be_clipboard
->Data();
214 ssize_t size
= data
->FlattenedSize();
215 fClipboardSize
= size
< 0 ? 0 : (uint32
)size
;
219 if (data
->FindData("text/plain", B_MIME_TYPE
, &text
, &textSize
) >= B_OK
)
220 fClipboardTextSize
= textSize
;
223 be_clipboard
->Unlock();