2014-10-30 18:42:18 +00:00
|
|
|
local BasePowerD = require("device/generic/powerd")
|
2022-06-28 03:02:37 +00:00
|
|
|
local WakeupMgr = require("device/wakeupmgr")
|
|
|
|
local logger = require("logger")
|
2014-01-04 13:38:07 +00:00
|
|
|
-- liblipclua, see require below
|
|
|
|
|
|
|
|
local KindlePowerD = BasePowerD:new{
|
2014-03-13 13:52:43 +00:00
|
|
|
fl_min = 0, fl_max = 24,
|
2022-03-14 18:56:18 +00:00
|
|
|
fl_warmth_min = 0, fl_warmth_max = 24,
|
2014-07-04 18:10:38 +00:00
|
|
|
|
2014-03-13 13:52:43 +00:00
|
|
|
lipc_handle = nil,
|
2014-01-04 13:38:07 +00:00
|
|
|
}
|
|
|
|
|
2014-10-30 18:42:18 +00:00
|
|
|
function KindlePowerD:init()
|
2015-09-26 23:02:37 +00:00
|
|
|
local haslipc, lipc = pcall(require, "liblipclua")
|
|
|
|
if haslipc and lipc then
|
2014-07-04 18:10:38 +00:00
|
|
|
self.lipc_handle = lipc.init("com.github.koreader.kindlepowerd")
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
2020-03-26 03:56:34 +00:00
|
|
|
|
|
|
|
-- On devices where lipc step 0 is *not* off, we add a synthetic fl level where 0 *is* off,
|
|
|
|
-- which allows us to keep being able to use said step 0 as the first "on" step.
|
|
|
|
if not self.device:canTurnFrontlightOff() then
|
|
|
|
self.fl_max = self.fl_max + 1
|
|
|
|
end
|
2022-06-28 03:02:37 +00:00
|
|
|
|
|
|
|
self:initWakeupMgr()
|
2020-03-26 03:56:34 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- If we start with the light off (fl_intensity is fl_min), ensure a toggle will set it to the lowest "on" step,
|
2020-09-21 01:48:06 +00:00
|
|
|
-- and that we update fl_intensity (by using setIntensity and not setIntensityHW).
|
2020-03-26 03:56:34 +00:00
|
|
|
function KindlePowerD:turnOnFrontlightHW()
|
|
|
|
self:setIntensity(self.fl_intensity == self.fl_min and self.fl_min + 1 or self.fl_intensity)
|
|
|
|
end
|
|
|
|
-- Which means we need to get rid of the insane fl_intensity == fl_min shortcut in turnOnFrontlight, too...
|
|
|
|
-- That dates back to #2941, and I have no idea what it's supposed to help with.
|
|
|
|
function BasePowerD:turnOnFrontlight()
|
|
|
|
if not self.device:hasFrontlight() then return end
|
|
|
|
if self:isFrontlightOn() then return false end
|
|
|
|
self:turnOnFrontlightHW()
|
|
|
|
self.is_fl_on = true
|
2020-09-21 01:48:06 +00:00
|
|
|
self:stateChanged()
|
2020-03-26 03:56:34 +00:00
|
|
|
return true
|
2014-01-04 13:38:07 +00:00
|
|
|
end
|
|
|
|
|
2017-06-14 17:32:16 +00:00
|
|
|
function KindlePowerD:frontlightIntensityHW()
|
2019-04-08 21:05:08 +00:00
|
|
|
if not self.device:hasFrontlight() then return 0 end
|
2020-03-26 03:56:34 +00:00
|
|
|
-- Kindle stock software does not use intensity file directly, so go through lipc to keep us in sync.
|
2017-06-14 17:32:16 +00:00
|
|
|
if self.lipc_handle ~= nil then
|
2020-03-26 03:56:34 +00:00
|
|
|
-- Handle the step 0 switcheroo on ! canTurnFrontlightOff devices...
|
|
|
|
if self.device:canTurnFrontlightOff() then
|
|
|
|
return self.lipc_handle:get_int_property("com.lab126.powerd", "flIntensity")
|
|
|
|
else
|
|
|
|
local lipc_fl_intensity = self.lipc_handle:get_int_property("com.lab126.powerd", "flIntensity")
|
|
|
|
-- NOTE: If lipc returns 0, compare against what the kernel says,
|
|
|
|
-- to avoid breaking on/off detection on devices where lipc 0 doesn't actually turn it off (<= PW3),
|
|
|
|
-- c.f., #5986
|
|
|
|
if lipc_fl_intensity == self.fl_min then
|
|
|
|
local sysfs_fl_intensity = self:_readFLIntensity()
|
|
|
|
if sysfs_fl_intensity ~= self.fl_min then
|
|
|
|
-- Return something potentially slightly off (as we can't be sure of the sysfs -> lipc mapping),
|
|
|
|
-- but, more importantly, something that's not fl_min (0), so we properly detect the light as on,
|
|
|
|
-- and update fl_intensity accordingly.
|
|
|
|
-- That's only tripped if it was set to fl_min from the stock UI,
|
|
|
|
-- as we ourselves *do* really turn it off when we do that.
|
|
|
|
return self.fl_min + 1
|
|
|
|
else
|
|
|
|
return self.fl_min
|
|
|
|
end
|
|
|
|
else
|
|
|
|
-- We've added a synthetic step...
|
|
|
|
return lipc_fl_intensity + 1
|
|
|
|
end
|
|
|
|
end
|
2014-03-13 13:52:43 +00:00
|
|
|
else
|
2018-12-10 21:10:35 +00:00
|
|
|
-- 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.
|
2017-06-14 17:32:16 +00:00
|
|
|
return self:_readFLIntensity()
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
2014-01-04 13:38:07 +00:00
|
|
|
end
|
|
|
|
|
2017-06-14 17:32:16 +00:00
|
|
|
function KindlePowerD:setIntensityHW(intensity)
|
2020-03-26 03:56:34 +00:00
|
|
|
-- Handle the synthetic step switcheroo on ! canTurnFrontlightOff devices...
|
|
|
|
local turn_it_off = false
|
|
|
|
if not self.device:canTurnFrontlightOff() then
|
|
|
|
if intensity > 0 then
|
|
|
|
intensity = intensity - 1
|
|
|
|
else
|
|
|
|
-- And if we *really* requested 0, turn it off manually.
|
|
|
|
turn_it_off = true
|
|
|
|
end
|
|
|
|
end
|
2018-12-10 21:10:35 +00:00
|
|
|
-- 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
|
2017-02-26 10:19:20 +00:00
|
|
|
-- NOTE: We want to bypass setIntensity's shenanigans and simply restore the light as-is
|
2022-03-14 18:56:18 +00:00
|
|
|
self.lipc_handle:set_int_property("com.lab126.powerd", "flIntensity", intensity)
|
2018-12-10 21:10:35 +00:00
|
|
|
end
|
2020-03-26 03:56:34 +00:00
|
|
|
if turn_it_off then
|
2018-12-10 21:10:35 +00:00
|
|
|
-- NOTE: when intensity is 0, we want to *really* kill the light, so do it manually
|
2020-03-26 03:56:34 +00:00
|
|
|
-- (asking lipc to set it to 0 would in fact set it to > 0 on ! canTurnFrontlightOff Kindles).
|
2018-12-10 21:10:35 +00:00
|
|
|
-- We do *both* to make the fl restore on resume less jarring on devices where lipc 0 != off.
|
2021-04-24 23:15:38 +00:00
|
|
|
os.execute("printf '%s' ".. intensity .." > " .. self.fl_intensity_file)
|
2022-03-14 18:56:18 +00:00
|
|
|
|
|
|
|
-- And in case there are two LED groups...
|
|
|
|
if self.warmth_intensity_file then
|
|
|
|
os.execute("printf '%s' ".. intensity .." > " .. self.warmth_intensity_file)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function KindlePowerD:frontlightWarmthHW()
|
|
|
|
if self.lipc_handle ~= nil then
|
|
|
|
local nat_warmth = self.lipc_handle:get_int_property("com.lab126.powerd", "currentAmberLevel")
|
|
|
|
if nat_warmth then
|
|
|
|
-- [0...24] -> [0...100]
|
|
|
|
return self:fromNativeWarmth(nat_warmth)
|
|
|
|
else
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function KindlePowerD:setWarmthHW(warmth)
|
|
|
|
if self.lipc_handle ~= nil then
|
|
|
|
self.lipc_handle:set_int_property("com.lab126.powerd", "currentAmberLevel", warmth)
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
2014-01-04 13:38:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function KindlePowerD:getCapacityHW()
|
2014-03-13 13:52:43 +00:00
|
|
|
if self.lipc_handle ~= nil then
|
2017-03-24 06:36:15 +00:00
|
|
|
return self.lipc_handle:get_int_property("com.lab126.powerd", "battLevel")
|
2019-08-01 16:45:30 +00:00
|
|
|
elseif self.batt_capacity_file then
|
2017-03-24 06:36:15 +00:00
|
|
|
return self:read_int_file(self.batt_capacity_file)
|
2019-08-01 16:45:30 +00:00
|
|
|
else
|
|
|
|
local std_out = io.popen("gasgauge-info -c 2>/dev/null", "r")
|
|
|
|
if std_out then
|
2021-01-16 03:41:46 +00:00
|
|
|
local result = std_out:read("*number")
|
2019-08-01 16:45:30 +00:00
|
|
|
std_out:close()
|
2021-01-16 03:41:46 +00:00
|
|
|
return result or 0
|
2019-08-01 16:45:30 +00:00
|
|
|
else
|
|
|
|
return 0
|
|
|
|
end
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
2014-01-04 13:38:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function KindlePowerD:isChargingHW()
|
2017-03-24 06:36:15 +00:00
|
|
|
local is_charging
|
2014-03-13 13:52:43 +00:00
|
|
|
if self.lipc_handle ~= nil then
|
2017-03-24 06:36:15 +00:00
|
|
|
is_charging = self.lipc_handle:get_int_property("com.lab126.powerd", "isCharging")
|
2014-03-13 13:52:43 +00:00
|
|
|
else
|
2017-03-24 06:36:15 +00:00
|
|
|
is_charging = self:read_int_file(self.is_charging_file)
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
2017-03-24 06:36:15 +00:00
|
|
|
return is_charging == 1
|
2014-01-04 13:38:07 +00:00
|
|
|
end
|
|
|
|
|
A host of low power states related tweaks (#9036)
* Disable all non power management related input during suspend. (This prevents wonky touch events from being tripped when closing a sleep cover on an already-in-suspend device, among other things).
* Kobo: Use our WakeupMgr instance, not the class.
* WakupMgr: split `removeTask` in two:
* `removeTask`, which *only* takes a queue index as input, and only removes a single task. Greatly simplifies the function (i.e., it's just a `table.remove`).
* `removeTasks`, which takes an epoch or a cb ref, and removes *every* task that matches.
* Both of these will also *always* re-schedule the next task (if any) on exit, since we can have multiple WakeupMgr tasks queued, but we can only have a single RTC wake alarm set ;).
* `wakeupAction` now takes a `proximity` argument, which it passes on to its `validateWakeupAlarmByProximity` call, allowing call sites to avoir having to duplicate that call themselves when they want to use a custom proximity window.
* `wakeupAction` now re-schedules the next task (if any) on exit.
* Simplify `Kobo:checkUnexpectedWakeup`, by removing the duplicate `WakerupMgr:validateWakeupAlarmByProximity` call, now that we can pass a proximity window to `WakeuoMgr:wakeupAction`.
* The various network activity timeouts are now halved when autostandby is enabled.
* Autostandby: get rid of the dummy deadline_guard task, as it's no longer necessary since #9009.
* UIManager: The previous change allows us to simplify `getNextTaskTimes` into a simpler `getNextTaskTime` variant, getting rid of a table & a loop.
* ReaderFooter & ReaderHeader: Make sure we only perform a single refresh when exiting standby.
* Kobo: Rewrite sysfs writes to use ANSI C via FFI instead of stdio via Lua, as it obscured some common error cases (e.g., EBUSY on /sys/power/state).
* Kobo: Simplify `suspend`, now that we have sane error handling in sysfs writes.
* Kobo.powerd: Change `isCharging` & `isAuxCharging` behavior to match the behavior of the NTX ioctl (i.e., Charging == Plugged-in). This has the added benefit of making the AutoSuspend checks behave sensibly in the "fully-charged but still plugged in" scenario (because being plugged in is enough to break PM on `!canPowerSaveWhileCharging` devices).
* AutoSuspend: Disable our `AllowStandby` handler when auto standby is disabled, so as to not interfere with other modules using `UIManager:allowStandby` (fix #9038).
* PowerD: Allow platforms to implement `isCharged`, indicating that the battery is full while still plugged in to a power source (battery icon becomes a power plug icon).
* Kobo.powerd: Implement `isCharged`, and kill charging LEDs once battery is full.
* Kindle.powerd: Implement `isCharged` on post-Wario devices. (`isCharging` is still true in that state, as it ought to).
2022-05-01 21:41:08 +00:00
|
|
|
function KindlePowerD:isChargedHW()
|
|
|
|
-- Older kernels don't necessarily have this...
|
|
|
|
if self.batt_status_file then
|
|
|
|
return self:read_str_file(self.batt_status_file) == "Full"
|
|
|
|
end
|
|
|
|
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2017-02-26 10:19:20 +00:00
|
|
|
function KindlePowerD:_readFLIntensity()
|
|
|
|
return self:read_int_file(self.fl_intensity_file)
|
|
|
|
end
|
|
|
|
|
|
|
|
function KindlePowerD:afterResume()
|
2019-04-08 21:05:08 +00:00
|
|
|
if not self.device:hasFrontlight() then
|
2017-02-26 10:19:20 +00:00
|
|
|
return
|
|
|
|
end
|
2018-12-10 21:10:35 +00:00
|
|
|
local UIManager = require("ui/uimanager")
|
2017-06-14 17:32:16 +00:00
|
|
|
if self:isFrontlightOn() then
|
2018-12-10 21:10:35 +00:00
|
|
|
-- 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)
|
2017-02-26 10:19:20 +00:00
|
|
|
else
|
2018-12-10 21:10:35 +00:00
|
|
|
-- But in the off case, we *do* use sysfs, so this one actually matters.
|
|
|
|
UIManager:tickAfterNext(function() self:turnOffFrontlightHW() end)
|
2017-02-26 10:19:20 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-21 10:52:58 +00:00
|
|
|
function KindlePowerD:toggleSuspend()
|
|
|
|
if self.lipc_handle then
|
|
|
|
self.lipc_handle:set_int_property("com.lab126.powerd", "powerButton", 1)
|
2019-06-05 19:36:38 +00:00
|
|
|
else
|
|
|
|
os.execute("powerd_test -p")
|
2019-03-21 10:52:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-06-28 03:02:37 +00:00
|
|
|
-- Kindle only allows setting the RTC via lipc during the ReadyToSuspend state
|
|
|
|
function KindlePowerD:setRtcWakeup(seconds_from_now)
|
|
|
|
if self.lipc_handle then
|
|
|
|
self.lipc_handle:set_int_property("com.lab126.powerd", "rtcWakeup", seconds_from_now)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Check the powerd state: are we still in screensaver mode.
|
|
|
|
function KindlePowerD:getPowerdState()
|
|
|
|
if self.lipc_handle then
|
|
|
|
return self.lipc_handle:get_string_property("com.lab126.powerd", "state")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function KindlePowerD:checkUnexpectedWakeup()
|
|
|
|
local state = self:getPowerdState()
|
|
|
|
logger.info("Powerd resume state:", state)
|
|
|
|
-- If we moved on to the active state,
|
|
|
|
-- then we were woken by user input not our alarm.
|
|
|
|
if state ~= "screenSaver" and state ~= "suspended" then return end
|
|
|
|
|
|
|
|
if self.device.wakeup_mgr:isWakeupAlarmScheduled() and self.device.wakeup_mgr:wakeupAction() then
|
|
|
|
logger.info("Kindle scheduled wakeup")
|
|
|
|
else
|
|
|
|
logger.info("Kindle unscheduled wakeup")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Dummy fuctions. They will be defined in initWakeupMgr
|
|
|
|
function KindlePowerD:wakeupFromSuspend() end
|
|
|
|
function KindlePowerD:readyToSuspend() end
|
|
|
|
|
|
|
|
-- Support WakeupMgr on Lipc & supportsScreensaver devices.
|
|
|
|
function KindlePowerD:initWakeupMgr()
|
|
|
|
if not self.device:supportsScreensaver() then return end
|
|
|
|
if self.lipc_handle == nil then return end
|
|
|
|
|
|
|
|
function KindlePowerD:wakeupFromSuspend()
|
|
|
|
logger.info("Kindle wakeupFromSuspend")
|
|
|
|
-- Give the device a few seconds to settle.
|
|
|
|
-- This filters out user input resumes -> device will resume to active
|
|
|
|
-- Also the Kindle stays in Ready to suspend for 10 seconds
|
|
|
|
-- so the alarm may fire 10 seconds early
|
|
|
|
local UIManager = require("ui/uimanager")
|
|
|
|
UIManager:scheduleIn(15, self.checkUnexpectedWakeup, self)
|
|
|
|
end
|
|
|
|
|
|
|
|
function KindlePowerD:readyToSuspend()
|
|
|
|
logger.info("Kindle readyToSuspend")
|
|
|
|
if self.device.wakeup_mgr:isWakeupAlarmScheduled() then
|
|
|
|
local now = os.time()
|
|
|
|
local alarm = self.device.wakeup_mgr:getWakeupAlarmEpoch()
|
|
|
|
if alarm > now then
|
|
|
|
-- Powerd / Lipc need seconds_from_now not epoch
|
|
|
|
self:setRtcWakeup(alarm - now)
|
|
|
|
else
|
|
|
|
-- wakeup time is in the past
|
|
|
|
self.device.wakeup_mgr:removeTasks(alarm)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
self.device.wakeup_mgr = WakeupMgr:new{rtc = require("device/kindle/mockrtc")}
|
|
|
|
end
|
|
|
|
|
2020-12-19 04:32:23 +00:00
|
|
|
--- @fixme: This won't ever fire, as KindlePowerD is already a metatable on a plain table.
|
|
|
|
function KindlePowerD:__gc()
|
|
|
|
if self.lipc_handle then
|
|
|
|
self.lipc_handle:close()
|
|
|
|
self.lipc_handle = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-01-04 13:38:07 +00:00
|
|
|
return KindlePowerD
|