]>
Commit | Line | Data |
---|---|---|
5a945372 | 1 | #include "chart.h" |
0f9e308a SW |
2 | #include <ctype.h> |
3 | #include <errno.h> | |
79ac753b | 4 | #include <gtk/gtk.h> |
0f9e308a SW |
5 | #include <math.h> |
6 | #include <stdio.h> | |
7 | #include <stdlib.h> | |
8 | ||
9 | const char voltage_filename[] = "/sys/class/power_supply/BAT0/voltage_now"; | |
10 | const char current_filename[] = "/sys/class/power_supply/BAT0/current_now"; | |
79ac753b | 11 | |
fc1a2773 SW |
12 | struct State { |
13 | BVChart *voltage; | |
14 | BVChart *current; | |
15 | }; | |
16 | ||
0f9e308a SW |
17 | static float fatof(const char *filename) { |
18 | FILE *f = fopen(filename, "r"); | |
19 | if (f == NULL) { | |
20 | return NAN; | |
21 | } | |
22 | ||
23 | const int bufsize = 1024; | |
24 | char buf[bufsize]; | |
25 | const int read = fread(buf, sizeof(char), bufsize, f); | |
26 | if (read == 0 || read == bufsize || ferror(f)) { | |
27 | fclose(f); | |
28 | return NAN; | |
29 | } | |
30 | fclose(f); | |
31 | ||
32 | char *end; | |
33 | errno = 0; | |
34 | float val = strtof(buf, &end); | |
35 | int parsed = end - buf; | |
36 | gboolean parsed_all = parsed == read; | |
37 | gboolean parsed_all_but_space = parsed == read - 1 && isspace(*end); | |
38 | gboolean parse_ok = parsed_all || parsed_all_but_space; | |
39 | if (errno != 0 || parsed == 0 || !parse_ok) { | |
40 | return NAN; | |
41 | } | |
42 | return val; | |
43 | } | |
44 | ||
6f50819e | 45 | static gboolean collect_data(struct State *state __attribute__((unused))) { |
0f9e308a SW |
46 | fprintf(stderr, "Voltage: %f, current: %f\n", fatof(voltage_filename), |
47 | fatof(current_filename)); | |
6f50819e SW |
48 | return TRUE; |
49 | } | |
50 | ||
fc1a2773 | 51 | static void activate(GtkApplication *app, gpointer user_data) { |
79ac753b | 52 | |
cecfa926 | 53 | GtkWidget *window = gtk_application_window_new(app); |
a8d35c4c | 54 | gtk_window_set_title(GTK_WINDOW(window), "BatteryViewer"); |
79ac753b SW |
55 | gtk_window_set_default_size(GTK_WINDOW(window), 200, 200); |
56 | ||
1bd5b327 SW |
57 | GtkWidget *box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 1); |
58 | gtk_container_add(GTK_CONTAINER(window), box); | |
59 | ||
fc1a2773 SW |
60 | struct State *state = (struct State *)user_data; |
61 | state->voltage = BV_CHART(bv_chart_new()); | |
62 | bv_chart_add_point(state->voltage, 0.0, 0.0); | |
63 | bv_chart_add_point(state->voltage, 1.0, 1.0); | |
64 | bv_chart_add_point(state->voltage, 3.0, 2.0); | |
1bd5b327 SW |
65 | gboolean expand = TRUE; |
66 | gboolean fill = TRUE; | |
67 | guint padding = 0; | |
fc1a2773 SW |
68 | gtk_box_pack_start(GTK_BOX(box), GTK_WIDGET(state->voltage), expand, fill, |
69 | padding); | |
1bd5b327 | 70 | |
fc1a2773 SW |
71 | state->current = BV_CHART(bv_chart_new()); |
72 | bv_chart_add_point(state->current, 0.0, 1.0); | |
73 | bv_chart_add_point(state->current, 1.0, 0.0); | |
74 | gtk_box_pack_end(GTK_BOX(box), GTK_WIDGET(state->current), expand, fill, | |
75 | padding); | |
79ac753b | 76 | |
a9a80e4e | 77 | gtk_widget_show_all(window); |
6f50819e SW |
78 | |
79 | g_timeout_add_seconds(1, (GSourceFunc)collect_data, user_data); | |
79ac753b SW |
80 | } |
81 | ||
82 | int main(int argc, char **argv) { | |
fc1a2773 | 83 | struct State state; |
79ac753b | 84 | |
a8d35c4c SW |
85 | GtkApplication *app = gtk_application_new("com.scottworley.batteryviewer", |
86 | G_APPLICATION_DEFAULT_FLAGS); | |
fc1a2773 | 87 | g_signal_connect(app, "activate", G_CALLBACK(activate), &state); |
cecfa926 | 88 | int status = g_application_run(G_APPLICATION(app), argc, argv); |
79ac753b SW |
89 | g_object_unref(app); |
90 | ||
91 | return status; | |
92 | } |