xbattbar-check-sysfs

Priskribo de la ĉi-paĝa enhavo

Fortha programo ebliganta al xbattbar labori per sysfs.

Etikedoj:

La celo de ĉi programo estas ebligi funkciadon de xbattbar per sysfs anstataŭ per APMACPI.

Mi verkis ĝin ĉar xbattbar ne funkciis sur Debian en Pandora ĉar Linukso ne estis pretigita por uzi APMACPI, kaj rekompili ĝin proe estus tre malfacila tasko pro la specialaĵoj de la komputilo Pandora.

Post esplorado mi eltrovis, ke la nova sistemo sysfs disponigas la informon de la baterio. En Pandora, ĝi situas en la jena dosierujo:

/sys/class/power_supply/bq27500-0/

Tiam mi jam ekkonis sufiĉe pri la funkciado de xbattbar, kiu uzas du programojn por legi la bateri-informon, depende de la sistemo elektita:

/usr/lib/xbattbar/xbattbar-check-acpi
/usr/lib/xbattbar/xbattbar-check-apm

Bonŝance la dua programo estas verkita en Perl kaj eblas kompreni kion ĝi faras, simple printi du datenojn en certa formato. Do sufiĉis verki alternativan programon kiu faru same el la informoj disponigitaj per sysfs kaj trompi xbattbar por uzi ĝin anstataŭ uzi la originalan. Mi verkis ĝin en Forth per Gforth.

Fontkodo

La programo longas pli o planite ĉar slurp-file (vorto de Gforth) kaj read-file (vorto de ANS Forth) ne funckias kun la dosieroj sub /sys/. Tial necesis verki alternativon, nome content, kiu uzas read-line.

#! /usr/bin/env gforth 

\ xbattbar-check-sysfs.fs
\ xbattbar-check-sysfs (version A-0020121104)

\ This program, 'xbattbar-check-sysfs', prints the status of the
\ Pandora's battery with the format required by xbattbar
\ (emulating the output of the Perl program
\ /usr/lib/xbattbar/xbattbar-check-acpi) 
\
\ Its goal is to make xbattbar to work without APM or ACPI,
\ getting the battery data from sysfs instead.

\ Copyright (C) 2012 Marcos Cruz (programandala.net)

\ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

\ 2012-11-04 First version.
\ 2012-12-04 Simpler usage intructions, without installation.

\ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
\ Usage as a parameter (the simple way)

\ Simply start xbattbar as usual, but add the -s parameter and
\ the path to this program:

\ xbattbar -s ~/WHATEVER/YOUR/PATH/IS/TO/xbattbar-check-sysfs.fs

\ Of course, consult the other parameters in the manual ('man
\ xbattbar').

\ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
\ Usage with installation (the hard way)

\ This way you don't need to change the way you call xbattbar,
\ but the installation may be overriden by an upgrade of
\ xbattbar.

\ As root user:

\ 1) cd /usr/lib/xbattbar/
\ 2) mv xbattbar-check-acpi xbattbar-check-acpi.ORIGINAL
\ 3) mv xbattbar-check-apm xbattbar-check-apm.ORIGINAL
\ 4) ln -s ~/WHATEVER/YOUR/PATH/IS/TO/xbattbar-check-sysfs.fs xbattbar-check-acpi
\ 5) ln -s ~/WHATEVER/YOUR/PATH/IS/TO/xbattbar-check-sysfs.fs xbattbar-check-apm

\ Remark: xbattbar uses APM by default unless you start it with
\ the "-c" option to use ACPI instead. Since both original
\ programs have been redirected to this new one, xbattbar will
\ work fine in both cases.

\ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

: sconstant  (  a1 u1 "name" -- )
  create  s,
  does>  ( pfa -- a2 u1 )  count
  ;

\ Directory that contains the Pandora's battery information:
s" /sys/class/power_supply/bq27500-0/" sconstant sys_dir

sys_dir s" status" s+ sconstant status_file
sys_dir s" capacity" s+ sconstant capacity_file

false [if]  \ use Gforth's 'slurp-file'

\ 'slurp-file' does not work with files under /sys/,
\ it causes the error "could not read whole file".

' slurp-file alias content

[else] \ alternative to 'slurp-file'

variable fid  \ file id
256 constant max_line
create line_buffer max_line 2 + allot

: open_file  ( a u -- )
  \ a u = file name
  r/o open-file throw fid !
  ;
: close_file  ( -- )
  fid @ close-file throw
  ;
: read_file  ( -- a u )
  \ Get the content of the open file (only its first line).
  \ a u = file content (first line)
  line_buffer dup max_line fid @ read-line throw and
  ;
: content  ( a1 u1 -- a2 u2 )
  \ Get file content; an alternative to 'slurp-file'.
  \ a1 u1 = file name
  \ a2 u2 = file content (first line)
  open_file read_file close_file
  ;

[then]

: .battery
  \ Print the capacity of the battery.
  s" battery=" type
  capacity_file content type cr
  ;
: .ac_line
  \ Print the status of the charger.
  s" ac_line=" type
  status_file content s" Charging" compare
  if  s" off"  else  s" on"  then  type cr
  ;

.battery .ac_line bye

Deŝutoj

Uzo

La programon eblas uzi dumaniere: kiel parametron de xbattbar aŭ kiel anstataŭon de la originalaj bateriinformaj programoj provizitaj de xbattbar. La unua tielo pli simplas.

Noto por linuksaj komencantoj: En la subaj programeroj, «~/VOJO/AL/» kompreneble estas ekzemplo kiu reprezentas la vojon al la programo, al la dosierujo kie ĝi estas; oni skribu la veran vojon.

Parametra uzo

Sufiĉas aldoni al la jam uzataj parametroj (konsultu la manlibron per man xbattbar) la parametron -s kun la kompleta vojo al la programo:

xbattbar -s ~/VOJO/AL/xbattbar-check-sysfs.fs

Instala uzo

Ĉi instalo anstataŭigas la du bateriinformajn programojn de xbattbar provizitajn, do ebligas nemodifi la xbattbar -vokon. Tamen ebla ĝistadigo de xbattbar nuligos la ŝanĝojn kaj devigos refari ĝin.

La programo xbattbar uzas APM -on krom se la parametro -c estas uzata, kiu devigas uzi ACPI -on. Anstataŭigi ambajn antaŭe cititajn programojn per ligiloj garantias ke xbattbar funkcios ambakaze.

Oni ordonu kiel administranto (nome root):

  1. cd /usr/lib/xbattbar/
  2. mv xbattbar-check-acpi xbattbar-check-acpi.ORIGINAL
  3. mv xbattbar-check-apm xbattbar-check-apm.ORIGINAL
  4. ln -s ~/VOJO/AL/xbattbar-check-sysfs.fs xbattbar-check-acpi
  5. ln -s ~/VOJO/AL/xbattbar-check-sysfs.fs xbattbar-check-apm

Rilataj paĝoj

Pandbatt
Fortho-programo montranta la staton de Pandora-baterio.