Big refactoring: Device handles frontlight device, ReaderFrontLight now only GUI

pull/212/head
Paulo Matias 11 years ago
parent 232d8cd74a
commit 3f3fba2fdf

@ -5,6 +5,18 @@ Device = {
touch_dev = nil,
model = nil,
firmware_rev = nil,
frontlight = nil,
}
KindleFrontLight = {
kpw_fl = "/sys/devices/system/fl_tps6116x/fl_tps6116x0/fl_intensity",
intensity = nil,
lipc_handle = nil,
}
KoboFrontLight = {
intensity = nil,
fl = nil,
}
function Device:getModel()
@ -25,7 +37,7 @@ function Device:getModel()
end
if cpu_mod == "MX50" then
-- for KPW
local pw_test_fd = lfs.attributes("/sys/devices/system/fl_tps6116x/fl_tps6116x0/fl_intensity")
local pw_test_fd = lfs.attributes(KindleFrontLight.kpw_fl)
-- for Kobo
local kg_test_fd = lfs.attributes("/bin/kobo_config.sh")
-- for KT
@ -58,9 +70,7 @@ function Device:getModel()
end
function Device:getFirmVer()
if not self.model then
self.model = self:getModel()
end
if not self.model then self:getModel() end
return self.firmware_rev
end
@ -69,40 +79,19 @@ function Device:isKindle4()
end
function Device:isKindle3()
re_val = os.execute("cat /proc/cpuinfo | grep MX35")
if re_val == 0 then
return true
else
return false
end
return (self:getModel() == "Kindle3")
end
function Device:isKindle2()
re_val = os.execute("cat /proc/cpuinfo | grep MX3")
if re_val == 0 then
return true
else
return false
end
return (self:getModel() == "Kindle2")
end
function Device:isKobo()
if not self.model then
self.model = self:getModel()
end
re_val = string.find(self.model,"Kobo_")
if re_val == 1 then
return true
else
return false
end
return string.find(self:getModel(),"Kobo_") == 1
end
function Device:hasNoKeyboard()
if not self.model then
self.model = self:getModel()
end
return self:isTouchDevice() or (self.model == "Kindle4")
return self:isTouchDevice() or (self:getModel() == "Kindle4")
end
function Device:hasKeyboard()
@ -110,17 +99,13 @@ function Device:hasKeyboard()
end
function Device:isTouchDevice()
if not self.model then
self.model = self:getModel()
end
return (self.model == "KindlePaperWhite") or (self.model == "KindleTouch") or self:isKobo() or util.isEmulated()
local model = self:getModel()
return (model == "KindlePaperWhite") or (model == "KindleTouch") or self:isKobo() or util.isEmulated()
end
function Device:hasFrontlight()
if not self.model then
self.model = self:getModel()
end
return (self.model == "KindlePaperWhite") or (self.model == "Kobo_dragon") or (self.model == "Kobo_kraken") or (self.model == "Kobo_phoenix") or util.isEmulated()
local model = self:getModel()
return (model == "KindlePaperWhite") or (model == "Kobo_dragon") or (model == "Kobo_kraken") or (model == "Kobo_phoenix") or util.isEmulated()
end
function Device:setTouchInputDev(dev)
@ -185,3 +170,71 @@ function Device:usbPlugOut()
--@TODO signal filemanager for file changes 13.06 2012 (houqp)
self.charging_mode = false
end
function Device:getFrontlight()
if self.frontlight ~= nil then
return self.frontlight
elseif self:hasFrontlight() then
if self:getModel() == "KindlePaperWhite" then
self.frontlight = KindleFrontLight
elseif self:isKobo() then
self.frontlight = KoboFrontLight
end
if self.frontlight ~= nil then
self.frontlight:init()
end
end
return self.frontlight
end
function KindleFrontLight:init()
require "liblipclua"
self.lipc_handle = lipc.init("com.github.koreader")
if self.lipc_handle then
self.intensity = self.lipc_handle:get_int_property("com.lab126.powerd", "flIntensity")
end
end
function KindleFrontLight:toggle()
local f = io.open(self.kpw_fl, "r")
local sysint = tonumber(f:read("*all"):match("%d+"))
f:close()
if sysint == 0 then
self:setIntensity(self.intensity)
else
os.execute("echo -n 0 > " .. self.kpw_fl)
end
end
function KindleFrontLight:setIntensity(intensity)
if self.lipc_handle ~= nil then
intensity = intensity < 0 and 0 or intensity
intensity = intensity > 24 and 24 or intensity
self.intensity = intensity
self.lipc_handle:set_int_property("com.lab126.powerd", "flIntensity", intensity)
end
end
function KoboFrontLight:init()
self.fl = kobolight.open()
self.intensity = G_reader_settings:readSetting("frontlight_intensity")
if not self.intensity then
self.intensity = 20
end
self:setIntensity(self.intensity)
end
function KoboFrontLight:toggle()
if self.fl ~= nil then
self.fl:toggle()
end
end
function KoboFrontLight:setIntensity(intensity)
if self.fl ~= nil then
intensity = intensity < 1 and 1 or intensity
intensity = intensity > 100 and 100 or intensity
self.fl:setBrightness(intensity)
self.intensity = intensity
end
end

