diff --git a/frontend/device/kindle/device.lua b/frontend/device/kindle/device.lua index 39746873f..c9527ecff 100644 --- a/frontend/device/kindle/device.lua +++ b/frontend/device/kindle/device.lua @@ -16,6 +16,30 @@ local function kindleEnableWifi(toggle) end end +local function isWifiUp() + -- NOTE: Pilfered from Cervantes, c.f., #4380 + -- Possibly simpler and more compatible than the lipc approach ;). + local file = io.open("/sys/class/net/wlan0/carrier", "rb") + if not file then return 0 end + local status = tonumber(file:read("*all")) or 0 + file:close() + return status + + --[[ + local status = 0 + local haslipc, lipc = pcall(require, "liblipclua") + local lipc_handle = nil + if haslipc and lipc then + lipc_handle = lipc.init("com.github.koreader.networkmgr") + end + if lipc_handle then + status = lipc_handle:get_int_property("com.lab126.wifid", "enable") + lipc_handle:close() + end + return status + --]] +end + --[[ Test if a kindle device has Special Offers --]] @@ -62,6 +86,10 @@ function Kindle:initNetworkManager(NetworkMgr) NetworkMgr.turnOffWifi = function() kindleEnableWifi(0) end + + NetworkMgr.isWifiOn = function() + return 1 == isWifiUp() + end end function Kindle:supportsScreensaver() diff --git a/frontend/device/kindle/powerd.lua b/frontend/device/kindle/powerd.lua index 68d582fc4..b1ab91a17 100644 --- a/frontend/device/kindle/powerd.lua +++ b/frontend/device/kindle/powerd.lua @@ -21,18 +21,24 @@ function KindlePowerD:frontlightIntensityHW() if self.lipc_handle ~= nil then return self.lipc_handle:get_int_property("com.lab126.powerd", "flIntensity") else + -- NOTE: This fallback is of dubious use, as it will NOT match our expected [fl_min..fl_max] range, + -- each model has a specific curve. return self:_readFLIntensity() end end function KindlePowerD:setIntensityHW(intensity) - if self.lipc_handle ~= nil and intensity > 0 then + -- NOTE: This means we *require* a working lipc handle to set the FL: + -- it knows what the UI values should map to for the specific hardware much better than us. + if self.lipc_handle ~= nil then -- NOTE: We want to bypass setIntensity's shenanigans and simply restore the light as-is self.lipc_handle:set_int_property( "com.lab126.powerd", "flIntensity", intensity) - else - -- NOTE: when intensity is 0, We want to really kill the light, so do it manually - -- (asking lipc to set it to 0 would in fact set it to 1)... + end + if intensity == 0 then + -- NOTE: when intensity is 0, we want to *really* kill the light, so do it manually + -- (asking lipc to set it to 0 would in fact set it to 1 on most Kindles). + -- We do *both* to make the fl restore on resume less jarring on devices where lipc 0 != off. os.execute("echo -n ".. intensity .." > " .. self.fl_intensity_file) end end @@ -70,12 +76,16 @@ function KindlePowerD:afterResume() if not self.device.hasFrontlight() then return end + local UIManager = require("ui/uimanager") if self:isFrontlightOn() then - -- Kindle stock software should turn on the front light automatically. The follow statement - -- ensure the consistency of intensity. - self:turnOnFrontlightHW() + -- The Kindle framework should turn the front light back on automatically. + -- The following statement ensures consistency of intensity, but should basically always be redundant, + -- since we set intensity via lipc and not sysfs ;). + -- NOTE: This is race-y, and we want to *lose* the race, hence the use of the scheduler (c.f., #4392) + UIManager:tickAfterNext(function() self:turnOnFrontlightHW() end) else - self:turnOffFrontlightHW() + -- But in the off case, we *do* use sysfs, so this one actually matters. + UIManager:tickAfterNext(function() self:turnOffFrontlightHW() end) end end diff --git a/spec/unit/device_spec.lua b/spec/unit/device_spec.lua index 0908d91b2..1270eaf1c 100644 --- a/spec/unit/device_spec.lua +++ b/spec/unit/device_spec.lua @@ -232,18 +232,20 @@ describe("device module", function() assert.is.same(kindle_dev.powerd.fl_intensity, 12) kindle_dev.powerd:setIntensity(5) - assert.stub(os.execute).was_called_with( - "echo -n 5 > /sys/class/backlight/max77696-bl/brightness") assert.is.same(kindle_dev.powerd.fl_intensity, 5) kindle_dev.powerd:toggleFrontlight() assert.stub(os.execute).was_called_with( "echo -n 0 > /sys/class/backlight/max77696-bl/brightness") + -- Here be shenanigans: we don't override powerd's fl_intensity when we turn the light off, + -- so that we can properly turn it back on at the previous intensity ;) assert.is.same(kindle_dev.powerd.fl_intensity, 5) + -- But if we were to cat /sys/class/backlight/max77696-bl/brightness, it should now be 0. kindle_dev.powerd:toggleFrontlight() - assert.stub(os.execute).was_called_with( - "echo -n 5 > /sys/class/backlight/max77696-bl/brightness") + assert.is.same(kindle_dev.powerd.fl_intensity, 5) + -- And /sys/class/backlight/max77696-bl/brightness is now !0 + -- (exact value is HW-dependent, each model has a different curve, we let lipc do the work for us). end) it("oasis should interpret orientation event", function()