mirror of
https://github.com/DeviceFarmer/stf.git
synced 2026-04-18 08:03:30 +02:00
New multitouch-compatible touch system.
This commit is contained in:
@@ -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)
|
||||
|
||||
93
lib/units/device/resources/minitouch.js
Normal file
93
lib/units/device/resources/minitouch.js
Normal file
@@ -0,0 +1,93 @@
|
||||
var util = require('util')
|
||||
|
||||
var Promise = require('bluebird')
|
||||
var syrup = require('syrup')
|
||||
|
||||
var logger = require('../../../util/logger')
|
||||
var pathutil = require('../../../util/pathutil')
|
||||
var devutil = require('../../../util/devutil')
|
||||
var streamutil = require('../../../util/streamutil')
|
||||
|
||||
module.exports = syrup.serial()
|
||||
.dependency(require('../support/adb'))
|
||||
.dependency(require('../support/properties'))
|
||||
.define(function(options, adb, properties) {
|
||||
var log = logger.createLogger('device:resources:minitouch')
|
||||
|
||||
var resources = {
|
||||
bin: {
|
||||
src: pathutil.vendor(util.format(
|
||||
'minitouch/%s/minitouch'
|
||||
, properties['ro.product.cpu.abi']
|
||||
))
|
||||
, dest: '/data/local/tmp/minitouch'
|
||||
, comm: 'minitouch'
|
||||
, mode: 0755
|
||||
}
|
||||
}
|
||||
|
||||
function removeResource(res) {
|
||||
return adb.shell(options.serial, ['rm', res.dest])
|
||||
.timeout(10000)
|
||||
.then(function(out) {
|
||||
return streamutil.readAll(out)
|
||||
})
|
||||
.return(res)
|
||||
}
|
||||
|
||||
function installResource(res) {
|
||||
return adb.push(options.serial, res.src, res.dest, res.mode)
|
||||
.timeout(10000)
|
||||
.then(function(transfer) {
|
||||
return new Promise(function(resolve, reject) {
|
||||
transfer.on('error', reject)
|
||||
transfer.on('end', resolve)
|
||||
})
|
||||
})
|
||||
.return(res)
|
||||
}
|
||||
|
||||
function ensureNotBusy(res) {
|
||||
return adb.shell(options.serial, [res.dest, '--help'])
|
||||
.timeout(10000)
|
||||
.then(function(out) {
|
||||
// Can be "Text is busy", "text busy"
|
||||
return streamutil.findLine(out, (/busy/i))
|
||||
.timeout(10000)
|
||||
.then(function() {
|
||||
log.info('Binary is busy, will retry')
|
||||
return Promise.delay(1000)
|
||||
})
|
||||
.then(function() {
|
||||
return ensureNotBusy(res)
|
||||
})
|
||||
.catch(streamutil.NoSuchLineError, function() {
|
||||
return res
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
function installAll() {
|
||||
return Promise.all([
|
||||
removeResource(resources.bin).then(installResource).then(ensureNotBusy)
|
||||
])
|
||||
}
|
||||
|
||||
function stop() {
|
||||
return devutil.killProcsByComm(
|
||||
adb
|
||||
, options.serial
|
||||
, resources.bin.comm
|
||||
, resources.bin.dest
|
||||
)
|
||||
.timeout(15000)
|
||||
}
|
||||
|
||||
return stop()
|
||||
.then(installAll)
|
||||
.then(function() {
|
||||
return {
|
||||
bin: resources.bin.dest
|
||||
}
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user