-/* alsa volume percentage */
-char *
-volume()
-{
- int mute = 0;
- long vol = 0, max = 0, min = 0;
-
- /* get volume from alsa */
- 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);
- snd_mixer_selem_id_set_name(vol_info, channel);
- snd_mixer_selem_id_set_name(mute_info, 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);
-
- /* return the string (mute) */
- if (!mute)
- return "mute";
- else
- return smprintf("%d%%", (vol * 100) / max);
-}
-
-/* wifi percentage */
-char *
-wifi_signal()
-{
- int bufsize = 255;
- int strength;
- char buf[bufsize];
+static char *
+swap_free(void)
+{
+ long total, free;
+ FILE *fp;
+ char buf[2048];
+ size_t bytes_read;
+ char *match;
+
+ fp = fopen("/proc/meminfo", "r");
+ if (fp == NULL) {
+ warn("Failed to open file /proc/meminfo");
+ return smprintf("%s", UNKNOWN_STR);
+ }
+
+ if ((bytes_read = fread(buf, sizeof(char), sizeof(buf), fp)) == 0) {
+ warn("swap_free: read error");
+ fclose(fp);
+ return smprintf("%s", UNKNOWN_STR);
+ }
+
+ buf[bytes_read] = '\0';
+ fclose(fp);
+
+ if ((match = strstr(buf, "SwapTotal")) == NULL) {
+ return smprintf("%s", UNKNOWN_STR);
+ }
+ sscanf(match, "SwapTotal: %ld kB\n", &total);
+
+ if ((match = strstr(buf, "SwapFree")) == NULL) {
+ return smprintf("%s", UNKNOWN_STR);
+ }
+ sscanf(match, "SwapFree: %ld kB\n", &free);
+
+ return smprintf("%f", (float)free / 1024 / 1024);
+}
+
+static char *
+swap_perc(void)
+{
+ long total, free, cached;
+ FILE *fp;
+ char buf[2048];
+ size_t bytes_read;
+ char *match;
+
+ fp = fopen("/proc/meminfo", "r");
+ if (fp == NULL) {
+ warn("Failed to open file /proc/meminfo");
+ return smprintf("%s", UNKNOWN_STR);
+ }
+
+ if ((bytes_read = fread(buf, sizeof(char), sizeof(buf), fp)) == 0) {
+ warn("swap_perc: read error");
+ fclose(fp);
+ return smprintf("%s", UNKNOWN_STR);
+ }
+
+ buf[bytes_read] = '\0';
+ fclose(fp);
+
+ if ((match = strstr(buf, "SwapTotal")) == NULL) {
+ return smprintf("%s", UNKNOWN_STR);
+ }
+ sscanf(match, "SwapTotal: %ld kB\n", &total);
+
+ if ((match = strstr(buf, "SwapCached")) == NULL) {
+ return smprintf("%s", UNKNOWN_STR);
+ }
+ sscanf(match, "SwapCached: %ld kB\n", &cached);
+
+ if ((match = strstr(buf, "SwapFree")) == NULL) {
+ return smprintf("%s", UNKNOWN_STR);
+ }
+ sscanf(match, "SwapFree: %ld kB\n", &free);
+
+ return smprintf("%d%%", 100 * (total - free - cached) / total);
+}
+
+static char *
+swap_total(void)
+{
+ long total;
+ FILE *fp;
+ char buf[2048];
+ size_t bytes_read;
+ char *match;
+
+ fp = fopen("/proc/meminfo", "r");
+ if (fp == NULL) {
+ warn("Failed to open file /proc/meminfo");
+ return smprintf("%s", UNKNOWN_STR);
+ }
+ if ((bytes_read = fread(buf, sizeof(char), sizeof(buf), fp)) == 0) {
+ warn("swap_total: read error");
+ fclose(fp);
+ return smprintf("%s", UNKNOWN_STR);
+ }
+
+ buf[bytes_read] = '\0';
+ fclose(fp);
+
+ if ((match = strstr(buf, "SwapTotal")) == NULL) {
+ return smprintf("%s", UNKNOWN_STR);
+ }
+ sscanf(match, "SwapTotal: %ld kB\n", &total);
+
+ return smprintf("%f", (float)total / 1024 / 1024);
+}
+
+static char *
+swap_used(void)
+{
+ long total, free, cached;
+ FILE *fp;
+ char buf[2048];
+ size_t bytes_read;
+ char *match;
+
+ fp = fopen("/proc/meminfo", "r");
+ if (fp == NULL) {
+ warn("Failed to open file /proc/meminfo");
+ return smprintf("%s", UNKNOWN_STR);
+ }
+ if ((bytes_read = fread(buf, sizeof(char), sizeof(buf), fp)) == 0) {
+ warn("swap_used: read error");
+ fclose(fp);
+ return smprintf("%s", UNKNOWN_STR);
+ }
+
+ buf[bytes_read] = '\0';
+ fclose(fp);
+
+ if ((match = strstr(buf, "SwapTotal")) == NULL) {
+ return smprintf("%s", UNKNOWN_STR);
+ }
+ sscanf(match, "SwapTotal: %ld kB\n", &total);
+
+ if ((match = strstr(buf, "SwapCached")) == NULL) {
+ return smprintf("%s", UNKNOWN_STR);
+ }
+ sscanf(match, "SwapCached: %ld kB\n", &cached);
+
+ if ((match = strstr(buf, "SwapFree")) == NULL) {
+ return smprintf("%s", UNKNOWN_STR);
+ }
+ sscanf(match, "SwapFree: %ld kB\n", &free);
+
+ return smprintf("%f", (float)(total - free - cached) / 1024 / 1024);
+}
+
+static char *
+temp(const char *file)
+{
+ int temp;
+ FILE *fp;
+
+ fp = fopen(file, "r");
+ if (fp == NULL) {
+ warn("Failed to open file %s", file);
+ return smprintf("%s", UNKNOWN_STR);
+ }
+ fscanf(fp, "%d", &temp);
+ fclose(fp);
+
+ return smprintf("%d°C", temp / 1000);
+}
+
+static char *
+uptime(void)
+{
+ struct sysinfo info;
+ int h = 0;
+ int m = 0;
+
+ sysinfo(&info);
+ h = info.uptime / 3600;
+ m = (info.uptime - h * 3600 ) / 60;
+
+ return smprintf("%dh %dm", h, m);
+}
+
+static char *
+username(void)
+{
+ uid_t uid = geteuid();
+ struct passwd *pw = getpwuid(uid);
+
+ if (pw == NULL) {
+ warn("Failed to get username");
+ return smprintf("%s", UNKNOWN_STR);
+ }
+
+ return smprintf("%s", pw->pw_name);
+}
+
+static char *
+uid(void)
+{
+ return smprintf("%d", geteuid());
+}
+
+
+static char *
+vol_perc(const char *card)
+{
+ unsigned int i;
+ int v, afd, devmask;
+ char *vnames[] = SOUND_DEVICE_NAMES;
+
+ afd = open(card, O_RDONLY);
+ if (afd < 0) {
+ warn("Cannot open %s", card);
+ return smprintf(UNKNOWN_STR);
+ }
+
+ ioctl(afd, MIXER_READ(SOUND_MIXER_DEVMASK), &devmask);
+ for (i = 0; i < (sizeof(vnames) / sizeof((vnames[0]))); i++) {
+ if (devmask & (1 << i)) {
+ if (!strcmp("vol", vnames[i])) {
+ ioctl(afd, MIXER_READ(i), &v);
+ }
+ }
+ }
+
+ close(afd);
+ if (v == 0) {
+ return smprintf("mute");
+ }
+ return smprintf("%d%%", v & 0xff);
+}
+
+static char *
+wifi_perc(const char *iface)
+{
+ int perc;
+ char buf[255];