Last active
September 25, 2016 11:30
-
-
Save univrsal/b019b8fa4caaccb8e225f215b6f3ea77 to your computer and use it in GitHub Desktop.
C program querying the currently playing song in mpd and setting it to the dwm bar (root window title)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <mpd/client.h> | |
#include <mpd/stats.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <signal.h> | |
#include <stdio.h> | |
#include <unistd.h> | |
#include <string.h> | |
static volatile int runFlag = 1; | |
void stop(int d) { runFlag = 0; } | |
char* append(char* a, char* b) | |
{ | |
char* out; | |
if((out = (char *)malloc(strlen(a) + strlen(b) + 1)) != NULL) | |
{ | |
strcpy(out, a); | |
strcat(out, b); | |
} | |
else | |
{ | |
printf("Not enough memory to concat strings!"); | |
exit(-1); | |
} | |
return out; | |
} | |
int main(int argc, char const *argv[]) | |
{ | |
// Vars | |
int debug = argc > 1; | |
unsigned time = 0; | |
struct mpd_connection *conn; | |
struct mpd_status *status; | |
struct mpd_song *song; | |
enum mpd_state state; | |
char *song_name; | |
char *artist_name; | |
char *cmd; | |
char *base_cmd = "xsetroot -name \""; | |
char *cmd_end = "\""; | |
char *space = " - "; | |
char *paused = " [Paused]"; | |
// Listen for CTRL + C | |
signal(SIGINT, stop); | |
// Connect to local MPD | |
conn = mpd_connection_new(NULL, 0, 0); | |
if (conn && debug) | |
printf("Connected!\n"); | |
else if (!conn) | |
{ | |
if (debug) printf("Connection failure!\n"); | |
return 0; | |
} | |
// Get song | |
while (runFlag) | |
{ | |
status = mpd_run_status(conn); | |
time = mpd_status_get_elapsed_time(status); | |
song = mpd_run_current_song(conn); | |
state = mpd_status_get_state(status); | |
if (!song || !status) | |
{ | |
if (!song && debug) printf("Couldn't recieve current song!\n"); | |
if (!status && debug) printf("Couldn't recieve status of MPD!\n"); | |
return 0; | |
} | |
// Get song info | |
song_name = (char *) mpd_song_get_tag(song, MPD_TAG_TITLE, 0); | |
artist_name = (char *) mpd_song_get_tag(song, MPD_TAG_ARTIST, 0); | |
switch (state) | |
{ | |
case 0: | |
case 1: | |
cmd = "xsetroot -name dwm-6.1"; // Stopped/Unknown status | |
break; | |
case 2: | |
cmd = append(base_cmd, artist_name); // Playing | |
cmd = append(cmd, space); | |
cmd = append(cmd, song_name); | |
cmd = append(cmd, cmd_end); | |
break; | |
case 3: | |
cmd = append(base_cmd, artist_name); // Paused | |
cmd = append(cmd, space); | |
cmd = append(cmd, song_name); | |
cmd = append(cmd, paused); | |
cmd = append(cmd, cmd_end); | |
break; | |
} | |
system(cmd); | |
sleep(1); | |
} | |
printf("Disconnecting...\n"); | |
system("xsetroot -name dwm-6.1"); | |
// Close Connection & free resources | |
mpd_status_free(status); | |
mpd_connection_free(conn); | |
mpd_song_free(song); | |
song_name = NULL; | |
artist_name = NULL; | |
paused = NULL; | |
space = NULL; | |
cmd = NULL; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment