xref: /haiku/src/apps/pulse/NormalPulseView.cpp (revision d1d811ec7007913f727f6b44d2d730554eacfa19)
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 NormalPulseView::NormalPulseView(BRect rect)
28 	: PulseView(rect, "NormalPulseView"),
29 	fHasBrandLogo(false)
30 {
31 	rgb_color color = { 168, 168, 168, 0xff };
32 	SetViewColor(color);
33 	SetLowColor(color);
34 
35 	mode1->SetLabel("Mini Mode");
36 	mode1->SetMessage(new BMessage(PV_MINI_MODE));
37 	mode2->SetLabel("Deskbar Mode");
38 	mode2->SetMessage(new BMessage(PV_DESKBAR_MODE));
39 
40 	DetermineVendorAndProcessor();
41 
42 	// Allocate progress bars and button pointers
43 	system_info sys_info;
44 	get_system_info(&sys_info);
45 	fProgressBars = new ProgressBar *[sys_info.cpu_count];
46 	fCpuButtons = new CPUButton *[sys_info.cpu_count];
47 
48 	// Set up the CPU activity bars and buttons
49 	for (int x = 0; x < sys_info.cpu_count; x++) {
50 		BRect r(PROGRESS_MLEFT, PROGRESS_MTOP + ITEM_OFFSET * x,
51 			PROGRESS_MLEFT + ProgressBar::PROGRESS_WIDTH,
52 			PROGRESS_MTOP + ITEM_OFFSET * x + ProgressBar::PROGRESS_HEIGHT);
53 		fProgressBars[x] = new ProgressBar(r, "CPU Progress Bar");
54 		AddChild(fProgressBars[x]);
55 
56 		r.Set(CPUBUTTON_MLEFT, CPUBUTTON_MTOP + ITEM_OFFSET * x,
57 			CPUBUTTON_MLEFT + CPUBUTTON_WIDTH,
58 			CPUBUTTON_MTOP + ITEM_OFFSET * x + CPUBUTTON_HEIGHT);
59 		char temp[4];
60 		sprintf(temp, "%d", x + 1);
61 		fCpuButtons[x] = new CPUButton(r, "CPUButton", temp, NULL);
62 		AddChild(fCpuButtons[x]);
63 
64 		//	If there is only 1 cpu it will be hidden below
65 		//	thus, no need to add the dragger as it will still
66 		//	be visible when replicants are turned on
67 		if (sys_info.cpu_count > 1) {
68 			BRect dragger_rect;
69 			dragger_rect = r;
70 			dragger_rect.top = dragger_rect.bottom;
71 			dragger_rect.left = dragger_rect.right;
72 			dragger_rect.bottom += 7;
73 			dragger_rect.right += 7;
74 			dragger_rect.OffsetBy(-1, -1);
75 			BDragger *dragger = new BDragger(dragger_rect, fCpuButtons[x], 0);
76 			AddChild(dragger);
77 		}
78 	}
79 
80 	if (sys_info.cpu_count == 1) {
81 		fProgressBars[0]->MoveBy(-3, 12);
82 		fCpuButtons[0]->Hide();
83 	}
84 }
85 
86 
87 int
88 NormalPulseView::CalculateCPUSpeed()
89 {
90 	system_info sys_info;
91 	get_system_info(&sys_info);
92 
93 	int target = sys_info.cpu_clock_speed / 1000000;
94 	int frac = target % 100;
95 	int delta = -frac;
96 	int at = 0;
97 	int freqs[] = { 100, 50, 25, 75, 33, 67, 20, 40, 60, 80, 10, 30, 70, 90 };
98 
99 	for (uint x = 0; x < sizeof(freqs) / sizeof(freqs[0]); x++) {
100 		int ndelta = freqs[x] - frac;
101 		if (abs(ndelta) < abs(delta)) {
102 			at = freqs[x];
103 			delta = ndelta;
104 		}
105 	}
106 	return target + delta;
107 }
108 
109 
110 void
111 NormalPulseView::DetermineVendorAndProcessor()
112 {
113 	system_info sys_info;
114 	get_system_info(&sys_info);
115 
116 	// Initialize logos
117 	BRect r(0, 0, 63, 62);
118 	fCpuLogo = new BBitmap(r, B_COLOR_8_BIT);
119 #if __POWERPC__
120 	fCpuLogo->SetBits(Anim1, 11718, 0, B_COLOR_8_BIT);
121 #endif
122 #if __INTEL__
123 	if ((sys_info.cpu_type & B_CPU_x86_VENDOR_MASK) == B_CPU_INTEL_x86) {
124 		fCpuLogo->SetBits(IntelLogo, 11718, 0, B_COLOR_8_BIT);
125 		fHasBrandLogo = true;
126 	} else
127 		fCpuLogo->SetBits(BlankLogo, 11718, 0, B_COLOR_8_BIT);
128 #endif
129 
130 	get_cpu_type(fVendor, sizeof(fVendor), fProcessor, sizeof(fProcessor));
131 }
132 
133 
134 void
135 NormalPulseView::Draw(BRect rect)
136 {
137 	PushState();
138 
139 	// Black frame
140 	SetHighColor(0, 0, 0);
141 	BRect frame = Bounds();
142 	frame.right--;
143 	frame.bottom--;
144 	StrokeRect(frame);
145 
146 	// Bevelled edges
147 	SetHighColor(255, 255, 255);
148 	StrokeLine(BPoint(1, 1), BPoint(frame.right - 1, 1));
149 	StrokeLine(BPoint(1, 1), BPoint(1, frame.bottom - 1));
150 	SetHighColor(80, 80, 80);
151 	StrokeLine(BPoint(frame.right, 1), BPoint(frame.right, frame.bottom));
152 	StrokeLine(BPoint(2, frame.bottom), BPoint(frame.right - 1, frame.bottom));
153 
154 	// Dividing line
155 	SetHighColor(96, 96, 96);
156 	StrokeLine(BPoint(1, frame.bottom + 1), BPoint(frame.right, frame.bottom + 1));
157 	SetHighColor(255, 255, 255);
158 	StrokeLine(BPoint(1, frame.bottom + 2), BPoint(frame.right, frame.bottom + 2));
159 
160 	// Processor picture
161 	DrawBitmap(fCpuLogo, BPoint(10, 10));
162 
163 #if __INTEL__
164 	// Do nothing in the case of Intel CPUs - they already have a logo
165 	if (!fHasBrandLogo) {
166 		SetDrawingMode(B_OP_OVER);
167 		SetHighColor(240,240,240);
168 
169 		float width = StringWidth(fVendor);
170 		MovePenTo(10 + (32 - width / 2), 30);
171 		DrawString(fVendor);
172 	}
173 #endif
174 
175 	// Draw processor type and speed
176 	char buf[500];
177 	sprintf(buf, "%d MHz", CalculateCPUSpeed());
178 	SetDrawingMode(B_OP_OVER);
179 	SetHighColor(240, 240, 240);
180 
181 	float width = StringWidth(fProcessor);
182 	MovePenTo(10 + (32 - width / 2), 48);
183 	DrawString(fProcessor);
184 
185 	width = StringWidth(buf);
186 	MovePenTo(10 + (32 - width / 2), 60);
187 	DrawString(buf);
188 
189 	PopState();
190 }
191 
192 
193 void
194 NormalPulseView::Pulse()
195 {
196 	// Don't recalculate and redraw if this view is hidden
197 	if (!IsHidden()) {
198 		Update();
199 		if (Window()->Lock()) {
200 			system_info sys_info;
201 			get_system_info(&sys_info);
202 
203 			// Set the value of each CPU bar
204 			for (int x = 0; x < sys_info.cpu_count; x++) {
205 				fProgressBars[x]->Set(max_c(0, cpu_times[x] * 100));
206 			}
207 
208 			Sync();
209 			Window()->Unlock();
210 		}
211 	}
212 }
213 
214 
215 void
216 NormalPulseView::AttachedToWindow()
217 {
218 	// Use a smaller font on x86 to accomodate longer processor names
219 	SetFont(be_bold_font);
220 #if __INTEL__
221 	SetFontSize(7);
222 #else
223 	SetFontSize(9);
224 #endif
225 	fPreviousTime = system_time();
226 
227 	BMessenger messenger(Window());
228 	mode1->SetTarget(messenger);
229 	mode2->SetTarget(messenger);
230 	preferences->SetTarget(messenger);
231 	about->SetTarget(messenger);
232 
233 	system_info sys_info;
234 	get_system_info(&sys_info);
235 	if (sys_info.cpu_count >= 2) {
236 		for (int x = 0; x < sys_info.cpu_count; x++) {
237 			cpu_menu_items[x]->SetTarget(messenger);
238 		}
239 	}
240 }
241 
242 
243 void
244 NormalPulseView::UpdateColors(BMessage *message)
245 {
246 	int32 color = message->FindInt32("color");
247 	bool fade = message->FindBool("fade");
248 	system_info sys_info;
249 	get_system_info(&sys_info);
250 
251 	for (int x = 0; x < sys_info.cpu_count; x++) {
252 		fProgressBars[x]->UpdateColors(color, fade);
253 		fCpuButtons[x]->UpdateColors(color);
254 	}
255 }
256 
257 
258 NormalPulseView::~NormalPulseView()
259 {
260 	delete fCpuLogo;
261 	delete fCpuButtons;
262 	delete fProgressBars;
263 }
264