Added support for set message types.
authorTilman Sauerbeck <tilman@code-monkey.de>
Thu, 28 Apr 2005 18:17:21 +0000 (18:17 +0000)
committerTilman Sauerbeck <tilman@code-monkey.de>
Thu, 28 Apr 2005 18:17:21 +0000 (18:17 +0000)
src/rb_edje.c

index 1202b79..6a9b7f7 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: rb_edje.c 330 2005-04-28 15:38:47Z tilman $
+ * $Id: rb_edje.c 331 2005-04-28 18:17:21Z tilman $
  *
  * Copyright (C) 2004 Tilman Sauerbeck (tilman at code-monkey de)
  *
@@ -19,6 +19,7 @@
  */
 
 #include <ruby.h>
+#include <stdbool.h>
 
 #include <Edje.h>
 #include <evas/rb_evas.h>
@@ -410,45 +411,229 @@ static VALUE c_data_get (VALUE self, VALUE key)
        return s ? rb_str_new2 (s) : Qnil;
 }
 
+static Edje_Message_Type get_msg_type (VALUE val)
+{
+       VALUE ary, entry;
+       Edje_Message_Type type;
+       int i, len;
+
+       if (NIL_P (val))
+               return EDJE_MESSAGE_NONE;
+
+       if (!NIL_P (rb_check_string_type (val))) {
+               return EDJE_MESSAGE_STRING;
+       } else if (rb_obj_is_kind_of (val, rb_cFixnum)) {
+               return EDJE_MESSAGE_INT;
+       } else if (rb_obj_is_kind_of (val, rb_cFloat)) {
+               return EDJE_MESSAGE_FLOAT;
+       } else if (NIL_P (ary = rb_check_array_type (val)))
+               return EDJE_MESSAGE_NONE;
+
+       len = RARRAY (ary)->len;
+       if (len <= 0)
+               return EDJE_MESSAGE_NONE;
+
+       entry = rb_ary_entry (ary, 0);
+
+       if (rb_obj_is_kind_of (entry, rb_cFixnum))
+               return EDJE_MESSAGE_INT_SET;
+       else if (rb_obj_is_kind_of (entry, rb_cFloat))
+               return EDJE_MESSAGE_FLOAT_SET;
+       else if (NIL_P (rb_check_string_type (entry)))
+               return EDJE_MESSAGE_NONE;
+
+       /* first entry is a string.
+        * so if we only have one entry, it's a string set
+        */
+       if (len == 1)
+               return EDJE_MESSAGE_STRING_SET;
+
+       entry = rb_ary_entry (ary, 1);
+
+       if (!NIL_P (rb_check_string_type (entry)))
+               type = EDJE_MESSAGE_STRING_SET;
+       else if (rb_obj_is_kind_of (entry, rb_cFixnum))
+               type = len == 2 ? EDJE_MESSAGE_STRING_INT : EDJE_MESSAGE_STRING_INT_SET;
+       else if (rb_obj_is_kind_of (entry, rb_cFloat))
+               type = len == 2 ? EDJE_MESSAGE_STRING_FLOAT :EDJE_MESSAGE_STRING_FLOAT_SET;
+       else
+               return EDJE_MESSAGE_NONE;
+
+       switch (type) {
+               case EDJE_MESSAGE_STRING_SET:
+                       for (i = 2; i < len; i++) {
+                               entry = rb_ary_entry (ary, i);
+                               if (NIL_P(rb_check_string_type (entry)))
+                                       return EDJE_MESSAGE_NONE;
+                       }
+
+                       break;
+               case EDJE_MESSAGE_INT_SET:
+               case EDJE_MESSAGE_STRING_INT_SET:
+                       for (i = 2; i < len; i++) {
+                               entry = rb_ary_entry (ary, i);
+                               if (!rb_obj_is_kind_of (entry, rb_cFixnum))
+                                       return EDJE_MESSAGE_NONE;
+                       }
+
+                       break;
+               case EDJE_MESSAGE_FLOAT_SET:
+               case EDJE_MESSAGE_STRING_FLOAT_SET:
+                       for (i = 2; i < len; i++) {
+                               entry = rb_ary_entry (ary, 2);
+                               if (!rb_obj_is_kind_of (entry, rb_cFloat))
+                                       return EDJE_MESSAGE_NONE;
+                       }
+
+                       break;
+               default:
+                       break;
+       }
+
+       return type;
+}
+
 static VALUE c_send_message (VALUE self, VALUE msg)
 {
-       Edje_Message_String s;
-       Edje_Message_Int i;
-       Edje_Message_Float f;
+       Edje_Message_String msg_s;
+       Edje_Message_Int msg_i;
+       Edje_Message_Float msg_f;
+       Edje_Message_String_Set *s_set = NULL;
+       Edje_Message_Int_Set *i_set = NULL;
+       Edje_Message_Float_Set *f_set = NULL;
+       Edje_Message_String_Int si;
+       Edje_Message_String_Float sf;
+       Edje_Message_String_Int_Set *si_set = NULL;
+       Edje_Message_String_Float_Set *sf_set = NULL;
        Edje_Message_Type type;
-       void *data;
-       VALUE v, tmp;
+       void *data = NULL;
+       int id, i, len = 0;
+       bool free_data = false;
+       VALUE v, ary, entry;
 
        GET_OBJ (self, RbEdje, e);
 
        CHECK_CLASS (msg, cMsg);
 
+       id = NUM2INT (rb_iv_get (msg, "@id"));
        v = rb_iv_get (msg, "@value");
 
-       if (NIL_P (v)) {
-               rb_raise (eEdjeError, "value must not be nil");
-               return Qnil;
+       type = get_msg_type (v);
+       ary = rb_check_array_type (v);
+
+       switch (type) {
+               case EDJE_MESSAGE_NONE:
+                       rb_raise (eEdjeError, "unsupported value");
+                       return Qnil;
+               case EDJE_MESSAGE_SIGNAL:
+                       return Qnil; /* cannot happen */
+               case EDJE_MESSAGE_STRING:
+                       msg_s.str = StringValuePtr (v);
+                       data = &msg_s;
+                       break;
+               case EDJE_MESSAGE_INT:
+                       msg_i.val = FIX2INT (v);
+                       data = &msg_i;
+                       break;
+               case EDJE_MESSAGE_FLOAT:
+                       msg_f.val = NUM2DBL (v);
+                       data = &msg_f;
+                       break;
+               case EDJE_MESSAGE_STRING_SET:
+                       s_set = malloc (sizeof (Edje_Message_String_Set) + ((len - 1) * sizeof (char *)));
+                       s_set->count = len;
+                       free_data = true;
+
+                       for (i = 0; i < len; i++) {
+                               entry = rb_ary_entry (ary, i);
+                               s_set->str[i] = StringValuePtr (entry);
+                       }
+
+                       data = s_set;
+
+                       break;
+               case EDJE_MESSAGE_INT_SET:
+                       i_set = malloc (sizeof (Edje_Message_Int_Set) + ((len - 1) * sizeof (int)));
+                       i_set->count = len;
+                       free_data = true;
+
+                       for (i = 0; i < len; i++) {
+                               entry = rb_ary_entry (ary, i);
+                               i_set->val[i] = FIX2INT (entry);
+                       }
+
+                       data = i_set;
+
+                       break;
+               case EDJE_MESSAGE_FLOAT_SET:
+                       f_set = malloc (sizeof (Edje_Message_Float_Set) + ((len - 1) * sizeof (double)));
+                       f_set->count = len;
+                       free_data = true;
+
+                       for (i = 0; i < len; i++) {
+                               entry = rb_ary_entry (ary, i);
+                               f_set->val[i] = NUM2DBL (entry);
+                       }
+
+                       data = f_set;
+
+                       break;
+               case EDJE_MESSAGE_STRING_INT:
+                       entry = rb_ary_entry (ary, 0);
+                       si.str = StringValuePtr (entry);
+                       entry = rb_ary_entry (ary, 1);
+                       si.val = FIX2INT (entry);
+
+                       data = &si;
+
+                       break;
+               case EDJE_MESSAGE_STRING_FLOAT:
+                       entry = rb_ary_entry (ary, 0);
+                       sf.str = StringValuePtr (entry);
+                       entry = rb_ary_entry (ary, 1);
+                       sf.val = NUM2DBL (entry);
+
+                       data = &sf;
+
+                       break;
+               case EDJE_MESSAGE_STRING_INT_SET:
+                       si_set = malloc (sizeof (Edje_Message_String_Int_Set) + ((len - 1) * sizeof (int)));
+                       si_set->count = len - 1;
+                       free_data = true;
+
+                       entry = rb_ary_entry (ary, 0);
+                       si_set->str = StringValuePtr (entry);
+
+                       for (i = 1; i < len; i++) {
+                               entry = rb_ary_entry (ary, i);
+                               si_set->val[i - 1] = FIX2INT (entry);
+                       }
+
+                       data = &si_set;
+
+                       break;
+               case EDJE_MESSAGE_STRING_FLOAT_SET:
+                       sf_set = malloc (sizeof (Edje_Message_String_Float_Set) + ((len - 1) * sizeof (double)));
+                       sf_set->count = len - 1;
+                       free_data = true;
+
+                       entry = rb_ary_entry (ary, 0);
+                       sf_set->str = StringValuePtr (entry);
+
+                       for (i = 1; i < len; i++) {
+                               entry = rb_ary_entry (ary, i);
+                               sf_set->val[i - 1] = NUM2DBL (entry);
+                       }
+
+                       data = &sf_set;
+
+                       break;
        }
 
-       if (!NIL_P (tmp = rb_check_string_type (v))) {
-               type = EDJE_MESSAGE_STRING;
-               s.str = StringValuePtr (tmp);
-               data = &s;
-       } else if (rb_obj_is_kind_of (v, rb_cFixnum)) {
-               type = EDJE_MESSAGE_INT;
-               i.val = FIX2INT (v);
-               data = &i;
-       } else if (rb_obj_is_kind_of (v, rb_cFloat)) {
-               type = EDJE_MESSAGE_FLOAT;
-               f.val = NUM2DBL (v);
-               data = &v;
-       /* } else if (!NIL_P (tmp = rb_check_array_type (v))) { */
-       } else {
-               rb_raise (eEdjeError, "unsupported value");
-       }
+       edje_object_message_send (e->real.real, type, id, data);
 
-       edje_object_message_send (e->real.real, type,
-                                 NUM2INT (rb_iv_get (msg, "@id")), data);
+       if (free_data)
+               free (data);
 
        return Qnil;
 }