summaryrefslogtreecommitdiff
path: root/src/examples/edbus/complex-types-client-eina-value.c
diff options
context:
space:
mode:
authorGustavo Sverzut Barbieri <barbieri@gmail.com>2012-12-28 17:53:25 +0000
committerGustavo Sverzut Barbieri <barbieri@gmail.com>2012-12-28 17:53:25 +0000
commit331488d1ce3db2d07533ee276dabdc23f153fa61 (patch)
treea091fe83bfcf0c9d8f9f34ebfe44a5347537bff8 /src/examples/edbus/complex-types-client-eina-value.c
parent6800b178dc918633ede220c572e5b99fa0046e7e (diff)
efl: merge edbus (v2).
SVN revision: 81825
Diffstat (limited to 'src/examples/edbus/complex-types-client-eina-value.c')
-rw-r--r--src/examples/edbus/complex-types-client-eina-value.c323
1 files changed, 323 insertions, 0 deletions
diff --git a/src/examples/edbus/complex-types-client-eina-value.c b/src/examples/edbus/complex-types-client-eina-value.c
new file mode 100644
index 0000000000..5f7fa48313
--- /dev/null
+++ b/src/examples/edbus/complex-types-client-eina-value.c
@@ -0,0 +1,323 @@
1#include "EDBus.h"
2#include <Ecore.h>
3
4#define BUS "com.profusion"
5#define PATH "/com/profusion/Test"
6#define IFACE "com.profusion.Test"
7
8#define size_of_array 5
9static const char *array_string[] = {
10 "aaaa", "bbbb", "cccc", "dddd", "eeee"
11};
12
13typedef struct _sub_struct
14{
15 char *txt;
16 int num;
17} sub_struct;
18
19typedef struct _main_struct
20{
21 int size;
22 sub_struct array[];
23} main_struct;
24
25static unsigned int
26_type_offset(unsigned base, unsigned size)
27{
28 unsigned padding;
29 if (!(base % size))
30 return base;
31 padding = abs(base - size);
32 return base + padding;
33}
34
35static void
36_fill_receive_array_of_string_int_with_size(EDBus_Message *msg, int size, const char *array[])
37{
38 Eina_Value *value_struct, *value_array;
39 int i;
40 unsigned offset;
41 Eina_Value_Struct_Member main_members[2];
42 Eina_Value_Struct_Member sub_members[] = {
43 EINA_VALUE_STRUCT_MEMBER(EINA_VALUE_TYPE_STRING, sub_struct, txt),
44 EINA_VALUE_STRUCT_MEMBER(EINA_VALUE_TYPE_INT, sub_struct, num)
45 };
46 Eina_Value_Struct_Desc desc_sub_struct = {
47 EINA_VALUE_STRUCT_DESC_VERSION,
48 NULL, // no special operations
49 sub_members,
50 2,
51 sizeof(sub_struct)
52 };
53 Eina_Value_Struct_Desc desc_struct = {
54 EINA_VALUE_STRUCT_DESC_VERSION,
55 NULL, // no special operations
56 main_members,
57 2,
58 0//will be set below
59 };
60
61 offset = _type_offset(sizeof(int), sizeof(Eina_Value_Array));
62 main_members[0].name = "size";
63 main_members[0].type = EINA_VALUE_TYPE_INT, 0;
64 main_members[0].offset = 0;
65 main_members[1].name = "array";
66 main_members[1].type = EINA_VALUE_TYPE_ARRAY;
67 main_members[1].offset = offset;
68 desc_struct.size = offset + sizeof(Eina_Value_Array);
69
70 value_struct = eina_value_struct_new(&desc_struct);
71 eina_value_struct_set(value_struct, "size", size);
72
73 value_array = eina_value_array_new(EINA_VALUE_TYPE_STRUCT, size);
74 for (i = 0; i < size; i++)
75 {
76 Eina_Value *value_sub_struct = eina_value_struct_new(&desc_sub_struct);
77 Eina_Value_Struct st;
78 eina_value_struct_set(value_sub_struct, "txt", array[i]);
79 eina_value_struct_set(value_sub_struct, "num", i);
80 eina_value_get(value_sub_struct, &st);
81 eina_value_array_append(value_array, st);
82 eina_value_free(value_sub_struct);
83 }
84 eina_value_struct_value_set(value_struct, "array", value_array);
85
86 edbus_message_from_eina_value("ia(si)", msg, value_struct);
87
88 eina_value_free(value_struct);
89 eina_value_free(value_array);
90}
91
92static void
93on_send_array(void *data, const EDBus_Message *msg, EDBus_Pending *pending)
94{
95 Eina_Value *v, array;
96 const char *txt;
97 unsigned i;
98 printf("2 - on_send_array()\n");
99
100 if (edbus_message_error_get(msg, NULL, NULL))
101 {
102 printf("Message error\n\n");
103 return;
104 }
105
106 v = edbus_message_to_eina_value(msg);
107 eina_value_struct_value_get(v, "arg0", &array);
108 for (i = 0; i < eina_value_array_count(&array); i++)
109 {
110 eina_value_array_get(&array, i, &txt);
111 printf("\t%s\n", txt);
112 }
113
114 eina_value_free(v);
115 eina_value_flush(&array);
116}
117
118static void
119on_receive_array_with_size(void *data, const EDBus_Message *msg, EDBus_Pending *pending)
120{
121 const char *errname;
122 const char *errmsg;
123
124 printf("1 - on_receive_array_with_size()\n");
125 if (edbus_message_error_get(msg, &errname, &errmsg))
126 {
127 fprintf(stderr, "Error: %s %s\n", errname, errmsg);
128 }
129}
130
131static void
132on_plus_one(void *data, const EDBus_Message *msg, EDBus_Pending *pending)
133{
134 Eina_Value *v;
135 int num2;
136
137 if (edbus_message_error_get(msg, NULL, NULL))
138 {
139 printf("Message error\n\n");
140 return;
141 }
142
143 v = edbus_message_to_eina_value(msg);
144 eina_value_struct_get(v, "arg0", &num2);
145
146 printf("3 - on_plus_one() %d\n", num2);
147 eina_value_free(v);
148}
149
150static void
151receive_variant_cb(void *data, const EDBus_Message *msg, EDBus_Pending *pending)
152{
153 Eina_Value *v, variant, array;
154 unsigned i;
155
156 printf("4 - receive a variant with an array of strings\n");
157 if (edbus_message_error_get(msg, NULL, NULL))
158 {
159 printf("Message error\n\n");
160 return;
161 }
162
163 v = edbus_message_to_eina_value(msg);
164
165 eina_value_struct_value_get(v, "arg0", &variant);
166 eina_value_struct_value_get(&variant, "arg0", &array);
167 for (i = 0; i < eina_value_array_count(&array); i++)
168 {
169 const char *txt;
170 eina_value_array_get(&array, i, &txt);
171 printf("\t%s\n", txt);
172 }
173
174 eina_value_flush(&array);
175 eina_value_flush(&variant);
176 eina_value_free(v);
177}
178
179static void
180_property_removed(void *data, EDBus_Proxy *proxy, void *event_info)
181{
182 EDBus_Proxy_Event_Property_Removed *event = event_info;
183
184 printf("\nproperty removed: %s", event->name);
185}
186
187static void
188_property_changed(void *data, EDBus_Proxy *proxy, void *event_info)
189{
190 EDBus_Proxy_Event_Property_Changed *event = event_info;
191 const char *name;
192 const Eina_Value *value;
193 printf("\nproperty changed\n");
194
195 name = event->name;
196 value = event->value;
197
198 if (!strcmp(name, "text") || !strcmp(name, "Resp2"))
199 {
200 const char *txt;
201 eina_value_get(value, &txt);
202 printf("[%s] = %s\n", name, txt);
203 }
204 else if (!strcmp(name, "int32"))
205 {
206 int num;
207 eina_value_get(value, &num);
208 printf("[%s] = %d\n", name, num);
209 }
210 else if (!strcmp(name, "st"))
211 {
212 const char *txt;
213 eina_value_struct_get(value, "arg0", &txt);
214 printf("[%s] %s | ", name, txt);
215 eina_value_struct_get(value, "arg1", &txt);
216 printf("%s\n", txt);
217 }
218}
219
220static Eina_Bool
221_read_cache(void *data)
222{
223 EDBus_Proxy *proxy = data;
224 const char *txt;
225 int num;
226 Eina_Value *v;
227
228 v = edbus_proxy_property_local_get(proxy, "text");
229 if (v)
230 {
231 eina_value_get(v, &txt);
232 printf("Read cache: [txt] = %s\n", txt);
233 }
234
235 v = edbus_proxy_property_local_get(proxy, "int32");
236 if (v)
237 {
238 eina_value_get(v, &num);
239 printf("Read cache: [int32] = %d\n", num);
240 }
241
242 v = edbus_proxy_property_local_get(proxy, "st");
243 if (v)
244 {
245 eina_value_struct_get(v, "arg0", &txt);
246 printf("Read cache: [st] %s | ", txt);
247 eina_value_struct_get(v, "arg1", &txt);
248 printf("%s\n", txt);
249 }
250
251 return EINA_FALSE;
252}
253
254static void
255_fill_plus_one(EDBus_Message *msg, int num)
256{
257 Eina_Value *v;
258 Eina_Value_Struct_Member main_members[] = {
259 {"num", EINA_VALUE_TYPE_INT, 0}
260 };
261 Eina_Value_Struct_Desc desc_struct = {
262 EINA_VALUE_STRUCT_DESC_VERSION,
263 NULL, // no special operations
264 main_members,
265 1,
266 sizeof(int)
267 };
268 v = eina_value_struct_new(&desc_struct);
269 eina_value_struct_set(v, "num", num);
270
271 edbus_message_from_eina_value("i", msg, v);
272
273 eina_value_free(v);
274}
275
276int
277main(void)
278{
279 EDBus_Connection *conn;
280 EDBus_Object *obj;
281 EDBus_Proxy *proxy;
282 EDBus_Message *msg;
283
284 ecore_init();
285 edbus_init();
286
287 conn = edbus_connection_get(EDBUS_CONNECTION_TYPE_SESSION);
288 obj = edbus_object_get(conn, BUS, PATH);
289 proxy = edbus_proxy_get(obj, IFACE);
290
291 msg = edbus_proxy_method_call_new(proxy, "ReceiveArrayOfStringIntWithSize");
292 _fill_receive_array_of_string_int_with_size(msg, size_of_array, array_string);
293 edbus_proxy_send(proxy, msg, on_receive_array_with_size, NULL, -1);
294 edbus_message_unref(msg);
295
296 edbus_proxy_call(proxy, "SendArray", on_send_array, NULL, -1 , "");
297
298 msg = edbus_proxy_method_call_new(proxy, "PlusOne");
299 _fill_plus_one(msg, 14);
300 edbus_proxy_send(proxy, msg, on_plus_one, NULL, -1);
301 edbus_message_unref(msg);
302
303 edbus_proxy_event_callback_add(proxy,
304 EDBUS_PROXY_EVENT_PROPERTY_CHANGED,
305 _property_changed, NULL);
306 edbus_proxy_event_callback_add(proxy, EDBUS_PROXY_EVENT_PROPERTY_REMOVED,
307 _property_removed, NULL);
308
309 edbus_proxy_properties_monitor(proxy, EINA_TRUE);
310 ecore_timer_add(10, _read_cache, proxy);
311
312 edbus_proxy_call(proxy, "ReceiveVariantData", receive_variant_cb, NULL, -1, "");
313
314 ecore_main_loop_begin();
315
316 edbus_proxy_event_callback_del(proxy, EDBUS_PROXY_EVENT_PROPERTY_CHANGED,
317 _property_changed, NULL);
318 edbus_connection_unref(conn);
319
320 edbus_shutdown();
321 ecore_shutdown();
322 return 0;
323}