2014-10-30 18:42:18 +00:00
|
|
|
local Device = require("device")
|
|
|
|
local Screen = Device.screen
|
|
|
|
local Input = require("device").input
|
2013-10-18 20:38:07 +00:00
|
|
|
local Event = require("ui/event")
|
2014-11-28 20:12:54 +00:00
|
|
|
local Geom = require("ui/geometry")
|
2014-08-26 10:10:26 +00:00
|
|
|
local util = require("ffi/util")
|
2016-03-29 06:37:15 +00:00
|
|
|
local dbg = require("dbg")
|
2013-10-24 13:45:02 +00:00
|
|
|
local _ = require("gettext")
|
2012-04-22 19:29:48 +00:00
|
|
|
|
2016-03-29 07:56:29 +00:00
|
|
|
local noop = function() end
|
2015-12-26 00:48:40 +00:00
|
|
|
local MILLION = 1000000
|
|
|
|
|
2012-04-22 19:29:48 +00:00
|
|
|
-- there is only one instance of this
|
2013-10-18 20:38:07 +00:00
|
|
|
local UIManager = {
|
2014-03-13 13:52:43 +00:00
|
|
|
-- trigger a full refresh when counter reaches FULL_REFRESH_COUNT
|
2016-02-25 08:54:41 +00:00
|
|
|
FULL_REFRESH_COUNT =
|
|
|
|
G_reader_settings:readSetting("full_refresh_count") or DRCOUNTMAX,
|
2014-03-13 13:52:43 +00:00
|
|
|
refresh_count = 0,
|
2013-03-12 17:18:53 +00:00
|
|
|
|
2014-06-26 17:37:33 +00:00
|
|
|
event_handlers = nil,
|
|
|
|
|
2014-03-13 13:52:43 +00:00
|
|
|
_running = true,
|
|
|
|
_window_stack = {},
|
2015-12-26 00:48:40 +00:00
|
|
|
_task_queue = {},
|
|
|
|
_task_queue_dirty = false,
|
2014-06-23 14:33:11 +00:00
|
|
|
_dirty = {},
|
|
|
|
_zeromqs = {},
|
2014-11-28 20:12:54 +00:00
|
|
|
_refresh_stack = {},
|
|
|
|
_refresh_func_stack = {},
|
2012-04-22 19:29:48 +00:00
|
|
|
}
|
|
|
|
|
2014-06-26 17:37:33 +00:00
|
|
|
function UIManager:init()
|
|
|
|
self.event_handlers = {
|
|
|
|
__default__ = function(input_event)
|
|
|
|
self:sendEvent(input_event)
|
|
|
|
end,
|
|
|
|
SaveState = function()
|
|
|
|
self:sendEvent(Event:new("FlushSettings"))
|
2014-08-26 10:10:26 +00:00
|
|
|
end,
|
|
|
|
Power = function(input_event)
|
|
|
|
Device:onPowerEvent(input_event)
|
|
|
|
end,
|
2014-06-26 17:37:33 +00:00
|
|
|
}
|
|
|
|
if Device:isKobo() then
|
2016-03-18 23:33:14 +00:00
|
|
|
-- We do not want auto suspend procedure to waste battery during
|
|
|
|
-- suspend. So let's unschedule it when suspending, and restart it after
|
|
|
|
-- resume.
|
|
|
|
self:_initAutoSuspend()
|
2014-08-26 10:10:26 +00:00
|
|
|
self.event_handlers["Suspend"] = function(input_event)
|
2016-03-29 07:56:29 +00:00
|
|
|
self:_stopAutoSuspend()
|
2014-08-26 10:10:26 +00:00
|
|
|
Device:onPowerEvent(input_event)
|
|
|
|
end
|
|
|
|
self.event_handlers["Resume"] = function(input_event)
|
|
|
|
Device:onPowerEvent(input_event)
|
|
|
|
self:sendEvent(Event:new("Resume"))
|
2016-03-29 07:56:29 +00:00
|
|
|
self:_startAutoSuspend()
|
2014-06-26 17:37:33 +00:00
|
|
|
end
|
|
|
|
self.event_handlers["Light"] = function()
|
|
|
|
Device:getPowerDevice():toggleFrontlight()
|
|
|
|
end
|
2014-06-26 18:28:57 +00:00
|
|
|
self.event_handlers["__default__"] = function(input_event)
|
|
|
|
if Device.screen_saver_mode then
|
|
|
|
-- Suspension in Kobo can be interrupted by screen updates. We
|
|
|
|
-- ignore user touch input here so screen udpate won't be
|
|
|
|
-- triggered in suspend mode
|
|
|
|
return
|
|
|
|
else
|
|
|
|
self:sendEvent(input_event)
|
|
|
|
end
|
|
|
|
end
|
2016-01-07 14:30:28 +00:00
|
|
|
local kobo_light_on_start = tonumber(KOBO_LIGHT_ON_START)
|
|
|
|
if kobo_light_on_start then
|
2016-01-12 13:52:44 +00:00
|
|
|
local new_intensity
|
2016-03-02 18:08:07 +00:00
|
|
|
local is_frontlight_on
|
2016-02-25 08:54:41 +00:00
|
|
|
if kobo_light_on_start > 0 then
|
2016-01-07 14:30:28 +00:00
|
|
|
new_intensity = math.min(kobo_light_on_start, 100)
|
2016-03-02 18:08:07 +00:00
|
|
|
is_frontlight_on = true
|
2016-02-25 08:54:41 +00:00
|
|
|
elseif kobo_light_on_start == 0 then
|
2016-03-02 18:08:07 +00:00
|
|
|
is_frontlight_on = false
|
2016-01-07 14:30:28 +00:00
|
|
|
elseif kobo_light_on_start == -2 then
|
2016-01-12 13:52:44 +00:00
|
|
|
local NickelConf = require("device/kobo/nickel_conf")
|
2016-02-25 08:54:41 +00:00
|
|
|
new_intensity = NickelConf.frontLightLevel.get()
|
2016-03-02 18:08:07 +00:00
|
|
|
is_frontlight_on = NickelConf.frontLightState:get()
|
|
|
|
if is_frontlight_on == nil then
|
2016-03-02 06:30:29 +00:00
|
|
|
-- this device does not support frontlight toggle,
|
2016-03-02 08:05:56 +00:00
|
|
|
-- we set the value based on frontlight intensity.
|
2016-03-02 06:30:29 +00:00
|
|
|
if new_intensity > 0 then
|
2016-03-02 18:08:07 +00:00
|
|
|
is_frontlight_on = true
|
2016-03-02 06:30:29 +00:00
|
|
|
else
|
2016-03-02 18:08:07 +00:00
|
|
|
is_frontlight_on = false
|
2016-03-02 06:30:29 +00:00
|
|
|
end
|
|
|
|
end
|
2016-01-07 14:30:28 +00:00
|
|
|
end
|
2016-03-02 06:30:29 +00:00
|
|
|
-- Since this kobo-specific, we save all values in settings here
|
|
|
|
-- and let the code (reader.lua) pick it up later during bootstrap.
|
2016-01-07 14:30:28 +00:00
|
|
|
if new_intensity then
|
2016-03-02 06:30:29 +00:00
|
|
|
G_reader_settings:saveSetting("frontlight_intensity",
|
|
|
|
new_intensity)
|
2016-01-07 14:30:28 +00:00
|
|
|
end
|
2016-03-02 18:08:07 +00:00
|
|
|
G_reader_settings:saveSetting("is_frontlight_on", is_frontlight_on)
|
2014-08-12 11:11:13 +00:00
|
|
|
end
|
2014-06-26 17:37:33 +00:00
|
|
|
elseif Device:isKindle() then
|
|
|
|
self.event_handlers["IntoSS"] = function()
|
|
|
|
Device:intoScreenSaver()
|
|
|
|
end
|
|
|
|
self.event_handlers["OutOfSS"] = function()
|
|
|
|
Device:outofScreenSaver()
|
2014-08-26 10:10:26 +00:00
|
|
|
self:sendEvent(Event:new("Resume"))
|
2014-06-26 17:37:33 +00:00
|
|
|
end
|
|
|
|
self.event_handlers["Charging"] = function()
|
|
|
|
Device:usbPlugIn()
|
|
|
|
end
|
|
|
|
self.event_handlers["NotCharging"] = function()
|
|
|
|
Device:usbPlugOut()
|
|
|
|
self:sendEvent(Event:new("NotCharging"))
|
2014-06-21 16:24:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-04-22 19:29:48 +00:00
|
|
|
-- register & show a widget
|
2014-10-30 08:01:01 +00:00
|
|
|
-- modal widget should be always on the top
|
2014-11-30 22:01:13 +00:00
|
|
|
-- for refreshtype & refreshregion see description of setDirty()
|
2014-12-01 12:32:27 +00:00
|
|
|
function UIManager:show(widget, refreshtype, refreshregion, x, y)
|
2016-03-29 06:37:15 +00:00
|
|
|
dbg("show widget", widget._name)
|
2014-09-03 03:13:46 +00:00
|
|
|
self._running = true
|
2014-10-30 08:01:01 +00:00
|
|
|
local window = {x = x or 0, y = y or 0, widget = widget}
|
|
|
|
-- put this window on top of the toppest non-modal window
|
|
|
|
for i = #self._window_stack, 0, -1 do
|
|
|
|
local top_window = self._window_stack[i]
|
|
|
|
-- skip modal window
|
|
|
|
if not top_window or not top_window.widget.modal then
|
|
|
|
table.insert(self._window_stack, i + 1, window)
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
2014-03-13 13:52:43 +00:00
|
|
|
-- and schedule it to be painted
|
2014-12-01 12:32:27 +00:00
|
|
|
self:setDirty(widget, refreshtype, refreshregion)
|
2014-03-13 13:52:43 +00:00
|
|
|
-- tell the widget that it is shown now
|
|
|
|
widget:handleEvent(Event:new("Show"))
|
|
|
|
-- check if this widget disables double tap gesture
|
|
|
|
if widget.disable_double_tap then
|
|
|
|
Input.disable_double_tap = true
|
|
|
|
end
|
2012-04-22 19:29:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- unregister a widget
|
2014-11-30 22:01:13 +00:00
|
|
|
-- for refreshtype & refreshregion see description of setDirty()
|
|
|
|
function UIManager:close(widget, refreshtype, refreshregion)
|
2014-11-06 06:00:47 +00:00
|
|
|
if not widget then
|
2016-03-29 06:37:15 +00:00
|
|
|
dbg("widget not exist to be closed")
|
2014-11-06 06:00:47 +00:00
|
|
|
return
|
|
|
|
end
|
2016-03-29 06:37:15 +00:00
|
|
|
dbg("close widget", widget.id)
|
2014-11-20 19:24:02 +00:00
|
|
|
-- TODO: Why do we the following?
|
2014-03-13 13:52:43 +00:00
|
|
|
Input.disable_double_tap = DGESDETECT_DISABLE_DOUBLE_TAP
|
|
|
|
local dirty = false
|
|
|
|
for i = #self._window_stack, 1, -1 do
|
|
|
|
if self._window_stack[i].widget == widget then
|
2014-12-01 12:32:27 +00:00
|
|
|
-- tell the widget that it is closed now
|
|
|
|
widget:handleEvent(Event:new("CloseWidget"))
|
2014-03-13 13:52:43 +00:00
|
|
|
table.remove(self._window_stack, i)
|
|
|
|
dirty = true
|
|
|
|
elseif self._window_stack[i].widget.disable_double_tap then
|
|
|
|
Input.disable_double_tap = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if dirty then
|
|
|
|
-- schedule remaining widgets to be painted
|
|
|
|
for i = 1, #self._window_stack do
|
2014-11-30 22:01:13 +00:00
|
|
|
self:setDirty(self._window_stack[i].widget)
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
2014-11-30 22:01:13 +00:00
|
|
|
self:_refresh(refreshtype, refreshregion)
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
2012-04-22 19:29:48 +00:00
|
|
|
end
|
|
|
|
|
2015-12-26 00:48:40 +00:00
|
|
|
-- schedule an execution task, task queue is in ascendant order
|
2012-04-22 19:29:48 +00:00
|
|
|
function UIManager:schedule(time, action)
|
2015-12-26 00:48:40 +00:00
|
|
|
local p, s, e = 1, 1, #self._task_queue
|
|
|
|
if e ~= 0 then
|
|
|
|
local us = time[1] * MILLION + time[2]
|
|
|
|
-- do a binary insert
|
|
|
|
repeat
|
|
|
|
p = math.floor(s + (e - s) / 2)
|
|
|
|
local ptime = self._task_queue[p].time
|
|
|
|
local ptus = ptime[1] * MILLION + ptime[2]
|
|
|
|
if us > ptus then
|
|
|
|
if s == e then
|
|
|
|
p = e + 1
|
|
|
|
break
|
|
|
|
elseif s + 1 == e then
|
|
|
|
s = e
|
|
|
|
else
|
|
|
|
s = p
|
|
|
|
end
|
|
|
|
elseif us < ptus then
|
|
|
|
e = p
|
|
|
|
if s == e then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
else
|
|
|
|
-- for fairness, it's better to make p+1 is strictly less than p
|
|
|
|
-- might want to revisit here in the future
|
|
|
|
break
|
|
|
|
end
|
|
|
|
until e < s
|
|
|
|
end
|
|
|
|
table.insert(self._task_queue, p, { time = time, action = action })
|
|
|
|
self._task_queue_dirty = true
|
2012-04-22 19:29:48 +00:00
|
|
|
end
|
2016-03-29 06:37:15 +00:00
|
|
|
dbg:guard(UIManager, 'schedule',
|
|
|
|
function(self, time, action)
|
|
|
|
assert(time[1] >= 0 and time[2] >= 0, "Only positive time allowed")
|
|
|
|
assert(action ~= nil)
|
|
|
|
end)
|
2012-04-22 19:29:48 +00:00
|
|
|
|
|
|
|
-- schedule task in a certain amount of seconds (fractions allowed) from now
|
|
|
|
function UIManager:scheduleIn(seconds, action)
|
2014-03-13 13:52:43 +00:00
|
|
|
local when = { util.gettime() }
|
|
|
|
local s = math.floor(seconds)
|
2015-12-26 00:48:40 +00:00
|
|
|
local usecs = (seconds - s) * MILLION
|
2014-03-13 13:52:43 +00:00
|
|
|
when[1] = when[1] + s
|
|
|
|
when[2] = when[2] + usecs
|
2015-12-26 00:48:40 +00:00
|
|
|
if when[2] > MILLION then
|
2014-03-13 13:52:43 +00:00
|
|
|
when[1] = when[1] + 1
|
2015-12-26 00:48:40 +00:00
|
|
|
when[2] = when[2] - MILLION
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
|
|
|
self:schedule(when, action)
|
2012-04-22 19:29:48 +00:00
|
|
|
end
|
2016-03-29 06:37:15 +00:00
|
|
|
dbg:guard(UIManager, 'scheduleIn',
|
|
|
|
function(self, seconds, action)
|
|
|
|
assert(seconds >= 0, "Only positive seconds allowed")
|
|
|
|
end)
|
2012-04-22 19:29:48 +00:00
|
|
|
|
2016-02-17 06:36:40 +00:00
|
|
|
function UIManager:nextTick(action)
|
|
|
|
return self:scheduleIn(0, action)
|
|
|
|
end
|
|
|
|
|
2014-11-09 15:38:58 +00:00
|
|
|
-- unschedule an execution task
|
|
|
|
-- in order to unschedule anonymous functions, store a reference
|
|
|
|
-- for example:
|
|
|
|
-- self.anonymousFunction = function() self:regularFunction() end
|
|
|
|
-- UIManager:scheduleIn(10, self.anonymousFunction)
|
|
|
|
-- UIManager:unschedule(self.anonymousFunction)
|
2014-11-06 11:59:00 +00:00
|
|
|
function UIManager:unschedule(action)
|
2015-12-26 00:48:40 +00:00
|
|
|
for i = #self._task_queue, 1, -1 do
|
2016-03-08 06:52:52 +00:00
|
|
|
if self._task_queue[i].action == action then
|
2015-12-26 00:48:40 +00:00
|
|
|
table.remove(self._task_queue, i)
|
2014-11-06 11:59:00 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-03-29 06:37:15 +00:00
|
|
|
dbg:guard(UIManager, 'unschedule',
|
|
|
|
function(self, action) assert(action ~= nil) end)
|
2014-11-06 11:59:00 +00:00
|
|
|
|
2014-11-28 20:12:54 +00:00
|
|
|
--[[
|
|
|
|
register a widget to be repainted and enqueue a refresh
|
|
|
|
|
|
|
|
the second parameter (refreshtype) can either specify a refreshtype
|
|
|
|
(optionally in combination with a refreshregion - which is suggested)
|
|
|
|
or a function that returns refreshtype AND refreshregion and is called
|
|
|
|
after painting the widget.
|
|
|
|
|
|
|
|
E.g.:
|
|
|
|
UIManager:setDirty(self.widget, "partial")
|
|
|
|
UIManager:setDirty(self.widget, "partial", Geom:new{x=10,y=10,w=100,h=50})
|
|
|
|
UIManager:setDirty(self.widget, function() return "ui", self.someelement.dimen end)
|
|
|
|
--]]
|
|
|
|
function UIManager:setDirty(widget, refreshtype, refreshregion)
|
2014-08-20 06:45:11 +00:00
|
|
|
if widget then
|
2014-11-28 20:12:54 +00:00
|
|
|
if widget == "all" then
|
|
|
|
-- special case: set all top-level widgets as being "dirty".
|
|
|
|
for i = 1, #self._window_stack do
|
|
|
|
self._dirty[self._window_stack[i].widget] = true
|
|
|
|
end
|
|
|
|
else
|
|
|
|
self._dirty[widget] = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
-- handle refresh information
|
|
|
|
if type(refreshtype) == "function" then
|
|
|
|
-- callback, will be issued after painting
|
|
|
|
table.insert(self._refresh_func_stack, refreshtype)
|
|
|
|
else
|
|
|
|
-- otherwise, enqueue refresh
|
|
|
|
self:_refresh(refreshtype, refreshregion)
|
2014-08-20 06:45:11 +00:00
|
|
|
end
|
2012-04-22 19:29:48 +00:00
|
|
|
end
|
2016-03-29 06:37:15 +00:00
|
|
|
dbg:guard(UIManager, 'setDirty',
|
|
|
|
nil,
|
|
|
|
function(self, widget, refreshtype, refreshregion)
|
|
|
|
if not widget then return end
|
|
|
|
-- when debugging, we check if we get handed a valid widget,
|
|
|
|
-- which would be a dialog that was previously passed via show()
|
|
|
|
local found = false
|
|
|
|
for i = 1, #self._window_stack do
|
|
|
|
if self._window_stack[i].widget == widget then found = true end
|
|
|
|
end
|
|
|
|
if not found then
|
|
|
|
dbg("INFO: invalid widget for setDirty()", debug.traceback())
|
|
|
|
end
|
|
|
|
end)
|
2012-04-22 19:29:48 +00:00
|
|
|
|
2014-06-23 14:33:11 +00:00
|
|
|
function UIManager:insertZMQ(zeromq)
|
|
|
|
table.insert(self._zeromqs, zeromq)
|
|
|
|
return zeromq
|
|
|
|
end
|
|
|
|
|
|
|
|
function UIManager:removeZMQ(zeromq)
|
|
|
|
for i = #self._zeromqs, 1, -1 do
|
|
|
|
if self._zeromqs[i] == zeromq then
|
|
|
|
table.remove(self._zeromqs, i)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-07-26 14:22:52 +00:00
|
|
|
-- set full refresh rate for e-ink screen
|
|
|
|
-- and make the refresh rate persistant in global reader settings
|
|
|
|
function UIManager:setRefreshRate(rate)
|
2016-03-29 06:37:15 +00:00
|
|
|
dbg("set screen full refresh rate", rate)
|
2014-07-26 14:22:52 +00:00
|
|
|
self.FULL_REFRESH_COUNT = rate
|
|
|
|
G_reader_settings:saveSetting("full_refresh_count", rate)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- get full refresh rate for e-ink screen
|
|
|
|
function UIManager:getRefreshRate(rate)
|
|
|
|
return self.FULL_REFRESH_COUNT
|
|
|
|
end
|
|
|
|
|
2012-04-22 19:29:48 +00:00
|
|
|
-- signal to quit
|
|
|
|
function UIManager:quit()
|
2016-03-29 06:37:15 +00:00
|
|
|
dbg("quiting uimanager")
|
2015-12-26 00:48:40 +00:00
|
|
|
self._task_queue_dirty = false
|
2014-03-13 13:52:43 +00:00
|
|
|
self._running = false
|
2015-03-02 09:15:26 +00:00
|
|
|
self._run_forever = nil
|
2014-09-03 03:13:46 +00:00
|
|
|
for i = #self._window_stack, 1, -1 do
|
|
|
|
table.remove(self._window_stack, i)
|
|
|
|
end
|
2015-12-26 00:48:40 +00:00
|
|
|
for i = #self._task_queue, 1, -1 do
|
|
|
|
table.remove(self._task_queue, i)
|
2014-09-03 03:13:46 +00:00
|
|
|
end
|
2014-06-23 14:33:11 +00:00
|
|
|
for i = #self._zeromqs, 1, -1 do
|
|
|
|
self._zeromqs[i]:stop()
|
|
|
|
table.remove(self._zeromqs, i)
|
|
|
|
end
|
2015-03-02 09:15:26 +00:00
|
|
|
if self.looper then
|
|
|
|
self.looper:close()
|
|
|
|
self.looper = nil
|
|
|
|
end
|
2012-04-22 19:29:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- transmit an event to registered widgets
|
|
|
|
function UIManager:sendEvent(event)
|
2014-06-07 05:36:58 +00:00
|
|
|
if #self._window_stack == 0 then return end
|
2014-03-13 13:52:43 +00:00
|
|
|
-- top level widget has first access to the event
|
|
|
|
if self._window_stack[#self._window_stack].widget:handleEvent(event) then
|
|
|
|
return
|
|
|
|
end
|
2012-04-22 19:29:48 +00:00
|
|
|
|
2014-03-13 13:52:43 +00:00
|
|
|
-- if the event is not consumed, active widgets can access it
|
|
|
|
for _, widget in ipairs(self._window_stack) do
|
|
|
|
if widget.widget.is_always_active then
|
|
|
|
if widget.widget:handleEvent(event) then return end
|
|
|
|
end
|
|
|
|
if widget.widget.active_widgets then
|
|
|
|
for _, active_widget in ipairs(widget.widget.active_widgets) do
|
|
|
|
if active_widget:handleEvent(event) then return end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-04-22 19:29:48 +00:00
|
|
|
end
|
|
|
|
|
2014-11-28 20:12:54 +00:00
|
|
|
function UIManager:_checkTasks()
|
2014-03-13 13:52:43 +00:00
|
|
|
local now = { util.gettime() }
|
2015-12-26 00:48:40 +00:00
|
|
|
local now_us = now[1] * MILLION + now[2]
|
2014-03-13 13:52:43 +00:00
|
|
|
local wait_until = nil
|
2015-12-26 00:48:40 +00:00
|
|
|
|
2016-03-18 23:33:14 +00:00
|
|
|
-- task.action may schedule other events
|
|
|
|
self._task_queue_dirty = false
|
2015-12-26 00:48:40 +00:00
|
|
|
while true do
|
2016-01-03 09:18:54 +00:00
|
|
|
local nu_task = #self._task_queue
|
|
|
|
if nu_task == 0 then
|
2015-12-26 00:48:40 +00:00
|
|
|
-- all tasks checked
|
|
|
|
break
|
|
|
|
end
|
|
|
|
local task = self._task_queue[1]
|
|
|
|
local task_us = 0
|
|
|
|
if task.time ~= nil then
|
|
|
|
task_us = task.time[1] * MILLION + task.time[2]
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
2015-12-26 00:48:40 +00:00
|
|
|
if task_us <= now_us then
|
|
|
|
-- remove from table
|
|
|
|
table.remove(self._task_queue, 1)
|
|
|
|
-- task is pending to be executed right now. do it.
|
|
|
|
-- NOTE: be careful that task.action() might modify
|
|
|
|
-- _task_queue here. So need to avoid race condition
|
|
|
|
task.action()
|
|
|
|
else
|
|
|
|
-- queue is sorted in ascendant order, safe to assume all items
|
|
|
|
-- are future tasks for now
|
|
|
|
wait_until = task.time
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-11-14 08:12:46 +00:00
|
|
|
return wait_until, now
|
2013-01-05 14:28:14 +00:00
|
|
|
end
|
|
|
|
|
2014-11-28 20:12:54 +00:00
|
|
|
-- precedence of refresh modes:
|
|
|
|
local refresh_modes = { fast = 1, ui = 2, partial = 3, full = 4 }
|
|
|
|
-- refresh methods in framebuffer implementation
|
|
|
|
local refresh_methods = {
|
|
|
|
fast = "refreshFast",
|
|
|
|
ui = "refreshUI",
|
|
|
|
partial = "refreshPartial",
|
|
|
|
full = "refreshFull",
|
|
|
|
}
|
|
|
|
|
|
|
|
--[[
|
|
|
|
refresh mode comparision
|
|
|
|
|
|
|
|
will return the mode that takes precedence
|
|
|
|
--]]
|
|
|
|
local function update_mode(mode1, mode2)
|
|
|
|
if refresh_modes[mode1] > refresh_modes[mode2] then
|
|
|
|
return mode1
|
|
|
|
else
|
|
|
|
return mode2
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--[[
|
|
|
|
enqueue a refresh
|
|
|
|
|
|
|
|
Widgets call this in their paintTo() method in order to notify
|
|
|
|
UIManager that a certain part of the screen is to be refreshed.
|
|
|
|
|
|
|
|
mode: refresh mode ("full", "partial", "ui", "fast")
|
|
|
|
region: Rect() that specifies the region to be updated
|
|
|
|
optional, update will affect whole screen if not specified.
|
|
|
|
Note that this should be the exception.
|
|
|
|
--]]
|
|
|
|
function UIManager:_refresh(mode, region)
|
2014-12-01 15:09:00 +00:00
|
|
|
if not mode then return end
|
2014-11-28 20:12:54 +00:00
|
|
|
-- special case: full screen partial update
|
|
|
|
-- will get promoted every self.FULL_REFRESH_COUNT updates
|
2014-12-04 07:46:47 +00:00
|
|
|
-- since _refresh can be called mutiple times via setDirty called in
|
|
|
|
-- different widget before a real screen repaint, we should make sure
|
|
|
|
-- refresh_count is incremented by only once at most for each repaint
|
|
|
|
if not region and mode == "partial" and not self.refresh_counted then
|
2014-11-28 20:12:54 +00:00
|
|
|
self.refresh_count = (self.refresh_count + 1) % self.FULL_REFRESH_COUNT
|
|
|
|
if self.refresh_count == self.FULL_REFRESH_COUNT - 1 then
|
2016-03-29 06:37:15 +00:00
|
|
|
dbg("promote refresh to full refresh")
|
2014-11-28 20:12:54 +00:00
|
|
|
mode = "full"
|
|
|
|
end
|
2014-12-04 07:46:47 +00:00
|
|
|
self.refresh_counted = true
|
2014-11-28 20:12:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- if no region is specified, define default region
|
|
|
|
region = region or Geom:new{w=Screen:getWidth(), h=Screen:getHeight()}
|
|
|
|
|
|
|
|
for i = 1, #self._refresh_stack do
|
|
|
|
-- check for collision with updates that are already enqueued
|
|
|
|
if region:intersectWith(self._refresh_stack[i].region) then
|
|
|
|
-- combine both refreshes' regions
|
|
|
|
local combined = region:combine(self._refresh_stack[i].region)
|
|
|
|
-- update the mode, if needed
|
2016-02-16 07:10:07 +00:00
|
|
|
mode = update_mode(mode, self._refresh_stack[i].mode)
|
2014-11-28 20:12:54 +00:00
|
|
|
-- remove colliding update
|
|
|
|
table.remove(self._refresh_stack, i)
|
|
|
|
-- and try again with combined data
|
|
|
|
return self:_refresh(mode, combined)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
-- if we hit no (more) collides, enqueue the update
|
|
|
|
table.insert(self._refresh_stack, {mode = mode, region = region})
|
|
|
|
end
|
|
|
|
|
2014-11-14 08:09:50 +00:00
|
|
|
-- repaint dirty widgets
|
2014-11-28 20:12:54 +00:00
|
|
|
function UIManager:_repaint()
|
2014-11-20 19:24:02 +00:00
|
|
|
-- flag in which we will record if we did any repaints at all
|
|
|
|
-- will trigger a refresh if set.
|
2014-11-14 08:09:50 +00:00
|
|
|
local dirty = false
|
2014-11-20 19:24:02 +00:00
|
|
|
|
2014-11-14 08:09:50 +00:00
|
|
|
for _, widget in ipairs(self._window_stack) do
|
2014-11-28 20:12:54 +00:00
|
|
|
-- paint if current widget or any widget underneath is dirty
|
|
|
|
if dirty or self._dirty[widget.widget] then
|
|
|
|
-- pass hint to widget that we got when setting widget dirty
|
|
|
|
-- the widget can use this to decide which parts should be refreshed
|
|
|
|
widget.widget:paintTo(Screen.bb, widget.x, widget.y, self._dirty[widget.widget])
|
2014-11-20 19:24:02 +00:00
|
|
|
|
2014-11-14 08:09:50 +00:00
|
|
|
-- and remove from list after painting
|
|
|
|
self._dirty[widget.widget] = nil
|
2014-11-20 19:24:02 +00:00
|
|
|
|
2014-11-14 08:09:50 +00:00
|
|
|
-- trigger repaint
|
|
|
|
dirty = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-11-28 20:12:54 +00:00
|
|
|
-- execute pending refresh functions
|
|
|
|
for _, refreshfunc in ipairs(self._refresh_func_stack) do
|
|
|
|
local refreshtype, region = refreshfunc()
|
|
|
|
if refreshtype then self:_refresh(refreshtype, region) end
|
|
|
|
end
|
|
|
|
self._refresh_func_stack = {}
|
|
|
|
|
|
|
|
-- we should have at least one refresh if we did repaint.
|
|
|
|
-- If we don't, we add one now and print a warning if we
|
|
|
|
-- are debugging
|
|
|
|
if dirty and #self._refresh_stack == 0 then
|
2016-03-29 06:37:15 +00:00
|
|
|
dbg("WARNING: no refresh got enqueued. Will do a partial full screen refresh, which might be inefficient")
|
2014-11-28 20:12:54 +00:00
|
|
|
self:_refresh("partial")
|
|
|
|
end
|
2014-11-20 19:24:02 +00:00
|
|
|
|
2014-11-28 20:12:54 +00:00
|
|
|
-- execute refreshes:
|
|
|
|
for _, refresh in ipairs(self._refresh_stack) do
|
2016-03-29 06:37:15 +00:00
|
|
|
dbg("triggering refresh", refresh)
|
2014-11-28 20:12:54 +00:00
|
|
|
Screen[refresh_methods[refresh.mode]](Screen,
|
|
|
|
refresh.region.x - 1, refresh.region.y - 1,
|
|
|
|
refresh.region.w + 2, refresh.region.h + 2)
|
2014-11-14 08:09:50 +00:00
|
|
|
end
|
2014-11-28 20:12:54 +00:00
|
|
|
self._refresh_stack = {}
|
2014-12-04 07:46:47 +00:00
|
|
|
self.refresh_counted = false
|
2014-11-14 08:09:50 +00:00
|
|
|
end
|
|
|
|
|
2015-03-21 05:18:34 +00:00
|
|
|
function UIManager:setInputTimeout(timeout)
|
|
|
|
self.INPUT_TIMEOUT = timeout or 200*1000
|
|
|
|
end
|
|
|
|
|
|
|
|
function UIManager:resetInputTimeout()
|
|
|
|
self.INPUT_TIMEOUT = nil
|
|
|
|
end
|
|
|
|
|
2015-03-02 09:15:26 +00:00
|
|
|
function UIManager:handleInput()
|
|
|
|
local wait_until, now
|
|
|
|
-- run this in a loop, so that paints can trigger events
|
|
|
|
-- that will be honored when calculating the time to wait
|
|
|
|
-- for input events:
|
|
|
|
repeat
|
|
|
|
wait_until, now = self:_checkTasks()
|
2016-03-29 06:37:15 +00:00
|
|
|
--dbg("---------------------------------------------------")
|
|
|
|
--dbg("exec stack", self._task_queue)
|
|
|
|
--dbg("window stack", self._window_stack)
|
|
|
|
--dbg("dirty stack", self._dirty)
|
|
|
|
--dbg("---------------------------------------------------")
|
2015-03-02 09:15:26 +00:00
|
|
|
|
|
|
|
-- stop when we have no window to show
|
|
|
|
if #self._window_stack == 0 and not self._run_forever then
|
2016-03-29 06:37:15 +00:00
|
|
|
dbg("no dialog left to show")
|
2015-03-02 09:15:26 +00:00
|
|
|
self:quit()
|
|
|
|
return nil
|
|
|
|
end
|
2012-04-22 19:29:48 +00:00
|
|
|
|
2015-03-02 09:15:26 +00:00
|
|
|
self:_repaint()
|
2015-12-26 00:48:40 +00:00
|
|
|
until not self._task_queue_dirty
|
2015-03-02 09:15:26 +00:00
|
|
|
|
|
|
|
-- wait for next event
|
|
|
|
-- note that we will skip that if we have tasks that are ready to run
|
|
|
|
local input_event = nil
|
|
|
|
if not wait_until then
|
|
|
|
if #self._zeromqs > 0 then
|
|
|
|
-- pending message queue, wait 100ms for input
|
|
|
|
input_event = Input:waitEvent(1000*100)
|
|
|
|
if not input_event or input_event.handler == "onInputError" then
|
|
|
|
for _, zeromq in ipairs(self._zeromqs) do
|
|
|
|
input_event = zeromq:waitEvent()
|
|
|
|
if input_event then break end
|
2014-06-23 14:33:11 +00:00
|
|
|
end
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
2015-03-02 09:15:26 +00:00
|
|
|
else
|
|
|
|
-- no pending task, wait without timeout
|
|
|
|
input_event = Input:waitEvent(self.INPUT_TIMEOUT)
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
2015-03-02 09:15:26 +00:00
|
|
|
elseif wait_until[1] > now[1]
|
|
|
|
or wait_until[1] == now[1] and wait_until[2] > now[2] then
|
|
|
|
local wait_for = { s = wait_until[1] - now[1], us = wait_until[2] - now[2] }
|
|
|
|
if wait_for.us < 0 then
|
|
|
|
wait_for.s = wait_for.s - 1
|
2015-12-26 00:48:40 +00:00
|
|
|
wait_for.us = MILLION + wait_for.us
|
2015-03-02 09:15:26 +00:00
|
|
|
end
|
|
|
|
-- wait until next task is pending
|
|
|
|
input_event = Input:waitEvent(wait_for.us, wait_for.s)
|
|
|
|
end
|
2012-04-22 19:29:48 +00:00
|
|
|
|
2015-03-02 09:15:26 +00:00
|
|
|
-- delegate input_event to handler
|
|
|
|
if input_event then
|
2016-03-29 07:56:29 +00:00
|
|
|
self:_resetAutoSuspendTimer()
|
2015-03-02 09:15:26 +00:00
|
|
|
local handler = self.event_handlers[input_event]
|
|
|
|
if handler then
|
|
|
|
handler(input_event)
|
|
|
|
else
|
|
|
|
self.event_handlers["__default__"](input_event)
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
|
|
|
end
|
2015-03-02 09:15:26 +00:00
|
|
|
|
|
|
|
if self.looper then
|
2016-03-29 06:37:15 +00:00
|
|
|
dbg("handle input in turbo I/O looper")
|
2015-03-21 05:18:34 +00:00
|
|
|
self.looper:add_callback(function()
|
|
|
|
-- FIXME: force close looper when there is unhandled error,
|
|
|
|
-- otherwise the looper will hang. Any better solution?
|
|
|
|
xpcall(function() self:handleInput() end, function(err)
|
|
|
|
io.stderr:write(err .. "\n")
|
|
|
|
io.stderr:write(debug.traceback() .. "\n")
|
|
|
|
io.stderr:flush()
|
|
|
|
self.looper:close()
|
|
|
|
os.exit(1)
|
|
|
|
end)
|
|
|
|
end)
|
2015-03-02 09:15:26 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function UIManager:initLooper()
|
2015-06-02 05:07:17 +00:00
|
|
|
if DUSE_TURBO_LIB and not self.looper then
|
2016-02-16 17:58:24 +00:00
|
|
|
TURBO_SSL = true -- luacheck: ignore
|
|
|
|
__TURBO_USE_LUASOCKET__ = true -- luacheck: ignore
|
2015-03-02 09:15:26 +00:00
|
|
|
local turbo = require("turbo")
|
|
|
|
self.looper = turbo.ioloop.instance()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- this is the main loop of the UI controller
|
|
|
|
-- it is intended to manage input events and delegate
|
|
|
|
-- them to dialogs
|
|
|
|
function UIManager:run()
|
|
|
|
self._running = true
|
2015-03-20 02:14:13 +00:00
|
|
|
self:initLooper()
|
2015-04-28 03:06:00 +00:00
|
|
|
-- currently there is no Turbo support for Windows
|
2015-03-20 02:14:13 +00:00
|
|
|
-- use our own main loop
|
|
|
|
if not self.looper then
|
|
|
|
while self._running do
|
|
|
|
self:handleInput()
|
|
|
|
end
|
2015-03-02 09:15:26 +00:00
|
|
|
else
|
2015-03-20 02:14:13 +00:00
|
|
|
self.looper:add_callback(function() self:handleInput() end)
|
2015-03-02 09:15:26 +00:00
|
|
|
self.looper:start()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- run uimanager forever for testing purpose
|
|
|
|
function UIManager:runForever()
|
|
|
|
self._run_forever = true
|
|
|
|
self:run()
|
2012-04-22 19:29:48 +00:00
|
|
|
end
|
2013-10-18 20:38:07 +00:00
|
|
|
|
2016-03-18 23:33:14 +00:00
|
|
|
-- Kobo does not have an auto suspend function, so we implement it ourselves.
|
|
|
|
function UIManager:_initAutoSuspend()
|
2016-03-29 07:56:29 +00:00
|
|
|
local function isAutoSuspendEnabled()
|
|
|
|
return Device:isKobo() and self.auto_suspend_sec > 0
|
|
|
|
end
|
|
|
|
|
2016-03-18 23:33:14 +00:00
|
|
|
local sec = G_reader_settings:readSetting("auto_suspend_timeout_seconds")
|
|
|
|
if sec then
|
|
|
|
self.auto_suspend_sec = sec
|
|
|
|
else
|
|
|
|
-- default setting is 60 minutes
|
|
|
|
self.auto_suspend_sec = 60 * 60
|
|
|
|
end
|
2016-03-29 07:56:29 +00:00
|
|
|
|
|
|
|
if isAutoSuspendEnabled() then
|
2016-03-18 23:33:14 +00:00
|
|
|
self.auto_suspend_action = function()
|
|
|
|
local now = util.gettime()
|
|
|
|
-- Do not repeat auto suspend procedure after suspend.
|
|
|
|
if self.last_action_sec + self.auto_suspend_sec <= now then
|
|
|
|
Device:onPowerEvent("Suspend")
|
|
|
|
else
|
|
|
|
self:scheduleIn(
|
|
|
|
self.last_action_sec + self.auto_suspend_sec - now,
|
|
|
|
self.auto_suspend_action)
|
|
|
|
end
|
|
|
|
end
|
2016-03-29 07:56:29 +00:00
|
|
|
|
|
|
|
function UIManager:_startAutoSuspend()
|
|
|
|
self.last_action_sec = util.gettime()
|
|
|
|
self:nextTick(self.auto_suspend_action)
|
|
|
|
end
|
|
|
|
dbg:guard(UIManager, '_startAutoSuspend',
|
|
|
|
function()
|
|
|
|
assert(isAutoSuspendEnabled())
|
|
|
|
end)
|
|
|
|
|
|
|
|
function UIManager:_stopAutoSuspend()
|
|
|
|
self:unschedule(self.auto_suspend_action)
|
|
|
|
end
|
|
|
|
|
|
|
|
function UIManager:_resetAutoSuspendTimer()
|
|
|
|
self.last_action_sec = util.gettime()
|
|
|
|
end
|
|
|
|
|
2016-03-18 23:33:14 +00:00
|
|
|
self:_startAutoSuspend()
|
2016-03-29 07:56:29 +00:00
|
|
|
else
|
|
|
|
self._startAutoSuspend = noop
|
|
|
|
self._stopAutoSuspend = noop
|
2016-03-18 23:33:14 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-29 07:56:29 +00:00
|
|
|
UIManager._resetAutoSuspendTimer = noop
|
2016-03-18 23:33:14 +00:00
|
|
|
|
2014-06-26 17:37:33 +00:00
|
|
|
UIManager:init()
|
2013-10-18 20:38:07 +00:00
|
|
|
return UIManager
|