usb gaghet hid 绝对鼠标

0x05, 0x01,        // Usage Page (Generic Desktop Ctrls)
0x09, 0x02,        // Usage (Mouse)
0xA1, 0x01,        // Collection (Application)
0x09, 0x01,        //   Usage (Pointer)
0xA1, 0x00,        //   Collection (Physical)
0x85, 0x01,        //     Report ID (1)
0x05, 0x09,        //     Usage Page (Button)
0x19, 0x01,        //     Usage Minimum (0x01)
0x29, 0x03,        //     Usage Maximum (0x03)
0x15, 0x00,        //     Logical Minimum (0)
0x25, 0x01,        //     Logical Maximum (1)
0x75, 0x01,        //     Report Size (1)
0x95, 0x03,        //     Report Count (3)
0x81, 0x02,        //     Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
0x75, 0x05,        //     Report Size (5)
0x95, 0x01,        //     Report Count (1)
0x81, 0x01,        //     Input (Const,Array,Abs,No Wrap,Linear,Preferred State,No Null Position)
0x05, 0x01,        //     Usage Page (Generic Desktop Ctrls)
0x09, 0x30,        //     Usage (X)
0x09, 0x31,        //     Usage (Y)
0x16, 0x01, 0xF8,  //     Logical Minimum (-2047)
0x26, 0xFF, 0x07,  //     Logical Maximum (2047)
0x75, 0x10,        //     Report Size (16)
0x95, 0x02,        //     Report Count (2)
0x81, 0x06,        //     Input (Data,Var,Rel,No Wrap,Linear,Preferred State,No Null Position)
0xC0,              //   End Collection
0xC0,              // End Collection
0x05, 0x01,        // Usage Page (Generic Desktop Ctrls)
0x09, 0x02,        // Usage (Mouse)
0xA1, 0x01,        // Collection (Application)
0x09, 0x01,        //   Usage (Pointer)
0xA1, 0x00,        //   Collection (Physical)
0x85, 0x02,        //     Report ID (2)
0x05, 0x09,        //     Usage Page (Button)
0x19, 0x01,        //     Usage Minimum (0x01)
0x29, 0x03,        //     Usage Maximum (0x03)
0x15, 0x00,        //     Logical Minimum (0)
0x25, 0x01,        //     Logical Maximum (1)
0x75, 0x01,        //     Report Size (1)
0x95, 0x03,        //     Report Count (3)
0x81, 0x02,        //     Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
0x75, 0x01,        //     Report Size (1)
0x95, 0x05,        //     Report Count (5)
0x81, 0x03,        //     Input (Const,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
0x05, 0x01,        //     Usage Page (Generic Desktop Ctrls)
0x09, 0x30,        //     Usage (X)
0x15, 0x00,        //     Logical Minimum (0)
0x26, 0x80, 0x07,  //     Logical Maximum (1920)
0x35, 0x00,        //     Physical Minimum (0)
0x46, 0x80, 0x07,  //     Physical Maximum (1920)
0x75, 0x10,        //     Report Size (16)
0x95, 0x01,        //     Report Count (1)
0x81, 0x02,        //     Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
0x09, 0x31,        //     Usage (Y)
0x15, 0x00,        //     Logical Minimum (0)
0x26, 0x38, 0x04,  //     Logical Maximum (1080)
0x35, 0x00,        //     Physical Minimum (0)
0x46, 0x38, 0x04,  //     Physical Maximum (1080)
0x75, 0x10,        //     Report Size (16)
0x95, 0x01,        //     Report Count (1)
0x81, 0x02,        //     Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
0x09, 0x38,        //     Usage (Wheel)
0x15, 0x81,        //     Logical Minimum (-127)
0x25, 0x7F,        //     Logical Maximum (127)
0x35, 0x00,        //     Physical Minimum (0)
0x45, 0x00,        //     Physical Maximum (0)
0x75, 0x08,        //     Report Size (8)
0x95, 0x01,        //     Report Count (1)
0x81, 0x06,        //     Input (Data,Var,Rel,No Wrap,Linear,Preferred State,No Null Position)
0xC0,              //   End Collection
0xC0,              // End Collection

