From 7ed46f531ccf25db7edb980cfb0910d6a63f38b9 Mon Sep 17 00:00:00 2001 From: Daniel Jones Date: Wed, 11 Dec 2019 23:59:00 +1030 Subject: added support for military, binary and 12 hour time also changed the text displayed for battery states as they were far too long my thinkpad claims the batteyr status is "unknown" when fully charged, so we interpret that as meaning fully charged... --- binstatus.c | 78 +++++++++--------------------------------------------------- components.c | 30 +++++++++++++++++++++-- components.h | 1 + config.h | 2 +- 4 files changed, 41 insertions(+), 70 deletions(-) diff --git a/binstatus.c b/binstatus.c index edce1ec..6211c07 100644 --- a/binstatus.c +++ b/binstatus.c @@ -19,71 +19,13 @@ #include #include #include - -#define MAXLENGTH 256 -int binary = 1; /* 0 = decimal display, 1 = binary display */ -int miltime = 0; /* 0 = 12 hour time, 1 = 24 hour time */ - -int -dectobin(int dec) -{ - if (dec == 0) return 0; - if (dec == 1) return 1; - return (dec % 2) + 10 * dectobin(dec / 2); -} - -void -gettime(int *store) -{ - time_t t = time(NULL); - struct tm *tm = localtime(&t); - store[0] = tm->tm_hour; - store[1] = tm->tm_min; -} - -void -formatstring(char *status, int *time) -{ - if (binary) - { - - snprintf(status, MAXLENGTH, "%05d %06d", dectobin(time[0]), - dectobin(time[1])); - } - else - { - snprintf(status, MAXLENGTH, "%02d:%02d", time[0], time[1]); - } -} - -int -converthour(int hour) -{ - return (hour - 12 < 0) ? 0 : hour - 12; -} +#include "components.h" +#include "config.h" int -main(int argc, char *argv[]) +main(void) { - for (int c = 1; c < argc; c++) - { - if (argc >= 2) - { - if (strcmp(argv[c], "-d") == 0) - binary = 0; - else if (strcmp(argv[c], "-m") == 0) - miltime = 1; - else - { - printf("usage: %s [-d] [-m]\n-d: decimal output\n" - "-m: 24-hour format\n", argv[0]); - exit(EXIT_SUCCESS); - } - } - } - int exitflag = EXIT_SUCCESS; - char status[MAXLENGTH]; - int time[2]; + char status[MAXLENGTH] = ""; Display *dsp = XOpenDisplay(NULL); if(!dsp) { @@ -92,14 +34,16 @@ main(int argc, char *argv[]) } while (1) { - gettime(time); - if (!miltime && time[0] > 12) - time[0] = converthour(time[0]); - formatstring(status, time); + memset(status, 0, sizeof(status)); + for (size_t i = 0; i < sizeof(components)/sizeof(components[0]); i++) + { + char store[20]; + strncat(status, components[i].function(store, sizeof(store), components[i].flag), sizeof(store)); + } XStoreName(dsp, DefaultRootWindow(dsp), status); XFlush(dsp); sleep(1); } XCloseDisplay(dsp); - return exitflag; + return 0; } diff --git a/components.c b/components.c index a8a27db..09c1fc0 100644 --- a/components.c +++ b/components.c @@ -14,6 +14,8 @@ */ #include "components.h" +#include "config.h" + int dectobin(int dec) { @@ -51,8 +53,22 @@ char *currenttime(char *store, size_t size, int flag) { int time[2]; gettime(time); - snprintf(store, size, "%02d:%02d ", time[0], time[1]); - return store; + if (flag & NORMALTIME) + { + if (time[0] > 12) + time[0] = converthour(time[0]); + } + if (flag & BINARYTIME) + { + snprintf(store, size, "%05d:%05d ", dectobin(time[0]), + dectobin(time[1])); + } + /* military time is the default, so we need not do anything */ + else + { + snprintf(store, size, "%02d:%02d ", time[0], time[1]); + } + return store; } char *battery(char *store, size_t size, int flag) @@ -83,7 +99,17 @@ char *charging(char *store, size_t size, int flag) } fgets(cap, 15, state); eat(cap, strlen(cap)); + /* my thinkpad says "Unknown" when charged ... */ + if (strcmp(cap, "Unknown") == 0) strcpy(cap, "⚡F"); + if (strcmp(cap, "Charged") == 0) strcpy(cap, "⚡F"); + if (strcmp(cap, "Charging") == 0) strcpy(cap, "⚡C"); + if (strcmp(cap, "Discharging") == 0) strcpy(cap, "⚡D"); fclose(state); snprintf(store, size, "%s ", cap); return store; } + +char *remainingtime(char *store, size_t size, int flag) +{ + return store; +} diff --git a/components.h b/components.h index bf3bc79..8af712e 100644 --- a/components.h +++ b/components.h @@ -25,5 +25,6 @@ char *currenttime(char *store, size_t size, int flag); char *battery(char *store, size_t size, int flag); char *charging(char *store, size_t size, int flag); +char *remainingtime(char *store, size_t size, int flag); #endif diff --git a/config.h b/config.h index bb0e59a..b006afd 100644 --- a/config.h +++ b/config.h @@ -35,7 +35,7 @@ struct component * add or remove components as desired * components are shown in the order of this array */ -struct component components[] ={ +static const struct component components[] ={ /* function flag */ {currenttime, NORMALTIME}, {battery, 0}, -- cgit v1.2.3