aboutsummaryrefslogtreecommitdiff
path: root/xbattext.c
blob: aa139292bd110be291c369b948e7f9814f5243bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
 *  This is a simple X11 program that displays the battery percentage
 *  on NetBSD.
 *
 *  If you are curious about how it works, you can download the commented
 *  version of the source code at the following address:
 *
 *     http://img.ankarstrom.se/xbattext.pdf
 *
 *  xbattext should be compiled with the following flags:
 *
 *     CFLAGS = -I/usr/X11R7/include -I/usr/pkg/include
 *     LDFLAGS = -lXm -L/usr/X11R7/lib -L/usr/pkg/lib
 *     LDFLAGS += -Wl,-R/usr/X11R7/lib -Wl,-R/usr/pkg/lib
 *
 *  It requires x11/motif to be installed.
 *
 *  The follow resources are used:
 *
 *      xbattext*fontList		= normal font
 *      xbattext*foreground		= normal text color
 *      xbattext*alertFontList		= font when battery is low
 *      xbattext*alertForeground	= text color when battery is low
 *      xbattext*chargeFontList		= font when charging
 *      xbattext*chargeForeground	= text color when charging
 *
 *  Set their values in ~/.Xdefaults to customize the battery display.
 *
 *  xbattext is written by John Ankarstr\xf6m <john@ankarstrom.se> and
 *  is released into the public domain; do whatever you want with it.
 */

#include <err.h>
#include <fcntl.h>
#include <machine/apmvar.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <Xm/Label.h>

/* interval in seconds */
#define INTERVAL 5

/* low battery level */
#define ALERT 30

void update(XtPointer, XtIntervalId *);

/* resources */
struct res {
	XmFontList font_list;
	XmFontList alert_font_list;
	XmFontList charge_font_list;
	Pixel foreground;
	Pixel alert_foreground;
	Pixel charge_foreground;
} res;
static XtResource res_opts[] = {
	{"fontList", "FontList", XmRFontList, sizeof(XmFontList),
	XtOffset(struct res*, font_list), XtRImmediate, (caddr_t)NULL},
	{"alertFontList", "AlertFontList", XmRFontList, sizeof(XmFontList),
	XtOffset(struct res*, alert_font_list), XtRImmediate, (caddr_t)NULL},
	{"chargeFontList", "ChargeFontList", XmRFontList, sizeof(XmFontList),
	XtOffset(struct res*, charge_font_list), XtRImmediate, (caddr_t)NULL},
	{"foreground", "foreground", XmRPixel, sizeof(Pixel),
	XtOffset(struct res*, foreground), XtRImmediate, (caddr_t)NULL},
	{"alertForeground", "AlertForeground", XmRPixel, sizeof(Pixel),
	XtOffset(struct res*, alert_foreground), XtRImmediate, (caddr_t)NULL},
	{"chargeForeground", "ChargeForeground", XmRPixel, sizeof(Pixel),
	XtOffset(struct res*, charge_foreground), XtRImmediate, (caddr_t)NULL},
};

/* state changes */
enum {
	SET_NONE = 0,
	SET_NOALERT = 1 << 0,
	SET_NOCHARGE = 1 << 1,
	SET_ALERT = 1 << 2,
	SET_CHARGE = 1 << 3
} change;

/* application state */
Arg wargs[10];
char *s;
int apmfd, alerting, charging;
struct apm_power_info info;
Widget toplevel, label;
XmString xms;
XtAppContext app_context;

/* program start */
int
main(int argc, char* argv[])
{
	toplevel = XtVaAppInitialize(
		&app_context,
		"xbattext",
		NULL, 0,
		&argc, argv,
		NULL,
		NULL);

	if ((apmfd = open("/dev/apm", O_RDONLY)) == -1)
		err(1, "open");

	if ((s = malloc(5*sizeof(char))) == NULL)
		err(1, "malloc");

	/* load application resources */
	XtGetApplicationResources(toplevel,
		&res, res_opts, XtNumber(res_opts), NULL, 0);

	/* create motif label */
	label = XtVaCreateManagedWidget("label",
		xmLabelWidgetClass, toplevel,
		XmNlabel, "",
		NULL);
	alerting = 0;
	charging = 0;

	update(NULL, NULL);
	XtRealizeWidget(toplevel);
	XtAppMainLoop(app_context);
}

/* update battery status and (re-)add timer */
void
update(XtPointer client_data, XtIntervalId *t)
{
	int i;

	/* reset temporary variables */
	i = 0;
	change = SET_NONE;

	/* get battery info */
	memset(&info, 0, sizeof(info));
	if (ioctl(apmfd, APM_IOC_GETPOWER, &info) == -1) {
		fprintf(stderr, "ioctl APM_IOC_GETPOWER failed\n");
		sprintf(s, "?");
		goto end;
	}

	/* put battery status into label */
	sprintf(s, "%d%%", info.battery_life);
	xms = XmStringCreate(s, XmFONTLIST_DEFAULT_TAG);
	XtSetArg(wargs[i], XmNlabelString, xms); i++;

	/* check charging status */
	if (!charging && info.ac_state == APM_AC_ON)
		change |= SET_CHARGE;
	else if (charging && info.ac_state != APM_AC_ON)
		change |= SET_NOCHARGE;
	charging = info.ac_state == APM_AC_ON;

	/* check low battery */
	if (!alerting && info.battery_life < ALERT)
		change |= SET_ALERT;
	else if (alerting && info.battery_life >= ALERT)
		change |= SET_NOALERT;
	alerting = info.battery_life < ALERT;

	/* prioritize charging and low battery indications */
	if (change & SET_CHARGE) change = SET_CHARGE;
	if (change & SET_NOCHARGE && alerting) change = SET_ALERT;
	if (change & SET_NOALERT && charging) change = SET_CHARGE;

	/* act on state changes */
	switch(change & 0xf) {
	case SET_NOCHARGE:
	case SET_NOALERT:
		XtSetArg(wargs[i], XtNforeground, res.foreground);
		i++;
		if (res.font_list != NULL) {
			XtSetArg(wargs[i], XmNfontList, res.font_list);
			i++;
		}
		break;
	case SET_ALERT:
		XtSetArg(wargs[i], XtNforeground, res.alert_foreground);
		i++;
		if (res.alert_font_list != NULL) {
			XtSetArg(wargs[i], XmNfontList, res.alert_font_list);
			i++;
		}
		break;
	case SET_CHARGE:
		XtSetArg(wargs[i], XtNforeground, res.charge_foreground);
		i++;
		if (res.charge_font_list != NULL) {
			XtSetArg(wargs[i], XmNfontList, res.charge_font_list);
			i++;
		} else if (res.font_list != NULL) {
			XtSetArg(wargs[i], XmNfontList, res.font_list);
			i++;
		}
		break;
	}

set:	XtSetValues(label, wargs, i);
	XmStringFree(xms);

	/* add new timer */
end:	XtAppAddTimeOut(app_context, INTERVAL * 1000, update, NULL);
}