1ed7a3501SAxel Dörfler /*
2*f6581478SAugustin Cavalier * Copyright 2000, Georges-Edouard Berenger. All rights reserved.
3*f6581478SAugustin Cavalier * Distributed under the terms of the MIT License.
4ed7a3501SAxel Dörfler */
5ed7a3501SAxel Dörfler
6a12827f2SAxel Dörfler
7ed7a3501SAxel Dörfler #include "TeamBarMenu.h"
8ed7a3501SAxel Dörfler #include "ThreadBarMenu.h"
9ed7a3501SAxel Dörfler #include "TeamBarMenuItem.h"
10ed7a3501SAxel Dörfler #include "NoiseBarMenuItem.h"
11a12827f2SAxel Dörfler #include "ProcessController.h"
12a12827f2SAxel Dörfler
13a12827f2SAxel Dörfler #include <Bitmap.h>
14a12827f2SAxel Dörfler #include <Roster.h>
15ed7a3501SAxel Dörfler #include <Window.h>
16ed7a3501SAxel Dörfler
17a12827f2SAxel Dörfler #include <stdlib.h>
18a12827f2SAxel Dörfler
19a12827f2SAxel Dörfler
20ed7a3501SAxel Dörfler #define EXTRA 10
21ed7a3501SAxel Dörfler
22a12827f2SAxel Dörfler
TeamBarMenu(const char * title,info_pack * infos,int32 teamCount)23a12827f2SAxel Dörfler TeamBarMenu::TeamBarMenu(const char* title, info_pack* infos, int32 teamCount)
24a12827f2SAxel Dörfler : BMenu(title),
25a12827f2SAxel Dörfler fTeamCount(teamCount+EXTRA),
26a12827f2SAxel Dörfler fFirstShow(true)
27ed7a3501SAxel Dörfler {
28ed7a3501SAxel Dörfler SetFlags(Flags() | B_PULSE_NEEDED);
29ed7a3501SAxel Dörfler fTeamList = (team_id*)malloc(sizeof(team_id) * fTeamCount);
30ed7a3501SAxel Dörfler int k;
31a12827f2SAxel Dörfler for (k = 0; k < teamCount; k++) {
32a12827f2SAxel Dörfler fTeamList[k] = infos[k].team_info.team;
33a12827f2SAxel Dörfler }
34a12827f2SAxel Dörfler while (k < fTeamCount) {
35ed7a3501SAxel Dörfler fTeamList[k++] = -1;
36a12827f2SAxel Dörfler }
37ed7a3501SAxel Dörfler fRecycleCount = EXTRA;
38ed7a3501SAxel Dörfler fRecycleList = (TRecycleItem*)malloc(sizeof(TRecycleItem) * fRecycleCount);
39ed7a3501SAxel Dörfler SetFont(be_plain_font);
40ed7a3501SAxel Dörfler gCurrentThreadBarMenu = NULL;
41ed7a3501SAxel Dörfler fLastTotalTime = system_time();
42ed7a3501SAxel Dörfler AddItem(new NoiseBarMenuItem());
43ed7a3501SAxel Dörfler }
44ed7a3501SAxel Dörfler
45a12827f2SAxel Dörfler
~TeamBarMenu()46ed7a3501SAxel Dörfler TeamBarMenu::~TeamBarMenu()
47ed7a3501SAxel Dörfler {
48ed7a3501SAxel Dörfler gCurrentThreadBarMenu = NULL;
49ed7a3501SAxel Dörfler free(fTeamList);
50ed7a3501SAxel Dörfler free(fRecycleList);
51ed7a3501SAxel Dörfler }
52ed7a3501SAxel Dörfler
53a12827f2SAxel Dörfler
54a12827f2SAxel Dörfler void
Draw(BRect updateRect)55a12827f2SAxel Dörfler TeamBarMenu::Draw(BRect updateRect)
56ed7a3501SAxel Dörfler {
57ed7a3501SAxel Dörfler BMenu::Draw (updateRect);
58a12827f2SAxel Dörfler if (fFirstShow) {
59ed7a3501SAxel Dörfler Pulse();
60ed7a3501SAxel Dörfler fFirstShow = false;
61ed7a3501SAxel Dörfler }
62ed7a3501SAxel Dörfler }
63ed7a3501SAxel Dörfler
64a12827f2SAxel Dörfler
65a12827f2SAxel Dörfler void
Pulse()66a12827f2SAxel Dörfler TeamBarMenu::Pulse()
67ed7a3501SAxel Dörfler {
68ed7a3501SAxel Dörfler Window()->BeginViewTransaction();
69ed7a3501SAxel Dörfler
70ed7a3501SAxel Dörfler // create the list of items to remove, for their team is gone. Update the old teams.
71ed7a3501SAxel Dörfler int lastRecycle = 0;
72ed7a3501SAxel Dörfler int firstRecycle = 0;
73ed7a3501SAxel Dörfler int k;
74ed7a3501SAxel Dörfler TeamBarMenuItem *item;
75ed7a3501SAxel Dörfler double total = 0;
76ed7a3501SAxel Dörfler for (k = 1; (item = (TeamBarMenuItem*)ItemAt(k)) != NULL; k++) {
77ed7a3501SAxel Dörfler item->BarUpdate();
78ed7a3501SAxel Dörfler if (item->fKernel < 0) {
79ed7a3501SAxel Dörfler if (lastRecycle == fRecycleCount) {
80ed7a3501SAxel Dörfler fRecycleCount += EXTRA;
81a12827f2SAxel Dörfler fRecycleList = (TRecycleItem*)realloc(fRecycleList,
82a12827f2SAxel Dörfler sizeof(TRecycleItem)*fRecycleCount);
83ed7a3501SAxel Dörfler }
84ed7a3501SAxel Dörfler fRecycleList[lastRecycle].index = k;
85ed7a3501SAxel Dörfler fRecycleList[lastRecycle++].item = item;
86ed7a3501SAxel Dörfler } else {
87ed7a3501SAxel Dörfler if (lastRecycle > 0) {
88ed7a3501SAxel Dörfler RemoveItems(fRecycleList[0].index, lastRecycle, true);
89ed7a3501SAxel Dörfler k -= lastRecycle;
90ed7a3501SAxel Dörfler lastRecycle = 0;
91ed7a3501SAxel Dörfler }
92ed7a3501SAxel Dörfler total += item->fUser + item->fKernel;
93ed7a3501SAxel Dörfler }
94ed7a3501SAxel Dörfler }
95ed7a3501SAxel Dörfler
96ed7a3501SAxel Dörfler // Look new teams that have appeared. Create an item for them, or recycle from the list.
97ed7a3501SAxel Dörfler int32 cookie = 0;
98a12827f2SAxel Dörfler info_pack infos;
99ed7a3501SAxel Dörfler item = NULL;
100a12827f2SAxel Dörfler while (get_next_team_info(&cookie, &infos.team_info) == B_OK) {
101ed7a3501SAxel Dörfler int j = 0;
102a12827f2SAxel Dörfler while (j < fTeamCount && infos.team_info.team != fTeamList[j])
103ed7a3501SAxel Dörfler j++;
104a12827f2SAxel Dörfler if (infos.team_info.team != fTeamList[j]) {
105ed7a3501SAxel Dörfler // new team
106ed7a3501SAxel Dörfler team_info info;
107ed7a3501SAxel Dörfler j = 0;
108ed7a3501SAxel Dörfler while (j < fTeamCount && fTeamList[j] != -1)
109ed7a3501SAxel Dörfler if (get_team_info(fTeamList[j], &info) != B_OK)
110ed7a3501SAxel Dörfler fTeamList[j] = -1;
111ed7a3501SAxel Dörfler else
112ed7a3501SAxel Dörfler j++;
113ed7a3501SAxel Dörfler if (j == fTeamCount) {
114ed7a3501SAxel Dörfler fTeamCount += 10;
115ed7a3501SAxel Dörfler fTeamList = (team_id*)realloc(fTeamList, sizeof(team_id) * fTeamCount);
116ed7a3501SAxel Dörfler }
117a12827f2SAxel Dörfler fTeamList[j] = infos.team_info.team;
118ed7a3501SAxel Dörfler if (!get_team_name_and_icon(infos, true)) {
119ed7a3501SAxel Dörfler // the team is already gone!
120ed7a3501SAxel Dörfler fTeamList[j] = -1;
121ed7a3501SAxel Dörfler } else {
122ed7a3501SAxel Dörfler if (!item && firstRecycle < lastRecycle) {
123ed7a3501SAxel Dörfler item = fRecycleList[firstRecycle++].item;
124ed7a3501SAxel Dörfler }
125ed7a3501SAxel Dörfler if (item) {
126a12827f2SAxel Dörfler item->Reset(infos.team_name, infos.team_info.team, infos.team_icon, true);
127ed7a3501SAxel Dörfler } else {
128ed7a3501SAxel Dörfler BMessage* kill_team = new BMessage('KlTm');
129a12827f2SAxel Dörfler kill_team->AddInt32("team", infos.team_info.team);
130a12827f2SAxel Dörfler item = new TeamBarMenuItem(new ThreadBarMenu(infos.team_name,
131a12827f2SAxel Dörfler infos.team_info.team, infos.team_info.thread_count),
132a12827f2SAxel Dörfler kill_team, infos.team_info.team, infos.team_icon, true);
133ed7a3501SAxel Dörfler item->SetTarget(gPCView);
134ed7a3501SAxel Dörfler AddItem(item);
135ed7a3501SAxel Dörfler item->BarUpdate();
136ed7a3501SAxel Dörfler }
137ed7a3501SAxel Dörfler if (item->fKernel >= 0) {
138ed7a3501SAxel Dörfler total += item->fUser + item->fKernel;
139ed7a3501SAxel Dörfler item = NULL;
140ed7a3501SAxel Dörfler } else {
141ed7a3501SAxel Dörfler fTeamList[j] = -1;
142ed7a3501SAxel Dörfler }
143ed7a3501SAxel Dörfler }
144ed7a3501SAxel Dörfler }
145ed7a3501SAxel Dörfler }
146ed7a3501SAxel Dörfler if (item) {
147ed7a3501SAxel Dörfler RemoveItem(item);
148ed7a3501SAxel Dörfler delete item;
149ed7a3501SAxel Dörfler }
150ed7a3501SAxel Dörfler
151ed7a3501SAxel Dörfler // Delete the items that haven't been recycled.
152ed7a3501SAxel Dörfler if (firstRecycle < lastRecycle)
153ed7a3501SAxel Dörfler RemoveItems(IndexOf(fRecycleList[firstRecycle].item), lastRecycle - firstRecycle, true);
154ed7a3501SAxel Dörfler
155ed7a3501SAxel Dörfler total /= gCPUcount;
156ed7a3501SAxel Dörfler total = 1 - total;
157ed7a3501SAxel Dörfler
158ed7a3501SAxel Dörfler fLastTotalTime = system_time();
159ed7a3501SAxel Dörfler NoiseBarMenuItem* noiseItem;
160ed7a3501SAxel Dörfler if ((noiseItem = (NoiseBarMenuItem*)ItemAt(0)) != NULL) {
16146e13cfcSIngo Weinhold noiseItem->SetBusyWaiting(0);
16246e13cfcSIngo Weinhold if (total >= 0)
16346e13cfcSIngo Weinhold noiseItem->SetLost(total);
16446e13cfcSIngo Weinhold else
16546e13cfcSIngo Weinhold noiseItem->SetLost(0);
166ed7a3501SAxel Dörfler noiseItem->DrawBar(false);
167ed7a3501SAxel Dörfler }
168ed7a3501SAxel Dörfler
169ed7a3501SAxel Dörfler if (gCurrentThreadBarMenu && gCurrentThreadBarMenu->LockLooperWithTimeout(25000) == B_OK) {
170ed7a3501SAxel Dörfler gCurrentThreadBarMenu->Window()->BeginViewTransaction();
171ed7a3501SAxel Dörfler gCurrentThreadBarMenu->Update();
172ed7a3501SAxel Dörfler gCurrentThreadBarMenu->Window()->EndViewTransaction();
173ed7a3501SAxel Dörfler gCurrentThreadBarMenu->Window()->Flush();
174ed7a3501SAxel Dörfler gCurrentThreadBarMenu->UnlockLooper();
175ed7a3501SAxel Dörfler }
176a12827f2SAxel Dörfler
177ed7a3501SAxel Dörfler Window()->EndViewTransaction();
178ed7a3501SAxel Dörfler Window()->Flush();
179ed7a3501SAxel Dörfler }
180