2012-09-25 15:56:42 +00:00
|
|
|
#!./kpdfview
|
2011-11-14 17:30:16 +00:00
|
|
|
--[[
|
|
|
|
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-10-18 13:38:46 +00:00
|
|
|
require "koptreader"
|
2012-10-08 15:39:03 +00:00
|
|
|
require "picviewer"
|
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 "commands"
|
2012-04-15 00:34:09 +00:00
|
|
|
require "dialog"
|
2012-05-19 10:59:38 +00:00
|
|
|
require "extentions"
|
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-05-19 10:59:38 +00:00
|
|
|
|
|
|
|
reader = ext:getReader(file_type)
|
2012-03-19 23:10:19 +00:00
|
|
|
if reader then
|
2012-10-02 22:45:45 +00:00
|
|
|
InfoMessage:inform("Opening document... ", nil, 0, MSG_AUX)
|
2012-04-19 06:39:50 +00:00
|
|
|
reader:preLoadSettings(filename)
|
2012-10-20 12:53:57 +00:00
|
|
|
-- re-establish the reader due to use_koptreader setting
|
|
|
|
reader = ext:getReader(file_type, reader)
|
|
|
|
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-06-02 21:51:55 +00:00
|
|
|
G_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-09-25 12:48:21 +00:00
|
|
|
if err then
|
|
|
|
Debug("openFile(): "..err)
|
2012-10-02 22:45:45 +00:00
|
|
|
InfoMessage:inform(err:sub(1,30), 2000, 1, MSG_ERROR)
|
2012-09-25 12:48:21 +00:00
|
|
|
else
|
2012-10-02 22:45:45 +00:00
|
|
|
InfoMessage:inform("Error opening document! ", 2000, 1, MSG_ERROR)
|
2012-09-25 12:48:21 +00:00
|
|
|
end
|
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
|
2012-09-25 12:11:21 +00:00
|
|
|
Debug = function() end
|
|
|
|
dump = function() end
|
2012-04-19 03:05:48 +00:00
|
|
|
debug = function() end
|
|
|
|
end
|
|
|
|
|
|
|
|
if optarg["G"] ~= nil then
|
|
|
|
globalgamma = optarg["G"]
|
|
|
|
end
|
|
|
|
|
2012-10-07 11:59:37 +00:00
|
|
|
local vfile = io.open("git-rev", "r")
|
|
|
|
if vfile then
|
|
|
|
G_program_version = vfile:read("*a") or "?"
|
|
|
|
G_program_version = G_program_version:gsub("[\n\r]+", "")
|
|
|
|
vfile.close()
|
|
|
|
else
|
|
|
|
G_program_version = "(unknown version)"
|
|
|
|
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)
|
2012-09-25 11:24:48 +00:00
|
|
|
if FileExists("/dev/input/event2") then
|
|
|
|
Debug("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
|
|
|
|
|
2012-06-07 15:39:28 +00:00
|
|
|
G_screen_saver_mode = false
|
|
|
|
G_charging_mode = false
|
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
|
2012-06-02 21:51:55 +00:00
|
|
|
G_reader_settings = DocSettings:open(".reader")
|
|
|
|
fontmap = G_reader_settings:readSetting("fontmap")
|
2012-04-09 17:04:26 +00:00
|
|
|
if fontmap ~= nil then
|
2012-09-07 14:26:05 +00:00
|
|
|
-- we need to iterate over all fonts used in reader to support upgrade from older configuration
|
|
|
|
for name,path in pairs(fontmap) do
|
|
|
|
if Font.fontmap[name] then
|
|
|
|
Font.fontmap[name] = path
|
|
|
|
else
|
|
|
|
Debug("missing "..name.." in user configuration, using default font "..path)
|
|
|
|
end
|
|
|
|
end
|
2012-02-23 16:30:02 +00:00
|
|
|
end
|
|
|
|
|
2012-09-06 18:38:54 +00:00
|
|
|
-- set up the mode to manage files
|
|
|
|
FileChooser.filemanager_expert_mode = G_reader_settings:readSetting("filemanager_expert_mode") or 1
|
2012-10-02 22:45:45 +00:00
|
|
|
InfoMessage:initInfoMessageSettings()
|
|
|
|
|
2012-03-07 10:16:46 +00:00
|
|
|
-- initialize global settings shared among all readers
|
2012-06-02 21:51:55 +00:00
|
|
|
UniReader:initGlobalSettings(G_reader_settings)
|
2012-03-04 11:34:19 +00:00
|
|
|
-- initialize specific readers
|
|
|
|
PDFReader:init()
|
|
|
|
DJVUReader:init()
|
2012-10-18 17:20:48 +00:00
|
|
|
KOPTReader:init()
|
2012-10-08 15:39:03 +00:00
|
|
|
PICViewer: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-06-02 21:51:55 +00:00
|
|
|
local patharg = G_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-09-25 15:54:37 +00:00
|
|
|
local file = FileChooser:choose(0, G_height)
|
|
|
|
if file then
|
|
|
|
running = openFile(file)
|
2012-03-03 09:17:35 +00:00
|
|
|
else
|
2012-09-25 15:54:37 +00:00
|
|
|
running = false
|
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-06-02 21:51:55 +00:00
|
|
|
G_reader_settings:saveSetting("fontmap", Font.fontmap)
|
2012-10-02 22:45:45 +00:00
|
|
|
InfoMessage:saveInfoMessageSettings()
|
2012-06-02 21:51:55 +00:00
|
|
|
G_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
|