]> git.scottworley.com Git - batteryviewer/blob - batteryviewer.c
5a516218e74ee01eb4dc0697bf12087ba44cfc24
[batteryviewer] / batteryviewer.c
1 // For vasprintf
2 #define _GNU_SOURCE
3
4 #include "chart.h"
5 #include <ctype.h>
6 #include <errno.h>
7 #include <glob.h>
8 #include <gtk/gtk.h>
9 #include <math.h>
10 #include <stdarg.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14
15 char *sasprintf(const char *fmt, ...) {
16 char *result;
17 va_list args;
18 va_start(args, fmt);
19 if (vasprintf(&result, fmt, args) == -1) {
20 fprintf(stderr, "Can't allocate memory!?\n");
21 abort();
22 }
23 va_end(args);
24 return result;
25 }
26
27 struct State {
28 char *voltage_filename;
29 char *current_filename;
30 BVChart *voltage;
31 BVChart *current;
32 };
33
34 static float fatof(const char *filename) {
35 FILE *f = fopen(filename, "r");
36 if (f == NULL) {
37 return NAN;
38 }
39
40 const int bufsize = 1024;
41 char buf[bufsize];
42 const int read = fread(buf, sizeof(char), bufsize, f);
43 if (read == 0 || read == bufsize || ferror(f)) {
44 fclose(f);
45 return NAN;
46 }
47 fclose(f);
48
49 char *end;
50 errno = 0;
51 float val = strtof(buf, &end);
52 int parsed = end - buf;
53 gboolean parsed_all = parsed == read;
54 gboolean parsed_all_but_space = parsed == read - 1 && isspace(*end);
55 gboolean parse_ok = parsed_all || parsed_all_but_space;
56 if (errno != 0 || parsed == 0 || !parse_ok) {
57 return NAN;
58 }
59 return val;
60 }
61
62 static gboolean collect_data(struct State *state) {
63 fprintf(stderr, "Voltage: %f, current: %f\n", fatof(state->voltage_filename),
64 fatof(state->current_filename));
65 return TRUE;
66 }
67
68 static void activate(GtkApplication *app, gpointer user_data) {
69
70 GtkWidget *window = gtk_application_window_new(app);
71 gtk_window_set_title(GTK_WINDOW(window), "BatteryViewer");
72 gtk_window_set_default_size(GTK_WINDOW(window), 200, 200);
73
74 GtkWidget *box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 1);
75 gtk_container_add(GTK_CONTAINER(window), box);
76
77 struct State *state = (struct State *)user_data;
78 state->voltage = BV_CHART(bv_chart_new());
79 bv_chart_add_point(state->voltage, 0.0, 0.0);
80 bv_chart_add_point(state->voltage, 1.0, 1.0);
81 bv_chart_add_point(state->voltage, 3.0, 2.0);
82 gboolean expand = TRUE;
83 gboolean fill = TRUE;
84 guint padding = 0;
85 gtk_box_pack_start(GTK_BOX(box), GTK_WIDGET(state->voltage), expand, fill,
86 padding);
87
88 state->current = BV_CHART(bv_chart_new());
89 bv_chart_add_point(state->current, 0.0, 1.0);
90 bv_chart_add_point(state->current, 1.0, 0.0);
91 gtk_box_pack_end(GTK_BOX(box), GTK_WIDGET(state->current), expand, fill,
92 padding);
93
94 gtk_widget_show_all(window);
95
96 g_timeout_add_seconds(1, (GSourceFunc)collect_data, user_data);
97 }
98
99 char *find_battery_dir() {
100 glob_t globbuf = {.gl_offs = 0};
101 char pattern[] = "/sys/class/power_supply/*[Bb][Aa][Tt]*";
102 if (glob(pattern, GLOB_ERR, NULL, &globbuf) != 0 || globbuf.gl_pathc == 0) {
103 fprintf(stderr, "Could not find battery dir %s\n", pattern);
104 exit(1);
105 }
106 if (globbuf.gl_pathc != 1) {
107 // TODO: Filter for directories that have {voltage,current}_now files.
108 // TODO: Support multiple batteries
109 fprintf(stderr, "Multiple batteries found. Arbitrarily choosing %s\n",
110 globbuf.gl_pathv[0]);
111 }
112 char *dir = strdup(globbuf.gl_pathv[0]);
113 globfree(&globbuf);
114 return dir;
115 }
116
117 int main(int argc, char **argv) {
118 char *battery_dir = find_battery_dir();
119 struct State state = {
120 .voltage_filename = sasprintf("%s/voltage_now", battery_dir),
121 .current_filename = sasprintf("%s/current_now", battery_dir),
122 };
123
124 GtkApplication *app = gtk_application_new("com.scottworley.batteryviewer",
125 G_APPLICATION_DEFAULT_FLAGS);
126 g_signal_connect(app, "activate", G_CALLBACK(activate), &state);
127 int status = g_application_run(G_APPLICATION(app), argc, argv);
128 g_object_unref(app);
129
130 return status;
131 }