Skip to content
This repository has been archived by the owner on Jan 5, 2018. It is now read-only.

Commit

Permalink
code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Unia committed Sep 29, 2013
1 parent b69e896 commit 0c951d8
Showing 1 changed file with 44 additions and 50 deletions.
94 changes: 44 additions & 50 deletions dwmst.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include <unistd.h>
#include <string.h>
#include <X11/Xlib.h>
#include <dirent.h>
#include <iwlib.h>
#include <alsa/asoundlib.h>
#include <audacious/dbus.h>
Expand All @@ -16,15 +15,13 @@ char *get_aud(char *buf, DBusGProxy *session) {

psong = audacious_remote_get_playlist_title(session, audacious_remote_get_playlist_pos(session));
if (psong) {
if (audacious_remote_is_paused(session)) {
if (audacious_remote_is_paused(session))
sprintf(buf, AUD_P_STR, psong);
free(psong);
} else if (audacious_remote_is_playing(session)) {
else if (audacious_remote_is_playing(session))
sprintf(buf, AUD_STR, psong);
free(psong);
}
free(psong);
} else
sprintf(buf, AUD_S_STR);
sprintf(buf, AUD_S_STR);
return buf;
}

Expand Down Expand Up @@ -56,74 +53,65 @@ char *get_net(char *buf, int skfd, wireless_info *winfo) {
sprintf(buf, LAN_STR);
else if (is_up(WIRELESS_DEVICE)) {
if (iw_get_basic_config(skfd, WIRELESS_DEVICE, &(winfo->b)) > -1) {
if (winfo->b.has_essid && winfo->b.essid_on) {
if (winfo->b.has_essid && winfo->b.essid_on)
sprintf(buf, WLAN_STR, winfo->b.essid);
}
}
} else
sprintf(buf, NO_CON_STR);
return buf;
}

char *get_volume(char *buf) {
long vol = 0, max = 0, min = 0;
int mute = 0, realvol = 0;

snd_mixer_t *handle;
char *get_volume(char *buf, snd_mixer_t *handle) {
snd_mixer_elem_t *pcm_mixer, *mas_mixer;
snd_mixer_selem_id_t *vol_info, *mute_info;
long vol = 0, max = 0, min = 0;
int mute = 0, realvol = 0;

snd_mixer_open(&handle, 0);
snd_mixer_attach(handle, "default");
snd_mixer_selem_register(handle, NULL, NULL);
snd_mixer_load(handle);
snd_mixer_handle_events(handle);
snd_mixer_selem_id_malloc(&vol_info);
snd_mixer_selem_id_malloc(&mute_info);
snd_mixer_selem_id_set_name(vol_info, VOL_CH);
snd_mixer_selem_id_set_name(mute_info, VOL_CH);
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);
snd_mixer_selem_get_playback_volume((snd_mixer_elem_t *)pcm_mixer, 0, &vol);
snd_mixer_selem_get_playback_switch(mas_mixer, 0, &mute);
if (!mute)
sprintf(buf, VOL_MUTE_STR);
else {
realvol = (vol * 100) / max;
sprintf(buf, VOL_STR, realvol);
}
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);
snd_mixer_selem_id_free(vol_info);
snd_mixer_selem_id_free(mute_info);
return buf;
}

