emulate Power button with F2 on emulator

pull/852/head
chrox 10 years ago
parent 451092905e
commit 40bddf0735

@ -2,14 +2,14 @@ local CenterContainer = require("ui/widget/container/centercontainer")
local InputContainer = require("ui/widget/container/inputcontainer") local InputContainer = require("ui/widget/container/inputcontainer")
local InputDialog = require("ui/widget/inputdialog") local InputDialog = require("ui/widget/inputdialog")
local InfoMessage = require("ui/widget/infomessage") local InfoMessage = require("ui/widget/infomessage")
local Screensaver = require("ui/screensaver")
local lfs = require("libs/libkoreader-lfs") local lfs = require("libs/libkoreader-lfs")
local UIManager = require("ui/uimanager") local UIManager = require("ui/uimanager")
local Menu = require("ui/widget/menu") local Menu = require("ui/widget/menu")
local Screen = require("ui/screen") local Screen = require("ui/screen")
local _ = require("gettext")
local Font = require("ui/font")
local UIToolbox = require("ui/uitoolbox")
local util = require("ffi/util") local util = require("ffi/util")
local Font = require("ui/font")
local _ = require("gettext")
local calibre = "metadata.calibre" local calibre = "metadata.calibre"
local koreaderfile = "temp/metadata.koreader" local koreaderfile = "temp/metadata.koreader"
@ -472,7 +472,7 @@ function Search:onMenuHold(item)
item.notchecked = false item.notchecked = false
end end
local thumbwidth = math.min(240, Screen:getWidth()/3) local thumbwidth = math.min(240, Screen:getWidth()/3)
UIManager:show(InfoMessage:new{text = item.info,image = UIToolbox:getPicture(item.path), image_width = thumbwidth,image_height = thumbwidth/2*3}) UIManager:show(InfoMessage:new{text = item.info,image = Screensaver:getCoverPicture(item.path), image_width = thumbwidth,image_height = thumbwidth/2*3})
end end
end end

