2019-09-11 16:11:24 +00:00
|
|
|
--[[--
|
|
|
|
Access and modify values in `Kobo eReader.conf` used by Nickel.
|
|
|
|
Only PowerOptions:FrontLightLevel is currently supported.
|
2016-01-07 14:30:28 +00:00
|
|
|
]]
|
|
|
|
|
2018-02-04 11:38:48 +00:00
|
|
|
local dbg = require("dbg")
|
|
|
|
|
2016-01-07 14:30:28 +00:00
|
|
|
local NickelConf = {}
|
|
|
|
NickelConf.frontLightLevel = {}
|
2016-02-25 08:54:41 +00:00
|
|
|
NickelConf.frontLightState = {}
|
2018-02-04 11:38:48 +00:00
|
|
|
NickelConf.colorSetting = {}
|
2018-03-31 14:00:49 +00:00
|
|
|
NickelConf.autoColorEnabled = {}
|
2016-01-07 14:30:28 +00:00
|
|
|
|
|
|
|
local kobo_conf_path = '/mnt/onboard/.kobo/Kobo/Kobo eReader.conf'
|
2016-02-25 08:54:41 +00:00
|
|
|
local front_light_level_str = "FrontLightLevel"
|
|
|
|
local front_light_state_str = "FrontLightState"
|
2018-02-04 11:38:48 +00:00
|
|
|
local color_setting_str = "ColorSetting"
|
2018-03-31 14:00:49 +00:00
|
|
|
local auto_color_enabled_str = "AutoColorEnabled"
|
2016-02-25 08:54:41 +00:00
|
|
|
-- 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*$"
|
2018-02-04 11:38:48 +00:00
|
|
|
-- Nickel will set ColorSetting to 1500 - 6400
|
|
|
|
local re_ColorSetting = "^" .. color_setting_str .. "%s*=%s*([0-9]+)%s*$"
|
2018-03-31 14:00:49 +00:00
|
|
|
-- AutoColorEnabled is 'true' or 'false'
|
|
|
|
-- We do not support 'BedTime' (it is saved as QVariant in Nickel)
|
|
|
|
local re_AutoColorEnabled = "^" .. auto_color_enabled_str .. "%s*=%s*([a-z]+)%s*$"
|
2016-01-07 14:30:28 +00:00
|
|
|
local re_PowerOptionsSection = "^%[PowerOptions%]%s*"
|
|
|
|
local re_AnySection = "^%[.*%]%s*"
|
|
|
|
|
2016-02-03 18:35:47 +00:00
|
|
|
|
|
|
|
function NickelConf._set_kobo_conf_path(new_path)
|
|
|
|
kobo_conf_path = new_path
|
|
|
|
end
|
|
|
|
|
2016-02-25 08:54:41 +00:00
|
|
|
function NickelConf._read_kobo_conf(re_Match)
|
|
|
|
local value
|
2016-01-07 14:30:28 +00:00
|
|
|
local correct_section = false
|
2016-01-12 13:52:44 +00:00
|
|
|
local kobo_conf = io.open(kobo_conf_path, "r")
|
|
|
|
|
|
|
|
if kobo_conf then
|
|
|
|
for line in kobo_conf:lines() do
|
2016-02-25 08:54:41 +00:00
|
|
|
if string.match(line, re_PowerOptionsSection) then
|
|
|
|
correct_section = true
|
|
|
|
elseif string.match(line, re_AnySection) then
|
2016-01-12 13:52:44 +00:00
|
|
|
correct_section = false
|
2016-02-25 08:54:41 +00:00
|
|
|
elseif correct_section then
|
|
|
|
value = string.match(line, re_Match)
|
|
|
|
if value then
|
2016-01-12 13:52:44 +00:00
|
|
|
break
|
|
|
|
end
|
2016-01-07 14:30:28 +00:00
|
|
|
end
|
|
|
|
end
|
2016-01-12 13:52:44 +00:00
|
|
|
kobo_conf:close()
|
2016-01-07 14:30:28 +00:00
|
|
|
end
|
2016-01-12 13:52:44 +00:00
|
|
|
|
2016-02-25 08:54:41 +00:00
|
|
|
return value
|
|
|
|
end
|
|
|
|
|
2019-09-11 16:11:24 +00:00
|
|
|
--[[--
|
|
|
|
Get frontlight level.
|
|
|
|
|
|
|
|
@treturn int Frontlight level.
|
|
|
|
--]]
|
2016-02-25 08:54:41 +00:00
|
|
|
function NickelConf.frontLightLevel.get()
|
|
|
|
local new_intensity = NickelConf._read_kobo_conf(re_FrontLightLevel)
|
|
|
|
if new_intensity then
|
2016-03-21 06:58:59 +00:00
|
|
|
-- we need 0 to signal frontlight off for device that does not support
|
|
|
|
-- FrontLightState config, so don't normalize the value here yet.
|
|
|
|
return tonumber(new_intensity)
|
2016-02-25 08:54:41 +00:00
|
|
|
else
|
2017-06-14 17:32:16 +00:00
|
|
|
local fallback_fl_level = 1
|
2016-03-02 06:06:21 +00:00
|
|
|
assert(NickelConf.frontLightLevel.set(fallback_fl_level))
|
|
|
|
return fallback_fl_level
|
2016-01-12 13:52:44 +00:00
|
|
|
end
|
2016-01-07 14:30:28 +00:00
|
|
|
end
|
|
|
|
|
2019-09-11 16:11:24 +00:00
|
|
|
--[[--
|
|
|
|
Get frontlight state.
|
|
|
|
|
|
|
|
This entry will be missing for devices that do not have a hardware toggle button.
|
|
|
|
We return nil in this case.
|
|
|
|
|
|
|
|
@treturn int Frontlight state (or nil).
|
|
|
|
--]]
|
2016-02-25 08:54:41 +00:00
|
|
|
function NickelConf.frontLightState.get()
|
|
|
|
local new_state = NickelConf._read_kobo_conf(re_FrontLightState)
|
2019-09-11 16:11:24 +00:00
|
|
|
|
2016-02-25 08:54:41 +00:00
|
|
|
if new_state then
|
|
|
|
new_state = (new_state == "true") or false
|
|
|
|
end
|
2019-09-11 16:11:24 +00:00
|
|
|
|
2016-02-25 08:54:41 +00:00
|
|
|
return new_state
|
|
|
|
end
|
|
|
|
|
2019-09-11 16:11:24 +00:00
|
|
|
--[[--
|
|
|
|
Get color setting.
|
|
|
|
|
|
|
|
@treturn int Color setting.
|
|
|
|
--]]
|
2018-02-04 11:38:48 +00:00
|
|
|
function NickelConf.colorSetting.get()
|
|
|
|
local new_colorsetting = NickelConf._read_kobo_conf(re_ColorSetting)
|
|
|
|
if new_colorsetting then
|
|
|
|
return tonumber(new_colorsetting)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-11 16:11:24 +00:00
|
|
|
--[[--
|
|
|
|
Get auto color enabled.
|
|
|
|
|
|
|
|
@treturn bool Auto color enabled.
|
|
|
|
--]]
|
2018-03-31 14:00:49 +00:00
|
|
|
function NickelConf.autoColorEnabled.get()
|
|
|
|
local new_autocolor = NickelConf._read_kobo_conf(re_AutoColorEnabled)
|
|
|
|
if new_autocolor then
|
|
|
|
return (new_autocolor == "true")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-11 16:11:24 +00:00
|
|
|
--[[--
|
|
|
|
Write Kobo configuration.
|
|
|
|
|
|
|
|
@string re_Match Lua pattern.
|
|
|
|
@string key Kobo conf key.
|
|
|
|
@param value
|
|
|
|
@bool dontcreate Don't create if key doesn't exist.
|
|
|
|
--]]
|
2016-03-02 07:09:14 +00:00
|
|
|
function NickelConf._write_kobo_conf(re_Match, key, value, dont_create)
|
2016-02-25 08:54:41 +00:00
|
|
|
local kobo_conf = io.open(kobo_conf_path, "r")
|
2016-01-07 14:30:28 +00:00
|
|
|
local lines = {}
|
2016-02-25 08:54:41 +00:00
|
|
|
local found = false
|
|
|
|
local remaining
|
2016-01-07 14:30:28 +00:00
|
|
|
local correct_section = false
|
2016-02-25 08:54:41 +00:00
|
|
|
local new_value_line = key .. "=" .. tostring(value)
|
2016-01-12 13:52:44 +00:00
|
|
|
if kobo_conf then
|
2016-02-25 08:54:41 +00:00
|
|
|
local pos
|
2016-01-12 13:52:44 +00:00
|
|
|
for line in kobo_conf:lines() do
|
|
|
|
if string.match(line, re_AnySection) then
|
|
|
|
if correct_section then
|
|
|
|
-- found a new section after having found the correct one,
|
|
|
|
-- therefore the key was missing: let the code below add it
|
2016-02-25 08:54:41 +00:00
|
|
|
kobo_conf:seek("set", pos)
|
2016-01-12 13:52:44 +00:00
|
|
|
break
|
2016-02-25 08:54:41 +00:00
|
|
|
elseif string.match(line, re_PowerOptionsSection) then
|
2016-01-12 13:52:44 +00:00
|
|
|
correct_section = true
|
|
|
|
end
|
|
|
|
end
|
2016-02-25 08:54:41 +00:00
|
|
|
local old_value = string.match(line, re_Match)
|
|
|
|
if correct_section and old_value then
|
|
|
|
lines[#lines + 1] = new_value_line
|
|
|
|
found = true
|
2016-01-12 13:52:44 +00:00
|
|
|
break
|
|
|
|
else
|
|
|
|
lines[#lines + 1] = line
|
2016-01-07 14:30:28 +00:00
|
|
|
end
|
2016-02-25 08:54:41 +00:00
|
|
|
pos = kobo_conf:seek()
|
2016-01-07 14:30:28 +00:00
|
|
|
end
|
2016-02-25 08:54:41 +00:00
|
|
|
|
|
|
|
remaining = kobo_conf:read("*a")
|
|
|
|
kobo_conf:close()
|
2016-01-12 13:52:44 +00:00
|
|
|
end
|
|
|
|
|
2017-06-29 06:23:32 +00:00
|
|
|
if not found then
|
|
|
|
if dont_create then return true end
|
|
|
|
|
2016-01-12 13:52:44 +00:00
|
|
|
if not correct_section then
|
2016-02-25 08:54:41 +00:00
|
|
|
lines[#lines + 1] = "[PowerOptions]"
|
2016-01-07 14:30:28 +00:00
|
|
|
end
|
2016-02-25 08:54:41 +00:00
|
|
|
lines[#lines + 1] = new_value_line
|
2016-01-07 14:30:28 +00:00
|
|
|
end
|
|
|
|
|
2016-02-15 09:33:48 +00:00
|
|
|
local kobo_conf_w = assert(io.open(kobo_conf_path, "w"))
|
2016-01-07 14:30:28 +00:00
|
|
|
for i, line in ipairs(lines) do
|
2016-01-12 13:52:44 +00:00
|
|
|
kobo_conf_w:write(line, "\n")
|
2016-01-07 14:30:28 +00:00
|
|
|
end
|
2016-02-25 08:54:41 +00:00
|
|
|
if remaining then
|
|
|
|
kobo_conf_w:write(remaining)
|
|
|
|
end
|
2016-01-12 13:52:44 +00:00
|
|
|
kobo_conf_w:close()
|
2016-02-25 08:54:41 +00:00
|
|
|
|
2016-01-12 13:52:44 +00:00
|
|
|
return true
|
2016-01-07 14:30:28 +00:00
|
|
|
end
|
|
|
|
|
2019-09-11 16:11:24 +00:00
|
|
|
--[[--
|
|
|
|
Set frontlight level.
|
|
|
|
|
|
|
|
@int new_intensity
|
|
|
|
--]]
|
2016-02-25 08:54:41 +00:00
|
|
|
function NickelConf.frontLightLevel.set(new_intensity)
|
2019-09-11 16:11:24 +00:00
|
|
|
if type(new_intensity) ~= "number" or (not (new_intensity >= 0) and (not new_intensity <= 100)) then return end
|
2016-02-25 08:54:41 +00:00
|
|
|
return NickelConf._write_kobo_conf(re_FrontLightLevel,
|
|
|
|
front_light_level_str,
|
|
|
|
new_intensity)
|
|
|
|
end
|
2019-09-11 16:11:24 +00:00
|
|
|
dbg:guard(NickelConf.frontLightLevel, "set",
|
|
|
|
function(new_intensity)
|
|
|
|
assert(type(new_intensity) == "number",
|
|
|
|
"Wrong brightness value type (expected number)!")
|
|
|
|
assert(new_intensity >= 0 and new_intensity <= 100,
|
|
|
|
"Wrong brightness value given!")
|
|
|
|
end)
|
|
|
|
|
|
|
|
--[[--
|
|
|
|
Set frontlight state.
|
2016-02-25 08:54:41 +00:00
|
|
|
|
2019-09-11 16:11:24 +00:00
|
|
|
@bool new_state
|
|
|
|
--]]
|
2016-02-25 08:54:41 +00:00
|
|
|
function NickelConf.frontLightState.set(new_state)
|
2019-09-11 16:11:24 +00:00
|
|
|
if new_state == nil or type(new_state) ~= "boolean" then return end
|
2016-02-25 08:54:41 +00:00
|
|
|
return NickelConf._write_kobo_conf(re_FrontLightState,
|
|
|
|
front_light_state_str,
|
2016-03-02 07:09:14 +00:00
|
|
|
new_state,
|
2019-09-11 16:11:24 +00:00
|
|
|
-- Do not create if this entry is missing.
|
2016-03-02 07:09:14 +00:00
|
|
|
true)
|
2016-02-25 08:54:41 +00:00
|
|
|
end
|
2019-09-11 16:11:24 +00:00
|
|
|
dbg:guard(NickelConf.frontLightState, "set",
|
|
|
|
function(new_state)
|
|
|
|
assert(type(new_state) == "boolean",
|
|
|
|
"Wrong front light state value type (expected boolean)!")
|
|
|
|
end)
|
2016-02-25 08:54:41 +00:00
|
|
|
|
2019-09-11 16:11:24 +00:00
|
|
|
--[[--
|
|
|
|
Set color setting.
|
|
|
|
|
|
|
|
@int new_color >= 1500 and <= 6400
|
|
|
|
--]]
|
2018-02-04 11:38:48 +00:00
|
|
|
function NickelConf.colorSetting.set(new_color)
|
|
|
|
return NickelConf._write_kobo_conf(re_ColorSetting,
|
|
|
|
color_setting_str,
|
|
|
|
new_color)
|
|
|
|
end
|
2019-09-11 16:11:24 +00:00
|
|
|
dbg:guard(NickelConf.colorSetting, "set",
|
|
|
|
function(new_color)
|
|
|
|
assert(type(new_color) == "number",
|
|
|
|
"Wrong color value type (expected number)!")
|
|
|
|
assert(new_color >= 1500 and new_color <= 6400,
|
|
|
|
"Wrong colorSetting value given!")
|
|
|
|
end)
|
|
|
|
|
|
|
|
--[[--
|
|
|
|
Set auto color enabled.
|
2018-02-04 11:38:48 +00:00
|
|
|
|
2019-09-11 16:11:24 +00:00
|
|
|
@bool new_autocolor
|
|
|
|
--]]
|
2018-03-31 14:00:49 +00:00
|
|
|
function NickelConf.autoColorEnabled.set(new_autocolor)
|
|
|
|
return NickelConf._write_kobo_conf(re_AutoColorEnabled,
|
|
|
|
auto_color_enabled_str,
|
|
|
|
new_autocolor)
|
|
|
|
end
|
2019-09-11 16:11:24 +00:00
|
|
|
dbg:guard(NickelConf.autoColorEnabled, "set",
|
|
|
|
function(new_autocolor)
|
|
|
|
assert(type(new_autocolor) == "boolean",
|
|
|
|
"Wrong type for autocolor (expected boolean)!")
|
|
|
|
end)
|
2018-03-31 14:00:49 +00:00
|
|
|
|
2016-01-07 14:30:28 +00:00
|
|
|
return NickelConf
|