seperate out the battery funcs
This commit is contained in:
6
include/battery.h
Normal file
6
include/battery.h
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
char *battery_percent(void);
|
||||||
|
bool plugged_in(char *fn);
|
54
src/battery.c
Normal file
54
src/battery.c
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
|
#include "config.h"
|
||||||
|
|
||||||
|
char
|
||||||
|
*battery_percent(void)
|
||||||
|
{
|
||||||
|
char *r, c, *d;
|
||||||
|
int i;
|
||||||
|
FILE *f;
|
||||||
|
|
||||||
|
/* open up the battery capacity for reading */
|
||||||
|
d = concat(battery_dir, "/capacity");
|
||||||
|
f = fopen(d, "r");
|
||||||
|
free(d);
|
||||||
|
|
||||||
|
/* create enough space for the battery percentage */
|
||||||
|
r = calloc(4, sizeof(char));
|
||||||
|
|
||||||
|
/* read in the battery percentage */
|
||||||
|
i = 0;
|
||||||
|
while ((c = getc(f)) && i < 4) {
|
||||||
|
/* make sure to replace the newline with a string terminator */
|
||||||
|
if (c == '\n') {
|
||||||
|
r[i] = '\0';
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
r[i] = c;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose(f);
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
plugged_in(char *fn)
|
||||||
|
{
|
||||||
|
FILE *f;
|
||||||
|
char buf[1];
|
||||||
|
|
||||||
|
f = fopen(fn, "r");
|
||||||
|
if (read(fileno(f), buf, 1) < 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
fclose(f);
|
||||||
|
|
||||||
|
return buf[0] - '0';
|
||||||
|
}
|
48
src/main.c
48
src/main.c
@@ -1,6 +1,5 @@
|
|||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <getopt.h>
|
#include <getopt.h>
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <strings.h>
|
#include <strings.h>
|
||||||
@@ -17,57 +16,12 @@
|
|||||||
#include "wayland.h"
|
#include "wayland.h"
|
||||||
#include "acpi.h"
|
#include "acpi.h"
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
#include "battery.h"
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
#define MAX_BUFLEN 1024
|
#define MAX_BUFLEN 1024
|
||||||
|
|
||||||
char
|
|
||||||
*battery_percent(void)
|
|
||||||
{
|
|
||||||
char *r, c, *d;
|
|
||||||
int i;
|
|
||||||
FILE *f;
|
|
||||||
|
|
||||||
/* open up the battery capacity for reading */
|
|
||||||
d = concat(battery_dir, "/capacity");
|
|
||||||
f = fopen(d, "r");
|
|
||||||
free(d);
|
|
||||||
|
|
||||||
/* create enough space for the battery percentage */
|
|
||||||
r = calloc(4, sizeof(char));
|
|
||||||
|
|
||||||
/* read in the battery percentage */
|
|
||||||
i = 0;
|
|
||||||
while ((c = getc(f)) && i < 4) {
|
|
||||||
/* make sure to replace the newline with a string terminator */
|
|
||||||
if (c == '\n') {
|
|
||||||
r[i] = '\0';
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
r[i] = c;
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
|
|
||||||
fclose(f);
|
|
||||||
return r;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool
|
|
||||||
plugged_in(char *fn)
|
|
||||||
{
|
|
||||||
FILE *f;
|
|
||||||
char buf[1];
|
|
||||||
|
|
||||||
f = fopen(fn, "r");
|
|
||||||
if (read(fileno(f), buf, 1) < 0) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
fclose(f);
|
|
||||||
|
|
||||||
return buf[0] - '0';
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
void
|
||||||
on_battery_event(char *percent, bool plugged)
|
on_battery_event(char *percent, bool plugged)
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user