[PATCH evdev v2] type-safe inline functions for bitmask manipulation
Max Schwarz
Max at x-quadraht.de
Mon Oct 17 11:34:53 PDT 2011
We can't use BitIsSet/SetBit from the server (inputstr.h) since they
operate on byte arrays. EvdevSetBit is added in preparation for the
"smooth-scrolling on wheel emulation" patch.
Signed-off-by: Max Schwarz <Max at x-quadraht.de>
---
sorry, forgot to actually make EvdevBitIsSet/EvdevSetBit inline...
src/evdev.c | 79 +++++++++++++++++++++++++++++++++-------------------------
1 files changed, 45 insertions(+), 34 deletions(-)
diff --git a/src/evdev.c b/src/evdev.c
index 2281206..69a8750 100644
--- a/src/evdev.c
+++ b/src/evdev.c
@@ -107,6 +107,9 @@ static BOOL EvdevGrabDevice(InputInfoPtr pInfo, int grab, int ungrab);
static void EvdevSetCalibration(InputInfoPtr pInfo, int num_calibration, int calibration[4]);
static int EvdevOpenDevice(InputInfoPtr pInfo);
+static int EvdevBitIsSet(const unsigned long *array, int bit);
+static void EvdevSetBit(unsigned long *array, int bit);
+
static void EvdevInitAxesLabels(EvdevPtr pEvdev, int natoms, Atom *atoms);
static void EvdevInitButtonLabels(EvdevPtr pEvdev, int natoms, Atom *atoms);
static void EvdevInitProperty(DeviceIntPtr dev);
@@ -174,6 +177,16 @@ static size_t EvdevCountBits(unsigned long *array, size_t nlongs)
return count;
}
+inline int EvdevBitIsSet(const unsigned long *array, int bit)
+{
+ return array[bit / LONG_BITS] & (1LL << (bit % LONG_BITS));
+}
+
+inline void EvdevSetBit(unsigned long *array, int bit)
+{
+ array[bit / LONG_BITS] |= (1LL << (bit % LONG_BITS));
+}
+
static int
EvdevGetMajorMinor(InputInfoPtr pInfo)
{
@@ -895,8 +908,6 @@ EvdevReadInput(InputInfoPtr pInfo)
}
}
-#define TestBit(bit, array) ((array[(bit) / LONG_BITS]) & (1L << ((bit) % LONG_BITS)))
-
static void
EvdevPtrCtrlProc(DeviceIntPtr device, PtrCtrl *ctrl)
{
@@ -972,7 +983,7 @@ EvdevAddAbsValuatorClass(DeviceIntPtr device)
pInfo = device->public.devicePrivate;
pEvdev = pInfo->private;
- if (!TestBit(EV_ABS, pEvdev->bitmask))
+ if (!EvdevBitIsSet(pEvdev->bitmask, EV_ABS))
goto out;
num_axes = EvdevCountBits(pEvdev->abs_bitmask, NLONGS(ABS_MAX));
@@ -997,7 +1008,7 @@ EvdevAddAbsValuatorClass(DeviceIntPtr device)
for (axis = ABS_X; i < MAX_VALUATORS && axis <= ABS_MAX; axis++) {
pEvdev->axis_map[axis] = -1;
- if (!TestBit(axis, pEvdev->abs_bitmask))
+ if (!EvdevBitIsSet(pEvdev->abs_bitmask, axis))
continue;
pEvdev->axis_map[axis] = i;
i++;
@@ -1039,7 +1050,7 @@ EvdevAddAbsValuatorClass(DeviceIntPtr device)
if (!pEvdev->use_proximity)
break;
- if (TestBit(proximity_bits[i], pEvdev->key_bitmask))
+ if (EvdevBitIsSet(pEvdev->key_bitmask, proximity_bits[i]))
{
InitProximityClassDeviceStruct(device);
pEvdev->prox = valuator_mask_new(num_axes);
@@ -1096,7 +1107,7 @@ EvdevAddRelValuatorClass(DeviceIntPtr device)
pInfo = device->public.devicePrivate;
pEvdev = pInfo->private;
- if (!TestBit(EV_REL, pEvdev->bitmask))
+ if (!EvdevBitIsSet(pEvdev->bitmask, EV_REL))
goto out;
num_axes = EvdevCountBits(pEvdev->rel_bitmask, NLONGS(REL_MAX));
@@ -1106,11 +1117,11 @@ EvdevAddRelValuatorClass(DeviceIntPtr device)
#ifndef HAVE_SMOOTH_SCROLLING
/* Wheels are special, we post them as button events. So let's ignore them
* in the axes list too */
- if (TestBit(REL_WHEEL, pEvdev->rel_bitmask))
+ if (EvdevBitIsSet(pEvdev->rel_bitmask, REL_WHEEL))
num_axes--;
- if (TestBit(REL_HWHEEL, pEvdev->rel_bitmask))
+ if (EvdevBitIsSet(pEvdev->rel_bitmask, REL_HWHEEL))
num_axes--;
- if (TestBit(REL_DIAL, pEvdev->rel_bitmask))
+ if (EvdevBitIsSet(pEvdev->rel_bitmask, REL_DIAL))
num_axes--;
if (num_axes <= 0)
@@ -1138,7 +1149,7 @@ EvdevAddRelValuatorClass(DeviceIntPtr device)
if (axis == REL_WHEEL || axis == REL_HWHEEL || axis == REL_DIAL)
continue;
#endif
- if (!TestBit(axis, pEvdev->rel_bitmask))
+ if (!EvdevBitIsSet(pEvdev->rel_bitmask, axis))
continue;
pEvdev->axis_map[axis] = i;
i++;
@@ -1542,7 +1553,7 @@ EvdevCache(InputInfoPtr pInfo)
* to be static, always refresh it in evdev structure.
*/
for (i = ABS_X; i <= ABS_MAX; i++) {
- if (TestBit(i, abs_bitmask)) {
+ if (EvdevBitIsSet(abs_bitmask, i)) {
len = ioctl(pInfo->fd, EVIOCGABS(i), &pEvdev->absinfo[i]);
if (len < 0) {
xf86IDrvMsg(pInfo, X_ERROR, "ioctl EVIOCGABSi(%d) failed: %s\n",
@@ -1640,7 +1651,7 @@ EvdevProbe(InputInfoPtr pInfo)
for (i = BTN_MISC; i < BTN_JOYSTICK; i++)
{
int mapping = 0;
- if (TestBit(i, pEvdev->key_bitmask))
+ if (EvdevBitIsSet(pEvdev->key_bitmask, i))
{
mapping = EvdevUtilButtonEventToButtonNumber(pEvdev, i);
if (mapping > num_buttons)
@@ -1648,9 +1659,9 @@ EvdevProbe(InputInfoPtr pInfo)
}
}
- has_lmr = TestBit(BTN_LEFT, pEvdev->key_bitmask) ||
- TestBit(BTN_MIDDLE, pEvdev->key_bitmask) ||
- TestBit(BTN_RIGHT, pEvdev->key_bitmask);
+ has_lmr = EvdevBitIsSet(pEvdev->key_bitmask, BTN_LEFT) ||
+ EvdevBitIsSet(pEvdev->key_bitmask, BTN_MIDDLE) ||
+ EvdevBitIsSet(pEvdev->key_bitmask, BTN_RIGHT);
if (num_buttons)
{
@@ -1660,16 +1671,16 @@ EvdevProbe(InputInfoPtr pInfo)
}
for (i = 0; i < REL_MAX; i++) {
- if (TestBit(i, pEvdev->rel_bitmask)) {
+ if (EvdevBitIsSet(pEvdev->rel_bitmask, i)) {
has_rel_axes = TRUE;
break;
}
}
if (has_rel_axes) {
- if (TestBit(REL_WHEEL, pEvdev->rel_bitmask) ||
- TestBit(REL_HWHEEL, pEvdev->rel_bitmask) ||
- TestBit(REL_DIAL, pEvdev->rel_bitmask)) {
+ if (EvdevBitIsSet(pEvdev->rel_bitmask, REL_WHEEL) ||
+ EvdevBitIsSet(pEvdev->rel_bitmask, REL_HWHEEL) ||
+ EvdevBitIsSet(pEvdev->rel_bitmask, REL_DIAL)) {
xf86IDrvMsg(pInfo, X_PROBED, "Found scroll wheel(s)\n");
has_scroll = TRUE;
if (!num_buttons)
@@ -1684,8 +1695,8 @@ EvdevProbe(InputInfoPtr pInfo)
xf86IDrvMsg(pInfo, X_PROBED, "Found relative axes\n");
pEvdev->flags |= EVDEV_RELATIVE_EVENTS;
- if (TestBit(REL_X, pEvdev->rel_bitmask) &&
- TestBit(REL_Y, pEvdev->rel_bitmask)) {
+ if (EvdevBitIsSet(pEvdev->rel_bitmask, REL_X) &&
+ EvdevBitIsSet(pEvdev->rel_bitmask, REL_Y)) {
xf86IDrvMsg(pInfo, X_PROBED, "Found x and y relative axes\n");
}
} else {
@@ -1695,7 +1706,7 @@ EvdevProbe(InputInfoPtr pInfo)
}
for (i = 0; i < ABS_MAX; i++) {
- if (TestBit(i, pEvdev->abs_bitmask)) {
+ if (EvdevBitIsSet(pEvdev->abs_bitmask, i)) {
has_abs_axes = TRUE;
break;
}
@@ -1709,12 +1720,12 @@ EvdevProbe(InputInfoPtr pInfo)
xf86IDrvMsg(pInfo, X_PROBED, "Found absolute axes\n");
pEvdev->flags |= EVDEV_ABSOLUTE_EVENTS;
- if ((TestBit(ABS_X, pEvdev->abs_bitmask) &&
- TestBit(ABS_Y, pEvdev->abs_bitmask))) {
+ if ((EvdevBitIsSet(pEvdev->abs_bitmask, ABS_X) &&
+ EvdevBitIsSet(pEvdev->abs_bitmask, ABS_Y))) {
xf86IDrvMsg(pInfo, X_PROBED, "Found x and y absolute axes\n");
- if (TestBit(BTN_TOOL_PEN, pEvdev->key_bitmask) ||
- TestBit(BTN_STYLUS, pEvdev->key_bitmask) ||
- TestBit(BTN_STYLUS2, pEvdev->key_bitmask))
+ if (EvdevBitIsSet(pEvdev->key_bitmask, BTN_TOOL_PEN) ||
+ EvdevBitIsSet(pEvdev->key_bitmask, BTN_STYLUS) ||
+ EvdevBitIsSet(pEvdev->key_bitmask, BTN_STYLUS2))
{
xf86IDrvMsg(pInfo, X_PROBED, "Found absolute tablet.\n");
pEvdev->flags |= EVDEV_TABLET;
@@ -1723,9 +1734,9 @@ EvdevProbe(InputInfoPtr pInfo)
pEvdev->num_buttons = 7; /* LMR + scroll wheels */
pEvdev->flags |= EVDEV_BUTTON_EVENTS;
}
- } else if (TestBit(ABS_PRESSURE, pEvdev->abs_bitmask) ||
- TestBit(BTN_TOUCH, pEvdev->key_bitmask)) {
- if (has_lmr || TestBit(BTN_TOOL_FINGER, pEvdev->key_bitmask)) {
+ } else if (EvdevBitIsSet(pEvdev->abs_bitmask, ABS_PRESSURE) ||
+ EvdevBitIsSet(pEvdev->key_bitmask, BTN_TOUCH)) {
+ if (has_lmr || EvdevBitIsSet(pEvdev->key_bitmask, BTN_TOOL_FINGER)) {
xf86IDrvMsg(pInfo, X_PROBED, "Found absolute touchpad.\n");
pEvdev->flags |= EVDEV_TOUCHPAD;
} else {
@@ -1733,8 +1744,8 @@ EvdevProbe(InputInfoPtr pInfo)
pEvdev->flags |= EVDEV_TOUCHSCREEN;
pEvdev->flags |= EVDEV_BUTTON_EVENTS;
}
- } else if (!(TestBit(REL_X, pEvdev->rel_bitmask) &&
- TestBit(REL_Y, pEvdev->rel_bitmask)) && has_lmr) {
+ } else if (!(EvdevBitIsSet(pEvdev->rel_bitmask, REL_X) &&
+ EvdevBitIsSet(pEvdev->rel_bitmask, REL_Y)) && has_lmr) {
/* some touchscreens use BTN_LEFT rather than BTN_TOUCH */
xf86IDrvMsg(pInfo, X_PROBED, "Found absolute touchscreen\n");
pEvdev->flags |= EVDEV_TOUCHSCREEN;
@@ -1744,7 +1755,7 @@ EvdevProbe(InputInfoPtr pInfo)
}
for (i = 0; i < BTN_MISC; i++) {
- if (TestBit(i, pEvdev->key_bitmask)) {
+ if (EvdevBitIsSet(pEvdev->key_bitmask, i)) {
xf86IDrvMsg(pInfo, X_PROBED, "Found keys\n");
pEvdev->flags |= EVDEV_KEYBOARD_EVENTS;
has_keys = TRUE;
@@ -2247,7 +2258,7 @@ static void EvdevInitButtonLabels(EvdevPtr pEvdev, int natoms, Atom *atoms)
for (button = BTN_MISC; button < BTN_JOYSTICK; button++)
{
- if (TestBit(button, pEvdev->key_bitmask))
+ if (EvdevBitIsSet(pEvdev->key_bitmask, button))
{
int group = (button % 0x100)/16;
int idx = button - ((button/16) * 16);
--
1.7.5.4
More information about the xorg-devel
mailing list