diff --git a/Makefile b/Makefile index 945b5e3..ece9dfc 100644 --- a/Makefile +++ b/Makefile @@ -17,10 +17,12 @@ COM =\ components/keyboard_indicators\ components/keymap\ components/load_avg\ + components/mpd\ components/netspeeds\ components/num_files\ components/ram\ components/run_command\ + components/status2d\ components/swap\ components/temperature\ components/uptime\ diff --git a/components/mpd.c b/components/mpd.c new file mode 100644 index 0000000..3686ce1 --- /dev/null +++ b/components/mpd.c @@ -0,0 +1,53 @@ +#include +#include +#include +#include +#include + +#include "../util.h" + +/* add to config.mk : +# mpd ++ MPDLIB = -lmpdclient ++ MPDFLAG = -DMPD + +- LIBS = -L/usr/lib -lc -L${X11LIB} -lX11 ++ LIBS = -L/usr/lib -lc -L${X11LIB} -lX11 ${MPDLIB} + +- CPPFLAGS = -DVERSION=\"${VERSION}\" ++ CPPFLAGS = ${MPDFLAG} -DVERSION=\"${VERSION}\" +*/ +/* simple function to retrieve mpd status */ +const char * +mpd_song() { + int ret = -1; + struct mpd_song * song = NULL; + struct mpd_connection * conn ; + if (!(conn = mpd_connection_new("localhost", 0, 30000)) || + mpd_connection_get_error(conn)){ + return NULL; + } + + 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) && (mpd_status_get_state(theStatus) == MPD_STATE_PLAY)) { + mpd_response_next(conn); + song = mpd_recv_song(conn); + ret = esnprintf(buf, sizeof(buf), "%s", mpd_song_get_tag(song, MPD_TAG_TITLE, 0)); + if (!strncmp(buf, "(null)", sizeof("(null)") - 1)) + ret = esnprintf(buf, sizeof(buf), "%s", basename(mpd_song_get_uri(song))); + /* + artist = smprintf("%s",mpd_song_get_tag(song, MPD_TAG_ARTIST, 0)); + elapsed = mpd_status_get_elapsed_time(theStatus); + total = mpd_status_get_total_time(theStatus); + */ + mpd_song_free(song); + } + mpd_response_finish(conn); + mpd_connection_free(conn); + return (ret < 0) ? NULL : buf; +} diff --git a/components/status2d.c b/components/status2d.c new file mode 100644 index 0000000..d2a8ad0 --- /dev/null +++ b/components/status2d.c @@ -0,0 +1,127 @@ +/* See LICENSE file for copyright and license details. */ +#include +#include +#include +#include +#include +#include +#include + +#include "../util.h" + +#if defined(__linux__) + #include + #include + + const char *battery_state(const char *); + + const char * + s2d_battery_perc(const char *bat) + { + int perc; + int c; + char path[PATH_MAX]; + const char *state; + const char *colors[] = { + "^c#ff0000^", + "^c#ff8800^", + "^c#ddff00^", + "^c#00ff00^", + "^c#00ff00^", /* Just in case the battery report a charge over 100% */ + "^c#0000ff^", /* Full charge color */ + "^c#888888^" + }; + const char *elicon = "^c#000000^^r7,6,9,3^^r10,9,9,3^^c#ffffff^^r8,7,7,1^^r13,8,1,1^^r12,9,1,1^^r11,10,7,1^"; + const char *bat_icon = "^r0,7,2,4^^r2,4,22,10^^c#000000^^r3,5,20,8^%s^r%d,5,%d,8^%s^d^^f24^"; + + snprintf(path, sizeof(path), "%s%s%s", "/sys/class/power_supply/", bat, "/capacity"); + if (pscanf(path, "%i", &perc) != 1) + return NULL; + + state = battery_state(bat); + if (!strcmp(state, "?")) + c = 5; + else + c = perc / 25; + + return (bprintf(bat_icon, colors[c], 3 + 20 - perc/5, perc/5, strcmp(state,"+") ? "" : elicon)); + } + + const char * + s2d_datetime(const char *fmt) + { + time_t t; + int day, month, hour, min, sec; + const char *bin[2] = {"^c#000000^", "^c#ff0000^"}; + char clock[] = "^c#888888^^r0,1,62,6^^r0,8,62,4^^r0,13,62,4^^c#ff0000^^r1,9,%d,2^^r1,14,%d,2^%s^r2,2,10,4^%s^r14,2,10,4^%s^r26,2,10,4^%s^r38,2,10,4^%s^r50,2,10,4^^f62^^d^"; + + fmt = "%d/%m %T"; + + t = time(NULL); + if (strftime(buf, sizeof(buf), fmt, localtime(&t)) == 0) + return NULL; + + if (sscanf(buf, "%2d/%2d %2d:%2d:%2d", &day, &month, &hour, &min, &sec) != 5) + return NULL; + + return bprintf(clock, min, sec, bin[hour >> 4 & 1], bin[hour >> 3 & 1], bin[hour >> 2 & 1], bin[hour >> 1 & 1], bin[hour & 1]); + } + + const char * + s2d_wifi_perc(const char *iface) + { + int i, cur; + char *p, *datastart; + char path[PATH_MAX]; + char status[5]; + FILE *fp; + const char *colors[5] = { + "^c#ff0000^", + "^c#ff8800^", + "^c#ddff00^", + "^c#00ff00^", + "^c#00ff00^", /* Just in case the value is over 100% */ + }; + const char *defc = "^c#888888^"; + const char *wifi_icon = "%s^r0,11,2,3^%s^r3,8,2,6^%s^r6,5,2,9^%s^r9,2,2,12^^f12^^d^"; + + if (esnprintf(path, sizeof(path), "/sys/class/net/%s/operstate", iface) < 0) { + return NULL; + } + if (!(fp = fopen(path, "r"))) { + warn("fopen '%s':", path); + return NULL; + } + p = fgets(status, 5, fp); + fclose(fp); + if (!p || strcmp(status, "up\n") != 0) { + return NULL; + } + + if (!(fp = fopen("/proc/net/wireless", "r"))) { + warn("fopen '/proc/net/wireless':"); + return NULL; + } + + for (i = 0; i < 3; i++) { + if (!(p = fgets(buf, sizeof(buf) - 1, fp))) + break; + } + fclose(fp); + if (i < 2 || !p) { + return NULL; + } + + if (!(datastart = strstr(buf, iface))) { + return NULL; + } + + datastart = (datastart+(strlen(iface)+1)); + sscanf(datastart + 1, " %*d %d %*d %*d\t\t %*d\t " + "%*d\t\t%*d\t\t %*d\t %*d\t\t %*d", &cur); + + /* 70 is the max of /proc/net/wireless */ + cur = (int)((float)cur / 70 * 100); + return bprintf(wifi_icon, colors[cur/25], cur > 25 ? "" : defc, cur > 50 ? "" : defc, cur > 75 ? "" : defc); + } +#endif diff --git a/config.mk b/config.mk index 9d26bc3..fb40a80 100644 --- a/config.mk +++ b/config.mk @@ -10,12 +10,15 @@ MANPREFIX = $(PREFIX)/share/man X11INC = /usr/X11R6/include X11LIB = /usr/X11R6/lib +MPDLIB = -lmpdclient +MPDFLAG = -DMPD + # flags -CPPFLAGS = -I$(X11INC) -D_DEFAULT_SOURCE +CPPFLAGS = $(MPDFLAG) -I$(X11INC) -D_DEFAULT_SOURCE CFLAGS = -std=c99 -pedantic -Wall -Wextra -Os LDFLAGS = -L$(X11LIB) -s # OpenBSD: add -lossaudio -LDLIBS = -lX11 +LDLIBS = -lX11 $(MPDLIB) # compiler and linker CC = cc diff --git a/slstatus.h b/slstatus.h index 08f610a..71c5f0f 100644 --- a/slstatus.h +++ b/slstatus.h @@ -40,6 +40,9 @@ const char *keymap(void); /* load_avg */ const char *load_avg(void); +/* mpd */ +const char *mpd_song(void); + /* netspeeds */ const char *netspeed_rx(const char *interface); const char *netspeed_tx(const char *interface); @@ -56,6 +59,10 @@ const char *ram_used(void); /* run_command */ const char *run_command(const char *cmd); +/* status2d */ +const char *s2d_battery_perc(const char *bat); +const char *s2d_wifi_perc(const char *iface); + /* swap */ const char *swap_free(void); const char *swap_perc(void);