1
0

revised everything

This commit is contained in:
Aaron Marcher 2016-03-05 14:07:02 +01:00 committed by Aaron Marcher (drkhsh)
parent 864f2246b5
commit 6b2b839cfd

View File

@ -65,6 +65,7 @@ volume()
int mute = 0; int mute = 0;
long vol = 0, max = 0, min = 0; long vol = 0, max = 0, min = 0;
/* get volume from alsa */
snd_mixer_t *handle; snd_mixer_t *handle;
snd_mixer_elem_t *pcm_mixer, *mas_mixer; snd_mixer_elem_t *pcm_mixer, *mas_mixer;
snd_mixer_selem_id_t *vol_info, *mute_info; snd_mixer_selem_id_t *vol_info, *mute_info;
@ -84,7 +85,6 @@ volume()
SND_MIXER_SCHN_MONO, &vol); SND_MIXER_SCHN_MONO, &vol);
snd_mixer_selem_get_playback_switch(mas_mixer, SND_MIXER_SCHN_MONO, snd_mixer_selem_get_playback_switch(mas_mixer, SND_MIXER_SCHN_MONO,
&mute); &mute);
if (vol_info) if (vol_info)
snd_mixer_selem_id_free(vol_info); snd_mixer_selem_id_free(vol_info);
if (mute_info) if (mute_info)
@ -92,6 +92,7 @@ volume()
if (handle) if (handle)
snd_mixer_close(handle); snd_mixer_close(handle);
/* return the string (mute) */
if (!mute) if (!mute)
return "mute"; return "mute";
else else
@ -111,8 +112,10 @@ cpu_temperature()
exit(1); exit(1);
} }
/* extract temperature, close file */ /* extract temperature */
fscanf(fp, "%d", &temperature); fscanf(fp, "%d", &temperature);
/* close temperature file */
fclose(fp); fclose(fp);
/* return temperature in degrees */ /* return temperature in degrees */
@ -130,6 +133,7 @@ wifi_signal()
char path_start[16] = "/sys/class/net/"; char path_start[16] = "/sys/class/net/";
char path_end[11] = "/operstate"; char path_end[11] = "/operstate";
char path[32]; char path[32];
char status[5];
FILE *fp; FILE *fp;
/* generate the path name */ /* generate the path name */
@ -137,13 +141,16 @@ wifi_signal()
strcat(path, wificard); strcat(path, wificard);
strcat(path, path_end); strcat(path, path_end);
/* open wifi file, extract status, close file */ /* open wifi file */
if(!(fp = fopen(path, "r"))) { if(!(fp = fopen(path, "r"))) {
fprintf(stderr, "Error opening wifi operstate file."); fprintf(stderr, "Error opening wifi operstate file.");
exit(1); exit(1);
} }
char status[5];
/* read the status */
fgets(status, 5, fp); fgets(status, 5, fp);
/* close wifi file */
fclose(fp); fclose(fp);
/* check if interface down */ /* check if interface down */
@ -157,7 +164,7 @@ wifi_signal()
exit(1); exit(1);
} }
/* extract the signal strength and close the file */ /* extract the signal strength */
fgets(buf, bufsize, fp); fgets(buf, bufsize, fp);
fgets(buf, bufsize, fp); fgets(buf, bufsize, fp);
fgets(buf, bufsize, fp); fgets(buf, bufsize, fp);
@ -166,6 +173,8 @@ wifi_signal()
sscanf(datastart + 1, " %*d %d %*d %*d %*d %*d %*d %*d %*d %*d", sscanf(datastart + 1, " %*d %d %*d %*d %*d %*d %*d %*d %*d %*d",
&strength); &strength);
} }
/* close wifi file */
fclose(fp); fclose(fp);
/* return strength in percent */ /* return strength in percent */
@ -176,31 +185,37 @@ wifi_signal()
char * char *
battery() battery()
{ {
int batt_now; int batt_now, batt_full, batt_perc;
int batt_full;
int batt_perc;
FILE *fp; FILE *fp;
/* open battery now file, extract and close */ /* open battery now file */
if (!(fp = fopen(batterynowfile, "r"))) { if (!(fp = fopen(batterynowfile, "r"))) {
fprintf(stderr, "Error opening battery file."); fprintf(stderr, "Error opening battery file.");
exit(1); exit(1);
} }
/* read value */
fscanf(fp, "%i", &batt_now); fscanf(fp, "%i", &batt_now);
/* close battery now file */
fclose(fp); fclose(fp);
/* extract battery full file, extract and close */ /* open battery full file */
if (!(fp = fopen(batteryfullfile, "r"))) { if (!(fp = fopen(batteryfullfile, "r"))) {
fprintf(stderr, "Error opening battery file."); fprintf(stderr, "Error opening battery file.");
exit(1); exit(1);
} }
/* read value */
fscanf(fp, "%i", &batt_full); fscanf(fp, "%i", &batt_full);
/* close battery full file */
fclose(fp); fclose(fp);
/* calculate percent */ /* calculate percent */
batt_perc = batt_now / (batt_full / 100); batt_perc = batt_now / (batt_full / 100);
/* return percent */ /* return batt_perc as string */
return smprintf("%d%%", batt_perc); return smprintf("%d%%", batt_perc);
} }
@ -227,53 +242,70 @@ datetime()
char * char *
cpu_usage() cpu_usage()
{ {
int cpu_perc;
long double a[4], b[4];
FILE *fp; FILE *fp;
long double a[4], b[4], cpu_perc;
/* open stat file, read and close, do same after 1 second */ /* open stat file */
if (!(fp = fopen("/proc/stat","r"))) { if (!(fp = fopen("/proc/stat","r"))) {
fprintf(stderr, "Error opening stat file."); fprintf(stderr, "Error opening stat file.");
exit(1); exit(1);
} }
fscanf(fp,"%*s %Lf %Lf %Lf %Lf",&a[0],&a[1],&a[2],&a[3]);
/* read values */
fscanf(fp, "%*s %Lf %Lf %Lf %Lf", &a[0], &a[1], &a[2], &a[3]);
/* close stat file */
fclose(fp); fclose(fp);
/* wait a second (for avg values) */
sleep(1); sleep(1);
/* open stat file */
if (!(fp = fopen("/proc/stat","r"))) { if (!(fp = fopen("/proc/stat","r"))) {
fprintf(stderr, "Error opening stat file."); fprintf(stderr, "Error opening stat file.");
exit(1); exit(1);
} }
fscanf(fp,"%*s %Lf %Lf %Lf %Lf",&b[0],&b[1],&b[2],&b[3]);
/* read values */
fscanf(fp, "%*s %Lf %Lf %Lf %Lf", &b[0], &b[1], &b[2], &b[3]);
/* close stat file */
fclose(fp); fclose(fp);
/* calculate average in 1 second */ /* calculate avg in this second */
cpu_perc = 100 * ((b[0]+b[1]+b[2]) - (a[0]+a[1]+a[2])) / ((b[0]+b[1]+b[2]+b[3]) - (a[0]+a[1]+a[2]+a[3])); cpu_perc = 100 * ((b[0]+b[1]+b[2]) - (a[0]+a[1]+a[2])) / ((b[0]+b[1]+b[2]+b[3]) - (a[0]+a[1]+a[2]+a[3]));
/* return avg cpu percentage */ /* return cpu_perc as string */
return smprintf("%d%%", (int)cpu_perc); return smprintf("%d%%", cpu_perc);
} }
/* ram percentage */ /* ram percentage */
char * char *
ram_usage() ram_usage()
{ {
FILE *fp;
long total, free, available;
int ram_perc; int ram_perc;
long total, free, available;
FILE *fp;
/* read meminfo file, extract and close */ /* open meminfo file */
if (!(fp = fopen("/proc/meminfo", "r"))) { if (!(fp = fopen("/proc/meminfo", "r"))) {
fprintf(stderr, "Error opening meminfo file."); fprintf(stderr, "Error opening meminfo file.");
exit(1); exit(1);
} }
/* read the values */
fscanf(fp, "MemTotal: %ld kB\n", &total); fscanf(fp, "MemTotal: %ld kB\n", &total);
fscanf(fp, "MemFree: %ld kB\n", &free); fscanf(fp, "MemFree: %ld kB\n", &free);
fscanf(fp, "MemAvailable: %ld kB\n", &available); fscanf(fp, "MemAvailable: %ld kB\n", &available);
/* close meminfo file */
fclose(fp); fclose(fp);
/* calculate percentage */ /* calculate percentage */
ram_perc = 100 * (total - available) / total; ram_perc = 100 * (total - available) / total;
/* return in percent */ /* return ram_perc as string */
return smprintf("%d%%",ram_perc); return smprintf("%d%%",ram_perc);
} }
@ -283,7 +315,7 @@ main()
char status[1024]; char status[1024];
/* open display */ /* open display */
if (( dpy = XOpenDisplay(0x0)) == NULL ) { if (!(dpy = XOpenDisplay(0x0))) {
fprintf(stderr, "Cannot open display!\n"); fprintf(stderr, "Cannot open display!\n");
exit(1); exit(1);
} }