BatteryMeter.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. htop - BatteryMeter.c
  3. (C) 2004-2011 Hisham H. Muhammad
  4. Released under the GNU GPLv2+, see the COPYING file
  5. in the source distribution for its full text.
  6. This meter written by Ian P. Hands (iphands@gmail.com, ihands@redhat.com).
  7. */
  8. #include "config.h" // IWYU pragma: keep
  9. #include "BatteryMeter.h"
  10. #include <math.h>
  11. #include "CRT.h"
  12. #include "Macros.h"
  13. #include "Object.h"
  14. #include "Platform.h"
  15. #include "XUtils.h"
  16. static const int BatteryMeter_attributes[] = {
  17. BATTERY
  18. };
  19. static void BatteryMeter_updateValues(Meter* this) {
  20. ACPresence isOnAC;
  21. double percent;
  22. Platform_getBattery(&percent, &isOnAC);
  23. if (!isNonnegative(percent)) {
  24. this->values[0] = NAN;
  25. xSnprintf(this->txtBuffer, sizeof(this->txtBuffer), "N/A");
  26. return;
  27. }
  28. this->values[0] = percent;
  29. const char* text;
  30. switch (isOnAC) {
  31. case AC_PRESENT:
  32. text = this->mode == TEXT_METERMODE ? " (Running on A/C)" : "(A/C)";
  33. break;
  34. case AC_ABSENT:
  35. text = this->mode == TEXT_METERMODE ? " (Running on battery)" : "(bat)";
  36. break;
  37. case AC_ERROR:
  38. default:
  39. text = "";
  40. break;
  41. }
  42. xSnprintf(this->txtBuffer, sizeof(this->txtBuffer), "%.1f%%%s", percent, text);
  43. }
  44. const MeterClass BatteryMeter_class = {
  45. .super = {
  46. .extends = Class(Meter),
  47. .delete = Meter_delete
  48. },
  49. .updateValues = BatteryMeter_updateValues,
  50. .defaultMode = TEXT_METERMODE,
  51. .supportedModes = METERMODE_DEFAULT_SUPPORTED,
  52. .maxItems = 1,
  53. .total = 100.0,
  54. .attributes = BatteryMeter_attributes,
  55. .name = "Battery",
  56. .uiName = "Battery",
  57. .caption = "Battery: "
  58. };