|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + |
| 7 | + "golang.org/x/sys/unix" |
| 8 | + |
| 9 | + "github.com/kenshaw/evdev" |
| 10 | +) |
| 11 | + |
| 12 | +func handel_touch_using_direct_touch(index int) touch_control_func { |
| 13 | + fd, err := os.OpenFile(fmt.Sprintf("/dev/input/event%d", index), os.O_RDWR, 0) |
| 14 | + if err != nil { |
| 15 | + logger.Errorf("打开设备失败 : %v,请检查是否有权限写入", err) |
| 16 | + os.Exit(3) |
| 17 | + } |
| 18 | + d := evdev.Open(fd) |
| 19 | + go func() { |
| 20 | + <-global_close_signal |
| 21 | + defer d.Close() |
| 22 | + }() |
| 23 | + |
| 24 | + MTPositionX := d.AbsoluteTypes()[evdev.AbsoluteMTPositionX] |
| 25 | + MTPositionY := d.AbsoluteTypes()[evdev.AbsoluteMTPositionY] |
| 26 | + direct_screen_x := int64(MTPositionX.Max) |
| 27 | + direct_screen_y := int64(MTPositionY.Max) |
| 28 | + logger.Warnf("数据将直接写入设备真实触屏 (%d, %d)", MTPositionX, MTPositionY) |
| 29 | + translateDirectXY := func(x, y int32) (int32, int32) { |
| 30 | + switch global_device_orientation { |
| 31 | + case 0: |
| 32 | + return int32(int64(x) * direct_screen_x / 0x7ffffffe), int32(int64(y) * direct_screen_y / 0x7ffffffe) |
| 33 | + case 1: |
| 34 | + return int32(int64(0x7ffffffe-y) * direct_screen_x / 0x7ffffffe), int32(int64(x) * direct_screen_y / 0x7ffffffe) |
| 35 | + case 2: |
| 36 | + return int32(int64(0x7ffffffe-x) * direct_screen_x / 0x7ffffffe), int32(int64(0x7ffffffe-y) * direct_screen_y / 0x7ffffffe) |
| 37 | + case 3: |
| 38 | + return int32(int64(y) * direct_screen_x / 0x7ffffffe), int32(int64(0x7ffffffe-x) * direct_screen_y / 0x7ffffffe) |
| 39 | + default: |
| 40 | + return int32(int64(x) * direct_screen_x / 0x7ffffffe), int32(int64(y) * direct_screen_y / 0x7ffffffe) |
| 41 | + } |
| 42 | + return int32(int64(x) * direct_screen_x / 0x7ffffffe), int32(int64(y) * direct_screen_y / 0x7ffffffe) |
| 43 | + } |
| 44 | + var count int32 = 0 //BTN_TOUCH 申请时为1 则按下 释放时为0 则松开 |
| 45 | + var last_id int32 = -1 //ABS_MT_SLOT last_id每次动作后修改 如果不等则额外发送MT_SLOT事件 |
| 46 | + unixFd := int(fd.Fd()) |
| 47 | + go func() { |
| 48 | + <-global_close_signal |
| 49 | + fd.Close() |
| 50 | + }() |
| 51 | + require_init := makeEventsMMap(6 * 24) |
| 52 | + require := makeEventsMMap(5 * 24) |
| 53 | + |
| 54 | + release := makeEventsMMap(2 * 24) |
| 55 | + switch_release := makeEventsMMap(3 * 24) |
| 56 | + switch_release_btnup := makeEventsMMap(4 * 24) |
| 57 | + release_btnup := makeEventsMMap(3 * 24) |
| 58 | + |
| 59 | + switch_move := makeEventsMMap(4 * 24) |
| 60 | + move := makeEventsMMap(3 * 24) |
| 61 | + |
| 62 | + require_init.Events[0] = evdev.Event{Type: EV_ABS, Code: ABS_MT_SLOT, Value: 0} |
| 63 | + require_init.Events[1] = evdev.Event{Type: EV_ABS, Code: ABS_MT_TRACKING_ID, Value: 0} |
| 64 | + require_init.Events[2] = evdev.Event{Type: EV_KEY, Code: BTN_TOUCH, Value: DOWN} |
| 65 | + require_init.Events[3] = evdev.Event{Type: EV_ABS, Code: ABS_MT_POSITION_X, Value: 0} |
| 66 | + require_init.Events[4] = evdev.Event{Type: EV_ABS, Code: ABS_MT_POSITION_Y, Value: 0} |
| 67 | + require_init.Events[5] = evdev.Event{Type: EV_SYN, Code: 0, Value: 0} |
| 68 | + |
| 69 | + require.Events[0] = evdev.Event{Type: EV_ABS, Code: ABS_MT_SLOT, Value: 0} |
| 70 | + require.Events[1] = evdev.Event{Type: EV_ABS, Code: ABS_MT_TRACKING_ID, Value: 0} |
| 71 | + require.Events[2] = evdev.Event{Type: EV_ABS, Code: ABS_MT_POSITION_X, Value: 0} |
| 72 | + require.Events[3] = evdev.Event{Type: EV_ABS, Code: ABS_MT_POSITION_Y, Value: 0} |
| 73 | + require.Events[4] = evdev.Event{Type: EV_SYN, Code: 0, Value: 0} |
| 74 | + |
| 75 | + release.Events[0] = evdev.Event{Type: EV_ABS, Code: ABS_MT_TRACKING_ID, Value: -1} |
| 76 | + release.Events[1] = evdev.Event{Type: EV_SYN, Code: 0, Value: 0} |
| 77 | + |
| 78 | + switch_release.Events[0] = evdev.Event{Type: EV_ABS, Code: ABS_MT_SLOT, Value: 0} |
| 79 | + switch_release.Events[1] = evdev.Event{Type: EV_ABS, Code: ABS_MT_TRACKING_ID, Value: -1} |
| 80 | + switch_release.Events[2] = evdev.Event{Type: EV_SYN, Code: 0, Value: 0} |
| 81 | + |
| 82 | + switch_release_btnup.Events[0] = evdev.Event{Type: EV_ABS, Code: ABS_MT_SLOT, Value: 0} |
| 83 | + switch_release_btnup.Events[1] = evdev.Event{Type: EV_ABS, Code: ABS_MT_TRACKING_ID, Value: -1} |
| 84 | + switch_release_btnup.Events[2] = evdev.Event{Type: EV_KEY, Code: BTN_TOUCH, Value: UP} |
| 85 | + switch_release_btnup.Events[3] = evdev.Event{Type: EV_SYN, Code: 0, Value: 0} |
| 86 | + |
| 87 | + release_btnup.Events[0] = evdev.Event{Type: EV_ABS, Code: ABS_MT_TRACKING_ID, Value: -1} |
| 88 | + release_btnup.Events[1] = evdev.Event{Type: EV_KEY, Code: BTN_TOUCH, Value: UP} |
| 89 | + release_btnup.Events[2] = evdev.Event{Type: EV_SYN, Code: 0, Value: 0} |
| 90 | + |
| 91 | + switch_move.Events[0] = evdev.Event{Type: EV_ABS, Code: ABS_MT_SLOT, Value: 0} |
| 92 | + switch_move.Events[1] = evdev.Event{Type: EV_ABS, Code: ABS_MT_POSITION_X, Value: 0} |
| 93 | + switch_move.Events[2] = evdev.Event{Type: EV_ABS, Code: ABS_MT_POSITION_Y, Value: 0} |
| 94 | + switch_move.Events[3] = evdev.Event{Type: EV_SYN, Code: 0, Value: 0} |
| 95 | + |
| 96 | + move.Events[0] = evdev.Event{Type: EV_ABS, Code: ABS_MT_POSITION_X, Value: 0} |
| 97 | + move.Events[1] = evdev.Event{Type: EV_ABS, Code: ABS_MT_POSITION_Y, Value: 0} |
| 98 | + |
| 99 | + return func(control_data touch_control_pack) { |
| 100 | + // write_events := make([]*evdev.Event, 0) |
| 101 | + // start := time.Now() |
| 102 | + if control_data.id == -1 { //在任何正常情况下 这里是拿不到ID=-1的控制包的因此可以直接丢弃 |
| 103 | + return |
| 104 | + } |
| 105 | + if control_data.action == TouchActionRequire { |
| 106 | + x, y := translateDirectXY(control_data.x, control_data.y) |
| 107 | + last_id = control_data.id |
| 108 | + if count += 1; count == 1 { |
| 109 | + require_init.Events[0].Value = control_data.id |
| 110 | + require_init.Events[1].Value = control_data.id |
| 111 | + require_init.Events[3].Value = x |
| 112 | + require_init.Events[4].Value = y |
| 113 | + unix.Write(unixFd, require_init.data) |
| 114 | + // packChan <- pack{data: require_init.data, ts: start} |
| 115 | + } else { |
| 116 | + require.Events[0].Value = control_data.id |
| 117 | + require.Events[1].Value = control_data.id |
| 118 | + require.Events[2].Value = x |
| 119 | + require.Events[3].Value = y |
| 120 | + unix.Write(unixFd, require.data) |
| 121 | + // packChan <- pack{data: require.data, ts: start} |
| 122 | + |
| 123 | + } |
| 124 | + |
| 125 | + } else if control_data.action == TouchActionRelease { |
| 126 | + if last_id != control_data.id { |
| 127 | + last_id = control_data.id |
| 128 | + if count -= 1; count == 0 { |
| 129 | + switch_release_btnup.Events[0].Value = control_data.id |
| 130 | + unix.Write(unixFd, switch_release_btnup.data) |
| 131 | + // packChan <- pack{data: switch_release_btnup.data, ts: start} |
| 132 | + } else { |
| 133 | + switch_release.Events[0].Value = control_data.id |
| 134 | + unix.Write(unixFd, switch_release.data) |
| 135 | + // packChan <- pack{data: switch_release.data, ts: start} |
| 136 | + } |
| 137 | + } else { |
| 138 | + if count -= 1; count == 0 { |
| 139 | + unix.Write(unixFd, release_btnup.data) |
| 140 | + // packChan <- pack{data: release_btnup.data, ts: start} |
| 141 | + } else { |
| 142 | + unix.Write(unixFd, release.data) |
| 143 | + // packChan <- pack{data: release.data, ts: start} |
| 144 | + } |
| 145 | + } |
| 146 | + } else if control_data.action == TouchActionMove { |
| 147 | + x, y := translateDirectXY(control_data.x, control_data.y) |
| 148 | + if last_id != control_data.id { |
| 149 | + last_id = control_data.id |
| 150 | + switch_move.Events[0].Value = control_data.id |
| 151 | + switch_move.Events[1].Value = x |
| 152 | + switch_move.Events[2].Value = y |
| 153 | + unix.Write(unixFd, switch_move.data) |
| 154 | + // packChan <- pack{data: switch_move.data, ts: start} |
| 155 | + |
| 156 | + } else { |
| 157 | + move.Events[0].Value = x |
| 158 | + move.Events[1].Value = y |
| 159 | + unix.Write(unixFd, move.data) |
| 160 | + // packChan <- pack{data: move.data, ts: start} |
| 161 | + } |
| 162 | + } |
| 163 | + // logger.Debugf("uinput handeler%v", time.Since(start)) |
| 164 | + } |
| 165 | +} |
0 commit comments