diff options
| author | 2021-03-05 01:57:08 +0000 | |
|---|---|---|
| committer | 2021-03-05 02:29:16 +0000 | |
| commit | 38b7f7faaede3ad09c55d933e94d28366e2a1cdd (patch) | |
| tree | d637bd5c5ed1d8f6e664de08863e23e14f1739bc | |
| parent | a170ec6a87f2720eb9846ea75cd9807db54b12ad (diff) | |
Use bool directly in InputMessage
In order to send the data via socket, we use the InputMessage struct. In
this struct, various fields are stored. This struct's purpose is to
serialize the input event information.
We are storing bools in InputMessage as various integer types. To read
these bools, we compare the integers to 1. It's not very convenient.
To simplify this, let's store bools directly in InputMessage. Bool size
is implementation-dependent, but it's typically 1, so we can just guard
against that with a static_assert.
Bug: 169866723
Test: presubmit
Change-Id: Iae4870fac95e884cc328791c0035df6e31e34a7b
| -rw-r--r-- | include/input/InputTransport.h | 23 | ||||
| -rw-r--r-- | libs/input/InputTransport.cpp | 16 | ||||
| -rw-r--r-- | libs/input/tests/StructLayout_test.cpp | 10 |
3 files changed, 31 insertions, 18 deletions
diff --git a/include/input/InputTransport.h b/include/input/InputTransport.h index 3b46cb5997..ba9ae204eb 100644 --- a/include/input/InputTransport.h +++ b/include/input/InputTransport.h @@ -33,6 +33,7 @@ #include <unordered_map> #include <android-base/chrono_utils.h> +#include <android-base/unique_fd.h> #include <binder/IBinder.h> #include <binder/Parcelable.h> @@ -44,7 +45,6 @@ #include <utils/RefBase.h> #include <utils/Timers.h> -#include <android-base/unique_fd.h> namespace android { class Parcel; @@ -76,10 +76,15 @@ struct InputMessage { uint32_t seq; } header; - // Body *must* be 8 byte aligned. // For keys and motions, rely on the fact that std::array takes up exactly as much space // as the underlying data. This is not guaranteed by C++, but it simplifies the conversions. static_assert(sizeof(std::array<uint8_t, 32>) == 32); + + // For bool values, rely on the fact that they take up exactly one byte. This is not guaranteed + // by C++ and is implementation-dependent, but it simplifies the conversions. + static_assert(sizeof(bool) == 1); + + // Body *must* be 8 byte aligned. union Body { struct Key { int32_t eventId; @@ -154,8 +159,8 @@ struct InputMessage { } motion; struct Finished { - uint32_t empty1; - uint32_t handled; // actually a bool, but we must maintain 8-byte alignment + bool handled; + uint8_t empty[7]; nsecs_t consumeTime; // The time when the event was consumed by the receiving end inline size_t size() const { return sizeof(Finished); } @@ -163,16 +168,18 @@ struct InputMessage { struct Focus { int32_t eventId; - // 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 + // The following 3 fields take up 4 bytes total + bool hasFocus; + bool inTouchMode; + uint8_t empty[2]; inline size_t size() const { return sizeof(Focus); } } focus; struct Capture { int32_t eventId; - uint32_t pointerCaptureEnabled; // actually a bool, but we maintain 8-byte alignment + bool pointerCaptureEnabled; + uint8_t empty[3]; inline size_t size() const { return sizeof(Capture); } } capture; diff --git a/libs/input/InputTransport.cpp b/libs/input/InputTransport.cpp index 1e47a3c3e6..ee2daec5fc 100644 --- a/libs/input/InputTransport.cpp +++ b/libs/input/InputTransport.cpp @@ -577,8 +577,8 @@ status_t InputPublisher::publishFocusEvent(uint32_t seq, int32_t eventId, bool h msg.header.type = InputMessage::Type::FOCUS; msg.header.seq = seq; msg.body.focus.eventId = eventId; - msg.body.focus.hasFocus = hasFocus ? 1 : 0; - msg.body.focus.inTouchMode = inTouchMode ? 1 : 0; + msg.body.focus.hasFocus = hasFocus; + msg.body.focus.inTouchMode = inTouchMode; return mChannel->sendMessage(&msg); } @@ -595,7 +595,7 @@ status_t InputPublisher::publishCaptureEvent(uint32_t seq, int32_t eventId, msg.header.type = InputMessage::Type::CAPTURE; msg.header.seq = seq; msg.body.capture.eventId = eventId; - msg.body.capture.pointerCaptureEnabled = pointerCaptureEnabled ? 1 : 0; + msg.body.capture.pointerCaptureEnabled = pointerCaptureEnabled; return mChannel->sendMessage(&msg); } @@ -615,7 +615,7 @@ status_t InputPublisher::receiveFinishedSignal( mChannel->getName().c_str(), NamedEnum::string(msg.header.type).c_str()); return UNKNOWN_ERROR; } - callback(msg.header.seq, msg.body.finished.handled == 1, msg.body.finished.consumeTime); + callback(msg.header.seq, msg.body.finished.handled, msg.body.finished.consumeTime); return OK; } @@ -1168,7 +1168,7 @@ status_t InputConsumer::sendUnchainedFinishedSignal(uint32_t seq, bool handled) InputMessage msg; msg.header.type = InputMessage::Type::FINISHED; msg.header.seq = seq; - msg.body.finished.handled = handled ? 1 : 0; + msg.body.finished.handled = handled; msg.body.finished.consumeTime = getConsumeTime(seq); status_t result = mChannel->sendMessage(&msg); if (result == OK) { @@ -1228,12 +1228,12 @@ void InputConsumer::initializeKeyEvent(KeyEvent* event, const InputMessage* msg) } void InputConsumer::initializeFocusEvent(FocusEvent* event, const InputMessage* msg) { - event->initialize(msg->body.focus.eventId, msg->body.focus.hasFocus == 1, - msg->body.focus.inTouchMode == 1); + event->initialize(msg->body.focus.eventId, msg->body.focus.hasFocus, + msg->body.focus.inTouchMode); } void InputConsumer::initializeCaptureEvent(CaptureEvent* event, const InputMessage* msg) { - event->initialize(msg->body.capture.eventId, msg->body.capture.pointerCaptureEnabled == 1); + event->initialize(msg->body.capture.eventId, msg->body.capture.pointerCaptureEnabled); } void InputConsumer::initializeMotionEvent(MotionEvent* event, const InputMessage* msg) { diff --git a/libs/input/tests/StructLayout_test.cpp b/libs/input/tests/StructLayout_test.cpp index a8865858d3..8f436085f7 100644 --- a/libs/input/tests/StructLayout_test.cpp +++ b/libs/input/tests/StructLayout_test.cpp @@ -49,6 +49,7 @@ void TestInputMessageAlignment() { CHECK_OFFSET(InputMessage::Body::Key, downTime, 88); CHECK_OFFSET(InputMessage::Body::Motion, eventId, 0); + CHECK_OFFSET(InputMessage::Body::Motion, empty1, 4); CHECK_OFFSET(InputMessage::Body::Motion, eventTime, 8); CHECK_OFFSET(InputMessage::Body::Motion, deviceId, 16); CHECK_OFFSET(InputMessage::Body::Motion, source, 20); @@ -60,6 +61,7 @@ void TestInputMessageAlignment() { CHECK_OFFSET(InputMessage::Body::Motion, metaState, 72); CHECK_OFFSET(InputMessage::Body::Motion, buttonState, 76); CHECK_OFFSET(InputMessage::Body::Motion, classification, 80); + CHECK_OFFSET(InputMessage::Body::Motion, empty2, 81); CHECK_OFFSET(InputMessage::Body::Motion, edgeFlags, 84); CHECK_OFFSET(InputMessage::Body::Motion, downTime, 88); CHECK_OFFSET(InputMessage::Body::Motion, dsdx, 96); @@ -73,16 +75,20 @@ void TestInputMessageAlignment() { CHECK_OFFSET(InputMessage::Body::Motion, xCursorPosition, 128); CHECK_OFFSET(InputMessage::Body::Motion, yCursorPosition, 132); CHECK_OFFSET(InputMessage::Body::Motion, pointerCount, 136); + CHECK_OFFSET(InputMessage::Body::Motion, empty3, 140); CHECK_OFFSET(InputMessage::Body::Motion, pointers, 144); CHECK_OFFSET(InputMessage::Body::Focus, eventId, 0); CHECK_OFFSET(InputMessage::Body::Focus, hasFocus, 4); - CHECK_OFFSET(InputMessage::Body::Focus, inTouchMode, 6); + CHECK_OFFSET(InputMessage::Body::Focus, inTouchMode, 5); + CHECK_OFFSET(InputMessage::Body::Focus, empty, 6); CHECK_OFFSET(InputMessage::Body::Capture, eventId, 0); CHECK_OFFSET(InputMessage::Body::Capture, pointerCaptureEnabled, 4); + CHECK_OFFSET(InputMessage::Body::Capture, empty, 5); - CHECK_OFFSET(InputMessage::Body::Finished, handled, 4); + CHECK_OFFSET(InputMessage::Body::Finished, handled, 0); + CHECK_OFFSET(InputMessage::Body::Finished, empty, 1); CHECK_OFFSET(InputMessage::Body::Finished, consumeTime, 8); } |