From 82a3e0f9ad9416119248f177a3dc7b2bcec79fc5 Mon Sep 17 00:00:00 2001 From: Qingping Hou Date: Mon, 23 May 2016 22:38:08 -0700 Subject: [PATCH] automatically adjust ev time for kobo touch --- README.md | 4 +++ frontend/device/input.lua | 7 ---- frontend/device/kobo/device.lua | 38 ++++++++++++++++++++- kodev | 3 +- spec/unit/device_spec.lua | 60 +++++++++++++++++++++++++++++++++ 5 files changed, 103 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index cb9098bca..1e2177ba6 100644 --- a/README.md +++ b/README.md @@ -190,11 +190,15 @@ To run unit tests: ./kodev test front ``` +NOTE: Extra dependencies for tests: busted and ansicolors from luarocks + To run Lua static analysis: ``` make static-check ``` +NOTE: Extra dependencies for tests: luacheck from luarocks + You may need to checkout the [travis config file][travis-conf] to setup up a proper testing environment. Briefly, you need to install `luarocks` and then install `busted` with `luarocks`. The "eng" language data file for diff --git a/frontend/device/input.lua b/frontend/device/input.lua index 016be3b90..4edccbac7 100644 --- a/frontend/device/input.lua +++ b/frontend/device/input.lua @@ -214,13 +214,6 @@ function Input:adjustTouchTranslate(ev, by) end end -function Input:adjustTouchAlyssum(ev) - ev.time = TimeVal:now() - if ev.type == EV_ABS and ev.code == ABS_MT_TRACKING_ID then - ev.value = ev.value - 1 - end -end - function Input:setTimeout(cb, tv_out) local item = { callback = cb, diff --git a/frontend/device/kobo/device.lua b/frontend/device/kobo/device.lua index 84504fe6d..8f43f12da 100644 --- a/frontend/device/kobo/device.lua +++ b/frontend/device/kobo/device.lua @@ -1,4 +1,5 @@ local Generic = require("device/generic/device") +local TimeVal = require("ui/timeval") local Geom = require("ui/geometry") local dbg = require("dbg") @@ -26,6 +27,10 @@ local KoboTrilogy = Kobo:new{ model = "Kobo_trilogy", needsTouchScreenProbe = yes, touch_switch_xy = false, + -- Some Kobo Touch models' kernel does not generate touch event with epoch + -- timestamp. This flag will probe for those models and setup event adjust + -- hook accordingly + touch_probe_ev_epoch_time = true, hasKeys = yes, } @@ -133,6 +138,31 @@ function Kobo:init() end end +local probeEvEpochTime +-- this function will update itself after the first touch event +probeEvEpochTime = function(self, ev) + -- this check should work if the device has uptime less than 10 years + if ev.time.sec <= 315569260 then + -- time is seconds since boot, force it to epoch + probeEvEpochTime = function(_, _ev) + _ev.time = TimeVal:now() + end + probeEvEpochTime(nil, ev) + else + -- time is already epoch time, no need to do anything + probeEvEpochTime = function(_, _) end + end +end + +local ABS_MT_TRACKING_ID = 57 +local EV_ABS = 3 +local adjustTouchAlyssum = function(self, ev) + ev.time = TimeVal:now() + if ev.type == EV_ABS and ev.code == ABS_MT_TRACKING_ID then + ev.value = ev.value - 1 + end +end + function Kobo:initEventAdjustHooks() -- it's called KOBO_TOUCH_MIRRORED in defaults.lua, but what it -- actually did in its original implementation was to switch X/Y. @@ -153,7 +183,13 @@ function Kobo:initEventAdjustHooks() end if self.touch_alyssum_protocol then - self.input:registerEventAdjustHook(self.input.adjustTouchAlyssum) + self.input:registerEventAdjustHook(adjustTouchAlyssum) + end + + if self.touch_probe_ev_epoch_time then + self.input:registerEventAdjustHook(function(_, ev) + probeEvEpochTime(_, ev) + end) end if self.touch_phoenix_protocol then diff --git a/kodev b/kodev index 09e689d4b..70dd30ccb 100755 --- a/kodev +++ b/kodev @@ -321,7 +321,8 @@ OPTIONS: if [ ! -z "$2" ]; then test_path="${test_path}/$2" fi - busted --lua="./luajit ${opts}" \ + + busted --lua="./luajit" ${opts} \ --no-auto-insulate \ --lazy \ -o "./spec/$1/unit/verbose_print" \ diff --git a/spec/unit/device_spec.lua b/spec/unit/device_spec.lua index bfdc4b0ba..c45013008 100644 --- a/spec/unit/device_spec.lua +++ b/spec/unit/device_spec.lua @@ -15,7 +15,9 @@ describe("device module", function() end) describe("kobo", function() + local TimeVal setup(function() + TimeVal = require("ui/timeval") mock_fb = { new = function() return { @@ -63,6 +65,7 @@ describe("device module", function() kobo_dev:init() local Screen = kobo_dev.screen + kobo_dev.touch_probe_ev_epoch_time = false assert.is.same("Kobo_trilogy", kobo_dev.model) assert.truthy(kobo_dev:needsTouchScreenProbe()) G_reader_settings:saveSetting("kobo_touch_switch_xy", true) @@ -93,6 +96,63 @@ describe("device module", function() package.loaded['ffi/framebuffer_mxcfb'] = nil os.getenv:revert() mock_input.open:revert() + -- reset eventAdjustHook + kobo_dev.input.eventAdjustHook = function() end + kobo_dev.touch_probe_ev_epoch_time = true + end) + + it("should setup eventAdjustHooks properly for trilogy with non-epoch ev time", function() + local saved_getenv = os.getenv + stub(os, "getenv") + os.getenv.invokes(function(key) + if key == "PRODUCT" then + return "trilogy" + else + return saved_getenv(key) + end + end) + package.loaded['device/kobo/device'] = nil + package.loaded['ffi/framebuffer_mxcfb'] = mock_fb + mock_input = require('device/input') + stub(mock_input, "open") + + local kobo_dev = require("device/kobo/device") + kobo_dev:init() + local Screen = kobo_dev.screen + + assert.is.same("Kobo_trilogy", kobo_dev.model) + local x, y = Screen:getWidth()-5, 10 + local EV_ABS = 3 + local ABS_X = 00 + local ABS_Y = 01 + -- mirror x, then switch_xy + local ev_x = { + type = EV_ABS, + code = ABS_X, + value = x, + time = {sec = 1000} + } + local ev_y = { + type = EV_ABS, + code = ABS_Y, + value = y, + time = {sec = 1000} + } + + assert.truthy(kobo_dev.touch_probe_ev_epoch_time) + G_reader_settings:saveSetting("kobo_touch_switch_xy", true) + kobo_dev:touchScreenProbe() + + kobo_dev.input:eventAdjustHook(ev_x) + kobo_dev.input:eventAdjustHook(ev_y) + local cur_sec = TimeVal:now().sec + assert.truthy(cur_sec - ev_x.time.sec < 10) + assert.truthy(cur_sec - ev_y.time.sec < 10) + + package.loaded['ffi/framebuffer_mxcfb'] = nil + os.getenv:revert() + mock_input.open:revert() + kobo_dev.input.eventAdjustHook = function() end end) it("should flush book settings before suspend", function()