Skip to content

Commit c5ffdfc

Browse files
committed
适配vmouse旋转
1 parent f07b224 commit c5ffdfc

6 files changed

Lines changed: 68 additions & 208 deletions

File tree

handler.go

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,8 @@ func InitTouchHandler(
292292
abs_last_map.Store("RS_Y", 0.5)
293293

294294
screenSizeX := config_json.Get("SCREEN").Get("SIZE").GetIndex(0).MustInt()
295-
global_screen_x = int32(screenSizeX)
296295
screenSizeY := config_json.Get("SCREEN").Get("SIZE").GetIndex(1).MustInt()
296+
global_screen_x = int32(screenSizeX)
297297
global_screen_y = int32(screenSizeY)
298298
KEYBOARD_SWITCH_KEY_NAME_S := make(map[string]bool)
299299
for _, key := range config_json.Get("MOUSE").Get("SWITCH_KEYS").MustStringArray() {
@@ -376,6 +376,8 @@ func (self *TouchHandler) reloadConfigure(mapperFilePath string) {
376376
config_json, _ := simplejson.NewJson(content)
377377
screenSizeX := config_json.Get("SCREEN").Get("SIZE").GetIndex(0).MustInt()
378378
screenSizeY := config_json.Get("SCREEN").Get("SIZE").GetIndex(1).MustInt()
379+
global_screen_x = int32(screenSizeX)
380+
global_screen_y = int32(screenSizeY)
379381
self.config = config_json
380382
self.screen_x = int32(screenSizeX)
381383
self.screen_y = int32(screenSizeY)
@@ -417,17 +419,17 @@ func (self *TouchHandler) get_scaled_pos(x int32, y int32) (int32, int32) {
417419
return int32(int64(x) * 0x7ffffffe / int64(self.rel_screen_x)), int32(int64(y) * 0x7ffffffe / int64(self.rel_screen_y))
418420
}
419421

420-
func (self *TouchHandler) touch_require(x int32, y int32, scale uint8) int32 {
422+
func (self *TouchHandler) touch_require(x int32, y int32, scale bool) int32 {
421423
self.touch_control_lock.Lock()
422424
defer self.touch_control_lock.Unlock()
423425
for i, v := range self.allocated_id {
424426
if !v {
425427
self.allocated_id[i] = true
426-
if scale == 0 {
427-
self.send_touch_control_pack(TouchActionRequire, int32(i), x, y)
428-
} else {
428+
if scale {
429429
scaled_x, scaled_y := self.get_scaled_pos(x, y)
430430
self.send_touch_control_pack(TouchActionRequire, int32(i), scaled_x, scaled_y)
431+
} else {
432+
self.send_touch_control_pack(TouchActionRequire, int32(i), x, y)
431433
}
432434
logger.Debugf("touch require [%v] => (%v,%v)", i, x, y)
433435
return int32(i)
@@ -447,16 +449,16 @@ func (self *TouchHandler) touch_release(id int32) int32 {
447449
return -1
448450
}
449451

450-
func (self *TouchHandler) touch_move(id int32, x int32, y int32, scale uint8) {
452+
func (self *TouchHandler) touch_move(id int32, x int32, y int32, scale bool) {
451453
self.touch_control_lock.Lock()
452454
defer self.touch_control_lock.Unlock()
453455
logger.Debugf("touch move to (%v,%v) [%v]", x, y, id)
454456
if id != -1 {
455-
if scale == 0 {
456-
self.send_touch_control_pack(TouchActionMove, id, x, y)
457-
} else {
457+
if scale {
458458
scaled_x, scaled_y := self.get_scaled_pos(x, y)
459459
self.send_touch_control_pack(TouchActionMove, id, scaled_x, scaled_y)
460+
} else {
461+
self.send_touch_control_pack(TouchActionMove, id, x, y)
460462
}
461463

462464
}
@@ -511,7 +513,7 @@ func (self *TouchHandler) handel_view_move(offset_x int32, offset_y int32) { //
511513
self.auto_release_view_count = 0
512514
if self.view_id == -1 {
513515
self.view_current_x, self.view_current_y = self.get_scaled_pos(self.view_init_x+rand_offset(), self.view_init_y+rand_offset())
514-
self.view_id = self.touch_require(self.view_current_x, self.view_current_y, 0)
516+
self.view_id = self.touch_require(self.view_current_x, self.view_current_y, false)
515517
}
516518
self.view_current_x += offset_x * self.view_speed_x
517519
self.view_current_y += offset_y * self.view_speed_y
@@ -541,14 +543,14 @@ func (self *TouchHandler) handel_view_move(offset_x int32, offset_y int32) { //
541543
// 采用了0x7ffffffe作为边界的情况下,所情况有都为有界
542544
if self.view_current_x < 0 || self.view_current_y < 0 { //出现负数 表示到达边界
543545
self.view_current_x, self.view_current_y = self.get_scaled_pos(self.view_init_x+rand_offset(), self.view_init_y+rand_offset())
544-
tmp_view_id := self.touch_require(self.view_current_x, self.view_current_y, 0)
546+
tmp_view_id := self.touch_require(self.view_current_x, self.view_current_y, false)
545547
self.view_current_x += offset_x * self.view_speed_x
546548
self.view_current_y += offset_y * self.view_speed_y
547-
self.touch_move(tmp_view_id, self.view_current_x, self.view_current_y, 0)
549+
self.touch_move(tmp_view_id, self.view_current_x, self.view_current_y, false)
548550
self.touch_release(self.view_id)
549551
self.view_id = tmp_view_id
550552
} else {
551-
self.touch_move(self.view_id, self.view_current_x, self.view_current_y, 0)
553+
self.touch_move(self.view_id, self.view_current_x, self.view_current_y, false)
552554
}
553555

554556
}
@@ -585,9 +587,9 @@ func (self *TouchHandler) handel_wheel_action(action int8, abs_x int32, abs_y in
585587
}
586588
} else if action == Wheel_action_move { //移动
587589
if self.wheel_id == -1 { //如果在移动之前没有按下
588-
self.wheel_id = self.touch_require(self.wheel_init_x, self.wheel_init_y, touch_pos_scale)
590+
self.wheel_id = self.touch_require(self.wheel_init_x, self.wheel_init_y, true)
589591
}
590-
self.touch_move(self.wheel_id, abs_x, abs_y, touch_pos_scale)
592+
self.touch_move(self.wheel_id, abs_x, abs_y, true)
591593
}
592594
self.wheel_lock.Unlock()
593595
}
@@ -718,7 +720,7 @@ func (self *TouchHandler) execute_key_action(start time.Time, key_name string, u
718720
if up_down == DOWN {
719721
x := int32(action.Get("POS").GetIndex(0).MustFloat64()*float64(self.rel_screen_x)) + rand_offset()
720722
y := int32(action.Get("POS").GetIndex(1).MustFloat64()*float64(self.rel_screen_y)) + rand_offset()
721-
self.key_action_state_save.Store(key_name, self.touch_require(x, y, touch_pos_scale))
723+
self.key_action_state_save.Store(key_name, self.touch_require(x, y, true))
722724
} else if up_down == UP {
723725
tid := state.(int32)
724726
self.touch_release(tid)
@@ -729,7 +731,7 @@ func (self *TouchHandler) execute_key_action(start time.Time, key_name string, u
729731
go (func() {
730732
x := int32(action.Get("POS").GetIndex(0).MustFloat64()*float64(self.rel_screen_x)) + rand_offset()
731733
y := int32(action.Get("POS").GetIndex(1).MustFloat64()*float64(self.rel_screen_y)) + rand_offset()
732-
tid := self.touch_require(x, y, touch_pos_scale)
734+
tid := self.touch_require(x, y, true)
733735
time.Sleep(time.Duration(8) * time.Millisecond) //8ms 120HZ下一次
734736
self.touch_release(tid)
735737
})()
@@ -744,7 +746,7 @@ func (self *TouchHandler) execute_key_action(start time.Time, key_name string, u
744746
self.key_action_state_save.Store(key_name, true)
745747
go (func() {
746748
for {
747-
tid := self.touch_require(x+rand_offset(), y+rand_offset(), touch_pos_scale)
749+
tid := self.touch_require(x+rand_offset(), y+rand_offset(), true)
748750
time.Sleep(time.Duration(down_time) * time.Millisecond)
749751
self.touch_release(tid)
750752
time.Sleep(time.Duration(interval_time) * time.Millisecond)
@@ -768,7 +770,7 @@ func (self *TouchHandler) execute_key_action(start time.Time, key_name string, u
768770
for i := range action.Get("POS_S").MustArray() {
769771
x := int32(action.Get("POS_S").GetIndex(i).GetIndex(0).MustFloat64()*float64(self.rel_screen_x)) + rand_offset()
770772
y := int32(action.Get("POS_S").GetIndex(i).GetIndex(1).MustFloat64()*float64(self.rel_screen_y)) + rand_offset()
771-
tid := self.touch_require(x, y, touch_pos_scale)
773+
tid := self.touch_require(x, y, true)
772774
tid_save = append(tid_save, tid)
773775
time.Sleep(time.Duration(8) * time.Millisecond) // 间隔8ms 是否需要延迟有待验证
774776
}
@@ -794,17 +796,17 @@ func (self *TouchHandler) execute_key_action(start time.Time, key_name string, u
794796
interval_time := action.Get("INTERVAL").GetIndex(0).MustInt()
795797
init_x := int32(action.Get("POS_S").GetIndex(0).GetIndex(0).MustFloat64() * float64(self.rel_screen_x))
796798
init_y := int32(action.Get("POS_S").GetIndex(0).GetIndex(1).MustFloat64() * float64(self.rel_screen_y))
797-
tid := self.touch_require(init_x, init_y, touch_pos_scale)
799+
tid := self.touch_require(init_x, init_y, true)
798800
time.Sleep(time.Duration(interval_time) * time.Millisecond)
799801
for index := 1; index < pos_len-1; index++ {
800802
x := int32(action.Get("POS_S").GetIndex(index).GetIndex(0).MustFloat64()*float64(self.rel_screen_x)) + rand_offset()
801803
y := int32(action.Get("POS_S").GetIndex(index).GetIndex(1).MustFloat64()*float64(self.rel_screen_y)) + rand_offset()
802-
self.touch_move(tid, x, y, touch_pos_scale)
804+
self.touch_move(tid, x, y, true)
803805
time.Sleep(time.Duration(interval_time) * time.Millisecond)
804806
}
805807
end_x := int32(action.Get("POS_S").GetIndex(pos_len-1).GetIndex(0).MustFloat64() * float64(self.rel_screen_x))
806808
end_y := int32(action.Get("POS_S").GetIndex(pos_len-1).GetIndex(1).MustFloat64() * float64(self.rel_screen_y))
807-
self.touch_move(tid, end_x, end_y, touch_pos_scale)
809+
self.touch_move(tid, end_x, end_y, true)
808810
self.touch_release(tid)
809811
})()
810812
} else if up_down == UP {
@@ -1085,7 +1087,7 @@ func (self *TouchHandler) mix_touch(touch_events chan *event_pack, max_mt_x, max
10851087
if copy_id_statuses[i] != id_statuses[i] {
10861088
if id_statuses[i] { //false -> true 申请
10871089
x, y := translate_xy(pos_s[i][0], pos_s[i][1])
1088-
id_2_vid[i] = self.touch_require(x, y, 0)
1090+
id_2_vid[i] = self.touch_require(x, y, false)
10891091
logger.Debugf("mixTouch\trequire\t[%d] translate_xy(%d,%d) => (%d,%d)", i, pos_s[i][0], pos_s[i][1], x, y)
10901092
} else {
10911093
self.touch_release(id_2_vid[i])
@@ -1094,7 +1096,7 @@ func (self *TouchHandler) mix_touch(touch_events chan *event_pack, max_mt_x, max
10941096
} else {
10951097
if pos_s[i][0] != copy_pos_s[i][0] || pos_s[i][1] != copy_pos_s[i][1] {
10961098
x, y := translate_xy(pos_s[i][0], pos_s[i][1])
1097-
self.touch_move(id_2_vid[i], x, y, 0)
1099+
self.touch_move(id_2_vid[i], x, y, false)
10981100
logger.Debugf("mixTouch\tmove\t[%d] translate_xy(%d,%d) => (%d,%d)", i, pos_s[i][0], pos_s[i][1], x, y)
10991101
}
11001102
}

main.go

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,9 @@ func udp_event_injector(ch chan *event_pack, port int) {
141141
}
142142
}
143143

144-
var global_close_signal = make(chan bool) //仅会在程序退出时关闭 不用于其他用途
145-
var global_device_orientation int32 = 0
144+
var global_close_signal = make(chan bool) //仅会在程序退出时关闭 不用于其他用途
146145
var global_is_wordking_remote bool = false // 是否正在远程控制 并且无法获取触屏状态
146+
var global_device_orientation int32 = 0
147147
var global_screen_x int32 = 1000
148148
var global_screen_y int32 = 1000
149149

@@ -183,6 +183,21 @@ func listen_device_orientation() {
183183
}
184184
}
185185

186+
func rotateAbsoluteXY(x, y int32) (int32, int32) { //根据方向旋转坐标
187+
switch global_device_orientation {
188+
case 0:
189+
return x, y
190+
case 1:
191+
return 0x7ffffffe - y, x
192+
case 2:
193+
return 0x7ffffffe - x, 0x7ffffffe - y
194+
case 3:
195+
return y, 0x7ffffffe - x
196+
default:
197+
return x, y
198+
}
199+
}
200+
186201
type dev_type uint8
187202

188203
const (
@@ -277,17 +292,6 @@ func get_dev_name_by_index(index int) string {
277292
return d.Name()
278293
}
279294

280-
// func get_dev_phys_info_by_index(index int) string {
281-
// fd, err := os.OpenFile(fmt.Sprintf("/dev/input/event%d", index), os.O_RDONLY, 0)
282-
// if err != nil {
283-
// return "read name error"
284-
// }
285-
// d := evdev.Open(fd)
286-
// defer d.Close()
287-
// return d.Name()
288-
// // return d
289-
// }
290-
291295
func execute_view_move(handelerInstance *TouchHandler, x, stepValue, sleepMS int) {
292296
handelerInstance.handel_view_move(0, 0)
293297
time.Sleep(time.Millisecond * time.Duration(sleepMS))
@@ -749,7 +753,6 @@ func main() {
749753
logger.Infof("启用触屏混合 %s(/dev/input/event%d) : %s", devName, index, devTypeFriendlyName[devType])
750754
go dev_reader(touch_event_ch, index)
751755
max_mt_x, max_mt_y = get_MT_size(map[int]bool{index: true})
752-
// break
753756
}
754757
}
755758
}
@@ -781,12 +784,13 @@ func main() {
781784
}
782785
}
783786
})()
784-
touch_control_func = handel_touch_using_hid_manager(port, *usingDeviceRotation)
787+
touch_control_func = handel_touch_using_hid_manager(port)
785788
} else if *usingOTGMode { //本机模拟成otg的触屏
786789
global_is_wordking_remote = true
787790
logger.Info("触屏控制将使用本机模拟为HID设备发送至主机")
788791
logger.Infof("触屏方向:%d", *usingDeviceRotation)
789-
touch_control_func = handel_touch_using_otg_manager(*usingDeviceRotation)
792+
global_device_orientation = int32(*usingDeviceRotation)
793+
touch_control_func = handel_touch_using_otg_manager()
790794
go (func() {
791795
for {
792796
select {
@@ -799,12 +803,10 @@ func main() {
799803
} else if *usingInputManagerID != -1 {
800804
logger.Info("触屏控制将使用inputManager在本机处理")
801805
go handel_u_input_mouse_keyboard(fileted_u_input_control_ch)
802-
go listen_device_orientation()
803806
touch_control_func = handel_touch_using_input_manager(*usingInputManagerID)
804807
} else {
805808
logger.Info("触屏控制将使用uinput在本机处理")
806809
go handel_u_input_mouse_keyboard(fileted_u_input_control_ch)
807-
go listen_device_orientation()
808810
touch_control_func = handel_touch_using_uinput_touch()
809811
}
810812

@@ -818,8 +820,11 @@ func main() {
818820
map_switch_signal,
819821
*measure_sensitivity_mode,
820822
)
821-
if global_is_wordking_remote == false { //只有本机运行的时候 才有必要开启触屏混合
823+
if global_is_wordking_remote { //只有本机运行的时候 才有必要开启触屏混合
824+
825+
} else {
822826
go touchHandler.mix_touch(touch_event_ch, max_mt_x, max_mt_y)
827+
go listen_device_orientation()
823828
}
824829
go touchHandler.auto_handel_view_release(*view_release_timeout)
825830
go touchHandler.loop_handel_wasd_wheel()

otg_hid_manager.go

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"os"
66
)
77

8-
func handel_touch_using_otg_manager(rotation int) touch_control_func {
8+
func handel_touch_using_otg_manager() touch_control_func {
99
touch_fd, err := os.OpenFile("/dev/hidg0", os.O_RDWR, 0666)
1010
if err != nil {
1111
logger.Errorf("无法打开OTG HID设备文件: %s", err.Error())
@@ -35,25 +35,10 @@ func handel_touch_using_otg_manager(rotation int) touch_control_func {
3535
buf[11] = 0
3636
}
3737

38-
rot_xy := func(pack touch_control_pack) (int32, int32) { //根据方向旋转坐标
39-
switch rotation {
40-
case 0:
41-
return pack.x, pack.y
42-
case 1:
43-
return 0x7ffffffe - pack.y, pack.x
44-
case 2:
45-
return 0x7ffffffe - pack.x, 0x7ffffffe - pack.y
46-
case 3:
47-
return pack.y, 0x7ffffffe - pack.x
48-
default:
49-
return pack.x, pack.y
50-
}
51-
}
52-
5338
return func(control_data touch_control_pack) {
5439
switch control_data.action {
5540
case TouchActionRequire, TouchActionMove:
56-
x, y := rot_xy(control_data)
41+
x, y := rotateAbsoluteXY(control_data.x, control_data.y)
5742
setReport(0x01, uint8(control_data.id), uint32(x), uint32(y))
5843
touch_fd.Write(buf[:])
5944
case TouchActionRelease:

tty_hid_manager.go

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"go.bug.st/serial"
77
)
88

9-
func handel_touch_using_hid_manager(port serial.Port, rotation int) touch_control_func {
9+
func handel_touch_using_hid_manager(port serial.Port) touch_control_func {
1010
var buf [12]byte
1111
buf[0] = 0xF4
1212
setReport := func(action uint8, id uint8, x, y uint32) {
@@ -17,25 +17,10 @@ func handel_touch_using_hid_manager(port serial.Port, rotation int) touch_contro
1717
buf[11] = 0
1818
}
1919

20-
rot_xy := func(pack touch_control_pack) (int32, int32) { //根据方向旋转坐标
21-
switch rotation {
22-
case 0:
23-
return pack.x, pack.y
24-
case 1:
25-
return 0x7ffffffe - pack.y, pack.x
26-
case 2:
27-
return 0x7ffffffe - pack.x, 0x7ffffffe - pack.y
28-
case 3:
29-
return pack.y, 0x7ffffffe - pack.x
30-
default:
31-
return pack.x, pack.y
32-
}
33-
}
34-
3520
return func(control_data touch_control_pack) {
3621
switch control_data.action {
3722
case TouchActionRequire, TouchActionMove:
38-
x, y := rot_xy(control_data)
23+
x, y := rotateAbsoluteXY(control_data.x, control_data.y)
3924
setReport(0x01, uint8(control_data.id), uint32(x), uint32(y))
4025
port.Write(buf[:])
4126
case TouchActionRelease:

0 commit comments

Comments
 (0)