--- xbattbar.c.upstream	Sat Jan 12 01:11:08 2002
+++ xbattbar.c	Sat Jan 12 02:57:41 2002
@@ -35,7 +35,8 @@
 #include <X11/Xlib.h>
 
 #define PollingInterval 10	/* APM polling interval in sec */
-#define BI_THICKNESS    3	/* battery indicator thickness in pixels */
+#define BI_THICKNESS	4	/* battery indicator thickness in pixels */
+#define BI_GAP		1	/* gap between indicator and edge of screen */
 
 #define BI_Bottom	0
 #define BI_Top		1
@@ -45,7 +46,7 @@
 #define BI_Vertical	((bi_direction & 2) == 2)
 
 #define myEventMask (ExposureMask|EnterWindowMask|LeaveWindowMask|VisibilityChangeMask)
-#define DefaultFont "fixed"
+#define DefaultFont "7x13bold"
 #define DiagXMergin 20
 #define DiagYMergin 5
 
@@ -55,6 +56,7 @@
 
 int ac_line = -1;               /* AC line status */
 int battery_level = -1;         /* battery level */
+int battery_time = -1;          /* time remaining */
 
 unsigned long onin, onout;      /* indicator colors for AC online */
 unsigned long offin, offout;    /* indicator colors for AC offline */
@@ -76,9 +78,12 @@
 int bi_width;                       /* width of Battery Indicator */
 int bi_x;                           /* x coordinate of upper left corner */
 int bi_y;                           /* y coordinate of upper left corner */
+int bi_gap = BI_GAP;                /* thickness of Battery Indicator */
 int bi_thick = BI_THICKNESS;        /* thickness of Battery Indicator */
 int bi_interval = PollingInterval;  /* interval of polling APM */
 
+char *diag_font = DefaultFont;
+
 Display *disp;
 Window winbar;                  /* bar indicator window */
 Window winstat = -1;            /* battery status window */
@@ -98,7 +103,7 @@
 void redraw(void);
 void showdiagbox(void);
 void disposediagbox(void);
-void usage(char **);
+void usage(void);
 void about_this_program(void);
 void estimate_remain(void);
 
@@ -113,22 +118,23 @@
 	  ReleaseVersion);
 }
 
-void usage(char **argv)
+void usage(void)
 {
   fprintf(stderr,
     "\n"	  
-    "usage:\t%s [-a] [-h|v] [-p sec] [-t thickness]\n"
+    "usage:\txbattbar [-a] [-h|v] [-p sec] [-t thickness] [-g gap]\n"
     "\t\t[-I color] [-O color] [-i color] [-o color]\n"
     "\t\t[ top | bottom | left | right ]\n"
     "-a:     always on top.\n"
     "-v, -h: show this message.\n"
-    "-t:     bar (indicator) thickness. [def: 3 pixels]\n"
+    "-F:     font.\n"
+    "-t:     bar (indicator) thickness. [def: 4 pixels]\n"
+    "-g:     gap between bar and edge of screen. [def: 1 pixel]\n"
     "-p:     polling interval. [def: 10 sec.]\n"
     "-I, -O: bar colors in AC on-line. [def: \"green\" & \"olive drab\"]\n"
     "-i, -o: bar colors in AC off-line. [def: \"blue\" and \"red\"]\n"
-    "top, bottom, left, right: bar localtion. [def: \"bottom\"]\n",
-	  argv[0]);
-  exit(0);
+    "top, bottom, left, right: bar localtion. [def: \"bottom\"]\n");
+  exit(1);
 }
 
 /*
@@ -177,29 +183,34 @@
     exit(1);
   }
 
+  if (XLoadQueryFont(disp, diag_font) == NULL) {
+    fprintf(stderr, "xbattbar: font %s not found\n", optarg);
+    exit(1);
+  }
+
   switch (bi_direction) {
   case BI_Top: /* (0,0) - (width, bi_thick) */
     bi_width = width;
     bi_height = bi_thick;
     bi_x = 0;
-    bi_y = 0;
+    bi_y = bi_gap;
     break;
   case BI_Bottom:
     bi_width = width;
     bi_height = bi_thick;
     bi_x = 0;
