xref: /haiku/src/apps/pulse/NormalPulseView.cpp (revision 9760dcae2038d47442f4658c2575844c6cf92c40)
1 //****************************************************************************************
2 //
3 //	File:		NormalPulseView.cpp
4 //
5 //	Written by:	Daniel Switkin
6 //
7 //	Copyright 1999, Be Incorporated
8 //
9 //****************************************************************************************
10 
11 
12 #include "NormalPulseView.h"
13 #include "Common.h"
14 #include "Pictures"
15 
16 #include <Bitmap.h>
17 #include <Dragger.h>
18 #include <Window.h>
19 
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23 
24 #include <cpu_type.h>
25 
26 
27 float
28 max_font_size(BFont font, const char* text, float maxSize, float maxWidth)
29 {
30 	const float steps = 0.5f;
31 
32 	for (float size = maxSize; size > 4; size -= steps) {
33 		font.SetSize(size);
34 		if (font.StringWidth(text) <= maxWidth)
35 			return size;
36 	}
37 
38 	return 4;
39 }
40 
41 
42 //	#pragma mark -
43 
44 
45 NormalPulseView::NormalPulseView(BRect rect)
46 	: PulseView(rect, "NormalPulseView"),
47 	fHasBrandLogo(false)
48 {
49 	rgb_color color = { 168, 168, 168, 0xff };
50 	SetViewColor(color);
51 	SetLowColor(color);
52 
53 	mode1->SetLabel("Mini mode");
54 	mode1->SetMessage(new BMessage(PV_MINI_MODE));
55 	mode2->SetLabel("Deskbar mode");
56 	mode2->SetMessage(new BMessage(PV_DESKBAR_MODE));
57 
58 	DetermineVendorAndProcessor();
59 
60 	// Allocate progress bars and button pointers
61 	system_info systemInfo;
62 	get_system_info(&systemInfo);
63 	fCpuCount = systemInfo.cpu_count;
64 	fProgressBars = new ProgressBar *[fCpuCount];
65 	fCpuButtons = new CPUButton *[fCpuCount];
66 
67 	// Set up the CPU activity bars and buttons
68 	for (int x = 0; x < fCpuCount; x++) {
69 		BRect r(PROGRESS_MLEFT, PROGRESS_MTOP + ITEM_OFFSET * x,
70 			PROGRESS_MLEFT + ProgressBar::PROGRESS_WIDTH,
71 			PROGRESS_MTOP + ITEM_OFFSET * x + ProgressBar::PROGRESS_HEIGHT);
72 		fProgressBars[x] = new ProgressBar(r, "CPU progress bar");
73 		AddChild(fProgressBars[x]);
74 
75 		r.Set(CPUBUTTON_MLEFT, CPUBUTTON_MTOP + ITEM_OFFSET * x,
76 			CPUBUTTON_MLEFT + CPUBUTTON_WIDTH + 7,
77 			CPUBUTTON_MTOP + ITEM_OFFSET * x + CPUBUTTON_HEIGHT + 7);
78 		char temp[4];
79 		sprintf(temp, "%d", x + 1);
80 		fCpuButtons[x] = new CPUButton(r, "Pulse", temp, NULL);
81 		AddChild(fCpuButtons[x]);
82 	}
83 
84 	if (fCpuCount == 1) {
85 		fProgressBars[0]->MoveBy(-3, 12);
86 		fCpuButtons[0]->Hide();
87 	}
88 }
89 
90 
91 NormalPulseView::~NormalPulseView()
92 {
93 	delete fCpuLogo;
94 	delete[] fCpuButtons;
95 	delete[] fProgressBars;
96 }
97 
98 
99 void
100 NormalPulseView::CalculateFontSizes()
101 {
102 	BFont font;
103 	GetFont(&font);
104 
105 	fProcessorFontSize = max_font_size(font, fProcessor, 11.0f, 46.0f);
106 
107 	if (!fHasBrandLogo)
108 		fVendorFontSize = max_font_size(font, fVendor, 13.0f, 46.0f);
109 }
110 
111 
112 void
113 NormalPulseView::DetermineVendorAndProcessor()
114 {
115 	system_info sys_info;
116 	get_system_info(&sys_info);
117 
118 	// Initialize logo
119 
120 	fCpuLogo = new BBitmap(BRect(0, 0, 63, 62), B_CMAP8);
121 	unsigned char *logo = BlankLogo;
122 
123 #if __POWERPC__
124 	logo = PowerPCLogo;
125 #endif
126 #if __INTEL__
127 
128 	switch (sys_info.cpu_type & B_CPU_x86_VENDOR_MASK) {
129 	case B_CPU_INTEL_x86:
130 		logo = IntelLogo;
131 		break;
132 
133 	case B_CPU_AMD_x86:
134 		logo = AmdLogo;
135 		break;
136 	};
137 #endif
138 
139 	fCpuLogo->SetBits(logo, fCpuLogo->BitsLength(), 0, B_CMAP8);
140 	fHasBrandLogo = (logo != BlankLogo);
141 
142 	get_cpu_type(fVendor, sizeof(fVendor), fProcessor, sizeof(fProcessor));
143 }
144 
145 
146 void
147 NormalPulseView::Draw(BRect rect)
148 {
149 	PushState();
150 
151 	// Black frame
152 	SetHighColor(0, 0, 0);
153 	BRect frame = Bounds();
154 	frame.right--;
155 	frame.bottom--;
156 	StrokeRect(frame);
157 
158 	// Bevelled edges
159 	SetHighColor(255, 255, 255);
160 	StrokeLine(BPoint(1, 1), BPoint(frame.right - 1, 1));
161 	StrokeLine(BPoint(1, 1), BPoint(1, frame.bottom - 1));
162 	SetHighColor(80, 80, 80);
163 	StrokeLine(BPoint(frame.right, 1), BPoint(frame.right, frame.bottom));
164 	StrokeLine(BPoint(2, frame.bottom), BPoint(frame.right - 1, frame.bottom));
165 
166 	// Dividing line
167 	SetHighColor(96, 96, 96);
168 	StrokeLine(BPoint(1, frame.bottom + 1), BPoint(frame.right, frame.bottom + 1));
169 	SetHighColor(255, 255, 255);
170 	StrokeLine(BPoint(1, frame.bottom + 2), BPoint(frame.right, frame.bottom + 2));
171 
172 	// Processor picture
173 	DrawBitmap(fCpuLogo, BPoint(10, 10));
174 
175 #if __INTEL__
176 	// Do nothing in the case of non-Intel CPUs - they already have a logo
177 	if (!fHasBrandLogo) {
178 		SetDrawingMode(B_OP_OVER);
179 		SetHighColor(240, 240, 240);
180 		SetFontSize(fVendorFontSize);
181 
182 		float width = StringWidth(fVendor);
183 		MovePenTo(10 + (32 - width / 2), 30);
184 		DrawString(fVendor);
185 	}
186 #endif
187 
188 	// Draw processor type and speed
189 	SetDrawingMode(B_OP_OVER);
190 	SetHighColor(240, 240, 240);
191 
192 	SetFontSize(fProcessorFontSize);
193 	float width = StringWidth(fProcessor);
194 	MovePenTo(10 + (32 - width / 2), 48);
195 	DrawString(fProcessor);
196 
197 	char buffer[64];
198 	int32 cpuSpeed = get_rounded_cpu_speed();
199 	if (cpuSpeed > 1000 && (cpuSpeed % 10) == 0)
200 		snprintf(buffer, sizeof(buffer), "%.2f GHz", cpuSpeed / 1000.0f);
201 	else
202 		snprintf(buffer, sizeof(buffer), "%ld MHz", cpuSpeed);
203 
204 	// We can't assume anymore that a CPU clock speed is always static.
205 	// Let's compute the best font size for the CPU speed string each time...
206 	BFont font;
207 	GetFont(&font);
208 	SetFontSize(max_font_size(font, buffer, fProcessorFontSize, 46.0f));
209 	width = StringWidth(buffer);
210 	MovePenTo(10 + (32 - width / 2), 60);
211 	DrawString(buffer);
212 
213 	PopState();
214 }
215 
216 
217 void
218 NormalPulseView::Pulse()
219 {
220 	// Don't recalculate and redraw if this view is hidden
221 	if (!IsHidden()) {
222 		Update();
223 		if (Window()->Lock()) {
224 			// Set the value of each CPU bar
225 			for (int x = 0; x < fCpuCount; x++) {
226 				fProgressBars[x]->Set((int32)max_c(0, cpu_times[x] * 100));
227 			}
228 
229 			Sync();
230 			Window()->Unlock();
231 		}
232 	}
233 }
234 
235 
236 void
237 NormalPulseView::AttachedToWindow()
238 {
239 	SetFont(be_bold_font);
240 	CalculateFontSizes();
241 
242 	fPreviousTime = system_time();
243 
244 	BMessenger messenger(Window());
245 	mode1->SetTarget(messenger);
246 	mode2->SetTarget(messenger);
247 	preferences->SetTarget(messenger);
248 	about->SetTarget(messenger);
249 
250 	system_info sys_info;
251 	get_system_info(&sys_info);
252 	if (sys_info.cpu_count >= 2) {
253 		for (int x = 0; x < sys_info.cpu_count; x++) {
254 			cpu_menu_items[x]->SetTarget(messenger);
255 		}
256 	}
257 }
258 
259 
260 void
261 NormalPulseView::UpdateColors(BMessage *message)
262 {
263 	int32 color = message->FindInt32("color");
264 	bool fade = message->FindBool("fade");
265 	system_info sys_info;
266 	get_system_info(&sys_info);
267 
268 	for (int x = 0; x < sys_info.cpu_count; x++) {
269 		fProgressBars[x]->UpdateColors(color, fade);
270 		fCpuButtons[x]->UpdateColors(color);
271 	}
272 }
273 
274