Index | Thread | Search

From:
Landry Breuil <landry@openbsd.org>
Subject:
Re: qcpas: generate apm events upon ac state changes
To:
Peter Hessler <phessler@theapt.org>
Cc:
tech@openbsd.org, patrick@openbsd.org, jca@openbsd.org
Date:
Fri, 8 Nov 2024 20:02:58 +0100

Download raw body.

Thread
Le Fri, Nov 08, 2024 at 07:47:00PM +0100, Peter Hessler a écrit :
> On 2024 Nov 08 (Fri) at 19:31:08 +0100 (+0100), Landry Breuil wrote:
> :Le Fri, Nov 08, 2024 at 07:24:38PM +0100, Jeremie Courreges-Anglas a écrit :
> :> On Fri, Nov 08, 2024 at 07:06:44PM +0100, Landry Breuil wrote:
> :> > hi,
> :> > 
> :> > small diff that allows qcpas to send apm events when the ac is plugged
> :> > in/unplugged, thanks jca@ for the pointer.
> :> > 
> :> > in userland, that's used by sysutils/upower that gives me the fancy battery/ac
> :> > status via xfce4-power-manager on the x13s, before that it never got events on
> :> > ac state changes, only the battery levels.
> :> > 
> :> > i don't know if events should be sent upon battery level changes,
> :> 
> :> That would be nice for people using apmd -z/-Z.  Probably using the
> :> same trick but with info->battery_life.  Untested diff below if you
> :> want to give it a try.
> :
> :well, i actually needed it too so that the apm device "wokes" upowerd so
> :that it queries the sensors, otherwise it only reacted to ac events and not the
> :battery levels changing.. so i ended up with more or less the same diff, except
> :using uint32_t instead of u_char; not sold on it :)
> :
> :with that, upowerd properly updates the battery levels upon events.
> :
> 
> I slightly prefer the u_char version bcause that matches the type of
> info->battery_life

good point, will do a last test and commit that version. in the
meantime, i've tested that events were also properly sent on the
omnibook x14.

Index: qcpas.c
===================================================================
RCS file: /cvs/src/sys/dev/fdt/qcpas.c,v
diff -u -p -r1.7 qcpas.c
--- qcpas.c     1 Sep 2024 03:14:48 -0000       1.7
+++ qcpas.c     8 Nov 2024 19:02:32 -0000
@@ -1461,6 +1461,7 @@ qcpas_pmic_rtr_bat_status(struct qcpas_s
        extern int hw_power;
        struct apm_power_info *info = &qcpas_pmic_rtr_apm_power_info;
        uint32_t delta;
+       u_char nblife;
 #endif
 
 #ifndef SMALL_KERNEL
@@ -1509,8 +1510,10 @@ qcpas_pmic_rtr_bat_status(struct qcpas_s
                return;
        }
 
-       info->battery_life =
-           ((bat->capacity * 100) / sc->sc_last_full_capacity);
+       nblife = ((bat->capacity * 100) / sc->sc_last_full_capacity);
+       if (info->battery_life != nblife)
+               apm_record_event(APM_POWER_CHANGE);
+       info->battery_life = nblife;
        if (info->battery_life > 50)
                info->battery_state = APM_BATT_HIGH;
        else if (info->battery_life > 25)
@@ -1532,9 +1535,13 @@ qcpas_pmic_rtr_bat_status(struct qcpas_s
                info->minutes_left = (60 * delta) / abs(bat->rate);
 
        if (bat->power_state & BATTMGR_PWR_STATE_AC_ON) {
+               if (info->ac_state != APM_AC_ON)
+                       apm_record_event(APM_POWER_CHANGE);
                info->ac_state = APM_AC_ON;
                hw_power = 1;
        } else {
+               if (info->ac_state != APM_AC_OFF)
+                       apm_record_event(APM_POWER_CHANGE);
                info->ac_state = APM_AC_OFF;
                hw_power = 0;
        }