Skip to content

Instantly share code, notes, and snippets.

@selurvedu
Forked from grawity/mpris-now-playing.c
Last active July 18, 2021 10:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save selurvedu/97d8953bb7cc94c34ef8f0bc80baa242 to your computer and use it in GitHub Desktop.
Save selurvedu/97d8953bb7cc94c34ef8f0bc80baa242 to your computer and use it in GitHub Desktop.
make -f mpris-now-playing.c
#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