[PATCH] Fix battery information on FreeBSD

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

[PATCH] Fix battery information on FreeBSD

Ahmed Khanzada
I just emailed my signed document to become a GNU contributor. I am in
the US so my turnaround should be quick.

Battery status was not working for me on FreeBSD. I have made an
attempt to fix that.

Sorry if I am doing anything wrong. I will need some guidance on this
patching process. I hope to be an efficient GNU contributor soon.

I have only tested this on FreeBSD, as I do not have a laptop with
OpenBSD or NetBSD on it. I was wondering if maybe I should test these
changes on those OSs on some USB stick.

Man documents for APM on each BSD platform:

NetBSD: http://netbsd.gw.com/cgi-bin/man-cgi?apm++NetBSD-current
OpenBSD: https://man.openbsd.org/apm.8
FreeBSD: https://www.freebsd.org/cgi/man.cgi?apm

diff --git a/lisp/battery.el b/lisp/battery.el
index efd2a2181a..b14a9e45c2 100644
--- a/lisp/battery.el
+++ b/lisp/battery.el
@@ -618,30 +618,37 @@ battery-bsd-apm
 %t Remaining battery charge time in the form `h:min'"
   (let* ((os-name (car (split-string
             (shell-command-to-string "/usr/bin/uname"))))
-     (apm-flag (if (equal os-name "OpenBSD") "P" "s"))
-     (apm-cmd (concat "/usr/sbin/apm -ablm" apm-flag))
+     (apm-flag (if (equal os-name "OpenBSD") "Pm"))
+     (apm-flag (if (equal os-name "NetBSD") "sm"))
+     (apm-flag (if (equal os-name "FreeBSD") "st"))
+     (apm-cmd (concat "/usr/sbin/apm -abl" apm-flag))
      (apm-output (split-string (shell-command-to-string apm-cmd)))
+     (battery-status-index (if (equal os-name "FreeBSD") 1 0))
+     (apm-mode-index (if (equal os-name "FreeBSD") 3 4))
+     (battery-percentage-index (if (equal os-name "FreeBSD") 2 1))
+     (battery-life-index (if (equal os-name "FreeBSD") 4 3))
+     (ac-index (if (equal os-name "FreeBSD") 0 3))
      ;; Battery status
      (battery-status
-      (let ((stat (string-to-number (nth 0 apm-output))))
+      (let ((stat (string-to-number (nth battery-status-index apm-output))))
         (cond ((eq stat 0) '("high" . ""))
           ((eq stat 1) '("low" . "-"))
           ((eq stat 2) '("critical" . "!"))
           ((eq stat 3) '("charging" . "+"))
           ((eq stat 4) '("absent" . nil)))))
      ;; Battery percentage
-     (battery-percentage (nth 1 apm-output))
+     (battery-percentage (nth battery-percentage-index apm-output))
      ;; Battery life
-     (battery-life (nth 2 apm-output))
+     (battery-life (nth battery-life-index apm-output))
      ;; AC status
      (line-status
-      (let ((ac (string-to-number (nth 3 apm-output))))
+      (let ((ac (string-to-number (nth ac-index apm-output))))
         (cond ((eq ac 0) "disconnected")
           ((eq ac 1) "connected")
           ((eq ac 2) "backup power"))))
      ;; Advanced power savings mode
      (apm-mode
-      (let ((apm (string-to-number (nth 4 apm-output))))
+      (let ((apm (string-to-number (nth apm-mode-index apm-output))))
         (if (string= os-name "OpenBSD")
         (cond ((eq apm 0) "manual")
               ((eq apm 1) "automatic")

Reply | Threaded
Open this post in threaded view
|

Re: [PATCH] Fix battery information on FreeBSD

Stefan Monnier
>    (let* ((os-name (car (split-string
>              (shell-command-to-string "/usr/bin/uname"))))
> -     (apm-flag (if (equal os-name "OpenBSD") "P" "s"))
> -     (apm-cmd (concat "/usr/sbin/apm -ablm" apm-flag))
> +     (apm-flag (if (equal os-name "OpenBSD") "Pm"))
> +     (apm-flag (if (equal os-name "NetBSD") "sm"))
> +     (apm-flag (if (equal os-name "FreeBSD") "st"))

This can't be right: with the above apm-flag will be nil except on
FreeBSD (since the last apm-flag hides the previous ones).

You probably want

        (apm-flag (pcase os-name
                    ("OpenBSD" "Pm")
                    ("FreeBSD" "st")
                    (_         "sm")))

instead.


        Stefan


Reply | Threaded
Open this post in threaded view
|

[OFFTOPIC] (was: [PATCH] Fix battery information on FreeBSD)

Van L
In reply to this post by Ahmed Khanzada
Ahmed Khanzada writes:

> I hope to be an efficient GNU contributor soon.

Feature request. I had GNU/Emacs running beautifully with doom-themes,
doom-modeline. The temperature sensor for graceful shutdown can do
better.

While in /usr/pkgsrc on NetBSD doing a `make update` the temperature
critical event alarm tripped followed by shutdown. GNU/Emacs hasn't been
the same since. Gnus fails to connect. Eww fails to connect randomly.

--
© 2019 Van L
gpg using EEF2 37E9 3840 0D5D 9183  251E 9830 384E 9683 B835
"What's so strange when you know that you're a Wizard at 3?" -Joni Mitchell


Reply | Threaded
Open this post in threaded view
|

Re: [PATCH] Fix battery information on FreeBSD

Ahmed Khanzada
In reply to this post by Ahmed Khanzada
Thanks for the fix Stefan, sorry for the elementary error.

Updated patch below.

diff --git a/lisp/battery.el b/lisp/battery.el
index efd2a2181a..1ddca3d38e 100644
--- a/lisp/battery.el
+++ b/lisp/battery.el
@@ -618,30 +618,38 @@ battery-bsd-apm
 %t Remaining battery charge time in the form `h:min'"
   (let* ((os-name (car (split-string
             (shell-command-to-string "/usr/bin/uname"))))
-     (apm-flag (if (equal os-name "OpenBSD") "P" "s"))
-     (apm-cmd (concat "/usr/sbin/apm -ablm" apm-flag))
+     (apm-flag (pcase os-name
+             ("OpenBSD" "Pm")
+             ("FreeBSD" "st")
+             (_         "sm")))
+     (apm-cmd (concat "/usr/sbin/apm -abl" apm-flag))
      (apm-output (split-string (shell-command-to-string apm-cmd)))
+     (battery-status-index (if (equal os-name "FreeBSD") 1 0))
+     (apm-mode-index (if (equal os-name "FreeBSD") 3 4))
+     (battery-percentage-index (if (equal os-name "FreeBSD") 2 1))
+     (battery-life-index (if (equal os-name "FreeBSD") 4 3))
+     (ac-index (if (equal os-name "FreeBSD") 0 3))
      ;; Battery status
      (battery-status
-      (let ((stat (string-to-number (nth 0 apm-output))))
+      (let ((stat (string-to-number (nth battery-status-index apm-output))))
         (cond ((eq stat 0) '("high" . ""))
           ((eq stat 1) '("low" . "-"))
           ((eq stat 2) '("critical" . "!"))
           ((eq stat 3) '("charging" . "+"))
           ((eq stat 4) '("absent" . nil)))))
      ;; Battery percentage
-     (battery-percentage (nth 1 apm-output))
+     (battery-percentage (nth battery-percentage-index apm-output))
      ;; Battery life
-     (battery-life (nth 2 apm-output))
+     (battery-life (nth battery-life-index apm-output))
      ;; AC status
      (line-status
-      (let ((ac (string-to-number (nth 3 apm-output))))
+      (let ((ac (string-to-number (nth ac-index apm-output))))
         (cond ((eq ac 0) "disconnected")
           ((eq ac 1) "connected")
           ((eq ac 2) "backup power"))))
      ;; Advanced power savings mode
      (apm-mode
-      (let ((apm (string-to-number (nth 4 apm-output))))
+      (let ((apm (string-to-number (nth apm-mode-index apm-output))))
         (if (string= os-name "OpenBSD")
         (cond ((eq apm 0) "manual")
               ((eq apm 1) "automatic")

-- Ahmed

Reply | Threaded
Open this post in threaded view
|

Re: [OFFTOPIC] (was: [PATCH] Fix battery information on FreeBSD)

Ahmed Khanzada
In reply to this post by Van L
Love the Joni Mitchell quote, and her music.

I am interested in helping all things BSD in Emacs. Do you think the
best way to duplicate your error is to create a scenario like yours?

I'm also confused on a detail: did elisp or the OS trigger a shutdown
when a certain temperature was reached?

Reply | Threaded
Open this post in threaded view
|

Re: [PATCH] Fix battery information on FreeBSD

Stefan Monnier
In reply to this post by Ahmed Khanzada
> Thanks for the fix Stefan, sorry for the elementary error.
> Updated patch below.

Installed into `master`, thanks,


        Stefan


> diff --git a/lisp/battery.el b/lisp/battery.el
> index efd2a2181a..1ddca3d38e 100644
> --- a/lisp/battery.el
> +++ b/lisp/battery.el
> @@ -618,30 +618,38 @@ battery-bsd-apm
>  %t Remaining battery charge time in the form `h:min'"
>    (let* ((os-name (car (split-string
>              (shell-command-to-string "/usr/bin/uname"))))
> -     (apm-flag (if (equal os-name "OpenBSD") "P" "s"))
> -     (apm-cmd (concat "/usr/sbin/apm -ablm" apm-flag))
> +     (apm-flag (pcase os-name
> +             ("OpenBSD" "Pm")
> +             ("FreeBSD" "st")
> +             (_         "sm")))
> +     (apm-cmd (concat "/usr/sbin/apm -abl" apm-flag))
>       (apm-output (split-string (shell-command-to-string apm-cmd)))
> +     (battery-status-index (if (equal os-name "FreeBSD") 1 0))
> +     (apm-mode-index (if (equal os-name "FreeBSD") 3 4))
> +     (battery-percentage-index (if (equal os-name "FreeBSD") 2 1))
> +     (battery-life-index (if (equal os-name "FreeBSD") 4 3))
> +     (ac-index (if (equal os-name "FreeBSD") 0 3))
>       ;; Battery status
>       (battery-status
> -      (let ((stat (string-to-number (nth 0 apm-output))))
> +      (let ((stat (string-to-number (nth battery-status-index apm-output))))
>          (cond ((eq stat 0) '("high" . ""))
>            ((eq stat 1) '("low" . "-"))
>            ((eq stat 2) '("critical" . "!"))
>            ((eq stat 3) '("charging" . "+"))
>            ((eq stat 4) '("absent" . nil)))))
>       ;; Battery percentage
> -     (battery-percentage (nth 1 apm-output))
> +     (battery-percentage (nth battery-percentage-index apm-output))
>       ;; Battery life
> -     (battery-life (nth 2 apm-output))
> +     (battery-life (nth battery-life-index apm-output))
>       ;; AC status
>       (line-status
> -      (let ((ac (string-to-number (nth 3 apm-output))))
> +      (let ((ac (string-to-number (nth ac-index apm-output))))
>          (cond ((eq ac 0) "disconnected")
>            ((eq ac 1) "connected")
>            ((eq ac 2) "backup power"))))
>       ;; Advanced power savings mode
>       (apm-mode
> -      (let ((apm (string-to-number (nth 4 apm-output))))
> +      (let ((apm (string-to-number (nth apm-mode-index apm-output))))
>          (if (string= os-name "OpenBSD")
>          (cond ((eq apm 0) "manual")
>                ((eq apm 1) "automatic")
>
> -- Ahmed


Reply | Threaded
Open this post in threaded view
|

Re: graceful shutdown response to temperature climb

Van L
In reply to this post by Ahmed Khanzada
Ahmed Khanzada <[hidden email]> writes:

> Love the Joni Mitchell quote, and her music.

Me too. What an artist. She paints to recharge her creativity batteries
for music, as a farmer does by letting the land lie fallow.

> I am interested in helping all things BSD in Emacs. Do you think the
> best way to duplicate your error is to create a scenario like yours?

On NetBSD-8 stable /usr/sbin/envstat lists

-- quote
                           Current  CritMax  WarnMax  WarnMin  CritMin  Unit
            temperature:    73.000  127.000                             degC
  cpu0/cpu1 temperature:    72.000   97.000                             degC
       cpu0 temperature:    73.000                                      degC
       cpu1 temperature:    72.000                                      degC
          temperature 0:    72.000                                      degC
          temperature 1:    47.000                                      degC
          temperature 3:    69.000                                      degC
            fan speed 0:      3984                                       RPM

-- quote ends

… perhaps what is needed is to track the temperature changes and to set
customizable thresholds allowing time enough to call shutdown gracefully.

> I'm also confused on a detail: did elisp or the OS trigger a shutdown
> when a certain temperature was reached?

I'd say the OS. The kernel build config lets you set the wait interval
before actual poweroff IIRC. But I don't think that interval allows the
file system to sort itself out to avoid corrupting and breaking the machine.

--
© 2019 Van L
gpg using EEF2 37E9 3840 0D5D 9183  251E 9830 384E 9683 B835
"What's so strange when you know that you're a Wizard at 3?" -Joni Mitchell