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

Commit

Permalink
huge cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Unia committed Apr 18, 2013
1 parent fa5e497 commit a2d8c2e
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 118 deletions.
24 changes: 14 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
PROG = dwmst
PREFIX ?= /usr/local
PROG = dwmst
CC = gcc
PREFIX ?= /usr/local
BINPREFIX = ${PREFIX}/bin

# MPD, comment if you don't want it
#MPDLIB = -lmpdclient
Expand All @@ -9,24 +11,26 @@ PREFIX ?= /usr/local
AUDLIB = `pkg-config --cflags --libs glib-2.0 dbus-glib-1 audclient`
AUDFLAG = -DAUD

# Clock, comment if you don't want it
#CLKFLAG = -DCLK

LIBS = -liw -lasound -lX11 -lcanberra ${MPDLIB} ${AUDLIB}
CPPFLAGS = ${MPDFLAG} ${AUDFLAG} ${CLKFLAG}
CFLAGS = -Os -Wall -Wextra -pedantic -Wno-format-zero-length -Wno-unused-parameter -Wno-unused-result ${CPPFLAGS}
CFLAGS = -Os -Wall -Wextra ${CPPFLAGS}

${PROG}: ${PROG}.c ${PROG}.h
@${CC} ${CFLAGS} ${LIBS} -o ${PROG} ${PROG}.c
@strip ${PROG}

clean:
rm -f ${PROG}
debug: CFLAGS += -O0 -g -pedantic
debug: ${PROG}

install:
install -Dm755 ${PROG} ${DESTDIR}${PREFIX}/bin/${PROG}
install -Dm755 ${PROG} ${DESTDIR}${BINPREFIX}/${PROG}
install -Dm644 ${PROG}.service ${DESTDIR}/usr/lib/systemd/system/${PROG}.service

uninstall:
rm -f ${PREFIX}/bin/${PROG}
rm -f ${BINPREFIX}/${PROG}
rm -f /usr/lib/systemd/system/${PROG}.service

clean:
rm -f ${PROG}

