aboutsummaryrefslogtreecommitdiff
path: root/xbattext.c
diff options
context:
space:
mode:
authorJohn Ankarstr\xf6m <john@ankarstrom.se>2021-06-01 21:52:23 +0200
committerJohn Ankarstr\xf6m <john@ankarstrom.se>2021-06-01 21:52:23 +0200
commit9d7c5bdf7bb04c368d1819224b6f6c1e6ba5d9ee (patch)
treead1156a5364a86c9857de888912343b08efe80b6 /xbattext.c
parentf7f8b4038409dda87fd840242d5949f26e8beb0b (diff)
downloadxbattext-9d7c5bdf7bb04c368d1819224b6f6c1e6ba5d9ee.tar.gz
Add custom 'alertFontList', 'alertForeground' resources
Diffstat (limited to 'xbattext.c')
-rw-r--r--xbattext.c68
1 files changed, 57 insertions, 11 deletions
diff --git a/xbattext.c b/xbattext.c
index efa7aa4..ec4a673 100644
--- a/xbattext.c
+++ b/xbattext.c
@@ -9,7 +9,15 @@
* LDFLAGS += -Wl,-R/usr/X11R7/lib -Wl,-R/usr/pkg/lib
*
* It requires x11/motif to be installed.
- * The fontList resource controls the font used.
+ *
+ * The program uses the following resources:
+ *
+ * xbattext*fontList = normal font
+ * xbattext*foreground = normal text color
+ * xbattext*alertFontList = font when battery is low
+ * xbattext*alertForeground = text color when battery is low
+ *
+ * Set their values in ~/.Xdefaults to customize xbattext.
*
* xbattext is written by John Ankarstr\xf6m <john@ankarstrom.se> and
* is released into the public domain; do whatever you want with it.
@@ -36,12 +44,31 @@
void update(XtPointer, XtIntervalId *);
+/* resources */
+struct _res {
+ XmFontList font_list;
+ XmFontList alert_font_list;
+ Pixel foreground;
+ Pixel alert_foreground;
+} res;
+static XtResource res_opts[] = {
+ {"fontList", "FontList", XmRFontList, sizeof(XmFontList),
+ XtOffset(struct _res*, font_list), XtRImmediate, (caddr_t)NULL},
+ {"foreground", "foreground", XmRPixel, sizeof(Pixel),
+ XtOffset(struct _res*, foreground), XtRImmediate, (caddr_t)NULL},
+ {"alertFontList", "AlertFontList", XmRFontList, sizeof(XmFontList),
+ XtOffset(struct _res*, alert_font_list), XtRImmediate, (caddr_t)NULL},
+ {"alertForeground", "AlertForeground", XmRPixel, sizeof(Pixel),
+ XtOffset(struct _res*, alert_foreground), XtRImmediate, (caddr_t)NULL},
+};
+
/* application state */
Arg wargs[10];
char *s;
int apmfd, alert;
struct apm_power_info info;
Widget toplevel, label;
+XmFontList font_list;
XmString str;
XtAppContext app_context;
XtIntervalId timer = 0;
@@ -65,6 +92,10 @@ main(int argc, char* argv[])
XtSetLanguageProc(NULL, NULL, NULL);
+ /* load application resources */
+ XtGetApplicationResources(toplevel,
+ &res, res_opts, XtNumber(res_opts), NULL, 0);
+
/* create motif label */
label = XtVaCreateManagedWidget("text",
xmLabelWidgetClass, toplevel,
@@ -81,6 +112,8 @@ main(int argc, char* argv[])
void
update(XtPointer client_data, XtIntervalId *t)
{
+ int i;
+
/* remove current timer */
if (t == NULL && timer) {
XtRemoveTimeOut(timer);
@@ -99,20 +132,33 @@ update(XtPointer client_data, XtIntervalId *t)
sprintf(s, "%d%%", info.battery_life);
str = XmStringCreate(s, XmFONTLIST_DEFAULT_TAG);
+ i = 0;
#ifdef ALERT
if (!alert && info.battery_life < ALERT) {
- XtVaSetValues(label,
- XmNlabelString, str,
- XtVaTypedArg, XtNforeground, XtRString, "red", 4,
- NULL);
alert = 1;
+ XtSetArg(wargs[i], XmNlabelString, str); i++;
+ if (res.alert_foreground != -1) {
+ XtSetArg(wargs[i], XtNforeground, res.alert_foreground);
+ i++;
+ }
+ if (res.alert_font_list != NULL) {
+ XtSetArg(wargs[i], XmNfontList, res.alert_font_list);
+ i++;
+ }
+ XtSetValues(label, wargs, i);
}
- else if (alert && info.battery_life > ALERT) {
- XtVaSetValues(label,
- XmNlabelString, str,
- XtVaTypedArg, XtNforeground, XtRString, "black", 6,
- NULL);
- alert = 0;
+ else if (alert && info.battery_life >= ALERT) {
+ alert = 1;
+ XtSetArg(wargs[i], XmNlabelString, str); i++;
+ if (res.foreground != -1) {
+ XtSetArg(wargs[i], XtNforeground, res.foreground);
+ i++;
+ }
+ if (res.font_list != NULL) {
+ XtSetArg(wargs[i], XmNfontList, res.font_list);
+ i++;
+ }
+ XtSetValues(label, wargs, i);
}
else
#endif