2016-12-29 00:10:38 -08:00
|
|
|
local logger = require("logger")
|
2016-11-16 11:49:34 +01:00
|
|
|
local _ = require("gettext")
|
2014-10-30 19:42:18 +01:00
|
|
|
|
2015-09-27 09:25:47 +08:00
|
|
|
local function yes() return true end
|
2014-10-30 19:42:18 +01:00
|
|
|
local function no() return false end
|
|
|
|
|
|
|
|
|
|
local Device = {
|
|
|
|
|
screen_saver_mode = false,
|
|
|
|
|
charging_mode = false,
|
|
|
|
|
survive_screen_saver = false,
|
2016-09-13 22:16:31 -07:00
|
|
|
is_cover_closed = false,
|
2014-10-30 19:42:18 +01:00
|
|
|
model = nil,
|
|
|
|
|
powerd = nil,
|
|
|
|
|
screen = nil,
|
|
|
|
|
input = nil,
|
2016-09-19 01:16:41 -07: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 19:42:18 +01:00
|
|
|
|
|
|
|
|
-- hardware feature tests: (these are functions!)
|
|
|
|
|
hasKeyboard = no,
|
|
|
|
|
hasKeys = no,
|
2015-09-27 09:05:37 +08:00
|
|
|
hasDPad = no,
|
2014-10-30 19:42:18 +01:00
|
|
|
isTouchDevice = no,
|
|
|
|
|
hasFrontlight = no,
|
2016-04-02 21:52:30 -07:00
|
|
|
needsTouchScreenProbe = no,
|
2017-11-14 19:20:58 +01:00
|
|
|
hasClipboard = no,
|
2017-10-01 19:23:06 +02:00
|
|
|
hasColorScreen = no,
|
2014-10-30 19:42:18 +01: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-25 00:51:57 +08:00
|
|
|
isPocketBook = no,
|
2014-10-30 19:42:18 +01:00
|
|
|
isAndroid = no,
|
2015-10-03 14:18:47 +08:00
|
|
|
isSDL = no,
|
2014-10-30 19:42:18 +01:00
|
|
|
|
|
|
|
|
-- some devices have part of their screen covered by the bezel
|
|
|
|
|
viewport = nil,
|
2016-02-26 17:46:23 +08: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 09:25:47 +08:00
|
|
|
-- needs full screen refresh when resumed from screensaver?
|
|
|
|
|
needsScreenRefreshAfterResume = yes,
|
2014-10-30 19:42:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function Device:new(o)
|
2016-02-15 23:10:07 -08:00
|
|
|
o = o or {}
|
2014-10-30 19:42:18 +01:00
|
|
|
setmetatable(o, self)
|
|
|
|
|
self.__index = self
|
|
|
|
|
return o
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function Device:init()
|
2017-06-14 10:32:16 -07:00
|
|
|
assert(self ~= nil)
|
2014-10-30 19:42:18 +01:00
|
|
|
if not self.screen then
|
2014-11-20 20:24:02 +01:00
|
|
|
error("screen/framebuffer must be implemented")
|
2014-10-30 19:42:18 +01:00
|
|
|
end
|
2014-11-22 21:55:53 +00:00
|
|
|
|
2017-10-01 19:23:06 +02:00
|
|
|
self.screen.isColorScreen = self.hasColorScreen
|
2017-10-01 00:16:38 +02:00
|
|
|
self.screen.isColorEnabled = function()
|
|
|
|
|
if G_reader_settings:has("color_rendering") then return G_reader_settings:isTrue("color_rendering") end
|
2017-10-01 19:23:06 +02:00
|
|
|
return self.screen.isColorScreen()
|
2017-10-01 00:16:38 +02:00
|
|
|
end
|
|
|
|
|
|
2016-02-29 15:08:57 +08:00
|
|
|
local is_eink = G_reader_settings:readSetting("eink")
|
2016-03-08 19:38:06 -08:00
|
|
|
self.screen.eink = (is_eink == nil) or is_eink
|
2016-02-29 15:08:57 +08:00
|
|
|
|
2016-12-29 00:10:38 -08:00
|
|
|
logger.info("initializing for device", self.model)
|
|
|
|
|
logger.info("framebuffer resolution:", self.screen:getSize())
|
2014-11-27 10:34:16 +00:00
|
|
|
|
2014-10-30 19:42:18 +01:00
|
|
|
if not self.input then
|
2014-11-03 10:08:55 +01:00
|
|
|
self.input = require("device/input"):new{device = self}
|
2014-10-30 19:42:18 +01:00
|
|
|
end
|
|
|
|
|
if not self.powerd then
|
|
|
|
|
self.powerd = require("device/generic/powerd"):new{device = self}
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if self.viewport then
|
2016-12-29 00:10:38 -08:00
|
|
|
logger.dbg("setting a viewport:", self.viewport)
|
2014-10-30 19:42:18 +01: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
|
|
|
|
|
|
2016-09-19 01:16:41 -07:00
|
|
|
function Device:rescheduleSuspend()
|
2016-09-13 22:16:31 -07:00
|
|
|
local UIManager = require("ui/uimanager")
|
2016-09-19 01:16:41 -07:00
|
|
|
UIManager:unschedule(self.suspend)
|
|
|
|
|
UIManager:scheduleIn(self.suspend_wait_timeout, self.suspend)
|
2016-09-13 22:16:31 -07:00
|
|
|
end
|
|
|
|
|
|
2015-02-01 12:49:46 +08:00
|
|
|
-- ONLY used for Kobo and PocketBook devices
|
2014-10-30 19:42:18 +01:00
|
|
|
function Device:onPowerEvent(ev)
|
2016-09-10 17:33:21 -07:00
|
|
|
if self.screen_saver_mode then
|
|
|
|
|
if ev == "Power" or ev == "Resume" then
|
2016-09-13 22:16:31 -07: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 01:16:41 -07:00
|
|
|
self:rescheduleSuspend()
|
2016-09-13 22:16:31 -07:00
|
|
|
else
|
2016-12-29 00:10:38 -08:00
|
|
|
logger.dbg("Resuming...")
|
2016-09-27 00:18:47 -07:00
|
|
|
local UIManager = require("ui/uimanager")
|
|
|
|
|
UIManager:unschedule(self.suspend)
|
2016-09-13 22:16:31 -07: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 00:51:44 -07:00
|
|
|
network_manager:restoreWifiAsync()
|
2016-09-13 22:16:31 -07: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 00:18:47 -07:00
|
|
|
UIManager:scheduleIn(1, function() self.screen:refreshFull() end)
|
2016-09-13 22:16:31 -07:00
|
|
|
end
|
|
|
|
|
self.screen_saver_mode = false
|
|
|
|
|
self.powerd:afterResume()
|
2016-09-10 17:33:21 -07: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.
|
2016-12-29 00:10:38 -08:00
|
|
|
logger.dbg("Already in screen saver mode, suspending...")
|
2016-09-19 01:16:41 -07:00
|
|
|
self:rescheduleSuspend()
|
2016-09-10 17:33:21 -07:00
|
|
|
end
|
|
|
|
|
-- else we we not in screensaver mode
|
|
|
|
|
elseif ev == "Power" or ev == "Suspend" then
|
2016-02-26 17:46:23 +08:00
|
|
|
self.powerd:beforeSuspend()
|
2014-10-30 19:42:18 +01:00
|
|
|
local UIManager = require("ui/uimanager")
|
2016-12-29 00:10:38 -08:00
|
|
|
logger.dbg("Suspending...")
|
2014-10-30 19:42:18 +01:00
|
|
|
-- always suspend in portrait mode
|
|
|
|
|
self.orig_rotation_mode = self.screen:getRotationMode()
|
|
|
|
|
self.screen:setRotationMode(0)
|
2017-12-17 18:27:24 +01:00
|
|
|
require("ui/screensaver"):show()
|
2016-02-26 17:46:23 +08:00
|
|
|
self.screen:refreshFull()
|
|
|
|
|
self.screen_saver_mode = true
|
2017-04-01 20:24:15 +01:00
|
|
|
UIManager:scheduleIn(0.1, function()
|
|
|
|
|
local network_manager = require("ui/network/manager")
|
2018-05-10 06:26:07 -04:00
|
|
|
-- NOTE: wifi_was_on does not necessarily mean that WiFi is *currently* on! It means *we* enabled it.
|
|
|
|
|
-- This is critical on Kobos (c.f., #3936), where it might still be on from KSM or Nickel,
|
|
|
|
|
-- without us being aware of it (i.e., wifi_was_on still unset or false),
|
|
|
|
|
-- because suspend will at best fail, and at worst deadlock the system if WiFi is on,
|
|
|
|
|
-- regardless of who enabled it!
|
|
|
|
|
if network_manager.wifi_was_on or network_manager:isWifiOn() then
|
2017-04-01 20:24:15 +01:00
|
|
|
network_manager:releaseIP()
|
|
|
|
|
network_manager:turnOffWifi()
|
|
|
|
|
end
|
|
|
|
|
UIManager:scheduleIn(self.suspend_wait_timeout, self.suspend)
|
|
|
|
|
end)
|
2014-10-30 19:42:18 +01:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2016-10-22 00:04:37 -07:00
|
|
|
-- Hardware specific method to handle usb plug in event
|
|
|
|
|
function Device:usbPlugIn() end
|
|
|
|
|
|
|
|
|
|
-- Hardware specific method to handle usb plug out event
|
|
|
|
|
function Device:usbPlugOut() end
|
|
|
|
|
|
2016-03-07 00:02:15 -08:00
|
|
|
-- Hardware specific method to suspend the device
|
2016-02-26 17:46:23 +08:00
|
|
|
function Device:suspend() end
|
2014-10-30 19:42:18 +01:00
|
|
|
|
2016-03-07 00:02:15 -08:00
|
|
|
-- Hardware specific method to resume the device
|
2016-02-26 17:46:23 +08:00
|
|
|
function Device:resume() end
|
2014-10-30 19:42:18 +01:00
|
|
|
|
2016-04-26 23:30:52 +01:00
|
|
|
-- Hardware specific method to power off the device
|
|
|
|
|
function Device:powerOff() end
|
|
|
|
|
|
2017-05-14 18:43:08 +02:00
|
|
|
-- Hardware specific method to reboot the device
|
|
|
|
|
function Device:reboot() end
|
|
|
|
|
|
2016-06-12 11:50:30 -07:00
|
|
|
-- Hardware specific method to initialize network manager module
|
2016-10-22 00:04:37 -07:00
|
|
|
function Device:initNetworkManager() end
|
2016-06-12 11:50:30 -07:00
|
|
|
|
2017-04-13 19:55:31 +02:00
|
|
|
function Device:supportsScreensaver() return false end
|
|
|
|
|
|
2017-09-24 05:58:34 +08:00
|
|
|
-- Device specific method to set datetime
|
|
|
|
|
function Device:setDateTime(year, month, day, hour, min, sec) end
|
|
|
|
|
|
2017-09-04 21:05:05 +02:00
|
|
|
-- Device specific method if any setting needs being saved
|
|
|
|
|
function Device:saveSettings() end
|
|
|
|
|
|
2014-10-30 19:42:18 +01:00
|
|
|
--[[
|
|
|
|
|
prepare for application shutdown
|
|
|
|
|
--]]
|
|
|
|
|
function Device:exit()
|
|
|
|
|
require("ffi/input"):closeAll()
|
|
|
|
|
self.screen:close()
|
|
|
|
|
end
|
|
|
|
|
|
2016-07-26 21:32:13 -07:00
|
|
|
function Device:retrieveNetworkInfo()
|
|
|
|
|
local std_out = io.popen("ifconfig | " ..
|
|
|
|
|
"sed -n " ..
|
|
|
|
|
"-e 's/ \\+$//g' " ..
|
|
|
|
|
"-e 's/ \\+/ /g' " ..
|
2016-12-04 15:23:57 +01:00
|
|
|
"-e 's/ \\?inet6\\? addr: \\?\\([^ ]\\+\\) .*$/IP: \\1/p' " ..
|
|
|
|
|
"-e 's/Link encap:Ethernet\\(.*\\)/\\1/p'",
|
2016-07-26 21:32:13 -07:00
|
|
|
"r")
|
|
|
|
|
if std_out then
|
|
|
|
|
local result = std_out:read("*all")
|
|
|
|
|
std_out:close()
|
2018-05-01 14:49:37 +02:00
|
|
|
std_out = io.popen('2>/dev/null iwconfig | grep ESSID | cut -d\\" -f2')
|
2016-12-04 15:23:57 +01:00
|
|
|
if std_out then
|
|
|
|
|
local ssid = std_out:read("*all")
|
|
|
|
|
result = result .. "SSID: " .. ssid:gsub("(.-)%s*$", "%1") .. "\n"
|
|
|
|
|
std_out:close()
|
|
|
|
|
end
|
|
|
|
|
if os.execute("ip r | grep -q default") == 0 then
|
|
|
|
|
local pingok = os.execute("ping -q -w 3 -c 2 `ip r | grep default | cut -d ' ' -f 3` > /dev/null")
|
|
|
|
|
if pingok == 0 then
|
2017-03-25 21:07:47 +00:00
|
|
|
result = result .. "Gateway ping successful"
|
2016-12-04 15:23:57 +01:00
|
|
|
else
|
|
|
|
|
result = result .. "Gateway ping FAILED"
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
result = result .. "No default gateway to ping"
|
|
|
|
|
end
|
2016-07-26 21:32:13 -07:00
|
|
|
return result
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2017-08-15 19:54:02 +02:00
|
|
|
function Device:setTime(hour, min)
|
|
|
|
|
return false
|
|
|
|
|
end
|
|
|
|
|
|
2017-06-14 10:32:16 -07:00
|
|
|
-- Return an integer value to indicate the brightness of the environment. The value should be in
|
2017-06-24 00:55:31 -07:00
|
|
|
-- range [0, 4].
|
2017-06-14 10:32:16 -07:00
|
|
|
-- 0: dark.
|
|
|
|
|
-- 1: dim, frontlight is needed.
|
2017-06-24 00:55:31 -07:00
|
|
|
-- 2: neutral, turning frontlight on or off does not impact the reading experience.
|
|
|
|
|
-- 3: bright, frontlight is not needed.
|
|
|
|
|
-- 4: dazzling.
|
2017-06-14 10:32:16 -07:00
|
|
|
function Device:ambientBrightnessLevel()
|
|
|
|
|
return 0
|
|
|
|
|
end
|
|
|
|
|
|
2014-10-30 19:42:18 +01:00
|
|
|
return Device
|