Skip to content

Instantly share code, notes, and snippets.

@grawity
Created October 23, 2015 07:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save grawity/5cfba06c70addcbcabfe to your computer and use it in GitHub Desktop.
Save grawity/5cfba06c70addcbcabfe to your computer and use it in GitHub Desktop.
#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>
int main(void) {
GError *error = NULL;
GDBusConnection *bus;
GVariant *result, *props;
gchar **artists = NULL, *artist = NULL, *title = NULL;
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
"org.mpris.MediaPlayer2.mpd",
// 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
@selurvedu
Copy link

Cool, thanks. The best thing here is how to build it:
make -f mpris-now-playing.c
:)

@selurvedu
Copy link

I've posted a modified version of it. It supports selecting a player name via a command line argument.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment