Add a flState in Kobo, and KoboPowerD:toggleFrontlight uses flState to control the light instead of intensity.

pull/1848/head
zijiehe 8 years ago
parent a1d4a9603a
commit e84093c414

@ -47,10 +47,13 @@ function BasePowerD:read_str_file(file)
end end
end end
function BasePowerD:setIntensity(intensity) function BasePowerD:normalizeIntensity(intensity)
intensity = intensity < self.fl_min and self.fl_min or intensity intensity = intensity < self.fl_min and self.fl_min or intensity
intensity = intensity > self.fl_max and self.fl_max or intensity return intensity > self.fl_max and self.fl_max or intensity
self.flIntensity = intensity end
function BasePowerD:setIntensity(intensity)
self.flIntensity = self:normalizeIntensity(intensity)
self:setIntensityHW() self:setIntensityHW()
end end

@ -5,10 +5,15 @@
local NickelConf = {} local NickelConf = {}
NickelConf.frontLightLevel = {} NickelConf.frontLightLevel = {}
NickelConf.frontLightState = {}
local kobo_conf_path = '/mnt/onboard/.kobo/Kobo/Kobo eReader.conf' local kobo_conf_path = '/mnt/onboard/.kobo/Kobo/Kobo eReader.conf'
local re_BrightnessValue = "[0-9]+" local front_light_level_str = "FrontLightLevel"
local re_FrontLightLevel = "^FrontLightLevel%s*=%s*(" .. re_BrightnessValue .. ")%s*$" local front_light_state_str = "FrontLightState"
-- Nickel will set FrontLightLevel to 0 - 100
local re_FrontLightLevel = "^" .. front_light_level_str .. "%s*=%s*([0-9]+)%s*$"
-- Nickel will set FrontLightState to true (light on) or false (light off)
local re_FrontLightState = "^" .. front_light_state_str .. "%s*=%s*(.+)%s*$"
local re_PowerOptionsSection = "^%[PowerOptions%]%s*" local re_PowerOptionsSection = "^%[PowerOptions%]%s*"
local re_AnySection = "^%[.*%]%s*" local re_AnySection = "^%[.*%]%s*"
@ -17,24 +22,20 @@ function NickelConf._set_kobo_conf_path(new_path)
kobo_conf_path = new_path kobo_conf_path = new_path
end end
function NickelConf.frontLightLevel.get() function NickelConf._read_kobo_conf(re_Match)
local value
local new_intensity
local correct_section = false local correct_section = false
local kobo_conf = io.open(kobo_conf_path, "r") local kobo_conf = io.open(kobo_conf_path, "r")
if kobo_conf then if kobo_conf then
for line in kobo_conf:lines() do for line in kobo_conf:lines() do
if string.match(line, re_AnySection) then if string.match(line, re_PowerOptionsSection) then
correct_section = true
elseif string.match(line, re_AnySection) then
correct_section = false correct_section = false
if string.match(line, re_PowerOptionsSection) then elseif correct_section then
correct_section = true value = string.match(line, re_Match)
end if value then
end
if correct_section then
new_intensity = string.match(line, re_FrontLightLevel)
if new_intensity then
new_intensity = tonumber(new_intensity)
break break
end end
end end
@ -42,75 +43,108 @@ function NickelConf.frontLightLevel.get()
kobo_conf:close() kobo_conf:close()
end end
if not new_intensity then return value
local Device = require("device") end
local powerd = Device:getPowerDevice()
local fallback_FrontLightLevel = powerd.flIntensity or 1 function NickelConf.frontLightLevel.get()
local new_intensity = NickelConf._read_kobo_conf(re_FrontLightLevel)
if new_intensity then
new_intensity = tonumber(new_intensity)
end
-- In NickelConfSpec, require("device") won't return KoboDevice
local powerd = require("device/kobo/powerd")
if new_intensity then
return powerd:normalizeIntensity(new_intensity)
else
local fallback_FrontLightLevel = powerd.flIntensity or 1
assert(NickelConf.frontLightLevel.set(fallback_FrontLightLevel)) assert(NickelConf.frontLightLevel.set(fallback_FrontLightLevel))
return fallback_FrontLightLevel return fallback_FrontLightLevel
end end
return new_intensity
end end
function NickelConf.frontLightLevel.set(new_intensity) function NickelConf.frontLightState.get()
assert(new_intensity >= 0 and new_intensity <= 100, local new_state = NickelConf._read_kobo_conf(re_FrontLightState)
"Wrong brightness value given!") if new_state then
new_state = (new_state == "true") or false
end
local kobo_conf if new_state == nil then
local old_intensity assert(NickelConf.frontLightState.set(false))
local remaining_file = "" return false
end
return new_state
end
function NickelConf._write_kobo_conf(re_Match, key, value)
local kobo_conf = io.open(kobo_conf_path, "r")
local lines = {} local lines = {}
local current_position local found = false
local remaining
local correct_section = false local correct_section = false
local modified_brightness = false local new_value_line = key .. "=" .. tostring(value)
kobo_conf = io.open(kobo_conf_path, "r")
if kobo_conf then if kobo_conf then
local pos
for line in kobo_conf:lines() do for line in kobo_conf:lines() do
if string.match(line, re_AnySection) then if string.match(line, re_AnySection) then
if correct_section then if correct_section then
-- found a new section after having found the correct one, -- found a new section after having found the correct one,
-- therefore the key was missing: let the code below add it -- therefore the key was missing: let the code below add it
kobo_conf:seek("set", current_position) kobo_conf:seek("set", pos)
break break
end elseif string.match(line, re_PowerOptionsSection) then
if string.match(line, re_PowerOptionsSection) then
correct_section = true correct_section = true
end end
end end
old_intensity = string.match(line, re_FrontLightLevel) local old_value = string.match(line, re_Match)
if correct_section and old_intensity then if correct_section and old_value then
lines[#lines + 1] = string.gsub(line, re_BrightnessValue, new_intensity, 1) lines[#lines + 1] = new_value_line
modified_brightness = true found = true
break break
else else
lines[#lines + 1] = line lines[#lines + 1] = line
end end
current_position = kobo_conf:seek() pos = kobo_conf:seek()
end end
remaining = kobo_conf:read("*a")
kobo_conf:close()
end end
if not modified_brightness then if not found then
if not correct_section then if not correct_section then
lines[#lines + 1] = '[PowerOptions]' lines[#lines + 1] = "[PowerOptions]"
end end
lines[#lines + 1] = 'FrontLightLevel=' .. new_intensity lines[#lines + 1] = new_value_line
end
if kobo_conf then
remaining_file = kobo_conf:read("*a")
kobo_conf:close()
end end
local kobo_conf_w = assert(io.open(kobo_conf_path, "w")) local kobo_conf_w = assert(io.open(kobo_conf_path, "w"))
for i, line in ipairs(lines) do for i, line in ipairs(lines) do
kobo_conf_w:write(line, "\n") kobo_conf_w:write(line, "\n")
end end
kobo_conf_w:write(remaining_file) if remaining then
kobo_conf_w:write(remaining)
end
kobo_conf_w:close() kobo_conf_w:close()
return true return true
end end
function NickelConf.frontLightLevel.set(new_intensity)
assert(new_intensity >= 0 and new_intensity <= 100,
"Wrong brightness value given!")
return NickelConf._write_kobo_conf(re_FrontLightLevel,
front_light_level_str,
new_intensity)
end
function NickelConf.frontLightState.set(new_state)
assert(type(new_state) == "boolean",
"Wrong front light state value type (expect boolean)!")
return NickelConf._write_kobo_conf(re_FrontLightState,
front_light_state_str,
new_state)
end
return NickelConf return NickelConf

@ -1,14 +1,20 @@
local BasePowerD = require("device/generic/powerd") local BasePowerD = require("device/generic/powerd")
local NickelConf = require("device/kobo/nickel_conf") local NickelConf = require("device/kobo/nickel_conf")
local batt_state_folder =
"/sys/devices/platform/pmic_battery.1/power_supply/mc13892_bat/"
local KoboPowerD = BasePowerD:new{ local KoboPowerD = BasePowerD:new{
fl_min = 0, fl_max = 100, -- Do not actively set front light to 0, it may confuse users -- pressing
-- hardware button won't take any effect.
fl_min = 1, fl_max = 100,
flIntensity = 20, flIntensity = 20,
restore_settings = true, restore_settings = true,
fl = nil, fl = nil,
batt_capacity_file = "/sys/devices/platform/pmic_battery.1/power_supply/mc13892_bat/capacity", flState = false,
is_charging_file = "/sys/devices/platform/pmic_battery.1/power_supply/mc13892_bat/status", batt_capacity_file = batt_state_folder .. "capacity",
is_charging_file = batt_state_folder .. "status",
battCapacity = nil, battCapacity = nil,
is_charging = nil, is_charging = nil,
} }
@ -23,7 +29,15 @@ end
function KoboPowerD:toggleFrontlight() function KoboPowerD:toggleFrontlight()
if self.fl ~= nil then if self.fl ~= nil then
self.fl:toggle() if self.flState then
self.fl:setBrightness(0)
else
self.fl:setBrightness(self.flIntensity)
end
self.flState = not self.flState
if KOBO_SYNC_BRIGHTNESS_WITH_NICKEL then
NickelConf.frontLightState.set(self.flState)
end
end end
end end

@ -12,7 +12,8 @@ local MILLION = 1000000
-- there is only one instance of this -- there is only one instance of this
local UIManager = { local UIManager = {
-- trigger a full refresh when counter reaches FULL_REFRESH_COUNT -- trigger a full refresh when counter reaches FULL_REFRESH_COUNT
FULL_REFRESH_COUNT = G_reader_settings:readSetting("full_refresh_count") or DRCOUNTMAX, FULL_REFRESH_COUNT =
G_reader_settings:readSetting("full_refresh_count") or DRCOUNTMAX,
refresh_count = 0, refresh_count = 0,
event_handlers = nil, event_handlers = nil,
@ -63,17 +64,24 @@ function UIManager:init()
local kobo_light_on_start = tonumber(KOBO_LIGHT_ON_START) local kobo_light_on_start = tonumber(KOBO_LIGHT_ON_START)
if kobo_light_on_start then if kobo_light_on_start then
local new_intensity local new_intensity
if kobo_light_on_start >= 0 then local new_state
if kobo_light_on_start > 0 then
new_intensity = math.min(kobo_light_on_start, 100) new_intensity = math.min(kobo_light_on_start, 100)
new_state = true
elseif kobo_light_on_start == 0 then
new_state = false
elseif kobo_light_on_start == -2 then elseif kobo_light_on_start == -2 then
local NickelConf = require("device/kobo/nickel_conf") local NickelConf = require("device/kobo/nickel_conf")
new_intensity = NickelConf.frontLightLevel:get() new_intensity = NickelConf.frontLightLevel.get()
new_state = NickelConf.frontLightState:get()
end end
if new_intensity then if new_intensity then
-- Since this kobo-specific, we save here and let the code pick -- Since this kobo-specific, we save here and let the code pick
-- it up later from the reader settings. -- it up later from the reader settings.
G_reader_settings:saveSetting("frontlight_intensity", new_intensity) G_reader_settings:saveSetting(
"frontlight_intensity", new_intensity)
end end
G_reader_settings:saveSetting("frontlight_state", new_state)
end end
elseif Device:isKindle() then elseif Device:isKindle() then
self.event_handlers["IntoSS"] = function() self.event_handlers["IntoSS"] = function()

@ -6,8 +6,12 @@ local DataStorage = require("datastorage")
pcall(dofile, DataStorage:getDataDir() .. "/defaults.persistent.lua") pcall(dofile, DataStorage:getDataDir() .. "/defaults.persistent.lua")
-- set search path for 'require()' -- set search path for 'require()'
package.path = "common/?.lua;rocks/share/lua/5.1/?.lua;frontend/?.lua;" .. package.path package.path =
package.cpath = "common/?.so;common/?.dll;/usr/lib/lua/?.so;rocks/lib/lua/5.1/?.so;" .. package.cpath "common/?.lua;rocks/share/lua/5.1/?.lua;frontend/?.lua;" ..
package.path
package.cpath =
"common/?.so;common/?.dll;/usr/lib/lua/?.so;rocks/lib/lua/5.1/?.so;" ..
package.cpath
-- set search path for 'ffi.load()' -- set search path for 'ffi.load()'
local ffi = require("ffi") local ffi = require("ffi")
@ -117,8 +121,12 @@ if Device:isKobo() then
local powerd = Device:getPowerDevice() local powerd = Device:getPowerDevice()
if powerd and powerd.restore_settings then if powerd and powerd.restore_settings then
local intensity = G_reader_settings:readSetting("frontlight_intensity") local intensity = G_reader_settings:readSetting("frontlight_intensity")
intensity = intensity or powerd.flIntensity powerd.flIntensity = intensity or powerd.flIntensity
powerd:setIntensity(intensity) local state = G_reader_settings:readSetting("frontlight_state")
if state then
-- Default state is off
powerd:toggleFrontlight()
end
end end
if Device:getCodeName() == "trilogy" then if Device:getCodeName() == "trilogy" then
require("utils/kobo_touch_probe") require("utils/kobo_touch_probe")
@ -132,8 +140,8 @@ if ARGV[argidx] and ARGV[argidx] ~= "" then
elseif open_last and last_file then elseif open_last and last_file then
file = last_file file = last_file
end end
-- if file is given in command line argument or open last document is set true -- if file is given in command line argument or open last document is set
-- the given file or the last file is opened in the reader -- true, the given file or the last file is opened in the reader
if file then if file then
local ReaderUI = require("apps/reader/readerui") local ReaderUI = require("apps/reader/readerui")
ReaderUI:showReader(file) ReaderUI:showReader(file)
@ -141,7 +149,8 @@ if ARGV[argidx] and ARGV[argidx] ~= "" then
-- the filemanger will show the files in that path -- the filemanger will show the files in that path
else else
local FileManager = require("apps/filemanager/filemanager") local FileManager = require("apps/filemanager/filemanager")
local home_dir = G_reader_settings:readSetting("home_dir") or ARGV[argidx] local home_dir =
G_reader_settings:readSetting("home_dir") or ARGV[argidx]
FileManager:showFiles(home_dir) FileManager:showFiles(home_dir)
end end
UIManager:run() UIManager:run()
@ -154,7 +163,8 @@ else
end end
local function exitReader() local function exitReader()
local ReaderActivityIndicator = require("apps/reader/modules/readeractivityindicator") local ReaderActivityIndicator =
require("apps/reader/modules/readeractivityindicator")
G_reader_settings:close() G_reader_settings:close()

@ -12,6 +12,7 @@ describe("Nickel configuation module", function()
foo=bar foo=bar
[PowerOptions] [PowerOptions]
FrontLightLevel=55 FrontLightLevel=55
FrontLightState=true
[YetAnotherThing] [YetAnotherThing]
bar=baz bar=baz
]]) ]])
@ -19,6 +20,46 @@ bar=baz
NickelConf._set_kobo_conf_path(fn) NickelConf._set_kobo_conf_path(fn)
assert.Equals(NickelConf.frontLightLevel.get(), 55) assert.Equals(NickelConf.frontLightLevel.get(), 55)
assert.Equals(NickelConf.frontLightState.get(), true)
os.remove(fn)
end)
it("should also read value", function()
local fn = os.tmpname()
local fd = io.open(fn, "w")
fd:write([[
[OtherThing]
foo=bar
[PowerOptions]
FrontLightLevel=30
FrontLightState=false
[YetAnotherThing]
bar=baz
]])
fd:close()
NickelConf._set_kobo_conf_path(fn)
assert.Equals(NickelConf.frontLightLevel.get(), 30)
assert.Equals(NickelConf.frontLightState.get(), false)
os.remove(fn)
end)
it("should have default value", function()
local fn = os.tmpname()
local fd = io.open(fn, "w")
fd:write([[
[OtherThing]
foo=bar
[YetAnotherThing]
bar=baz
]])
fd:close()
NickelConf._set_kobo_conf_path(fn)
assert.Equals(NickelConf.frontLightLevel.get(), 20)
assert.Equals(NickelConf.frontLightState.get(), false)
os.remove(fn) os.remove(fn)
end) end)
@ -34,6 +75,7 @@ FrontLightLevel=6
NickelConf._set_kobo_conf_path(fn) NickelConf._set_kobo_conf_path(fn)
NickelConf.frontLightLevel.set(100) NickelConf.frontLightLevel.set(100)
NickelConf.frontLightState.set(true)
fd = io.open(fn, "r") fd = io.open(fn, "r")
assert.Equals(fd:read("*a"), [[ assert.Equals(fd:read("*a"), [[
@ -41,6 +83,7 @@ FrontLightLevel=6
FrontLightLevel=6 FrontLightLevel=6
[PowerOptions] [PowerOptions]
FrontLightLevel=100 FrontLightLevel=100
FrontLightState=true
]]) ]])
fd:close() fd:close()
os.remove(fn) os.remove(fn)
@ -50,11 +93,13 @@ FrontLightLevel=100
fd:close() fd:close()
NickelConf.frontLightLevel.set(20) NickelConf.frontLightLevel.set(20)
NickelConf.frontLightState.set(false)
fd = io.open(fn, "r") fd = io.open(fn, "r")
assert.Equals(fd:read("*a"), [[ assert.Equals(fd:read("*a"), [[
[PowerOptions] [PowerOptions]
FrontLightLevel=20 FrontLightLevel=20
FrontLightState=false
]]) ]])
fd:close() fd:close()
os.remove(fn) os.remove(fn)
@ -68,6 +113,7 @@ FrontLightLevel=20
foo=bar foo=bar
[PowerOptions] [PowerOptions]
FrontLightLevel=6 FrontLightLevel=6
FrontLightState=false
[YetAnotherThing] [YetAnotherThing]
bar=baz bar=baz
]]) ]])
@ -75,6 +121,7 @@ bar=baz
NickelConf._set_kobo_conf_path(fn) NickelConf._set_kobo_conf_path(fn)
NickelConf.frontLightLevel.set(100) NickelConf.frontLightLevel.set(100)
NickelConf.frontLightState.set(true)
fd = io.open(fn, "r") fd = io.open(fn, "r")
assert.Equals(fd:read("*a"), [[ assert.Equals(fd:read("*a"), [[
@ -82,6 +129,7 @@ bar=baz
foo=bar foo=bar
[PowerOptions] [PowerOptions]
FrontLightLevel=100 FrontLightLevel=100
FrontLightState=true
[YetAnotherThing] [YetAnotherThing]
bar=baz bar=baz
]]) ]])
@ -102,12 +150,14 @@ bar=baz
NickelConf._set_kobo_conf_path(fn) NickelConf._set_kobo_conf_path(fn)
NickelConf.frontLightLevel.set(1) NickelConf.frontLightLevel.set(1)
NickelConf.frontLightState.set(true)
fd = io.open(fn, "r") fd = io.open(fn, "r")
assert.Equals(fd:read("*a"), [[ assert.Equals(fd:read("*a"), [[
[PowerOptions] [PowerOptions]
foo=bar foo=bar
FrontLightLevel=1 FrontLightLevel=1
FrontLightState=true
[OtherThing] [OtherThing]
bar=baz bar=baz
]]) ]])
@ -122,11 +172,13 @@ bar=baz
NickelConf._set_kobo_conf_path(fn) NickelConf._set_kobo_conf_path(fn)
NickelConf.frontLightLevel.set(15) NickelConf.frontLightLevel.set(15)
NickelConf.frontLightState.set(false)
fd = io.open(fn, "r") fd = io.open(fn, "r")
assert.Equals([[ assert.Equals([[
[PowerOptions] [PowerOptions]
FrontLightLevel=15 FrontLightLevel=15
FrontLightState=false
]], ]],
fd:read("*a")) fd:read("*a"))
fd:close() fd:close()

Loading…
Cancel
Save