wmacpi: Bump to version 2.2rc1.
[dockapps.git] / wmacpi / wmacpi.c
blob241eeb57ac11eff7e1d19c0edc74e7402ea43183
1 /* apm/acpi dockapp - phear it 1.34
2 * Copyright (C) 2000, 2001, 2002 timecop@japan.co.jp
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 #define _GNU_SOURCE
21 #include <dockapp.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <ctype.h>
27 #include <unistd.h>
28 #include <time.h>
30 #include <X11/X.h>
31 #include <X11/Xlib.h>
32 #include <X11/Xutil.h>
33 #include <X11/extensions/shape.h>
34 #include <X11/xpm.h>
36 #include "libacpi.h"
37 #include "wmacpi.h"
39 #define WMACPI_VER "2.2rc1"
41 /* main pixmap */
42 #ifdef LOW_COLOR
43 #include "master_low.xpm"
44 static char **master_xpm = master_low_xpm;
45 #else
46 #include "master.xpm"
47 #endif
49 struct dockapp {
50 Display *display; /* display */
51 Window win; /* main window */
52 Pixmap pixmap; /* main pixmap */
53 Pixmap mask; /* mask pixmap */
54 Pixmap text; /* pixmap for text scroller */
55 unsigned short width; /* width of pixmap */
56 unsigned short height; /* height of pixmap */
57 int screen; /* current screen */
58 int tw; /* text width inside text pixmap */
59 int update; /* need to redraw? */
60 int blink; /* should we blink the LED? (critical battery) */
61 int bell; /* bell on critical low, or not? */
62 int scroll; /* scroll message text? */
63 int scroll_reset; /* reset the scrolling text */
66 /* globals */
67 struct dockapp *dockapp;
68 /* global_t *globals; */
70 /* this gives us a variable scroll rate, depending on the importance of the
71 * message . . . */
72 #define DEFAULT_SCROLL_RESET 150;
73 int scroll_reset = DEFAULT_SCROLL_RESET;
75 /* copy a chunk of pixmap around the app */
76 static void copy_xpm_area(int x, int y, int w, int h, int dx, int dy)
78 XCopyArea(DADisplay, dockapp->pixmap, dockapp->pixmap,
79 DAGC, x, y, w, h, dx, dy);
80 dockapp->update = 1;
83 /* display AC power symbol */
84 static void display_power_glyph(void)
86 copy_xpm_area(67, 38, 12, 7, 6, 17);
89 /* get rid of AC power symbol */
90 static void kill_power_glyph(void)
92 copy_xpm_area(67, 48, 12, 7, 6, 17);
95 /* display battery symbol */
96 static void display_battery_glyph(void)
98 copy_xpm_area(82, 38, 12, 7, 20, 17);
101 /* get rid of battery symbol */
102 static void kill_battery_glyph(void)
104 copy_xpm_area(82, 48, 12, 7, 20, 17);
107 /* clear the time display */
108 static void clear_time_display(void)
110 copy_xpm_area(114, 76, 31, 11, 7, 32);
113 /* set time display to -- -- */
114 static void invalid_time_display(void)
116 copy_xpm_area(122, 13, 31, 11, 7, 32);
119 static void reset_scroll(void) {
120 dockapp->scroll_reset = 1;
123 static void clear_text_area(void) {
124 copy_xpm_area(66, 9, 52, 7, 6, 50);
127 static void redraw_window(void)
129 if (dockapp->update) {
130 XCopyArea(dockapp->display, dockapp->pixmap, dockapp->win,
131 DAGC, 0, 0, 64, 64, 0, 0);
132 dockapp->update = 0;
136 static void new_window(char *display, char *name, int argc, char **argv)
138 XSizeHints *hints;
140 /* Initialise the dockapp window and appicon */
141 DAOpenDisplay(display, argc, argv);
142 DACreateIcon(name, 64, 64, argc, argv);
143 dockapp->display = DADisplay;
144 dockapp->win = DAWindow;
146 XSelectInput(dockapp->display, dockapp->win,
147 ExposureMask | ButtonPressMask | ButtonReleaseMask |
148 StructureNotifyMask);
150 /* create the main pixmap . . . */
151 DAMakePixmapFromData(master_xpm, &dockapp->pixmap, &dockapp->mask,
152 &dockapp->width, &dockapp->height);
153 DASetPixmap(dockapp->pixmap);
154 DASetShape(dockapp->mask);
156 /* text area is 318x7, or 53 characters long */
157 dockapp->text = XCreatePixmap(dockapp->display, dockapp->win, 318, 7,
158 DefaultDepth(dockapp->display,
159 dockapp->screen));
160 if (!dockapp->text) {
161 pfatal("FATAL: Cannot create text scroll pixmap!\n");
162 exit(1);
165 /* force the window to stay this size - otherwise the user could
166 * resize us and see our panties^Wmaster pixmap . . . */
167 hints = XAllocSizeHints();
168 if(hints) {
169 hints->flags |= PMinSize | PMaxSize;
170 hints->min_width = 64;
171 hints->max_width = 64;
172 hints->min_height = 64;
173 hints->max_height = 64;
174 XSetWMNormalHints(dockapp->display, dockapp->win, hints);
175 XFree(hints);
178 DAShow();
181 static void copy_to_text_buffer(int sx, int sy, int w, int h, int dx, int dy)
183 XCopyArea(dockapp->display, dockapp->pixmap, dockapp->text,
184 DAGC, sx, sy, w, h, dx, dy);
187 static void copy_to_text_area(int sx, int sy, int w, int h, int dx, int dy)
189 XCopyArea(dockapp->display, dockapp->text, dockapp->pixmap,
190 DAGC, sx, sy, w, h, dx, dy);
193 static void scroll_text(void)
195 static int start, end, stop;
196 int x = 6; /* x coord of the start of the text area */
197 int y = 50; /* y coord */
198 int width = 52; /* width of the text area */
199 int height = 7; /* height of the text area */
200 int tw = dockapp->tw; /* width of the rendered text */
201 int sx, dx, w;
203 if (!dockapp->scroll)
204 return;
207 * Conceptually this is viewing the text through a scrolling
208 * window - the window starts out with the end immediately before
209 * the text, and stops when the start of the window is immediately
210 * after the end of the text.
212 * We begin with the start of the window at pixel (0 - width) and
213 * as we scroll we render only the portion of the window above
214 * pixel 0. The destination of the copy during this period starts
215 * out at the end of the text area and works left as more of the
216 * text is being copied, until a full window is being copied.
218 * As the end of the window moves out past the end of the text, we
219 * want to keep the destination at the beginning of the text area,
220 * but copy a smaller and smaller chunk of the text. Eventually the
221 * start of the window will scroll past the end of the text, at
222 * which point we stop doing any work and wait to be reset.
225 if (dockapp->scroll_reset) {
226 start = 0 - width;
227 end = 0;
228 stop = 0;
229 clear_text_area();
230 dockapp->scroll_reset = 0;
233 if (stop)
234 return;
236 w = 52;
237 if (end < 52)
238 w = end;
239 else if (end > tw)
240 w = 52 - (end - tw);
242 dx = x + 52 - w;
243 if (end > tw)
244 dx = x;
246 sx = start;
247 if (start < 0)
248 sx = 0;
250 if (start > tw)
251 stop = 1;
253 clear_text_area();
254 copy_to_text_area(sx, 0, w, height, dx, y);
255 start += 2;
256 end += 2;
258 dockapp->update = 1;
261 static void render_text(char *string)
263 int i, c, k;
265 /* drop out immediately if scrolling is disabled - we don't render
266 * any text at all, since there's not much else we could do
267 * sensibly without scrolling. */
268 if (!dockapp->scroll)
269 return;
271 if (strlen(string) > 53)
272 return;
274 /* prepare the text area by clearing it */
275 for (i = 0; i < 54; i++) {
276 copy_to_text_buffer(133, 57, 6, 8, i * 6, 0);
278 k = 0;
280 for (i = 0; string[i]; i++) {
281 c = toupper(string[i]);
282 if (c >= 'A' && c <= 'Z') { /* letter */
283 c = c - 'A';
284 copy_to_text_buffer(c * 6, 67, 6, 7, k, 0);
285 } else if (c >= '0' && c <= '9') { /* number */
286 c = c - '0';
287 copy_to_text_buffer(c * 6 + 66, 58, 6, 7, k, 0);
288 } else if (c == '.') {
289 copy_to_text_buffer(140, 58, 6, 7, k, 0);
290 } else if (c == '-') {
291 copy_to_text_buffer(126, 58, 6, 7, k, 0);
293 k += 6;
295 dockapp->tw = k; /* length of text segment */
296 /* re-scroll the message */
297 reset_scroll();
298 scroll_text();
301 static void display_percentage(int percent)
303 static int op = -1;
304 static unsigned int obar;
305 unsigned int bar;
306 int width = 54; /* width of the bar */
307 float ratio = 100.0/width; /* ratio between the current percentage
308 * and the number of pixels in the bar */
310 if (percent == -1)
311 percent = 0;
313 if (op == percent)
314 return;
316 if (percent < 0)
317 percent = 0;
318 if (percent > 100)
319 percent = 100;
321 if (percent < 100) { /* 0 - 99 */
322 copy_xpm_area(95, 48, 8, 7, 37, 17);
323 if (percent >= 10)
324 copy_xpm_area((percent / 10) * 6 + 67, 28, 5, 7, 40, 17);
325 copy_xpm_area((percent % 10) * 6 + 67, 28, 5, 7, 46, 17);
326 } else
327 copy_xpm_area(95, 37, 21, 9, 37, 16); /* 100% */
328 op = percent;
330 bar = (int)((float)percent / ratio);
332 if (bar == obar)
333 return;
335 copy_xpm_area(66, 0, bar, 8, 5, 5);
336 if (bar < 54)
337 copy_xpm_area(66 + bar, 18, 54 - bar, 8, bar + 5, 5);
338 obar = bar;
341 static void display_time(int minutes)
343 static int ohour = -1, omin = -1;
344 int hour, min, tmp;
346 if (minutes <= 0) { /* error - clear the display */
347 invalid_time_display();
348 ohour = omin = -1;
349 return;
352 /* render time on the display */
353 hour = minutes / 60;
354 /* our display area only fits %2d:%2d, so we need to make sure
355 * what we're displaying will fit in those constraints. I don't
356 * think we're likely to see any batteries that do more than
357 * 100 hours any time soon, so it's fairly safe. */
358 if (hour >= 100) {
359 hour = 99;
360 min = 59;
361 } else
362 min = minutes % 60;
364 if (hour == ohour && min == omin)
365 return;
367 tmp = hour / 10;
368 copy_xpm_area(tmp * 7 + 1, 76, 6, 11, 7, 32);
369 tmp = hour % 10;
370 copy_xpm_area(tmp * 7 + 1, 76, 6, 11, 14, 32);
371 tmp = min / 10;
372 copy_xpm_area(tmp * 7 + 1, 76, 6, 11, 25, 32);
373 tmp = min % 10;
374 copy_xpm_area(tmp * 7 + 1, 76, 6, 11, 32, 32);
375 copy_xpm_area(71, 76, 3, 11, 21, 32);
376 ohour = hour;
377 omin = min;
381 * The reworked state handling stuff.
384 /* set the current state of the power panel */
385 enum panel_states {
386 PS_AC,
387 PS_BATT,
388 PS_NULL,
391 static void really_blink_power_glyph(void)
393 static int counter = 0;
395 if (counter == 10)
396 display_power_glyph();
397 else if (counter == 20)
398 kill_power_glyph();
399 else if (counter > 30)
400 counter = 0;
401 counter++;
404 static void blink_power_glyph(void)
406 if (dockapp->blink)
407 really_blink_power_glyph();
410 static void really_blink_battery_glyph(void)
412 static int counter = 0;
414 if (counter == 10)
415 display_battery_glyph();
416 else if (counter == 20)
417 kill_battery_glyph();
418 else if (counter > 30)
419 counter = 0;
420 counter++;
423 static void blink_battery_glyph(void)
425 if (dockapp->blink)
426 really_blink_battery_glyph();
429 static void set_power_panel(global_t *globals)
431 static enum panel_states power = PS_NULL;
432 battery_t *binfo = globals->binfo;
433 adapter_t *ap = &globals->adapter;
435 if (ap->power == AC) {
436 if (power != PS_AC) {
437 power = PS_AC;
438 kill_battery_glyph();
439 display_power_glyph();
441 } else if (ap->power == BATT) {
442 if (power != PS_BATT) {
443 power = PS_BATT;
444 kill_power_glyph();
445 display_battery_glyph();
449 if (binfo->charge_state == CHARGE)
450 blink_power_glyph();
452 if ((binfo->state == CRIT) && (ap->power == BATT))
453 blink_battery_glyph();
455 if (binfo->state == HARD_CRIT) {
456 really_blink_battery_glyph();
457 /* we only do this here because it'd be obnoxious to
458 * do it anywhere else. */
459 if (dockapp->bell) {
460 XBell(dockapp->display, 100);
465 void scroll_faster(double factor) {
466 scroll_reset = scroll_reset * factor;
469 void scroll_slower(double factor) {
470 scroll_reset = scroll_reset * factor;
473 void reset_scroll_speed(void) {
474 scroll_reset = DEFAULT_SCROLL_RESET;
478 * The message that needs to be displayed needs to be decided
479 * according to a heirarchy: a message like not present needs to take
480 * precedence over a global thing like the current power status, and
481 * something like a low battery warning should take precedence over
482 * the "on battery" message. Likewise, a battery charging message
483 * needs to take precedence over the on ac power message. The other
484 * question is how much of a precedence local messages should take
485 * over global ones . . .
487 * So, there are three possible sets of messages: not present, on-line
488 * and off-line messages. We need to decide which of those sets is
489 * appropriate right now, and then decide within them.
491 enum messages {
492 M_NP, /* not present */
493 M_AC, /* on ac power */
494 M_CH, /* battery charging */
495 M_BATT, /* on battery */
496 M_LB, /* low battery */
497 M_CB, /* critical low battery */
498 M_HCB, /* battery reported critical capacity state */
499 M_NULL, /* empty starting state */
502 static void set_message(global_t *globals)
504 static enum messages state = M_NULL;
505 battery_t *binfo = globals->binfo;
506 adapter_t *ap = &globals->adapter;
508 /* battery not present case */
509 if (!binfo->present) {
510 if (state != M_NP) {
511 state = M_NP;
512 reset_scroll_speed();
513 render_text("not present");
515 } else if (ap->power == AC) {
516 if (binfo->charge_state == CHARGE) {
517 if (state != M_CH) {
518 state = M_CH;
519 reset_scroll_speed();
520 render_text("battery charging");
522 } else {
523 if (state != M_AC) {
524 state = M_AC;
525 reset_scroll_speed();
526 render_text("on ac power");
529 } else {
530 if (binfo->state == CRIT) {
531 if (state != M_CB) {
532 state = M_CB;
533 scroll_faster(0.75);
534 render_text("critical low battery");
536 } else if (binfo->state == HARD_CRIT) {
537 if (state != M_HCB) {
538 state = M_HCB;
539 scroll_faster(0.5);
540 render_text("hard critical low battery");
542 } else if (binfo->state == LOW) {
543 if (state != M_LB) {
544 state = M_LB;
545 scroll_faster(0.85);
546 render_text("low battery");
548 } else {
549 if (state != M_BATT) {
550 state = M_BATT;
551 reset_scroll_speed();
552 render_text("on battery");
558 void set_time_display(global_t *globals)
560 if (globals->binfo->charge_state == CHARGE)
561 display_time(globals->binfo->charge_time);
562 else if (globals->binfo->charge_state == DISCHARGE)
563 display_time(globals->rtime);
564 else
565 invalid_time_display();
568 void set_batt_id_area(int bno)
570 int w = 7; /* Width of the number */
571 int h = 11; /* Height of the number */
572 int dx = 50; /* x coord of the target area */
573 int dy = 31; /* y coord of the target area */
574 int sx = (bno + 1) * 7; /* source x coord */
575 int sy = 76; /* source y coord */
577 copy_xpm_area(sx, sy, w, h, dx, dy);
580 #define VERSION "wmacpi version " WMACPI_VER "\nUsing libacpi version " LIBACPI_VER
582 void cli_wmacpi(global_t *globals, int samples)
584 int i, j, sleep_time = 0;
585 battery_t *binfo;
586 adapter_t *ap;
588 pdebug("samples: %d\n", samples);
589 if(samples > 1)
590 sleep_time = 1000000/samples;
592 /* we want to acquire samples over some period of time, so . . . */
593 for(i = 0; i < samples + 2; i++) {
594 for(j = 0; j < globals->battery_count; j++)
595 acquire_batt_info(globals, j);
596 acquire_global_info(globals);
597 usleep(sleep_time);
600 ap = &globals->adapter;
601 if(ap->power == AC) {
602 printf("On AC Power");
603 for(i = 0; i < globals->battery_count; i++) {
604 binfo = &batteries[i];
605 if(binfo->present && (binfo->charge_state == CHARGE)) {
606 printf("; Battery %s charging", binfo->name);
607 printf(", currently at %2d%%", binfo->percentage);
608 if(binfo->charge_time >= 0)
609 printf(", %2d:%02d remaining",
610 binfo->charge_time/60,
611 binfo->charge_time%60);
614 printf("\n");
615 } else if(ap->power == BATT) {
616 printf("On Battery");
617 for(i = 0; i < globals->battery_count; i++) {
618 binfo = &batteries[i];
619 if(binfo->present && (binfo->percentage >= 0))
620 printf(", Battery %s at %d%%", binfo->name,
621 binfo->percentage);
623 if(globals->rtime >= 0)
624 printf("; %d:%02d remaining", globals->rtime/60,
625 globals->rtime%60);
626 printf("\n");
628 return;
631 int main(int argc, char **argv)
633 char *display = NULL;
634 int sample_count = 0;
635 int batt_reinit, ac_reinit;
636 int batt_count = 0;
637 int ac_count = 0;
638 int cli = 0, samples = 1, critical = 10;
639 int samplerate = 20;
640 int sleep_rate = 10;
641 int sleep_time = 1000000/sleep_rate;
642 int scroll_count = 0;
643 enum rtime_mode rt_mode = RT_RATE;
644 int rt_forced = 0;
645 battery_t *binfo;
646 global_t *globals;
648 DAProgramOption options[] = {
649 {"-r", "--no-scroll", "disable scrolling message", DONone, False, {NULL}},
650 {"-n", "--no-blink", "disable blinking of various UI elements", DONone, False, {NULL}},
651 {"-x", "--cmdline", "run in command line mode", DONone, False, {NULL}},
652 {"-f", "--force-capacity-mode", "force the use of capacity mode for calculating time remaining", DONone, False, {NULL}},
653 {"-d", "--display", "display or remote display", DOString, False, {&display}},
654 {"-c", "--critical", "set critical low alarm at <number> percent\n (default: 10 percent)", DONatural, False, {&critical}},
655 {"-m", "--battery", "battery number to monitor", DONatural, False, {&battery_no}},
656 {"-s", "--sample-rate", "number of times per minute to sample battery information\n default 20 (once every three seconds)", DONatural, False, {&samplerate}},
657 {"-V", "--verbosity", "Set verbosity", DONatural, False, {&verbosity}},
658 {"-a", "--samples", "number of samples to average over (cli mode only)", DONatural, False, {&samples}},
661 dockapp = calloc(1, sizeof(struct dockapp));
662 globals = calloc(1, sizeof(global_t));
664 dockapp->blink = 1;
665 dockapp->bell = 0;
666 dockapp->scroll = 1;
667 dockapp->scroll_reset = 0;
668 globals->crit_level = 10;
669 battery_no = 1;
671 /* after this many samples, we reinit the battery and AC adapter
672 * information.
673 * XXX: make these configurable . . . */
674 batt_reinit = 100;
675 ac_reinit = 1000;
677 /* this needs to be up here because we need to know what batteries
678 * are available /before/ we can decide if the battery we want to
679 * monitor is available. */
680 /* parse command-line options */
681 DAParseArguments(argc, argv, options, 10,
682 "A battery monitor dockapp for ACPI based systems",
683 VERSION);
685 if (options[0].used)
686 dockapp->scroll = 0;
687 if (options[1].used)
688 dockapp->blink = 0;
689 if (options[2].used)
690 cli = 1;
691 if (options[3].used) {
692 rt_mode = RT_CAP;
693 rt_forced = 1;
696 if (samplerate == 0) samplerate = 1;
697 if (samplerate > 600) samplerate = 600;
699 if (critical > 100) {
700 fprintf(stderr, "Please use values between 0 and 100%%\n");
701 fprintf(stderr, "Using default value of 10%%\n");
702 critical = 10;
704 globals->crit_level = critical;
706 if (battery_no >= MAXBATT) {
707 fprintf(stderr, "Please specify a battery number below %d\n", MAXBATT);
708 return 1;
710 pinfo("Monitoring battery %d\n", battery_no);
712 if (power_init(globals))
713 /* power_init functions handle printing error messages */
714 exit(1);
716 globals->rt_mode = rt_mode;
717 globals->rt_forced = rt_forced;
719 if (battery_no > globals->battery_count) {
720 pfatal("Battery %d not available for monitoring.\n", battery_no);
721 exit(1);
724 /* check for cli mode */
725 if (cli) {
726 cli_wmacpi(globals, samples);
727 exit(0);
729 /* check to see if we've got a valid DISPLAY env variable, as a simple check to see if
730 * we're running under X */
731 if (!getenv("DISPLAY")) {
732 pdebug("Not running under X - using cli mode\n");
733 cli_wmacpi(globals, samples);
734 exit(0);
737 battery_no--;
739 /* make new dockapp window */
740 /* Don't even /think/ of asking me why, but if I set the window name to
741 * "acpi", the app refuses to dock properly - it's just plain /weird/.
742 * So, wmacpi it is . . . */
743 new_window(display, "wmacpi", argc, argv);
745 /* get initial statistics */
746 acquire_all_info(globals);
747 binfo = &batteries[battery_no];
748 globals->binfo = binfo;
749 pinfo("monitoring battery %s\n", binfo->name);
750 clear_time_display();
751 set_power_panel(globals);
752 set_message(globals);
753 set_batt_id_area(battery_no);
755 /* main loop */
756 while (1) {
757 Atom atom;
758 Atom wmdelwin;
759 XEvent event;
760 while (XPending(dockapp->display)) {
761 XNextEvent(dockapp->display, &event);
762 switch (event.type) {
763 case Expose:
764 /* update */
765 dockapp->update = 1;
766 while (XCheckTypedEvent(dockapp->display, Expose, &event));
767 redraw_window();
768 break;
769 case DestroyNotify:
770 XCloseDisplay(dockapp->display);
771 exit(0);
772 break;
773 case ButtonPress:
774 break;
775 case ButtonRelease:
776 /* cycle through the known batteries. */
777 battery_no++;
778 battery_no = battery_no % globals->battery_count;
779 globals->binfo = &batteries[battery_no];
780 binfo = globals->binfo;
781 pinfo("changing to monitor battery %s\n", binfo->name);
782 set_batt_id_area(battery_no);
783 dockapp->update = 1;
784 break;
785 case ClientMessage:
786 /* what /is/ this crap?
787 * Turns out that libdockapp adds the WM_DELETE_WINDOW atom to
788 * the WM_PROTOCOLS property for the window, which means that
789 * rather than get a simple DestroyNotify message, we get a
790 * nice little message from the WM saying "hey, can you delete
791 * yourself, pretty please?". So, when running as a window
792 * rather than an icon, we're impossible to kill in a friendly
793 * manner, because we're expecting to die from a DestroyNotify
794 * and thus blithely ignoring the WM knocking on our window
795 * border . . .
797 * This simply checks for that scenario - it may fail oddly if
798 * something else comes to us via a WM_PROTOCOLS ClientMessage
799 * event, but I suspect it's not going to be an issue. */
800 wmdelwin = XInternAtom(dockapp->display, "WM_DELETE_WINDOW", 1);
801 atom = event.xclient.data.l[0];
802 if (atom == wmdelwin) {
803 XCloseDisplay(dockapp->display);
804 exit(0);
806 break;
810 /* XXX: some laptops have problems with sampling the battery
811 * regularly - apparently, the BIOS disables interrupts while
812 * reading from the battery, which is generally on a slow bus
813 * and is a slow device, so you get significant periods without
814 * interrupts. This causes interactivity to suffer . . .
816 * My proposed workaround is to allow the user to set the sample
817 * rate - it defaults to ten, but can be set lower (or higher).
819 * The only problem with this is that we need to sample less
820 * frequently, while still allowing the app to update normally.
821 * That means calling redraw_window() and all the set_*() functions
822 * normally, but only calling acquire_all_info() every so often.
823 * As it stands, we only call acquire_all_info() once every three
824 * seconds (once every thirty updates) . . . I'm not entirely sure
825 * /how/ this could cause interactivity problems, but hey . . .
827 * So, given the base rate of once every three seconds, we want to
828 * change this test to . . . */
829 /* Okay, this needs /fixing/ - it's ridiculous. We should be giving
830 * the user the option of saying how many times per minute the
831 * battery should be sampled, defaulting to 20 times.
833 * We sleep for one tenth of a second at a time, so 60 seconds
834 * translates to 600 sleeps. So, we change the default sample
835 * rate to 20, and the calculation below becomes . . .*/
836 if (sample_count++ == ((sleep_rate*60)/samplerate)) {
837 acquire_all_info(globals);
839 /* we need to be able to reinitialise batteries and adapters, because
840 * they change - you can hotplug batteries on most laptops these days
841 * and who knows what kind of shit will be happening soon . . . */
842 if (batt_count++ >= batt_reinit) {
843 if(reinit_batteries(globals))
844 pfatal("Oh my god, the batteries are gone!\n");
845 batt_count = 0;
848 if (ac_count++ >= ac_reinit) {
849 if(reinit_ac_adapters(globals))
850 pfatal("What happened to our AC adapters?!?\n");
851 ac_count = 0;
853 sample_count = 0;
856 if (scroll_count++ >= scroll_reset) {
857 reset_scroll();
858 scroll_count = 0;
861 /* The old code had some kind of weird crap with timers and the like.
862 * As far as I can tell, it's meaningless - the time we want to display
863 * is the time calculated from the remaining capacity, as per the
864 * ACPI spec. The only thing I'd change is the handling of a charging
865 * state: my best guess, based on the behaviour I'm seeing with my
866 * Lifebook, is that the present rate value when charging is the rate
867 * at which the batteries are being charged, which would mean I'd just
868 * need to reverse the rtime calculation to be able to work out how
869 * much time remained until the batteries were fully charged . . .
870 * That would be rather useful, though given it would vary rather a lot
871 * it seems likely that it'd be little more than a rough guesstimate. */
872 set_time_display(globals);
873 set_power_panel(globals);
874 set_message(globals);
875 display_percentage(binfo->percentage);
876 scroll_text();
878 /* redraw_window, if anything changed - determined inside
879 * redraw_window. */
880 redraw_window();
882 usleep(sleep_time);
884 return 0;