char *get_battery(char *buf) {
DIR *dir;
FILE *infile;
char state[8];
long now = -1, full = -1, voltage = -1, rate = -1;
int perc, hours, minutes, seconds = -1;

dir = opendir("/sys/class/power_supply/BAT1");
if (dir) {
infile = fopen(BATT_NOW, "r"); fscanf(infile, "%ld\n", &now); fclose(infile);
infile = fopen(BATT_FULL, "r"); fscanf(infile, "%ld\n", &full); fclose(infile);
if(access("/sys/class/power_supply/BAT1/", F_OK) == 0) {
infile = fopen(BATT_STAT, "r"); fscanf(infile, "%s\n", state); fclose(infile);
infile = fopen(BATT_VOLT, "r"); fscanf(infile, "%ld\n", &voltage); fclose(infile);
infile = fopen(BATT_CNOW, "r"); fscanf(infile, "%ld\n", &rate); fclose(infile);
now = ((float)voltage * (float)now);
full = ((float)voltage * (float)full);
rate = ((float)voltage * (float)rate);
perc = (now * 100) / full;
if (strncmp(state, "Full", 8) == 0)
if (strncmp(state, "Full", 8) == 0) {
sprintf(buf, BAT_FULL_STR);
else if (strncmp(state, "Unknown", 8) == 0)
return buf;
} else if (strncmp(state, "Unknown", 8) == 0) {
sprintf(buf, BAT_UNK_STR);
else {
return buf;
} else {
infile = fopen(BATT_NOW, "r"); fscanf(infile, "%ld\n", &now); fclose(infile);
infile = fopen(BATT_FULL, "r"); fscanf(infile, "%ld\n", &full); fclose(infile);
infile = fopen(BATT_VOLT, "r"); fscanf(infile, "%ld\n", &voltage); fclose(infile);
infile = fopen(BATT_CNOW, "r"); fscanf(infile, "%ld\n", &rate); fclose(infile);
now = ((float)voltage * (float)now);
full = ((float)voltage * (float)full);
rate = ((float)voltage * (float)rate);
perc = (now * 100) / full;

if (strncmp(state, "Charging", 8) == 0)
seconds = 3600 * (((float)full - (float)now) / (float)rate);
else
Expand All @@ -135,7 +123,7 @@ char *get_battery(char *buf) {
if (strncmp(state, "Charging", 8) == 0)
sprintf(buf, BAT_CHRG_STR, perc, hours, minutes);
else {
if (perc < BAT_LOW_P || minutes < BAT_LOW_T)
/*if (perc < BAT_LOW_P || minutes < BAT_LOW_T)*/
/*notify*/
sprintf(buf, BAT_STR, perc, hours, minutes);
}
Expand All @@ -151,23 +139,31 @@ int main(void) {
Window root;
char status[201], music[100], skype[7], net[30], volume[14], battery[35];
int netloops = 60, musicloops = 10;
int skfd;
struct wireless_info *winfo;
DBusGProxy *session = NULL;
DBusGConnection *connection = NULL;
int skfd;
struct wireless_info *winfo;
snd_mixer_t *handle;

dpy = XOpenDisplay(NULL);
if (dpy == NULL) {
fprintf(stderr, "ERROR: could not open display\n");
return 1;
}

root = XRootWindow(dpy, DefaultScreen(dpy));

connection = dbus_g_bus_get(DBUS_BUS_SESSION, NULL);
session = dbus_g_proxy_new_for_name(connection, AUDACIOUS_DBUS_SERVICE, AUDACIOUS_DBUS_PATH, AUDACIOUS_DBUS_INTERFACE);

winfo = malloc(sizeof(struct wireless_info));
memset(winfo, 0, sizeof(struct wireless_info));
skfd = iw_sockets_open();

connection = dbus_g_bus_get(DBUS_BUS_SESSION, NULL);
session = dbus_g_proxy_new_for_name(connection, AUDACIOUS_DBUS_SERVICE, AUDACIOUS_DBUS_PATH, AUDACIOUS_DBUS_INTERFACE);
snd_mixer_open(&handle, 0);
snd_mixer_attach(handle, "default");
snd_mixer_selem_register(handle, NULL, NULL);
snd_mixer_load(handle);

while(1) {
if (++musicloops > 10) {
Expand All @@ -179,22 +175,20 @@ int main(void) {
netloops = 0;
get_net(net, skfd, winfo);
}
get_volume(volume);
get_volume(volume, handle);
get_battery(battery);

sprintf(status, "%s %s %s %s %s", music, skype, net, volume, battery);
sprintf(status, "%s %s %s %s %s", music, skype, net, volume, battery);

XStoreName(dpy, root, status);
XFlush(dpy);
sleep(INTERVAL);
}

/* NEXT LINES SHOULD NEVER EXECUTE, only here to satisfy Trilby's O.C.D. ;) */
XCloseDisplay(dpy);

dbus_g_connection_unref(connection);
g_object_unref(session);
#endif
iw_sockets_close(skfd);
snd_mixer_close(handle);
return 0;
}

0 comments on commit 0c951d8

Please sign in to comment.