.PHONY: all debug clean install uninstall
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ ToDo
----
* See if there's a better way to implement wired connection checks
* Re-check event sounds
* Code left to clean up:
* MPD;
* Alsa;
173 changes: 83 additions & 90 deletions dwmst.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,73 +7,73 @@ int main() {
exit(1);
}
root = XRootWindow(dpy, DefaultScreen(dpy));
winfo = (struct wireless_info *) calloc(1, sizeof(struct wireless_info));
ca_context_create(&sound);
#ifdef CLK
setlocale(LC_ALL, "");
#ifdef MPD
conn = mpd_connection_new(NULL, 0, 30000);
#endif
#ifdef AUD
g_type_init();
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);
#endif
winfo = (struct wireless_info *) calloc(1, sizeof(struct wireless_info));
memset(winfo, 0, sizeof(struct wireless_info));
skfd = iw_sockets_open();
/* MAIN LOOP STARTS HERE */
for (;;) {
status[0]='\0';
/* MUSIC */
#ifdef MPD
if (++musicloops > 10) {
musicloops = 0;
struct mpd_connection *conn = mpd_connection_new(NULL, 0, 30000);
if (mpd_connection_get_error(conn))
sprintf(musicstring, NO_MPD_STR);
mpd_command_list_begin(conn, true);
mpd_send_status(conn);
mpd_send_current_song(conn);
mpd_command_list_end(conn);
struct mpd_status* theStatus = mpd_recv_status(conn);
if (!theStatus)
sprintf(musicstring, NO_MPD_STR);
else {
if (mpd_status_get_state(theStatus) == MPD_STATE_PLAY) {
mpd_response_next(conn);
song = mpd_recv_song(conn);
title = mpd_song_get_tag(song, MPD_TAG_TITLE, 0);
artist = mpd_song_get_tag(song, MPD_TAG_ARTIST, 0);
sprintf(musicstring, MPD_STR, title, artist);
mpd_song_free(song);
} else if (mpd_status_get_state(theStatus) == MPD_STATE_PAUSE) {
mpd_response_next(conn);
song = mpd_recv_song(conn);
title = mpd_song_get_tag(song, MPD_TAG_TITLE, 0);
artist = mpd_song_get_tag(song, MPD_TAG_ARTIST, 0);
sprintf(musicstring, MPD_P_STR, title, artist);
mpd_song_free(song);
} else if (mpd_status_get_state(theStatus) == MPD_STATE_STOP)
sprintf(musicstring, MPD_S_STR);
}
mpd_status = mpd_recv_status(conn);
if (!mpd_status)
sprintf(musicstring, NO_MPD_STR);
else {
if (mpd_status_get_state(mpd_status) == MPD_STATE_PLAY) {
mpd_response_next(conn);
song = mpd_recv_song(conn);
title = mpd_song_get_tag(song, MPD_TAG_TITLE, 0);
artist = mpd_song_get_tag(song, MPD_TAG_ARTIST, 0);
sprintf(musicstring, MPD_STR, title, artist);
mpd_song_free(song);
free(title);
free(artist);
} else if (mpd_status_get_state(mpd_status) == MPD_STATE_PAUSE) {
mpd_response_next(conn);
song = mpd_recv_song(conn);
title = mpd_song_get_tag(song, MPD_TAG_TITLE, 0);
artist = mpd_song_get_tag(song, MPD_TAG_ARTIST, 0);
sprintf(musicstring, MPD_P_STR, title, artist);
mpd_song_free(song);
free(title);
free(artist);
} else if (mpd_status_get_state(mpd_status) == MPD_STATE_STOP)
sprintf(musicstring, MPD_S_STR);
}
mpd_response_finish(conn);
mpd_connection_free(conn);
}
strcat(status, musicstring);
#endif
#ifdef AUD
if (++musicloops > 10) {
musicloops = 0;
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);
playpos = audacious_remote_get_playlist_pos(session);
psong = audacious_remote_get_playlist_title(session, playpos);
psong = audacious_remote_get_playlist_title(session, audacious_remote_get_playlist_pos(session));
if (psong) {
if (audacious_remote_is_paused(session)) {
sprintf(musicstring, MUSIC_P_STR, psong);
free(psong);
} else if (audacious_remote_is_playing(session)) {
sprintf(musicstring, MUSIC_STR, psong);
free(psong);
} else
sprintf(musicstring, MUSIC_S_STR);
} else
sprintf(musicstring, MUSIC_S_STR);
g_object_unref(session);
}
}
}
strcat(status, musicstring);
#endif
Expand All @@ -82,17 +82,15 @@ int main() {
if (infile) {
sprintf(statnext, SKYPE_STR);
fclose(infile);
} else
sprintf(statnext, NO_SKYPE_STR);
strcat(status, statnext);
strcat(status, statnext);
}
/* NET */
if (++netloops > 60) {
netloops = 0;
if(fopen(NET_FILE, "r")) {
if (fopen(NET_FILE, "r")) {
fclose(infile);
sprintf(netstring, LAN_STR);
} else {
skfd = iw_sockets_open();
if (iw_get_basic_config(skfd, WLAN, &(winfo->b)) > -1) {
if (iw_get_stats(skfd, WLAN, &(winfo->stats), &winfo->range, winfo->has_range) >= 0)
winfo->has_stats = 1;
Expand All @@ -102,11 +100,9 @@ int main() {
if (winfo->b.essid_on)
sprintf(netstring, WLAN_STR, winfo->b.essid, (winfo->stats.qual.qual * 100) / winfo->range.max_qual.qual);
else
sprintf(netstring, NO_WLAN_STR);
sprintf(netstring, NO_CON_STR);
}
}
iw_sockets_close(skfd);
memset(winfo, 0, sizeof(struct wireless_info));
}
}
strcat(status, netstring);
Expand All @@ -116,81 +112,70 @@ int main() {
snd_mixer_selem_register(handle, NULL, NULL);
snd_mixer_load(handle);
snd_mixer_selem_id_malloc(&vol_info);
snd_mixer_selem_id_set_name(vol_info, VOL_CH);
pcm_mixer = snd_mixer_find_selem(handle, vol_info);
snd_mixer_selem_get_playback_volume_range(pcm_mixer, &min, &max); /* get volume */
snd_mixer_selem_get_playback_volume(pcm_mixer, SND_MIXER_SCHN_MONO, &vol);
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_switch(mas_mixer, SND_MIXER_SCHN_MONO, &mute); /* get mute state */
if(mute == 0)
snd_mixer_selem_get_playback_volume_range(pcm_mixer, &min, &max);
snd_mixer_selem_get_playback_volume(pcm_mixer, SND_MIXER_SCHN_MONO, &vol);
snd_mixer_selem_get_playback_switch(mas_mixer, SND_MIXER_SCHN_MONO, &mute);
if (!mute)
sprintf(statnext, VOL_MUTE_STR);
else {
realvol = (vol * 100) / max;
sprintf(statnext, VOL_STR, realvol);
if(oldvol > 0 && oldvol != realvol)
if (oldvol > 0 && oldvol != realvol)
ca_context_play(sound, 0, CA_PROP_APPLICATION_NAME, "Dwmst", CA_PROP_EVENT_ID, "audio-volume-change", CA_PROP_CANBERRA_CACHE_CONTROL, "never", NULL);
oldvol = realvol;
}
if(vol_info)
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);
strcat(status,statnext);
/* Power / Battery */
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_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 / 1000) * ((float)now / 1000);
full = ((float)voltage / 1000) * ((float)full / 1000);
rate = ((float)voltage / 1000) * ((float)rate / 1000);
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_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)
sprintf(statnext, BAT_FULL_STR, perc);
else if(strncmp(state, "Unknown", 8) == 0)
sprintf(statnext, BAT_FULL_STR);
else if (strncmp(state, "Unknown", 8) == 0)
sprintf(statnext, BAT_UNK_STR);
else if (strncmp(state, "Charging", 8) == 0) {
seconds = 3600 * (((float)full - (float)now) / (float)rate);
hours = seconds / 3600;
seconds -= 3600 * hours;
minutes = seconds / 60;
seconds -= 60 * minutes;
sprintf(statnext, BAT_CHRG_STR, perc, hours, minutes);
warning = 0;
} else {
seconds = 3600 * ((float)now / (float)rate);
else {
if (strncmp(state, "Charging", 8) == 0)
seconds = 3600 * (((float)full - (float)now) / (float)rate);
else
seconds = 3600 * ((float)now / (float)rate);
hours = seconds / 3600;
seconds -= 3600 * hours;
minutes = seconds / 60;
seconds -= 60 * minutes;
if (perc < BAT_LOW_P || minutes < BAT_LOW_T) {
sprintf(statnext, BAT_LOW_STR, perc, hours, minutes);
if(!warning) {
ca_context_play(sound, 0, CA_PROP_APPLICATION_NAME, "Dwmst", CA_PROP_EVENT_ID, "suspend-error", CA_PROP_CANBERRA_CACHE_CONTROL, "never", NULL);
warning = 1;
}
} else {
sprintf(statnext, BAT_STR, perc, hours, minutes);
if (strncmp(state, "Charging", 8) == 0) {
sprintf(statnext, BAT_CHRG_STR, perc, hours, minutes);
warning = 0;
} else {
if (perc < BAT_LOW_P || minutes < BAT_LOW_T) {
sprintf(statnext, BAT_LOW_STR, perc, hours, minutes);
if (!warning) {
ca_context_play(sound, 0, CA_PROP_APPLICATION_NAME, "Dwmst", CA_PROP_EVENT_ID, "suspend-error", CA_PROP_CANBERRA_CACHE_CONTROL, "never", NULL);
warning = 1;
}
} else {
sprintf(statnext, BAT_STR, perc, hours, minutes);
warning = 0;
}
}
}
hours = minutes = seconds = 0;
strcat(status, statnext);
#ifdef CLK
time(&current);
strftime(statnext, 38, DATE_TIME_STR, localtime(&current));
strcat(status, statnext);
#endif
/* Set root name */
XStoreName(dpy, root, status);
XFlush(dpy);
Expand All @@ -199,5 +184,13 @@ int main() {
/* NEXT LINES SHOULD NEVER EXECUTE, only here to satisfy Trilby's O.C.D. ;) */
XCloseDisplay(dpy);
ca_context_destroy(sound);
#ifdef MPD
mpd_connection_free(conn);
#endif
#ifdef AUD
dbus_g_connection_unref(connection);
g_object_unref(session);
#endif
iw_sockets_close(skfd);
return 0;
}
Loading

0 comments on commit a2d8c2e

Please sign in to comment.