// 144 bytes
05010902a10185020901a100050919012908150025017501950881020501093009311601f826ff07750c9502810609381581257f750895018106050c0a3802750895018106c0c0050c0901a1018503050609201500266400750895018102c00600ff0901a101851075089506150026ff000901810009019100c00600ff0902a101851175089513150026ff000902810009029100c005010906a101850475019508050719e029e715002501810295017508810395057501050819012905910295017503910395067508150026ff000507190029ff8100c0050c0901a101850515002501750195020a25020a24028102950175068103c0
public class HidController {
    public byte[] kCode = null;
    public byte[] mCode = null;
    private OutputStream kDev = null;
    private OutputStream mDev = null;
    private String TAG = "HidController ";

    public void UnInit() {
        if (kDev != null) {
            try {
                kDev.write(new byte[]{0, 0, 0, 0, 0, 0, 0, 0});
                kDev.close();
            } catch (IOException e) {
                Log.e("UnInit", String.valueOf(e));
            }
        }
        if (mDev != null) {
            try {
                mDev.write(new byte[]{0, 0, 0, 0, 0});
                mDev.close();
            } catch (IOException e) {
                Log.e("UnInit", String.valueOf(e));
            }
        }
    }

    public boolean kInit() {
        kCode = new byte[]{0, 0, 0, 0, 0, 0, 0, 0};
        if (!SuFile.open("/dev/hidg0").exists()) {
            return true;
        }
        try {
            kDev = SuFileOutputStream.open(SuFile.open("/dev/hidg0"));
            kDev.write(kCode);
            return false;
        } catch (Exception e) {
            Log.e("kInit", String.valueOf(e));
            return true;
        }
    }

    public boolean mInit() {
        mCode = new byte[]{0, 0, 0, 0, 0, 0, 0};
        if (!SuFile.open("/dev/hidg1").exists()) {
            return true;
        }
        try {
            mDev = SuFileOutputStream.open(SuFile.open("/dev/hidg1"));
            mDev.write(mCode);
            return false;
        } catch (Exception e) {
            Log.e("mInit", String.valueOf(e));
            return true;
        }
    }

    public void kSend() {
        String log = "";
        for (int i = 0; i < kCode.length; i++){
            log += String.format("%02x ", kCode[i]);
        }

        Log.e(TAG,  "kSend:" + log);


        if (kDev == null) return;
        try {
            kDev.write(kCode);
        } catch (Exception e) {
            Log.e("kSend", String.valueOf(e));
        }
    }

    public void kPress(byte code) {
        for (byte i = 2; i < 8; i++) {
            if (kCode[i] == 0) {
                kCode[i] = code;
                break;
            }
        }
        kSend();
    }

    public void kRelease(byte code) {
        for (byte i = 2; i < 8; i++) {
            if (kCode[i] == code) {
                kCode[i] = 0;
                break;
            }
        }
        kSend();
    }

    public void kPress_c(byte code) {
        kCode[0] += code;
        kSend();

    }

    public void kRelease_c(byte code) {
        kCode[0] -= code;
        kSend();
    }

    public void mSend() {
        String log = "";
        for (int i = 0; i < mCode.length; i++){
            log += String.format("%02x ", mCode[i]);
        }

        Log.e(TAG, "mSend:" + log);


        if (mDev == null) return;

        try {
            mDev.write(mCode);
        } catch (Exception e) {
            Log.e("mSend", String.valueOf(e));
        }
    }

    public void mPress(byte code) {
        for (int i = 0; i < mCode.length; i++) {
            mCode[i] = 0;
        }

        mCode[0] = 02;
        mCode[1] = code;
        mSend();

    }

    public void mRelease(byte code) {
        for (int i = 0; i < mCode.length; i++) {
                mCode[i] = 0;
        }

        mCode[0] = 02;
        mSend();
    }
}

public class MouseKeyboard {
    private String TAG = "MouseKeyboard";
    private final byte LeftCode = 1;
    private final byte MiddleCode = 4;
    private final byte RightCode = 2;

    private int MoveSpeed = 1;

    private boolean isInitDev = false;

    HidController hid = new HidController();

    public enum PROTOCOL_EVENT {
        EVENT,
//        MOUSECLICK,
        MOUSEDOWN,
        MOUSEUP,
        MOVE,
        SROLL,
    }

