edbus: Add edbus_message to/from eina_value examples
authorJosé Roberto de Souza <zehortigoza@profusion.mobi>
Fri, 26 Oct 2012 18:20:02 +0000 (18:20 +0000)
committerLucas De Marchi <lucas.demarchi@profusion.mobi>
Fri, 26 Oct 2012 18:20:02 +0000 (18:20 +0000)
Also move properties changed example to this file.

Patch by: José Roberto de Souza  <zehortigoza@profusion.mobi>

SVN revision: 78550

.gitignore
Makefile.am
src/examples/complex_types.c
src/examples/complex_types_client_eina_value.c [new file with mode: 0644]

index 5d3da96..eaa83e9 100644 (file)
@@ -38,6 +38,7 @@ src/examples/complex_types_server
 src/examples/server
 src/examples/simple-signal-emit
 src/examples/client
+src/examples/complex_types_client_eina_value
 doc/Doxyfile
 doc/html/
 doc/latex/
index fef3d1e..cac29a9 100644 (file)
@@ -92,7 +92,8 @@ noinst_PROGRAMS = \
        src/examples/complex_types_server \
        src/examples/server \
        src/examples/client \
-       src/examples/simple-signal-emit
+       src/examples/simple-signal-emit \
+       src/examples/complex_types_client_eina_value
 
 EXAMPLES_LIBS = libedbus2.la @ECORE_LIBS@
 
@@ -121,6 +122,9 @@ src_examples_client_LDADD = $(EXAMPLES_LIBS)
 src_examples_simple_signal_emit_SOURCES = src/examples/simple-signal-emit.c
 src_examples_simple_signal_emit_LDADD = $(EXAMPLES_LIBS)
 
+src_examples_complex_types_client_eina_value_SOURCES = src/examples/complex_types_client_eina_value.c
+src_examples_complex_types_client_eina_value_LDADD = $(EXAMPLES_LIBS)
+
 endif
 
 .PHONY: doc
index f90daec..9b8c164 100644 (file)
@@ -15,28 +15,6 @@ _timer1_cb(void *data)
    return EINA_TRUE;
 }
 
-static Eina_Bool
-_read_cache(void *data)
-{
-   EDBus_Proxy *proxy = data;
-   const char *txt;
-   int num;
-   Eina_Value *v = edbus_proxy_property_local_get(proxy, "text");
-   eina_value_get(v, &txt);
-   v = edbus_proxy_property_local_get(proxy, "int32");
-   eina_value_get(v, &num);
-
-   printf("Read cache: %s | %d\n", txt, num);
-
-   v = edbus_proxy_property_local_get(proxy, "st");
-   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
 on_plus_one(void *data, const EDBus_Message *msg, EDBus_Pending *pending)
 {
@@ -201,39 +179,6 @@ on_receive_array(void *data, const EDBus_Message *msg, EDBus_Pending *pending)
      }
 }
 
-static void
-_property_changed(void *data, EDBus_Proxy *proxy, void *event_info)
-{
-   EDBus_Proxy_Event_Property_Changed *event = event_info;
-   const char *name;
-   const Eina_Value *value;
-   printf("property changed\n");
-
-   name = event->name;
-   value = event->value;
-
-   if (!strcmp(name, "text"))
-     {
-        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);
-     }
-}
-
 int
 main(void)
 {
@@ -326,14 +271,11 @@ main(void)
                               -1 , "i", plus_one);
 
    pending = edbus_proxy_property_get(test2_proxy, "Resp2", get_property_resp2, test2_proxy);
-   edbus_proxy_event_callback_add(test2_proxy, EDBUS_PROXY_EVENT_PROPERTY_CHANGED, _property_changed, NULL);
 
-   ecore_timer_add(10, _read_cache, test2_proxy);
-   ecore_timer_add(50, _timer1_cb, NULL);
+   ecore_timer_add(10, _timer1_cb, NULL);
 
    ecore_main_loop_begin();
 
-   edbus_proxy_event_callback_del(test2_proxy, EDBUS_PROXY_EVENT_PROPERTY_CHANGED, _property_changed, NULL);
    edbus_connection_unref(conn);
 
    edbus_shutdown();
diff --git a/src/examples/complex_types_client_eina_value.c b/src/examples/complex_types_client_eina_value.c
new file mode 100644 (file)
index 0000000..f5dcfb4
--- /dev/null
@@ -0,0 +1,272 @@
+#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, const EDBus_Message *msg, EDBus_Pending *pending)
+{
+   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, const EDBus_Message *msg, EDBus_Pending *pending)
+{
+   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, const EDBus_Message *msg, EDBus_Pending *pending)
+{
+   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
+_property_changed(void *data, EDBus_Proxy *proxy, 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"))
+     {
+        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");
+   eina_value_get(v, &txt);
+   printf("Read cache: [txt] = %s\n", txt);
+
+   v = edbus_proxy_property_local_get(proxy, "int32");
+   eina_value_get(v, &num);
+   printf("Read cache: [int32] = %d\n", num);
+
+   v = edbus_proxy_property_local_get(proxy, "st");
+   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_message_unref(msg);
+
+   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_message_unref(msg);
+
+   edbus_proxy_event_callback_add(proxy,
+                                  EDBUS_PROXY_EVENT_PROPERTY_CHANGED,
+                                  _property_changed, NULL);
+
+   ecore_timer_add(10, _read_cache, proxy);
+
+   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;
+}