aboutsummaryrefslogtreecommitdiff
path: root/xbattext.c
diff options
context:
space:
mode:
authorJohn Ankarstr\xf6m <john@ankarstrom.se>2021-06-03 18:34:32 +0200
committerJohn Ankarstr\xf6m <john@ankarstrom.se>2021-06-03 18:35:04 +0200
commit024eb2930059e11dcb1d1a299c5aa545c4cf9bbe (patch)
tree7eee67f789cb9d98416a63c4ac8801065582a938 /xbattext.c
parent6bbf3aeed8c70b3f268aa3975fbd29a89e449a2c (diff)
downloadxbattext-024eb2930059e11dcb1d1a299c5aa545c4cf9bbe.tar.gz
Fix state changing bugs, include screenshot in document
Diffstat (limited to 'xbattext.c')
-rw-r--r--xbattext.c89
1 files changed, 51 insertions, 38 deletions
diff --git a/xbattext.c b/xbattext.c
index 4f56225..36c093e 100644
--- a/xbattext.c
+++ b/xbattext.c
@@ -34,7 +34,7 @@
#include <Xm/Label.h>
/* interval in seconds */
-#define INTERVAL 10
+#define INTERVAL 5
/* low battery level */
#define ALERT 30
@@ -65,10 +65,19 @@ static XtResource res_opts[] = {
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, alert, charge;
+int apmfd, alerting, charging;
struct apm_power_info info;
Widget toplevel, label;
XmString xms;
@@ -100,8 +109,8 @@ main(int argc, char* argv[])
xmLabelWidgetClass, toplevel,
XmNlabel, "",
NULL);
- alert = 0;
- charge = 0;
+ alerting = 0;
+ charging = 0;
update(NULL, NULL);
XtRealizeWidget(toplevel);
@@ -114,6 +123,10 @@ 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) {
@@ -125,57 +138,57 @@ update(XtPointer client_data, XtIntervalId *t)
/* put battery status into label */
sprintf(s, "%d%%", info.battery_life);
xms = XmStringCreate(s, XmFONTLIST_DEFAULT_TAG);
-
- /* set color and font depending on battery status */
- i = 0;
XtSetArg(wargs[i], XmNlabelString, xms); i++;
- /* check if the ac adapter has been plugged in */
- if (!charge && info.ac_state == APM_AC_ON) {
- charge = 1;
- XtSetArg(wargs[i], XtNforeground, res.charge_foreground);
+ /* 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.charge_font_list != NULL) {
- XtSetArg(wargs[i], XmNfontList, res.charge_font_list);
- i++;
- } else if (res.font_list != NULL) {
+ if (res.font_list != NULL) {
XtSetArg(wargs[i], XmNfontList, res.font_list);
i++;
}
- goto set; /* skip further checks */
- }
-
- /* check low battery */
-#ifdef ALERT
- if (!alert && info.battery_life < ALERT) {
- alert = 1;
+ 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++;
}
- } else if (alert && info.battery_life >= ALERT) {
- alert = 0;
- XtSetArg(wargs[i], XtNforeground, res.foreground);
+ break;
+ case SET_CHARGE:
+ XtSetArg(wargs[i], XtNforeground, res.charge_foreground);
i++;
- if (res.font_list != NULL) {
- XtSetArg(wargs[i], XmNfontList, res.font_list);
+ if (res.charge_font_list != NULL) {
+ XtSetArg(wargs[i], XmNfontList, res.charge_font_list);
i++;
- }
- }
-#endif
-
- /* check if the ac adapter has been plugged out */
- if (charge && info.ac_state != APM_AC_ON) {
- charge = 0;
- if (i > 1) goto set; /* don't overwrite alert */
- XtSetArg(wargs[i], XtNforeground, res.foreground);
- i++;
- if (res.font_list != NULL) {
+ } else if (res.font_list != NULL) {
XtSetArg(wargs[i], XmNfontList, res.font_list);
i++;
}
+ break;
}
set: XtSetValues(label, wargs, i);