#include #include #include #include #include "battery.h" #define stub() \ printf("%s: i'm a stub! fixme!\n",__func__) #define panic(pmsg) \ fprintf(stderr,pmsg), fflush(NULL), exit(EXIT_FAILURE) #define ignore() typedef struct battStatus { unsigned int present :1; unsigned int charging:1; // 1 if battery is charging unsigned int charged:1; // 1 if full unsigned int ac; // 1 if AC is connected int rate; // in mW unsigned int capacity; // in mWh int voltage; // mV }battStatus_t; #define BATT_STATE_FILE "/proc/acpi/battery/BAT0/state" #define BUF_SIZE 64 static battStatus_t *battery; static void battStatusUpdate (void){ extern struct battStatus *battery; static FILE *f; static char buf[BUF_SIZE]; char *str,*str1,*str2; int n,t; if(!f){ if ( !(f=fopen(BATT_STATE_FILE,"r")) ){ perror("error reading battery state"); fflush(NULL); exit(EXIT_FAILURE); } } rewind(f); n=0; while ( !fgets(buf,BUF_SIZE,f) ){ t=0; str=str1=str2=NULL; static char d[8]=":"; // set the delimiter to ":" as the first string is closed by that while ( !(str=strtok(buf,d)) ){ strcpy(d," \t\n"); // the second token is delimited by whitespace switch (++t){ case 1: str1=str; break; case 2: str2=str; break; default: panic("tokenization"); } } // while() tokenizer if ( !strcmp(str1,"present") ){ if ( !(strcmp(str2,"yes")) ) battery->present=1; else if ( !(strcmp(str2,"no")) ) battery->present=0; else panic("Unknown batery state!"); } else if ( !strcmp(str1,"capacity state") ){ stub(); } else if ( !strcmp(str1,"charging state") ){ stub(); } else if ( !strcmp(str1,"present rate") ){ stub(); } else if ( !strcmp(str1,"remaining capacity") ){ stub(); } else if ( !strcmp(str1,"present voltage") ){ stub(); } else { ignore(); // here only to anticipate that we _really_ ignore it :) } } // while() sor olvaso if ( !feof(f) ){ // nem file vege miatt allt meg a while panic("nem file vége, értelmes hiba helye"); } }