mpd status

This commit is contained in:
lhark 2024-03-18 23:55:03 +01:00
parent 9c0243a9a9
commit d591c04416
4 changed files with 70 additions and 24 deletions

View file

@ -20,8 +20,12 @@
/* simple function to retrieve mpd status */
const char *
mpd_song() {
const int titlethres = 79;
int ret = -1;
struct mpd_song * song = NULL;
const char *title;
const char *artist;
char *uri;
struct mpd_connection * conn ;
if (!(conn = mpd_connection_new("localhost", 0, 30000)) ||
mpd_connection_get_error(conn)){
@ -37,9 +41,22 @@ mpd_song() {
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)));
title = mpd_song_get_tag(song, MPD_TAG_TITLE, 0);
artist = mpd_song_get_tag(song, MPD_TAG_ARTIST, 0);
if (!title) {
uri = strdup(mpd_song_get_uri(song));
/* Don't apply basename() to URLs */
if (!strncmp(uri, "https://", strlen("https://"))) {
ret = esnprintf(buf, sizeof(buf), "%s", uri);
} else {
ret = esnprintf(buf, sizeof(buf), "%s", basename(uri));
}
free(uri);
} else if (!artist) {
ret = esnprintf(buf, sizeof(buf), "%s", title);
} else {
ret = esnprintf(buf, sizeof(buf), "%s - %s", artist, title);
}
/*
artist = smprintf("%s",mpd_song_get_tag(song, MPD_TAG_ARTIST, 0));
elapsed = mpd_status_get_elapsed_time(theStatus);
@ -49,5 +66,14 @@ mpd_song() {
}
mpd_response_finish(conn);
mpd_connection_free(conn);
if (ret < 0) {
ret = esnprintf(buf, sizeof(buf), "▮▮");
} else if (ret >= titlethres && titlethres + 3 < sizeof(buf)) {
/* Crop overly long titles */
buf[titlethres] = buf[titlethres+1] = buf[titlethres+2] = '.';
buf[titlethres+3] = '\0';
}
return (ret < 0) ? NULL : buf;
}

View file

@ -13,6 +13,14 @@
#include <limits.h>
#include <linux/wireless.h>
static const char col_blue[] = "#458588";
static const char col_dark[] = "#3c3836";
static const char col_gray[] = "#928374";
static const char col_green[] = "#b8bb26";
static const char col_orange[] = "#fe8019";
static const char col_red[] = "#fb4934";
static const char col_yellow[] = "#fabd2f";
const char *battery_state(const char *);
const char *
@ -23,28 +31,33 @@
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^"
col_red,
col_orange,
col_yellow,
col_green,
col_green, /* Just in case the battery report a charge over 100% */
col_blue, /* Full charge color */
col_gray
};
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^";
const char *elicon = "^c#3c3836^^r8,7,7,1^^r13,8,1,1^^r12,9,1,1^^r11,10,7,1^";
const char *bat_icon = "^c%s^^r0,0,36,19^^f6^^c%s^^r0,7,2,4^^r2,3,21,12^^c%s^^r4,5,17,8^^c%s^^r%d,6,%d,6^%s^d^^f30^";
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, "?"))
if (perc > 100) {
perc = 100;
c = 6;
} else if (!strcmp(state, "?")) {
c = 5;
else
} else {
c = perc / 25;
}
return (bprintf(bat_icon, colors[c], 3 + 20 - perc/5, perc/5, strcmp(state,"+") ? "" : elicon));
return (bprintf(bat_icon, colors[c], col_dark, colors[c], col_dark,
4 + 16 - perc * 16 / 100, perc * 16 / 100, strcmp(state,"+") ? "" : elicon));
}
const char *
@ -76,14 +89,20 @@
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% */
col_red,
col_orange,
col_yellow,
col_green,
col_green, /* Just in case the value is over 100% */
};
const char *bars[] = {
"^r0,12,2,3^",
"^r3,9,2,6^",
"^r6,6,2,9^",
"^r9,3,2,12^"
};
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^";
const char *wifi_icon = "^c%s^^r0,0,36,19^^f12^^c%s^%s%s%s%s^f24^^d^";
if (esnprintf(path, sizeof(path), "/sys/class/net/%s/operstate", iface) < 0) {
return NULL;
@ -122,6 +141,6 @@
/* 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);
return bprintf(wifi_icon, colors[cur/25], col_dark, bars[0], cur > 25 ? bars[1] : "", cur > 50 ? bars[2] : "", cur > 75 ? bars[3] : "");
}
#endif

View file

@ -15,8 +15,8 @@ MPDFLAG = -DMPD
# flags
CPPFLAGS = $(MPDFLAG) -I$(X11INC) -D_DEFAULT_SOURCE
CFLAGS = -std=c99 -pedantic -Wall -Wextra -Os
LDFLAGS = -L$(X11LIB) -s
CFLAGS = -g -std=c99 -pedantic -Wall -Wextra
LDFLAGS = -L$(X11LIB)
# OpenBSD: add -lossaudio
LDLIBS = -lX11 $(MPDLIB)

1
util.h
View file

@ -3,6 +3,7 @@
extern char buf[1024];
#define MIN(A, B) ((A) < (B) ? (A) : (B))
#define LEN(x) (sizeof (x) / sizeof *(x))
extern char *argv0;