diff --git a/src/apps/powerstatus/ACPIDriverInterface.h b/src/apps/powerstatus/ACPIDriverInterface.h index 04f71190d5..37cfb31664 100644 --- a/src/apps/powerstatus/ACPIDriverInterface.h +++ b/src/apps/powerstatus/ACPIDriverInterface.h @@ -5,10 +5,10 @@ * Authors: * Clemens Zeidler, haiku@clemens-zeidler.de */ - #ifndef ACPI_DRIVER_INTERFACE_H #define ACPI_DRIVER_INTERFACE_H + #include "DriverInterface.h" #include @@ -17,13 +17,12 @@ const int8 kRateBufferSize = 10; -class RateBuffer -{ +class RateBuffer { public: RateBuffer(); void AddRate(int32 rate); int32 GetMeanRate(); - + private: int32 fRateBuffer[kRateBufferSize]; int8 fPosition; @@ -32,11 +31,10 @@ private: }; -class Battery -{ +class Battery { public: Battery(int driverHandler); - ~Battery(); + ~Battery(); status_t InitCheck(); @@ -49,19 +47,18 @@ public: private: void _Init(); - + int fDriverHandler; status_t fInitStatus; acpi_extended_battery_info fExtendedBatteryInfo; - + RateBuffer fRateBuffer; acpi_battery_info fCachedAcpiInfo; }; -class ACPIDriverInterface : public PowerStatusDriverInterface -{ +class ACPIDriverInterface : public PowerStatusDriverInterface { public: virtual ~ACPIDriverInterface(); @@ -75,13 +72,13 @@ public: protected: // Read the battery info from the hardware. virtual status_t _ReadBatteryInfo(); - + virtual void _WatchPowerStatus(); virtual status_t _FindDrivers(const char* path); BObjectList fDriverList; - + BLocker fInterfaceLocker; }; -#endif +#endif // ACPI_DRIVER_INTERFACE_H diff --git a/src/apps/powerstatus/APMDriverInterface.h b/src/apps/powerstatus/APMDriverInterface.h index ecdb48618f..dc50bb17a1 100644 --- a/src/apps/powerstatus/APMDriverInterface.h +++ b/src/apps/powerstatus/APMDriverInterface.h @@ -5,31 +5,30 @@ * Authors: * Clemens Zeidler, haiku@clemens-zeidler.de */ - #ifndef APM_DRIVER_INTERFACE_H #define APM_DRIVER_INTERFACE_H + #include "DriverInterface.h" -class APMDriverInterface : public PowerStatusDriverInterface -{ +class APMDriverInterface : public PowerStatusDriverInterface { public: virtual ~APMDriverInterface(); virtual status_t Connect(); virtual status_t GetBatteryInfo(battery_info* info, int32 index); virtual status_t GetExtendedBatteryInfo(acpi_extended_battery_info* info, - int32 index); + int32 index); virtual int32 GetBatteryCount(); protected: virtual void _WatchPowerStatus(); - -private: + +private: #ifndef HAIKU_TARGET_PLATFORM_HAIKU int fDevice; #endif }; -#endif +#endif // APM_DRIVER_INTERFACE_H diff --git a/src/apps/powerstatus/DriverInterface.cpp b/src/apps/powerstatus/DriverInterface.cpp index 6c9029d14c..d3b4fc5894 100644 --- a/src/apps/powerstatus/DriverInterface.cpp +++ b/src/apps/powerstatus/DriverInterface.cpp @@ -1,11 +1,12 @@ /* - * Copyright 2006, Haiku, Inc. All Rights Reserved. + * Copyright 2009, Haiku, Inc. All Rights Reserved. * Distributed under the terms of the MIT License. * * Authors: * Clemens Zeidler, haiku@Clemens-Zeidler.de */ - + + #include "DriverInterface.h" #include @@ -14,7 +15,6 @@ Monitor::~Monitor() { - } @@ -39,8 +39,7 @@ Monitor::StopWatching(BHandler* target) void Monitor::Broadcast(uint32 message) { - for (int i = 0; i < fWatcherList.CountItems(); i++) - { + for (int i = 0; i < fWatcherList.CountItems(); i++) { BMessenger messenger(fWatcherList.ItemAt(i)); messenger.SendMessage(message); } @@ -50,9 +49,9 @@ Monitor::Broadcast(uint32 message) PowerStatusDriverInterface::PowerStatusDriverInterface() : fIsWatching(0), - fThreadId(-1) + fThread(-1) { - + } @@ -67,27 +66,25 @@ PowerStatusDriverInterface::StartWatching(BHandler* target) { BAutolock autolock(fListLocker); status_t status = Monitor::StartWatching(target); - + if (status != B_OK) return status; - if (fThreadId > 0) + if (fThread > 0) return B_OK; - - fThreadId = spawn_thread(&_ThreadWatchPowerFunction, "PowerStatusThread", - B_LOW_PRIORITY, this); - if (fThreadId >= 0) { - atomic_set(&fIsWatching, 1); - status = resume_thread(fThreadId); - } - else - return fThreadId; - if (status != B_OK && fWatcherList.CountItems() == 0) { + fThread = spawn_thread(&_ThreadWatchPowerFunction, "PowerStatusThread", + B_LOW_PRIORITY, this); + if (fThread >= 0) { + atomic_set(&fIsWatching, 1); + status = resume_thread(fThread); + } else + return fThread; + + if (status != B_OK && fWatcherList.CountItems() == 0) atomic_set(&fIsWatching, 0); - } + return status; - } @@ -95,17 +92,15 @@ status_t PowerStatusDriverInterface::StopWatching(BHandler* target) { BAutolock autolock(fListLocker); - if (fThreadId < 0) + if (fThread < 0) return B_BAD_VALUE; - status_t status; if (fWatcherList.CountItems() == 1) { atomic_set(&fIsWatching, 0); - - status = wait_for_thread(fThreadId, &status); - fThreadId = -1; + wait_for_thread(fThread, NULL); + fThread = -1; } - + return Monitor::StopWatching(target); } @@ -122,9 +117,8 @@ void PowerStatusDriverInterface::Disconnect() { atomic_set(&fIsWatching, 0); - status_t status; - wait_for_thread(fThreadId, &status); - fThreadId = -1; + wait_for_thread(fThread, NULL); + fThread = -1; } diff --git a/src/apps/powerstatus/DriverInterface.h b/src/apps/powerstatus/DriverInterface.h index 7040783abe..c2268bea94 100644 --- a/src/apps/powerstatus/DriverInterface.h +++ b/src/apps/powerstatus/DriverInterface.h @@ -1,14 +1,14 @@ /* - * Copyright 2006, Haiku, Inc. All Rights Reserved. + * Copyright 2009, Haiku, Inc. All Rights Reserved. * Distributed under the terms of the MIT License. * * Authors: * Clemens Zeidler, haiku@Clemens-Zeidler.de */ - #ifndef DRIVER_INTERFACE_H #define DRIVER_INTERFACE_H + #include #include #include @@ -16,13 +16,13 @@ #include "device/power_managment.h" + typedef BObjectList WatcherList; const uint32 kMsgUpdate = 'updt'; -struct battery_info -{ +struct battery_info { int8 state; int32 capacity; int32 full_capacity; @@ -32,11 +32,10 @@ struct battery_info /*! Handle a list of watcher and broadcast a messages to them. */ -class Monitor -{ +class Monitor { public: virtual ~Monitor(); - + virtual status_t StartWatching(BHandler* target); virtual status_t StopWatching(BHandler* target); @@ -44,12 +43,10 @@ public: protected: WatcherList fWatcherList; - }; -class PowerStatusDriverInterface : public Monitor, public Referenceable -{ +class PowerStatusDriverInterface : public Monitor, public Referenceable { public: PowerStatusDriverInterface(); ~PowerStatusDriverInterface(); @@ -57,26 +54,27 @@ public: virtual status_t StartWatching(BHandler* target); virtual status_t StopWatching(BHandler* target); virtual void Broadcast(uint32 message); - + virtual status_t Connect() = 0; virtual void Disconnect(); - + virtual status_t GetBatteryInfo(battery_info* status, int32 index) = 0; virtual status_t GetExtendedBatteryInfo(acpi_extended_battery_info* info, int32 index) = 0; - + virtual int32 GetBatteryCount() = 0; protected: virtual void _WatchPowerStatus() = 0; vint32 fIsWatching; + private: static int32 _ThreadWatchPowerFunction(void* data); - thread_id fThreadId; + thread_id fThread; BLocker fListLocker; }; -#endif +#endif // DRIVER_INTERFACE_H diff --git a/src/apps/powerstatus/PowerStatus.cpp b/src/apps/powerstatus/PowerStatus.cpp index 786f0ac523..647fb8bebd 100644 --- a/src/apps/powerstatus/PowerStatus.cpp +++ b/src/apps/powerstatus/PowerStatus.cpp @@ -79,7 +79,6 @@ PowerStatus::ReadyToRun() isInstalled = deskbar.HasItem(kDeskbarItemName); } -isInstalled = true; if (isDeskbarRunning && !isInstalled) { BAlert* alert = new BAlert("", "Do you want PowerStatus to live in the Deskbar?", "Don't", "Install", NULL, B_WIDTH_AS_USUAL, B_WARNING_ALERT); diff --git a/src/apps/powerstatus/PowerStatusView.cpp b/src/apps/powerstatus/PowerStatusView.cpp index 57557044e4..f092227fac 100644 --- a/src/apps/powerstatus/PowerStatusView.cpp +++ b/src/apps/powerstatus/PowerStatusView.cpp @@ -191,30 +191,27 @@ PowerStatusView::_DrawBattery(BRect rect) rect.left - 1, floorf(rect.bottom - rect.Height() / 4))); int32 percent = fPercent; - if (percent > 100 || percent < 0) + if (percent > 100 || percent < 0 || !fHasBattery) percent = 100; if (percent > 0) { rgb_color base; - if (fOnline) { + if (fHasBattery) { if (percent <= 15) base.set_to(180, 0, 0); else base.set_to(20, 180, 0); } else { base = HighColor(); - percent = 100; } rect.InsetBy(gap, gap); rect.right = rect.left + rect.Width() * percent / 100.0; - if (be_control_look != NULL) + if (be_control_look != NULL) { be_control_look->DrawButtonBackground(this, rect, rect, base, - fOnline ? 0 : BControlLook::B_DISABLED); - //if (!) - - else + fHasBattery ? 0 : BControlLook::B_DISABLED); + } else FillRect(rect); } @@ -343,11 +340,15 @@ PowerStatusView::Update(bool force) fPercent = (100 * fBatteryInfo.capacity) / fBatteryInfo.full_capacity; fTimeLeft = fBatteryInfo.time_left; - if (fBatteryInfo.state & BATTERY_CHARGING) + if ((fBatteryInfo.state & BATTERY_CHARGING) != 0) fOnline = true; else fOnline = false; + // TODO: if critical really means that, its name should be changed... + fHasBattery = (fBatteryInfo.state & BATTERY_CRITICAL_STATE) == 0 + && fPercent >= 0; + if (fInDeskbar) { // make sure the tray icon is large enough float width = fShowStatusIcon ? kMinIconWidth + 2 : 0; @@ -358,6 +359,15 @@ PowerStatusView::Update(bool force) if (text[0]) width += ceilf(StringWidth(text)) + 4; + } else { + char text[256]; + if (fHasBattery) { + snprintf(text, sizeof(text), "%ld%%\n%ld:%02ld\n%s", + fPercent, fTimeLeft / 3600, (fTimeLeft / 60) % 60, + fOnline ? "charging" : "discharging"); + } else + strcpy(text, "no battery"); + SetToolTip(text); } if (width == 0) { // make sure we're not going away completely diff --git a/src/apps/powerstatus/PowerStatusView.h b/src/apps/powerstatus/PowerStatusView.h index 9b789cec3e..c5715b706e 100644 --- a/src/apps/powerstatus/PowerStatusView.h +++ b/src/apps/powerstatus/PowerStatusView.h @@ -60,6 +60,7 @@ protected: int32 fPercent; time_t fTimeLeft; bool fOnline; + bool fHasBattery; BSize fPreferredSize; };