    VitureMediaClient mParent = null;
    public MouseKeyboard(VitureMediaClient parent){
        Log.i(TAG, "");

        mParent = parent;
    }

    public void InitDev (boolean mIsPush){
        Log.i(TAG, "InitDev mIsPush:" + mIsPush);

        if (mIsPush) {
            if (isInitDev == true) return;
            isInitDev = true;

            String value = new StringBuilder("hid,uac2").toString();
            PropUtil.setProp("sys.usb.config", value);

            Log.d(TAG, "setUsbConfigProp: " + value);

            try {
                Thread.sleep(1000);

            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

            if (hid.kInit() | hid.mInit()) {
                Log.e(TAG, "hid.kInit()/mInit fail!!!");
                isInitDev = false;
            }
        }
    }

    public void Destory(){
        Log.i(TAG + "Destory", "");

        if (isInitDev)
            hid.UnInit();

        isInitDev = false;
    }

//    public void MouseLeftClick(){
//        byte[] Bytes = {(byte)PROTOCOL_EVENT.MOUSECLICK.ordinal(), LeftCode};
//        mParent.SendUdpMessage(Bytes);
//
//        Log.i(TAG + "MouseLeftClick", LogHex(Bytes));
//    }
//
//    public void MouseMiddleClick(){
//        byte[] Bytes = {(byte)PROTOCOL_EVENT.MOUSECLICK.ordinal(), MiddleCode};
//        mParent.SendUdpMessage(Bytes);
//
//        Log.i(TAG + "MouseMiddleClick", LogHex(Bytes));
//    }
//
//    public void MouseRightClick(){
//        byte[] Bytes = {(byte)PROTOCOL_EVENT.MOUSECLICK.ordinal(), RightCode};
//        mParent.SendUdpMessage(Bytes);
//
//        Log.i(TAG + "MouseRightClick", LogHex(Bytes));
//    }


    public void MouseLeftDown(){
        byte[] Bytes = {(byte)PROTOCOL_EVENT.MOUSEDOWN.ordinal(), LeftCode};
        mParent.SendUdpMessage(Bytes);

        Log.i(TAG , "MouseLeftDown:" + LogHex(Bytes));
    }

    public void MouseLeftUp(){
        byte[] Bytes = {(byte)PROTOCOL_EVENT.MOUSEUP.ordinal()};
        mParent.SendUdpMessage(Bytes);

        Log.i(TAG , "MouseLeftUp:" + LogHex(Bytes));
    }

    public void MouseMiddleDown(){
        byte[] Bytes = {(byte)PROTOCOL_EVENT.MOUSEDOWN.ordinal(), MiddleCode};
        mParent.SendUdpMessage(Bytes);

        Log.i(TAG, "MouseMiddleDown:" + LogHex(Bytes));
    }

    public void MouseMiddleUp(){
        byte[] Bytes = {(byte)PROTOCOL_EVENT.MOUSEUP.ordinal()};
        mParent.SendUdpMessage(Bytes);

        Log.i(TAG, "MouseMiddleUp:" + LogHex(Bytes));
    }

    public void MouseRightDown(){
        byte[] Bytes = {(byte)PROTOCOL_EVENT.MOUSEDOWN.ordinal(), RightCode};
        mParent.SendUdpMessage(Bytes);

        Log.i(TAG, "MouseRightDown:" + LogHex(Bytes));
    }

    public void MouseRightUp(){
        byte[] Bytes = {(byte)PROTOCOL_EVENT.MOUSEUP.ordinal()};
        mParent.SendUdpMessage(Bytes);

        Log.i(TAG,  "MouseRightUp:" + LogHex(Bytes));

    }
    
    public void OnMove(float coordinateX, float coordinateY){
        int X = (int)coordinateX;
        int Y = (int)coordinateY;
        byte b1 = (byte) (X & 0xFF);
        byte b2 = (byte) ((X >> 8) & 0xFF);
        byte b3 = (byte) (Y & 0xFF);
        byte b4 = (byte) ((Y >> 8) & 0xFF);


        byte[] Bytes = {(byte)PROTOCOL_EVENT.MOVE.ordinal(), b1, b2, b3, b4};

        mParent.SendUdpMessage(Bytes);

        Log.i(TAG, "OnMove:" + LogHex(Bytes));
    }

    public void OnSroll(float direct){
            byte[] Bytes = {(byte)PROTOCOL_EVENT.SROLL.ordinal(), (byte)(direct < 0 ? 1 : 255)};
            mParent.SendUdpMessage(Bytes);

            Log.i(TAG, "OnSroll:" + LogHex(Bytes));
    }

    private String LogHex(byte[] bytes){
        String log = "";
        for (int i = 0; i < bytes.length; i++){
            log += String.format("%02x ", bytes[i]);
        }

        return log;//Log.i(TAG + "OnMessage", log);
    }

    private byte b1X = 0;
    private byte b2X = 0;
    private byte b1Y = 0;
    private byte b2Y = 0;


    private void SetMouseOnTopRight(){
        int X = 1920;
        int Y = 1080 / 2;
        byte b1 = (byte) (X & 0xFF);
        byte b2 = (byte) ((X >> 8) & 0xFF);
        byte b3 = (byte) (Y & 0xFF);
        byte b4 = (byte) ((Y >> 8) & 0xFF);

        hid.mCode[0] = 02;
        hid.mCode[1] = 00;
        hid.mCode[2] = b1;
        hid.mCode[3] = b2;
        hid.mCode[4] = b3;
        hid.mCode[5] = b4;
        hid.mCode[6] = 00;

        hid.mSend();
    }

    private void SetMouseLastLocationAndClick(byte  clickCode){
        hid.mCode[0] = 02;
        hid.mCode[1] = clickCode;
        hid.mCode[2] = b1X;
        hid.mCode[3] = b2X;
        hid.mCode[4] = b1Y;
        hid.mCode[5] = b2Y;
        hid.mCode[6] = 00;

        hid.mSend();
    }

    private boolean isDragMouse = false;
    public void OnMessage(byte[] bytes) {
        Log.i(TAG, "OnMessage:" + LogHex(bytes));

        if (bytes[0] == (byte)PROTOCOL_EVENT.MOUSEDOWN.ordinal()) {
            Log.i(TAG, "MOUSEDOWN");

            SetMouseLastLocationAndClick(bytes[1]);

            if (bytes[1] == LeftCode){
                isDragMouse = true;
            }

        } else if (bytes[0] == (byte)PROTOCOL_EVENT.MOUSEUP.ordinal()) {
            Log.i(TAG, "MOUSEUP");
            hid.mRelease((byte)0);
            SetMouseOnTopRight();

            isDragMouse = false;

        } else if (bytes[0] == (byte)PROTOCOL_EVENT.MOVE.ordinal()) {
            Log.i(TAG, "MOVE");

            b1X = bytes[1];
            b2X = bytes[2];
            b1Y = bytes[3];
            b2Y = bytes[4];

//            hid.mCode[0] = 02;
//            hid.mCode[1] = 00;
//            hid.mCode[2] = bytes[1];
//            hid.mCode[3] = bytes[2];
//            hid.mCode[4] = bytes[3];
//            hid.mCode[5] = bytes[4];
//            hid.mCode[6] = 00;
//
//            hid.mSend();

            if (isDragMouse){
                SetMouseLastLocationAndClick(LeftCode);
            }
        } else if (bytes[0] == (byte)PROTOCOL_EVENT.SROLL.ordinal()) {
            Log.i(TAG, "SROLL");

            SetMouseLastLocationAndClick((byte)0);

            hid.mCode[0] = 02;
            hid.mCode[1] = 00;
            hid.mCode[2] = 00;
            hid.mCode[3] = 00;
            hid.mCode[4] = 00;
            hid.mCode[5] = 00;
            hid.mCode[6] = bytes[1];
            hid.mSend();

        }
    }
}

hid_usb_num 2
hid_usb0_report_length 8
hid_usb0_report_desc 05010906a101050719e029e71500250175019508810295017508810395057501050819012905910295017503910395067508150025650507190029658100c0
hid_usb1_report_length 7
hid_usb1_report_desc 05010902a1010901a1008501050919012903150025017501950381027505950181010501093009311581257f750895028106c0c005010902a1010901a100850205091901290315002501750195038102750195058103050109301500268007350046800775109501810209311500263804350046380475109501810209381581257f35004500750895018106c0c0
cfg end
  • 10
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值