author | Stiletto <blasux@blasux.ru> |
Fri, 24 Oct 2014 14:13:46 +0400 | |
changeset 10 | 442bed4a072e |
parent 9 | 96b47c0505b5 |
child 11 | ad3d40f11f6d |
permissions | -rw-r--r-- |
6
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
1 |
#include <stdio.h> |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
2 |
#include <stdlib.h> |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
3 |
#include <math.h> |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
4 |
|
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
5 |
#include <sys/types.h> |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
6 |
#include <sys/stat.h> |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
7 |
#include <fcntl.h> |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
8 |
#include <assert.h> |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
9 |
#include <string.h> |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
10 |
#include <limits.h> |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
11 |
#include <unistd.h> |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
12 |
#include <errno.h> |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
13 |
|
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
14 |
#include "config.h" |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
15 |
#ifdef USE_UDEV |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
16 |
#include <poll.h> |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
17 |
#include <libudev.h> |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
18 |
#endif |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
19 |
|
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
20 |
char *eights[] = { "ø", "▁", "▂", "▃", "▄", "▅", "▆","▇", "█" }; |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
21 |
|
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
22 |
char *catfile(char *name) { |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
23 |
struct stat buffer; |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
24 |
int status; |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
25 |
int f; |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
26 |
char* resbuf; |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
27 |
f = open(name, O_RDONLY); |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
28 |
if (f == -1) return NULL; |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
29 |
status = fstat(f, &buffer); |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
30 |
if (status != 0) return NULL; |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
31 |
resbuf = malloc(buffer.st_size+1); |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
32 |
if (resbuf == NULL) return NULL; |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
33 |
status = read(f,resbuf,buffer.st_size); |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
34 |
if (status == -1) { |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
35 |
free(resbuf); |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
36 |
return NULL; |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
37 |
} |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
38 |
if ((status>0)&&(resbuf[status-1]=='\n')) |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
39 |
resbuf[status-1]='\0'; |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
40 |
resbuf[status]='\0'; |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
41 |
return resbuf; |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
42 |
} |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
43 |
|
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
44 |
int catint(char *name) { |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
45 |
char *cont = catfile(name); |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
46 |
assert (cont != NULL); |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
47 |
int res = atoi(cont); |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
48 |
free(cont); |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
49 |
return res; |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
50 |
} |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
51 |
|
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
52 |
char *color_stop; |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
53 |
char *color_red; |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
54 |
char *color_orange; |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
55 |
char *color_green; |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
56 |
char *color_none; |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
57 |
|
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
58 |
char *color(int low,int mid,int rev,int value) { |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
59 |
if (value>mid) { |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
60 |
return rev ? color_red : color_green; |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
61 |
} else if (value>low) { |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
62 |
return color_orange; |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
63 |
} |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
64 |
return rev ? color_green : color_red; |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
65 |
} |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
66 |
|
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
67 |
int main(int argc, char *argv[]) { |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
68 |
if (argc<=1) { |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
69 |
fprintf(stderr, "Usage: %s <BAT1> [term|pango]\n", argv[0]); |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
70 |
fflush(stderr); |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
71 |
return 1; |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
72 |
} |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
73 |
|
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
74 |
char *arrow_color; |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
75 |
char *eights_color; |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
76 |
char *percent_color; |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
77 |
char *remtimes_color; |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
78 |
if (argc>2 && !strcmp(argv[2],"term")) { |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
79 |
color_stop = "\x1b[0m"; |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
80 |
color_red = "\x1b[1;31m"; |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
81 |
color_green = "\x1b[1;32m"; |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
82 |
color_orange = "\x1b[1;33m"; |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
83 |
color_none = color_stop; |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
84 |
} else if (argc>2 && !strcmp(argv[2],"pango")) { |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
85 |
color_stop = "</span>"; |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
86 |
color_red = "<span color='#f33'>"; |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
87 |
color_green = "<span color='#0f0'>"; |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
88 |
color_orange = "<span color='#fa0'>"; |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
89 |
color_none = "<span>"; |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
90 |
} else { |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
91 |
color_stop = color_red = color_green = color_orange = color_none = ""; |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
92 |
} |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
93 |
|
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
94 |
#ifdef USE_UDEV |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
95 |
struct udev *udev; |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
96 |
struct udev_device *dev; |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
97 |
|
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
98 |
struct udev_monitor *mon; |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
99 |
int mon_fd; |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
100 |
|
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
101 |
udev = udev_new(); |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
102 |
if (!udev) { |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
103 |
printf("Can't create udev\n"); |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
104 |
exit(1); |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
105 |
} |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
106 |
mon = udev_monitor_new_from_netlink(udev, "udev"); |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
107 |
udev_monitor_filter_add_match_subsystem_devtype(mon, "power_supply", NULL); |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
108 |
udev_monitor_enable_receiving(mon); |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
109 |
mon_fd = udev_monitor_get_fd(mon); |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
110 |
static struct pollfd fds[1]; |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
111 |
fds[0].fd = mon_fd; |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
112 |
fds[0].events = POLLIN; |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
113 |
#endif |
9
96b47c0505b5
Support energy in addition to charge
Stiletto <blasux@blasux.ru>
parents:
6
diff
changeset
|
114 |
char filename[PATH_MAX+1]; |
96b47c0505b5
Support energy in addition to charge
Stiletto <blasux@blasux.ru>
parents:
6
diff
changeset
|
115 |
snprintf(filename,PATH_MAX,"/sys/class/power_supply/%s/energy_now",argv[1]); |
96b47c0505b5
Support energy in addition to charge
Stiletto <blasux@blasux.ru>
parents:
6
diff
changeset
|
116 |
struct stat st; |
96b47c0505b5
Support energy in addition to charge
Stiletto <blasux@blasux.ru>
parents:
6
diff
changeset
|
117 |
int energy = 0==stat(filename, &st); |
6
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
118 |
while (1) { |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
119 |
|
9
96b47c0505b5
Support energy in addition to charge
Stiletto <blasux@blasux.ru>
parents:
6
diff
changeset
|
120 |
snprintf(filename,PATH_MAX,"/sys/class/power_supply/%s/%s_now",argv[1], energy ? "energy" : "charge"); |
6
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
121 |
int charge_now = catint(filename); |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
122 |
|
9
96b47c0505b5
Support energy in addition to charge
Stiletto <blasux@blasux.ru>
parents:
6
diff
changeset
|
123 |
snprintf(filename,PATH_MAX,"/sys/class/power_supply/%s/%s_now",argv[1], energy ? "power" : "current"); |
6
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
124 |
int current_now = catint(filename); |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
125 |
|
9
96b47c0505b5
Support energy in addition to charge
Stiletto <blasux@blasux.ru>
parents:
6
diff
changeset
|
126 |
snprintf(filename,PATH_MAX,"/sys/class/power_supply/%s/%s_full",argv[1], energy ? "energy" : "charge"); |
6
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
127 |
int charge_full = catint(filename); |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
128 |
|
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
129 |
snprintf(filename,PATH_MAX,"/sys/class/power_supply/%s/status",argv[1]); |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
130 |
char *status = catfile(filename); |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
131 |
|
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
132 |
int remtime = 0; |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
133 |
char remtimes[128]; |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
134 |
char *arrow; |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
135 |
|
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
136 |
remtimes_color = color_none; |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
137 |
if (!strcmp(status,"Charging")) { |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
138 |
if (current_now != 0) |
9
96b47c0505b5
Support energy in addition to charge
Stiletto <blasux@blasux.ru>
parents:
6
diff
changeset
|
139 |
remtime = (charge_full-charge_now) / (current_now/60); |
6
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
140 |
arrow = "↑"; |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
141 |
arrow_color = color_orange; |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
142 |
} |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
143 |
else if (!strcmp(status,"Discharging")) { |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
144 |
if (current_now != 0) |
9
96b47c0505b5
Support energy in addition to charge
Stiletto <blasux@blasux.ru>
parents:
6
diff
changeset
|
145 |
remtime = charge_now / (current_now/60); |
6
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
146 |
arrow = "↓"; |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
147 |
arrow_color = color_none; |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
148 |
remtimes_color = color(20,120,0,remtime); |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
149 |
} else { |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
150 |
arrow = "—"; |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
151 |
arrow_color = color_green; |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
152 |
} |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
153 |
free(status); |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
154 |
snprintf(remtimes,127,"%d:%02d",remtime/60,remtime%60); |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
155 |
//printf("%d %d %d %s %d %s %s\n",charge_now,current_now,charge_full, |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
156 |
// eights[charge_now*8/charge_full], charge_now*100/charge_full,status,remtimes); |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
157 |
|
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
158 |
|
9
96b47c0505b5
Support energy in addition to charge
Stiletto <blasux@blasux.ru>
parents:
6
diff
changeset
|
159 |
int percent = charge_now/(charge_full/100); |
6
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
160 |
eights_color = percent_color = color(15,40,0,percent); |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
161 |
printf("%s%s%s ",eights_color,eights[charge_now*8/charge_full],color_stop); |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
162 |
printf("%s%d%%%s ",percent_color,percent,color_stop); |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
163 |
printf("%s%s%s ",arrow_color,arrow,color_stop); |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
164 |
printf("%s%s%s\n",remtimes_color,remtimes,color_stop); |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
165 |
fflush(stdout); |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
166 |
#ifdef USE_UDEV |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
167 |
int poll_status = poll(fds,1, UPDATE_PERIOD*1000); |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
168 |
if (poll_status < 0) { |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
169 |
if (poll_status != EINTR) { |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
170 |
perror("poll"); |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
171 |
exit(12); |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
172 |
} |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
173 |
} |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
174 |
if (fds[0].revents & POLLIN) { |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
175 |
dev = udev_monitor_receive_device(mon); |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
176 |
if (dev) { |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
177 |
udev_device_unref(dev); |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
178 |
} else { |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
179 |
printf("UDEV DC\n"); |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
180 |
exit(13); |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
181 |
} |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
182 |
} |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
183 |
#else |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
184 |
sleep(UPDATE_PERIOD); |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
185 |
#endif |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
186 |
} |
de6b88d1fa3c
status-bat (with optional udev support!)
Stiletto <blasux@blasux.ru>
parents:
diff
changeset
|
187 |
} |