[amiwm] Add the basic battery info printing bits

* add a batteryinfo config option to enable whether or not to display
  the current battery info string

* populate that battery info string whenever we receive a battery
  update from the module

* document it all

This is very specific to a battery thing rather than a generic menu
widget thing, but I think I'll go and twiddle with this stuff a bit
more first and then work on a more generic "menu widget" thing later.

It'd be nice to have "menu bar things" like battery and the date/time
field on a linked list of "things".

There's also some bugs - notably, rendering artifacts when the
string length shrinks, and it only updating on the 'current' menu bar
(when you have multiple desktops visible at once.)  Those also
should be fixed for completeness. :-)
This commit is contained in:
Adrian Chadd
2022-05-15 19:36:03 -07:00
parent fc23b3e054
commit 2a9bd912a1
6 changed files with 33 additions and 5 deletions

View File

@@ -94,6 +94,12 @@ The time string is formatted with the standard strftime() parameters.
The default is "%c". It has been found that "%a %b %e %Y %l:%M %p" works
well too. Number is the update interval in seconds.
.SH BatteryInfo {yes|no}
This lets you display battery information on the menu bar.
It reqiures a module (such as Battery) to gather current battery status
and push it into amiwm to display.
.SH ToolItem \f1\*(lq\f3name" \f1\*(lq\f3command" \f1\*(lq\f3hotkey"
Adds an item in the Tools menu with the specified name, which executes

2
gram.y
View File

@@ -66,6 +66,7 @@ static int ti_level=0;
%token <num> INTERSCREENGAP AUTORAISE FOCUS FOLLOWMOUSE CLICKTOTYPE SLOPPY
%token <num> CUSTOMICONSONLY
%token <num> TITLEBARCLOCK TITLECLOCKFORMAT
%token <num> BATTERYINFO
%token <num> OPAQUEMOVE OPAQUERESIZE SCREENMENU STYLE CLASS TITLE ICONTITLE ICON
%token <num> SHORTLABELICONS
%token <ptr> STRING
@@ -118,6 +119,7 @@ stmt : error
prefs.titleclockinterval=$2;
prefs.titleclockformat=$3; }
| SCREENMENU truth { prefs.screenmenu=$2; }
| BATTERYINFO truth { prefs.battery_info = $2; }
| stylespec styleitems RIGHTBRACE
;

11
menu.c
View File

@@ -37,6 +37,8 @@ extern struct Library *XLibBase;
#define CHECKED 2
#define DISABLED 4
char battery_status[128];
extern Display *dpy;
extern Cursor wm_curs;
extern XContext screen_context, client_context;
@@ -303,7 +305,7 @@ void redraw_item(struct Item *i, Window w)
XSetForeground(dpy, scr->menubargc, scr->dri.dri_Pens[BARDETAILPEN]);
XSetBackground(dpy, scr->menubargc, scr->dri.dri_Pens[BARBLOCKPEN]);
}
if(i->text)
if(i->text) {
#ifdef USE_FONTSETS
XmbDrawImageString(dpy, w, scr->dri.dri_FontSet,
scr->menubargc, (i->flags&CHECKIT)?1+scr->checkmarkspace:1,
@@ -312,8 +314,9 @@ void redraw_item(struct Item *i, Window w)
XDrawImageString(dpy, w, scr->menubargc, (i->flags&CHECKIT)?1+scr->checkmarkspace:1,
scr->dri.dri_Ascent+1, i->text, i->textlen);
#endif
else
} else {
XFillRectangle(dpy, w, scr->menubargc, 2, 2, m->width-10, 2);
}
if(i->sub) {
int x=m->width-6-scr->hotkeyspace-1+8;
#ifdef USE_FONTSETS
@@ -547,11 +550,11 @@ void redrawmenubar(Window w)
/*
* Update the battery indicator if it's enabled.
*/
if (1) {
if (prefs.battery_info) {
char battery_buf[512];
int l;
sprintf(battery_buf, "| Battery |");
sprintf(battery_buf, "| %s |", battery_status);
#ifdef USE_FONTSETS
l = XmbTextEscapement(scr->dri.dri_FontSet, battery_buf, strlen(battery_buf));
XmbDrawImageString(dpy, w, scr->dri.dri_FontSet, scr->menubargc,

View File

@@ -493,19 +493,34 @@ static void handle_module_cmd(struct module *m, char *data, int data_len)
case MCMD_UPDATE_BATTERY:
{
struct mcmd_update_battery *batt;
extern char battery_status[];
if (data == NULL) {
reply_module(m, NULL, -1);
break;
}
if (data_len != sizeof(struct mcmd_update_battery)) {
reply_module(m, NULL, -1);
break;
}
batt = (void *) data;
#if 0
fprintf(stderr, "%s: called, BATTERY, pct=%d, time=%d, ac=%d\n",
__func__,
batt->battery_pct,
batt->battery_time,
batt->battery_ac);
#endif
/* XXX TODO: update the battery menu thingy here */
/*
* XXX TODO: for now we're just populating a string here.
* Later on we should just store the current battery state
* somewhere (a key/value table would be nice!) and then
* the widget code can pull out its needed state to render.
*/
snprintf(battery_status, 128, "%d pct%s", batt->battery_pct,
batt->battery_ac == 1 ? " A" : " -");
reply_module(m, NULL, 0);
break;

View File

@@ -17,6 +17,7 @@ extern struct prefs_struct {
char *titleclockformat; /* format to use for the clock */
int titleclockinterval; /* how often do we update the clock?*/
int icontray; // if true then icons will be shown in a tray on top of each screen (besides clock and screen name)
int battery_info; /* display battery info? */
struct _Style *firststyle, *laststyle;
} prefs;

1
rc.c
View File

@@ -105,6 +105,7 @@ struct keyword { char *name; int token; } keywords[] = {
{ "barblockpen", T_BARBLOCKPEN },
{ "bardetailpen", T_BARDETAILPEN },
{ "bartrimpen", T_BARTRIMPEN },
{ "batteryinfo", BATTERYINFO },
{ "blockpen", T_BLOCKPEN },
{ "both", BOTH },
{ "bottom", BOTTOM },