@ -424,6 +424,11 @@ function Input:handleKeyBoardEv(ev)
return keycode
end
if ev.value == EVENT_VALUE_KEY_RELEASE
and keycode == "Light" then
return keycode
end
-- handle modifier keys
if self.modifiers[keycode] ~= nil then
if ev.value == EVENT_VALUE_KEY_PRESS then

@ -3,22 +3,11 @@ require "ui/widget/inputdialog"
require "ui/device"
ReaderFrontLight = InputContainer:new{
fldial_menu_title = _("Frontlight settings"),
fl_dialog_title = _("Frontlight Level"),
steps = {0,1,2,3,4,5,6,7,8,9,10},
intensity = nil,
fl = nil,
}
function ReaderFrontLight:init()
if not Device:hasFrontlight() then return end
local dev_mod = Device:getModel()
if dev_mod == "KindlePaperWhite" then
require "liblipclua"
self.lipc_handle = lipc.init("com.github.koreader")
if self.lipc_handle then
self.intensity = self.lipc_handle:get_int_property("com.lab126.powerd", "flIntensity")
end
if Device:getFrontlight() ~= nil then
self.ges_events = {
Adjust = {
GestureRange:new{
@ -32,68 +21,37 @@ function ReaderFrontLight:init()
}
},
}
elseif Device:isKobo() then
self.fl = kobolight.open()
self.intensity = G_reader_settings:readSetting("frontlight_intensity")
if not self.intensity then
self.intensity = 20
end
self:setIntensity(self.intensity, _("Set intensity "))
self.key_events.Toggle = { {"Light"}, doc = _("Toggle light") }
self.ui.menu:registerToMainMenu(self)
end
self.ui.menu:registerToMainMenu(self)
end
function ReaderFrontLight:onAdjust(arg, ges)
if self.lipc_handle and self.intensity ~=nil then
local fl = Device.frontlight
if fl.intensity ~= nil then
local rel_proportion = ges.distance / Screen:getWidth()
local delta_int = self.steps[math.ceil(#self.steps*rel_proportion)] or self.steps[#self.steps]
local msg = ""
local msg = nil
if ges.direction == "north" then
msg = _("Increase front light intensity to ")
self.intensity = self.intensity + delta_int
self:setIntensity(self.intensity, msg)
fl:setIntensity(fl.intensity + delta_int)
elseif ges.direction == "south" then
msg = _("Decrease front light intensity to ")
self.intensity = self.intensity - delta_int
self:setIntensity(self.intensity, msg)
fl:setIntensity(fl.intensity - delta_int)
end
end
return true
end
function ReaderFrontLight:setIntensity(intensity, msg)
if self.lipc_handle then
intensity = intensity < 0 and 0 or intensity
intensity = intensity > 24 and 24 or intensity
self.intensity = intensity
self.lipc_handle:set_int_property("com.lab126.powerd", "flIntensity", intensity)
UIManager:show(Notification:new{
text = msg..intensity,
timeout = 1
})
elseif Device:isKobo() then
intensity = intensity < 1 and 1 or intensity
intensity = intensity > 100 and 100 or intensity
if self.fl ~= nil then
self.fl:setBrightness(intensity)
self.intensity = intensity
if msg ~= nil then
UIManager:show(Notification:new{
text = msg..fl.intensity,
timeout = 1
})
end
end
return true
end
function ReaderFrontLight:onToggle()
if Device:isKobo() and self.fl ~= nil then
self.fl:toggle()
end
return true
end
function ReaderFrontLight:addToMainMenu(tab_item_table)
-- insert fldial command to main reader menu
table.insert(tab_item_table.main, {
text = self.fldial_menu_title,
text = _("Frontlight settings"),
callback = function()
self:onShowFlDialog()
end,
@ -103,10 +61,17 @@ end
function ReaderFrontLight:onShowFlDialog()
DEBUG("show fldial dialog")
self.fl_dialog = InputDialog:new{
title = self.fl_dialog_title,
title = _("Frontlight Level"),
input_hint = Device:isKobo() and "(1 - 100)" or "(0 - 24)",
buttons = {
{
{
text = _("Toggle"),
enabled = true,
callback = function()
Device.frontlight:toggle()
end,
},
{
text = _("Apply"),
enabled = true,
@ -135,14 +100,13 @@ end
function ReaderFrontLight:close()
self.fl_dialog:onClose()
G_reader_settings:saveSetting("frontlight_intensity", self.intensity)
G_reader_settings:saveSetting("frontlight_intensity", Device.frontlight.intensity)
UIManager:close(self.fl_dialog)
end
function ReaderFrontLight:fldialIntensity()
local number = tonumber(self.fl_dialog:getInputText())
if number then
self:setIntensity(number, _("Set intensity "))
if number ~= nil then
Device.frontlight:setIntensity(number)
end
return true
end

@ -1,6 +1,7 @@
require "ui/geometry"
require "ui/device"
require "ui/inputevent"
require "ui/widget/container"
require "ui/screen"
require "debug"
require "gettext"
@ -290,6 +291,8 @@ function UIManager:run()
Device:usbPlugIn()
elseif input_event == "NotCharging" then
Device:usbPlugOut()
elseif input_event == "Light" then
Device:getFrontlight():toggle()
else
self:sendEvent(input_event)
end

Loading…
Cancel
Save