This repository has been archived by the owner on May 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 275
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ee41a69
commit f198573
Showing
6 changed files
with
291 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,7 @@ | |
|
||
go generate | ||
GOOS=linux GOARCH=arm go build -tags vfs | ||
if test "$1" = "i" | ||
then | ||
cmd "/c install.bat" | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"net" | ||
|
||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
type toucher struct { | ||
width, height int | ||
rotation int | ||
} | ||
|
||
type TouchRequest struct { | ||
Operation string `json:"operation"` // d, m, u | ||
Index int `json:"index"` | ||
PercentX float64 `json:"pX"` | ||
PercentY float64 `json:"pY"` | ||
Pressure int `json:"pressure"` | ||
} | ||
|
||
// coord(0, 0) is always left-top conner, no matter the rotation changes | ||
func drainTouchRequests(conn net.Conn, reqC chan TouchRequest) error { | ||
var maxX, maxY int | ||
var flag string | ||
var ver int | ||
var maxContacts, maxPressure int | ||
var pid int | ||
|
||
lineRd := lineFormatReader{bufrd: bufio.NewReader(conn)} | ||
lineRd.Scanf("%s %d", &flag, &ver) | ||
lineRd.Scanf("%s %d %d %d %d", &flag, &maxContacts, &maxX, &maxY, &maxPressure) | ||
if err := lineRd.Scanf("%s %d", &flag, &pid); err != nil { | ||
return err | ||
} | ||
log.WithFields(log.Fields{ | ||
"maxX": maxX, | ||
"maxY": maxY, | ||
"maxPressure": maxPressure, | ||
"maxContacts": maxContacts, | ||
}).Info("handle touch requests") | ||
go io.Copy(ioutil.Discard, conn) // ignore the rest output | ||
var posX, posY int | ||
for req := range reqC { | ||
var err error | ||
switch req.Operation { | ||
case "d": | ||
fallthrough | ||
case "m": | ||
posX = int(req.PercentX * float64(maxX)) | ||
posY = int(req.PercentY * float64(maxY)) | ||
if req.Pressure == 0 { | ||
req.Pressure = 50 | ||
} | ||
line := fmt.Sprintf("%s %d %d %d %d\n", req.Operation, req.Index, posX, posY, req.Pressure) | ||
log.WithFields(log.Fields{ | ||
"touch": req, | ||
"remoteAddr": conn.RemoteAddr(), | ||
}).Debug("write to @minitouch", line) | ||
_, err = conn.Write([]byte(line)) | ||
case "u": | ||
_, err = conn.Write([]byte(fmt.Sprintf("u %d\n", req.Index))) | ||
case "c": | ||
_, err = conn.Write([]byte("c\n")) | ||
default: | ||
err = errors.New("unsupported operation: " + req.Operation) | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
type lineFormatReader struct { | ||
bufrd *bufio.Reader | ||
err error | ||
} | ||
|
||
func (r *lineFormatReader) Scanf(format string, args ...interface{}) error { | ||
if r.err != nil { | ||
return r.err | ||
} | ||
var line []byte | ||
line, _, r.err = r.bufrd.ReadLine() | ||
if r.err != nil { | ||
return r.err | ||
} | ||
_, r.err = fmt.Sscanf(string(line), format, args...) | ||
return r.err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"net" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type MockConn struct { | ||
buffer *bytes.Buffer | ||
} | ||
|
||
func (c *MockConn) Read(b []byte) (n int, err error) { | ||
return c.buffer.Read(b) | ||
} | ||
|
||
func (c *MockConn) Write(b []byte) (n int, err error) { | ||
return c.buffer.Write(b) | ||
} | ||
|
||
func (c *MockConn) Close() error { return nil } | ||
func (c *MockConn) LocalAddr() net.Addr { return nil } | ||
func (c *MockConn) RemoteAddr() net.Addr { return nil } | ||
func (c *MockConn) SetDeadline(t time.Time) error { return nil } | ||
func (c *MockConn) SetReadDeadline(t time.Time) error { return nil } | ||
func (c *MockConn) SetWriteDeadline(t time.Time) error { return nil } | ||
|
||
func TestDrainTouchRequests(t *testing.T) { | ||
reqC := make(chan TouchRequest, 0) | ||
conn := &MockConn{ | ||
buffer: bytes.NewBuffer(nil), | ||
} | ||
err := drainTouchRequests(conn, reqC) | ||
assert.Error(t, err) | ||
|
||
conn = &MockConn{ | ||
buffer: bytes.NewBufferString(`v 1 | ||
^ 10 1080 1920 255 | ||
$ 25654`), | ||
} | ||
reqC = make(chan TouchRequest, 4) | ||
reqC <- TouchRequest{ | ||
Operation: "d", | ||
Index: 1, | ||
PercentX: 1.0, | ||
PercentY: 1.0, | ||
Pressure: 10, | ||
} | ||
reqC <- TouchRequest{ | ||
Operation: "c", | ||
} | ||
reqC <- TouchRequest{ | ||
Operation: "m", | ||
Index: 3, | ||
PercentX: 0.5, | ||
PercentY: 0.5, | ||
Pressure: 30, | ||
} | ||
reqC <- TouchRequest{ | ||
Operation: "u", | ||
Index: 4, | ||
} | ||
close(reqC) | ||
drainTouchRequests(conn, reqC) | ||
output := string(conn.buffer.Bytes()) | ||
assert.Equal(t, "d 1 1080 1920 10\nc\nm 3 540 960 30\nu 4\n", output) | ||
} |