@ -6,6 +6,7 @@ local BasePowerD = require("ui/device/basepowerd")
local lfs = require("libs/libkoreader-lfs") local lfs = require("libs/libkoreader-lfs")
local Screen = require("ui/device/screen") local Screen = require("ui/device/screen")
local util = require("ffi/util") local util = require("ffi/util")
local DEBUG = require("dbg")
local ffi = require("ffi") local ffi = require("ffi")
local Device = { local Device = {
@ -201,26 +202,54 @@ function Device:outofScreenSaver()
self.screen_saver_mode = false self.screen_saver_mode = false
end end
function Device:prepareSuspend() -- currently only used for kobo devices function Device:onPowerEvent(ev)
local Screensaver = require("ui/screensaver")
local UIManager = require("ui/uimanager")
if (ev == "Power" or ev == "Suspend") and not self.screen_saver_mode then
DEBUG("Suspending...")
Screensaver:show()
self:prepareSuspend()
UIManager:scheduleIn(2, function() self:Suspend() end)
elseif (ev == "Power" or ev == "Resume") and self.screen_saver_mode then
DEBUG("Resuming...")
self:Resume()
Screensaver:close()
end
end
function Device:prepareSuspend()
local powerd = self:getPowerDevice() local powerd = self:getPowerDevice()
if powerd ~= nil then if powerd.fl ~= nil then
-- in no case should the frontlight be turned on in suspend mode
powerd.fl:sleep() powerd.fl:sleep()
end end
self.screen:refresh(0) self.screen:refresh(0)
self.screen_saver_mode = true self.screen_saver_mode = true
end end
function Device:Suspend() -- currently only used for kobo devices function Device:Suspend()
os.execute("./suspend.sh") if self:isKobo() then
os.execute("./suspend.sh")
end
end end
function Device:Resume() -- currently only used for kobo devices function Device:Resume()
os.execute("echo 0 > /sys/power/state-extended") if self:isKobo() then
os.execute("echo 0 > /sys/power/state-extended")
end
self.screen:refresh(1) self.screen:refresh(1)
local powerd = self:getPowerDevice() local powerd = self:getPowerDevice()
if powerd ~= nil then if powerd.fl ~= nil then
powerd.fl:restore() powerd.fl:restore()
end end
-- FIXME: this conflicts with powerd.fl:restore
--[[
if KOBO_LIGHT_ON_START and tonumber(KOBO_LIGHT_ON_START) > -1 then
if powerd then
powerd:setIntensity(math.max(math.min(KOBO_LIGHT_ON_START,100),0))
end
end
--]]
self.screen_saver_mode = false self.screen_saver_mode = false
end end

@ -205,6 +205,7 @@ function Input:initKeyMap()
[64] = "Alt", -- left alt [64] = "Alt", -- left alt
[65] = " ", -- Spacebar [65] = " ", -- Spacebar
[67] = "Menu", -- F[1] [67] = "Menu", -- F[1]
[68] = "Power", -- F[2]
[72] = "LPgBack", -- F[6] [72] = "LPgBack", -- F[6]
[73] = "LPgFwd", -- F[7] [73] = "LPgFwd", -- F[7]
[95] = "VPlus", -- F[11] [95] = "VPlus", -- F[11]
@ -237,6 +238,7 @@ function Input:initKeyMap()
[226] = "Alt", -- left alt [226] = "Alt", -- left alt
[44] = " ", -- Spacebar [44] = " ", -- Spacebar
[58] = "Menu", -- F[1] [58] = "Menu", -- F[1]
[59] = "Power", -- F[2]
[63] = "LPgBack", -- F[6] [63] = "LPgBack", -- F[6]
[64] = "LPgFwd", -- F[7] [64] = "LPgFwd", -- F[7]
[68] = "VPlus", -- F[11] [68] = "VPlus", -- F[11]

@ -1,10 +1,12 @@
local InputContainer = require("ui/widget/container/inputcontainer") local UIManager = require("ui/uimanager")
local Screen = require("ui/screen")
local DEBUG = require("dbg")
local _ = require("gettext")
local UIToolbox = InputContainer:new{ local Screensaver = {
} }
function UIToolbox:getPicture(file) function Screensaver:getCoverPicture(file)
local contentopf local contentopf
local contentpath local contentpath
local epub_folder = "temp/epub" local epub_folder = "temp/epub"
@ -69,13 +71,13 @@ function UIToolbox:getPicture(file)
end end
return check_extension(cover) return check_extension(cover)
end end
local function checkoldfile(cover) local function checkoldfile(cover)
if io.open(cover) then if io.open(cover) then
return cover return cover
end end
end end
local cover local cover
local oldfile = "temp/" .. file:gsub("/","#") .. "." local oldfile = "temp/" .. file:gsub("/","#") .. "."
@ -104,13 +106,13 @@ function UIToolbox:getPicture(file)
contentpath = "" contentpath = ""
end end
pcall(os.execute("unzip \"" .. file .. "\" \"" .. contentopf .. "\" -oq -d " .. epub_folder)) -- read content.opf pcall(os.execute("unzip \"" .. file .. "\" \"" .. contentopf .. "\" -oq -d " .. epub_folder)) -- read content.opf
cover = try_content_opf("^%s*<meta name=\"cover\"","content=",true) -- Make Room cover = try_content_opf("^%s*<meta name=\"cover\"","content=",true) -- Make Room
if not cover then cover = try_content_opf('id="cover',"item href=",false) end -- Kishon if not cover then cover = try_content_opf('id="cover',"item href=",false) end -- Kishon
if not cover then cover = try_content_opf("cover","href=",true) end if not cover then cover = try_content_opf("cover","href=",true) end
if not cover then cover = try_content_opf("cover","=",true) end if not cover then cover = try_content_opf("cover","=",true) end
if not cover then cover = try_content_opf("cover","=",false) end if not cover then cover = try_content_opf("cover","=",false) end
if not cover then guess("jpg") end if not cover then guess("jpg") end
if not cover then guess("jpeg") end if not cover then guess("jpeg") end
if not cover then guess("png") end if not cover then guess("png") end
@ -129,10 +131,10 @@ function UIToolbox:getPicture(file)
return cover return cover
end end
function UIToolbox:getRandomPicture(dir) function Screensaver:getRandomPicture(dir)
local pics = {} local pics = {}
local i = 0 local i = 0
math.randomseed( os.time() ) math.randomseed(os.time())
for entry in lfs.dir(dir) do for entry in lfs.dir(dir) do
if lfs.attributes(dir .. entry, "mode") == "file" then if lfs.attributes(dir .. entry, "mode") == "file" then
local extension = string.lower(string.match(entry, ".+%.([^.]+)") or "") local extension = string.lower(string.match(entry, ".+%.([^.]+)") or "")
@ -145,4 +147,45 @@ function UIToolbox:getRandomPicture(dir)
return pics[math.random(i)] return pics[math.random(i)]
end end
return UIToolbox function Screensaver:show()
DEBUG("show screensaver")
local InfoMessage = require("ui/widget/infomessage")
local ImageWidget = require("ui/widget/imagewidget")
local file = nil
-- first check book cover image
if KOBO_SCREEN_SAVER_LAST_BOOK then
file = self:getCoverPicture(G_reader_settings:readSetting("lastfile"))
-- then screensaver image
elseif type(KOBO_SCREEN_SAVER) == "string" then
file = KOBO_SCREEN_SAVER
if lfs.attributes(file, "mode") == "directory" then
if string.sub(file,string.len(file)) ~= "/" then
file = file .. "/"
end
local dummy = self:getRandomPicture(file)
if dummy then file = file .. dummy end
end
end
if file and lfs.attributes(file, "mode") == "file" then
self.suspend_msg = ImageWidget:new{
file = file,
width = Screen:getWidth(),
height = Screen:getHeight(),
}
end
-- fallback to suspended message
if not self.suspend_msg then
self.suspend_msg = InfoMessage:new{ text = _("Suspended") }
end
UIManager:show(self.suspend_msg)
end
function Screensaver:close()
DEBUG("close screensaver")
if self.suspend_msg then
UIManager:close(self.suspend_msg)
self.suspend_msg = nil
end
end
return Screensaver

@ -2,11 +2,9 @@ local Device = require("ui/device")
local Screen = require("ui/screen") local Screen = require("ui/screen")
local Input = require("ui/input") local Input = require("ui/input")
local Event = require("ui/event") local Event = require("ui/event")
local util = require("ffi/util")
local DEBUG = require("dbg") local DEBUG = require("dbg")
local _ = require("gettext") local _ = require("gettext")
local util = require("ffi/util")
local ImageWidget = require("ui/widget/imagewidget")
local UIToolbox = require("ui/uitoolbox")
-- initialize output module, this must be initialized before Input -- initialize output module, this must be initialized before Input
Screen:init() Screen:init()
@ -57,7 +55,6 @@ local UIManager = {
_execution_stack = {}, _execution_stack = {},
_dirty = {}, _dirty = {},
_zeromqs = {}, _zeromqs = {},
suspend_msg = nil
} }
function UIManager:init() function UIManager:init()
@ -67,68 +64,20 @@ function UIManager:init()
end, end,
SaveState = function() SaveState = function()
self:sendEvent(Event:new("FlushSettings")) self:sendEvent(Event:new("FlushSettings"))
end end,
Power = function(input_event)
Device:onPowerEvent(input_event)
end,
} }
if Device:isKobo() then if Device:isKobo() then
-- lazy create suspend_msg to avoid dependence loop self.event_handlers["Suspend"] = function(input_event)
self:sendEvent(Event:new("FlushSettings"))
function kobo_power(input_event) Device:onPowerEvent(input_event)
end
if (input_event == "Power" or input_event == "Suspend") self.event_handlers["Resume"] = function(input_event)
and not Device.screen_saver_mode then Device:onPowerEvent(input_event)
if not UIManager.suspend_msg then self:sendEvent(Event:new("Resume"))
local file
if KOBO_SCREEN_SAVER_LAST_BOOK then
file = UIToolbox:getPicture(G_reader_settings:readSetting("lastfile"))
end
if type(KOBO_SCREEN_SAVER) == "string" or file then
if not file then
file = KOBO_SCREEN_SAVER
if lfs.attributes(file, "mode") == "directory" then
if string.sub(file,string.len(file)) ~= "/" then
file = file .. "/"
end
local dummy = UIToolbox:getRandomPicture(file)
if dummy then file = file .. dummy end
end
end
if lfs.attributes(file, "mode") == "file" then
UIManager.suspend_msg = ImageWidget:new{
file = file,
width = Screen:getWidth(),
height = Screen:getHeight(),
}
end
end
if not UIManager.suspend_msg then
local InfoMessage = require("ui/widget/infomessage")
UIManager.suspend_msg = InfoMessage:new{ text = _("Suspended") }
end
end
if KOBO_LIGHT_OFF_ON_SUSPEND then Device:getPowerDevice():setIntensity(0) end
self:show(UIManager.suspend_msg)
self:sendEvent(Event:new("FlushSettings"))
Device:prepareSuspend()
self:scheduleIn(2, function() Device:Suspend() end)
elseif (input_event == "Power" or input_event == "Resume")
and Device.screen_saver_mode then
Device:Resume()
self:sendEvent(Event:new("Resume"))
if UIManager.suspend_msg then
self:close(UIManager.suspend_msg)
UIManager.suspend_msg = nil
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) )
end
end
end end
self.event_handlers["Power"] = kobo_power
self.event_handlers["Suspend"] = kobo_power
self.event_handlers["Resume"] = kobo_power
self.event_handlers["Light"] = function() self.event_handlers["Light"] = function()
Device:getPowerDevice():toggleFrontlight() Device:getPowerDevice():toggleFrontlight()
end end
@ -152,6 +101,7 @@ function UIManager:init()
end end
self.event_handlers["OutOfSS"] = function() self.event_handlers["OutOfSS"] = function()
Device:outofScreenSaver() Device:outofScreenSaver()
self:sendEvent(Event:new("Resume"))
end end
self.event_handlers["Charging"] = function() self.event_handlers["Charging"] = function()
Device:usbPlugIn() Device:usbPlugIn()

Loading…
Cancel
Save