use shorter commands

This commit is contained in:
Unrud 2018-06-26 06:01:16 +02:00
parent 1c7a277328
commit b5ed8ee23b
3 changed files with 30 additions and 23 deletions

File diff suppressed because one or more lines are too long

25
main.go
View file

@ -35,6 +35,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"time" "time"
"unicode/utf8"
) )
const ( const (
@ -47,29 +48,35 @@ const (
) )
func processCommand(plugin Plugin, command string) error { func processCommand(plugin Plugin, command string) error {
commandParts := strings.Split(command, " ") if len(command) == 0 {
if commandParts[0] == "KeyboardText" { return errors.New("empty command")
text := strings.Join(commandParts[1:], " ") }
if command[0] == 't' {
text := command[1:]
if !utf8.ValidString(text) {
return errors.New("invalid utf-8")
}
return plugin.KeyboardText(text) return plugin.KeyboardText(text)
} }
if len(commandParts) != 3 { arguments := strings.Split(command[1:], ";")
if len(arguments) != 2 {
return errors.New("wrong number of arguments") return errors.New("wrong number of arguments")
} }
x, err := strconv.ParseInt(commandParts[1], 10, 32) x, err := strconv.ParseInt(arguments[0], 10, 32)
if err != nil { if err != nil {
return err return err
} }
y, err := strconv.ParseInt(commandParts[2], 10, 32) y, err := strconv.ParseInt(arguments[1], 10, 32)
if err != nil { if err != nil {
return err return err
} }
if commandParts[0] == "PointerMove" { if command[0] == 'm' {
return plugin.PointerMove(int(x), int(y)) return plugin.PointerMove(int(x), int(y))
} }
if commandParts[0] == "PointerScroll" { if command[0] == 's' {
return plugin.PointerScroll(int(x), int(y)) return plugin.PointerScroll(int(x), int(y))
} }
if commandParts[0] == "PointerButton" { if command[0] == 'b' {
if x <= 0 || x > 3 { if x <= 0 || x > 3 {
return errors.New("unknown pointer button") return errors.New("unknown pointer button")
} }

View file

@ -141,7 +141,7 @@ function calculatePointerAccelerationMult(speed) {
function onDraggingTimeout() { function onDraggingTimeout() {
draggingTimeout = null; draggingTimeout = null;
ws.send("PointerButton 1 0"); ws.send("b1;0");
} }
function updateMoveAndScroll() { function updateMoveAndScroll() {
@ -161,7 +161,7 @@ function onUpdateTimeout() {
moveXSum -= moveX; moveXSum -= moveX;
moveYSum -= moveY; moveYSum -= moveY;
idleUpdates = 0; idleUpdates = 0;
ws.send("PointerMove " + moveX + " " + moveY); ws.send("m" + moveX + ";" + moveY);
} }
var scrollX = Math.trunc(scrollXSum); var scrollX = Math.trunc(scrollXSum);
var scrollY = Math.trunc(scrollYSum); var scrollY = Math.trunc(scrollYSum);
@ -169,7 +169,7 @@ function onUpdateTimeout() {
scrollXSum -= scrollX; scrollXSum -= scrollX;
scrollYSum -= scrollY; scrollYSum -= scrollY;
idleUpdates = 0; idleUpdates = 0;
ws.send("PointerScroll " + scrollX + " " + scrollY); ws.send("s" + scrollX + ";" + scrollY);
} }
if (idleUpdates >= MAX_IDLE_UPDATES) { if (idleUpdates >= MAX_IDLE_UPDATES) {
clearInterval(updateTimer); clearInterval(updateTimer);
@ -222,7 +222,7 @@ function handleEnd(evt) {
} }
if (ongoingTouches.length == 0 && touchReleasedCount >= 1 && if (ongoingTouches.length == 0 && touchReleasedCount >= 1 &&
dragging) { dragging) {
ws.send("PointerButton 1 0"); ws.send("b1;0");
} }
if (ongoingTouches.length == 0 && touchReleasedCount >= 1 && if (ongoingTouches.length == 0 && touchReleasedCount >= 1 &&
!touchMoved && evt.timeStamp - touchStart < TOUCH_TIMEOUT) { !touchMoved && evt.timeStamp - touchStart < TOUCH_TIMEOUT) {
@ -234,11 +234,11 @@ function handleEnd(evt) {
}else if (touchReleasedCount == 3) { }else if (touchReleasedCount == 3) {
button = 2; button = 2;
} }
ws.send("PointerButton " + button + " 1"); ws.send("b" + button + ";1");
if (button == 1) { if (button == 1) {
draggingTimeout = setTimeout(onDraggingTimeout, TOUCH_TIMEOUT); draggingTimeout = setTimeout(onDraggingTimeout, TOUCH_TIMEOUT);
} else { } else {
ws.send("PointerButton " + button + " 0"); ws.send("b" + button + ";0");
} }
} }
} }
@ -365,7 +365,7 @@ window.addEventListener("load", function() {
document.getElementById("sendbutton").addEventListener("click", document.getElementById("sendbutton").addEventListener("click",
function(e) { function(e) {
if (text.value != "") { if (text.value != "") {
ws.send("KeyboardText " + text.value); ws.send("t" + text.value);
text.value = ""; text.value = "";
} }
window.history.back(); window.history.back();