From 26ca407bfc94336678258a2151ff0f92ca449021 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Sat, 9 Jul 2005 21:01:23 +0000 Subject: [PATCH] Moved the CPU speed "clearing" function from Pulse to cpu_type.h header. It's now also used by AboutHaiku. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@13569 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- headers/private/shared/cpu_type.h | 29 ++++++++++++++++++++-- src/apps/abouthaiku/AboutHaiku.cpp | 7 +++--- src/apps/pulse/NormalPulseView.cpp | 39 ++++++------------------------ src/apps/pulse/NormalPulseView.h | 1 - 4 files changed, 39 insertions(+), 37 deletions(-) diff --git a/headers/private/shared/cpu_type.h b/headers/private/shared/cpu_type.h index 3a14e6a42b..75efbcef68 100644 --- a/headers/private/shared/cpu_type.h +++ b/headers/private/shared/cpu_type.h @@ -1,10 +1,10 @@ /* - * Copyright 2004, Axel Dörfler, axeld@pinc-software.de. All rights reserved. + * Copyright 2004-2005, Axel Dörfler, axeld@pinc-software.de. All rights reserved. * Distributed under the terms of the MIT License. */ /* Taken from the Pulse application, and extended. - * It's used by Pulse and sysinfo. + * It's used by Pulse, AboutHaiku, and sysinfo. */ #ifdef __cplusplus @@ -14,6 +14,7 @@ extern "C" { const char *get_cpu_vendor_string(enum cpu_types type); const char *get_cpu_model_string(enum cpu_types type); void get_cpu_type(char *vendorBuffer, size_t vendorSize, char *modelBuffer, size_t modelSize); +int32 get_rounded_cpu_speed(void); #ifdef __cplusplus } @@ -202,3 +203,27 @@ get_cpu_type(char *vendorBuffer, size_t vendorSize, char *modelBuffer, size_t mo strlcpy(modelBuffer, model, modelSize); #endif } + + +int32 +get_rounded_cpu_speed(void) +{ + system_info sys_info; + get_system_info(&sys_info); + + int target = sys_info.cpu_clock_speed / 1000000; + int frac = target % 100; + int delta = -frac; + int at = 0; + int freqs[] = { 100, 50, 25, 75, 33, 67, 20, 40, 60, 80, 10, 30, 70, 90 }; + + for (uint x = 0; x < sizeof(freqs) / sizeof(freqs[0]); x++) { + int ndelta = freqs[x] - frac; + if (abs(ndelta) < abs(delta)) { + at = freqs[x]; + delta = ndelta; + } + } + return target + delta; +} + diff --git a/src/apps/abouthaiku/AboutHaiku.cpp b/src/apps/abouthaiku/AboutHaiku.cpp index 25c7891532..50efd059d7 100644 --- a/src/apps/abouthaiku/AboutHaiku.cpp +++ b/src/apps/abouthaiku/AboutHaiku.cpp @@ -207,10 +207,11 @@ AboutView::AboutView(const BRect &r) r.OffsetBy(0, labelHeight); r.bottom = r.top + textHeight; - if (systemInfo.cpu_clock_speed < 1000000000) - sprintf(string,"%d MHz", int(systemInfo.cpu_clock_speed / 1000000.0f)); + int32 clockSpeed = get_rounded_cpu_speed(); + if (clockSpeed < 1000000000) + sprintf(string,"%ld MHz", clockSpeed); else - sprintf(string,"%.2f GHz", systemInfo.cpu_clock_speed / 1000000000.0f); + sprintf(string,"%.2f GHz", clockSpeed / 1000.0f); stringView = new BStringView(r, "mhztext", string); fInfoView->AddChild(stringView); diff --git a/src/apps/pulse/NormalPulseView.cpp b/src/apps/pulse/NormalPulseView.cpp index 6a2cb9b466..2481284e70 100644 --- a/src/apps/pulse/NormalPulseView.cpp +++ b/src/apps/pulse/NormalPulseView.cpp @@ -124,29 +124,6 @@ NormalPulseView::CalculateFontSize() } -int -NormalPulseView::CalculateCPUSpeed() -{ - system_info sys_info; - get_system_info(&sys_info); - - int target = sys_info.cpu_clock_speed / 1000000; - int frac = target % 100; - int delta = -frac; - int at = 0; - int freqs[] = { 100, 50, 25, 75, 33, 67, 20, 40, 60, 80, 10, 30, 70, 90 }; - - for (uint x = 0; x < sizeof(freqs) / sizeof(freqs[0]); x++) { - int ndelta = freqs[x] - frac; - if (abs(ndelta) < abs(delta)) { - at = freqs[x]; - delta = ndelta; - } - } - return target + delta; -} - - void NormalPulseView::DetermineVendorAndProcessor() { @@ -215,12 +192,12 @@ NormalPulseView::Draw(BRect rect) #endif // Draw processor type and speed - char buf[500]; - int cpu_speed = CalculateCPUSpeed(); - if (cpu_speed > 1000 && (cpu_speed % 10) == 0) - sprintf(buf, "%.2f GHz", cpu_speed / 1000.0f); + char buffer[64]; + int32 cpuSpeed = get_rounded_cpu_speed(); + if (cpuSpeed > 1000 && (cpuSpeed % 10) == 0) + snprintf(buffer, sizeof(buffer), "%.2f GHz", cpuSpeed / 1000.0f); else - sprintf(buf, "%d MHz", cpu_speed); + snprintf(buffer, sizeof(buffer), "%d MHz", cpuSpeed); SetDrawingMode(B_OP_OVER); SetHighColor(240, 240, 240); SetFontSize(fProcessorFontSize); @@ -229,10 +206,10 @@ NormalPulseView::Draw(BRect rect) MovePenTo(10 + (32 - width / 2), 48); DrawString(fProcessor); - width = StringWidth(buf); + width = StringWidth(buffer); MovePenTo(10 + (32 - width / 2), 60); - DrawString(buf); - + DrawString(buffer); + PopState(); } diff --git a/src/apps/pulse/NormalPulseView.h b/src/apps/pulse/NormalPulseView.h index c406bee084..de0cb8e3ce 100644 --- a/src/apps/pulse/NormalPulseView.h +++ b/src/apps/pulse/NormalPulseView.h @@ -28,7 +28,6 @@ class NormalPulseView : public PulseView { void UpdateColors(BMessage *message); private: - int CalculateCPUSpeed(); void DetermineVendorAndProcessor(); void CalculateFontSize();