Skip to content

Instantly share code, notes, and snippets.

@alunux
Last active August 29, 2017 22:36
Show Gist options
  • Save alunux/7b8c18b7fbdbc06afc7a7bfe3313d4a0 to your computer and use it in GitHub Desktop.
Save alunux/7b8c18b7fbdbc06afc7a7bfe3313d4a0 to your computer and use it in GitHub Desktop.
It will control spotify playback. Compile: gcc -Wall `pkg-config --cflags --libs gio-2.0` test-mediakeys.c -o test-mediakeys
/*
* Copyright (C) 2017 La Ode Muh. Fadlun Akbar <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <gio/gio.h>
static void
on_signal(GDBusProxy* proxy,
__attribute__((unused)) gchar* sender_name,
__attribute__((unused)) gchar* signal_name,
GVariant* parameters,
__attribute__((unused)) gpointer user_data)
{
GDBusProxy* player_proxy = NULL;
GVariant* player_dbus_ret = NULL;
GError* error = NULL;
gchar* player_control = NULL;
player_proxy =
g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SESSION,
G_DBUS_PROXY_FLAGS_NONE,
NULL,
"org.mpris.MediaPlayer2.spotify",
"/org/mpris/MediaPlayer2",
"org.mpris.MediaPlayer2.Player",
NULL,
&error);
/* https://specifications.freedesktop.org/mpris-spec/latest/Player_Interface.html
Next () → nothing
Previous () → nothing
Pause () → nothing
PlayPause() → nothing
Stop () → nothing
Play () → nothing
*/
g_variant_get(parameters, "(ss)", NULL, &player_control);
if (g_strcmp0(player_control, "Play") == 0) {
player_dbus_ret = g_dbus_proxy_call_sync(player_proxy,
"PlayPause",
NULL,
G_DBUS_CALL_FLAGS_NONE,
-1,
NULL,
&error);
} else {
player_dbus_ret = g_dbus_proxy_call_sync(player_proxy,
player_control,
NULL,
G_DBUS_CALL_FLAGS_NONE,
-1,
NULL,
&error);
}
if (player_dbus_ret != NULL) {
g_variant_unref(player_dbus_ret);
}
g_free(player_control);
}
int
main(int argc, char* argv[])
{
GMainLoop* loop = NULL;
GDBusProxy* proxy = NULL;
GError* error = NULL;
loop = g_main_loop_new(NULL, FALSE);
proxy = g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SESSION,
G_DBUS_PROXY_FLAGS_NONE,
NULL,
"org.gnome.SettingsDaemon",
"/org/gnome/SettingsDaemon/MediaKeys",
"org.gnome.SettingsDaemon.MediaKeys",
NULL,
&error);
g_signal_connect(proxy, "g-signal", G_CALLBACK(on_signal), NULL);
g_dbus_proxy_call(proxy,
"GrabMediaPlayerKeys",
g_variant_new("(su)", "ExampleMediaEvent", 0),
G_DBUS_CALL_FLAGS_NO_AUTO_START,
-1,
NULL,
NULL,
NULL);
g_main_loop_run(loop);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment