- FILE *fp;
- long double a[4], b[4], cpu_perc;
-
- /* open stat file, read and close, do same after 1 second */
- if (!(fp = fopen("/proc/stat","r"))) {
- fprintf(stderr, "Error opening stat file.");
- exit(1);
- }
- fscanf(fp,"%*s %Lf %Lf %Lf %Lf",&a[0],&a[1],&a[2],&a[3]);
- fclose(fp);
- sleep(1);
- if (!(fp = fopen("/proc/stat","r"))) {
- fprintf(stderr, "Error opening stat file.");
- exit(1);
- }
- fscanf(fp,"%*s %Lf %Lf %Lf %Lf",&b[0],&b[1],&b[2],&b[3]);
- fclose(fp);
-
- /* calculate average in 1 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]));
-
- /* return avg cpu percentage */
- return smprintf("%d%%", (int)cpu_perc);
+ /*
+ * TODO: FIXME:
+ * https://github.com/drkh5h/slstatus/issues/12
+ */
+ int mute = 0;
+ long vol = 0, max = 0, min = 0;
+ snd_mixer_t *handle;
+ snd_mixer_elem_t *pcm_mixer, *mas_mixer;
+ snd_mixer_selem_id_t *vol_info, *mute_info;
+
+ snd_mixer_open(&handle, 0);
+ snd_mixer_attach(handle, soundcard);
+ snd_mixer_selem_register(handle, NULL, NULL);
+ snd_mixer_load(handle);
+
+ snd_mixer_selem_id_malloc(&vol_info);
+ snd_mixer_selem_id_malloc(&mute_info);
+ if (vol_info == NULL || mute_info == NULL) {
+ fprintf(stderr, "Could not get alsa volume.\n");
+ return smprintf(UNKNOWN_STR);
+ }
+ snd_mixer_selem_id_set_name(vol_info, ALSA_CHANNEL);
+ snd_mixer_selem_id_set_name(mute_info, ALSA_CHANNEL);
+ pcm_mixer = snd_mixer_find_selem(handle, vol_info);
+ mas_mixer = snd_mixer_find_selem(handle, mute_info);
+
+ snd_mixer_selem_get_playback_volume_range((snd_mixer_elem_t *)pcm_mixer, &min, &max);
+ snd_mixer_selem_get_playback_volume((snd_mixer_elem_t *)pcm_mixer, SND_MIXER_SCHN_MONO, &vol);
+ snd_mixer_selem_get_playback_switch(mas_mixer, SND_MIXER_SCHN_MONO, &mute);
+
+ if (vol_info)
+ snd_mixer_selem_id_free(vol_info);
+ if (mute_info)
+ snd_mixer_selem_id_free(mute_info);
+ if (handle)
+ snd_mixer_close(handle);
+
+ if (!mute)
+ return smprintf("mute");
+ else
+ return smprintf("%d%%", (vol * 100) / max);
+}
+
+static char *
+wifi_perc(const char *wificard)
+{
+ int strength;
+ char buf[255];
+ char *datastart;
+ char path[64];
+ char status[5];
+ char needle[strlen(wificard)+2];
+ FILE *fp;
+
+ strlcpy(path, "/sys/class/net/", sizeof(path));
+ strlcat(path, wificard, sizeof(path));
+ strlcat(path, "/operstate", sizeof(path));
+
+ fp = fopen(path, "r");
+
+ if(fp == NULL) {
+ fprintf(stderr, "Error opening wifi operstate file: %s\n",
+ strerror(errno));
+ return smprintf(UNKNOWN_STR);
+ }
+
+ fgets(status, 5, fp);
+ fclose(fp);
+ if(strcmp(status, "up\n") != 0)
+ return smprintf(UNKNOWN_STR);
+
+ fp = fopen("/proc/net/wireless", "r");
+ if (fp == NULL) {
+ fprintf(stderr, "Error opening wireless file: %s\n",
+ strerror(errno));
+ return smprintf(UNKNOWN_STR);
+ }
+
+ strlcpy(needle, wificard, sizeof(needle));
+ strlcat(needle, ":", sizeof(needle));
+ fgets(buf, sizeof(buf), fp);
+ fgets(buf, sizeof(buf), fp);
+ fgets(buf, sizeof(buf), fp);
+
+ datastart = strstr(buf, needle);
+ if (datastart != NULL) {
+ datastart = strstr(buf, ":");
+ sscanf(datastart + 1, " %*d %d %*d %*d %*d %*d %*d %*d %*d %*d", &strength);
+ }
+
+ fclose(fp);
+ return smprintf("%d%%", strength);