PowerMonitor.h 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #pragma once
  2. #include <atomic>
  3. #include <filesystem>
  4. #include <optional>
  5. #include <thread>
  6. namespace WallpaperEngine::Desktop {
  7. // a system battery is discharging and no external supply is online, nullopt without a system battery
  8. std::optional<bool> sysfsOnBattery (const std::filesystem::path& powerSupplies);
  9. /**
  10. * Battery (UPower, else sysfs) and idle state (ext-idle-notify-v1, MIT-SCREEN-SAVER or Mutter's IdleMonitor), polled
  11. * on its own thread so a slow bus never stalls a frame
  12. */
  13. class PowerMonitor {
  14. public:
  15. PowerMonitor ();
  16. ~PowerMonitor ();
  17. PowerMonitor (const PowerMonitor&) = delete;
  18. PowerMonitor& operator= (const PowerMonitor&) = delete;
  19. [[nodiscard]] bool onBattery () const { return this->m_onBattery; }
  20. [[nodiscard]] bool idle () const { return this->m_idle; }
  21. /** 0 turns idle detection off */
  22. void setIdleTimeout (int minutes);
  23. struct Sources;
  24. private:
  25. void run ();
  26. void wake () const;
  27. std::atomic<bool> m_onBattery = false;
  28. std::atomic<bool> m_idle = false;
  29. std::atomic<int> m_idleMinutes = 0;
  30. std::atomic<bool> m_stop = false;
  31. int m_wakeFd = -1;
  32. Sources* m_sources = nullptr;
  33. std::thread m_thread;
  34. };
  35. } // namespace WallpaperEngine::Desktop