2015-01-17 16:58:08 +00:00
|
|
|
local Event = require("ui/event")
|
2014-10-30 18:42:18 +00:00
|
|
|
local util = require("ffi/util")
|
|
|
|
local DEBUG = require("dbg")
|
|
|
|
|
2015-09-27 01:25:47 +00:00
|
|
|
local function yes() return true end
|
2014-10-30 18:42:18 +00:00
|
|
|
local function no() return false end
|
|
|
|
|
|
|
|
local Device = {
|
|
|
|
screen_saver_mode = false,
|
|
|
|
charging_mode = false,
|
|
|
|
survive_screen_saver = false,
|
2016-09-14 05:16:31 +00:00
|
|
|
is_cover_closed = false,
|
2014-10-30 18:42:18 +00:00
|
|
|
model = nil,
|
|
|
|
powerd = nil,
|
|
|
|
screen = nil,
|
|
|
|
input = nil,
|
2016-09-19 08:16:41 +00:00
|
|
|
-- For Kobo, wait at least 15 seconds before calling suspend script. Otherwise, suspend might
|
|
|
|
-- fail and the battery will be drained while we are in screensaver mode
|
|
|
|
suspend_wait_timeout = 15,
|
2014-10-30 18:42:18 +00:00
|
|
|
|
|
|
|
-- hardware feature tests: (these are functions!)
|
|
|
|
hasKeyboard = no,
|
|
|
|
hasKeys = no,
|
2015-09-27 01:05:37 +00:00
|
|
|
hasDPad = no,
|
2014-10-30 18:42:18 +00:00
|
|
|
isTouchDevice = no,
|
|
|
|
hasFrontlight = no,
|
2016-04-03 04:52:30 +00:00
|
|
|
needsTouchScreenProbe = no,
|
2014-10-30 18:42:18 +00:00
|
|
|
|
|
|
|
-- use these only as a last resort. We should abstract the functionality
|
|
|
|
-- and have device dependent implementations in the corresponting
|
|
|
|
-- device/<devicetype>/device.lua file
|
|
|
|
-- (these are functions!)
|
|
|
|
isKindle = no,
|
|
|
|
isKobo = no,
|
2015-01-24 16:51:57 +00:00
|
|
|
isPocketBook = no,
|
2014-10-30 18:42:18 +00:00
|
|
|
isAndroid = no,
|
2015-10-03 06:18:47 +00:00
|
|
|
isSDL = no,
|
2014-10-30 18:42:18 +00:00
|
|
|
|
|
|
|
-- some devices have part of their screen covered by the bezel
|
|
|
|
viewport = nil,
|
2016-02-26 09:46:23 +00:00
|
|
|
-- enforce portrait orientation on display, no matter how configured at
|
|
|
|
-- startup
|
2014-11-27 15:33:48 +00:00
|
|
|
isAlwaysPortrait = no,
|
2015-09-27 01:25:47 +00:00
|
|
|
-- needs full screen refresh when resumed from screensaver?
|
|
|
|
needsScreenRefreshAfterResume = yes,
|
2014-10-30 18:42:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function Device:new(o)
|
2016-02-16 07:10:07 +00:00
|
|
|
o = o or {}
|
2014-10-30 18:42:18 +00:00
|
|
|
setmetatable(o, self)
|
|
|
|
self.__index = self
|
|
|
|
return o
|
|
|
|
end
|
|
|
|
|
|
|
|
function Device:init()
|
|
|
|
if not self.screen then
|
2014-11-20 19:24:02 +00:00
|
|
|
error("screen/framebuffer must be implemented")
|
2014-10-30 18:42:18 +00:00
|
|
|
end
|
2014-11-22 21:55:53 +00:00
|
|
|
|
2016-02-29 07:08:57 +00:00
|
|
|
local is_eink = G_reader_settings:readSetting("eink")
|
2016-03-09 03:38:06 +00:00
|
|
|
self.screen.eink = (is_eink == nil) or is_eink
|
2016-02-29 07:08:57 +00:00
|
|
|
|
2014-11-27 10:34:16 +00:00
|
|
|
DEBUG("initializing for device", self.model)
|
|
|
|
DEBUG("framebuffer resolution:", self.screen:getSize())
|
|
|
|
|
2014-10-30 18:42:18 +00:00
|
|
|
if not self.input then
|
2014-11-03 09:08:55 +00:00
|
|
|
self.input = require("device/input"):new{device = self}
|
2014-10-30 18:42:18 +00:00
|
|
|
end
|
|
|
|
if not self.powerd then
|
|
|
|
self.powerd = require("device/generic/powerd"):new{device = self}
|
|
|
|
end
|
|
|
|
|
|
|
|
if self.viewport then
|
2014-11-27 10:34:16 +00:00
|
|
|
DEBUG("setting a viewport:", self.viewport)
|
2014-10-30 18:42:18 +00:00
|
|
|
self.screen:setViewport(self.viewport)
|
|
|
|
self.input:registerEventAdjustHook(
|
|
|
|
self.input.adjustTouchTranslate,
|
|
|
|
{x = 0 - self.viewport.x, y = 0 - self.viewport.y})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function Device:getPowerDevice()
|
|
|
|
return self.powerd
|
|
|
|
end
|
|
|
|
|
2015-02-01 04:49:46 +00:00
|
|
|
-- ONLY used for Kindle devices
|
2014-10-30 18:42:18 +00:00
|
|
|
function Device:intoScreenSaver()
|
2015-01-17 16:58:08 +00:00
|
|
|
local UIManager = require("ui/uimanager")
|
2016-02-29 17:22:52 +00:00
|
|
|
if self.charging_mode == false and self.screen_saver_mode == false then
|
2014-10-30 18:42:18 +00:00
|
|
|
self.screen:saveCurrentBB()
|
|
|
|
self.screen_saver_mode = true
|
2016-02-29 17:22:52 +00:00
|
|
|
-- On FW >= 5.7.2, we sigstop awesome, but we need it to show stuff...
|
|
|
|
if os.getenv("AWESOME_STOPPED") == "yes" then
|
|
|
|
os.execute("killall -cont awesome")
|
|
|
|
end
|
2014-10-30 18:42:18 +00:00
|
|
|
end
|
2016-08-18 15:47:51 +00:00
|
|
|
UIManager:broadcastEvent(Event:new("FlushSettings"))
|
2014-10-30 18:42:18 +00:00
|
|
|
end
|
|
|
|
|
2015-02-01 04:49:46 +00:00
|
|
|
-- ONLY used for Kindle devices
|
2014-10-30 18:42:18 +00:00
|
|
|
function Device:outofScreenSaver()
|
2016-02-29 17:22:52 +00:00
|
|
|
if self.screen_saver_mode == true and self.charging_mode == false then
|
|
|
|
-- On FW >= 5.7.2, put awesome to sleep again...
|
|
|
|
if os.getenv("AWESOME_STOPPED") == "yes" then
|
|
|
|
os.execute("killall -stop awesome")
|
|
|
|
end
|
2014-10-30 18:42:18 +00:00
|
|
|
-- wait for native system update screen before we recover saved
|
|
|
|
-- Blitbuffer.
|
|
|
|
util.usleep(1500000)
|
|
|
|
self.screen:restoreFromSavedBB()
|
2015-02-01 04:49:46 +00:00
|
|
|
self:resume()
|
2016-02-29 15:59:15 +00:00
|
|
|
if self:needsScreenRefreshAfterResume() then
|
|
|
|
self.screen:refreshFull()
|
|
|
|
end
|
2016-02-29 18:49:37 +00:00
|
|
|
self.powerd:refreshCapacity()
|
2014-10-30 18:42:18 +00:00
|
|
|
end
|
|
|
|
self.screen_saver_mode = false
|
|
|
|
end
|
|
|
|
|
2016-09-19 08:16:41 +00:00
|
|
|
function Device:rescheduleSuspend()
|
2016-09-14 05:16:31 +00:00
|
|
|
local UIManager = require("ui/uimanager")
|
2016-09-19 08:16:41 +00:00
|
|
|
UIManager:unschedule(self.suspend)
|
|
|
|
UIManager:scheduleIn(self.suspend_wait_timeout, self.suspend)
|
2016-09-14 05:16:31 +00:00
|
|
|
end
|
|
|
|
|
2015-02-01 04:49:46 +00:00
|
|
|
-- ONLY used for Kobo and PocketBook devices
|
2014-10-30 18:42:18 +00:00
|
|
|
function Device:onPowerEvent(ev)
|
2016-09-11 00:33:21 +00:00
|
|
|
if self.screen_saver_mode then
|
|
|
|
if ev == "Power" or ev == "Resume" then
|
2016-09-14 05:16:31 +00:00
|
|
|
if self.is_cover_closed then
|
|
|
|
-- don't let power key press wake up device when the cover is in closed state
|
2016-09-19 08:16:41 +00:00
|
|
|
self:rescheduleSuspend()
|
2016-09-14 05:16:31 +00:00
|
|
|
else
|
|
|
|
DEBUG("Resuming...")
|
2016-09-27 07:18:47 +00:00
|
|
|
local UIManager = require("ui/uimanager")
|
|
|
|
UIManager:unschedule(self.suspend)
|
2016-09-14 05:16:31 +00:00
|
|
|
local network_manager = require("ui/network/manager")
|
|
|
|
if network_manager.wifi_was_on and G_reader_settings:nilOrTrue("auto_restore_wifi") then
|
2016-09-30 07:51:44 +00:00
|
|
|
network_manager:restoreWifiAsync()
|
2016-09-14 05:16:31 +00:00
|
|
|
end
|
|
|
|
self:resume()
|
|
|
|
require("ui/screensaver"):close()
|
|
|
|
-- restore to previous rotation mode
|
|
|
|
self.screen:setRotationMode(self.orig_rotation_mode)
|
|
|
|
if self:needsScreenRefreshAfterResume() then
|
2016-09-27 07:18:47 +00:00
|
|
|
UIManager:scheduleIn(1, function() self.screen:refreshFull() end)
|
2016-09-14 05:16:31 +00:00
|
|
|
end
|
|
|
|
self.screen_saver_mode = false
|
|
|
|
self.powerd:refreshCapacity()
|
|
|
|
self.powerd:afterResume()
|
2016-09-11 00:33:21 +00:00
|
|
|
end
|
|
|
|
elseif ev == "Suspend" then
|
|
|
|
-- Already in screen saver mode, no need to update UI/state before
|
|
|
|
-- suspending the hardware. This usually happens when sleep cover
|
|
|
|
-- is closed after the device was sent to suspend state.
|
|
|
|
DEBUG("Already in screen saver mode, suspending...")
|
2016-09-19 08:16:41 +00:00
|
|
|
self:rescheduleSuspend()
|
2016-09-11 00:33:21 +00:00
|
|
|
end
|
|
|
|
-- else we we not in screensaver mode
|
|
|
|
elseif ev == "Power" or ev == "Suspend" then
|
2016-02-26 09:46:23 +00:00
|
|
|
self.powerd:beforeSuspend()
|
2016-09-11 00:33:21 +00:00
|
|
|
local network_manager = require("ui/network/manager")
|
2016-09-01 07:05:40 +00:00
|
|
|
if network_manager.wifi_was_on then
|
|
|
|
network_manager:releaseIP()
|
|
|
|
network_manager:turnOffWifi()
|
|
|
|
end
|
2014-10-30 18:42:18 +00:00
|
|
|
local UIManager = require("ui/uimanager")
|
2016-02-26 09:46:23 +00:00
|
|
|
-- flushing settings first in case the screensaver takes too long time
|
|
|
|
-- that flushing has no chance to run
|
2016-08-18 15:47:51 +00:00
|
|
|
UIManager:broadcastEvent(Event:new("FlushSettings"))
|
2014-10-30 18:42:18 +00:00
|
|
|
DEBUG("Suspending...")
|
|
|
|
-- always suspend in portrait mode
|
|
|
|
self.orig_rotation_mode = self.screen:getRotationMode()
|
|
|
|
self.screen:setRotationMode(0)
|
2016-09-11 00:33:21 +00:00
|
|
|
require("ui/screensaver"):show()
|
2016-02-26 09:46:23 +00:00
|
|
|
self.screen:refreshFull()
|
|
|
|
self.screen_saver_mode = true
|
2016-09-19 08:16:41 +00:00
|
|
|
UIManager:scheduleIn(self.suspend_wait_timeout, self.suspend)
|
2014-10-30 18:42:18 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-07 08:02:15 +00:00
|
|
|
-- Hardware specific method to suspend the device
|
2016-02-26 09:46:23 +00:00
|
|
|
function Device:suspend() end
|
2014-10-30 18:42:18 +00:00
|
|
|
|
2016-03-07 08:02:15 +00:00
|
|
|
-- Hardware specific method to resume the device
|
2016-02-26 09:46:23 +00:00
|
|
|
function Device:resume() end
|
2014-10-30 18:42:18 +00:00
|
|
|
|
2016-04-26 22:30:52 +00:00
|
|
|
-- Hardware specific method to power off the device
|
|
|
|
function Device:powerOff() end
|
|
|
|
|
2014-10-30 18:42:18 +00:00
|
|
|
function Device:usbPlugIn()
|
|
|
|
if self.charging_mode == false and self.screen_saver_mode == false then
|
|
|
|
self.screen:saveCurrentBB()
|
2016-02-29 17:22:52 +00:00
|
|
|
-- On FW >= 5.7.2, we sigstop awesome, but we need it to show stuff...
|
|
|
|
if os.getenv("AWESOME_STOPPED") == "yes" then
|
|
|
|
os.execute("killall -cont awesome")
|
|
|
|
end
|
2014-10-30 18:42:18 +00:00
|
|
|
end
|
|
|
|
self.charging_mode = true
|
|
|
|
end
|
|
|
|
|
|
|
|
function Device:usbPlugOut()
|
|
|
|
if self.charging_mode == true and self.screen_saver_mode == false then
|
2016-02-29 17:22:52 +00:00
|
|
|
-- On FW >= 5.7.2, put awesome to sleep again...
|
|
|
|
if os.getenv("AWESOME_STOPPED") == "yes" then
|
|
|
|
os.execute("killall -stop awesome")
|
|
|
|
end
|
|
|
|
-- Same as when going out of screensaver, wait for the native system
|
|
|
|
util.usleep(1500000)
|
2014-10-30 18:42:18 +00:00
|
|
|
self.screen:restoreFromSavedBB()
|
2014-11-26 08:25:18 +00:00
|
|
|
self.screen:refreshFull()
|
2016-02-29 18:49:37 +00:00
|
|
|
self.powerd:refreshCapacity()
|
2014-10-30 18:42:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
--@TODO signal filemanager for file changes 13.06 2012 (houqp)
|
|
|
|
self.charging_mode = false
|
|
|
|
end
|
|
|
|
|
2016-06-12 18:50:30 +00:00
|
|
|
-- Hardware specific method to initialize network manager module
|
|
|
|
function Device:initNetworkManager()
|
|
|
|
end
|
|
|
|
|
2014-10-30 18:42:18 +00:00
|
|
|
--[[
|
|
|
|
prepare for application shutdown
|
|
|
|
--]]
|
|
|
|
function Device:exit()
|
|
|
|
require("ffi/input"):closeAll()
|
|
|
|
self.screen:close()
|
|
|
|
end
|
|
|
|
|
2016-07-27 04:32:13 +00:00
|
|
|
function Device:retrieveNetworkInfo()
|
|
|
|
local std_out = io.popen("ifconfig | " ..
|
|
|
|
"sed -n " ..
|
|
|
|
"-e 's/ \\+$//g' " ..
|
|
|
|
"-e 's/ \\+/ /g' " ..
|
|
|
|
"-e 's/inet6\\? addr: \\?\\([^ ]\\+\\) .*$/\\1/p' " ..
|
|
|
|
"-e 's/Link encap:\\(.*\\)/\\1/p'",
|
|
|
|
"r")
|
|
|
|
if std_out then
|
|
|
|
local result = std_out:read("*all")
|
|
|
|
std_out:close()
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-10-30 18:42:18 +00:00
|
|
|
return Device
|