15 char *sasprintf(const char *fmt
, ...) {
19 if (vasprintf(&result
, fmt
, args
) == -1) {
20 fprintf(stderr
, "Can't allocate memory!?\n");
28 char *voltage_filename
;
29 char *current_filename
;
34 static float fatof(const char *filename
) {
35 FILE *f
= fopen(filename
, "r");
40 const int bufsize
= 1024;
42 const int read
= fread(buf
, sizeof(char), bufsize
, f
);
43 if (read
== 0 || read
== bufsize
|| ferror(f
)) {
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
) {
62 static gboolean
collect_data(struct State
*state
) {
63 float now
= g_get_monotonic_time() / 1e6
;
64 fprintf(stderr
, "%f: Voltage: %f, current: %f\n", now
,
65 fatof(state
->voltage_filename
), fatof(state
->current_filename
));
69 static void activate(GtkApplication
*app
, gpointer user_data
) {
71 GtkWidget
*window
= gtk_application_window_new(app
);
72 gtk_window_set_title(GTK_WINDOW(window
), "BatteryViewer");
73 gtk_window_set_default_size(GTK_WINDOW(window
), 200, 200);
75 GtkWidget
*box
= gtk_box_new(GTK_ORIENTATION_VERTICAL
, 1);
76 gtk_container_add(GTK_CONTAINER(window
), box
);
78 struct State
*state
= (struct State
*)user_data
;
79 state
->voltage
= BV_CHART(bv_chart_new());
80 bv_chart_add_point(state
->voltage
, 0.0, 0.0);
81 bv_chart_add_point(state
->voltage
, 1.0, 1.0);
82 bv_chart_add_point(state
->voltage
, 3.0, 2.0);
83 gboolean expand
= TRUE
;
86 gtk_box_pack_start(GTK_BOX(box
), GTK_WIDGET(state
->voltage
), expand
, fill
,
89 state
->current
= BV_CHART(bv_chart_new());
90 bv_chart_add_point(state
->current
, 0.0, 1.0);
91 bv_chart_add_point(state
->current
, 1.0, 0.0);
92 gtk_box_pack_end(GTK_BOX(box
), GTK_WIDGET(state
->current
), expand
, fill
,
95 gtk_widget_show_all(window
);
97 g_timeout_add_seconds(1, (GSourceFunc
)collect_data
, user_data
);
100 char *find_battery_dir() {
101 glob_t globbuf
= {.gl_offs
= 0};
102 char pattern
[] = "/sys/class/power_supply/*[Bb][Aa][Tt]*";
103 if (glob(pattern
, GLOB_ERR
, NULL
, &globbuf
) != 0 || globbuf
.gl_pathc
== 0) {
104 fprintf(stderr
, "Could not find battery dir %s\n", pattern
);
107 if (globbuf
.gl_pathc
!= 1) {
108 // TODO: Filter for directories that have {voltage,current}_now files.
109 // TODO: Support multiple batteries
110 fprintf(stderr
, "Multiple batteries found. Arbitrarily choosing %s\n",
111 globbuf
.gl_pathv
[0]);
113 char *dir
= strdup(globbuf
.gl_pathv
[0]);
118 int main(int argc
, char **argv
) {
119 char *battery_dir
= find_battery_dir();
120 struct State state
= {
121 .voltage_filename
= sasprintf("%s/voltage_now", battery_dir
),
122 .current_filename
= sasprintf("%s/current_now", battery_dir
),
125 GtkApplication
*app
= gtk_application_new("com.scottworley.batteryviewer",
126 G_APPLICATION_DEFAULT_FLAGS
);
127 g_signal_connect(app
, "activate", G_CALLBACK(activate
), &state
);
128 int status
= g_application_run(G_APPLICATION(app
), argc
, argv
);