2011-11-14 17:30:16 +00:00
|
|
|
#!./kpdfview
|
|
|
|
--[[
|
|
|
|
KindlePDFViewer: a reader implementation
|
|
|
|
Copyright (C) 2011 Hans-Werner Hilse <hilse@web.de>
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
]]--
|
|
|
|
|
2011-11-16 16:13:08 +00:00
|
|
|
require "alt_getopt"
|
2011-12-07 23:45:39 +00:00
|
|
|
require "pdfreader"
|
2012-03-01 13:04:46 +00:00
|
|
|
require "djvureader"
|
2012-03-28 16:09:26 +00:00
|
|
|
require "crereader"
|
2011-12-08 00:17:33 +00:00
|
|
|
require "filechooser"
|
2012-02-23 16:30:02 +00:00
|
|
|
require "settings"
|
2012-03-09 01:40:46 +00:00
|
|
|
require "screen"
|
2012-03-13 21:51:25 +00:00
|
|
|
require "keys"
|
|
|
|
require "commands"
|
2012-04-15 00:34:09 +00:00
|
|
|
require "dialog"
|
2011-11-21 00:02:03 +00:00
|
|
|
|
2011-11-16 16:13:08 +00:00
|
|
|
-- option parsing:
|
|
|
|
longopts = {
|
|
|
|
password = "p",
|
2011-11-17 10:58:49 +00:00
|
|
|
goto = "g",
|
2011-11-16 16:13:08 +00:00
|
|
|
gamma = "G",
|
2012-04-19 03:05:48 +00:00
|
|
|
debug = "d",
|
2011-11-16 16:13:08 +00:00
|
|
|
help = "h"
|
|
|
|
}
|
2012-03-03 11:51:49 +00:00
|
|
|
|
|
|
|
function openFile(filename)
|
2012-03-19 21:35:16 +00:00
|
|
|
local file_type = string.lower(string.match(filename, ".+%.([^.]+)"))
|
2012-03-19 23:10:19 +00:00
|
|
|
local reader = nil
|
2012-03-03 11:51:49 +00:00
|
|
|
if file_type == "djvu" then
|
2012-03-19 23:10:19 +00:00
|
|
|
reader = DJVUReader
|
2012-03-19 21:35:16 +00:00
|
|
|
elseif file_type == "pdf" or file_type == "xps" or file_type == "cbz" then
|
2012-03-19 23:10:19 +00:00
|
|
|
reader = PDFReader
|
2012-04-24 12:44:59 +00:00
|
|
|
elseif file_type == "epub" or file_type == "txt" or file_type == "rtf" or file_type == "htm" or file_type == "html" or file_type == "fb2" or file_type == "chm" or file_type == "mobi" or file_type == "doc" or file_type == "zip" then
|
2012-03-28 16:09:26 +00:00
|
|
|
reader = CREReader
|
2012-03-19 23:10:19 +00:00
|
|
|
end
|
|
|
|
if reader then
|
2012-04-16 19:54:22 +00:00
|
|
|
InfoMessage:show("Opening document, please wait... ", 0)
|
2012-04-19 06:39:50 +00:00
|
|
|
reader:preLoadSettings(filename)
|
2012-03-19 23:10:19 +00:00
|
|
|
local ok, err = reader:open(filename)
|
|
|
|
if ok then
|
|
|
|
reader:loadSettings(filename)
|
2012-03-30 05:07:48 +00:00
|
|
|
page_num = reader:getLastPageOrPos()
|
2012-04-18 09:01:22 +00:00
|
|
|
reader:goto(tonumber(page_num), true)
|
2012-04-18 09:29:27 +00:00
|
|
|
reader_settings:saveSetting("lastfile", filename)
|
2012-03-20 19:15:24 +00:00
|
|
|
return reader:inputLoop()
|
2012-03-19 23:10:19 +00:00
|
|
|
else
|
2012-04-16 19:54:22 +00:00
|
|
|
InfoMessage:show("Error opening document.", 0)
|
2012-04-15 00:34:09 +00:00
|
|
|
util.sleep(2)
|
2012-03-03 11:51:49 +00:00
|
|
|
end
|
|
|
|
end
|
2012-03-19 23:10:19 +00:00
|
|
|
return true -- on failed attempts, we signal to keep running
|
2012-03-03 11:51:49 +00:00
|
|
|
end
|
|
|
|
|
2012-03-04 22:03:04 +00:00
|
|
|
function showusage()
|
|
|
|
print("usage: ./reader.lua [OPTION] ... path")
|
|
|
|
print("Read PDFs and DJVUs on your E-Ink reader")
|
2011-11-16 16:13:08 +00:00
|
|
|
print("")
|
|
|
|
print("-p, --password=PASSWORD set password for reading PDF document")
|
2011-11-16 18:12:35 +00:00
|
|
|
print("-g, --goto=page start reading on page")
|
2011-11-16 16:13:08 +00:00
|
|
|
print("-G, --gamma=GAMMA set gamma correction")
|
2012-04-19 03:05:48 +00:00
|
|
|
print("-d, --debug start in debug mode")
|
2011-11-16 16:13:08 +00:00
|
|
|
print(" (floating point notation, e.g. \"1.5\")")
|
|
|
|
print("-h, --help show this usage help")
|
|
|
|
print("")
|
2012-03-04 22:03:04 +00:00
|
|
|
print("If you give the name of a directory instead of a file path, a file")
|
|
|
|
print("chooser will show up and let you select a PDF|DJVU file")
|
|
|
|
print("")
|
|
|
|
print("If you don't pass any path, the last viewed document will be opened")
|
2011-12-08 00:17:33 +00:00
|
|
|
print("")
|
2011-11-16 16:13:08 +00:00
|
|
|
print("This software is licensed under the GPLv3.")
|
|
|
|
print("See http://github.com/hwhw/kindlepdfviewer for more info.")
|
|
|
|
return
|
2011-11-14 17:30:16 +00:00
|
|
|
end
|
|
|
|
|
2012-04-19 03:05:48 +00:00
|
|
|
optarg, optind = alt_getopt.get_opts(ARGV, "p:g:G:hg:dg:", longopts)
|
2012-03-04 22:03:04 +00:00
|
|
|
if optarg["h"] then
|
|
|
|
return showusage()
|
|
|
|
end
|
|
|
|
|
2012-04-19 03:05:48 +00:00
|
|
|
if not optarg["d"] then
|
|
|
|
debug = function() end
|
|
|
|
end
|
|
|
|
|
|
|
|
if optarg["G"] ~= nil then
|
|
|
|
globalgamma = optarg["G"]
|
|
|
|
end
|
|
|
|
|
2012-04-09 10:29:13 +00:00
|
|
|
if util.isEmulated()==1 then
|
2011-11-16 23:14:36 +00:00
|
|
|
input.open("")
|
|
|
|
-- SDL key codes
|
2012-03-20 19:15:24 +00:00
|
|
|
setEmuKeycodes()
|
2011-11-16 16:13:08 +00:00
|
|
|
else
|
2012-04-09 10:29:13 +00:00
|
|
|
input.open("slider")
|
2011-11-16 16:13:08 +00:00
|
|
|
input.open("/dev/input/event0")
|
|
|
|
input.open("/dev/input/event1")
|
2011-12-23 22:27:26 +00:00
|
|
|
|
|
|
|
-- check if we are running on Kindle 3 (additional volume input)
|
|
|
|
local f=lfs.attributes("/dev/input/event2")
|
|
|
|
if f then
|
|
|
|
print("Auto-detected Kindle 3")
|
2012-02-19 21:51:56 +00:00
|
|
|
input.open("/dev/input/event2")
|
2012-03-20 19:15:24 +00:00
|
|
|
setK3Keycodes()
|
2011-12-23 22:27:26 +00:00
|
|
|
end
|
2011-11-16 16:13:08 +00:00
|
|
|
end
|
|
|
|
|
2011-11-14 17:30:16 +00:00
|
|
|
fb = einkfb.open("/dev/fb0")
|
2012-04-09 07:42:19 +00:00
|
|
|
G_width, G_height = fb:getSize()
|
2012-03-09 01:40:46 +00:00
|
|
|
-- read current rotation mode
|
|
|
|
Screen:updateRotationMode()
|
2012-04-06 05:51:37 +00:00
|
|
|
Screen.native_rotation_mode = Screen.cur_rotation_mode
|
2011-11-14 17:30:16 +00:00
|
|
|
|
2012-02-23 16:30:02 +00:00
|
|
|
-- set up reader's setting: font
|
|
|
|
reader_settings = DocSettings:open(".reader")
|
2012-04-09 17:04:26 +00:00
|
|
|
fontmap = reader_settings:readSetting("fontmap")
|
|
|
|
if fontmap ~= nil then
|
|
|
|
Font.fontmap = fontmap
|
2012-02-23 16:30:02 +00:00
|
|
|
end
|
|
|
|
|
2012-03-07 10:16:46 +00:00
|
|
|
-- initialize global settings shared among all readers
|
|
|
|
UniReader:initGlobalSettings(reader_settings)
|
2012-03-04 11:34:19 +00:00
|
|
|
-- initialize specific readers
|
|
|
|
PDFReader:init()
|
|
|
|
DJVUReader:init()
|
2012-03-28 16:09:26 +00:00
|
|
|
CREReader:init()
|
2012-03-04 11:34:19 +00:00
|
|
|
|
2012-03-03 11:51:49 +00:00
|
|
|
-- display directory or open file
|
2012-03-20 19:15:24 +00:00
|
|
|
local patharg = reader_settings:readSetting("lastfile")
|
2012-03-05 22:36:58 +00:00
|
|
|
if ARGV[optind] and lfs.attributes(ARGV[optind], "mode") == "directory" then
|
2012-03-03 09:17:35 +00:00
|
|
|
local running = true
|
2011-12-08 18:01:40 +00:00
|
|
|
FileChooser:setPath(ARGV[optind])
|
2012-03-03 09:17:35 +00:00
|
|
|
while running do
|
2012-04-09 07:42:19 +00:00
|
|
|
local file, callback = FileChooser:choose(0, G_height)
|
2012-03-08 15:28:16 +00:00
|
|
|
if callback then
|
|
|
|
callback()
|
2012-03-03 09:17:35 +00:00
|
|
|
else
|
2012-03-08 15:28:16 +00:00
|
|
|
if file ~= nil then
|
|
|
|
running = openFile(file)
|
2012-03-19 13:22:45 +00:00
|
|
|
print(file)
|
2012-03-08 15:28:16 +00:00
|
|
|
else
|
|
|
|
running = false
|
|
|
|
end
|
2012-03-03 09:17:35 +00:00
|
|
|
end
|
|
|
|
end
|
2012-03-06 21:11:00 +00:00
|
|
|
elseif ARGV[optind] and lfs.attributes(ARGV[optind], "mode") == "file" then
|
|
|
|
openFile(ARGV[optind], optarg["p"])
|
2012-03-05 22:36:58 +00:00
|
|
|
elseif patharg and lfs.attributes(patharg, "mode") == "file" then
|
2012-03-04 22:03:04 +00:00
|
|
|
openFile(patharg, optarg["p"])
|
2012-03-03 09:17:35 +00:00
|
|
|
else
|
2012-03-04 22:03:04 +00:00
|
|
|
return showusage()
|
2012-03-03 09:17:35 +00:00
|
|
|
end
|
2012-02-23 16:30:02 +00:00
|
|
|
|
2012-02-23 04:44:49 +00:00
|
|
|
|
2012-03-03 11:51:49 +00:00
|
|
|
-- save reader settings
|
2012-04-18 09:29:27 +00:00
|
|
|
reader_settings:saveSetting("fontmap", Font.fontmap)
|
2012-03-03 11:51:49 +00:00
|
|
|
reader_settings:close()
|
2012-02-23 16:30:02 +00:00
|
|
|
|
2012-03-09 10:34:56 +00:00
|
|
|
-- @TODO dirty workaround, find a way to force native system poll
|
|
|
|
-- screen orientation and upside down mode 09.03 2012
|
2012-04-06 05:51:37 +00:00
|
|
|
fb:setOrientation(Screen.native_rotation_mode)
|
2012-03-09 10:34:56 +00:00
|
|
|
|
2012-02-23 04:44:49 +00:00
|
|
|
input.closeAll()
|
2012-04-09 14:32:19 +00:00
|
|
|
if util.isEmulated()==0 then
|
2012-04-18 11:43:55 +00:00
|
|
|
os.execute("killall -cont cvm")
|
2012-03-02 06:21:15 +00:00
|
|
|
os.execute('echo "send '..KEY_MENU..'" > /proc/keypad;echo "send '..KEY_MENU..'" > /proc/keypad')
|
|
|
|
end
|