2014-09-20 04:23:18 +00:00
|
|
|
#include "config.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <X11/xpm.h>
|
|
|
|
#include <X11/extensions/shape.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#ifdef HAVE_GETOPT_H
|
|
|
|
#include <getopt.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "wmbattery.h"
|
|
|
|
#include "mask.xbm"
|
|
|
|
#include "sonypi.h"
|
|
|
|
#include "acpi.h"
|
|
|
|
#ifdef HAL
|
|
|
|
#include "simplehal.h"
|
|
|
|
#endif
|
|
|
|
#ifdef UPOWER
|
|
|
|
#include "upower.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
Pixmap images[NUM_IMAGES];
|
|
|
|
Window root, iconwin, win;
|
|
|
|
int screen;
|
|
|
|
XpmIcon icon;
|
|
|
|
Display *display;
|
|
|
|
GC NormalGC;
|
|
|
|
int pos[2] = {0, 0};
|
|
|
|
|
|
|
|
char *crit_audio_fn = NULL;
|
|
|
|
char *crit_audio;
|
|
|
|
int crit_audio_size;
|
2014-10-05 15:30:01 +00:00
|
|
|
char *crit_command = NULL;
|
2014-09-20 04:23:18 +00:00
|
|
|
|
|
|
|
int battnum = 1;
|
|
|
|
#ifdef HAL
|
|
|
|
int use_simplehal = 0;
|
|
|
|
#endif
|
|
|
|
#ifdef UPOWER
|
|
|
|
int use_upower = 0;
|
|
|
|
#endif
|
|
|
|
int use_sonypi = 0;
|
|
|
|
int use_acpi = 0;
|
|
|
|
int delay = 0;
|
|
|
|
int always_estimate_remaining = 0;
|
|
|
|
int granularity_estimate_remaining = 1;
|
|
|
|
int initial_state = WithdrawnState;
|
|
|
|
|
|
|
|
signed int low_pct = -1;
|
|
|
|
signed int critical_pct = -1;
|
|
|
|
|
2014-10-05 15:30:02 +00:00
|
|
|
void error(const char *fmt, ...)
|
|
|
|
{
|
2014-09-20 04:23:18 +00:00
|
|
|
va_list arglist;
|
2014-10-05 15:29:59 +00:00
|
|
|
|
2014-09-20 04:23:18 +00:00
|
|
|
va_start(arglist, fmt);
|
|
|
|
fprintf(stderr, "Error: ");
|
|
|
|
vfprintf(stderr, fmt, arglist);
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
va_end(arglist);
|
|
|
|
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2014-10-05 15:30:02 +00:00
|
|
|
int apm_change(apm_info *cur)
|
|
|
|
{
|
2014-09-20 04:23:18 +00:00
|
|
|
static int ac_line_status = 0, battery_status = 0, battery_flags = 0,
|
|
|
|
battery_percentage = 0, battery_time = 0, using_minutes = 0;
|
|
|
|
|
|
|
|
int i = cur->ac_line_status == ac_line_status &&
|
|
|
|
cur->battery_status == battery_status &&
|
|
|
|
cur->battery_flags == battery_flags &&
|
|
|
|
cur->battery_percentage == battery_percentage &&
|
|
|
|
cur->battery_time == battery_time &&
|
|
|
|
cur->using_minutes == using_minutes;
|
|
|
|
|
|
|
|
ac_line_status = cur->ac_line_status;
|
|
|
|
battery_status = cur->battery_status;
|
|
|
|
battery_flags = cur->battery_flags;
|
|
|
|
battery_percentage = cur->battery_percentage;
|
|
|
|
battery_time = cur->battery_time;
|
|
|
|
using_minutes = cur->using_minutes;
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Calculate battery estimate */
|
2014-10-05 15:30:02 +00:00
|
|
|
void estimate_timeleft(apm_info *cur_info)
|
|
|
|
{
|
2014-09-20 04:23:18 +00:00
|
|
|
/* Time of the last estimate */
|
2014-10-05 15:30:02 +00:00
|
|
|
static time_t estimate_time;
|
2014-09-20 04:23:18 +00:00
|
|
|
/* Estimated time left */
|
2014-10-05 15:30:02 +00:00
|
|
|
static time_t estimate;
|
2014-09-20 04:23:18 +00:00
|
|
|
/* Time when we last noticed a battery level change */
|
2014-10-05 15:30:02 +00:00
|
|
|
static time_t battery_change_time;
|
2014-09-20 04:23:18 +00:00
|
|
|
/* The previous estimation we had before the battery level changed */
|
2014-10-05 15:30:02 +00:00
|
|
|
static time_t prev_estimate;
|
2014-09-20 04:23:18 +00:00
|
|
|
/* Percentage at the last estimate */
|
2014-10-05 15:30:02 +00:00
|
|
|
static short percent;
|
2014-09-20 04:23:18 +00:00
|
|
|
/* Where we charging or discharging the last time we were called? */
|
|
|
|
static short was_charging = 1;
|
|
|
|
/* Have we made a guess lately? */
|
2014-10-05 15:30:02 +00:00
|
|
|
static short guessed_lately;
|
2014-09-20 04:23:18 +00:00
|
|
|
|
|
|
|
time_t t;
|
|
|
|
int interval;
|
|
|
|
short is_charging = cur_info->battery_flags & BATTERY_FLAGS_CHARGING;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
if (time(&t) == ((time_t)-1) && errno != 0)
|
|
|
|
goto estim_values;
|
|
|
|
|
|
|
|
if ((
|
2014-10-05 15:30:02 +00:00
|
|
|
/* AC is on and battery is not charging anymore or ... */
|
|
|
|
(cur_info->ac_line_status == AC_LINE_STATUS_ON) && !is_charging
|
|
|
|
) ||
|
2014-09-20 04:23:18 +00:00
|
|
|
(
|
2014-10-05 15:30:02 +00:00
|
|
|
/* ... the charging state has changed */
|
|
|
|
is_charging ^ was_charging
|
|
|
|
)) {
|
2014-09-20 04:23:18 +00:00
|
|
|
/* Reset counters */
|
|
|
|
battery_change_time = t;
|
|
|
|
estimate = -1;
|
|
|
|
guessed_lately = 0;
|
|
|
|
estimate_time = t;
|
|
|
|
prev_estimate = 0;
|
|
|
|
goto estim_values;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No change: decrease estimate */
|
|
|
|
if ((percent - cur_info->battery_percentage)
|
|
|
|
/ granularity_estimate_remaining == 0) {
|
|
|
|
estimate -= t - estimate_time;
|
|
|
|
estimate_time = t;
|
|
|
|
if (guessed_lately && estimate < 0)
|
|
|
|
estimate = 0;
|
|
|
|
goto estim_values;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The battery level changed: calculate estimate based
|
|
|
|
* on change speed and previous estimate */
|
|
|
|
guessed_lately = 1;
|
|
|
|
estimate_time = t;
|
|
|
|
interval = estimate_time - battery_change_time;
|
|
|
|
prev_estimate = estimate;
|
|
|
|
battery_change_time = estimate_time;
|
|
|
|
estimate = (is_charging
|
|
|
|
? (cur_info->battery_percentage - 100)
|
|
|
|
: cur_info->battery_percentage)
|
|
|
|
* interval / (percent - cur_info->battery_percentage);
|
|
|
|
if (prev_estimate > 0)
|
|
|
|
estimate = (estimate * 2 + prev_estimate) / 3;
|
|
|
|
|
|
|
|
estim_values:
|
|
|
|
percent = cur_info->battery_percentage;
|
|
|
|
was_charging = is_charging;
|
|
|
|
cur_info->battery_time = estimate;
|
|
|
|
if (estimate < 0)
|
|
|
|
estimate = 0;
|
|
|
|
cur_info->using_minutes = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Load up the images this program uses. */
|
2014-10-05 15:30:02 +00:00
|
|
|
void load_images(void)
|
|
|
|
{
|
|
|
|
int x;
|
2014-09-20 04:23:18 +00:00
|
|
|
char fn[128]; /* enough? */
|
|
|
|
|
2014-10-05 15:30:02 +00:00
|
|
|
for (x = 0; x < NUM_IMAGES; x++) {
|
|
|
|
sprintf(fn, "%s/%s.xpm", ICONDIR, image_info[x].filename);
|
|
|
|
if (XpmReadFileToPixmap(display, root, fn, &images[x], NULL, NULL)) {
|
|
|
|
/* Check in current direcotry for fallback. */
|
|
|
|
sprintf(fn, "%s.xpm", image_info[x].filename);
|
|
|
|
if (XpmReadFileToPixmap(display, root, fn, &images[x], NULL, NULL))
|
|
|
|
error("Failed to load %s\n", fn);
|
2014-09-20 04:23:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-05 15:30:02 +00:00
|
|
|
void load_audio(void)
|
|
|
|
{
|
2014-09-20 04:23:18 +00:00
|
|
|
int fd;
|
|
|
|
struct stat s;
|
|
|
|
|
|
|
|
crit_audio = NULL;
|
2014-10-05 15:30:02 +00:00
|
|
|
if (crit_audio_fn == NULL)
|
2014-09-20 04:23:18 +00:00
|
|
|
return;
|
|
|
|
fd = open(crit_audio_fn, 0);
|
2014-10-05 15:30:02 +00:00
|
|
|
if (fd == -1)
|
2014-09-20 04:23:18 +00:00
|
|
|
error("unable to open audio file");
|
|
|
|
if (fstat(fd, &s) == 0) {
|
|
|
|
crit_audio_size = s.st_size;
|
|
|
|
crit_audio = malloc(crit_audio_size);
|
|
|
|
/* XXX: make this more robust? (loop?) */
|
|
|
|
if (read(fd, crit_audio, crit_audio_size) != crit_audio_size) {
|
|
|
|
free(crit_audio);
|
|
|
|
crit_audio = NULL;
|
|
|
|
error("unable to read audio file");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
2014-10-05 15:30:01 +00:00
|
|
|
/* string replacement function by Laird Shaw, in public domain
|
|
|
|
* http://creativeandcritical.net/str-replace-c */
|
|
|
|
char *replace_str(const char *str, const char *old, const char *new)
|
|
|
|
{
|
|
|
|
char *ret, *r;
|
|
|
|
const char *p, *q;
|
|
|
|
size_t oldlen = strlen(old);
|
|
|
|
size_t count, retlen, newlen = strlen(new);
|
|
|
|
|
|
|
|
if (oldlen != newlen) {
|
|
|
|
for (count = 0, p = str; (q = strstr(p, old)) != NULL; p = q + oldlen)
|
|
|
|
count++;
|
|
|
|
/* this is undefined if p - str > PTRDIFF_MAX */
|
|
|
|
retlen = p - str + strlen(p) + count * (newlen - oldlen);
|
|
|
|
} else
|
|
|
|
retlen = strlen(str);
|
|
|
|
|
2014-10-05 15:30:02 +00:00
|
|
|
ret = malloc(retlen + 1);
|
|
|
|
if (!ret)
|
2014-10-05 15:30:01 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
for (r = ret, p = str; (q = strstr(p, old)) != NULL; p = q + oldlen) {
|
|
|
|
/* this is undefined if q - p > PTRDIFF_MAX */
|
|
|
|
ptrdiff_t l = q - p;
|
|
|
|
memcpy(r, p, l);
|
|
|
|
r += l;
|
|
|
|
memcpy(r, new, newlen);
|
|
|
|
r += newlen;
|
|
|
|
}
|
|
|
|
strcpy(r, p);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-10-05 15:30:02 +00:00
|
|
|
void cmd_crit(const char *cmd, int percentage, int time)
|
|
|
|
{
|
2014-10-05 15:30:01 +00:00
|
|
|
char prc_str[255] = "";
|
|
|
|
char min_str[255] = "";
|
|
|
|
char sec_str[255] = "";
|
|
|
|
char *tmp_a = NULL;
|
|
|
|
char *tmp_b = NULL;
|
|
|
|
char *command = NULL;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!cmd)
|
|
|
|
return;
|
|
|
|
if (percentage > 100 || percentage < 0)
|
|
|
|
return;
|
|
|
|
if (time > 65535 || time < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
sprintf(prc_str, "%i", percentage);
|
|
|
|
sprintf(min_str, "%i", time / 60);
|
|
|
|
sprintf(sec_str, "%i", time % 60);
|
|
|
|
|
|
|
|
tmp_a = replace_str(cmd, STR_SUB_PERCENT, prc_str);
|
|
|
|
if (!tmp_a)
|
|
|
|
return;
|
|
|
|
tmp_b = replace_str(tmp_a, STR_SUB_MINUTES, min_str);
|
|
|
|
if (!tmp_b)
|
|
|
|
return;
|
|
|
|
command = replace_str(tmp_b, STR_SUB_SECONDS, sec_str);
|
|
|
|
if (!command)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ret = system(command);
|
|
|
|
if (ret == -1)
|
|
|
|
error("unable to run command: %s", command);
|
|
|
|
|
|
|
|
free(tmp_a);
|
|
|
|
free(tmp_b);
|
|
|
|
free(command);
|
|
|
|
}
|
|
|
|
|
2014-09-20 04:23:18 +00:00
|
|
|
/* Returns the display to run on (or NULL for default). */
|
2014-10-05 15:30:02 +00:00
|
|
|
char *parse_commandline(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
int c = 0;
|
|
|
|
char *ret = NULL;
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
while (c != -1) {
|
|
|
|
c = getopt(argc, argv, "hd:g:if:b:w:c:l:es:a:x:");
|
2014-09-20 04:23:18 +00:00
|
|
|
switch (c) {
|
2014-10-05 15:30:02 +00:00
|
|
|
case 'h':
|
2014-09-20 04:23:18 +00:00
|
|
|
printf("Usage: wmbattery [options]\n");
|
2014-10-05 15:30:02 +00:00
|
|
|
printf("\t-d <display>\tselects target display\n");
|
|
|
|
printf("\t-h\t\tdisplay this help\n");
|
|
|
|
printf("\t-g +x+y\t\tposition of the window\n");
|
|
|
|
printf("\t-i start\n");
|
2014-09-20 04:23:18 +00:00
|
|
|
printf("\t-b num\t\tnumber of battery to display\n");
|
|
|
|
printf("\t-w secs\t\tseconds between updates\n");
|
|
|
|
printf("\t-l percent\tlow percentage\n");
|
|
|
|
printf("\t-c percent\tcritical percentage\n");
|
|
|
|
printf("\t-e\t\tuse own time estimates\n");
|
|
|
|
printf("\t-s granularity\tignore fluctuations less than granularity%% (implies -e)\n");
|
|
|
|
printf("\t-a file\t\twhen critical send file to /dev/audio\n");
|
2014-10-05 15:30:01 +00:00
|
|
|
printf("\t-x command\twhen critical execute this command\n");
|
2014-10-05 15:30:02 +00:00
|
|
|
exit(0);
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
ret = strdup(optarg);
|
|
|
|
break;
|
|
|
|
case 'g':
|
2014-09-20 04:23:18 +00:00
|
|
|
s = strtok(optarg, "+");
|
|
|
|
if (s) {
|
2014-10-05 15:30:02 +00:00
|
|
|
pos[0] = atoi(s);
|
|
|
|
s = strtok(NULL, "+");
|
|
|
|
if (s)
|
|
|
|
pos[1] = atoi(s);
|
|
|
|
else
|
|
|
|
pos[0] = 0;
|
2014-09-20 04:23:18 +00:00
|
|
|
}
|
|
|
|
break;
|
2014-10-05 15:30:02 +00:00
|
|
|
case 'i':
|
2014-09-20 04:23:18 +00:00
|
|
|
initial_state = IconicState;
|
|
|
|
break;
|
2014-10-05 15:30:02 +00:00
|
|
|
case 'b':
|
2014-09-20 04:23:18 +00:00
|
|
|
battnum = atoi(optarg);
|
|
|
|
break;
|
2014-10-05 15:30:02 +00:00
|
|
|
case 'w':
|
2014-09-20 04:23:18 +00:00
|
|
|
delay = atoi(optarg);
|
|
|
|
break;
|
2014-10-05 15:30:02 +00:00
|
|
|
case 'l':
|
2014-09-20 04:23:18 +00:00
|
|
|
low_pct = atoi(optarg);
|
|
|
|
break;
|
2014-10-05 15:30:02 +00:00
|
|
|
case 'c':
|
2014-09-20 04:23:18 +00:00
|
|
|
critical_pct = atoi(optarg);
|
|
|
|
break;
|
2014-10-05 15:30:02 +00:00
|
|
|
case 'e':
|
2014-09-20 04:23:18 +00:00
|
|
|
always_estimate_remaining = 1;
|
|
|
|
break;
|
2014-10-05 15:30:02 +00:00
|
|
|
case 's':
|
2014-09-20 04:23:18 +00:00
|
|
|
always_estimate_remaining = 1;
|
|
|
|
granularity_estimate_remaining = atoi(optarg);
|
|
|
|
break;
|
2014-10-05 15:30:02 +00:00
|
|
|
case 'a':
|
2014-09-20 04:23:18 +00:00
|
|
|
crit_audio_fn = strdup(optarg);
|
|
|
|
break;
|
2014-10-05 15:30:02 +00:00
|
|
|
case 'x':
|
2014-10-05 15:30:01 +00:00
|
|
|
crit_command = strdup(optarg);
|
|
|
|
break;
|
2014-09-20 04:23:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Sets up the window and icon and all the nasty X stuff. */
|
2014-10-05 15:30:02 +00:00
|
|
|
void make_window(char *display_name, int argc, char *argv[])
|
|
|
|
{
|
2014-09-20 04:23:18 +00:00
|
|
|
XClassHint classhint;
|
|
|
|
char *wname = argv[0];
|
|
|
|
XTextProperty name;
|
|
|
|
XGCValues gcv;
|
2014-10-05 15:30:02 +00:00
|
|
|
int dummy = 0, borderwidth = 1;
|
2014-09-20 04:23:18 +00:00
|
|
|
XSizeHints sizehints;
|
|
|
|
XWMHints wmhints;
|
|
|
|
Pixel back_pix, fore_pix;
|
|
|
|
Pixmap pixmask;
|
|
|
|
|
2014-10-05 15:30:02 +00:00
|
|
|
display = XOpenDisplay(display_name);
|
|
|
|
if (!display)
|
|
|
|
error("can't open display %s", XDisplayName(display_name));
|
2014-09-20 04:23:18 +00:00
|
|
|
|
2014-10-05 15:30:02 +00:00
|
|
|
screen = DefaultScreen(display);
|
|
|
|
root = RootWindow(display, screen);
|
2014-09-20 04:23:18 +00:00
|
|
|
|
|
|
|
/* Create window. */
|
|
|
|
sizehints.flags = USSize | USPosition;
|
|
|
|
sizehints.x = 0;
|
|
|
|
sizehints.y = 0;
|
|
|
|
XWMGeometry(display, screen, "", NULL, borderwidth,
|
|
|
|
&sizehints, &sizehints.x, &sizehints.y,
|
|
|
|
&sizehints.width, &sizehints.height, &dummy);
|
|
|
|
|
|
|
|
sizehints.width = 64;
|
|
|
|
sizehints.height = 64;
|
|
|
|
sizehints.x = pos[0];
|
|
|
|
sizehints.y = pos[1];
|
|
|
|
back_pix = WhitePixel(display, screen);
|
|
|
|
fore_pix = BlackPixel(display, screen);
|
|
|
|
win = XCreateSimpleWindow(display, root, sizehints.x, sizehints.y,
|
|
|
|
sizehints.width, sizehints.height,
|
|
|
|
borderwidth, fore_pix, back_pix);
|
|
|
|
iconwin = XCreateSimpleWindow(display, win, sizehints.x,
|
|
|
|
sizehints.y, sizehints.width,
|
|
|
|
sizehints.height, borderwidth,
|
|
|
|
fore_pix, back_pix);
|
|
|
|
|
|
|
|
/* Activate hints */
|
|
|
|
XSetWMNormalHints(display, win, &sizehints);
|
|
|
|
classhint.res_name = wname;
|
|
|
|
classhint.res_class = wname;
|
|
|
|
XSetClassHint(display, win, &classhint);
|
2014-10-05 15:29:59 +00:00
|
|
|
|
2014-10-05 15:30:02 +00:00
|
|
|
if (!XStringListToTextProperty(&wname, 1, &name))
|
2014-09-20 04:23:18 +00:00
|
|
|
error("Can't allocate window name.");
|
|
|
|
|
|
|
|
XSetWMName(display, win, &name);
|
2014-10-05 15:29:59 +00:00
|
|
|
|
2014-09-20 04:23:18 +00:00
|
|
|
/* Create GC for drawing */
|
|
|
|
gcv.foreground = fore_pix;
|
|
|
|
gcv.background = back_pix;
|
|
|
|
gcv.graphics_exposures = 0;
|
2014-10-05 15:29:59 +00:00
|
|
|
NormalGC = XCreateGC(display, root,
|
2014-09-20 04:23:18 +00:00
|
|
|
GCForeground | GCBackground | GCGraphicsExposures,
|
|
|
|
&gcv);
|
|
|
|
|
|
|
|
pixmask = XCreateBitmapFromData(display, win, mask_bits,
|
2014-10-05 15:30:02 +00:00
|
|
|
mask_width, mask_height);
|
2014-09-20 04:23:18 +00:00
|
|
|
XShapeCombineMask(display, win, ShapeBounding, 0, 0,
|
|
|
|
pixmask, ShapeSet);
|
|
|
|
XShapeCombineMask(display, iconwin, ShapeBounding, 0, 0,
|
|
|
|
pixmask, ShapeSet);
|
2014-10-05 15:29:59 +00:00
|
|
|
|
2014-09-20 04:23:18 +00:00
|
|
|
wmhints.initial_state = initial_state;
|
|
|
|
wmhints.icon_window = iconwin;
|
|
|
|
wmhints.icon_x = sizehints.x;
|
|
|
|
wmhints.icon_y = sizehints.y;
|
|
|
|
wmhints.window_group = win;
|
2014-10-05 15:29:59 +00:00
|
|
|
wmhints.flags = StateHint | IconWindowHint |
|
2014-10-05 15:30:02 +00:00
|
|
|
IconPositionHint | WindowGroupHint;
|
2014-10-05 15:29:59 +00:00
|
|
|
|
2014-09-20 04:23:18 +00:00
|
|
|
XSetWMHints(display, win, &wmhints);
|
|
|
|
XSetCommand(display, win, argv, argc);
|
|
|
|
|
|
|
|
XSelectInput(display, iconwin, ExposureMask);
|
|
|
|
XSelectInput(display, win, ExposureMask);
|
|
|
|
|
|
|
|
XMapWindow(display, win);
|
|
|
|
}
|
|
|
|
|
2014-10-05 15:30:02 +00:00
|
|
|
void flush_expose(Window w)
|
|
|
|
{
|
2014-09-20 04:23:18 +00:00
|
|
|
XEvent dummy;
|
2014-10-05 15:29:59 +00:00
|
|
|
|
2014-10-05 15:30:02 +00:00
|
|
|
while (XCheckTypedWindowEvent(display, w, Expose, &dummy))
|
|
|
|
;
|
2014-09-20 04:23:18 +00:00
|
|
|
}
|
|
|
|
|
2014-10-05 15:30:02 +00:00
|
|
|
void redraw_window(void)
|
|
|
|
{
|
2014-09-20 04:23:18 +00:00
|
|
|
XCopyArea(display, images[FACE], iconwin, NormalGC, 0, 0,
|
2014-10-05 15:30:02 +00:00
|
|
|
image_info[FACE].width, image_info[FACE].height, 0, 0);
|
2014-09-20 04:23:18 +00:00
|
|
|
flush_expose(iconwin);
|
2014-10-05 15:29:59 +00:00
|
|
|
XCopyArea(display, images[FACE], win, NormalGC, 0, 0,
|
2014-10-05 15:30:02 +00:00
|
|
|
image_info[FACE].width, image_info[FACE].height, 0, 0);
|
2014-09-20 04:23:18 +00:00
|
|
|
flush_expose(win);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Display an image, using XCopyArea. Can display only part of an image,
|
|
|
|
* located anywhere.
|
|
|
|
*/
|
|
|
|
void copy_image(int image, int xoffset, int yoffset,
|
2014-10-05 15:30:02 +00:00
|
|
|
int width, int height, int x, int y)
|
|
|
|
{
|
2014-09-20 04:23:18 +00:00
|
|
|
XCopyArea(display, images[image], images[FACE], NormalGC,
|
2014-10-05 15:30:02 +00:00
|
|
|
xoffset, yoffset, width, height, x, y);
|
2014-09-20 04:23:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Display a letter in one of two fonts, at the specified x position.
|
2014-10-05 15:29:59 +00:00
|
|
|
* Note that 10 is passed for special characters `:' or `1' at the
|
|
|
|
* end of the font.
|
2014-09-20 04:23:18 +00:00
|
|
|
*/
|
2014-10-05 15:30:02 +00:00
|
|
|
void draw_letter(int letter, int font, int x)
|
|
|
|
{
|
2014-09-20 04:23:18 +00:00
|
|
|
copy_image(font, image_info[font].charwidth * letter, 0,
|
|
|
|
image_info[font].charwidth, image_info[font].height,
|
|
|
|
x, image_info[font].y);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Display an image at its normal location. */
|
2014-10-05 15:30:02 +00:00
|
|
|
void draw_image(int image)
|
|
|
|
{
|
|
|
|
copy_image(image, 0, 0,
|
2014-09-20 04:23:18 +00:00
|
|
|
image_info[image].width, image_info[image].height,
|
|
|
|
image_info[image].x, image_info[image].y);
|
|
|
|
}
|
|
|
|
|
2014-10-05 15:30:02 +00:00
|
|
|
void recalc_window(apm_info cur_info)
|
|
|
|
{
|
2014-09-20 04:23:18 +00:00
|
|
|
int time_left, hour_left, min_left, digit, x;
|
2014-10-05 15:30:02 +00:00
|
|
|
static int blinked;
|
2014-10-05 15:29:59 +00:00
|
|
|
|
2014-09-20 04:23:18 +00:00
|
|
|
/* Display if it's plugged in. */
|
2014-10-05 15:30:02 +00:00
|
|
|
switch (cur_info.ac_line_status) {
|
|
|
|
case AC_LINE_STATUS_ON:
|
2014-09-20 04:23:18 +00:00
|
|
|
draw_image(PLUGGED);
|
|
|
|
break;
|
2014-10-05 15:30:02 +00:00
|
|
|
default:
|
2014-09-20 04:23:18 +00:00
|
|
|
draw_image(UNPLUGGED);
|
|
|
|
}
|
2014-10-05 15:29:59 +00:00
|
|
|
|
2014-10-05 15:30:02 +00:00
|
|
|
/* Display the appropriate color battery. */
|
|
|
|
switch (cur_info.battery_status) {
|
|
|
|
case BATTERY_STATUS_HIGH:
|
|
|
|
case BATTERY_STATUS_CHARGING:
|
2014-09-20 04:23:18 +00:00
|
|
|
draw_image(BATTERY_HIGH);
|
|
|
|
break;
|
2014-10-05 15:30:02 +00:00
|
|
|
case BATTERY_STATUS_LOW:
|
2014-09-20 04:23:18 +00:00
|
|
|
draw_image(BATTERY_LOW);
|
|
|
|
break;
|
2014-10-05 15:30:02 +00:00
|
|
|
case BATTERY_STATUS_CRITICAL: /* blinking red battery */
|
2014-09-20 04:23:18 +00:00
|
|
|
if (blinked)
|
|
|
|
draw_image(BATTERY_CRITICAL);
|
|
|
|
else
|
|
|
|
draw_image(BATTERY_BLINK);
|
2014-10-05 15:30:02 +00:00
|
|
|
blinked = !blinked;
|
2014-09-20 04:23:18 +00:00
|
|
|
break;
|
2014-10-05 15:30:02 +00:00
|
|
|
default:
|
2014-09-20 04:23:18 +00:00
|
|
|
draw_image(BATTERY_NONE);
|
2014-10-05 15:30:02 +00:00
|
|
|
}
|
2014-09-20 04:23:18 +00:00
|
|
|
|
2014-10-05 15:30:02 +00:00
|
|
|
/* Show if the battery is charging. */
|
|
|
|
if (cur_info.battery_flags & BATTERY_FLAGS_CHARGING)
|
2014-09-20 04:23:18 +00:00
|
|
|
draw_image(CHARGING);
|
2014-10-05 15:30:02 +00:00
|
|
|
else
|
2014-09-20 04:23:18 +00:00
|
|
|
draw_image(NOCHARGING);
|
2014-10-05 15:30:02 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Display the percent left dial. This has the side effect of
|
|
|
|
* clearing the time left field.
|
|
|
|
*/
|
|
|
|
x = DIAL_MULTIPLIER * cur_info.battery_percentage;
|
|
|
|
if (x >= 0) {
|
2014-09-20 04:23:18 +00:00
|
|
|
/* Start by displaying bright on the dial. */
|
|
|
|
copy_image(DIAL_BRIGHT, 0, 0,
|
|
|
|
x, image_info[DIAL_BRIGHT].height,
|
|
|
|
image_info[DIAL_BRIGHT].x,
|
|
|
|
image_info[DIAL_BRIGHT].y);
|
2014-10-05 15:30:02 +00:00
|
|
|
}
|
|
|
|
/* Now display dim on the remainder of the dial. */
|
|
|
|
copy_image(DIAL_DIM, x, 0,
|
2014-09-20 04:23:18 +00:00
|
|
|
image_info[DIAL_DIM].width - x,
|
|
|
|
image_info[DIAL_DIM].height,
|
|
|
|
image_info[DIAL_DIM].x + x,
|
|
|
|
image_info[DIAL_DIM].y);
|
2014-10-05 15:29:59 +00:00
|
|
|
|
2014-09-20 04:23:18 +00:00
|
|
|
/* Show percent remaining */
|
2014-10-05 15:30:02 +00:00
|
|
|
if (cur_info.battery_percentage >= 0) {
|
|
|
|
digit = cur_info.battery_percentage / 10;
|
|
|
|
if (digit == 10) {
|
|
|
|
/* 11 is the `1' for the hundreds place. */
|
|
|
|
draw_letter(11, SMALLFONT, HUNDREDS_OFFSET);
|
|
|
|
digit = 0;
|
2014-09-20 04:23:18 +00:00
|
|
|
}
|
2014-10-05 15:30:02 +00:00
|
|
|
draw_letter(digit, SMALLFONT, TENS_OFFSET);
|
2014-09-20 04:23:18 +00:00
|
|
|
digit = cur_info.battery_percentage % 10;
|
2014-10-05 15:30:02 +00:00
|
|
|
draw_letter(digit, SMALLFONT, ONES_OFFSET);
|
|
|
|
} else {
|
|
|
|
/* There is no battery, so we need to dim out the
|
2014-09-20 04:23:18 +00:00
|
|
|
* percent sign that is normally bright. */
|
2014-10-05 15:30:02 +00:00
|
|
|
draw_letter(10, SMALLFONT, PERCENT_OFFSET);
|
2014-09-20 04:23:18 +00:00
|
|
|
}
|
|
|
|
|
2014-10-05 15:30:02 +00:00
|
|
|
/* Show time left */
|
2014-09-20 04:23:18 +00:00
|
|
|
|
|
|
|
/* A negative number means that it is unknown. Dim the field. */
|
|
|
|
if (cur_info.battery_time < 0) {
|
|
|
|
draw_letter(10, BIGFONT, COLON_OFFSET);
|
|
|
|
redraw_window();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-05 15:30:02 +00:00
|
|
|
if (cur_info.using_minutes)
|
|
|
|
time_left = cur_info.battery_time;
|
|
|
|
else
|
|
|
|
time_left = cur_info.battery_time / 60;
|
|
|
|
hour_left = time_left / 60;
|
|
|
|
min_left = time_left % 60;
|
|
|
|
digit = hour_left / 10;
|
|
|
|
draw_letter(digit, BIGFONT, HOURS_TENS_OFFSET);
|
|
|
|
digit = hour_left % 10;
|
|
|
|
draw_letter(digit, BIGFONT, HOURS_ONES_OFFSET);
|
|
|
|
digit = min_left / 10;
|
|
|
|
draw_letter(digit, BIGFONT, MINUTES_TENS_OFFSET);
|
|
|
|
digit = min_left % 10;
|
|
|
|
draw_letter(digit, BIGFONT, MINUTES_ONES_OFFSET);
|
2014-10-05 15:29:59 +00:00
|
|
|
|
2014-09-20 04:23:18 +00:00
|
|
|
redraw_window();
|
|
|
|
}
|
|
|
|
|
2014-10-05 15:30:02 +00:00
|
|
|
void snd_crit(void)
|
|
|
|
{
|
2014-09-20 04:23:18 +00:00
|
|
|
int audio, n;
|
|
|
|
|
|
|
|
if (crit_audio) {
|
|
|
|
audio = open("/dev/audio", O_WRONLY);
|
|
|
|
if (audio >= 0) {
|
|
|
|
n = write(audio, crit_audio, crit_audio_size);
|
2014-10-05 15:30:02 +00:00
|
|
|
if (n != crit_audio_size)
|
2014-09-20 04:23:18 +00:00
|
|
|
fprintf(stderr, "write failed (%d/%d bytes)\n", n, crit_audio_size);
|
|
|
|
close(audio);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-05 15:30:02 +00:00
|
|
|
void alarmhandler(int sig)
|
|
|
|
{
|
2014-09-20 04:23:18 +00:00
|
|
|
apm_info cur_info;
|
|
|
|
int old_status;
|
|
|
|
|
|
|
|
#ifdef UPOWER
|
|
|
|
if (use_upower) {
|
|
|
|
if (upower_read(1, &cur_info) != 0)
|
|
|
|
error("Cannot read upower information.");
|
2014-10-05 15:30:02 +00:00
|
|
|
} else if (use_acpi) {
|
2014-09-20 04:23:18 +00:00
|
|
|
#else
|
|
|
|
if (use_acpi) {
|
|
|
|
#endif
|
|
|
|
if (acpi_read(battnum, &cur_info) != 0)
|
|
|
|
error("Cannot read ACPI information.");
|
|
|
|
}
|
|
|
|
#ifdef HAL
|
|
|
|
else if (use_simplehal) {
|
|
|
|
if (simplehal_read(battnum, &cur_info) != 0)
|
|
|
|
error("Cannot read HAL information.");
|
|
|
|
}
|
|
|
|
#endif
|
2014-10-05 15:30:02 +00:00
|
|
|
else if (!use_sonypi) {
|
2014-09-20 04:23:18 +00:00
|
|
|
if (apm_read(&cur_info) != 0)
|
|
|
|
error("Cannot read APM information.");
|
2014-10-05 15:30:02 +00:00
|
|
|
} else {
|
2014-09-20 04:23:18 +00:00
|
|
|
if (sonypi_read(&cur_info) != 0)
|
|
|
|
error("Cannot read sonypi information.");
|
|
|
|
}
|
2014-09-20 04:23:23 +00:00
|
|
|
|
|
|
|
old_status = cur_info.battery_status;
|
|
|
|
|
2014-09-20 04:23:18 +00:00
|
|
|
/* Always calculate remaining lifetime? apm and acpi both use a
|
|
|
|
* negative number here to indicate error, missing battery, or
|
|
|
|
* cannot determine time. */
|
|
|
|
if (always_estimate_remaining || cur_info.battery_time < 0)
|
|
|
|
estimate_timeleft(&cur_info);
|
2014-10-05 15:29:59 +00:00
|
|
|
|
2014-09-20 04:23:18 +00:00
|
|
|
/* Override the battery status? */
|
2014-10-05 15:29:59 +00:00
|
|
|
if ((low_pct > -1 || critical_pct > -1) &&
|
2014-09-20 04:23:18 +00:00
|
|
|
cur_info.ac_line_status != AC_LINE_STATUS_ON) {
|
|
|
|
if (cur_info.battery_percentage <= critical_pct)
|
|
|
|
cur_info.battery_status = BATTERY_STATUS_CRITICAL;
|
|
|
|
else if (cur_info.battery_percentage <= low_pct)
|
|
|
|
cur_info.battery_status = BATTERY_STATUS_LOW;
|
|
|
|
else
|
|
|
|
cur_info.battery_status = BATTERY_STATUS_HIGH;
|
|
|
|
}
|
2014-10-05 15:29:59 +00:00
|
|
|
|
2014-09-20 04:23:18 +00:00
|
|
|
/* If APM data changes redraw and wait for next update */
|
|
|
|
/* Always redraw if the status is critical, to make it blink. */
|
|
|
|
if (!apm_change(&cur_info) || cur_info.battery_status == BATTERY_STATUS_CRITICAL)
|
|
|
|
recalc_window(cur_info);
|
|
|
|
|
|
|
|
if ((old_status == BATTERY_STATUS_HIGH) &&
|
|
|
|
(cur_info.battery_status == BATTERY_STATUS_LOW)) {
|
|
|
|
snd_crit();
|
2014-10-05 15:30:02 +00:00
|
|
|
} else if (cur_info.battery_status == BATTERY_STATUS_CRITICAL) {
|
2014-09-20 04:23:18 +00:00
|
|
|
snd_crit();
|
2014-10-05 15:30:01 +00:00
|
|
|
cmd_crit(crit_command, cur_info.battery_percentage,
|
|
|
|
cur_info.battery_time);
|
2014-09-20 04:23:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
alarm(delay);
|
|
|
|
}
|
|
|
|
|
2014-10-05 15:30:02 +00:00
|
|
|
void check_battery_num(int real, int requested)
|
|
|
|
{
|
2014-09-20 04:23:18 +00:00
|
|
|
if (requested > real || requested < 1) {
|
|
|
|
error("There %s only %i batter%s, and you asked for number %i.",
|
2014-10-05 15:30:02 +00:00
|
|
|
real == 1 ? "is" : "are",
|
|
|
|
real,
|
|
|
|
real == 1 ? "y" : "ies",
|
|
|
|
requested);
|
2014-09-20 04:23:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-05 15:30:02 +00:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
make_window(parse_commandline(argc, argv), argc, argv);
|
2014-09-20 04:23:18 +00:00
|
|
|
|
|
|
|
/* Check for APM support (returns 0 on success). */
|
|
|
|
if (apm_exists() == 0) {
|
2014-10-05 15:30:02 +00:00
|
|
|
if (!delay)
|
2014-09-20 04:23:18 +00:00
|
|
|
delay = 1;
|
|
|
|
}
|
|
|
|
#ifdef HAL
|
|
|
|
/* Check for hal support. */
|
|
|
|
else if (simplehal_supported()) {
|
|
|
|
use_simplehal = 1;
|
2014-10-05 15:30:02 +00:00
|
|
|
if (!delay)
|
2014-09-20 04:23:18 +00:00
|
|
|
delay = 2;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef UPOWER
|
2014-10-05 15:30:02 +00:00
|
|
|
else if (upower_supported())
|
2014-09-20 04:23:18 +00:00
|
|
|
use_upower = 1;
|
|
|
|
#endif
|
|
|
|
/* Check for ACPI support. */
|
|
|
|
else if (acpi_supported() && acpi_batt_count > 0) {
|
|
|
|
check_battery_num(acpi_batt_count, battnum);
|
|
|
|
use_acpi = 1;
|
2014-10-05 15:30:02 +00:00
|
|
|
if (!delay)
|
2014-09-20 04:23:18 +00:00
|
|
|
delay = 3; /* slow interface! */
|
2014-10-05 15:30:02 +00:00
|
|
|
} else if (sonypi_supported()) {
|
2014-09-20 04:23:18 +00:00
|
|
|
use_sonypi = 1;
|
|
|
|
low_pct = 10;
|
|
|
|
critical_pct = 5;
|
2014-10-05 15:30:02 +00:00
|
|
|
if (!delay)
|
2014-09-20 04:23:18 +00:00
|
|
|
delay = 1;
|
2014-10-05 15:30:02 +00:00
|
|
|
} else {
|
2014-09-20 04:23:18 +00:00
|
|
|
error("No APM, ACPI, UPOWER, HAL or SPIC support detected.");
|
|
|
|
}
|
2014-10-05 15:29:59 +00:00
|
|
|
|
2014-09-20 04:23:18 +00:00
|
|
|
load_images();
|
2014-10-05 15:30:02 +00:00
|
|
|
load_audio();
|
2014-09-20 04:23:18 +00:00
|
|
|
|
|
|
|
signal(SIGALRM, alarmhandler);
|
|
|
|
alarmhandler(SIGALRM);
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
XEvent ev;
|
|
|
|
XNextEvent(display, &ev);
|
|
|
|
if (ev.type == Expose)
|
|
|
|
redraw_window();
|
|
|
|
}
|
|
|
|
}
|