2013-10-18 20:38:07 +00:00
|
|
|
local Device = require("ui/device")
|
|
|
|
local Screen = require("ui/screen")
|
|
|
|
local Input = require("ui/input")
|
|
|
|
local Event = require("ui/event")
|
2013-10-22 18:51:29 +00:00
|
|
|
local DEBUG = require("dbg")
|
2013-10-24 13:45:02 +00:00
|
|
|
local _ = require("gettext")
|
2012-04-22 19:29:48 +00:00
|
|
|
|
2013-02-02 06:36:29 +00:00
|
|
|
-- initialize output module, this must be initialized before Input
|
|
|
|
Screen:init()
|
2012-06-10 15:36:19 +00:00
|
|
|
-- initialize the input handling
|
2012-04-22 19:29:48 +00:00
|
|
|
Input:init()
|
|
|
|
|
2013-10-18 20:38:07 +00:00
|
|
|
local WAVEFORM_MODE_INIT = 0x0 -- Screen goes to white (clears)
|
|
|
|
local WAVEFORM_MODE_DU = 0x1 -- Grey->white/grey->black
|
|
|
|
local WAVEFORM_MODE_GC16 = 0x2 -- High fidelity (flashing)
|
|
|
|
local WAVEFORM_MODE_GC4 = WAVEFORM_MODE_GC16 -- For compatibility
|
|
|
|
local WAVEFORM_MODE_GC16_FAST = 0x3 -- Medium fidelity
|
|
|
|
local WAVEFORM_MODE_A2 = 0x4 -- Faster but even lower fidelity
|
|
|
|
local WAVEFORM_MODE_GL16 = 0x5 -- High fidelity from white transition
|
|
|
|
local WAVEFORM_MODE_GL16_FAST = 0x6 -- Medium fidelity from white transition
|
|
|
|
local WAVEFORM_MODE_AUTO = 0x101
|
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 = {
|
2013-11-27 13:38:36 +00:00
|
|
|
default_refresh_type = 0, -- 0 for partial refresh, 1 for full refresh
|
|
|
|
default_waveform_mode = WAVEFORM_MODE_GC16, -- high fidelity waveform
|
2013-07-23 06:51:38 +00:00
|
|
|
fast_waveform_mode = WAVEFORM_MODE_A2,
|
2012-12-07 15:29:24 +00:00
|
|
|
-- force to repaint all the widget is stack, will be reset to false
|
|
|
|
-- after each ui loop
|
|
|
|
repaint_all = false,
|
2013-03-07 10:05:22 +00:00
|
|
|
-- force to do full refresh, will be reset to false
|
|
|
|
-- after each ui loop
|
|
|
|
full_refresh = false,
|
2013-07-22 14:04:54 +00:00
|
|
|
-- force to do patial refresh, will be reset to false
|
|
|
|
-- after each ui loop
|
|
|
|
patial_refresh = false,
|
2013-02-24 12:48:42 +00:00
|
|
|
-- trigger a full refresh when counter reaches FULL_REFRESH_COUNT
|
2013-07-01 06:41:33 +00:00
|
|
|
FULL_REFRESH_COUNT = DRCOUNTMAX,
|
2013-02-24 12:48:42 +00:00
|
|
|
refresh_count = 0,
|
2013-03-12 17:18:53 +00:00
|
|
|
|
2012-04-22 19:29:48 +00:00
|
|
|
_running = true,
|
|
|
|
_window_stack = {},
|
|
|
|
_execution_stack = {},
|
|
|
|
_dirty = {}
|
|
|
|
}
|
|
|
|
|
|
|
|
-- register & show a widget
|
|
|
|
function UIManager:show(widget, x, y)
|
|
|
|
-- put widget on top of stack
|
|
|
|
table.insert(self._window_stack, {x = x or 0, y = y or 0, widget = widget})
|
|
|
|
-- and schedule it to be painted
|
|
|
|
self:setDirty(widget)
|
|
|
|
-- tell the widget that it is shown now
|
|
|
|
widget:handleEvent(Event:new("Show"))
|
2013-07-31 12:02:26 +00:00
|
|
|
-- 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
|
|
|
|
function UIManager:close(widget)
|
2013-07-31 12:02:26 +00:00
|
|
|
Input.disable_double_tap = DGESDETECT_DISABLE_DOUBLE_TAP
|
2012-04-22 19:29:48 +00:00
|
|
|
local dirty = false
|
|
|
|
for i = #self._window_stack, 1, -1 do
|
|
|
|
if self._window_stack[i].widget == widget then
|
|
|
|
table.remove(self._window_stack, i)
|
|
|
|
dirty = true
|
2013-07-31 12:02:26 +00:00
|
|
|
elseif self._window_stack[i].widget.disable_double_tap then
|
|
|
|
Input.disable_double_tap = true
|
2012-04-22 19:29:48 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
if dirty then
|
|
|
|
-- schedule remaining widgets to be painted
|
|
|
|
for i = 1, #self._window_stack do
|
|
|
|
self:setDirty(self._window_stack[i].widget)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- schedule an execution task
|
|
|
|
function UIManager:schedule(time, action)
|
|
|
|
table.insert(self._execution_stack, { time = time, action = action })
|
|
|
|
end
|
|
|
|
|
|
|
|
-- schedule task in a certain amount of seconds (fractions allowed) from now
|
|
|
|
function UIManager:scheduleIn(seconds, action)
|
|
|
|
local when = { util.gettime() }
|
|
|
|
local s = math.floor(seconds)
|
|
|
|
local usecs = (seconds - s) * 1000000
|
|
|
|
when[1] = when[1] + s
|
|
|
|
when[2] = when[2] + usecs
|
|
|
|
if when[2] > 1000000 then
|
|
|
|
when[1] = when[1] + 1
|
|
|
|
when[2] = when[2] - 1000000
|
|
|
|
end
|
|
|
|
self:schedule(when, action)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- register a widget to be repainted
|
2012-06-19 02:44:54 +00:00
|
|
|
function UIManager:setDirty(widget, refresh_type)
|
2013-03-06 14:24:48 +00:00
|
|
|
-- "auto": request full refresh
|
|
|
|
-- "full": force full refresh
|
|
|
|
-- "partial": partial refresh
|
2012-06-19 02:44:54 +00:00
|
|
|
if not refresh_type then
|
2013-03-06 14:24:48 +00:00
|
|
|
refresh_type = "auto"
|
2012-06-19 02:44:54 +00:00
|
|
|
end
|
|
|
|
self._dirty[widget] = refresh_type
|
2012-04-22 19:29:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- signal to quit
|
|
|
|
function UIManager:quit()
|
|
|
|
self._running = false
|
|
|
|
end
|
|
|
|
|
|
|
|
-- transmit an event to registered widgets
|
|
|
|
function UIManager:sendEvent(event)
|
|
|
|
-- top level widget has first access to the event
|
2013-03-07 10:05:22 +00:00
|
|
|
if self._window_stack[#self._window_stack].widget:handleEvent(event) then
|
|
|
|
return
|
|
|
|
end
|
2012-04-22 19:29:48 +00:00
|
|
|
|
2013-03-07 10:05:22 +00:00
|
|
|
-- if the event is not consumed, active widgets can access it
|
2012-04-22 19:29:48 +00:00
|
|
|
for _, widget in ipairs(self._window_stack) do
|
|
|
|
if widget.widget.is_always_active then
|
2013-03-07 10:05:22 +00:00
|
|
|
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
|
2012-04-22 19:29:48 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-05 14:28:14 +00:00
|
|
|
function UIManager:checkTasks()
|
|
|
|
local now = { util.gettime() }
|
|
|
|
|
|
|
|
-- check if we have timed events in our queue and search next one
|
|
|
|
local wait_until = nil
|
|
|
|
local all_tasks_checked
|
|
|
|
repeat
|
|
|
|
all_tasks_checked = true
|
|
|
|
for i = #self._execution_stack, 1, -1 do
|
|
|
|
local task = self._execution_stack[i]
|
|
|
|
if not task.time
|
|
|
|
or task.time[1] < now[1]
|
|
|
|
or task.time[1] == now[1] and task.time[2] < now[2] then
|
|
|
|
-- task is pending to be executed right now. do it.
|
|
|
|
task.action()
|
|
|
|
-- and remove from table
|
|
|
|
table.remove(self._execution_stack, i)
|
|
|
|
-- start loop again, since new tasks might be on the
|
|
|
|
-- queue now
|
|
|
|
all_tasks_checked = false
|
|
|
|
elseif not wait_until
|
|
|
|
or wait_until[1] > task.time[1]
|
|
|
|
or wait_until[1] == task.time[1] and wait_until[2] > task.time[2] then
|
|
|
|
-- task is to be run in the future _and_ is scheduled
|
|
|
|
-- earlier than the tasks we looked at already
|
|
|
|
-- so adjust to the currently examined task instead.
|
|
|
|
wait_until = task.time
|
|
|
|
end
|
|
|
|
end
|
|
|
|
until all_tasks_checked
|
|
|
|
return wait_until
|
|
|
|
end
|
|
|
|
|
2012-04-22 19:29:48 +00:00
|
|
|
-- 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
|
|
|
|
while self._running do
|
|
|
|
local now = { util.gettime() }
|
2013-01-05 14:28:14 +00:00
|
|
|
local wait_until = self:checkTasks()
|
2013-03-12 17:18:53 +00:00
|
|
|
|
2012-05-28 16:59:16 +00:00
|
|
|
--DEBUG("---------------------------------------------------")
|
|
|
|
--DEBUG("exec stack", self._execution_stack)
|
|
|
|
--DEBUG("window stack", self._window_stack)
|
|
|
|
--DEBUG("dirty stack", self._dirty)
|
|
|
|
--DEBUG("---------------------------------------------------")
|
2012-04-22 19:29:48 +00:00
|
|
|
|
2013-01-09 05:12:37 +00:00
|
|
|
-- stop when we have no window to show
|
2012-04-22 19:29:48 +00:00
|
|
|
if #self._window_stack == 0 then
|
2013-01-09 05:12:37 +00:00
|
|
|
DEBUG("no dialog left to show, would loop endlessly")
|
|
|
|
return nil
|
2012-04-22 19:29:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- repaint dirty widgets
|
|
|
|
local dirty = false
|
2013-03-06 14:24:48 +00:00
|
|
|
local request_full_refresh = false
|
|
|
|
local force_full_refresh = false
|
2013-07-22 14:04:54 +00:00
|
|
|
local force_patial_refresh = false
|
2013-07-23 06:51:38 +00:00
|
|
|
local force_fast_refresh = false
|
2012-04-22 19:29:48 +00:00
|
|
|
for _, widget in ipairs(self._window_stack) do
|
2012-12-07 15:29:24 +00:00
|
|
|
if self.repaint_all or self._dirty[widget.widget] then
|
2013-02-02 06:36:29 +00:00
|
|
|
widget.widget:paintTo(Screen.bb, widget.x, widget.y)
|
2013-03-06 14:24:48 +00:00
|
|
|
if self._dirty[widget.widget] == "auto" then
|
|
|
|
request_full_refresh = true
|
2012-06-19 02:44:54 +00:00
|
|
|
end
|
2013-03-06 14:24:48 +00:00
|
|
|
if self._dirty[widget.widget] == "full" then
|
|
|
|
force_full_refresh = true
|
2013-03-12 17:18:53 +00:00
|
|
|
end
|
2013-07-23 09:10:29 +00:00
|
|
|
if self._dirty[widget.widget] == "partial" then
|
2013-07-22 14:04:54 +00:00
|
|
|
force_patial_refresh = true
|
|
|
|
end
|
2013-07-23 06:51:38 +00:00
|
|
|
if self._dirty[widget.widget] == "fast" then
|
|
|
|
force_fast_refresh = true
|
|
|
|
end
|
2012-04-22 19:29:48 +00:00
|
|
|
-- and remove from list after painting
|
|
|
|
self._dirty[widget.widget] = nil
|
|
|
|
-- trigger repaint
|
|
|
|
dirty = true
|
|
|
|
end
|
|
|
|
end
|
2013-07-23 06:51:38 +00:00
|
|
|
|
2013-03-07 10:05:22 +00:00
|
|
|
if self.full_refresh then
|
|
|
|
dirty = true
|
|
|
|
force_full_refresh = true
|
|
|
|
end
|
2013-07-23 06:51:38 +00:00
|
|
|
|
2013-07-22 14:04:54 +00:00
|
|
|
if self.patial_refresh then
|
|
|
|
dirty = true
|
|
|
|
force_patial_refresh = true
|
|
|
|
end
|
2013-07-23 06:51:38 +00:00
|
|
|
|
2012-12-07 15:29:24 +00:00
|
|
|
self.repaint_all = false
|
2013-03-07 10:05:22 +00:00
|
|
|
self.full_refresh = false
|
2013-07-22 14:04:54 +00:00
|
|
|
self.patial_refresh = false
|
2013-07-23 06:51:38 +00:00
|
|
|
|
|
|
|
local refresh_type = self.default_refresh_type
|
|
|
|
local waveform_mode = self.default_waveform_mode
|
2012-04-22 19:29:48 +00:00
|
|
|
if dirty then
|
2013-07-23 06:51:38 +00:00
|
|
|
if force_patial_refresh or force_fast_refresh then
|
|
|
|
refresh_type = 0
|
2013-11-27 13:38:36 +00:00
|
|
|
elseif force_full_refresh or self.refresh_count == self.FULL_REFRESH_COUNT - 1 then
|
|
|
|
refresh_type = 1
|
2013-07-23 06:51:38 +00:00
|
|
|
end
|
|
|
|
if force_fast_refresh then
|
2013-07-31 08:55:50 +00:00
|
|
|
waveform_mode = self.fast_waveform_mode
|
2013-02-24 12:48:42 +00:00
|
|
|
end
|
2013-07-22 14:04:54 +00:00
|
|
|
if self.update_region_func then
|
|
|
|
local update_region = self.update_region_func()
|
2013-10-16 12:52:32 +00:00
|
|
|
-- in some rare cases update region has 1 pixel offset
|
2013-07-23 06:51:38 +00:00
|
|
|
Screen:refresh(refresh_type, waveform_mode,
|
2013-10-16 12:52:32 +00:00
|
|
|
update_region.x-1, update_region.y-1,
|
|
|
|
update_region.w+2, update_region.h+2)
|
2013-07-22 14:04:54 +00:00
|
|
|
else
|
2013-07-23 06:51:38 +00:00
|
|
|
Screen:refresh(refresh_type, waveform_mode)
|
2013-07-22 14:04:54 +00:00
|
|
|
end
|
2013-11-27 13:38:36 +00:00
|
|
|
if self.refresh_type == 1 then
|
2013-07-22 14:04:54 +00:00
|
|
|
self.refresh_count = 0
|
|
|
|
elseif not force_patial_refresh and not force_full_refresh then
|
|
|
|
self.refresh_count = (self.refresh_count + 1)%self.FULL_REFRESH_COUNT
|
|
|
|
end
|
|
|
|
self.update_region_func = nil
|
2012-04-22 19:29:48 +00:00
|
|
|
end
|
2013-03-12 17:18:53 +00:00
|
|
|
|
2013-01-05 14:28:14 +00:00
|
|
|
self:checkTasks()
|
2013-03-12 17:18:53 +00:00
|
|
|
|
2012-04-22 19:29:48 +00:00
|
|
|
-- wait for next event
|
|
|
|
-- note that we will skip that if in the meantime we have tasks that are ready to run
|
|
|
|
local input_event = nil
|
|
|
|
if not wait_until then
|
|
|
|
-- no pending task, wait endlessly
|
|
|
|
input_event = Input:waitEvent()
|
|
|
|
elseif wait_until[1] > now[1]
|
|
|
|
or wait_until[1] == now[1] and wait_until[2] > now[2] then
|
2012-04-23 20:28:22 +00:00
|
|
|
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
|
|
|
|
wait_for.us = 1000000 + wait_for.us
|
|
|
|
end
|
2012-04-22 19:29:48 +00:00
|
|
|
-- wait until next task is pending
|
2012-04-23 20:28:22 +00:00
|
|
|
input_event = Input:waitEvent(wait_for.us, wait_for.s)
|
2012-04-22 19:29:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- delegate input_event to handler
|
|
|
|
if input_event then
|
2013-02-24 19:17:54 +00:00
|
|
|
--DEBUG("in ui.lua:", input_event)
|
2013-01-10 06:18:33 +00:00
|
|
|
if input_event == "IntoSS" then
|
|
|
|
Device:intoScreenSaver()
|
|
|
|
elseif input_event == "OutOfSS" then
|
|
|
|
Device:outofScreenSaver()
|
|
|
|
elseif input_event == "Charging" then
|
|
|
|
Device:usbPlugIn()
|
|
|
|
elseif input_event == "NotCharging" then
|
|
|
|
Device:usbPlugOut()
|
2013-08-12 05:11:54 +00:00
|
|
|
elseif input_event == "Light" then
|
|
|
|
Device:getFrontlight():toggle()
|
2013-09-30 19:48:46 +00:00
|
|
|
elseif (input_event == "Power" and not Device.screen_saver_mode) or
|
|
|
|
input_event == "Suspend" then
|
2013-10-24 12:54:55 +00:00
|
|
|
local InfoMessage = require("ui/widget/infomessage")
|
2013-10-18 20:38:07 +00:00
|
|
|
self:show(InfoMessage:new{
|
2013-09-30 19:48:46 +00:00
|
|
|
text = _("Standby"),
|
|
|
|
timeout = 1,
|
|
|
|
})
|
|
|
|
Device:prepareSuspend()
|
2013-10-18 20:38:07 +00:00
|
|
|
self:scheduleIn(0.5, function() Device:Suspend() end)
|
2013-09-30 19:48:46 +00:00
|
|
|
elseif (input_event == "Power" and Device.screen_saver_mode) or
|
|
|
|
input_event == "Resume" then
|
2013-09-09 07:04:09 +00:00
|
|
|
Device:Resume()
|
2013-01-10 06:18:33 +00:00
|
|
|
else
|
|
|
|
self:sendEvent(input_event)
|
|
|
|
end
|
2012-04-22 19:29:48 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-10-18 20:38:07 +00:00
|
|
|
|
|
|
|
return UIManager
|