1 /* Not particularly good interface to hal, for programs that used to use
12 int num_batteries
= 0;
23 static void get_devinfo(gpointer device
, gpointer result
)
31 struct context
* ctx
= result
;
33 g_object_get(G_OBJECT(device
), "percentage", &percentage
,
37 "time-to-empty", &time_to_empty
,
38 "time-to-full", &time_to_full
,
40 if (kind
== UP_DEVICE_KIND_BATTERY
) {
41 if (ctx
->current
== ctx
->needed
) {
42 ctx
->percentage
= (int)percentage
;
45 ctx
->time
= time_to_empty
;
47 ctx
->time
= time_to_full
;
51 } else if (kind
== UP_DEVICE_KIND_LINE_POWER
) {
56 int upower_supported (void) {
64 GPtrArray
* devices
= up_client_get_devices(up
);
70 g_ptr_array_unref(devices
);
77 /* Fill the passed apm_info struct. */
78 int upower_read(int battery
, apm_info
*info
) {
80 GPtrArray
* devices
= NULL
;
88 #if !UP_CHECK_VERSION(0, 9, 99)
89 /* Allow a battery that was not present before to appear. */
90 up_client_enumerate_devices_sync(up
, NULL
, NULL
);
93 devices
= up_client_get_devices(up
);
99 info
->battery_flags
= 0;
100 info
->using_minutes
= 0;
102 struct context ctx
= {
104 .needed
= battery
- 1,
105 .state
= UP_DEVICE_STATE_UNKNOWN
,
111 g_ptr_array_foreach(devices
, &get_devinfo
, &ctx
);
113 info
->ac_line_status
= ctx
.ac
;
115 /* remaining_time and charge_level.percentage are not a mandatory
116 * keys, so if not present, -1 will be returned */
117 info
->battery_time
= ctx
.time
;
118 info
->battery_percentage
= ctx
.percentage
;
119 if (ctx
.state
== UP_DEVICE_STATE_DISCHARGING
) {
120 info
->battery_status
= BATTERY_STATUS_CHARGING
;
121 /* charge_level.warning and charge_level.low are not
122 * required to be available; this is good enough */
123 if (info
->battery_percentage
< 1) {
124 info
->battery_status
= BATTERY_STATUS_CRITICAL
;
126 else if (info
->battery_percentage
< 10) {
127 info
->battery_status
= BATTERY_STATUS_LOW
;
130 else if (info
->ac_line_status
&& ctx
.state
== UP_DEVICE_STATE_CHARGING
) {
131 info
->battery_status
= BATTERY_STATUS_CHARGING
;
132 info
->battery_flags
= info
->battery_flags
| BATTERY_FLAGS_CHARGING
;
134 else if (info
->ac_line_status
) {
135 /* Must be fully charged. */
136 info
->battery_status
= BATTERY_STATUS_HIGH
;
139 fprintf(stderr
, "unknown battery state\n");
142 if (ctx
.percentage
< 0) {
143 info
->battery_percentage
= 0;
144 info
->battery_time
= 0;
145 info
->battery_status
= BATTERY_STATUS_ABSENT
;
148 g_ptr_array_free(devices
, TRUE
);