Compare commits

...

157 commits

Author SHA1 Message Date
Sicelo A. Mhlongo
f6e63cfc40 upower: deprecate luminosity property
Since d2053eff ("linux: Remove user-space support for Logitech Unifying
devices"), the luminosity property is no longer in use and no code for it
exists except in the lib. Deprecate and eventually drop it.
2025-12-17 16:46:28 +08:00
Kate Hsuan
0f57b6bf73 Drop the incorrect "since version" and mark the capacity_level property as deprecated
Fix: 09ada72 (dbus: org.freedesktop.UPower.Device: deprecate CapacityLevel property)
2025-12-17 16:19:55 +08:00
Kate Hsuan
38ab5417b7 linux: integration-test: test for the battery recalibration
Test case for ignoring the CriticalPowerAction when performing the battery
recalibration.
2025-12-15 14:19:13 +08:00
Kate Hsuan
d9f42cfc9d up-daemon: Report UP_DEVICE_LEVEL_NONE when battery is performing recalibration
If the battery is performing recalibration and AC is online,
UP_DEVICE_LEVEL_NONE is considered.
2025-12-15 14:19:13 +08:00
Kate Hsuan
4722fcfaac up-config: introduce the ExpectBatteryReCalibration for battery recalibration
ExpectBatteryReCalibration allows the user to ignore the
CriticalPowerAction when performing the battery recalibration.
2025-12-15 14:19:13 +08:00
Kate Hsuan
1ba5f3a55e linux: up-device-supply-battery: Fix assigning an error to a non-NULL GError pointer
As some systems only allow for one charge threshold setting, the overall
process is treated as a success as long as one of the values is set
correctly. We only treat it as an error (and return FALSE) if both the
start and end threshold settings fail. For this reason, we don't need
to track specific errors for every write, so the GError parameter in
g_file_set_contents_full() is set to NULL.

Fedora upowerd[1326]: g_file_set_contents_full: assertion 'error == NULL || *error == NULL' failed
Fedora upowerd[1326]: GError set over the top of a previous GError or uninitialized memory.
                      This indicates a bug in someone's code. You must ensure an error is NULL before it's set.
                      The overwriting error message was: Failed to set charge control thresholds

Resolves: #331
2025-12-12 20:14:04 +08:00
Kate Hsuan
c5df437cc1 linux: up-device-supply: fix the resource leak
1. free the udev parent node automatically.
2. free new_model_name variable in the same scope.
2025-11-28 15:00:22 +08:00
Kate Hsuan
d79841fe40 meson: Release 1.91.0
The major change includes:
1. Support multiple keyboard backlight LED control.
2025-11-25 16:39:56 +08:00
Kate Hsuan
a4d0d469f3 meson: so relase bump up due to recent property changes 2025-11-25 15:25:06 +08:00
Kate Hsuan
09ada726f7 dbus: org.freedesktop.UPower.Device: deprecate CapacityLevel property
This property is duplicated from BatteryLevel and confuses the user.
2025-11-25 14:33:55 +08:00
Kate Hsuan
391e3323f0 etc: UPower.conf.d: add UPower.conf.d
The configuration files in the UPower.conf.d will override the primary
configuration.
2025-11-12 15:46:00 +08:00
Manuel A. Fernandez Montecelo
46bbc8a602 Add support for conf.d style dirs (UPower.conf.d)
This change adds the feature to read config from conf.d style
directories (UPower.conf.d), commonly supported by other tools, as an
extension of the main config file.

This is useful and convenient in several situations, for example:

- distributions can set different values from the defaults shipped
  upstream without having to modify the main UPower.conf

- different packages or config-management tools can change config just
  by adding, removing or modifying files in that directory

The main config file, e.g. '/etc/UPower/UPower.conf', will be
processed first, and then files in the UPower.conf.d dir, if existing.

The directory to use is derived automatically, e.g.
'/etc/UPower/UPower.conf.d/' if the main config file is
'/etc/UPower/UPower.conf'.  Only files within that directory are
considered, and only those with valid config-group 'UPower' and with
the filename format: starting with '00-' to '99-', ending in '.conf'
and with alphanumeric characters, dash or underscore in between.

The candidate files within the given directory are sorted (with
g_strcmp0(), so the ordering will be as with strcmp()).  The
configuration in the files being processed later will override
previous config, in particular the main config, but also the one from
previous files processed, if the Group and Key coincide.

Add also relevant integration test: 'test_conf_d_support'
2025-11-12 15:45:56 +08:00
Luca Boccassi
5f572ffd9a polkit: fix config syntax
These fields do not start with an underscore. polkitd complains
about them:

 polkitd[2017783]: skipping unknown tag <_description> at line 18
 polkitd[2017783]: skipping unknown tag <_message> at line 19
2025-10-21 16:59:38 +08:00
Kate Hsuan
9c8769d7df ci: fix pre-commit test
Resolve the libatomic dependency issue.
2025-10-21 16:51:40 +08:00
Kate Hsuan
8ce78b7f9e meson_options: replace deprecated boolean variable 2025-09-17 12:27:42 +08:00
Kate Hsuan
f26409e925 rules: 95-upower-hid: update hwdb from upstream NUT
Update to the latest hwdb from NUT.

Link: https://networkupstools.org
Link: https://raw.githubusercontent.com/networkupstools/nut/master/scripts/upower/95-upower-hid.hwdb
2025-09-16 15:51:36 +08:00
Luciano Santos
5bf9cac60d build: Make installation of tests optional
Default to installing them, while letting people/distributors decide
what they want.
2025-09-16 15:25:49 +08:00
Sicelo A. Mhlongo
84aab7aefa linux: up-enumerator-udev: also permit lp5523:kb name
While common keyboard backlight devices use 'kbd_backlight' in the name, per
linux kernel include/dt-bindings/leds/common.h, there are a few legacy names,
including 'lp5523:kbN'. They cannot be easily changed now because it would
break userspace, hence the names are still provided in the kernel.  Support
them in upower as well. The lp5523 as used on the Nokia N900 exposes six
independent keyboard backlight LEDs, and UPower is able to manage them with
this patch in place.
2025-09-05 17:22:08 +08:00
Kate Hsuan
afb346cf77 linux: integration-test: Multiple keyboard backlight LED support
The integration test for multiple keyboard backlight support.
2025-09-05 17:22:08 +08:00
Kate Hsuan
e0baa3fdcb up-daemon: The implementation of EnumerateKbdBacklight
The implementation for enumerating all keyboard backlight LED objects
on the system.
2025-09-05 17:22:08 +08:00
Kate Hsuan
d316a111f7 dbus: org.freedesktop.UPower: Enumerate kbd backlight LEDs
Enumerate all keyboard backlight LED objects on the system.
2025-09-05 17:22:08 +08:00
Kate Hsuan
4ba78c54a9 linux: up-enumerator-udev: enumerate the LED subsystem
The LED udev subsystem was added to the list to watch the
keyboard backlight LED device events.
2025-09-05 17:22:08 +08:00
Kate Hsuan
6bfc3d6100 up-daemon: device-added and device-removed keyboard backlight device
The keyboard backlight device will be added to or removed from DeviceList
when receiving the "device-added" and "device-removed" signal.
2025-09-05 17:22:08 +08:00
Kate Hsuan
796ce9e579 up-daemon: DeviceList for keyboard backlight LED devices
The DeviceList was created to store the keyboard backlight LED devices.
2025-09-05 17:22:08 +08:00
Kate Hsuan
fd6eebb397 linux: up-enumerator-udev: Process add, change, and remove udev events for keyboard backlight device
Add, update, and remove the keyboard backlight LED device when receiving
add, change, and remove udev events.
2025-09-05 17:18:17 +08:00
Kate Hsuan
e01aa7c903 linux: up-kbd-backlight-led: the keyboard backlight control implementation for Linux
The implementation for Linux included:
1. Set and get brightness.
2. Get max brightness.
3. Signal if the brightness was changed by the device.
2025-09-05 16:47:11 +08:00
Kate Hsuan
1d40bc2027 linux: up-backend: Add and remove keyboard backlight LED device
These are the callback functions for the signals "device-added" and
"device-removed" to add and remove keyboard backlight devices.
2025-09-05 11:49:38 +08:00
Kate Hsuan
9f9b8478bd up-device-list: Introduce the UpDeviceKbdBacklight object
This commit makes UpDeviceList store the UpDeviceKbdBacklight objects and
sets the udev native path property of UpDevice and UpDeviceKbdBacklight.
2025-09-05 11:49:38 +08:00
Kate Hsuan
aaf52de8dc up-device-kbd-backlight: The parent class for keyboard backlight control
This parent class can be used for multiple implementations, such as freebsd
and Linux. The child class inherits it to register the device to upower
and access the dbus services.
2025-09-05 11:49:38 +08:00
Kate Hsuan
0dcb46475f dbus: org.freedesktop.UPower.KbdBacklight: Description for deprecating the KbdBacklight object path
The object path /org/freedesktop/UPower/KbdBacklight is going to be deprecated in the future.
The upper-layer app should migrate to the new API instead.
2025-09-05 11:49:38 +08:00
Kate Hsuan
40a4498454 dbus: KbdBacklight: Add a native path attribute
The DBus property shows the udev native path.
2025-09-05 11:49:38 +08:00
Kate Hsuan
c56ff5f207 Release 1.90.10
- Fix wrong model name of the devices (!267, #309)
- Switch charge_types to "Custom" when charging threshold is enabled (!268, #275)
- Fix invalid command line arguments (!269)
- Fix leak when reporting daemon usage error (!270)
- OpenBSD: support battery status from qcpas (!272)
- Fix history progression (!274, #316)
- Add a battery filter to the upower command line (!275)
- Change the charging behaviors using charge_types (!276, !46, #275)
- Fix integration tests issues, including floating point value and race between umockdev and upower (!277, !278)
- Rework upower command (!280)
- Propagate charge-threshold-enabled to display device (!281)
2025-08-26 16:04:24 +08:00
Josh Triplett
3369d31472 upower: Add a -b/--battery option to dump only battery information
This is useful when asking people to give information about battery
health. It avoids having to give two-step instructions of first
enumerating devices with `upower -e`, then looking for a device whose
name starts with `/org/freedesktop/UPower/devices/battery_`, then
running `upower -i` on that device.

Signed-off-by: Josh Triplett <josh@joshtriplett.org>
2025-08-26 13:29:52 +08:00
Florian Müllner
ff605e51c1
up-daemon: Propagate charge-threshold-enabled to display device
The property indicates that a device is plugged-in but not charging
because of the charging threshold. Desktops may want to indicate
that state, but the property is currently only exposed on actual
devices, not the virtual display device that is usually exposed
in the interface.

To address this, propagate the property to the display device
if at least one device has charge thresholds enabled.

Related: https://gitlab.gnome.org/GNOME/gnome-shell/-/issues/5228
2025-08-21 14:30:45 +02:00
Kate Hsuan
4d6da1bb0f tools: up-tool: rework upower dump and enumerate
1. Move dump and enumerate to the separated functions.
2. Introduce an output filter.

Co-work-with: Cursor
Reviewed-by: Kate Hsuan <hpa@redhat.com>
2025-08-20 14:25:37 +08:00
Kate Hsuan
e1d25fa549 linux: integration-test: The type of percentage property is double
The data type of the "Percentage" property is double, so the value is a
floating point number, ex, 100.0
2025-08-12 16:10:52 +08:00
Michael Biebl
12a165e153 linux: integration-test: fix comparison of floating point values
Use assertAlmostEqual for all values that are floating point numbers:

https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertAlmostEqual

This avoids issues with excessive precision on i386.
More information https://wiki.debian.org/ArchitectureSpecificsMemo#i386

See also the corresponding downstream bug report
https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1050436

The patch was generated using:

for i in Capacity Energy EnergyEmpty EnergyFull EnergyFullDesign EnergyRate Luminosity Percentage Temperature Voltage ; do
    sed -i -r "s/(assertEqual)(.*$i)/assertAlmostEqual\2/g" src/linux/integration-test.py
done

Fixes: #251
2025-08-12 15:46:23 +08:00
Kate Hsuan
903a8278f8 linux: integration-test: the settings discovery for charge threshold
The test case for settings discovery for charge threshold.
The test cases include:
1. only charge_types (4)
2. charge_control_start_threshold + charge_control_end_threshold +
   charge_types (7)
3. charge_control_end_threshold + charge_types (6)
2025-08-11 15:05:10 +08:00
Kate Hsuan
3a2fe524d0 linux: up-device-supply-battery: Discover the settings of charge threshold
This commit discovers the settings, including charge_control_start_threshold,
charge_control_end_threshold, and charge_types and update the results to
the battery information.
2025-08-11 15:05:10 +08:00
Kate Hsuan
1bf86b5483 up-device-battery: set up charge-threshold-settings-supported property
Setup and update the charge-threshold-settings-supported property.
2025-08-11 15:05:10 +08:00
Kate Hsuan
1c7b9d8bae dbus: org.freedesktop.UPower.Device: the ChargeThresholdSettingsSupported property
The types of settings for charge thresholds that are supported. The value
is a bitmask value and it is the sum of the supported types.

1: The system supports charge start threshold. The battery discharges to a
   percentage, then starts charging.
2: The system supports charge end threshold. The battery charges to a
   percentage, then stops charging.
4: The system supports optimized charging behaviors controlled by the
   system firmware.

Examples:

If the system supports charge start threshold and charge end threshold,
the value is 3.

If the system supports charge end threshold and the battery charging
behavior is controlled by the system firmware, the value is 6.
2025-08-11 15:05:10 +08:00
Kate Hsuan
2afc3c62ca linux: integration-test: Switch between Long_Life, Fast, Standard, and Adaptive
Charge_types has to be "Long_Life" if charging threshold is enabled.
Charge_types has to be "Fast", "Standard", or "Adaptive" if charging
threshold is disabled.
2025-08-11 15:05:04 +08:00
Kate Hsuan
33efe441f6 linux: up-device-supply-battery: Set charge_types to enable/disable the charge threshold
If the charge threshold is enabled, the charge_types set "Long_Life". If
the charge threshold is disabled, the charge_types set to any of
charge_types for charging, such as "Fast", "Standard", and "Adaptive".
This commit is only for the system that only has the charge_types attribute
to control the battery behaviour.
2025-08-07 14:06:06 +08:00
Kate Hsuan
a97c3ceaf5 linux: up-device-supply-battery: Using charge_types Long_Life and Standard to manage charging threshold
We found some systems, such as Lenovo ideapad that don't support
charge_control_start_threshold  and charge_control_end_threshold but it
provide the charge_types Long_Life and Standard. The chatre_types
Long_Life operate the battery in the conservation mode. That means
if Long_Life and Standard are found, the system supports the
charge threshold feature.

Co-work-with: Cursor
Reviewed-by: Kate Hsuan <hpa@redhat.com>
2025-08-07 14:06:06 +08:00
Kate Hsuan
0e6493ce22 linux: up-device-supply-battery: Set charge_types through an enum
The charge enum is used to set the charge_types and namespacing the
emum to prevent varaible name conflicts.
2025-08-07 14:06:06 +08:00
Kate Hsuan
e8fa017be4 linux: integration-test: reduce the test failure for the slow system
On a slow system like riscv64, the test_daemon_version and
test_daemon_restart tests sometimes failed. A delay was added after the
daemon starts to give it enough time to fully initialize, which has
reduced the chance of these tests failing.
2025-08-07 12:38:18 +08:00
Kate Hsuan
fe36d9ee4a linux: integration-test: fix random test failure for test_bluetooth_hidpp_mouse
Sometime we found the errors shows below when pipeline test was running.

379s ERROR: test_bluetooth_hidpp_mouse (__main__.Tests.test_bluetooth_hidpp_mouse)
379s Logitech Bluetooth LE mouse with HID++ kernel support
379s ----------------------------------------------------------------------
379s Traceback (most recent call last):
379s   File "/usr/libexec/upower/integration-test.py", line 4380, in test_bluetooth_hidpp_mouse
379s     self.assertEventually(
379s     ~~~~~~~~~~~~~~~~~~~~~^
379s         lambda: self.get_dbus_dev_property(bat0_up, "Model"), value=alias
379s         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
379s     )
379s     ^
379s   File "/usr/libexec/upower/integration-test.py", line 399, in assertEventually
379s     if condition() == value:
379s        ~~~~~~~~~^^
379s   File "/usr/libexec/upower/integration-test.py", line 4381, in <lambda>
379s     lambda: self.get_dbus_dev_property(bat0_up, "Model"), value=alias
379s             ~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^
379s   File "/usr/libexec/upower/integration-test.py", line 316, in get_dbus_dev_property
379s     return self.dbus.call_sync(
379s            ~~~~~~~~~~~~~~~~~~~^
379s         UP,
379s         ^^^
379s     ...<7 lines>...
379s         None,
379s         ^^^^^
379s     ).unpack()[0]
379s     ^
379s gi.repository.GLib.GError: g-dbus-error-quark: GDBus.Error:org.freedesktop.DBus.Error.UnknownMethod: Object does not exist at path “/org/freedesktop/UPower/devices/mouse_dev_11_22_33_44_AA_BB” (19)
379s

The upower and bluez daemons were started after the udev was
initialized to avoid the random errors.
2025-08-07 12:34:35 +08:00
Kate Hsuan
bdb05da4a1 linux: integration-test: Switch the charge_types between Custom and Fast with charging threshold status
The charge_types is set to "Custom" when the charging threshold is
enabled. If the charging threshold is disabled, it will be reset
to "Fast".
2025-07-23 11:06:45 +08:00
Kate Hsuan
559b4ac208 linux: up-device-supply-battery: Set charge_types to "Custom" when enabling charging threshold
If the charge_types isn't set to "Custom" on a Dell laptop, the charging
threshold doesn't work. This commit sets the charge_types to "Custom" if
the charging threshold is enabled. Also, the charge_types will be set to
"Fast", "Standard", or "Adaptive" when the charging threshold is disabled.

This commit reads and writes the battery udev attribute "charge_types".
However, this attribute can't be found on some of the systems, so the error
on reading and writing charge_types attribute is ignored.
2025-07-23 11:06:38 +08:00
Fabrice Bellet
e7893f4624 up-history: reverse preset progression 2025-06-25 17:04:48 +08:00
Landry Breuil
fd4aaf3c65 openbsd: add support to fetch battery status from qcpas 2025-06-05 17:03:02 +08:00
Ula Shipman
2724566a2a tools: Reorder upower(1) command-line options to match manual page 2025-06-04 10:45:49 -07:00
Ula Shipman
9efac77ae7 docs: Document all upower(1) command-line options 2025-06-04 10:45:41 -07:00
Ula Shipman
ec6631a11e tools: Handle invalid command-line arguments 2025-05-29 13:53:09 +08:00
Ula Shipman
8f870fc3d2 daemon: Fix memory leak when reporting usage error 2025-05-27 19:20:53 -07:00
Kate Hsuan
a2bf0200f2 linux: integration-test: Two mice were paired with a unifying receiver
The model name of the two mice should be displayed when paired with one
unifying receiver.
2025-05-13 11:08:48 +08:00
Kate Hsuan
38317a118d linux: up-enumerator-udev: Prevent reading the sysfs path from a non-GUdevDevice object
A non-GUdevDevice object may be included in the sibling device list,
since the udev information was created slowly. This object triggers
errors when upower tries to get udev information. Therefore,
get_latest_udev_device() returns NULL, when it receives a non-GUdevDevice
object.

The error messages are shown as follows:
(upowerd:2026931): GUdev-CRITICAL **: 10:38:29.775: g_udev_device_get_sysfs_path: assertion 'G_UDEV_IS_DEVICE (device)' failed

(upowerd:2026931): GUdev-CRITICAL **: 10:38:29.775: g_udev_client_query_by_sysfs_path: assertion 'sysfs_path != NULL' failed
2025-05-13 11:06:24 +08:00
Kate Hsuan
7ae19b529f linux: up-device-supply: Only update model name when the device is under the same parent
f17d3bf fixed the unstable joypad model name but led the hid++ devices
to show the incorrect model name if more than one device is paired to a
unifying receiver. The patch only updates the model name when the device
is under the same parent.

Resolves: #309
2025-04-30 15:44:07 +08:00
Sicelo A. Mhlongo
0f1506b61a linux: integration-test: Test exporting sysfs attributes
Add test for DBus exporting of the following sysfs attributes:
1. capacity_level
2. voltage_min_design
3. voltage_max_design

Related: #301

Signed-off-by: Sicelo A. Mhlongo <absicsz@gmail.com>
2025-04-16 15:08:48 +08:00
Sicelo A. Mhlongo
240fa30ab5 lib: up-device: Expose CapacityLevel, VoltageMinDesign, and VoltageMaxDesign
For supplies that report the voltage_min_design, voltage_max_design, and
capacity_level attributes on sysfs, expose them via libupower for possible
use by clients.

Related: #301

Signed-off-by: Sicelo A. Mhlongo <absicsz@gmail.com>
2025-04-16 15:08:39 +08:00
Sicelo A. Mhlongo
c4b32387b0 linux: up-device-supply-battery: read values of capacity_level and voltage_min/max
For Linux systems that provide these attributes, extend the implementation by
reading the values for the design maximum and minimum voltage for the battery,
as well as the coarse capacity level reported by the kernel.

Related: #301

Signed-off-by: Sicelo A. Mhlongo <absicsz@gmail.com>
2025-04-16 14:39:47 +08:00
Sicelo A. Mhlongo
1decdd53f4 up-device-battery: Define capacity-level, voltage-min-design and voltage-max-design properties
Extend the parent class of the battery to support reporting the design maximum
and minimum voltage, as well as the kernel-derived capacity level.

Related: #301

Signed-off-by: Sicelo A. Mhlongo <absicsz@gmail.com>
2025-04-16 14:39:02 +08:00
Sicelo A. Mhlongo
9ee3a36a6e dbus: Define CapacityLevel, VoltageMinDesign and VoltageMaxDesign properties
Some drivers provide capacity_level, voltage_min_design, and voltage_max_design
properties on sysfs even for system supplies. Expose these properties over DBus
for consumption by clients.

Related: #301

Signed-off-by: Sicelo A. Mhlongo <absicsz@gmail.com>
2025-04-16 14:38:56 +08:00
Kate Hsuan
89c6d2726e linux: integration_test: Wait 5 seconds for bluez to set up the device information 2025-04-11 15:33:31 +08:00
Kate Hsuan
c9ee32e5d5 src: meson: extend the timeout for test_battery_state_guessing
Wait longer for the state guessing. It runs much slower on the CI than on
a local host.
2025-04-11 15:02:14 +08:00
Kate Hsuan
bdbd99b809 Release 1.90.9
- Fix unstable OnBattery status (#306 #307)
2025-04-10 14:35:54 +08:00
initramfs
ebcefcea55
linux: up-device-supply-battery: Don't mark fully charged batteries as discharging
This commit addresses the changes from commit d7ff457784
by excluding fully charged batteries even if their current_now is
negative.
2025-04-09 14:37:26 +00:00
initramfs
a86d2d059c
linux: integration-test: Update test case for "state_all_discharging" support
Updates the "test_battery_ac" test case with the changes introduced by
the addition of the "state_all_discharging" field.

Namely, we now assert that the "OnBattery" property is False while
still checking that the correct "WarningLevel" property is set.
2025-04-09 00:58:28 +00:00
initramfs
485ca6f20e
up-daemon: introduce state_all_discharging member to allow critical action dispatch on external power
In order to handle a situation where external power is connected but
all batteries are still discharging, introduce a new field
"state_all_discharging" that tracks if all batteries are currently
discharging irrespective of external power availability.

By predicating the warning level check and dispatch on this new
parameter we can ensure critical actions are still dispatched when we
have external power available, but the batteries are still draining.

Resolves: #276
2025-04-09 00:58:27 +00:00
initramfs
51b30dae8b
Revert "up-daemon: Handle the AC is online but the battery is discharging"
The commit being reverted introduces the bug #306 by claiming external
power is not available when the device battery begins to discharge for
any reason.

We fix this issue by reverting to the old behaviour of reporting
external power being available as long as it's online. We then fix
issue #276 via alternative means.

Resolves: #306

This reverts commit 9d754d8b2f.
2025-04-09 00:58:02 +00:00
Kate Hsuan
707af75194 Release 1.90.8
- Fix the incorrect percentage value of a removed battery. (#292) (!246)
- Fix device type for the gaming device. (#296 #295) (!247)
- Determine the battery discharging status when connecting a low-power
  charger. (#276) (!244)
- Fix memory leak when refreshing battery info. (#305) (!261)
- Fix memory leak when getting device id. (!261)
2025-03-31 14:58:25 +08:00
Kate Hsuan
51550b912f up-device: Fix mem leak when get device id
The up_device_get_id() returns a string, and the caller has to free it.
A comment is also left in the code to inform the behavior.

...
<snip>
...
=229301== 1,280 bytes in 10 blocks are definitely lost in loss record 1,837 of 1,852
==229301==    at 0x4849347: realloc (vg_replace_malloc.c:1801)
==229301==    by 0x48E7D6A: g_realloc (gmem.c:171)
==229301==    by 0x4907239: g_string_expand (gstring.c:82)
==229301==    by 0x49072B0: g_string_sized_new (gstring.c:113)
==229301==    by 0x10E287: up_device_get_id (up-device.c:347)
==229301==    by 0x10ED55: up_device_notify (up-device.c:241)
==229301==    by 0x49F67D9: g_closure_invoke (gclosure.c:833)
==229301==    by 0x4A26A1F: signal_emit_unlocked_R.isra.0 (gsignal.c:3720)
==229301==    by 0x4A173E8: signal_emit_valist_unlocked (gsignal.c:3519)
==229301==    by 0x4A17670: g_signal_emit_valist (gsignal.c:3262)
==229301==    by 0x4A17732: g_signal_emit (gsignal.c:3582)
==229301==    by 0x4A02973: g_object_dispatch_properties_changed.lto_priv.0 (gobject.c:1819)
2025-03-28 16:00:59 +08:00
Kate Hsuan
2ebaa01347 linux: up-device-supply-battery: Fix mem leak when refreshing battery info
The AC was plugged and unplugged repeatedly, and hit this leak in
up_device_supply_battery_refresh(). Four variables in UpBatteryInfo
need to be free.

...
<snip>
...
==2825085== 1,152 bytes in 9 blocks are definitely lost in loss record 1,851 of 1,865
==2825085==    at 0x484CE40: realloc (vg_replace_malloc.c:1801)
==2825085==    by 0x4916ADA: g_realloc (gmem.c:171)
==2825085==    by 0x4939E06: g_string_expand (gstring.c:82)
==2825085==    by 0x4939EA1: g_string_sized_new (gstring.c:113)
==2825085==    by 0x11262C: up_device_get_id (up-device.c:347)
==2825085==    by 0x113136: up_device_notify (up-device.c:241)
==2825085==    by 0x4A1B811: g_closure_invoke (gclosure.c:833)
==2825085==    by 0x4A4C0C1: signal_emit_unlocked_R.isra.0 (gsignal.c:3735)
==2825085==    by 0x4A3CCE8: signal_emit_valist_unlocked (gsignal.c:3534)
==2825085==    by 0x4A3CF71: g_signal_emit_valist (gsignal.c:3277)
==2825085==    by 0x4A3D033: g_signal_emit (gsignal.c:3597)
==2825085==    by 0x4A27C75: g_object_dispatch_properties_changed.lto_priv.0 (gobject.c:1827)
==2825085==
==2825085== LEAK SUMMARY:
==2825085==    definitely lost: 3,663 bytes in 333 blocks
==2825085==    indirectly lost: 0 bytes in 0 blocks
==2825085==      possibly lost: 960 bytes in 3 blocks
==2825085==    still reachable: 172,351 bytes in 2,205 blocks
==2825085==         suppressed: 0 bytes in 0 blocks
==2825085== Reachable blocks (those to which a pointer was found) are not shown.
==2825085== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==2825085==
==2825085== For lists of detected and suppressed errors, rerun with: -s
==2825085== ERROR SUMMARY: 20 errors from 20 contexts (suppressed: 0 from 0)

Resolves: #305
2025-03-28 16:00:26 +08:00
Kate Hsuan
df51aef98a rules: 95-upower-hid.hwdb: update hwdb for HID devices
Update HID hwdb.
2025-03-25 17:08:40 +08:00
Kate Hsuan
71e473902f linux: integration-test: Handle connecting the low-power charger
Add a scenario for connecting a low-power charger.
1. The AC is online.
2. The battery is discharging.
3. The percentage drops to 2.5%.
4. The warning level will become UP_DEVICE_LEVEL_CRITICAL.

Signed-off-by: Kate Hsuan <hpa@redhat.com>
2025-02-24 06:13:47 +00:00
Kate Hsuan
d7ff457784 linux: up-device-supply-battery: reset the status to discharging when current_now is negative
For some USB battery solutions, the kernel reports charging when
connecting a charger but in reality, the battery discharges and
current_now is negative. This work reset the battery status to
"Discharging" when current_now is negative and line power is
online.

Resolves: #276

Signed-off-by: Kate Hsuan <hpa@redhat.com>
2025-02-24 06:13:47 +00:00
Kate Hsuan
9d754d8b2f up-daemon: Handle the AC is online but the battery is discharging
If a low-power charger connects to the system, the battery still reports
discharging since the AC charger can't provide enough power to make      
the system runs and charges the battery at the same time. In this situation,
we estimate the warning level to trigger the CriticalPowerAction until
the battery status is changed to charging.

Resolves: #276

Signed-off-by: Kate Hsuan <hpa@redhat.com>
2025-02-24 06:13:47 +00:00
Jelle van der Waa
6f9d84694d linux: rules: fix warnings from udevadm verify
rules/60-upower-battery.rules:5 style: whitespace after comma is expected.
rules/60-upower-battery.rules:5 style: a comma between tokens is expected.
2025-02-22 15:20:12 +01:00
Kate Hsuan
4f1ef04475 linux: integration-test: a test case for Dualshock4 with a headphone jack 2025-02-08 07:58:03 +00:00
Kate Hsuan
f17d3bfcd0 linux: up-device-supply: Update the model name after changing the device type
The model name of a device component may be different. For example,
Dualshock joystick owns "Sony Interactive Entertainment DualSense
Wireless Controller" for the joystick and "Sony Interactive
Entertainment DualSense Wireless Controller Motion Sensors" for the
accelerometer. If the device type is changed based on the device
priority, the corresponding model name have to be changed.

Resolves: #295

Tested-by: Mateus Rodrigues Costa <mateusrodcosta@gmail.com>
Signed-off-by: Kate Hsuan <hpa@redhat.com>
2025-02-08 07:58:03 +00:00
Mateus Rodrigues Costa
351affe12f linux: device_supply: Prioritize Gaming Input
The DualSense and DualShock 4 have 3 main components: the gamepad proper,
a touchpad and motion sensors. They also have an extra component in the
form of a headphone jack, which is only available via BT on the
PlayStation consoles but can be used on other devices via USB.

Commit 00eb31a63c increased the priority of
Gaming Input so the controllers weren't accidentally identified as only
the Touchpad, which works on BT. This commit prioritizes Gaming Input over
Audio Device, fixing an issue that only happens on USB.

Resolves: #296
2025-02-08 07:58:03 +00:00
Sicelo A. Mhlongo
a16c752e85 dbus: UPower.xml: Update GetCriticalAction Values
UPower now supports Suspend and Ignore as possible actions. Update the
documentation accordingly.
2025-02-06 10:46:33 +02:00
Kate Hsuan
478c51e4ea linux: integration-test: test case for the removable battery 2025-01-24 17:39:54 +08:00
Kate Hsuan
3452140a6c src: up-device-battery: Reset values when the battery is not present
If the battery is not present, reset the attribute values.

Resolves: #292
2025-01-24 17:39:37 +08:00
Kate Hsuan
6bd8cd90b3 src: up-daemon: skip the battery if present = 0
If the battery is not present, skip it when estimating the battery
percentage.

Resolves: #292
2025-01-24 17:39:03 +08:00
NorwayFun
eb5a327127 Update LINGUAS 2025-01-17 02:11:22 +00:00
Kate Hsuan
319650c145 pre-commit-config: Remove deprecated default_stages "commit"
The new stage name "pre-commit" was used to update the pre-commit
configuration.
2025-01-13 08:55:18 +00:00
Kate Hsuan
003c251fdc contrib: setup: Install and setup pre-commit 2025-01-13 08:55:18 +00:00
fossdd
3731b03553 Fix test race in test_bluetooth_hidpp_mouse
We ship the proposed patch already since Oct 2024 in Alpine Linux and is
working for us correctly and fixes the test.

Closes https://gitlab.freedesktop.org/upower/upower/-/issues/228

Co-Authored-By: Bastien Nocera <hadess@hadess.net>
2025-01-13 06:28:36 +00:00
Marcus B Spencer
83eecbab65 etc/UPower.conf: fix "riscky" typo
Fix the typo on line 86 where it says "riscky", when it should say
"risky"
2025-01-13 13:30:11 +08:00
Kate Hsuan
08bfb29f37 Bump the version for the next release 2025-01-09 13:55:45 +08:00
Kate Hsuan
70a5026662 Release 1.90.7
- Find the correct parent id for input devices (#268 #286)
- Fix race condition in test_sibling_priority_no_overwrite (!240)
- Show charge-threshold-* status as yes/no (!242)
- Add zsh-completions for CLI (!241)
- Wait 10 seconds for stopping the daemon (#290)
- Added pre-commit test and fixed code spelling, markdown, and code style issues. (!248)
2025-01-09 13:47:51 +08:00
Kate Hsuan
0f9bdf1d1c Ignore commits for tree wide changes 2025-01-08 16:54:58 +08:00
Kate Hsuan
7d64c232dc HACKING.md: Rename HACKING to HACKING.md and fix markdown with markdownlint 2025-01-08 15:08:04 +08:00
Kate Hsuan
99de6c9744 README: Rename to README.md and fix markdown with markdownlint 2025-01-08 14:25:55 +08:00
Kate Hsuan
d11300fac6 ci: Add a pre-commit stage 2025-01-08 14:01:34 +08:00
Kate Hsuan
bab81e5e44 Fix spelling issues with codespell 2025-01-08 14:01:34 +08:00
Kate Hsuan
17e5903670 Reformat python code style with black 2025-01-08 14:01:34 +08:00
Kate Hsuan
2678cb00d4 Fix trailing white space and end of file 2025-01-08 14:01:34 +08:00
Kate Hsuan
52bfd8d924 ci: Add python code style check to the pre-commit test 2025-01-08 14:01:29 +08:00
Kate Hsuan
2b371cc290 Update README.md and HACK to match markdown.lint style 2025-01-08 13:59:50 +08:00
Kate Hsuan
9c3b678d7f ci: markdownlint: Add markdownlint to the pre-commit check 2025-01-08 13:59:50 +08:00
Kate Hsuan
2cfc6f625f ci: Add codespell test to the pre-commit test 2025-01-08 13:59:50 +08:00
Kate Hsuan
9fac8f2ae2 pre-commit-config: Add pre-commit test 2025-01-08 13:59:43 +08:00
Kate Hsuan
1a64550dad rules: 95-upower-hid.hwdb: Update hwdb for HID devices 2024-12-31 13:39:50 +08:00
Kate Hsuan
a038b237fb linux: integration-test: wait 10 seconds for stopping the daemon
Some slow systems take more than 5 seconds to stop the daemon.  The
waiting time was extended to 10 seconds to ensure the daemon
shutdowns properly.

Resolves: #290

Signed-off-by: Kate Hsuan <hpa@redhat.com>
2024-12-06 06:30:03 +00:00
Natalie Clarius
422ac4be74 add completions for CLI 2024-10-26 19:56:28 +00:00
Jelle van der Waa
faa6d8a6b3 dbus: show charge-threshold-* status as yes/no
Previously shown as a boolean integer and not properly aligned.
2024-10-11 16:40:13 +02:00
Kate Hsuan
7d7bb84fde linux: up-enumerator-udev: Find the correct parent id for input devices
The parent id for the devices under "/sys/devices/virtual/misc/uhid/*"
points to /sys/devices/virtual/misc/uhid. It will cause the device
information to be updated incorrectly. For example, the type for a mouse is
updated to "keyboard". This patch makes sure the correct parent id for
each input device.

Resolves: #268 #286
2024-09-27 16:11:29 +08:00
Stuart Hayhurst
9ee76826bd Fix race condition in test_sibling_priority_no_overwrite 2024-09-25 16:56:11 +01:00
Kate Hsuan
94c91f93f1 NEWS: Update release date 2024-09-18 11:25:08 +08:00
Kate Hsuan
7fbc02e8af Release 1.90.6
- Fix g_object_weak_ref: assertion 'g_atomic_int_get (&object->ref_count) >= 1' failed (#281, !233)
- Fractional battery percentage (!226)
- CI enhancement. (!236)
- Tweak the default battery charging threshold to 75-80. (!234)
2024-09-18 02:39:43 +00:00
Jelle van der Waa
3390fa48ae linux: integration-test: correct unsupported test comment 2024-09-17 20:13:50 +02:00
Jelle van der Waa
23cf8c19f7 linux: integration-test: drop f-string without placeholder 2024-09-17 18:41:14 +02:00
Kate Hsuan
131ab3a9d5 up-polkit: Replace with G_DEFINE_TYPE_WITH_PRIVATE
Since no interface was inplemented in UpPolkit class so G_DEFINE_TYPE_WITH_PRIVATE
macro is batter.
2024-09-12 08:11:50 +00:00
Kate Hsuan
7db90b28d8 up-polkit: remove global variable and remove g_object_add_weak_pointer()
Since UpPolkit is only created in the daemon, so the global variable and
g_object_add_weak_pointer() can be removed.
2024-09-12 08:11:50 +00:00
Rogerio Alves
88031e563f Support fractional battery percent.
Make upower supports fractional battery percent
(e.g., 0.5% instead of 1%).

Signed-off-by: Rogerio Alves Cardoso <rogerio.cardoso@collabora.com>
2024-09-12 15:55:00 +08:00
Kate Hsuan
fda3a0b563 ci: Test on Debian system
We run the test on the Debian system to ensure upower can run on
different Linux distributions.
2024-09-12 15:10:15 +08:00
Kate Hsuan
cca76c8684 rules: 60-upower-battery.hwdb: Change the threshold value to 75-80
The CHARGE_LIMIT is set to 75-80 to increase the start threshold value.

Resolves: #282
2024-09-02 14:48:10 +08:00
Jan Alexander Steffens (heftig)
b4697dbc62
up-polkit: Add G_ADD_PRIVATE (UpPolkit)
Without this, accesses to `UpPolkitPrivate` are actually out of bounds
and writing to it will cause heap corruption.

Fixes: https://gitlab.freedesktop.org/upower/upower/-/issues/281
2024-08-31 11:05:54 +02:00
Kate Hsuan
b71996a526 linux: integration-test: Add polkit test
Test action is allowed and not allowed when calling EnableChargeThreshold
dbus API.
2024-08-29 06:31:37 +00:00
Kate Hsuan
f55641cd43 Revert "Remove polkit tests"
This reverts commit bddc559713.

Changes:
- Removed Makefile
- Removed test for UpWakeups
2024-08-29 06:31:37 +00:00
Kate Hsuan
b26c8c79c9 ci: Add polkit dependency 2024-08-29 13:55:02 +08:00
Kate Hsuan
12d1b7ec43 Release 1.90.5
- Suspend and Ignore as the CriticalPowerActions (!218 and !218)
- Support for battery charging threshold (!208)
- Tweak the device priority to make sure the device kind joystick can be correctly shown. (#267)
2024-08-26 09:19:37 +00:00
Kate Hsuan
9449cb5831 dbus: G_MAXUINT means ignore ChargeStartThreshold or ChargeEndThreshold
If the value is G_MAXUINT, it means charge_control_start_threshold or
charge_control_end_threshold will be skipped.
2024-08-23 22:21:52 +08:00
Michael Biebl
d7005f618d Fix typo: deamon -> daemon 2024-08-23 08:30:22 +02:00
Kate Hsuan
399ec5d1ec linux: up-device-supply-battery: Don't report error when one of the start/stop charge threshold isn't writable
One of the charge_control_start|end_threshold isn't writable for some
systems, for example, Macbook Air. Therefore, if both files can't be
written, the error is returned.
2024-08-22 14:20:33 +08:00
Kate Hsuan
5882721f19 linux: up-device-supply-battery: Allow skipping start or end charging threshold setting
This allows the one of charge_control_start|end_threshold can be ignored
by a "_" character.
The udev hwdb can be set in the following format.

- Skip charging start limit
CHARGE_LIMIT=_,80
- Skip charging end limit
CHARGE_LIMIT=30,_
2024-08-22 14:20:33 +08:00
Kate Hsuan
4e8e8b43f8 daemon: Get and store env override configurations in the deamon 2024-08-22 14:20:33 +08:00
Kate Hsuan
b604a297be meson: Enable/disable polkit through an option
The user can enable and disable polkit using -Dpolkit.
2024-08-22 14:20:33 +08:00
Kate Hsuan
bfde9fefb7 build: Enbale/disable polkit based on HAVE_POLKIT 2024-08-22 14:20:33 +08:00
Kate Hsuan
7011b93c28 Revert "policy: Remove policy for removed features"
This reverts commit ababfa2aaf.

Changes:
1. Drop the policy for the old APIs.
2. Add a policy for enableChargeLimit.
2024-08-22 14:20:33 +08:00
Kate Hsuan
293e00e0f9 up-device-battery: Test the user authorization before setting the charging threshold 2024-08-22 14:20:33 +08:00
Kate Hsuan
b032ad7aa9 up-device: Accessing polkit service in UpDevice 2024-08-22 14:17:07 +08:00
Kate Hsuan
09aab8018f up-daemon: Testing the authorization is allowed through polkit
An function is used to test the user authorization through polkit.
2024-08-22 14:12:50 +08:00
Kate Hsuan
0c9ba8048f Revert "Remove polkit dependency"
This reverts commit 9a2b263309.

Revisions:
1. Drop autoconf and migrate to meson build.
2. Drop dbus-glib dependency
2024-08-20 17:04:12 +08:00
Jelle van der Waa
5b656780ff linux: integration-test: Test for battery charging threshold
Signed-off-by: Kate Hsuan <hpa@redhat.com>
2024-08-20 17:04:12 +08:00
Jelle van der Waa
aed03b0705 up-device-battery: Recover the charging threshold settings when restarting
The configuration file holds the enable status of the battery charging
threshold for individual batteries. If it is enabled, upower will recover
the settings when upower starts up. If the configuration file is not
present, the battery charging threshold is disabled by default.

Signed-off-by: Kate Hsuan <hpa@redhat.com>
2024-08-20 17:04:12 +08:00
Jelle van der Waa
16e373add0 up-device-battery: Enable and preserve the battery charging threshold config
It sets the battery start and end charging threshold to the kernel and also
saves the configuration in a file. If upower is restarted, upower
can recover the charging threshold setting through the configuration file.

The configuration file is based on the model name and serial number of the
battery so the battery can be configured individually.

Signed-off-by: Kate Hsuan <hpa@redhat.com>
2024-08-20 17:04:04 +08:00
Jelle van der Waa
dc28eccd1c dbus: Define EnableChargeLimit() dbus method
EnableChargeLimit() is used to enable/disable the battery charging threshold
feature.

Signed-off-by: Kate Hsuan <hpa@redhat.com>
2024-08-20 16:58:42 +08:00
Jelle van der Waa
d2c7e183c3 linux: up-device-supply-battery: write the charging threshold to kernel
Write the battery charging start and end threshold to the kernel through
sysfs.

Signed-off-by: Kate Hsuan <hpa@redhat.com>
2024-08-15 23:21:01 +08:00
Jelle van der Waa
f3a81a610f linux: up-device-supply: reset the values for battery charging threshold
Reset the value of the properties.

Signed-off-by: Kate Hsuan <hpa@redhat.com>
2024-08-02 13:58:48 +08:00
Jelle van der Waa
a955bae8c4 up-device-battery: show support status for battery charging threshold
If the charging threshold is supported, the ChargeThresholdSupported will be
True.

Signed-off-by: Kate Hsuan <hpa@redhat.com>
2024-08-02 13:58:48 +08:00
Jelle van der Waa
3df2599629 dbus: Define ChargeThresholdEnabled and ChargeThresholdSupported properties
ChargeThresholdSupported property is used to show if the battery supports
Charging threshold feature. ChargeThresholdEnabled is used to show the
configuration (enabled/disabled) status of the feature.

Signed-off-by: Kate Hsuan <hpa@redhat.com>
2024-08-02 13:58:48 +08:00
Jelle van der Waa
a12f7d7eef dbus: Define ChargeStartThreshold and ChargeEndThreshold properties
Define dbus interface for ChargeStartThreshold and ChargeEndThreshold.

Signed-off-by: Kate Hsuan <hpa@redhat.com>
2024-08-02 13:58:48 +08:00
Jelle van der Waa
3ff4d63cfc linux: up-device-supply-battery: Get default charging limit from udev
Get the default charge limit through udev.

Signed-off-by: Kate Hsuan <hpa@redhat.com>
2024-08-02 13:58:47 +08:00
Jelle van der Waa
16277c88c4 rules: add battery charge limit udev and hwdb files
Set per battery-specific start/stop charge limits in udev as environment
variable with the option to allow a user to override the default 60%
start and 80% stop charge limit.

Signed-off-by: Kate Hsuan <hpa@redhat.com>
2024-07-23 15:42:52 +08:00
Kate Hsuan
669a1c2a27 linux: test: test for Ignore as CriticalPowerAction 2024-06-11 09:38:08 +00:00
Kate Hsuan
7ca11d476d linux: up-backend: Ignore as CriticalPowerAction
If AllowRiskyCriticalPowerAction is set to true, upower will ignore
the CriticalPowerAction and does nothing when battery level is at
critical level.

Link: https://aur.archlinux.org/cgit/aur.git/tree/0001-Add-a-critical-action-Ignore.patch?h=upower-nocritical-nosystemd-git
2024-06-11 09:38:08 +00:00
Kate Hsuan
d756a7d512 up-config: A config and warning message for Ignore as the CriticalPowerAction
Action "Ignore" is one of the CriticalPowerAction. It is a risky action
so AllowRiskyCriticalPowerAction should be set to true to enable it.
2024-06-11 09:38:08 +00:00
Kate Hsuan
00eb31a63c linux: device_supply: The new priority of device kind
Give a new priority for device type since the gaming_input may include
a keyboard, a touchpad... etc, for example, a Sony DualShock4 joypad.
A mouse and a touchpad may include a mouse and a keyboard.
Therefore, the priority is:
1. Audio
2. Gaming_input
3. Keyboard
4. Tablet
5. Touchpad
6. Mouse
2024-06-03 14:18:17 +08:00
Kate Hsuan
544eba5543 test: the test for CriticalPowerAction=Suspend
The test cases for AllowRiskyCriticalPowerAction=true and false when
CriticalPowerAction=Suspend.
2024-04-25 16:07:11 +08:00
Kate Hsuan
69436647c5 linux: up-backend: A safeguard for the risky CriticalPowerAction
If AllowRiskyCriticalPowerAction is true and the CriticalPowerAction
is "Suspend", The "Suspend" can be the CriticalPowerAction.
Otherwise, "HybridSleep" will be the CriticalPowerAction.
2024-04-25 16:07:11 +08:00
Kate Hsuan
469346b6bf up-config: set filename variable with g_autofree 2024-04-25 16:07:11 +08:00
Kate Hsuan
8cb9fb0231 up-config: The warning message for the risky CriticalPowerAction
The warning message will be shown when CriticalPowerAction is "Suspend".
One iwarning message is for enabling the risky CriticalPowerAction and
the other is to show the potential risk when
AllowRiskyCriticalPowerAction is true.
2024-04-25 16:07:11 +08:00
Alexander Couzens
18b4748435 linux: allow Suspend as CriticalPowerAction
When using UPower in a non-hybernate setup, the result of a
CriticalPowerAction would be PowerOff. Certain users prefer
Suspend instead, because this is the only way to recover
graceful from that state.
Suspend action will only used, when explicit configured.

Closes: #23
Closes: #59
Signed-off-by: Kate Hsuan <hpa@redhat.com>
2024-04-25 16:07:11 +08:00
Kate Hsuan
1146047df9 ci: replace compiling umockdev with a package
Test is based on umockdev package without compile it
during runner creation step.
2024-04-25 15:35:30 +08:00
90 changed files with 8765 additions and 1911 deletions

View file

@ -3,24 +3,38 @@
from lxml import etree from lxml import etree
import sys import sys
def format_title(title): def format_title(title):
"""Put title in a box""" """Put title in a box"""
box = { box = {
'tl': '', 'tr': '', 'bl': '', 'br': '', 'h': '', 'v': '', "tl": "",
"tr": "",
"bl": "",
"br": "",
"h": "",
"v": "",
} }
hline = box['h'] * (len(title) + 2) hline = box["h"] * (len(title) + 2)
return "\n".join(
[
f"{box['tl']}{hline}{box['tr']}",
f"{box['v']} {title} {box['v']}",
f"{box['bl']}{hline}{box['br']}",
]
)
return '\n'.join([
f"{box['tl']}{hline}{box['tr']}",
f"{box['v']} {title} {box['v']}",
f"{box['bl']}{hline}{box['br']}",
])
# The log coloring causes the XML to be invalid, so set recover=True # The log coloring causes the XML to be invalid, so set recover=True
tree = etree.parse(sys.argv[1], etree.XMLParser(recover=True)) tree = etree.parse(sys.argv[1], etree.XMLParser(recover=True))
for suite in tree.xpath('/testsuites/testsuite'): for suite in tree.xpath("/testsuites/testsuite"):
skipped = suite.get('skipped') skipped = suite.get("skipped")
if int(skipped) != 0: if int(skipped) != 0:
print(format_title('Tests were skipped when they should not have been. All the tests must be run in the CI'), print(
end='\n\n', flush=True) format_title(
"Tests were skipped when they should not have been. All the tests must be run in the CI"
),
end="\n\n",
flush=True,
)
sys.exit(1) sys.exit(1)

8
.git-blame-ignore-revs Normal file
View file

@ -0,0 +1,8 @@
# Fix spelling issues with codespell check
bab81e5e44a13275b93a3e08ace2c2b7a00bef33
# Reformat python code style with black
17e5903670801ec8a38ecb2808e43d28706a10b0
# Fix trailing white space and end of file
2678cb00d4cb5426c4e5b714338065d9d3323f7b

2
.gitconfig Normal file
View file

@ -0,0 +1,2 @@
[blame]
ignoreRevsFile = .git-blame-ignore-revs

View file

@ -2,6 +2,9 @@ include:
- project: 'freedesktop/ci-templates' - project: 'freedesktop/ci-templates'
ref: master ref: master
file: '/templates/fedora.yml' file: '/templates/fedora.yml'
- project: 'freedesktop/ci-templates'
ref: master
file: '/templates/debian.yml'
variables: variables:
FDO_DISTRIBUTION_TAG: latest FDO_DISTRIBUTION_TAG: latest
@ -25,6 +28,7 @@ variables:
libplist-devel libplist-devel
umockdev umockdev
dbus-x11 dbus-x11
polkit-devel
python3-gobject python3-gobject
python3-dbusmock python3-dbusmock
python3-pip python3-pip
@ -40,6 +44,7 @@ workflow:
- if: $CI_PIPELINE_SOURCE == 'schedule' - if: $CI_PIPELINE_SOURCE == 'schedule'
stages: stages:
- pre-commit
- check-source - check-source
- build - build
- test - test
@ -47,6 +52,13 @@ stages:
image: $FEDORA_IMAGE image: $FEDORA_IMAGE
pre_commit:
stage: pre-commit
script:
- dnf install -y libatomic
- dnf install -y pre-commit
- pre-commit run --all-files
test_nut_hwdb: test_nut_hwdb:
stage: check-source stage: check-source
except: except:
@ -80,6 +92,31 @@ build:
- "${CI_PROJECT_DIR}" - "${CI_PROJECT_DIR}"
expire_in: 3h30min expire_in: 3h30min
build_debian:
stage: build
image: registry.freedesktop.org/upower/upower/debian/trixie:latest
before_script:
- apt update
- apt install -y git
- git -c http.sslVerify=false clone https://gitlab.gnome.org/GNOME/libgudev.git
- cd libgudev
- apt install -y umockdev libumockdev-dev libumockdev0 libsystemd-dev systemd
- meson _debian_build -Dprefix=/usr
- ninja -C _debian_build install
- cd ..
script:
- meson _debian_build -Dintrospection=enabled -Dman=true -Dgtk-doc=true -Didevice=enabled
- ninja -C _debian_build
except:
variables:
- $CI_PIPELINE_SOURCE == "schedule"
# avoid recompiling in test stage
artifacts:
name: untracked
paths:
- "${CI_PROJECT_DIR}"
expire_in: 3h30min
# Compile test the other backends (they don't have extra dependencies currently) # Compile test the other backends (they don't have extra dependencies currently)
.build_backend_template: &build_backend .build_backend_template: &build_backend
@ -116,17 +153,6 @@ test:
- ./configure - ./configure
- make install - make install
- cd .. - cd ..
- git clone https://github.com/martinpitt/umockdev.git
- cd umockdev
- dnf install -y chrpath libpcap-devel systemd-udev vala
- meson _build -Dprefix=/usr
- ninja -C _build install
- cd ..
- cd libgudev
- dnf install -y glibc-langpack-fr umockdev-devel
- meson _build -Dprefix=/usr
- ninja -C _build install
- cd ..
script: script:
- meson _build -Dintrospection=enabled -Dman=true -Dgtk-doc=true -Didevice=enabled - meson _build -Dintrospection=enabled -Dman=true -Dgtk-doc=true -Didevice=enabled
- ninja -C _build - ninja -C _build
@ -142,6 +168,37 @@ test:
variables: variables:
- $CI_PIPELINE_SOURCE == "schedule" - $CI_PIPELINE_SOURCE == "schedule"
test_debian:
stage: test
image: registry.freedesktop.org/upower/upower/debian/trixie:latest
dependencies:
- build_debian
before_script:
- git -c http.sslVerify=false clone https://github.com/zatrazz/glibc-tools.git
- cd glibc-tools
- ./configure
- make install
- cd ..
- cd libgudev
- apt update
- apt install -y umockdev libumockdev-dev libumockdev0 libsystemd-dev systemd
- ninja -C _debian_build install
- cd ..
script:
- meson _debian_build -Dintrospection=enabled -Dman=true -Dgtk-doc=true -Didevice=enabled --wipe
- ninja -C _debian_build
- catchsegv meson test -C _debian_build --print-errorlogs --no-stdsplit
- .ci/fail_skipped_tests.py _debian_build/meson-logs/testlog.junit.xml
artifacts:
when: always
expire_in: 1 week
paths:
- "${CI_PROJECT_DIR}/_debian_build/meson-logs/"
except:
variables:
- $CI_PIPELINE_SOURCE == "schedule"
check_abi: check_abi:
stage: test stage: test
before_script: before_script:
@ -192,3 +249,55 @@ container_fedora_build:
$DEPENDENCIES $DEPENDENCIES
FDO_DISTRIBUTION_EXEC: | FDO_DISTRIBUTION_EXEC: |
curl https://gitlab.freedesktop.org/hadess/check-abi/-/raw/main/contrib/check-abi-fedora.sh | bash curl https://gitlab.freedesktop.org/hadess/check-abi/-/raw/main/contrib/check-abi-fedora.sh | bash
# CONTAINERS creation stage
container_debian_build:
extends: .fdo.container-build@debian
only:
variables:
- $CI_PIPELINE_SOURCE == "schedule" && $CRON_TASK == "BUILD_CI_IMAGES"
variables:
FDO_DISTRIBUTION_TAG: latest
FDO_DISTRIBUTION_VERSION: trixie
GIT_STRATEGY: none # no need to pull the whole tree for rebuilding the image
FDO_FORCE_REBUILD: 1
# a list of packages to install
FDO_DISTRIBUTION_PACKAGES:
autoconf
automake
autopoint
autotools-dev
binutils
binutils-common
binutils-x86-64-linux-gnu
bsdextrautils
build-essential
bzip2
cpp
cpp-14
cpp-14-x86-64-linux-gnu
cpp-x86-64-linux-gnu
curl
debhelper
git
g++
g++-14
g++-14-x86-64-linux-gnu
g++-x86-64-linux-gnu
gcc
gcc-14
gcc-14-x86-64-linux-gnu
gcc-x86-64-linux-gnu
m4
make
systemd-dev
python3-dbus
python3-packaging
python3-dbusmock
gir1.2-upowerglib-1.0
libpolkit-gobject-1-dev
FDO_DISTRIBUTION_EXEC: >-
echo "deb-src http://deb.debian.org/debian/ trixie main contrib non-free" >> /etc/apt/sources.list &&
apt update &&
apt build-dep -y upower &&
curl https://gitlab.freedesktop.org/hadess/check-abi/-/raw/main/contrib/check-abi-fedora.sh | bash

10
.markdownlint.json Normal file
View file

@ -0,0 +1,10 @@
{
"default": true,
"MD033": false,
"MD041": false,
"MD036": false,
"MD013": {
"tables": false,
"line_length": 1000
}
}

40
.pre-commit-config.yaml Normal file
View file

@ -0,0 +1,40 @@
default_stages: [pre-commit]
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v5.0.0
hooks:
- id: no-commit-to-branch
args: [--branch, main, --pattern, 1_.*_X]
- id: check-added-large-files
- id: check-byte-order-marker
- id: check-executables-have-shebangs
- id: forbid-new-submodules
- id: check-yaml
exclude: '.clang-format'
- id: check-json
- id: pretty-format-json
args: ['--no-sort-keys', '--autofix']
- id: check-symlinks
- id: check-xml
- id: end-of-file-fixer
types_or: [c, shell, python, proto]
- id: trailing-whitespace
types_or: [c, shell, python, xml]
- id: check-docstring-first
- id: check-merge-conflict
- id: mixed-line-ending
args: [--fix=lf]
- repo: https://github.com/codespell-project/codespell
rev: v2.2.2
hooks:
- id: codespell
args: ['--config', './contrib/codespell.cfg', --write-changes]
- repo: https://github.com/ambv/black
rev: 24.10.0
hooks:
- id: black
- repo: https://github.com/igorshubovych/markdownlint-cli
rev: v0.38.0
hooks:
- id: markdownlint
args: ['--fix', '--ignore', '.github']

99
HACKING
View file

@ -1,99 +0,0 @@
SCM
===
- anonymous checkouts
$ git clone git://git.freedesktop.org/git/upower.git
- checkouts if you got an ssh account on fd.o (username@ is optional)
$ git clone ssh://[username@]git.freedesktop.org/git/upower.git
- commit to local repository
$ git commit -a
- push local repository to master repository at fd.o (remember most patches
requires review at the mailing list)
$ git push
- pull changes from master repository at fd.o
$ git pull
- diff of working tree versus local repository
$ git diff
- diff of local repository vs. master repository at fd.o
synchronize with upstream repo:
$ git pull
(possibly merge changes)
generate the diff:
$ git diff origin HEAD
- influential environment variables (set these in e.g. .bash_profile)
export GIT_AUTHOR_NAME='Your Full Name'
export GIT_COMMITTER_NAME='Your Full Name'
export GIT_COMMITTER_EMAIL=youremail@domain.net
export GIT_AUTHOR_EMAIL=youremail@domain.net
- see also
http://www.kernel.org/pub/software/scm/git/docs/
Committing code
===
- Commit messages should be of the form (the five lines between the
lines starting with ===)
=== begin example commit ===
short explanation of the commit
Longer explanation explaining exactly what's changed, whether any
external or private interfaces changed, what bugs were fixed (with bug
tracker reference if applicable) and so forth. Be concise but not too brief.
=== end example commit ===
- Always add a brief description of the commit to the _first_ line of
the commit and terminate by two newlines (it will work without the
second newline, but that is not nice for the interfaces).
- First line (the brief description) must only be one sentence and
must not start with a capital letter. Don't use a trailing period
either.
- The main description (the body) is normal prose and should use normal
punctuation and capital letters where appropriate. Normally, for patches
sent to a mailing list it's copied from there.
- When committing code on behalf of others use the --author option, e.g.
git commit -a --author "Joe Coder <joe@coder.org>"
Coding Style
===
- Please follow the coding style already used.
- Write docs for all functions and structs and so on. We use gtkdoc format.
- All external interfaces (network protocols, file formats, etc.)
should have documented specifications sufficient to allow an
alternative implementation to be written. Our implementation should
be strict about specification compliance (should not for example
heuristically parse a file and accept not-well-formed
data). Avoiding heuristics is also important for security reasons;
if it looks funny, ignore it (or exit, or disconnect).
Testing
===
Under Linux, with the umockdev package installed, you can run "make check"
in the root build directory to run an automated test suite.

124
HACKING.md Normal file
View file

@ -0,0 +1,124 @@
## SCM
- anonymous checkouts
```console
git clone git://git.freedesktop.org/git/upower.git
```
- checkouts if you got an ssh account on fd.o (username@ is optional)
```console
git clone ssh://[username@]git.freedesktop.org/git/upower.git
```
- commit to local repository
```console
git commit -a
```
- push local repository to master repository at fd.o (remember most patches
requires review at the mailing list)
```console
git push
```
- pull changes from master repository at fd.o
```console
git pull
```
- diff of working tree versus local repository
```console
git diff
```
- diff of local repository vs. master repository at fd.o
- synchronize with upstream repo:
```console
git pull #(possibly merge changes)
```
- generate the diff:
```console
git diff origin HEAD
```
- influential environment variables (set these in e.g. .bash_profile)
```bash
export GIT_AUTHOR_NAME='Your Full Name'
export GIT_COMMITTER_NAME='Your Full Name'
export GIT_COMMITTER_EMAIL=<youremail@domain.net>
export GIT_AUTHOR_EMAIL=<youremail@domain.net>
```
- see also
<http://www.kernel.org/pub/software/scm/git/docs/>
## Set up Git environment
Run the following command to include the upower Git configurations.
```bash
cd contrib
./setup
```
## Committing code
- Commit messages should be of the form (the five lines between the
lines starting with ===)
```text
=== begin example commit ===
short explanation of the commit
Longer explanation explaining exactly what's changed, whether any
external or private interfaces changed, what bugs were fixed (with bug
tracker reference if applicable) and so forth. Be concise but not too brief.
=== end example commit ===
```
- Always add a brief description of the commit to the _first_ line of
the commit and terminate by two newlines (it will work without the
second newline, but that is not nice for the interfaces).
- First line (the brief description) must only be one sentence and
must not start with a capital letter. Don't use a trailing period
either.
- The main description (the body) is normal prose and should use normal
punctuation and capital letters where appropriate. Normally, for patches
sent to a mailing list it's copied from there.
- When committing code on behalf of others use the `--author` option, e.g.
```console
git commit -a --author "Joe Coder <joe@coder.org>"
```
## Coding Style
- Please follow the coding style already used.
- Write docs for all functions and structs and so on. We use gtkdoc format.
- All external interfaces (network protocols, file formats, etc.)
should have documented specifications sufficient to allow an
alternative implementation to be written. Our implementation should
be strict about specification compliance (should not for example
heuristically parse a file and accept not-well-formed
data). Avoiding heuristics is also important for security reasons;
if it looks funny, ignore it (or exit, or disconnect).
## Testing
Under Linux, with the umockdev package installed, you can run
```console
ninja test
```
in the root build directory to run an automated test suite.

79
NEWS
View file

@ -1,3 +1,72 @@
Version 1.91.0
--------------
Released: 2025-11-25
- New feature: Support multiple keyboard backlight LED control. (#291 !203)
- Make installation of tests optional (!202)
- Support conf.d style config file (!286)
- Deprecate CapacityLevel property (!290 #333)
Version 1.90.10
--------------
Released: 2025-08-26
- Fix wrong model name of the devices (!267, #309)
- Switch charge_types to "Custom" when charging threshold is enabled (!268, #275)
- Fix invalid command line arguments (!269)
- Fix leak when reporting daemon usage error (!270)
- OpenBSD: support battery status from qcpas (!272)
- Fix history progression (!274, #316)
- Add a battery filter to the upower command line (!275)
- Change the charging behaviors using charge_types (!276, !46, #275)
- Fix integration tests issues, including floating point value and race between umockdev and upower (!277, !278)
- Rework upower command (!280)
- Propagate charge-threshold-enabled to display device (!281)
Version 1.90.9
--------------
Released: 2025-04-10
- Fix unstable OnBattery status (#306 #307)
Version 1.90.8
--------------
Released: 2025-03-31
- Fix the incorrect percentage value of a removed battery. (#292) (!246)
- Fix device type for the gaming device. (#296 #295) (!247)
- Determine the battery discharging status when connecting a low-power charger. (#276) (!244)
- Fix memory leak when refreshing battery info. (#305) (!261)
- Fix memory leak when getting device id. (!261)
Version 1.90.7
--------------
Released: 2025-01-09
- Find the correct parent id for input devices. (#268 #286)
- Fix race condition in test_sibling_priority_no_overwrite. (!240)
- Show charge-threshold-* status as yes/no. (!242)
- Add zsh-completions for CLI. (!241)
- Fix integration test. Extend the daemon stopping timeout to 10sec (#290)
- Added pre-commit test and fixed code spelling, markdown, and code style issues. (!248)
Version 1.90.6
--------------
Released: 2024-09-18
- Fractional battery percentage (!226)
- CI enhancement. (!236)
- Tweak the default battery charging threshold to 75-80. (!234)
- Fix g_object_weak_ref: assertion 'g_atomic_int_get (&object->ref_count) >= 1' failed (#281, !233)
Version 1.90.5
--------------
Released: 2024-08-22
- Suspend and Ignore as the CriticalPowerActions (!218 and !218)
- Support for battery charging threshold (!208)
- Tweak the device priority to make sure the device kind joystick can be correctly shown. (#267)
Version 1.90.4 Version 1.90.4
-------------- --------------
Released: 2024-04-08 Released: 2024-04-08
@ -48,7 +117,7 @@ Released: 2023-07-04
connected through Bluetooth would show as 2 batteries) connected through Bluetooth would show as 2 batteries)
- Hide duplicate Logitech wireless devices when they get connected - Hide duplicate Logitech wireless devices when they get connected
through USB as well through USB as well
- Fix Bluetooth device names not synchronising, and use user-chosen - Fix Bluetooth device names not synchronizing, and use user-chosen
names when available names when available
- Handle the "present" sysfs attribute changing - Handle the "present" sysfs attribute changing
- Fix iDevices not appearing - Fix iDevices not appearing
@ -614,7 +683,7 @@ Bugfixes:
- Depend on stable GUdev API (Michael Biebl) - Depend on stable GUdev API (Michael Biebl)
- Drop devkit and devkit-power-daemon symlinks (Michael Biebl) - Drop devkit and devkit-power-daemon symlinks (Michael Biebl)
- Fix bitmap check in the input code (Arnaud Patard) - Fix bitmap check in the input code (Arnaud Patard)
- Honour ACLOCAL_FLAGS in Makefile.am (Richard Hughes) - Honor ACLOCAL_FLAGS in Makefile.am (Richard Hughes)
Version 0.9.8 Version 0.9.8
~~~~~~~~~~~~~ ~~~~~~~~~~~~~
@ -834,7 +903,7 @@ Bugfixes:
- Update the list of HID UPS (Arnaud Quette) - Update the list of HID UPS (Arnaud Quette)
- Use a gdouble for percentage to fix on-battery reporting (Byron Clark) - Use a gdouble for percentage to fix on-battery reporting (Byron Clark)
- Bug 24262 incorrect battery recall warning for Lenovo T61 (Martin Pitt) - Bug 24262 incorrect battery recall warning for Lenovo T61 (Martin Pitt)
- Fix the toshiba battery recal notices by matching up the double quotes (Richard Hughes) - Fix the toshiba battery recall notices by matching up the double quotes (Richard Hughes)
- Avoid going from discharging to pending-discharge when the expansion battery - Avoid going from discharging to pending-discharge when the expansion battery
is very low (Richard Hughes) is very low (Richard Hughes)
- Some vendors fill the NVRAM full of junk. Don't crash the daemon if the - Some vendors fill the NVRAM full of junk. Don't crash the daemon if the
@ -973,7 +1042,7 @@ Bugfixes:
- Remove the 0x prefix from some rule matches (Ronald) - Remove the 0x prefix from some rule matches (Ronald)
- Allow all DBus properties to be read with the new DBus (Richard Hughes) - Allow all DBus properties to be read with the new DBus (Richard Hughes)
- Only reset the update-time if the read was successful (Richard Hughes) - Only reset the update-time if the read was successful (Richard Hughes)
- Optimise the udev rules to skip non-usb devices (Richard Hughes) - Optimize the udev rules to skip non-usb devices (Richard Hughes)
- Ensure we get properties on devices correctly that have not yet been changed (Richard Hughes) - Ensure we get properties on devices correctly that have not yet been changed (Richard Hughes)
- Make the suspend and hibernate scripts execute synchronously. Fixes rh#497563 (Richard Hughes) - Make the suspend and hibernate scripts execute synchronously. Fixes rh#497563 (Richard Hughes)
- Ignore method timeouts when we suspend and hibernate (Richard Hughes) - Ignore method timeouts when we suspend and hibernate (Richard Hughes)
@ -1003,7 +1072,7 @@ New Features:
- Get rid of internal Object and instead use the GObject property system (Richard Hughes) - Get rid of internal Object and instead use the GObject property system (Richard Hughes)
- Move the library directory from libdevkit-power to devkit-power-gobject (Richard Hughes) - Move the library directory from libdevkit-power to devkit-power-gobject (Richard Hughes)
- Ship a shared library. There are now three external projects using copies of - Ship a shared library. There are now three external projects using copies of
this, which is rediculous (Richard Hughes) this, which is ridiculous (Richard Hughes)
- Require I_KNOW_THE_DEVICEKIT_POWER_API_IS_SUBJECT_TO_CHANGE (Richard Hughes) - Require I_KNOW_THE_DEVICEKIT_POWER_API_IS_SUBJECT_TO_CHANGE (Richard Hughes)
Bugfixes: Bugfixes:

49
README
View file

@ -1,49 +0,0 @@
===============
UPower
===============
Requirements:
glib-2.0 >= 2.66.0
gio-2.0 >= 2.16.1
gudev-1.0 >= 235 (Linux)
libimobiledevice-1.0 >= 0.9.7 (optional)
UPower is an abstraction for enumerating power devices,
listening to device events and querying history and statistics.
Any application or service on the system can access the
org.freedesktop.UPower service via the system message bus.
Debugging
---------
When doing bug reports, the following information can be useful:
* `grep . /sys/class/power_supply/*/*`:
This includes the current kernel view of all power supplies in the
system. It is always a good idea to include this information.
* `udevadm info -e`:
This shows the hardware configuration and is relevant when e.g. the
type of an external device is misdetected.
* `upower -d`:
Shows upower's view of the state
* `upower --monitor-detail`:
Dumps device information every time that a change happens. This helps
with debugging dynamic issues.
* `udevadm monitor`:
Dumps the udev/kernel reported hardware changes (and addition/removal).
This is helpful when debugging dynamic issues, in particular if it is
not clear whether the issue is coming from the kernel or upower.
In addition, it can also be useful to run upower in debug mode and post the
logs. There are two ways of doing so:
* Run upower daemon manually, you can do so using:
`sudo /usr/libexec/upowerd -rd`
* Modify the systemd service and restart. This is best done by:
1. `sudo systemctl edit upower.service`
2. Adding the two lines:
```
[Service]
Environment=G_MESSAGES_DEBUG=all
```
3. `sudo systemctl restart upower.service`
4. Grab logs using `journalctl -u upower.service` or similar

53
README.md Normal file
View file

@ -0,0 +1,53 @@
# UPower
Requirements:
```text
glib-2.0 >= 2.66.0
gio-2.0 >= 2.16.1
gudev-1.0 >= 235 (Linux)
libimobiledevice-1.0 >= 0.9.7 (optional)
polkit-gobject-1 >= 124
```
UPower is an abstraction for enumerating power devices,
listening to device events and querying history and statistics.
Any application or service on the system can access the
org.freedesktop.UPower service via the system message bus.
## Debugging
When doing bug reports, the following information can be useful:
- `grep . /sys/class/power_supply/*/*`
This includes the current kernel view of all power supplies in the
system. It is always a good idea to include this information.
- `udevadm info -e`
This shows the hardware configuration and is relevant when e.g. the
type of an external device is misdetected.
- `upower -d`
Shows upower's view of the state
- `upower --monitor-detail`
Dumps device information every time that a change happens. This helps
with debugging dynamic issues.
- `udevadm monitor`
Dumps the udev/kernel reported hardware changes (and addition/removal).
This is helpful when debugging dynamic issues, in particular if it is
not clear whether the issue is coming from the kernel or upower.
In addition, it can also be useful to run upower in debug mode and post the
logs. There are two ways of doing so:
- Run upower daemon manually, you can do so using:
`sudo /usr/libexec/upowerd -rd`
- Modify the systemd service and restart. This is best done by:
1. `sudo systemctl edit upower.service`
2. Adding the two lines:
```text
[Service]
Environment=G_MESSAGES_DEBUG=all
```
3. `sudo systemctl restart upower.service`
4. Grab logs using `journalctl -u upower.service` or similar

4
contrib/codespell.cfg Normal file
View file

@ -0,0 +1,4 @@
[codespell]
builtin = clear,informal,en-GB_to_en-US
skip = *.po,*.csv,*.svg,*.p7c,subprojects,.git,pcrs,build*,.ossfuzz,*/tests/*,contrib/codespell.cfg
ignore-words-list = conexant,Conexant,gir,GIR,hsi,HSI,cancelled,Cancelled,te,mitre,distroname,wel,FPT,$FPT,inout,som,SoM

26
contrib/setup Executable file
View file

@ -0,0 +1,26 @@
#!/usr/bin/env -S bash -e
#set up local repository
install_pip()
{
package=$1
args=$2
python3 -m pip install $package $args
}
setup_precommit()
{
echo "Configuring pre-commit hooks"
install_pip pre-commit
pre-commit install
}
setup_git()
{
echo "Configuring git environment"
git config include.path ../.gitconfig
}
setup_git
setup_precommit

View file

@ -0,0 +1,22 @@
#compdef upower
_enumerate_objects() {
local expl
local -a objects
objects=( ${(f)"$(
_call_program upower-enumerate-objects \
upower -e
)"} )
_wanted objects expl "object path" \
compadd -a objects -d objects
}
_arguments \
"(--battery -b)"{--battery,-b}"[Dump all parameters for battery objects]" \
"(--enumerate -e)"{--enumerate,-e}"[Enumerate objects paths for devices]" \
"(--dump -d)"{--dump,-d}"[Dump all parameters for all objects]" \
"(--monitor -m)"{--monitor,-m}"[Monitor activity from the power daemon]" \
"--monitor-detail""[Monitor with detail]" \
"(--show-info -i)"{--show-info,-i}"[Show information about object path]"":::_enumerate_objects" \
"(--version -v)"{--version,-v}"[Print version of client and daemon"] \
compdef _upower upower

View file

@ -0,0 +1,12 @@
zshcompletiondir = get_option('zshcompletiondir')
if zshcompletiondir == ''
zshcompletiondir = get_option('datadir') / 'zsh' / 'site-functions'
endif
if zshcompletiondir != 'no'
install_data(
'_upower',
install_dir: zshcompletiondir,
install_mode: 'rw-r--r--',
)
endif

View file

@ -148,7 +148,7 @@ method return sender=:1.386 -> dest=:1.477 reply_serial=2
</doc:description> </doc:description>
<doc:permission>Callers will need to make sure that the daemon was started in debug mode</doc:permission> <doc:permission>Callers will need to make sure that the daemon was started in debug mode</doc:permission>
<doc:errors> <doc:errors>
<doc:error name="&ERROR_GENERAL;">if an error occured while refreshing</doc:error> <doc:error name="&ERROR_GENERAL;">if an error occurred while refreshing</doc:error>
</doc:errors> </doc:errors>
</doc:doc> </doc:doc>
</method> </method>
@ -245,6 +245,29 @@ method return sender=:1.386 -> dest=:1.477 reply_serial=2
</doc:doc> </doc:doc>
</method> </method>
<!-- ************************************************************ -->
<method name="EnableChargeThreshold">
<arg name="chargeThreshold" direction="in" type="b">
<doc:doc>
<doc:summary>
If it is true, the battery charge will be limited to ChargeEndThreshold and start to charge when the battery is lower than ChargeStartThreshold.
Moreovere, if ChargeEndThreshold and/or ChargeStartThreshold are not supported, charge limit functionality on the whole may still be supported but the firmware will choose its own thresholds.
If it is false, the battery will always be fully charged.
</doc:summary>
</doc:doc>
</arg>
<doc:doc>
<doc:description>
<doc:para>
Limiting the battery charge to the configured thresholds ChargeStartThreshold and ChargeEndThreshold.
</doc:para>
</doc:description>
<doc:errors>
<doc:error name="&ERROR_GENERAL;">if an error occurred while setting the battery charge limits</doc:error>
</doc:errors>
</doc:doc>
</method>
<!-- ************************************************************ --> <!-- ************************************************************ -->
<property name="NativePath" type="s" access="read"> <property name="NativePath" type="s" access="read">
<doc:doc> <doc:doc>
@ -393,7 +416,7 @@ method return sender=:1.386 -> dest=:1.477 reply_serial=2
<doc:term>27</doc:term><doc:definition>Toy</doc:definition> <doc:term>27</doc:term><doc:definition>Toy</doc:definition>
</doc:item> </doc:item>
<doc:item> <doc:item>
<doc:term>28</doc:term><doc:definition>Bluetooth Genreic</doc:definition> <doc:term>28</doc:term><doc:definition>Bluetooth Generic</doc:definition>
</doc:item> </doc:item>
</doc:list> </doc:list>
<doc:para> <doc:para>
@ -549,11 +572,18 @@ method return sender=:1.386 -> dest=:1.477 reply_serial=2
</property> </property>
<property name="Luminosity" type="d" access="read"> <property name="Luminosity" type="d" access="read">
<annotation name="org.freedesktop.DBus.Deprecated" value="true"/>
<doc:doc> <doc:doc>
<doc:description> <doc:description>
<doc:para> <doc:para>
Luminosity being recorded by the meter. Luminosity being recorded by the meter.
</doc:para> </doc:para>
<doc:para>
DEPRECATED
</doc:para>
<doc:para>
This property is deprecated since the code it depends on was removed in 0.99.12
</doc:para>
</doc:description> </doc:description>
</doc:doc> </doc:doc>
</property> </property>
@ -828,6 +858,125 @@ method return sender=:1.386 -> dest=:1.477 reply_serial=2
</doc:description> </doc:description>
</doc:doc> </doc:doc>
</property> </property>
<property name="ChargeStartThreshold" type="u" access="read">
<doc:doc>
<doc:description>
<doc:para>
<p>When a start charge threshold is set the battery won't get charged until the charge drops under this threshold.</p>
<p>In general case, ChargeStartThreshold is between 0 and 100. If it is G_MAXUINT, upower will skip to set charge_control_start_threshold.</p>
</doc:para>
</doc:description>
</doc:doc>
</property>
<property name="ChargeEndThreshold" type="u" access="read">
<doc:doc>
<doc:description>
<doc:para>
<p>The end charge threshold stops the battery from getting charged after the set threshold</p>
<p>In general case, ChargeEndThreshold is between 0 and 100. If it is G_MAXUINT, upower will skip to set charge_control_end_threshold.</p>
</doc:para>
</doc:description>
</doc:doc>
</property>
<property name="ChargeThresholdEnabled" type="b" access="read">
<doc:doc>
<doc:description>
<doc:para>
<p>If battery charge start and end limits are applied.</p>
</doc:para>
</doc:description>
</doc:doc>
</property>
<property name="ChargeThresholdSupported" type="b" access="read">
<doc:doc>
<doc:description>
<doc:para>
<p>If setting battery charge limits is supported.</p>
</doc:para>
</doc:description>
</doc:doc>
</property>
<property name="ChargeThresholdSettingsSupported" type="u" access="read">
<doc:doc>
<doc:description>
<doc:para>The types of settings for charge thresholds that are supported. The value is a bitmask value and it is the sum of the supported types.</doc:para>
<doc:list>
<doc:item>
<doc:term>1</doc:term><doc:definition>The system supports charge start threshold. The battery discharges to a percentage, then starts charging.</doc:definition>
</doc:item>
<doc:item>
<doc:term>2</doc:term><doc:definition>The system supports charge end threshold. The battery charges to a percentage, then stops charging.</doc:definition>
</doc:item>
<doc:item>
<doc:term>4</doc:term><doc:definition>The system supports optimized charging behaviors controlled by the system firmware.</doc:definition>
</doc:item>
</doc:list>
<doc:para>Examples:</doc:para>
<doc:para>If the system supports charge start threshold and charge end threshold, the value is 3. If the system supports charge end threshold and the battery charging behavior is controlled by the system firmware, the value is 6.</doc:para>
</doc:description>
</doc:doc>
</property>
<property name="VoltageMinDesign" type="d" access="read">
<doc:doc>
<doc:description>
<doc:para>
The minimum design voltage of the battery, as reported by the kernel.
</doc:para>
</doc:description>
</doc:doc>
</property>
<property name="VoltageMaxDesign" type="d" access="read">
<doc:doc>
<doc:description>
<doc:para>
The maximum design voltage of the battery, as reported by the kernel.
</doc:para>
</doc:description>
</doc:doc>
</property>
<property name="CapacityLevel" type="s" access="read">
<annotation name="org.freedesktop.DBus.Deprecated" value="true"/>
<doc:doc>
<doc:description>
<doc:para>
Coarse representation of battery capacity. The value is one of the following:
</doc:para>
<doc:list>
<doc:item>
<doc:term>Unknown</doc:term>
</doc:item>
<doc:item>
<doc:term>Critical</doc:term>
</doc:item>
<doc:item>
<doc:term>Low</doc:term>
</doc:item>
<doc:item>
<doc:term>Normal</doc:term>
</doc:item>
<doc:item>
<doc:term>High</doc:term>
</doc:item>
<doc:item>
<doc:term>Full</doc:term>
</doc:item>
</doc:list>
<doc:para>
DEPRECATED
</doc:para>
<doc:para>
This property is deprecated since it is duplicated from the 'BatteryLevel' property.
</doc:para>
</doc:description>
</doc:doc>
</property>
</interface> </interface>
</node> </node>

View file

@ -11,6 +11,12 @@
org.freedesktop.UPower.KbdBacklight is a DBus interface implemented org.freedesktop.UPower.KbdBacklight is a DBus interface implemented
by UPower. by UPower.
It allows the keyboard backlight (if present) to be controlled. It allows the keyboard backlight (if present) to be controlled.
The object path "/org/freedesktop/UPower/KbdBacklight" is going to be deprecated in the future.
The new object path is based on this, but the suffix of the path is the device name.
For example:
The interface is: org.freedesktop.UPower.KbdBacklight
The object path will be: /org/freedesktop/UPower/KbdBacklight/tpacpiookbd_backlight
</doc:para> </doc:para>
</doc:description> </doc:description>
</doc:doc> </doc:doc>
@ -31,7 +37,7 @@
</doc:para> </doc:para>
</doc:description> </doc:description>
<doc:errors> <doc:errors>
<doc:error name="&ERROR_GENERAL;">if an error occured while getting the maximum brightness</doc:error> <doc:error name="&ERROR_GENERAL;">if an error occurred while getting the maximum brightness</doc:error>
</doc:errors> </doc:errors>
</doc:doc> </doc:doc>
</method> </method>
@ -52,7 +58,7 @@
</doc:para> </doc:para>
</doc:description> </doc:description>
<doc:errors> <doc:errors>
<doc:error name="&ERROR_GENERAL;">if an error occured while getting the brightness</doc:error> <doc:error name="&ERROR_GENERAL;">if an error occurred while getting the brightness</doc:error>
</doc:errors> </doc:errors>
</doc:doc> </doc:doc>
</method> </method>
@ -73,11 +79,24 @@
</doc:para> </doc:para>
</doc:description> </doc:description>
<doc:errors> <doc:errors>
<doc:error name="&ERROR_GENERAL;">if an error occured while setting the brightness</doc:error> <doc:error name="&ERROR_GENERAL;">if an error occurred while setting the brightness</doc:error>
</doc:errors> </doc:errors>
</doc:doc> </doc:doc>
</method> </method>
<!-- ************************************************************ -->
<property name="NativePath" type="s" access="read">
<doc:doc>
<doc:description>
<doc:para>
OS specific native path of the keyboard backlight LED device. On Linux this
is the sysfs path, for
example <doc:tt>/sys/devices/platform/thinkpad_acpi/leds/tpacpi\:\:kbd_backlight</doc:tt>.
</doc:para>
</doc:description>
</doc:doc>
</property>
<!-- ************************************************************ --> <!-- ************************************************************ -->
<signal name="BrightnessChanged"> <signal name="BrightnessChanged">
<arg name="value" direction="out" type="i"> <arg name="value" direction="out" type="i">

View file

@ -51,6 +51,21 @@ method return sender=:1.386 -> dest=:1.451 reply_serial=2
</doc:doc> </doc:doc>
</method> </method>
<method name="EnumerateKbdBacklights">
<annotation name="org.freedesktop.DBus.GLib.Async" value=""/>
<arg name="KbdBacklight" direction="out" type="ao">
<doc:doc><doc:summary>An array of object paths for keyboard backlight LEDs.</doc:summary></doc:doc>
</arg>
<doc:doc>
<doc:description>
<doc:para>
Enumerate all keyboard backlight LED objects on the system.
</doc:para>
</doc:description>
</doc:doc>
</method>
<method name="GetDisplayDevice"> <method name="GetDisplayDevice">
<annotation name="org.freedesktop.DBus.GLib.Async" value=""/> <annotation name="org.freedesktop.DBus.GLib.Async" value=""/>
<arg name="device" direction="out" type="o"> <arg name="device" direction="out" type="o">
@ -126,6 +141,12 @@ method return sender=:1.386 -> dest=:1.451 reply_serial=2
<doc:item> <doc:item>
<doc:term>PowerOff</doc:term> <doc:term>PowerOff</doc:term>
</doc:item> </doc:item>
<doc:item>
<doc:term>Suspend</doc:term>
</doc:item>
<doc:item>
<doc:term>Ignore</doc:term>
</doc:item>
</doc:list> </doc:list>
</doc:para> </doc:para>
</doc:description> </doc:description>

View file

@ -19,6 +19,7 @@
<refsynopsisdiv> <refsynopsisdiv>
<cmdsynopsis> <cmdsynopsis>
<command>upower</command> <command>upower</command>
<arg><option>--battery</option></arg>
<arg><option>--dump</option></arg> <arg><option>--dump</option></arg>
<arg><option>--enumerate</option></arg> <arg><option>--enumerate</option></arg>
<arg><option>--monitor-detail</option></arg> <arg><option>--monitor-detail</option></arg>
@ -35,7 +36,7 @@
<emphasis><refentrytitle>upower</refentrytitle></emphasis> is a <emphasis><refentrytitle>upower</refentrytitle></emphasis> is a
simple command line client for the simple command line client for the
<citerefentry><refentrytitle>UPower</refentrytitle><manvolnum>7</manvolnum></citerefentry> <citerefentry><refentrytitle>UPower</refentrytitle><manvolnum>7</manvolnum></citerefentry>
daemon. TODO: not fully documented. daemon.
</para> </para>
</refsect1> </refsect1>
@ -43,7 +44,31 @@
<title>OPTIONS</title> <title>OPTIONS</title>
<variablelist> <variablelist>
<varlistentry> <varlistentry>
<term><option>--monitor</option></term> <term><option>-b</option>, <option>--battery</option></term>
<listitem>
<para>
Dump all parameters for battery devices.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><option>-d</option>, <option>--dump</option></term>
<listitem>
<para>
Dump all parameters for all devices.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><option>-e</option>, <option>--enumerate</option></term>
<listitem>
<para>
Enumerate the object paths of all devices on the system.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><option>-m</option>, <option>--monitor</option></term>
<listitem> <listitem>
<para> <para>
Connect to the UPower daemon and print a line Connect to the UPower daemon and print a line
@ -61,7 +86,23 @@
</listitem> </listitem>
</varlistentry> </varlistentry>
<varlistentry> <varlistentry>
<term><option>--help</option></term> <term><option>-i</option>, <option>--show-info</option> <varname>OBJECT_PATH</varname></term>
<listitem>
<para>
Show information about the given device.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><option>-v</option>, <option>--version</option></term>
<listitem>
<para>
Print version information for the client and daemon.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><option>-h</option>, <option>--help</option></term>
<listitem> <listitem>
<para> <para>
Show help options. Show help options.

View file

@ -86,7 +86,7 @@
<title>libupower-glib helpers</title> <title>libupower-glib helpers</title>
<partintro> <partintro>
<para> <para>
This part documents helper funtions in libupower-glib. This part documents helper functions in libupower-glib.
</para> </para>
</partintro> </partintro>
<xi:include href="xml/up-types.xml"/> <xi:include href="xml/up-types.xml"/>

View file

@ -59,12 +59,12 @@ UsePercentageForPolicy=true
# will be used. # will be used.
# #
# Defaults: # Defaults:
# PercentageLow=20 # PercentageLow=20.0
# PercentageCritical=5 # PercentageCritical=5.0
# PercentageAction=2 # PercentageAction=2.0
PercentageLow=20 PercentageLow=20.0
PercentageCritical=5 PercentageCritical=5.0
PercentageAction=2 PercentageAction=2.0
# When UsePercentageForPolicy is false, the time remaining in seconds at # When UsePercentageForPolicy is false, the time remaining in seconds at
# which UPower will consider the battery low, critical, or take action for # which UPower will consider the battery low, critical, or take action for
@ -81,6 +81,22 @@ TimeLow=1200
TimeCritical=300 TimeCritical=300
TimeAction=120 TimeAction=120
# Enable the risky CriticalPowerAction-Suspend
# This option is not recommended, but it is here for users who
# want to enable the risky CriticalPowerAction, such as "Suspend"
# to fulfill their needs.
# Default is false
AllowRiskyCriticalPowerAction=false
# If the user performs the battery recalibration, the battery behavior
# will be set to "force discharge" and the user expects the battery will
# be fully discharged. However, upower performs the
# CriticalPowerAction earlier than the battery was fully discharged.
# ExpectBatteryRecalibration allows upower to ignore the
# CriticalPowerAction when the user attempts to perform the battery recalibration.
# Default is false
ExpectBatteryRecalibration=false
# The action to take when "TimeAction" or "PercentageAction" above has been # The action to take when "TimeAction" or "PercentageAction" above has been
# reached for the batteries (UPS or laptop batteries) supplying the computer # reached for the batteries (UPS or laptop batteries) supplying the computer
# #
@ -88,7 +104,10 @@ TimeAction=120
# PowerOff # PowerOff
# Hibernate # Hibernate
# HybridSleep # HybridSleep
# Suspend (AllowRiskyCriticalPowerAction should be true to use this option but risky)
# Ignore (AllowRiskyCriticalPowerAction should be true to use this option but risky)
# #
# If Suspend isn't available or AllowRiskyCriticalPowerAction=false, HybridSleep will be used
# If HybridSleep isn't available, Hibernate will be used # If HybridSleep isn't available, Hibernate will be used
# If Hibernate isn't available, PowerOff will be used # If Hibernate isn't available, PowerOff will be used
CriticalPowerAction=HybridSleep CriticalPowerAction=HybridSleep

View file

@ -0,0 +1,83 @@
# UPower Configuration Override
The configuration in UPower.conf.d will override the primary configuration
in UPower.conf.
The primary method for overriding settings in the main UPower.conf file is by placing configuration snippets in the `/etc/UPower/UPower.conf.d/` directory.
## The format of filename
For a file to be correctly processed as an override, its filename must adhere to a strict format:
- It must start with a two-digit number between `00` and `99`.
- It must end with the `.conf` extension.
- The middle section can contain `alphanumeric characters`, `dashes`, and `underscores`.
This format is captured by the regular expression `^([0-9][0-9])-([a-zA-Z0-9-_])*\.conf$`.
- The valid examples
```text
01-upower-example.conf
02-upower-test-123.conf
03-upower-1a2b-3c4D__.conf
```
- The invalid examples
```text
0000-upower-abcd1234.conf
001-upower-@bcd.config
```
## The configuration override
UPower processes configuration files in sorted order, where settings in later files override identical settings (matching both Group and Key) from previous files, including the main UPower.conf. This hierarchy ensures that local, numerically-prefixed files in the drop-in directory (UPower.conf.d/) take precedence.
For example, consider `UPower.conf` that contains the defaults:
```text
PercentageLow=20.0
PercentageCritical=5.0
PercentageAction=2.0
```
and there is a file `UPower.conf.d/70-change-percentages.conf`
containing settings for all `Percentage*` keys:
```text
[UPower]
PercentageLow=15.0
PercentageCritical=10.0
PercentageAction=5.0
```
and another `UPower.conf.d/99-change-percentages-local.conf`
containing settings only for `PercentageAction`:
```text
[UPower]
PercentageAction=7.5
```
First the main `UPower.conf` will be processed, then
`UPower.conf.d/70-change-percentages.conf` overriding the defaults
of all percentages from the main config file with the given values,
and finally `UPower.conf.d/99-change-percentages-local.conf`
overriding once again only `PercentageAction`. The final, effective
values are:
```text
PercentageLow=15.0
PercentageCritical=10.0
PercentageAction=7.5
```
## The examples of overriding the default percentage policy values
```text
[UPower]
PercentageLow=25.0
PercentageCritical=10.0
PercentageAction=5.0
```

View file

@ -0,0 +1,5 @@
install_data(
'README.md',
install_tag: 'doc',
install_dir: get_option('sysconfdir') / 'UPower' / 'UPower.conf.d',
)

View file

@ -1 +1,2 @@
subdir('UPower.conf.d')
install_data('UPower.conf', install_dir: get_option('sysconfdir') / 'UPower') install_data('UPower.conf', install_dir: get_option('sysconfdir') / 'UPower')

View file

@ -615,7 +615,7 @@ up_client_new_full (GCancellable *cancellable, GError **error)
* Creates a new #UpClient object. If connecting to upowerd on D-Bus fails, * Creates a new #UpClient object. If connecting to upowerd on D-Bus fails,
* this returns %NULL and prints out a warning with the error message. * this returns %NULL and prints out a warning with the error message.
* Consider using up_client_new_full() instead which allows you to handle errors * Consider using up_client_new_full() instead which allows you to handle errors
* and cancelling long operations yourself. * and canceling long operations yourself.
* *
* Return value: a new UpClient object, or %NULL on failure. * Return value: a new UpClient object, or %NULL on failure.
* *
@ -716,4 +716,3 @@ up_client_new_finish (GAsyncResult *res,
return g_task_propagate_pointer (G_TASK (res), error); return g_task_propagate_pointer (G_TASK (res), error);
} }

View file

@ -103,4 +103,3 @@ gboolean up_client_get_on_battery (UpClient *client);
G_END_DECLS G_END_DECLS
#endif /* __UP_CLIENT_H */ #endif /* __UP_CLIENT_H */

View file

@ -90,6 +90,13 @@ enum {
PROP_BATTERY_LEVEL, PROP_BATTERY_LEVEL,
PROP_ICON_NAME, PROP_ICON_NAME,
PROP_CHARGE_CYCLES, PROP_CHARGE_CYCLES,
PROP_CHARGE_START_THRESHOLD,
PROP_CHARGE_END_THRESHOLD,
PROP_CHARGE_THRESHOLD_ENABLED,
PROP_CHARGE_THRESHOLD_SUPPORTED,
PROP_VOLTAGE_MIN_DESIGN,
PROP_VOLTAGE_MAX_DESIGN,
PROP_CAPACITY_LEVEL,
PROP_LAST PROP_LAST
}; };
@ -266,6 +273,7 @@ up_device_to_text (UpDevice *device)
const gchar *vendor; const gchar *vendor;
const gchar *model; const gchar *model;
const gchar *serial; const gchar *serial;
const gchar *capacity_level;
UpDeviceKind kind; UpDeviceKind kind;
gboolean is_display; gboolean is_display;
UpDeviceLevel battery_level; UpDeviceLevel battery_level;
@ -337,6 +345,20 @@ up_device_to_text (UpDevice *device)
g_string_append_printf (string, " energy-full: %g Wh\n", up_exported_device_get_energy_full (priv->proxy_device)); g_string_append_printf (string, " energy-full: %g Wh\n", up_exported_device_get_energy_full (priv->proxy_device));
if (!is_display) if (!is_display)
g_string_append_printf (string, " energy-full-design: %g Wh\n", up_exported_device_get_energy_full_design (priv->proxy_device)); g_string_append_printf (string, " energy-full-design: %g Wh\n", up_exported_device_get_energy_full_design (priv->proxy_device));
if (up_exported_device_get_voltage_min_design (priv->proxy_device) > 0)
g_string_append_printf (string, " voltage-min-design: %g V\n", up_exported_device_get_voltage_min_design (priv->proxy_device));
if (up_exported_device_get_voltage_max_design (priv->proxy_device) > 0)
g_string_append_printf (string, " voltage-max-design: %g V\n", up_exported_device_get_voltage_max_design (priv->proxy_device));
/* Eliminate the display of the deprecation warning specific to the CapacityLevel property. */
/* DEPRECATED. This property is deprecated since it is duplicated from the 'BatteryLevel' property. */
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
capacity_level = up_exported_device_get_capacity_level (priv->proxy_device);
#pragma GCC diagnostic pop
if (capacity_level != NULL && capacity_level[0] != '\0')
g_string_append_printf (string, " capacity-level: %s\n", capacity_level);
} }
if (kind == UP_DEVICE_KIND_BATTERY || if (kind == UP_DEVICE_KIND_BATTERY ||
kind == UP_DEVICE_KIND_MONITOR) kind == UP_DEVICE_KIND_MONITOR)
@ -354,8 +376,12 @@ up_device_to_text (UpDevice *device)
g_string_append_printf (string, " charge-cycles: %s\n", "N/A"); g_string_append_printf (string, " charge-cycles: %s\n", "N/A");
} }
if (kind == UP_DEVICE_KIND_KEYBOARD) { if (kind == UP_DEVICE_KIND_KEYBOARD) {
/* DEPRECATED. This property is deprecated since the code it depends on was removed in 0.99.12. */
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
if (up_exported_device_get_luminosity (priv->proxy_device) > 0) if (up_exported_device_get_luminosity (priv->proxy_device) > 0)
g_string_append_printf (string, " luminosity: %g lx\n", up_exported_device_get_luminosity (priv->proxy_device)); g_string_append_printf (string, " luminosity: %g lx\n", up_exported_device_get_luminosity (priv->proxy_device));
#pragma GCC diagnostic pop
} }
if (kind == UP_DEVICE_KIND_BATTERY || if (kind == UP_DEVICE_KIND_BATTERY ||
kind == UP_DEVICE_KIND_UPS) { kind == UP_DEVICE_KIND_UPS) {
@ -386,6 +412,14 @@ up_device_to_text (UpDevice *device)
if (kind == UP_DEVICE_KIND_BATTERY) { if (kind == UP_DEVICE_KIND_BATTERY) {
if (up_exported_device_get_technology (priv->proxy_device) != UP_DEVICE_TECHNOLOGY_UNKNOWN) if (up_exported_device_get_technology (priv->proxy_device) != UP_DEVICE_TECHNOLOGY_UNKNOWN)
g_string_append_printf (string, " technology: %s\n", up_device_technology_to_string (up_exported_device_get_technology (priv->proxy_device))); g_string_append_printf (string, " technology: %s\n", up_device_technology_to_string (up_exported_device_get_technology (priv->proxy_device)));
if (up_exported_device_get_charge_start_threshold (priv->proxy_device) > 0)
g_string_append_printf (string, " charge-start-threshold: %d%%\n", up_exported_device_get_charge_start_threshold (priv->proxy_device));
if (up_exported_device_get_charge_end_threshold (priv->proxy_device) > 0)
g_string_append_printf (string, " charge-end-threshold: %d%%\n", up_exported_device_get_charge_end_threshold (priv->proxy_device));
if (up_exported_device_get_charge_threshold_enabled (priv->proxy_device))
g_string_append_printf (string, " charge-threshold-enabled: %s\n", up_device_bool_to_string (up_exported_device_get_charge_threshold_enabled (priv->proxy_device)));
if (up_exported_device_get_charge_threshold_supported (priv->proxy_device))
g_string_append_printf (string, " charge-threshold-supported: %s\n", up_device_bool_to_string (up_exported_device_get_charge_threshold_supported (priv->proxy_device)));
} }
if (kind == UP_DEVICE_KIND_LINE_POWER) if (kind == UP_DEVICE_KIND_LINE_POWER)
g_string_append_printf (string, " online: %s\n", up_device_bool_to_string (up_exported_device_get_online (priv->proxy_device))); g_string_append_printf (string, " online: %s\n", up_device_bool_to_string (up_exported_device_get_online (priv->proxy_device)));
@ -663,7 +697,11 @@ up_device_set_property (GObject *object, guint prop_id, const GValue *value, GPa
up_exported_device_set_voltage (device->priv->proxy_device, g_value_get_double (value)); up_exported_device_set_voltage (device->priv->proxy_device, g_value_get_double (value));
break; break;
case PROP_LUMINOSITY: case PROP_LUMINOSITY:
/* DEPRECATED. This property is deprecated since the code it depends on was removed in 0.99.12. */
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
up_exported_device_set_luminosity (device->priv->proxy_device, g_value_get_double (value)); up_exported_device_set_luminosity (device->priv->proxy_device, g_value_get_double (value));
#pragma GCC diagnostic pop
break; break;
case PROP_TIME_TO_EMPTY: case PROP_TIME_TO_EMPTY:
up_exported_device_set_time_to_empty (device->priv->proxy_device, g_value_get_int64 (value)); up_exported_device_set_time_to_empty (device->priv->proxy_device, g_value_get_int64 (value));
@ -692,6 +730,32 @@ up_device_set_property (GObject *object, guint prop_id, const GValue *value, GPa
case PROP_CHARGE_CYCLES: case PROP_CHARGE_CYCLES:
up_exported_device_set_charge_cycles (device->priv->proxy_device, g_value_get_int (value)); up_exported_device_set_charge_cycles (device->priv->proxy_device, g_value_get_int (value));
break; break;
case PROP_CHARGE_START_THRESHOLD:
up_exported_device_set_charge_start_threshold (device->priv->proxy_device, g_value_get_double (value));
break;
case PROP_CHARGE_END_THRESHOLD:
up_exported_device_set_charge_end_threshold (device->priv->proxy_device, g_value_get_double (value));
break;
case PROP_CHARGE_THRESHOLD_ENABLED:
up_exported_device_set_charge_threshold_enabled (device->priv->proxy_device, g_value_get_boolean (value));
break;
case PROP_CHARGE_THRESHOLD_SUPPORTED:
up_exported_device_set_charge_threshold_supported (device->priv->proxy_device, g_value_get_boolean (value));
break;
case PROP_VOLTAGE_MIN_DESIGN:
up_exported_device_set_voltage_min_design (device->priv->proxy_device, g_value_get_double (value));
break;
case PROP_VOLTAGE_MAX_DESIGN:
up_exported_device_set_voltage_max_design (device->priv->proxy_device, g_value_get_double (value));
break;
/* Eliminate the display of the deprecation warning specific to the CapacityLevel property. */
/* DEPRECATED. This property is deprecated since it is duplicated from the 'BatteryLevel' property. */
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
case PROP_CAPACITY_LEVEL:
up_exported_device_set_capacity_level (device->priv->proxy_device, g_value_get_string (value));
break;
#pragma GCC diagnostic pop
default: default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break; break;
@ -783,7 +847,11 @@ up_device_get_property (GObject *object, guint prop_id, GValue *value, GParamSpe
g_value_set_double (value, up_exported_device_get_voltage (device->priv->proxy_device)); g_value_set_double (value, up_exported_device_get_voltage (device->priv->proxy_device));
break; break;
case PROP_LUMINOSITY: case PROP_LUMINOSITY:
/* DEPRECATED. This property is deprecated since the code it depends on was removed in 0.99.12. */
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
g_value_set_double (value, up_exported_device_get_luminosity (device->priv->proxy_device)); g_value_set_double (value, up_exported_device_get_luminosity (device->priv->proxy_device));
#pragma GCC diagnostic pop
break; break;
case PROP_TIME_TO_EMPTY: case PROP_TIME_TO_EMPTY:
g_value_set_int64 (value, up_exported_device_get_time_to_empty (device->priv->proxy_device)); g_value_set_int64 (value, up_exported_device_get_time_to_empty (device->priv->proxy_device));
@ -809,6 +877,32 @@ up_device_get_property (GObject *object, guint prop_id, GValue *value, GParamSpe
case PROP_CHARGE_CYCLES: case PROP_CHARGE_CYCLES:
g_value_set_int (value, up_exported_device_get_charge_cycles (device->priv->proxy_device)); g_value_set_int (value, up_exported_device_get_charge_cycles (device->priv->proxy_device));
break; break;
case PROP_CHARGE_START_THRESHOLD:
g_value_set_uint (value, up_exported_device_get_charge_start_threshold (device->priv->proxy_device));
break;
case PROP_CHARGE_END_THRESHOLD:
g_value_set_uint (value, up_exported_device_get_charge_end_threshold (device->priv->proxy_device));
break;
case PROP_CHARGE_THRESHOLD_ENABLED:
g_value_set_boolean (value, up_exported_device_get_charge_threshold_enabled (device->priv->proxy_device));
break;
case PROP_CHARGE_THRESHOLD_SUPPORTED:
g_value_set_boolean (value, up_exported_device_get_charge_threshold_supported (device->priv->proxy_device));
break;
case PROP_VOLTAGE_MIN_DESIGN:
g_value_set_double (value, up_exported_device_get_voltage_min_design (device->priv->proxy_device));
break;
case PROP_VOLTAGE_MAX_DESIGN:
g_value_set_double (value, up_exported_device_get_voltage_max_design (device->priv->proxy_device));
break;
/* Eliminate the display of the deprecation warning specific to the CapacityLevel property. */
/* DEPRECATED. This property is deprecated since it is duplicated from the 'BatteryLevel' property. */
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
case PROP_CAPACITY_LEVEL:
g_value_set_string (value, up_exported_device_get_capacity_level (device->priv->proxy_device));
break;
#pragma GCC diagnostic pop
default: default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break; break;
@ -935,7 +1029,7 @@ up_device_class_init (UpDeviceClass *klass)
/** /**
* UpDevice:is-rechargeable: * UpDevice:is-rechargeable:
* *
* If the device has a rechargable battery. * If the device has a rechargeable battery.
* *
* Since: 0.9.0 * Since: 0.9.0
**/ **/
@ -1108,13 +1202,16 @@ up_device_class_init (UpDeviceClass *klass)
* *
* The current luminosity of the device. * The current luminosity of the device.
* *
* NOTE: As of 1.91.1, this property is deprecated since the code it
* depends on was removed in 0.99.12.
*
* Since: 0.9.19 * Since: 0.9.19
**/ **/
g_object_class_install_property (object_class, g_object_class_install_property (object_class,
PROP_LUMINOSITY, PROP_LUMINOSITY,
g_param_spec_double ("luminosity", NULL, NULL, g_param_spec_double ("luminosity", NULL, NULL,
0.0, G_MAXDOUBLE, 0.0, 0.0, G_MAXDOUBLE, 0.0,
G_PARAM_READWRITE)); G_PARAM_READWRITE | G_PARAM_DEPRECATED));
/** /**
* UpDevice:time-to-empty: * UpDevice:time-to-empty:
* *
@ -1202,7 +1299,7 @@ up_device_class_init (UpDeviceClass *klass)
/** /**
* UpDevice:icon-name: * UpDevice:icon-name:
* *
* The icon name, following the Icon Naming Speficiation * The icon name, following the Icon Naming Specification
* *
* Since: 1.0 * Since: 1.0
**/ **/
@ -1225,6 +1322,106 @@ up_device_class_init (UpDeviceClass *klass)
g_param_spec_int ("charge-cycles", g_param_spec_int ("charge-cycles",
NULL, NULL, NULL, NULL,
-1, G_MAXINT, -1, G_PARAM_READWRITE)); -1, G_MAXINT, -1, G_PARAM_READWRITE));
/**
* UpDevice:charge-start-threshold:
*
* The charge start threshold of a battery.
*
* Since: 1.90.5
**/
g_object_class_install_property (object_class,
PROP_CHARGE_START_THRESHOLD,
g_param_spec_uint ("charge-start-threshold",
NULL, NULL,
0, 100, 0, G_PARAM_READWRITE));
/**
* UpDevice:charge-end-threshold:
*
* The charge end threshold of a battery.
*
* Since: 1.90.5
**/
g_object_class_install_property (object_class,
PROP_CHARGE_END_THRESHOLD,
g_param_spec_uint ("charge-end-threshold",
NULL, NULL,
0, 100, 100, G_PARAM_READWRITE));
/**
* UpDevice:charge-threshold-enabled:
*
* The charge threshold of a battery is enabled, or false if unknown
* or non-applicable.
*
* Since: 1.90.5
**/
g_object_class_install_property (object_class,
PROP_CHARGE_THRESHOLD_ENABLED,
g_param_spec_boolean ("charge-threshold-enabled",
NULL, NULL,
FALSE,
G_PARAM_READWRITE));
/**
* UpDevice:charge-threshold-supported:
*
* The charge threshold of a battery is supported, or false if unknown
* or non-applicable.
*
* Since: 1.90.5
**/
g_object_class_install_property (object_class,
PROP_CHARGE_THRESHOLD_SUPPORTED,
g_param_spec_boolean ("charge-threshold-supported",
NULL, NULL,
FALSE,
G_PARAM_READWRITE));
/**
* UpDevice:voltage_min_design:
*
* The minimum supported voltage of the device as reported by the kernel.
*
* Since: 1.90.10
**/
g_object_class_install_property (object_class,
PROP_VOLTAGE_MIN_DESIGN,
g_param_spec_double ("voltage-min-design", NULL, NULL,
0.0, G_MAXDOUBLE, 0.0,
G_PARAM_READWRITE));
/**
* UpDevice:voltage_max_design:
*
* The maximum supported voltage of the device as reported by the kernel.
*
* Since: 1.90.10
**/
g_object_class_install_property (object_class,
PROP_VOLTAGE_MAX_DESIGN,
g_param_spec_double ("voltage-max-design", NULL, NULL,
0.0, G_MAXDOUBLE, 0.0,
G_PARAM_READWRITE));
/**
* UpDevice:capacity-level:
*
* Coarse representation of battery capacity. The value is one of the following:
* Unknown, Critical, Low, Normal, High, and Full.
*
* Since: 1.90.10
*
* DEPRECATED.
* This property is deprecated since it is duplicated from the 'BatteryLevel' property.
**/
g_object_class_install_property (object_class,
PROP_CAPACITY_LEVEL,
g_param_spec_string ("capacity-level",
NULL, NULL, NULL,
G_PARAM_READWRITE | G_PARAM_DEPRECATED));
} }
static void static void

View file

@ -94,4 +94,3 @@ const gchar *up_device_get_object_path (UpDevice *device);
G_END_DECLS G_END_DECLS
#endif /* __UP_DEVICE_H */ #endif /* __UP_DEVICE_H */

View file

@ -352,4 +352,3 @@ up_history_item_new (void)
{ {
return UP_HISTORY_ITEM (g_object_new (UP_TYPE_HISTORY_ITEM, NULL)); return UP_HISTORY_ITEM (g_object_new (UP_TYPE_HISTORY_ITEM, NULL));
} }

View file

@ -69,4 +69,3 @@ gboolean up_history_item_set_from_string (UpHistoryItem *history_item,
G_END_DECLS G_END_DECLS
#endif /* __UP_HISTORY_ITEM_H */ #endif /* __UP_HISTORY_ITEM_H */

View file

@ -226,4 +226,3 @@ up_stats_item_new (void)
{ {
return UP_STATS_ITEM (g_object_new (UP_TYPE_STATS_ITEM, NULL)); return UP_STATS_ITEM (g_object_new (UP_TYPE_STATS_ITEM, NULL));
} }

View file

@ -61,4 +61,3 @@ void up_stats_item_set_accuracy (UpStatsItem *stats_item,
G_END_DECLS G_END_DECLS
#endif /* __UP_STATS_ITEM_H */ #endif /* __UP_STATS_ITEM_H */

View file

@ -139,4 +139,3 @@ UpDeviceLevel up_device_level_from_string (const gchar *level);
G_END_DECLS G_END_DECLS
#endif /* __UP_TYPES_H */ #endif /* __UP_TYPES_H */

View file

@ -45,4 +45,3 @@
#undef __UPOWER_H_INSIDE__ #undef __UPOWER_H_INSIDE__
#endif /* __UPOWER_H__ */ #endif /* __UPOWER_H__ */

View file

@ -1,5 +1,5 @@
project('upower', 'c', project('upower', 'c',
version: '1.90.4', version: '1.91.0',
license: 'GPLv2+', license: 'GPLv2+',
default_options: [ default_options: [
'buildtype=debugoptimized', 'buildtype=debugoptimized',
@ -10,7 +10,7 @@ project('upower', 'c',
soversion = 3 soversion = 3
current = 1 current = 1
revision = 0 revision = 1
libversion = '@0@.@1@.@2@'.format(soversion, current, revision) libversion = '@0@.@1@.@2@'.format(soversion, current, revision)
gnome = import('gnome') gnome = import('gnome')
@ -30,7 +30,7 @@ cdata.set_quoted('PACKAGE_VERSION', meson.project_version())
cdata.set_quoted('VERSION', meson.project_version()) cdata.set_quoted('VERSION', meson.project_version())
cdata.set_quoted('PACKAGE_SYSCONF_DIR', get_option('sysconfdir')) cdata.set_quoted('PACKAGE_SYSCONF_DIR', get_option('sysconfdir'))
glib_min_version = '2.66' glib_min_version = '2.76'
glib_version_def = 'GLIB_VERSION_@0@_@1@'.format( glib_version_def = 'GLIB_VERSION_@0@_@1@'.format(
glib_min_version.split('.')[0], glib_min_version.split('.')[1]) glib_min_version.split('.')[0], glib_min_version.split('.')[1])
@ -47,6 +47,16 @@ gio_dep = dependency('gio-2.0', version: '>=' + glib_min_version)
gio_unix_dep = dependency('gio-unix-2.0', version: '>=' + glib_min_version) gio_unix_dep = dependency('gio-unix-2.0', version: '>=' + glib_min_version)
m_dep = cc.find_library('m', required: true) m_dep = cc.find_library('m', required: true)
polkit = dependency('polkit-gobject-1', version: '>= 0.103',
required: get_option('polkit').disable_auto_if(host_machine.system() != 'linux'))
if polkit.found()
cdata.set('HAVE_POLKIT', '1')
if polkit.version().version_compare('>= 0.114')
cdata.set('HAVE_POLKIT_0_114', '1')
endif
cdata.set_quoted ('POLKIT_ACTIONDIR', polkit.get_variable(pkgconfig: 'actiondir'))
endif
xsltproc = find_program('xsltproc', disabler: true, required: get_option('gtk-doc') or get_option('man')) xsltproc = find_program('xsltproc', disabler: true, required: get_option('gtk-doc') or get_option('man'))
# Resolve OS backend # Resolve OS backend
@ -100,6 +110,11 @@ if historydir == ''
historydir = get_option('prefix') / get_option('localstatedir') / 'lib' / 'upower' historydir = get_option('prefix') / get_option('localstatedir') / 'lib' / 'upower'
endif endif
statedir = get_option('statedir')
if statedir == ''
statedir = get_option('prefix') / get_option('localstatedir') / 'lib' / 'upower'
endif
dbusdir = get_option('datadir') / 'dbus-1' dbusdir = get_option('datadir') / 'dbus-1'
systemdsystemunitdir = get_option('systemdsystemunitdir') systemdsystemunitdir = get_option('systemdsystemunitdir')
if systemdsystemunitdir == '' if systemdsystemunitdir == ''
@ -107,6 +122,11 @@ if systemdsystemunitdir == ''
systemdsystemunitdir = systemd_dep.get_variable(pkgconfig: 'systemdsystemunitdir') systemdsystemunitdir = systemd_dep.get_variable(pkgconfig: 'systemdsystemunitdir')
endif endif
datadir = get_option('datadir')
if datadir == ''
datadir = join_paths(prefix, get_option('datadir'))
endif
# Generate configuration file # Generate configuration file
config_h = configure_file(output: 'config.h', configuration: cdata) config_h = configure_file(output: 'config.h', configuration: cdata)
@ -118,6 +138,8 @@ subdir('libupower-glib')
subdir('src') subdir('src')
subdir('tools') subdir('tools')
subdir('doc') subdir('doc')
subdir('data/zsh-completion')
subdir('policy')
pkgconfig = import('pkgconfig') pkgconfig = import('pkgconfig')
pkgconfig.generate( pkgconfig.generate(
@ -125,7 +147,7 @@ pkgconfig.generate(
description: 'UPower is a system daemon for managing power devices', description: 'UPower is a system daemon for managing power devices',
version: meson.project_version(), version: meson.project_version(),
libraries: libupower_glib, libraries: libupower_glib,
requires: [glib_dep, gobject_dep], requires: [glib_dep, gobject_dep, polkit],
subdirs: 'libupower-glib', subdirs: 'libupower-glib',
) )

View file

@ -1,10 +1,10 @@
option('man', option('man',
type : 'boolean', type : 'boolean',
value : 'true', value : true,
description : 'Build manpages') description : 'Build manpages')
option('gtk-doc', option('gtk-doc',
type : 'boolean', type : 'boolean',
value : 'true', value : true,
description : 'Build developer documentation') description : 'Build developer documentation')
option('introspection', option('introspection',
type : 'feature', type : 'feature',
@ -21,6 +21,9 @@ option('udevhwdbdir',
option('historydir', option('historydir',
type : 'string', type : 'string',
description : 'Directory for upower history files will be stored') description : 'Directory for upower history files will be stored')
option('statedir',
type : 'string',
description : 'Directory for upower status files will be stored')
option('systemdsystemunitdir', option('systemdsystemunitdir',
type : 'string', type : 'string',
description : 'Directory for systemd service files ("no" to disable)') description : 'Directory for systemd service files ("no" to disable)')
@ -33,3 +36,15 @@ option('idevice',
type : 'feature', type : 'feature',
value : 'auto', value : 'auto',
description : 'Build with libimobiledevice') description : 'Build with libimobiledevice')
option('polkit',
type: 'feature',
value: 'auto',
description: 'PolKit support in daemon')
option('zshcompletiondir',
type: 'string',
value: '',
description: 'Directory for zsh completion scripts ["no" disables]')
option('installed_tests',
type : 'boolean',
value : true,
description : 'Install integration tests')

View file

@ -2,6 +2,7 @@
# #
fr fr
it it
ka
sv sv
pl pl

24
policy/meson.build Normal file
View file

@ -0,0 +1,24 @@
if polkit.found()
#newer polkit has the ITS rules included
if polkit.version().version_compare('>0.113')
i18n.merge_file(
input: 'org.freedesktop.upower.policy.in',
output: 'org.freedesktop.upower.policy',
install: true,
install_dir: join_paths(datadir, 'polkit-1', 'actions') ,
type: 'xml',
po_dir: join_paths(meson.project_source_root(), 'po')
)
#older polkit is missing ITS rules and will fail
else
i18n.merge_file(
input: 'org.freedesktop.upower.policy.in',
output: 'org.freedesktop.upower.policy',
install: true,
install_dir: join_paths(datadir, 'polkit-1', 'actions') ,
type: 'xml',
data_dirs: join_paths(meson.project_source_root(), 'policy'),
po_dir: join_paths(meson.project_source_root(), 'po')
)
endif
endif

View file

@ -0,0 +1,32 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE policyconfig PUBLIC
"-//freedesktop//DTD PolicyKit Policy Configuration 1.0//EN"
"http://www.freedesktop.org/standards/PolicyKit/1.0/policyconfig.dtd">
<!--
Policy definitions for UPower
Copyright (c) 2008 David Zeuthen <david@fubar.dk>
Copyright (c) 2008-2010 Richard Hughes <richard@hughsie.com>
NOTE: If you make changes to this file, make sure to validate the file
using the polkit-policy-file-validate(1) tool. Changes made to this
file are instantly applied.
-->
<policyconfig>
<vendor>The UPower Project</vendor>
<vendor_url>http://upower.freedesktop.org/</vendor_url>
<icon_name>system-suspend</icon_name>
<action id="org.freedesktop.UPower.enable-charging-limit">
<description>Enable battery charging limit</description>
<message>Authentication is required to set battery charging start and end limit.</message>
<defaults>
<allow_inactive>no</allow_inactive>
<allow_active>yes</allow_active>
</defaults>
</action>
</policyconfig>

View file

@ -0,0 +1,35 @@
# This file is part of upower.
#
# The lookup keys are composed in:
# 60-upower-battery.rules
#
# Match string format:
# battery:<kernel>:<model_name>:dmi:<dmi pattern>
#
# The kernel is the name of battery in /sys/class/power_supply for
# differentiating between multiple batteries.
#
# The model_name is battery model name in for example
# /sys/class/power_supply/BAT0/model_name.
#
# The full DMI string of the running machine can be read from
# /sys/class/dmi/id/modalias
# That requires a kernel built with CONFIG_DMIID set, which is common.
# The full DMI string is not needed here and the meaning of individual parts
# can be seen in the source of the DMIID kernel module
# https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/firmware/dmi-id.c
#
# To add local overrides, create a new file
# /etc/udev/hwdb.d/61-battery-local.hwdb
# and add your rules there. To load the new rules execute (as root):
# systemd-hwdb update
# udevadm trigger -v -p /sys/class/power_supply/BAT
# where BAT is the battery in question.
#
# CHARGE_LIMIT is in tuple format and each of the variables can be disabled by "_" character.
# For example:
# CHARGE_LIMIT=60,80 (charge_control_start_threshold is 60 and charge_control_end_threshold is 80.)
# CHARGE_LIMIT=_,80 (charge_control_start_threshold will be skipped and charge_control_end_threshold is 80.)
battery:*:*:dmi:*
CHARGE_LIMIT=75,80

View file

@ -0,0 +1,7 @@
ACTION=="remove", GOTO="battery_end"
SUBSYSTEM=="power_supply", ATTR{charge_control_start_threshold}!="", \
IMPORT{builtin}="hwdb 'battery:$kernel:$attr{model_name}:$attr{[dmi/id]modalias}'", \
GOTO="battery_end"
LABEL="battery_end"

View file

@ -72,6 +72,7 @@ usb:v051Dp0000*
usb:v051Dp0002* usb:v051Dp0002*
usb:v051Dp0003* usb:v051Dp0003*
usb:v051Dp0004* usb:v051Dp0004*
usb:v051Dp0005*
UPOWER_BATTERY_TYPE=ups UPOWER_BATTERY_TYPE=ups
UPOWER_VENDOR=APC UPOWER_VENDOR=APC
@ -141,6 +142,11 @@ usb:v09AEp4008*
UPOWER_BATTERY_TYPE=ups UPOWER_BATTERY_TYPE=ups
UPOWER_VENDOR=TrippLite UPOWER_VENDOR=TrippLite
# KSTAR under Berkeley Varitronics Systems ID
usb:v09D6p0001*
UPOWER_BATTERY_TYPE=ups
UPOWER_VENDOR=KSTAR under Berkeley Varitronics Systems ID
# PowerCOM # PowerCOM
usb:v0D9Fp0001* usb:v0D9Fp0001*
usb:v0D9Fp0004* usb:v0D9Fp0004*
@ -202,6 +208,11 @@ usb:v2E66p0302*
UPOWER_BATTERY_TYPE=ups UPOWER_BATTERY_TYPE=ups
UPOWER_VENDOR=Salicru UPOWER_VENDOR=Salicru
# EcoFlow
usb:v3746pFFFF*
UPOWER_BATTERY_TYPE=ups
UPOWER_VENDOR=EcoFlow
# Powervar # Powervar
usb:v4234p0002* usb:v4234p0002*
UPOWER_BATTERY_TYPE=ups UPOWER_BATTERY_TYPE=ups

View file

@ -1,10 +1,12 @@
rules = [ rules = [
'60-upower-battery.rules',
'95-upower-wup.rules', '95-upower-wup.rules',
'95-upower-hid.rules', '95-upower-hid.rules',
] ]
hwdb = [ hwdb = [
'95-upower-hid.hwdb', '95-upower-hid.hwdb',
'60-upower-battery.hwdb',
] ]
if os_backend == 'linux' if os_backend == 'linux'

View file

@ -37,4 +37,3 @@ up_native_get_native_path (GObject *object)
{ {
return "/sys/dummy"; return "/sys/dummy";
} }

View file

@ -228,4 +228,3 @@ static void up_acpi_native_get_property (GObject * object, guint property_id, GV
break; break;
} }
} }

View file

@ -383,4 +383,3 @@ up_backend_new (void)
{ {
return g_object_new (UP_TYPE_BACKEND, NULL); return g_object_new (UP_TYPE_BACKEND, NULL);
} }

View file

@ -52,4 +52,3 @@ GType up_device_supply_get_type (void);
G_END_DECLS G_END_DECLS
#endif /* __UP_DEVICE_SUPPLY_H__ */ #endif /* __UP_DEVICE_SUPPLY_H__ */

View file

@ -118,4 +118,3 @@ up_get_string_sysctl (GError **err, const gchar *format, ...)
return g_strdup ("asdf"); return g_strdup ("asdf");
#endif #endif
} }

File diff suppressed because it is too large Load diff

View file

@ -18,6 +18,8 @@ upshared += { 'linux': static_library('upshared',
'up-device-wup.h', 'up-device-wup.h',
'up-device-bluez.c', 'up-device-bluez.c',
'up-device-bluez.h', 'up-device-bluez.h',
'up-kbd-backlight-led.c',
'up-kbd-backlight-led.h',
'up-input.c', 'up-input.c',
'up-input.h', 'up-input.h',
'up-backend.c', 'up-backend.c',

View file

@ -26,21 +26,27 @@ import threading
import select import select
import errno import errno
class OutputChecker(object):
class OutputChecker(object):
def __init__(self, out=sys.stdout): def __init__(self, out=sys.stdout):
self._output = out self._output = out
self._pipe_fd_r, self._pipe_fd_w = os.pipe() self._pipe_fd_r, self._pipe_fd_w = os.pipe()
self._partial_buf = b'' self._partial_buf = b""
self._lines_sem = threading.Semaphore() self._lines_sem = threading.Semaphore()
self._lines = [] self._lines = []
self._reader_io = io.StringIO() self._reader_io = io.StringIO()
# Just to be sure, shouldn't be a problem even if we didn't set it # Just to be sure, shouldn't be a problem even if we didn't set it
fcntl.fcntl(self._pipe_fd_r, fcntl.F_SETFL, fcntl.fcntl(
fcntl.fcntl(self._pipe_fd_r, fcntl.F_GETFL) | os.O_CLOEXEC | os.O_NONBLOCK) self._pipe_fd_r,
fcntl.fcntl(self._pipe_fd_w, fcntl.F_SETFL, fcntl.F_SETFL,
fcntl.fcntl(self._pipe_fd_w, fcntl.F_GETFL) | os.O_CLOEXEC) fcntl.fcntl(self._pipe_fd_r, fcntl.F_GETFL) | os.O_CLOEXEC | os.O_NONBLOCK,
)
fcntl.fcntl(
self._pipe_fd_w,
fcntl.F_SETFL,
fcntl.fcntl(self._pipe_fd_w, fcntl.F_GETFL) | os.O_CLOEXEC,
)
# Start copier thread # Start copier thread
self._thread = threading.Thread(target=self._copy, daemon=True) self._thread = threading.Thread(target=self._copy, daemon=True)
@ -73,7 +79,7 @@ class OutputChecker(object):
self._lines_sem.release() self._lines_sem.release()
return return
l = r.split(b'\n') l = r.split(b"\n")
l[0] = self._partial_buf + l[0] l[0] = self._partial_buf + l[0]
self._lines.extend(l[:-1]) self._lines.extend(l[:-1])
self._partial_buf = l[-1] self._partial_buf = l[-1]
@ -86,7 +92,7 @@ class OutputChecker(object):
deadline = time.time() + timeout deadline = time.time() + timeout
if isinstance(needle_re, str): if isinstance(needle_re, str):
needle_re = needle_re.encode('ascii') needle_re = needle_re.encode("ascii")
r = re.compile(needle_re) r = re.compile(needle_re)
ret = [] ret = []
@ -98,16 +104,23 @@ class OutputChecker(object):
# EOF, throw error # EOF, throw error
if self._pipe_fd_r == -1: if self._pipe_fd_r == -1:
if failmsg: if failmsg:
raise AssertionError("No further messages: " % failmsg) from None raise AssertionError(
"No further messages: " % failmsg
) from None
else: else:
raise AssertionError('No client waiting for needle %s' % (str(needle_re))) from None raise AssertionError(
"No client waiting for needle %s" % (str(needle_re))
) from None
# Check if should wake up # Check if should wake up
if not self._lines_sem.acquire(timeout = deadline - time.time()): if not self._lines_sem.acquire(timeout=deadline - time.time()):
if failmsg: if failmsg:
raise AssertionError(failmsg) from None raise AssertionError(failmsg) from None
else: else:
raise AssertionError('Timed out waiting for needle %s (timeout: %0.2f)' % (str(needle_re), timeout)) from None raise AssertionError(
"Timed out waiting for needle %s (timeout: %0.2f)"
% (str(needle_re), timeout)
) from None
continue continue
ret.append(l) ret.append(l)
@ -116,7 +129,7 @@ class OutputChecker(object):
def check_line(self, needle, timeout=0, failmsg=None): def check_line(self, needle, timeout=0, failmsg=None):
if isinstance(needle, str): if isinstance(needle, str):
needle = needle.encode('ascii') needle = needle.encode("ascii")
needle_re = re.escape(needle) needle_re = re.escape(needle)
@ -126,7 +139,7 @@ class OutputChecker(object):
deadline = time.time() + wait deadline = time.time() + wait
if isinstance(needle_re, str): if isinstance(needle_re, str):
needle_re = needle_re.encode('ascii') needle_re = needle_re.encode("ascii")
r = re.compile(needle_re) r = re.compile(needle_re)
ret = [] ret = []
@ -140,7 +153,7 @@ class OutputChecker(object):
break break
# Check if should wake up # Check if should wake up
if not self._lines_sem.acquire(timeout = deadline - time.time()): if not self._lines_sem.acquire(timeout=deadline - time.time()):
# Timed out, so everything is good # Timed out, so everything is good
break break
continue continue
@ -150,13 +163,16 @@ class OutputChecker(object):
if failmsg: if failmsg:
raise AssertionError(failmsg) raise AssertionError(failmsg)
else: else:
raise AssertionError('Found needle %s but shouldn\'t have been there (timeout: %0.2f)' % (str(needle_re), wait)) raise AssertionError(
"Found needle %s but shouldn't have been there (timeout: %0.2f)"
% (str(needle_re), wait)
)
return ret return ret
def check_no_line(self, needle, wait=0, failmsg=None): def check_no_line(self, needle, wait=0, failmsg=None):
if isinstance(needle, str): if isinstance(needle, str):
needle = needle.encode('ascii') needle = needle.encode("ascii")
needle_re = re.escape(needle) needle_re = re.escape(needle)
@ -174,7 +190,6 @@ class OutputChecker(object):
raise AssertionError("OutputCheck: Write side has not been closed yet!") raise AssertionError("OutputCheck: Write side has not been closed yet!")
def force_close(self): def force_close(self):
fd = self._pipe_fd_r fd = self._pipe_fd_r
self._pipe_fd_r = -1 self._pipe_fd_r = -1
if fd >= 0: if fd >= 0:

View file

@ -22,23 +22,25 @@ import inspect
import os import os
import unittest import unittest
def list_tests(module): def list_tests(module):
tests = [] tests = []
for name, obj in inspect.getmembers(module): for name, obj in inspect.getmembers(module):
if inspect.isclass(obj) and issubclass(obj, unittest.TestCase): if inspect.isclass(obj) and issubclass(obj, unittest.TestCase):
cases = unittest.defaultTestLoader.getTestCaseNames(obj) cases = unittest.defaultTestLoader.getTestCaseNames(obj)
tests += [ (obj, '{}.{}'.format(name, t)) for t in cases ] tests += [(obj, "{}.{}".format(name, t)) for t in cases]
return tests return tests
if __name__ == '__main__': if __name__ == "__main__":
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument('unittest_source', type=argparse.FileType('r')) parser.add_argument("unittest_source", type=argparse.FileType("r"))
args = parser.parse_args() args = parser.parse_args()
source_path = args.unittest_source.name source_path = args.unittest_source.name
spec = importlib.util.spec_from_file_location( spec = importlib.util.spec_from_file_location(
os.path.basename(source_path), source_path) os.path.basename(source_path), source_path
)
module = importlib.util.module_from_spec(spec) module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module) spec.loader.exec_module(module)

View file

@ -33,6 +33,7 @@
#include "up-backend.h" #include "up-backend.h"
#include "up-daemon.h" #include "up-daemon.h"
#include "up-device.h" #include "up-device.h"
#include "up-device-kbd-backlight.h"
#include "up-enumerator-udev.h" #include "up-enumerator-udev.h"
@ -421,9 +422,13 @@ up_device_disconnected_cb (GObject *gobject,
NULL); NULL);
if (disconnected) { if (disconnected) {
g_debug("Device %s became disconnected, hiding device", path); g_debug("Device %s became disconnected, hiding device", path);
if (up_device_is_registered (UP_DEVICE (gobject))) { if (UP_IS_DEVICE (gobject)) {
g_signal_emit (backend, signals[SIGNAL_DEVICE_REMOVED], 0, gobject); if (up_device_is_registered (UP_DEVICE (gobject))) {
up_device_unregister (UP_DEVICE (gobject)); g_signal_emit (backend, signals[SIGNAL_DEVICE_REMOVED], 0, gobject);
up_device_unregister (UP_DEVICE (gobject));
}
} else if (UP_IS_DEVICE_KBD_BACKLIGHT (gobject)) {
up_device_kbd_backlight_unregister (UP_DEVICE_KBD_BACKLIGHT (gobject));
} }
} else { } else {
g_debug ("Device %s became connected, showing device", path); g_debug ("Device %s became connected, showing device", path);
@ -433,20 +438,28 @@ up_device_disconnected_cb (GObject *gobject,
} }
static void static void
udev_device_added_cb (UpBackend *backend, UpDevice *device) udev_device_added_cb (UpBackend *backend, GObject *device)
{ {
g_debug ("Got new device from udev enumerator: %p", device); g_debug ("Got new device from udev enumerator: %p", device);
g_signal_connect (device, "notify::disconnected", g_signal_connect (device, "notify::disconnected",
G_CALLBACK (up_device_disconnected_cb), backend); G_CALLBACK (up_device_disconnected_cb), backend);
if (update_added_duplicate_device (backend, device)) if (UP_IS_DEVICE (device)) {
if (update_added_duplicate_device (backend, UP_DEVICE (device)))
g_signal_emit (backend, signals[SIGNAL_DEVICE_ADDED], 0, device);
} else if (UP_IS_DEVICE_KBD_BACKLIGHT (device)) {
g_signal_emit (backend, signals[SIGNAL_DEVICE_ADDED], 0, device); g_signal_emit (backend, signals[SIGNAL_DEVICE_ADDED], 0, device);
} else {
g_warning ("Unknown device type");
}
} }
static void static void
udev_device_removed_cb (UpBackend *backend, UpDevice *device) udev_device_removed_cb (UpBackend *backend, GObject *device)
{ {
g_debug ("Removing device from udev enumerator: %p", device); g_debug ("Removing device from udev enumerator: %p", device);
update_removed_duplicate_device (backend, device);
if (UP_IS_DEVICE (device))
update_removed_duplicate_device (backend, UP_DEVICE (device));
g_signal_emit (backend, signals[SIGNAL_DEVICE_REMOVED], 0, device); g_signal_emit (backend, signals[SIGNAL_DEVICE_REMOVED], 0, device);
} }
@ -553,24 +566,38 @@ up_backend_get_critical_action (UpBackend *backend)
const gchar *method; const gchar *method;
const gchar *can_method; const gchar *can_method;
} actions[] = { } actions[] = {
{ "Suspend", "CanSuspend" },
{ "HybridSleep", "CanHybridSleep" }, { "HybridSleep", "CanHybridSleep" },
{ "Hibernate", "CanHibernate" }, { "Hibernate", "CanHibernate" },
{ "PowerOff", NULL }, { "PowerOff", NULL },
{ "Ignore", NULL },
}; };
guint i = 0; g_autofree gchar *action = NULL;
char *action; gboolean can_risky = FALSE;
guint i = 1;
g_return_val_if_fail (backend->priv->logind_proxy != NULL, NULL); g_return_val_if_fail (backend->priv->logind_proxy != NULL, NULL);
/* Find the configured action first */ can_risky = up_config_get_boolean (backend->priv->config,
"AllowRiskyCriticalPowerAction");
/* find the configured action first */
action = up_config_get_string (backend->priv->config, "CriticalPowerAction"); action = up_config_get_string (backend->priv->config, "CriticalPowerAction");
/* safeguard for the risky actions */
if (!can_risky) {
if (!g_strcmp0 (action, "Suspend") || !g_strcmp0 (action, "Ignore")) {
g_free (action);
action = g_strdup_printf ("HybridSleep");
}
}
if (action != NULL) { if (action != NULL) {
for (i = 0; i < G_N_ELEMENTS (actions); i++) for (i = 0; i < G_N_ELEMENTS (actions); i++)
if (g_str_equal (actions[i].method, action)) if (g_str_equal (actions[i].method, action))
break; break;
if (i >= G_N_ELEMENTS (actions)) if (i >= G_N_ELEMENTS (actions))
i = 0; i = 1;
g_free (action);
} }
for (; i < G_N_ELEMENTS (actions); i++) { for (; i < G_N_ELEMENTS (actions); i++) {
@ -613,6 +640,12 @@ up_backend_take_action (UpBackend *backend)
/* Take action */ /* Take action */
g_debug ("About to call logind method %s", method); g_debug ("About to call logind method %s", method);
/* Do nothing if the action is set to "Ignore" */
if (g_strcmp0 (method, "Ignore") == 0) {
return;
}
g_dbus_proxy_call (backend->priv->logind_proxy, g_dbus_proxy_call (backend->priv->logind_proxy,
method, method,
g_variant_new ("(b)", FALSE), g_variant_new ("(b)", FALSE),
@ -755,13 +788,13 @@ up_backend_class_init (UpBackendClass *klass)
G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST, G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (UpBackendClass, device_added), G_STRUCT_OFFSET (UpBackendClass, device_added),
NULL, NULL, NULL, NULL, NULL, NULL,
G_TYPE_NONE, 1, UP_TYPE_DEVICE); G_TYPE_NONE, 1, G_TYPE_OBJECT);
signals [SIGNAL_DEVICE_REMOVED] = signals [SIGNAL_DEVICE_REMOVED] =
g_signal_new ("device-removed", g_signal_new ("device-removed",
G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST, G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (UpBackendClass, device_removed), G_STRUCT_OFFSET (UpBackendClass, device_removed),
NULL, NULL, NULL, NULL, NULL, NULL,
G_TYPE_NONE, 1, UP_TYPE_DEVICE); G_TYPE_NONE, 1, G_TYPE_OBJECT);
} }
static void static void
@ -843,4 +876,3 @@ up_backend_new (void)
{ {
return g_object_new (UP_TYPE_BACKEND, NULL); return g_object_new (UP_TYPE_BACKEND, NULL);
} }

View file

@ -52,4 +52,3 @@ void up_device_bluez_update (UpDeviceBluez *bluez,
G_END_DECLS G_END_DECLS
#endif /* __UP_DEVICE_BLUEZ_H__ */ #endif /* __UP_DEVICE_BLUEZ_H__ */

View file

@ -218,7 +218,7 @@ up_device_hid_get_all_data (UpDeviceHid *hid)
rinfo.report_type = rtype; rinfo.report_type = rtype;
rinfo.report_id = HID_REPORT_ID_FIRST; rinfo.report_id = HID_REPORT_ID_FIRST;
while (ioctl (hid->priv->fd, HIDIOCGREPORTINFO, &rinfo) >= 0) { while (ioctl (hid->priv->fd, HIDIOCGREPORTINFO, &rinfo) >= 0) {
for (i = 0; i < rinfo.num_fields; i++) { for (i = 0; i < rinfo.num_fields; i++) {
memset (&finfo, 0, sizeof (finfo)); memset (&finfo, 0, sizeof (finfo));
finfo.report_type = rinfo.report_type; finfo.report_type = rinfo.report_type;
finfo.report_id = rinfo.report_id; finfo.report_id = rinfo.report_id;

View file

@ -51,4 +51,3 @@ GType up_device_hid_get_type (void);
G_END_DECLS G_END_DECLS
#endif /* __UP_DEVICE_HID_H__ */ #endif /* __UP_DEVICE_HID_H__ */

View file

@ -53,4 +53,3 @@ GType up_device_idevice_get_type (void);
G_END_DECLS G_END_DECLS
#endif /* __UP_DEVICE_IDEVICE_H__ */ #endif /* __UP_DEVICE_IDEVICE_H__ */

View file

@ -45,6 +45,28 @@ enum {
PROP_IGNORE_SYSTEM_PERCENTAGE PROP_IGNORE_SYSTEM_PERCENTAGE
}; };
typedef enum {
UP_DEVICE_SUPPLY_BATTERY_CHARGE_TYPES_0,
UP_DEVICE_SUPPLY_BATTERY_CHARGE_TYPES_UNKNOWN = 1 << 0,
UP_DEVICE_SUPPLY_BATTERY_CHARGE_TYPES_NA = 1 << 1,
UP_DEVICE_SUPPLY_BATTERY_CHARGE_TYPES_TRICKLE = 1 << 2,
UP_DEVICE_SUPPLY_BATTERY_CHARGE_TYPES_FAST = 1 << 3,
UP_DEVICE_SUPPLY_BATTERY_CHARGE_TYPES_STANDARD = 1 << 4,
UP_DEVICE_SUPPLY_BATTERY_CHARGE_TYPES_ADAPTIVE = 1 << 5,
UP_DEVICE_SUPPLY_BATTERY_CHARGE_TYPES_CUSTOM = 1 << 6,
UP_DEVICE_SUPPLY_BATTERY_CHARGE_TYPES_LONG_LIFE = 1 << 7,
UP_DEVICE_SUPPLY_BATTERY_CHARGE_TYPES_BYPASS = 1 << 8,
UP_DEVICE_SUPPLY_BATTERY_CHARGE_TYPE_LAST,
} UpDeviceSupplyBatteryChargeTypes;
typedef enum {
UP_DEVICE_SUPPLY_BATTERY_CHARGE_THRESHOLD_SETTINGS_0,
UP_DEVICE_SUPPLY_BATTERY_CHARGE_THRESHOLD_SETTINGS_CHARGE_CONTROL_START_THRESHOLD = 1 << 0,
UP_DEVICE_SUPPLY_BATTERY_CHARGE_THRESHOLD_SETTINGS_CHARGE_CONTROL_END_THRESHOLD = 1 << 1,
UP_DEVICE_SUPPLY_BATTERY_CHARGE_THRESHOLD_SETTINGS_CHARGE_TYPES = 1 << 2,
UP_DEVICE_SUPPLY_BATTERY_CHARGE_THRESHOLD_SETTINGS_LAST,
} UpDeviceSupplyBatteryChargeThresholdSettings;
struct _UpDeviceSupplyBattery struct _UpDeviceSupplyBattery
{ {
UpDeviceBattery parent; UpDeviceBattery parent;
@ -53,6 +75,10 @@ struct _UpDeviceSupplyBattery
gdouble *energy_old; gdouble *energy_old;
guint energy_old_first; guint energy_old_first;
gdouble rate_old; gdouble rate_old;
guint supported_charge_types;
UpDeviceSupplyBatteryChargeTypes charge_type;
UpDeviceSupplyBatteryChargeThresholdSettings charge_threshold_settings;
gboolean charge_threshold_by_charge_type;
gboolean shown_invalid_voltage_warning; gboolean shown_invalid_voltage_warning;
gboolean ignore_system_percentage; gboolean ignore_system_percentage;
}; };
@ -127,6 +153,233 @@ get_sysfs_attr_uncached (GUdevDevice *native, const gchar *key)
return g_steal_pointer (&value); return g_steal_pointer (&value);
} }
static gboolean
up_device_supply_battery_convert_to_double (const gchar *str_value, gdouble *value)
{
gdouble conv_value;
gchar *end = NULL;
if (str_value == NULL || value == NULL)
return FALSE;
conv_value = g_ascii_strtod (str_value, &end);
if (end == str_value || conv_value < 0.0 || conv_value > 100.0)
return FALSE;
*value = conv_value;
return TRUE;
}
static gboolean
up_device_supply_battery_get_charge_control_limits (GUdevDevice *native, UpBatteryInfo *info)
{
const gchar *charge_limit;
g_auto(GStrv) pairs = NULL;
gdouble charge_control_start_threshold;
gdouble charge_control_end_threshold;
charge_limit = g_udev_device_get_property (native, "CHARGE_LIMIT");
if (charge_limit == NULL)
return FALSE;
pairs = g_strsplit (charge_limit, ",", 0);
if (g_strv_length (pairs) != 2) {
g_warning("Could not parse CHARGE_LIMIT, expected 'number,number', got '%s'", charge_limit);
return FALSE;
}
if (g_strcmp0 (pairs[0], "_") == 0) {
g_debug ("charge_control_start_threshold is disabled");
charge_control_start_threshold = G_MAXUINT;
} else if (!up_device_supply_battery_convert_to_double (pairs[0], &charge_control_start_threshold)) {
g_warning ("failed to convert charge_control_start_threshold: %s", pairs[0]);
return FALSE;
}
if (g_strcmp0 (pairs[1], "_") == 0) {
g_debug ("charge_control_end_threshold is disabled");
charge_control_end_threshold = G_MAXUINT;
} else if (!up_device_supply_battery_convert_to_double (pairs[1], &charge_control_end_threshold)) {
g_warning ("failed to convert charge_control_start_threshold: %s", pairs[0]);
return FALSE;
}
info->charge_control_start_threshold = charge_control_start_threshold;
info->charge_control_end_threshold = charge_control_end_threshold;
if (g_udev_device_has_sysfs_attr (native, "charge_control_start_threshold"))
info->charge_threshold_settings |= UP_DEVICE_SUPPLY_BATTERY_CHARGE_THRESHOLD_SETTINGS_CHARGE_CONTROL_START_THRESHOLD;
if (g_udev_device_has_sysfs_attr (native, "charge_control_end_threshold"))
info->charge_threshold_settings |= UP_DEVICE_SUPPLY_BATTERY_CHARGE_THRESHOLD_SETTINGS_CHARGE_CONTROL_END_THRESHOLD;
return TRUE;
}
static gchar*
remove_brackets (const gchar *type)
{
GString *washed_type = NULL;
washed_type = g_string_new(NULL);
for (int i = 0; type[i] != '\0'; i++) {
if (type[i] == '[')
continue;
if (type[i] == ']')
break;
g_string_append_c (washed_type, type[i]);
}
return g_string_free (washed_type, FALSE);
}
static UpDeviceSupplyBatteryChargeTypes
up_device_battery_charge_type_str_to_enum (const gchar *type)
{
if (type == NULL)
return UP_DEVICE_SUPPLY_BATTERY_CHARGE_TYPE_LAST;
if (!g_strcmp0 ("Unknown", type))
return UP_DEVICE_SUPPLY_BATTERY_CHARGE_TYPES_UNKNOWN;
else if (!g_strcmp0 ("N/A", type))
return UP_DEVICE_SUPPLY_BATTERY_CHARGE_TYPES_NA;
else if (!g_strcmp0 ("Trickle", type))
return UP_DEVICE_SUPPLY_BATTERY_CHARGE_TYPES_TRICKLE;
else if (!g_strcmp0 ("Fast", type))
return UP_DEVICE_SUPPLY_BATTERY_CHARGE_TYPES_FAST;
else if (!g_strcmp0 ("Standard", type))
return UP_DEVICE_SUPPLY_BATTERY_CHARGE_TYPES_STANDARD;
else if (!g_strcmp0 ("Adaptive", type))
return UP_DEVICE_SUPPLY_BATTERY_CHARGE_TYPES_ADAPTIVE;
else if (!g_strcmp0 ("Custom", type))
return UP_DEVICE_SUPPLY_BATTERY_CHARGE_TYPES_CUSTOM;
else if (!g_strcmp0 ("Long_Life", type))
return UP_DEVICE_SUPPLY_BATTERY_CHARGE_TYPES_LONG_LIFE;
else if (!g_strcmp0 ("Bypass", type))
return UP_DEVICE_SUPPLY_BATTERY_CHARGE_TYPES_BYPASS;
/* invalid type */
return UP_DEVICE_SUPPLY_BATTERY_CHARGE_TYPE_LAST;
}
static gchar *
up_device_battery_charge_type_enum_to_str (UpDeviceSupplyBatteryChargeTypes types)
{
if (types == UP_DEVICE_SUPPLY_BATTERY_CHARGE_TYPES_UNKNOWN)
return g_strdup ("Unknown");
if (types == UP_DEVICE_SUPPLY_BATTERY_CHARGE_TYPES_NA)
return g_strdup ("N/A");
if (types == UP_DEVICE_SUPPLY_BATTERY_CHARGE_TYPES_TRICKLE)
return g_strdup ("Trickle");
if (types == UP_DEVICE_SUPPLY_BATTERY_CHARGE_TYPES_FAST)
return g_strdup ("Fast");
if (types == UP_DEVICE_SUPPLY_BATTERY_CHARGE_TYPES_STANDARD)
return g_strdup ("Standard");
if (types == UP_DEVICE_SUPPLY_BATTERY_CHARGE_TYPES_ADAPTIVE)
return g_strdup ("Adaptive");
if (types == UP_DEVICE_SUPPLY_BATTERY_CHARGE_TYPES_CUSTOM)
return g_strdup ("Custom");
if (types == UP_DEVICE_SUPPLY_BATTERY_CHARGE_TYPES_LONG_LIFE)
return g_strdup ("Long_Life");
if (types == UP_DEVICE_SUPPLY_BATTERY_CHARGE_TYPES_BYPASS)
return g_strdup ("Bypass");
/* invalid type */
return g_strdup ("Unknown");
}
static UpDeviceSupplyBatteryChargeTypes
up_device_battery_charge_find_available_charge_types_for_charging (UpDevice *device) {
UpDeviceSupplyBattery *self = UP_DEVICE_SUPPLY_BATTERY (device);
UpDeviceSupplyBatteryChargeTypes charge_types = self->supported_charge_types;
if (charge_types & UP_DEVICE_SUPPLY_BATTERY_CHARGE_TYPES_FAST)
return UP_DEVICE_SUPPLY_BATTERY_CHARGE_TYPES_FAST;
if (charge_types & UP_DEVICE_SUPPLY_BATTERY_CHARGE_TYPES_STANDARD)
return UP_DEVICE_SUPPLY_BATTERY_CHARGE_TYPES_STANDARD;
if (charge_types & UP_DEVICE_SUPPLY_BATTERY_CHARGE_TYPES_ADAPTIVE)
return UP_DEVICE_SUPPLY_BATTERY_CHARGE_TYPES_ADAPTIVE;
return UP_DEVICE_SUPPLY_BATTERY_CHARGE_TYPE_LAST;
}
static void
up_device_battery_get_supported_charge_types (UpDevice *device, UpBatteryInfo *info)
{
UpDeviceSupplyBattery *self = UP_DEVICE_SUPPLY_BATTERY (device);
GUdevDevice *native;
const gchar * charge_type_str = NULL;
gchar *tmp_type = NULL;
g_auto (GStrv) types = NULL;
native = G_UDEV_DEVICE (up_device_get_native (device));
charge_type_str = g_udev_device_get_sysfs_attr (native, "charge_types");
if (charge_type_str == NULL)
return;
info->charge_threshold_settings |= UP_DEVICE_SUPPLY_BATTERY_CHARGE_THRESHOLD_SETTINGS_CHARGE_TYPES;
types = g_strsplit (charge_type_str, " ", 0);
for (int i = 0; i < g_strv_length(types); i++) {
if (g_utf8_strchr (types[i], 1, '[') != NULL) {
tmp_type = remove_brackets (types[i]);
self->charge_type = up_device_battery_charge_type_str_to_enum (tmp_type);
} else {
tmp_type = g_strdup (types[i]);
}
self->supported_charge_types |= up_device_battery_charge_type_str_to_enum (tmp_type);
g_free (tmp_type);
}
}
/**
* up_device_supply_battery_is_charge_threshold_by_charge_type:
* @device: the device to check
*
* Return %TRUE if the charge threshold is controlled by charge_types,
* %FALSE otherwise.
*
* Co-work-with: Cursor
* Reviewed-by: Kate Hsuan <hpa@redhat.com>
*/
static gboolean
up_device_supply_battery_is_charge_threshold_by_charge_type (UpDevice *device) {
GUdevDevice *native = NULL;
UpDeviceSupplyBattery *self = UP_DEVICE_SUPPLY_BATTERY (device);
native = G_UDEV_DEVICE (up_device_get_native (device));
/* if the charge_control_start_threshold or charge_control_end_threshold is found,
* then the charge threshold is not controlled by charge_types. */
if (g_udev_device_has_sysfs_attr (native, "charge_control_start_threshold") ||
g_udev_device_has_sysfs_attr (native, "charge_control_end_threshold"))
return FALSE;
if (self->supported_charge_types & UP_DEVICE_SUPPLY_BATTERY_CHARGE_TYPES_LONG_LIFE) {
if (self->supported_charge_types & (UP_DEVICE_SUPPLY_BATTERY_CHARGE_TYPES_STANDARD |
UP_DEVICE_SUPPLY_BATTERY_CHARGE_TYPES_ADAPTIVE |
UP_DEVICE_SUPPLY_BATTERY_CHARGE_TYPES_FAST)) {
g_debug ("charge_control_start_threshold and charge_control_end_threshold are not found but the supported charge_types Long_lift, Standard or Adaptive was found. Assuming charging threshold is supported");
return TRUE;
}
}
return FALSE;
}
static gboolean static gboolean
up_device_supply_battery_refresh (UpDevice *device, up_device_supply_battery_refresh (UpDevice *device,
UpRefreshReason reason) UpRefreshReason reason)
@ -136,6 +389,11 @@ up_device_supply_battery_refresh (UpDevice *device,
GUdevDevice *native; GUdevDevice *native;
UpBatteryInfo info = { 0 }; UpBatteryInfo info = { 0 };
UpBatteryValues values = { 0 }; UpBatteryValues values = { 0 };
g_autofree gchar *vendor = NULL;
g_autofree gchar *model = NULL;
g_autofree gchar *serial = NULL;
g_autofree gchar *technology = NULL;
g_autofree gchar *capacity_level = NULL;
native = G_UDEV_DEVICE (up_device_get_native (device)); native = G_UDEV_DEVICE (up_device_get_native (device));
@ -153,9 +411,13 @@ up_device_supply_battery_refresh (UpDevice *device,
return TRUE; return TRUE;
} }
info.vendor = up_make_safe_string (get_sysfs_attr_uncached (native, "manufacturer")); vendor = up_make_safe_string (get_sysfs_attr_uncached (native, "manufacturer"));
info.model = up_make_safe_string (get_sysfs_attr_uncached (native, "model_name")); model = up_make_safe_string (get_sysfs_attr_uncached (native, "model_name"));
info.serial = up_make_safe_string (get_sysfs_attr_uncached (native, "serial_number")); serial = up_make_safe_string (get_sysfs_attr_uncached (native, "serial_number"));
info.vendor = vendor;
info.model = model;
info.serial = serial;
info.voltage_design = up_device_supply_battery_get_design_voltage (self, native); info.voltage_design = up_device_supply_battery_get_design_voltage (self, native);
info.charge_cycles = g_udev_device_get_sysfs_attr_as_int_uncached (native, "cycle_count"); info.charge_cycles = g_udev_device_get_sysfs_attr_as_int_uncached (native, "cycle_count");
@ -170,9 +432,35 @@ up_device_supply_battery_refresh (UpDevice *device,
info.energy.full = g_udev_device_get_sysfs_attr_as_double_uncached (native, "charge_full") / 1000000.0; info.energy.full = g_udev_device_get_sysfs_attr_as_double_uncached (native, "charge_full") / 1000000.0;
info.energy.design = g_udev_device_get_sysfs_attr_as_double_uncached (native, "charge_full_design") / 1000000.0; info.energy.design = g_udev_device_get_sysfs_attr_as_double_uncached (native, "charge_full_design") / 1000000.0;
} }
info.technology = up_convert_device_technology (get_sysfs_attr_uncached (native, "technology")); technology = get_sysfs_attr_uncached (native, "technology");
info.technology = up_convert_device_technology (technology);
/* NOTE: We used to warn about full > design, but really that is prefectly fine to happen. */ info.voltage_max_design = g_udev_device_get_sysfs_attr_as_double_uncached (native, "voltage_max_design") / 1000000.0;
info.voltage_min_design = g_udev_device_get_sysfs_attr_as_double_uncached (native, "voltage_min_design") / 1000000.0;
if (up_device_supply_battery_get_charge_control_limits (native, &info)) {
info.charge_control_supported = TRUE;
info.charge_control_enabled = FALSE;
} else {
info.charge_control_enabled = FALSE;
info.charge_control_supported = FALSE;
}
/* refresh the changes of charge_types */
up_device_battery_get_supported_charge_types (device, &info);
/* Test charge_types attribute, if "Long_Life", "Standard", "Adaptive" or "Fast" are found,
* then set charge_control_supported to TRUE.
*
* Co-work-with: Cursor
* Reviewed-by: Kate Hsuan <hpa@redhat.com> */
if (up_device_supply_battery_is_charge_threshold_by_charge_type (device)) {
g_debug ("charge_types attribute is found, set charge_control_supported to TRUE");
info.charge_control_supported = TRUE;
self->charge_threshold_by_charge_type = TRUE;
}
/* NOTE: We used to warn about full > design, but really that is perfectly fine to happen. */
/* Update the battery information (will only fire events for actual changes) */ /* Update the battery information (will only fire events for actual changes) */
up_device_battery_update_info (battery, &info); up_device_battery_update_info (battery, &info);
@ -186,6 +474,8 @@ up_device_supply_battery_refresh (UpDevice *device,
if (values.voltage < 0.01) if (values.voltage < 0.01)
values.voltage = g_udev_device_get_sysfs_attr_as_double_uncached (native, "voltage_avg") / 1000000.0; values.voltage = g_udev_device_get_sysfs_attr_as_double_uncached (native, "voltage_avg") / 1000000.0;
capacity_level = up_make_safe_string (get_sysfs_attr_uncached (native, "capacity_level"));
values.capacity_level = capacity_level;
switch (values.units) { switch (values.units) {
case UP_BATTERY_UNIT_CHARGE: case UP_BATTERY_UNIT_CHARGE:
@ -196,7 +486,7 @@ up_device_supply_battery_refresh (UpDevice *device,
* consistency (but we used to read it in the past). * consistency (but we used to read it in the past).
* *
* See https://bugs.freedesktop.org/show_bug.cgi?id=60104#c2 * See https://bugs.freedesktop.org/show_bug.cgi?id=60104#c2
* whichs reports energy_now of 15.05 Wh while our calculation * which's reports energy_now of 15.05 Wh while our calculation
* will be ~16.4Wh by multiplying charge with voltage). * will be ~16.4Wh by multiplying charge with voltage).
*/ */
values.energy.rate = fabs (g_udev_device_get_sysfs_attr_as_double_uncached (native, "current_now") / 1000000.0); values.energy.rate = fabs (g_udev_device_get_sysfs_attr_as_double_uncached (native, "current_now") / 1000000.0);
@ -226,8 +516,16 @@ up_device_supply_battery_refresh (UpDevice *device,
values.percentage = CLAMP(values.percentage, 0.0f, 100.0f); values.percentage = CLAMP(values.percentage, 0.0f, 100.0f);
} }
/* For some battery solutions, for example axp20x-battery, the kernel reports
* status = charging but the battery actually discharges when connecting a
* charger. Upower reports the battery is "discharging" when current_now is
* found and is a negative value as long as the battery isn't fully charged.*/
values.state = up_device_supply_get_state (native); values.state = up_device_supply_get_state (native);
if (values.state != UP_DEVICE_STATE_FULLY_CHARGED &&
g_udev_device_get_sysfs_attr_as_double_uncached (native, "current_now") < 0.0)
values.state = UP_DEVICE_STATE_DISCHARGING;
values.temperature = g_udev_device_get_sysfs_attr_as_double_uncached (native, "temp") / 10.0; values.temperature = g_udev_device_get_sysfs_attr_as_double_uncached (native, "temp") / 10.0;
up_device_battery_report (battery, &values, reason); up_device_battery_report (battery, &values, reason);
@ -312,16 +610,155 @@ up_device_supply_battery_get_property (GObject *object,
} }
} }
static char *
up_device_supply_device_path (GUdevDevice *device)
{
const char *root;
root = g_getenv ("UMOCKDEV_DIR");
if (!root || *root == '\0') {
return g_strdup (g_udev_device_get_sysfs_path (device));
}
return g_build_filename (root,
g_udev_device_get_sysfs_path (device),
NULL);
}
static gboolean
up_device_supply_battery_is_charge_type_exist (UpDevice *device, const gchar *charge_type) {
UpDeviceSupplyBattery *self = UP_DEVICE_SUPPLY_BATTERY (device);
UpDeviceSupplyBatteryChargeTypes type;
type = up_device_battery_charge_type_str_to_enum (charge_type);
if (type & self->supported_charge_types)
return TRUE;
return FALSE;
}
static gboolean
up_device_supply_battery_set_battery_charge_types (UpDevice *device,
UpDeviceSupplyBatteryChargeTypes charge_type,
GError **error) {
GUdevDevice *native;
g_autofree gchar *charge_type_str = NULL;
g_autofree gchar *native_path = NULL;
g_autofree gchar *type_filename = NULL;
native = G_UDEV_DEVICE (up_device_get_native (device));
charge_type_str = up_device_battery_charge_type_enum_to_str (charge_type);
/* return, if the attribute "charge_types" is not found */
if (!g_udev_device_has_sysfs_attr (native, "charge_types"))
return TRUE;
native_path = up_device_supply_device_path (native);
type_filename = g_build_filename (native_path, "charge_types", NULL);
if (!up_device_supply_battery_is_charge_type_exist (device, charge_type_str)) {
g_debug ("charge_type %s is not supported, skip setting", charge_type_str);
return TRUE;
}
if (!g_file_set_contents_full (type_filename, charge_type_str, -1,
G_FILE_SET_CONTENTS_ONLY_EXISTING, 0644, error)) {
g_set_error_literal (error, G_IO_ERROR,
G_IO_ERROR_FAILED, "Failed to set charge_types");
return TRUE;
}
return TRUE;
}
static gboolean
up_device_supply_battery_set_battery_charge_thresholds(UpDevice *device, guint start, guint end, GError **error) {
guint err_count = 0;
GUdevDevice *native;
UpDeviceSupplyBattery *self = UP_DEVICE_SUPPLY_BATTERY (device);
g_autofree gchar *native_path = NULL;
g_autofree gchar *start_filename = NULL;
g_autofree gchar *end_filename = NULL;
g_autoptr (GString) start_str = g_string_new (NULL);
g_autoptr (GString) end_str = g_string_new (NULL);
UpDeviceSupplyBatteryChargeTypes charge_type_enum;
native = G_UDEV_DEVICE (up_device_get_native (device));
native_path = up_device_supply_device_path (native);
start_filename = g_build_filename (native_path, "charge_control_start_threshold", NULL);
end_filename = g_build_filename (native_path, "charge_control_end_threshold", NULL);
/* if the charge threshold is controlled by charge_types,
* the charge_types will be set to Long_life when enabling the charge threshold.
* the charge_types will be set to Standard/Adaptive/Fast when disabling the charge threshold. */
if (self->charge_threshold_by_charge_type) {
if (start == 0 && end == 100) {
charge_type_enum = up_device_battery_charge_find_available_charge_types_for_charging (device);
up_device_supply_battery_set_battery_charge_types (device,
charge_type_enum,
NULL);
} else {
up_device_supply_battery_set_battery_charge_types (device,
UP_DEVICE_SUPPLY_BATTERY_CHARGE_TYPES_LONG_LIFE,
NULL);
}
return TRUE;
}
if (start != G_MAXUINT) {
g_string_printf (start_str, "%d", CLAMP (start, 0, 100));
if (!g_file_set_contents_full (start_filename, start_str->str, start_str->len,
G_FILE_SET_CONTENTS_ONLY_EXISTING, 0644, NULL))
err_count++;
} else {
g_debug ("Ignore charge_control_start_threshold setting");
}
if (end != G_MAXUINT) {
g_string_printf (end_str, "%d", CLAMP (end, 0, 100));
if (!g_file_set_contents_full (end_filename, end_str->str, end_str->len,
G_FILE_SET_CONTENTS_ONLY_EXISTING, 0644, NULL))
err_count++;
} else {
g_debug ("Ignore charge_control_end_threshold setting");
}
if (err_count == 2) {
g_set_error_literal (error, G_IO_ERROR,
G_IO_ERROR_FAILED, "Failed to set charge control thresholds");
return FALSE;
}
if (start == 0 && end == 100) {
charge_type_enum = up_device_battery_charge_find_available_charge_types_for_charging (device);
up_device_supply_battery_set_battery_charge_types (device,
charge_type_enum,
NULL);
} else {
/* for the Dell laptops, the charge_types has to be set to "Custom" to enable the charging threshold */
up_device_supply_battery_set_battery_charge_types (device,
UP_DEVICE_SUPPLY_BATTERY_CHARGE_TYPES_CUSTOM,
NULL);
}
return TRUE;
}
static void static void
up_device_supply_battery_class_init (UpDeviceSupplyBatteryClass *klass) up_device_supply_battery_class_init (UpDeviceSupplyBatteryClass *klass)
{ {
GObjectClass *object_class = G_OBJECT_CLASS (klass); GObjectClass *object_class = G_OBJECT_CLASS (klass);
UpDeviceClass *device_class = UP_DEVICE_CLASS (klass); UpDeviceClass *device_class = UP_DEVICE_CLASS (klass);
UpDeviceBatteryClass *battery_class = UP_DEVICE_BATTERY_CLASS (klass);
object_class->set_property = up_device_supply_battery_set_property; object_class->set_property = up_device_supply_battery_set_property;
object_class->get_property = up_device_supply_battery_get_property; object_class->get_property = up_device_supply_battery_get_property;
device_class->coldplug = up_device_supply_coldplug; device_class->coldplug = up_device_supply_coldplug;
device_class->refresh = up_device_supply_battery_refresh; device_class->refresh = up_device_supply_battery_refresh;
battery_class->set_battery_charge_thresholds = up_device_supply_battery_set_battery_charge_thresholds;
g_object_class_install_property (object_class, PROP_IGNORE_SYSTEM_PERCENTAGE, g_object_class_install_property (object_class, PROP_IGNORE_SYSTEM_PERCENTAGE,
g_param_spec_boolean ("ignore-system-percentage", g_param_spec_boolean ("ignore-system-percentage",

View file

@ -107,6 +107,10 @@ up_device_supply_reset_values (UpDeviceSupply *supply)
"temperature", (gdouble) 0.0, "temperature", (gdouble) 0.0,
"technology", UP_DEVICE_TECHNOLOGY_UNKNOWN, "technology", UP_DEVICE_TECHNOLOGY_UNKNOWN,
"charge-cycles", -1, "charge-cycles", -1,
"charge-start-threshold", 0,
"charge-end-threshold", 100,
"charge-threshold-enabled", FALSE,
"charge-threshold-supported", FALSE,
NULL); NULL);
} }
@ -327,8 +331,12 @@ static void
up_device_supply_sibling_discovered_guess_type (UpDevice *device, up_device_supply_sibling_discovered_guess_type (UpDevice *device,
GObject *sibling) GObject *sibling)
{ {
GUdevDevice *input; GUdevDevice *input, *native_device;
g_autoptr (GUdevDevice) parent_device = NULL;
g_autoptr (GUdevDevice) parent_sibling = NULL;
UpDeviceKind cur_type, new_type; UpDeviceKind cur_type, new_type;
gboolean is_same_parent = FALSE;
char *new_model_name;
char *model_name; char *model_name;
char *serial_number; char *serial_number;
int i; int i;
@ -345,14 +353,25 @@ up_device_supply_sibling_discovered_guess_type (UpDevice *device,
{ "ID_INPUT_KEYBOARD", UP_DEVICE_KIND_KEYBOARD }, { "ID_INPUT_KEYBOARD", UP_DEVICE_KIND_KEYBOARD },
}; };
/* The type priority if we have multiple siblings, /* The type priority if we have multiple siblings,
* i.e. we select the first of the current type of the found type. */ * i.e. we select the first of the current type of the found type.
* Give a new priority for device type since the GAMING_INPUT may include
* a keyboard, a touchpad, and... etc, for example Sony DualShock4 joystick.
* A mouse and a touchpad may include a mouse and a keyboard.
* Therefore, the priority is:
* 1. Gaming_input
* 2. Audio
* 3. Keyboard
* 4. Tablet
* 5. Touchpad
* 6. Mouse
*/
UpDeviceKind priority[] = { UpDeviceKind priority[] = {
UP_DEVICE_KIND_GAMING_INPUT,
UP_DEVICE_KIND_OTHER_AUDIO, UP_DEVICE_KIND_OTHER_AUDIO,
UP_DEVICE_KIND_KEYBOARD, UP_DEVICE_KIND_KEYBOARD,
UP_DEVICE_KIND_TABLET, UP_DEVICE_KIND_TABLET,
UP_DEVICE_KIND_TOUCHPAD, UP_DEVICE_KIND_TOUCHPAD,
UP_DEVICE_KIND_MOUSE, UP_DEVICE_KIND_MOUSE
UP_DEVICE_KIND_GAMING_INPUT,
}; };
/* Form-factors set in rules.d/78-sound-card.rules in systemd */ /* Form-factors set in rules.d/78-sound-card.rules in systemd */
struct { struct {
@ -450,10 +469,35 @@ up_device_supply_sibling_discovered_guess_type (UpDevice *device,
} }
if (cur_type != new_type) { if (cur_type != new_type) {
native_device = G_UDEV_DEVICE (up_device_get_native (device));
parent_device = g_udev_device_get_parent (native_device);
parent_sibling = g_udev_device_get_parent (input);
g_debug ("Type changed from %s to %s", g_debug ("Type changed from %s to %s",
up_device_kind_to_string(cur_type), up_device_kind_to_string (cur_type),
up_device_kind_to_string(new_type)); up_device_kind_to_string (new_type));
g_object_set (device, "type", new_type, NULL);
/* Check if the device and the sibling have the same parent. */
if (!g_strcmp0 (g_udev_device_get_sysfs_path (parent_device),
g_udev_device_get_sysfs_path (parent_sibling)))
is_same_parent = TRUE;
new_model_name = up_device_supply_get_string (input, "name");
/* The model name of a device component may be different. For example, DualSense
* joystick owns "Sony Interactive Entertainment DualSense Wireless Controller"
* for the joystick and "Sony Interactive Entertainment DualSense Wireless Controller
* Motion Sensors" for the accelerometer. If the type is change, the corresponding
* model name have to be changed too. */
if (new_model_name != NULL && is_same_parent) {
up_make_safe_string (new_model_name);
g_object_set (device,
"type", new_type,
"model", new_model_name,
NULL);
} else {
g_object_set (device, "type", new_type, NULL);
}
g_free (new_model_name);
} }
} }
@ -539,7 +583,7 @@ up_device_supply_guess_type (GUdevDevice *native,
g_warning ("USB power supply %s without usb_type property, please report", g_warning ("USB power supply %s without usb_type property, please report",
native_path); native_path);
} else { } else {
g_warning ("did not recognise type %s, please report", device_type); g_warning ("did not recognize type %s, please report", device_type);
} }
out: out:

View file

@ -54,4 +54,3 @@ UpDeviceState up_device_supply_get_state (GUdevDevice *native);
G_END_DECLS G_END_DECLS
#endif /* __UP_DEVICE_SUPPLY_H__ */ #endif /* __UP_DEVICE_SUPPLY_H__ */

View file

@ -201,7 +201,7 @@ up_device_wup_parse_command (UpDeviceWup *wup, const gchar *data)
} }
} }
/* check we have enough data inthe packet */ /* check we have enough data in the packet */
tokens = g_strsplit (packet, ",", -1); tokens = g_strsplit (packet, ",", -1);
number_tokens = g_strv_length (tokens); number_tokens = g_strv_length (tokens);
if (number_tokens < 3) { if (number_tokens < 3) {

View file

@ -51,4 +51,3 @@ GType up_device_wup_get_type (void);
G_END_DECLS G_END_DECLS
#endif /* __UP_DEVICE_WUP_H__ */ #endif /* __UP_DEVICE_WUP_H__ */

View file

@ -31,6 +31,7 @@
#include "up-device-supply-battery.h" #include "up-device-supply-battery.h"
#include "up-device-hid.h" #include "up-device-hid.h"
#include "up-device-wup.h" #include "up-device-wup.h"
#include "up-kbd-backlight-led.h"
#ifdef HAVE_IDEVICE #ifdef HAVE_IDEVICE
#include "up-device-idevice.h" #include "up-device-idevice.h"
#endif /* HAVE_IDEVICE */ #endif /* HAVE_IDEVICE */
@ -66,8 +67,17 @@ device_parent_id (GUdevDevice *dev)
return NULL; return NULL;
/* Continue walk if the parent is a "hid" device */ /* Continue walk if the parent is a "hid" device */
if (g_strcmp0 (subsystem, "hid") == 0) if (g_strcmp0 (subsystem, "hid") == 0) {
/* if the parent is under /sys/devices/virtual/misc/uhid, the device should be input devices
* and return the path immediately to make sure they belongs to the correct parent.
* for example:
* root@fedora:/sys/devices/virtual/misc/uhid# ls
* 0005:046D:B01A.0005 0005:05AC:0250.000B dev power subsystem uevent */
if (g_strrstr (g_udev_device_get_sysfs_path (parent), "/sys/devices/virtual/misc/uhid"))
return g_strdup (g_udev_device_get_sysfs_path (parent));
return device_parent_id (parent); return device_parent_id (parent);
}
/* Also skip over USB interfaces, we care about full devices */ /* Also skip over USB interfaces, we care about full devices */
if (g_strcmp0 (subsystem, "usb") == 0 && if (g_strcmp0 (subsystem, "usb") == 0 &&
@ -169,6 +179,12 @@ get_latest_udev_device (UpEnumeratorUdev *self,
{ {
const char *sysfs_path; const char *sysfs_path;
/* return NULL when receiving a non-GUdevDevice object */
if (!G_UDEV_IS_DEVICE (obj)) {
g_debug ("Receiving a non-udev object.");
return NULL;
}
sysfs_path = g_udev_device_get_sysfs_path (G_UDEV_DEVICE (obj)); sysfs_path = g_udev_device_get_sysfs_path (G_UDEV_DEVICE (obj));
return g_udev_client_query_by_sysfs_path (self->udev, sysfs_path); return g_udev_client_query_by_sysfs_path (self->udev, sysfs_path);
} }
@ -201,13 +217,102 @@ emit_changes_for_siblings (UpEnumeratorUdev *self,
} }
} }
static void
power_supply_add_helper (UpEnumeratorUdev *self,
const gchar *action,
GUdevDevice *device,
GUdevClient *client,
GObject *obj,
const gchar *device_key)
{
g_autoptr(UpDevice) up_dev = NULL;
g_autofree char *parent_id = NULL;
up_dev = device_new (self, device);
/* We work with `obj` further down, which is the UpDevice
* if we have it, or the GUdevDevice if not. */
if (up_dev)
obj = G_OBJECT (up_dev);
else
obj = G_OBJECT (device);
g_hash_table_insert (self->known, (char*) device_key, g_object_ref (obj));
/* Fire relevant sibling events and insert into lookup table */
parent_id = device_parent_id (device);
g_debug ("device %s has parent id: %s", device_key, parent_id);
if (parent_id) {
GPtrArray *devices = NULL;
char *parent_id_key = NULL;
int i;
g_hash_table_lookup_extended (self->siblings, parent_id,
(gpointer*)&parent_id_key, (gpointer*)&devices);
if (!devices)
devices = g_ptr_array_new_with_free_func (g_object_unref);
for (i = 0; i < devices->len; i++) {
GObject *sibling = g_ptr_array_index (devices, i);
if (up_dev) {
g_autoptr(GUdevDevice) d = get_latest_udev_device (self, sibling);
if (d)
up_device_sibling_discovered (up_dev, G_OBJECT (d));
}
if (UP_IS_DEVICE (sibling))
up_device_sibling_discovered (UP_DEVICE (sibling), obj);
}
g_ptr_array_add (devices, g_object_ref (obj));
if (!parent_id_key) {
parent_id_key = g_strdup (parent_id);
g_hash_table_insert (self->siblings, parent_id_key, devices);
}
/* Just a reference to the hash table key */
g_object_set_data (obj, "udev-parent-id", parent_id_key);
}
if (up_dev)
g_signal_emit_by_name (self, "device-added", up_dev);
}
static void
kbd_backlight_add_helper (UpEnumeratorUdev *self,
const gchar *action,
GUdevDevice *device,
GUdevClient *client,
GObject *obj,
const char *device_key)
{
UpDaemon *daemon;
g_autoptr(UpDeviceKbdBacklight) up_kbd = NULL;
g_autofree char *parent_id = NULL;
daemon = up_enumerator_get_daemon (UP_ENUMERATOR (self));
up_kbd = g_initable_new (UP_TYPE_KBD_BACKLIGHT_LED, NULL, NULL,
"daemon", daemon,
"native", device,
NULL);
if (up_kbd)
obj = G_OBJECT (up_kbd);
else
obj = G_OBJECT (device);
g_hash_table_insert (self->known, (char*) device_key, g_object_ref (obj));
if (up_kbd)
g_signal_emit_by_name (self, "device-added", G_OBJECT (up_kbd));
}
static void static void
uevent_signal_handler_cb (UpEnumeratorUdev *self, uevent_signal_handler_cb (UpEnumeratorUdev *self,
const gchar *action, const gchar *action,
GUdevDevice *device, GUdevDevice *device,
GUdevClient *client) GUdevClient *client)
{ {
const char *device_key = g_udev_device_get_sysfs_path (device); const char *device_key = g_udev_device_get_sysfs_path (device);
gboolean is_kbd_backlight = FALSE;
g_debug ("Received uevent %s on device %s", action, device_key); g_debug ("Received uevent %s on device %s", action, device_key);
@ -215,6 +320,19 @@ uevent_signal_handler_cb (UpEnumeratorUdev *self,
if (g_strcmp0 (g_udev_device_get_subsystem (device), "power_supply") == 0) if (g_strcmp0 (g_udev_device_get_subsystem (device), "power_supply") == 0)
device_key = g_udev_device_get_name (device); device_key = g_udev_device_get_name (device);
/* Consider both 'kbd_backlight' and 'lp5523:kb' as keyboard backlight
* devices. See include/dt-bindings/leds/common.h. 'lp5523:kb' is still
* in use, despite being marked as obsolete/legacy.
*/
if (g_strcmp0 (g_udev_device_get_subsystem (device), "leds") == 0) {
if (g_strrstr (device_key, "kbd_backlight") == NULL &&
g_strrstr (device_key, "lp5523:kb") == NULL)
return;
is_kbd_backlight = TRUE;
}
g_debug ("uevent subsystem %s", g_udev_device_get_subsystem (device));
/* It appears that we may not always receive an "add" event. As such, /* It appears that we may not always receive an "add" event. As such,
* treat "add"/"change" in the same way, by first checking if we have * treat "add"/"change" in the same way, by first checking if we have
* seen the device. * seen the device.
@ -235,56 +353,10 @@ uevent_signal_handler_cb (UpEnumeratorUdev *self,
} }
if (!obj) { if (!obj) {
g_autoptr(UpDevice) up_dev = NULL; if (is_kbd_backlight)
g_autofree char *parent_id = NULL; kbd_backlight_add_helper (self, action, device, client, obj, device_key);
up_dev = device_new (self, device);
/* We work with `obj` further down, which is the UpDevice
* if we have it, or the GUdevDevice if not. */
if (up_dev)
obj = G_OBJECT (up_dev);
else else
obj = G_OBJECT (device); power_supply_add_helper (self, action, device, client, obj, device_key);
g_hash_table_insert (self->known, (char*) device_key, g_object_ref (obj));
/* Fire relevant sibling events and insert into lookup table */
parent_id = device_parent_id (device);
g_debug ("device %s has parent id: %s", device_key, parent_id);
if (parent_id) {
GPtrArray *devices = NULL;
char *parent_id_key = NULL;
int i;
g_hash_table_lookup_extended (self->siblings, parent_id,
(gpointer*)&parent_id_key, (gpointer*)&devices);
if (!devices)
devices = g_ptr_array_new_with_free_func (g_object_unref);
for (i = 0; i < devices->len; i++) {
GObject *sibling = g_ptr_array_index (devices, i);
if (up_dev) {
g_autoptr(GUdevDevice) d = get_latest_udev_device (self, sibling);
if (d)
up_device_sibling_discovered (up_dev, G_OBJECT (d));
}
if (UP_IS_DEVICE (sibling))
up_device_sibling_discovered (UP_DEVICE (sibling), obj);
}
g_ptr_array_add (devices, g_object_ref (obj));
if (!parent_id_key) {
parent_id_key = g_strdup (parent_id);
g_hash_table_insert (self->siblings, parent_id_key, devices);
}
/* Just a reference to the hash table key */
g_object_set_data (obj, "udev-parent-id", parent_id_key);
}
if (up_dev)
g_signal_emit_by_name (self, "device-added", up_dev);
} else { } else {
if (!UP_IS_DEVICE (obj)) { if (!UP_IS_DEVICE (obj)) {
@ -311,6 +383,11 @@ uevent_signal_handler_cb (UpEnumeratorUdev *self,
g_debug ("removing device for path %s", g_udev_device_get_sysfs_path (device)); g_debug ("removing device for path %s", g_udev_device_get_sysfs_path (device));
if (is_kbd_backlight) {
g_signal_emit_by_name (self, "device-removed", obj);
return;
}
parent_id = g_object_get_data (obj, "udev-parent-id"); parent_id = g_object_get_data (obj, "udev-parent-id");
/* Remove from siblings table. */ /* Remove from siblings table. */
@ -352,8 +429,8 @@ up_enumerator_udev_initable_init (UpEnumerator *enumerator)
guint i; guint i;
const gchar **subsystems; const gchar **subsystems;
/* List "input" first just to avoid some sibling hotplugging later */ /* List "input" first just to avoid some sibling hotplugging later */
const gchar *subsystems_no_wup[] = {"input", "power_supply", "usb", "usbmisc", "sound", NULL}; const gchar *subsystems_no_wup[] = {"input", "power_supply", "usb", "usbmisc", "sound", "leds", NULL};
const gchar *subsystems_wup[] = {"input", "power_supply", "usb", "usbmisc", "sound", "tty", NULL}; const gchar *subsystems_wup[] = {"input", "power_supply", "usb", "usbmisc", "sound", "tty", "leds", NULL};
config = up_config_new (); config = up_config_new ();
if (up_config_get_boolean (config, "EnableWattsUpPro")) if (up_config_get_boolean (config, "EnableWattsUpPro"))

View file

@ -39,4 +39,3 @@ gboolean up_input_get_switch_value (UpInput *input);
G_END_DECLS G_END_DECLS
#endif /* __UP_INPUT_H__ */ #endif /* __UP_INPUT_H__ */

View file

@ -0,0 +1,323 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
*
* Copyright (C) 2025 Kate Hsuan <p.hsuan@gmail.com>
*
* Licensed under the GNU General Public License Version 2
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "config.h"
#include <glib.h>
#include <glib/gi18n.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <dirent.h>
#include <errno.h>
#include "up-kbd-backlight-led.h"
#include "up-native.h"
#include "up-types.h"
static void up_kbd_backlight_led_finalize (GObject *object);
struct UpKbdBacklightLedPrivate
{
gint max_brightness;
guint brightness;
gint fd_hw_changed;
GIOChannel *channel_hw_changed;
};
G_DEFINE_TYPE_WITH_PRIVATE (UpKbdBacklightLed, up_kbd_backlight_led, UP_TYPE_DEVICE_KBD_BACKLIGHT)
/**
* up_kbd_backlight_led_brightness_write:
*
* Write the brightness value to the LED device with a given path.
**/
static gboolean
up_kbd_backlight_led_brightness_write (UpDeviceKbdBacklight *kbd_backlight, const gchar *native_path, gint value)
{
UpKbdBacklightLed *kbd;
UpKbdBacklightLedPrivate *priv;
g_autoptr (GString) value_str = g_string_new (NULL);
g_return_val_if_fail (UP_IS_DEVICE_KBD_BACKLIGHT (kbd_backlight), FALSE);
kbd = UP_KBD_BACKLIGHT_LED (kbd_backlight);
priv = up_kbd_backlight_led_get_instance_private (kbd);
g_string_printf (value_str, "%d", CLAMP (value, 0, priv->max_brightness));
if (!g_file_set_contents_full (native_path, value_str->str, value_str->len,
G_FILE_SET_CONTENTS_ONLY_EXISTING, 0644, NULL)) {
g_debug ("Failed on setting keyboard backlight LED brightness: %s", native_path);
return FALSE;
}
return TRUE;
}
/**
* up_kbd_backlight_led_brightness_read:
*
* Read the brightness value from the LED device with a given path.
**/
static gint
up_kbd_backlight_led_brightness_read (UpDeviceKbdBacklight *kbd_backlight, const gchar *native_path)
{
g_autofree gchar *buf = NULL;
gint64 brightness = -1;
g_return_val_if_fail (UP_IS_DEVICE_KBD_BACKLIGHT (kbd_backlight), brightness);
if (!g_file_get_contents (native_path, &buf, NULL, NULL))
return -1;
g_strstrip (buf);
g_debug ("brightness: %s", buf);
brightness = g_ascii_strtoll (buf, NULL, 10);
if (brightness < 0) {
g_warning ("failed to convert brightness.");
return -1;
}
return brightness;
}
/**
* up_kbd_backlight_led_set_brightness:
*
* Set the brightness.
**/
static gboolean
up_kbd_backlight_led_set_brightness (UpDeviceKbdBacklight *kbd_backlight, gint value)
{
GObject *native;
g_autofree gchar *filename = NULL;
const gchar *native_path;
gboolean ret = FALSE;
native = up_device_kbd_backlight_get_native (UP_DEVICE_KBD_BACKLIGHT (kbd_backlight));
g_return_val_if_fail (native != NULL, FALSE);
native_path = up_native_get_native_path (native);
g_return_val_if_fail (native_path != NULL, FALSE);
filename = g_build_filename (native_path, "brightness", NULL);
ret = up_kbd_backlight_led_brightness_write (kbd_backlight, filename, value);
return ret;
}
/**
* up_kbd_backlight_led_get_brightness:
*
* Get the brightness.
**/
static gint
up_kbd_backlight_led_get_brightness (UpDeviceKbdBacklight *kbd_backlight)
{
GObject *native;
const gchar *native_path;
g_autofree gchar *filename = NULL;
gint brightness = -1;
native = up_device_kbd_backlight_get_native (UP_DEVICE_KBD_BACKLIGHT (kbd_backlight));
g_return_val_if_fail (native != NULL, brightness);
native_path = up_native_get_native_path (native);
g_return_val_if_fail (native_path != NULL, brightness);
filename = g_build_filename (native_path, "brightness", NULL);
brightness = up_kbd_backlight_led_brightness_read (kbd_backlight, filename);
return brightness;
}
/**
* up_kbd_backlight_led_get_max_brightness:
*
* Gets the max brightness.
**/
static gint
up_kbd_backlight_led_get_max_brightness (UpDeviceKbdBacklight *kbd_backlight)
{
UpKbdBacklightLed *kbd = UP_KBD_BACKLIGHT_LED (kbd_backlight);
UpKbdBacklightLedPrivate *priv = up_kbd_backlight_led_get_instance_private (kbd);
return priv->max_brightness;
}
/**
* up_kbd_backlight_led_event_io:
*
* This function is called when the brightness of the LED device changes.
**/
static gboolean
up_kbd_backlight_led_event_io (GIOChannel *channel, GIOCondition condition, gpointer data)
{
UpDeviceKbdBacklight *kbd_backlight = UP_DEVICE_KBD_BACKLIGHT (data);
UpKbdBacklightLed *kbd = UP_KBD_BACKLIGHT_LED (kbd_backlight);
UpKbdBacklightLedPrivate *priv = up_kbd_backlight_led_get_instance_private (kbd);
gint brightness = 0;
gchar buf[16];
g_autofree gchar *ret_str = NULL;
glong len;
gchar *end = NULL;
if (priv->fd_hw_changed < 0)
return FALSE;
if (!(condition & G_IO_PRI))
return FALSE;
lseek (priv->fd_hw_changed, 0, SEEK_SET);
len = read (priv->fd_hw_changed, buf, G_N_ELEMENTS (buf) - 1);
if (len > 0) {
buf[len] = '\0';
g_strstrip (buf);
brightness = g_ascii_strtoll (buf, &end, 10);
if (brightness < 0 ||
brightness > priv->max_brightness ||
end == buf) {
brightness = -1;
g_warning ("kbd eventio: failed to convert brightness.");
}
}
g_debug ("kbd eventio: brightness: %d", brightness);
if (brightness < 0)
return FALSE;
if (brightness >= 0)
up_device_kbd_backlight_emit_change (kbd_backlight, brightness, "internal");
return TRUE;
}
/**
* up_kbd_backlight_led_coldplug:
*
* Update the LED settings to UpKbdBacklightLed device.
**/
static gboolean
up_kbd_backlight_led_coldplug (UpDeviceKbdBacklight *kbd_backlight)
{
UpKbdBacklightLed *kbd;
UpKbdBacklightLedPrivate *priv;
GObject *native;
g_autofree gchar *filename = NULL;
g_autofree gchar *path_hw_changed = NULL;
const gchar *native_path = NULL;
g_return_val_if_fail (UP_IS_DEVICE_KBD_BACKLIGHT (kbd_backlight), FALSE);
kbd = UP_KBD_BACKLIGHT_LED (kbd_backlight);
priv = up_kbd_backlight_led_get_instance_private (kbd);
native = up_device_kbd_backlight_get_native (kbd_backlight);
if (native == NULL) {
priv->max_brightness = 0;
return FALSE;
}
native_path = up_native_get_native_path (native);
filename = g_build_filename (native_path, "max_brightness", NULL);
priv->max_brightness = up_kbd_backlight_led_brightness_read (kbd_backlight, filename);
/* Set up device watcher */
path_hw_changed = g_build_filename (native_path, "brightness_hw_changed", NULL);
priv->fd_hw_changed = open (path_hw_changed, O_RDONLY);
if (priv->fd_hw_changed >= 0) {
priv->channel_hw_changed = g_io_channel_unix_new (priv->fd_hw_changed);
g_io_add_watch (priv->channel_hw_changed,
G_IO_PRI, up_kbd_backlight_led_event_io, kbd_backlight);
}
return TRUE;
}
/**
* up_kbd_backlight_led_finalize:
**/
static void
up_kbd_backlight_led_finalize (GObject *object)
{
UpKbdBacklightLed *kbd_backlight;
UpKbdBacklightLedPrivate *priv;
g_return_if_fail (object != NULL);
g_return_if_fail (UP_IS_KBD_BACKLIGHT_LED (object));
kbd_backlight = UP_KBD_BACKLIGHT_LED (object);
priv = up_kbd_backlight_led_get_instance_private (kbd_backlight);
if (priv->channel_hw_changed) {
g_io_channel_shutdown (priv->channel_hw_changed, FALSE, NULL);
g_io_channel_unref (priv->channel_hw_changed);
}
if (priv->fd_hw_changed >= 0)
close (priv->fd_hw_changed);
G_OBJECT_CLASS (up_kbd_backlight_led_parent_class)->finalize (object);
}
/**
* up_kbd_backlight_led_class_init:
**/
static void
up_kbd_backlight_led_class_init (UpKbdBacklightLedClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
UpDeviceKbdBacklightClass *dev_kbd_klass = UP_DEVICE_KBD_BACKLIGHT_CLASS (klass);
object_class->finalize = up_kbd_backlight_led_finalize;
dev_kbd_klass->coldplug = up_kbd_backlight_led_coldplug;
dev_kbd_klass->get_max_brightness = up_kbd_backlight_led_get_max_brightness;
dev_kbd_klass->get_brightness = up_kbd_backlight_led_get_brightness;
dev_kbd_klass->set_brightness = up_kbd_backlight_led_set_brightness;
}
/**
* up_kbd_backlight_led_init:
**/
static void
up_kbd_backlight_led_init (UpKbdBacklightLed *kbd_backlight)
{
kbd_backlight->priv = up_kbd_backlight_led_get_instance_private (kbd_backlight);
}
/**
* up_kbd_backlight_led_new:
**/
UpKbdBacklightLed *
up_kbd_backlight_led_new (void)
{
return g_object_new (UP_TYPE_KBD_BACKLIGHT_LED, NULL);
}

View file

@ -0,0 +1,55 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
*
* Copyright (C) 2025 Kate Hsuan <p.hsuan@gmail.com>
*
* Licensed under the GNU General Public License Version 2
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef __UP_KBD_BACKLIGHT_LED_H
#define __UP_KBD_BACKLIGHT_LED_H
#include <glib-object.h>
#include "up-device-kbd-backlight.h"
G_BEGIN_DECLS
#define UP_TYPE_KBD_BACKLIGHT_LED (up_kbd_backlight_led_get_type ())
#define UP_KBD_BACKLIGHT_LED(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), UP_TYPE_KBD_BACKLIGHT_LED, UpKbdBacklightLed))
#define UP_KBD_BACKLIGHT_LED_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), UP_TYPE_KBD_BACKLIGHT_LED, UpKbdBacklightLedClass))
#define UP_IS_KBD_BACKLIGHT_LED(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), UP_TYPE_KBD_BACKLIGHT_LED))
#define UP_IS_KBD_BACKLIGHT_LED_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), UP_TYPE_KBD_BACKLIGHT_LED))
#define UP_KBD_BACKLIGHT_LED_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), UP_TYPE_KBD_BACKLIGHT_LED, UpKbdBacklightLedClass))
typedef struct UpKbdBacklightLedPrivate UpKbdBacklightLedPrivate;
typedef struct
{
UpDeviceKbdBacklight parent;
UpKbdBacklightLedPrivate *priv;
} UpKbdBacklightLed;
typedef struct
{
UpDeviceKbdBacklightClass parent_class;
} UpKbdBacklightLedClass;
UpKbdBacklightLed *up_kbd_backlight_led_new (void);
GType up_kbd_backlight_led_get_type (void);
G_END_DECLS
#endif /* __UP_KBD_BACKLIGHT_LED_H */

View file

@ -59,4 +59,3 @@ up_native_get_native_path (GObject *object)
* fallback for completeness */ * fallback for completeness */
return g_udev_device_get_sysfs_path (device); return g_udev_device_get_sysfs_path (device);
} }

View file

@ -6,11 +6,12 @@ upowerd_deps = declare_dependency(
include_directories('../dbus'), include_directories('../dbus'),
], ],
dependencies: [ dependencies: [
m_dep, glib_dep, gobject_dep, gio_dep, gio_unix_dep, libupower_glib_dep, upowerd_dbus_dep m_dep, glib_dep, gobject_dep, gio_dep, gio_unix_dep, libupower_glib_dep, upowerd_dbus_dep, polkit
], ],
compile_args: [ compile_args: [
'-DUP_COMPILATION', '-DUP_COMPILATION',
'-DHISTORY_DIR="@0@"'.format(historydir), '-DHISTORY_DIR="@0@"'.format(historydir),
'-DSTATE_DIR="@0@"'.format(statedir),
], ],
) )
@ -39,12 +40,16 @@ upowerd_private = static_library('upowerd-private',
'up-enumerator.h', 'up-enumerator.h',
'up-kbd-backlight.h', 'up-kbd-backlight.h',
'up-kbd-backlight.c', 'up-kbd-backlight.c',
'up-device-kbd-backlight.c',
'up-device-kbd-backlight.h',
'up-history.h', 'up-history.h',
'up-history.c', 'up-history.c',
'up-backend.h', 'up-backend.h',
'up-native.h', 'up-native.h',
'up-common.h', 'up-common.h',
'up-common.c', 'up-common.c',
'up-polkit.c',
'up-polkit.h',
], ],
dependencies: [ upowerd_deps ], dependencies: [ upowerd_deps ],
c_args: [ '-DG_LOG_DOMAIN="UPower"' ], c_args: [ '-DG_LOG_DOMAIN="UPower"' ],
@ -139,24 +144,27 @@ if os_backend == 'linux' and gobject_introspection.found()
args: ut_args, args: ut_args,
env: env, env: env,
depends: [ libupower_glib_typelib, upowerd ], depends: [ libupower_glib_typelib, upowerd ],
timeout: 80, timeout: 150,
) )
endforeach endforeach
install_data( [ installed_tests = get_option('installed_tests')
'linux/integration-test.py', if installed_tests
'linux/output_checker.py', install_data( [
], 'linux/integration-test.py',
install_dir: get_option('prefix') / get_option('libexecdir') / 'upower' 'linux/output_checker.py',
) ],
install_subdir('linux/tests/', install_dir: get_option('prefix') / get_option('libexecdir') / 'upower'
install_dir: get_option('prefix') / get_option('libexecdir') / 'upower' )
) install_subdir('linux/tests/',
install_dir: get_option('prefix') / get_option('libexecdir') / 'upower'
)
configure_file( configure_file(
input: 'upower-integration.test.in', input: 'upower-integration.test.in',
output: 'upower-integration.test', output: 'upower-integration.test',
install_dir: get_option('datadir') / 'installed-tests' / 'upower', install_dir: get_option('datadir') / 'installed-tests' / 'upower',
configuration: cdata configuration: cdata
) )
endif
endif endif

View file

@ -383,6 +383,8 @@ up_backend_update_battery_state(UpDevice* device)
(void*) NULL); (void*) NULL);
if(up_native_get_sensordev("acpibat0", &sdev)) if(up_native_get_sensordev("acpibat0", &sdev))
up_backend_update_acpibat_state(device, sdev); up_backend_update_acpibat_state(device, sdev);
else if(up_native_get_sensordev("qcpas0", &sdev))
up_backend_update_acpibat_state(device, sdev);
return TRUE; return TRUE;
} }
return FALSE; return FALSE;
@ -602,7 +604,7 @@ up_backend_apm_event_thread(gpointer object)
} }
#endif #endif
return NULL; return NULL;
/* shouldnt be reached ? */ /* shouldn't be reached ? */
} }
/** /**
@ -685,4 +687,3 @@ up_backend_finalize (GObject *object)
G_OBJECT_CLASS (up_backend_parent_class)->finalize (object); G_OBJECT_CLASS (up_backend_parent_class)->finalize (object);
} }

View file

@ -78,4 +78,3 @@ int up_backend_inhibitor_lock_take (UpBackend *backend,
G_END_DECLS G_END_DECLS
#endif /* __UP_BACKEND_H */ #endif /* __UP_BACKEND_H */

View file

@ -68,6 +68,22 @@ up_config_get_uint (UpConfig *config, const gchar *key)
return val; return val;
} }
/**
* up_config_get_double:
**/
gdouble
up_config_get_double (UpConfig *config, const gchar *key)
{
int val;
val = g_key_file_get_double (config->priv->keyfile,
"UPower", key, NULL);
if (val < 0.0)
return 0.0;
return val;
}
/** /**
* up_config_get_string: * up_config_get_string:
**/ **/
@ -88,22 +104,169 @@ up_config_class_init (UpConfigClass *klass)
object_class->finalize = up_config_finalize; object_class->finalize = up_config_finalize;
} }
/**
* up_config_list_compare_files:
**/
static gint
up_config_list_compare_files (gconstpointer a, gconstpointer b)
{
return g_strcmp0 ((const gchar*)a, (const gchar*)b);
}
/**
* up_config_list_confd_files:
*
* The format of the filename should be '^([0-9][0-9])-([a-zA-Z0-9-_])*\.conf$',
* that is, starting with "00-" to "99-", ending in ".conf", and with a mix of
* alphanumeric characters with dashes and underscores in between. For example:
* '01-upower-override.conf'.
*
* Files named differently, or containing invalid groups (currently only
* 'UPower' is valid), will not be considered.
*
* The candidate files within the given directory are sorted (with g_strcmp0(),
* so the ordering will be as with strcmp()). The configuration in the files
* being processed later will override previous config, in particular the main
* config, but also the one from previous files processed, if the Group and Key
* coincide.
*
* For example, consider 'UPower.conf' that contains the defaults:
* PercentageLow=20.0
* PercentageCritical=5.0
* PercentageAction=2.0
*
* and there is a file 'UPower.conf.d/70-change-percentages.conf'
* containing settings for all 'Percentage*' keys:
* [UPower]
* PercentageLow=15.0
* PercentageCritical=10.0
* PercentageAction=5.0
*
* and another 'UPower.conf.d/99-change-percentages-local.conf'
* containing settings only for 'PercentageAction':
* [UPower]
* PercentageAction=7.5
*
* First the main 'UPower.conf' will be processed, then
* 'UPower.conf.d/70-change-percentages.conf' overriding the defaults
* of all percentages from the main config file with the given values,
* and finally 'UPower.conf.d/99-change-percentages-local.conf'
* overriding once again only 'PercentageAction'. The final, effective
* values are:
* PercentageLow=15.0
* PercentageCritical=10.0
* PercentageAction=7.5
**/
static GPtrArray*
up_config_list_confd_files (const gchar* conf_d_path, GError** error)
{
g_autoptr (GPtrArray) ret_conf_d_files = NULL;
GDir *dir = NULL;
const gchar *filename = NULL;
const char *regex_pattern = "^([0-9][0-9])-([a-zA-Z0-9-_])*\\.conf$";
g_autoptr (GRegex) regex = NULL;
dir = g_dir_open (conf_d_path, 0, error);
if (dir == NULL)
return NULL;
regex = g_regex_new (regex_pattern, G_REGEX_DEFAULT, G_REGEX_MATCH_DEFAULT, NULL);
g_assert (regex != NULL);
ret_conf_d_files = g_ptr_array_new_full (0, g_free);
while ((filename = g_dir_read_name (dir)) != NULL) {
g_autofree gchar *file_path = NULL;
g_autoptr (GFile) file = NULL;
g_autoptr (GFileInfo) file_info = NULL;
if (!g_regex_match (regex, filename, G_REGEX_MATCH_DEFAULT, NULL))
continue;
file_path = g_build_filename (conf_d_path, filename, NULL);
file = g_file_new_for_path (file_path);
file_info = g_file_query_info (file,
G_FILE_ATTRIBUTE_STANDARD_TYPE,
G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
NULL,
NULL);
if (file_info != NULL) {
g_debug ("Will consider additional config file '%s'", file_path);
g_ptr_array_add (ret_conf_d_files, g_strdup (file_path));
}
}
g_dir_close (dir);
g_ptr_array_sort_values (ret_conf_d_files, up_config_list_compare_files);
return g_ptr_array_ref (ret_conf_d_files);
}
/**
* up_config_override_from_confd:
**/
static void
up_config_override_from_confd (GKeyFile *key_file, const gchar* new_config_path)
{
g_autoptr (GKeyFile) new_keyfile = NULL;
gchar **keys = NULL;
gsize keys_size = 0;
new_keyfile = g_key_file_new();
if (!g_key_file_load_from_file (new_keyfile, new_config_path, G_KEY_FILE_NONE, NULL))
return;
if (!g_key_file_has_group (new_keyfile, "UPower"))
return;
keys = g_key_file_get_keys (new_keyfile, "UPower", &keys_size, NULL);
if (keys == NULL)
return;
for (gsize i = 0; i < keys_size; i++) {
g_autofree gchar *value = NULL;
g_autofree gchar *old_value = NULL;
value = g_key_file_get_value (new_keyfile, "UPower", keys[i], NULL);
if (value == NULL)
continue;
old_value = g_key_file_get_value (key_file, "UPower", keys[i], NULL);
if (old_value != NULL)
g_key_file_set_value (key_file, "UPower", keys[i], value);
}
g_strfreev (keys);
}
/** /**
* up_config_init: * up_config_init:
**/ **/
static void static void
up_config_init (UpConfig *config) up_config_init (UpConfig *config)
{ {
gboolean allow_risky_critical_action = FALSE;
gboolean expect_battery_recalibration = FALSE;
g_autofree gchar *critical_action = NULL;
g_autoptr (GError) error = NULL;
g_autofree gchar *filename = NULL;
gboolean ret; gboolean ret;
GError *error = NULL; g_autofree gchar *conf_dir = NULL;
gchar *filename; g_autofree gchar *conf_d_path = NULL;
g_autoptr (GPtrArray) conf_d_files = NULL;
config->priv = up_config_get_instance_private (config); config->priv = up_config_get_instance_private (config);
config->priv->keyfile = g_key_file_new (); config->priv->keyfile = g_key_file_new ();
filename = g_strdup (g_getenv ("UPOWER_CONF_FILE_NAME")); filename = g_strdup (g_getenv ("UPOWER_CONF_FILE_NAME"));
if (filename == NULL) if (filename == NULL) {
filename = g_build_filename (PACKAGE_SYSCONF_DIR,"UPower", "UPower.conf", NULL); filename = g_build_filename (PACKAGE_SYSCONF_DIR,"UPower", "UPower.conf", NULL);
conf_d_path = g_build_filename (PACKAGE_SYSCONF_DIR, "UPower", "UPower.conf.d", NULL);
} else {
conf_dir = g_path_get_dirname (filename);
conf_d_path = g_build_filename (conf_dir, "UPower.conf.d", NULL);
}
/* load */ /* load */
ret = g_key_file_load_from_file (config->priv->keyfile, ret = g_key_file_load_from_file (config->priv->keyfile,
@ -114,10 +277,49 @@ up_config_init (UpConfig *config)
if (!ret) { if (!ret) {
g_warning ("failed to load config file '%s': %s", g_warning ("failed to load config file '%s': %s",
filename, error->message); filename, error->message);
g_error_free (error); g_clear_error (&error);
} }
g_free (filename); conf_d_files = up_config_list_confd_files (conf_d_path, &error);
if (conf_d_files != NULL) {
for (guint i = 0; i < conf_d_files->len; i++) {
const gchar* conf_d_file = (const gchar*)(g_ptr_array_index (conf_d_files, i));
up_config_override_from_confd (config->priv->keyfile,
conf_d_file);
}
} else {
g_debug ("failed to find files in 'UPower.conf.d': %s", error->message);
}
/* Warn for any dangerous configurations */
critical_action = up_config_get_string (config, "CriticalPowerAction");
allow_risky_critical_action = up_config_get_boolean (config, "AllowRiskyCriticalPowerAction");
if (!g_strcmp0 (critical_action, "Suspend") || !g_strcmp0 (critical_action, "Ignore")) {
if (allow_risky_critical_action) {
g_warning ("The \"%s\" CriticalPowerAction setting is considered risky:"
" abrupt power loss due to battery exhaustion may lead to data"
" corruption. Use AllowRiskyCriticalPowerAction=false to disable"
" support for risky settings.", critical_action);
} else {
g_warning ("The \"%s\" CriticalPowerAction setting is considered risky:"
" abrupt power loss due to battery exhaustion may lead to data"
" corruption. The system will perform \"HybridSleep\" instead."
" Use AllowRiskyCriticalPowerAction=true to enable support for"
" risky settings.", critical_action);
}
}
expect_battery_recalibration = up_config_get_boolean (config, "ExpectBatteryRecalibration");
if (expect_battery_recalibration) {
if (allow_risky_critical_action) {
g_warning ("The \"ExpectBatteryRecalibration\" setting is considered risky:"
" abrupt power loss due to battery exhaustion may lead to data"
" corruption. The system will unexpected down when the AC is disconnected."
" Use AllowRiskyCriticalPowerAction=false to disable support for"
" risky settings.");
}
}
} }
/** /**
@ -148,4 +350,3 @@ up_config_new (void)
} }
return UP_CONFIG (up_config_object); return UP_CONFIG (up_config_object);
} }

View file

@ -52,6 +52,8 @@ gboolean up_config_get_boolean (UpConfig *config,
const gchar *key); const gchar *key);
guint up_config_get_uint (UpConfig *config, guint up_config_get_uint (UpConfig *config,
const gchar *key); const gchar *key);
gdouble up_config_get_double (UpConfig *config,
const gchar *key);
gchar *up_config_get_string (UpConfig *config, gchar *up_config_get_string (UpConfig *config,
const gchar *key); const gchar *key);

View file

@ -30,8 +30,10 @@
#include "up-config.h" #include "up-config.h"
#include "up-constants.h" #include "up-constants.h"
#include "up-polkit.h"
#include "up-device-list.h" #include "up-device-list.h"
#include "up-device.h" #include "up-device.h"
#include "up-device-kbd-backlight.h"
#include "up-backend.h" #include "up-backend.h"
#include "up-daemon.h" #include "up-daemon.h"
@ -39,8 +41,10 @@ struct UpDaemonPrivate
{ {
UpConfig *config; UpConfig *config;
gboolean debug; gboolean debug;
UpPolkit *polkit;
UpBackend *backend; UpBackend *backend;
UpDeviceList *power_devices; UpDeviceList *power_devices;
UpDeviceList *kbd_backlight_devices;
guint action_timeout_id; guint action_timeout_id;
guint refresh_batteries_id; guint refresh_batteries_id;
guint warning_level_id; guint warning_level_id;
@ -59,14 +63,20 @@ struct UpDaemonPrivate
gint64 time_to_empty; gint64 time_to_empty;
gint64 time_to_full; gint64 time_to_full;
gboolean charge_threshold_enabled;
gboolean state_all_discharging;
/* WarningLevel configuration */ /* WarningLevel configuration */
gboolean use_percentage_for_policy; gboolean use_percentage_for_policy;
guint low_percentage; gdouble low_percentage;
guint critical_percentage; gdouble critical_percentage;
guint action_percentage; gdouble action_percentage;
guint low_time; guint low_time;
guint critical_time; guint critical_time;
guint action_time; guint action_time;
/* environment variable override */
const char *state_dir_override;
}; };
static void up_daemon_finalize (GObject *object); static void up_daemon_finalize (GObject *object);
@ -142,8 +152,12 @@ up_daemon_update_display_battery (UpDaemon *daemon)
gint64 time_to_empty_total = 0; gint64 time_to_empty_total = 0;
gint64 time_to_full_total = 0; gint64 time_to_full_total = 0;
gboolean is_present_total = FALSE; gboolean is_present_total = FALSE;
gboolean charge_threshold_enabled_total = FALSE;
guint num_batteries = 0; guint num_batteries = 0;
gboolean state_all_discharging = TRUE;
gboolean state_any_discharging = FALSE;
/* Gather state from each device */ /* Gather state from each device */
array = up_device_list_get_array (daemon->priv->power_devices); array = up_device_list_get_array (daemon->priv->power_devices);
for (i = 0; i < array->len; i++) { for (i = 0; i < array->len; i++) {
@ -151,6 +165,7 @@ up_daemon_update_display_battery (UpDaemon *daemon)
UpDeviceState state = UP_DEVICE_STATE_UNKNOWN; UpDeviceState state = UP_DEVICE_STATE_UNKNOWN;
UpDeviceKind kind = UP_DEVICE_KIND_UNKNOWN; UpDeviceKind kind = UP_DEVICE_KIND_UNKNOWN;
gboolean present = FALSE;
gdouble percentage = 0.0; gdouble percentage = 0.0;
gdouble energy = 0.0; gdouble energy = 0.0;
gdouble energy_full = 0.0; gdouble energy_full = 0.0;
@ -158,9 +173,11 @@ up_daemon_update_display_battery (UpDaemon *daemon)
gint64 time_to_empty = 0; gint64 time_to_empty = 0;
gint64 time_to_full = 0; gint64 time_to_full = 0;
gboolean power_supply = FALSE; gboolean power_supply = FALSE;
gboolean charge_threshold_enabled = FALSE;
device = g_ptr_array_index (array, i); device = g_ptr_array_index (array, i);
g_object_get (device, g_object_get (device,
"is-present", &present,
"type", &kind, "type", &kind,
"state", &state, "state", &state,
"percentage", &percentage, "percentage", &percentage,
@ -170,8 +187,12 @@ up_daemon_update_display_battery (UpDaemon *daemon)
"time-to-empty", &time_to_empty, "time-to-empty", &time_to_empty,
"time-to-full", &time_to_full, "time-to-full", &time_to_full,
"power-supply", &power_supply, "power-supply", &power_supply,
"charge-threshold-enabled", &charge_threshold_enabled,
NULL); NULL);
if (!present)
continue;
/* When we have a UPS, it's either a desktop, and /* When we have a UPS, it's either a desktop, and
* has no batteries, or a laptop, in which case we * has no batteries, or a laptop, in which case we
* ignore the batteries */ * ignore the batteries */
@ -221,6 +242,17 @@ up_daemon_update_display_battery (UpDaemon *daemon)
else else
state_total = UP_DEVICE_STATE_UNKNOWN; state_total = UP_DEVICE_STATE_UNKNOWN;
/* Update charging state variables by considering any battery that is charging or fully charged to not
* be discharging. Additionally, also update state_any_discharge for any batteries explicitly
* discharging. */
if (state == UP_DEVICE_STATE_CHARGING || state == UP_DEVICE_STATE_FULLY_CHARGED) {
state_all_discharging = FALSE;
} else if (state == UP_DEVICE_STATE_DISCHARGING)
state_any_discharging = TRUE;
/* If at least one battery has charge thresholds enabled, propagate that. */
charge_threshold_enabled_total = charge_threshold_enabled_total || charge_threshold_enabled;
/* sum up composite */ /* sum up composite */
kind_total = UP_DEVICE_KIND_BATTERY; kind_total = UP_DEVICE_KIND_BATTERY;
is_present_total = TRUE; is_present_total = TRUE;
@ -283,6 +315,9 @@ out:
time_to_full_total = SECONDS_PER_HOUR * ((energy_full_total - energy_total) / energy_rate_total); time_to_full_total = SECONDS_PER_HOUR * ((energy_full_total - energy_total) / energy_rate_total);
} }
/* Compute state_all_discharging by ensuring at least one battery is discharging */
state_all_discharging = state_all_discharging && state_any_discharging;
/* Did anything change? */ /* Did anything change? */
if (daemon->priv->kind == kind_total && if (daemon->priv->kind == kind_total &&
daemon->priv->state == state_total && daemon->priv->state == state_total &&
@ -291,7 +326,9 @@ out:
daemon->priv->energy_rate == energy_rate_total && daemon->priv->energy_rate == energy_rate_total &&
daemon->priv->time_to_empty == time_to_empty_total && daemon->priv->time_to_empty == time_to_empty_total &&
daemon->priv->time_to_full == time_to_full_total && daemon->priv->time_to_full == time_to_full_total &&
daemon->priv->percentage == percentage_total) daemon->priv->percentage == percentage_total &&
daemon->priv->charge_threshold_enabled == charge_threshold_enabled_total &&
daemon->priv->state_all_discharging == state_all_discharging)
return FALSE; return FALSE;
daemon->priv->kind = kind_total; daemon->priv->kind = kind_total;
@ -304,6 +341,9 @@ out:
daemon->priv->percentage = percentage_total; daemon->priv->percentage = percentage_total;
daemon->priv->charge_threshold_enabled = charge_threshold_enabled_total;
daemon->priv->state_all_discharging = state_all_discharging;
g_object_set (daemon->priv->display_device, g_object_set (daemon->priv->display_device,
"type", kind_total, "type", kind_total,
"state", state_total, "state", state_total,
@ -314,6 +354,7 @@ out:
"time-to-full", time_to_full_total, "time-to-full", time_to_full_total,
"percentage", percentage_total, "percentage", percentage_total,
"is-present", is_present_total, "is-present", is_present_total,
"charge-threshold-enabled", charge_threshold_enabled_total,
"power-supply", TRUE, "power-supply", TRUE,
"update-time", (guint64) g_get_real_time () / G_USEC_PER_SEC, "update-time", (guint64) g_get_real_time () / G_USEC_PER_SEC,
NULL); NULL);
@ -324,7 +365,7 @@ out:
/** /**
* up_daemon_get_warning_level_local: * up_daemon_get_warning_level_local:
* *
* As soon as _all_ batteries are low, this is true * As soon as _all_ batteries are low, external power is not available or not charging at least one battery, this is true
**/ **/
static UpDeviceLevel static UpDeviceLevel
up_daemon_get_warning_level_local (UpDaemon *daemon) up_daemon_get_warning_level_local (UpDaemon *daemon)
@ -337,9 +378,10 @@ up_daemon_get_warning_level_local (UpDaemon *daemon)
daemon->priv->state != UP_DEVICE_STATE_DISCHARGING) daemon->priv->state != UP_DEVICE_STATE_DISCHARGING)
return UP_DEVICE_LEVEL_NONE; return UP_DEVICE_LEVEL_NONE;
/* Check to see if the batteries have not noticed we are on AC */ /* Ignore battery level if we have external power and not all batteries are discharging */
if (daemon->priv->kind == UP_DEVICE_KIND_BATTERY && if (daemon->priv->kind == UP_DEVICE_KIND_BATTERY &&
up_daemon_get_on_ac_local (daemon, NULL)) up_daemon_get_on_ac_local (daemon, NULL) &&
!daemon->priv->state_all_discharging)
return UP_DEVICE_LEVEL_NONE; return UP_DEVICE_LEVEL_NONE;
return up_daemon_compute_warning_level (daemon, return up_daemon_compute_warning_level (daemon,
@ -457,6 +499,38 @@ up_daemon_enumerate_devices (UpExportedDaemon *skeleton,
return TRUE; return TRUE;
} }
static gboolean
up_daemon_enumerate_kbd_backlights (UpExportedDaemon *skeleton,
GDBusMethodInvocation *invocation,
UpDaemon *daemon)
{
guint i;
GPtrArray *array;
GPtrArray *object_paths;
UpDeviceKbdBacklight *kbd_backlight;
/* build a pointer array of the object paths */
object_paths = g_ptr_array_new_with_free_func (g_free);
array = up_device_list_get_array (daemon->priv->kbd_backlight_devices);
for (i = 0; i < array->len; i++) {
const char *object_path;
kbd_backlight = (UpDeviceKbdBacklight *) g_ptr_array_index (array, i);
object_path = up_device_kbd_backlight_get_object_path (kbd_backlight);
if (object_path != NULL)
g_ptr_array_add (object_paths, g_strdup (object_path));
}
g_ptr_array_unref (array);
g_ptr_array_add (object_paths, NULL);
/* return it on the bus */
up_exported_daemon_complete_enumerate_kbd_backlights (skeleton, invocation,
(const gchar **) object_paths->pdata);
/* free */
g_ptr_array_unref (object_paths);
return TRUE;
}
/** /**
* up_daemon_get_display_device: * up_daemon_get_display_device:
**/ **/
@ -561,6 +635,7 @@ up_daemon_shutdown (UpDaemon *daemon)
/* forget about discovered devices */ /* forget about discovered devices */
up_device_list_clear (daemon->priv->power_devices); up_device_list_clear (daemon->priv->power_devices);
up_device_list_clear (daemon->priv->kbd_backlight_devices);
/* release UpDaemon reference */ /* release UpDaemon reference */
g_object_run_dispose (G_OBJECT (daemon->priv->display_device)); g_object_run_dispose (G_OBJECT (daemon->priv->display_device));
@ -694,6 +769,8 @@ up_daemon_compute_warning_level (UpDaemon *daemon,
gdouble percentage, gdouble percentage,
gint64 time_to_empty) gint64 time_to_empty)
{ {
gboolean can_risky = FALSE;
gboolean expect_battery_recalibration = FALSE;
gboolean use_percentage = TRUE; gboolean use_percentage = TRUE;
UpDeviceLevel default_level = UP_DEVICE_LEVEL_NONE; UpDeviceLevel default_level = UP_DEVICE_LEVEL_NONE;
@ -716,6 +793,21 @@ up_daemon_compute_warning_level (UpDaemon *daemon,
default_level = UP_DEVICE_LEVEL_DISCHARGING; default_level = UP_DEVICE_LEVEL_DISCHARGING;
} }
/* Check if the battery is performing the battery recalibration and
* AC is online, the battery level is UP_DEVICE_LEVEL_NONE. */
can_risky = up_config_get_boolean (daemon->priv->config,
"AllowRiskyCriticalPowerAction");
expect_battery_recalibration = up_config_get_boolean (daemon->priv->config,
"ExpectBatteryRecalibration");
if (can_risky && kind == UP_DEVICE_KIND_BATTERY) {
if (expect_battery_recalibration &&
up_daemon_get_on_ac_local (daemon, NULL)) {
g_debug ("ExpectBatteryRecalibration is enabled and the AC is connected, so the battery level is not critical");
return UP_DEVICE_LEVEL_NONE;
}
}
if (power_supply && if (power_supply &&
!daemon->priv->use_percentage_for_policy && !daemon->priv->use_percentage_for_policy &&
time_to_empty > 0.0) time_to_empty > 0.0)
@ -803,6 +895,32 @@ up_daemon_get_charge_icon (UpDaemon *daemon,
} }
} }
/**
* up_daemon_polkit_is_allowed:
**/
gboolean
up_daemon_polkit_is_allowed (UpDaemon *daemon, const gchar *action_id, GDBusMethodInvocation *invocation)
{
#ifdef HAVE_POLKIT
g_autoptr (PolkitSubject) subject = NULL;
g_autoptr (GError) error = NULL;
subject = up_polkit_get_subject (daemon->priv->polkit, invocation);
if (subject == NULL) {
g_debug ("Can't get sender subject");
return FALSE;
}
if (!up_polkit_is_allowed (daemon->priv->polkit, subject, action_id, &error)) {
if (error != NULL)
g_debug ("Error on Polkit check authority: %s", error->message);
return FALSE;
}
#endif
return TRUE;
}
/** /**
* up_daemon_device_changed_cb: * up_daemon_device_changed_cb:
**/ **/
@ -947,39 +1065,71 @@ up_daemon_get_debug (UpDaemon *daemon)
return daemon->priv->debug; return daemon->priv->debug;
} }
/**
* up_daemon_get_state_dir_env_override:
*
* Get UPOWER_STATE_DIR environment variable.
**/
const gchar *
up_daemon_get_state_dir_env_override (UpDaemon *daemon)
{
return daemon->priv->state_dir_override;
}
static void
up_daemon_get_env_override (UpDaemon *self)
{
self->priv->state_dir_override = g_getenv ("UPOWER_STATE_DIR");
}
/** /**
* up_daemon_device_added_cb: * up_daemon_device_added_cb:
**/ **/
static void static void
up_daemon_device_added_cb (UpBackend *backend, UpDevice *device, UpDaemon *daemon) up_daemon_device_added_cb (UpBackend *backend, GObject *device, UpDaemon *daemon)
{ {
const gchar *object_path; const gchar *object_path;
UpDaemonPrivate *priv = daemon->priv; UpDaemonPrivate *priv = daemon->priv;
g_return_if_fail (UP_IS_DAEMON (daemon)); g_return_if_fail (UP_IS_DAEMON (daemon));
g_return_if_fail (UP_IS_DEVICE (device)); g_return_if_fail (UP_IS_DEVICE (device) || UP_IS_DEVICE_KBD_BACKLIGHT (device));
/* add to device list */ if (UP_IS_DEVICE (device)) {
up_device_list_insert (priv->power_devices, device); /* power_supply */
/* add to device list */
up_device_list_insert (priv->power_devices, device);
/* connect, so we get changes */ /* connect, so we get changes */
g_signal_connect (device, "notify", g_signal_connect (device, "notify",
G_CALLBACK (up_daemon_device_changed_cb), daemon); G_CALLBACK (up_daemon_device_changed_cb), daemon);
/* emit */ /* emit */
object_path = up_device_get_object_path (device); object_path = up_device_get_object_path (UP_DEVICE (device));
if (object_path == NULL) { if (object_path == NULL) {
g_debug ("Device %s was unregistered before it was on the bus", g_debug ("Device %s was unregistered before it was on the bus",
up_exported_device_get_native_path (UP_EXPORTED_DEVICE (device))); up_exported_device_get_native_path (UP_EXPORTED_DEVICE (device)));
return; return;
}
/* Ensure we poll the new device if needed */
g_source_set_ready_time (daemon->priv->poll_source, 0);
g_debug ("emitting added: %s", object_path);
up_daemon_update_warning_level (daemon);
up_exported_daemon_emit_device_added (UP_EXPORTED_DAEMON (daemon), object_path);
} else {
/*leds*/
g_debug ("Add a led device to the device list");
/* emit */
object_path = up_device_kbd_backlight_get_object_path (UP_DEVICE_KBD_BACKLIGHT (device));
if (object_path == NULL) {
g_debug ("Device %s was unregistered before it was on the bus",
up_exported_kbd_backlight_get_native_path (UP_EXPORTED_KBD_BACKLIGHT (device)));
return;
}
up_device_list_insert (priv->kbd_backlight_devices, G_OBJECT (device));
up_exported_daemon_emit_device_added (UP_EXPORTED_DAEMON (daemon), object_path);
} }
/* Ensure we poll the new device if needed */
g_source_set_ready_time (daemon->priv->poll_source, 0);
g_debug ("emitting added: %s", object_path);
up_daemon_update_warning_level (daemon);
up_exported_daemon_emit_device_added (UP_EXPORTED_DAEMON (daemon), object_path);
} }
/** /**
@ -992,15 +1142,22 @@ up_daemon_device_removed_cb (UpBackend *backend, UpDevice *device, UpDaemon *dae
UpDaemonPrivate *priv = daemon->priv; UpDaemonPrivate *priv = daemon->priv;
g_return_if_fail (UP_IS_DAEMON (daemon)); g_return_if_fail (UP_IS_DAEMON (daemon));
g_return_if_fail (UP_IS_DEVICE (device)); g_return_if_fail (UP_IS_DEVICE (device) || UP_IS_DEVICE_KBD_BACKLIGHT (device));
g_signal_handlers_disconnect_by_data (device, daemon); g_signal_handlers_disconnect_by_data (device, daemon);
/* remove from list (device remains valid during the function call) */
up_device_list_remove (priv->power_devices, device);
/* emit */ /* emit */
object_path = up_device_get_object_path (device); if (UP_IS_DEVICE (device)) {
/* remove from list (device remains valid during the function call) */
up_device_list_remove (priv->power_devices, device);
object_path = up_device_get_object_path (device);
} else if (UP_IS_DEVICE_KBD_BACKLIGHT (device)) {
/* remove from list (device remains valid during the function call) */
up_device_list_remove (priv->kbd_backlight_devices, device);
object_path = up_device_kbd_backlight_get_object_path (UP_DEVICE_KBD_BACKLIGHT (device));
} else {
return;
}
/* don't crash the session */ /* don't crash the session */
if (object_path == NULL) { if (object_path == NULL) {
@ -1011,20 +1168,25 @@ up_daemon_device_removed_cb (UpBackend *backend, UpDevice *device, UpDaemon *dae
g_debug ("emitting device-removed: %s", object_path); g_debug ("emitting device-removed: %s", object_path);
up_exported_daemon_emit_device_removed (UP_EXPORTED_DAEMON (daemon), object_path); up_exported_daemon_emit_device_removed (UP_EXPORTED_DAEMON (daemon), object_path);
/* Unregister keyboard backlight dbus path */
if (UP_IS_DEVICE_KBD_BACKLIGHT (device))
up_device_kbd_backlight_unregister (UP_DEVICE_KBD_BACKLIGHT (device));
/* In case a battery was removed */ /* In case a battery was removed */
up_daemon_refresh_battery_devices (daemon); up_daemon_refresh_battery_devices (daemon);
up_daemon_update_warning_level (daemon); up_daemon_update_warning_level (daemon);
} }
#define LOAD_OR_DEFAULT(val, str, def) val = (load_default ? def : up_config_get_uint (daemon->priv->config, str)) #define LOAD_OR_DEFAULT(val, str, def) val = (load_default ? def : up_config_get_uint (daemon->priv->config, str))
#define LOAD_OR_DEFAULT_DOUBLE(val, str, def) val = (load_default ? def : up_config_get_double (daemon->priv->config, str))
static void static void
load_percentage_policy (UpDaemon *daemon, load_percentage_policy (UpDaemon *daemon,
gboolean load_default) gboolean load_default)
{ {
LOAD_OR_DEFAULT (daemon->priv->low_percentage, "PercentageLow", 20); LOAD_OR_DEFAULT_DOUBLE (daemon->priv->low_percentage, "PercentageLow", 20.0);
LOAD_OR_DEFAULT (daemon->priv->critical_percentage, "PercentageCritical", 5); LOAD_OR_DEFAULT_DOUBLE (daemon->priv->critical_percentage, "PercentageCritical", 5.0);
LOAD_OR_DEFAULT (daemon->priv->action_percentage, "PercentageAction", 2); LOAD_OR_DEFAULT_DOUBLE (daemon->priv->action_percentage, "PercentageAction", 2.0);
} }
static void static void
@ -1041,9 +1203,9 @@ load_time_policy (UpDaemon *daemon,
static void static void
policy_config_validate (UpDaemon *daemon) policy_config_validate (UpDaemon *daemon)
{ {
if (daemon->priv->low_percentage >= 100 || if (daemon->priv->low_percentage >= 100.0 ||
daemon->priv->critical_percentage >= 100 || daemon->priv->critical_percentage >= 100.0 ||
daemon->priv->action_percentage >= 100) { daemon->priv->action_percentage >= 100.0) {
load_percentage_policy (daemon, TRUE); load_percentage_policy (daemon, TRUE);
} else if (!IS_DESCENDING (daemon->priv->low_percentage, } else if (!IS_DESCENDING (daemon->priv->low_percentage,
daemon->priv->critical_percentage, daemon->priv->critical_percentage,
@ -1067,8 +1229,10 @@ up_daemon_init (UpDaemon *daemon)
daemon->priv = up_daemon_get_instance_private (daemon); daemon->priv = up_daemon_get_instance_private (daemon);
daemon->priv->critical_action_lock_fd = -1; daemon->priv->critical_action_lock_fd = -1;
daemon->priv->polkit = up_polkit_new ();
daemon->priv->config = up_config_new (); daemon->priv->config = up_config_new ();
daemon->priv->power_devices = up_device_list_new (); daemon->priv->power_devices = up_device_list_new ();
daemon->priv->kbd_backlight_devices = up_device_list_new ();
daemon->priv->display_device = up_device_new (daemon, NULL); daemon->priv->display_device = up_device_new (daemon, NULL);
daemon->priv->poll_source = g_source_new (&poll_source_funcs, sizeof (GSource)); daemon->priv->poll_source = g_source_new (&poll_source_funcs, sizeof (GSource));
@ -1083,6 +1247,8 @@ up_daemon_init (UpDaemon *daemon)
load_time_policy (daemon, FALSE); load_time_policy (daemon, FALSE);
policy_config_validate (daemon); policy_config_validate (daemon);
up_daemon_get_env_override (daemon);
daemon->priv->backend = up_backend_new (); daemon->priv->backend = up_backend_new ();
g_signal_connect (daemon->priv->backend, "device-added", g_signal_connect (daemon->priv->backend, "device-added",
G_CALLBACK (up_daemon_device_added_cb), daemon); G_CALLBACK (up_daemon_device_added_cb), daemon);
@ -1093,6 +1259,8 @@ up_daemon_init (UpDaemon *daemon)
g_signal_connect (daemon, "handle-enumerate-devices", g_signal_connect (daemon, "handle-enumerate-devices",
G_CALLBACK (up_daemon_enumerate_devices), daemon); G_CALLBACK (up_daemon_enumerate_devices), daemon);
g_signal_connect (daemon, "handle-enumerate-kbd_backlights",
G_CALLBACK (up_daemon_enumerate_kbd_backlights), daemon);
g_signal_connect (daemon, "handle-get-critical-action", g_signal_connect (daemon, "handle-get-critical-action",
G_CALLBACK (up_daemon_get_critical_action), daemon); G_CALLBACK (up_daemon_get_critical_action), daemon);
g_signal_connect (daemon, "handle-get-display-device", g_signal_connect (daemon, "handle-get-display-device",
@ -1151,7 +1319,9 @@ up_daemon_finalize (GObject *object)
g_clear_pointer (&daemon->priv->poll_source, g_source_destroy); g_clear_pointer (&daemon->priv->poll_source, g_source_destroy);
g_object_unref (priv->power_devices); g_object_unref (priv->power_devices);
g_object_unref (priv->kbd_backlight_devices);
g_object_unref (priv->display_device); g_object_unref (priv->display_device);
g_object_unref (priv->polkit);
g_object_unref (priv->config); g_object_unref (priv->config);
g_object_unref (priv->backend); g_object_unref (priv->backend);

View file

@ -22,6 +22,7 @@
#define __UP_DAEMON_H__ #define __UP_DAEMON_H__
#include <dbus/up-daemon-generated.h> #include <dbus/up-daemon-generated.h>
#include "up-types.h" #include "up-types.h"
#include "up-device-list.h" #include "up-device-list.h"
@ -86,6 +87,10 @@ const gchar *up_daemon_get_charge_icon (UpDaemon *daemon,
gdouble percentage, gdouble percentage,
UpDeviceLevel battery_level, UpDeviceLevel battery_level,
gboolean charging); gboolean charging);
const gchar *up_daemon_get_state_dir_env_override (UpDaemon *daemon);
gboolean up_daemon_polkit_is_allowed (UpDaemon *daemon,
const gchar *action_id,
GDBusMethodInvocation *invocation);
void up_daemon_pause_poll (UpDaemon *daemon); void up_daemon_pause_poll (UpDaemon *daemon);
void up_daemon_resume_poll (UpDaemon *daemon); void up_daemon_resume_poll (UpDaemon *daemon);

View file

@ -52,6 +52,9 @@ typedef struct {
/* dynamic values */ /* dynamic values */
gint64 fast_repoll_until; gint64 fast_repoll_until;
gboolean repoll_needed; gboolean repoll_needed;
/* state path */
const char *state_dir;
} UpDeviceBatteryPrivate; } UpDeviceBatteryPrivate;
G_DEFINE_TYPE_EXTENDED (UpDeviceBattery, up_device_battery, UP_TYPE_DEVICE, 0, G_DEFINE_TYPE_EXTENDED (UpDeviceBattery, up_device_battery, UP_TYPE_DEVICE, 0,
@ -196,7 +199,7 @@ up_device_battery_update_poll_frequency (UpDeviceBattery *self,
* and one of the following holds true: * and one of the following holds true:
* 1. The current stat is unknown; we hope that this is transient * 1. The current stat is unknown; we hope that this is transient
* and re-poll. * and re-poll.
* 2. A change occured on a line power supply. This likely means that * 2. A change occurred on a line power supply. This likely means that
* batteries switch between charging/discharging which does not * batteries switch between charging/discharging which does not
* always result in a separate uevent. * always result in a separate uevent.
* *
@ -256,7 +259,7 @@ up_device_battery_report (UpDeviceBattery *self,
/* QUIRK: /* QUIRK:
* *
* There is an old bug where some Lenovo machine switched from reporting * There is an old bug where some Lenovo machine switched from reporting
* energy to reporting charge numbers. The code used to react by * energy to reporting charge numbers. The code used to react by
* reloading everything, however, what apparently happens is that the * reloading everything, however, what apparently happens is that the
* *energy* value simply starts being reported through *charge* * *energy* value simply starts being reported through *charge*
* attributes. * attributes.
@ -267,7 +270,7 @@ up_device_battery_report (UpDeviceBattery *self,
* incorrectly multiplied by the voltage. * incorrectly multiplied by the voltage.
* *
* Said differently, just assuming the units did *not* change should * Said differently, just assuming the units did *not* change should
* give us a saner value. Obviously, things will fall appart if upower * give us a saner value. Obviously, things will fall apart if upower
* is restarted and this should be fixed in the kernel or firmware. * is restarted and this should be fixed in the kernel or firmware.
* *
* Unfortunately, the hardware is quite old (X201s) which makes it hard * Unfortunately, the hardware is quite old (X201s) which makes it hard
@ -388,6 +391,7 @@ up_device_battery_report (UpDeviceBattery *self,
"energy-rate", values->energy.rate, "energy-rate", values->energy.rate,
"time-to-empty", time_to_empty, "time-to-empty", time_to_empty,
"time-to-full", time_to_full, "time-to-full", time_to_full,
"capacity-level", values->capacity_level,
/* XXX: Move "update-time" updates elsewhere? */ /* XXX: Move "update-time" updates elsewhere? */
"update-time", (guint64) g_get_real_time () / G_USEC_PER_SEC, "update-time", (guint64) g_get_real_time () / G_USEC_PER_SEC,
NULL); NULL);
@ -395,9 +399,90 @@ up_device_battery_report (UpDeviceBattery *self,
up_device_battery_update_poll_frequency (self, values->state, reason); up_device_battery_update_poll_frequency (self, values->state, reason);
} }
static gboolean
up_device_battery_set_charge_thresholds(UpDeviceBattery *self, gdouble start, gdouble end, GError **error)
{
UpDeviceBatteryClass *klass = UP_DEVICE_BATTERY_GET_CLASS (self);
if (klass->set_battery_charge_thresholds == NULL)
return FALSE;
return klass->set_battery_charge_thresholds(&self->parent_instance, start, end, error);
}
static const gchar *
up_device_battery_get_state_dir (UpDeviceBattery *self)
{
UpDevice *device = UP_DEVICE (self);
const gchar *state_dir_override = NULL;
UpDeviceBatteryPrivate *priv = up_device_battery_get_instance_private (self);
if (priv->state_dir != NULL)
return priv->state_dir;
state_dir_override = up_device_get_state_dir_override (device);
if (state_dir_override != NULL)
priv->state_dir = state_dir_override;
else
priv->state_dir = STATE_DIR;
return priv->state_dir;
}
static gboolean
up_device_battery_get_battery_charge_threshold_config(UpDeviceBattery *self)
{
g_autofree gchar *filename = NULL;
g_autofree gchar *data = NULL;
g_autoptr(GError) error = NULL;
g_autofree gchar *state_filename = NULL;
const char *state_dir = NULL;
state_filename = g_strdup_printf("charging-threshold-status");
state_dir = up_device_battery_get_state_dir (self);
filename = g_build_filename (state_dir, state_filename, NULL);
if (g_file_get_contents (filename, &data, NULL, &error) == FALSE) {
g_debug ("failed to read battery charge threshold: %s", error->message);
return FALSE;
}
if (g_strcmp0(data, "1") == 0)
return TRUE;
return FALSE;
}
static void
up_device_battery_recover_battery_charging_threshold (UpDeviceBattery *self, UpBatteryInfo *info, gboolean *charge_threshold_enabled)
{
gboolean enabled = FALSE;
GError *error = NULL;
if (info == NULL)
return;
enabled = up_device_battery_get_battery_charge_threshold_config (self);
if (info->charge_control_supported == TRUE) {
if (enabled == TRUE) {
up_device_battery_set_charge_thresholds (self,
info->charge_control_start_threshold,
info->charge_control_end_threshold,
&error);
if (error != NULL) {
enabled = FALSE;
g_warning ("Fail on setting charging threshold: %s", error->message);
g_clear_error (&error);
}
}
}
*charge_threshold_enabled = enabled;
}
void void
up_device_battery_update_info (UpDeviceBattery *self, UpBatteryInfo *info) up_device_battery_update_info (UpDeviceBattery *self, UpBatteryInfo *info)
{ {
gboolean charge_threshold_enabled = FALSE;
UpDeviceBatteryPrivate *priv = up_device_battery_get_instance_private (self); UpDeviceBatteryPrivate *priv = up_device_battery_get_instance_private (self);
/* First, sanitize the information. */ /* First, sanitize the information. */
@ -437,6 +522,9 @@ up_device_battery_update_info (UpDeviceBattery *self, UpBatteryInfo *info)
/* See above, we have a (new) battery plugged in. */ /* See above, we have a (new) battery plugged in. */
if (!priv->present) { if (!priv->present) {
/* Set up battery charging threshold when a new battery was plugged in */
up_device_battery_recover_battery_charging_threshold (self, info, &charge_threshold_enabled);
g_object_set (self, g_object_set (self,
"is-present", TRUE, "is-present", TRUE,
"vendor", info->vendor, "vendor", info->vendor,
@ -445,6 +533,13 @@ up_device_battery_update_info (UpDeviceBattery *self, UpBatteryInfo *info)
"technology", info->technology, "technology", info->technology,
"has-history", TRUE, "has-history", TRUE,
"has-statistics", TRUE, "has-statistics", TRUE,
"charge-start-threshold", info->charge_control_start_threshold,
"charge-end-threshold", info->charge_control_end_threshold,
"charge-threshold-enabled", charge_threshold_enabled,
"charge-threshold-supported", info->charge_control_supported,
"charge-threshold-settings-supported", info->charge_threshold_settings,
"voltage-min-design", info->voltage_min_design,
"voltage-max-design", info->voltage_max_design,
NULL); NULL);
priv->present = TRUE; priv->present = TRUE;
@ -504,19 +599,121 @@ up_device_battery_update_info (UpDeviceBattery *self, UpBatteryInfo *info)
"vendor", NULL, "vendor", NULL,
"model", NULL, "model", NULL,
"serial", NULL, "serial", NULL,
"state", UP_DEVICE_STATE_UNKNOWN,
"technology", UP_DEVICE_TECHNOLOGY_UNKNOWN, "technology", UP_DEVICE_TECHNOLOGY_UNKNOWN,
"capacity", (gdouble) 0.0, "capacity", (gdouble) 0.0,
"energy", (gdouble) 0.0,
"energy-full", (gdouble) 0.0, "energy-full", (gdouble) 0.0,
"energy-full-design", (gdouble) 0.0, "energy-full-design", (gdouble) 0.0,
"voltage", (gdouble) 0.0,
"charge-cycles", -1, "charge-cycles", -1,
"has-history", FALSE, "has-history", FALSE,
"has-statistics", FALSE, "has-statistics", FALSE,
"percentage", (gdouble) 0.0,
"temperature", (gdouble) 0.0,
"update-time", (guint64) g_get_real_time () / G_USEC_PER_SEC, "update-time", (guint64) g_get_real_time () / G_USEC_PER_SEC,
"charge-start-threshold", 0,
"charge-end-threshold", 0,
"charge-threshold-enabled", FALSE,
"charge-threshold-supported", FALSE,
"charge-threshold-settings-supported", 0,
"voltage-min-design", (gdouble) 0.0,
"voltage-max-design", (gdouble) 0.0,
"capacity-level", NULL,
NULL); NULL);
} }
} }
static gboolean
up_device_battery_charge_threshold_state_write(UpDeviceBattery *self, gboolean enabled, const gchar *state_file) {
g_autofree gchar *filename = NULL;
GError *error = NULL;
const gchar *state_dir;
state_dir = up_device_battery_get_state_dir (self);
filename = g_build_filename (state_dir, state_file, NULL);
if (!g_file_set_contents (filename, enabled ? "1": "0" , -1, &error)) {
g_error ("failed to save battery charge threshold: %s", error->message);
return FALSE;
}
return TRUE;
}
/**
* up_device_battery_set_charge_threshold:
**/
static gboolean
up_device_battery_set_charge_threshold (UpExportedDevice *skeleton,
GDBusMethodInvocation *invocation,
gboolean enabled,
UpDeviceBattery *self)
{
gboolean ret = FALSE;
gboolean charge_threshold_enabled;
gboolean charge_threshold_supported;
guint charge_start_threshold = 0;
guint charge_end_threshold = 100;
g_autoptr (GError) error = NULL;
g_autofree gchar *state_file = NULL;
UpDevice *device = UP_DEVICE (self);
if (device == NULL) {
g_dbus_method_invocation_return_error (invocation,
UP_DAEMON_ERROR, UP_DAEMON_ERROR_GENERAL,
"Error on getting device");
return FALSE;
}
if (!up_device_polkit_is_allowed (device, invocation)) {
g_dbus_method_invocation_return_error (invocation,
UP_DAEMON_ERROR, UP_DAEMON_ERROR_GENERAL,
"Operation is not allowed.");
return TRUE;
}
g_object_get (self,
"charge-threshold-enabled", &charge_threshold_enabled,
"charge-threshold-supported", &charge_threshold_supported,
"charge-start-threshold", &charge_start_threshold,
"charge-end-threshold", &charge_end_threshold,
NULL);
if (!charge_threshold_supported) {
g_dbus_method_invocation_return_error (invocation,
UP_DAEMON_ERROR, UP_DAEMON_ERROR_GENERAL,
"setting battery charge thresholds is unsupported");
return TRUE;
}
state_file = g_strdup_printf("charging-threshold-status");
if (!up_device_battery_charge_threshold_state_write (self, enabled, state_file)) {
g_dbus_method_invocation_return_error (invocation,
UP_DAEMON_ERROR, UP_DAEMON_ERROR_GENERAL,
"writing charge limits state file '%s' failed", state_file);
return TRUE;
}
if (enabled)
ret = up_device_battery_set_charge_thresholds (self, charge_start_threshold, charge_end_threshold, &error);
else
ret = up_device_battery_set_charge_thresholds (self, 0, 100, &error);
if (!ret) {
g_dbus_method_invocation_return_error (invocation,
UP_DAEMON_ERROR, UP_DAEMON_ERROR_GENERAL,
"failed on setting charging threshold: %s", error->message);
return TRUE;
}
g_object_set(self,
"charge-threshold-enabled", enabled,
NULL);
up_exported_device_complete_enable_charge_threshold (skeleton, invocation);
return TRUE;
}
static void static void
up_device_battery_init (UpDeviceBattery *self) up_device_battery_init (UpDeviceBattery *self)
@ -526,6 +723,9 @@ up_device_battery_init (UpDeviceBattery *self)
"power-supply", TRUE, "power-supply", TRUE,
"is-rechargeable", TRUE, "is-rechargeable", TRUE,
NULL); NULL);
g_signal_connect (self, "handle-enable-charge-threshold",
G_CALLBACK (up_device_battery_set_charge_threshold), self);
} }
static void static void

View file

@ -32,7 +32,12 @@ G_DECLARE_DERIVABLE_TYPE (UpDeviceBattery, up_device_battery, UP, DEVICE_BATTERY
struct _UpDeviceBatteryClass struct _UpDeviceBatteryClass
{ {
UpDeviceClass parent_class; UpDeviceClass parent_class;
gboolean (* set_battery_charge_thresholds) (UpDevice *device,
guint start,
guint end,
GError **error);
}; };
typedef enum { typedef enum {
@ -59,6 +64,7 @@ typedef struct {
gdouble percentage; gdouble percentage;
gdouble voltage; gdouble voltage;
gdouble temperature; gdouble temperature;
gchar *capacity_level;
} UpBatteryValues; } UpBatteryValues;
typedef struct { typedef struct {
@ -84,6 +90,15 @@ typedef struct {
UpDeviceTechnology technology; UpDeviceTechnology technology;
gdouble voltage_design; gdouble voltage_design;
gint charge_cycles; gint charge_cycles;
gdouble voltage_max_design;
gdouble voltage_min_design;
/* battery charging threshold */
gboolean charge_control_enabled;
gboolean charge_control_supported;
guint charge_control_start_threshold;
guint charge_control_end_threshold;
guint charge_threshold_settings;
} UpBatteryInfo; } UpBatteryInfo;

View file

@ -0,0 +1,410 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
*
* Copyright (C) 2025 Kate Hsuan <p.hsuan@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "config.h"
#include <glib.h>
#include <glib/gstdio.h>
#include <glib/gi18n-lib.h>
#include <glib-object.h>
#include "up-native.h"
#include "up-device-kbd-backlight.h"
#include "up-stats-item.h"
typedef struct
{
UpDaemon *daemon;
GObject *native;
} UpDeviceKbdBacklightPrivate;
static void up_device_kbd_backlight_initable_iface_init (GInitableIface *iface);
G_DEFINE_TYPE_EXTENDED (UpDeviceKbdBacklight, up_device_kbd_backlight, UP_TYPE_EXPORTED_KBD_BACKLIGHT_SKELETON, 0,
G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
up_device_kbd_backlight_initable_iface_init)
G_ADD_PRIVATE (UpDeviceKbdBacklight))
enum {
PROP_0,
PROP_DAEMON,
PROP_NATIVE,
N_PROPS
};
/* Upower keeps the legacy keyboard backlight DBus API (/org/freedesktop/UPower/KbdBacklight/)
* for backward compatibility.
* The new API path is based on the legacy one and the suffix is the name of the keyboard
* backlight device. For example, the path of the keyboard backlight device
* "tpacpiookbd_backlight" is
* "/org/freedesktop/UPower/KbdBacklight/tpacpiookbd_backlight".*/
#define UP_DEVICES_KBD_BACKLIGHT_DBUS_PATH "/org/freedesktop/UPower/KbdBacklight"
static GParamSpec *properties[N_PROPS];
/**
* up_kbd_backlight_emit_change:
**/
void
up_device_kbd_backlight_emit_change(UpDeviceKbdBacklight *kbd_backlight, int value, const char *source)
{
up_exported_kbd_backlight_emit_brightness_changed (UP_EXPORTED_KBD_BACKLIGHT (kbd_backlight), value);
up_exported_kbd_backlight_emit_brightness_changed_with_source (UP_EXPORTED_KBD_BACKLIGHT (kbd_backlight), value, source);
}
/**
* up_kbd_backlight_get_brightness:
*
* Gets the current brightness
**/
static gboolean
up_kbd_backlight_get_brightness (UpExportedKbdBacklight *skeleton,
GDBusMethodInvocation *invocation,
UpDeviceKbdBacklight *kbd_backlight)
{
UpDeviceKbdBacklightClass *klass;
gint brightness = 0;
g_return_val_if_fail (UP_IS_DEVICE_KBD_BACKLIGHT (kbd_backlight), FALSE);
klass = UP_DEVICE_KBD_BACKLIGHT_GET_CLASS (kbd_backlight);
brightness = klass->get_brightness (kbd_backlight);
if (brightness >= 0) {
up_exported_kbd_backlight_complete_get_brightness (skeleton, invocation,
brightness);
} else {
g_dbus_method_invocation_return_error (invocation,
UP_DAEMON_ERROR, UP_DAEMON_ERROR_GENERAL,
"error reading brightness");
}
return TRUE;
}
/**
* up_kbd_backlight_get_max_brightness:
*
* Gets the max brightness
**/
static gboolean
up_kbd_backlight_get_max_brightness (UpExportedKbdBacklight *skeleton,
GDBusMethodInvocation *invocation,
UpDeviceKbdBacklight *kbd_backlight)
{
UpDeviceKbdBacklightClass *klass;
gint brightness = -1;
g_return_val_if_fail (UP_IS_DEVICE_KBD_BACKLIGHT (kbd_backlight), FALSE);
klass = UP_DEVICE_KBD_BACKLIGHT_GET_CLASS (kbd_backlight);
if (klass->get_max_brightness != NULL)
brightness = klass->get_max_brightness (kbd_backlight);
if (brightness >= 0) {
up_exported_kbd_backlight_complete_get_max_brightness (skeleton, invocation,
brightness);
} else {
g_dbus_method_invocation_return_error (invocation,
UP_DAEMON_ERROR, UP_DAEMON_ERROR_GENERAL,
"error reading max brightness");
}
return TRUE;
}
/**
* up_kbd_backlight_set_brightness:
*
* Sets the kbd backlight LED brightness.
**/
static gboolean
up_kbd_backlight_set_brightness (UpExportedKbdBacklight *skeleton,
GDBusMethodInvocation *invocation,
gint value,
UpDeviceKbdBacklight *kbd_backlight)
{
UpDeviceKbdBacklightClass *klass;
gboolean ret = FALSE;
g_return_val_if_fail (UP_IS_DEVICE_KBD_BACKLIGHT (kbd_backlight), FALSE);
klass = UP_DEVICE_KBD_BACKLIGHT_GET_CLASS (kbd_backlight);
if (klass->set_brightness == NULL) {
g_dbus_method_invocation_return_error (invocation,
UP_DAEMON_ERROR, UP_DAEMON_ERROR_GENERAL,
"setting brightness is unsupported");
return TRUE;
}
ret = klass->set_brightness (kbd_backlight, value);
if (ret) {
up_exported_kbd_backlight_complete_set_brightness (skeleton, invocation);
up_device_kbd_backlight_emit_change (kbd_backlight, value, "external");
} else {
g_dbus_method_invocation_return_error (invocation,
UP_DAEMON_ERROR, UP_DAEMON_ERROR_GENERAL,
"error writing brightness %d", value);
}
return TRUE;
}
GObject *
up_device_kbd_backlight_get_native (UpDeviceKbdBacklight *device)
{
UpDeviceKbdBacklightPrivate *priv = up_device_kbd_backlight_get_instance_private (device);
g_return_val_if_fail (UP_IS_DEVICE_KBD_BACKLIGHT (device), NULL);
return priv->native;
}
static gchar *
up_device_kbd_backlight_compute_object_path (UpDeviceKbdBacklight *device)
{
UpDeviceKbdBacklightPrivate *priv = up_device_kbd_backlight_get_instance_private (device);
g_autofree gchar *basename = NULL;
g_autofree gchar *id = NULL;
gchar *object_path;
const gchar *native_path;
guint i;
if (priv->native == NULL) {
return g_build_filename (UP_DEVICES_KBD_BACKLIGHT_DBUS_PATH, "KbdBacklight", NULL);
}
native_path = up_exported_kbd_backlight_get_native_path (UP_EXPORTED_KBD_BACKLIGHT (device));
basename = g_path_get_basename (native_path);
id = g_strjoin ("_", basename, NULL);
/* make DBUS valid path */
for (i=0; id[i] != '\0'; i++) {
if (id[i] == '-')
id[i] = '_';
if (id[i] == '.')
id[i] = 'x';
if (id[i] == ':')
id[i] = 'o';
if (id[i] == '@')
id[i] = '_';
}
object_path = g_build_filename (UP_DEVICES_KBD_BACKLIGHT_DBUS_PATH, id, NULL);
return object_path;
}
static void
up_device_kbd_backlight_export_skeleton (UpDeviceKbdBacklight *device,
const gchar *object_path)
{
UpDeviceKbdBacklightPrivate *priv = up_device_kbd_backlight_get_instance_private (device);
GError *error = NULL;
g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (device),
g_dbus_interface_skeleton_get_connection (G_DBUS_INTERFACE_SKELETON (priv->daemon)),
object_path,
&error);
if (error != NULL) {
g_critical ("error registering device on system bus: %s", error->message);
g_error_free (error);
}
}
gboolean
up_device_kbd_backlight_register (UpDeviceKbdBacklight *device)
{
g_autofree char *computed_object_path = NULL;
if (g_dbus_interface_skeleton_get_object_path (G_DBUS_INTERFACE_SKELETON (device)) != NULL)
return FALSE;
computed_object_path = up_device_kbd_backlight_compute_object_path (device);
g_debug ("Exported Keyboard backlight with path %s", computed_object_path);
up_device_kbd_backlight_export_skeleton (device, computed_object_path);
return TRUE;
}
void
up_device_kbd_backlight_unregister (UpDeviceKbdBacklight *device)
{
g_autofree char *object_path = NULL;
object_path = g_strdup (g_dbus_interface_skeleton_get_object_path (G_DBUS_INTERFACE_SKELETON (device)));
if (object_path != NULL) {
g_dbus_interface_skeleton_unexport (G_DBUS_INTERFACE_SKELETON (device));
g_debug ("Unexported UpDeviceKbdBacklight with path %s", object_path);
}
}
const gchar *
up_device_kbd_backlight_get_object_path (UpDeviceKbdBacklight *device)
{
g_return_val_if_fail (UP_IS_DEVICE_KBD_BACKLIGHT (device), NULL);
return g_dbus_interface_skeleton_get_object_path (G_DBUS_INTERFACE_SKELETON (device));
}
static void
up_device_kbd_backlight_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
UpDeviceKbdBacklight *device = UP_DEVICE_KBD_BACKLIGHT (object);
UpDeviceKbdBacklightPrivate *priv = up_device_kbd_backlight_get_instance_private (device);
switch (prop_id)
{
case PROP_DAEMON:
priv->daemon = g_value_dup_object (value);
break;
case PROP_NATIVE:
priv->native = g_value_dup_object (value);
if (priv->native == NULL)
g_warning ("KBD native is NULL");
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
up_device_kbd_backlight_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
switch (prop_id)
{
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static gboolean
up_device_kbd_backlight_initable_init (GInitable *initable,
GCancellable *cancellable,
GError **error)
{
UpDeviceKbdBacklight *device = UP_DEVICE_KBD_BACKLIGHT (initable);
UpDeviceKbdBacklightPrivate *priv = up_device_kbd_backlight_get_instance_private (device);
const gchar *native_path = NULL;
UpDeviceKbdBacklightClass *klass = UP_DEVICE_KBD_BACKLIGHT_GET_CLASS (device);
int ret;
g_return_val_if_fail (UP_IS_DEVICE_KBD_BACKLIGHT (device), FALSE);
if (priv->native) {
native_path = up_native_get_native_path (priv->native);
up_exported_kbd_backlight_set_native_path (UP_EXPORTED_KBD_BACKLIGHT (device), native_path);
}
/* coldplug source */
if (klass->coldplug != NULL) {
ret = klass->coldplug (device);
if (!ret) {
g_debug ("failed to coldplug %s", native_path);
g_propagate_error (error, g_error_new (G_IO_ERROR, G_IO_ERROR_FAILED,
"Failed to coldplug %s", native_path));
return FALSE;
}
}
up_device_kbd_backlight_register (device);
return TRUE;
}
static void
up_device_kbd_backlight_initable_iface_init (GInitableIface *iface)
{
iface->init = up_device_kbd_backlight_initable_init;
}
/**
* up_kbd_backlight_init:
**/
static void
up_device_kbd_backlight_init (UpDeviceKbdBacklight *kbd_backlight)
{
g_signal_connect (kbd_backlight, "handle-get-brightness",
G_CALLBACK (up_kbd_backlight_get_brightness), kbd_backlight);
g_signal_connect (kbd_backlight, "handle-get-max-brightness",
G_CALLBACK (up_kbd_backlight_get_max_brightness), kbd_backlight);
g_signal_connect (kbd_backlight, "handle-set-brightness",
G_CALLBACK (up_kbd_backlight_set_brightness), kbd_backlight);
}
/**
* up_kbd_backlight_finalize:
**/
static void
up_device_kbd_backlight_finalize (GObject *object)
{
g_return_if_fail (object != NULL);
g_return_if_fail (UP_IS_DEVICE_KBD_BACKLIGHT (object));
G_OBJECT_CLASS (up_device_kbd_backlight_parent_class)->finalize (object);
}
static void
up_device_kbd_backlight_class_init (UpDeviceKbdBacklightClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = up_device_kbd_backlight_finalize;
object_class->set_property = up_device_kbd_backlight_set_property;
object_class->get_property = up_device_kbd_backlight_get_property;
properties[PROP_DAEMON] =
g_param_spec_object ("daemon",
"UpDaemon",
"UpDaemon reference",
UP_TYPE_DAEMON,
G_PARAM_STATIC_STRINGS | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY);
properties[PROP_NATIVE] =
g_param_spec_object ("native",
"Native",
"Native Object",
G_TYPE_OBJECT,
G_PARAM_STATIC_STRINGS | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY);
g_object_class_install_properties (object_class, N_PROPS, properties);
}
/**
* up_kbd_backlight_new:
**/
UpDeviceKbdBacklight *
up_device_kbd_backlight_new (UpDaemon *daemon, GObject *native)
{
return UP_DEVICE_KBD_BACKLIGHT (g_object_new (UP_TYPE_DEVICE_KBD_BACKLIGHT,
"daemon", daemon,
"native", native,
NULL));
}

View file

@ -0,0 +1,60 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
*
* Copyright (C) 2025 Kate Hsuan <p.hsuan@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifndef __UP_DEVICE_KBD_BACKLIGHT_H__
#define __UP_DEVICE_KBD_BACKLIGHT_H__
#include <dbus/up-kbd-backlight-generated.h>
#include "up-daemon.h"
G_BEGIN_DECLS
#define UP_TYPE_DEVICE_KBD_BACKLIGHT (up_device_kbd_backlight_get_type ())
G_DECLARE_DERIVABLE_TYPE (UpDeviceKbdBacklight, up_device_kbd_backlight, UP, DEVICE_KBD_BACKLIGHT, UpExportedKbdBacklightSkeleton)
struct _UpDeviceKbdBacklightClass
{
UpExportedKbdBacklightSkeletonClass parent_class;
gboolean (*coldplug) (UpDeviceKbdBacklight *device);
gint (*get_max_brightness) (UpDeviceKbdBacklight *device);
gint (*get_brightness) (UpDeviceKbdBacklight *device);
gboolean (*set_brightness) (UpDeviceKbdBacklight *device, gint brightness);
};
GType up_device_kbd_backlight_get_type (void);
void up_device_kbd_backlight_emit_change (UpDeviceKbdBacklight *kbd_backlight,
int value,
const char *source);
const gchar *up_device_kbd_backlight_get_object_path (UpDeviceKbdBacklight *device);
GObject *up_device_kbd_backlight_get_native (UpDeviceKbdBacklight *device);
UpDeviceKbdBacklight *up_device_kbd_backlight_new (UpDaemon *daemon,
GObject *native);
gboolean up_device_kbd_backlight_register (UpDeviceKbdBacklight *device);
void up_device_kbd_backlight_unregister (UpDeviceKbdBacklight *device);
G_END_DECLS
#endif /* __UP_DEVICE_KBD_BACKLIGHT_H__ */

View file

@ -28,6 +28,7 @@
#include "up-native.h" #include "up-native.h"
#include "up-device-list.h" #include "up-device-list.h"
#include "up-device.h" #include "up-device.h"
#include "up-device-kbd-backlight.h"
static void up_device_list_finalize (GObject *object); static void up_device_list_finalize (GObject *object);
@ -80,7 +81,10 @@ up_device_list_insert (UpDeviceList *list, gpointer device)
g_return_val_if_fail (UP_IS_DEVICE_LIST (list), FALSE); g_return_val_if_fail (UP_IS_DEVICE_LIST (list), FALSE);
g_return_val_if_fail (device != NULL, FALSE); g_return_val_if_fail (device != NULL, FALSE);
native = up_device_get_native (UP_DEVICE (device)); if (UP_IS_DEVICE_KBD_BACKLIGHT (device))
native = up_device_kbd_backlight_get_native (UP_DEVICE_KBD_BACKLIGHT (device));
else
native = up_device_get_native (UP_DEVICE (device));
g_return_val_if_fail (native != NULL, FALSE); g_return_val_if_fail (native != NULL, FALSE);
native_path = up_native_get_native_path (native); native_path = up_native_get_native_path (native);
@ -212,4 +216,3 @@ up_device_list_new (void)
{ {
return g_object_new (UP_TYPE_DEVICE_LIST, NULL); return g_object_new (UP_TYPE_DEVICE_LIST, NULL);
} }

View file

@ -65,4 +65,3 @@ GPtrArray *up_device_list_get_array (UpDeviceList *list);
G_END_DECLS G_END_DECLS
#endif /* __UP_DEVICE_LIST_H */ #endif /* __UP_DEVICE_LIST_H */

View file

@ -220,6 +220,7 @@ up_device_notify (GObject *object, GParamSpec *pspec)
{ {
UpDevice *device = UP_DEVICE (object); UpDevice *device = UP_DEVICE (object);
UpDevicePrivate *priv = up_device_get_instance_private (device); UpDevicePrivate *priv = up_device_get_instance_private (device);
g_autofree gchar *id = NULL;
/* Not finished setting up the object? */ /* Not finished setting up the object? */
if (priv->daemon == NULL) if (priv->daemon == NULL)
@ -227,18 +228,20 @@ up_device_notify (GObject *object, GParamSpec *pspec)
G_OBJECT_CLASS (up_device_parent_class)->notify (object, pspec); G_OBJECT_CLASS (up_device_parent_class)->notify (object, pspec);
id = up_device_get_id (device);
if (g_strcmp0 (pspec->name, "type") == 0 || if (g_strcmp0 (pspec->name, "type") == 0 ||
g_strcmp0 (pspec->name, "is-present") == 0) { g_strcmp0 (pspec->name, "is-present") == 0) {
update_icon_name (device); update_icon_name (device);
/* Clearing the history object for lazily loading when device id was changed. */ /* Clearing the history object for lazily loading when device id was changed. */
if (priv->history != NULL && if (priv->history != NULL &&
!up_history_is_device_id_equal (priv->history, up_device_get_id(device))) !up_history_is_device_id_equal (priv->history, id))
g_clear_object (&priv->history); g_clear_object (&priv->history);
} else if (g_strcmp0 (pspec->name, "vendor") == 0 || } else if (g_strcmp0 (pspec->name, "vendor") == 0 ||
g_strcmp0 (pspec->name, "model") == 0 || g_strcmp0 (pspec->name, "model") == 0 ||
g_strcmp0 (pspec->name, "serial") == 0) { g_strcmp0 (pspec->name, "serial") == 0) {
if (priv->history != NULL && if (priv->history != NULL &&
!up_history_is_device_id_equal (priv->history, up_device_get_id(device))) !up_history_is_device_id_equal (priv->history, id))
g_clear_object (&priv->history); g_clear_object (&priv->history);
} else if (g_strcmp0 (pspec->name, "power-supply") == 0 || } else if (g_strcmp0 (pspec->name, "power-supply") == 0 ||
g_strcmp0 (pspec->name, "time-to-empty") == 0) { g_strcmp0 (pspec->name, "time-to-empty") == 0) {
@ -289,6 +292,13 @@ up_device_get_online (UpDevice *device, gboolean *online)
return klass->get_online (device, online); return klass->get_online (device, online);
} }
/**
* up_device_get_id:
*
* Return the id of a Device.
* Note: The caller of the method takes ownership of the returned data, and is
* responsible for freeing it.
**/
static gchar * static gchar *
up_device_get_id (UpDevice *device) up_device_get_id (UpDevice *device)
{ {
@ -392,6 +402,22 @@ up_device_get_daemon (UpDevice *device)
return g_object_ref (priv->daemon); return g_object_ref (priv->daemon);
} }
/**
* up_device_polkit_is_allowed
**/
gboolean
up_device_polkit_is_allowed (UpDevice *device, GDBusMethodInvocation *invocation)
{
UpDevicePrivate *priv = up_device_get_instance_private (device);
if (!up_daemon_polkit_is_allowed (priv->daemon,
"org.freedesktop.UPower.enable-charging-limit",
invocation))
return FALSE;
return TRUE;
}
static void static void
up_device_export_skeleton (UpDevice *device, up_device_export_skeleton (UpDevice *device,
const gchar *object_path) const gchar *object_path)
@ -644,7 +670,7 @@ up_device_get_history (UpExportedDevice *skeleton,
else if (g_strcmp0 (type_string, "time-empty") == 0) else if (g_strcmp0 (type_string, "time-empty") == 0)
type = UP_HISTORY_TYPE_TIME_EMPTY; type = UP_HISTORY_TYPE_TIME_EMPTY;
/* something recognised */ /* something recognized */
if (type != UP_HISTORY_TYPE_UNKNOWN) { if (type != UP_HISTORY_TYPE_UNKNOWN) {
ensure_history (device); ensure_history (device);
array = up_history_get_data (priv->history, type, timespan, resolution); array = up_history_get_data (priv->history, type, timespan, resolution);
@ -735,6 +761,17 @@ up_device_get_native (UpDevice *device)
return priv->native; return priv->native;
} }
const gchar *
up_device_get_state_dir_override (UpDevice *device)
{
UpDevicePrivate *priv = up_device_get_instance_private (device);
if (priv->daemon == NULL)
return NULL;
return up_daemon_get_state_dir_env_override (priv->daemon);
}
static void static void
up_device_init (UpDevice *device) up_device_init (UpDevice *device)
{ {
@ -878,7 +915,7 @@ up_device_class_init (UpDeviceClass *klass)
properties[PROP_DISCONNECTED] = properties[PROP_DISCONNECTED] =
g_param_spec_boolean ("disconnected", g_param_spec_boolean ("disconnected",
"Disconnected", "Disconnected",
"Whethe wireless device is disconnected", "Whether wireless device is disconnected",
FALSE, FALSE,
G_PARAM_STATIC_STRINGS | G_PARAM_WRITABLE | G_PARAM_READABLE); G_PARAM_STATIC_STRINGS | G_PARAM_WRITABLE | G_PARAM_READABLE);

View file

@ -67,6 +67,9 @@ gboolean up_device_get_on_battery (UpDevice *device,
gboolean *on_battery); gboolean *on_battery);
gboolean up_device_get_online (UpDevice *device, gboolean up_device_get_online (UpDevice *device,
gboolean *online); gboolean *online);
const gchar *up_device_get_state_dir_override (UpDevice *device);
gboolean up_device_polkit_is_allowed (UpDevice *device,
GDBusMethodInvocation *invocation);
void up_device_sibling_discovered (UpDevice *device, void up_device_sibling_discovered (UpDevice *device,
GObject *sibling); GObject *sibling);
gboolean up_device_refresh_internal (UpDevice *device, gboolean up_device_refresh_internal (UpDevice *device,

View file

@ -155,11 +155,11 @@ up_history_array_limit_resolution (GPtrArray *array, guint max_num)
guint64 preset; guint64 preset;
item = (UpHistoryItem *) g_ptr_array_index (array, i); item = (UpHistoryItem *) g_ptr_array_index (array, i);
preset = last + ((first - last) * (guint64) step) / max_num; preset = first - ((first - last) * (guint64) step) / max_num;
/* if state changed or we went over the preset do a new point */ /* if state changed or we went over the preset do a new point */
if (count > 0 && if (count > 0 &&
(up_history_item_get_time (item) > preset || (up_history_item_get_time (item) < preset ||
up_history_item_get_state (item) != state)) { up_history_item_get_state (item) != state)) {
item_new = up_history_item_new (); item_new = up_history_item_new ();
up_history_item_set_time (item_new, time_s / count); up_history_item_set_time (item_new, time_s / count);
@ -255,7 +255,7 @@ up_history_get_data (UpHistory *history, UpHistoryType type, guint timespan, gui
else if (type == UP_HISTORY_TYPE_TIME_EMPTY) else if (type == UP_HISTORY_TYPE_TIME_EMPTY)
array_data = history->priv->data_time_empty; array_data = history->priv->data_time_empty;
/* not recognised */ /* not recognized */
if (array_data == NULL) if (array_data == NULL)
return NULL; return NULL;
@ -956,4 +956,3 @@ up_history_new (void)
{ {
return g_object_new (UP_TYPE_HISTORY, NULL); return g_object_new (UP_TYPE_HISTORY, NULL);
} }

View file

@ -91,4 +91,3 @@ void up_history_set_directory (UpHistory *history,
G_END_DECLS G_END_DECLS
#endif /* __UP_HISTORY_H */ #endif /* __UP_HISTORY_H */

View file

@ -210,6 +210,7 @@ main (gint argc, gchar **argv)
if (!g_option_context_parse (context, &argc, &argv, &error)) { if (!g_option_context_parse (context, &argc, &argv, &error)) {
g_warning ("Failed to parse command-line options: %s", error->message); g_warning ("Failed to parse command-line options: %s", error->message);
g_error_free (error); g_error_free (error);
g_option_context_free (context);
return 1; return 1;
} }
g_option_context_free (context); g_option_context_free (context);
@ -284,7 +285,7 @@ main (gint argc, gchar **argv)
g_source_set_name_by_id (timer_id, "[upower] up_main_timed_exit_cb"); g_source_set_name_by_id (timer_id, "[upower] up_main_timed_exit_cb");
} }
/* immediatly exit */ /* immediately exit */
if (immediate_exit) { if (immediate_exit) {
g_timeout_add (50, (GSourceFunc) up_main_timed_exit_cb, state); g_timeout_add (50, (GSourceFunc) up_main_timed_exit_cb, state);
g_source_set_name_by_id (timer_id, "[upower] up_main_timed_exit_cb"); g_source_set_name_by_id (timer_id, "[upower] up_main_timed_exit_cb");

View file

@ -30,4 +30,3 @@ const gchar *up_native_get_native_path (GObject *object);
G_END_DECLS G_END_DECLS
#endif /* __UP_NATIVE_H__ */ #endif /* __UP_NATIVE_H__ */

192
src/up-polkit.c Normal file
View file

@ -0,0 +1,192 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
*
* Copyright (C) 2008 David Zeuthen <davidz@redhat.com>
* Copyright (C) 2008 Richard Hughes <richard@hughsie.com>
*
* Licensed under the GNU General Public License Version 2
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <glib.h>
#ifdef HAVE_POLKIT
#include <polkit/polkit.h>
#endif
#include "up-polkit.h"
#include "up-daemon.h"
#define UP_POLKIT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), UP_TYPE_POLKIT, UpPolkitPrivate))
struct UpPolkitPrivate
{
GDBusConnection *connection;
#ifdef HAVE_POLKIT
PolkitAuthority *authority;
#endif
};
G_DEFINE_TYPE_WITH_PRIVATE (UpPolkit, up_polkit, G_TYPE_OBJECT)
#ifdef HAVE_POLKIT
/**
* up_polkit_get_subject:
**/
PolkitSubject *
up_polkit_get_subject (UpPolkit *polkit, GDBusMethodInvocation *invocation)
{
g_autoptr (GError) error = NULL;
const gchar *sender;
g_autoptr (PolkitSubject) subject = NULL;
sender = g_dbus_method_invocation_get_sender (invocation);
subject = polkit_system_bus_name_new (sender);
if (subject == NULL) {
error = g_error_new (UP_DAEMON_ERROR, UP_DAEMON_ERROR_GENERAL, "failed to get PolicyKit subject");
g_dbus_method_invocation_return_error (invocation,
UP_DAEMON_ERROR, UP_DAEMON_ERROR_GENERAL,
"failed to get Polkit subject: %s", error->message);
}
return g_steal_pointer (&subject);
}
/**
* up_polkit_check_auth:
**/
gboolean
up_polkit_check_auth (UpPolkit *polkit, PolkitSubject *subject, const gchar *action_id, GDBusMethodInvocation *invocation)
{
g_autoptr (GError) error = NULL;
g_autoptr (GError) error_local = NULL;
g_autoptr (PolkitAuthorizationResult) result = NULL;
/* check auth */
result = polkit_authority_check_authorization_sync (polkit->priv->authority,
subject, action_id, NULL,
POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION,
NULL, &error_local);
if (result == NULL) {
error = g_error_new (UP_DAEMON_ERROR, UP_DAEMON_ERROR_GENERAL, "failed to check authorization: %s", error_local->message);
g_dbus_method_invocation_return_gerror (invocation, error);
return FALSE;
}
/* okay? */
if (polkit_authorization_result_get_is_authorized (result)) {
return TRUE;
} else {
error = g_error_new (UP_DAEMON_ERROR, UP_DAEMON_ERROR_GENERAL, "not authorized");
g_dbus_method_invocation_return_gerror (invocation, error);
}
return FALSE;
}
/**
* up_polkit_is_allowed:
**/
gboolean
up_polkit_is_allowed (UpPolkit *polkit, PolkitSubject *subject, const gchar *action_id, GError **error)
{
gboolean ret = FALSE;
g_autoptr (GError) error_local = NULL;
g_autoptr (PolkitAuthorizationResult) result = NULL;
/* check auth */
result = polkit_authority_check_authorization_sync (polkit->priv->authority,
subject, action_id, NULL,
POLKIT_CHECK_AUTHORIZATION_FLAGS_NONE,
NULL, &error_local);
if (result == NULL) {
if (error_local != NULL)
g_set_error (error, UP_DAEMON_ERROR, UP_DAEMON_ERROR_GENERAL, "%s", error_local->message);
else
g_set_error (error, UP_DAEMON_ERROR, UP_DAEMON_ERROR_GENERAL, "failed to check authorization");
return FALSE;
}
ret = polkit_authorization_result_get_is_authorized (result) ||
polkit_authorization_result_get_is_challenge (result);
return ret;
}
#endif
/**
* up_polkit_finalize:
**/
static void
up_polkit_finalize (GObject *object)
{
#ifdef HAVE_POLKIT
g_autoptr (GError) error = NULL;
UpPolkit *polkit;
g_return_if_fail (UP_IS_POLKIT (object));
polkit = UP_POLKIT (object);
if (polkit->priv->connection != NULL)
g_object_unref (polkit->priv->connection);
g_object_unref (polkit->priv->authority);
#endif
G_OBJECT_CLASS (up_polkit_parent_class)->finalize (object);
}
/**
* up_polkit_class_init:
**/
static void
up_polkit_class_init (UpPolkitClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = up_polkit_finalize;
}
/**
* up_polkit_init:
*
* initializes the polkit class. NOTE: We expect polkit objects
* to *NOT* be removed or added during the session.
* We only control the first polkit object if there are more than one.
**/
static void
up_polkit_init (UpPolkit *polkit)
{
g_autoptr (GError) error = NULL;
polkit->priv = up_polkit_get_instance_private (polkit);
#ifdef HAVE_POLKIT
polkit->priv->authority = polkit_authority_get_sync (NULL, &error);
if (polkit->priv->authority == NULL)
g_error ("failed to get polkit authority: %s", error->message);
#endif
}
/**
* up_polkit_new:
* Return value: A new polkit class instance.
**/
UpPolkit *
up_polkit_new (void)
{
return UP_POLKIT (g_object_new (UP_TYPE_POLKIT, NULL));
}

72
src/up-polkit.h Normal file
View file

@ -0,0 +1,72 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
*
* Copyright (C) 2008 Richard Hughes <richard@hughsie.com>
*
* Licensed under the GNU General Public License Version 2
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef __UP_POLKIT_H
#define __UP_POLKIT_H
#include "config.h"
#include <glib-object.h>
#ifdef HAVE_POLKIT
#include <polkit/polkit.h>
#endif
G_BEGIN_DECLS
#define UP_TYPE_POLKIT (up_polkit_get_type ())
#define UP_POLKIT(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), UP_TYPE_POLKIT, UpPolkit))
#define UP_POLKIT_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), UP_TYPE_POLKIT, UpPolkitClass))
#define UP_IS_POLKIT(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), UP_TYPE_POLKIT))
#define UP_IS_POLKIT_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), UP_TYPE_POLKIT))
#define UP_POLKIT_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), UP_TYPE_POLKIT, UpPolkitClass))
typedef struct UpPolkitPrivate UpPolkitPrivate;
typedef struct
{
GObject parent;
UpPolkitPrivate *priv;
} UpPolkit;
typedef struct
{
GObjectClass parent_class;
} UpPolkitClass;
GType up_polkit_get_type (void);
UpPolkit *up_polkit_new (void);
#ifdef HAVE_POLKIT
PolkitSubject *up_polkit_get_subject (UpPolkit *polkit,
GDBusMethodInvocation *context);
gboolean up_polkit_check_auth (UpPolkit *polkit,
PolkitSubject *subject,
const gchar *action_id,
GDBusMethodInvocation *context);
gboolean up_polkit_is_allowed (UpPolkit *polkit,
PolkitSubject *subject,
const gchar *action_id,
GError **error);
#endif
G_END_DECLS
#endif /* __UP_POLKIT_H */

View file

@ -33,6 +33,7 @@
#include "up-device-list.h" #include "up-device-list.h"
#include "up-history.h" #include "up-history.h"
#include "up-native.h" #include "up-native.h"
#include "up-polkit.h"
gchar *history_dir = NULL; gchar *history_dir = NULL;
@ -64,6 +65,12 @@ up_test_daemon_func (void)
{ {
UpDaemon *daemon; UpDaemon *daemon;
/* needs polkit, which only listens to the system bus */
if (!g_file_test (DBUS_SYSTEM_SOCKET, G_FILE_TEST_EXISTS)) {
puts("No system D-BUS running, skipping test");
return;
}
daemon = up_daemon_new (); daemon = up_daemon_new ();
g_assert (daemon != NULL); g_assert (daemon != NULL);
@ -159,7 +166,7 @@ up_test_history_func (void)
ret = up_history_set_id (history, "test"); ret = up_history_set_id (history, "test");
g_assert (ret); g_assert (ret);
/* get nonexistant data */ /* get nonexistent data */
array = up_history_get_data (history, UP_HISTORY_TYPE_CHARGE, 10, 100); array = up_history_get_data (history, UP_HISTORY_TYPE_CHARGE, 10, 100);
g_assert (array != NULL); g_assert (array != NULL);
g_assert_cmpint (array->len, ==, 0); g_assert_cmpint (array->len, ==, 0);
@ -274,6 +281,24 @@ up_test_history_func (void)
rmdir (history_dir); rmdir (history_dir);
} }
static void
up_test_polkit_func (void)
{
UpPolkit *polkit;
/* polkit only listens to the system bus */
if (!g_file_test (DBUS_SYSTEM_SOCKET, G_FILE_TEST_EXISTS)) {
puts("No system D-BUS running, skipping test");
return;
}
polkit = up_polkit_new ();
g_assert (polkit != NULL);
/* unref */
g_object_unref (polkit);
}
int int
main (int argc, char **argv) main (int argc, char **argv)
{ {
@ -290,8 +315,8 @@ main (int argc, char **argv)
g_test_add_func ("/power/device_list", up_test_device_list_func); g_test_add_func ("/power/device_list", up_test_device_list_func);
g_test_add_func ("/power/history", up_test_history_func); g_test_add_func ("/power/history", up_test_history_func);
g_test_add_func ("/power/native", up_test_native_func); g_test_add_func ("/power/native", up_test_native_func);
g_test_add_func ("/power/polkit", up_test_polkit_func);
g_test_add_func ("/power/daemon", up_test_daemon_func); g_test_add_func ("/power/daemon", up_test_daemon_func);
return g_test_run (); return g_test_run ();
} }

View file

@ -181,30 +181,116 @@ up_tool_do_monitor (UpClient *client)
return FALSE; return FALSE;
} }
static void
up_tool_output_daemon (UpClient *client)
{
g_print ("Daemon:\n");
up_client_print (client);
}
static gint
up_tool_output_display_device (UpClient *client)
{
g_autoptr (UpDevice) device = NULL;
g_autofree gchar *text = NULL;
device = up_client_get_display_device (client);
if (!device) {
g_print ("Failed to get display device\n");
return 1;
}
g_print ("Device: %s\n", up_device_get_object_path (device));
text = up_device_to_text (device);
g_print ("%s\n", text);
return 0;
}
static gint
up_tool_output_device_dump (UpClient *client, GList *device_filter)
{
g_autoptr (GPtrArray) devices = NULL;
UpDevice *device;
guint i;
guint kind = 0;
gint ret = 0;
gchar *text = NULL;
devices = up_client_get_devices2 (client);
if (!devices) {
g_print ("Failed to get device list\n");
return 1;
}
for (i=0; i < devices->len; i++) {
device = (UpDevice*) g_ptr_array_index (devices, i);
g_object_get (device, "kind", &kind, NULL);
if (g_list_find (device_filter, GINT_TO_POINTER (kind)) || device_filter == NULL) {
g_print ("Device: %s\n", up_device_get_object_path (device));
text = up_device_to_text (device);
g_print ("%s\n", text);
g_free (text);
}
}
if (device_filter == NULL) {
ret = up_tool_output_display_device (client);
up_tool_output_daemon (client);
}
return ret;
}
static gint
up_tool_output_enumerate (UpClient *client)
{
g_autoptr (GPtrArray) devices = NULL;
g_autoptr (UpDevice) display_device = NULL;
UpDevice *device;
guint i;
devices = up_client_get_devices2 (client);
for (i = 0; i < devices->len; i++) {
device = (UpDevice*) g_ptr_array_index (devices, i);
g_print ("%s\n", up_device_get_object_path (device));
}
display_device = up_client_get_display_device (client);
if (display_device == NULL) {
g_print ("Failed to get display device\n");
return 1;
}
g_print ("%s\n", up_device_get_object_path (display_device));
return 0;
}
/** /**
* main: * main:
**/ **/
int int
main (int argc, char **argv) main (int argc, char **argv)
{ {
gint retval = EXIT_FAILURE;
guint i;
GOptionContext *context; GOptionContext *context;
gboolean opt_battery = FALSE;
gboolean opt_dump = FALSE; gboolean opt_dump = FALSE;
gboolean opt_enumerate = FALSE; gboolean opt_enumerate = FALSE;
gboolean opt_monitor = FALSE; gboolean opt_monitor = FALSE;
gchar *opt_show_info = FALSE; gchar *opt_show_info = FALSE;
gboolean opt_version = FALSE; gboolean opt_version = FALSE;
GList *device_filter = NULL;
gboolean ret; gboolean ret;
gint retval;
GError *error = NULL; GError *error = NULL;
gchar *text = NULL; gchar *text = NULL;
UpClient *client; g_autoptr (UpClient) client = NULL;
UpDevice *device; UpDevice *device;
const GOptionEntry entries[] = { const GOptionEntry entries[] = {
{ "enumerate", 'e', 0, G_OPTION_ARG_NONE, &opt_enumerate, _("Enumerate objects paths for devices"), NULL }, { "battery", 'b', 0, G_OPTION_ARG_NONE, &opt_battery, _("Dump all parameters for battery objects"), NULL },
{ "dump", 'd', 0, G_OPTION_ARG_NONE, &opt_dump, _("Dump all parameters for all objects"), NULL }, { "dump", 'd', 0, G_OPTION_ARG_NONE, &opt_dump, _("Dump all parameters for all objects"), NULL },
{ "enumerate", 'e', 0, G_OPTION_ARG_NONE, &opt_enumerate, _("Enumerate objects paths for devices"), NULL },
{ "monitor", 'm', 0, G_OPTION_ARG_NONE, &opt_monitor, _("Monitor activity from the power daemon"), NULL }, { "monitor", 'm', 0, G_OPTION_ARG_NONE, &opt_monitor, _("Monitor activity from the power daemon"), NULL },
{ "monitor-detail", 0, 0, G_OPTION_ARG_NONE, &opt_monitor_detail, _("Monitor with detail"), NULL }, { "monitor-detail", 0, 0, G_OPTION_ARG_NONE, &opt_monitor_detail, _("Monitor with detail"), NULL },
{ "show-info", 'i', 0, G_OPTION_ARG_STRING, &opt_show_info, _("Show information about object path"), NULL }, { "show-info", 'i', 0, G_OPTION_ARG_STRING, &opt_show_info, _("Show information about object path"), NULL },
@ -219,8 +305,13 @@ main (int argc, char **argv)
context = g_option_context_new ("UPower tool"); context = g_option_context_new ("UPower tool");
g_option_context_add_main_entries (context, entries, NULL); g_option_context_add_main_entries (context, entries, NULL);
g_option_context_parse (context, &argc, &argv, NULL); ret = g_option_context_parse (context, &argc, &argv, &error);
g_option_context_free (context); g_option_context_free (context);
if (!ret) {
g_print ("Failed to parse command-line options: %s\n", error->message);
g_error_free (error);
return EXIT_FAILURE;
}
loop = g_main_loop_new (NULL, FALSE); loop = g_main_loop_new (NULL, FALSE);
client = up_client_new_full (NULL, &error); client = up_client_new_full (NULL, &error);
@ -231,64 +322,34 @@ main (int argc, char **argv)
} }
if (opt_version) { if (opt_version) {
gchar *daemon_version; g_autofree gchar *daemon_version = NULL;
g_object_get (client, g_object_get (client,
"daemon-version", &daemon_version, "daemon-version", &daemon_version,
NULL); NULL);
g_print ("UPower client version %s\n" g_print ("UPower client version %s\n"
"UPower daemon version %s\n", "UPower daemon version %s\n",
PACKAGE_VERSION, daemon_version); PACKAGE_VERSION, daemon_version);
g_free (daemon_version); return EXIT_SUCCESS;
retval = 0;
goto out;
} }
if (opt_enumerate || opt_dump) { if (opt_enumerate)
GPtrArray *devices; return up_tool_output_enumerate (client);
devices = up_client_get_devices2 (client);
if (!devices) { if (opt_battery) {
g_print ("Failed to get device list\n"); device_filter = g_list_append (device_filter, GINT_TO_POINTER (UP_DEVICE_KIND_BATTERY));
goto out; opt_dump = TRUE;
} }
for (i=0; i < devices->len; i++) {
device = (UpDevice*) g_ptr_array_index (devices, i); if (opt_dump) {
if (opt_enumerate) { retval = up_tool_output_device_dump (client, device_filter);
g_print ("%s\n", up_device_get_object_path (device)); g_list_free (device_filter);
} else { return retval;
g_print ("Device: %s\n", up_device_get_object_path (device));
text = up_device_to_text (device);
g_print ("%s\n", text);
g_free (text);
}
}
g_ptr_array_unref (devices);
device = up_client_get_display_device (client);
if (!device) {
g_print ("Failed to get display device\n");
goto out;
}
if (opt_enumerate) {
g_print ("%s\n", up_device_get_object_path (device));
} else {
g_print ("Device: %s\n", up_device_get_object_path (device));
text = up_device_to_text (device);
g_print ("%s\n", text);
g_free (text);
}
g_object_unref (device);
if (opt_dump) {
g_print ("Daemon:\n");
up_client_print (client);
}
retval = EXIT_SUCCESS;
goto out;
} }
if (opt_monitor || opt_monitor_detail) { if (opt_monitor || opt_monitor_detail) {
if (!up_tool_do_monitor (client)) if (!up_tool_do_monitor (client))
goto out; return EXIT_FAILURE;
retval = EXIT_SUCCESS; return EXIT_SUCCESS;
goto out;
} }
if (opt_show_info != NULL) { if (opt_show_info != NULL) {
@ -303,10 +364,6 @@ main (int argc, char **argv)
g_free (text); g_free (text);
} }
g_object_unref (device); g_object_unref (device);
retval = EXIT_SUCCESS; return EXIT_SUCCESS;
goto out;
} }
out:
g_object_unref (client);
return retval;
} }