2

I'm working on a Qt application and would like to monitor the battery status (charging, percentage etc). There are files like status, uevent in /sys/class/power_supply/battery directory which have all the information I need. These files are getting updated on connecting / disconnecting the charger. I tried using QFileSystemWatcher class which didn't work as those files' timestamps are not changing with their content. Any other way of monitoring the battery status?

I could use a timer / separate thread to do this but would like to explore other options available through the OS (Debian on Arm).

ramtheconqueror
  • 1,907
  • 1
  • 22
  • 35
  • 1
    See [this question](http://stackoverflow.com/q/22803469/1329652) or [that question](http://stackoverflow.com/q/23146915/1329652). The `uevent` interface is what you need, I'd think. – Kuba hasn't forgotten Monica Jul 13 '16 at 19:30
  • 1
    `QFileSystemWatcher` relies on inotify on Linux, which [is not supported for `sysfs` and `procfs`](http://comments.gmane.org/gmane.linux.file-systems/83641). You'd need to `select` or `poll` on the appropriate files to get the behaviour you want. – Daniel Kamil Kozar Jul 13 '16 at 19:31
  • You could also take a look at D-Bus, there is a lot of messages in it - maybe it will help you. – Adam Jul 13 '16 at 19:35
  • @Adam Thanks, could you please show some examples? Quick googling didn't show anything useful. – ramtheconqueror Jul 13 '16 at 20:48

1 Answers1

3

You could also take a look at D-Bus as I have said in comment.

Specially you should look at:

  • signal Changed() - it will notify you when something change [3]

and properties [2]

  • Energy
  • EnergyEmpty
  • EnergyFull

QT5 supports D-Bus [4] [5].

Shell command to test:

dbus-send --print-reply \
          --system \
          --dest=org.freedesktop.UPower \
          /org/freedesktop/UPower/devices/battery_BAT0 \ # change BAT0 to proper value - you can check it with d-feet [6]
          org.freedesktop.DBus.Properties.GetAll \
          string:org.freedesktop.UPower.Device

d-feet

  1. https://upower.freedesktop.org/docs/UPower.html
  2. https://upower.freedesktop.org/docs/Device.html
  3. https://upower.freedesktop.org/docs/Device.html#Device::Changed
  4. http://doc.qt.io/qt-5/qtdbus-index.html
  5. http://doc.qt.io/qt-5/examples-dbus.html
  6. https://wiki.gnome.org/action/show/Apps/DFeet?action=show&redirect=DFeet
Adam
  • 639
  • 9
  • 22
  • It seems like signals are not emitted for the battery or line power objects. This got me 90% of the way there, but in the end, you still have to poll on a timer, meaning it is no better than checking the files as the question asker described. – Tozar Feb 02 '18 at 23:08