Skip to content
This repository has been archived by the owner on May 27, 2024. It is now read-only.

Commit

Permalink
finish whatsinput
Browse files Browse the repository at this point in the history
  • Loading branch information
codeskyblue committed Apr 4, 2018
1 parent 986ab0e commit 57b16a8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 16 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,14 +231,15 @@ Websocket连接 `$DEVICE_URL/whatsinput`, 接收JSON格式
{"text":"hello world", "type":"InputStart"}
```

会自动清除原有的内容,替换为新的内容
开始编辑时的内容

- 结束编辑

```json
{"type": "InputFinish"}
```

### 前端 --> 手机
- KeyCode的输入

```json
Expand All @@ -247,8 +248,7 @@ Websocket连接 `$DEVICE_URL/whatsinput`, 接收JSON格式

[KeyCode参考列表](https://testerhome.com/topics/1386)

### 前端 --> 手机
- 编辑框内容变化
- 编辑框内容输入

```json
{"type": "InputChange", "text": "some text"}
Expand Down
51 changes: 38 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -452,8 +452,13 @@ func ServeHTTP(lis net.Listener, tunnel *TunnelProxy) error {
renderHTML(w, "remote.html")
})

const whatsInputFinishedMagic = "inputFinished--201804031742"
var whatsInputChannel = make(chan string, 0)
/* WhatsInput */
var whatsinput = struct {
C chan string
Recent string
}{make(chan string, 0), ""}

const whatsInputFinishedMagic = "__inputFinished__"

// Send input to device
// Highly affected by project https://github.com/willerce/WhatsInput
Expand All @@ -468,8 +473,19 @@ func ServeHTTP(lis net.Listener, tunnel *TunnelProxy) error {
go func() {
for {
select {
case msg := <-whatsInputChannel:
case msg := <-whatsinput.C:
log.Println("Receive msg", msg)
if msg == whatsInputFinishedMagic {
log.Println("FinishedInput")
ws.WriteJSON(map[string]string{
"type": "InputFinish",
})
} else {
ws.WriteJSON(map[string]string{
"type": "InputStart",
"text": msg,
})
}
case <-quit:
return
}
Expand All @@ -481,15 +497,10 @@ func ServeHTTP(lis net.Listener, tunnel *TunnelProxy) error {
log.Println(err)
break
}
log.Println(v.Type, v.Text, v.Code)
switch v.Type {
case "InputStart":
if v.Text == "" {
runShell("am", "broadcast", "-a", "ADB_CLEAR_TEXT")
} else {
base64Str := base64.StdEncoding.EncodeToString([]byte(v.Text))
runShell("am", "broadcast", "-a", "ADB_SET_TEXT", "--es", "text", base64Str)
}
case "InputChange":
base64Str := base64.StdEncoding.EncodeToString([]byte(v.Text))
runShell("am", "broadcast", "-a", "ADB_SET_TEXT", "--es", "text", strconv.Quote(base64Str))
case "InputKey":
runShell("input", "keyevent", "KEYCODE_ENTER") // HOTFIX(ssx): need fix later
// runShell("am", "broadcast", "-a", "ADB_INPUT_KEYCODE", "--ei", "code", strconv.Itoa(v.Code))
Expand All @@ -499,9 +510,23 @@ func ServeHTTP(lis net.Listener, tunnel *TunnelProxy) error {

m.HandleFunc("/whatsinput", func(w http.ResponseWriter, r *http.Request) {
data, _ := ioutil.ReadAll(r.Body)
if string(data) == "" {
http.Error(w, "Empty body", http.StatusForbidden)
return
}
var input string
if data[0] == 'I' {
input = string(data[1:])
whatsinput.Recent = input
} else {
input = whatsInputFinishedMagic
whatsinput.Recent = ""
}
select {
case whatsInputChannel <- string(data):
case <-time.After(1 * time.Second):
case whatsinput.C <- input:
io.WriteString(w, "Success")
case <-time.After(100 * time.Millisecond):
io.WriteString(w, "No WebSocket client connected")
}
}).Methods("POST")

Expand Down

0 comments on commit 57b16a8

Please sign in to comment.