This repository has been archived by the owner on Jan 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dwmst.c
220 lines (184 loc) · 4.93 KB
/
dwmst.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include <unistd.h>
#include <time.h>
#include <X11/Xlib.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <alsa/asoundlib.h>
#include <linux/wireless.h>
#include "dwmst.h"
static Display *dpy;
char *
smprintf(char *fmt, ...) {
va_list fmtargs;
char *ret;
int len;
va_start(fmtargs, fmt);
len = vsnprintf(NULL, 0, fmt, fmtargs);
va_end(fmtargs);
ret = malloc(++len);
if (ret == NULL) {
perror("malloc");
exit(1);
}
va_start(fmtargs, fmt);
vsnprintf(ret, len, fmt, fmtargs);
va_end(fmtargs);
return ret;
}
int
is_up (char *device) {
char devpath[32], state[5];
FILE *infile;
snprintf(devpath, sizeof(devpath), "/sys/class/net/%s/operstate", device);
infile = fopen(devpath, "r");
if(infile != NULL) {
fscanf(infile, "%s", state);
fclose(infile);
if(strncmp(state, "up", 2) == 0)
return 1;
}
return 0;
}
char *
get_skype(void) {
if(access(SKYPE_LOCK, F_OK) == 0)
return smprintf("%s", SKYPE_STR);
else
return NULL;
}
char *
get_net(struct iwreq wreq, int socket) {
if(is_up(WIRELESS_DEVICE)) {
char essid[30];
memset(essid, 0, sizeof(essid));
wreq.u.essid.pointer = essid;
wreq.u.essid.length = sizeof(essid);
ioctl(socket, SIOCGIWESSID, &wreq);
return smprintf(WLAN_STR, essid);
} else if(is_up(WIRED_DEVICE))
return smprintf("%s", LAN_STR);
else
return smprintf("%s", NO_CON_STR);
}
char *
get_vol(snd_mixer_t *handle) {
int mute = 0;
long vol = 0, max = 0, min = 0;
snd_mixer_elem_t *pcm_mixer, *max_mixer;
snd_mixer_selem_id_t *vol_info, *mute_info;
/*ToDo: maybe move all this to main?*/
snd_mixer_handle_events(handle);
snd_mixer_selem_id_malloc(&vol_info);
snd_mixer_selem_id_malloc(&mute_info);
snd_mixer_selem_id_set_name(vol_info, VOL_CH);
snd_mixer_selem_id_set_name(mute_info, VOL_CH);
pcm_mixer = snd_mixer_find_selem(handle, vol_info);
max_mixer = snd_mixer_find_selem(handle, mute_info);
snd_mixer_selem_get_playback_volume_range(pcm_mixer, &min, &max);
snd_mixer_selem_get_playback_volume(pcm_mixer, 0, &vol);
snd_mixer_selem_get_playback_switch(max_mixer, 0, &mute);
snd_mixer_selem_id_free(vol_info);
snd_mixer_selem_id_free(mute_info);
if(mute == 0)
return smprintf(VOL_MUTE_STR, (vol * 100) / max);
return smprintf(VOL_STR, (vol * 100) / max);
}
char *
get_batt(void) {
FILE *f;
char state[11];
if(access("/sys/class/power_supply/BAT1/", F_OK) != 0)
return smprintf("%s", NO_BAT_STR);
memset(state, 0, sizeof(state));
f = fopen(BATT_STAT, "r");
if(f == NULL)
return NULL;
fscanf(f, "%s\n", state);
fclose(f);
if(strncmp(state, "Full", 4) == 0)
return smprintf("%s", BAT_FULL_STR);
else if(strncmp(state, "Unknown", 7) == 0)
return smprintf("%s", BAT_UNK_STR);
float now, full, voltage, rate;
unsigned int perc, minutes, hours;
f = fopen (BATT_NOW, "r"); if(f == NULL) return NULL; fscanf (f, "%f\n", &now); fclose (f);
f = fopen (BATT_FULL, "r"); if(f == NULL) return NULL; fscanf (f, "%f\n", &full); fclose (f);
f = fopen (BATT_VOLT, "r"); if(f == NULL) return NULL; fscanf (f, "%f\n", &voltage); fclose (f);
f = fopen (BATT_CNOW, "r"); if(f == NULL) return NULL; fscanf (f, "%f\n", &rate); fclose (f);
now *= voltage;
full *= voltage;
rate *= voltage;
perc = (now * 100) / full;
if(perc > 100) perc = 100;
if(strncmp(state, "Charging", 8) == 0) {
minutes = 60 * ((full - now) / rate);
hours = minutes / 60;
if(hours > 24) hours = 24;
minutes -= 60 * hours;
return smprintf(BAT_CHRG_STR, perc, hours, minutes);
} else {
minutes = 60 * (now / rate);
hours = minutes / 60;
if(hours > 24) hours = 24;
minutes -= 60 * hours;
/*notify*/
return smprintf(BAT_DIS_STR, perc, hours, minutes);
}
}
char *
get_time(void) {
char clock[38];
time_t current;
memset(clock, 0, sizeof(clock));
time(¤t);
if(!strftime(clock, sizeof(clock) - 1, CLK_STR, localtime(¤t)))
return smprintf("%s", "strftime == 0");
return smprintf("%s", clock);
}
void
setstatus(char *str) {
XStoreName(dpy, DefaultRootWindow(dpy), str);
XSync(dpy, False);
}
int
main(void) {
struct iwreq wreq;
snd_mixer_t *handle;
int sockfd, loops = 60;
char *status, *skype, *net, *vol, *batt, *clk;
if(!(dpy = XOpenDisplay(NULL))) {
fprintf(stderr, "dwmst: cannot open display.\n");
return 1;
}
memset(&wreq, 0, sizeof(struct iwreq));
snprintf(wreq.ifr_name, sizeof(WIRELESS_DEVICE), WIRELESS_DEVICE);
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
snd_mixer_open(&handle, 0);
snd_mixer_attach(handle, "default");
snd_mixer_selem_register(handle, NULL, NULL);
snd_mixer_load(handle);
for(;;sleep(INTERVAL)) {
if(++loops > 60) {
loops = 0;
if(sockfd > 0)
net = get_net(wreq, sockfd);
clk = get_time();
}
skype = get_skype();
vol = get_vol(handle);
batt = get_batt();
status = smprintf("%s %s %s %s %s", skype, net, vol, batt, clk);
setstatus(status);
free(skype);
free(vol);
free(batt);
free(status);
}
if(sockfd > 0)
free(net);
free(clk);
XCloseDisplay(dpy);
close(sockfd);
snd_mixer_close(handle);
return 0;
}