Commits

Kyle Isom committed eba3aca

adding scrotstat display program

Comments (0)

Files changed (2)

scrotstat

Binary file added.
  * simple status bar for scrotwm                                            *
  ****************************************************************************/
 
+#include <sys/ioctl.h>
 #include <sys/param.h>
 #include <sys/sensors.h>
 #include <sys/sysctl.h>
+#include <machine/apmvar.h>
+#include <fcntl.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
+#include <time.h>
 #include <unistd.h>
 
+#define APM_DEVICE      "/dev/apm"
+#define POWER_STR_SZ    (size_t)18  /* length of power string */
+#define TIMEFORMAT      "%H:%M | %Y %b %e (%a)"
+#define TIMESTAMP_LEN   (size_t)26
+
+char *get_powerstate(void);
+char *get_timestamp(void);
+
 /* 
  * ACPI sensors:
  * ------------
 int
 main(void)
 {
-    int lavg[3], mib[5];    /* load average and 
-    size_t len;
-    struct sensordev *sd;   /* sd is short for sensor devices. */
+        char *bar, *foo;
 
-    mib[0] = CTL_HW;
-    mib[1] = HW_SENSORS;
+        bar = get_powerstate();
+        foo = get_timestamp();
+        printf("[%s] ", bar);
+        printf("[%s] \n", foo);
+        free(bar);
+        bar = NULL;
+    
+        return 0;
+}
 
+char *
+get_powerstate( )
+{
+        struct apm_power_info bat_info;
+        int apm_fd, ioctl_rv;
+        char adapter_state[2], *res;
 
-    /* Always return success for the sake of scrotwm. */
-    return EXIT_SUCCESS;
+        memset(adapter_state, 0x20, (size_t)2);
+        res = calloc(POWER_STR_SZ, sizeof(char));
+        if (NULL == res)
+            return NULL;
+
+
+        apm_fd = open(APM_DEVICE, O_RDONLY);
+        if (-1 == apm_fd) 
+            snprintf(res, POWER_STR_SZ, "[!] *APM ERR*");
+
+        else {
+            ioctl_rv = ioctl(apm_fd, APM_IOC_GETPOWER, &bat_info);
+            close(apm_fd);
+            
+            if (-1 == ioctl_rv)
+                snprintf(res, POWER_STR_SZ, "[!] *APM ERR*");
+          
+            else {
+                switch (bat_info.battery_state) {
+                case APM_BATT_UNKNOWN:
+                    adapter_state[1] = '?';
+                    break;
+                case APM_BATTERY_ABSENT:
+                    adapter_state[1] = '!';
+                    break;
+                case APM_BATT_CHARGING:
+                    adapter_state[1] = '+';
+                    break;
+                default:
+                    /* adapter_state[1] = '⌁'; */
+                    adapter_state[1] = '-';
+                }
+
+                switch (bat_info.ac_state) {
+                case APM_AC_OFF:
+                    adapter_state[0] = 'D';
+                    break;
+                case APM_AC_ON:
+                    adapter_state[0] = 'C';
+                    break;
+                default:
+                    adapter_state[0] = '?';
+                }
+
+                if (-1 != (int)bat_info.minutes_left)
+                    snprintf(res, POWER_STR_SZ, "%c%c|%3d%%|%3dm", 
+                             adapter_state[0], adapter_state[1], 
+                             bat_info.battery_life, bat_info.minutes_left);
+                else
+                    snprintf(res, POWER_STR_SZ, "%c%c| %3d%% |(unk)", 
+                             adapter_state[0], adapter_state[1], 
+                             bat_info.battery_life);
+                         
+                    
+            }
+        }
+
+        return res;
 }
+
+char *
+get_timestamp(void)
+{
+        time_t t0;
+        size_t ts_sz;
+        struct tm *tm;
+        char *res;
+
+        res = NULL;
+        res = calloc(TIMESTAMP_LEN + 1, sizeof(char));
+        if (NULL == res)
+            return res;
+        
+        t0 = time(NULL);
+        tm = localtime(&t0);
+        ts_sz = strftime(res, TIMESTAMP_LEN, TIMEFORMAT, tm);
+
+        return (0 == ts_sz) ? NULL : res;
+}