wmbattery: make it work without libapm
[dockapps.git] / wmbattery / wmbattery.c
blob1d45d65251f830f232f2447455030a87950481f3
1 /* wmbattery - display laptop battery info, dockable in WindowMaker
2 * Copyright (C) 1998-2014 Joey Hess <joey@kitenet.net>
3 * Copyright (C) 2014 Window Maker Developers Team
4 * <wmaker-dev@lists.windowmaker.org>
6 * Portions of code derived from:
7 * wmapm - Copyright (C) 1998-1999 Chris D. Faulhaber <jedgar@fxp.org>
8 * wmmon - Copyright (C) 1998 Martijn Pieterse <pieterse@xs4all.nl>
9 * Copyright (C) 1998 Antoine Nulle <warp@xs4all.nl>
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of version 2 of the GNU General Public License
13 * as published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include <stdio.h>
24 #include <unistd.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <X11/xpm.h>
28 #include <X11/extensions/shape.h>
29 #include <stdarg.h>
30 #include <signal.h>
31 #include <time.h>
32 #include <errno.h>
33 #include <fcntl.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include "wmbattery.h"
38 #ifdef HAVE_GETOPT_H
39 #include <getopt.h>
40 #endif
42 #include "mask.xbm"
43 #include "sonypi.h"
44 #include "acpi.h"
45 #ifdef HAL
46 #include "simplehal.h"
47 #endif
48 #ifdef UPOWER
49 #include "upower.h"
50 #endif
52 Pixmap images[NUM_IMAGES];
53 Window root, iconwin, win;
54 int screen;
55 XpmIcon icon;
56 Display *display;
57 GC NormalGC;
58 int pos[2] = {0, 0};
60 #ifdef HAVE__DEV_APM
61 #define APM_STATUS_FILE "/dev/apm"
62 #else
63 #define APM_STATUS_FILE "/proc/apm"
64 #endif
66 char *apm_status_file = APM_STATUS_FILE;
68 char *crit_audio_fn = NULL;
69 char *crit_audio;
70 int crit_audio_size;
71 char *crit_command = NULL;
73 int battnum = 1;
74 #ifdef HAL
75 int use_simplehal = 0;
76 #endif
77 #ifdef UPOWER
78 int use_upower = 0;
79 #endif
80 int use_sonypi = 0;
81 int use_acpi = 0;
82 int delay = 0;
83 int always_estimate_remaining = 0;
84 int granularity_estimate_remaining = 1;
85 int initial_state = WithdrawnState;
87 signed int low_pct = -1;
88 signed int critical_pct = -1;
90 void error(const char *fmt, ...)
92 va_list arglist;
94 va_start(arglist, fmt);
95 fprintf(stderr, "Error: ");
96 vfprintf(stderr, fmt, arglist);
97 fprintf(stderr, "\n");
98 va_end(arglist);
100 exit(1);
103 #if defined (HAVE_MACHINE_APM_BIOS_H) || defined (HAVE_I386_APMVAR_H) /* BSD */
104 int apm_read(apm_info *i)
106 int fd;
107 #ifdef HAVE_MACHINE_APM_BIOS_H /* FreeBSD */
108 unsigned long request = APMIO_GETINFO;
109 struct apm_info info;
110 #else /* NetBSD or OpenBSD */
111 unsigned long request= APM_IOC_GETPOWER;
112 struct apm_power_info info;
113 #endif
115 if ((fd = open(apm_status_file, O_RDONLY)) == -1) {
116 return 0;
118 if (ioctl(fd, request, &info) == -1) {
119 return 0;
121 close(fd);
123 #ifdef HAVE_MACHINE_APM_BIOS_H /* FreeBSD */
124 i->ac_line_status = info.ai_acline;
125 i->battery_status = info.ai_batt_stat;
126 i->battery_flags = (info.ai_batt_stat == 3) ? 8: 0;
127 i->battery_percentage = info.ai_batt_life;
128 i->battery_time = info.ai_batt_time;
129 i->using_minutes = 0;
130 #else /* NetBSD or OpenBSD */
131 i->ac_line_status = info.ac_state;
132 i->battery_status = info.battery_state;
133 i->battery_flags = (info.battery_state == 3) ? 8: 0;
134 i->battery_percentage = info.battery_life;
135 i->battery_time = info.minutes_left;
136 i->using_minutes = 1;
137 #endif
139 return 1;
142 int apm_exists(void)
144 apm_info i;
146 if (access(apm_status_file, R_OK))
147 return 0;
148 return apm_read(&i);
150 #endif
151 #if !defined(HAVE_APM_H)
152 int apm_read(apm_info *i)
154 return -1;
156 int apm_exists(void)
158 return -1;
160 #endif
162 int apm_change(apm_info *cur)
164 static int ac_line_status = 0, battery_status = 0, battery_flags = 0,
165 battery_percentage = 0, battery_time = 0, using_minutes = 0;
167 int i = cur->ac_line_status == ac_line_status &&
168 cur->battery_status == battery_status &&
169 cur->battery_flags == battery_flags &&
170 cur->battery_percentage == battery_percentage &&
171 cur->battery_time == battery_time &&
172 cur->using_minutes == using_minutes;
174 ac_line_status = cur->ac_line_status;
175 battery_status = cur->battery_status;
176 battery_flags = cur->battery_flags;
177 battery_percentage = cur->battery_percentage;
178 battery_time = cur->battery_time;
179 using_minutes = cur->using_minutes;
181 return i;
184 /* Calculate battery estimate */
185 void estimate_timeleft(apm_info *cur_info)
187 /* Time of the last estimate */
188 static time_t estimate_time;
189 /* Estimated time left */
190 static time_t estimate;
191 /* Time when we last noticed a battery level change */
192 static time_t battery_change_time;
193 /* The previous estimation we had before the battery level changed */
194 static time_t prev_estimate;
195 /* Percentage at the last estimate */
196 static short percent;
197 /* Where we charging or discharging the last time we were called? */
198 static short was_charging = 1;
199 /* Have we made a guess lately? */
200 static short guessed_lately;
202 time_t t;
203 int interval;
204 short is_charging = cur_info->battery_flags & BATTERY_FLAGS_CHARGING;
206 errno = 0;
207 if (time(&t) == ((time_t)-1) && errno != 0)
208 goto estim_values;
210 if ((
211 /* AC is on and battery is not charging anymore or ... */
212 (cur_info->ac_line_status == AC_LINE_STATUS_ON) && !is_charging
213 ) ||
215 /* ... the charging state has changed */
216 is_charging ^ was_charging
217 )) {
218 /* Reset counters */
219 battery_change_time = t;
220 estimate = -1;
221 guessed_lately = 0;
222 estimate_time = t;
223 prev_estimate = 0;
224 goto estim_values;
227 /* No change: decrease estimate */
228 if ((percent - cur_info->battery_percentage)
229 / granularity_estimate_remaining == 0) {
230 estimate -= t - estimate_time;
231 estimate_time = t;
232 if (guessed_lately && estimate < 0)
233 estimate = 0;
234 goto estim_values;
237 /* The battery level changed: calculate estimate based
238 * on change speed and previous estimate */
239 guessed_lately = 1;
240 estimate_time = t;
241 interval = estimate_time - battery_change_time;
242 prev_estimate = estimate;
243 battery_change_time = estimate_time;
244 estimate = (is_charging
245 ? (cur_info->battery_percentage - 100)
246 : cur_info->battery_percentage)
247 * interval / (percent - cur_info->battery_percentage);
248 if (prev_estimate > 0)
249 estimate = (estimate * 2 + prev_estimate) / 3;
251 estim_values:
252 percent = cur_info->battery_percentage;
253 was_charging = is_charging;
254 cur_info->battery_time = estimate;
255 if (estimate < 0)
256 estimate = 0;
257 cur_info->using_minutes = 0;
260 /* Load up the images this program uses. */
261 void load_images(void)
263 int x;
264 char fn[128]; /* enough? */
266 for (x = 0; x < NUM_IMAGES; x++) {
267 sprintf(fn, "%s/%s.xpm", ICONDIR, image_info[x].filename);
268 if (XpmReadFileToPixmap(display, root, fn, &images[x], NULL, NULL)) {
269 /* Check in current direcotry for fallback. */
270 sprintf(fn, "%s.xpm", image_info[x].filename);
271 if (XpmReadFileToPixmap(display, root, fn, &images[x], NULL, NULL))
272 error("Failed to load %s\n", fn);
277 void load_audio(void)
279 int fd;
280 struct stat s;
282 crit_audio = NULL;
283 if (crit_audio_fn == NULL)
284 return;
285 fd = open(crit_audio_fn, 0);
286 if (fd == -1)
287 error("unable to open audio file");
288 if (fstat(fd, &s) == 0) {
289 crit_audio_size = s.st_size;
290 crit_audio = malloc(crit_audio_size);
291 /* XXX: make this more robust? (loop?) */
292 if (read(fd, crit_audio, crit_audio_size) != crit_audio_size) {
293 free(crit_audio);
294 crit_audio = NULL;
295 error("unable to read audio file");
298 close(fd);
301 /* string replacement function by Laird Shaw, in public domain
302 * http://creativeandcritical.net/str-replace-c */
303 char *replace_str(const char *str, const char *old, const char *new)
305 char *ret, *r;
306 const char *p, *q;
307 size_t oldlen = strlen(old);
308 size_t count, retlen, newlen = strlen(new);
310 if (oldlen != newlen) {
311 for (count = 0, p = str; (q = strstr(p, old)) != NULL; p = q + oldlen)
312 count++;
313 /* this is undefined if p - str > PTRDIFF_MAX */
314 retlen = p - str + strlen(p) + count * (newlen - oldlen);
315 } else
316 retlen = strlen(str);
318 ret = malloc(retlen + 1);
319 if (!ret)
320 return NULL;
322 for (r = ret, p = str; (q = strstr(p, old)) != NULL; p = q + oldlen) {
323 /* this is undefined if q - p > PTRDIFF_MAX */
324 ptrdiff_t l = q - p;
325 memcpy(r, p, l);
326 r += l;
327 memcpy(r, new, newlen);
328 r += newlen;
330 strcpy(r, p);
332 return ret;
335 void cmd_crit(const char *cmd, int percentage, int time)
337 char prc_str[255] = "";
338 char min_str[255] = "";
339 char sec_str[255] = "";
340 char *tmp_a = NULL;
341 char *tmp_b = NULL;
342 char *command = NULL;
343 int ret;
345 if (!cmd)
346 return;
347 if (percentage > 100 || percentage < 0)
348 return;
349 if (time > 65535 || time < 0)
350 return;
352 sprintf(prc_str, "%i", percentage);
353 sprintf(min_str, "%i", time / 60);
354 sprintf(sec_str, "%i", time % 60);
356 tmp_a = replace_str(cmd, STR_SUB_PERCENT, prc_str);
357 if (!tmp_a)
358 return;
359 tmp_b = replace_str(tmp_a, STR_SUB_MINUTES, min_str);
360 if (!tmp_b)
361 return;
362 command = replace_str(tmp_b, STR_SUB_SECONDS, sec_str);
363 if (!command)
364 return;
366 ret = system(command);
367 if (ret == -1)
368 error("unable to run command: %s", command);
370 free(tmp_a);
371 free(tmp_b);
372 free(command);
375 /* Returns the display to run on (or NULL for default). */
376 char *parse_commandline(int argc, char *argv[])
378 int c = 0;
379 char *ret = NULL;
380 char *s;
382 while (c != -1) {
383 c = getopt(argc, argv, "hd:g:if:b:w:c:l:es:a:x:v");
384 switch (c) {
385 case 'h':
386 printf("Usage: wmbattery [options]\n");
387 printf("\t-d <display>\tselects target display\n");
388 printf("\t-h\t\tdisplay this help\n");
389 printf("\t-g +x+y\t\tposition of the window\n");
390 printf("\t-i start\n");
391 printf("\t-b num\t\tnumber of battery to display\n");
392 printf("\t-w secs\t\tseconds between updates\n");
393 printf("\t-l percent\tlow percentage\n");
394 printf("\t-c percent\tcritical percentage\n");
395 printf("\t-e\t\tuse own time estimates\n");
396 printf("\t-s granularity\tignore fluctuations less than granularity%% (implies -e)\n");
397 printf("\t-a file\t\twhen critical send file to /dev/audio\n");
398 printf("\t-x command\twhen critical execute this command\n");
399 printf("\t-v\t\tdisplay version number\n");
400 exit(0);
401 break;
402 case 'd':
403 ret = strdup(optarg);
404 break;
405 case 'g':
406 s = strtok(optarg, "+");
407 if (s) {
408 pos[0] = atoi(s);
409 s = strtok(NULL, "+");
410 if (s)
411 pos[1] = atoi(s);
412 else
413 pos[0] = 0;
415 break;
416 case 'i':
417 initial_state = IconicState;
418 break;
419 case 'b':
420 battnum = atoi(optarg);
421 break;
422 case 'w':
423 delay = atoi(optarg);
424 break;
425 case 'l':
426 low_pct = atoi(optarg);
427 break;
428 case 'c':
429 critical_pct = atoi(optarg);
430 break;
431 case 'e':
432 always_estimate_remaining = 1;
433 break;
434 case 's':
435 always_estimate_remaining = 1;
436 granularity_estimate_remaining = atoi(optarg);
437 break;
438 case 'a':
439 crit_audio_fn = strdup(optarg);
440 break;
441 case 'x':
442 crit_command = strdup(optarg);
443 break;
444 case 'v':
445 printf("wmbattery "PACKAGE_VERSION"\n");
446 exit(0);
447 break;
451 return ret;
454 /* Sets up the window and icon and all the nasty X stuff. */
455 void make_window(char *display_name, int argc, char *argv[])
457 XClassHint classhint;
458 char *wname = argv[0];
459 XTextProperty name;
460 XGCValues gcv;
461 int dummy = 0, borderwidth = 1;
462 XSizeHints sizehints;
463 XWMHints wmhints;
464 Pixel back_pix, fore_pix;
465 Pixmap pixmask;
467 display = XOpenDisplay(display_name);
468 if (!display)
469 error("can't open display %s", XDisplayName(display_name));
471 screen = DefaultScreen(display);
472 root = RootWindow(display, screen);
474 /* Create window. */
475 sizehints.flags = USSize | USPosition;
476 sizehints.x = 0;
477 sizehints.y = 0;
478 XWMGeometry(display, screen, "", NULL, borderwidth,
479 &sizehints, &sizehints.x, &sizehints.y,
480 &sizehints.width, &sizehints.height, &dummy);
482 sizehints.width = 64;
483 sizehints.height = 64;
484 sizehints.x = pos[0];
485 sizehints.y = pos[1];
486 back_pix = WhitePixel(display, screen);
487 fore_pix = BlackPixel(display, screen);
488 win = XCreateSimpleWindow(display, root, sizehints.x, sizehints.y,
489 sizehints.width, sizehints.height,
490 borderwidth, fore_pix, back_pix);
491 iconwin = XCreateSimpleWindow(display, win, sizehints.x,
492 sizehints.y, sizehints.width,
493 sizehints.height, borderwidth,
494 fore_pix, back_pix);
496 /* Activate hints */
497 XSetWMNormalHints(display, win, &sizehints);
498 classhint.res_name = wname;
499 classhint.res_class = wname;
500 XSetClassHint(display, win, &classhint);
502 if (!XStringListToTextProperty(&wname, 1, &name))
503 error("Can't allocate window name.");
505 XSetWMName(display, win, &name);
507 /* Create GC for drawing */
508 gcv.foreground = fore_pix;
509 gcv.background = back_pix;
510 gcv.graphics_exposures = 0;
511 NormalGC = XCreateGC(display, root,
512 GCForeground | GCBackground | GCGraphicsExposures,
513 &gcv);
515 pixmask = XCreateBitmapFromData(display, win, mask_bits,
516 mask_width, mask_height);
517 XShapeCombineMask(display, win, ShapeBounding, 0, 0,
518 pixmask, ShapeSet);
519 XShapeCombineMask(display, iconwin, ShapeBounding, 0, 0,
520 pixmask, ShapeSet);
522 wmhints.initial_state = initial_state;
523 wmhints.icon_window = iconwin;
524 wmhints.icon_x = sizehints.x;
525 wmhints.icon_y = sizehints.y;
526 wmhints.window_group = win;
527 wmhints.flags = StateHint | IconWindowHint |
528 IconPositionHint | WindowGroupHint;
530 XSetWMHints(display, win, &wmhints);
531 XSetCommand(display, win, argv, argc);
533 XSelectInput(display, iconwin, ExposureMask);
534 XSelectInput(display, win, ExposureMask);
536 XMapWindow(display, win);
539 void flush_expose(Window w)
541 XEvent dummy;
543 while (XCheckTypedWindowEvent(display, w, Expose, &dummy))
547 void redraw_window(void)
549 XCopyArea(display, images[FACE], iconwin, NormalGC, 0, 0,
550 image_info[FACE].width, image_info[FACE].height, 0, 0);
551 flush_expose(iconwin);
552 XCopyArea(display, images[FACE], win, NormalGC, 0, 0,
553 image_info[FACE].width, image_info[FACE].height, 0, 0);
554 flush_expose(win);
558 * Display an image, using XCopyArea. Can display only part of an image,
559 * located anywhere.
561 void copy_image(int image, int xoffset, int yoffset,
562 int width, int height, int x, int y)
564 XCopyArea(display, images[image], images[FACE], NormalGC,
565 xoffset, yoffset, width, height, x, y);
569 * Display a letter in one of two fonts, at the specified x position.
570 * Note that 10 is passed for special characters `:' or `1' at the
571 * end of the font.
573 void draw_letter(int letter, int font, int x)
575 copy_image(font, image_info[font].charwidth * letter, 0,
576 image_info[font].charwidth, image_info[font].height,
577 x, image_info[font].y);
580 /* Display an image at its normal location. */
581 void draw_image(int image)
583 copy_image(image, 0, 0,
584 image_info[image].width, image_info[image].height,
585 image_info[image].x, image_info[image].y);
588 void recalc_window(apm_info cur_info)
590 int time_left, hour_left, min_left, digit, x;
591 static int blinked;
593 /* Display if it's plugged in. */
594 switch (cur_info.ac_line_status) {
595 case AC_LINE_STATUS_ON:
596 draw_image(PLUGGED);
597 break;
598 default:
599 draw_image(UNPLUGGED);
602 /* Display the appropriate color battery. */
603 switch (cur_info.battery_status) {
604 case BATTERY_STATUS_HIGH:
605 case BATTERY_STATUS_CHARGING:
606 draw_image(BATTERY_HIGH);
607 break;
608 case BATTERY_STATUS_LOW:
609 draw_image(BATTERY_LOW);
610 break;
611 case BATTERY_STATUS_CRITICAL: /* blinking red battery */
612 if (blinked)
613 draw_image(BATTERY_CRITICAL);
614 else
615 draw_image(BATTERY_BLINK);
616 blinked = !blinked;
617 break;
618 default:
619 draw_image(BATTERY_NONE);
622 /* Show if the battery is charging. */
623 if (cur_info.battery_flags & BATTERY_FLAGS_CHARGING)
624 draw_image(CHARGING);
625 else
626 draw_image(NOCHARGING);
629 * Display the percent left dial. This has the side effect of
630 * clearing the time left field.
632 x = DIAL_MULTIPLIER * cur_info.battery_percentage;
633 if (x >= 0) {
634 /* Start by displaying bright on the dial. */
635 copy_image(DIAL_BRIGHT, 0, 0,
636 x, image_info[DIAL_BRIGHT].height,
637 image_info[DIAL_BRIGHT].x,
638 image_info[DIAL_BRIGHT].y);
640 /* Now display dim on the remainder of the dial. */
641 copy_image(DIAL_DIM, x, 0,
642 image_info[DIAL_DIM].width - x,
643 image_info[DIAL_DIM].height,
644 image_info[DIAL_DIM].x + x,
645 image_info[DIAL_DIM].y);
647 /* Show percent remaining */
648 if (cur_info.battery_percentage >= 0) {
649 digit = cur_info.battery_percentage / 10;
650 if (digit == 10) {
651 /* 11 is the `1' for the hundreds place. */
652 draw_letter(11, SMALLFONT, HUNDREDS_OFFSET);
653 digit = 0;
655 draw_letter(digit, SMALLFONT, TENS_OFFSET);
656 digit = cur_info.battery_percentage % 10;
657 draw_letter(digit, SMALLFONT, ONES_OFFSET);
658 } else {
659 /* There is no battery, so we need to dim out the
660 * percent sign that is normally bright. */
661 draw_letter(10, SMALLFONT, PERCENT_OFFSET);
664 /* Show time left */
666 /* A negative number means that it is unknown. Dim the field. */
667 if (cur_info.battery_time < 0) {
668 draw_letter(10, BIGFONT, COLON_OFFSET);
669 redraw_window();
670 return;
673 if (cur_info.using_minutes)
674 time_left = cur_info.battery_time;
675 else
676 time_left = cur_info.battery_time / 60;
677 hour_left = time_left / 60;
678 min_left = time_left % 60;
679 digit = hour_left / 10;
680 draw_letter(digit, BIGFONT, HOURS_TENS_OFFSET);
681 digit = hour_left % 10;
682 draw_letter(digit, BIGFONT, HOURS_ONES_OFFSET);
683 digit = min_left / 10;
684 draw_letter(digit, BIGFONT, MINUTES_TENS_OFFSET);
685 digit = min_left % 10;
686 draw_letter(digit, BIGFONT, MINUTES_ONES_OFFSET);
688 redraw_window();
691 void snd_crit(void)
693 int audio, n;
695 if (crit_audio) {
696 audio = open("/dev/audio", O_WRONLY);
697 if (audio >= 0) {
698 n = write(audio, crit_audio, crit_audio_size);
699 if (n != crit_audio_size)
700 fprintf(stderr, "write failed (%d/%d bytes)\n", n, crit_audio_size);
701 close(audio);
706 void alarmhandler(int sig)
708 apm_info cur_info;
709 int old_status;
711 #ifdef UPOWER
712 if (use_upower) {
713 if (upower_read(1, &cur_info) != 0)
714 error("Cannot read upower information.");
715 } else if (use_acpi) {
716 #else
717 if (use_acpi) {
718 #endif
719 if (acpi_read(battnum, &cur_info) != 0)
720 error("Cannot read ACPI information.");
722 #ifdef HAL
723 else if (use_simplehal) {
724 if (simplehal_read(battnum, &cur_info) != 0)
725 error("Cannot read HAL information.");
727 #endif
728 else if (!use_sonypi) {
729 if (apm_read(&cur_info) != 0)
730 error("Cannot read APM information.");
731 } else {
732 if (sonypi_read(&cur_info) != 0)
733 error("Cannot read sonypi information.");
736 old_status = cur_info.battery_status;
738 /* Always calculate remaining lifetime? apm and acpi both use a
739 * negative number here to indicate error, missing battery, or
740 * cannot determine time. */
741 if (always_estimate_remaining || cur_info.battery_time < 0)
742 estimate_timeleft(&cur_info);
744 /* Override the battery status? */
745 if ((low_pct > -1 || critical_pct > -1) &&
746 cur_info.ac_line_status != AC_LINE_STATUS_ON) {
747 if (cur_info.battery_percentage <= critical_pct)
748 cur_info.battery_status = BATTERY_STATUS_CRITICAL;
749 else if (cur_info.battery_percentage <= low_pct)
750 cur_info.battery_status = BATTERY_STATUS_LOW;
751 else
752 cur_info.battery_status = BATTERY_STATUS_HIGH;
755 /* If APM data changes redraw and wait for next update */
756 /* Always redraw if the status is critical, to make it blink. */
757 if (!apm_change(&cur_info) || cur_info.battery_status == BATTERY_STATUS_CRITICAL)
758 recalc_window(cur_info);
760 if ((old_status == BATTERY_STATUS_HIGH) &&
761 (cur_info.battery_status == BATTERY_STATUS_LOW)) {
762 snd_crit();
763 } else if (cur_info.battery_status == BATTERY_STATUS_CRITICAL) {
764 snd_crit();
765 cmd_crit(crit_command, cur_info.battery_percentage,
766 cur_info.battery_time);
769 alarm(delay);
772 void check_battery_num(int real, int requested)
774 if (requested > real || requested < 1) {
775 error("There %s only %i batter%s, and you asked for number %i.",
776 real == 1 ? "is" : "are",
777 real,
778 real == 1 ? "y" : "ies",
779 requested);
783 int main(int argc, char *argv[])
785 make_window(parse_commandline(argc, argv), argc, argv);
787 /* Check for APM support (returns 0 on success). */
788 if (apm_exists() == 0) {
789 if (!delay)
790 delay = 1;
792 #ifdef HAL
793 /* Check for hal support. */
794 else if (simplehal_supported()) {
795 use_simplehal = 1;
796 if (!delay)
797 delay = 2;
799 #endif
800 #ifdef UPOWER
801 else if (upower_supported())
802 use_upower = 1;
803 #endif
804 /* Check for ACPI support. */
805 else if (acpi_supported() && acpi_batt_count > 0) {
806 check_battery_num(acpi_batt_count, battnum);
807 use_acpi = 1;
808 if (!delay)
809 delay = 3; /* slow interface! */
810 } else if (sonypi_supported()) {
811 use_sonypi = 1;
812 low_pct = 10;
813 critical_pct = 5;
814 if (!delay)
815 delay = 1;
816 } else {
817 error("No APM, ACPI, UPOWER, HAL or SPIC support detected.");
820 load_images();
821 load_audio();
823 signal(SIGALRM, alarmhandler);
824 alarmhandler(SIGALRM);
826 while (1) {
827 XEvent ev;
828 XNextEvent(display, &ev);
829 if (ev.type == Expose)
830 redraw_window();