Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 52402776 authored by Siarhei Vishniakou's avatar Siarhei Vishniakou
Browse files

Use enum class for InputMessage type

Using enum class helps with type safety, and allows compile-time
detection of missing switch/case statements.

Bug: 70668286
Test: compile-time only
Change-Id: I5ae95cfbf2b38330fa34eca1c9f444a90190cd25
parent a6a58048
Loading
Loading
Loading
Loading
+5 −5
Original line number Diff line number Diff line
@@ -60,14 +60,14 @@ class Parcel;
 * in StructLayout_test should be made.
 */
struct InputMessage {
    enum {
        TYPE_KEY = 1,
        TYPE_MOTION = 2,
        TYPE_FINISHED = 3,
    enum class Type : uint32_t {
        KEY,
        MOTION,
        FINISHED,
    };

    struct Header {
        uint32_t type;
        Type type; // 4 bytes
        // We don't need this field in order to align the body below but we
        // leave it here because InputMessage::size() and other functions
        // compute the size of this structure as sizeof(Header) + sizeof(Body).
+54 −55
Original line number Diff line number Diff line
@@ -93,12 +93,11 @@ inline static bool isPointerEvent(int32_t source) {
bool InputMessage::isValid(size_t actualSize) const {
    if (size() == actualSize) {
        switch (header.type) {
        case TYPE_KEY:
            case Type::KEY:
                return true;
        case TYPE_MOTION:
            return body.motion.pointerCount > 0
                    && body.motion.pointerCount <= MAX_POINTERS;
        case TYPE_FINISHED:
            case Type::MOTION:
                return body.motion.pointerCount > 0 && body.motion.pointerCount <= MAX_POINTERS;
            case Type::FINISHED:
                return true;
        }
    }
@@ -107,11 +106,11 @@ bool InputMessage::isValid(size_t actualSize) const {

size_t InputMessage::size() const {
    switch (header.type) {
    case TYPE_KEY:
        case Type::KEY:
            return sizeof(Header) + body.key.size();
    case TYPE_MOTION:
        case Type::MOTION:
            return sizeof(Header) + body.motion.size();
    case TYPE_FINISHED:
        case Type::FINISHED:
            return sizeof(Header) + body.finished.size();
    }
    return sizeof(Header);
@@ -129,7 +128,7 @@ void InputMessage::getSanitizedCopy(InputMessage* msg) const {

    // Write the body
    switch(header.type) {
        case InputMessage::TYPE_KEY: {
        case InputMessage::Type::KEY: {
            // uint32_t seq
            msg->body.key.seq = body.key.seq;
            // nsecs_t eventTime
@@ -156,7 +155,7 @@ void InputMessage::getSanitizedCopy(InputMessage* msg) const {
            msg->body.key.downTime = body.key.downTime;
            break;
        }
        case InputMessage::TYPE_MOTION: {
        case InputMessage::Type::MOTION: {
            // uint32_t seq
            msg->body.motion.seq = body.motion.seq;
            // nsecs_t eventTime
@@ -212,7 +211,7 @@ void InputMessage::getSanitizedCopy(InputMessage* msg) const {
            }
            break;
        }
        case InputMessage::TYPE_FINISHED: {
        case InputMessage::Type::FINISHED: {
            msg->body.finished.seq = body.finished.seq;
            msg->body.finished.handled = body.finished.handled;
            break;
@@ -457,7 +456,7 @@ status_t InputPublisher::publishKeyEvent(
    }

    InputMessage msg;
    msg.header.type = InputMessage::TYPE_KEY;
    msg.header.type = InputMessage::Type::KEY;
    msg.body.key.seq = seq;
    msg.body.key.deviceId = deviceId;
    msg.body.key.source = source;
@@ -511,7 +510,7 @@ status_t InputPublisher::publishMotionEvent(
    }

    InputMessage msg;
    msg.header.type = InputMessage::TYPE_MOTION;
    msg.header.type = InputMessage::Type::MOTION;
    msg.body.motion.seq = seq;
    msg.body.motion.deviceId = deviceId;
    msg.body.motion.source = source;
@@ -553,7 +552,7 @@ status_t InputPublisher::receiveFinishedSignal(uint32_t* outSeq, bool* outHandle
        *outHandled = false;
        return result;
    }
    if (msg.header.type != InputMessage::TYPE_FINISHED) {
    if (msg.header.type != InputMessage::Type::FINISHED) {
        ALOGE("channel '%s' publisher ~ Received unexpected message of type %d from consumer",
                mChannel->getName().c_str(), msg.header.type);
        return UNKNOWN_ERROR;
@@ -614,7 +613,7 @@ status_t InputConsumer::consume(InputEventFactoryInterface* factory,
        }

        switch (mMsg.header.type) {
        case InputMessage::TYPE_KEY: {
            case InputMessage::Type::KEY: {
                KeyEvent* keyEvent = factory->createKeyEvent();
                if (!keyEvent) return NO_MEMORY;

@@ -628,7 +627,7 @@ status_t InputConsumer::consume(InputEventFactoryInterface* factory,
            break;
            }

        case InputMessage::TYPE_MOTION: {
            case InputMessage::Type::MOTION: {
                ssize_t batchIndex = findBatch(mMsg.body.motion.deviceId, mMsg.body.motion.source);
                if (batchIndex >= 0) {
                    Batch& batch = mBatches.editItemAt(batchIndex);
@@ -653,8 +652,8 @@ status_t InputConsumer::consume(InputEventFactoryInterface* factory,
                        // We cannot append to the batch in progress, so we need to consume
                        // the previous batch right now and defer the new message until later.
                        mMsgDeferred = true;
                    status_t result = consumeSamples(factory,
                            batch, batch.samples.size(), outSeq, outEvent);
                        status_t result = consumeSamples(factory, batch, batch.samples.size(),
                                                         outSeq, outEvent);
                        mBatches.removeAt(batchIndex);
                        if (result) {
                            return result;
@@ -1074,7 +1073,7 @@ status_t InputConsumer::sendFinishedSignal(uint32_t seq, bool handled) {

status_t InputConsumer::sendUnchainedFinishedSignal(uint32_t seq, bool handled) {
    InputMessage msg;
    msg.header.type = InputMessage::TYPE_FINISHED;
    msg.header.type = InputMessage::Type::FINISHED;
    msg.body.finished.seq = seq;
    msg.body.finished.handled = handled;
    return mChannel->sendMessage(&msg);
+4 −4
Original line number Diff line number Diff line
@@ -86,7 +86,7 @@ TEST_F(InputChannelTest, OpenInputChannelPair_ReturnsAPairOfConnectedChannels) {
    // Server->Client communication
    InputMessage serverMsg;
    memset(&serverMsg, 0, sizeof(InputMessage));
    serverMsg.header.type = InputMessage::TYPE_KEY;
    serverMsg.header.type = InputMessage::Type::KEY;
    serverMsg.body.key.action = AKEY_EVENT_ACTION_DOWN;
    EXPECT_EQ(OK, serverChannel->sendMessage(&serverMsg))
            << "server channel should be able to send message to client channel";
@@ -102,7 +102,7 @@ TEST_F(InputChannelTest, OpenInputChannelPair_ReturnsAPairOfConnectedChannels) {
    // Client->Server communication
    InputMessage clientReply;
    memset(&clientReply, 0, sizeof(InputMessage));
    clientReply.header.type = InputMessage::TYPE_FINISHED;
    clientReply.header.type = InputMessage::Type::FINISHED;
    clientReply.body.finished.seq = 0x11223344;
    clientReply.body.finished.handled = true;
    EXPECT_EQ(OK, clientChannel->sendMessage(&clientReply))
@@ -161,7 +161,7 @@ TEST_F(InputChannelTest, SendSignal_WhenPeerClosed_ReturnsAnError) {
    serverChannel.clear(); // close server channel

    InputMessage msg;
    msg.header.type = InputMessage::TYPE_KEY;
    msg.header.type = InputMessage::Type::KEY;
    EXPECT_EQ(DEAD_OBJECT, clientChannel->sendMessage(&msg))
            << "sendMessage should have returned DEAD_OBJECT";
}
@@ -180,7 +180,7 @@ TEST_F(InputChannelTest, SendAndReceive_MotionClassification) {
    };

    InputMessage serverMsg = {}, clientMsg;
    serverMsg.header.type = InputMessage::TYPE_MOTION;
    serverMsg.header.type = InputMessage::Type::MOTION;
    serverMsg.body.motion.seq = 1;
    serverMsg.body.motion.pointerCount = 1;