-
-
Save selurvedu/97d8953bb7cc94c34ef8f0bc80baa242 to your computer and use it in GitHub Desktop.
make -f mpris-now-playing.c
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
#if 0 | |
pkg = glib-2.0 gio-2.0 | |
src = $(MAKEFILE_LIST) | |
app = $(basename $(src)) | |
CFLAGS = $(shell pkg-config --cflags $(pkg)) -x c | |
LDFLAGS = $(shell pkg-config --libs $(pkg)) | |
$(app): $(src) | |
define source | |
#endif | |
#include <glib.h> | |
#include <glib/gprintf.h> | |
#include <gio/gio.h> | |
#include <string.h> | |
int main(int argc, char *argv[]) { | |
GError *error = NULL; | |
GDBusConnection *bus; | |
GVariant *result, *props; | |
gchar **artists = NULL, *artist = NULL, *title = NULL; | |
gchar *bus_prefix = "org.mpris.MediaPlayer2."; | |
gchar *player = (argv[1]) ? argv[1] : "mpd"; | |
gchar bus_name[strlen(bus_prefix) + strlen(player) + 1]; | |
g_snprintf(bus_name, sizeof(bus_name), "%s%s", bus_prefix, player); | |
bus = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error); | |
if (!bus) { | |
g_warning("Failed to connect to session bus: %s", error->message); | |
g_error_free(error); | |
return 1; | |
} | |
result = g_dbus_connection_call_sync(bus, | |
// bus name | |
bus_name, | |
// object path | |
"/org/mpris/MediaPlayer2", | |
// interface | |
"org.freedesktop.DBus.Properties", | |
// method name | |
"Get", | |
// argument | |
g_variant_new("(ss)", | |
// property interface | |
"org.mpris.MediaPlayer2.Player", | |
// property name | |
"Metadata"), | |
// return value | |
G_VARIANT_TYPE("(v)"), | |
// flags | |
G_DBUS_CALL_FLAGS_NONE, | |
-1, | |
NULL, | |
&error); | |
if (!result) { | |
g_warning("Failed to call Get: %s\n", error->message); | |
g_error_free(error); | |
return 1; | |
} | |
g_variant_get(result, "(v)", &props); | |
g_variant_lookup(props, "xesam:artist", "^a&s", &artists); | |
g_variant_lookup(props, "xesam:title", "s", &title); | |
if (artists) | |
artist = g_strjoinv(", ", artists); | |
else | |
artist = g_strdup("(Unknown Artist)"); | |
if (!title) | |
title = g_strdup("(Unknown Song)"); | |
g_printf("%s – %s\n", artist, title); | |
g_free(artist); | |
g_free(title); | |
return 0; | |
} | |
#if 0 | |
endef | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment