1 //*****************************************************************************
3 // File: NormalPulseView.cpp
5 // Written by: Daniel Switkin
7 // Copyright 1999, Be Incorporated
9 //*****************************************************************************
12 #include "NormalPulseView.h"
27 #undef B_TRANSLATION_CONTEXT
28 #define B_TRANSLATION_CONTEXT "NormalPulseView"
32 max_font_size(BFont font
, const char* text
, float maxSize
, float maxWidth
)
34 const float steps
= 0.5f
;
36 for (float size
= maxSize
; size
> 4; size
-= steps
) {
38 if (font
.StringWidth(text
) <= maxWidth
)
49 NormalPulseView::NormalPulseView(BRect rect
)
50 : PulseView(rect
, "NormalPulseView"),
53 SetViewUIColor(B_PANEL_BACKGROUND_COLOR
);
54 SetLowUIColor(ViewUIColor());
56 mode1
->SetLabel(B_TRANSLATE("Mini mode"));
57 mode1
->SetMessage(new BMessage(PV_MINI_MODE
));
58 mode2
->SetLabel(B_TRANSLATE("Deskbar mode"));
59 mode2
->SetMessage(new BMessage(PV_DESKBAR_MODE
));
61 DetermineVendorAndProcessor();
63 // Allocate progress bars and button pointers
64 system_info systemInfo
;
65 get_system_info(&systemInfo
);
66 fCpuCount
= systemInfo
.cpu_count
;
67 fProgressBars
= new ProgressBar
*[fCpuCount
];
68 fCpuButtons
= new CPUButton
*[fCpuCount
];
70 // Set up the CPU activity bars and buttons
71 for (int x
= 0; x
< fCpuCount
; x
++) {
72 BRect
r(PROGRESS_MLEFT
, PROGRESS_MTOP
+ ITEM_OFFSET
* x
,
73 PROGRESS_MLEFT
+ ProgressBar::PROGRESS_WIDTH
,
74 PROGRESS_MTOP
+ ITEM_OFFSET
* x
+ ProgressBar::PROGRESS_HEIGHT
);
75 char* str2
= (char *)B_TRANSLATE("CPU progress bar");
76 fProgressBars
[x
] = new ProgressBar(r
, str2
);
77 AddChild(fProgressBars
[x
]);
79 r
.Set(CPUBUTTON_MLEFT
, CPUBUTTON_MTOP
+ ITEM_OFFSET
* x
,
80 CPUBUTTON_MLEFT
+ CPUBUTTON_WIDTH
+ 7,
81 CPUBUTTON_MTOP
+ ITEM_OFFSET
* x
+ CPUBUTTON_HEIGHT
+ 7);
83 sprintf(temp
, "%d", x
+ 1);
84 fCpuButtons
[x
] = new CPUButton(r
, B_TRANSLATE("Pulse"), temp
, NULL
);
85 AddChild(fCpuButtons
[x
]);
89 fProgressBars
[0]->MoveBy(-3, 12);
90 fCpuButtons
[0]->Hide();
95 NormalPulseView::~NormalPulseView()
99 delete[] fProgressBars
;
104 NormalPulseView::CalculateFontSizes()
109 fProcessorFontSize
= max_font_size(font
, fProcessor
, 11.0f
, 46.0f
);
112 fVendorFontSize
= max_font_size(font
, fVendor
, 13.0f
, 46.0f
);
117 NormalPulseView::DetermineVendorAndProcessor()
119 system_info sys_info
;
120 get_system_info(&sys_info
);
124 fCpuLogo
= new BBitmap(BRect(0, 0, 63, 62), B_CMAP8
);
125 unsigned char *logo
= BlankLogo
;
130 uint32 topologyNodeCount
= 0;
131 cpu_topology_node_info
* topology
= NULL
;
133 get_cpu_topology_info(NULL
, &topologyNodeCount
);
134 if (topologyNodeCount
!= 0)
135 topology
= new cpu_topology_node_info
[topologyNodeCount
];
136 get_cpu_topology_info(topology
, &topologyNodeCount
);
138 for (uint32 i
= 0; i
< topologyNodeCount
; i
++) {
139 if (topology
[i
].type
== B_TOPOLOGY_PACKAGE
) {
140 switch (topology
[i
].data
.package
.vendor
) {
141 case B_CPU_VENDOR_INTEL
:
145 case B_CPU_VENDOR_AMD
:
160 fCpuLogo
->SetBits(logo
, fCpuLogo
->BitsLength(), 0, B_CMAP8
);
161 fHasBrandLogo
= (logo
!= BlankLogo
);
163 get_cpu_type(fVendor
, sizeof(fVendor
), fProcessor
, sizeof(fProcessor
));
168 NormalPulseView::Draw(BRect rect
)
173 SetHighColor(0, 0, 0);
174 BRect frame
= Bounds();
180 SetHighColor(255, 255, 255);
181 StrokeLine(BPoint(1, 1), BPoint(frame
.right
- 1, 1));
182 StrokeLine(BPoint(1, 1), BPoint(1, frame
.bottom
- 1));
183 SetHighColor(80, 80, 80);
184 StrokeLine(BPoint(frame
.right
, 1), BPoint(frame
.right
, frame
.bottom
));
185 StrokeLine(BPoint(2, frame
.bottom
), BPoint(frame
.right
- 1, frame
.bottom
));
188 SetHighColor(96, 96, 96);
189 StrokeLine(BPoint(1, frame
.bottom
+ 1), BPoint(frame
.right
, frame
.bottom
+ 1));
190 SetHighColor(255, 255, 255);
191 StrokeLine(BPoint(1, frame
.bottom
+ 2), BPoint(frame
.right
, frame
.bottom
+ 2));
194 DrawBitmap(fCpuLogo
, BPoint(10, 10));
197 // Do nothing in the case of non-Intel CPUs - they already have a logo
198 if (!fHasBrandLogo
) {
199 SetDrawingMode(B_OP_OVER
);
200 SetHighColor(240, 240, 240);
201 SetFontSize(fVendorFontSize
);
203 float width
= StringWidth(fVendor
);
204 MovePenTo(10 + (32 - width
/ 2), 30);
209 // Draw processor type and speed
210 SetDrawingMode(B_OP_OVER
);
211 SetHighColor(240, 240, 240);
213 SetFontSize(fProcessorFontSize
);
214 float width
= StringWidth(fProcessor
);
215 MovePenTo(10 + (32 - width
/ 2), 48);
216 DrawString(fProcessor
);
219 int32 cpuSpeed
= get_rounded_cpu_speed();
220 if (cpuSpeed
> 1000 && (cpuSpeed
% 10) == 0)
221 snprintf(buffer
, sizeof(buffer
), B_TRANSLATE("%.2f GHz"), cpuSpeed
/ 1000.0f
);
223 snprintf(buffer
, sizeof(buffer
), B_TRANSLATE("%ld MHz"), cpuSpeed
);
225 // We can't assume anymore that a CPU clock speed is always static.
226 // Let's compute the best font size for the CPU speed string each time...
229 SetFontSize(max_font_size(font
, buffer
, fProcessorFontSize
, 46.0f
));
230 width
= StringWidth(buffer
);
231 MovePenTo(10 + (32 - width
/ 2), 60);
239 NormalPulseView::Pulse()
241 // Don't recalculate and redraw if this view is hidden
244 if (Window()->Lock()) {
245 // Set the value of each CPU bar
246 for (int x
= 0; x
< fCpuCount
; x
++) {
247 fProgressBars
[x
]->Set((int32
)max_c(0, cpu_times
[x
] * 100));
258 NormalPulseView::AttachedToWindow()
260 SetFont(be_bold_font
);
261 CalculateFontSizes();
263 fPreviousTime
= system_time();
265 BMessenger
messenger(Window());
266 mode1
->SetTarget(messenger
);
267 mode2
->SetTarget(messenger
);
268 preferences
->SetTarget(messenger
);
269 about
->SetTarget(messenger
);
271 system_info sys_info
;
272 get_system_info(&sys_info
);
273 if (sys_info
.cpu_count
>= 2) {
274 for (unsigned int x
= 0; x
< sys_info
.cpu_count
; x
++)
275 cpu_menu_items
[x
]->SetTarget(messenger
);
281 NormalPulseView::UpdateColors(BMessage
*message
)
283 int32 color
= message
->FindInt32("color");
284 bool fade
= message
->FindBool("fade");
285 system_info sys_info
;
286 get_system_info(&sys_info
);
288 for (unsigned int x
= 0; x
< sys_info
.cpu_count
; x
++) {
289 fProgressBars
[x
]->UpdateColors(color
, fade
);
290 fCpuButtons
[x
]->UpdateColors(color
);