From 2a9bd912a16ebfee64dcabd0d134a876a027c74f Mon Sep 17 00:00:00 2001 From: Adrian Chadd Date: Sun, 15 May 2022 19:36:03 -0700 Subject: [PATCH] [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. :-) --- amiwm.1 | 6 ++++++ gram.y | 2 ++ menu.c | 11 +++++++---- module.c | 17 ++++++++++++++++- prefs.h | 1 + rc.c | 1 + 6 files changed, 33 insertions(+), 5 deletions(-) diff --git a/amiwm.1 b/amiwm.1 index fd092a8..6e90cf2 100644 --- a/amiwm.1 +++ b/amiwm.1 @@ -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 diff --git a/gram.y b/gram.y index fb7857f..76c821e 100644 --- a/gram.y +++ b/gram.y @@ -66,6 +66,7 @@ static int ti_level=0; %token INTERSCREENGAP AUTORAISE FOCUS FOLLOWMOUSE CLICKTOTYPE SLOPPY %token CUSTOMICONSONLY %token TITLEBARCLOCK TITLECLOCKFORMAT +%token BATTERYINFO %token OPAQUEMOVE OPAQUERESIZE SCREENMENU STYLE CLASS TITLE ICONTITLE ICON %token SHORTLABELICONS %token 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 ; diff --git a/menu.c b/menu.c index 0ec80de..f5dd35b 100644 --- a/menu.c +++ b/menu.c @@ -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, diff --git a/module.c b/module.c index 955e2f6..bb9dba7 100644 --- a/module.c +++ b/module.c @@ -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; diff --git a/prefs.h b/prefs.h index e89c96e..243331d 100644 --- a/prefs.h +++ b/prefs.h @@ -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; diff --git a/rc.c b/rc.c index ca24166..64846f6 100644 --- a/rc.c +++ b/rc.c @@ -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 },