Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.windows.x > #630

Re: a battery monitor

From Anton Antimo <anton@safunu.org>
Newsgroups comp.windows.x
Subject Re: a battery monitor
Date 2026-09-02 18:48 -0300
Organization A noiseless patient Spider
Message-ID <87tso7nx3k.fsf@safunu.org> (permalink)
References <87v78oop87.fsf@safunu.org>

Show all headers | View raw


Anton Antimo <anton@safunu.org> writes:

> I wrote a battery monitor.  The program watches its stdin for lines
> containing numbers that tell what is the current percentage of the
> battery.  You can see a picture of it on the lower right of the image at 
>
>   https://archive.org/details/x11-battery-monitor
>
> If you compile it, then how you can see a demonstration of it running
> (using bash):
>
>   for i in $(seq 100 -5 0); do echo $i && sleep 0.5 ; done | ./xbattery 
>
> So, yes, the program is more like a progress bar to be used by some kind
> of shell program.  Here's how I've been using it.  My system has this
> program called acpi, which gives me battery information:
>
> %acpi -b
> Battery 0: Charging, 91%, 00:18:43 until charged
>
> I run CWM, the calm window manager.  So from my ~/xinitrc, I run:
>
> while true; \
>   do acpi -b | awk '{printf("%d\n",$4)}' | tr -d '%,' && sleep 60; \
>   done | xbattery 220x40-0-0 &
>
> So, yes, xbattery.c (source code below) is able to read the desired
> window geometry from the command line (as it's typical of X programs).
>
> Be kind with criticism---that's my first one.
>
> #include <X11/Xlib.h>
> #include <X11/Xutil.h>
> #include <X11/keysym.h>
> #include <stdio.h>
> #include <stdlib.h>
> #include <unistd.h>
> #include <sys/select.h>
> #include <errno.h>
>
> void draw(Display *d, Window w, GC gc, int p, 
>                   unsigned int width, unsigned int height, 
>                   unsigned long green, unsigned long red) {
>   XClearWindow(d, w);
>   
>   if (p < 0) p = 0;
>   if (p > 100) p = 100;
>   
>   int howmuch;
>   if (p >= 100) 
>     howmuch = width; 
>   else 
>     howmuch = (width * p) / 100;
>   
>   if (howmuch > 0) {
>     if (p <= 10) XSetForeground(d, gc, red); else XSetForeground(d, gc, green);
>     XFillRectangle(d, w, gc, 0, 0, howmuch, width);
>   }
> }

Oops!  That XFillRectangle call should be 

  XFillRectangle(d, w, gc, 0, 0, howmuch, height);

Let me rewrite the entire procedure (while applying this fix).

void draw(Display *d, Window w, GC gc, int p,
                  unsigned int width, unsigned int height,
                  unsigned long green, unsigned long red) {

  int howmuch; XClearWindow(d, w);

  if (p < 0) p = 0; if (p > 100) p = 100;
  if (p >= 100) howmuch = width; else howmuch = (p * width)/100;
  if (p <= 10) XSetForeground(d, gc, red); else XSetForeground(d, gc, green);

  if (howmuch > 0)
    XFillRectangle(d, w, gc, 0, 0, howmuch, height);
}

Back to comp.windows.x | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

a battery monitor Anton Antimo <anton@safunu.org> - 2026-09-01 14:28 -0300
  Re: a battery monitor Kragen Javier Sitaker <kragen@canonical.org> - 2026-09-02 00:52 -0300
    Re: a battery monitor Anton Antimo <anton@safunu.org> - 2026-09-02 12:22 -0300
      Re: a battery monitor Anton Antimo <anton@safunu.org> - 2026-09-11 16:58 -0300
  Re: a battery monitor Lawrence D’Oliveiro <ldo@nz.invalid> - 2026-09-02 21:45 +0000
    Re: a battery monitor Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid> - 2026-09-03 12:34 +0800
      Re: a battery monitor Eli the Bearded <*@eli.users.panix.com> - 2026-09-03 05:30 +0000
        Re: a battery monitor Lawrence D’Oliveiro <ldo@nz.invalid> - 2026-09-03 21:24 +0000
          Re: a battery monitor Eli the Bearded <*@eli.users.panix.com> - 2026-09-04 02:51 +0000
    Re: a battery monitor David Brown <david.brown@hesbynett.no> - 2026-09-03 09:28 +0200
      Re: a battery monitor boltar@caprica.universe - 2026-09-03 15:55 +0000
        Re: a battery monitor David Brown <david.brown@hesbynett.no> - 2026-09-04 09:07 +0200
          Re: a battery monitor Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid> - 2026-09-04 15:14 +0800
          Re: a battery monitor Lane W <cactus_DAC@yahoo.com> - 2026-09-04 05:32 -0600
          Re: a battery monitor scott@slp53.sl.home (Scott Lurndal) - 2026-09-04 14:29 +0000
          Re: a battery monitor Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid> - 2026-09-04 23:17 +0800
            Re: a battery monitor boltar@caprica.universe - 2026-09-05 09:39 +0000
        Re: a battery monitor gazelle@shell.xmission.com (Kenny McCormack) - 2026-09-04 12:05 +0000
  Re: a battery monitor Anton Antimo <anton@safunu.org> - 2026-09-02 18:48 -0300

csiph-web