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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
|
/*
* Dalos
* Copyright (C) 2004 Nicolas "Pixel" Noble
*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* $Id: Dalos.cc,v 1.5 2004-07-25 10:20:36 pixel Exp $ */
#include <SDL.h>
#include <SDL_thread.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <vector>
#include <Main.h>
#include <Handle.h>
#include <Input.h>
#include <Buffer.h>
#include <BLua.h>
#include <LuaHandle.h>
#include <engine.h>
#include <glbase.h>
#include <glshape.h>
#include <font.h>
#include <luacd.h>
#include <luapsx.h>
#include "cd-tool-hc.h"
#ifdef __MINGW32__
#define main SDL_main
#endif
bool auto_exec = true;
bool lua_started = false;
bool do_lua_break = false;
String prompt = "> ";
SDL_mutex * lua_command_lock;
SDL_sem * lua_command_sem;
Buffer lua_command;
SDL_mutex * key_vect_mutex;
SDL_sem * key_vect_size;
std::vector<Uint16> key_vect;
CODE_BEGINS
class console;
console * Console;
mogltk::widgets::Root * Root;
mogltk::widget * Frame;
mogltk::widgets::Menu * MainMenu;
class threaded_Lua : public Lua {
public:
threaded_Lua() : mutex(SDL_CreateMutex()) { }
virtual ~threaded_Lua() { SDL_DestroyMutex(mutex); }
virtual void lock() {
SDL_mutexP(mutex);
}
virtual void unlock() {
SDL_mutexV(mutex);
}
private:
SDL_mutex * mutex;
};
class threaded_locker : public locker_t {
public:
threaded_locker() : mutex(SDL_CreateMutex()) { }
virtual ~threaded_locker() { SDL_DestroyMutex(mutex); }
virtual void lock() {
SDL_mutexP(mutex);
}
virtual void unlock() {
SDL_mutexV(mutex);
}
private:
SDL_mutex * mutex;
};
static int lua_print(lua_State * _L) {
Lua * L = Lua::find(_L);
String t = L->tostring() + "\n";
char * tc = t.strdup();
printm(M_STATUS, "%s", tc);
free(tc);
return 0;
}
static void lua_hook(lua_State * _L, lua_Debug * ar) {
if (!lua_started)
return;
Lua * L = Lua::find(_L);
if (do_lua_break) {
L->do_break();
do_lua_break = false;
}
}
static int rl_getc_func(FILE * d) {
int k;
SDL_SemWait(key_vect_size);
SDL_mutexP(key_vect_mutex);
k = key_vect[0];
key_vect.erase(key_vect.begin());
SDL_mutexV(key_vect_mutex);
return k;
}
static void rl_redisplay_func(void) {
Application->Console->page_reset();
}
static void rl_prep_term_func(int b) {
}
static void rl_deprep_term_func(void) {
}
static int readline_thread(void * d) {
char * line_read;
String line, endline;
bool runit;
int pos;
rl_getc_function = rl_getc_func;
rl_redisplay_function = rl_redisplay_func;
rl_prep_term_function = rl_prep_term_func;
rl_deprep_term_function = rl_deprep_term_func;
rl_bind_key('\t', rl_insert);
while (true) {
line_read = readline("");
if (!line_read)
continue;
if (*line_read)
add_history(line_read);
Application->Console->add_line(prompt + line_read);
line = line_read;
line = line.trim();
endline = "";
/* Splitting the line between ;; */
while (line.strlen()) {
runit = false;
if ((pos = line.strstr(";;")) >= 0) {
endline = line.extract(pos + 2);
line = line.extract(0, pos);
runit = true;
} else {
endline = "";
}
if (line[line.strlen() - 1] == '\\') {
line[line.strlen() - 1] = ' ';
} else if (auto_exec) {
runit = true;
}
SDL_mutexP(lua_command_lock);
lua_command << line;
SDL_mutexV(lua_command_lock);
if (runit) {
SDL_SemPost(lua_command_sem);
prompt = "> ";
} else {
prompt = "- ";
}
line = endline.trim();
}
free(line_read);
}
return 0;
}
void start_lua() {
Lua * L = new threaded_Lua();
Buffer built;
bool use_builtin_cdtool = false;
int i;
L->open_base();
L->open_math();
L->open_string();
L->open_table();
LuaInput::pushconstruct(L);
LuaOutput::pushconstruct(L);
LuaBuffer::pushconstruct(L);
CD_PUSHSTATICS(L);
Luapsx::pushstatics(L);
L->push("print");
L->push(lua_print);
L->setvar();
L->sethook(lua_hook, LUA_MASKLINE, 0);
lua_command_lock = SDL_CreateMutex();
lua_command_sem = SDL_CreateSemaphore(0);
try {
L->load(&Input("cd-tool.lua"));
} catch (GeneralException e) {
printm(M_WARNING, "There was an error loading cd-tool.lua: %s, using built-in.\n", e.GetMsg());
use_builtin_cdtool = true;
}
if (use_builtin_cdtool) {
for (i = 0; i < cd_tool_lua_size; i++) {
built.writeU8(cd_tool_lua[i]);
}
try {
L->load(&built);
}
catch (GeneralException e) {
printm(M_WARNING, "There was an error loading built-in cd-tool.lua: %s\n", e.GetMsg());
}
}
SDL_CreateThread(LuaThread, L);
}
static int LuaThread(void * d) {
Lua * L = (Lua *) d;
while (true) {
SDL_SemWait(lua_command_sem);
lua_started = true;
SDL_mutexP(lua_command_lock);
try {
L->load(&lua_command);
}
catch (LuaException e) {
/* If there was an error, ignore it, and free the stack */
while(L->gettop())
L->pop();
}
catch (GeneralException e) {
/* A more severe exception... */
while(L->gettop())
L->pop();
printm(M_ERROR, "Aborted. LUA caused the following exception: %s\n", e.GetMsg());
}
SDL_mutexV(lua_command_lock);
lua_started = false;
}
}
class console_keyevent : public mogltk::engine::keyevent {
public:
virtual void down(SDL_keysym k) {
if (Application->Console->GetVisible()) {
SDL_mutexP(key_vect_mutex);
switch (k.sym) {
case SDLK_ESCAPE:
Application->Console->SetVisible(false);
SDL_mutexV(key_vect_mutex);
return;
case SDLK_DELETE:
key_vect.push_back('D' - '@');
break;
case SDLK_LEFT:
key_vect.push_back('B' - '@');
break;
case SDLK_RIGHT:
key_vect.push_back('F' - '@');
break;
case SDLK_UP:
key_vect.push_back('P' - '@');
break;
case SDLK_DOWN:
key_vect.push_back('N' - '@');
break;
case SDLK_HOME:
key_vect.push_back('A' - '@');
break;
case SDLK_END:
key_vect.push_back('E' - '@');
break;
case SDLK_PAGEUP:
Application->Console->page_up();
SDL_mutexV(key_vect_mutex);
return;
case SDLK_PAGEDOWN:
Application->Console->page_down();
SDL_mutexV(key_vect_mutex);
return;
case SDLK_c:
if (k.mod & KMOD_CTRL) {
if (lua_started)
do_lua_break = true;
SDL_mutexV(key_vect_mutex);
return;
}
default:
if (k.unicode) {
switch (k.unicode) {
case 0xb4:
key_vect.push_back('\'');
break;
case 0xa8:
key_vect.push_back('"');
break;
default:
key_vect.push_back(k.unicode);
}
} else {
SDL_mutexV(key_vect_mutex);
if (old_handler)
old_handler->down(k);
return;
}
}
// hack...
if (lua_started)
key_vect.pop_back();
SDL_mutexV(key_vect_mutex);
if (!lua_started)
SDL_SemPost(key_vect_size);
} else {
if (k.sym == SDLK_ESCAPE) {
Application->Console->SetVisible(true);
return;
}
if (old_handler)
old_handler->down(k);
}
}
virtual void up(SDL_keysym k) {
if (old_handler)
old_handler->up(k);
}
};
class myprinter : public printer_t {
public:
myprinter() : lock(SDL_CreateMutex()) { }
virtual ~myprinter() { SDL_DestroyMutex(lock); }
virtual bool printm(int level, const char * m, va_list ap) {
static String heads[] = {"EE", "--", "WW", "II"};
static char buffer[20480];
if (level >= M_INFO)
return true;
SDL_mutexP(lock);
vsprintf(buffer, m, ap);
if (level >= 0)
Application->Console->add_line("(" + heads[level] + ") " + buffer);
else
Application->Console->add_line(buffer);
SDL_mutexV(lock);
return true;
}
private:
SDL_mutex * lock;
};
class console : public mogltk::widget {
public:
console(mogltk::shape * sh, mogltk::widget * father, int y, int _nlines) :
widget(father, 0, y, father->GetW(), _nlines * 13, 0, "console", sh), nlines(_nlines), page(0), protect_add_line(SDL_CreateMutex()) { SetVisible(false); }
virtual ~console() { SDL_DestroyMutex(protect_add_line); }
void add_line(const String & s) {
SDL_mutexP(protect_add_line);
lines.insert(lines.begin(), 1, s);
while (lines.size() >= 1024) {
lines.pop_back();
}
SDL_mutexV(protect_add_line);
}
void page_reset() {
page = 0;
}
void page_up() {
page += nlines - 1;
if (page > (lines.size() - nlines))
page = lines.size() - nlines;
}
void page_down() {
page -= nlines - 1;
if (page < 0)
page = 0;
}
protected:
virtual void draw() {
int cursor_pos, start, line_length, line_pos, cur_page;
mogltk::ColorP::Max.A = 180;
std::vector<String>::iterator i;
// Background
Shaper()->box(GetAX(), GetAY(), GetAX2(), GetAY2(), DODGERBLUE);
Shaper()->obox(GetAX(), GetAY(), GetAX2(), GetAY2(), BLUE);
mogltk::ColorP::Max.A = 255;
mogltk::FixedFont->setcolor(WHITE);
// Lines
SDL_mutexP(protect_add_line);
i = lines.begin();
for (cur_page = page; cur_page && (i != lines.end()); cur_page--, i++);
for (line_pos = nlines - 2; (line_pos >= 0) && (i != lines.end()); line_pos--, i++) {
const char * line;
mogltk::FixedFont->putcursor(GetAX(), GetAY() + line_pos * 13);
line = i->to_charp();
mogltk::FixedFont->printf("%s", line);
}
SDL_mutexV(protect_add_line);
// Cursor
cursor_pos = rl_point;
start = 0;
line_length = strlen(rl_line_buffer);
// while ((((cursor_pos + 2) * 6) >= GetW() || (((line_length - start + 2) * 6) >= GetW())) && (cursor_pos >= 16)) {
while (((cursor_pos + 2) * 6) >= GetW()) {
cursor_pos -= 16;
start += 16;
}
mogltk::ColorP::Max.A = 220;
if (lua_started) {
Shaper()->box(GetAX(), GetAY() + (nlines - 1) * 13, GetAX() + 6, GetAY() + nlines * 13, FORESTGREEN);
} else {
Shaper()->box(GetAX() + 6 * (cursor_pos + 2), GetAY() + (nlines - 1) * 13, GetAX() + 6 * (cursor_pos + 3) - 1, GetAY() + nlines * 13, FORESTGREEN);
mogltk::ColorP::Max.A = 255;
mogltk::FixedFont->putcursor(GetAX(), GetAY() + (nlines - 1) * 13);
mogltk::FixedFont->printf(prompt);
mogltk::FixedFont->printf("%s", rl_line_buffer + start);
}
}
virtual bool process_event(int mx, int my, mogltk::event_t event) {
return false;
}
private:
int nlines, page;
std::vector<String> lines;
SDL_mutex * protect_add_line;
};
class hexview : public mogltk::widget {
public:
hexview(mogltk::shape * sh, mogltk::widget * father) :
widget(father, 0, 0, father->GetW(), father->GetH(), 0, "hexview", sh), h(0), width(16), offset(0), offset_loaded(-1), size_loaded(0), data(0)
{ nlines = GetH() / 13; }
virtual ~hexview() { free(data); }
void bind_handle(Handle * _h) {
h = _h;
}
Uint8 * get_data() {
if (!data) {
data = (Uint8 *) malloc(nlines * width);
}
if ((offset_loaded == offset) && (size_loaded == (nlines * width)))
return data;
h->seek(offset);
h->read(data, nlines * width);
offset_loaded = offset;
size_loaded = nlines * width;
return data;
}
virtual void draw() {
int i, max_o, j, k, start_o, l, c;
if (!h)
return;
get_data();
mogltk::FixedFont->setcolor(WHITE);
mogltk::FixedFont->putcursor(GetAX(), GetAY());
max_o = min(h->GetSize(), offset + nlines * width);
for (i = start_o = offset, j = 0, l = 0; i < max_o; i++, j) {
if ((j % width) == 0) {
mogltk::FixedFont->printf("%08X ", i);
}
mogltk::FixedFont->printf("%02X ", data[i]);
j++;
if ((j % width) == 0) {
for (k = start_o, c = 0; k < start_o + width; k++, c++) {
mogltk::FixedFont->putcursor(GetAX() + (c + width * 3 + 14) * 6, GetAY() + l * 13);
if (data[k] >= 0x20)
mogltk::FixedFont->putentry(data[k] - 0x20);
}
l++;
mogltk::FixedFont->putcursor(GetAX(), GetAY() + l * 13);
start_o = i;
}
}
}
private:
Handle * h;
int width, offset, nlines;
int offset_loaded, size_loaded;
Uint8 * data;
};
class frame : public mogltk::widget {
public:
frame(mogltk::shape * sh, mogltk::widget * father) :
widget(father, 2, 2, father->GetW() - 4, father->GetH() - 4, 0, "MyFrame", sh) { }
protected:
virtual void draw() {
}
virtual bool process_event(int mx, int my, mogltk::event_t event) {
mx -= GetAX();
my -= GetAY();
return false;
}
};
class timer : public mogltk::widget {
public:
timer() :
widget(Application->Root, 0, 0, 0, 0, 0, "Timer", 0), tick(0)
{ set_timed_event(100); }
protected:
virtual bool process_event(int, int, mogltk::event_t event) {
if (event == mogltk::E_TIMER) {
set_timed_event(100);
tick = (tick + 1) % 4;
switch (tick) {
case 0:
Application->MainMenu->SetCaption(0, "/");
break;
case 1:
Application->MainMenu->SetCaption(0, "-");
break;
case 2:
Application->MainMenu->SetCaption(0, "\\");
break;
case 3:
Application->MainMenu->SetCaption(0, "I");
break;
}
Application->MainMenu->SetCaption(3, String("FPS: ") + mogltk::engine::FPS());
return true;
}
return false;
}
private:
int tick;
};
class quit : public mogltk::widgets::action {
public:
virtual void do_action(mogltk::widget * w) {
mogltk::engine::quit();
}
} action_quit;
class about : public mogltk::widgets::action {
public:
virtual void do_action(mogltk::widget * w) {
new mogltk::widgets::MsgBox(w->Shaper(), w->Father(), "About...",
"Dalos version 0.1 - OpenGL version\n"
"Copyright (C) 2004 Nicolas \"Pixel\" Noble\n"
"\n"
"Thanks and greetings fly to (no particular order)\n"
"GreatSkaori, Orphis, Ti Dragon, Yaz0r, S-O-R,\n"
"Meradrin, SkeuD, Moogle, InVerse, LavosSpawn\n"
"\n"
"And to all I forgot!\n"
);
}
} about_dialog;
virtual int startup() throw (GeneralException) {
verbosity = M_INFO;
try {
new Archive(argv[0], ARCHIVE_EXECUTABLE);
}
catch (...) {
new Archive("Dalos.paq");
}
mogltk::widgets::ContextMenu * c;
mogltk::base * gl = new mogltk::glbase();
mogltk::shape * sh = new mogltk::glshape();
key_vect_mutex = SDL_CreateMutex();
key_vect_size = SDL_CreateSemaphore(0);
SDL_EnableKeyRepeat(250, 40);
mogltk::engine::setcursorvisible(true);
mogltk::engine::setappactive(true);
Root = new mogltk::widgets::Root(sh);
MainMenu = new mogltk::widgets::Menu(sh, Root);
Frame = new frame(sh, new mogltk::widgets::Frame(sh, Root, 0, MainMenu->GetH(), Root->GetW() - 1, Root->GetH() - MainMenu->GetH() - 1));
(new hexview(sh, Frame))->bind_handle(new Input(argv[0]));
Console = new console(sh, Frame, 0, 8);
Console->move(0, Frame->GetH() - Console->GetH());
Console->add_line("Dalos v0.1 - LUA console");
printer = new myprinter();
locker = new threaded_locker();
start_lua();
SDL_CreateThread(readline_thread, 0);
MainMenu->addnode("/", 0);
c = MainMenu->createsub("File");
c->addnode("Quit", &action_quit);
c = MainMenu->createsub("Help");
c->addnode("About", &about_dialog);
MainMenu->addnode("FPS:", 0);
new timer();
new console_keyevent;
Root->mainloop();
// Should cleanup here...
return 0;
}
CODE_ENDS
|