#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/sysctl.h>
void updateClock(int sig){
int life, state;
size_t len = sizeof(int);
sysctlbyname("hw.acpi.battery.life",&life,&len,NULL,0);
sysctlbyname("hw.acpi.battery.state",&state,&len,NULL,0);
char* color;
if(state != 1){
// AC online
color = "0;32m"; //
}else{
// Battery
color = (life < 30) ? "0;41m" : "0;42m";
}
time_t now = time(NULL);
printf("\x1b[0;0H\x1b[0;%s%3d\x1b[0m %s", color,life, ctime(&now));
}
int main(int argc, char** args){
printf("\x1b[2J\x1b[0;0H"); // clear screen
struct itimerval timer = {{1,0}, {1,0}};
int err = setitimer(ITIMER_REAL, &timer, NULL);
if(err < 0){
printf("%d: %s\n",err,"Can not set itimer");
return err;
}
signal(SIGALRM, updateClock);
for(;;)pause();
}
|