[PATCH] dix: add valuator_mask_fetch_double()

Peter Hutterer peter.hutterer at who-t.net
Thu Sep 29 21:29:02 PDT 2011


Using this call simplifies callers that don't know if the mask bit is set.

Before:
  if (valuator_mask_isset(mask, valnum))
    value = valuator_mask_get_double(mask, valnum));
  else
    value = someothervalue;

Now:
 if (!valuator_mask_fetch_double(mask, valnum, &value))
    value = someothervalue;

Signed-off-by: Peter Hutterer <peter.hutterer at who-t.net>
---
 dix/inpututils.c |   36 ++++++++++++++++++++++++++++++++++++
 include/input.h  |    4 ++++
 test/input.c     |   15 ++++++++++++++-
 3 files changed, 54 insertions(+), 1 deletions(-)

diff --git a/dix/inpututils.c b/dix/inpututils.c
index 2a2b543..582135e 100644
--- a/dix/inpututils.c
+++ b/dix/inpututils.c
@@ -539,6 +539,42 @@ valuator_mask_get(const ValuatorMask *mask, int valuator)
 }
 
 /**
+ * Set value to the requested valuator. If the mask bit is set for this
+ * valuator, value contains the requested valuator value and TRUE is
+ * returned.
+ * If the mask bit is not set for this valuator, value is unchanged and
+ * FALSE is returned.
+ */
+Bool
+valuator_mask_fetch_double(const ValuatorMask *mask, int valuator, double *value)
+{
+    if (valuator_mask_isset(mask, valuator))
+    {
+        *value = valuator_mask_get_double(mask, valuator);
+        return TRUE;
+    } else
+        return FALSE;
+}
+
+/**
+ * Set value to the requested valuator. If the mask bit is set for this
+ * valuator, value contains the requested valuator value and TRUE is
+ * returned.
+ * If the mask bit is not set for this valuator, value is unchanged and
+ * FALSE is returned.
+ */
+Bool
+valuator_mask_fetch(const ValuatorMask *mask, int valuator, int *value)
+{
+    if (valuator_mask_isset(mask, valuator))
+    {
+        *value = valuator_mask_get(mask, valuator);
+        return TRUE;
+    } else
+        return FALSE;
+}
+
+/**
  * Remove the valuator from the mask.
  */
 void
diff --git a/include/input.h b/include/input.h
index 2544996..47956d6 100644
--- a/include/input.h
+++ b/include/input.h
@@ -597,6 +597,10 @@ extern _X_EXPORT void valuator_mask_copy(ValuatorMask *dest,
 extern _X_EXPORT int valuator_mask_get(const ValuatorMask *mask, int valnum);
 extern _X_EXPORT double valuator_mask_get_double(const ValuatorMask *mask,
                                                  int valnum);
+extern _X_EXPORT Bool valuator_mask_fetch(const ValuatorMask *mask,
+                                          int valnum, int *val);
+extern _X_EXPORT Bool valuator_mask_fetch_double(const ValuatorMask *mask,
+                                                 int valnum, double *val);
 
 /* InputOption handling interface */
 extern _X_EXPORT InputOption* input_option_new(InputOption *list, const char *key, const char *value);
diff --git a/test/input.c b/test/input.c
index afc4d4d..5fb9a90 100644
--- a/test/input.c
+++ b/test/input.c
@@ -1199,14 +1199,19 @@ static void dix_input_valuator_masks(void)
     assert(valuator_mask_num_valuators(mask) == num_vals);
     for (i = 0; i < nvaluators; i++)
     {
+        double val;
         if (i < first_val || i >= first_val + num_vals)
+        {
             assert(!valuator_mask_isset(mask, i));
-        else
+            assert(!valuator_mask_fetch_double(mask, i, &val));
+        } else
         {
             assert(valuator_mask_isset(mask, i));
             assert(valuator_mask_get(mask, i) == val_ranged[i - first_val]);
             assert(valuator_mask_get_double(mask, i) ==
                     val_ranged[i - first_val]);
+            assert(valuator_mask_fetch_double(mask, i, &val));
+            assert(val_ranged[i - first_val] == val);
         }
     }
 
@@ -1218,10 +1223,18 @@ static void dix_input_valuator_masks(void)
 
     for (i = 0; i < nvaluators; i++)
     {
+        double a, b;
         assert(valuator_mask_isset(mask, i) == valuator_mask_isset(copy, i));
+
+        if (!valuator_mask_isset(mask, i))
+            continue;
+
         assert(valuator_mask_get(mask, i) == valuator_mask_get(copy, i));
         assert(valuator_mask_get_double(mask, i) ==
                 valuator_mask_get_double(copy, i));
+        assert(valuator_mask_fetch_double(mask, i, &a));
+        assert(valuator_mask_fetch_double(copy, i, &b));
+        assert(a == b);
     }
 
     valuator_mask_free(&mask);
-- 
1.7.6



More information about the xorg-devel mailing list