Add FocusEvent and InputMessage::Type::FOCUS
FocusEvents will be consumed by InputConsumer on the app side. They
will be used to notify app that focus has been gained / lost. They will
also carry information about the current state of touch mode.
Also add a new type of InputMessage with type FOCUS.
This new data structure will be used to pass focus events to the apps
from input across the socket.
Bug: 70668286
Test: presubmit
Change-Id: I88582c64ee41ecb49623b9b7f5c149eafa694788
diff --git a/include/input/Input.h b/include/input/Input.h
index a7e706e..f871847 100644
--- a/include/input/Input.h
+++ b/include/input/Input.h
@@ -167,6 +167,8 @@
class Parcel;
#endif
+const char* inputEventTypeToString(int32_t type);
+
/*
* Flags that flow alongside events in the input dispatch system to help with certain
* policy decisions such as waking from device sleep.
@@ -687,6 +689,28 @@
};
/*
+ * Focus events.
+ */
+class FocusEvent : public InputEvent {
+public:
+ virtual ~FocusEvent() {}
+
+ virtual int32_t getType() const override { return AINPUT_EVENT_TYPE_FOCUS; }
+
+ inline bool getHasFocus() const { return mHasFocus; }
+
+ inline bool getInTouchMode() const { return mInTouchMode; }
+
+ void initialize(bool hasFocus, bool inTouchMode);
+
+ void initialize(const FocusEvent& from);
+
+protected:
+ bool mHasFocus;
+ bool mInTouchMode;
+};
+
+/*
* Input event factory.
*/
class InputEventFactoryInterface {
@@ -698,6 +722,7 @@
virtual KeyEvent* createKeyEvent() = 0;
virtual MotionEvent* createMotionEvent() = 0;
+ virtual FocusEvent* createFocusEvent() = 0;
};
/*
@@ -711,10 +736,12 @@
virtual KeyEvent* createKeyEvent() override { return &mKeyEvent; }
virtual MotionEvent* createMotionEvent() override { return &mMotionEvent; }
+ virtual FocusEvent* createFocusEvent() override { return &mFocusEvent; }
private:
KeyEvent mKeyEvent;
MotionEvent mMotionEvent;
+ FocusEvent mFocusEvent;
};
/*
@@ -727,6 +754,7 @@
virtual KeyEvent* createKeyEvent() override;
virtual MotionEvent* createMotionEvent() override;
+ virtual FocusEvent* createFocusEvent() override;
void recycle(InputEvent* event);
@@ -735,6 +763,7 @@
std::queue<std::unique_ptr<KeyEvent>> mKeyEventPool;
std::queue<std::unique_ptr<MotionEvent>> mMotionEventPool;
+ std::queue<std::unique_ptr<FocusEvent>> mFocusEventPool;
};
} // namespace android
diff --git a/include/input/InputTransport.h b/include/input/InputTransport.h
index d39ee25..ae47438 100644
--- a/include/input/InputTransport.h
+++ b/include/input/InputTransport.h
@@ -64,6 +64,7 @@
KEY,
MOTION,
FINISHED,
+ FOCUS,
};
struct Header {
@@ -92,9 +93,7 @@
uint32_t empty2;
nsecs_t downTime __attribute__((aligned(8)));
- inline size_t size() const {
- return sizeof(Key);
- }
+ inline size_t size() const { return sizeof(Key); }
} key;
struct Motion {
@@ -110,7 +109,7 @@
int32_t metaState;
int32_t buttonState;
MotionClassification classification; // base type: uint8_t
- uint8_t empty2[3];
+ uint8_t empty2[3]; // 3 bytes to fill gap created by classification
int32_t edgeFlags;
nsecs_t downTime __attribute__((aligned(8)));
float xOffset;
@@ -148,10 +147,17 @@
uint32_t seq;
uint32_t handled; // actually a bool, but we must maintain 8-byte alignment
- inline size_t size() const {
- return sizeof(Finished);
- }
+ inline size_t size() const { return sizeof(Finished); }
} finished;
+
+ struct Focus {
+ uint32_t seq;
+ // The following two fields take up 4 bytes total
+ uint16_t hasFocus; // actually a bool
+ uint16_t inTouchMode; // actually a bool, but we must maintain 8-byte alignment
+
+ inline size_t size() const { return sizeof(Focus); }
+ } focus;
} __attribute__((aligned(8))) body;
bool isValid(size_t actualSize) const;
@@ -294,6 +300,15 @@
uint32_t pointerCount, const PointerProperties* pointerProperties,
const PointerCoords* pointerCoords);
+ /* Publishes a focus event to the input channel.
+ *
+ * Returns OK on success.
+ * Returns WOULD_BLOCK if the channel is full.
+ * Returns DEAD_OBJECT if the channel's peer has been closed.
+ * Other errors probably indicate that the channel is broken.
+ */
+ status_t publishFocusEvent(uint32_t seq, bool hasFocus, bool inTouchMode);
+
/* Receives the finished signal from the consumer in reply to the original dispatch signal.
* If a signal was received, returns the message sequence number,
* and whether the consumer handled the message.
@@ -349,8 +364,8 @@
* Returns NO_MEMORY if the event could not be created.
* Other errors probably indicate that the channel is broken.
*/
- status_t consume(InputEventFactoryInterface* factory, bool consumeBatches,
- nsecs_t frameTime, uint32_t* outSeq, InputEvent** outEvent);
+ status_t consume(InputEventFactoryInterface* factory, bool consumeBatches, nsecs_t frameTime,
+ uint32_t* outSeq, InputEvent** outEvent);
/* Sends a finished signal to the publisher to inform it that the message
* with the specified sequence number has finished being process and whether
@@ -521,6 +536,7 @@
static void rewriteMessage(TouchState& state, InputMessage& msg);
static void initializeKeyEvent(KeyEvent* event, const InputMessage* msg);
static void initializeMotionEvent(MotionEvent* event, const InputMessage* msg);
+ static void initializeFocusEvent(FocusEvent* event, const InputMessage* msg);
static void addSample(MotionEvent* event, const InputMessage* msg);
static bool canAddSample(const Batch& batch, const InputMessage* msg);
static ssize_t findSampleNoLaterThan(const Batch& batch, nsecs_t time);