diff options
author | Martin Pitt <martin.pitt@ubuntu.com> | 2011-04-20 00:03:45 +0200 |
---|---|---|
committer | Martin Pitt <martin.pitt@ubuntu.com> | 2011-04-26 11:50:36 +0200 |
commit | 5cb1f4a9a33d3afac1d2c0d49e0def486f9c3557 (patch) | |
tree | 2c5f371e3b69b9710ecdca339bd5d059681958e1 | |
parent | a786d2fd16b8ea474674a99acecc1e5e026ac609 (diff) |
Consider a discharging UPS as "on battery"
Implement get_{on,low}_battery() methods on HID devices for Linux. This will
treat discharging UPSes like batteries and set the "on-battery" and
"on-low-battery" properties accordingly.
This also fixes the test_ups_ac() case in the Linux integration tests.
-rw-r--r-- | src/linux/up-device-hid.c | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/src/linux/up-device-hid.c b/src/linux/up-device-hid.c index 0172930..7f9f20e 100644 --- a/src/linux/up-device-hid.c +++ b/src/linux/up-device-hid.c @@ -435,6 +435,67 @@ out: } /** + * up_device_hid_get_on_battery: + **/ +static gboolean +up_device_hid_get_on_battery (UpDevice *device, gboolean *on_battery) +{ + UpDeviceHid *hid = UP_DEVICE_HID (device); + UpDeviceKind type; + UpDeviceState state; + gboolean is_present; + g_return_val_if_fail (UP_IS_DEVICE_HID (hid), FALSE); + g_return_val_if_fail (on_battery != NULL, FALSE); + + g_object_get (device, + "type", &type, + "state", &state, + "is-present", &is_present, + NULL); + + + if (type != UP_DEVICE_KIND_UPS) + return FALSE; + if (state == UP_DEVICE_STATE_UNKNOWN) + return FALSE; + if (!is_present) + return FALSE; + + *on_battery = (state == UP_DEVICE_STATE_DISCHARGING); + return TRUE; +} + +/** + * up_device_hid_get_low_battery: + **/ +static gboolean +up_device_hid_get_low_battery (UpDevice *device, gboolean *low_battery) +{ + gboolean ret; + gboolean on_battery; + UpDeviceHid *hid = UP_DEVICE_HID (device); + gdouble percentage; + + g_return_val_if_fail (UP_IS_DEVICE_HID (hid), FALSE); + g_return_val_if_fail (low_battery != NULL, FALSE); + + /* reuse the common checks */ + ret = up_device_hid_get_on_battery (device, &on_battery); + if (!ret) + return FALSE; + + /* shortcut */ + if (!on_battery) { + *low_battery = FALSE; + return TRUE; + } + + g_object_get (device, "percentage", &percentage, NULL); + *low_battery = (percentage < 10.0f); + return TRUE; +} + +/** * up_device_hid_init: **/ static void @@ -482,6 +543,8 @@ up_device_hid_class_init (UpDeviceHidClass *klass) object_class->finalize = up_device_hid_finalize; device_class->coldplug = up_device_hid_coldplug; + device_class->get_on_battery = up_device_hid_get_on_battery; + device_class->get_low_battery = up_device_hid_get_low_battery; device_class->refresh = up_device_hid_refresh; g_type_class_add_private (klass, sizeof (UpDeviceHidPrivate)); |