-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.lua
202 lines (180 loc) · 5.96 KB
/
reader.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!./kpdfview
--[[
Librerator: a fork of KindlePDFViewer by Kai771
See http://github.com/kai771/kindlepdfviewer, librerator branch.
KindlePDFViewer: a reader implementation
Copyright (C) 2011 Hans-Werner Hilse <[email protected]>
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/>.
]]--
require "pdfreader"
require "djvureader"
require "koptreader"
require "picviewer"
require "crereader"
require "filechooser"
require "settings"
require "screen"
require "commands"
require "dialog"
require "readerchooser"
require "device"
require "defaults"
G_ScreenKB_pressed = false
function openFile(filename)
local reader = ReaderChooser:getReaderByName(filename)
if reader then
InfoMessage:inform("Opening document... ", DINFO_NODELAY, 0, MSG_AUX)
reader:preLoadSettings(filename)
local ok, err = reader:open(filename)
if ok then
reader:loadSettings(filename)
page_num = reader:getLastPageOrPos()
reader:goto(tonumber(page_num), true)
G_reader_settings:saveSetting("lastfile", filename)
return reader:inputLoop()
else
if err then
Debug("openFile(): "..err)
InfoMessage:inform(err:sub(1,30), DINFO_DELAY, 1, MSG_ERROR)
else
InfoMessage:inform("Error opening document ", DINFO_DELAY, 1, MSG_ERROR)
end
end
end
return true -- on failed attempts, we signal to keep running
end
function showusage()
print("usage: ./reader.lua [OPTION] ... path")
print("Read PDF/DjVu/ePub/MOBI/FB2/CHM/HTML/TXT/DOC/RTF/JPEG on your E-Ink reader")
print("")
print("-d, --debug start in debug mode")
print(" (floating point notation, e.g. \"1.5\")")
print("-h, --help show this usage help")
print("")
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 file")
print("")
print("If you don't pass any path, the last viewed document will be opened")
print("")
print("This software is licensed under the GPLv3.")
print("See http://github.com/hwhw/kindlepdfviewer for more info.")
return
end
if ARGV[1] == "-h" then
return showusage()
end
local argidx = 1
if ARGV[1] == "-d" then
argidx = argidx + 1
else
Debug = function() end
dump = function() end
debug = function() end
end
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
if util.isEmulated()==1 then
input.open("")
-- SDL key codes
setEmuKeycodes()
else
input.open("slider")
input.open("/dev/input/event0")
input.open("/dev/input/event1")
-- check if we are running on Kindle 3 (additional volume input)
if FileExists("/dev/input/event2") then
Debug("Auto-detected Kindle 3")
input.open("/dev/input/event2")
setK3Keycodes()
end
-- check if we are running on Kindle 4 (as far as I can tell, K4NT has the same key codes as K3)
if Device:getModel() == "Kindle4" then
Debug("Auto-detected Kindle 4")
setK4Keycodes()
end
end
G_screen_saver_mode = false
G_charging_mode = false
fb = einkfb.open("/dev/fb0")
-- read current rotation mode
Screen.cur_rotation_mode = fb:getOrientation()
Screen.native_rotation_mode = Screen.cur_rotation_mode
-- force portrait mode
if DFORCE_PORTRAIT then
Screen:setRotationMode(0)
end
G_width, G_height = fb:getSize()
-- set up reader's setting: font
G_reader_settings = DocSettings:open(".reader")
fontmap = G_reader_settings:readSetting("fontmap")
if fontmap ~= nil then
-- 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
end
-- set up the mode to manage files
FileChooser.filemanager_mode = G_reader_settings:readSetting("filemanager_mode") or FileChooser.RESTRICTED
InfoMessage:initInfoMessageSettings()
local tmp = G_reader_settings:readSetting("G_battery_logging")
if tmp ~= nil then
G_battery_logging = tmp
else
G_battery_logging = DBATTERY_LOGGING
end
-- initialize global settings shared among all readers
UniReader:initGlobalSettings(G_reader_settings)
PDFReader:init()
DJVUReader:init()
KOPTReader:init()
PICViewer:init()
CREReader:init()
FileChooser:init()
-- display directory or open file
local patharg = G_reader_settings:readSetting("lastfile")
if ARGV[argidx] and lfs.attributes(ARGV[argidx], "mode") == "directory" then
FileChooser:setPath(ARGV[argidx])
FileChooser:choose(0, G_height)
elseif ARGV[argidx] and lfs.attributes(ARGV[argidx], "mode") == "file" then
openFile(ARGV[argidx])
elseif patharg and lfs.attributes(patharg, "mode") == "file" then
local handle = io.popen("dirname '"..patharg.."'")
local lfdir = handle:read("*a")
handle:close()
lfdir = string.gsub(lfdir, "\n", "")
FileChooser:setPath(lfdir)
FileChooser:choose(0, G_height, patharg)
else
return showusage()
end
-- save reader settings
G_reader_settings:saveSetting("fontmap", Font.fontmap)
InfoMessage:saveInfoMessageSettings()
G_reader_settings:close()
-- @TODO dirty workaround, find a way to force native system poll
-- screen orientation and upside down mode 09.03 2012
fb:setOrientation(Screen.native_rotation_mode)
input.closeAll()
if util.isEmulated()==0 then
os.execute("killall -cont cvm")
os.execute('echo "send '..KEY_MENU..'" > /proc/keypad;echo "send '..KEY_MENU..'" > /proc/keypad')
end