-    bi_y = height - bi_thick;
+    bi_y = height - bi_thick - bi_gap;
     break;
   case BI_Left:
     bi_width = bi_thick;
     bi_height = height;
-    bi_x = 0;
+    bi_x = bi_gap;
     bi_y = 0;
     break;
   case BI_Right:
     bi_width = bi_thick;
     bi_height = height;
-    bi_x = width - bi_thick;
+    bi_x = width - bi_thick - bi_gap;
     bi_y = 0;
   }
 
@@ -223,7 +234,7 @@
   int ch;
 
   about_this_program();
-  while ((ch = getopt(argc, argv, "at:f:hI:i:O:o:p:v")) != -1)
+  while ((ch = getopt(argc, argv, "at:f:g:hI:i:O:o:p:F:v")) != -1)
     switch (ch) {
     case 'a':
       alwaysontop = True;
@@ -234,6 +245,10 @@
       bi_thick = atoi(optarg);
       break;
 
+    case 'g':
+      bi_gap = atoi(optarg);
+      break;
+
     case 'I':
       ONIN_C = optarg;
       break;
@@ -251,9 +266,12 @@
       bi_interval = atoi(optarg);
       break;
 
-    case 'h':
-    case 'v':
-      usage(argv);
+     case 'F':
+       diag_font = optarg;
+       break;
+ 
+    default:
+      usage();
       break;
     }
   argc -= optind;
@@ -268,8 +286,15 @@
       bi_direction = BI_Left;
     else if (strcasecmp(*argv, "right") == 0)
       bi_direction = BI_Right;
+    else
+      usage();
+    argc -= 1;
+    argv += 1;
   }
 
+  if (argc > 0)
+    usage();
+
   /*
    * set APM polling interval timer
    */
@@ -338,13 +363,18 @@
   XGCValues theGC;
   int pixw, pixh;
   int boxw, boxh;
-  char diagmsg[64];
+  char diagmsg[100];
 
   /* compose diag message and calculate its size in pixels */
-  sprintf(diagmsg,
-         "AC %s-line: battery level is %d%%",
-         ac_line ? "on" : "off", battery_level);
-  fontp = XLoadQueryFont(disp, DefaultFont);
+  if (battery_time > 0)
+    sprintf(diagmsg, "AC %s-line; battery level is %d%%;"
+	    " time remaining %d:%02d:%02d",
+	    ac_line ? "on" : "off", battery_level,
+	    battery_time / 3600, battery_time / 60 % 60, battery_time % 60);
+  else
+    sprintf(diagmsg, "AC %s-line: battery level is %d%%",
+            ac_line ? "on" : "off", battery_level);
+  fontp = XLoadQueryFont(disp, diag_font);
   pixw = XTextWidth(fontp, diagmsg, strlen(diagmsg));
   pixh = fontp->ascent + fontp->descent;
   boxw = pixw + DiagXMergin * 2;
@@ -355,14 +385,16 @@
   winstat = XCreateSimpleWindow(disp, DefaultRootWindow(disp),
                                (width-boxw)/2, (height-boxh)/2,
                                boxw, boxh,
-                               2, BlackPixel(disp,0), WhitePixel(disp,0));
+                               2, WhitePixel(disp,0), BlackPixel(disp,0));
 
   /* make this window without time titlebar */
   att.override_redirect = True;
   XChangeWindowAttributes(disp, winstat, CWOverrideRedirect, &att);
   XMapWindow(disp, winstat);
   theGC.font = fontp->fid;
-  gcstat = XCreateGC(disp, winstat, GCFont, &theGC);
+  theGC.foreground = WhitePixel(disp,0);
+  theGC.background = BlackPixel(disp,0);
+  gcstat = XCreateGC(disp, winstat, GCFont|GCForeground|GCBackground, &theGC);
   XDrawString(disp, winstat,
              gcstat,
              DiagXMergin, fontp->ascent+DiagYMergin,
@@ -563,6 +595,8 @@
   } else {
     p = APM_STAT_LINE_OFF;
   }
+
+  battery_time = info.ai_batt_time;
 
   if (first || ac_line != p || battery_level != r) {
     first = 0;

