-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbatperc.c
53 lines (43 loc) · 809 Bytes
/
batperc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/* battery percentage */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include "batcommon.h"
#include "statusbar.h"
typedef struct Battery{
char *fullfile;
char *nowfile;
}Battery;
static void *
init(char *arg)
{
Battery *b;
if((b = malloc(sizeof(*b))) == NULL)
return NULL;
if(batinit(&b->fullfile, &b->nowfile, arg) == -1){
free(b);
return NULL;
}
return b;
}
static ssize_t
tick(char *out, size_t n, void *data, msec delay)
{
unsigned long long full, now;
Battery *b = data;
if(n < 4)
return 0;
full = readull(b->fullfile);
now = readull(b->nowfile);
if(full == 0 || now == 0){
strcpy(out, "?%");
return 2;
}
return sprintf(out, "%llu%%", now * 100 / full);
}
Module batperc = {
.init = init,
.tick = tick,
.name = "batperc",
};