Add status2d and mpd components

This commit is contained in:
lhark 2018-07-13 01:05:48 -04:00
parent dd7f1896aa
commit 9c0243a9a9
5 changed files with 194 additions and 2 deletions

View file

@ -17,10 +17,12 @@ COM =\
components/keyboard_indicators\ components/keyboard_indicators\
components/keymap\ components/keymap\
components/load_avg\ components/load_avg\
components/mpd\
components/netspeeds\ components/netspeeds\
components/num_files\ components/num_files\
components/ram\ components/ram\
components/run_command\ components/run_command\
components/status2d\
components/swap\ components/swap\
components/temperature\ components/temperature\
components/uptime\ components/uptime\

53
components/mpd.c Normal file
View file

@ -0,0 +1,53 @@
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mpd/client.h>
#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;
}

127
components/status2d.c Normal file
View file

@ -0,0 +1,127 @@
/* See LICENSE file for copyright and license details. */
#include <ifaddrs.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <time.h>
#include <unistd.h>
#include "../util.h"
#if defined(__linux__)
#include <limits.h>
#include <linux/wireless.h>
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

View file

@ -10,12 +10,15 @@ MANPREFIX = $(PREFIX)/share/man
X11INC = /usr/X11R6/include X11INC = /usr/X11R6/include
X11LIB = /usr/X11R6/lib X11LIB = /usr/X11R6/lib
MPDLIB = -lmpdclient
MPDFLAG = -DMPD
# flags # flags
CPPFLAGS = -I$(X11INC) -D_DEFAULT_SOURCE CPPFLAGS = $(MPDFLAG) -I$(X11INC) -D_DEFAULT_SOURCE
CFLAGS = -std=c99 -pedantic -Wall -Wextra -Os CFLAGS = -std=c99 -pedantic -Wall -Wextra -Os
LDFLAGS = -L$(X11LIB) -s LDFLAGS = -L$(X11LIB) -s
# OpenBSD: add -lossaudio # OpenBSD: add -lossaudio
LDLIBS = -lX11 LDLIBS = -lX11 $(MPDLIB)
# compiler and linker # compiler and linker
CC = cc CC = cc

View file

@ -40,6 +40,9 @@ const char *keymap(void);
/* load_avg */ /* load_avg */
const char *load_avg(void); const char *load_avg(void);
/* mpd */
const char *mpd_song(void);
/* netspeeds */ /* netspeeds */
const char *netspeed_rx(const char *interface); const char *netspeed_rx(const char *interface);
const char *netspeed_tx(const char *interface); const char *netspeed_tx(const char *interface);
@ -56,6 +59,10 @@ const char *ram_used(void);
/* run_command */ /* run_command */
const char *run_command(const char *cmd); 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 */ /* swap */
const char *swap_free(void); const char *swap_free(void);
const char *swap_perc(void); const char *swap_perc(void);