-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple-status.c
133 lines (111 loc) · 3.04 KB
/
simple-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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include "simple-status.h"
#include "modules/clock.h"
#include "modules/cpu.h"
#include "modules/gpu.h"
#include "modules/keyboard.h"
#include "modules/loadavg.h"
#include "modules/memory.h"
#include "modules/mpd.h"
#include "modules/network.h"
#include "modules/sound.h"
struct module {
const char *name;
struct block *(*update)(void);
void (*init)(void);
void (*deinit)(void);
int interval;
struct block *current;
};
static struct module modules[] = {
{ .name = "keyboard", .update = keyboard_update, .interval = 1,
.init = keyboard_init, .deinit = keyboard_deinit },
{ .name = "mpd", .update = mpd_update, .interval = 1,
.init = mpd_init, .deinit = mpd_deinit },
{ .name = "sound", .update = sound_update, .interval = 1 },
{ .name = "cpu", .update = cpu_update, .interval = 1, .init = cpu_init },
{ .name = "gpu", .update = gpu_update, .interval = 1, .init = gpu_init },
{ .name = "memory", .update = memory_update, .interval = 1 },
{ .name = "loadavg", .update = loadavg_update, .interval = 1 },
{ .name = "network", .update = network_update, .interval = 1 },
{ .name = "clock", .update = clock_update, .interval = 1 },
};
static bool quit = false;
static void signal_handler(int signum) {
if (signum != SIGUSR1)
quit = true;
}
static void setup_signals(void) {
struct sigaction sa = {
.sa_flags = SA_RESTART,
.sa_handler = signal_handler,
};
sigaction(SIGTERM, &sa, NULL);
sigaction(SIGINT, &sa, NULL);
sigaction(SIGPIPE, &sa, NULL);
sigaction(SIGUSR1, &sa, NULL);
};
void error(const char *format, ...) {
fprintf(stderr, "simple-status: error: ");
va_list args;
va_start(args, format);
vfprintf(stderr, format, args);
va_end(args);
fputc('\n', stderr);
}
int pscanf(const char *path, const char *format, ...) {
FILE *f = fopen(path, "r");
if (f == NULL) {
error("can't open %s: %s.", path, strerror(errno));
return -1;
}
va_list args;
va_start(args, format);
int r = vfscanf(f, format, args);
va_end(args);
fclose(f);
return r;
}
int find_hwmon(const char *name) {
char cur_path[64], cur_name[16];
for (int i = 0; ; i++) {
snprintf(cur_path, size(cur_path), "/sys/class/hwmon/hwmon%d/name", i);
if (pscanf(cur_path, "%15s", cur_name) == 1) {
if (strcmp(cur_name, name) == 0)
return i;
continue;
}
return -1;
}
}
int main(void) {
setup_signals();
for (size_t i = 0; i < size(modules); i++) {
struct module *m = &modules[i];
if (m->init != NULL)
m->init();
}
printf("{\"version\":1,\"stop_signal\":0}\n[\n");
for (int tick = 0; !quit; tick++) {
printf("[{");
for (size_t i = 0; i < size(modules); i++) {
struct module *m = &modules[i];
if (tick % m->interval == 0)
m->current = m->update();
if (i > 0)
printf("},{");
printf("\"name\":\"%s\"", m->name);
printf(",\"full_text\":\"%s\"", m->current->full_text);
if (m->current->urgent)
printf(",\"urgent\":true");
}
printf("}],\n");
fflush(stdout);
sleep(1);
}
for (size_t i = 0; i < size(modules); i++) {
struct module *m = &modules[i];
if (m->deinit != NULL)
m->deinit();
}
return EXIT_SUCCESS;
}