From eb47c5a39fbabd07a45d79629ecf88c6a9fad557 Mon Sep 17 00:00:00 2001 From: Dimitrios Semitsoglou-Tsiapos Date: Thu, 7 Jan 2016 16:30:28 +0200 Subject: [PATCH] Synchronise frontlight level with nickel config * Drops support for mocking the frontlight setting internally which may cause incorrect in-memory values. * Adds new supported value for `KOBO_LIGHT_ON_START` (-2), which sets 'Kobo eReader.conf' as the source to update `settings.reader.lua`'s brightness setting on startup, thus using the value from it indirectly. * Adds the `KOBO_SYNC_BRIGHTNESS_WITH_NICKEL` configuration variable which updates 'Kobo eReader.conf' every time the brightness setting is changed within koreader. * Fixes missing call to save brightness when modifying via two-finger swipe. Closes #1523. --- defaults.lua | 6 +- .../apps/reader/modules/readerfrontlight.lua | 9 ++- frontend/device/generic/powerd.lua | 9 --- frontend/device/kobo/nickel_conf.lua | 70 +++++++++++++++++++ frontend/device/kobo/powerd.lua | 11 ++- frontend/ui/uimanager.lua | 15 +++- reader.lua | 3 +- 7 files changed, 99 insertions(+), 24 deletions(-) create mode 100644 frontend/device/kobo/nickel_conf.lua diff --git a/defaults.lua b/defaults.lua index f06ca850d..c420f5832 100644 --- a/defaults.lua +++ b/defaults.lua @@ -173,7 +173,11 @@ SEARCH_SERIES = true SEARCH_PATH = true -- Light parameter for Kobo -KOBO_LIGHT_ON_START = -1 -- -1 or 0-100. -1 leaves light as it is, other sets light on start/wake up +KOBO_LIGHT_ON_START = -2 -- -1, -2 or 0-100. -1 leaves light as it + -- is, -2 uses 'Kobo eReader.conf', other + -- sets light on start/wake up +KOBO_SYNC_BRIGHTNESS_WITH_NICKEL = true -- Save brightness set in KOreader + -- with nickel's 'Kobo eReader.conf' KOBO_SCREEN_SAVER = "" -- image or directory with pictures or "-" KOBO_SCREEN_SAVER_LAST_BOOK = true -- get screensaver from last book if possible diff --git a/frontend/apps/reader/modules/readerfrontlight.lua b/frontend/apps/reader/modules/readerfrontlight.lua index 558493563..df28e03f9 100644 --- a/frontend/apps/reader/modules/readerfrontlight.lua +++ b/frontend/apps/reader/modules/readerfrontlight.lua @@ -46,10 +46,14 @@ function ReaderFrontLight:onAdjust(arg, ges) DEBUG("step = ", step) local delta_int = self.steps[step] or self.steps[#self.steps] DEBUG("delta_int = ", delta_int) + local new_intensity if ges.direction == "north" then - powerd:setIntensity(powerd.flIntensity + delta_int) + new_intensity = powerd.flIntensity + delta_int elseif ges.direction == "south" then - powerd:setIntensity(powerd.flIntensity - delta_int) + new_intensity = powerd.flIntensity - delta_int + end + if new_intensity ~= nil then + powerd:setIntensity(new_intensity) end end return true @@ -121,7 +125,6 @@ end function ReaderFrontLight:close() self.fl_dialog:onClose() - G_reader_settings:saveSetting("frontlight_intensity", Device:getPowerDevice().flIntensity) UIManager:close(self.fl_dialog) end diff --git a/frontend/device/generic/powerd.lua b/frontend/device/generic/powerd.lua index 036f2b300..e8902061b 100644 --- a/frontend/device/generic/powerd.lua +++ b/frontend/device/generic/powerd.lua @@ -20,7 +20,6 @@ end function BasePowerD:init() end function BasePowerD:toggleFrontlight() end function BasePowerD:setIntensityHW() end -function BasePowerD:setIntensitySW() end function BasePowerD:getCapacityHW() return "0" end function BasePowerD:isChargingHW() end function BasePowerD:suspendHW() end @@ -55,14 +54,6 @@ function BasePowerD:setIntensity(intensity) self:setIntensityHW() end -function BasePowerD:setIntensityWithoutHW(intensity) - intensity = intensity < self.fl_min and self.fl_min or intensity - intensity = intensity > self.fl_max and self.fl_max or intensity - self.flIntensity = intensity - self:setIntensitySW() -end - - function BasePowerD:getCapacity() if self.capacity_pulled_count == self.capacity_cached_count then self.capacity_pulled_count = 0 diff --git a/frontend/device/kobo/nickel_conf.lua b/frontend/device/kobo/nickel_conf.lua new file mode 100644 index 000000000..f4bf745ad --- /dev/null +++ b/frontend/device/kobo/nickel_conf.lua @@ -0,0 +1,70 @@ +--[[ + Access and modify values in 'Kobo eReader.conf' used by Nickel. + Only PowerOptions:FrontLightLevel is currently supported . +]] + +local NickelConf = {} +NickelConf.frontLightLevel = {} + +local kobo_conf_path = '/mnt/onboard/.kobo/Kobo/Kobo eReader.conf' +local re_BrightnessValue = "[0-9]+" +local re_FrontLightLevel = "^FrontLightLevel%s*=%s*(" .. re_BrightnessValue .. ")%s*$" +local re_PowerOptionsSection = "^%[PowerOptions%]%s*" +local re_AnySection = "^%[.*%]%s*" + +function NickelConf.frontLightLevel.get() + local correct_section = false + local kobo_conf = assert(io.open(kobo_conf_path, "r")) + for line in kobo_conf:lines() do + if string.match(line, re_AnySection) then + correct_section = false + if string.match(line, re_PowerOptionsSection) then + correct_section = true + end + end + if correct_section then + new_intensity = string.match(line, re_FrontLightLevel) + if new_intensity then + new_intensity = tonumber(new_intensity) + break + end + end + end + kobo_conf:close() + return new_intensity +end + +function NickelConf.frontLightLevel.set(new_intensity) + assert(new_intensity >= 0 and new_intensity <= 100, + "Wrong brightness value given!") + + local lines = {} + local correct_section = false + local kobo_conf = assert(io.open(kobo_conf_path, "r")) + for line in kobo_conf:lines() do + if string.match(line, re_AnySection) then + correct_section = false + if string.match(line, re_PowerOptionsSection) then + correct_section = true + end + end + old_intensity = string.match(line, re_FrontLightLevel) + if correct_section and old_intensity then + lines[#lines + 1] = string.gsub(line, re_BrightnessValue, new_intensity, 1) + remaining_file = kobo_conf:read("*a") + break + else + lines[#lines + 1] = line + end + end + kobo_conf:close() + + kobo_conf = assert(io.open(kobo_conf_path, "w")) + for i, line in ipairs(lines) do + kobo_conf:write(line, "\n") + end + kobo_conf:write(remaining_file) + kobo_conf:close() +end + +return NickelConf diff --git a/frontend/device/kobo/powerd.lua b/frontend/device/kobo/powerd.lua index 93d7cbebd..8128ca0df 100644 --- a/frontend/device/kobo/powerd.lua +++ b/frontend/device/kobo/powerd.lua @@ -1,4 +1,5 @@ local BasePowerD = require("device/generic/powerd") +local NickelConf = require("device/kobo/nickel_conf") local KoboPowerD = BasePowerD:new{ fl_min = 0, fl_max = 100, @@ -29,16 +30,12 @@ end function KoboPowerD:setIntensityHW() if self.fl ~= nil then self.fl:setBrightness(self.flIntensity) + if KOBO_SYNC_BRIGHTNESS_WITH_NICKEL then + NickelConf.frontLightLevel.set(self.flIntensity) + end end end -function KoboPowerD:setIntensitySW() - if self.fl ~= nil then - self.fl:restoreBrightness(self.flIntensity) - end -end - - function KoboPowerD:getCapacityHW() self.battCapacity = self:read_int_file(self.batt_capacity_file) return self.battCapacity diff --git a/frontend/ui/uimanager.lua b/frontend/ui/uimanager.lua index e98aa184f..0b767069f 100644 --- a/frontend/ui/uimanager.lua +++ b/frontend/ui/uimanager.lua @@ -6,6 +6,7 @@ local Geom = require("ui/geometry") local util = require("ffi/util") local DEBUG = require("dbg") local _ = require("gettext") +local NickelConf = require("device/kobo/nickel_conf") local MILLION = 1000000 @@ -60,8 +61,18 @@ function UIManager:init() self:sendEvent(input_event) end end - if KOBO_LIGHT_ON_START and tonumber(KOBO_LIGHT_ON_START) > -1 then - Device:getPowerDevice():setIntensity( math.max( math.min(KOBO_LIGHT_ON_START,100) ,0) ) + local kobo_light_on_start = tonumber(KOBO_LIGHT_ON_START) + if kobo_light_on_start then + if kobo_light_on_start >= 0 then + new_intensity = math.min(kobo_light_on_start, 100) + elseif kobo_light_on_start == -2 then + new_intensity = NickelConf.frontLightLevel:get() + end + if new_intensity then + -- Since this kobo-specific, we save here and let the code pick + -- it up later from the reader settings. + G_reader_settings:saveSetting("frontlight_intensity", new_intensity) + end end elseif Device:isKindle() then self.event_handlers["IntoSS"] = function() diff --git a/reader.lua b/reader.lua index e42cfa6cc..9dc62a1e2 100755 --- a/reader.lua +++ b/reader.lua @@ -118,8 +118,7 @@ if Device:isKobo() then if powerd and powerd.restore_settings then local intensity = G_reader_settings:readSetting("frontlight_intensity") intensity = intensity or powerd.flIntensity - powerd:setIntensityWithoutHW(intensity) - -- powerd:setIntensity(intensity) + powerd:setIntensity(intensity) end if Device:getCodeName() == "trilogy" then require("utils/kobo_touch_probe")