xref: /haiku/src/apps/pulse/NormalPulseView.cpp (revision a4f6a81235ca2522c01f532de13cad9b729d4029)
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,
77 			CPUBUTTON_MTOP + ITEM_OFFSET * x + CPUBUTTON_HEIGHT);
78 		char temp[4];
79 		sprintf(temp, "%d", x + 1);
80 		fCpuButtons[x] = new CPUButton(r, "CPUButton", temp, NULL);
81 		AddChild(fCpuButtons[x]);
82 
83 		//	If there is only 1 cpu it will be hidden below
84 		//	thus, no need to add the dragger as it will still
85 		//	be visible when replicants are turned on
86 		if (fCpuCount > 1) {
87 			BRect dragger_rect;
88 			dragger_rect = r;
89 			dragger_rect.top = dragger_rect.bottom;
90 			dragger_rect.left = dragger_rect.right;
91 			dragger_rect.bottom += 7;
92 			dragger_rect.right += 7;
93 			dragger_rect.OffsetBy(-1, -1);
94 			BDragger *dragger = new BDragger(dragger_rect, fCpuButtons[x], 0);
95 			AddChild(dragger);
96 		}
97 	}
98 
99 	if (fCpuCount == 1) {
100 		fProgressBars[0]->MoveBy(-3, 12);
101 		fCpuButtons[0]->Hide();
102 	}
103 }
104 
105 
106 NormalPulseView::~NormalPulseView()
107 {
108 	delete fCpuLogo;
109 	delete[] fCpuButtons;
110 	delete[] fProgressBars;
111 }
112 
113 
114 void
115 NormalPulseView::CalculateFontSize()
116 {
117 	BFont font;
118 	GetFont(&font);
119 
120 	fProcessorFontSize = max_font_size(font, fProcessor, 11.0f, 46.0f);
121 
122 	if (!fHasBrandLogo)
123 		fVendorFontSize = max_font_size(font, fVendor, 13.0f, 46.0f);
124 }
125 
126 
127 void
128 NormalPulseView::DetermineVendorAndProcessor()
129 {
130 	system_info sys_info;
131 	get_system_info(&sys_info);
132 
133 	// Initialize logo
134 
135 	fCpuLogo = new BBitmap(BRect(0, 0, 63, 62), B_COLOR_8_BIT);
136 	unsigned char *logo = BlankLogo;
137 
138 #if __POWERPC__
139 	logo = PowerPCLogo;
140 #endif
141 #if __INTEL__
142 
143 	switch (sys_info.cpu_type & B_CPU_x86_VENDOR_MASK) {
144 	case B_CPU_INTEL_x86:
145 		logo = IntelLogo;
146 		break;
147 
148 	case B_CPU_AMD_x86:
149 		logo = AmdLogo;
150 		break;
151 	};
152 #endif
153 
154 	fCpuLogo->SetBits(logo, fCpuLogo->BitsLength(), 0, B_COLOR_8_BIT);
155 	fHasBrandLogo = (logo != BlankLogo);
156 
157 	get_cpu_type(fVendor, sizeof(fVendor), fProcessor, sizeof(fProcessor));
158 }
159 
160 
161 void
162 NormalPulseView::Draw(BRect rect)
163 {
164 	PushState();
165 
166 	// Black frame
167 	SetHighColor(0, 0, 0);
168 	BRect frame = Bounds();
169 	frame.right--;
170 	frame.bottom--;
171 	StrokeRect(frame);
172 
173 	// Bevelled edges
174 	SetHighColor(255, 255, 255);
175 	StrokeLine(BPoint(1, 1), BPoint(frame.right - 1, 1));
176 	StrokeLine(BPoint(1, 1), BPoint(1, frame.bottom - 1));
177 	SetHighColor(80, 80, 80);
178 	StrokeLine(BPoint(frame.right, 1), BPoint(frame.right, frame.bottom));
179 	StrokeLine(BPoint(2, frame.bottom), BPoint(frame.right - 1, frame.bottom));
180 
181 	// Dividing line
182 	SetHighColor(96, 96, 96);
183 	StrokeLine(BPoint(1, frame.bottom + 1), BPoint(frame.right, frame.bottom + 1));
184 	SetHighColor(255, 255, 255);
185 	StrokeLine(BPoint(1, frame.bottom + 2), BPoint(frame.right, frame.bottom + 2));
186 
187 	// Processor picture
188 	DrawBitmap(fCpuLogo, BPoint(10, 10));
189 
190 #if __INTEL__
191 	// Do nothing in the case of non-Intel CPUs - they already have a logo
192 	if (!fHasBrandLogo) {
193 		SetDrawingMode(B_OP_OVER);
194 		SetHighColor(240, 240, 240);
195 		SetFontSize(fVendorFontSize);
196 
197 		float width = StringWidth(fVendor);
198 		MovePenTo(10 + (32 - width / 2), 30);
199 		DrawString(fVendor);
200 	}
201 #endif
202 
203 	// Draw processor type and speed
204 	char buffer[64];
205 	int32 cpuSpeed = get_rounded_cpu_speed();
206 	if (cpuSpeed > 1000 && (cpuSpeed % 10) == 0)
207 		snprintf(buffer, sizeof(buffer), "%.2f GHz", cpuSpeed / 1000.0f);
208 	else
209 		snprintf(buffer, sizeof(buffer), "%ld MHz", cpuSpeed);
210 	SetDrawingMode(B_OP_OVER);
211 	SetHighColor(240, 240, 240);
212 	SetFontSize(fProcessorFontSize);
213 
214 	float width = StringWidth(fProcessor);
215 	MovePenTo(10 + (32 - width / 2), 48);
216 	DrawString(fProcessor);
217 
218 	width = StringWidth(buffer);
219 	MovePenTo(10 + (32 - width / 2), 60);
220 	DrawString(buffer);
221 
222 	PopState();
223 }
224 
225 
226 void
227 NormalPulseView::Pulse()
228 {
229 	// Don't recalculate and redraw if this view is hidden
230 	if (!IsHidden()) {
231 		Update();
232 		if (Window()->Lock()) {
233 			// Set the value of each CPU bar
234 			for (int x = 0; x < fCpuCount; x++) {
235 				fProgressBars[x]->Set(max_c(0, cpu_times[x] * 100));
236 			}
237 
238 			Sync();
239 			Window()->Unlock();
240 		}
241 	}
242 }
243 
244 
245 void
246 NormalPulseView::AttachedToWindow()
247 {
248 	SetFont(be_bold_font);
249 	CalculateFontSize();
250 
251 	fPreviousTime = system_time();
252 
253 	BMessenger messenger(Window());
254 	mode1->SetTarget(messenger);
255 	mode2->SetTarget(messenger);
256 	preferences->SetTarget(messenger);
257 	about->SetTarget(messenger);
258 
259 	system_info sys_info;
260 	get_system_info(&sys_info);
261 	if (sys_info.cpu_count >= 2) {
262 		for (int x = 0; x < sys_info.cpu_count; x++) {
263 			cpu_menu_items[x]->SetTarget(messenger);
264 		}
265 	}
266 }
267 
268 
269 void
270 NormalPulseView::UpdateColors(BMessage *message)
271 {
272 	int32 color = message->FindInt32("color");
273 	bool fade = message->FindBool("fade");
274 	system_info sys_info;
275 	get_system_info(&sys_info);
276 
277 	for (int x = 0; x < sys_info.cpu_count; x++) {
278 		fProgressBars[x]->UpdateColors(color, fade);
279 		fCpuButtons[x]->UpdateColors(color);
280 	}
281 }
282 
283