-
Notifications
You must be signed in to change notification settings - Fork 0
/
status.c
117 lines (105 loc) · 2.4 KB
/
status.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
** p4p1: http://p4p1.github.io/
** Created on: dim. 31 mai 2020 07:50:09 CEST
** status.c
** File description:
** <..>
*/
#include <time.h>
#include <dirent.h>
#define TESTING_COLORS (" \x06[ok]\x01 \x05[custom]\x01 \x04[urgent]\x01 \x03[warning]\x01 \x02[selected]\x01 [normal]\0")
static char *
set_ac(void)
{
char tmp;
char *str = malloc(sizeof(char) * BUFSIZ);
FILE *fp = fopen("/sys/class/power_supply/AC/online", "r");
if (str == NULL || fp == NULL)
return (NULL);
memset(str, 0, BUFSIZ);
if (fgets(&tmp, 1, (FILE *)fp) == NULL)
return (NULL);
if (tmp == '1') {
strcpy(str, "\x02ch -- \x01");
}
return (str);
}
static char *
set_bat(char *bat)
{
char tmp[5] = { 0, 0, 0, 0, 0};
char *str = malloc(sizeof(char) * BUFSIZ);
char *bat_name = malloc(sizeof(char) * (32 + strlen(bat)));
FILE *fp = NULL;
sprintf(bat_name, "/sys/class/power_supply/%s/capacity", bat);
fp = fopen(bat_name, "r");
if (str == NULL || fp == NULL)
return (NULL);
memset(str, 0, BUFSIZ);
if (fgets(tmp, 3, (FILE *)fp) == NULL) {
free(bat_name);
return (NULL);
}
sprintf(str, "%s%%", tmp);
free(bat_name);
return (str);
}
static char *
status_get_battery(void)
{
char *str = NULL;
char *tmp = NULL;
struct dirent *de;
DIR *dr = opendir("/sys/class/power_supply/");
if (dr == NULL)
return (NULL);
if ((str = malloc(sizeof(char) * BUFSIZ)) == NULL)
return (NULL);
memset(str, 0, BUFSIZ);
strcat(str, "[");
while ((de = readdir(dr)) != NULL) {
if (de->d_name[0] == '.')
continue;
if (strcmp(de->d_name, "AC") == 0) {
if ((tmp = set_ac()) == NULL)
return (NULL);
strcat(str, tmp);
free(tmp);
} else {
if ((tmp = set_bat(de->d_name)) == NULL)
return (NULL);
strcat(str, tmp);
free(tmp);
}
}
strcat(str, "]");
return (str);
}
static char *
status_get_time(void)
{
char *str = malloc(sizeof(char) * BUFSIZ);
time_t t = time(NULL);
struct tm tm = *localtime(&t);
if (str == NULL)
return (NULL);
memset(str, 0, BUFSIZ);
sprintf(str, "[%d/%02d/%02d %02d:%02d] ", tm.tm_year + 1900,
tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min);
return (str);
}
void
status(void)
{
char *tmp = NULL;
char *(*fp[])(void) = { &status_get_time, &status_get_battery, NULL };
memset(stext, 0, 256);
strcat(stext, " ");
for (unsigned int i = 0; fp[i] && strlen(stext) < 256 ; i++) {
tmp = fp[i]();
if (tmp != NULL)
strcat(stext, tmp);
free(tmp);
}
strcpy(stext, TESTING_COLORS);
}