Initial commit.
[platform/core/uifw/at-spi2-atk.git] / libspi / value.c
index 0434411..b9ff9e5 100644 (file)
@@ -2,6 +2,7 @@
  * AT-SPI - Assistive Technology Service Provider Interface
  * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
  *
+ * Copyright 2008 Novell, Inc.
  * Copyright 2001, 2002 Sun Microsystems Inc.,
  * Copyright 2001, 2002 Ximian, Inc.
  *
  * Boston, MA 02111-1307, USA.
  */
 
-/* value.c : implements the Value interface */
-#include <config.h>
+#include "accessible.h"
 #include <math.h>
-#include <stdio.h>
-#include <libspi/value.h>
-
-#define PARENT_TYPE SPI_TYPE_BASE
-
-/* Static function declarations */
-
-static double
-get_double_from_gvalue (GValue *gvalue);
-static void
-gvalue_set_from_double (GValue *gvalue, double value);
-static void
-spi_value_class_init (SpiValueClass *klass);
-static void
-spi_value_init (SpiValue *value);
-static double
-impl__get_minimumValue (PortableServer_Servant _servant,
-                       CORBA_Environment *    ev);
-static double
-impl__get_maximumValue (PortableServer_Servant _servant,
-                       CORBA_Environment *    ev);
-static double
-impl__get_currentValue (PortableServer_Servant _servant,
-                       CORBA_Environment *    ev);
-static void 
-impl__set_currentValue (PortableServer_Servant _servant,
-                       const CORBA_double     value,
-                       CORBA_Environment *    ev);
-static double
-impl__get_minimumIncrement (PortableServer_Servant _servant,
-                       CORBA_Environment *    ev);
-
-
-BONOBO_TYPE_FUNC_FULL (SpiValue,
-                      Accessibility_Value,
-                      PARENT_TYPE,
-                      spi_value)
-
-
-static void
-spi_value_class_init (SpiValueClass *klass)
-{
-  POA_Accessibility_Value__epv *epv = &klass->epv;
-
-  /* Initialize epv table */
-
-  epv->_get_minimumValue = impl__get_minimumValue;
-  epv->_get_maximumValue = impl__get_maximumValue;
-  epv->_get_currentValue = impl__get_currentValue;
-  epv->_set_currentValue = impl__set_currentValue;
-  epv->_get_minimumIncrement = impl__get_minimumIncrement;
-}
-
-
-static void
-spi_value_init (SpiValue *value)
-{
-}
-
-
-SpiValue *
-spi_value_interface_new (AtkObject *obj)
-{
-  SpiValue *new_value = g_object_new (SPI_VALUE_TYPE, NULL);
-
-  spi_base_construct (SPI_BASE (new_value), G_OBJECT(obj));
-
-  return new_value;
-}
-
 
 static AtkValue *
-get_value_from_servant (PortableServer_Servant servant)
+get_value_from_path (const char *path, void *user_data)
 {
-  SpiBase *object = SPI_BASE (bonobo_object_from_servant (servant));
-
-  g_return_val_if_fail (object, NULL);
-  g_return_val_if_fail (ATK_IS_OBJECT(object->gobj), NULL);
-  return ATK_VALUE (object->gobj);
+  AtkObject *obj = spi_dbus_get_object (path);
+  if (!obj)
+    return NULL;
+  return ATK_VALUE (obj);
 }
 
 static double
