summaryrefslogtreecommitdiff
path: root/legacy/edbus/src/examples/complex_types_client_eina_value.c
diff options
context:
space:
mode:
authorJosé Roberto de Souza <zehortigoza@profusion.mobi>2012-10-26 18:20:02 +0000
committerLucas De Marchi <lucas.demarchi@profusion.mobi>2012-10-26 18:20:02 +0000
commita69f6ce3382c63bdc4d0fb661c02c25ab2a00455 (patch)
tree00132e2b11884767f0d88cee471c910a835bea5c /legacy/edbus/src/examples/complex_types_client_eina_value.c
parentc256bce1517776c0abf0553cc67e2c2088518b77 (diff)
edbus: Add edbus_message to/from eina_value examples
Also move properties changed example to this file. Patch by: José Roberto de Souza <zehortigoza@profusion.mobi> SVN revision: 78550
Diffstat (limited to '')
-rw-r--r--legacy/edbus/src/examples/complex_types_client_eina_value.c272
1 files changed, 272 insertions, 0 deletions
diff --git a/legacy/edbus/src/examples/complex_types_client_eina_value.c b/legacy/edbus/src/examples/complex_types_client_eina_value.c
new file mode 100644
index 0000000000..f5dcfb420e
--- /dev/null
+++ b/legacy/edbus/src/examples/complex_types_client_eina_value.c
@@ -0,0 +1,272 @@
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
151_property_changed(void *data, EDBus_Proxy *proxy, void *event_info)
152{
153 EDBus_Proxy_Event_Property_Changed *event = event_info;
154 const char *name;
155 const Eina_Value *value;
156 printf("\nproperty changed\n");
157
158 name = event->name;
159 value = event->value;
160
161 if (!strcmp(name, "text"))
162 {
163 const char *txt;
164 eina_value_get(value, &txt);
165 printf("[%s] = %s\n", name, txt);
166 }
167 else if (!strcmp(name, "int32"))
168 {
169 int num;
170 eina_value_get(value, &num);
171 printf("[%s] = %d\n", name, num);
172 }
173 else if (!strcmp(name, "st"))
174 {
175 const char *txt;
176 eina_value_struct_get(value, "arg0", &txt);
177 printf("[%s] %s | ", name, txt);
178 eina_value_struct_get(value, "arg1", &txt);
179 printf("%s\n", txt);
180 }
181}
182
183static Eina_Bool
184_read_cache(void *data)
185{
186 EDBus_Proxy *proxy = data;
187 const char *txt;
188 int num;
189 Eina_Value *v;
190
191 v = edbus_proxy_property_local_get(proxy, "text");
192 eina_value_get(v, &txt);
193 printf("Read cache: [txt] = %s\n", txt);
194
195 v = edbus_proxy_property_local_get(proxy, "int32");
196 eina_value_get(v, &num);
197 printf("Read cache: [int32] = %d\n", num);
198
199 v = edbus_proxy_property_local_get(proxy, "st");
200 eina_value_struct_get(v, "arg0", &txt);
201 printf("Read cache: [st] %s | ", txt);
202 eina_value_struct_get(v, "arg1", &txt);
203 printf("%s\n", txt);
204
205 return EINA_FALSE;
206}
207
208static void
209_fill_plus_one(EDBus_Message *msg, int num)
210{
211 Eina_Value *v;
212 Eina_Value_Struct_Member main_members[] = {
213 {"num", EINA_VALUE_TYPE_INT, 0}
214 };
215 Eina_Value_Struct_Desc desc_struct = {
216 EINA_VALUE_STRUCT_DESC_VERSION,
217 NULL, // no special operations
218 main_members,
219 1,
220 sizeof(int)
221 };
222 v = eina_value_struct_new(&desc_struct);
223 eina_value_struct_set(v, "num", num);
224
225 edbus_message_from_eina_value("i", msg, v);
226
227 eina_value_free(v);
228}
229
230int
231main(void)
232{
233 EDBus_Connection *conn;
234 EDBus_Object *obj;
235 EDBus_Proxy *proxy;
236 EDBus_Message *msg;
237
238 ecore_init();
239 edbus_init();
240
241 conn = edbus_connection_get(EDBUS_CONNECTION_TYPE_SESSION);
242 obj = edbus_object_get(conn, BUS, PATH);
243 proxy = edbus_proxy_get(obj, IFACE);
244
245 msg = edbus_proxy_method_call_new(proxy, "ReceiveArrayOfStringIntWithSize");
246 _fill_receive_array_of_string_int_with_size(msg, size_of_array, array_string);
247 edbus_proxy_send(proxy, msg, on_receive_array_with_size, NULL, -1);
248 edbus_message_unref(msg);
249
250 edbus_proxy_call(proxy, "SendArray", on_send_array, NULL, -1 , "");
251
252 msg = edbus_proxy_method_call_new(proxy, "PlusOne");
253 _fill_plus_one(msg, 14);
254 edbus_proxy_send(proxy, msg, on_plus_one, NULL, -1);
255 edbus_message_unref(msg);
256
257 edbus_proxy_event_callback_add(proxy,
258 EDBUS_PROXY_EVENT_PROPERTY_CHANGED,
259 _property_changed, NULL);
260
261 ecore_timer_add(10, _read_cache, proxy);
262
263 ecore_main_loop_begin();
264
265 edbus_proxy_event_callback_del(proxy, EDBUS_PROXY_EVENT_PROPERTY_CHANGED,
266 _property_changed, NULL);
267 edbus_connection_unref(conn);
268
269 edbus_shutdown();
270 ecore_shutdown();
271 return 0;
272}