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