-get_double_from_gvalue (GValue *gvalue)
+get_double_from_gvalue (GValue * gvalue)
 {
-  double       retval = 0;
+  double retval = 0;
   if (G_TYPE_IS_FUNDAMENTAL (G_VALUE_TYPE (gvalue)))
     {
       switch (gvalue->g_type)
-        {
-         case G_TYPE_DOUBLE:
-            retval = g_value_get_double (gvalue);
-           break;
-         case G_TYPE_FLOAT:
-            retval = g_value_get_float (gvalue);
-           break;
-         case G_TYPE_ULONG:
-            retval = g_value_get_ulong (gvalue);
-           break;
-         case G_TYPE_LONG:
-            retval = g_value_get_long (gvalue);
-           break;
-         case G_TYPE_UINT:
-            retval = g_value_get_uint (gvalue);
-           break;
-         case G_TYPE_INT:
-            retval = g_value_get_int (gvalue);
-           break;
-         case G_TYPE_UCHAR:
-            retval = g_value_get_uchar (gvalue);
-           break;
-         case G_TYPE_CHAR:
-            retval = g_value_get_char (gvalue);
-           break;
-         case G_TYPE_BOOLEAN:
-            retval = g_value_get_boolean (gvalue);
-           break;
-        }
+       {
+       case G_TYPE_DOUBLE:
+         retval = g_value_get_double (gvalue);
+         break;
+       case G_TYPE_FLOAT:
+         retval = g_value_get_float (gvalue);
+         break;
+       case G_TYPE_ULONG:
+         retval = g_value_get_ulong (gvalue);
+         break;
+       case G_TYPE_LONG:
+         retval = g_value_get_long (gvalue);
+         break;
+       case G_TYPE_UINT:
+         retval = g_value_get_uint (gvalue);
+         break;
+       case G_TYPE_INT:
+         retval = g_value_get_int (gvalue);
+         break;
+       case G_TYPE_UCHAR:
+         retval = g_value_get_uchar (gvalue);
+         break;
+       case G_TYPE_CHAR:
+         retval = g_value_get_char (gvalue);
+         break;
+       case G_TYPE_BOOLEAN:
+         retval = g_value_get_boolean (gvalue);
+         break;
+       }
     }
   else
     {
-      g_warning ("SpiValue requested from a non-fundamental type\n");      
+      g_warning ("SpiValue requested from a non-fundamental type\n");
     }
   return retval;
-}  
+}
+
+static gboolean
+get_double_from_variant (DBusMessageIter * iter, double *out)
+{
+  DBusMessageIter sub;
+
+  dbus_message_iter_recurse (iter, &sub);
+  switch (dbus_message_iter_get_arg_type (&sub))
+    {
+    case DBUS_TYPE_DOUBLE:
+      {
+       dbus_message_iter_get_basic (&sub, out);
+       return TRUE;
+      }
+    case DBUS_TYPE_UINT32:
+      {
+       dbus_uint32_t v;
+       dbus_message_iter_get_basic (&sub, &v);
+       *out = (double) v;
+       return TRUE;
+      }
+    case DBUS_TYPE_INT32:
+      {
+       dbus_int32_t v;
+       dbus_message_iter_get_basic (&sub, &v);
+       *out = (double) v;
+       return TRUE;
+      }
+    case DBUS_TYPE_UINT16:
+      {
+       dbus_uint16_t v;
+       dbus_message_iter_get_basic (&sub, &v);
+       *out = (double) v;
+       return TRUE;
+      }
+    case DBUS_TYPE_INT16:
+      {
+       dbus_int16_t v;
+       dbus_message_iter_get_basic (&sub, &v);
+       *out = (double) v;
+       return TRUE;
+      }
+    case DBUS_TYPE_BYTE:
+      {
+       char v;
+       dbus_message_iter_get_basic (&sub, &v);
+       *out = (double) v;
+       return TRUE;
+      }
+    case DBUS_TYPE_BOOLEAN:
+      {
+       dbus_bool_t v;
+       dbus_message_iter_get_basic (&sub, &v);
+       *out = (double) v;
+       return TRUE;
+      }
+    default:
+      return FALSE;
+    }
+}
 
 static void
