-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbookr.cpp
413 lines (372 loc) · 10.3 KB
/
bookr.cpp
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
* Bookr: document reader for the Sony PSP
* Copyright (C) 2005 Carlos Carrasco Martinez (carloscm at gmail dot com),
* 2007 Christian Payeur (christian dot payeur at gmail dot com)
* 2009 Nguyen Chi Tam (nguyenchitam at gmail dot com)
*
* 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 2 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, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "fzscreen.h"
#include "bkdocument.h"
#include "bkfilechooser.h"
#include "bkcachechooser.h"
#include "bkcolorchooser.h"
#include "bkpagechooser.h"
#include "bkcolorschememanager.h"
#include "bkmainmenu.h"
#include "bklogo.h"
#include "bkpopup.h"
#include "bklogging.h"
#ifdef PSP
#include <pspkernel.h>
PSP_MODULE_INFO("Bookr", 0, 1, 1);
PSP_HEAP_SIZE_KB(-7*1024);
PSP_MAIN_THREAD_ATTR(PSP_THREAD_ATTR_USER);
#endif
extern int processCHMHTM(bkLayers& layers, string filepath, bool convertToVN);
extern "C" {
int main(int argc, char* argv[]);
};
static bool isTTF(string& file) {
char header[4];
memset((void*)header, 0, 4);
FILE* f = fopen(file.c_str(), "r");
fread(header, 4, 1, f);
fclose(f);
return header[0] == 0x0 && header[1] == 0x1 && header[2] == 0x0 && header[3] == 0x0;
}
bool isHTMOrCHM(string& file) {
string tmp = file.substr(file.find_last_of("."));
return stricmp(tmp.c_str(), ".htm") == 0 || stricmp(tmp.c_str(), ".html")
== 0 || stricmp(tmp.c_str(), ".chm") == 0;
}
int main(int argc, char* argv[]) {
initLogging();
BKDocument* documentLayer = 0;
FZScreen::setupCallbacks();
FZScreen::open(argc, argv);
FZScreen::setupCtrl();
BKUser::init();
FZScreen::setSpeed(BKUser::options.pspMenuSpeed);
FZ_DEBUG_SCREEN_INIT
BKLayer::load();
bkLayers layers;
BKFileChooser* fs = 0;
BKCacheChooser* ccs = 0;
BKColorChooser* cs = 0;
BKColorSchemeManager* csm = 0;
BKPageChooser* ps = 0;
BKMainMenu* mm = BKMainMenu::create();
layers.push_back(BKLogo::create());
layers.push_back(mm);
FZScreen::dcacheWritebackAll();
if( BKUser::options.loadLastFile )
{
string s = BKBookmarksManager::getLastFile();
if( s.substr(0,5) == "ms0:/" )
{
// clear layers
bkLayersIt it(layers.begin());
bkLayersIt end(layers.end());
while (it != end) {
(*it)->release();
++it;
}
layers.clear();
// little hack to display a loading screen
BKLogo* l = BKLogo::create();
l->setLoading(true);
FZScreen::startDirectList();
l->render();
FZScreen::endAndDisplayList();
FZScreen::waitVblankStart();
FZScreen::swapBuffers();
FZScreen::checkEvents();
l->release();
// detect file type and add a new display layer
documentLayer = BKDocument::create(s);
if (documentLayer == 0) {
// error, back to logo screen
BKLogo* l = BKLogo::create();
l->setError(true);
layers.push_back(l);
FZScreen::swapBuffers();
} else {
// file loads ok, add the layer
layers.push_back(documentLayer);
}
}
}
FZScreen::setSpeed(BKUser::options.pspSpeed);
bool dirty = true;
// bool vdirty = true;
bool exitApp = false;
int reloadTimer = 0;
while (!exitApp) {
// if(vdirty) {
// FZScreen::startDirectList();
// // render layers back to front
// bkLayersIt it(layers.begin());
// bkLayersIt end(layers.end());
// while (it != end) {
// (*it)->update(0);
// (*it)->render();
// ++it;
// }
// FZScreen::endAndDisplayList();
// } else
if (dirty) {
FZScreen::startDirectList();
// render layers back to front
bkLayersIt it(layers.begin());
bkLayersIt end(layers.end());
while (it != end) {
(*it)->render();
++it;
}
FZScreen::endAndDisplayList();
}
FZScreen::waitVblankStart();
if (dirty) {
FZScreen::swapBuffers();
}
FZScreen::checkEvents();
FZ_DEBUG_SCREEN_SET00
int buttons = FZScreen::readCtrl();
dirty = buttons != 0;
// the last layer always owns the input focus
bkLayersIt it(layers.end());
--it;
int command = 0;
// the PSP hangs when doing file I/O just after a power resume, delay it a few vsyncs
if (reloadTimer <= 0)
command = (*it)->update(buttons);
if (command == BK_CMD_RELOAD) {
reloadTimer = 60;
}
if (reloadTimer > 0) {
reloadTimer--;
if (reloadTimer == 0)
command = BK_CMD_RELOAD;
}
// dont proc events while in the reload timer
if (reloadTimer > 0)
continue;
switch (command) {
case BK_CMD_CLOSE_TOP_LAYER_RELOAD:
case BK_CMD_CLOSE_TOP_LAYER: {
bkLayersIt it(layers.end());
--it;
(*it)->release();
layers.erase(it);
if (command == BK_CMD_CLOSE_TOP_LAYER_RELOAD) {
// repaint
dirty = true;
}
} break;
case BK_CMD_MARK_DIRTY:
dirty = true;
// mm->rebuildMenu();
break;
case BK_CMD_INVOKE_OPEN_FILE: {
// add a file chooser layer
string title("Open (use SQUARE to open Vietnamese chm/html)");
fs = BKFileChooser::create(title, BK_CMD_OPEN_FILE);
layers.push_back(fs);
} break;
case BK_CMD_INVOKE_OPEN_FONT: {
// add a file chooser layer
string title("Select font file to open");
fs = BKFileChooser::create(title, BK_CMD_SET_FONT);
layers.push_back(fs);
} break;
case BK_CMD_INVOKE_PAGE_CHOOSER: {
if (documentLayer && documentLayer->isPaginated()) {
ps = BKPageChooser::create(documentLayer->getTotalPages(),
documentLayer->getCurrentPage(),
BK_CMD_OPEN_PAGE);
layers.push_back(ps);
}
} break;
case BK_CMD_OPEN_PAGE: {
if (documentLayer && documentLayer->isPaginated()) {
int pagenum = ps->getCurrentPage();
int r = documentLayer->setCurrentPage(pagenum);
if (r != 0)
command = r;
}
bkLayersIt it(layers.end());
--it;
(*it)->release();
layers.erase(it);
} break;
case BK_CMD_INVOKE_BROWSE_CACHE: {
// add a file chooser layer
string title("Cache");
ccs = BKCacheChooser::create(title, BK_CMD_OPEN_CACHE);
layers.push_back(ccs);
} break;
case BK_CMD_OPEN_FILE:
case BK_CMD_OPEN_CACHE:
case BK_CMD_RELOAD: {
// open a file as a document
string s;
FZScreen::setSpeed(BKUser::options.pspMenuSpeed);
bool convertToVN = false;
if (command == BK_CMD_RELOAD) {
documentLayer->getFileName(s);
documentLayer = 0;
}
if (command == BK_CMD_OPEN_FILE) {
// open selected file
fs->getFullPath(s);
convertToVN = fs->isConvertToVN();
fs = 0;
}
if (command == BK_CMD_OPEN_CACHE) {
// open selected cache
ccs->getFullPath(s);
ccs = 0;
}
// clear layers
bkLayersIt it(layers.begin());
bkLayersIt end(layers.end());
while (it != end) {
(*it)->release();
++it;
}
layers.clear();
// little hack to display a loading screen
BKLogo* l = BKLogo::create();
l->setLoading(true);
FZScreen::startDirectList();
l->render();
FZScreen::endAndDisplayList();
FZScreen::waitVblankStart();
FZScreen::swapBuffers();
FZScreen::checkEvents();
l->release();
if (isHTMOrCHM(s)) {
processCHMHTM(layers, s, convertToVN);
// add a main menu layer
layers.push_back(BKLogo::create());
mm = BKMainMenu::create();
layers.push_back(mm);
dirty = true;
break;
}
// detect file type and add a new display layer
documentLayer = BKDocument::create(s);
if (documentLayer == 0) {
// error, back to logo screen
BKLogo* l = BKLogo::create();
l->setError(true);
layers.push_back(l);
} else {
// file loads ok, add the layer
layers.push_back(documentLayer);
}
FZScreen::setSpeed(BKUser::options.pspSpeed);
dirty = true;
} break;
case BK_CMD_SET_FONT: {
// change font for plain text rendering
string s;
FZDirent de;
// open selected file
fs->getCurrentDirent(de);
fs->getFullPath(s);
// detect file type and display error if needed
if (!isTTF(s)) {
layers.push_back(BKPopup::create(
BKPOPUP_ERROR,
"The selected file is not a valid TrueType font."
)
);
break;
}
// file ok
bkLayersIt it(layers.end());
--it;
(*it)->release();
layers.erase(it);
fs = 0;
BKUser::options.txtFont = s;
BKUser::save();
mm->rebuildMenu();
} break;
case BK_CMD_INVOKE_MENU:
// add a main menu layer
mm = BKMainMenu::create();
layers.push_back(mm);
break;
case BK_CMD_MAINMENU_POPUP:
layers.push_back(BKPopup::create(
mm->getPopupMode(),
mm->getPopupText()
)
);
break;
case BK_CMD_INVOKE_COLOR_SCHEME_MANAGER: {//TXTFG:
// add a color scheme manager layer
string title("Manage color schemes");
csm = BKColorSchemeManager::create(title);
layers.push_back(csm);
} break;
case BK_CMD_INVOKE_COLOR_CHOOSER_TXTFG:
// add a color chooser layer
cs = BKColorChooser::create(csm->getColorScheme(), BK_CMD_SET_TXTFG);
layers.push_back(cs);
break;
case BK_CMD_SET_TXTFG: {
BKUser::options.colorSchemes[csm->getColorScheme()].txtFGColor = cs->getColor();
BKUser::save();
mm->rebuildMenu();
bkLayersIt it(layers.end());
--it;
(*it)->release();
layers.erase(it);
// After choosing the foreground color, choose the background one
cs = BKColorChooser::create(csm->getColorScheme(), BK_CMD_SET_TXTBG);
layers.push_back(cs);
} break;
case BK_CMD_SET_TXTBG: {
BKUser::options.colorSchemes[csm->getColorScheme()].txtBGColor = cs->getColor();
BKUser::save();
mm->rebuildMenu();
bkLayersIt it(layers.end());
--it;
(*it)->release();
layers.erase(it);
} break;
case BK_CMD_EXIT: {
FZScreen::setSpeed(BKUser::options.pspMenuSpeed);
exitApp = true;
}
break;
}
}
bkLayersIt it(layers.begin());
bkLayersIt end(layers.end());
while (it != end) {
(*it)->release();
++it;
}
layers.clear();
FZScreen::close();
FZScreen::exit();
finalizeLogging();
return 0;
}