diff --git a/lib/units/device/plugins/touch.js b/lib/units/device/plugins/touch.js index 267b4094..5986d3d8 100644 --- a/lib/units/device/plugins/touch.js +++ b/lib/units/device/plugins/touch.js @@ -95,34 +95,38 @@ module.exports = syrup.serial() socket.write(command) } - var getX = function(point) { - return Math.floor(point.x * socket.maxX) - } - - var getY = function(point) { - return Math.floor(point.y * socket.maxY) - } - - switch (flags.get('forceTouchOrigin', 'top left')) { - case 'bottom left': - // So far the only device we've seen exhibiting this behavior - // is Yoga Tablet 8. - log.info('Touch origin is bottom left') - getX = function(point) { - return Math.floor((1 - point.y) * socket.maxX) - } - getY = function(point) { - return Math.floor(point.x * socket.maxY) - } - break - } + // Usually the touch origin is the same as the display's origin, + // but sometimes it might not be. + var getters = (function(origin) { + log.info('Touch origin is %s', origin) + return { + 'top left': { + x: function(point) { + return Math.floor(point.x * socket.maxX) + } + , y: function(point) { + return Math.floor(point.y * socket.maxY) + } + } + // So far the only device we've seen exhibiting this behavior + // is Yoga Tablet 8. + , 'bottom left': { + x: function(point) { + return Math.floor((1 - point.y) * socket.maxX) + }, + y: function(point) { + return Math.floor(point.x * socket.maxY) + } + } + }[origin] + })(flags.get('forceTouchOrigin', 'top left')) plugin.touchDown = function(point) { send(util.format( 'd %s %s %s %s\n' , point.contact - , getX(point) - , getY(point) + , getters.x(point) + , getters.y(point) , Math.floor((point.pressure || 0.5) * socket.maxPressure) )) } @@ -131,8 +135,8 @@ module.exports = syrup.serial() send(util.format( 'm %s %s %s %s\n' , point.contact - , getX(point) - , getY(point) + , getters.x(point) + , getters.y(point) , Math.floor((point.pressure || 0.5) * socket.maxPressure) )) }