-gvalue_set_from_double (GValue *gvalue, double value)
+gvalue_set_from_double (GValue * gvalue, double value)
 {
   if (G_TYPE_IS_FUNDAMENTAL (G_VALUE_TYPE (gvalue)))
     {
       switch (gvalue->g_type)
-        {
-         case G_TYPE_DOUBLE:
-            g_value_set_double (gvalue, value);
-           break;
-         case G_TYPE_FLOAT:
-            g_value_set_float (gvalue, value);
-           break;
-         case G_TYPE_ULONG:
-            g_value_set_ulong (gvalue, value);
-           break;
-         case G_TYPE_LONG:
-            g_value_set_long (gvalue, value);
-           break;
-         case G_TYPE_UINT:
-            g_value_set_uint (gvalue, value);
-           break;
-         case G_TYPE_INT:
-            g_value_set_int (gvalue, value);
-           break;
-         case G_TYPE_UCHAR:
-            g_value_set_uchar (gvalue, value);
-           break;
-         case G_TYPE_CHAR:
-            g_value_set_char (gvalue, value);
-           break;
-         case G_TYPE_BOOLEAN:
-            g_value_set_boolean (gvalue, ((fabs (value) > 0.5) ? 1 : 0));
-           break;
-        }
+       {
+       case G_TYPE_DOUBLE:
+         g_value_set_double (gvalue, value);
+         break;
+       case G_TYPE_FLOAT:
+         g_value_set_float (gvalue, value);
+         break;
+       case G_TYPE_ULONG:
+         g_value_set_ulong (gvalue, value);
+         break;
+       case G_TYPE_LONG:
+         g_value_set_long (gvalue, value);
+         break;
+       case G_TYPE_UINT:
+         g_value_set_uint (gvalue, value);
+         break;
+       case G_TYPE_INT:
+         g_value_set_int (gvalue, value);
+         break;
+       case G_TYPE_UCHAR:
+         g_value_set_uchar (gvalue, value);
+         break;
+       case G_TYPE_CHAR:
+         g_value_set_char (gvalue, value);
+         break;
+       case G_TYPE_BOOLEAN:
+         g_value_set_boolean (gvalue, ((fabs (value) > 0.5) ? 1 : 0));
+         break;
+       }
     }
   else
     {
-      g_warning ("SpiValue change requested for a non-fundamental type\n");        
+      g_warning ("SpiValue change requested for a non-fundamental type\n");
     }
 }
 
-static double
-impl__get_minimumValue (PortableServer_Servant servant,
-                       CORBA_Environment     *ev)
+static dbus_bool_t
+impl_get_minimumValue (const char *path, DBusMessageIter * iter,
+                      void *user_data)
 {
-  GValue    gvalue = {0, };
-  AtkValue *value = get_value_from_servant (servant);
-
-  g_return_val_if_fail (value != NULL, 0.0);
-
+  AtkValue *value = get_value_from_path (path, user_data);
+  GValue gvalue = { 0, };
+  if (!value)
+    return FALSE;
   atk_value_get_minimum_value (value, &gvalue);
-
-  return get_double_from_gvalue (&gvalue);
+  return droute_return_v_double (iter, get_double_from_gvalue (&gvalue));
 }
 
-
-static double
-impl__get_maximumValue (PortableServer_Servant servant,
-                       CORBA_Environment     *ev)
+static char *
+impl_get_minimumValue_str (void *datum)
 {
-  GValue   gvalue = {0, };
-  AtkValue *value = get_value_from_servant (servant);
-
-  g_return_val_if_fail (value != NULL, 0.0);
+  AtkValue *value = (AtkValue *) datum;
+  GValue gvalue = { 0, };
+  if (!value)
+    return FALSE;
+  atk_value_get_minimum_value (value, &gvalue);
+  return g_strdup_printf ("%lf", get_double_from_gvalue (&gvalue));
+}
 
+static dbus_bool_t
+impl_get_maximumValue (const char *path, DBusMessageIter * iter,
+                      void *user_data)
+{
+  AtkValue *value = get_value_from_path (path, user_data);
+  GValue gvalue = { 0, };
+  if (!value)
+    return FALSE;
   atk_value_get_maximum_value (value, &gvalue);
-
-  return get_double_from_gvalue (&gvalue);
+  return droute_return_v_double (iter, get_double_from_gvalue (&gvalue));
 }
 
+static char *
+impl_get_maximumValue_str (void *datum)
+{
+  AtkValue *value = (AtkValue *) datum;
+  GValue gvalue = { 0, };
+  if (!value)
+    return FALSE;
+  atk_value_get_maximum_value (value, &gvalue);
+  return g_strdup_printf ("%lf", get_double_from_gvalue (&gvalue));
+}
 
