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.
32 #include <X11/Xutil.h>
33 #include <X11/extensions/shape.h>
39 #define WMACPI_VER "2.2rc1"
43 #include "master_low.xpm"
44 static char **master_xpm
= master_low_xpm
;
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 */
67 struct dockapp
*dockapp
;
68 /* global_t *globals; */
70 /* this gives us a variable scroll rate, depending on the importance of the
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
);
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);
136 static void new_window(char *display
, char *name
, int argc
, char **argv
)
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
,
160 if (!dockapp
->text
) {
161 pfatal("FATAL: Cannot create text scroll pixmap!\n");
165 /* force the window to stay this size - otherwise the user could
166 * resize us and see our panties^Wmaster pixmap . . . */
167 hints
= XAllocSizeHints();
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
);
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 */
203 if (!dockapp
->scroll
)
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
) {
230 dockapp
->scroll_reset
= 0;
254 copy_to_text_area(sx
, 0, w
, height
, dx
, y
);
261 static void render_text(char *string
)
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
)
271 if (strlen(string
) > 53)
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);
280 for (i
= 0; string
[i
]; i
++) {
281 c
= toupper(string
[i
]);
282 if (c
>= 'A' && c
<= 'Z') { /* letter */
284 copy_to_text_buffer(c
* 6, 67, 6, 7, k
, 0);
285 } else if (c
>= '0' && c
<= '9') { /* number */
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);
295 dockapp
->tw
= k
; /* length of text segment */
296 /* re-scroll the message */
301 static void display_percentage(int percent
)
304 static unsigned int obar
;
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 */
321 if (percent
< 100) { /* 0 - 99 */
322 copy_xpm_area(95, 48, 8, 7, 37, 17);
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);
327 copy_xpm_area(95, 37, 21, 9, 37, 16); /* 100% */
330 bar
= (int)((float)percent
/ ratio
);
335 copy_xpm_area(66, 0, bar
, 8, 5, 5);
337 copy_xpm_area(66 + bar
, 18, 54 - bar
, 8, bar
+ 5, 5);
341 static void display_time(int minutes
)
343 static int ohour
= -1, omin
= -1;
346 if (minutes
<= 0) { /* error - clear the display */
347 invalid_time_display();
352 /* render time on the display */
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. */
364 if (hour
== ohour
&& min
== omin
)
368 copy_xpm_area(tmp
* 7 + 1, 76, 6, 11, 7, 32);
370 copy_xpm_area(tmp
* 7 + 1, 76, 6, 11, 14, 32);
372 copy_xpm_area(tmp
* 7 + 1, 76, 6, 11, 25, 32);
374 copy_xpm_area(tmp
* 7 + 1, 76, 6, 11, 32, 32);
375 copy_xpm_area(71, 76, 3, 11, 21, 32);
381 * The reworked state handling stuff.
384 /* set the current state of the power panel */
391 static void really_blink_power_glyph(void)
393 static int counter
= 0;
396 display_power_glyph();
397 else if (counter
== 20)
399 else if (counter
> 30)
404 static void blink_power_glyph(void)
407 really_blink_power_glyph();
410 static void really_blink_battery_glyph(void)
412 static int counter
= 0;
415 display_battery_glyph();
416 else if (counter
== 20)
417 kill_battery_glyph();
418 else if (counter
> 30)
423 static void blink_battery_glyph(void)
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
) {
438 kill_battery_glyph();
439 display_power_glyph();
441 } else if (ap
->power
== BATT
) {
442 if (power
!= PS_BATT
) {
445 display_battery_glyph();
449 if (binfo
->charge_state
== CHARGE
)
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. */
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.
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
) {
512 reset_scroll_speed();
513 render_text("not present");
515 } else if (ap
->power
== AC
) {
516 if (binfo
->charge_state
== CHARGE
) {
519 reset_scroll_speed();
520 render_text("battery charging");
525 reset_scroll_speed();
526 render_text("on ac power");
530 if (binfo
->state
== CRIT
) {
534 render_text("critical low battery");
536 } else if (binfo
->state
== HARD_CRIT
) {
537 if (state
!= M_HCB
) {
540 render_text("hard critical low battery");
542 } else if (binfo
->state
== LOW
) {
546 render_text("low battery");
549 if (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
);
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;
588 pdebug("samples: %d\n", samples
);
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
);
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);
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
,
623 if(globals
->rtime
>= 0)
624 printf("; %d:%02d remaining", globals
->rtime
/60,
631 int main(int argc
, char **argv
)
633 char *display
= NULL
;
634 int sample_count
= 0;
635 int batt_reinit
, ac_reinit
;
638 int cli
= 0, samples
= 1, critical
= 10;
641 int sleep_time
= 1000000/sleep_rate
;
642 int scroll_count
= 0;
643 enum rtime_mode rt_mode
= RT_RATE
;
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
));
667 dockapp
->scroll_reset
= 0;
668 globals
->crit_level
= 10;
671 /* after this many samples, we reinit the battery and AC adapter
673 * XXX: make these configurable . . . */
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",
691 if (options
[3].used
) {
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");
704 globals
->crit_level
= critical
;
706 if (battery_no
>= MAXBATT
) {
707 fprintf(stderr
, "Please specify a battery number below %d\n", MAXBATT
);
710 pinfo("Monitoring battery %d\n", battery_no
);
712 if (power_init(globals
))
713 /* power_init functions handle printing error messages */
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
);
724 /* check for cli mode */
726 cli_wmacpi(globals
, samples
);
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
);
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
);
760 while (XPending(dockapp
->display
)) {
761 XNextEvent(dockapp
->display
, &event
);
762 switch (event
.type
) {
766 while (XCheckTypedEvent(dockapp
->display
, Expose
, &event
));
770 XCloseDisplay(dockapp
->display
);
776 /* cycle through the known batteries. */
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
);
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
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
);
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");
848 if (ac_count
++ >= ac_reinit
) {
849 if(reinit_ac_adapters(globals
))
850 pfatal("What happened to our AC adapters?!?\n");
856 if (scroll_count
++ >= scroll_reset
) {
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
);
878 /* redraw_window, if anything changed - determined inside