summaryrefslogtreecommitdiff
path: root/src/examples/edbus/banshee.c
blob: 8223139e8ed343592256df96de3e83723f2fb108 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include "EDBus.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 EDBus_Signal_Handler *state_changed2;

static Eina_Bool
_timeout_application(void *data EINA_UNUSED)
{
   printf("\n## ecore_main_loop_quit()\n");
   ecore_main_loop_quit();
   return EINA_TRUE;
}

static void
on_get_playlists(void *data EINA_UNUSED, const EDBus_Message *msg, EDBus_Pending *pending EINA_UNUSED)
{
   EDBus_Message_Iter *array, *struct_entry;
   const char *path, *name, *image;
   int i = 0;

   EINA_SAFETY_ON_TRUE_RETURN(edbus_message_error_get(msg, NULL, NULL));

   if (!edbus_message_arguments_get(msg, "a(oss)", &array))
     {
        fprintf(stderr, "Error: could not get entry contents\n");
        return;
     }
   printf("on_get_playlists() \n\n");
   while (edbus_message_iter_get_and_next(array, 'r', &struct_entry))
     {
        if (!edbus_message_iter_arguments_get(struct_entry, "oss", &path, &name, &image))
          {
             printf("error on edbus_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, EDBus_Message_Iter *var)
{
   const char *skey = key;

   if (!strcmp(skey, "PlaylistCount"))
     {
        unsigned count;
        if (!edbus_message_iter_arguments_get(var, "u", &count))
          printf("error2\n");
        printf("PlaylistCount=%d\n", count);
     }
   else if (!strcmp(skey, "Orderings"))
     {
        EDBus_Message_Iter *as;
        const char *txt;
        printf("- Orderings\n");
        if (!edbus_message_iter_arguments_get(var, "as", &as))
          printf("error1\n");
        while (edbus_message_iter_get_and_next(as, 's', &txt))
          printf("\t%s\n", txt);
     }
}

static void
playlist_get_all_cb(void *data EINA_UNUSED, const EDBus_Message *msg, EDBus_Pending *pending EINA_UNUSED)
{
   EDBus_Message_Iter *array;
   EINA_SAFETY_ON_TRUE_RETURN(edbus_message_error_get(msg, NULL, NULL));

   if (edbus_message_arguments_get(msg, "a{sv}", &array))
     edbus_message_iter_dict_iterate(array, "sv", iterate_dict, NULL);
}

static void
on_introspect(void *data EINA_UNUSED, const EDBus_Message *msg, EDBus_Pending *pending EINA_UNUSED)
{
   const char *string;

   EINA_SAFETY_ON_TRUE_RETURN(edbus_message_error_get(msg, NULL, NULL));

   if (!edbus_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 EDBus_Message *msg, EDBus_Pending *pending EINA_UNUSED)
{
   const char *status = data;

   EINA_SAFETY_ON_TRUE_RETURN(edbus_message_error_get(msg, NULL, NULL));

   printf("%s\n", status);
}

static void
on_state_changed(void *data EINA_UNUSED, const EDBus_Message *msg)
{
   const char *status;
   EINA_SAFETY_ON_TRUE_RETURN(edbus_message_error_get(msg, NULL, NULL));

   if (!edbus_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 EDBus_Message *msg)
{
   const char *status;
   EINA_SAFETY_ON_TRUE_RETURN(edbus_message_error_get(msg, NULL, NULL));

   if (!edbus_message_arguments_get(msg, "s", &status))
     {
        fprintf(stderr, "Error: could not get entry contents\n");
        return;
     }

   printf("on_state_changed2 = %s\n", status);
   edbus_signal_handler_unref(state_changed2);
   state_changed2 = NULL;
}

static void
on_banshee_startup(void *data EINA_UNUSED, const EDBus_Message *msg)
{
   const char *bus, *older_id, *new_id;

   EINA_SAFETY_ON_TRUE_RETURN(edbus_message_error_get(msg, NULL, NULL));
   if (!edbus_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 EDBus_Message *msg)
{
   const char *bus, *older_id, *new_id;

   EINA_SAFETY_ON_TRUE_RETURN(edbus_message_error_get(msg, NULL, NULL));
   if (!edbus_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)
{
   EDBus_Connection *conn;
   EDBus_Object *engine_obj, *controller_obj, *mpris_obj;
   EDBus_Proxy *engine, *controler, *playlists;
   EDBus_Signal_Handler *sh;

   ecore_init();
   edbus_init();

   conn = edbus_connection_get(EDBUS_CONNECTION_TYPE_SESSION);

   engine_obj = edbus_object_get(conn, BUS, ENGINE_PATH);
   controller_obj = edbus_object_get(conn, BUS, CONTROLLER_PATH);
   mpris_obj = edbus_object_get(conn, BUS, MPRIS_PATH);

   engine = edbus_proxy_get(engine_obj, ENGINE_IFACE);
   EINA_SAFETY_ON_NULL_GOTO(engine, end);
   controler = edbus_proxy_get(controller_obj, CONTROLLER_IFACE);
   EINA_SAFETY_ON_NULL_GOTO(controler, end);
   playlists = edbus_proxy_get(mpris_obj, MPRIS_IFACE);
   EINA_SAFETY_ON_NULL_GOTO(playlists, end);

   edbus_object_introspect(engine_obj, on_introspect, NULL);

   edbus_proxy_signal_handler_add(engine, "StateChanged", on_state_changed, NULL);
   edbus_proxy_call(engine, "Pause", on_next_or_pause, "Pause", -1, "");

   edbus_proxy_call(controler, "Next", on_next_or_pause, "Next", -1, "b", EINA_TRUE);

   edbus_proxy_property_get_all(playlists, playlist_get_all_cb, NULL);
   edbus_proxy_call(playlists, "GetPlaylists", on_get_playlists, NULL, -1,
                    "uusb", (unsigned)0, (unsigned)30, "asc", EINA_FALSE);

   edbus_signal_handler_add(conn, BUS, ENGINE_PATH, ENGINE_IFACE,
                            "StateChanged", on_state_changed, NULL);
   state_changed2 = edbus_signal_handler_add(conn, BUS, ENGINE_PATH, ENGINE_IFACE,
                                             "StateChanged", on_state_changed2, NULL);

   sh = edbus_signal_handler_add(conn, EDBUS_FDO_BUS, EDBUS_FDO_PATH,
                                 EDBUS_FDO_INTERFACE, "NameOwnerChanged",
                                 on_name_owner_changed, NULL);
   edbus_signal_handler_match_extra_set(sh, "arg0", BUS, NULL);

   sh = edbus_signal_handler_add(conn, EDBUS_FDO_BUS, EDBUS_FDO_PATH,
                                 EDBUS_FDO_INTERFACE, "NameOwnerChanged",
                                 on_banshee_startup, NULL);
   edbus_signal_handler_match_extra_set(sh, "arg0", BUS, "arg1", "", NULL);

   ecore_timer_add(50, _timeout_application, NULL);

   ecore_main_loop_begin();

end:
   /**
    *  It's not necessary unref all objecs, proxys and signal handlers
    *  When a parent have ref = 0, it will unref all your childrens
    *  before free it self.
    **/
   edbus_connection_unref(conn);

   edbus_shutdown();
   ecore_shutdown();
   return 0;
}