-static double
-impl__get_currentValue (PortableServer_Servant servant,
-                       CORBA_Environment     *ev)
+static dbus_bool_t
+impl_get_minimumIncrement (const char *path, DBusMessageIter * iter,
+                          void *user_data)
 {
-  GValue   gvalue = {0, };
-  AtkValue *value = get_value_from_servant (servant);
+  AtkValue *value = get_value_from_path (path, user_data);
+  GValue gvalue = { 0, };
+  if (!value)
+    return FALSE;
+  atk_value_get_minimum_value (value, &gvalue);
+  return droute_return_v_double (iter, get_double_from_gvalue (&gvalue));
+}
 
-  g_return_val_if_fail (value != NULL, 0.0);
+static char *
+impl_get_minimumIncrement_str (void *datum)
+{
+  AtkValue *value = (AtkValue *) datum;
+  GValue gvalue = { 0, };
+  if (!value)
+    return FALSE;
+  atk_value_get_minimum_value (value, &gvalue);
+  return g_strdup_printf ("%lf", get_double_from_gvalue (&gvalue));
+}
 
+static dbus_bool_t
+impl_get_currentValue (const char *path, DBusMessageIter * iter,
+                      void *user_data)
+{
+  AtkValue *value = get_value_from_path (path, user_data);
+  GValue gvalue = { 0, };
+  if (!value)
+    return FALSE;
   atk_value_get_current_value (value, &gvalue);
-
-  return get_double_from_gvalue (&gvalue);
+  return droute_return_v_double (iter, get_double_from_gvalue (&gvalue));
 }
 
-
-static void 
-impl__set_currentValue (PortableServer_Servant servant,
-                       const CORBA_double     value,
-                       CORBA_Environment     *ev)
+static char *
+impl_get_currentValue_str (void *datum)
 {
-  GValue    gvalue = { 0, };
-  AtkValue *avalue = get_value_from_servant (servant);
-
-  g_return_if_fail (avalue != NULL);
-
-  atk_value_get_current_value (avalue, &gvalue);
-  gvalue_set_from_double (&gvalue, value);
-
-  atk_value_set_current_value (avalue, &gvalue);
+  AtkValue *value = (AtkValue *) datum;
+  GValue gvalue = { 0, };
+  if (!value)
+    return FALSE;
+  atk_value_get_current_value (value, &gvalue);
+  return g_strdup_printf ("%lf", get_double_from_gvalue (&gvalue));
 }
 
-static double
-impl__get_minimumIncrement (PortableServer_Servant servant,
-                           CORBA_Environment     *ev)
+static dbus_bool_t
+impl_set_currentValue (const char *path, DBusMessageIter * iter,
+                      void *user_data)
 {
-  GValue    gvalue = {0, };
-  AtkValue *value = get_value_from_servant (servant);
-
-  g_return_val_if_fail (value != NULL, 0.0);
-
-  atk_value_get_minimum_increment (value, &gvalue);
-
-  return get_double_from_gvalue (&gvalue);
+  AtkValue *value = get_value_from_path (path, user_data);
+  GValue gvalue = { 0, };
+  double dbl;
+
+  if (!value)
+    return FALSE;
+  if (!get_double_from_variant (iter, &dbl))
+    return FALSE;
+  atk_value_get_current_value (value, &gvalue);
+  gvalue_set_from_double (&gvalue, dbl);
+  return TRUE;
 }
 
-
+static DRouteProperty properties[] = {
+  {impl_get_minimumValue, impl_get_minimumValue_str, NULL, NULL,
+   "minimumValue", "d"},
+  {impl_get_maximumValue, impl_get_maximumValue_str, NULL, NULL,
+   "maximumValue", "d"},
+  {impl_get_minimumIncrement, impl_get_minimumIncrement_str, NULL, NULL,
+   "minimumIncrement", "d"},
+  {impl_get_currentValue, impl_get_currentValue_str, impl_set_currentValue,
+   NULL, "currentValue", "d"},
+  {NULL, NULL, NULL, NULL, NULL, NULL}
+};
+
+void
+spi_initialize_value (DRouteData * data)
+{
+  droute_add_interface (data, "org.freedesktop.accessibility.Value", NULL,
+                       properties,
+                       (DRouteGetDatumFunction) get_value_from_path, NULL);
+};