2
0
mirror of https://github.com/koreader/koreader synced 2024-11-16 06:12:56 +00:00
koreader/frontend/ui/device/screen.lua

162 lines
3.8 KiB
Lua
Raw Normal View History

local Geom = require("ui/geometry")
2013-10-22 18:51:29 +00:00
local DEBUG = require("dbg")
-- Blitbuffer
-- einkfb
--[[
Codes for rotation modes:
1 for no rotation,
2 for landscape with bottom on the right side of screen, etc.
2
+--------------+
| +----------+ |
| | | |
| | Freedom! | |
| | | |
| | | |
3 | | | | 1
| | | |
| | | |
| +----------+ |
| |
| |
+--------------+
0
--]]
local Screen = {
2014-03-13 13:52:43 +00:00
cur_rotation_mode = 0,
native_rotation_mode = nil,
blitbuffer_rotation_mode = 0,
2014-03-13 13:52:43 +00:00
bb = nil,
saved_bb = nil,
2014-03-13 13:52:43 +00:00
fb = einkfb.open("/dev/fb0"),
-- will be set upon loading by Device class:
device = nil,
}
function Screen:init()
2014-03-13 13:52:43 +00:00
self.bb = self.fb.bb
self.blitbuffer_rotation_mode = self.bb:getRotation()
-- asking the framebuffer for orientation is error prone,
-- so we do this simple heuristic (for now)
if self:getWidth() > self:getHeight() then
self.native_rotation_mode = 1
else
self.native_rotation_mode = 0
end
self.cur_rotation_mode = self.native_rotation_mode
end
function Screen:refresh(refresh_type, waveform_mode, x, y, w, h)
2014-03-13 13:52:43 +00:00
self.fb:refresh(refresh_type, waveform_mode, x, y, w, h)
end
function Screen:getSize()
2014-03-13 13:52:43 +00:00
return Geom:new{w = self.bb:getWidth(), h = self.bb:getHeight()}
end
function Screen:getWidth()
2014-03-13 13:52:43 +00:00
return self.bb:getWidth()
end
function Screen:getHeight()
2014-03-13 13:52:43 +00:00
return self.bb:getHeight()
end
function Screen:getDPI()
if self.dpi ~= nil then return self.dpi end
local model = self.device:getModel()
if model == "KindlePaperWhite" or model == "KindlePaperWhite2"
or model == "Kobo_kraken" or model == "Kobo_phoenix" then
self.dpi = 212
elseif model == "Kobo_dragon" then
self.dpi = 265
elseif model == "Kobo_pixie" then
self.dpi = 200
2014-03-13 13:52:43 +00:00
else
self.dpi = 167
2014-03-13 13:52:43 +00:00
end
return self.dpi
end
function Screen:scaleByDPI(px)
2014-03-13 13:52:43 +00:00
return math.floor(px * self:getDPI()/167)
end
function Screen:rescaleByDPI(px)
2014-03-13 13:52:43 +00:00
return math.ceil(px * 167/self:getDPI())
end
function Screen:getRotationMode()
2014-03-13 13:52:43 +00:00
return self.cur_rotation_mode
end
function Screen:getScreenMode()
2014-03-13 13:52:43 +00:00
if self:getWidth() > self:getHeight() then
return "landscape"
else
return "portrait"
end
end
function Screen:setRotationMode(mode)
2014-03-13 13:52:43 +00:00
self.fb.bb:rotateAbsolute(-90 * (mode - self.native_rotation_mode - self.blitbuffer_rotation_mode))
self.cur_rotation_mode = mode
end
function Screen:setScreenMode(mode)
2014-03-13 13:52:43 +00:00
if mode == "portrait" then
if self.cur_rotation_mode ~= 0 then
self:setRotationMode(0)
end
elseif mode == "landscape" then
if self.cur_rotation_mode == 0 or self.cur_rotation_mode == 2 then
self:setRotationMode(DLANDSCAPE_CLOCKWISE_ROTATION and 1 or 3)
elseif self.cur_rotation_mode == 1 or self.cur_rotation_mode == 3 then
self:setRotationMode((self.cur_rotation_mode + 2) % 4)
end
end
end
function Screen:saveCurrentBB()
2014-03-13 13:52:43 +00:00
local width, height = self:getWidth(), self:getHeight()
2014-03-13 13:52:43 +00:00
if not self.saved_bb then
self.saved_bb = Blitbuffer.new(width, height)
end
if self.saved_bb:getWidth() ~= width then
self.saved_bb:free()
self.saved_bb = Blitbuffer.new(width, height)
end
self.saved_bb:blitFullFrom(self.bb)
end
function Screen:restoreFromSavedBB()
2014-03-13 13:52:43 +00:00
self:restoreFromBB(self.saved_bb)
-- free data
self.saved_bb = nil
end
function Screen:getCurrentScreenBB()
2014-03-13 13:52:43 +00:00
local bb = Blitbuffer.new(self:getWidth(), self:getHeight())
bb:blitFullFrom(self.bb)
return bb
end
function Screen:restoreFromBB(bb)
2014-03-13 13:52:43 +00:00
if bb then
self.bb:blitFullFrom(bb)
else
DEBUG("Got nil bb in restoreFromSavedBB!")
end
end
return Screen