add battery module

This commit is contained in:
randomuser 2022-09-20 18:08:37 -05:00
parent 03717f37a9
commit 287a96875b
3 changed files with 65 additions and 1 deletions

38
battery.c Normal file
View File

@ -0,0 +1,38 @@
/* see LICENSE file for details on license */
#include <stdio.h>
#include <unistd.h>
#include "module.h"
#include "battery.h"
void populate_buffer(char *buffer, char pattern) {
/* XXX: we assume the buffer is at least four bytes long */
int i;
for(i = 0; i < 3; i++) *(buffer + i) = pattern;
*(buffer + i + 1) = '\0';
}
int battery_update(struct module *module) {
char *filepath = BATTERY_PRE BATTERY_DIR BATTERY_CAP;
int i;
if (!access(filepath, F_OK) == 0) {
populate_buffer((char *)&module->buffer, '?');
return 1;
}
FILE *file = fopen(filepath, "r");
i = fread(&module->buffer, 1, MODULE_BUFFER_LEN, file);
module->buffer[i - 1] = '\0';
if(ferror(file)) {
populate_buffer((char *)&module->buffer, '!');
return 1;
}
module->buffer[i] = '\0';
return 0;
}

19
battery.h Normal file
View File

@ -0,0 +1,19 @@
#ifndef TSTATUS_BATTERY_H
#ifdef FISH
#define BATTERY_DIR "sbs-20-000b"
#elif TOASTER
#define BATTERY_DIR "BAT0"
#else
/* just assume we're running on fish */
#define BATTERY_DIR "sbs-20-000b"
#endif
#define BATTERY_PRE "/sys/class/power_supply/"
#define BATTERY_CAP "/capacity"
void populate_buffer(char *buffer, char pattern);
int battery_module(struct module *module);
#endif
#define TSTATUS_BATTERY_H

View File

@ -1,14 +1,21 @@
/* see LICENSE file for details on license */
#include "bspwm.c"
#include <stdio.h>
#include "module.h"
#include "bspwm.c"
#include "battery.c"
struct module table[] = {
{0, 10, bspwm_update, {'\0'}},
{0, 10, battery_update, {'\0'}},
};
int main(void) {
table[0].updatecallback(&table[0]);
printf("%s\n", table[0].buffer);
table[1].updatecallback(&table[1]);
printf("%s\n", table[1].buffer);
return 0;
}