diff --git a/dwmst.c b/dwmst.c index 60ecbbc..4c4a313 100644 --- a/dwmst.c +++ b/dwmst.c @@ -44,7 +44,7 @@ is_up (char *device) { if(infile != NULL) { fscanf(infile, "%s", state); fclose(infile); - if (strncmp (state, "up", 2) == 0) + if(strncmp(state, "up", 2) == 0) return 1; } return 0; @@ -52,7 +52,7 @@ is_up (char *device) { char * get_aud(DBusGProxy *session) { - char *music = NULL; /*ToDo: free?*/ + char *music = NULL; int pos; pos = audacious_remote_get_playlist_pos(session); @@ -60,20 +60,20 @@ get_aud(DBusGProxy *session) { if(music != NULL) return smprintf(AUD_STR, music); else - return NULL; /*ToDo: fix this*/ + return NULL; } char * get_skype(void) { - if(access (SKYPE_LOCK, F_OK) == 0) + if(access(SKYPE_LOCK, F_OK) == 0) return smprintf("%s", SKYPE_STR); else - return NULL; /*ToDo: fix this*/ + return NULL; } char * get_net(struct iwreq wreq, int socket) { - if(is_up (WIRELESS_DEVICE)) { + if(is_up(WIRELESS_DEVICE)) { char essid[30]; memset(essid, 0, sizeof(essid)); @@ -81,7 +81,7 @@ get_net(struct iwreq wreq, int socket) { wreq.u.essid.length = sizeof(essid); ioctl(socket, SIOCGIWESSID, &wreq); return smprintf(WLAN_STR, essid); - } else if(is_up (WIRED_DEVICE)) + } else if(is_up(WIRED_DEVICE)) return smprintf("%s", LAN_STR); else return smprintf("%s", NO_CON_STR); @@ -89,7 +89,7 @@ get_net(struct iwreq wreq, int socket) { char * get_vol(snd_mixer_t *handle) { - int mute = 0, realvol = 0; + int mute = 0; long vol = 0, max = 0, min = 0; snd_mixer_elem_t *pcm_mixer, *max_mixer; snd_mixer_selem_id_t *vol_info, *mute_info; @@ -108,84 +108,57 @@ get_vol(snd_mixer_t *handle) { snd_mixer_selem_id_free(vol_info); snd_mixer_selem_id_free(mute_info); - realvol = (vol * 100) / max; if(mute == 0) - return smprintf(VOL_MUTE_STR, realvol); - else { - if (realvol < 20) - return smprintf(VOL_LOW_STR, realvol); - else if (realvol > 60) - return smprintf(VOL_HIGH_STR, realvol); - else - return smprintf(VOL_MID_STR, realvol); - } + return smprintf(VOL_MUTE_STR, (vol * 100) / max); + return smprintf(VOL_STR, (vol * 100) / max); } char * -get_batt(void) { /*ToDo: fix many strncmps*/ - FILE *f1, *f2, *f3, *f4; +get_batt(void) { + FILE *f; char state[11]; if(access("/sys/class/power_supply/BAT1/", F_OK) != 0) return smprintf("%s", NO_BAT_STR); memset(state, 0, sizeof(state)); - f1 = fopen(BATT_STAT, "r"); - if(f1 != NULL) { - fscanf(f1, "%s\n", state); - fclose(f1); - } else - return NULL; /*ToDo: fix this*/ + f = fopen(BATT_STAT, "r"); + if(f == NULL) + return NULL; + fscanf(f, "%s\n", state); + fclose(f); if(strncmp(state, "Full", 4) == 0) return smprintf("%s", BAT_FULL_STR); else if(strncmp(state, "Unknown", 7) == 0) return smprintf("%s", BAT_UNK_STR); - else { - long now = -1, full = -1, voltage = -1, rate = -1; - int perc = -1, seconds = -1, minutes, hours; - - f1 = fopen(BATT_NOW, "r"); - f2 = fopen(BATT_FULL, "r"); - f3 = fopen(BATT_VOLT, "r"); - f4 = fopen(BATT_CNOW, "r"); - if(f1 != NULL && f2 != NULL && f3 != NULL && f4 != NULL) { - fscanf(f1, "%ld\n", &now); fclose(f1); - fscanf(f2, "%ld\n", &full); fclose(f2); - fscanf(f3, "%ld\n", &voltage); fclose(f3); - fscanf(f4, "%ld\n", &rate); fclose(f4); - } - - now = voltage * now; - full = voltage * full; - rate = voltage * rate; - perc = ((float)now * 100) / (float)full; - - if(perc > 100) perc = 100; - if(perc < 0) perc = 0; - - if(strncmp(state, "Charging", 8) == 0) - seconds = 3600 * ((full - now) / rate); - else - seconds = 3600 * (now / rate); - hours = seconds / 3600; - seconds -= 3600 * hours; - minutes = seconds / 60; - seconds -= 60 * minutes; - - if(strncmp(state, "Charging", 8) == 0) - return smprintf(BAT_CHRG_STR, perc, hours, minutes); - else { - if(perc < 5) - return smprintf(BAT_EMPTY_STR, perc, hours, minutes); - /*notify*/ - else if(perc > 5 && perc < 50) - return smprintf(BAT_LOW_STR, perc, hours, minutes); - else if(perc > 50 && perc < 95) - return smprintf(BAT_HIGH_STR, perc, hours, minutes); - else - return smprintf(BAT_FULL_STR, perc, hours, minutes); - } + + float now, full, voltage, rate; + unsigned int perc, minutes, hours; + + f = fopen (BATT_NOW, "r"); if(f == NULL) return NULL; fscanf (f, "%f\n", &now); fclose (f); + f = fopen (BATT_FULL, "r"); if(f == NULL) return NULL; fscanf (f, "%f\n", &full); fclose (f); + f = fopen (BATT_VOLT, "r"); if(f == NULL) return NULL; fscanf (f, "%f\n", &voltage); fclose (f); + f = fopen (BATT_CNOW, "r"); if(f == NULL) return NULL; fscanf (f, "%f\n", &rate); fclose (f); + + now *= voltage; + full *= voltage; + rate *= voltage; + perc = (now * 100) / full; + + if(perc > 100) perc = 100; + + if(strncmp(state, "Charging", 8) == 0) { + minutes = 60 * ((full - now) / rate); + hours = minutes / 60; + minutes -= 60 * hours; + return smprintf(BAT_CHRG_STR, perc, hours, minutes); + } else { + minutes = 60 * (now / rate); + hours = minutes / 60; + minutes -= 60 * hours; + /*notify*/ + return smprintf(BAT_DIS_STR, perc, hours, minutes); } } @@ -195,33 +168,14 @@ setstatus(char *str) { XSync(dpy, False); } - -/*char **/ -void -read_stdin(void) { - char inbuf[256]; - char *status; - - memset(inbuf, 0, sizeof(inbuf)); - if(fgets(inbuf, sizeof(inbuf), stdin) != NULL) { - inbuf[strlen(inbuf) - 1] = '\0'; - status = smprintf("%s", inbuf); - setstatus(status); - free(status); - /*return smprintf("%s", inbuf);*/ - sleep(3); - } - /*return NULL;*/ -} - int main(void) { - int sockfd, netloops = 60, musicloops = 60; struct iwreq wreq; snd_mixer_t *handle; - char *status, *pipe, *aud, *skype, *net, *vol, *batt; DBusGProxy *session = NULL; DBusGConnection *conn = NULL; + int sockfd, netloops = 60, musicloops = 60; + char *status, *aud, *skype, *net, *vol, *batt; if(!(dpy = XOpenDisplay(NULL))) { fprintf(stderr, "dwmst: cannot open display.\n"); @@ -232,8 +186,8 @@ main(void) { session = dbus_g_proxy_new_for_name(conn, AUDACIOUS_DBUS_SERVICE, AUDACIOUS_DBUS_PATH, AUDACIOUS_DBUS_INTERFACE); memset(&wreq, 0, sizeof(struct iwreq)); - snprintf(wreq.ifr_name, sizeof(WIRELESS_DEVICE), WIRELESS_DEVICE); /*ToDo: what with multiple devices?*/ - sockfd = socket (AF_INET, SOCK_DGRAM, 0); /*ToDo: what do these mean?*/ + snprintf(wreq.ifr_name, sizeof(WIRELESS_DEVICE), WIRELESS_DEVICE); + sockfd = socket (AF_INET, SOCK_DGRAM, 0); snd_mixer_open(&handle, 0); snd_mixer_attach(handle, "default"); @@ -241,29 +195,19 @@ main(void) { snd_mixer_load(handle); for(;;sleep(INTERVAL)) { - /*pipe = read_stdin(); - if(strlen(pipe) > 2) { - status = smprintf("%s", pipe); - setstatus(status); - free(pipe); - free(status); - sleep(3); - }*/ - /*read_stdin();*/ if(++musicloops > 60 && session != NULL) aud = get_aud(session); skype = get_skype(); - if(++netloops > 60 && sockfd != -1) + if(++netloops > 60 && sockfd > 0) net = get_net(wreq, sockfd); vol = get_vol(handle); batt = get_batt(); - status = smprintf("%s %s %s %s %s", aud, skype, net, vol, batt); setstatus(status); if(++musicloops > 60 && session != NULL) free(aud); free(skype); - if(++netloops > 60 && sockfd != -1) + if(++netloops > 60 && sockfd > 0) free(net); free(vol); free(batt); diff --git a/dwmst.h b/dwmst.h index d9ca604..06fc8ec 100644 --- a/dwmst.h +++ b/dwmst.h @@ -10,25 +10,21 @@ #define BATT_VOLT "/sys/class/power_supply/BAT1/voltage_now" #define BATT_CNOW "/sys/class/power_supply/BAT1/power_now" -#define AUD_STR "\uF240 %s" +#define AUD_STR "%s" #define NO_AUD_STR "" -#define SKYPE_STR "\uF17E" +#define SKYPE_STR "Skype" #define NO_SKYPE_STR "" -#define LAN_STR "\uF3AC Verbonden" -#define WLAN_STR "\uF405 %s" +#define LAN_STR "Verbonden" +#define WLAN_STR "%s" #define NO_CON_STR "Geen verbinding" -#define VOL_HIGH_STR "\uF357 %d%%" -#define VOL_MID_STR "\uF359 %d%%" -#define VOL_LOW_STR "\uF358 %d%%" -#define VOL_MUTE_STR "\uF35A %d%%" +#define VOL_MUTE_STR "M %d%%" +#define VOL_STR "V %d%%" -#define BAT_EMPTY_STR "\uF212 %d%%, %02d:%02d" -#define BAT_LOW_STR "\uF215 %d%%, %02d:%02d" -#define BAT_HIGH_STR "\uF214 %d%%, %02d:%02d" -#define BAT_FULL_STR "\uF213 %d%%, %02d:%02d" -#define BAT_CHRG_STR "\uF211 %d%%, %02d:%02d" -#define BAT_UNK_STR "\uF242 U" -#define NO_BAT_STR "\uF200 G" +#define BAT_FULL_STR "F" +#define BAT_DIS_STR "D %d%%, %02d:%02d" +#define BAT_CHRG_STR "C %d%%, %02d:%02d" +#define BAT_UNK_STR "U" +#define NO_BAT_STR "N"