Last active
April 18, 2018 22:07
-
-
Save melvyniandrag/3eb43c5f1af97392b75a4aae02b8330c to your computer and use it in GitHub Desktop.
Able to detect signals.
This file contains hidden or 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
/* | |
g++ cppdbus.cpp -I/usr/include/glib-2.0 \ | |
-I/usr/lib/x86_64-linux-gnu/glib-2.0/include/ \ | |
-lglib-2.0 -lgio-2.0 -lgobject-2.0 | |
This small program can pause/play media in vlc. Then, if the | |
tracklist is modified, the program will exit. | |
This is the dbus-monitor output corresponding to adding a track to a playlist: | |
signal time=1524079919.626002 sender=:1.97 -> destination=(null destination) \ | |
serial=37 path=/org/mpris/MediaPlayer2; \ | |
interface=org.freedesktop.DBus.Properties; \ | |
member=PropertiesChanged | |
string "org.mpris.MediaPlayer2.TrackList" | |
array [ | |
] | |
array [ | |
string "Tracks" | |
] | |
But seemingly the same output is shown in dbus-monitor if you | |
remove a track from the playlist: | |
signal time=1524079988.735333 sender=:1.97 -> destination=(null destination) \ | |
serial=38 path=/org/mpris/MediaPlayer2; \ | |
interface=org.freedesktop.DBus.Properties; \ | |
member=PropertiesChanged | |
string "org.mpris.MediaPlayer2.TrackList" | |
array [ | |
] | |
array [ | |
string "Tracks" | |
] | |
Is there a way to distinguish? | |
*/ | |
#include <iostream> | |
#include <gio/gio.h> | |
static GMainLoop* loop = NULL; | |
static void handle_proxy_call_sync_result( GVariant* result ) | |
{ | |
const gchar* str; | |
g_assert (result != NULL); | |
g_assert_cmpstr (g_variant_get_type_string (result), ==, "(s)"); | |
g_variant_get (result, "(&s)", &str); | |
std::cout << str << std::endl; | |
g_variant_unref (result); | |
} | |
static void | |
myCallback (GDBusConnection* connection, | |
const gchar* sender_name, | |
const gchar* object_path, | |
const gchar* interface_name, | |
const gchar* signal_name, | |
GVariant* parameters, | |
gpointer user_data) { | |
g_message ("Works for me."); | |
g_main_loop_quit (loop); | |
} | |
static void | |
neverCalled (GDBusConnection* connection, | |
const gchar* sender_name, | |
const gchar* object_path, | |
const gchar* interface_name, | |
const gchar* signal_name, | |
GVariant* parameters, | |
gpointer user_data) { | |
g_message ("U Can't C Me."); | |
g_main_loop_quit (loop); | |
} | |
static void | |
handleIntrospection( GObject* source_object, | |
GAsyncResult* result, | |
gpointer user_data ) | |
{ | |
const char* data = (char*) user_data; | |
std::cout << "Printing data in the GAsyncResult function:" << data << std::endl; | |
} | |
int main() | |
{ | |
g_autoptr( GDBusConnection ) conn = NULL; | |
g_autoptr( GError ) error = NULL; | |
conn = g_bus_get_sync( G_BUS_TYPE_SESSION, NULL, &error ); | |
g_assert_no_error( error ); | |
if (conn == NULL) | |
std::cout << "Error!" <<std::endl; //assert(), cerr, w/e. | |
g_autoptr( GError ) error2 = NULL; | |
g_autoptr( GDBusProxy ) proxy = NULL; | |
proxy = g_dbus_proxy_new_sync( conn, | |
G_DBUS_PROXY_FLAGS_NONE, | |
NULL, | |
"org.mpris.MediaPlayer2.vlc", | |
"/org/mpris/MediaPlayer2", | |
"org.mpris.MediaPlayer2.Player", | |
NULL, | |
&error2 ); | |
g_assert_no_error( error2 ); | |
if( proxy == NULL ) | |
std::cout << "Error!!" << std::endl; | |
g_dbus_proxy_call( proxy, | |
"Play", // "Pause" | |
NULL, | |
G_DBUS_CALL_FLAGS_NONE, | |
-1, | |
NULL, | |
NULL, | |
NULL ); | |
g_autoptr( GError ) error3 = NULL; | |
g_autoptr( GDBusProxy ) proxy2 = NULL; | |
proxy2 = g_dbus_proxy_new_sync( conn, | |
G_DBUS_PROXY_FLAGS_NONE, | |
NULL, | |
"org.mpris.MediaPlayer2.vlc", | |
"/org/mpris/MediaPlayer2", | |
"org.freedesktop.DBus.Introspectable", | |
NULL, | |
&error3 ); | |
g_assert_no_error( error3 ); | |
if( proxy2 == NULL ) | |
std::cout << "Error!!" << std::endl; | |
GVariant* ret = NULL; | |
g_autoptr( GError ) error4 = NULL; | |
ret = g_dbus_proxy_call_sync( proxy2, | |
"Introspect", | |
NULL, | |
G_DBUS_CALL_FLAGS_NONE, | |
-1, | |
NULL, | |
&error4 ); | |
g_assert_no_error( error4 ); | |
// <sorcery> | |
const gchar* str; | |
g_assert (ret != NULL); | |
g_assert_cmpstr (g_variant_get_type_string (ret), ==, "(s)"); | |
g_variant_get (ret, "(&s)", &str); | |
std::cout << str << std::endl; | |
g_variant_unref (ret); | |
//</sorcery> | |
char cData[4] = {'a', 'b', 'c', '\0'}; | |
gpointer data = (void*) cData; | |
std::cout << (char*) data << std::endl; | |
g_dbus_proxy_call( proxy2, | |
"Introspect", | |
NULL, | |
G_DBUS_CALL_FLAGS_NONE, | |
-1, | |
NULL, | |
(GAsyncReadyCallback) handleIntrospection, | |
data ); | |
//handle_proxy_call_sync_result( ret ); | |
guint doesNothing = g_dbus_connection_signal_subscribe( conn, | |
"org.mpris.MediaPlayer2.vlc", | |
"org.mpris.MediaPlayer2.Tracklist", | |
"TrackAdded", | |
"/org/mpris/MediaPlayer2", | |
NULL, | |
G_DBUS_SIGNAL_FLAGS_NONE, | |
(GDBusSignalCallback) neverCalled, | |
NULL, | |
NULL ); | |
guint sigId = g_dbus_connection_signal_subscribe( conn, | |
"org.mpris.MediaPlayer2.vlc", | |
"org.freedesktop.DBus.Properties", | |
"PropertiesChanged", | |
"/org/mpris/MediaPlayer2", | |
"org.mpris.MediaPlayer2.TrackList", //Filter out the signals by string | |
G_DBUS_SIGNAL_FLAGS_NONE, | |
(GDBusSignalCallback) myCallback, | |
NULL, | |
NULL ); | |
loop = g_main_loop_new (NULL, FALSE); | |
g_main_loop_run (loop); | |
g_main_loop_unref (loop); | |
g_object_unref (conn); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment