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"
|
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"
|
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",
|
|
|
|
device = "d",
|
|
|
|
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
|
|
|
|
end
|
|
|
|
if reader then
|
|
|
|
local ok, err = reader:open(filename)
|
|
|
|
if ok then
|
|
|
|
reader:loadSettings(filename)
|
2012-03-20 19:15:24 +00:00
|
|
|
page_num = reader.settings:readSetting("last_page") or 1
|
2012-03-19 23:10:19 +00:00
|
|
|
reader:goto(tonumber(page_num))
|
2012-03-04 22:03:04 +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
|
|
|
|
-- TODO: error handling
|
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")
|
|
|
|
print(" (floating point notation, e.g. \"1.5\")")
|
|
|
|
print("-d, --device=DEVICE set device specific configuration,")
|
2011-11-16 22:34:23 +00:00
|
|
|
print(" currently one of \"kdxg\" (default), \"k3\"")
|
2012-01-19 17:17:16 +00:00
|
|
|
print(" \"emu\" (DXG emulation)")
|
2011-11-16 16:13:08 +00:00
|
|
|
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-03-04 22:03:04 +00:00
|
|
|
optarg, optind = alt_getopt.get_opts(ARGV, "p:G:hg:d:", longopts)
|
|
|
|
if optarg["h"] then
|
|
|
|
return showusage()
|
|
|
|
end
|
|
|
|
|
2012-03-01 13:04:46 +00:00
|
|
|
|
2011-11-16 16:13:08 +00:00
|
|
|
if optarg["d"] == "k3" then
|
|
|
|
-- for now, the only difference is the additional input device
|
|
|
|
input.open("/dev/input/event0")
|
|
|
|
input.open("/dev/input/event1")
|
|
|
|
input.open("/dev/input/event2")
|
2012-03-20 19:15:24 +00:00
|
|
|
setK3Keycodes()
|
2011-11-16 23:14:36 +00:00
|
|
|
elseif optarg["d"] == "emu" then
|
|
|
|
input.open("")
|
|
|
|
-- SDL key codes
|
2012-03-20 19:15:24 +00:00
|
|
|
setEmuKeycodes()
|
2011-11-16 16:13:08 +00:00
|
|
|
else
|
|
|
|
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")
|
|
|
|
print(f)
|
|
|
|
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-20 21:36:31 +00:00
|
|
|
if optarg["G"] ~= nil then
|
|
|
|
globalgamma = optarg["G"]
|
|
|
|
end
|
|
|
|
|
2011-11-14 17:30:16 +00:00
|
|
|
fb = einkfb.open("/dev/fb0")
|
|
|
|
width, height = fb:getSize()
|
2012-03-09 01:40:46 +00:00
|
|
|
-- read current rotation mode
|
|
|
|
Screen:updateRotationMode()
|
2012-03-09 10:34:56 +00:00
|
|
|
origin_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-03-20 19:15:24 +00:00
|
|
|
r_cfont = reader_settings:readSetting("cfont")
|
2012-02-23 16:30:02 +00:00
|
|
|
if r_cfont ~=nil then
|
2012-03-10 08:41:23 +00:00
|
|
|
Font.cfont = r_cfont
|
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-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-03-08 15:28:16 +00:00
|
|
|
local file, callback = FileChooser:choose(0,height)
|
|
|
|
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-03-10 08:41:23 +00:00
|
|
|
reader_settings:savesetting("cfont", Font.cfont)
|
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
|
|
|
|
fb:setOrientation(origin_rotation_mode)
|
|
|
|
|
2012-02-23 04:44:49 +00:00
|
|
|
input.closeAll()
|
2012-03-02 06:21:15 +00:00
|
|
|
--os.execute('test -e /proc/keypad && echo "send '..KEY_HOME..'" > /proc/keypad ')
|
|
|
|
if optarg["d"] ~= "emu" then
|
|
|
|
os.execute('echo "send '..KEY_MENU..'" > /proc/keypad;echo "send '..KEY_MENU..'" > /proc/keypad')
|
|
|
|
end
|