- 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);
+ long total;
+ FILE *fp = fopen("/proc/meminfo", "r");
+
+ if (fp == NULL) {
+ fprintf(stderr, "Error opening meminfo file: %s\n",
+ strerror(errno));
+ return smprintf(UNKNOWN_STR);
+ }
+
+ fscanf(fp, "MemTotal: %ld kB\n", &total);
+ fclose(fp);
+ return smprintf("%f", (float)total / 1024 / 1024);
+}
+
+static char *
+ram_used(void)
+{
+ long free, total, buffers, cached, used;
+ FILE *fp = fopen("/proc/meminfo", "r");
+
+ if (fp == NULL) {
+ fprintf(stderr, "Error opening meminfo file: %s\n",
+ strerror(errno));
+ return smprintf(UNKNOWN_STR);
+ }
+
+ fscanf(fp, "MemTotal: %ld kB\n", &total);
+ fscanf(fp, "MemFree: %ld kB\n", &free);
+ fscanf(fp, "MemAvailable: %ld kB\nBuffers: %ld kB\n", &buffers, &buffers);
+ fscanf(fp, "Cached: %ld kB\n", &cached);
+
+ fclose(fp);
+ used = total - free - buffers - cached;
+ return smprintf("%f", (float)used / 1024 / 1024);
+}
+
+static char *
+run_command(const char* command)
+{
+ int good;
+ FILE *fp = popen(command, "r");
+ char buffer[64];
+
+ if (fp == NULL) {
+ fprintf(stderr, "Could not get command output for: %s: %s\n",
+ command, strerror(errno));
+ return smprintf(UNKNOWN_STR);
+ }
+
+ fgets(buffer, sizeof(buffer)-1, fp);
+ pclose(fp);
+ for (int i = 0 ; i != sizeof(buffer); i++) {
+ if (buffer[i] == '\0') {
+ good = 1;
+ break;
+ }
+ }
+ if (good)
+ buffer[strlen(buffer)-1] = '\0';
+ return smprintf("%s", buffer);
+}
+
+static char *
+temp(const char *file)
+{
+ int temperature;
+ FILE *fp = fopen(file, "r");
+
+ if (fp == NULL) {
+ fprintf(stderr, "Could not open temperature file: %s\n",
+ strerror(errno));
+ return smprintf(UNKNOWN_STR);
+ }
+
+ fscanf(fp, "%d", &temperature);
+ fclose(fp);
+ return smprintf("%d°C", temperature / 1000);
+}
+
+static char *
+uptime(void)
+{
+ struct sysinfo info;
+ int hours = 0;
+ int minutes = 0;
+
+ sysinfo(&info);
+ hours = info.uptime / 3600;
+ minutes = (info.uptime - hours * 3600 ) / 60;
+
+ return smprintf("%dh %dm", hours, minutes);