diff --git a/defaults.lua b/defaults.lua index b1200b5a2..865788d5a 100644 --- a/defaults.lua +++ b/defaults.lua @@ -178,8 +178,6 @@ KOBO_LIGHT_ON_START = -2 -- -1, -2 or 0-100. -1 leaves light as it -- sets light on start/wake up KOBO_SYNC_BRIGHTNESS_WITH_NICKEL = true -- Save brightness set in KOreader -- with nickel's 'Kobo eReader.conf' -KOBO_SCREEN_SAVER = "" -- image or directory with pictures or "-" -KOBO_SCREEN_SAVER_LAST_BOOK = true -- get screensaver from last book if possible -- Network proxy settings -- proxy url should be a string in the format of "http://localhost:3128" diff --git a/frontend/device/kobo/device.lua b/frontend/device/kobo/device.lua index d0604ea63..bf8f5b580 100644 --- a/frontend/device/kobo/device.lua +++ b/frontend/device/kobo/device.lua @@ -15,6 +15,8 @@ local Kobo = Generic:new{ touch_mirrored_x = true, -- enforce protrait mode on Kobos: isAlwaysPortrait = yes, + -- the internal storage mount point users can write to + internal_storage_mount_point = "/mnt/onboard/" } -- TODO: hasKeys for some devices? diff --git a/frontend/ui/screensaver.lua b/frontend/ui/screensaver.lua index b219eafa6..ac029258f 100644 --- a/frontend/ui/screensaver.lua +++ b/frontend/ui/screensaver.lua @@ -1,6 +1,7 @@ local DocumentRegistry = require("document/documentregistry") local UIManager = require("ui/uimanager") -local Screen = require("device").screen +local Device = require("device") +local Screen = Device.screen local DocSettings = require("docsettings") local DEBUG = require("dbg") local _ = require("gettext") @@ -8,85 +9,74 @@ local _ = require("gettext") local Screensaver = { } -function Screensaver:getCoverImage(file) - local ImageWidget = require("ui/widget/imagewidget") - local CenterContainer = require("ui/widget/container/centercontainer") - local FrameContainer = require("ui/widget/container/framecontainer") - local AlphaContainer = require("ui/widget/container/alphacontainer") - local image_height - local image_width - local screen_height = Screen:getHeight() - local screen_width = Screen:getWidth() - local doc = DocumentRegistry:openDocument(file) - if doc then - local image = doc:getCoverPageImage() - doc:close() - local lastfile = G_reader_settings:readSetting("lastfile") - local data = DocSettings:open(lastfile) - local proportional_cover = data:readSetting("proportional_screensaver") - if image then - if proportional_cover then - image_height = image:getHeight() - image_width = image:getWidth() - local image_ratio = image_width / image_height - if image_ratio < 1 then - image_height = screen_height - image_width = image_height * image_ratio - else - image_width = screen_width - image_height = image_width / image_ratio - end - else - image_height = screen_height - image_width = screen_width - end - local image_widget = ImageWidget:new{ - image = image, - width = image_width, - height = image_height, - } - return AlphaContainer:new{ - alpha = 1, - height = screen_height, - width = screen_width, - CenterContainer:new{ - dimen = Screen:getSize(), - FrameContainer:new{ - bordersize = 0, - padding = 0, - height = screen_height, - width = screen_width, - image_widget - } - } +local function createWidgetFromImage(image_widget) + if image_widget then + local AlphaContainer = require("ui/widget/container/alphacontainer") + local CenterContainer = require("ui/widget/container/centercontainer") + return AlphaContainer:new{ + alpha = 1, + height = Screen:getHeight(), + width = Screen:getWidth(), + CenterContainer:new{ + dimen = Screen:getSize(), + image_widget, } - end + } end end -function Screensaver:getRandomImage(dir) - local ImageWidget = require("ui/widget/imagewidget") +local function createWidgetFromFile(file) + if lfs.attributes(file, "mode") == "file" then + local ImageWidget = require("ui/widget/imagewidget") + return createWidgetFromImage( + ImageWidget:new{ + file = file, + height = Screen:getHeight(), + width = Screen:getWidth(), + autostretch = true, + }) + end +end + +local function getRandomImage(dir) + if string.sub(dir, string.len(dir)) ~= "/" then + dir = dir .. "/" + end local pics = {} local i = 0 math.randomseed(os.time()) for entry in lfs.dir(dir) do if lfs.attributes(dir .. entry, "mode") == "file" then - local extension = string.lower(string.match(entry, ".+%.([^.]+)") or "") - if extension == "jpg" or extension == "jpeg" or extension == "png" then + local extension = + string.lower(string.match(entry, ".+%.([^.]+)") or "") + if extension == "jpg" + or extension == "jpeg" + or extension == "png" then i = i + 1 pics[i] = entry end end end - local image = pics[math.random(i)] - if image then - image = dir .. image - if lfs.attributes(image, "mode") == "file" then - return ImageWidget:new{ - file = image, - width = Screen:getWidth(), - height = Screen:getHeight(), - } + return createWidgetFromFile(dir .. pics[math.random(i)]) +end + +function Screensaver:getCoverImage(file) + local ImageWidget = require("ui/widget/imagewidget") + local doc = DocumentRegistry:openDocument(file) + if doc then + local image = doc:getCoverPageImage() + doc:close() + local lastfile = G_reader_settings:readSetting("lastfile") + local data = DocSettings:open(lastfile) + local proportional_cover = data:readSetting("proportional_screensaver") + if image then + return createWidgetFromImage( + ImageWidget:new{ + image = image, + height = Screen:getHeight(), + width = Screen:getWidth(), + autostretch = proportional_cover, + }) end end end @@ -95,7 +85,9 @@ function Screensaver:show() DEBUG("show screensaver") local InfoMessage = require("ui/widget/infomessage") -- first check book cover image - if KOBO_SCREEN_SAVER_LAST_BOOK then + screen_saver_last_book = + G_reader_settings:readSetting("use_lastfile_as_screensaver") + if screen_saver_last_book == nil or screen_saver_last_book then local lastfile = G_reader_settings:readSetting("lastfile") if lastfile then local data = DocSettings:open(lastfile) @@ -107,20 +99,19 @@ function Screensaver:show() end -- then screensaver directory or file image if not self.suspend_msg then - if type(KOBO_SCREEN_SAVER) == "string" then - local file = KOBO_SCREEN_SAVER + local screen_saver_folder = + G_reader_settings:readSetting("screensaver_folder") + if screen_saver_folder == nil + and Device.internal_storage_mount_point ~= nil then + screen_saver_folder = + Device.internal_storage_mount_point .. "screensaver" + end + if screen_saver_folder then + local file = screen_saver_folder if lfs.attributes(file, "mode") == "directory" then - if string.sub(file,string.len(file)) ~= "/" then - file = file .. "/" - end - self.suspend_msg = self:getRandomImage(file) - elseif lfs.attributes(file, "mode") == "file" then - local ImageWidget = require("ui/widget/imagewidget") - self.suspend_msg = ImageWidget:new{ - file = file, - width = Screen:getWidth(), - height = Screen:getHeight(), - } + self.suspend_msg = getRandomImage(file) + else + self.suspend_msg = createWidgetFromFile(file) end end end diff --git a/frontend/ui/widget/imagewidget.lua b/frontend/ui/widget/imagewidget.lua index 5d93fa88a..d17511d46 100644 --- a/frontend/ui/widget/imagewidget.lua +++ b/frontend/ui/widget/imagewidget.lua @@ -39,6 +39,14 @@ local ImageWidget = Widget:new{ autoscale = false, -- when alpha is set to true, alpha values from the image will be honored alpha = false, + -- when autostretch is set to true, image will be stretched to best fit the + -- widget size. i.e. either fit the width or fit the height according to the + -- original image size. + autostretch = false, + -- when overflow is set to true, image will be stretched to fit the widget + -- size vertically and horizontally, without impact original aspect ratio. + -- But overflow part will be ignored. + overflow = false, _bb = nil } @@ -79,15 +87,35 @@ function ImageWidget:_render() error("cannot render image") end local native_w, native_h = self._bb:getWidth(), self._bb:getHeight() - local scaled_w, scaled_h = self.width, self.height + local w, h if self.autoscale then local dpi_scale = Screen:getDPI() / 167 -- rounding off to power of 2 to avoid alias with pow(2, floor(log(x)/log(2)) local scale = math.pow(2, math.max(0, math.floor(math.log(dpi_scale)/0.69))) - scaled_w, scaled_h = scale * native_w, scale * native_h + w, h = scale * native_w, scale * native_h + elseif self.width and self.height then + if self.autostretch then + local ratio = native_w / self.width / native_h * self.height + if ratio < 1 then + h = self.height + w = self.width * ratio + else + h = self.height * ratio + w = self.width + end + elseif self.overflow then + local ratio = native_w / self.width / native_h * self.height + if ratio < 1 then + h = self.height / ratio + w = self.width + else + h = self.height + w = self.width / ratio + end + end end - if (scaled_w and scaled_w ~= native_w) or (scaled_h and scaled_h ~= native_h) then - self._bb = self._bb:scale(scaled_w or native_w, scaled_h or native_h) + if (w and w ~= native_w) or (h and h ~= native_h) then + self._bb = self._bb:scale(w or native_w, h or native_h) end end