-
Notifications
You must be signed in to change notification settings - Fork 0
/
background-battery.c
54 lines (49 loc) · 1.58 KB
/
background-battery.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
#include <gtk/gtk.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/errno.h>
#include <string.h>
void on_button_clicked(GtkWidget *widget, gpointer data) {
g_print("exit on button press\n");
exit(0);
}
int main(int argc, char* argv) {
daemon(1,1);// running in the background forever
for(;;){
FILE *fp = popen("bash check.sh", "r");
if (fp == NULL) {
perror("popen error");
exit(EXIT_FAILURE);
}
int c;
char percent[2] = "";
while ((c = fgetc(fp)) != EOF) {
char charStr[2];
sprintf(charStr, "%c", c);
strcat(percent, charStr);
}
if (pclose(fp) == -1) {
perror("pclose error");
}
int battery = atoi(percent);
if(battery>25) {
puts("suffient battery");
}
else {
puts("charge now");
gtk_init(&argc, &argv);
GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "battery alert");
gtk_container_set_border_width(GTK_CONTAINER(window), 100);
GtkWidget *button = gtk_button_new_with_label("BATTERY LOW");
// callback function
g_signal_connect(button, "clicked", G_CALLBACK(on_button_clicked), NULL);
gtk_container_add(GTK_CONTAINER(window), button);
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show_all(window);
gtk_main();
}
sleep(15);
}
return 0;
}