/* See LICENSE file for copyright and license details. */
#include <alsa/asoundlib.h>
-#include <arpa/inet.h>
#include <fcntl.h>
#include <ifaddrs.h>
#include <limits.h>
#include <linux/wireless.h>
-#include <locale.h>
#include <netdb.h>
#include <pwd.h>
#include <stdarg.h>
#include "strlcat.h"
#include "strlcpy.h"
-typedef char *(*op_fun)();
struct arg {
- op_fun func;
+ char *(*func)();
const char *format;
const char *args;
};
-static void setstatus(const char *);
static char *smprintf(const char *, ...);
static char *battery_perc(const char *);
static char *cpu_perc(void);
#include "config.h"
-static void
-setstatus(const char *str)
-{
- /* set WM_NAME via X11 */
- XStoreName(dpy, DefaultRootWindow(dpy), str);
- XSync(dpy, False);
-}
-
static char *
smprintf(const char *fmt, ...)
{
fscanf(fp, "%*s %Lf %Lf %Lf %Lf", &a[0], &a[1], &a[2], &a[3]);
fclose(fp);
- /* wait a second (for avg values) */
sleep(1);
fp = fopen("/proc/stat","r");
FILE *fp = fopen("/proc/sys/kernel/random/entropy_avail", "r");
if (fp == NULL) {
- fprintf(stderr, "Could not open entropy file.\n");
+ fprintf(stderr, "Could not open entropy file: %s\n",
+ strerror(errno));
return smprintf(UNKNOWN_STR);
}
static char *
gid(void)
{
- gid_t gid = getgid();
- return smprintf("%d", gid);
+ return smprintf("%d", getgid());
}
static char *
static char *
username(void)
{
- register struct passwd *pw;
- register uid_t uid;
-
- uid = geteuid();
- pw = getpwuid(uid);
+ uid_t uid = geteuid();
+ struct passwd *pw = getpwuid(uid);
- if (pw)
+ if (pw == NULL)
return smprintf("%s", pw->pw_name);
- else {
- fprintf(stderr, "Could not get username: %s\n",
- strerror(errno));
- return smprintf(UNKNOWN_STR);
- }
+ fprintf(stderr, "Could not get username: %s\n",
+ strerror(errno));
return smprintf(UNKNOWN_STR);
}
static char *
uid(void)
{
- /* FIXME: WHY USE register modifier? */
- register uid_t uid;
-
- uid = geteuid();
-
- if (uid)
- return smprintf("%d", uid);
- else {
- fprintf(stderr, "Could not get uid.\n");
- return smprintf(UNKNOWN_STR);
- }
-
- return smprintf(UNKNOWN_STR);
+ return smprintf("%d", geteuid());
}
-static char *
-vol_perc(const char *soundcard)
-{
- int mute = 0;
- long vol = 0, max = 0, min = 0;
+static char *
+vol_perc(const char *snd_card)
+{ /* thanks to botika for this function */
+ long int vol, max, min;
snd_mixer_t *handle;
- snd_mixer_elem_t *pcm_mixer, *mas_mixer;
- snd_mixer_selem_id_t *vol_info, *mute_info;
+ snd_mixer_elem_t *elem;
+ snd_mixer_selem_id_t *s_elem;
snd_mixer_open(&handle, 0);
- snd_mixer_attach(handle, soundcard);
+ snd_mixer_attach(handle, snd_card);
snd_mixer_selem_register(handle, NULL, NULL);
snd_mixer_load(handle);
+ snd_mixer_selem_id_malloc(&s_elem);
+ snd_mixer_selem_id_set_name(s_elem, "Master");
+ elem = snd_mixer_find_selem(handle, s_elem);
- 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");
+ if (elem == NULL) {
+ snd_mixer_selem_id_free(s_elem);
+ snd_mixer_close(handle);
+ perror("alsa error: ");
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);
+ snd_mixer_handle_events(handle);
+ snd_mixer_selem_get_playback_volume_range(elem, &min, &max);
+ snd_mixer_selem_get_playback_volume(elem, 0, &vol);
+
+ snd_mixer_selem_id_free(s_elem);
+ snd_mixer_close(handle);
+
+ return smprintf("%d", (vol * 100) / max);
}
static char *
char path[64];
char status[5];
char needle[strlen(wificard)+2];
- FILE *fp = fopen(path, "r");
+ 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));
char *res, *element;
struct arg argument;
- dpy = XOpenDisplay(0x0);
- if (!dpy) {
- fprintf(stderr, "Cannot open display!\n");
- exit(1);
- }
+ stderr = stderr;
+ dpy = XOpenDisplay(NULL);
for (;;) {
memset(status_string, 0, sizeof(status_string));
free(res);
free(element);
}
-
- setstatus(status_string);
- sleep(UPDATE_INTERVAL -1);
}
+ XStoreName(dpy, DefaultRootWindow(dpy), status_string);
+ XSync(dpy, False);
XCloseDisplay(dpy);
+
return 0;
}