Skip to content

Commit 00710e9

Browse files
author
Jeff Brown
committed
Make InputDevice.SOURCE_STYLUS meaningful.
Bug: 5424551 Change-Id: I415cb1842422e050cc41b17f5a1f13b4fab17a44
1 parent 6ec0c6a commit 00710e9

File tree

3 files changed

+65
-6
lines changed

3 files changed

+65
-6
lines changed

core/java/android/view/InputDevice.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,19 @@ public final class InputDevice implements Parcelable {
167167

168168
/**
169169
* The input source is a stylus pointing device.
170+
* <p>
171+
* Note that this bit merely indicates that an input device is capable of obtaining
172+
* input from a stylus. To determine whether a given touch event was produced
173+
* by a stylus, examine the tool type returned by {@link MotionEvent#getToolType(int)}
174+
* for each individual pointer.
175+
* </p><p>
176+
* A single touch event may multiple pointers with different tool types,
177+
* such as an event that has one pointer with tool type
178+
* {@link MotionEvent#TOOL_TYPE_FINGER} and another pointer with tool type
179+
* {@link MotionEvent#TOOL_TYPE_STYLUS}. So it is important to examine
180+
* the tool type of each pointer, regardless of the source reported
181+
* by {@link MotionEvent#getSource()}.
182+
* </p>
170183
*
171184
* @see #SOURCE_CLASS_POINTER
172185
*/

services/input/InputReader.cpp

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,12 +1292,17 @@ void CursorScrollAccumulator::finishSync() {
12921292
// --- TouchButtonAccumulator ---
12931293

12941294
TouchButtonAccumulator::TouchButtonAccumulator() :
1295-
mHaveBtnTouch(false) {
1295+
mHaveBtnTouch(false), mHaveStylus(false) {
12961296
clearButtons();
12971297
}
12981298

12991299
void TouchButtonAccumulator::configure(InputDevice* device) {
13001300
mHaveBtnTouch = device->hasKey(BTN_TOUCH);
1301+
mHaveStylus = device->hasKey(BTN_TOOL_PEN)
1302+
|| device->hasKey(BTN_TOOL_RUBBER)
1303+
|| device->hasKey(BTN_TOOL_BRUSH)
1304+
|| device->hasKey(BTN_TOOL_PENCIL)
1305+
|| device->hasKey(BTN_TOOL_AIRBRUSH);
13011306
}
13021307

13031308
void TouchButtonAccumulator::reset(InputDevice* device) {
@@ -1421,6 +1426,10 @@ bool TouchButtonAccumulator::isHovering() const {
14211426
return mHaveBtnTouch && !mBtnTouch;
14221427
}
14231428

1429+
bool TouchButtonAccumulator::hasStylus() const {
1430+
return mHaveStylus;
1431+
}
1432+
14241433

14251434
// --- RawPointerAxes ---
14261435

@@ -1572,16 +1581,19 @@ void SingleTouchMotionAccumulator::process(const RawEvent* rawEvent) {
15721581
// --- MultiTouchMotionAccumulator ---
15731582

15741583
MultiTouchMotionAccumulator::MultiTouchMotionAccumulator() :
1575-
mCurrentSlot(-1), mSlots(NULL), mSlotCount(0), mUsingSlotsProtocol(false) {
1584+
mCurrentSlot(-1), mSlots(NULL), mSlotCount(0), mUsingSlotsProtocol(false),
1585+
mHaveStylus(false) {
15761586
}
15771587

15781588
MultiTouchMotionAccumulator::~MultiTouchMotionAccumulator() {
15791589
delete[] mSlots;
15801590
}
15811591

1582-
void MultiTouchMotionAccumulator::configure(size_t slotCount, bool usingSlotsProtocol) {
1592+
void MultiTouchMotionAccumulator::configure(InputDevice* device,
1593+
size_t slotCount, bool usingSlotsProtocol) {
15831594
mSlotCount = slotCount;
15841595
mUsingSlotsProtocol = usingSlotsProtocol;
1596+
mHaveStylus = device->hasAbsoluteAxis(ABS_MT_TOOL_TYPE);
15851597

15861598
delete[] mSlots;
15871599
mSlots = new Slot[slotCount];
@@ -1713,6 +1725,10 @@ void MultiTouchMotionAccumulator::finishSync() {
17131725
}
17141726
}
17151727

1728+
bool MultiTouchMotionAccumulator::hasStylus() const {
1729+
return mHaveStylus;
1730+
}
1731+
17161732

17171733
// --- MultiTouchMotionAccumulator::Slot ---
17181734

@@ -2872,10 +2888,16 @@ void TouchInputMapper::configureSurface(nsecs_t when, bool* outResetNeeded) {
28722888
&& mConfig.pointerGesturesEnabled) {
28732889
mSource = AINPUT_SOURCE_MOUSE;
28742890
mDeviceMode = DEVICE_MODE_POINTER;
2891+
if (hasStylus()) {
2892+
mSource |= AINPUT_SOURCE_STYLUS;
2893+
}
28752894
} else if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
28762895
&& mParameters.associatedDisplayId >= 0) {
28772896
mSource = AINPUT_SOURCE_TOUCHSCREEN;
28782897
mDeviceMode = DEVICE_MODE_DIRECT;
2898+
if (hasStylus()) {
2899+
mSource |= AINPUT_SOURCE_STYLUS;
2900+
}
28792901
} else {
28802902
mSource = AINPUT_SOURCE_TOUCHPAD;
28812903
mDeviceMode = DEVICE_MODE_UNSCALED;
@@ -5786,6 +5808,10 @@ void SingleTouchInputMapper::configureRawPointerAxes() {
57865808
getAbsoluteAxisInfo(ABS_TILT_Y, &mRawPointerAxes.tiltY);
57875809
}
57885810

5811+
bool SingleTouchInputMapper::hasStylus() const {
5812+
return mTouchButtonAccumulator.hasStylus();
5813+
}
5814+
57895815

57905816
// --- MultiTouchInputMapper ---
57915817

@@ -5920,12 +5946,19 @@ void MultiTouchInputMapper::configureRawPointerAxes() {
59205946
getDeviceName().string(), slotCount, MAX_SLOTS);
59215947
slotCount = MAX_SLOTS;
59225948
}
5923-
mMultiTouchMotionAccumulator.configure(slotCount, true /*usingSlotsProtocol*/);
5949+
mMultiTouchMotionAccumulator.configure(getDevice(),
5950+
slotCount, true /*usingSlotsProtocol*/);
59245951
} else {
5925-
mMultiTouchMotionAccumulator.configure(MAX_POINTERS, false /*usingSlotsProtocol*/);
5952+
mMultiTouchMotionAccumulator.configure(getDevice(),
5953+
MAX_POINTERS, false /*usingSlotsProtocol*/);
59265954
}
59275955
}
59285956

5957+
bool MultiTouchInputMapper::hasStylus() const {
5958+
return mMultiTouchMotionAccumulator.hasStylus()
5959+
|| mTouchButtonAccumulator.hasStylus();
5960+
}
5961+
59295962

59305963
// --- JoystickInputMapper ---
59315964

services/input/InputReader.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,12 @@ class InputDevice {
507507
return getEventHub()->hasScanCode(mId, code);
508508
}
509509

510+
bool hasAbsoluteAxis(int32_t code) {
511+
RawAbsoluteAxisInfo info;
512+
getEventHub()->getAbsoluteAxisInfo(mId, code, &info);
513+
return info.valid;
514+
}
515+
510516
bool isKeyPressed(int32_t code) {
511517
return getEventHub()->getScanCodeState(mId, code) == AKEY_STATE_DOWN;
512518
}
@@ -627,9 +633,11 @@ class TouchButtonAccumulator {
627633
int32_t getToolType() const;
628634
bool isToolActive() const;
629635
bool isHovering() const;
636+
bool hasStylus() const;
630637

631638
private:
632639
bool mHaveBtnTouch;
640+
bool mHaveStylus;
633641

634642
bool mBtnTouch;
635643
bool mBtnStylus;
@@ -817,10 +825,11 @@ class MultiTouchMotionAccumulator {
817825
MultiTouchMotionAccumulator();
818826
~MultiTouchMotionAccumulator();
819827

820-
void configure(size_t slotCount, bool usingSlotsProtocol);
828+
void configure(InputDevice* device, size_t slotCount, bool usingSlotsProtocol);
821829
void reset(InputDevice* device);
822830
void process(const RawEvent* rawEvent);
823831
void finishSync();
832+
bool hasStylus() const;
824833

825834
inline size_t getSlotCount() const { return mSlotCount; }
826835
inline const Slot* getSlot(size_t index) const { return &mSlots[index]; }
@@ -830,6 +839,7 @@ class MultiTouchMotionAccumulator {
830839
Slot* mSlots;
831840
size_t mSlotCount;
832841
bool mUsingSlotsProtocol;
842+
bool mHaveStylus;
833843

834844
void clearSlots(int32_t initialSlot);
835845
};
@@ -1257,6 +1267,7 @@ class TouchInputMapper : public InputMapper {
12571267
virtual void parseCalibration();
12581268
virtual void resolveCalibration();
12591269
virtual void dumpCalibration(String8& dump);
1270+
virtual bool hasStylus() const = 0;
12601271

12611272
virtual void syncTouch(nsecs_t when, bool* outHavePointerIds) = 0;
12621273

@@ -1605,6 +1616,7 @@ class SingleTouchInputMapper : public TouchInputMapper {
16051616
protected:
16061617
virtual void syncTouch(nsecs_t when, bool* outHavePointerIds);
16071618
virtual void configureRawPointerAxes();
1619+
virtual bool hasStylus() const;
16081620

16091621
private:
16101622
SingleTouchMotionAccumulator mSingleTouchMotionAccumulator;
@@ -1622,6 +1634,7 @@ class MultiTouchInputMapper : public TouchInputMapper {
16221634
protected:
16231635
virtual void syncTouch(nsecs_t when, bool* outHavePointerIds);
16241636
virtual void configureRawPointerAxes();
1637+
virtual bool hasStylus() const;
16251638

16261639
private:
16271640
MultiTouchMotionAccumulator mMultiTouchMotionAccumulator;

0 commit comments

Comments
 (0)