banshee.c

Access Banshee music player and send commands to it.

Access Banshee music player and send commands to it.

//Compile with:
// gcc -o banshee banshee.c `pkg-config --cflags --libs eldbus ecore eina`
#include "Eldbus.h"
#include <Ecore.h>
#define BUS "org.bansheeproject.Banshee"
#define ENGINE_PATH "/org/bansheeproject/Banshee/PlayerEngine"
#define CONTROLLER_PATH "/org/bansheeproject/Banshee/PlaybackController"
#define MPRIS_PATH "/org/mpris/MediaPlayer2"
#define ENGINE_IFACE "org.bansheeproject.Banshee.PlayerEngine"
#define CONTROLLER_IFACE "org.bansheeproject.Banshee.PlaybackController"
#define MPRIS_IFACE "org.mpris.MediaPlayer2.Playlists"
static Eldbus_Signal_Handler *state_changed2;
static Eina_Bool
_timeout_application(void *data EINA_UNUSED)
{
printf("\n## ecore_main_loop_quit()\n");
return EINA_TRUE;
}
static void
on_get_playlists(void *data EINA_UNUSED, const Eldbus_Message *msg, Eldbus_Pending *pending EINA_UNUSED)
{
Eldbus_Message_Iter *array, *struct_entry;
const char *path, *name, *image;
int i = 0;
if (!eldbus_message_arguments_get(msg, "a(oss)", &array))
{
fprintf(stderr, "Error: could not get entry contents\n");
return;
}
printf("on_get_playlists() \n\n");
while (eldbus_message_iter_get_and_next(array, 'r', &struct_entry))
{
if (!eldbus_message_iter_arguments_get(struct_entry, "oss", &path, &name, &image))
{
printf("error on eldbus_massage_iterator_arguments_get()");
return;
}
i++;
printf("%d - %s | %s | %s\n", i, path, name, image);
}
printf("end of on_get_playlists()\n\n");
}
static void
iterate_dict(void *data EINA_UNUSED, const void *key, Eldbus_Message_Iter *var)
{
const char *skey = key;
if (!strcmp(skey, "PlaylistCount"))
{
unsigned count;
if (!eldbus_message_iter_arguments_get(var, "u", &count))
printf("error2\n");
printf("PlaylistCount=%u\n", count);
}
else if (!strcmp(skey, "Orderings"))
{
const char *txt;
printf("- Orderings\n");
if (!eldbus_message_iter_arguments_get(var, "as", &as))
printf("error1\n");
while (eldbus_message_iter_get_and_next(as, 's', &txt))
printf("\t%s\n", txt);
}
}
static void
playlist_get_all_cb(void *data EINA_UNUSED, const Eldbus_Message *msg, Eldbus_Pending *pending EINA_UNUSED)
{
if (eldbus_message_arguments_get(msg, "a{sv}", &array))
eldbus_message_iter_dict_iterate(array, "sv", iterate_dict, NULL);
}
static void
on_introspect(void *data EINA_UNUSED, const Eldbus_Message *msg, Eldbus_Pending *pending EINA_UNUSED)
{
const char *string;
if (!eldbus_message_arguments_get(msg, "s", &string))
{
fprintf(stderr, "Error: could not get entry contents\n");
return;
}
printf("on_introspect() data=\n%s\n\n", string);
}
static void
on_next_or_pause(void *data, const Eldbus_Message *msg, Eldbus_Pending *pending EINA_UNUSED)
{
const char *status = data;
printf("%s\n", status);
}
static void
on_state_changed(void *data EINA_UNUSED, const Eldbus_Message *msg)
{
const char *status;
if (!eldbus_message_arguments_get(msg, "s", &status))
{
fprintf(stderr, "Error: could not get entry contents\n");
return;
}
printf("on_state_changed = %s\n", status);
}
static void
on_state_changed2(void *data EINA_UNUSED, const Eldbus_Message *msg)
{
const char *status;
if (!eldbus_message_arguments_get(msg, "s", &status))
{
fprintf(stderr, "Error: could not get entry contents\n");
return;
}
printf("on_state_changed2 = %s\n", status);
eldbus_signal_handler_unref(state_changed2);
state_changed2 = NULL;
}
static void
on_banshee_startup(void *data EINA_UNUSED, const Eldbus_Message *msg)
{
const char *bus, *older_id, *new_id;
if (!eldbus_message_arguments_get(msg, "sss", &bus, &older_id, &new_id))
{
printf("Error getting arguments from NameOwnerChanged");
return;
}
printf("banshee started on id=%s\n", new_id);
}
static void
on_name_owner_changed(void *data EINA_UNUSED, const Eldbus_Message *msg)
{
const char *bus, *older_id, *new_id;
if (!eldbus_message_arguments_get(msg, "sss", &bus, &older_id, &new_id))
{
printf("Error getting arguments from NameOwnerChanged");
return;
}
printf("bus = %s older=%s new=%s\n\n", bus, older_id, new_id);
}
int
main(void)
{
Eldbus_Object *engine_obj, *controller_obj, *mpris_obj;
Eldbus_Proxy *engine, *controler, *playlists;
conn = eldbus_connection_get(ELDBUS_CONNECTION_TYPE_SESSION);
engine_obj = eldbus_object_get(conn, BUS, ENGINE_PATH);
controller_obj = eldbus_object_get(conn, BUS, CONTROLLER_PATH);
mpris_obj = eldbus_object_get(conn, BUS, MPRIS_PATH);
engine = eldbus_proxy_get(engine_obj, ENGINE_IFACE);
controler = eldbus_proxy_get(controller_obj, CONTROLLER_IFACE);
EINA_SAFETY_ON_NULL_GOTO(controler, end);
playlists = eldbus_proxy_get(mpris_obj, MPRIS_IFACE);
EINA_SAFETY_ON_NULL_GOTO(playlists, end);
eldbus_object_introspect(engine_obj, on_introspect, NULL);
eldbus_proxy_signal_handler_add(engine, "StateChanged", on_state_changed, NULL);
eldbus_proxy_call(engine, "Pause", on_next_or_pause, "Pause", -1, "");
eldbus_proxy_call(controler, "Next", on_next_or_pause, "Next", -1, "b", EINA_TRUE);
eldbus_proxy_property_get_all(playlists, playlist_get_all_cb, NULL);
eldbus_proxy_call(playlists, "GetPlaylists", on_get_playlists, NULL, -1,
"uusb", (unsigned)0, (unsigned)30, "asc", EINA_FALSE);
eldbus_signal_handler_add(conn, BUS, ENGINE_PATH, ENGINE_IFACE,
"StateChanged", on_state_changed, NULL);
state_changed2 = eldbus_signal_handler_add(conn, BUS, ENGINE_PATH, ENGINE_IFACE,
"StateChanged", on_state_changed2, NULL);
ELDBUS_FDO_INTERFACE, "NameOwnerChanged",
on_name_owner_changed, NULL);
eldbus_signal_handler_match_extra_set(sh, "arg0", BUS, NULL);
ELDBUS_FDO_INTERFACE, "NameOwnerChanged",
on_banshee_startup, NULL);
eldbus_signal_handler_match_extra_set(sh, "arg0", BUS, "arg1", "", NULL);
ecore_timer_add(50, _timeout_application, NULL);
end:
return 0;
}
EAPI int ecore_shutdown(void)
Shuts down connections, signal handlers sockets etc.
Definition: ecore.c:371
EAPI int ecore_init(void)
Sets up connections, signal handlers, sockets etc.
Definition: ecore.c:230
void ecore_main_loop_quit(void)
Quits the main loop once all the events currently on the queue have been processed.
Definition: ecore_main.c:1321
void ecore_main_loop_begin(void)
Runs the application main loop.
Definition: ecore_main.c:1311
Ecore_Timer * ecore_timer_add(double in, Ecore_Task_Cb func, const void *data)
Creates a timer to call the given function in the given period of time.
Definition: ecore_timer.c:189
#define EINA_SAFETY_ON_NULL_GOTO(exp, label)
The macro doesn't do anything unless EINA_SAFETY_CHECKS is defined.
Definition: eina_safety_checks.h:243
#define EINA_SAFETY_ON_TRUE_RETURN(exp)
The macro doesn't do anything unless EINA_SAFETY_CHECKS is defined.
Definition: eina_safety_checks.h:251
#define EINA_TRUE
boolean value TRUE (numerical value 1)
Definition: eina_types.h:539
#define EINA_FALSE
boolean value FALSE (numerical value 0)
Definition: eina_types.h:533
unsigned char Eina_Bool
Type to mimic a boolean.
Definition: eina_types.h:527
#define EINA_UNUSED
Used to indicate that a function parameter is purposely unused.
Definition: eina_types.h:339
void eldbus_connection_unref(Eldbus_Connection *conn)
Decrement connection reference count.
Definition: eldbus_core.c:1306
Eldbus_Connection * eldbus_connection_get(Eldbus_Connection_Type type)
Establish a connection to bus and integrate it with the ecore main loop.
Definition: eldbus_core.c:1102
struct _Eldbus_Message_Iter Eldbus_Message_Iter
Represents an iterator over a complex message type (array, dict, struct, or variant).
Definition: Eldbus.h:181
struct _Eldbus_Signal_Handler Eldbus_Signal_Handler
Represents a listener that will listen for signals emitted by other applications.
Definition: Eldbus.h:196
struct _Eldbus_Message Eldbus_Message
Represents the way data is sent and received in DBus.
Definition: Eldbus.h:173
EAPI int eldbus_shutdown(void)
Shutdown eldbus.
Definition: eldbus_core.c:246
#define ELDBUS_FDO_PATH
Object that implements message bus interface.
Definition: Eldbus.h:126
struct _Eldbus_Pending Eldbus_Pending
Represents a message that has been sent but has not yet reached its destination.
Definition: Eldbus.h:188
#define ELDBUS_FDO_BUS
DBus message bus name.
Definition: Eldbus.h:125
EAPI int eldbus_init(void)
Initialize eldbus.
Definition: eldbus_core.c:128
#define ELDBUS_FDO_INTERFACE
DBus message bus interface.
Definition: Eldbus.h:127
Eldbus_Pending * eldbus_object_introspect(Eldbus_Object *obj, Eldbus_Message_Cb cb, const void *data)
Call the method "Introspect" on the eldbus object.
Definition: eldbus_object.c:676
Eldbus_Pending * eldbus_proxy_property_get_all(Eldbus_Proxy *proxy, Eldbus_Message_Cb cb, const void *data)
Get all properties.
Definition: eldbus_proxy.c:772
void eldbus_message_iter_dict_iterate(Eldbus_Message_Iter *array, const char *signature, Eldbus_Dict_Cb_Get cb, const void *data)
Iterate over a dictionary.
Definition: eldbus_message_helper.c:5
Eina_Bool eldbus_message_iter_get_and_next(Eldbus_Message_Iter *iter, char signature,...)
Useful when iterating over arrays.
Definition: eldbus_message.c:736
Eina_Bool eldbus_message_iter_arguments_get(Eldbus_Message_Iter *iter, const char *signature,...)
Get data from Eldbus_Message_Iter, for each complete type must have a pointer to store his value,...
Definition: eldbus_message.c:842
Eina_Bool eldbus_message_error_get(const Eldbus_Message *msg, const char **name, const char **text)
Get the error text and name from a Eldbus_Message.
Definition: eldbus_message.c:233
Eina_Bool eldbus_message_arguments_get(const Eldbus_Message *msg, const char *signature,...)
Get the arguments from an Eldbus_Message.
Definition: eldbus_message.c:274
Eldbus_Object * eldbus_object_get(Eldbus_Connection *conn, const char *bus, const char *path)
Get an object of the given bus and path.
Definition: eldbus_object.c:188
Eldbus_Signal_Handler * eldbus_proxy_signal_handler_add(Eldbus_Proxy *proxy, const char *member, Eldbus_Signal_Cb cb, const void *cb_data)
Add a signal handler.
Definition: eldbus_proxy.c:658
Eldbus_Pending * eldbus_proxy_call(Eldbus_Proxy *proxy, const char *member, Eldbus_Message_Cb cb, const void *cb_data, double timeout, const char *signature,...)
Call a method in proxy.
Definition: eldbus_proxy.c:621
Eldbus_Proxy * eldbus_proxy_get(Eldbus_Object *obj, const char *interface)
Get a proxy of the following interface name in a Eldbus_Object.
Definition: eldbus_proxy.c:208
Eina_Bool eldbus_signal_handler_match_extra_set(Eldbus_Signal_Handler *sh,...)
Add extra argument in match of signal handler to obtain specifics signals.
Definition: eldbus_signal_handler.c:124
void eldbus_signal_handler_unref(Eldbus_Signal_Handler *handler)
Decrease signal handler reference.
Definition: eldbus_signal_handler.c:272
Eldbus_Signal_Handler * eldbus_signal_handler_add(Eldbus_Connection *conn, const char *sender, const char *path, const char *interface, const char *member, Eldbus_Signal_Cb cb, const void *cb_data)
Add a signal handler.
Definition: eldbus_signal_handler.c:153
struct _Eldbus_Connection Eldbus_Connection
Represents a connection of one the type of connection with the DBus daemon.
Definition: Eldbus.h:227
struct _Eldbus_Proxy Eldbus_Proxy
Represents a client object bound to an interface.
Definition: Eldbus.h:221
struct _Eldbus_Object Eldbus_Object
Represents an object path already attached with bus name or unique id.
Definition: Eldbus.h:233