mirror of
https://github.com/openstf/stf
synced 2025-10-05 02:29:26 +02:00
New multitouch-compatible touch system.
This commit is contained in:
parent
38d20eba9a
commit
6c09a53d55
15 changed files with 861 additions and 280 deletions
|
@ -1,9 +1,10 @@
|
|||
var util = require('util')
|
||||
|
||||
var Promise = require('bluebird')
|
||||
var syrup = require('syrup')
|
||||
var monkey = require('adbkit-monkey')
|
||||
var split = require('split')
|
||||
|
||||
var wire = require('../../../wire')
|
||||
var devutil = require('../../../util/devutil')
|
||||
var logger = require('../../../util/logger')
|
||||
var lifecycle = require('../../../util/lifecycle')
|
||||
var streamutil = require('../../../util/streamutil')
|
||||
|
@ -12,111 +13,138 @@ var SeqQueue = require('../../../wire/seqqueue')
|
|||
module.exports = syrup.serial()
|
||||
.dependency(require('../support/adb'))
|
||||
.dependency(require('../support/router'))
|
||||
.dependency(require('../resources/remote'))
|
||||
.dependency(require('./display'))
|
||||
.dependency(require('./data'))
|
||||
.define(function(options, adb, router, remote, display, data) {
|
||||
.dependency(require('../resources/minitouch'))
|
||||
.define(function(options, adb, router, minitouch) {
|
||||
var log = logger.createLogger('device:plugins:touch')
|
||||
var plugin = Object.create(null)
|
||||
|
||||
var service = {
|
||||
port: 2820
|
||||
}
|
||||
|
||||
function openService() {
|
||||
function startService() {
|
||||
log.info('Launching touch service')
|
||||
return devutil.ensureUnusedPort(adb, options.serial, service.port)
|
||||
return adb.shell(options.serial, [
|
||||
'exec'
|
||||
, minitouch.bin
|
||||
])
|
||||
.timeout(10000)
|
||||
.then(function() {
|
||||
return adb.shell(options.serial, [
|
||||
'exec'
|
||||
, remote.bin
|
||||
, '--lib', remote.lib
|
||||
, '--listen-input', service.port
|
||||
])
|
||||
.timeout(10000)
|
||||
})
|
||||
.then(function(out) {
|
||||
lifecycle.share('Touch shell', out)
|
||||
streamutil.talk(log, 'Touch shell says: "%s"', out)
|
||||
})
|
||||
.then(function() {
|
||||
return devutil.waitForPort(adb, options.serial, service.port)
|
||||
.timeout(15000)
|
||||
})
|
||||
.then(function(conn) {
|
||||
return Promise.promisifyAll(monkey.connectStream(conn))
|
||||
})
|
||||
.then(function(monkey) {
|
||||
return lifecycle.share('Touch monkey', monkey)
|
||||
})
|
||||
}
|
||||
|
||||
function modifyCoords(message) {
|
||||
message.x = Math.floor(message.x * display.width)
|
||||
message.y = Math.floor(message.y * display.height)
|
||||
function connectService() {
|
||||
function tryConnect(times, delay) {
|
||||
return adb.openLocal(options.serial, '/data/local/tmp/minitouch.sock')
|
||||
.timeout(10000)
|
||||
.then(function(out) {
|
||||
lifecycle.share('Touch socket', out)
|
||||
return out
|
||||
})
|
||||
.then(function(out) {
|
||||
return new Promise(function(resolve, reject) {
|
||||
out.pipe(split()).on('data', function(line) {
|
||||
var args = line.toString().split(/ /g)
|
||||
switch (args[0]) {
|
||||
case 'v':
|
||||
out.version = +args[1]
|
||||
log.info('Touch protocol is version %d', out.version)
|
||||
break
|
||||
case '^':
|
||||
out.maxContacts = args[1]
|
||||
out.maxX = args[2]
|
||||
out.maxY = args[3]
|
||||
out.maxPressure = args[4]
|
||||
log.info(
|
||||
'Touch protocol reports %d contacts in a %dx%d grid '
|
||||
+ 'with a max pressure of %d'
|
||||
, out.maxContacts
|
||||
, out.maxX
|
||||
, out.maxY
|
||||
, out.maxPressure
|
||||
)
|
||||
return resolve(out)
|
||||
default:
|
||||
return reject(new Error(util.format(
|
||||
'Unknown metadata "%s"'
|
||||
, line
|
||||
)))
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
.catch(function(err) {
|
||||
if (/closed/.test(err.message) && times > 1) {
|
||||
return Promise.delay(delay)
|
||||
.then(function() {
|
||||
return tryConnect(--times, delay * 2)
|
||||
})
|
||||
}
|
||||
return Promise.reject(err)
|
||||
})
|
||||
}
|
||||
log.info('Connecting to touch service')
|
||||
return tryConnect(5, 100)
|
||||
}
|
||||
|
||||
return openService()
|
||||
.then(function(monkey) {
|
||||
var queue = new SeqQueue()
|
||||
, pressure = (data && data.touch && data.touch.defaultPressure) || 50
|
||||
return startService()
|
||||
.then(connectService)
|
||||
.then(function(socket) {
|
||||
var queue = new SeqQueue(100, 4)
|
||||
|
||||
log.info('Setting default pressure to %d', pressure)
|
||||
function send(command) {
|
||||
socket.write(command)
|
||||
}
|
||||
|
||||
plugin.touchDown = function(point) {
|
||||
modifyCoords(point)
|
||||
monkey.sendAsync([
|
||||
'touch down'
|
||||
, point.x
|
||||
, point.y
|
||||
, pressure
|
||||
].join(' '))
|
||||
.catch(function(err) {
|
||||
log.error('touchDown failed', err.stack)
|
||||
})
|
||||
send(util.format(
|
||||
'd %s %s %s %s\n'
|
||||
, point.contact
|
||||
, Math.floor(point.x * socket.maxX)
|
||||
, Math.floor(point.y * socket.maxY)
|
||||
, Math.floor((point.pressure || 0.5) * socket.maxPressure)
|
||||
))
|
||||
}
|
||||
|
||||
plugin.touchMove = function(point) {
|
||||
modifyCoords(point)
|
||||
monkey.sendAsync([
|
||||
'touch move'
|
||||
, point.x
|
||||
, point.y
|
||||
, pressure
|
||||
].join(' '))
|
||||
.catch(function(err) {
|
||||
log.error('touchMove failed', err.stack)
|
||||
})
|
||||
send(util.format(
|
||||
'm %s %s %s %s\n'
|
||||
, point.contact
|
||||
, Math.floor(point.x * socket.maxX)
|
||||
, Math.floor(point.y * socket.maxX)
|
||||
, Math.floor((point.pressure || 0.5) * socket.maxPressure)
|
||||
))
|
||||
}
|
||||
|
||||
plugin.touchUp = function(point) {
|
||||
modifyCoords(point)
|
||||
monkey.sendAsync([
|
||||
'touch up'
|
||||
, point.x
|
||||
, point.y
|
||||
, pressure
|
||||
].join(' '))
|
||||
.catch(function(err) {
|
||||
log.error('touchUp failed', err.stack)
|
||||
})
|
||||
send(util.format(
|
||||
'u %s\n'
|
||||
, point.contact
|
||||
))
|
||||
}
|
||||
|
||||
plugin.touchCommit = function() {
|
||||
send('c\n')
|
||||
}
|
||||
|
||||
plugin.touchReset = function() {
|
||||
send('r\n')
|
||||
}
|
||||
|
||||
plugin.tap = function(point) {
|
||||
modifyCoords(point)
|
||||
monkey.sendAsync([
|
||||
'tap'
|
||||
, point.x
|
||||
, point.y
|
||||
, pressure
|
||||
].join(' '))
|
||||
.catch(function(err) {
|
||||
log.error('tap failed', err.stack)
|
||||
})
|
||||
plugin.touchDown(point)
|
||||
plugin.touchCommit()
|
||||
plugin.touchUp(point)
|
||||
plugin.touchCommit()
|
||||
}
|
||||
|
||||
router
|
||||
.on(wire.GestureStartMessage, function(channel, message) {
|
||||
queue.start(message.seq)
|
||||
})
|
||||
.on(wire.GestureStopMessage, function(channel, message) {
|
||||
queue.push(message.seq, function() {
|
||||
queue.stop()
|
||||
})
|
||||
})
|
||||
.on(wire.TouchDownMessage, function(channel, message) {
|
||||
queue.push(message.seq, function() {
|
||||
plugin.touchDown(message)
|
||||
|
@ -131,12 +159,16 @@ module.exports = syrup.serial()
|
|||
queue.push(message.seq, function() {
|
||||
plugin.touchUp(message)
|
||||
})
|
||||
|
||||
// Reset queue
|
||||
queue = new SeqQueue()
|
||||
})
|
||||
.on(wire.TapMessage, function(channel, message) {
|
||||
plugin.tap(message)
|
||||
.on(wire.TouchCommitMessage, function(channel, message) {
|
||||
queue.push(message.seq, function() {
|
||||
plugin.touchCommit()
|
||||
})
|
||||
})
|
||||
.on(wire.TouchResetMessage, function(channel, message) {
|
||||
queue.push(message.seq, function() {
|
||||
plugin.touchReset()
|
||||
})
|
||||
})
|
||||
})
|
||||
.return(plugin)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue