Values in, values out. No magic state structs, keep it simple.

Signed-off-by: Peter Hutterer <[email protected]>
---
 src/wcmUSB.c       |   55 ++++++++++++++++++++++++++++++++++++---------------
 test/wacom-tests.c |   16 +++++++++++++++
 2 files changed, 55 insertions(+), 16 deletions(-)

diff --git a/src/wcmUSB.c b/src/wcmUSB.c
index e7e4366..9e656e2 100644
--- a/src/wcmUSB.c
+++ b/src/wcmUSB.c
@@ -950,11 +950,33 @@ static int usbParseAbsEvent(WacomCommonPtr common,
        return change;
 }
 
-#define MOD_BUTTONS(bit, value) do { \
-       int shift = 1 << (bit); \
-       ds->buttons = ((value) ? \
-                       (ds->buttons | (shift)) : (ds->buttons & ~(shift))); \
-       } while (0)
+/**
+ * Flip the mask bit in buttons corresponding to btn to the specified state.
+ *
+ * @param buttons The current button mask
+ * @param btn Zero-indexed button number to change
+ * @param state Zero to unset, non-zero to set the mask for the button
+ *
+ * @return The new button mask
+ */
+static int mod_buttons(int buttons, int btn, int state)
+{
+       int mask = 1 << btn;
+
+       if (btn >= sizeof(int))
+       {
+               xf86Msg(X_ERROR, "%s: Invalid button number %d. Insufficient "
+                               "storage\n", __func__, btn);
+               return buttons;
+       }
+
+       if (state)
+               buttons |= mask;
+       else
+               buttons &= ~mask;
+
+       return buttons;
+}
 
 static int usbParseAbsMTEvent(WacomCommonPtr common, struct input_event *event)
 {
@@ -983,7 +1005,8 @@ static int usbParseAbsMTEvent(WacomCommonPtr common, 
struct input_event *event)
                         */
                        if (TabletHasFeature(common, WCM_LCD) &&
                                        !private->wcmMTChannel)
-                               MOD_BUTTONS(0, event->value != -1);
+                               ds->buttons = mod_buttons(ds->buttons, 0,
+                                                         (event->value != -1));
                        break;
 
                case ABS_MT_POSITION_X:
@@ -1089,7 +1112,7 @@ static int usbParseKeyEvent(WacomCommonPtr common,
                                        ds->device_type = TOUCH_ID;
                                        ds->device_id = TOUCH_DEVICE_ID;
                                        ds->proximity = event->value;
-                                       MOD_BUTTONS(0, event->value);
+                                       ds->buttons = mod_buttons(ds->buttons, 
0, event->value);
                                }
                        }
                        break;
@@ -1130,7 +1153,7 @@ static int usbParseKeyEvent(WacomCommonPtr common,
                         */
                        if (common->wcmCapacityDefault < 0 &&
                            (TabletHasFeature(common, WCM_LCD)))
-                               MOD_BUTTONS(0, event->value);
+                               ds->buttons = mod_buttons(ds->buttons, 0, 
event->value);
                        break;
 
                case BTN_TOOL_TRIPLETAP:
@@ -1165,11 +1188,11 @@ static int usbParseKeyEvent(WacomCommonPtr common,
        switch (event->code)
        {
                case BTN_STYLUS:
-                       MOD_BUTTONS(1, event->value);
+                       ds->buttons = mod_buttons(ds->buttons, 1, event->value);
                        break;
 
                case BTN_STYLUS2:
-                       MOD_BUTTONS(2, event->value);
+                       ds->buttons = mod_buttons(ds->buttons, 2, event->value);
                        break;
 
                default:
@@ -1189,25 +1212,25 @@ static int usbParseBTNEvent(WacomCommonPtr common,
        switch (event->code)
        {
                case BTN_LEFT:
-                       MOD_BUTTONS(0, event->value);
+                       ds->buttons = mod_buttons(ds->buttons, 0, event->value);
                        break;
 
                case BTN_MIDDLE:
-                       MOD_BUTTONS(1, event->value);
+                       ds->buttons = mod_buttons(ds->buttons, 1, event->value);
                        break;
 
                case BTN_RIGHT:
-                       MOD_BUTTONS(2, event->value);
+                       ds->buttons = mod_buttons(ds->buttons, 2, event->value);
                        break;
 
                case BTN_SIDE:
                case BTN_BACK:
-                       MOD_BUTTONS(3, event->value);
+                       ds->buttons = mod_buttons(ds->buttons, 3, event->value);
                        break;
 
                case BTN_EXTRA:
                case BTN_FORWARD:
-                       MOD_BUTTONS(4, event->value);
+                       ds->buttons = mod_buttons(ds->buttons, 4, event->value);
                        break;
 
                default:
@@ -1215,7 +1238,7 @@ static int usbParseBTNEvent(WacomCommonPtr common,
                        {
                                if (event->code == common->padkey_code[nkeys])
                                {
-                                       MOD_BUTTONS(nkeys, event->value);
+                                       ds->buttons = mod_buttons(ds->buttons, 
nkeys, event->value);
                                        break;
                                }
                        }
diff --git a/test/wacom-tests.c b/test/wacom-tests.c
index 1eec90e..f7b0fcc 100644
--- a/test/wacom-tests.c
+++ b/test/wacom-tests.c
@@ -428,6 +428,21 @@ test_tilt_to_rotation(void)
 }
 
 
+static void
+test_mod_buttons(void)
+{
+       int i;
+       for (i = 0; i < sizeof(int); i++)
+       {
+               int buttons = mod_buttons(0, i, 1);
+               assert(buttons == (1 << i));
+               buttons = mod_buttons(0, i, 0);
+               assert(buttons == 0);
+       }
+
+       assert(mod_buttons(0, sizeof(int), 1) == 0);
+}
+
 int main(int argc, char** argv)
 {
        test_common_ref();
@@ -436,6 +451,7 @@ int main(int argc, char** argv)
        test_suppress();
        test_initial_size();
        test_tilt_to_rotation();
+       test_mod_buttons();
        return 0;
 }
 
-- 
1.7.4


------------------------------------------------------------------------------
Enable your software for Intel(R) Active Management Technology to meet the
growing manageability and security demands of your customers. Businesses
are taking advantage of Intel(R) vPro (TM) technology - will your software 
be a part of the solution? Download the Intel(R) Manageability Checker 
today! http://p.sf.net/sfu/intel-dev2devmar
_______________________________________________
Linuxwacom-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linuxwacom-devel

Reply via email to