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

Commit 7789005e authored by Prabir Pradhan's avatar Prabir Pradhan Committed by Automerger Merge Worker
Browse files

Uinput: Use enums for commands am: 895fe861

parents 5f9578a2 895fe861
Loading
Loading
Loading
Loading
+37 −18
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Objects;
import java.util.stream.IntStream;

import src.com.android.commands.uinput.InputAbsInfo;
@@ -36,11 +37,20 @@ import src.com.android.commands.uinput.InputAbsInfo;
public class Event {
    private static final String TAG = "UinputEvent";

    public static final String COMMAND_REGISTER = "register";
    public static final String COMMAND_DELAY = "delay";
    public static final String COMMAND_INJECT = "inject";
    private static final int ABS_CNT = 64;

    enum Command {
        REGISTER("register"),
        DELAY("delay"),
        INJECT("inject");

        final String mCommandName;

        Command(String command) {
            mCommandName = command;
        }
    }

    // These constants come from "include/uapi/linux/input.h" in the kernel
    enum Bus {
        USB(0x03), BLUETOOTH(0x05);
@@ -56,7 +66,7 @@ public class Event {
    }

    private int mId;
    private String mCommand;
    private Command mCommand;
    private String mName;
    private int mVid;
    private int mPid;
@@ -72,7 +82,7 @@ public class Event {
        return mId;
    }

    public String getCommand() {
    public Command getCommand() {
        return mCommand;
    }

@@ -146,7 +156,14 @@ public class Event {
        }

        private void setCommand(String command) {
            mEvent.mCommand = command;
            Objects.requireNonNull(command, "Command must not be null");
            for (Command cmd : Command.values()) {
                if (cmd.mCommandName.equals(command)) {
                    mEvent.mCommand = cmd;
                    return;
                }
            }
            throw new IllegalStateException("Unrecognized command: " + command);
        }

        public void setName(String name) {
@@ -195,21 +212,23 @@ public class Event {
            } else if (mEvent.mCommand == null) {
                throw new IllegalStateException("Event does not contain a command");
            }
            if (COMMAND_REGISTER.equals(mEvent.mCommand)) {
            switch (mEvent.mCommand) {
                case REGISTER -> {
                    if (mEvent.mConfiguration == null) {
                        throw new IllegalStateException(
                                "Device registration is missing configuration");
                    }
            } else if (COMMAND_DELAY.equals(mEvent.mCommand)) {
                }
                case DELAY -> {
                    if (mEvent.mDuration <= 0) {
                        throw new IllegalStateException("Delay has missing or invalid duration");
                    }
            } else if (COMMAND_INJECT.equals(mEvent.mCommand)) {
                }
                case INJECT -> {
                    if (mEvent.mInjections  == null) {
                        throw new IllegalStateException("Inject command is missing injection data");
                    }
            } else {
                throw new IllegalStateException("Unknown command " + mEvent.mCommand);
                }
            }
            return mEvent;
        }
+15 −16
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Objects;

/**
 * Uinput class encapsulates execution of "uinput" command. It parses the provided input stream
@@ -96,28 +97,26 @@ public class Uinput {

    private void process(Event e) {
        final int index = mDevices.indexOfKey(e.getId());
        if (index >= 0) {
            Device d = mDevices.valueAt(index);
            if (Event.COMMAND_DELAY.equals(e.getCommand())) {
                d.addDelay(e.getDuration());
            } else if (Event.COMMAND_INJECT.equals(e.getCommand())) {
                d.injectEvent(e.getInjections());
            } else {
                if (Event.COMMAND_REGISTER.equals(e.getCommand())) {
                    error("Device id=" + e.getId() + " is already registered. Ignoring event.");
                } else {
                    error("Unknown command \"" + e.getCommand() + "\". Ignoring event.");
                }
        if (index < 0) {
            if (e.getCommand() != Event.Command.REGISTER) {
                Log.e(TAG, "Unknown device id specified. Ignoring event.");
                return;
            }
        } else if (Event.COMMAND_REGISTER.equals(e.getCommand())) {
            registerDevice(e);
        } else {
            Log.e(TAG, "Unknown device id specified. Ignoring event.");
            return;
        }

        final Device d = mDevices.valueAt(index);
        switch (Objects.requireNonNull(e.getCommand())) {
            case REGISTER ->
                    error("Device id=" + e.getId() + " is already registered. Ignoring event.");
            case INJECT -> d.injectEvent(e.getInjections());
            case DELAY -> d.addDelay(e.getDuration());
        }
    }

    private void registerDevice(Event e) {
        if (!Event.COMMAND_REGISTER.equals(e.getCommand())) {
        if (!Event.Command.REGISTER.equals(e.getCommand())) {
            throw new IllegalStateException(
                    "Tried to send command \"" + e.getCommand() + "\" to an unregistered device!");
        }