summaryrefslogtreecommitdiff
path: root/src/examples/edbus/complex-types-client-eina-value.c
blob: cde7c9b2ce524e5dee5d69f1e01129c5add1d225 (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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#include "EDBus.h"
#include <Ecore.h>

#define BUS "com.profusion"
#define PATH "/com/profusion/Test"
#define IFACE "com.profusion.Test"

#define size_of_array 5
static const char *array_string[] = {
   "aaaa", "bbbb", "cccc", "dddd", "eeee"
};

typedef struct _sub_struct
{
   char *txt;
   int num;
} sub_struct;

typedef struct _main_struct
{
   int size;
   sub_struct array[];
} main_struct;

static unsigned int
_type_offset(unsigned base, unsigned size)
{
   unsigned padding;
   if (!(base % size))
     return base;
   padding = abs(base - size);
   return base + padding;
}

static void
_fill_receive_array_of_string_int_with_size(EDBus_Message *msg, int size, const char *array[])
{
   Eina_Value *value_struct, *value_array;
   int i;
   unsigned offset;
   Eina_Value_Struct_Member main_members[2];
   Eina_Value_Struct_Member sub_members[] = {
      EINA_VALUE_STRUCT_MEMBER(EINA_VALUE_TYPE_STRING, sub_struct, txt),
      EINA_VALUE_STRUCT_MEMBER(EINA_VALUE_TYPE_INT, sub_struct, num)
   };
   Eina_Value_Struct_Desc desc_sub_struct = {
      EINA_VALUE_STRUCT_DESC_VERSION,
      NULL, // no special operations
      sub_members,
      2,
      sizeof(sub_struct)
   };
   Eina_Value_Struct_Desc desc_struct = {
      EINA_VALUE_STRUCT_DESC_VERSION,
      NULL, // no special operations
      main_members,
      2,
      0//will be set below
   };

   offset = _type_offset(sizeof(int), sizeof(Eina_Value_Array));
   main_members[0].name = "size";
   main_members[0].type = EINA_VALUE_TYPE_INT, 0;
   main_members[0].offset = 0;
   main_members[1].name = "array";
   main_members[1].type = EINA_VALUE_TYPE_ARRAY;
   main_members[1].offset = offset;
   desc_struct.size = offset + sizeof(Eina_Value_Array);

   value_struct = eina_value_struct_new(&desc_struct);
   eina_value_struct_set(value_struct, "size", size);

   value_array = eina_value_array_new(EINA_VALUE_TYPE_STRUCT, size);
   for (i = 0; i < size; i++)
     {
        Eina_Value *value_sub_struct = eina_value_struct_new(&desc_sub_struct);
        Eina_Value_Struct st;
        eina_value_struct_set(value_sub_struct, "txt", array[i]);
        eina_value_struct_set(value_sub_struct, "num", i);
        eina_value_get(value_sub_struct, &st);
        eina_value_array_append(value_array, st);
        eina_value_free(value_sub_struct);
     }
   eina_value_struct_value_set(value_struct, "array", value_array);

   edbus_message_from_eina_value("ia(si)", msg, value_struct);

   eina_value_free(value_struct);
   eina_value_free(value_array);
}

static void
on_send_array(void *data EINA_UNUSED, const EDBus_Message *msg, EDBus_Pending *pending EINA_UNUSED)
{
   Eina_Value *v, array;
   const char *txt;
   unsigned i;
   printf("2 - on_send_array()\n");

   if (edbus_message_error_get(msg, NULL, NULL))
     {
        printf("Message error\n\n");
        return;
     }

   v = edbus_message_to_eina_value(msg);
   eina_value_struct_value_get(v, "arg0", &array);
   for (i = 0; i < eina_value_array_count(&array); i++)
     {
        eina_value_array_get(&array, i, &txt);
        printf("\t%s\n", txt);
     }

   eina_value_free(v);
   eina_value_flush(&array);
}

static void
on_receive_array_with_size(void *data EINA_UNUSED, const EDBus_Message *msg, EDBus_Pending *pending EINA_UNUSED)
{
   const char *errname;
   const char *errmsg;

   printf("1 - on_receive_array_with_size()\n");
   if (edbus_message_error_get(msg, &errname, &errmsg))
     {
        fprintf(stderr, "Error: %s %s\n", errname, errmsg);
     }
}

static void
on_plus_one(void *data EINA_UNUSED, const EDBus_Message *msg, EDBus_Pending *pending EINA_UNUSED)
{
   Eina_Value *v;
   int num2;

   if (edbus_message_error_get(msg, NULL, NULL))
     {
        printf("Message error\n\n");
        return;
     }

   v = edbus_message_to_eina_value(msg);
   eina_value_struct_get(v, "arg0", &num2);

   printf("3 - on_plus_one() %d\n", num2);
   eina_value_free(v);
}

static void
receive_variant_cb(void *data EINA_UNUSED, const EDBus_Message *msg, EDBus_Pending *pending EINA_UNUSED)
{
   Eina_Value *v, variant, array;
   unsigned i;

   printf("4 - receive a variant with an array of strings\n");
   if (edbus_message_error_get(msg, NULL, NULL))
     {
        printf("Message error\n\n");
        return;
     }

   v = edbus_message_to_eina_value(msg);

   eina_value_struct_value_get(v, "arg0", &variant);
   eina_value_struct_value_get(&variant, "arg0", &array);
   for (i = 0; i < eina_value_array_count(&array); i++)
     {
        const char *txt;
        eina_value_array_get(&array, i, &txt);
        printf("\t%s\n", txt);
     }

   eina_value_flush(&array);
   eina_value_flush(&variant);
   eina_value_free(v);
}

static void
_property_removed(void *data EINA_UNUSED, EDBus_Proxy *proxy EINA_UNUSED, void *event_info)
{
   EDBus_Proxy_Event_Property_Removed *event = event_info;

   printf("\nproperty removed: %s", event->name);
}

static void
_property_changed(void *data EINA_UNUSED, EDBus_Proxy *proxy EINA_UNUSED, void *event_info)
{
   EDBus_Proxy_Event_Property_Changed *event = event_info;
   const char *name;
   const Eina_Value *value;
   printf("\nproperty changed\n");

   name = event->name;
   value = event->value;

   if (!strcmp(name, "text") || !strcmp(name, "Resp2"))
     {
        const char *txt;
        eina_value_get(value, &txt);
        printf("[%s] = %s\n", name, txt);
     }
   else if (!strcmp(name, "int32"))
     {
        int num;
        eina_value_get(value, &num);
        printf("[%s] = %d\n", name, num);
     }
   else if (!strcmp(name, "st"))
     {
        const char *txt;
        eina_value_struct_get(value, "arg0", &txt);
        printf("[%s] %s | ", name, txt);
        eina_value_struct_get(value, "arg1", &txt);
        printf("%s\n", txt);
     }
}

static Eina_Bool
_read_cache(void *data)
{
   EDBus_Proxy *proxy = data;
   const char *txt;
   int num;
   Eina_Value *v;

   v = edbus_proxy_property_local_get(proxy, "text");
   if (v)
     {
        eina_value_get(v, &txt);
        printf("Read cache: [txt] = %s\n", txt);
     }

   v = edbus_proxy_property_local_get(proxy, "int32");
   if (v)
     {
        eina_value_get(v, &num);
        printf("Read cache: [int32] = %d\n", num);
     }

   v = edbus_proxy_property_local_get(proxy, "st");
   if (v)
     {
        eina_value_struct_get(v, "arg0", &txt);
        printf("Read cache: [st] %s | ", txt);
        eina_value_struct_get(v, "arg1", &txt);
        printf("%s\n", txt);
     }

   return EINA_FALSE;
}

static void
_fill_plus_one(EDBus_Message *msg, int num)
{
   Eina_Value *v;
   Eina_Value_Struct_Member main_members[] = {
      {"num", EINA_VALUE_TYPE_INT, 0}
   };
   Eina_Value_Struct_Desc desc_struct = {
         EINA_VALUE_STRUCT_DESC_VERSION,
         NULL, // no special operations
         main_members,
         1,
         sizeof(int)
   };
   v = eina_value_struct_new(&desc_struct);
   eina_value_struct_set(v, "num", num);

   edbus_message_from_eina_value("i", msg, v);

   eina_value_free(v);
}

int
main(void)
{
   EDBus_Connection *conn;
   EDBus_Object *obj;
   EDBus_Proxy *proxy;
   EDBus_Message *msg;

   ecore_init();
   edbus_init();

   conn = edbus_connection_get(EDBUS_CONNECTION_TYPE_SESSION);
   obj = edbus_object_get(conn, BUS, PATH);
   proxy = edbus_proxy_get(obj, IFACE);

   msg = edbus_proxy_method_call_new(proxy, "ReceiveArrayOfStringIntWithSize");
   _fill_receive_array_of_string_int_with_size(msg, size_of_array, array_string);
   edbus_proxy_send(proxy, msg, on_receive_array_with_size, NULL, -1);

   edbus_proxy_call(proxy, "SendArray", on_send_array, NULL, -1 , "");

   msg = edbus_proxy_method_call_new(proxy, "PlusOne");
   _fill_plus_one(msg, 14);
   edbus_proxy_send(proxy, msg, on_plus_one, NULL, -1);

   edbus_proxy_event_callback_add(proxy,
                                  EDBUS_PROXY_EVENT_PROPERTY_CHANGED,
                                  _property_changed, NULL);
   edbus_proxy_event_callback_add(proxy, EDBUS_PROXY_EVENT_PROPERTY_REMOVED,
                                  _property_removed, NULL);

   edbus_proxy_properties_monitor(proxy, EINA_TRUE);
   ecore_timer_add(10, _read_cache, proxy);

   edbus_proxy_call(proxy, "ReceiveVariantData", receive_variant_cb, NULL, -1, "");

   ecore_main_loop_begin();

   edbus_proxy_event_callback_del(proxy, EDBUS_PROXY_EVENT_PROPERTY_CHANGED,
                                  _property_changed, NULL);
   edbus_connection_unref(conn);

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